Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import { ProviderInstanceId } from "@t3tools/contracts";
3import { createModelSelection } from "@t3tools/shared/model";
4import * as Cause from "effect/Cause";
5import * as Effect from "effect/Effect";
6import * as Queue from "effect/Queue";
7import * as Schema from "effect/Schema";
8import * as Sink from "effect/Sink";
9import * as Stream from "effect/Stream";
10import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
11
12import { makePiTextGeneration } from "./textGeneration.ts";
13
14const decodeJsonLine = Schema.decodeUnknownSync(Schema.fromJsonString(Schema.Unknown));
15const encodeJsonLine = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
16
17const makeFakePi = (reply: string, setModelError?: string) =>
18 Effect.gen(function* () {
19 const stdout = yield* Queue.unbounded<Uint8Array, Cause.Done>();
20 const received: Array<Record<string, unknown>> = [];
21 const emit = (record: Record<string, unknown>) =>
22 Queue.offer(stdout, new TextEncoder().encode(`${encodeJsonLine(record)}\n`));
23 let buffered = "";
24 const onStdin = (chunk: Uint8Array) =>
25 Effect.gen(function* () {
26 buffered += new TextDecoder().decode(chunk);
27 const lines = buffered.split("\n");
28 buffered = lines.pop() ?? "";
29 for (const line of lines.filter((candidate) => candidate.length > 0)) {
30 const record = decodeJsonLine(line) as Record<string, unknown>;
31 received.push(record);
32 const response = { type: "response", id: record["id"], command: record["type"] };
33 if (record["type"] === "prompt") {
34 yield* emit({ ...response, success: true });
35 yield* emit({ type: "agent_settled" });
36 } else if (record["type"] === "get_last_assistant_text") {
37 yield* emit({ ...response, success: true, data: { text: reply } });
38 } else if (record["type"] === "set_model" && setModelError !== undefined) {
39 yield* emit({ ...response, success: false, error: setModelError });
40 } else {
41 yield* emit({ ...response, success: true });
42 }
43 }
44 });
45 const spawner = ChildProcessSpawner.make(() =>
46 Effect.succeed(
47 ChildProcessSpawner.makeHandle({
48 // Outside the valid pid range, so PiRpc's process-group kill never lands.
49 pid: ChildProcessSpawner.ProcessId(999_999_999),
50 exitCode: Effect.never,
51 isRunning: Effect.succeed(true),
52 kill: () => Effect.void,
53 unref: Effect.succeed(Effect.void),
54 stdin: Sink.forEach(onStdin),
55 stdout: Stream.fromQueue(stdout),
56 stderr: Stream.empty,
57 all: Stream.empty,
58 getInputFd: () => Sink.drain,
59 getOutputFd: () => Stream.empty,
60 }),
61 ),
62 );
63 return { spawner, received };
64 });
65
66it.effect("names the model and the disabled extensions when Pi cannot load it", () =>
67 Effect.gen(function* () {
68 const pi = yield* makeFakePi("", "Model not found: demo/demo-model");
69 const textGeneration = yield* makePiTextGeneration(
70 { enabled: true, binaryPath: "pi", launchArgs: "", customModels: [] },
71 {},
72 ).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, pi.spawner));
73
74 const error = yield* textGeneration
75 .generateCommitMessage({
76 cwd: process.cwd(),
77 branch: "main",
78 stagedSummary: "M README.md",
79 stagedPatch: "diff --git a/README.md b/README.md",
80 modelSelection: createModelSelection(ProviderInstanceId.make("pi"), "demo/demo-model"),
81 })
82 .pipe(Effect.flip);
83
84 assert.equal(error._tag, "TextGenerationError");
85 assert.include(error.detail, "Pi rejected model 'demo/demo-model'");
86 assert.include(error.detail, "Model not found: demo/demo-model");
87 assert.include(error.detail, "--no-extensions");
88 assert.isUndefined(pi.received.find((record) => record["type"] === "prompt"));
89 }),
90);