Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PiTextGeneration.test.ts

95 lines3.6 KB

1import { assert, it } from "@effect/vitest";
2import { ProviderInstanceId } from "@t3tools/contracts";
3import * as Cause from "effect/Cause";
4import * as Effect from "effect/Effect";
5import * as Queue from "effect/Queue";
6import * as Schema from "effect/Schema";
7import * as Sink from "effect/Sink";
8import * as Stream from "effect/Stream";
9import { ChildProcessSpawner } from "effect/unstable/process";
10​
11import { makePiTextGeneration } from "./PiTextGeneration.ts";
12​
13const decodeRecordLine = Schema.decodeSync(
14 Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown)),
15);
16const encodeJsonLine = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
17​
18/** Deliberately outside the valid pid range so a group-kill can never land. */
19const FAKE_PID = 999_999_999;
20​
21/**
22 * Fake `pi --mode rpc` for one ephemeral generation: records the request
23 * types it receives, settles right after the prompt, and replies with
24 * `assistantText`.
25 */
26function fakePiSpawner(assistantText: string, requests: Array<Record<string, unknown>>) {
27 return ChildProcessSpawner.make(() =>
28 Effect.gen(function* () {
29 const stdout = yield* Queue.unbounded<Uint8Array, Cause.Done>();
30 const emit = (record: Record<string, unknown>) =>
31 Queue.offer(stdout, new TextEncoder().encode(`${encodeJsonLine(record)}\n`));
32 let buffer = "";
33 return ChildProcessSpawner.makeHandle({
34 pid: ChildProcessSpawner.ProcessId(FAKE_PID),
35 exitCode: Effect.never,
36 isRunning: Effect.succeed(true),
37 kill: () => Effect.void,
38 unref: Effect.succeed(Effect.void),
39 stdin: Sink.forEach((chunk: Uint8Array) =>
40 Effect.gen(function* () {
41 buffer += new TextDecoder().decode(chunk);
42 let newline = buffer.indexOf("\n");
43 while (newline !== -1) {
44 const record = decodeRecordLine(buffer.slice(0, newline));
45 buffer = buffer.slice(newline + 1);
46 requests.push(record);
47 const data =
48 record["type"] === "get_last_assistant_text" ? { text: assistantText } : undefined;
49 yield* emit({ type: "response", id: record["id"], success: true, data });
50 if (record["type"] === "prompt") yield* emit({ type: "agent_settled" });
51 newline = buffer.indexOf("\n");
52 }
53 }),
54 ),
55 stdout: Stream.fromQueue(stdout),
56 stderr: Stream.empty,
57 all: Stream.empty,
58 getInputFd: () => Sink.drain,
59 getOutputFd: () => Stream.empty,
60 });
61 }),
62 );
64​
65it.effect("applies the selected thinking level and keeps a title's refinement flag", () =>
66 Effect.gen(function* () {
67 const requests: Array<Record<string, unknown>> = [];
68 const textGeneration = yield* makePiTextGeneration(
69 { enabled: true, binaryPath: "pi", launchArgs: "", customModels: [] },
70 {},
71 ).pipe(
72 Effect.provideService(
73 ChildProcessSpawner.ChildProcessSpawner,
74 fakePiSpawner('{"title":"Fix this","needsRefinement":true}', requests),
75 ),
76 );
77​
78 const generated = yield* textGeneration.generateThreadTitle({
79 cwd: process.cwd(),
80 message: "fix this",
81 modelSelection: {
82 instanceId: ProviderInstanceId.make("pi"),
83 model: "anthropic/claude-sonnet-5",
84 options: [{ id: "thinking", value: "high" }],
85 },
86 });
87​
88 assert.deepEqual(generated, { title: "Fix this", needsRefinement: true });
89 assert.deepEqual(
90 requests.map((record) => record["type"]),
91 ["set_model", "set_thinking_level", "prompt", "get_last_assistant_text"],
92 );
93 assert.equal(requests[1]?.["level"], "high");
94 }),
95);