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

textGeneration.ts

121 lines4.2 KB

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