Open History to see the last commit
1import { type GrokSettings, ProviderDriverKind } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Scope from "effect/Scope";
5import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
6import * as EffectAcpErrors from "effect-acp/errors";
7import type * as EffectAcpSchema from "effect-acp/schema";
8import { normalizeModelSlug } from "@t3tools/shared/model";
9
10import * as AcpSessionRuntime from "./AcpSessionRuntime.ts";
11import { makeXAiPromptCompletionRuntime } from "./XAiAcpExtension.ts";
12
13const GROK_API_KEY_ENV = "XAI_API_KEY";
14const GROK_OAUTH2_REFERRER_ENV = "GROK_OAUTH2_REFERRER";
15const T3_CODE_OAUTH_REFERRER = "t3code";
16const GROK_AUTH_METHOD_API_KEY = "xai.api_key";
17const GROK_AUTH_METHOD_CACHED_TOKEN = "cached_token";
18const GROK_DRIVER_KIND = ProviderDriverKind.make("grok");
19
20type GrokAcpRuntimeGrokSettings = Pick<GrokSettings, "binaryPath">;
21
22interface GrokAcpRuntimeInput extends Omit<
23 AcpSessionRuntime.AcpSessionRuntimeOptions,
24 "authMethodId" | "clientCapabilities" | "spawn"
25> {
26 readonly childProcessSpawner: ChildProcessSpawner.ChildProcessSpawner["Service"];
27 readonly grokSettings: GrokAcpRuntimeGrokSettings | null | undefined;
28 readonly environment?: NodeJS.ProcessEnv;
29}
30
31export function buildGrokAcpSpawnInput(
32 grokSettings: GrokAcpRuntimeGrokSettings | null | undefined,
33 cwd: string,
34 environment?: NodeJS.ProcessEnv,
35): AcpSessionRuntime.AcpSpawnInput {
36 return {
37 command: grokSettings?.binaryPath || "grok",
38 args: ["agent", "stdio"],
39 cwd,
40 env: {
41 ...environment,
42 [GROK_OAUTH2_REFERRER_ENV]: T3_CODE_OAUTH_REFERRER,
43 },
44 };
45}
46
47function resolveGrokAuthMethodId(environment: NodeJS.ProcessEnv | undefined): string {
48 return environment?.[GROK_API_KEY_ENV]?.trim()
49 ? GROK_AUTH_METHOD_API_KEY
50 : GROK_AUTH_METHOD_CACHED_TOKEN;
51}
52
53export const makeGrokAcpRuntime = (
54 input: GrokAcpRuntimeInput,
55): Effect.Effect<
56 AcpSessionRuntime.AcpSessionRuntime["Service"],
57 EffectAcpErrors.AcpError,
58 Scope.Scope
59> =>
60 Effect.gen(function* () {
61 const acpContext = yield* Layer.build(
62 AcpSessionRuntime.layer({
63 ...input,
64 spawn: buildGrokAcpSpawnInput(input.grokSettings, input.cwd, input.environment),
65 authMethodId: resolveGrokAuthMethodId(input.environment),
66 }).pipe(
67 Layer.provide(
68 Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, input.childProcessSpawner),
69 ),
70 ),
71 );
72 const runtime = yield* Effect.service(AcpSessionRuntime.AcpSessionRuntime).pipe(
73 Effect.provide(acpContext),
74 );
75 return yield* makeXAiPromptCompletionRuntime(runtime);
76 });
77
78export function resolveGrokAcpBaseModelId(model: string | null | undefined): string {
79 const trimmed = model?.trim();
80 const base = trimmed && trimmed.length > 0 ? trimmed : "grok-build";
81 return normalizeModelSlug(base, GROK_DRIVER_KIND) ?? "grok-build";
82}
83
84export function currentGrokModelIdFromSessionSetup(
85 sessionSetupResult:
86 | EffectAcpSchema.LoadSessionResponse
87 | EffectAcpSchema.NewSessionResponse
88 | EffectAcpSchema.ResumeSessionResponse,
89): string | undefined {
90 return sessionSetupResult.models?.currentModelId?.trim() || undefined;
91}
92
93export function applyGrokAcpModelSelection<E>(input: {
94 readonly runtime: Pick<AcpSessionRuntime.AcpSessionRuntime["Service"], "setSessionModel">;
95 readonly currentModelId: string | undefined;
96 readonly requestedModelId: string | undefined;
97 readonly mapError: (cause: EffectAcpErrors.AcpError) => E;
98}): Effect.Effect<string | undefined, E> {
99 const shouldSwitchModel =
100 input.requestedModelId !== undefined && input.requestedModelId !== input.currentModelId;
101 if (!shouldSwitchModel) {
102 return Effect.succeed(input.currentModelId);
103 }
104 return input.runtime
105 .setSessionModel(input.requestedModelId)
106 .pipe(Effect.mapError(input.mapError), Effect.as(input.requestedModelId));
107}