@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 390 lines 15.2 KB
Raw
1 import * as NodeServices from "@effect/platform-node/NodeServices";
2 import { assert, describe, it } from "@effect/vitest";
3 import * as Deferred from "effect/Deferred";
4 import type * as Duration from "effect/Duration";
5 import * as Effect from "effect/Effect";
6 import * as Fiber from "effect/Fiber";
7 import * as FileSystem from "effect/FileSystem";
8 import * as Path from "effect/Path";
9 import * as Redacted from "effect/Redacted";
10 import * as Schema from "effect/Schema";
11 import * as Sink from "effect/Sink";
12 import * as Stream from "effect/Stream";
13 import * as ChildProcess from "effect/process/ChildProcess";
14 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
15 import * as TestClock from "effect/testing/TestClock";
16
17 import { FxSettings } from "../settings.ts";
18 import { checkFxProviderStatus, readFxDefaultChoice } from "./status.ts";
19
20 const encoder = new TextEncoder();
21 const settings = Schema.decodeSync(FxSettings)({ enabled: true });
22
23 /** Holds stdout and the exit back until `answered` completes, like a CLI slow to answer. */
24 function processHandle(stdout: string, code: number, answered: Effect.Effect<void> = Effect.void) {
25 return ChildProcessSpawner.makeHandle({
26 pid: ChildProcessSpawner.ProcessId(900_000_101),
27 exitCode: Effect.as(answered, ChildProcessSpawner.ExitCode(code)),
28 isRunning: Effect.succeed(false),
29 kill: () => Effect.void,
30 unref: Effect.succeed(Effect.void),
31 stdin: Sink.drain,
32 stdout: Stream.unwrap(
33 Effect.as(
34 answered,
35 stdout.length === 0 ? Stream.empty : Stream.succeed(encoder.encode(stdout)),
36 ),
37 ),
38 stderr: Stream.empty,
39 all: Stream.empty,
40 getInputFd: () => Sink.drain,
41 getOutputFd: () => Stream.empty,
42 });
43 }
44
45 // `fx status --json` and `fx models --json` from fx 0.0.15, trimmed to the
46 // fields T3 reads plus a few it ignores.
47 const status = (auth: string, fields: Record<string, unknown> = {}) =>
48 JSON.stringify({ kind: "status", model: "default", auth, auth_refreshable: false, ...fields });
49 const models = (ids: ReadonlyArray<string>) =>
50 JSON.stringify({ kind: "models", count: ids.length, private_models_hidden: false, ids });
51 const GATEWAY_MODELS = models(["anthropic/claude-sonnet-5.5", "anthropic/claude-opus-5.5"]);
52 const SIGNED_OUT_LISTING = JSON.stringify({
53 kind: "models",
54 error: "could not list models: AuthenticationRejected",
55 code: "AuthenticationRejected",
56 });
57
58 interface Invocation {
59 readonly args: string;
60 readonly backend: string | undefined;
61 readonly cwd: string | undefined;
62 }
63
64 /**
65 * Answers `fx` invocations keyed by `FX_PROVIDER` and arguments, such as
66 * `codex status --json`; the unscoped status is ` status --json`. HOME holds
67 * an fx profile whose settings define `profileProviders`, or `FX_HOME` does
68 * when `profileInFxHome` is set.
69 */
70 const check = (
71 outputs: Record<string, string>,
72 profileProviders: ReadonlyArray<string> = [],
73 versionDelay: Duration.Input = 0,
74 versionSpawned?: Deferred.Deferred<void>,
75 profileInFxHome = false,
76 ) =>
77 Effect.gen(function* () {
78 const fileSystem = yield* FileSystem.FileSystem;
79 const path = yield* Path.Path;
80 const home = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-fx-status-" });
81 const profile = path.join(home, profileInFxHome ? "fx-home" : ".fx");
82 yield* fileSystem.makeDirectory(profile);
83 yield* fileSystem.writeFileString(
84 path.join(profile, "settings.json"),
85 JSON.stringify({
86 providers: Object.fromEntries(profileProviders.map((id) => [id, { protocol: "x" }])),
87 }),
88 );
89 const invoked: Array<Invocation> = [];
90 const spawner = ChildProcessSpawner.make((command) => {
91 const standard = ChildProcess.isStandardCommand(command) ? command : undefined;
92 const args = standard?.args.join(" ") ?? "";
93 const backendValue = standard?.options.env?.FX_PROVIDER;
94 const backend =
95 typeof backendValue === "string" || backendValue === undefined
96 ? backendValue
97 : Redacted.value(backendValue);
98 invoked.push({ args, backend, cwd: standard?.options.cwd });
99 const stdout = args === "--version" ? "fx 0.0.15\n" : outputs[`${backend ?? ""} ${args}`];
100 return Effect.gen(function* () {
101 if (args === "--version" && versionSpawned)
102 yield* Deferred.succeed(versionSpawned, undefined);
103 return processHandle(
104 stdout ?? "",
105 stdout === undefined ? 2 : 0,
106 args === "--version" ? Effect.sleep(versionDelay) : Effect.void,
107 );
108 });
109 });
110 const draft = yield* checkFxProviderStatus(
111 settings,
112 profileInFxHome ? { HOME: home, FX_HOME: profile } : { HOME: home },
113 ).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner));
114 return { draft, invoked };
115 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer));
116
117 describe("checkFxProviderStatus", () => {
118 it.effect.each([
119 {
120 label: "answers after 6 s",
121 versionDelay: "6 seconds",
122 status: "ready",
123 version: "0.0.15",
124 message: undefined,
125 invoked: [
126 "--version",
127 "status --json",
128 "status --json",
129 "status --json",
130 "status --json",
131 "models --json",
132 ],
133 },
134 {
135 label: "never answers",
136 versionDelay: "Infinity",
137 status: "error",
138 version: null,
139 message: "Failed to run `fx --version`.",
140 invoked: ["--version"],
141 },
142 ] as const)("gives `fx --version` 10 s when it $label", (row) =>
143 Effect.gen(function* () {
144 const versionSpawned = yield* Deferred.make<void>();
145 const fiber = yield* check(
146 {
147 " status --json": status("fx login"),
148 "gateway status --json": status("fx login"),
149 "gateway models --json": GATEWAY_MODELS,
150 "codex status --json": status("missing"),
151 "grok status --json": status("missing"),
152 },
153 [],
154 row.versionDelay,
155 versionSpawned,
156 ).pipe(Effect.forkChild);
157 yield* Deferred.await(versionSpawned);
158 yield* TestClock.adjust("10 seconds");
159 const { draft, invoked } = yield* Fiber.join(fiber);
160 assert.equal(draft.status, row.status);
161 assert.equal(draft.version, row.version);
162 assert.equal(draft.message, row.message);
163 assert.deepEqual(invoked.map(({ args }) => args).toSorted(), [...row.invoked].toSorted());
164 }),
165 );
166
167 it.effect("lists each backend's models and marks signed-out backends", () =>
168 Effect.gen(function* () {
169 const { draft, invoked } = yield* check(
170 {
171 " status --json": status("chatgpt subscription", {
172 model: "gpt-5.5",
173 model_source: "Codex subscription",
174 }),
175 "gateway status --json": status("fx login"),
176 "gateway models --json": GATEWAY_MODELS,
177 "codex status --json": status("chatgpt subscription"),
178 "codex models --json": models(["gpt-5.5", "gpt-5.4-mini"]),
179 "grok status --json": status("missing"),
180 "local status --json": status("missing", {
181 auth_help: "The configured provider credential is unavailable.",
182 }),
183 },
184 ["local"],
185 );
186 // Never `fx acp`, which saves a session; signed-out backends list nothing.
187 assert.deepEqual(invoked.map(({ backend, args }) => `${backend ?? ""} ${args}`).toSorted(), [
188 " --version",
189 " status --json",
190 "codex models --json",
191 "codex status --json",
192 "gateway models --json",
193 "gateway status --json",
194 "grok status --json",
195 "local status --json",
196 ]);
197 assert.equal(draft.status, "ready");
198 assert.deepEqual(draft.auth, { status: "authenticated", label: "chatgpt subscription" });
199 assert.deepEqual(
200 draft.models.map((model) => [model.slug, model.name, model.subProvider]),
201 [
202 ["default", "Default (gpt-5.5)", undefined],
203 [
204 "gateway:anthropic/claude-sonnet-5.5",
205 "anthropic/claude-sonnet-5.5",
206 "Vercel AI Gateway",
207 ],
208 ["gateway:anthropic/claude-opus-5.5", "anthropic/claude-opus-5.5", "Vercel AI Gateway"],
209 ["codex:gpt-5.5", "gpt-5.5", "Codex subscription"],
210 ["codex:gpt-5.4-mini", "gpt-5.4-mini", "Codex subscription"],
211 ],
212 );
213 assert.deepEqual(draft.subProviders, [
214 { name: "Vercel AI Gateway" },
215 { name: "Codex subscription" },
216 {
217 name: "Grok subscription",
218 unavailableReason: "Signed out. Run `fx login grok` on this machine.",
219 },
220 { name: "local", unavailableReason: "The configured provider credential is unavailable." },
221 ]);
222 assert.isDefined(draft.versionAdvisory);
223 }),
224 );
225
226 it.effect("reads configured backends from the profile `FX_HOME` names", () =>
227 Effect.gen(function* () {
228 const { draft } = yield* check(
229 {
230 " status --json": status("fx login"),
231 "gateway status --json": status("fx login"),
232 "gateway models --json": GATEWAY_MODELS,
233 "codex status --json": status("missing"),
234 "grok status --json": status("missing"),
235 "local status --json": status("fx login"),
236 "local models --json": models([]),
237 },
238 ["local"],
239 0,
240 undefined,
241 true,
242 );
243 assert.deepEqual(
244 draft.subProviders?.map((subProvider) => subProvider.name),
245 ["Vercel AI Gateway", "Codex subscription", "Grok subscription", "local"],
246 );
247 }),
248 );
249
250 it.effect("stays usable while fx's default backend is signed out", () =>
251 Effect.gen(function* () {
252 const { draft } = yield* check({
253 " status --json": status("missing", { model: "spacexai/grok-4.7" }),
254 "gateway status --json": status("missing"),
255 "gateway models --json": GATEWAY_MODELS,
256 "codex status --json": status("chatgpt subscription"),
257 "codex models --json": models(["gpt-5.5"]),
258 "grok status --json": status("missing"),
259 });
260 assert.equal(draft.status, "warning");
261 assert.deepEqual(draft.auth, { status: "authenticated", label: "Codex subscription" });
262 assert.equal(
263 draft.message,
264 "fx's default backend, Vercel AI Gateway, is signed out. Pick a model on another backend or sign in.",
265 );
266 // The gateway lists its public catalog signed out, but none of it can run.
267 assert.deepEqual(
268 draft.models.map((model) => model.slug),
269 ["default", "codex:gpt-5.5"],
270 );
271
272 // An earlier backend whose sign-in fx could not report does not hide Codex's.
273 const unreadable = yield* check({
274 " status --json": status("missing", { model: "spacexai/grok-4.7" }),
275 "gateway models --json": GATEWAY_MODELS,
276 "codex status --json": status("chatgpt subscription"),
277 "codex models --json": models(["gpt-5.5"]),
278 "grok status --json": status("missing"),
279 });
280 assert.deepEqual(unreadable.draft.auth, {
281 status: "authenticated",
282 label: "Codex subscription",
283 });
284 }),
285 );
286
287 it.effect("offers Full access only when `fx status` lists that mode", () =>
288 Effect.gen(function* () {
289 const backends = {
290 "gateway status --json": status("fx login"),
291 "gateway models --json": GATEWAY_MODELS,
292 "codex status --json": status("missing"),
293 "grok status --json": status("missing"),
294 };
295 // fx 0.0.13 lists no modes and has no full-access mode.
296 const withoutModes = yield* check({ ...backends, " status --json": status("fx login") });
297 assert.deepEqual(withoutModes.draft.supportedRuntimeModes, ["approval-required", "auto"]);
298 // fx 0.0.14+ lists the ACP session modes (src/core/output/output_contracts.zig).
299 const withModes = yield* check({
300 ...backends,
301 " status --json": status("fx login", {
302 modes: [{ id: "auto" }, { id: "ask" }, { id: "full-access" }],
303 }),
304 });
305 assert.deepEqual(withModes.draft.supportedRuntimeModes, [
306 "approval-required",
307 "auto",
308 "full-access",
309 ]);
310 }),
311 );
312
313 it.effect("reports a machine signed out of every backend", () =>
314 Effect.gen(function* () {
315 const signedOut = yield* check({
316 " status --json": status("missing", { model: "spacexai/grok-4.7" }),
317 "gateway status --json": status("missing"),
318 "codex status --json": status("missing"),
319 "codex models --json": SIGNED_OUT_LISTING,
320 "grok status --json": status("missing"),
321 });
322 assert.equal(signedOut.draft.status, "error");
323 assert.equal(signedOut.draft.auth.status, "unauthenticated");
324 assert.equal(signedOut.draft.message, "Run `fx login` to sign in.");
325
326 // An fx whose status output T3 cannot read is not reported as signed out.
327 const unknown = yield* check({ " status --json": "not json" });
328 assert.equal(unknown.draft.status, "warning");
329 assert.equal(unknown.draft.auth.status, "unknown");
330 }),
331 );
332
333 // A ready snapshot drops models it does not list, so only a real listing may report ready.
334 it.effect("treats `fx models` output without ids as a failed listing", () =>
335 Effect.gen(function* () {
336 const outputs = (gatewayModels: string) => ({
337 " status --json": status("fx login"),
338 "gateway status --json": status("fx login"),
339 "gateway models --json": gatewayModels,
340 "codex status --json": status("chatgpt subscription"),
341 "codex models --json": models(["gpt-5.5"]),
342 "grok status --json": status("missing"),
343 });
344 const reshaped = yield* check(outputs(JSON.stringify({ kind: "models", models: [] })));
345 assert.equal(reshaped.draft.status, "warning");
346 assert.deepEqual(
347 reshaped.draft.models.map((model) => model.slug),
348 ["default", "codex:gpt-5.5"],
349 );
350 // The server keeps the gateway's earlier models, which can still run.
351 assert.deepEqual(reshaped.draft.subProviders?.[0], { name: "Vercel AI Gateway" });
352
353 const empty = yield* check(outputs(models([])));
354 assert.equal(empty.draft.status, "ready");
355 }),
356 );
357 });
358
359 describe("readFxDefaultChoice", () => {
360 // fx's settings can give one folder its own model, which `fx status` reports only there.
361 it.effect("reads fx's default backend and model in the thread's folder", () =>
362 Effect.forEach(
363 [
364 [{ model: "openai/gpt-5.5" }, { backend: "gateway", model: "openai/gpt-5.5" }],
365 [
366 { model: "gpt-5.5", model_source: "Codex subscription" },
367 { backend: "codex", model: "gpt-5.5" },
368 ],
369 [
370 { model: "llama3", model_source: "local" },
371 { backend: "local", model: "llama3" },
372 ],
373 ] as const,
374 ([fields, expected]) =>
375 Effect.gen(function* () {
376 const cwds: Array<string | undefined> = [];
377 const spawner = ChildProcessSpawner.make((command) => {
378 if (ChildProcess.isStandardCommand(command)) cwds.push(command.options.cwd);
379 return Effect.succeed(processHandle(status("fx login", fields), 0));
380 });
381 const choice = yield* readFxDefaultChoice(settings, {}, "/work/project").pipe(
382 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
383 );
384 assert.deepEqual(choice, expected);
385 assert.deepEqual(cwds, ["/work/project"]);
386 }),
387 { discard: true },
388 ).pipe(Effect.provide(NodeServices.layer)),
389 );
390 });