diff --git a/app/src/shell/shell.tsx b/app/src/shell/shell.tsx index 6e102d95..afca1b71 100644 --- a/app/src/shell/shell.tsx +++ b/app/src/shell/shell.tsx @@ -2,6 +2,7 @@ import { createEffect, createSignal, onCleanup, Show, untrack } from "solid-js"; import type { JSX } from "@solidjs/web"; import { Outlet, type AnyRouter } from "@tanstack/solid-router"; import { RegionProvider, ShortcutContext, usePageTitleValue, useRegionFilled } from "@tokamak/sdk/client"; +import { createApi, type FunctionReference, type Id } from "@tokamak/sdk"; import { Button, icons } from "@tokamak/ui"; import { checkAccess, reach } from "../host/reach"; import { createServers, defaultServers, thisMac, type Servers } from "../host/servers"; @@ -16,6 +17,10 @@ import { createSession, type Session } from "./session"; import { createShortcuts, shortcutName } from "./shortcuts"; import { createTheme } from "./theme"; +const chatOpen = createApi<{ + open: { latest: FunctionReference<"query", {}, { _id: Id<"openRequests">; expiresAt: number } | null> }; +}>("chat").open; + export interface ShellProps { routing: PluginRouting; getEndpoint?: () => ServerEndpoint | Promise; @@ -88,6 +93,31 @@ export function createShellState( return [...shellPageList, ...(connection.state === "connected" ? connection.plugins.views() : [])]; }; const navigation = createNavigation(router, views, () => session.connection().state === "connected"); + createEffect( + () => { + const connection = session.connection(); + const chat = views().find((view) => view.route === "/p/chat"); + const path = navigation.pathname(); + return { connection, chat, connecting: path === shellPages.connect.route || path === "/join" }; + }, + ({ connection, chat, connecting }) => { + if (connection.state !== "connected" || !chat || connecting) return; + let opened: Id<"openRequests"> | undefined; + return connection.client.subscribe(chatOpen.latest, {}, (event) => { + if ( + "error" in event || + !event.value || + event.value.expiresAt <= Date.now() || + event.value._id === opened + ) + return; + const path = navigation.pathname(); + if (path === shellPages.connect.route || path === "/join") return; + opened = event.value._id; + if (navigation.current()?.route !== chat.route) navigation.openView(chat); + }); + }, + ); const shortcuts = createShortcuts(); const [paletteOpen, setPaletteOpen] = createSignal(false); const [serverMenuOpen, setServerMenuOpen] = createSignal(false); diff --git a/e2e/browser/chat.spec.ts b/e2e/browser/chat.spec.ts index e0fdad19..237eda99 100644 --- a/e2e/browser/chat.spec.ts +++ b/e2e/browser/chat.spec.ts @@ -1,4 +1,15 @@ import { test, expect, type Page } from "@playwright/test"; +import { execFile } from "node:child_process"; +import { mkdir, realpath } from "node:fs/promises"; +import { basename } from "node:path"; +import { promisify } from "node:util"; +import { + createApi, + TokamakClient, + WebSocketTransport, + type FunctionReference, + type JsonValue, +} from "../../packages/sdk/src/index"; // The real server runs chat and ACP; ACP finds only the scripted agent from // e2e/fixtures/scripted-agent.py, so replies, tool steps and permissions are real @@ -176,3 +187,142 @@ test("switching the thread agent keeps each earlier reply labelled with the agen await expect(turns.nth(3).getByText(/Tokamak workspace/)).toBeVisible(); await expect(turns.nth(1)).toHaveAccessibleName("Claude"); }); + +async function openFromCli(folder: string, args: string[], env: NodeJS.ProcessEnv) { + const { stdout, stderr } = await promisify(execFile)( + "proto", + ["run", "bun", "--", "run", "tokamak", "open", folder, "--json", ...args], + { env: { ...process.env, TOKAMAK_SERVER: undefined, TOKAMAK_TOKEN: undefined, ...env }, timeout: 45_000 }, + ); + return { result: JSON.parse(stdout), text: stderr }; +} + +test("tokamak open switches a connected window to the thread and sets the default agent folder", async ({ + page, +}, testInfo) => { + const serverDir = process.env.TOKAMAK_SERVER_DIR; + if (!serverDir || !process.env.TOKAMAK_E2E_WEB_ROOT) + throw new Error("Run this test through the browser suite's isolated server harness."); + const folder = testInfo.outputPath("working folder"); + await mkdir(folder, { recursive: true }); + + await openChat(page); + await expect(page.getByRole("textbox", { name: "Message" })).toBeEnabled(); + const chat = page.getByRole("region", { name: "Chat" }); + const previous = await chat.getAttribute("data-thread-id"); + await page.getByRole("link", { name: "FixtureView", exact: true }).click(); + await expect(chat).toBeHidden(); + + const { result: opened, text } = await openFromCli(folder, ["--new"], { TOKAMAK_SERVER_DIR: serverDir }); + expect(opened, text).toMatchObject({ + ok: true, + created: true, + folder: await realpath(folder), + folderRecorded: true, + agent: { plugin: "acp", backendId: "claude" }, + windowConnected: true, + shown: "acknowledged", + }); + expect(opened.threadId).not.toBe(previous); + await expect(page).toHaveURL(/\/p\/chat$/); + await expect(chat).toHaveAttribute("data-thread-id", opened.threadId); + await expect(page.getByRole("button", { name: /^Claude(?:\s|$)/ })).toContainText(basename(folder)); +}); + +test("tokamak open leaves the agent and folder to the window on a shared server", async ({}, testInfo) => { + const token = process.env.TOKAMAK_E2E_HOSTED_TOKEN; + if (!token) throw new Error("Run this test through the browser suite's isolated server harness."); + const folder = testInfo.outputPath("shared thread folder"); + await mkdir(folder, { recursive: true }); + + const address = "http://127.0.0.1:43124"; + const open = () => openFromCli(folder, ["--server", address], { TOKAMAK_TOKEN: token }); + const first = await open(); + const second = await open(); + for (const { result, text } of [first, second]) { + expect(result, text).toMatchObject({ + ok: true, + created: true, + server: address, + windowConnected: false, + shown: "pending", + agent: null, + folderRecorded: false, + }); + expect(text).toContain("Every member can see it"); + expect(text).toContain("the folder was not recorded"); + expect(text).toContain("running this command again creates another"); + } + expect(first.result.threadId).not.toBe(second.result.threadId); + const ws = new URL("/ws", address); + ws.protocol = "ws:"; + ws.searchParams.set("token", token); + const socket = new WebSocket(ws); + await new Promise((done, fail) => { + socket.addEventListener("open", () => done(), { once: true }); + socket.addEventListener("error", () => fail(new Error("Could not connect to the shared server")), { + once: true, + }); + }); + const transport = new WebSocketTransport(socket); + try { + const api = createApi<{ threads: { list: FunctionReference<"query", {}, JsonValue[]> } }>("chat"); + const threads = await new TokamakClient(transport).query(api.threads.list, {}); + for (const threadId of [first.result.threadId, second.result.threadId]) { + const row = threads.find( + (thread) => thread && typeof thread === "object" && "_id" in thread && thread._id === threadId, + ); + expect(row).toBeDefined(); + expect(row).not.toHaveProperty("backend"); + expect(row).not.toHaveProperty("starterFolder"); + expect(JSON.stringify(row)).not.toContain(await realpath(folder)); + } + } finally { + transport.close(); + } +}); + +test("tokamak open preserves the connect form and shows the latest request when it closes", async ({ + page, +}, testInfo) => { + const serverDir = process.env.TOKAMAK_SERVER_DIR; + if (!serverDir || !process.env.TOKAMAK_E2E_WEB_ROOT) + throw new Error("Run through the isolated browser harness"); + const folder = testInfo.outputPath("connect working folder"); + await mkdir(folder, { recursive: true }); + await openChat(page); + await page.goto("/connect"); + const address = page.getByRole("textbox", { name: "Server address" }); + await address.fill("unsaved.example"); + const first = await openFromCli(folder, ["--new"], { TOKAMAK_SERVER_DIR: serverDir }); + expect(first.result.shown).toBe("pending"); + await expect(page).toHaveURL(/\/connect$/); + await expect(address).toHaveValue("unsaved.example"); + const second = await openFromCli(folder, ["--new"], { TOKAMAK_SERVER_DIR: serverDir }); + expect(second.result.shown).toBe("pending"); + await expect(address).toHaveValue("unsaved.example"); + await page + .getByRole("navigation", { name: "Views" }) + .getByRole("link", { name: "Chat", exact: true }) + .click(); + await expect(page.getByRole("region", { name: "Chat" })).toHaveAttribute( + "data-thread-id", + second.result.threadId, + ); +}); + +test("a window ignores an open request that has expired by the time it is delivered", async ({ + page, +}, testInfo) => { + const serverDir = process.env.TOKAMAK_SERVER_DIR; + if (!serverDir || !process.env.TOKAMAK_E2E_WEB_ROOT) + throw new Error("Run through the isolated browser harness"); + const folder = testInfo.outputPath("expired working folder"); + await mkdir(folder, { recursive: true }); + await openChat(page); + await page.getByRole("link", { name: "FixtureView", exact: true }).click(); + await page.clock.setFixedTime(new Date(Date.now() + 6 * 60_000)); + const { result } = await openFromCli(folder, ["--new"], { TOKAMAK_SERVER_DIR: serverDir }); + expect(result.shown).toBe("pending"); + await expect(page).toHaveURL(/\/p\/integration-fixture$/); +}); diff --git a/packages/cli/src/commands.ts b/packages/cli/src/commands.ts index c914d9fc..999f6ac6 100644 --- a/packages/cli/src/commands.ts +++ b/packages/cli/src/commands.ts @@ -3,5 +3,6 @@ import build from "./commands/build"; import add from "./commands/add"; import remove from "./commands/remove"; import list from "./commands/list"; +import open from "./commands/open"; -export const commands: readonly Command[] = [build, add, remove, list]; +export const commands: readonly Command[] = [build, add, remove, list, open]; diff --git a/packages/cli/src/commands/open.ts b/packages/cli/src/commands/open.ts new file mode 100644 index 00000000..1aa5a934 --- /dev/null +++ b/packages/cli/src/commands/open.ts @@ -0,0 +1,134 @@ +import { realpath, stat } from "node:fs/promises"; +import { createApi, type FunctionReference } from "../../../sdk/src/api"; +import { TokamakClient } from "../../../sdk/src/client"; +import { WebSocketTransport } from "../../../sdk/src/transport"; +import type { Id } from "../../../sdk/src/values"; +import { CommandError, type Command } from "../command"; +import { matchesLocalServer, withServerEndpoint } from "../server"; + +type Agent = { plugin: string; backendId: string }; +type OpenResult = { + threadId: Id<"threads">; + requestId: Id<"openRequests">; + created: boolean; + agent: Agent | null; + folderRecorded: boolean; + local: boolean; +}; +type Shown = "acknowledged" | "pending" | "replaced"; +const requests = createApi<{ + open: { + request: FunctionReference<"action", { folder?: string; alwaysNew?: boolean }, OpenResult>; + status: FunctionReference<"query", { requestId: OpenResult["requestId"] }, Shown>; + }; +}>("chat").open; +const account = createApi<{ + tokamak: { whoami: FunctionReference<"query", {}, { id: string }> }; +}>().tokamak; + +async function connect(url: URL, token: string) { + const ws = new URL("/ws", url); + ws.protocol = url.protocol === "https:" ? "wss:" : "ws:"; + const socket = new WebSocket(ws, { headers: { Authorization: `Bearer ${token}` } }); + const signal = AbortSignal.timeout(5000); + try { + await new Promise((ready, fail) => { + socket.addEventListener("open", () => ready(), { once: true }); + socket.addEventListener("error", () => fail(new Error("Could not connect")), { once: true }); + signal.addEventListener("abort", () => fail(new Error("Connection timed out")), { once: true }); + }); + return new WebSocketTransport(socket); + } catch (error) { + socket.close(); + throw error; + } +} + +export async function waitForWindow( + client: TokamakClient, + requestId: OpenResult["requestId"], + timeoutMs = 2000, +): Promise { + let stop: (() => void) | undefined; + let timeout: ReturnType | undefined; + try { + return await new Promise((done) => { + timeout = setTimeout(() => done("pending"), timeoutMs); + stop = client.subscribe(requests.status, { requestId }, (event) => { + if ("error" in event) done("pending"); + else if (event.value !== "pending") done(event.value); + }); + }); + } catch { + return "pending"; + } finally { + clearTimeout(timeout); + stop?.(); + } +} + +export default { + path: "open", + usesServer: true, + summary: + "Open a thread in a folder. Only an explicitly set folder counts for reuse; the agent’s default home does not. Use --server
or TOKAMAK_SERVER with TOKAMAK_TOKEN for another server; saved names are only available in the window.", + args: { name: "folder", optional: true }, + flags: { new: { summary: "Always create a new thread." } }, + example: "tokamak open . --json", + async run([folderArg], { server, flags }) { + const path = folderArg ?? "."; + const folder = await realpath(path).catch((error: NodeJS.ErrnoException) => { + if (error.code === "ENOENT" || error.code === "ENOTDIR") + throw new CommandError(`Folder does not exist: ${path}`, "tokamak open --help"); + throw error; + }); + if (!(await stat(folder)).isDirectory()) + throw new CommandError(`Not a folder: ${folder}`, "tokamak open --help"); + + let transport: WebSocketTransport | undefined; + let timeout: ReturnType | undefined; + return withServerEndpoint(server, "open a thread", async (endpoint) => { + try { + const localEndpoint = endpoint.local || (await matchesLocalServer(endpoint.url)); + transport = await connect(endpoint.url, endpoint.token); + const client = new TokamakClient(transport); + const connection = transport; + timeout = setTimeout(() => connection.close(), 30_000); + const user = await client.query(account.whoami, {}); + const isLocal = user.id === "local" && localEndpoint; + const { threadId, requestId, created, agent, folderRecorded, local } = await client.action( + requests.request, + isLocal ? { folder, alwaysNew: flags.new === true } : {}, + ); + clearTimeout(timeout); + const shown = await waitForWindow(client, requestId); + const windowConnected = shown === "acknowledged"; + let text = local + ? `Thread ${threadId} exists.` + : `Created thread ${threadId} on a shared server. Every member can see it. Since the folder was not recorded, running this command again creates another thread. Choose an agent and folder in the window.`; + if (shown === "acknowledged") text += " A window showed it."; + else if (shown === "replaced") text += " A newer request replaced the request to show it."; + else + text += + " The request is pending; a window that opens within 5 minutes will show it if it is still pending."; + if (local && !agent) text += " Choose an agent in the window."; + return { + data: { + threadId, + created, + folder, + server: endpoint.url.origin, + windowConnected, + shown, + agent, + folderRecorded, + }, + text, + }; + } finally { + clearTimeout(timeout); + transport?.close(); + } + }); + }, +} satisfies Command; diff --git a/packages/cli/src/server.ts b/packages/cli/src/server.ts index a2b74cbd..b21ba654 100644 --- a/packages/cli/src/server.ts +++ b/packages/cli/src/server.ts @@ -49,7 +49,7 @@ async function readToken(folder: string) { return token; } -async function localEndpoint() { +async function localEndpoint(expectedOrigin?: string) { const folder = dataFolder(); const identity = discovery(JSON.parse(await readFile(join(folder, "server.json"), "utf8"))); const ws = new URL(identity.url); @@ -64,8 +64,10 @@ async function localEndpoint() { !(ws.hostname === "[::1]" || (isIP(ws.hostname) === 4 && ws.hostname.startsWith("127."))) ) throw new Error("Invalid local server discovery address"); + const url = httpAddress(identity.url); + if (expectedOrigin && expectedOrigin !== url.origin) throw new Error("Not the local server"); const token = await readToken(folder); - const endpoint = { url: httpAddress(identity.url), token }; + const endpoint = { url, token, local: true }; const health = discovery(await get("/health", endpoint)); if (JSON.stringify(health) !== JSON.stringify(identity)) throw new Error("Local server identity changed"); return endpoint; @@ -90,15 +92,25 @@ async function get(path: string, endpoint: { url: URL; token: string }): Promise } } -export async function serverGet(path: string, explicit?: string): Promise { +export async function matchesLocalServer(url: URL) { + if (!(url.hostname === "[::1]" || (isIP(url.hostname) === 4 && url.hostname.startsWith("127.")))) + return false; + return Boolean(await localEndpoint(url.origin).catch(() => null)); +} + +export async function withServerEndpoint( + explicit: string | undefined, + operation: string, + use: (endpoint: { url: URL; token: string; local?: boolean }) => Promise, +): Promise { const address = explicit ?? process.env.TOKAMAK_SERVER; if (!address) { const endpoint = await localEndpoint().catch(() => { throw new CommandError("No local server is running", "tokamak server start"); }); - return get(path, endpoint).catch((error: Error) => { + return use(endpoint).catch((error: unknown) => { throw new CommandError( - `Could not read ${path} from the local server: ${error.message}`, + `Could not ${operation} from the local server: ${error instanceof Error ? error.message : "Request failed"}`, "tokamak server start", ); }); @@ -106,11 +118,15 @@ export async function serverGet(path: string, explicit?: string): Promise { + return withServerEndpoint(explicit, `read ${path}`, (endpoint) => get(path, endpoint)); +} diff --git a/packages/cli/tests/cli-fixture.mjs b/packages/cli/tests/cli-fixture.mjs index 87da6efb..db161c9c 100644 --- a/packages/cli/tests/cli-fixture.mjs +++ b/packages/cli/tests/cli-fixture.mjs @@ -1,9 +1,9 @@ import { join } from "node:path"; import { workspace } from "../../build/tests/outside-fixture.mjs"; -export function cli(root, args, extra = {}) { - const result = Bun.spawnSync([process.execPath, "run", "--silent", "tokamak", ...args], { - cwd: workspace, +export function cli(root, args, extra = {}, cwd = workspace) { + const result = Bun.spawnSync([process.execPath, join(workspace, "packages/cli/src/main.ts"), ...args], { + cwd, timeout: 60_000, env: { ...process.env, diff --git a/packages/cli/tests/cli.test.mjs b/packages/cli/tests/cli.test.mjs index 41703a4d..1c1731c7 100644 --- a/packages/cli/tests/cli.test.mjs +++ b/packages/cli/tests/cli.test.mjs @@ -1,9 +1,11 @@ import { expect, test } from "bun:test"; -import { chmod, cp, mkdir, readFile, writeFile } from "node:fs/promises"; -import { join, resolve } from "node:path"; +import { chmod, cp, mkdir, readFile, symlink, writeFile } from "node:fs/promises"; +import { join, relative, resolve } from "node:path"; import { randomBytes } from "node:crypto"; -import { withPlugin, workspace } from "../../build/tests/outside-fixture.mjs"; +import { build, withPlugin, workspace } from "../../build/tests/outside-fixture.mjs"; import { cli } from "./cli-fixture.mjs"; +import { prepareChatSandbox } from "../../../e2e/chat-sandbox"; +import { createApi, TokamakClient, WebSocketTransport } from "../../sdk/src/index"; test("adds and removes folders and protects an existing id", async () => { await withPlugin(async (plugin, root) => { @@ -96,7 +98,7 @@ test("every registered command shares help and JSON output rules", async () => { }); }, 120_000); -async function withServer(root, check) { +async function withServer(root, check, env = {}) { const data = join(root, "data"); const plugins = join(root, "installed"); await mkdir(plugins, { recursive: true }); @@ -113,7 +115,7 @@ async function withServer(root, check) { ); const server = Bun.spawn([binary, "--local-dir", data, "--plugins", plugins], { cwd: root, - env: { ...process.env, TOKAMAK_WEB_DIR: undefined }, + env: { ...process.env, TOKAMAK_WEB_DIR: undefined, ...env }, stdout: "ignore", timeout: 110_000, stderr: "pipe", @@ -329,3 +331,139 @@ test("stale discovery sends no token and mismatched health cannot supply a catal } }); }, 120_000); + +test("open reports a missing folder and explains how to start a missing local server", async () => { + await withPlugin((folder, root) => { + const invalid = cli(root, ["open", join(root, "missing"), "--json"]); + expect(invalid.code).not.toBe(0); + expect(JSON.parse(invalid.stdout).error.message).toContain("Folder does not exist"); + const plain = cli(root, ["open", join(root, "missing")]); + expect(plain.code).not.toBe(0); + expect(plain.stdout).toBe(""); + expect(plain.stderr).toContain("Folder does not exist"); + expect(invalid.stderr).toContain("Folder does not exist"); + const offline = cli(root, ["open", folder, "--json"]); + expect(offline.code).not.toBe(0); + expect(JSON.parse(offline.stdout).error.next).toBe("tokamak server start"); + }); +}); + +test("open remembers the folder on a real local server without an agent or window", async () => { + await withPlugin(async (folder, root) => { + const chat = join(workspace, "plugins/chat"); + const built = build(chat); + expect(built.code, built.output).toBe(0); + await mkdir(join(root, "installed"), { recursive: true }); + await cp(join(chat, "dist"), join(root, "installed", "chat"), { recursive: true }); + await withServer(root, (address, token) => { + const open = (args, cwd = workspace, env = {}) => { + const result = cli(root, ["open", ...args, "--json"], env, cwd); + expect(result.code, result.stderr).toBe(0); + expect(result.stderr).toContain("opens within 5 minutes"); + expect(result.stderr).toContain("Choose an agent"); + return JSON.parse(result.stdout); + }; + const first = open([folder]); + expect(first).toEqual({ + ok: true, + threadId: expect.any(String), + created: true, + folder, + server: address, + windowConnected: false, + shown: "pending", + agent: null, + folderRecorded: true, + }); + const reused = { ...first, created: false }; + expect(open([relative(root, folder)], root)).toEqual(reused); + expect(open(["."], folder)).toEqual(reused); + expect(open([], folder, { TOKAMAK_SERVER: address, TOKAMAK_TOKEN: token })).toEqual(reused); + + const fresh = open([folder, "--new", "--server", address], workspace, { TOKAMAK_TOKEN: token }); + expect(fresh).toEqual({ ...first, threadId: expect.any(String) }); + expect(fresh.threadId).not.toBe(first.threadId); + expect(open([folder])).toEqual({ ...fresh, created: false }); + }); + }); +}, 180_000); + +const chatApi = createApi("chat"); + +test("open resolves a symlink before traversing its parent and rejects a redirected agent folder", async () => { + await withPlugin(async (_, root) => { + const sandbox = await prepareChatSandbox(root, { build: true }); + await cp(join(root, "plugins"), join(root, "installed"), { recursive: true }); + const outer = join(root, "outer"); + const child = join(outer, "child"); + const link = join(root, "link"); + const spaced = join(root, "name "); + await mkdir(child, { recursive: true }); + await symlink(child, link, "dir"); + await mkdir(spaced); + await mkdir(spaced.trimEnd()); + await withServer( + root, + async (address, token) => { + const socket = new WebSocket(address.replace(/^http/, "ws") + "/ws", { + headers: { Authorization: `Bearer ${token}` }, + }); + await new Promise((done, fail) => { + socket.addEventListener("open", done, { once: true }); + socket.addEventListener("error", fail, { once: true }); + }); + const transport = new WebSocketTransport(socket); + const client = new TokamakClient(transport); + try { + const opened = cli(root, ["open", link + "/..", "--json"]); + expect(opened.code, opened.stderr).toBe(0); + expect(JSON.parse(opened.stdout).folder).toBe(outer); + const before = await client.query(chatApi.threads.list, {}); + const invalid = cli(root, ["open", spaced, "--new", "--json"]); + expect(invalid.code).not.toBe(0); + expect(JSON.parse(invalid.stdout).error.message).toContain("different folder"); + expect(await client.query(chatApi.threads.list, {})).toEqual(before); + + const threadId = await client.action(chatApi.threads.create, {}); + const agent = { plugin: "acp", backendId: "claude" }; + const settings = await client.action(chatApi.backends.configure, { + ...agent, + settings: { cwd: link + "/" }, + }); + await client.mutation(chatApi.threads.setBackend, { + threadId, + backend: { kind: "plugin", ...agent, settings }, + }); + const reused = cli(root, ["open", child, "--json"]); + expect(reused.code, reused.stderr).toBe(0); + expect(JSON.parse(reused.stdout)).toMatchObject({ threadId, created: false }); + await expect( + client.mutation(chatApi.open.record, { threadId, backend: { settings } }), + ).rejects.toThrow(); + expect(await client.query(chatApi.threads.list, {})).toHaveLength(before.length + 1); + const previous = await client.query(chatApi.open.latest, {}); + const newer = await client.action(chatApi.open.request, { folder: child }); + expect(await client.query(chatApi.open.status, { requestId: previous._id })).toBe("replaced"); + expect(await client.query(chatApi.open.status, { requestId: newer.requestId })).toBe("pending"); + await client.mutation(chatApi.open.shown, { requestId: newer.requestId }); + expect(await client.query(chatApi.open.latest, {})).toBeNull(); + expect(await client.query(chatApi.open.status, { requestId: newer.requestId })).toBe( + "acknowledged", + ); + + const starterId = await client.mutation(chatApi.threads.insert, { starterFolder: child }); + await client.mutation(chatApi.threads.setBackend, { + threadId: starterId, + backend: { kind: "plugin", ...agent, settings }, + }); + const starter = (await client.query(chatApi.threads.list, {})).find(({ _id }) => _id === starterId); + expect(starter).toMatchObject({ backend: { settings } }); + expect(starter).not.toHaveProperty("starterFolder"); + } finally { + transport.close(); + } + }, + sandbox.env, + ); + }); +}, 180_000); diff --git a/packages/cli/tests/open.test.mjs b/packages/cli/tests/open.test.mjs new file mode 100644 index 00000000..992ea8b0 --- /dev/null +++ b/packages/cli/tests/open.test.mjs @@ -0,0 +1,93 @@ +import { expect, test } from "bun:test"; +import { join } from "node:path"; +import { withPlugin, workspace } from "../../build/tests/outside-fixture.mjs"; + +test("a shared server receives no folder path and a superseded request reports replaced", async () => { + await withPlugin(async (folder, root) => { + const messages = []; + const server = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + fetch(request, listener) { + if (listener.upgrade(request)) return; + return new Response(null, { status: 404 }); + }, + websocket: { + message(socket, payload) { + const call = JSON.parse(String(payload)); + messages.push(call); + const result = { + "tokamak:whoami": { id: "owner", name: "Owner", role: "owner" }, + "chat/open:request": { + threadId: "chat/threads:1", + requestId: "chat/openRequests:1", + created: true, + agent: null, + folderRecorded: false, + local: false, + }, + "chat/open:status": "replaced", + }[call.name]; + if (call.type === "unsubscribe") return; + socket.send( + JSON.stringify({ + type: call.type === "subscribe" ? "subscription" : "response", + id: call.id, + result: result === undefined ? { Err: "Unexpected call" } : { Ok: result }, + }), + ); + }, + }, + }); + try { + const child = Bun.spawn( + [ + process.execPath, + join(workspace, "packages/cli/src/main.ts"), + "open", + folder, + "--server", + `http://127.0.0.1:${server.port}`, + "--json", + ], + { + env: { ...process.env, TOKAMAK_SERVER_DIR: join(root, "data"), TOKAMAK_TOKEN: "fixture" }, + stdout: "pipe", + stderr: "pipe", + timeout: 30_000, + }, + ); + const [code, stdout, stderr] = await Promise.all([ + child.exited, + new Response(child.stdout).text(), + new Response(child.stderr).text(), + ]); + expect(code, stderr).toBe(0); + expect(JSON.parse(stdout)).toMatchObject({ shown: "replaced", windowConnected: false, folder }); + expect(stderr).toContain("newer request replaced"); + expect(messages.find(({ name }) => name === "chat/open:request")?.args).toEqual({}); + expect(JSON.stringify(messages)).not.toContain(folder); + } finally { + server.stop(true); + } + }); +}); + +test("window acknowledgement distinguishes pending, acknowledged and replaced with an injected deadline", async () => { + const { waitForWindow } = await import("../src/commands/open"); + for (const expected of ["pending", "acknowledged", "replaced"]) { + let stopped = false; + const client = { + subscribe(_status, args, notify) { + expect(args).toEqual({ requestId: "request" }); + notify({ value: "pending" }); + if (expected !== "pending") notify({ value: expected }); + return () => { + stopped = true; + }; + }, + }; + expect(await waitForWindow(client, "request", 0)).toBe(expected); + expect(stopped).toBe(true); + } +}); diff --git a/plugins/acp/src/server/api.ts b/plugins/acp/src/server/api.ts index 01085e79..0c35ffd1 100644 --- a/plugins/acp/src/server/api.ts +++ b/plugins/acp/src/server/api.ts @@ -1 +1 @@ -export { describe, turn, configure } from "./backend"; +export { describe, turn, configure, canonicalFolders } from "./backend"; diff --git a/plugins/acp/src/server/backend.ts b/plugins/acp/src/server/backend.ts index 1cb74a88..b2009f88 100644 --- a/plugins/acp/src/server/backend.ts +++ b/plugins/acp/src/server/backend.ts @@ -1,3 +1,5 @@ +import { realpathSync, statSync } from "node:fs"; +import { isAbsolute } from "node:path"; import { localChatPort } from "./turn/chat-port"; import { action, v, type JsonValue } from "@tokamak/sdk"; import { agents } from "./agents/client"; @@ -50,3 +52,20 @@ export const turn = action({ ); }, }); + +// Canonicalization is for folder equality only; configuring an agent still uses checkFolder. +export const canonicalFolders = action({ + args: { folders: v.array(v.string()) }, + handler: ({ user }, { folders }) => { + if (user.id !== "local") throw new Error("Folders are only available on the local server"); + return folders.map((folder) => { + if (!isAbsolute(folder)) return null; + try { + const path = realpathSync(folder); + return statSync(path).isDirectory() ? path : null; + } catch { + return null; + } + }); + }, +}); diff --git a/plugins/chat/src/client/lib/api.ts b/plugins/chat/src/client/lib/api.ts index 45b2384a..386f6524 100644 --- a/plugins/chat/src/client/lib/api.ts +++ b/plugins/chat/src/client/lib/api.ts @@ -16,6 +16,7 @@ export const api = createApi<{ null >; }; + open: Pick, "latest" | "shown">; messages: ApiFromModule; permissions: Pick, "answer">; runs: { diff --git a/plugins/chat/src/client/lib/state/use-chat.ts b/plugins/chat/src/client/lib/state/use-chat.ts index 99d825fe..15fbfa45 100644 --- a/plugins/chat/src/client/lib/state/use-chat.ts +++ b/plugins/chat/src/client/lib/state/use-chat.ts @@ -16,6 +16,7 @@ let lastThread: Id<"threads"> | undefined; export function useChat(client: TokamakClient, remote?: RemoteChat) { const threads = useQuery(client, api.threads.list, () => ({})); + const openRequest = useQuery(client, api.open.latest, () => ({})); const running = useQuery(client, api.runs.running, () => ({})); const [userId, setUserId] = createSignal(remote ? undefined : "local", { ownedWrite: true, @@ -88,6 +89,21 @@ export function useChat(client: TokamakClient, remote?: RemoteChat) { open(list[0]?._id); }); + createEffect( + () => [openRequest.data(), threads.data(), creating()] as const, + ([request, list, busy]) => { + if ( + !request || + request.expiresAt <= Date.now() || + busy || + !list?.some((item) => item._id === request.threadId) + ) + return; + open(request.threadId); + void client.mutation(api.open.shown, { requestId: request._id }).catch(() => {}); + }, + ); + if (!remote) useRunRecovery(client, () => Boolean(running.data()?.length), setError); const thread = createMemo(() => threads.data()?.find((item) => item._id === threadId())); diff --git a/plugins/chat/src/client/ui/composer/agent-picker.tsx b/plugins/chat/src/client/ui/composer/agent-picker.tsx index 27c0d875..7fca1e28 100644 --- a/plugins/chat/src/client/ui/composer/agent-picker.tsx +++ b/plugins/chat/src/client/ui/composer/agent-picker.tsx @@ -73,11 +73,12 @@ export function AgentPicker(props: { return; } if (!option) return; - void save({ plugin: option.plugin, backendId: option.id }, agent()?.folder || lastFolder).then( - (failure) => { - if (failure && failure !== "busy") props.chat.setError(failure); - }, - ); + void save( + { plugin: option.plugin, backendId: option.id }, + agent()?.folder || props.chat.thread()?.starterFolder || lastFolder, + ).then((failure) => { + if (failure && failure !== "busy") props.chat.setError(failure); + }); } async function submitFolder() { diff --git a/plugins/chat/src/server/backends/backend.ts b/plugins/chat/src/server/backends/backend.ts index a4bf958f..36f7f62f 100644 --- a/plugins/chat/src/server/backends/backend.ts +++ b/plugins/chat/src/server/backends/backend.ts @@ -6,3 +6,8 @@ export const backend = v.object({ settings: v.optional(v.any()), state: v.optional(v.any()), }); + +export function folderSetting(settings: unknown): string | undefined { + if (settings && typeof settings === "object" && "cwd" in settings && typeof settings.cwd === "string") + return settings.cwd || undefined; +} diff --git a/plugins/chat/src/server/backends/backends.ts b/plugins/chat/src/server/backends/backends.ts index 9450eeeb..07df39ca 100644 --- a/plugins/chat/src/server/backends/backends.ts +++ b/plugins/chat/src/server/backends/backends.ts @@ -1,4 +1,4 @@ -import { functionName, v, type ActionContext } from "@tokamak/sdk"; +import { functionName, v, type ActionContext, type JsonValue } from "@tokamak/sdk"; import { action } from "../_functions"; import type { BackendApi } from "../../shared/backends"; export const list = action({ @@ -14,11 +14,15 @@ export async function listBackends(ctx: ActionContext) { } export const configure = action({ args: { plugin: v.string(), backendId: v.string(), settings: v.any() }, - handler: async (ctx, args) => { - const provider = (await ctx.providers("chat.backend")).find( - (provider) => provider.backend.turn[functionName].split("/")[0] === args.plugin, - ); - if (!provider) throw new Error(`Backend plugin unavailable: ${args.plugin}`); - return ctx.use(provider).backend.configure({ backendId: args.backendId, settings: args.settings }); - }, + handler: configureBackend, }); +export async function configureBackend( + ctx: ActionContext, + args: { plugin: string; backendId: string; settings: JsonValue }, +) { + const provider = (await ctx.providers("chat.backend")).find( + (provider) => provider.backend.turn[functionName].split("/")[0] === args.plugin, + ); + if (!provider) throw new Error(`Backend plugin unavailable: ${args.plugin}`); + return ctx.use(provider).backend.configure({ backendId: args.backendId, settings: args.settings }); +} diff --git a/plugins/chat/src/server/open.ts b/plugins/chat/src/server/open.ts new file mode 100644 index 00000000..3e2185bd --- /dev/null +++ b/plugins/chat/src/server/open.ts @@ -0,0 +1,156 @@ +import { + createApi, + functionName, + v, + type ActionContext, + type DatabaseWriter, + type Doc, + type FunctionReference, + type Id, +} from "@tokamak/sdk"; +import { action, mutation, query, thread, type Model } from "./_functions"; +import { api } from "./_generated/api"; +import { folderSetting } from "./backends/backend"; +import { configureBackend, listBackends } from "./backends/backends"; +import type { BackendApi } from "../shared/backends"; + +const lifetime = 5 * 60_000; +type Thread = Doc; +type OpenResult = { + threadId: Id<"threads">; + requestId: Id<"openRequests">; + created: boolean; + agent: { plugin: string; backendId: string } | null; + folderRecorded: boolean; + local: boolean; +}; + +const foldersApi = createApi<{ + backend: { + canonicalFolders: FunctionReference<"action", { folders: string[] }, (string | null)[]>; + }; +}>("acp"); + +function folderOf(thread: Thread) { + return thread.backend ? folderSetting(thread.backend.settings) : thread.starterFolder; +} + +async function matchFolder(ctx: ActionContext, folder: string) { + const candidates = (await ctx.runQuery(api.threads.list, {})).flatMap((item: Thread) => { + const path = folderOf(item); + return path ? [{ item, path }] : []; + }); + if (candidates.some(({ path }) => path !== folder)) { + const providers = await ctx.providers("chat.backend"); + if (providers.some((provider) => provider.backend.describe[functionName] === "acp/backend:describe")) { + const [real, ...paths] = await ctx.use(foldersApi).backend.canonicalFolders({ + folders: [folder, ...candidates.map(({ path }) => path)], + }); + if (real) return candidates.find((_, i) => paths[i] === real)?.item; + } + } + return candidates.find(({ path }) => path === folder)?.item; +} + +export const request = action({ + args: { folder: v.optional(v.string()), alwaysNew: v.optional(v.boolean()) }, + handler: async (ctx, { folder, alwaysNew }): Promise => { + const local = ctx.user.id === "local"; + if (!local && (folder !== undefined || alwaysNew !== undefined)) + throw new Error("A shared server does not accept a working folder"); + if (local && !folder) throw new Error("A working folder is required"); + + const previous = folder && !alwaysNew ? await matchFolder(ctx, folder) : undefined; + let threadId = previous?._id; + if (!threadId) { + const first = folder ? (await listBackends(ctx))[0] : undefined; + if (first && folder) { + const settings = await configureBackend(ctx, { + plugin: first.plugin, + backendId: first.id, + settings: { cwd: folder }, + }); + if (folderSetting(settings) !== folder) + throw new Error("The agent would use a different folder; no thread was created"); + threadId = await ctx.runMutation(api.threads.insert, { + backend: { kind: "plugin", plugin: first.plugin, backendId: first.id, settings }, + }); + } else { + threadId = await ctx.runMutation(api.threads.insert, folder ? { starterFolder: folder } : {}); + } + } + + const { requestId, agent } = await ctx.runMutation(api.open.record, { threadId }); + return { requestId, threadId, created: !previous, agent, folderRecorded: local, local }; + }, +}); + +export const record = mutation({ + args: { threadId: v.id("threads") }, + handler: async (ctx, { threadId }) => { + const { db, user } = ctx; + const { backend } = await thread(db, threadId); + const pending = await db + .query("openRequests") + .withIndex("by_userId", (q) => q.eq("userId", user.id)) + .first(); + if (pending) await db.delete(pending._id); + const requestId = await db.insert("openRequests", { + userId: user.id, + threadId, + expiresAt: Date.now() + lifetime, + }); + const agent = + user.id === "local" && backend ? { plugin: backend.plugin, backendId: backend.backendId } : null; + return { requestId, agent }; + }, +}); + +export const latest = query({ + handler: async ({ db, user }) => { + const request = await db + .query("openRequests") + .withIndex("by_userId", (q) => q.eq("userId", user.id)) + .first(); + return request && request.expiresAt > Date.now() && (await db.get(request.threadId)) ? request : null; + }, +}); + +export const status = query({ + args: { requestId: v.id("openRequests") }, + handler: async ({ db, user }, { requestId }) => { + const request = await db.get(requestId); + if (request?.userId === user.id && request.expiresAt > Date.now()) return "pending" as const; + const receipt = await db + .query("openReceipts") + .withIndex("by_requestId", (q) => q.eq("requestId", requestId)) + .first(); + return receipt?.userId === user.id && receipt.expiresAt > Date.now() + ? ("acknowledged" as const) + : ("replaced" as const); + }, +}); + +export const shown = mutation({ + args: { requestId: v.id("openRequests") }, + handler: async ({ db, user }, { requestId }) => { + const request = await db.get(requestId); + if (request?.userId === user.id) { + await db.delete(requestId); + if (request.expiresAt > Date.now()) + await db.insert("openReceipts", { userId: user.id, requestId, expiresAt: request.expiresAt }); + } + return null; + }, +}); + +export async function pruneOpenRequests(db: DatabaseWriter) { + const now = Date.now(); + for (const table of ["openRequests", "openReceipts"] as const) { + for (const row of await db + .query(table) + .withIndex("by_expiresAt", (q) => q.lte("expiresAt", now)) + .take(32)) + await db.delete(row._id); + } +} diff --git a/plugins/chat/src/server/runs/runs.ts b/plugins/chat/src/server/runs/runs.ts index bb9051c2..058335a7 100644 --- a/plugins/chat/src/server/runs/runs.ts +++ b/plugins/chat/src/server/runs/runs.ts @@ -1,3 +1,4 @@ +import { pruneOpenRequests } from "../open"; import { interruptResponse } from "./run-messages"; import { v, type DatabaseWriter, type Doc } from "@tokamak/sdk"; import { mutation, query, type Model } from "../_functions"; @@ -89,6 +90,7 @@ export const observe = mutation({ export const recover = mutation({ handler: async ({ db }) => { + await pruneOpenRequests(db); const runs = await db .query("runs") .withIndex("by_status_expiresAt", (q) => diff --git a/plugins/chat/src/server/schema.ts b/plugins/chat/src/server/schema.ts index 8ea0eea1..bf9f7898 100644 --- a/plugins/chat/src/server/schema.ts +++ b/plugins/chat/src/server/schema.ts @@ -7,10 +7,25 @@ import { backend } from "./backends/backend"; export default defineSchema({ threads: defineTable({ backend: v.optional(backend), + starterFolder: v.optional(v.string()), title: v.string(), createdAt: v.number(), updatedAt: v.number(), }).index("by_updatedAt", ["updatedAt"]), + openRequests: defineTable({ + userId: v.string(), + threadId: v.id("threads"), + expiresAt: v.number(), + }) + .index("by_userId", ["userId"]) + .index("by_expiresAt", ["expiresAt"]), + openReceipts: defineTable({ + userId: v.string(), + requestId: v.id("openRequests"), + expiresAt: v.number(), + }) + .index("by_requestId", ["requestId"]) + .index("by_expiresAt", ["expiresAt"]), messages: defineTable({ threadId: v.id("threads"), role: v.union(v.literal("user"), v.literal("assistant")), diff --git a/plugins/chat/src/server/threads/threads.ts b/plugins/chat/src/server/threads/threads.ts index 830df06e..214d2410 100644 --- a/plugins/chat/src/server/threads/threads.ts +++ b/plugins/chat/src/server/threads/threads.ts @@ -1,4 +1,4 @@ -import { backend } from "../backends/backend"; +import { backend, folderSetting } from "../backends/backend"; import { listBackends } from "../backends/backends"; import { api } from "../_generated/api"; import { v } from "@tokamak/sdk"; @@ -20,13 +20,17 @@ export const create = action({ }); export const insert = mutation({ - args: { title: v.optional(v.string()), backend: v.optional(backend) }, - handler: (ctx, { title, backend }) => { + args: { + title: v.optional(v.string()), + backend: v.optional(backend), + starterFolder: v.optional(v.string()), + }, + handler: (ctx, { title, backend, starterFolder }) => { const { db } = ctx; const now = Date.now(); return db.insert("threads", { - ...(backend ? { backend } : {}), + ...(backend ? { backend } : ctx.user.id === "local" && starterFolder ? { starterFolder } : {}), title: title === undefined ? DEFAULT_TITLE : nonempty(title), createdAt: now, updatedAt: now, @@ -84,6 +88,10 @@ export const setBackend = mutation({ : {}), }; } + if (current.starterFolder && folderSetting(backend.settings)) { + const { _id, _creationTime, starterFolder: _starterFolder, ...fields } = current; + return db.replace(threadId, { ...fields, backend, updatedAt: Date.now() }); + } return db.patch(threadId, { backend, updatedAt: Date.now(),