Skip to content

Commit cec16df

Browse files
authored
feat(core): add WorkspaceContext (#15409)
1 parent 114eb42 commit cec16df

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Context } from "../util/context"
2+
3+
interface Context {
4+
workspaceID?: string
5+
}
6+
7+
const context = Context.create<Context>("workspace")
8+
9+
export const WorkspaceContext = {
10+
async provide<R>(input: { workspaceID?: string; fn: () => R }): Promise<R> {
11+
return context.provide({ workspaceID: input.workspaceID }, async () => {
12+
return input.fn()
13+
})
14+
},
15+
16+
get workspaceID() {
17+
try {
18+
return context.use().workspaceID
19+
} catch (e) {
20+
return undefined
21+
}
22+
},
23+
}

packages/opencode/src/server/server.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { Auth } from "../auth"
2121
import { Flag } from "../flag/flag"
2222
import { Command } from "../command"
2323
import { Global } from "../global"
24+
import { WorkspaceContext } from "../control-plane/workspace-context"
2425
import { ProjectRoutes } from "./routes/project"
2526
import { SessionRoutes } from "./routes/session"
2627
import { PtyRoutes } from "./routes/pty"
@@ -194,6 +195,7 @@ export namespace Server {
194195
)
195196
.use(async (c, next) => {
196197
if (c.req.path === "/log") return next()
198+
const workspaceID = c.req.query("workspace") || c.req.header("x-opencode-workspace")
197199
const raw = c.req.query("directory") || c.req.header("x-opencode-directory") || process.cwd()
198200
const directory = (() => {
199201
try {
@@ -202,11 +204,17 @@ export namespace Server {
202204
return raw
203205
}
204206
})()
205-
return Instance.provide({
206-
directory,
207-
init: InstanceBootstrap,
207+
208+
return WorkspaceContext.provide({
209+
workspaceID,
208210
async fn() {
209-
return next()
211+
return Instance.provide({
212+
directory,
213+
init: InstanceBootstrap,
214+
async fn() {
215+
return next()
216+
},
217+
})
210218
},
211219
})
212220
})
@@ -223,7 +231,15 @@ export namespace Server {
223231
},
224232
}),
225233
)
226-
.use(validator("query", z.object({ directory: z.string().optional() })))
234+
.use(
235+
validator(
236+
"query",
237+
z.object({
238+
directory: z.string().optional(),
239+
workspace: z.string().optional(),
240+
}),
241+
),
242+
)
227243
.route("/project", ProjectRoutes())
228244
.route("/pty", PtyRoutes())
229245
.route("/config", ConfigRoutes())

0 commit comments

Comments
 (0)