@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 130 lines 4.5 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off -- The scenario stages a local ACP executable and its wake 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 COMPLETION = "The background command printed DONE.";
11 const ACP = `
12 import { unwatchFile, watchFile } from "node:fs";
13 import { createInterface } from "node:readline";
14
15 const sessionId = "hermes-scenario";
16 const send = (message) => process.stdout.write(JSON.stringify({ jsonrpc: "2.0", ...message }) + "\\n");
17 const respond = (id, result) => send({ id, result });
18 const chunk = (text) => send({
19 method: "session/update",
20 params: { sessionId, update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text } } },
21 });
22 for await (const line of createInterface({ input: process.stdin })) {
23 const { id, method } = JSON.parse(line);
24 if (id === undefined) continue;
25 if (method === "initialize") {
26 respond(id, {
27 protocolVersion: 1,
28 agentInfo: { name: "hermes-agent", version: "0.23.1" },
29 agentCapabilities: { loadSession: true },
30 authMethods: [],
31 });
32 } else if (method === "session/new") {
33 respond(id, { sessionId });
34 } else if (method === "session/prompt") {
35 chunk("Started it in the background.");
36 respond(id, { stopReason: "end_turn" });
37 // Hermes sends the follow-up on the root session, without another prompt response.
38 const gate = process.argv[2];
39 watchFile(gate, { interval: 100 }, (stat) => {
40 if (stat.nlink === 0) return;
41 unwatchFile(gate);
42 chunk(${JSON.stringify(COMPLETION)});
43 });
44 } else {
45 send({ id, error: { code: -32601, message: "Method not found" } });
46 }
47 }
48 `;
49
50 test(
51 "Hermes sends its background completion reply when launched through a wrapper",
52 WEB,
53 async ({ t3, app, screen }) => {
54 const project = await t3.project("demo");
55 const script = NodePath.join(project, "hermes-acp.mjs");
56 const gate = NodePath.join(project, "process-exited");
57 const settingsPath = NodePath.join(T3_HOME, "userdata/settings.json");
58 const originalSettings = await NodeFSP.readFile(settingsPath, "utf8");
59 const settings = JSON.parse(originalSettings);
60 await NodeFSP.writeFile(script, ACP);
61 settings.providerInstances["hermes-wrapper"] = {
62 driver: "acpRegistry",
63 displayName: "Hermes Agent",
64 enabled: true,
65 config: {
66 source: "local",
67 commandPath: process.execPath,
68 commandArgs: [script, gate],
69 },
70 };
71 try {
72 await NodeFSP.writeFile(settingsPath, JSON.stringify(settings));
73 const { cli } = makeState(T3_HOME);
74 // The watcher reloads settings asynchronously. Retry only before a thread was created.
75 let threadId: string | undefined;
76 for (let attempt = 0; attempt < 8 && threadId === undefined; attempt++) {
77 const started = await runPromise(
78 cli(
79 [
80 "thread",
81 "start",
82 project,
83 "Start a background command that reports DONE when it exits.",
84 "--title",
85 "Hermes background reply",
86 "--provider",
87 "hermes-wrapper",
88 "--model",
89 "default",
90 "--wait",
91 "--json",
92 ],
93 [0, 1],
94 ),
95 );
96 if (!started.stdout.includes("threadId")) continue;
97 const result = JSON.parse(started.stdout) as {
98 threadId: string;
99 status: string;
100 reply: string | null;
101 };
102 if (
103 result.status !== "completed" ||
104 !result.reply?.includes("Started it in the background.")
105 ) {
106 throw new Error(
107 `Hermes foreground turn did not complete: ${started.stdout} ${started.stderr}`,
108 );
109 }
110 threadId = result.threadId;
111 }
112 if (threadId === undefined) throw new Error("Could not start the Hermes foreground turn.");
113
114 await t3.open({ threadId });
115 await screen
116 .getByText("Started it in the background.", { exact: true })
117 .waitFor({ timeout: 60_000 });
118 await NodeFSP.writeFile(gate, "");
119 try {
120 await expect(screen.getByText(COMPLETION, { exact: true })).toBeVisible({
121 timeout: 15_000,
122 });
123 } finally {
124 await app.screenshot("hermes-background-reply");
125 }
126 } finally {
127 await NodeFSP.writeFile(settingsPath, originalSettings);
128 }
129 },
130 );