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

GrokTextGeneration.ts

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