Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions app/src/shell/shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<ServerEndpoint>;
Expand Down Expand Up @@ -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);
Expand Down
150 changes: 150 additions & 0 deletions e2e/browser/chat.spec.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<void>((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$/);
});
3 changes: 2 additions & 1 deletion packages/cli/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
134 changes: 134 additions & 0 deletions packages/cli/src/commands/open.ts
Original file line number Diff line number Diff line change
@@ -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<void>((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<Shown> {
let stop: (() => void) | undefined;
let timeout: ReturnType<typeof setTimeout> | undefined;
try {
return await new Promise<Shown>((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 <address> 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<typeof setTimeout> | 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;
Loading