GeezerVison is showing github.com — refactor(control-plane): migrate workspace DTO schemas (#24056) · githubhjs/opencode@31d01d4
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/opencode/specs/effect/schema.md +3-3Lines changed: 3 additions & 3 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -354,9 +354,9 @@ piecewise. | 354 | 354 | - [ ] `src/cli/cmd/tui/event.ts` | 355 | 355 | - [ ] `src/cli/ui.ts` | 356 | 356 | - [ ] `src/command/index.ts` | 357 | | -- [ ] `src/control-plane/adaptors/worktree.ts` | 358 | | -- [ ] `src/control-plane/types.ts` | 359 | | -- [ ] `src/control-plane/workspace.ts` | | 357 | +- [x] `src/control-plane/adaptors/worktree.ts` | | 358 | +- [x] `src/control-plane/types.ts` | | 359 | +- [x] `src/control-plane/workspace.ts` | 360 | 360 | - [ ] `src/file/index.ts` | 361 | 361 | - [ ] `src/file/ripgrep.ts` | 362 | 362 | - [ ] `src/file/watcher.ts` | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/control-plane/adaptors/index.ts +1-7Lines changed: 1 addition & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { lazy } from "@/util/lazy" | 2 | 2 | import type { ProjectID } from "@/project/schema" | 3 | | -import type { WorkspaceAdaptor } from "../types" | 4 | | - | 5 | | -export type WorkspaceAdaptorEntry = { | 6 | | - type: string | 7 | | - name: string | 8 | | - description: string | 9 | | -} | | 3 | +import type { WorkspaceAdaptor, WorkspaceAdaptorEntry } from "../types" | 10 | 4 |
| 11 | 5 | const BUILTIN: Record<string, () => Promise<WorkspaceAdaptor>> = { | 12 | 6 | worktree: lazy(async () => (await import("./worktree")).WorktreeAdaptor), | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/control-plane/adaptors/worktree.ts +11-9Lines changed: 11 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import z from "zod" | | 1 | +import { Schema } from "effect" | 2 | 2 | import { AppRuntime } from "@/effect/app-runtime" | 3 | 3 | import { Worktree } from "@/worktree" | 4 | 4 | import { type WorkspaceAdaptor, WorkspaceInfo } from "../types" | | 5 | +import { zod } from "@/util/effect-zod" | | 6 | +import { withStatics } from "@/util/schema" | 5 | 7 |
| 6 | | -const WorktreeConfig = z.object({ | 7 | | - name: WorkspaceInfo.shape.name, | 8 | | - branch: WorkspaceInfo.shape.branch.unwrap(), | 9 | | - directory: WorkspaceInfo.shape.directory.unwrap(), | 10 | | -}) | | 8 | +const WorktreeConfig = Schema.Struct({ | | 9 | + name: WorkspaceInfo.fields.name, | | 10 | + branch: Schema.String, | | 11 | + directory: Schema.String, | | 12 | +}).pipe(withStatics((s) => ({ zod: zod(s) }))) | 11 | 13 |
| 12 | 14 | export const WorktreeAdaptor: WorkspaceAdaptor = { | 13 | 15 | name: "Worktree", | @@ -22,7 +24,7 @@ export const WorktreeAdaptor: WorkspaceAdaptor = { | 22 | 24 | } | 23 | 25 | }, | 24 | 26 | async create(info) { | 25 | | - const config = WorktreeConfig.parse(info) | | 27 | + const config = WorktreeConfig.zod.parse(info) | 26 | 28 | await AppRuntime.runPromise( | 27 | 29 | Worktree.Service.use((svc) => | 28 | 30 | svc.createFromInfo({ | @@ -34,11 +36,11 @@ export const WorktreeAdaptor: WorkspaceAdaptor = { | 34 | 36 | ) | 35 | 37 | }, | 36 | 38 | async remove(info) { | 37 | | - const config = WorktreeConfig.parse(info) | | 39 | + const config = WorktreeConfig.zod.parse(info) | 38 | 40 | await AppRuntime.runPromise(Worktree.Service.use((svc) => svc.remove({ directory: config.directory }))) | 39 | 41 | }, | 40 | 42 | target(info) { | 41 | | - const config = WorktreeConfig.parse(info) | | 43 | + const config = WorktreeConfig.zod.parse(info) | 42 | 44 | return { | 43 | 45 | type: "local", | 44 | 46 | directory: config.directory, | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/control-plane/types.ts +21-10Lines changed: 21 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import z from "zod" | | 1 | +import { Schema } from "effect" | 2 | 2 | import { ProjectID } from "@/project/schema" | 3 | 3 | import { WorkspaceID } from "./schema" | | 4 | +import { zod } from "@/util/effect-zod" | | 5 | +import { type DeepMutable, withStatics } from "@/util/schema" | 4 | 6 |
| 5 | | -export const WorkspaceInfo = z.object({ | 6 | | - id: WorkspaceID.zod, | 7 | | - type: z.string(), | 8 | | - name: z.string(), | 9 | | - branch: z.string().nullable(), | 10 | | - directory: z.string().nullable(), | 11 | | - extra: z.unknown().nullable(), | 12 | | - projectID: ProjectID.zod, | | 7 | +export const WorkspaceInfo = Schema.Struct({ | | 8 | + id: WorkspaceID, | | 9 | + type: Schema.String, | | 10 | + name: Schema.String, | | 11 | + branch: Schema.NullOr(Schema.String), | | 12 | + directory: Schema.NullOr(Schema.String), | | 13 | + extra: Schema.NullOr(Schema.Unknown), | | 14 | + projectID: ProjectID, | 13 | 15 | }) | 14 | | -export type WorkspaceInfo = z.infer<typeof WorkspaceInfo> | | 16 | + .annotate({ identifier: "Workspace" }) | | 17 | + .pipe(withStatics((s) => ({ zod: zod(s) }))) | | 18 | +export type WorkspaceInfo = DeepMutable<Schema.Schema.Type<typeof WorkspaceInfo>> | | 19 | + | | 20 | +export const WorkspaceAdaptorEntry = Schema.Struct({ | | 21 | + type: Schema.String, | | 22 | + name: Schema.String, | | 23 | + description: Schema.String, | | 24 | +}).pipe(withStatics((s) => ({ zod: zod(s) }))) | | 25 | +export type WorkspaceAdaptorEntry = Schema.Schema.Type<typeof WorkspaceAdaptorEntry> | 15 | 26 |
| 16 | 27 | export type Target = | 17 | 28 | | { | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/control-plane/workspace.ts +20-20Lines changed: 20 additions & 20 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import z from "zod" | 2 | 1 | import { Schema } from "effect" | 3 | 2 | import { setTimeout as sleep } from "node:timers/promises" | 4 | 3 | import { fn } from "@/util/fn" | @@ -16,7 +15,7 @@ import { ProjectID } from "@/project/schema" | 16 | 15 | import { Slug } from "@opencode-ai/shared/util/slug" | 17 | 16 | import { WorkspaceTable } from "./workspace.sql" | 18 | 17 | import { getAdaptor } from "./adaptors" | 19 | | -import { WorkspaceInfo } from "./types" | | 18 | +import { type WorkspaceInfo, WorkspaceInfo as WorkspaceInfoSchema } from "./types" | 20 | 19 | import { WorkspaceID } from "./schema" | 21 | 20 | import { parseSSE } from "./sse" | 22 | 21 | import { Session } from "@/session" | @@ -26,12 +25,11 @@ import { errorData } from "@/util/error" | 26 | 25 | import { AppRuntime } from "@/effect/app-runtime" | 27 | 26 | import { waitEvent } from "./util" | 28 | 27 | import { WorkspaceContext } from "./workspace-context" | 29 | | -import { NonNegativeInt } from "@/util/schema" | | 28 | +import { NonNegativeInt, withStatics } from "@/util/schema" | | 29 | +import { zod as effectZod, zodObject } from "@/util/effect-zod" | 30 | 30 |
| 31 | | -export const Info = WorkspaceInfo.meta({ | 32 | | - ref: "Workspace", | 33 | | -}) | 34 | | -export type Info = z.infer<typeof Info> | | 31 | +export const Info = WorkspaceInfoSchema | | 32 | +export type Info = WorkspaceInfo | 35 | 33 |
| 36 | 34 | export const ConnectionStatus = Schema.Struct({ | 37 | 35 | workspaceID: WorkspaceID, | @@ -75,15 +73,16 @@ function fromRow(row: typeof WorkspaceTable.$inferSelect): Info { | 75 | 73 | } | 76 | 74 | } | 77 | 75 |
| 78 | | -const CreateInput = z.object({ | 79 | | - id: WorkspaceID.zod.optional(), | 80 | | - type: Info.shape.type, | 81 | | - branch: Info.shape.branch, | 82 | | - projectID: ProjectID.zod, | 83 | | - extra: Info.shape.extra, | 84 | | -}) | | 76 | +export const CreateInput = Schema.Struct({ | | 77 | + id: Schema.optional(WorkspaceID), | | 78 | + type: Info.fields.type, | | 79 | + branch: Info.fields.branch, | | 80 | + projectID: ProjectID, | | 81 | + extra: Info.fields.extra, | | 82 | +}).pipe(withStatics((s) => ({ zod: effectZod(s), zodObject: zodObject(s) }))) | | 83 | +export type CreateInput = Schema.Schema.Type<typeof CreateInput> | 85 | 84 |
| 86 | | -export const create = fn(CreateInput, async (input) => { | | 85 | +export const create = fn(CreateInput.zod, async (input) => { | 87 | 86 | const id = WorkspaceID.ascending(input.id) | 88 | 87 | const adaptor = await getAdaptor(input.projectID, input.type) | 89 | 88 |
| @@ -139,12 +138,13 @@ export const create = fn(CreateInput, async (input) => { | 139 | 138 | return info | 140 | 139 | }) | 141 | 140 |
| 142 | | -const SessionRestoreInput = z.object({ | 143 | | - workspaceID: WorkspaceID.zod, | 144 | | - sessionID: SessionID.zod, | 145 | | -}) | | 141 | +export const SessionRestoreInput = Schema.Struct({ | | 142 | + workspaceID: WorkspaceID, | | 143 | + sessionID: SessionID, | | 144 | +}).pipe(withStatics((s) => ({ zod: effectZod(s), zodObject: zodObject(s) }))) | | 145 | +export type SessionRestoreInput = Schema.Schema.Type<typeof SessionRestoreInput> | 146 | 146 |
| 147 | | -export const sessionRestore = fn(SessionRestoreInput, async (input) => { | | 147 | +export const sessionRestore = fn(SessionRestoreInput.zod, async (input) => { | 148 | 148 | log.info("session restore requested", { | 149 | 149 | workspaceID: input.workspaceID, | 150 | 150 | sessionID: input.sessionID, | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/server/routes/control/workspace.ts +11-16Lines changed: 11 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,6 +3,7 @@ import { describeRoute, resolver, validator } from "hono-openapi" | 3 | 3 | import z from "zod" | 4 | 4 | import { listAdaptors } from "@/control-plane/adaptors" | 5 | 5 | import { Workspace } from "@/control-plane/workspace" | | 6 | +import { WorkspaceAdaptorEntry } from "@/control-plane/types" | 6 | 7 | import { zodObject } from "@/util/effect-zod" | 7 | 8 | import { Instance } from "@/project/instance" | 8 | 9 | import { errors } from "../../error" | @@ -26,13 +27,7 @@ export const WorkspaceRoutes = lazy(() => | 26 | 27 | content: { | 27 | 28 | "application/json": { | 28 | 29 | schema: resolver( | 29 | | - z.array( | 30 | | - z.object({ | 31 | | - type: z.string(), | 32 | | - name: z.string(), | 33 | | - description: z.string(), | 34 | | - }), | 35 | | - ), | | 30 | + z.array(zodObject(WorkspaceAdaptorEntry)), | 36 | 31 | ), | 37 | 32 | }, | 38 | 33 | }, | @@ -54,7 +49,7 @@ export const WorkspaceRoutes = lazy(() => | 54 | 49 | description: "Workspace created", | 55 | 50 | content: { | 56 | 51 | "application/json": { | 57 | | - schema: resolver(Workspace.Info), | | 52 | + schema: resolver(Workspace.Info.zod), | 58 | 53 | }, | 59 | 54 | }, | 60 | 55 | }, | @@ -63,12 +58,12 @@ export const WorkspaceRoutes = lazy(() => | 63 | 58 | }), | 64 | 59 | validator( | 65 | 60 | "json", | 66 | | - Workspace.create.schema.omit({ | | 61 | + Workspace.CreateInput.zodObject.omit({ | 67 | 62 | projectID: true, | 68 | 63 | }), | 69 | 64 | ), | 70 | 65 | async (c) => { | 71 | | - const body = c.req.valid("json") | | 66 | + const body = c.req.valid("json") as Omit<Workspace.CreateInput, "projectID"> | 72 | 67 | const workspace = await Workspace.create({ | 73 | 68 | projectID: Instance.project.id, | 74 | 69 | ...body, | @@ -87,7 +82,7 @@ export const WorkspaceRoutes = lazy(() => | 87 | 82 | description: "Workspaces", | 88 | 83 | content: { | 89 | 84 | "application/json": { | 90 | | - schema: resolver(z.array(Workspace.Info)), | | 85 | + schema: resolver(z.array(Workspace.Info.zod)), | 91 | 86 | }, | 92 | 87 | }, | 93 | 88 | }, | @@ -130,7 +125,7 @@ export const WorkspaceRoutes = lazy(() => | 130 | 125 | description: "Workspace removed", | 131 | 126 | content: { | 132 | 127 | "application/json": { | 133 | | - schema: resolver(Workspace.Info.optional()), | | 128 | + schema: resolver(Workspace.Info.zod.optional()), | 134 | 129 | }, | 135 | 130 | }, | 136 | 131 | }, | @@ -140,7 +135,7 @@ export const WorkspaceRoutes = lazy(() => | 140 | 135 | validator( | 141 | 136 | "param", | 142 | 137 | z.object({ | 143 | | - id: Workspace.Info.shape.id, | | 138 | + id: zodObject(Workspace.Info).shape.id, | 144 | 139 | }), | 145 | 140 | ), | 146 | 141 | async (c) => { | @@ -170,11 +165,11 @@ export const WorkspaceRoutes = lazy(() => | 170 | 165 | ...errors(400), | 171 | 166 | }, | 172 | 167 | }), | 173 | | - validator("param", z.object({ id: Workspace.Info.shape.id })), | 174 | | - validator("json", Workspace.sessionRestore.schema.omit({ workspaceID: true })), | | 168 | + validator("param", z.object({ id: zodObject(Workspace.Info).shape.id })), | | 169 | + validator("json", Workspace.SessionRestoreInput.zodObject.omit({ workspaceID: true })), | 175 | 170 | async (c) => { | 176 | 171 | const { id } = c.req.valid("param") | 177 | | - const body = c.req.valid("json") | | 172 | + const body = c.req.valid("json") as Omit<Workspace.SessionRestoreInput, "workspaceID"> | 178 | 173 | log.info("session restore route requested", { | 179 | 174 | workspaceID: id, | 180 | 175 | sessionID: body.sessionID, | |
|