Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import * as NodePath from "node:path";
import * as NodeOS from "node:os";
import * as NodeURL from "node:url";
import * as NodeFS from "node:fs";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";
import { createModelSelection } from "@akeru/shared/model";
import { expect } from "vite-plus/test";
import { GrokSettings, ProviderInstanceId } from "@akeru/contracts";
import * as ServerConfig from "../config.ts";
import * as TextGeneration from "./TextGeneration.ts";
import { makeGrokTextGeneration } from "./GrokTextGeneration.ts";
import { testSubscriptionAuthServiceForSecretsDir } from "../subscription-auth/testUtils/subscriptionAuthService.ts";
const decodeGrokSettings = Schema.decodeSync(GrokSettings);
const __dirname = NodePath.dirname(NodeURL.fileURLToPath(import.meta.url));
const mockAgentPath = NodePath.join(__dirname, "../../scripts/acp-mock-agent.ts");
function shellSingleQuote(value: string): string {
return `'${value.replaceAll("'", `'"'"'`)}'`;
}
const GrokTextGenerationTestLayer = ServerConfig.ServerConfig.layerTest(process.cwd(), {
prefix: "t3code-grok-text-generation-test-",
}).pipe(Layer.provideMerge(NodeServices.layer));
function makeAcpGrokWrapper(dir: string, env: Record<string, string>): string {
const binDir = NodePath.join(dir, "bin");
const grokPath = NodePath.join(binDir, "grok");
NodeFS.mkdirSync(binDir, { recursive: true });
NodeFS.writeFileSync(
grokPath,
[
"#!/bin/sh",
...Object.entries(env).map(([key, value]) => `export ${key}=${shellSingleQuote(value)}`),
'if [ "$1" != "agent" ] || [ "$2" != "stdio" ]; then',
' printf "%s\\n" "unexpected args: $*" >&2',
" exit 11",
"fi",
'if [ -n "$T3_TEST_XAI_KEY_LOG" ]; then printf "%s" "$XAI_API_KEY" > "$T3_TEST_XAI_KEY_LOG"; fi',
`exec ${JSON.stringify(process.execPath)} ${JSON.stringify(mockAgentPath)}`,
"",
].join("\n"),
"utf8",
);
NodeFS.chmodSync(grokPath, 0o755);
return grokPath;
}
function withFakeAcpGrok<A, E, R>(
env: Record<string, string>,
effectFn: (textGeneration: TextGeneration.TextGeneration["Service"]) => Effect.Effect<A, E, R>,
credentials?: { readonly secretsDir: string; readonly instanceId: string },
) {
return Effect.gen(function* () {
const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3code-grok-text-acp-"));
yield* Effect.addFinalizer(() =>
Effect.sync(() => {
NodeFS.rmSync(tempDir, { recursive: true, force: true });
}),
);
const binaryPath = makeAcpGrokWrapper(tempDir, env);
const config = decodeGrokSettings({ binaryPath });
const textGeneration = yield* makeGrokTextGeneration(
config,
{},
credentials?.secretsDir,
credentials?.instanceId,
);
return yield* effectFn(textGeneration);
}).pipe(Effect.scoped);
}
function readJsonRpcRequests(
filePath: string,
): ReadonlyArray<{ readonly method?: string; readonly params?: Schema.JsonObject }> {
return NodeFS.readFileSync(filePath, "utf8")
.trim()
.split("\n")
.filter((line) => line.length > 0)
.map((line) => JSON.parse(line) as { method?: string; params?: Schema.JsonObject });
}
it.layer(GrokTextGenerationTestLayer)("GrokTextGeneration", (it) => {
it.effect("uses ACP with disabled tool capabilities and forwards the requested model id", () => {
const requestLogDir = NodeFS.mkdtempSync(
NodePath.join(NodeOS.tmpdir(), "t3code-grok-text-log-"),
);
const requestLogPath = NodePath.join(requestLogDir, "requests.ndjson");
return withFakeAcpGrok(
{
T3_ACP_REQUEST_LOG_PATH: requestLogPath,
T3_ACP_PROMPT_RESPONSE_TEXT: JSON.stringify({
title: "Add Grok provider",
}),
},
(textGeneration) =>
Effect.gen(function* () {
const generated = yield* textGeneration.generateThreadTitle({
cwd: process.cwd(),
message: "Add important change",
modelSelection: createModelSelection(ProviderInstanceId.make("grok"), "grok-mock-alt"),
});
expect(generated.title).toBe("Add Grok provider");
const requests = readJsonRpcRequests(requestLogPath);
expect(
requests.find((request) => request.method === "initialize")?.params?.clientCapabilities,
).toMatchObject({
fs: { readTextFile: false, writeTextFile: false },
terminal: false,
});
expect(
requests.some(
(request) =>
request.method === "session/set_model" &&
request.params?.modelId === "grok-mock-alt",
),
).toBe(true);
}),
);
});
it.effect("extracts the JSON object when Grok wraps it in conversational text", () =>
withFakeAcpGrok(
{
T3_ACP_PROMPT_RESPONSE_TEXT:
"Sure! Here's a thread title:\n\n" +
JSON.stringify({ title: "Investigate failing CI" }) +
"\n\nLet me know if you need anything else.",
},
(textGeneration) =>
Effect.gen(function* () {
const generated = yield* textGeneration.generateThreadTitle({
cwd: process.cwd(),
message: "the lint job is red",
modelSelection: createModelSelection(ProviderInstanceId.make("grok"), "grok-mock-alt"),
});
expect(generated.title).toBe("Investigate failing CI");
}),
),
);
it.effect("surfaces ACP request failures as text generation errors", () =>
withFakeAcpGrok(
{
T3_ACP_PROMPT_RESPONSE_TEXT: JSON.stringify({ branch: "unreachable" }),
},
(textGeneration) =>
Effect.gen(function* () {
const error = yield* Effect.flip(
textGeneration.generateBranchName({
cwd: process.cwd(),
message: "wire up grok",
modelSelection: createModelSelection(
ProviderInstanceId.make("grok"),
"missing-grok-model",
),
}),
);
expect(error._tag).toBe("TextGenerationError");
expect(error.detail).toContain("Grok ACP base model");
}),
),
);
it.effect("fails with TextGenerationError when output is empty", () =>
withFakeAcpGrok(
{
T3_ACP_PROMPT_RESPONSE_TEXT: " \n ",
},
(textGeneration) =>
Effect.gen(function* () {
const error = yield* Effect.flip(
textGeneration.generateThreadTitle({
cwd: process.cwd(),
message: "anything",
modelSelection: createModelSelection(ProviderInstanceId.make("grok"), "grok-build"),
}),
);
expect(error._tag).toBe("TextGenerationError");
expect(error.detail).toMatch(/empty/i);
}),
),
);
it.effect("fails with TextGenerationError when output is unparseable JSON", () =>
withFakeAcpGrok(
{
T3_ACP_PROMPT_RESPONSE_TEXT: "totally not json output from a confused model",
},
(textGeneration) =>
Effect.gen(function* () {
const error = yield* Effect.flip(
textGeneration.generateThreadTitle({
cwd: process.cwd(),
message: "anything",
modelSelection: createModelSelection(ProviderInstanceId.make("grok"), "grok-build"),
}),
);
expect(error._tag).toBe("TextGenerationError");
expect(error.detail).toMatch(/invalid structured output/i);
}),
),
);
it.effect("uses the saved key of the instance it is bound to", () => {
const secretsDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3code-grok-text-auth-"));
const keyLogPath = NodePath.join(secretsDir, "xai-key.txt");
return Effect.gen(function* () {
yield* Effect.addFinalizer(() =>
Effect.sync(() => NodeFS.rmSync(secretsDir, { recursive: true, force: true })),
);
yield* Effect.promise(async () => {
const auth = await testSubscriptionAuthServiceForSecretsDir(secretsDir);
const personal = await auth.startLogin("xai", { authMode: "api-key" });
await auth.completeLogin(personal.loginId, "personal-key");
const work = await auth.startLogin("xai", {
authMode: "api-key",
instanceId: ProviderInstanceId.make("grok_work"),
});
await auth.completeLogin(work.loginId, "work-key");
});
yield* withFakeAcpGrok(
{
T3_TEST_XAI_KEY_LOG: keyLogPath,
T3_ACP_PROMPT_RESPONSE_TEXT: '{"title":"Work title"}',
},
(textGeneration) =>
textGeneration.generateThreadTitle({
cwd: process.cwd(),
message: "anything",
modelSelection: createModelSelection(
ProviderInstanceId.make("grok_work"),
"grok-build",
),
}),
{ secretsDir, instanceId: "grok_work" },
);
expect(NodeFS.readFileSync(keyLogPath, "utf8")).toBe("work-key");
}).pipe(Effect.scoped);
});
});