|
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | +import { assert, it } from "@effect/vitest"; |
| 3 | +import { CodexSettings } from "@t3tools/contracts"; |
| 4 | +import * as Effect from "effect/Effect"; |
| 5 | +import * as FileSystem from "effect/FileSystem"; |
| 6 | +import * as Path from "effect/Path"; |
| 7 | +import * as Schema from "effect/Schema"; |
| 8 | + |
| 9 | +import { checkCodexProviderStatus } from "./CodexProvider.ts"; |
| 10 | + |
| 11 | +/** |
| 12 | + * A stored ChatGPT login behind `requiresOpenaiAuth: true`, as the real CLI |
| 13 | + * reports for a custom provider that sets `requires_openai_auth = true`. |
| 14 | + * `account/rateLimits/read` leaves a marker file so a test can tell whether the |
| 15 | + * quota was fetched at all. |
| 16 | + */ |
| 17 | +const FAKE_CODEX_APP_SERVER = String.raw`#!/usr/bin/env node |
| 18 | +const fs = require("node:fs"); |
| 19 | +const path = require("node:path"); |
| 20 | +const readline = require("node:readline"); |
| 21 | +const write = (message) => process.stdout.write(JSON.stringify(message) + "\n"); |
| 22 | +fs.writeFileSync( |
| 23 | + path.join(process.env.CODEX_HOME, "models_cache.json"), |
| 24 | + JSON.stringify({ fetched_at: new Date().toISOString(), models: [] }), |
| 25 | +); |
| 26 | +readline.createInterface({ input: process.stdin }).on("line", (line) => { |
| 27 | + const { id, method } = JSON.parse(line); |
| 28 | + if (id === undefined) return; |
| 29 | + if (method === "initialize") { |
| 30 | + write({ id, result: { userAgent: "codex/0.162.0", codexHome: process.env.CODEX_HOME, platformFamily: "unix", platformOs: "macos" } }); |
| 31 | + } else if (method === "account/read") { |
| 32 | + write({ id, result: { account: { type: "chatgpt", email: "[email protected]", planType: "pro" }, requiresOpenaiAuth: true } }); |
| 33 | + } else if (method === "config/read") { |
| 34 | + write({ id, result: { config: JSON.parse(process.env.FAKE_CODEX_CONFIG), origins: {} } }); |
| 35 | + } else if (method === "account/rateLimits/read") { |
| 36 | + fs.writeFileSync(path.join(process.env.CODEX_HOME, "rate-limits-requested"), ""); |
| 37 | + write({ id, result: { rateLimits: { primary: { usedPercent: 90, windowDurationMins: 10080, resetsAt: 1789436313 } } } }); |
| 38 | + } else if (method === "skills/list") { |
| 39 | + write({ id, result: { data: [] } }); |
| 40 | + } else if (method === "model/list") { |
| 41 | + write({ id, result: { data: [], nextCursor: null } }); |
| 42 | + } else { |
| 43 | + write({ id, error: { code: -32601, message: "unsupported" } }); |
| 44 | + } |
| 45 | +}); |
| 46 | +`; |
| 47 | + |
| 48 | +const decodeCodexSettings = Schema.decodeSync(CodexSettings); |
| 49 | + |
| 50 | +const usageFor = Effect.fn("usageFor")(function* (config: Record<string, unknown>) { |
| 51 | + const fs = yield* FileSystem.FileSystem; |
| 52 | + const path = yield* Path.Path; |
| 53 | + const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-codex-usage-" }); |
| 54 | + const binaryPath = path.join(dir, "codex"); |
| 55 | + yield* fs.writeFileString(binaryPath, FAKE_CODEX_APP_SERVER); |
| 56 | + yield* fs.chmod(binaryPath, 0o755); |
| 57 | + |
| 58 | + const provider = yield* checkCodexProviderStatus( |
| 59 | + decodeCodexSettings({ binaryPath, homePath: dir }), |
| 60 | + undefined, |
| 61 | + { ...process.env, FAKE_CODEX_CONFIG: JSON.stringify(config) }, |
| 62 | + ); |
| 63 | + return { |
| 64 | + usageLimits: provider.usageLimits, |
| 65 | + fetched: yield* fs.exists(path.join(dir, "rate-limits-requested")), |
| 66 | + }; |
| 67 | +}); |
| 68 | + |
| 69 | +it.layer(NodeServices.layer, { excludeTestServices: true })("Codex native usage routing", (it) => { |
| 70 | + it.effect("reports the ChatGPT quota while requests go to OpenAI", () => |
| 71 | + Effect.gen(function* () { |
| 72 | + for (const config of [ |
| 73 | + {}, |
| 74 | + { model_provider: null, model_providers: {}, openai_base_url: null }, |
| 75 | + { openai_base_url: "https://api.openai.com/v1" }, |
| 76 | + { |
| 77 | + model_provider: "mirror", |
| 78 | + model_providers: { mirror: { base_url: "https://chatgpt.com/backend-api/codex" } }, |
| 79 | + }, |
| 80 | + // A custom provider without a `base_url` falls back to OpenAI's endpoint. |
| 81 | + { model_provider: "plain", model_providers: { plain: { base_url: null } } }, |
| 82 | + ]) { |
| 83 | + const { usageLimits, fetched } = yield* usageFor(config); |
| 84 | + assert.strictEqual(fetched, true); |
| 85 | + assert.strictEqual(usageLimits?.windows.length, 1); |
| 86 | + assert.strictEqual(usageLimits?.unavailable, undefined); |
| 87 | + } |
| 88 | + }).pipe(Effect.scoped), |
| 89 | + ); |
| 90 | + |
| 91 | + it.effect("skips the ChatGPT quota when requests go to a non-OpenAI base_url", () => |
| 92 | + Effect.gen(function* () { |
| 93 | + for (const config of [ |
| 94 | + { |
| 95 | + model_provider: "hub", |
| 96 | + model_providers: { |
| 97 | + hub: { base_url: "https://hub.example.com/v1", requires_openai_auth: true }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + { openai_base_url: "https://proxy.example.com/v1" }, |
| 101 | + // Look-alike hosts are not OpenAI's. |
| 102 | + { |
| 103 | + model_provider: "hub", |
| 104 | + model_providers: { hub: { base_url: "https://evilopenai.com/v1" } }, |
| 105 | + }, |
| 106 | + { model_provider: "hub", model_providers: { hub: { base_url: "not a url" } } }, |
| 107 | + ]) { |
| 108 | + const { usageLimits, fetched } = yield* usageFor(config); |
| 109 | + assert.strictEqual(fetched, false); |
| 110 | + assert.deepStrictEqual(usageLimits?.windows, []); |
| 111 | + assert.strictEqual(usageLimits?.unavailable?.reason, "unsupported"); |
| 112 | + } |
| 113 | + }).pipe(Effect.scoped), |
| 114 | + ); |
| 115 | +}); |
0 commit comments