|
| 1 | +import type { MiddlewareHandler } from "hono" |
| 2 | +import { getAdaptor } from "@/control-plane/adaptors" |
| 3 | +import { WorkspaceID } from "@/control-plane/schema" |
| 4 | +import { Workspace } from "@/control-plane/workspace" |
| 5 | +import { lazy } from "@/util/lazy" |
| 6 | +import { Filesystem } from "@/util/filesystem" |
| 7 | +import { Instance } from "@/project/instance" |
| 8 | +import { InstanceBootstrap } from "@/project/bootstrap" |
| 9 | +import { InstanceRoutes } from "./instance" |
| 10 | + |
| 11 | +type Rule = { method?: string; path: string; exact?: boolean; action: "local" | "forward" } |
| 12 | + |
| 13 | +const RULES: Array<Rule> = [ |
| 14 | + { path: "/session/status", action: "forward" }, |
| 15 | + { method: "GET", path: "/session", action: "local" }, |
| 16 | +] |
| 17 | + |
| 18 | +function local(method: string, path: string) { |
| 19 | + for (const rule of RULES) { |
| 20 | + if (rule.method && rule.method !== method) continue |
| 21 | + const match = rule.exact ? path === rule.path : path === rule.path || path.startsWith(rule.path + "/") |
| 22 | + if (match) return rule.action === "local" |
| 23 | + } |
| 24 | + return false |
| 25 | +} |
| 26 | + |
| 27 | +const routes = lazy(() => InstanceRoutes()) |
| 28 | + |
| 29 | +export const WorkspaceRouterMiddleware: MiddlewareHandler = async (c) => { |
| 30 | + const raw = c.req.query("directory") || c.req.header("x-opencode-directory") || process.cwd() |
| 31 | + const directory = Filesystem.resolve( |
| 32 | + (() => { |
| 33 | + try { |
| 34 | + return decodeURIComponent(raw) |
| 35 | + } catch { |
| 36 | + return raw |
| 37 | + } |
| 38 | + })(), |
| 39 | + ) |
| 40 | + |
| 41 | + const url = new URL(c.req.url) |
| 42 | + const workspaceParam = url.searchParams.get("workspace") |
| 43 | + |
| 44 | + // TODO: If session is being routed, force it to lookup the |
| 45 | + // project/workspace |
| 46 | + |
| 47 | + // If no workspace is provided we use the "project" workspace |
| 48 | + if (!workspaceParam) { |
| 49 | + return Instance.provide({ |
| 50 | + directory, |
| 51 | + init: InstanceBootstrap, |
| 52 | + async fn() { |
| 53 | + return routes().fetch(c.req.raw, c.env) |
| 54 | + }, |
| 55 | + }) |
| 56 | + } |
| 57 | + |
| 58 | + const workspaceID = WorkspaceID.make(workspaceParam) |
| 59 | + const workspace = await Workspace.get(workspaceID) |
| 60 | + if (!workspace) { |
| 61 | + return new Response(`Workspace not found: ${workspaceID}`, { |
| 62 | + status: 500, |
| 63 | + headers: { |
| 64 | + "content-type": "text/plain; charset=utf-8", |
| 65 | + }, |
| 66 | + }) |
| 67 | + } |
| 68 | + |
| 69 | + // Handle local workspaces directly so we can pass env to `fetch`, |
| 70 | + // necessary for websocket upgrades |
| 71 | + if (workspace.type === "worktree") { |
| 72 | + return Instance.provide({ |
| 73 | + directory: workspace.directory!, |
| 74 | + init: InstanceBootstrap, |
| 75 | + async fn() { |
| 76 | + return routes().fetch(c.req.raw, c.env) |
| 77 | + }, |
| 78 | + }) |
| 79 | + } |
| 80 | + |
| 81 | + // Remote workspaces |
| 82 | + |
| 83 | + if (local(c.req.method, url.pathname)) { |
| 84 | + // No instance provided because we are serving cached data; there |
| 85 | + // is no instance to work with |
| 86 | + return routes().fetch(c.req.raw, c.env) |
| 87 | + } |
| 88 | + |
| 89 | + const adaptor = await getAdaptor(workspace.type) |
| 90 | + const headers = new Headers(c.req.raw.headers) |
| 91 | + headers.delete("x-opencode-workspace") |
| 92 | + |
| 93 | + return adaptor.fetch(workspace, `${url.pathname}${url.search}`, { |
| 94 | + method: c.req.method, |
| 95 | + body: c.req.method === "GET" || c.req.method === "HEAD" ? undefined : await c.req.raw.arrayBuffer(), |
| 96 | + signal: c.req.raw.signal, |
| 97 | + headers, |
| 98 | + }) |
| 99 | +} |
0 commit comments