Skip to content

Commit e137684

Browse files
authored
fix(openwork): use workspace opencode proxy path (anomalyco#1685)
Co-authored-by: src-opn <[email protected]>
1 parent ef660c1 commit e137684

4 files changed

Lines changed: 72 additions & 21 deletions

File tree

‎apps/app/src/app/lib/opencode.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,11 @@ async function postSessionRequest<T>(
139139
function resolveOpenworkWorkspaceMount(baseUrl: string): { baseUrl: string; workspaceId: string } | null {
140140
try {
141141
const url = new URL(baseUrl);
142-
const match = url.pathname.replace(/\/+$/, "").match(/^(.*\/w\/([^/]+))\/opencode$/);
143-
if (!match?.[1] || !match[2]) return null;
144-
url.pathname = match[1];
142+
const match = url.pathname
143+
.replace(/\/+$/, "")
144+
.match(/^(.*)\/(?:w|workspace)\/([^/]+)\/opencode$/);
145+
if (!match || match[1] === undefined || !match[2]) return null;
146+
url.pathname = match[1] || "/";
145147
url.search = "";
146148
return {
147149
baseUrl: url.toString().replace(/\/+$/, ""),

‎apps/app/src/app/lib/openwork-server.ts‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,17 @@ export function parseOpenworkWorkspaceIdFromUrl(input: string) {
336336
try {
337337
const url = new URL(normalized);
338338
const segments = url.pathname.split("/").filter(Boolean);
339-
const last = segments[segments.length - 1] ?? "";
340-
const prev = segments[segments.length - 2] ?? "";
341-
if (prev !== "w" || !last) return null;
342-
return decodeURIComponent(last);
339+
const legacyIndex = segments.indexOf("w");
340+
if (legacyIndex >= 0 && segments[legacyIndex + 1]) {
341+
return decodeURIComponent(segments[legacyIndex + 1]);
342+
}
343+
const workspaceIndex = segments.indexOf("workspace");
344+
if (workspaceIndex >= 0 && segments[workspaceIndex + 1]) {
345+
return decodeURIComponent(segments[workspaceIndex + 1]);
346+
}
347+
return null;
343348
} catch {
344-
const match = normalized.match(/\/w\/([^/?#]+)/);
349+
const match = normalized.match(/\/(?:w|workspace)\/([^/?#]+)/);
345350
if (!match?.[1]) return null;
346351
try {
347352
return decodeURIComponent(match[1]);
@@ -358,23 +363,27 @@ export function buildOpenworkWorkspaceBaseUrl(hostUrl: string, workspaceId?: str
358363
try {
359364
const url = new URL(normalized);
360365
const segments = url.pathname.split("/").filter(Boolean);
361-
const last = segments[segments.length - 1] ?? "";
362-
const prev = segments[segments.length - 2] ?? "";
363-
const alreadyMounted = prev === "w" && Boolean(last);
364-
if (alreadyMounted) {
366+
const workspaceIndex = segments.indexOf("workspace");
367+
const legacyIndex = segments.indexOf("w");
368+
const mountIndex = workspaceIndex >= 0 ? workspaceIndex : legacyIndex;
369+
if (mountIndex >= 0 && segments[mountIndex + 1]) {
370+
const prefix = segments.slice(0, mountIndex).join("/");
371+
url.pathname = `${prefix ? `/${prefix}` : ""}/workspace/${encodeURIComponent(
372+
decodeURIComponent(segments[mountIndex + 1]),
373+
)}`;
365374
return url.toString().replace(/\/+$/, "");
366375
}
367376

368377
const id = (workspaceId ?? "").trim();
369378
if (!id) return url.toString().replace(/\/+$/, "");
370379

371380
const basePath = url.pathname.replace(/\/+$/, "");
372-
url.pathname = `${basePath}/w/${encodeURIComponent(id)}`;
381+
url.pathname = `${basePath}/workspace/${encodeURIComponent(id)}`;
373382
return url.toString().replace(/\/+$/, "");
374383
} catch {
375384
const id = (workspaceId ?? "").trim();
376385
if (!id) return normalized;
377-
return `${normalized.replace(/\/+$/, "")}/w/${encodeURIComponent(id)}`;
386+
return `${normalized.replace(/\/+$/, "")}/workspace/${encodeURIComponent(id)}`;
378387
}
379388
}
380389

‎apps/server/src/server.ts‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,19 @@ function parseWorkspaceMount(pathname: string): { workspaceId: string; restPath:
170170
return { workspaceId: decodeURIComponent(workspaceId), restPath };
171171
}
172172

173+
function parseWorkspaceOpencodeMount(pathname: string): { workspaceId: string; restPath: string } | null {
174+
if (!pathname.startsWith("/workspace/")) return null;
175+
const remainder = pathname.slice("/workspace/".length);
176+
if (!remainder) return null;
177+
const slash = remainder.indexOf("/");
178+
if (slash === -1) return null;
179+
const workspaceId = remainder.slice(0, slash);
180+
const restPath = remainder.slice(slash) || "/";
181+
if (!workspaceId.trim()) return null;
182+
if (restPath !== "/opencode" && !restPath.startsWith("/opencode/")) return null;
183+
return { workspaceId: decodeURIComponent(workspaceId), restPath };
184+
}
185+
173186
function normalizeOpencodeProxyPath(proxyPath: string): string {
174187
const raw = (proxyPath ?? "").trim() || "/";
175188
const withoutPrefix = raw.startsWith("/opencode") ? raw.slice("/opencode".length) : raw;
@@ -263,12 +276,7 @@ export async function startServer(config: ServerConfig): Promise<ServeResult> {
263276
return wrapped;
264277
};
265278

266-
if (request.method === "OPTIONS") {
267-
return finalize(new Response(null, { status: 204 }));
268-
}
269-
270-
const mount = parseWorkspaceMount(url.pathname);
271-
if (mount && (mount.restPath === "/opencode" || mount.restPath.startsWith("/opencode/"))) {
279+
const proxyWorkspaceOpencodeMount = async (mount: { workspaceId: string; restPath: string }) => {
272280
authMode = "client";
273281
try {
274282
const actor = await requireClient(request, config, tokens);
@@ -285,6 +293,20 @@ export async function startServer(config: ServerConfig): Promise<ServeResult> {
285293
errorMessage = apiError.message;
286294
return finalize(jsonResponse(formatError(apiError), apiError.status));
287295
}
296+
};
297+
298+
if (request.method === "OPTIONS") {
299+
return finalize(new Response(null, { status: 204 }));
300+
}
301+
302+
const canonicalOpencodeMount = parseWorkspaceOpencodeMount(url.pathname);
303+
if (canonicalOpencodeMount) {
304+
return proxyWorkspaceOpencodeMount(canonicalOpencodeMount);
305+
}
306+
307+
const mount = parseWorkspaceMount(url.pathname);
308+
if (mount && (mount.restPath === "/opencode" || mount.restPath.startsWith("/opencode/"))) {
309+
return proxyWorkspaceOpencodeMount(mount);
288310
}
289311

290312
// Allow clients to use a mounted base URL (e.g. http://host:8787/w/<id>) while

‎apps/server/src/session-read-model.e2e.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ describe("workspace session read APIs", () => {
266266
});
267267

268268
const response = await Promise.race([
269-
fetch(`http://127.0.0.1:${openwork.server.port}/w/ws_1/opencode/session/ses_1/command`, {
269+
fetch(`http://127.0.0.1:${openwork.server.port}/workspace/ws_1/opencode/session/ses_1/command`, {
270270
method: "POST",
271271
headers: { ...auth(openwork.token), "Content-Type": "application/json" },
272272
body: JSON.stringify({ command: "review", arguments: "" }),
@@ -282,6 +282,24 @@ describe("workspace session read APIs", () => {
282282
expect(sawCommand).toBe(true);
283283
});
284284

285+
test("keeps legacy /w workspace opencode proxy alias", async () => {
286+
const workspaceRoot = await createWorkspaceRoot();
287+
const mock = startMockOpencode();
288+
const openwork = await startOpenworkServer({
289+
workspaceRoot,
290+
opencodeBaseUrl: `http://127.0.0.1:${mock.server.port}`,
291+
});
292+
293+
const response = await fetch(`http://127.0.0.1:${openwork.server.port}/w/ws_1/opencode/session`, {
294+
headers: auth(openwork.token),
295+
});
296+
297+
expect(response.status).toBe(200);
298+
const body = await response.json();
299+
expect(Array.isArray(body)).toBe(true);
300+
expect(mock.requests.some((request) => request.pathname === "/session")).toBe(true);
301+
});
302+
285303
test("returns 502 when OpenCode returns an invalid session list payload", async () => {
286304
const workspaceRoot = await createWorkspaceRoot();
287305
const mock = startMockOpencode({ invalidList: true });

0 commit comments

Comments
 (0)