feat(id): brand WorkspaceID through Drizzle and Zod schemas (#16964)
This commit is contained in:
parent
f1c3a44190
commit
16a6d6feba
28 changed files with 205 additions and 157 deletions
|
|
@ -5,6 +5,7 @@ import { Provider } from "../../../provider/provider"
|
|||
import { Session } from "../../../session"
|
||||
import type { MessageV2 } from "../../../session/message-v2"
|
||||
import { Identifier } from "../../../id/id"
|
||||
import { MessageID } from "../../../session/schema"
|
||||
import { ToolRegistry } from "../../../tool/registry"
|
||||
import { Instance } from "../../../project/instance"
|
||||
import { PermissionNext } from "../../../permission/next"
|
||||
|
|
@ -113,7 +114,7 @@ function parseToolParams(input?: string) {
|
|||
|
||||
async function createToolContext(agent: Agent.Info) {
|
||||
const session = await Session.create({ title: `Debug tool run (${agent.name})` })
|
||||
const messageID = Identifier.ascending("message")
|
||||
const messageID = MessageID.ascending()
|
||||
const model = agent.model ?? (await Provider.defaultModel())
|
||||
const now = Date.now()
|
||||
const message: MessageV2.Assistant = {
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ import { bootstrap } from "../bootstrap"
|
|||
import { Session } from "../../session"
|
||||
import type { SessionID } from "../../session/schema"
|
||||
import { Identifier } from "../../id/id"
|
||||
import { MessageID } from "../../session/schema"
|
||||
import { Provider } from "../../provider/provider"
|
||||
import { Bus } from "../../bus"
|
||||
import { MessageV2 } from "../../session/message-v2"
|
||||
|
|
@ -935,7 +936,7 @@ export const GithubRunCommand = cmd({
|
|||
|
||||
const result = await SessionPrompt.prompt({
|
||||
sessionID: session.id,
|
||||
messageID: Identifier.ascending("message"),
|
||||
messageID: MessageID.ascending(),
|
||||
variant,
|
||||
model: {
|
||||
providerID,
|
||||
|
|
@ -989,7 +990,7 @@ export const GithubRunCommand = cmd({
|
|||
console.log("Requesting summary from agent...")
|
||||
const summary = await SessionPrompt.prompt({
|
||||
sessionID: session.id,
|
||||
messageID: Identifier.ascending("message"),
|
||||
messageID: MessageID.ascending(),
|
||||
variant,
|
||||
model: {
|
||||
providerID,
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
import type { Argv } from "yargs"
|
||||
import type { Session as SDKSession, Message, Part } from "@opencode-ai/sdk/v2"
|
||||
import { Session } from "../../session"
|
||||
import { SessionID } from "../../session/schema"
|
||||
import { SessionID, MessageID } from "../../session/schema"
|
||||
import { WorkspaceID } from "../../control-plane/schema"
|
||||
import { cmd } from "./cmd"
|
||||
import { bootstrap } from "../bootstrap"
|
||||
import { Database } from "../../storage/db"
|
||||
|
|
@ -157,7 +158,11 @@ export const ImportCommand = cmd({
|
|||
...exportData.info,
|
||||
id: SessionID.make(exportData.info.id),
|
||||
parentID: exportData.info.parentID ? SessionID.make(exportData.info.parentID) : undefined,
|
||||
workspaceID: exportData.info.workspaceID ? WorkspaceID.make(exportData.info.workspaceID) : undefined,
|
||||
projectID: Instance.project.id,
|
||||
revert: exportData.info.revert
|
||||
? { ...exportData.info.revert, messageID: MessageID.make(exportData.info.revert.messageID) }
|
||||
: undefined,
|
||||
})
|
||||
Database.use((db) =>
|
||||
db
|
||||
|
|
@ -168,28 +173,30 @@ export const ImportCommand = cmd({
|
|||
)
|
||||
|
||||
for (const msg of exportData.messages) {
|
||||
const { id: _mid, sessionID: _msid, ...msgData } = msg.info
|
||||
Database.use((db) =>
|
||||
db
|
||||
.insert(MessageTable)
|
||||
.values({
|
||||
id: msg.info.id,
|
||||
id: MessageID.make(msg.info.id),
|
||||
session_id: row.id,
|
||||
time_created: msg.info.time?.created ?? Date.now(),
|
||||
data: msg.info,
|
||||
data: msgData,
|
||||
})
|
||||
.onConflictDoNothing()
|
||||
.run(),
|
||||
)
|
||||
|
||||
for (const part of msg.parts) {
|
||||
const { id: _pid, sessionID: _psid, messageID: _pmid, ...partData } = part
|
||||
Database.use((db) =>
|
||||
db
|
||||
.insert(PartTable)
|
||||
.values({
|
||||
id: part.id,
|
||||
message_id: msg.info.id,
|
||||
message_id: MessageID.make(msg.info.id),
|
||||
session_id: row.id,
|
||||
data: part,
|
||||
data: partData,
|
||||
})
|
||||
.onConflictDoNothing()
|
||||
.run(),
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import { useSDK } from "@tui/context/sdk"
|
|||
import { useRoute } from "@tui/context/route"
|
||||
import { useSync } from "@tui/context/sync"
|
||||
import { Identifier } from "@/id/id"
|
||||
import { MessageID } from "@/session/schema"
|
||||
import { createStore, produce } from "solid-js/store"
|
||||
import { useKeybind } from "@tui/context/keybind"
|
||||
import { usePromptHistory, type PromptInfo } from "./history"
|
||||
|
|
@ -561,7 +562,7 @@ export function Prompt(props: PromptProps) {
|
|||
sessionID = res.data.id
|
||||
}
|
||||
|
||||
const messageID = Identifier.ascending("message")
|
||||
const messageID = MessageID.ascending()
|
||||
let inputText = store.prompt.input
|
||||
|
||||
// Expand pasted text inline before submitting
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { BusEvent } from "@/bus/bus-event"
|
||||
import { SessionID } from "@/session/schema"
|
||||
import { SessionID, MessageID } from "@/session/schema"
|
||||
import z from "zod"
|
||||
import { Config } from "../config/config"
|
||||
import { Instance } from "../project/instance"
|
||||
|
|
@ -17,7 +17,7 @@ export namespace Command {
|
|||
name: z.string(),
|
||||
sessionID: SessionID.zod,
|
||||
arguments: z.string(),
|
||||
messageID: Identifier.schema("message"),
|
||||
messageID: MessageID.zod,
|
||||
}),
|
||||
),
|
||||
}
|
||||
|
|
|
|||
17
packages/opencode/src/control-plane/schema.ts
Normal file
17
packages/opencode/src/control-plane/schema.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import { Schema } from "effect"
|
||||
import z from "zod"
|
||||
|
||||
import { withStatics } from "@/util/schema"
|
||||
import { Identifier } from "@/id/id"
|
||||
|
||||
const workspaceIdSchema = Schema.String.pipe(Schema.brand("WorkspaceId"))
|
||||
|
||||
export type WorkspaceID = typeof workspaceIdSchema.Type
|
||||
|
||||
export const WorkspaceID = workspaceIdSchema.pipe(
|
||||
withStatics((schema: typeof workspaceIdSchema) => ({
|
||||
make: (id: string) => schema.makeUnsafe(id),
|
||||
ascending: (id?: string) => schema.makeUnsafe(Identifier.ascending("workspace", id)),
|
||||
zod: z.string().startsWith("wrk").pipe(z.custom<WorkspaceID>()),
|
||||
})),
|
||||
)
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
import z from "zod"
|
||||
import { Identifier } from "@/id/id"
|
||||
import { ProjectID } from "@/project/schema"
|
||||
import { WorkspaceID } from "./schema"
|
||||
|
||||
export const WorkspaceInfo = z.object({
|
||||
id: Identifier.schema("workspace"),
|
||||
id: WorkspaceID.zod,
|
||||
type: z.string(),
|
||||
branch: z.string().nullable(),
|
||||
name: z.string().nullable(),
|
||||
|
|
|
|||
|
|
@ -1,13 +1,14 @@
|
|||
import { Context } from "../util/context"
|
||||
import type { WorkspaceID } from "./schema"
|
||||
|
||||
interface Context {
|
||||
workspaceID?: string
|
||||
workspaceID?: WorkspaceID
|
||||
}
|
||||
|
||||
const context = Context.create<Context>("workspace")
|
||||
|
||||
export const WorkspaceContext = {
|
||||
async provide<R>(input: { workspaceID?: string; fn: () => R }): Promise<R> {
|
||||
async provide<R>(input: { workspaceID?: WorkspaceID; fn: () => R }): Promise<R> {
|
||||
return context.provide({ workspaceID: input.workspaceID }, async () => {
|
||||
return input.fn()
|
||||
})
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import { InstanceBootstrap } from "../../project/bootstrap"
|
|||
import { SessionRoutes } from "../../server/routes/session"
|
||||
import { WorkspaceServerRoutes } from "./routes"
|
||||
import { WorkspaceContext } from "../workspace-context"
|
||||
import { WorkspaceID } from "../schema"
|
||||
|
||||
export namespace WorkspaceServer {
|
||||
export function App() {
|
||||
|
|
@ -20,9 +21,9 @@ export namespace WorkspaceServer {
|
|||
|
||||
return new Hono()
|
||||
.use(async (c, next) => {
|
||||
const workspaceID = c.req.query("workspace") || c.req.header("x-opencode-workspace")
|
||||
const rawWorkspaceID = c.req.query("workspace") || c.req.header("x-opencode-workspace")
|
||||
const raw = c.req.query("directory") || c.req.header("x-opencode-directory")
|
||||
if (workspaceID == null) {
|
||||
if (rawWorkspaceID == null) {
|
||||
throw new Error("workspaceID parameter is required")
|
||||
}
|
||||
if (raw == null) {
|
||||
|
|
@ -38,7 +39,7 @@ export namespace WorkspaceServer {
|
|||
})()
|
||||
|
||||
return WorkspaceContext.provide({
|
||||
workspaceID,
|
||||
workspaceID: WorkspaceID.make(rawWorkspaceID),
|
||||
async fn() {
|
||||
return Instance.provide({
|
||||
directory,
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
import { sqliteTable, text } from "drizzle-orm/sqlite-core"
|
||||
import { ProjectTable } from "../project/project.sql"
|
||||
import type { ProjectID } from "../project/schema"
|
||||
import type { WorkspaceID } from "./schema"
|
||||
|
||||
export const WorkspaceTable = sqliteTable("workspace", {
|
||||
id: text().primaryKey(),
|
||||
id: text().$type<WorkspaceID>().primaryKey(),
|
||||
type: text().notNull(),
|
||||
branch: text(),
|
||||
name: text(),
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import z from "zod"
|
||||
import { Identifier } from "@/id/id"
|
||||
import { fn } from "@/util/fn"
|
||||
import { Database, eq } from "@/storage/db"
|
||||
import { Project } from "@/project/project"
|
||||
|
|
@ -10,6 +9,7 @@ import { ProjectID } from "@/project/schema"
|
|||
import { WorkspaceTable } from "./workspace.sql"
|
||||
import { getAdaptor } from "./adaptors"
|
||||
import { WorkspaceInfo } from "./types"
|
||||
import { WorkspaceID } from "./schema"
|
||||
import { parseSSE } from "./sse"
|
||||
|
||||
export namespace Workspace {
|
||||
|
|
@ -46,7 +46,7 @@ export namespace Workspace {
|
|||
}
|
||||
|
||||
const CreateInput = z.object({
|
||||
id: Identifier.schema("workspace").optional(),
|
||||
id: WorkspaceID.zod.optional(),
|
||||
type: Info.shape.type,
|
||||
branch: Info.shape.branch,
|
||||
projectID: ProjectID.zod,
|
||||
|
|
@ -54,7 +54,7 @@ export namespace Workspace {
|
|||
})
|
||||
|
||||
export const create = fn(CreateInput, async (input) => {
|
||||
const id = Identifier.ascending("workspace", input.id)
|
||||
const id = WorkspaceID.ascending(input.id)
|
||||
const adaptor = await getAdaptor(input.type)
|
||||
|
||||
const config = await adaptor.configure({ ...input, id, name: null, directory: null })
|
||||
|
|
@ -94,13 +94,13 @@ export namespace Workspace {
|
|||
return rows.map(fromRow).sort((a, b) => a.id.localeCompare(b.id))
|
||||
}
|
||||
|
||||
export const get = fn(Identifier.schema("workspace"), async (id) => {
|
||||
export const get = fn(WorkspaceID.zod, async (id) => {
|
||||
const row = Database.use((db) => db.select().from(WorkspaceTable).where(eq(WorkspaceTable.id, id)).get())
|
||||
if (!row) return
|
||||
return fromRow(row)
|
||||
})
|
||||
|
||||
export const remove = fn(Identifier.schema("workspace"), async (id) => {
|
||||
export const remove = fn(WorkspaceID.zod, async (id) => {
|
||||
const row = Database.use((db) => db.select().from(WorkspaceTable).where(eq(WorkspaceTable.id, id)).get())
|
||||
if (row) {
|
||||
const info = fromRow(row)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { BusEvent } from "@/bus/bus-event"
|
||||
import { Bus } from "@/bus"
|
||||
import { SessionID } from "@/session/schema"
|
||||
import { SessionID, MessageID } from "@/session/schema"
|
||||
import z from "zod"
|
||||
import { Log } from "../util/log"
|
||||
import { Identifier } from "../id/id"
|
||||
|
|
@ -26,7 +26,7 @@ export namespace Permission {
|
|||
type: z.string(),
|
||||
pattern: z.union([z.string(), z.array(z.string())]).optional(),
|
||||
sessionID: SessionID.zod,
|
||||
messageID: z.string(),
|
||||
messageID: MessageID.zod,
|
||||
callID: z.string().optional(),
|
||||
message: z.string(),
|
||||
metadata: z.record(z.string(), z.any()),
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import { Bus } from "@/bus"
|
|||
import { BusEvent } from "@/bus/bus-event"
|
||||
import { Config } from "@/config/config"
|
||||
import { Identifier } from "@/id/id"
|
||||
import { SessionID } from "@/session/schema"
|
||||
import { SessionID, MessageID } from "@/session/schema"
|
||||
import { Instance } from "@/project/instance"
|
||||
import { Database, eq } from "@/storage/db"
|
||||
import { PermissionTable } from "@/session/session.sql"
|
||||
|
|
@ -77,7 +77,7 @@ export namespace PermissionNext {
|
|||
always: z.string().array(),
|
||||
tool: z
|
||||
.object({
|
||||
messageID: z.string(),
|
||||
messageID: MessageID.zod,
|
||||
callID: z.string(),
|
||||
})
|
||||
.optional(),
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { Bus } from "@/bus"
|
||||
import { BusEvent } from "@/bus/bus-event"
|
||||
import { Identifier } from "@/id/id"
|
||||
import { SessionID } from "@/session/schema"
|
||||
import { SessionID, MessageID } from "@/session/schema"
|
||||
import { Instance } from "@/project/instance"
|
||||
import { Log } from "@/util/log"
|
||||
import z from "zod"
|
||||
|
|
@ -39,7 +39,7 @@ export namespace Question {
|
|||
questions: z.array(Info).describe("Questions to ask"),
|
||||
tool: z
|
||||
.object({
|
||||
messageID: z.string(),
|
||||
messageID: MessageID.zod,
|
||||
callID: z.string(),
|
||||
})
|
||||
.optional(),
|
||||
|
|
@ -98,7 +98,7 @@ export namespace Question {
|
|||
export async function ask(input: {
|
||||
sessionID: SessionID
|
||||
questions: Info[]
|
||||
tool?: { messageID: string; callID: string }
|
||||
tool?: { messageID: MessageID; callID: string }
|
||||
}): Promise<Answer[]> {
|
||||
const s = await state()
|
||||
const id = Identifier.ascending("question")
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { Hono } from "hono"
|
||||
import { stream } from "hono/streaming"
|
||||
import { describeRoute, validator, resolver } from "hono-openapi"
|
||||
import { SessionID } from "@/session/schema"
|
||||
import { SessionID, MessageID } from "@/session/schema"
|
||||
import z from "zod"
|
||||
import { Session } from "../../session"
|
||||
import { MessageV2 } from "../../session/message-v2"
|
||||
|
|
@ -607,7 +607,7 @@ export const SessionRoutes = lazy(() =>
|
|||
"param",
|
||||
z.object({
|
||||
sessionID: SessionID.zod,
|
||||
messageID: z.string().meta({ description: "Message ID" }),
|
||||
messageID: MessageID.zod,
|
||||
}),
|
||||
),
|
||||
async (c) => {
|
||||
|
|
@ -642,7 +642,7 @@ export const SessionRoutes = lazy(() =>
|
|||
"param",
|
||||
z.object({
|
||||
sessionID: SessionID.zod,
|
||||
messageID: z.string().meta({ description: "Message ID" }),
|
||||
messageID: MessageID.zod,
|
||||
}),
|
||||
),
|
||||
async (c) => {
|
||||
|
|
@ -676,8 +676,8 @@ export const SessionRoutes = lazy(() =>
|
|||
"param",
|
||||
z.object({
|
||||
sessionID: SessionID.zod,
|
||||
messageID: z.string().meta({ description: "Message ID" }),
|
||||
partID: z.string().meta({ description: "Part ID" }),
|
||||
messageID: MessageID.zod,
|
||||
partID: z.string(),
|
||||
}),
|
||||
),
|
||||
async (c) => {
|
||||
|
|
@ -711,8 +711,8 @@ export const SessionRoutes = lazy(() =>
|
|||
"param",
|
||||
z.object({
|
||||
sessionID: SessionID.zod,
|
||||
messageID: z.string().meta({ description: "Message ID" }),
|
||||
partID: z.string().meta({ description: "Part ID" }),
|
||||
messageID: MessageID.zod,
|
||||
partID: z.string(),
|
||||
}),
|
||||
),
|
||||
validator("json", MessageV2.Part),
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import { Flag } from "../flag/flag"
|
|||
import { Command } from "../command"
|
||||
import { Global } from "../global"
|
||||
import { WorkspaceContext } from "../control-plane/workspace-context"
|
||||
import { WorkspaceID } from "../control-plane/schema"
|
||||
import { WorkspaceRouterMiddleware } from "../control-plane/workspace-router-middleware"
|
||||
import { ProjectRoutes } from "./routes/project"
|
||||
import { SessionRoutes } from "./routes/session"
|
||||
|
|
@ -190,7 +191,7 @@ export namespace Server {
|
|||
)
|
||||
.use(async (c, next) => {
|
||||
if (c.req.path === "/log") return next()
|
||||
const workspaceID = c.req.query("workspace") || c.req.header("x-opencode-workspace")
|
||||
const rawWorkspaceID = c.req.query("workspace") || c.req.header("x-opencode-workspace")
|
||||
const raw = c.req.query("directory") || c.req.header("x-opencode-directory") || process.cwd()
|
||||
const directory = Filesystem.resolve(
|
||||
(() => {
|
||||
|
|
@ -203,7 +204,7 @@ export namespace Server {
|
|||
)
|
||||
|
||||
return WorkspaceContext.provide({
|
||||
workspaceID,
|
||||
workspaceID: rawWorkspaceID ? WorkspaceID.make(rawWorkspaceID) : undefined,
|
||||
async fn() {
|
||||
return Instance.provide({
|
||||
directory,
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import { BusEvent } from "@/bus/bus-event"
|
|||
import { Bus } from "@/bus"
|
||||
import { Session } from "."
|
||||
import { Identifier } from "../id/id"
|
||||
import { SessionID } from "./schema"
|
||||
import { SessionID, MessageID } from "./schema"
|
||||
import { Instance } from "../project/instance"
|
||||
import { Provider } from "../provider/provider"
|
||||
import { MessageV2 } from "./message-v2"
|
||||
|
|
@ -100,7 +100,7 @@ export namespace SessionCompaction {
|
|||
}
|
||||
|
||||
export async function process(input: {
|
||||
parentID: string
|
||||
parentID: MessageID
|
||||
messages: MessageV2.WithParts[]
|
||||
sessionID: SessionID
|
||||
abort: AbortSignal
|
||||
|
|
@ -134,7 +134,7 @@ export namespace SessionCompaction {
|
|||
? await Provider.getModel(agent.model.providerID, agent.model.modelID)
|
||||
: await Provider.getModel(userMessage.model.providerID, userMessage.model.modelID)
|
||||
const msg = (await Session.updateMessage({
|
||||
id: Identifier.ascending("message"),
|
||||
id: MessageID.ascending(),
|
||||
role: "assistant",
|
||||
parentID: input.parentID,
|
||||
sessionID: input.sessionID,
|
||||
|
|
@ -237,7 +237,7 @@ When constructing the summary, try to stick to this template:
|
|||
if (replay) {
|
||||
const original = replay.info as MessageV2.User
|
||||
const replayMsg = await Session.updateMessage({
|
||||
id: Identifier.ascending("message"),
|
||||
id: MessageID.ascending(),
|
||||
role: "user",
|
||||
sessionID: input.sessionID,
|
||||
time: { created: Date.now() },
|
||||
|
|
@ -263,7 +263,7 @@ When constructing the summary, try to stick to this template:
|
|||
}
|
||||
} else {
|
||||
const continueMsg = await Session.updateMessage({
|
||||
id: Identifier.ascending("message"),
|
||||
id: MessageID.ascending(),
|
||||
role: "user",
|
||||
sessionID: input.sessionID,
|
||||
time: { created: Date.now() },
|
||||
|
|
@ -307,7 +307,7 @@ When constructing the summary, try to stick to this template:
|
|||
}),
|
||||
async (input) => {
|
||||
const msg = await Session.updateMessage({
|
||||
id: Identifier.ascending("message"),
|
||||
id: MessageID.ascending(),
|
||||
role: "user",
|
||||
model: input.model,
|
||||
sessionID: input.sessionID,
|
||||
|
|
|
|||
|
|
@ -24,7 +24,8 @@ import { Command } from "../command"
|
|||
import { Snapshot } from "@/snapshot"
|
||||
import { WorkspaceContext } from "../control-plane/workspace-context"
|
||||
import { ProjectID } from "../project/schema"
|
||||
import { SessionID } from "./schema"
|
||||
import { WorkspaceID } from "../control-plane/schema"
|
||||
import { SessionID, MessageID } from "./schema"
|
||||
|
||||
import type { Provider } from "@/provider/provider"
|
||||
import { PermissionNext } from "@/permission/next"
|
||||
|
|
@ -123,7 +124,7 @@ export namespace Session {
|
|||
id: SessionID.zod,
|
||||
slug: z.string(),
|
||||
projectID: ProjectID.zod,
|
||||
workspaceID: z.string().optional(),
|
||||
workspaceID: WorkspaceID.zod.optional(),
|
||||
directory: z.string(),
|
||||
parentID: SessionID.zod.optional(),
|
||||
summary: z
|
||||
|
|
@ -150,7 +151,7 @@ export namespace Session {
|
|||
permission: PermissionNext.Ruleset.optional(),
|
||||
revert: z
|
||||
.object({
|
||||
messageID: z.string(),
|
||||
messageID: MessageID.zod,
|
||||
partID: z.string().optional(),
|
||||
snapshot: z.string().optional(),
|
||||
diff: z.string().optional(),
|
||||
|
|
@ -221,7 +222,7 @@ export namespace Session {
|
|||
parentID: SessionID.zod.optional(),
|
||||
title: z.string().optional(),
|
||||
permission: Info.shape.permission,
|
||||
workspaceID: Identifier.schema("workspace").optional(),
|
||||
workspaceID: WorkspaceID.zod.optional(),
|
||||
})
|
||||
.optional(),
|
||||
async (input) => {
|
||||
|
|
@ -238,7 +239,7 @@ export namespace Session {
|
|||
export const fork = fn(
|
||||
z.object({
|
||||
sessionID: SessionID.zod,
|
||||
messageID: Identifier.schema("message").optional(),
|
||||
messageID: MessageID.zod.optional(),
|
||||
}),
|
||||
async (input) => {
|
||||
const original = await get(input.sessionID)
|
||||
|
|
@ -250,11 +251,11 @@ export namespace Session {
|
|||
title,
|
||||
})
|
||||
const msgs = await messages({ sessionID: input.sessionID })
|
||||
const idMap = new Map<string, string>()
|
||||
const idMap = new Map<string, MessageID>()
|
||||
|
||||
for (const msg of msgs) {
|
||||
if (input.messageID && msg.info.id >= input.messageID) break
|
||||
const newID = Identifier.ascending("message")
|
||||
const newID = MessageID.ascending()
|
||||
idMap.set(msg.info.id, newID)
|
||||
|
||||
const parentID = msg.info.role === "assistant" && msg.info.parentID ? idMap.get(msg.info.parentID) : undefined
|
||||
|
|
@ -297,7 +298,7 @@ export namespace Session {
|
|||
id?: SessionID
|
||||
title?: string
|
||||
parentID?: SessionID
|
||||
workspaceID?: string
|
||||
workspaceID?: WorkspaceID
|
||||
directory: string
|
||||
permission?: PermissionNext.Ruleset
|
||||
}) {
|
||||
|
|
@ -538,7 +539,7 @@ export namespace Session {
|
|||
|
||||
export function* list(input?: {
|
||||
directory?: string
|
||||
workspaceID?: string
|
||||
workspaceID?: WorkspaceID
|
||||
roots?: boolean
|
||||
start?: number
|
||||
search?: string
|
||||
|
|
@ -707,7 +708,7 @@ export namespace Session {
|
|||
export const removeMessage = fn(
|
||||
z.object({
|
||||
sessionID: SessionID.zod,
|
||||
messageID: Identifier.schema("message"),
|
||||
messageID: MessageID.zod,
|
||||
}),
|
||||
async (input) => {
|
||||
// CASCADE delete handles parts automatically
|
||||
|
|
@ -729,7 +730,7 @@ export namespace Session {
|
|||
export const removePart = fn(
|
||||
z.object({
|
||||
sessionID: SessionID.zod,
|
||||
messageID: Identifier.schema("message"),
|
||||
messageID: MessageID.zod,
|
||||
partID: Identifier.schema("part"),
|
||||
}),
|
||||
async (input) => {
|
||||
|
|
@ -777,7 +778,7 @@ export namespace Session {
|
|||
export const updatePartDelta = fn(
|
||||
z.object({
|
||||
sessionID: SessionID.zod,
|
||||
messageID: z.string(),
|
||||
messageID: MessageID.zod,
|
||||
partID: z.string(),
|
||||
field: z.string(),
|
||||
delta: z.string(),
|
||||
|
|
@ -877,7 +878,7 @@ export namespace Session {
|
|||
sessionID: SessionID.zod,
|
||||
modelID: z.string(),
|
||||
providerID: z.string(),
|
||||
messageID: Identifier.schema("message"),
|
||||
messageID: MessageID.zod,
|
||||
}),
|
||||
async (input) => {
|
||||
await SessionPrompt.command({
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
import { BusEvent } from "@/bus/bus-event"
|
||||
import { SessionID } from "./schema"
|
||||
import { SessionID, MessageID } from "./schema"
|
||||
import z from "zod"
|
||||
import { NamedError } from "@opencode-ai/util/error"
|
||||
import { APICallError, convertToModelMessages, LoadAPIKeyError, type ModelMessage, type UIMessage } from "ai"
|
||||
import { Identifier } from "../id/id"
|
||||
import { LSP } from "../lsp"
|
||||
import { Snapshot } from "@/snapshot"
|
||||
import { fn } from "@/util/fn"
|
||||
|
|
@ -81,7 +80,7 @@ export namespace MessageV2 {
|
|||
const PartBase = z.object({
|
||||
id: z.string(),
|
||||
sessionID: SessionID.zod,
|
||||
messageID: z.string(),
|
||||
messageID: MessageID.zod,
|
||||
})
|
||||
|
||||
export const SnapshotPart = PartBase.extend({
|
||||
|
|
@ -344,7 +343,7 @@ export namespace MessageV2 {
|
|||
export type ToolPart = z.infer<typeof ToolPart>
|
||||
|
||||
const Base = z.object({
|
||||
id: z.string(),
|
||||
id: MessageID.zod,
|
||||
sessionID: SessionID.zod,
|
||||
})
|
||||
|
||||
|
|
@ -411,7 +410,7 @@ export namespace MessageV2 {
|
|||
APIError.Schema,
|
||||
])
|
||||
.optional(),
|
||||
parentID: z.string(),
|
||||
parentID: MessageID.zod,
|
||||
modelID: z.string(),
|
||||
providerID: z.string(),
|
||||
/**
|
||||
|
|
@ -459,7 +458,7 @@ export namespace MessageV2 {
|
|||
"message.removed",
|
||||
z.object({
|
||||
sessionID: SessionID.zod,
|
||||
messageID: z.string(),
|
||||
messageID: MessageID.zod,
|
||||
}),
|
||||
),
|
||||
PartUpdated: BusEvent.define(
|
||||
|
|
@ -472,7 +471,7 @@ export namespace MessageV2 {
|
|||
"message.part.delta",
|
||||
z.object({
|
||||
sessionID: SessionID.zod,
|
||||
messageID: z.string(),
|
||||
messageID: MessageID.zod,
|
||||
partID: z.string(),
|
||||
field: z.string(),
|
||||
delta: z.string(),
|
||||
|
|
@ -482,7 +481,7 @@ export namespace MessageV2 {
|
|||
"message.part.removed",
|
||||
z.object({
|
||||
sessionID: SessionID.zod,
|
||||
messageID: z.string(),
|
||||
messageID: MessageID.zod,
|
||||
partID: z.string(),
|
||||
}),
|
||||
),
|
||||
|
|
@ -699,7 +698,7 @@ export namespace MessageV2 {
|
|||
// media (images, PDFs) in tool results
|
||||
if (media.length > 0) {
|
||||
result.push({
|
||||
id: Identifier.ascending("message"),
|
||||
id: MessageID.ascending(),
|
||||
role: "user",
|
||||
parts: [
|
||||
{
|
||||
|
|
@ -782,7 +781,7 @@ export namespace MessageV2 {
|
|||
}
|
||||
})
|
||||
|
||||
export const parts = fn(Identifier.schema("message"), async (message_id) => {
|
||||
export const parts = fn(MessageID.zod, async (message_id) => {
|
||||
const rows = Database.use((db) =>
|
||||
db.select().from(PartTable).where(eq(PartTable.message_id, message_id)).orderBy(PartTable.id).all(),
|
||||
)
|
||||
|
|
@ -794,7 +793,7 @@ export namespace MessageV2 {
|
|||
export const get = fn(
|
||||
z.object({
|
||||
sessionID: SessionID.zod,
|
||||
messageID: Identifier.schema("message"),
|
||||
messageID: MessageID.zod,
|
||||
}),
|
||||
async (input): Promise<WithParts> => {
|
||||
const row = Database.use((db) => db.select().from(MessageTable).where(eq(MessageTable.id, input.messageID)).get())
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ import { Config } from "@/config/config"
|
|||
import { SessionCompaction } from "./compaction"
|
||||
import { PermissionNext } from "@/permission/next"
|
||||
import { Question } from "@/question"
|
||||
import type { SessionID } from "./schema"
|
||||
import type { SessionID, MessageID } from "./schema"
|
||||
|
||||
export namespace SessionProcessor {
|
||||
const DOOM_LOOP_THRESHOLD = 3
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue