@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 147 lines 5.09 KB
Raw
1 import * as SecureRandom from "effect/crypto/SecureRandom";
2 import * as Effect from "effect/Effect";
3 import * as Option from "effect/Option";
4 import * as Ref from "effect/Ref";
5 import * as Schema from "effect/Schema";
6 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
7 import type * as EffectAcpErrors from "effect-acp/errors";
8
9 import { TextGenerationError } from "@t3tools/contracts";
10 import type { GrokSettings } from "../settings.ts";
11 import { getModelSelectionStringOptionValue } from "@t3tools/shared/model";
12
13 import * as TextGenerationOperations from "@t3tools/provider-core/server/textGenerationOperations";
14 import {
15 applyGrokAcpModelSelection,
16 currentGrokModelIdFromSessionSetup,
17 currentGrokReasoningEffortFromSessionSetup,
18 makeGrokAcpRuntime,
19 resolveGrokAcpBaseModelId,
20 } from "./acpSupport.ts";
21
22 const GROK_TIMEOUT_MS = 180_000;
23
24 const isTextGenerationError = Schema.is(TextGenerationError);
25
26 export const makeGrokTextGeneration = Effect.fn("makeGrokTextGeneration")(function* (
27 grokSettings: GrokSettings,
28 environment: NodeJS.ProcessEnv = process.env,
29 ) {
30 const random = yield* SecureRandom.SecureRandom;
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 // Grok config may always-approve; ask mode sends tool approvals here, and none are granted.
44 runtimeMode: "approval-required",
45 }).pipe(
46 Effect.provideService(SecureRandom.SecureRandom, random),
47 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, commandSpawner),
48 );
49
50 yield* runtime.handleRequestPermission(() =>
51 Effect.succeed({ outcome: { outcome: "cancelled" as const } }),
52 );
53
54 yield* runtime.handleSessionUpdate((notification) => {
55 const update = notification.update;
56 if (update.sessionUpdate !== "agent_message_chunk") {
57 return Effect.void;
58 }
59 const content = update.content;
60 if (content.type !== "text") {
61 return Effect.void;
62 }
63 return Ref.update(outputRef, (current) => current + content.text);
64 });
65
66 const promptResult = yield* Effect.gen(function* () {
67 const resolvedModel = resolveGrokAcpBaseModelId(modelSelection.model);
68 const started = yield* runtime.start();
69 const requestedReasoningEffort = getModelSelectionStringOptionValue(
70 modelSelection,
71 "reasoningEffort",
72 );
73 yield* applyGrokAcpModelSelection({
74 runtime,
75 currentModelId: currentGrokModelIdFromSessionSetup(started.sessionSetupResult),
76 currentReasoningEffort: currentGrokReasoningEffortFromSessionSetup(
77 started.sessionSetupResult,
78 ),
79 requestedModelId: resolvedModel,
80 requestedReasoningEffort,
81 mapError: (cause) =>
82 new TextGenerationError({
83 operation,
84 detail: "Failed to set Grok ACP base model for text generation.",
85 cause,
86 }),
87 });
88 return yield* runtime.prompt({
89 prompt: [{ type: "text", text: prompt }],
90 });
91 }).pipe(
92 Effect.timeoutOption(GROK_TIMEOUT_MS),
93 Effect.flatMap(
94 Option.match({
95 onNone: () =>
96 Effect.fail(
97 new TextGenerationError({ operation, detail: "Grok ACP request timed out." }),
98 ),
99 onSome: (value) => Effect.succeed(value),
100 }),
101 ),
102 Effect.mapError((cause: EffectAcpErrors.AcpError | TextGenerationError) =>
103 isTextGenerationError(cause)
104 ? cause
105 : new TextGenerationError({
106 operation,
107 detail: "Grok ACP request failed.",
108 cause,
109 }),
110 ),
111 );
112
113 const trimmed = (yield* Ref.get(outputRef)).trim();
114 if (!trimmed) {
115 return yield* new TextGenerationError({
116 operation,
117 detail:
118 promptResult.stopReason === "cancelled"
119 ? "Grok ACP request was cancelled."
120 : "Grok Agent returned empty output.",
121 });
122 }
123
124 return yield* TextGenerationOperations.decodeJsonReply(request, "Grok Agent", trimmed);
125 },
126 (effect, request) => {
127 const { operation } = request;
128 return effect.pipe(
129 Effect.mapError((cause) =>
130 isTextGenerationError(cause)
131 ? cause
132 : new TextGenerationError({
133 operation,
134 detail: "Grok ACP text generation failed.",
135 cause,
136 }),
137 ),
138 Effect.scoped,
139 );
140 },
141 );
142
143 return TextGenerationOperations.fromRunner(
144 "GrokTextGeneration",
145 TextGenerationOperations.withoutInvestigation("Grok", runGrokJson),
146 );
147 });