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

textGeneration.ts

131 lines4.5 KB

1import * as Crypto from "effect/Crypto";
2import * as Effect from "effect/Effect";
3import * as Option from "effect/Option";
4import * as Ref from "effect/Ref";
5import * as Schema from "effect/Schema";
6import { ChildProcessSpawner } from "effect/process";
7import type * as EffectAcpErrors from "effect-acp/errors";
8​
9import { TextGenerationError } from "@t3tools/contracts";
10import type { GrokSettings } from "../settings.ts";
11import { getModelSelectionStringOptionValue } from "@t3tools/shared/model";
12​
13import * as TextGenerationOperations from "@t3tools/provider-core/server/textGenerationOperations";
14import {
15 applyGrokAcpModelSelection,
16 currentGrokModelIdFromSessionSetup,
17 currentGrokReasoningEffortFromSessionSetup,
18 makeGrokAcpRuntime,
19 resolveGrokAcpBaseModelId,
20} from "./acpSupport.ts";
21​
22const GROK_TIMEOUT_MS = 180_000;
23​
24const isTextGenerationError = Schema.is(TextGenerationError);
25​
26export const makeGrokTextGeneration = Effect.fn("makeGrokTextGeneration")(function* (
27 grokSettings: GrokSettings,
28 environment: NodeJS.ProcessEnv = process.env,
29) {
30 const crypto = yield* Crypto.Crypto;
31 const commandSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
32​
33 const runGrokJson: TextGenerationOperations.Runner = (request) => {
34 const { operation, cwd, prompt, modelSelection } = request;
35 return Effect.gen(function* () {
36 const outputRef = yield* Ref.make("");
37 const runtime = yield* makeGrokAcpRuntime({
38 grokSettings,
39 environment,
40 childProcessSpawner: commandSpawner,
41 cwd,
42 clientInfo: { name: "t3-code-git-text", version: "0.0.0" },
43 }).pipe(Effect.provideService(Crypto.Crypto, crypto));
44​
45 yield* runtime.handleSessionUpdate((notification) => {
46 const update = notification.update;
47 if (update.sessionUpdate !== "agent_message_chunk") {
48 return Effect.void;
49 }
50 const content = update.content;
51 if (content.type !== "text") {
52 return Effect.void;
53 }
54 return Ref.update(outputRef, (current) => current + content.text);
55 });
56​
57 const promptResult = yield* Effect.gen(function* () {
58 const resolvedModel = resolveGrokAcpBaseModelId(modelSelection.model);
59 const started = yield* runtime.start();
60 const requestedReasoningEffort = getModelSelectionStringOptionValue(
61 modelSelection,
62 "reasoningEffort",
63 );
64 yield* applyGrokAcpModelSelection({
65 runtime,
66 currentModelId: currentGrokModelIdFromSessionSetup(started.sessionSetupResult),
67 currentReasoningEffort: currentGrokReasoningEffortFromSessionSetup(
68 started.sessionSetupResult,
69 ),
70 requestedModelId: resolvedModel,
71 requestedReasoningEffort,
72 mapError: (cause) =>
73 new TextGenerationError({
74 operation,
75 detail: "Failed to set Grok ACP base model for text generation.",
76 cause,
77 }),
78 });
79 return yield* runtime.prompt({
80 prompt: [{ type: "text", text: prompt }],
81 });
82 }).pipe(
83 Effect.timeoutOption(GROK_TIMEOUT_MS),
84 Effect.flatMap(
85 Option.match({
86 onNone: () =>
87 Effect.fail(
88 new TextGenerationError({ operation, detail: "Grok ACP request timed out." }),
89 ),
90 onSome: (value) => Effect.succeed(value),
91 }),
92 ),
93 Effect.mapError((cause: EffectAcpErrors.AcpError | TextGenerationError) =>
94 isTextGenerationError(cause)
95 ? cause
96 : new TextGenerationError({
97 operation,
98 detail: "Grok ACP request failed.",
99 cause,
100 }),
101 ),
102 );
103​
104 const trimmed = (yield* Ref.get(outputRef)).trim();
105 if (!trimmed) {
106 return yield* new TextGenerationError({
107 operation,
108 detail:
109 promptResult.stopReason === "cancelled"
110 ? "Grok ACP request was cancelled."
111 : "Grok Agent returned empty output.",
112 });
113 }
114​
115 return yield* TextGenerationOperations.decodeJsonReply(request, "Grok Agent", trimmed);
116 }).pipe(
117 Effect.mapError((cause) =>
118 isTextGenerationError(cause)
119 ? cause
120 : new TextGenerationError({
121 operation,
122 detail: "Grok ACP text generation failed.",
123 cause,
124 }),
125 ),
126 Effect.scoped,
127 );
128 };
129​
130 return TextGenerationOperations.fromRunner("GrokTextGeneration", runGrokJson);
131});