GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(server): Codex hides native quota when routed to a non-OpenAI end… · pingdotgg/t3code@a6904a7

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src/provider
    • CodexProvider.ts
    • CodexProvider.usage.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/server/src/provider/CodexProvider.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/provider/CodexProvider.ts
+49-6Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,49 @@ type CodexRateLimitsProbe =
6767
}
6868
| { readonly failure: string };
6969

70+
const CodexRoutingConfig = Schema.Struct({
71+
model_provider: Schema.optionalKey(Schema.NullOr(Schema.String)),
72+
openai_base_url: Schema.optionalKey(Schema.NullOr(Schema.String)),
73+
model_providers: Schema.optionalKey(
74+
Schema.Record(
75+
Schema.String,
76+
Schema.Struct({ base_url: Schema.optionalKey(Schema.NullOr(Schema.String)) }),
77+
),
78+
),
79+
});
80+
const decodeCodexRoutingConfig = Schema.decodeUnknownOption(CodexRoutingConfig);
81+
82+
/** `undefined` means no override: requests go to Codex's built-in OpenAI endpoints. */
83+
const activeProviderBaseUrl = (config: unknown): string | undefined => {
84+
const routing = Option.getOrUndefined(decodeCodexRoutingConfig(config));
85+
const providerId = routing?.model_provider ?? "openai";
86+
return (
87+
(providerId === "openai"
88+
? routing?.openai_base_url
89+
: routing?.model_providers?.[providerId]?.base_url) ?? undefined
90+
);
91+
};
92+
93+
const isOpenAiEndpoint = (baseUrl: string): boolean => {
94+
if (!URL.canParse(baseUrl)) return false;
95+
const { hostname } = new URL(baseUrl);
96+
return [".openai.com", ".chatgpt.com"].some((suffix) => `.${hostname}`.endsWith(suffix));
97+
};
98+
7099
/**
71100
* `requiresOpenaiAuth` follows the configured `model_provider`, not the login:
72101
* it is `false` for a custom provider even when a ChatGPT login is stored, and
73-
* that login's quota is not what the provider's traffic spends. API keys have
74-
* no subscription windows.
102+
* a custom provider can set it `true` while routing to a non-OpenAI `base_url`.
103+
* Either way that login's quota is not what the provider's traffic spends. API
104+
* keys have no subscription windows.
75105
*/
76-
const reportsNativeUsage = (account: CodexSchema.V2GetAccountResponse): boolean =>
77-
account.requiresOpenaiAuth && account.account?.type !== "apiKey";
106+
const reportsNativeUsage = (
107+
account: CodexSchema.V2GetAccountResponse,
108+
baseUrl: string | undefined,
109+
): boolean =>
110+
account.requiresOpenaiAuth &&
111+
account.account?.type !== "apiKey" &&
112+
(baseUrl === undefined || isOpenAiEndpoint(baseUrl));
78113

79114
const CODEX_APP_SERVER_PROBE_FORCE_KILL_AFTER = "2 seconds" as const;
80115
const CODEX_CATALOG_REFRESH_TIMEOUT = "3 seconds" as const;
@@ -89,6 +124,7 @@ const CODEX_PRESENTATION = {
89124

90125
export interface CodexAppServerProviderSnapshot {
91126
readonly account: CodexSchema.V2GetAccountResponse;
127+
readonly baseUrl?: string;
92128
readonly rateLimits?: CodexRateLimitsProbe;
93129
readonly version: string | undefined;
94130
readonly models: ReadonlyArray<ServerProviderModel>;
@@ -485,6 +521,12 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
485521
} satisfies CodexAppServerProviderSnapshot;
486522
}
487523

524+
// An unreadable config leaves the account alone to decide.
525+
const baseUrl = yield* client.request("config/read", {}).pipe(
526+
Effect.map((response) => activeProviderBaseUrl(response.config)),
527+
Effect.orElseSucceed(() => undefined),
528+
);
529+
488530
const [skillsResponse, models, rateLimits] = yield* Effect.all(
489531
[
490532
client.request("skills/list", {
@@ -493,7 +535,7 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
493535
accountResponse.account?.type === "chatgpt"
494536
? requestCodexChatGptModels({ client, codexHome: initialize.codexHome, spawnedAtMs })
495537
: requestAllCodexModels(client),
496-
input.skipNativeUsage || !reportsNativeUsage(accountResponse)
538+
input.skipNativeUsage || !reportsNativeUsage(accountResponse, baseUrl)
497539
? Effect.succeed(undefined)
498540
: client.request("account/rateLimits/read", null).pipe(
499541
Effect.map((response): CodexRateLimitsProbe => ({
@@ -520,6 +562,7 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
520562

521563
return {
522564
account: accountResponse,
565+
...(baseUrl === undefined ? {} : { baseUrl }),
523566
...(rateLimits ? { rateLimits } : {}),
524567
version,
525568
models: applyPreferredCodexDefaultModel(
@@ -718,7 +761,7 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
718761
const accountStatus = managedAuth
719762
? { status: "ready" as const, auth: managedAuth, message: undefined }
720763
: accountProbeStatus(snapshot);
721-
const usageLimits = !reportsNativeUsage(snapshot.account)
764+
const usageLimits = !reportsNativeUsage(snapshot.account, snapshot.baseUrl)
722765
? makeUnavailableUsageLimits({ checkedAt, reason: "unsupported" })
723766
: snapshot.rateLimits === undefined || "failure" in snapshot.rateLimits
724767
? makeUnavailableUsageLimits({
Collapse file

‎apps/server/src/provider/CodexProvider.usage.test.ts‎

Copy file name to clipboard
+115Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.