Skip to content

Commit 5621373

Browse files
authored
fix(core): move instance middleware after control plane routes (#23150)
1 parent 8858256 commit 5621373

3 files changed

Lines changed: 46 additions & 39 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { Command } from "@/command"
1515
import { QuestionRoutes } from "./question"
1616
import { PermissionRoutes } from "./permission"
1717
import { Flag } from "@/flag/flag"
18+
import { WorkspaceID } from "@/control-plane/schema"
1819
import { ExperimentalHttpApiServer } from "./httpapi/server"
1920
import { ProjectRoutes } from "./project"
2021
import { SessionRoutes } from "./session"
@@ -27,9 +28,10 @@ import { ProviderRoutes } from "./provider"
2728
import { EventRoutes } from "./event"
2829
import { SyncRoutes } from "./sync"
2930
import { AppRuntime } from "@/effect/app-runtime"
31+
import { InstanceMiddleware } from "./middleware"
3032

31-
export const InstanceRoutes = (upgrade: UpgradeWebSocket): Hono => {
32-
const app = new Hono()
33+
export const InstanceRoutes = (upgrade: UpgradeWebSocket, workspaceID?: WorkspaceID): Hono => {
34+
const app = new Hono().use(InstanceMiddleware(workspaceID))
3335

3436
if (Flag.OPENCODE_EXPERIMENTAL_HTTPAPI) {
3537
const handler = ExperimentalHttpApiServer.webHandler().handler
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { MiddlewareHandler } from "hono"
2+
import { Instance } from "@/project/instance"
3+
import { InstanceBootstrap } from "@/project/bootstrap"
4+
import { AppRuntime } from "@/effect/app-runtime"
5+
import { AppFileSystem } from "@opencode-ai/shared/filesystem"
6+
import { WorkspaceContext } from "@/control-plane/workspace-context"
7+
import { WorkspaceID } from "@/control-plane/schema"
8+
9+
export function InstanceMiddleware(workspaceID?: WorkspaceID): MiddlewareHandler {
10+
return async (c, next) => {
11+
const raw = c.req.query("directory") || c.req.header("x-opencode-directory") || process.cwd()
12+
const directory = AppFileSystem.resolve(
13+
(() => {
14+
try {
15+
return decodeURIComponent(raw)
16+
} catch {
17+
return raw
18+
}
19+
})(),
20+
)
21+
22+
return WorkspaceContext.provide({
23+
workspaceID,
24+
async fn() {
25+
return Instance.provide({
26+
directory,
27+
init: () => AppRuntime.runPromise(InstanceBootstrap),
28+
async fn() {
29+
return next()
30+
},
31+
})
32+
},
33+
})
34+
}
35+
}

packages/opencode/src/server/server.ts

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import { generateSpecs } from "hono-openapi"
22
import { Hono } from "hono"
3-
import type { MiddlewareHandler } from "hono"
43
import { adapter } from "#hono"
54
import { lazy } from "@/util/lazy"
65
import { Log } from "@/util"
76
import { Flag } from "@/flag/flag"
8-
import { Instance } from "@/project/instance"
9-
import { InstanceBootstrap } from "@/project/bootstrap"
10-
import { AppRuntime } from "@/effect/app-runtime"
11-
import { AppFileSystem } from "@opencode-ai/shared/filesystem"
127
import { WorkspaceID } from "@/control-plane/schema"
13-
import { WorkspaceContext } from "@/control-plane/workspace-context"
148
import { MDNS } from "./mdns"
159
import { AuthMiddleware, CompressionMiddleware, CorsMiddleware, ErrorMiddleware, LoggerMiddleware } from "./middleware"
1610
import { FenceMiddleware } from "./fence"
@@ -48,47 +42,23 @@ function create(opts: { cors?: string[] }) {
4842

4943
const runtime = adapter.create(app)
5044

51-
function InstanceMiddleware(workspaceID?: WorkspaceID): MiddlewareHandler {
52-
return async (c, next) => {
53-
const raw = c.req.query("directory") || c.req.header("x-opencode-directory") || process.cwd()
54-
const directory = AppFileSystem.resolve(
55-
(() => {
56-
try {
57-
return decodeURIComponent(raw)
58-
} catch {
59-
return raw
60-
}
61-
})(),
62-
)
63-
64-
return WorkspaceContext.provide({
65-
workspaceID,
66-
async fn() {
67-
return Instance.provide({
68-
directory,
69-
init: () => AppRuntime.runPromise(InstanceBootstrap),
70-
async fn() {
71-
return next()
72-
},
73-
})
74-
},
75-
})
76-
}
77-
}
78-
7945
if (Flag.OPENCODE_WORKSPACE_ID) {
8046
return {
8147
app: app
82-
.use(InstanceMiddleware(Flag.OPENCODE_WORKSPACE_ID ? WorkspaceID.make(Flag.OPENCODE_WORKSPACE_ID) : undefined))
8348
.use(FenceMiddleware)
84-
.route("/", InstanceRoutes(runtime.upgradeWebSocket)),
49+
.route(
50+
"/",
51+
InstanceRoutes(
52+
runtime.upgradeWebSocket,
53+
Flag.OPENCODE_WORKSPACE_ID ? WorkspaceID.make(Flag.OPENCODE_WORKSPACE_ID) : undefined,
54+
),
55+
),
8556
runtime,
8657
}
8758
}
8859

8960
return {
9061
app: app
91-
.use(InstanceMiddleware())
9262
.route("/", ControlPlaneRoutes())
9363
.use(WorkspaceRouterMiddleware(runtime.upgradeWebSocket))
9464
.route("/", InstanceRoutes(runtime.upgradeWebSocket))

0 commit comments

Comments
 (0)