Open History to see the last commit
1import * as NodeOS from "node:os";
2
3import * as NodeServices from "@effect/platform-node/NodeServices";
4import { it as effectIt } from "@effect/vitest";
5import type * as Crypto from "effect/Crypto";
6import * as Effect from "effect/Effect";
7import * as FileSystem from "effect/FileSystem";
8import * as Path from "effect/Path";
9import type * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
10import { describe, expect, it } from "vite-plus/test";
11import type * as EffectAcpSchema from "effect-acp/schema";
12import type { OmpSettings } from "@t3tools/contracts";
13import { createModelCapabilities } from "@t3tools/shared/model";
14
15import {
16 buildOmpProviderSnapshot,
17 buildOmpCapabilitiesFromConfigOptions,
18 checkOmpProviderStatus,
19 discoverOmpModelsViaAcp,
20 getOmpFallbackModels,
21 resolveOmpAcpConfigUpdates,
22} from "./OmpProvider.ts";
23
24const node = <A, E>(
25 effect: Effect.Effect<
26 A,
27 E,
28 ChildProcessSpawner.ChildProcessSpawner | Crypto.Crypto | FileSystem.FileSystem | Path.Path
29 >,
30): Effect.Effect<A, E> => effect.pipe(Effect.provide(NodeServices.layer));
31
32const resolveMockAgentPath = Effect.fn("resolveMockAgentPath")(function* () {
33 const path = yield* Path.Path;
34 return yield* path.fromFileUrl(new URL("../../../scripts/acp-mock-agent.ts", import.meta.url));
35});
36
37function selectDescriptor(
38 id: string,
39 label: string,
40 options: ReadonlyArray<{ id: string; label: string; isDefault?: boolean }>,
41) {
42 return {
43 id,
44 label,
45 type: "select" as const,
46 options: [...options],
47 ...(options.find((option) => option.isDefault)?.id
48 ? { currentValue: options.find((option) => option.isDefault)?.id }
49 : {}),
50 };
51}
52
53const makeMockAgentWrapper = Effect.fn("makeMockAgentWrapper")(function* (
54 extraEnv?: Record<string, string>,
55) {
56 const fileSystem = yield* FileSystem.FileSystem;
57 const path = yield* Path.Path;
58 const mockAgentPath = yield* resolveMockAgentPath();
59 const dir = yield* fileSystem.makeTempDirectory({
60 directory: NodeOS.tmpdir(),
61 prefix: "omp-provider-mock-",
62 });
63 const wrapperPath = path.join(dir, "fake-omp.sh");
64 const mockAgentCommand = ["node", mockAgentPath].map((arg) => JSON.stringify(arg)).join(" ");
65 const envExports = Object.entries({ T3_ACP_OMP_SHAPES: "1", ...extraEnv })
66 .map(([key, value]) => `export ${key}=${JSON.stringify(value)}`)
67 .join("\n");
68 const script = `#!/bin/sh
69${envExports}
70exec ${mockAgentCommand} "$@"
71`;
72 yield* fileSystem.writeFileString(wrapperPath, script);
73 yield* fileSystem.chmod(wrapperPath, 0o755);
74 return wrapperPath;
75});
76
77const makeMockAgentWithVersionWrapper = Effect.fn("makeMockAgentWithVersionWrapper")(function* () {
78 const fileSystem = yield* FileSystem.FileSystem;
79 const path = yield* Path.Path;
80 const mockAgentPath = yield* resolveMockAgentPath();
81 const dir = yield* fileSystem.makeTempDirectory({
82 directory: NodeOS.tmpdir(),
83 prefix: "omp-provider-version-mock-",
84 });
85 const wrapperPath = path.join(dir, "fake-omp.sh");
86 const mockAgentCommand = ["node", mockAgentPath].map((arg) => JSON.stringify(arg)).join(" ");
87 const script = `#!/bin/sh
88export T3_ACP_OMP_SHAPES=1
89if [ "$1" = "--version" ]; then
90 printf 'omp/18.0.6\\n'
91 exit 0
92fi
93exec ${mockAgentCommand} "$@"
94`;
95 yield* fileSystem.writeFileString(wrapperPath, script);
96 yield* fileSystem.chmod(wrapperPath, 0o755);
97 return wrapperPath;
98});
99
100const waitForFileContent = Effect.fn("waitForFileContent")(function* (
101 filePath: string,
102 attempts = 40,
103) {
104 const fileSystem = yield* FileSystem.FileSystem;
105 for (let attempt = 0; attempt < attempts; attempt += 1) {
106 const content = yield* fileSystem
107 .readFileString(filePath)
108 .pipe(Effect.catch(() => Effect.void));
109 if (content !== undefined) {
110 if (content.trim().length > 0) {
111 return content;
112 }
113 }
114 yield* Effect.sleep("50 millis");
115 }
116 return yield* Effect.die(`Timed out waiting for file content at ${filePath}`);
117});
118
119const makeProviderStatusEnvFixture = Effect.fn("makeProviderStatusEnvFixture")(function* () {
120 const fileSystem = yield* FileSystem.FileSystem;
121 const path = yield* Path.Path;
122 const tempDir = yield* fileSystem.makeTempDirectory({
123 directory: NodeOS.tmpdir(),
124 prefix: "omp-provider-status-env-",
125 });
126 return {
127 requestLogPath: path.join(tempDir, "requests.ndjson"),
128 wrapperPath: yield* makeMockAgentWithVersionWrapper(),
129 };
130});
131
132const makeExitLogFixture = Effect.fn("makeExitLogFixture")(function* (prefix: string) {
133 const fileSystem = yield* FileSystem.FileSystem;
134 const path = yield* Path.Path;
135 const tempDir = yield* fileSystem.makeTempDirectory({
136 directory: NodeOS.tmpdir(),
137 prefix,
138 });
139 const exitLogPath = path.join(tempDir, "exit.log");
140 return {
141 exitLogPath,
142 wrapperPath: yield* makeMockAgentWrapper({
143 T3_ACP_EXIT_LOG_PATH: exitLogPath,
144 }),
145 };
146});
147
148const ompConfigOptions = [
149 {
150 type: "select",
151 currentValue: "default",
152 options: [
153 { name: "Default", value: "default" },
154 { name: "Plan", value: "plan" },
155 ],
156 category: "mode",
157 id: "mode",
158 name: "Mode",
159 },
160 {
161 type: "select",
162 currentValue: "zhipu-coding-plan/glm-5.3",
163 options: [
164 { name: "GLM 5.3", value: "zhipu-coding-plan/glm-5.3" },
165 { name: "Claude Opus 4.6", value: "anthropic/claude-opus-4-6" },
166 { name: "GPT-5.4", value: "openai/gpt-5.4" },
167 ],
168 category: "model",
169 id: "model",
170 name: "Model",
171 },
172 {
173 type: "select",
174 currentValue: "high",
175 options: [
176 { name: "Off", value: "off" },
177 { name: "Low", value: "low" },
178 { name: "Medium", value: "medium" },
179 { name: "High", value: "high" },
180 { name: "Max", value: "max" },
181 ],
182 category: "thought_level",
183 id: "thinking",
184 name: "Thinking",
185 },
186] satisfies ReadonlyArray<EffectAcpSchema.SessionConfigOption>;
187
188const baseOmpSettings: OmpSettings = {
189 enabled: true,
190 binaryPath: "omp",
191 customModels: [],
192};
193const missingOmpBinaryPath = "/definitely/not/installed/t3-omp";
194const ompCliCommandMissingMessage = [
195 `Oh My Pi CLI command \`${missingOmpBinaryPath}\` was not found.`,
196 `Install or enable the omp CLI, make sure \`${missingOmpBinaryPath}\` is on PATH, then restart T3 Code.`,
197 "See https://github.com/can1357/oh-my-pi.",
198].join(" ");
199
200describe("getOmpFallbackModels", () => {
201 it("does not publish any built-in omp models before ACP discovery", () => {
202 expect(
203 getOmpFallbackModels({
204 customModels: ["internal/omp-model"],
205 }).map((model) => model.slug),
206 ).toEqual(["internal/omp-model"]);
207 });
208});
209
210describe("buildOmpProviderSnapshot", () => {
211 it("downgrades ready status to warning when ACP model discovery times out", () => {
212 expect(
213 buildOmpProviderSnapshot({
214 checkedAt: "2026-01-01T00:00:00.000Z",
215 ompSettings: baseOmpSettings,
216 version: "18.0.6",
217 discoveryWarning: "Oh My Pi ACP model discovery timed out after 15000ms.",
218 }),
219 ).toMatchObject({
220 status: "warning",
221 message: "Oh My Pi ACP model discovery timed out after 15000ms.",
222 models: [],
223 });
224 });
225
226 it("preserves provider error state while appending discovery warnings", () => {
227 expect(
228 buildOmpProviderSnapshot({
229 checkedAt: "2026-01-01T00:00:00.000Z",
230 ompSettings: {
231 ...baseOmpSettings,
232 customModels: ["internal/omp-model"],
233 },
234 version: "18.0.6",
235 status: "error",
236 message: "Oh My Pi CLI is installed but failed to run.",
237 discoveryWarning: "Oh My Pi ACP model discovery failed.",
238 }),
239 ).toMatchObject({
240 status: "error",
241 message: "Oh My Pi CLI is installed but failed to run. Oh My Pi ACP model discovery failed.",
242 models: [
243 {
244 slug: "internal/omp-model",
245 isCustom: true,
246 },
247 ],
248 });
249 });
250});
251
252describe("buildOmpCapabilitiesFromConfigOptions", () => {
253 it("maps the omp thought_level select onto a reasoning effort descriptor", () => {
254 expect(buildOmpCapabilitiesFromConfigOptions(ompConfigOptions)).toEqual(
255 createModelCapabilities({
256 optionDescriptors: [
257 selectDescriptor("reasoning", "Thinking", [
258 { id: "off", label: "Off" },
259 { id: "low", label: "Low" },
260 { id: "medium", label: "Medium" },
261 { id: "high", label: "High", isDefault: true },
262 { id: "max", label: "Max" },
263 ]),
264 ],
265 }),
266 );
267 });
268
269 it("exposes auto thinking levels on auto models", () => {
270 expect(
271 buildOmpCapabilitiesFromConfigOptions([
272 {
273 type: "select",
274 currentValue: "auto",
275 options: [
276 { name: "Off", value: "off" },
277 { name: "Auto", value: "auto" },
278 ],
279 category: "thought_level",
280 id: "thinking",
281 name: "Thinking",
282 },
283 ]),
284 ).toEqual(
285 createModelCapabilities({
286 optionDescriptors: [
287 selectDescriptor("reasoning", "Thinking", [
288 { id: "off", label: "Off" },
289 { id: "auto", label: "Auto", isDefault: true },
290 ]),
291 ],
292 }),
293 );
294 });
295
296 it("returns empty capabilities when no config options are advertised", () => {
297 expect(buildOmpCapabilitiesFromConfigOptions([])).toEqual(
298 createModelCapabilities({ optionDescriptors: [] }),
299 );
300 expect(buildOmpCapabilitiesFromConfigOptions(undefined)).toEqual(
301 createModelCapabilities({ optionDescriptors: [] }),
302 );
303 });
304});
305
306describe("checkOmpProviderStatus", () => {
307 effectIt.live("reports the install docs when the omp CLI command is missing", () =>
308 Effect.gen(function* () {
309 const provider = yield* node(
310 checkOmpProviderStatus({
311 enabled: true,
312 binaryPath: missingOmpBinaryPath,
313 customModels: [],
314 }),
315 );
316
317 expect(provider).toMatchObject({
318 installed: false,
319 status: "error",
320 auth: { status: "unknown" },
321 message: ompCliCommandMissingMessage,
322 });
323 }),
324 );
325
326 effectIt.live(
327 "discovers the meta-provider catalog through ACP with the injected environment",
328 () =>
329 Effect.gen(function* () {
330 const { requestLogPath, wrapperPath } = yield* node(makeProviderStatusEnvFixture());
331
332 const provider = yield* node(
333 checkOmpProviderStatus(
334 {
335 enabled: true,
336 binaryPath: wrapperPath,
337 customModels: [],
338 },
339 {
340 ...process.env,
341 T3_ACP_REQUEST_LOG_PATH: requestLogPath,
342 },
343 ),
344 );
345
346 expect(provider).toMatchObject({
347 installed: true,
348 version: "18.0.6",
349 status: "ready",
350 message: "3 upstream providers configured through Oh My Pi.",
351 });
352 expect(provider.models.map((model) => model.slug)).toEqual([
353 "anthropic/claude-opus-4-6",
354 "zhipu-coding-plan/glm-5.3",
355 "openai/gpt-5.4",
356 ]);
357 expect(provider.models.map((model) => model.subProvider)).toEqual([
358 "Anthropic",
359 "Zhipu Coding Plan",
360 "Openai",
361 ]);
362 const requestLog = yield* node(waitForFileContent(requestLogPath));
363 expect(requestLog).toContain("initialize");
364 }),
365 );
366});
367
368describe("discoverOmpModelsViaAcp", () => {
369 effectIt.live("builds the model catalog from the ACP model config option", () =>
370 Effect.gen(function* () {
371 const wrapperPath = yield* node(makeMockAgentWrapper());
372
373 const models = yield* node(
374 discoverOmpModelsViaAcp({
375 enabled: true,
376 binaryPath: wrapperPath,
377 customModels: [],
378 }).pipe(Effect.scoped),
379 );
380
381 expect(models.map((model) => model.slug)).toEqual([
382 "anthropic/claude-opus-4-6",
383 "zhipu-coding-plan/glm-5.3",
384 "openai/gpt-5.4",
385 ]);
386 expect(models[0]).toMatchObject({
387 name: "Claude Opus 4.6",
388 subProvider: "Anthropic",
389 isCustom: false,
390 });
391 }),
392 );
393
394 effectIt.live("closes the ACP probe runtime after discovery completes", () =>
395 Effect.gen(function* () {
396 const { exitLogPath, wrapperPath } = yield* node(
397 makeExitLogFixture("omp-provider-exit-log-"),
398 );
399
400 yield* node(
401 discoverOmpModelsViaAcp({
402 enabled: true,
403 binaryPath: wrapperPath,
404 customModels: [],
405 }),
406 );
407
408 const exitLog = yield* node(waitForFileContent(exitLogPath));
409 expect(exitLog).toContain("SIGTERM");
410 }),
411 );
412});
413
414describe("resolveOmpAcpConfigUpdates", () => {
415 it("maps reasoning selections onto the omp thinking config option", () => {
416 expect(
417 resolveOmpAcpConfigUpdates(ompConfigOptions, [{ id: "reasoning", value: "max" }]),
418 ).toEqual([{ configId: "thinking", value: "max" }]);
419 });
420
421 it("maps reasoning off so the adapter can clear a prior thinking selection", () => {
422 expect(
423 resolveOmpAcpConfigUpdates(ompConfigOptions, [{ id: "reasoning", value: "off" }]),
424 ).toEqual([{ configId: "thinking", value: "off" }]);
425 });
426
427 it("maps reasoning auto onto the omp thinking config option", () => {
428 expect(
429 resolveOmpAcpConfigUpdates(ompConfigOptions, [{ id: "reasoning", value: "auto" }]),
430 ).toEqual([]);
431 expect(
432 resolveOmpAcpConfigUpdates(
433 [
434 {
435 type: "select",
436 currentValue: "off",
437 options: [
438 { name: "Off", value: "off" },
439 { name: "Auto", value: "auto" },
440 ],
441 category: "thought_level",
442 id: "thinking",
443 name: "Thinking",
444 },
445 ],
446 [{ id: "reasoning", value: "auto" }],
447 ),
448 ).toEqual([{ configId: "thinking", value: "auto" }]);
449 });
450
451 it("ignores unknown reasoning values and empty selections", () => {
452 expect(
453 resolveOmpAcpConfigUpdates(ompConfigOptions, [{ id: "reasoning", value: "ludicrous" }]),
454 ).toEqual([]);
455 expect(resolveOmpAcpConfigUpdates(ompConfigOptions, undefined)).toEqual([]);
456 expect(resolveOmpAcpConfigUpdates([], [{ id: "reasoning", value: "max" }])).toEqual([]);
457 });
458});