@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 144 lines 7.14 KB
Raw
1 import { assert, it } from "@effect/vitest";
2 import * as Clock from "effect/Clock";
3 import * as Effect from "effect/Effect";
4 import * as Fiber from "effect/Fiber";
5 import * as Layer from "effect/Layer";
6 import * as TestClock from "effect/testing/TestClock";
7 import { HttpClient, HttpClientResponse } from "effect/http";
8 import { describe } from "vite-plus/test";
9
10 import { loadOpenCode2Catalog } from "./openCode2Catalog.ts";
11 import { makeOpenCode2ModelLoader } from "./status.ts";
12 import * as OpenCode2Client from "./v2/OpenCode2Client.ts";
13
14 const MODELS_BODY = `{"location":{"directory":"/project"},"data":[{"id":"step-5-preview-free","modelID":"step-5-preview-free","providerID":"opencode-go","name":"Step 5 Preview Free","compatibility":{"reasoningField":"reasoning_content"},"package":"@opencode/ai/providers/openai-compatible","settings":{"apiKey":"sk-demo-not-real","baseURL":"http://127.0.0.1:48990/v1","provider":"opencode-go"},"capabilities":{"tools":true,"input":["text","image","video"],"output":["text"]},"variants":[{"id":"low","settings":{"reasoningEffort":"low"}},{"id":"medium","settings":{"reasoningEffort":"medium"}},{"id":"high","settings":{"reasoningEffort":"high"}}],"time":{"released":1789516800000},"cost":[{"input":0,"output":0,"cache":{"read":0,"write":0}}],"status":"active","enabled":true,"limit":{"context":1000000,"input":1000000,"output":65536}},{"id":"step-5-preview-free","modelID":"step-5-preview-free","providerID":"opencode","name":"Step 5 Preview Free","compatibility":{"reasoningField":"reasoning_content"},"package":"@opencode/ai/providers/openai-compatible","settings":{"apiKey":"public","baseURL":"https://opencode.ai/zen/v1","provider":"opencode"},"capabilities":{"tools":true,"input":["text","image","video"],"output":["text"]},"variants":[{"id":"low","settings":{"reasoningEffort":"low"}},{"id":"medium","settings":{"reasoningEffort":"medium"}},{"id":"high","settings":{"reasoningEffort":"high"}}],"time":{"released":1789516800000},"cost":[{"input":0,"output":0,"cache":{"read":0,"write":0}}],"status":"active","enabled":true,"limit":{"context":1000000,"input":1000000,"output":65536}}]}`;
15
16 const PROVIDERS_BODY = `{"location":{"directory":"/project"},"data":[{"id":"opencode-go","integrationID":"opencode-go","name":"OpenCode Go","activation":"enabled","package":"@opencode/ai/providers/openai-compatible","settings":{"apiKey":"sk-demo-not-real","baseURL":"http://127.0.0.1:48990/v1","provider":"opencode-go"}},{"id":"opencode","integrationID":"opencode","name":"OpenCode Zen","activation":"enabled","package":"@opencode/ai/providers/openai-compatible","settings":{"apiKey":"public","baseURL":"https://opencode.ai/zen/v1"}}]}`;
17
18 const PLUGINS_ACTIVATING_BODY =
19 '{"_tag":"ServiceUnavailableError","message":"Location plugins did not finish activating","service":"plugin"}';
20
21 type Reply = string | { readonly status: number; readonly body: string };
22
23 const layerServerReplying = (
24 bodies: Readonly<Record<string, Reply | Effect.Effect<Reply>>>,
25 hanging: ReadonlyArray<string> = [],
26 ) =>
27 Layer.succeed(
28 HttpClient.HttpClient,
29 HttpClient.make((request) =>
30 Effect.gen(function* () {
31 const path = new URL(request.url).pathname;
32 if (hanging.includes(path)) {
33 return yield* Effect.never;
34 }
35 const recorded = bodies[path];
36 const reply = Effect.isEffect(recorded) ? yield* recorded : recorded;
37 const { status, body } =
38 reply === undefined
39 ? { status: 404, body: '{"_tag":"NotFoundError","message":"not recorded"}' }
40 : typeof reply === "string"
41 ? { status: 200, body: reply }
42 : reply;
43 return HttpClientResponse.fromWeb(
44 request,
45 new Response(body, { status, headers: { "content-type": "application/json" } }),
46 );
47 }),
48 ),
49 );
50
51 const readCatalog = (
52 bodies: Readonly<Record<string, Reply | Effect.Effect<Reply>>>,
53 hanging: ReadonlyArray<string> = [],
54 ) =>
55 Effect.gen(function* () {
56 const opencode = yield* OpenCode2Client.OpenCode2Client;
57 const { client } = yield* opencode.connect({
58 baseUrl: "http://127.0.0.1:4096",
59 password: "secret",
60 });
61 return yield* loadOpenCode2Catalog(client, "/project");
62 }).pipe(
63 Effect.provide(OpenCode2Client.layer.pipe(Layer.provide(layerServerReplying(bodies, hanging)))),
64 );
65
66 describe("loadOpenCode2Catalog", () => {
67 it.effect("names the provider each listed model came from", () =>
68 Effect.gen(function* () {
69 const models = yield* readCatalog({
70 "/api/model": MODELS_BODY,
71 "/api/provider": PROVIDERS_BODY,
72 });
73
74 assert.deepStrictEqual(
75 models.map((model) => [`${model.providerID}/${model.id}`, model.providerName]),
76 [
77 ["opencode-go/step-5-preview-free", "OpenCode Go"],
78 ["opencode/step-5-preview-free", "OpenCode Zen"],
79 ],
80 );
81 }),
82 );
83
84 it.effect("leaves a model unnamed when the provider list fails", () =>
85 Effect.gen(function* () {
86 const models = yield* readCatalog({ "/api/model": MODELS_BODY });
87
88 assert.deepStrictEqual(
89 models.map((model) => [`${model.providerID}/${model.id}`, model.providerName]),
90 [
91 ["opencode-go/step-5-preview-free", undefined],
92 ["opencode/step-5-preview-free", undefined],
93 ],
94 );
95 }),
96 );
97
98 // regression: #17424 only the provider list is optional; a model-list failure fails the read
99 it.effect("fails the read when the model list fails", () =>
100 Effect.gen(function* () {
101 const error = yield* readCatalog({
102 "/api/model": '{"_tag":"InternalServerError","message":"catalog unavailable"}',
103 "/api/provider": PROVIDERS_BODY,
104 }).pipe(Effect.flip);
105
106 assert.strictEqual(error.operation, "model.list");
107 assert.match(error.detail, /could not list its models/i);
108 }),
109 );
110
111 // regression: #30251 a busy cold OpenCode 2 holds each model read past 5 seconds, then 503s
112 it.effect("lists models once OpenCode 2 finishes activating its plugins", () =>
113 Effect.gen(function* () {
114 const activatedAt = (yield* Clock.currentTimeMillis) + 60_000;
115 // Timings measured from OpenCode 2.2.7 on a CPU-throttled cold start.
116 const listModels = Effect.gen(function* () {
117 if ((yield* Clock.currentTimeMillis) >= activatedAt) {
118 return yield* Effect.as(Effect.sleep("9 seconds"), MODELS_BODY);
119 }
120 yield* Effect.sleep("14 seconds");
121 return { status: 503, body: PLUGINS_ACTIVATING_BODY };
122 });
123 const load = yield* makeOpenCode2ModelLoader(
124 readCatalog({ "/api/model": listModels, "/api/provider": PROVIDERS_BODY }),
125 );
126 const fiber = yield* Effect.forkChild(load);
127 yield* TestClock.adjust("2 minutes");
128
129 assert.strictEqual((yield* Fiber.join(fiber)).length, 2);
130 }),
131 );
132
133 it.effect("lists no models once a model list that never answers reaches the deadline", () =>
134 Effect.gen(function* () {
135 const load = yield* makeOpenCode2ModelLoader(
136 readCatalog({ "/api/provider": PROVIDERS_BODY }, ["/api/model"]),
137 );
138 const fiber = yield* Effect.forkChild(load);
139 yield* TestClock.adjust("2 minutes");
140
141 assert.deepStrictEqual(yield* Fiber.join(fiber), []);
142 }),
143 );
144 });