Skip to content

Open source CapCut alternative.

TypeScript

fix(site): keep a chat turn's tools in the tab that ran its first [rebuild]

David Le28d94dfece1e
diff --git a/site/src/cut/lib/engineChat.ts b/site/src/cut/lib/engineChat.ts
index 85015f3e..84f7a49c 100644
--- a/site/src/cut/lib/engineChat.ts
+++ b/site/src/cut/lib/engineChat.ts
@@ -2,10 +2,15 @@ import type { UIMessage } from "ai";
import { localBackend } from "./backend/local";
import { pendingAsk, type JournalMark } from "./chatResume";
+/** This page among the tabs following a turn: the engine keeps a turn's tool
+ * calls in the page that claimed its first one, whose project copy holds
+ * what those calls made. */
+const PAGE_ID = crypto.randomUUID();
+
export async function claimEngineTool(sessionKey: string | null, toolCallId: string): Promise<boolean> {
const response = await localBackend.fetch("/api/cut/ai/tool-claim", {
method: "POST", headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ sessionKey, toolCallId }),
+ body: JSON.stringify({ sessionKey, toolCallId, page: PAGE_ID }),
});
if (!response.ok) throw new Error("The Mac app could not attach this editor to the chat. Update the app and reconnect.");
return (await response.json() as { claimed: boolean }).claimed;
diff --git a/site/src/cut/server/ai/bridge.ts b/site/src/cut/server/ai/bridge.ts
index a616713a..2035522c 100644
--- a/site/src/cut/server/ai/bridge.ts
+++ b/site/src/cut/server/ai/bridge.ts
@@ -15,6 +15,9 @@ interface Waiter {
resolve: (r: { output?: unknown; errorText?: string }) => void;
timer: ReturnType<typeof setTimeout>;
claimed: boolean;
+ /** Settles once a page claims the call or it ends unclaimed. */
+ taken: Promise<void>;
+ take: () => void;
toolName: string;
input: unknown;
}
@@ -25,6 +28,8 @@ interface Session {
/** Quality-gate asks the page has not answered yet, by gate id. */
gates: Map<string, (steer: string | null) => void>;
attached: boolean;
+ /** The page running this turn's tools: the first to claim one. */
+ owner?: string;
}
// Survives dev-server module reloads.
@@ -53,6 +58,7 @@ export function detachSession(key: string): void {
session.attached = false;
for (const waiter of session.waiters.values()) {
clearTimeout(waiter.timer);
+ waiter.take();
const projectId = sessionProjects.get(key);
if (!waiter.claimed && projectId)
void detachedTool(projectId, waiter.toolName, waiter.input).then(waiter.resolve);
@@ -67,6 +73,7 @@ export function unregisterSession(key: string) {
if (s) {
for (const w of s.waiters.values()) {
clearTimeout(w.timer);
+ w.take();
w.resolve({ errorText: "The chat request ended before the tool finished." });
}
settleGates(s);
@@ -139,9 +146,12 @@ export function callBrowserTool(
}
const toolCallId = crypto.randomUUID().slice(0, 12);
session.writer.write({ type: "tool-input-available", toolCallId, toolName, input });
+ let take!: () => void;
+ const taken = new Promise<void>((settle) => { take = settle; });
return new Promise((resolve) => {
const timer = setTimeout(() => {
session.waiters.delete(toolCallId);
+ take();
const errorText = `The editor did not answer the ${toolName} call in time.`;
console.warn(`[cut-ai] ${toolName}: editor tab did not answer within ${TOOL_TIMEOUT_MS}ms (session ${sessionKey})`);
session.writer.write({ type: "tool-output-error", toolCallId, errorText });
@@ -150,6 +160,8 @@ export function callBrowserTool(
session.waiters.set(toolCallId, {
timer,
claimed: false,
+ taken,
+ take,
toolName,
input,
resolve: (r) => {
@@ -164,10 +176,32 @@ export function callBrowserTool(
});
}
-export function claimBrowserTool(sessionKey: string, toolCallId: string): boolean {
- const waiter = sessions.get(sessionKey)?.waiters.get(toolCallId);
- if (!waiter || waiter.claimed) return false;
+// How long a call waits for the page running its turn before another page
+// may take it. That page claims within moments of the call; one that lets
+// this pass has closed or left the project.
+const OWNER_GRACE_MS = 2_000;
+
+/**
+ * Give a tool call to the page that asks for it. Every tab following a turn
+ * asks, and each edits its own copy of the project, so a turn's calls stay in
+ * the page that claimed its first one: a call landing in another tab would
+ * miss what the earlier calls made there. The turn moves only when that page
+ * lets a call go unclaimed. A page that names none (older editors) claims
+ * call by call.
+ */
+export async function claimBrowserTool(sessionKey: string, toolCallId: string, page?: string): Promise<boolean> {
+ const session = sessions.get(sessionKey);
+ const waiter = session?.waiters.get(toolCallId);
+ if (!session || !waiter || waiter.claimed) return false;
+ if (page && session.owner && session.owner !== page) {
+ let grace: ReturnType<typeof setTimeout> | undefined;
+ await Promise.race([waiter.taken, new Promise((resolve) => { grace = setTimeout(resolve, OWNER_GRACE_MS); })]);
+ clearTimeout(grace);
+ if (waiter.claimed || session.waiters.get(toolCallId) !== waiter) return false;
+ }
waiter.claimed = true;
+ waiter.take();
+ if (page) session.owner = page;
return true;
}
@@ -182,6 +216,7 @@ export function resolveBrowserTool(
if (!session || !waiter) return false;
session.waiters.delete(toolCallId);
clearTimeout(waiter.timer);
+ waiter.take();
waiter.resolve(result);
return true;
}
diff --git a/site/src/cut/server/ai/turnStreams.test.ts b/site/src/cut/server/ai/turnStreams.test.ts
index f017df0b..c20a0700 100644
--- a/site/src/cut/server/ai/turnStreams.test.ts
+++ b/site/src/cut/server/ai/turnStreams.test.ts
@@ -91,14 +91,45 @@ test("only one tab can claim a tool, and detached calls are not replayed", async
registerSession("session", { write: (chunk) => chunks.push(chunk) });
const pending = callBrowserTool("session", "get_state", {});
const id = String(chunks[0].toolCallId);
- expect(claimBrowserTool("session", id)).toBe(true);
- expect(claimBrowserTool("session", id)).toBe(false);
+ expect(await claimBrowserTool("session", id)).toBe(true);
+ expect(await claimBrowserTool("session", id)).toBe(false);
detachSession("session");
expect((await pending).errorText).toContain("disconnected");
- expect(claimBrowserTool("session", id)).toBe(false);
+ expect(await claimBrowserTool("session", id)).toBe(false);
unregisterSession("session");
});
+test("a turn's tools all run in the page that claimed its first one", async () => {
+ // Two tabs follow the same turn; each edits its own copy of the project,
+ // so a call landing in the other tab would miss what the first one made.
+ const chunks: Record<string, unknown>[] = [];
+ registerSession("pinned", { write: (chunk) => chunks.push(chunk) });
+ void callBrowserTool("pinned", "import_url", {});
+ const first = String(chunks[0].toolCallId);
+ expect(await claimBrowserTool("pinned", first, "page-a")).toBe(true);
+ expect(await claimBrowserTool("pinned", first, "page-b")).toBe(false);
+ void callBrowserTool("pinned", "detect_silence", {});
+ const next = String(chunks[1].toolCallId);
+ // The other tab asks first; the call still waits for the tab that runs the turn.
+ const other = claimBrowserTool("pinned", next, "page-b");
+ expect(await claimBrowserTool("pinned", next, "page-a")).toBe(true);
+ expect(await other).toBe(false);
+ unregisterSession("pinned");
+});
+
+test("a turn's tools move to another tab when the claiming one stops answering", async () => {
+ const chunks: Record<string, unknown>[] = [];
+ registerSession("moved", { write: (chunk) => chunks.push(chunk) });
+ void callBrowserTool("moved", "get_state", {});
+ expect(await claimBrowserTool("moved", String(chunks[0].toolCallId), "page-a")).toBe(true);
+ void callBrowserTool("moved", "get_state", {});
+ void callBrowserTool("moved", "find_media", {});
+ // page-a closed: page-b takes the call once it has waited, and the turn with it.
+ expect(await claimBrowserTool("moved", String(chunks[1].toolCallId), "page-b")).toBe(true);
+ expect(await claimBrowserTool("moved", String(chunks[2].toolCallId), "page-b")).toBe(true);
+ unregisterSession("moved");
+});
+
test("the page answers the quality gate, and a detached page lets the turn close", async () => {
const chunks: Record<string, unknown>[] = [];
registerSession("gate", { write: (chunk) => chunks.push(chunk) });
@@ -143,7 +174,7 @@ test("an unclaimed tool transfers to the engine when the tab disconnects", async
const result = callBrowserTool("unclaimed", "set_project_name", { name: "Transferred" });
const id = String(chunks[0].toolCallId);
detachSession("unclaimed");
- expect(claimBrowserTool("unclaimed", id)).toBe(false);
+ expect(await claimBrowserTool("unclaimed", id)).toBe(false);
expect((await result).errorText).toBe(undefined);
expect((await readProject(project.id))?.name).toBe("Transferred");
expect(chunks.filter((chunk) => chunk.toolCallId === id).map((chunk) => chunk.type)).toEqual(["tool-input-available", "tool-output-available"]);
diff --git a/site/src/cut/server/http/ai.ts b/site/src/cut/server/http/ai.ts
index 827cff7a..10b34685 100644
--- a/site/src/cut/server/http/ai.ts
+++ b/site/src/cut/server/http/ai.ts
@@ -679,9 +679,13 @@ export const aiApi = {
},
async claimTool(req: Request) {
- const body = z.object({ sessionKey: z.string().min(1).max(200), toolCallId: z.string().min(1).max(200) }).safeParse(await req.json().catch(() => null));
+ const body = z.object({
+ sessionKey: z.string().min(1).max(200),
+ toolCallId: z.string().min(1).max(200),
+ page: z.string().min(1).max(200).optional(),
+ }).safeParse(await req.json().catch(() => null));
if (!body.success) return Response.json({ error: "A chat session and tool call are required." }, { status: 400 });
- return Response.json({ claimed: claimBrowserTool(body.data.sessionKey, body.data.toolCallId) });
+ return Response.json({ claimed: await claimBrowserTool(body.data.sessionKey, body.data.toolCallId, body.data.page) });
},
async models() {