Open History to see the last commit
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 * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
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 = Effect.fn("GrokTextGeneration.runGrokJson")(
34 function* (request) {
35 const { operation, cwd, prompt, modelSelection } = request;
36
37 const outputRef = yield* Ref.make("");
38 const runtime = yield* makeGrokAcpRuntime({
39 grokSettings,
40 environment,
41 cwd,
42 clientInfo: { name: "t3-code-git-text", version: "0.0.0" },
43 }).pipe(
44 Effect.provideService(Crypto.Crypto, crypto),
45 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, commandSpawner),
46 );
47
48 yield* runtime.handleSessionUpdate((notification) => {
49 const update = notification.update;
50 if (update.sessionUpdate !== "agent_message_chunk") {
51 return Effect.void;
52 }
53 const content = update.content;
54 if (content.type !== "text") {
55 return Effect.void;
56 }
57 return Ref.update(outputRef, (current) => current + content.text);
58 });
59
60 const promptResult = yield* Effect.gen(function* () {
61 const resolvedModel = resolveGrokAcpBaseModelId(modelSelection.model);
62 const started = yield* runtime.start();
63 const requestedReasoningEffort = getModelSelectionStringOptionValue(
64 modelSelection,
65 "reasoningEffort",
66 );
67 yield* applyGrokAcpModelSelection({
68 runtime,
69 currentModelId: currentGrokModelIdFromSessionSetup(started.sessionSetupResult),
70 currentReasoningEffort: currentGrokReasoningEffortFromSessionSetup(
71 started.sessionSetupResult,
72 ),
73 requestedModelId: resolvedModel,
74 requestedReasoningEffort,
75 mapError: (cause) =>
76 new TextGenerationError({
77 operation,
78 detail: "Failed to set Grok ACP base model for text generation.",
79 cause,
80 }),
81 });
82 return yield* runtime.prompt({
83 prompt: [{ type: "text", text: prompt }],
84 });
85 }).pipe(
86 Effect.timeoutOption(GROK_TIMEOUT_MS),
87 Effect.flatMap(
88 Option.match({
89 onNone: () =>
90 Effect.fail(
91 new TextGenerationError({ operation, detail: "Grok ACP request timed out." }),
92 ),
93 onSome: (value) => Effect.succeed(value),
94 }),
95 ),
96 Effect.mapError((cause: EffectAcpErrors.AcpError | TextGenerationError) =>
97 isTextGenerationError(cause)
98 ? cause
99 : new TextGenerationError({
100 operation,
101 detail: "Grok ACP request failed.",
102 cause,
103 }),
104 ),
105 );
106
107 const trimmed = (yield* Ref.get(outputRef)).trim();
108 if (!trimmed) {
109 return yield* new TextGenerationError({
110 operation,
111 detail:
112 promptResult.stopReason === "cancelled"
113 ? "Grok ACP request was cancelled."
114 : "Grok Agent returned empty output.",
115 });
116 }
117
118 return yield* TextGenerationOperations.decodeJsonReply(request, "Grok Agent", trimmed);
119 },
120 (effect, request) => {
121 const { operation } = request;
122 return effect.pipe(
123 Effect.mapError((cause) =>
124 isTextGenerationError(cause)
125 ? cause
126 : new TextGenerationError({
127 operation,
128 detail: "Grok ACP text generation failed.",
129 cause,
130 }),
131 ),
132 Effect.scoped,
133 );
134 },
135 );
136
137 return TextGenerationOperations.fromRunner(
138 "GrokTextGeneration",
139 TextGenerationOperations.withoutInvestigation("Grok", runGrokJson),
140 );
141});