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

main
ts 111 lines 4.5 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off -- fixtures write a native session database.
2 import * as NodeSqlite from "node:sqlite";
3 import * as NodeURL from "node:url";
4
5 import * as Effect from "effect/Effect";
6 import * as FileSystem from "effect/FileSystem";
7 import * as Path from "effect/Path";
8
9 export const ROOT = "7d40e54d-5977-4239-940b-246cdd81a12b";
10 export const CHILD = "a357bf2720914589dd1ec49f7a816115";
11 export const LAUNCH = { role: "Readme Reader", prompt: "Read README.md and report the marker" };
12
13 // Mirrors the 1.4.5 runtime: Step{5: metadata{4: tool call{1 id, 2 name}}, 20: planner{7: calls},
14 // 143: invoke_subagent{9: specs{2 role, 3 prompt}, 10: results{1 conversation, 2 log uri}}}.
15 const varint = (value: number): number[] => {
16 const bytes: number[] = [];
17 while (value >= 0x80) {
18 bytes.push((value & 0x7f) | 0x80);
19 value = Math.floor(value / 0x80);
20 }
21 return [...bytes, value];
22 };
23 const field = (number: number, value: string | Uint8Array) => {
24 const bytes = typeof value === "string" ? new TextEncoder().encode(value) : value;
25 return [...varint(number * 8 + 2), ...varint(bytes.length), ...bytes];
26 };
27 const proto = (...parts: number[][]) => Uint8Array.from(parts.flat());
28 const toolCall = (id: string) => proto(field(1, id), field(2, "invoke_subagent"));
29
30 export const invokeStep = (input: {
31 readonly callId: string;
32 readonly launches: ReadonlyArray<typeof LAUNCH>;
33 readonly children: ReadonlyArray<{ readonly id: string; readonly logUri: string }>;
34 }) =>
35 proto(
36 [...varint(1 * 8), ...varint(127)],
37 field(5, proto(field(4, toolCall(input.callId)))),
38 field(
39 143,
40 proto(
41 ...input.launches.map((launch) =>
42 field(9, proto(field(1, "research"), field(2, launch.role), field(3, launch.prompt))),
43 ),
44 ...input.children.map((child) =>
45 field(10, proto(field(1, child.id), field(2, child.logUri))),
46 ),
47 ),
48 ),
49 );
50 export const plannerStep = (callIds: ReadonlyArray<string>) =>
51 proto(field(20, proto(...callIds.map((id) => field(7, toolCall(id))))));
52
53 export const transcriptLines = [
54 // Trimmed from 1.4.5: the launch envelope around the prompt the parent record already holds.
55 {
56 step_index: 0,
57 type: "SYSTEM_MESSAGE",
58 status: "DONE",
59 content: `<USER_REQUEST sender="7d40e54d-5977-4239-940b-246cdd81a12b" timestamp="2026-10-31T18:00:00Z">\n${LAUNCH.prompt}\n</USER_REQUEST>`,
60 },
61 {
62 step_index: 1,
63 type: "PLANNER_RESPONSE",
64 status: "DONE",
65 // The runtime stores each argument value as JSON text.
66 tool_calls: [{ name: "view_file", args: { AbsolutePath: '"/project/README.md"' } }],
67 },
68 { step_index: 2, type: "VIEW_FILE", status: "DONE", content: "2: child read marker 4242" },
69 { step_index: 3, type: "PLANNER_RESPONSE", status: "DONE", content: "marker 4242 found" },
70 ];
71
72 export const makeProfile = Effect.fn("makeProfile")(function* (input: {
73 /** Defaults to a scoped temp directory. */
74 readonly profile?: string;
75 readonly steps: (logUri: string) => ReadonlyArray<readonly [number, number, Uint8Array]>;
76 readonly transcript?: string;
77 }) {
78 const fs = yield* FileSystem.FileSystem;
79 const path = yield* Path.Path;
80 const profile = input.profile ?? (yield* fs.makeTempDirectoryScoped({ prefix: "t3-agy-child-" }));
81 const acp = path.join(profile, "antigravity-acp");
82 const logs = path.join(acp, "brain", CHILD, ".system_generated", "logs");
83 yield* fs.makeDirectory(path.join(acp, "conversations"), { recursive: true });
84 yield* fs.makeDirectory(logs, { recursive: true });
85 const transcriptPath = path.join(logs, "transcript.jsonl");
86 yield* fs.writeFileString(
87 transcriptPath,
88 input.transcript ?? transcriptLines.map((line) => JSON.stringify(line)).join("\n") + "\n",
89 );
90 const logUri = NodeURL.pathToFileURL(transcriptPath).href;
91 const database = new NodeSqlite.DatabaseSync(path.join(acp, "conversations", `${ROOT}.db`));
92 database.exec(
93 "CREATE TABLE steps (idx integer PRIMARY KEY, step_type integer NOT NULL DEFAULT 0, step_format integer NOT NULL DEFAULT 0, step_payload blob)",
94 );
95 const insert = database.prepare(
96 "INSERT INTO steps (idx, step_type, step_payload) VALUES (?, ?, ?)",
97 );
98 for (const [idx, type, payload] of input.steps(logUri)) insert.run(idx, type, payload);
99 database.close();
100 return { profile, transcriptPath, logUri };
101 });
102
103 export const autoApproved = (logUri: string) =>
104 [
105 [1, 15, plannerStep(["call-a"])],
106 [
107 2,
108 127,
109 invokeStep({ callId: "call-a", launches: [LAUNCH], children: [{ id: CHILD, logUri }] }),
110 ],
111 ] as const;