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

main
ts 342 lines 13.2 KB
Raw
1 import { assert, it } from "@effect/vitest";
2 import { ProviderInstanceId } from "@t3tools/contracts";
3 import { createModelSelection } from "@t3tools/shared/model";
4 import * as Cause from "effect/Cause";
5 import * as Effect from "effect/Effect";
6 import * as Queue from "effect/Queue";
7 import * as Schema from "effect/Schema";
8 import * as Sink from "effect/Sink";
9 import * as Stream from "effect/Stream";
10 import * as ChildProcess from "effect/process/ChildProcess";
11 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
12
13 import { makePiTextGeneration } from "./textGeneration.ts";
14
15 const decodeJsonLine = Schema.decodeUnknownSync(Schema.fromJsonString(Schema.Unknown));
16 const encodeJsonLine = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
17
18 const makeFakePi = (
19 reply: string,
20 setModelError?: string,
21 turnEvents: ReadonlyArray<Record<string, unknown>> = [],
22 models?: ReadonlyArray<Record<string, unknown>>,
23 version = "0.84.3",
24 thinkingError?: string,
25 ) =>
26 Effect.gen(function* () {
27 const stdout = yield* Queue.unbounded<Uint8Array, Cause.Done>();
28 const received: Array<Record<string, unknown>> = [];
29 const emit = (record: Record<string, unknown>) =>
30 Queue.offer(stdout, new TextEncoder().encode(`${encodeJsonLine(record)}\n`));
31 let buffered = "";
32 const onStdin = (chunk: Uint8Array) =>
33 Effect.gen(function* () {
34 buffered += new TextDecoder().decode(chunk);
35 const lines = buffered.split("\n");
36 buffered = lines.pop() ?? "";
37 for (const line of lines.filter((candidate) => candidate.length > 0)) {
38 const record = decodeJsonLine(line) as Record<string, unknown>;
39 received.push(record);
40 const response = { type: "response", id: record["id"], command: record["type"] };
41 if (record["type"] === "prompt") {
42 yield* emit({ ...response, success: true });
43 for (const event of turnEvents) yield* emit(event);
44 yield* emit({ type: "agent_settled" });
45 } else if (record["type"] === "get_last_assistant_text") {
46 yield* emit({ ...response, success: true, data: { text: reply } });
47 } else if (record["type"] === "get_available_models" && models !== undefined) {
48 yield* emit({ ...response, success: true, data: { models } });
49 } else if (record["type"] === "set_model" && setModelError !== undefined) {
50 yield* emit({ ...response, success: false, error: setModelError });
51 } else if (record["type"] === "set_thinking_level" && thinkingError !== undefined) {
52 yield* emit({ ...response, success: false, error: thinkingError });
53 } else {
54 yield* emit({ ...response, success: true });
55 }
56 }
57 });
58 const spawner = ChildProcessSpawner.make((command) => {
59 const versionProbe =
60 ChildProcess.isStandardCommand(command) && command.args.includes("--version");
61 return Effect.succeed(
62 ChildProcessSpawner.makeHandle({
63 // Outside the valid pid range, so PiRpc's process-group kill never lands.
64 pid: ChildProcessSpawner.ProcessId(999_999_999),
65 exitCode: versionProbe ? Effect.succeed(ChildProcessSpawner.ExitCode(0)) : Effect.never,
66 isRunning: Effect.succeed(true),
67 kill: () => Effect.void,
68 unref: Effect.succeed(Effect.void),
69 stdin: Sink.forEach(onStdin),
70 stdout: versionProbe
71 ? Stream.make(new TextEncoder().encode(version))
72 : Stream.fromQueue(stdout),
73 stderr: Stream.empty,
74 all: Stream.empty,
75 getInputFd: () => Sink.drain,
76 getOutputFd: () => Stream.empty,
77 }),
78 );
79 });
80 return { spawner, received };
81 });
82
83 it.effect("runs Pi offline, since the process is stopped as soon as it answers", () =>
84 Effect.gen(function* () {
85 const pi = yield* makeFakePi(JSON.stringify({ subject: "Update README", body: "" }));
86 const launches: Array<ReadonlyArray<string>> = [];
87 const spawner = ChildProcessSpawner.make((command) => {
88 if (ChildProcess.isStandardCommand(command)) launches.push(command.args.map(String));
89 return pi.spawner.spawn(command);
90 });
91 const textGeneration = yield* makePiTextGeneration(
92 { enabled: true, binaryPath: "pi", launchArgs: "", customModels: [] },
93 {},
94 ).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner));
95
96 const result = yield* textGeneration.generateCommitMessage({
97 cwd: process.cwd(),
98 branch: "main",
99 stagedSummary: "M README.md",
100 stagedPatch: "diff --git a/README.md b/README.md",
101 modelSelection: createModelSelection(ProviderInstanceId.make("pi"), "default"),
102 });
103
104 assert.equal(result.subject, "Update README");
105 assert.deepEqual(
106 launches.map((args) => args.filter((arg) => arg === "--offline")),
107 [["--offline"]],
108 );
109 }),
110 );
111
112 it.effect("names the model and the disabled extensions when Pi cannot load it", () =>
113 Effect.gen(function* () {
114 const pi = yield* makeFakePi("", "Model not found: demo/demo-model");
115 const textGeneration = yield* makePiTextGeneration(
116 { enabled: true, binaryPath: "pi", launchArgs: "", customModels: [] },
117 {},
118 ).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, pi.spawner));
119
120 const error = yield* textGeneration
121 .generateCommitMessage({
122 cwd: process.cwd(),
123 branch: "main",
124 stagedSummary: "M README.md",
125 stagedPatch: "diff --git a/README.md b/README.md",
126 modelSelection: createModelSelection(ProviderInstanceId.make("pi"), "demo/demo-model"),
127 })
128 .pipe(Effect.flip);
129
130 assert.equal(error._tag, "TextGenerationError");
131 assert.include(error.detail, "Pi rejected model 'demo/demo-model'");
132 assert.include(error.detail, "Model not found: demo/demo-model");
133 assert.include(error.detail, "--no-extensions");
134 assert.isUndefined(pi.received.find((record) => record["type"] === "prompt"));
135 }),
136 );
137
138 it.effect("selects the provider and id Pi reported when the provider contains a slash", () =>
139 Effect.gen(function* () {
140 const provider = "llama-server=http://127.0.0.1:8080";
141 const pi = yield* makeFakePi(
142 JSON.stringify({ subject: "Update README", body: "" }),
143 undefined,
144 [],
145 [{ provider, id: "qwen3" }],
146 );
147 const textGeneration = yield* makePiTextGeneration(
148 { enabled: true, binaryPath: "pi", launchArgs: "", customModels: [] },
149 {},
150 ).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, pi.spawner));
151
152 yield* textGeneration.generateCommitMessage({
153 cwd: process.cwd(),
154 branch: "main",
155 stagedSummary: "M README.md",
156 stagedPatch: "diff --git a/README.md b/README.md",
157 modelSelection: createModelSelection(ProviderInstanceId.make("pi"), `${provider}/qwen3`),
158 });
159
160 const setModel = pi.received.find((record) => record["type"] === "set_model");
161 assert.deepEqual(
162 { provider: setModel?.["provider"], modelId: setModel?.["modelId"] },
163 { provider, modelId: "qwen3" },
164 );
165 }),
166 );
167
168 const failedTurnWith = (errorMessage: string) => ({
169 type: "message_end",
170 message: { role: "assistant", stopReason: "error", errorMessage },
171 });
172
173 const failedTurn = failedTurnWith(
174 '401: {"error":{"message":"Incorrect API key provided: sk-test-SECRET123"}}',
175 );
176
177 const generateCommitMessage = (turnEvents: ReadonlyArray<Record<string, unknown>>, reply = "") =>
178 Effect.gen(function* () {
179 const pi = yield* makeFakePi(reply, undefined, turnEvents);
180 const textGeneration = yield* makePiTextGeneration(
181 { enabled: true, binaryPath: "pi", launchArgs: "", customModels: [] },
182 {},
183 ).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, pi.spawner));
184 return yield* textGeneration.generateCommitMessage({
185 cwd: process.cwd(),
186 branch: "main",
187 stagedSummary: "M README.md",
188 stagedPatch: "diff --git a/README.md b/README.md",
189 modelSelection: createModelSelection(ProviderInstanceId.make("pi"), "default"),
190 });
191 });
192
193 it.effect("reports Pi's turn error when the reply is empty", () =>
194 Effect.gen(function* () {
195 const error = yield* generateCommitMessage([failedTurn]).pipe(Effect.flip);
196
197 assert.equal(error._tag, "TextGenerationError");
198 assert.equal(error.detail, "Pi could not generate text: the model provider returned HTTP 401.");
199 }),
200 );
201
202 it.effect("keeps Pi's raw error text out of the error", () =>
203 Effect.gen(function* () {
204 const error = yield* generateCommitMessage([
205 failedTurnWith("Invalid API key sk-test-SECRET123"),
206 ]).pipe(Effect.flip);
207
208 assert.equal(error.detail, "Pi could not generate text: the model provider reported an error.");
209 assert.notInclude(error.message, "SECRET123");
210 }),
211 );
212
213 it.effect("keeps the reply when Pi answers after a failed attempt", () =>
214 Effect.gen(function* () {
215 const result = yield* generateCommitMessage(
216 [failedTurn, { type: "message_end", message: { role: "assistant", stopReason: "stop" } }],
217 JSON.stringify({ subject: "Update README", body: "" }),
218 );
219
220 assert.equal(result.subject, "Update README");
221 }),
222 );
223
224 it.effect("drops an earlier error once a later attempt succeeds", () =>
225 Effect.gen(function* () {
226 const error = yield* generateCommitMessage([
227 failedTurn,
228 { type: "message_end", message: { role: "assistant", stopReason: "stop" } },
229 ]).pipe(Effect.flip);
230
231 assert.equal(error.detail, "Pi returned empty output.");
232 }),
233 );
234
235 it.effect.each([
236 { model: "demo/demo-model", thinking: "off", expected: "off", version: "0.84.3" },
237 { model: "demo/demo-model", thinking: "max", expected: "max", version: "1.4.0" },
238 { model: "demo/demo-model", thinking: "invalid", expected: undefined, version: "0.84.3" },
239 { model: "demo/demo-model", thinking: undefined, expected: undefined, version: "0.84.3" },
240 { model: "default", thinking: "off", expected: "off", version: "0.84.3" },
241 { model: "demo/demo-model", thinking: "low", expected: undefined, version: "0.84.2" },
242 ])(
243 "handles thinking=$thinking for $model on Pi $version",
244 ({ model, thinking, expected, version }) =>
245 Effect.gen(function* () {
246 const pi = yield* makeFakePi(
247 JSON.stringify({ subject: "Update README", body: "" }),
248 undefined,
249 [],
250 undefined,
251 version,
252 );
253 const textGeneration = yield* makePiTextGeneration(
254 { enabled: true, binaryPath: "pi", launchArgs: "", customModels: [] },
255 {},
256 (_driver, detected) =>
257 Effect.succeed(
258 detected === "0.84.2"
259 ? {
260 status: "broken" as const,
261 message: null,
262 recommendedVersion: null,
263 recommendedRange: ">=1.0.0",
264 }
265 : undefined,
266 ),
267 ).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, pi.spawner));
268
269 const generation = textGeneration.generateCommitMessage({
270 cwd: process.cwd(),
271 branch: "main",
272 stagedSummary: "M README.md",
273 stagedPatch: "diff --git a/README.md b/README.md",
274 modelSelection: createModelSelection(
275 ProviderInstanceId.make("pi"),
276 model,
277 thinking === undefined ? [] : [{ id: "thinking", value: thinking }],
278 ),
279 });
280 if (version === "0.84.2") {
281 const error = yield* generation.pipe(Effect.flip);
282 assert.include(error.detail, "Pi 0.84.2 does not work");
283 assert.deepEqual(pi.received, []);
284 return;
285 }
286 const result = yield* generation;
287
288 assert.equal(result.subject, "Update README");
289 assert.deepEqual(
290 pi.received.map((record) => record["type"]),
291 [
292 ...(model === "default" ? [] : ["get_available_models", "set_model"]),
293 ...(expected === undefined ? [] : ["set_thinking_level"]),
294 "prompt",
295 "get_last_assistant_text",
296 ],
297 );
298 assert.equal(
299 pi.received.find((record) => record["type"] === "set_thinking_level")?.["level"],
300 expected,
301 );
302 }),
303 );
304
305 it.effect("generates at Pi's default level when Pi rejects the saved thinking level", () =>
306 Effect.gen(function* () {
307 const pi = yield* makeFakePi(
308 JSON.stringify({ subject: "Update README", body: "" }),
309 undefined,
310 [],
311 undefined,
312 "1.4.0",
313 'Thinking level "max" is not supported by demo/demo-model',
314 );
315 const textGeneration = yield* makePiTextGeneration(
316 { enabled: true, binaryPath: "pi", launchArgs: "", customModels: [] },
317 {},
318 ).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, pi.spawner));
319
320 const result = yield* textGeneration.generateCommitMessage({
321 cwd: process.cwd(),
322 branch: "main",
323 stagedSummary: "M README.md",
324 stagedPatch: "diff --git a/README.md b/README.md",
325 modelSelection: createModelSelection(ProviderInstanceId.make("pi"), "demo/demo-model", [
326 { id: "thinking", value: "max" },
327 ]),
328 });
329
330 assert.equal(result.subject, "Update README");
331 assert.deepEqual(
332 pi.received.map((record) => record["type"]),
333 [
334 "get_available_models",
335 "set_model",
336 "set_thinking_level",
337 "prompt",
338 "get_last_assistant_text",
339 ],
340 );
341 }),
342 );