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

textGeneration.ts

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