The best way to control Claude Code, Codex and more from any computer or phone

main
ts 109 lines 4.22 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off -- The scenario stages a local ACP executable and its release gate.
2 import * as NodeFSP from "node:fs/promises";
3 import * as NodePath from "node:path";
4
5 import { expect } from "e2e";
6
7 import { T3_HOME } from "../e2e.config.ts";
8 import { makeState, runPromise, test, WEB } from "./t3.ts";
9
10 const FILE = "location-proof.ts";
11 const ACP = `
12 import { unwatchFile, watchFile } from "node:fs";
13 import { createInterface } from "node:readline";
14
15 const [file, gate] = process.argv.slice(2);
16 const sessionId = "location-scenario";
17 const send = (message) => process.stdout.write(JSON.stringify({ jsonrpc: "2.0", ...message }) + "\\n");
18 const update = (update) => send({ method: "session/update", params: { sessionId, update } });
19 for await (const line of createInterface({ input: process.stdin })) {
20 const { id, method } = JSON.parse(line);
21 if (id === undefined) continue;
22 if (method === "initialize") {
23 send({ id, result: { protocolVersion: 1, agentInfo: { name: "location-agent", version: "1.0.0" }, agentCapabilities: {}, authMethods: [] } });
24 } else if (method === "session/new") {
25 send({ id, result: { sessionId } });
26 } else if (method === "session/prompt") {
27 update({ sessionUpdate: "tool_call", toolCallId: "edit-1", title: "Edit", kind: "edit", status: "in_progress" });
28 // The agent names the file it edits only after announcing the tool call.
29 update({ sessionUpdate: "tool_call_update", toolCallId: "edit-1", locations: [{ path: file }] });
30 watchFile(gate, { interval: 100 }, (stat) => {
31 if (stat.nlink === 0) return;
32 unwatchFile(gate);
33 update({ sessionUpdate: "tool_call_update", toolCallId: "edit-1", status: "completed" });
34 send({ id, result: { stopReason: "end_turn" } });
35 });
36 } else {
37 send({ id, error: { code: -32601, message: "Method not found" } });
38 }
39 }
40 `;
41
42 test(
43 "A running ACP edit names the file a location-only tool_call_update reports",
44 WEB,
45 async ({ t3, app, screen }) => {
46 const project = await t3.project("demo", { [`src/${FILE}`]: "export {};\n" });
47 const script = NodePath.join(project, "location-acp.mjs");
48 const gate = NodePath.join(project, "release-edit");
49 const settingsPath = NodePath.join(T3_HOME, "userdata/settings.json");
50 const originalSettings = await NodeFSP.readFile(settingsPath, "utf8");
51 const settings = JSON.parse(originalSettings);
52 await NodeFSP.writeFile(script, ACP);
53 settings.providerInstances["location-agent"] = {
54 driver: "acpRegistry",
55 displayName: "Location Agent",
56 enabled: true,
57 config: {
58 source: "local",
59 commandPath: process.execPath,
60 commandArgs: [script, NodePath.join(project, "src", FILE), gate],
61 },
62 };
63 try {
64 await NodeFSP.writeFile(settingsPath, JSON.stringify(settings));
65 const { cli } = makeState(T3_HOME);
66 // The watcher reloads settings asynchronously. Retry only before a thread was created.
67 let threadId: string | undefined;
68 for (let attempt = 0; attempt < 8 && threadId === undefined; attempt++) {
69 const started = await runPromise(
70 cli(
71 [
72 "thread",
73 "start",
74 project,
75 "Edit the proof file.",
76 "--title",
77 "Location-only update",
78 "--provider",
79 "location-agent",
80 "--model",
81 "default",
82 "--json",
83 ],
84 [0, 1],
85 ),
86 );
87 if (!started.stdout.includes("threadId")) continue;
88 threadId = (JSON.parse(started.stdout) as { threadId: string }).threadId;
89 }
90 if (threadId === undefined) throw new Error("Could not start the location agent's turn.");
91
92 await t3.open({ threadId });
93 await screen.getByText("Working for", { exact: false }).first().waitFor({ timeout: 60_000 });
94 try {
95 await expect(screen.getByRole("button", /^demo\/src\/location-proof\.ts$/)).toBeVisible({
96 timeout: 15_000,
97 });
98 } finally {
99 await app.screenshot("running-edit-row");
100 }
101 } finally {
102 try {
103 await NodeFSP.writeFile(gate, "");
104 } finally {
105 await NodeFSP.writeFile(settingsPath, originalSettings);
106 }
107 }
108 },
109 );