mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-04-23 00:54:43 +00:00
feat(core): add WorkspaceContext (#15409)
This commit is contained in:
23
packages/opencode/src/control-plane/workspace-context.ts
Normal file
23
packages/opencode/src/control-plane/workspace-context.ts
Normal file
@@ -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
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -21,6 +21,7 @@ import { Auth } from "../auth"
|
|||||||
import { Flag } from "../flag/flag"
|
import { Flag } from "../flag/flag"
|
||||||
import { Command } from "../command"
|
import { Command } from "../command"
|
||||||
import { Global } from "../global"
|
import { Global } from "../global"
|
||||||
|
import { WorkspaceContext } from "../control-plane/workspace-context"
|
||||||
import { ProjectRoutes } from "./routes/project"
|
import { ProjectRoutes } from "./routes/project"
|
||||||
import { SessionRoutes } from "./routes/session"
|
import { SessionRoutes } from "./routes/session"
|
||||||
import { PtyRoutes } from "./routes/pty"
|
import { PtyRoutes } from "./routes/pty"
|
||||||
@@ -194,6 +195,7 @@ export namespace Server {
|
|||||||
)
|
)
|
||||||
.use(async (c, next) => {
|
.use(async (c, next) => {
|
||||||
if (c.req.path === "/log") return 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 raw = c.req.query("directory") || c.req.header("x-opencode-directory") || process.cwd()
|
||||||
const directory = (() => {
|
const directory = (() => {
|
||||||
try {
|
try {
|
||||||
@@ -202,11 +204,17 @@ export namespace Server {
|
|||||||
return raw
|
return raw
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
return Instance.provide({
|
|
||||||
directory,
|
return WorkspaceContext.provide({
|
||||||
init: InstanceBootstrap,
|
workspaceID,
|
||||||
async fn() {
|
async fn() {
|
||||||
return next()
|
return Instance.provide({
|
||||||
|
directory,
|
||||||
|
init: InstanceBootstrap,
|
||||||
|
async fn() {
|
||||||
|
return next()
|
||||||
|
},
|
||||||
|
})
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -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("/project", ProjectRoutes())
|
||||||
.route("/pty", PtyRoutes())
|
.route("/pty", PtyRoutes())
|
||||||
.route("/config", ConfigRoutes())
|
.route("/config", ConfigRoutes())
|
||||||
|
|||||||
Reference in New Issue
Block a user