Skip to content

Commit 9ee89f7

Browse files
authored
refactor: move project read routes onto HttpApi (#23003)
1 parent 67dbb3c commit 9ee89f7

5 files changed

Lines changed: 119 additions & 47 deletions

File tree

packages/opencode/src/project/project.ts

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,52 @@ import { BusEvent } from "@/bus/bus-event"
88
import { GlobalBus } from "@/bus/global"
99
import { which } from "../util/which"
1010
import { ProjectID } from "./schema"
11-
import { Effect, Layer, Path, Scope, Context, Stream } from "effect"
11+
import { Effect, Layer, Path, Scope, Context, Stream, Types, Schema } from "effect"
1212
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
1313
import { NodePath } from "@effect/platform-node"
1414
import { AppFileSystem } from "@opencode-ai/shared/filesystem"
1515
import * as CrossSpawnSpawner from "@/effect/cross-spawn-spawner"
16+
import { zod } from "@/util/effect-zod"
17+
import { withStatics } from "@/util/schema"
1618

1719
const log = Log.create({ service: "project" })
1820

19-
export const Info = z
20-
.object({
21-
id: ProjectID.zod,
22-
worktree: z.string(),
23-
vcs: z.literal("git").optional(),
24-
name: z.string().optional(),
25-
icon: z
26-
.object({
27-
url: z.string().optional(),
28-
override: z.string().optional(),
29-
color: z.string().optional(),
30-
})
31-
.optional(),
32-
commands: z
33-
.object({
34-
start: z.string().optional().describe("Startup script to run when creating a new workspace (worktree)"),
35-
})
36-
.optional(),
37-
time: z.object({
38-
created: z.number(),
39-
updated: z.number(),
40-
initialized: z.number().optional(),
41-
}),
42-
sandboxes: z.array(z.string()),
43-
})
44-
.meta({
45-
ref: "Project",
46-
})
47-
export type Info = z.infer<typeof Info>
21+
const ProjectVcs = Schema.Literal("git")
22+
23+
const ProjectIcon = Schema.Struct({
24+
url: Schema.optional(Schema.String),
25+
override: Schema.optional(Schema.String),
26+
color: Schema.optional(Schema.String),
27+
})
28+
29+
const ProjectCommands = Schema.Struct({
30+
start: Schema.optional(
31+
Schema.String.annotate({ description: "Startup script to run when creating a new workspace (worktree)" }),
32+
),
33+
})
34+
35+
const ProjectTime = Schema.Struct({
36+
created: Schema.Number,
37+
updated: Schema.Number,
38+
initialized: Schema.optional(Schema.Number),
39+
})
40+
41+
export const Info = Schema.Struct({
42+
id: ProjectID,
43+
worktree: Schema.String,
44+
vcs: Schema.optional(ProjectVcs),
45+
name: Schema.optional(Schema.String),
46+
icon: Schema.optional(ProjectIcon),
47+
commands: Schema.optional(ProjectCommands),
48+
time: ProjectTime,
49+
sandboxes: Schema.Array(Schema.String),
50+
})
51+
.annotate({ identifier: "Project" })
52+
.pipe(withStatics((s) => ({ zod: zod(s) })))
53+
export type Info = Types.DeepMutable<Schema.Schema.Type<typeof Info>>
4854

4955
export const Event = {
50-
Updated: BusEvent.define("project.updated", Info),
56+
Updated: BusEvent.define("project.updated", Info.zod),
5157
}
5258

5359
type Row = typeof ProjectTable.$inferSelect
@@ -58,7 +64,7 @@ export function fromRow(row: Row): Info {
5864
return {
5965
id: row.id,
6066
worktree: row.worktree,
61-
vcs: row.vcs ? Info.shape.vcs.parse(row.vcs) : undefined,
67+
vcs: row.vcs ? Schema.decodeUnknownSync(ProjectVcs)(row.vcs) : undefined,
6268
name: row.name ?? undefined,
6369
icon,
6470
time: {
@@ -74,8 +80,8 @@ export function fromRow(row: Row): Info {
7480
export const UpdateInput = z.object({
7581
projectID: ProjectID.zod,
7682
name: z.string().optional(),
77-
icon: Info.shape.icon.optional(),
78-
commands: Info.shape.commands.optional(),
83+
icon: zod(ProjectIcon).optional(),
84+
commands: zod(ProjectCommands).optional(),
7985
})
8086
export type UpdateInput = z.infer<typeof UpdateInput>
8187

@@ -139,7 +145,7 @@ export const layer: Layer.Layer<
139145
}),
140146
)
141147

142-
const fakeVcs = Info.shape.vcs.parse(Flag.OPENCODE_FAKE_VCS)
148+
const fakeVcs = Schema.decodeUnknownSync(Schema.optional(ProjectVcs))(Flag.OPENCODE_FAKE_VCS)
143149

144150
const resolveGitPath = (cwd: string, name: string) => {
145151
if (!name) return cwd
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { Instance } from "@/project/instance"
2+
import { Project } from "@/project"
3+
import { Effect, Layer, Schema } from "effect"
4+
import { HttpApi, HttpApiBuilder, HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/unstable/httpapi"
5+
6+
const root = "/project"
7+
8+
export const ProjectApi = HttpApi.make("project")
9+
.add(
10+
HttpApiGroup.make("project")
11+
.add(
12+
HttpApiEndpoint.get("list", root, {
13+
success: Schema.Array(Project.Info),
14+
}).annotateMerge(
15+
OpenApi.annotations({
16+
identifier: "project.list",
17+
summary: "List all projects",
18+
description: "Get a list of projects that have been opened with OpenCode.",
19+
}),
20+
),
21+
HttpApiEndpoint.get("current", `${root}/current`, {
22+
success: Project.Info,
23+
}).annotateMerge(
24+
OpenApi.annotations({
25+
identifier: "project.current",
26+
summary: "Get current project",
27+
description: "Retrieve the currently active project that OpenCode is working with.",
28+
}),
29+
),
30+
)
31+
.annotateMerge(
32+
OpenApi.annotations({
33+
title: "project",
34+
description: "Experimental HttpApi project routes.",
35+
}),
36+
),
37+
)
38+
.annotateMerge(
39+
OpenApi.annotations({
40+
title: "opencode experimental HttpApi",
41+
version: "0.0.1",
42+
description: "Experimental HttpApi surface for selected instance routes.",
43+
}),
44+
)
45+
46+
export const projectHandlers = Layer.unwrap(
47+
Effect.gen(function* () {
48+
const svc = yield* Project.Service
49+
50+
const list = Effect.fn("ProjectHttpApi.list")(function* () {
51+
return yield* svc.list()
52+
})
53+
54+
const current = Effect.fn("ProjectHttpApi.current")(function* () {
55+
return Instance.project
56+
})
57+
58+
return HttpApiBuilder.group(ProjectApi, "project", (handlers) =>
59+
handlers.handle("list", list).handle("current", current),
60+
)
61+
}),
62+
).pipe(Layer.provide(Project.defaultLayer))

packages/opencode/src/server/instance/httpapi/server.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { lazy } from "@/util/lazy"
1212
import { Filesystem } from "@/util"
1313
import { ConfigApi, configHandlers } from "./config"
1414
import { PermissionApi, permissionHandlers } from "./permission"
15+
import { ProjectApi, projectHandlers } from "./project"
1516
import { ProviderApi, providerHandlers } from "./provider"
1617
import { QuestionApi, questionHandlers } from "./question"
1718

@@ -108,11 +109,13 @@ const instance = HttpRouter.middleware()(
108109

109110
const QuestionSecured = QuestionApi.middleware(Authorization)
110111
const PermissionSecured = PermissionApi.middleware(Authorization)
112+
const ProjectSecured = ProjectApi.middleware(Authorization)
111113
const ProviderSecured = ProviderApi.middleware(Authorization)
112114
const ConfigSecured = ConfigApi.middleware(Authorization)
113115

114116
export const routes = Layer.mergeAll(
115117
HttpApiBuilder.layer(ConfigSecured).pipe(Layer.provide(configHandlers)),
118+
HttpApiBuilder.layer(ProjectSecured).pipe(Layer.provide(projectHandlers)),
116119
HttpApiBuilder.layer(QuestionSecured).pipe(Layer.provide(questionHandlers)),
117120
HttpApiBuilder.layer(PermissionSecured).pipe(Layer.provide(permissionHandlers)),
118121
HttpApiBuilder.layer(ProviderSecured).pipe(Layer.provide(providerHandlers)),

packages/opencode/src/server/instance/index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,7 @@ import { WorkspaceRouterMiddleware } from "./middleware"
3030
import { AppRuntime } from "@/effect/app-runtime"
3131

3232
export const InstanceRoutes = (upgrade: UpgradeWebSocket): Hono => {
33-
const app = new Hono()
34-
.use(WorkspaceRouterMiddleware(upgrade))
35-
.route("/project", ProjectRoutes())
36-
.route("/pty", PtyRoutes(upgrade))
37-
.route("/config", ConfigRoutes())
38-
.route("/experimental", ExperimentalRoutes())
39-
.route("/session", SessionRoutes())
40-
.route("/permission", PermissionRoutes())
33+
const app = new Hono().use(WorkspaceRouterMiddleware(upgrade))
4134

4235
if (Flag.OPENCODE_EXPERIMENTAL_HTTPAPI) {
4336
const handler = ExperimentalHttpApiServer.webHandler().handler
@@ -52,9 +45,17 @@ export const InstanceRoutes = (upgrade: UpgradeWebSocket): Hono => {
5245
app.get("/provider/auth", (c) => handler(c.req.raw, context))
5346
app.post("/provider/:providerID/oauth/authorize", (c) => handler(c.req.raw, context))
5447
app.post("/provider/:providerID/oauth/callback", (c) => handler(c.req.raw, context))
48+
app.get("/project", (c) => handler(c.req.raw, context))
49+
app.get("/project/current", (c) => handler(c.req.raw, context))
5550
}
5651

5752
return app
53+
.route("/project", ProjectRoutes())
54+
.route("/pty", PtyRoutes(upgrade))
55+
.route("/config", ConfigRoutes())
56+
.route("/experimental", ExperimentalRoutes())
57+
.route("/session", SessionRoutes())
58+
.route("/permission", PermissionRoutes())
5859
.route("/question", QuestionRoutes())
5960
.route("/provider", ProviderRoutes())
6061
.route("/sync", SyncRoutes())

packages/opencode/src/server/instance/project.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const ProjectRoutes = lazy(() =>
2323
description: "List of projects",
2424
content: {
2525
"application/json": {
26-
schema: resolver(Project.Info.array()),
26+
schema: resolver(Project.Info.zod.array()),
2727
},
2828
},
2929
},
@@ -45,7 +45,7 @@ export const ProjectRoutes = lazy(() =>
4545
description: "Current project information",
4646
content: {
4747
"application/json": {
48-
schema: resolver(Project.Info),
48+
schema: resolver(Project.Info.zod),
4949
},
5050
},
5151
},
@@ -66,7 +66,7 @@ export const ProjectRoutes = lazy(() =>
6666
description: "Project information after git initialization",
6767
content: {
6868
"application/json": {
69-
schema: resolver(Project.Info),
69+
schema: resolver(Project.Info.zod),
7070
},
7171
},
7272
},
@@ -99,7 +99,7 @@ export const ProjectRoutes = lazy(() =>
9999
description: "Updated project information",
100100
content: {
101101
"application/json": {
102-
schema: resolver(Project.Info),
102+
schema: resolver(Project.Info.zod),
103103
},
104104
},
105105
},

0 commit comments

Comments
 (0)