Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/opencode/src/control-plane/workspace-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Context } from "../util/context"

interface Context {
workspaceID?: string
}

const context = Context.create<Context>("workspace")

export const WorkspaceContext = {
async provide<R>(input: { workspaceID?: string; fn: () => R }): Promise<R> {
return context.provide({ workspaceID: input.workspaceID }, async () => {
return input.fn()
})
},

get workspaceID() {
try {
return context.use().workspaceID
} catch (e) {
return undefined
}
},
}
26 changes: 21 additions & 5 deletions packages/opencode/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Auth } from "../auth"
import { Flag } from "../flag/flag"
import { Command } from "../command"
import { Global } from "../global"
import { WorkspaceContext } from "../control-plane/workspace-context"
import { ProjectRoutes } from "./routes/project"
import { SessionRoutes } from "./routes/session"
import { PtyRoutes } from "./routes/pty"
Expand Down Expand Up @@ -194,6 +195,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 raw = c.req.query("directory") || c.req.header("x-opencode-directory") || process.cwd()
const directory = (() => {
try {
Expand All @@ -202,11 +204,17 @@ export namespace Server {
return raw
}
})()
return Instance.provide({
directory,
init: InstanceBootstrap,

return WorkspaceContext.provide({
workspaceID,
async fn() {
return next()
return Instance.provide({
directory,
init: InstanceBootstrap,
async fn() {
return next()
},
})
},
})
})
Expand All @@ -223,7 +231,15 @@ export namespace Server {
},
}),
)
.use(validator("query", z.object({ directory: z.string().optional() })))
.use(
validator(
"query",
z.object({
directory: z.string().optional(),
workspace: z.string().optional(),
}),
),
)
.route("/project", ProjectRoutes())
.route("/pty", PtyRoutes())
.route("/config", ConfigRoutes())
Expand Down
Loading