@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, 04:45 UTC

main
ts 145 lines 5.52 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off
2 import * as NodePath from "node:path";
3 import * as NodeOS from "node:os";
4 import * as NodeURL from "node:url";
5 import * as NodeFS from "node:fs";
6
7 import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
8 import * as NodeServices from "@effect/platform-node/NodeServices";
9 import { it } from "@effect/vitest";
10 import * as Effect from "effect/Effect";
11 import * as Layer from "effect/Layer";
12 import * as Schema from "effect/Schema";
13 import { createModelSelection } from "@t3tools/shared/model";
14 import { expect } from "vite-plus/test";
15 import { ProviderInstanceId } from "@t3tools/contracts";
16 import { GrokSettings } from "@t3tools/provider-grok/settings";
17
18 import * as ServerConfig from "../config.ts";
19 import * as TextGeneration from "./TextGeneration.ts";
20 import { makeGrokTextGeneration } from "@t3tools/provider-grok/testing";
21 import { execScriptSource, writeFakeCli } from "@t3tools/provider-testing/fakeCli";
22 const decodeGrokSettings = Schema.decodeSync(GrokSettings);
23
24 const __dirname = NodePath.dirname(NodeURL.fileURLToPath(import.meta.url));
25 const mockAgentPath = NodePath.join(__dirname, "../provider/testFixtures/grok-text-mock-agent.mjs");
26
27 const layerGrokTextGenerationTest = ServerConfig.ServerConfig.layerTest(process.cwd(), {
28 prefix: "t3code-grok-text-generation-test-",
29 }).pipe(Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)));
30
31 function makeAcpGrokWrapper(dir: string, env: Record<string, string>): string {
32 return writeFakeCli({
33 directory: NodePath.join(dir, "bin"),
34 name: "grok",
35 env,
36 source: execScriptSource({
37 scriptPath: mockAgentPath,
38 expectedArgs: ["--permission-mode", "default", "agent", "stdio"],
39 }),
40 });
41 }
42
43 function withFakeAcpGrok<A, E, R>(
44 env: Record<string, string>,
45 effectFn: (textGeneration: TextGeneration.TextGeneration["Service"]) => Effect.Effect<A, E, R>,
46 ) {
47 return Effect.gen(function* () {
48 const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3code-grok-text-acp-"));
49 yield* Effect.addFinalizer(() =>
50 Effect.sync(() => {
51 NodeFS.rmSync(tempDir, { recursive: true, force: true });
52 }),
53 );
54 const binaryPath = makeAcpGrokWrapper(tempDir, env);
55 const config = decodeGrokSettings({ binaryPath });
56 const textGeneration = yield* makeGrokTextGeneration(config);
57 return yield* effectFn(textGeneration);
58 }).pipe(Effect.scoped);
59 }
60
61 function readJsonRpcRequests(filePath: string): ReadonlyArray<{
62 readonly id?: unknown;
63 readonly method?: string;
64 readonly params?: Record<string, unknown>;
65 }> {
66 return NodeFS.readFileSync(filePath, "utf8")
67 .trim()
68 .split("\n")
69 .filter((line) => line.length > 0)
70 .map(
71 (line) =>
72 JSON.parse(line) as { id?: unknown; method?: string; params?: Record<string, unknown> },
73 );
74 }
75
76 it.layer(layerGrokTextGenerationTest)("GrokTextGeneration", (it) => {
77 it.effect("uses ACP with disabled tool capabilities and forwards the requested model id", () => {
78 const requestLogDir = NodeFS.mkdtempSync(
79 NodePath.join(NodeOS.tmpdir(), "t3code-grok-text-log-"),
80 );
81 const requestLogPath = NodePath.join(requestLogDir, "requests.ndjson");
82
83 return withFakeAcpGrok(
84 {
85 T3_ACP_REQUEST_LOG_PATH: requestLogPath,
86 T3_ACP_REQUEST_PERMISSION: "1",
87 T3_ACP_PROMPT_RESPONSE_TEXT: JSON.stringify({
88 subject: "Add Grok provider",
89 body: "Wire up the ACP runtime and headless text generation path.",
90 }),
91 },
92 (textGeneration) =>
93 Effect.gen(function* () {
94 const generated = yield* textGeneration.generateCommitMessage({
95 cwd: process.cwd(),
96 branch: "feature/grok",
97 stagedSummary: "M apps/server/src/provider/Drivers/GrokDriver.ts",
98 stagedPatch: "diff --git a/.../GrokDriver.ts b/.../GrokDriver.ts",
99 modelSelection: createModelSelection(ProviderInstanceId.make("grok"), "grok-mock-alt"),
100 });
101
102 expect(generated.subject).toBe("Add Grok provider");
103 expect(generated.body).toBe("Wire up the ACP runtime and headless text generation path.");
104
105 const requests = readJsonRpcRequests(requestLogPath);
106 expect(
107 requests.find((request) => request.method === "initialize")?.params?.clientCapabilities,
108 ).toMatchObject({
109 fs: { readTextFile: false, writeTextFile: false },
110 terminal: false,
111 });
112 expect(
113 requests.some(
114 (request) =>
115 request.method === "session/set_model" &&
116 request.params?.modelId === "grok-mock-alt",
117 ),
118 ).toBe(true);
119 expect(requests.find((request) => request.id === 9001)).toMatchObject({
120 result: { outcome: { outcome: "cancelled" } },
121 });
122 }),
123 );
124 });
125
126 it.effect("extracts the JSON object when Grok wraps it in conversational text", () =>
127 withFakeAcpGrok(
128 {
129 T3_ACP_PROMPT_RESPONSE_TEXT:
130 "Sure! Here's a thread title:\n\n" +
131 JSON.stringify({ title: "Investigate failing CI" }) +
132 "\n\nLet me know if you need anything else.",
133 },
134 (textGeneration) =>
135 Effect.gen(function* () {
136 const generated = yield* textGeneration.generateThreadTitle({
137 cwd: process.cwd(),
138 message: "the lint job is red",
139 modelSelection: createModelSelection(ProviderInstanceId.make("grok"), "grok-mock-alt"),
140 });
141 expect(generated.title).toBe("Investigate failing CI");
142 }),
143 ),
144 );
145 });