Open History to see the last commit
1/**
2 * PiTextGeneration — commit messages, PR content, branch names, and thread
3 * titles generated through an ephemeral `pi --mode rpc --no-session` process.
4 * No session file is written; the user's Pi configuration (default model,
5 * auth, custom providers) still applies.
6 */
7import * as Effect from "effect/Effect";
8import * as Option from "effect/Option";
9import * as Queue from "effect/Queue";
10import * as Schema from "effect/Schema";
11import { ChildProcessSpawner } from "effect/process";
12
13import { TextGenerationError, type PiSettings } from "@t3tools/contracts";
14
15import { makePiRpcConnection, parsePiModelSlug } from "../orchestration-v2/Adapters/PiRpc.ts";
16import {
17 buildPiRpcLaunch,
18 resolvePiLaunchArgs,
19} from "../orchestration-v2/Adapters/piT3McpInjection.ts";
20import * as TextGenerationOperations from "./TextGenerationOperations.ts";
21
22const PI_TIMEOUT_MS = 180_000;
23
24const isTextGenerationError = Schema.is(TextGenerationError);
25
26export const makePiTextGeneration = Effect.fn("makePiTextGeneration")(function* (
27 piSettings: PiSettings,
28 environment: NodeJS.ProcessEnv = process.env,
29) {
30 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
31
32 const runPiJson: TextGenerationOperations.Runner = (request) => {
33 const { operation, cwd, prompt, modelSelection } = request;
34 return Effect.gen(function* () {
35 const resolvedLaunchArgs = resolvePiLaunchArgs(piSettings.launchArgs);
36 if (!resolvedLaunchArgs.ok) {
37 return yield* new TextGenerationError({
38 operation,
39 detail: resolvedLaunchArgs.message,
40 });
41 }
42 const launch = buildPiRpcLaunch({
43 launchArgs: resolvedLaunchArgs.args,
44 environment,
45 mcpSession: undefined,
46 extensionPath: undefined,
47 ephemeral: true,
48 // No user is present to answer a text-generation extension dialog.
49 disableExtensions: true,
50 // Background naming/content helpers must never mutate the workspace.
51 disableTools: true,
52 });
53 const connection = yield* makePiRpcConnection({
54 command: piSettings.binaryPath || "pi",
55 // Extensions and tools are disabled because no user is present to
56 // answer a dialog and background text generation is read-only. User
57 // model config and auth still apply.
58 args: launch.args,
59 cwd,
60 env: launch.env,
61 }).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner));
62
63 if (modelSelection.model !== "default") {
64 // `customModels` accepts arbitrary strings, so an unusable slug is
65 // rejected rather than skipped: running Pi's default model here would
66 // report success for a model the caller never asked for.
67 const parsed = parsePiModelSlug(modelSelection.model);
68 if (parsed === null) {
69 return yield* new TextGenerationError({
70 operation,
71 detail: `Pi model '${modelSelection.model}' must use provider/model format.`,
72 });
73 }
74 yield* connection.request({
75 type: "set_model",
76 provider: parsed.provider,
77 modelId: parsed.modelId,
78 });
79 }
80
81 yield* connection.request({ type: "prompt", message: prompt });
82 yield* Effect.gen(function* () {
83 while (true) {
84 const event = yield* Queue.take(connection.events);
85 if (event["type"] === "agent_settled") return;
86 }
87 });
88 const data = yield* connection.request({ type: "get_last_assistant_text" });
89 const text =
90 typeof data === "object" &&
91 data !== null &&
92 typeof (data as { text?: unknown }).text === "string"
93 ? (data as { text: string }).text.trim()
94 : "";
95 if (!text) {
96 return yield* new TextGenerationError({
97 operation,
98 detail: "Pi returned empty output.",
99 });
100 }
101 return yield* TextGenerationOperations.decodeJsonReply(request, "Pi", text);
102 }).pipe(
103 Effect.timeoutOption(PI_TIMEOUT_MS),
104 Effect.flatMap(
105 Option.match({
106 onNone: () =>
107 Effect.fail(new TextGenerationError({ operation, detail: "Pi request timed out." })),
108 onSome: (value) => Effect.succeed(value),
109 }),
110 ),
111 Effect.mapError((cause) =>
112 isTextGenerationError(cause)
113 ? cause
114 : new TextGenerationError({
115 operation,
116 detail: "Pi text generation failed.",
117 cause,
118 }),
119 ),
120 Effect.scoped,
121 );
122 };
123
124 return TextGenerationOperations.fromRunner("PiTextGeneration", runPiJson);
125});