GeezerVison is showing github.com — feat(id): brand WorkspaceID through Drizzle and Zod schemas (#16964) · ildella/opencode@16a6d6f
File treeExpand file treeCollapse file tree Open diff view settings Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/opencode/script/seed-e2e.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,14 +12,15 @@ const seed = async () => { | 12 | 12 | const { InstanceBootstrap } = await import("../src/project/bootstrap") | 13 | 13 | const { Session } = await import("../src/session") | 14 | 14 | const { Identifier } = await import("../src/id/id") | | 15 | + const { MessageID } = await import("../src/session/schema") | 15 | 16 | const { Project } = await import("../src/project/project") | 16 | 17 |
| 17 | 18 | await Instance.provide({ | 18 | 19 | directory: dir, | 19 | 20 | init: InstanceBootstrap, | 20 | 21 | fn: async () => { | 21 | 22 | const session = await Session.create({ title }) | 22 | | - const messageID = Identifier.descending("message") | | 23 | + const messageID = MessageID.ascending() | 23 | 24 | const partID = Identifier.descending("part") | 24 | 25 | const message = { | 25 | 26 | id: messageID, | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/debug/agent.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,6 +5,7 @@ import { Provider } from "../../../provider/provider" | 5 | 5 | import { Session } from "../../../session" | 6 | 6 | import type { MessageV2 } from "../../../session/message-v2" | 7 | 7 | import { Identifier } from "../../../id/id" | | 8 | +import { MessageID } from "../../../session/schema" | 8 | 9 | import { ToolRegistry } from "../../../tool/registry" | 9 | 10 | import { Instance } from "../../../project/instance" | 10 | 11 | import { PermissionNext } from "../../../permission/next" | @@ -113,7 +114,7 @@ function parseToolParams(input?: string) { | 113 | 114 |
| 114 | 115 | async function createToolContext(agent: Agent.Info) { | 115 | 116 | const session = await Session.create({ title: `Debug tool run (${agent.name})` }) | 116 | | - const messageID = Identifier.ascending("message") | | 117 | + const messageID = MessageID.ascending() | 117 | 118 | const model = agent.model ?? (await Provider.defaultModel()) | 118 | 119 | const now = Date.now() | 119 | 120 | const message: MessageV2.Assistant = { | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/github.ts +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,6 +24,7 @@ import { bootstrap } from "../bootstrap" | 24 | 24 | import { Session } from "../../session" | 25 | 25 | import type { SessionID } from "../../session/schema" | 26 | 26 | import { Identifier } from "../../id/id" | | 27 | +import { MessageID } from "../../session/schema" | 27 | 28 | import { Provider } from "../../provider/provider" | 28 | 29 | import { Bus } from "../../bus" | 29 | 30 | import { MessageV2 } from "../../session/message-v2" | @@ -935,7 +936,7 @@ export const GithubRunCommand = cmd({ | 935 | 936 |
| 936 | 937 | const result = await SessionPrompt.prompt({ | 937 | 938 | sessionID: session.id, | 938 | | - messageID: Identifier.ascending("message"), | | 939 | + messageID: MessageID.ascending(), | 939 | 940 | variant, | 940 | 941 | model: { | 941 | 942 | providerID, | @@ -989,7 +990,7 @@ export const GithubRunCommand = cmd({ | 989 | 990 | console.log("Requesting summary from agent...") | 990 | 991 | const summary = await SessionPrompt.prompt({ | 991 | 992 | sessionID: session.id, | 992 | | - messageID: Identifier.ascending("message"), | | 993 | + messageID: MessageID.ascending(), | 993 | 994 | variant, | 994 | 995 | model: { | 995 | 996 | providerID, | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/import.ts +12-5Lines changed: 12 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import type { Argv } from "yargs" | 2 | 2 | import type { Session as SDKSession, Message, Part } from "@opencode-ai/sdk/v2" | 3 | 3 | import { Session } from "../../session" | 4 | | -import { SessionID } from "../../session/schema" | | 4 | +import { SessionID, MessageID } from "../../session/schema" | | 5 | +import { WorkspaceID } from "../../control-plane/schema" | 5 | 6 | import { cmd } from "./cmd" | 6 | 7 | import { bootstrap } from "../bootstrap" | 7 | 8 | import { Database } from "../../storage/db" | @@ -157,7 +158,11 @@ export const ImportCommand = cmd({ | 157 | 158 | ...exportData.info, | 158 | 159 | id: SessionID.make(exportData.info.id), | 159 | 160 | parentID: exportData.info.parentID ? SessionID.make(exportData.info.parentID) : undefined, | | 161 | + workspaceID: exportData.info.workspaceID ? WorkspaceID.make(exportData.info.workspaceID) : undefined, | 160 | 162 | projectID: Instance.project.id, | | 163 | + revert: exportData.info.revert | | 164 | + ? { ...exportData.info.revert, messageID: MessageID.make(exportData.info.revert.messageID) } | | 165 | + : undefined, | 161 | 166 | }) | 162 | 167 | Database.use((db) => | 163 | 168 | db | @@ -168,28 +173,30 @@ export const ImportCommand = cmd({ | 168 | 173 | ) | 169 | 174 |
| 170 | 175 | for (const msg of exportData.messages) { | | 176 | + const { id: _mid, sessionID: _msid, ...msgData } = msg.info | 171 | 177 | Database.use((db) => | 172 | 178 | db | 173 | 179 | .insert(MessageTable) | 174 | 180 | .values({ | 175 | | - id: msg.info.id, | | 181 | + id: MessageID.make(msg.info.id), | 176 | 182 | session_id: row.id, | 177 | 183 | time_created: msg.info.time?.created ?? Date.now(), | 178 | | - data: msg.info, | | 184 | + data: msgData, | 179 | 185 | }) | 180 | 186 | .onConflictDoNothing() | 181 | 187 | .run(), | 182 | 188 | ) | 183 | 189 |
| 184 | 190 | for (const part of msg.parts) { | | 191 | + const { id: _pid, sessionID: _psid, messageID: _pmid, ...partData } = part | 185 | 192 | Database.use((db) => | 186 | 193 | db | 187 | 194 | .insert(PartTable) | 188 | 195 | .values({ | 189 | 196 | id: part.id, | 190 | | - message_id: msg.info.id, | | 197 | + message_id: MessageID.make(msg.info.id), | 191 | 198 | session_id: row.id, | 192 | | - data: part, | | 199 | + data: partData, | 193 | 200 | }) | 194 | 201 | .onConflictDoNothing() | 195 | 202 | .run(), | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,6 +10,7 @@ import { useSDK } from "@tui/context/sdk" | 10 | 10 | import { useRoute } from "@tui/context/route" | 11 | 11 | import { useSync } from "@tui/context/sync" | 12 | 12 | import { Identifier } from "@/id/id" | | 13 | +import { MessageID } from "@/session/schema" | 13 | 14 | import { createStore, produce } from "solid-js/store" | 14 | 15 | import { useKeybind } from "@tui/context/keybind" | 15 | 16 | import { usePromptHistory, type PromptInfo } from "./history" | @@ -561,7 +562,7 @@ export function Prompt(props: PromptProps) { | 561 | 562 | sessionID = res.data.id | 562 | 563 | } | 563 | 564 |
| 564 | | - const messageID = Identifier.ascending("message") | | 565 | + const messageID = MessageID.ascending() | 565 | 566 | let inputText = store.prompt.input | 566 | 567 |
| 567 | 568 | // Expand pasted text inline before submitting | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/command/index.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { BusEvent } from "@/bus/bus-event" | 2 | | -import { SessionID } from "@/session/schema" | | 2 | +import { SessionID, MessageID } from "@/session/schema" | 3 | 3 | import z from "zod" | 4 | 4 | import { Config } from "../config/config" | 5 | 5 | import { Instance } from "../project/instance" | @@ -17,7 +17,7 @@ export namespace Command { | 17 | 17 | name: z.string(), | 18 | 18 | sessionID: SessionID.zod, | 19 | 19 | arguments: z.string(), | 20 | | - messageID: Identifier.schema("message"), | | 20 | + messageID: MessageID.zod, | 21 | 21 | }), | 22 | 22 | ), | 23 | 23 | } | |
Collapse file Copy file name to clipboard +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { Schema } from "effect" | | 2 | +import z from "zod" | | 3 | + | | 4 | +import { withStatics } from "@/util/schema" | | 5 | +import { Identifier } from "@/id/id" | | 6 | + | | 7 | +const workspaceIdSchema = Schema.String.pipe(Schema.brand("WorkspaceId")) | | 8 | + | | 9 | +export type WorkspaceID = typeof workspaceIdSchema.Type | | 10 | + | | 11 | +export const WorkspaceID = workspaceIdSchema.pipe( | | 12 | + withStatics((schema: typeof workspaceIdSchema) => ({ | | 13 | + make: (id: string) => schema.makeUnsafe(id), | | 14 | + ascending: (id?: string) => schema.makeUnsafe(Identifier.ascending("workspace", id)), | | 15 | + zod: z.string().startsWith("wrk").pipe(z.custom<WorkspaceID>()), | | 16 | + })), | | 17 | +) |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/control-plane/types.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import z from "zod" | 2 | | -import { Identifier } from "@/id/id" | 3 | 2 | import { ProjectID } from "@/project/schema" | | 3 | +import { WorkspaceID } from "./schema" | 4 | 4 |
| 5 | 5 | export const WorkspaceInfo = z.object({ | 6 | | - id: Identifier.schema("workspace"), | | 6 | + id: WorkspaceID.zod, | 7 | 7 | type: z.string(), | 8 | 8 | branch: z.string().nullable(), | 9 | 9 | name: z.string().nullable(), | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/control-plane/workspace-context.ts +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { Context } from "../util/context" | | 2 | +import type { WorkspaceID } from "./schema" | 2 | 3 |
| 3 | 4 | interface Context { | 4 | | - workspaceID?: string | | 5 | + workspaceID?: WorkspaceID | 5 | 6 | } | 6 | 7 |
| 7 | 8 | const context = Context.create<Context>("workspace") | 8 | 9 |
| 9 | 10 | export const WorkspaceContext = { | 10 | | - async provide<R>(input: { workspaceID?: string; fn: () => R }): Promise<R> { | | 11 | + async provide<R>(input: { workspaceID?: WorkspaceID; fn: () => R }): Promise<R> { | 11 | 12 | return context.provide({ workspaceID: input.workspaceID }, async () => { | 12 | 13 | return input.fn() | 13 | 14 | }) | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/control-plane/workspace-server/server.ts +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,6 +4,7 @@ import { InstanceBootstrap } from "../../project/bootstrap" | 4 | 4 | import { SessionRoutes } from "../../server/routes/session" | 5 | 5 | import { WorkspaceServerRoutes } from "./routes" | 6 | 6 | import { WorkspaceContext } from "../workspace-context" | | 7 | +import { WorkspaceID } from "../schema" | 7 | 8 |
| 8 | 9 | export namespace WorkspaceServer { | 9 | 10 | export function App() { | @@ -20,9 +21,9 @@ export namespace WorkspaceServer { | 20 | 21 |
| 21 | 22 | return new Hono() | 22 | 23 | .use(async (c, next) => { | 23 | | - const workspaceID = c.req.query("workspace") || c.req.header("x-opencode-workspace") | | 24 | + const rawWorkspaceID = c.req.query("workspace") || c.req.header("x-opencode-workspace") | 24 | 25 | const raw = c.req.query("directory") || c.req.header("x-opencode-directory") | 25 | | - if (workspaceID == null) { | | 26 | + if (rawWorkspaceID == null) { | 26 | 27 | throw new Error("workspaceID parameter is required") | 27 | 28 | } | 28 | 29 | if (raw == null) { | @@ -38,7 +39,7 @@ export namespace WorkspaceServer { | 38 | 39 | })() | 39 | 40 |
| 40 | 41 | return WorkspaceContext.provide({ | 41 | | - workspaceID, | | 42 | + workspaceID: WorkspaceID.make(rawWorkspaceID), | 42 | 43 | async fn() { | 43 | 44 | return Instance.provide({ | 44 | 45 | directory, | |
|