From c04fdf044c3780f51ead0f25329a60a1375857e6 Mon Sep 17 00:00:00 2001 From: Vadym O Date: Fri, 13 Nov 2026 08:53:47 +0800 Subject: [PATCH] feat(providers): xAI by API key as its own provider; keys never take the sign-ins' traffic (.21) AgentRadar AGE-2278 lets a company add an API key for Claude, Codex or Grok and run it in its model waterfall next to the subscription sign-ins. The gateway needs two things for that. xai-apikey is a new key provider for https://api.x.ai/v1, placed next to the OpenAI and Anthropic key rows. It carries the same lineup and wire metadata as the xai sign-in row, but no default model. A company key written onto the xai row itself would replace the Grok sign-ins through allowKeyAuthOverride, so the key gets its own row. Bare model ids now reach a signed-in provider before its -apikey twin, whatever order the config was written in. Before this, a key added before the first sign-in sat earlier in the config, and routeByKnownModelPattern, the configured-default-model step and the model-list step all took the first match, so a bare claude-* or grok-* request went to the key. Pinned AgentRadar requests already refused a key route; unpinned ones could have spent the key. A key is now reached only through its own namespace (anthropic-apikey/..., xai-apikey/...). Tests: tests/routing/router.test.ts adds a control (a key alone serves bare ids) and the no-hijack row (keys written first, every bare id still lands on the sign-ins, the key only by its namespace). The parity list gains xai-apikey. Co-Authored-By: Claude Code --- desktop/src-tauri/Cargo.lock | 2 +- desktop/src-tauri/Cargo.toml | 2 +- desktop/src-tauri/tauri.conf.json | 2 +- package.json | 2 +- src/providers/registry/entries-core.ts | 22 +++++++++++-- src/router.ts | 9 +++-- .../provider-registry-parity.test.ts | 2 +- tests/routing/router.test.ts | 33 +++++++++++++++++++ 8 files changed, 64 insertions(+), 10 deletions(-) diff --git a/desktop/src-tauri/Cargo.lock b/desktop/src-tauri/Cargo.lock index adae31bc3b2..4af885c3bf9 100644 --- a/desktop/src-tauri/Cargo.lock +++ b/desktop/src-tauri/Cargo.lock @@ -2541,7 +2541,7 @@ dependencies = [ [[package]] name = "opencodex-desktop" -version = "2.65.0-vadymhimself.20" +version = "2.65.0-vadymhimself.21" dependencies = [ "dbus", "reqwest 0.12.24", diff --git a/desktop/src-tauri/Cargo.toml b/desktop/src-tauri/Cargo.toml index 504a80fa49a..750b35a7c88 100644 --- a/desktop/src-tauri/Cargo.toml +++ b/desktop/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "opencodex-desktop" -version = "2.65.0-vadymhimself.20" +version = "2.65.0-vadymhimself.21" description = "OpenCodex desktop shell" authors = ["OpenCodex contributors"] license = "MIT" diff --git a/desktop/src-tauri/tauri.conf.json b/desktop/src-tauri/tauri.conf.json index acf9de30119..449f00ce963 100644 --- a/desktop/src-tauri/tauri.conf.json +++ b/desktop/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "https://schema.tauri.app/config/2", "productName": "OpenCodex", - "version": "2.65.0-vadymhimself.20", + "version": "2.65.0-vadymhimself.21", "identifier": "com.opencodex.desktop", "build": { "frontendDist": "../ui", diff --git a/package.json b/package.json index 70606548ca2..c424be9e366 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bitkyc08/opencodex", - "version": "2.65.0-vadymhimself.20", + "version": "2.65.0-vadymhimself.21", "description": "Universal provider proxy for OpenAI Codex & Claude Code — use any LLM with Codex CLI/App/SDK and Claude Code", "type": "module", "main": "./bin/package-main.mjs", diff --git a/src/providers/registry/entries-core.ts b/src/providers/registry/entries-core.ts index 65242d3009d..2c04f97a755 100644 --- a/src/providers/registry/entries-core.ts +++ b/src/providers/registry/entries-core.ts @@ -109,7 +109,7 @@ const ANTHROPIC_FAST_MODELS: Readonly> = Object.freeze({ const ANTHROPIC_FAST_TIER_DESCRIPTION = "Claude fast mode: faster output at 2x price; needs usage credits (subscription) or fast-mode access (API)"; -export const PROVIDER_REGISTRY_CORE: readonly ProviderRegistryEntry[] = [ +export const PROVIDER_REGISTRY_CORE: readonly ProviderRegistryEntry[] = withXaiKeyEntry([ { id: "openai", label: "OpenAI (Codex login)", @@ -1408,4 +1408,22 @@ export const PROVIDER_REGISTRY_CORE: readonly ProviderRegistryEntry[] = [ }, note: "Serverless Inference subscription API. Live discovery exposes only kimi-k2-instruct because Vultr documents it as the sole tool-calling model.", }, -]; +]); + +/** + * AgentRadar image patch (AGE-2278): xAI by API key as its own provider, next to the OpenAI and + * Anthropic key rows. A company key on the `xai` row would replace the Grok sign-ins through + * allowKeyAuthOverride; here it stays separate. Same lineup and wire metadata as `xai`, and no + * default model, so a bare model id still reaches the sign-in. + */ +function withXaiKeyEntry(entries: ProviderRegistryEntry[]): ProviderRegistryEntry[] { + const { + oauthId: _oauthId, allowKeyAuthOverride: _override, keyAuthServiceTier: _keyTier, modelSupportsServiceTier: _oauthTier, + featured: _featured, jawcodeBundle: _jawcode, defaultModel: _defaultModel, ...shared + } = entries.find(entry => entry.id === "xai")!; + entries.splice(entries.findIndex(entry => entry.id === "openai-apikey") + 1, 0, { + ...shared, id: "xai-apikey", label: "xAI (API key)", authKind: "key", supportsServiceTier: true, + dashboardUrl: "https://console.x.ai", note: "Direct xAI API billing, no Grok subscription", + }); + return entries; +} diff --git a/src/router.ts b/src/router.ts index 3d0fba3c55f..14d8851aebb 100644 --- a/src/router.ts +++ b/src/router.ts @@ -509,9 +509,12 @@ export function routedProviderConfig(providerName: string, provider: OcxProvider return resolved; } +// AgentRadar image patch (AGE-2278): a bare model id reaches a signed-in provider before its `-apikey` +// twin, whatever order the config was written in, so adding an API key never takes the logins' traffic. function activeProviderEntries(config: OcxConfig): [string, OcxProviderConfig][] { - return Object.entries(config.providers) + const active = Object.entries(config.providers) .filter(([name, provider]) => name !== LEGACY_CHATGPT_PROVIDER_ID && provider.disabled !== true); + return [...active.filter(([name]) => !name.endsWith("-apikey")), ...active.filter(([name]) => name.endsWith("-apikey"))]; } export class NoEnabledOpenAiProviderError extends Error { @@ -960,8 +963,8 @@ export function routeConcreteModel(config: OcxConfig, modelId: string): RouteRes function routeByKnownModelPattern(config: OcxConfig, modelId: string): RouteResult | undefined { for (const { providerNames, prefixes } of MODEL_PROVIDER_PATTERNS) { if (prefixes.some(prefix => modelId.startsWith(prefix))) { - const matchingProvider = Object.entries(config.providers).find( - ([name, prov]) => prov.disabled !== true && providerNames.some(providerName => name === providerName || name.startsWith(`${providerName}-`)) + const matchingProvider = activeProviderEntries(config).find( + ([name]) => providerNames.some(providerName => name === providerName || name.startsWith(`${providerName}-`)) ); if (matchingProvider) { const [provName, prov] = matchingProvider; diff --git a/tests/providers/provider-registry-parity.test.ts b/tests/providers/provider-registry-parity.test.ts index 8feb629806c..024f93c6a46 100644 --- a/tests/providers/provider-registry-parity.test.ts +++ b/tests/providers/provider-registry-parity.test.ts @@ -40,7 +40,7 @@ function nativeTemplate(): Record { } const EXPECTED_KEY_PROVIDER_IDS = [ - "anthropic-apikey", "openai-apikey", "meta-model", "umans", "opencode-go", "neuralwatt", "openrouter", "cline-pass", "cline", "orcarouter", "packycode", "bizrouter", "groq", "google", "google-vertex", "azure-openai", + "anthropic-apikey", "openai-apikey", "xai-apikey", "meta-model", "umans", "opencode-go", "neuralwatt", "openrouter", "cline-pass", "cline", "orcarouter", "packycode", "bizrouter", "groq", "google", "google-vertex", "azure-openai", "deepseek", "cerebras", "chutes", "deepinfra", "hyperbolic", "nscale", "vultr", "baseten", "commandcode", "sambanova", "nebius", "crusoe", "digitalocean", "scaleway", "featherless", "novita", "together", "fireworks", "firepass", "moonshot", "huggingface", "nvidia", "venice", "zai", "zhipu-bigmodel", "zhipu-bigmodel-coding", "zhipu-bigmodel-responses", "nanogpt", "synthetic", "siliconflow", "qwen-cloud", "tencent-coding-plan", "volcengine", "volcengine-coding-plan", "volcengine-agent-plan", "qianfan", "alibaba", "alibaba-token-plan", "alibaba-token-plan-intl", "parallel", "zenmux", "litellm", "ollama-cloud", "mistral", diff --git a/tests/routing/router.test.ts b/tests/routing/router.test.ts index d8b92dfdc72..501e44c30fc 100644 --- a/tests/routing/router.test.ts +++ b/tests/routing/router.test.ts @@ -1,6 +1,7 @@ import { describe, expect, test } from "bun:test"; import { mapReasoningEffort } from "../../src/reasoning-effort"; import { NoEnabledOpenAiProviderError, routeCompactionModel, routeModel } from "../../src/router"; +import { enrichProviderFromRegistry } from "../../src/providers/derive"; import type { OcxConfig, OcxProviderConfig } from "../../src/types"; describe("routeModel registry effort defaults", () => { @@ -772,3 +773,35 @@ describe("routeCompactionModel (#2901)", () => { expect(() => routeCompactionModel(openAiDefaultDisabled, "gpt-5.6-sol")).toThrow(NoEnabledOpenAiProviderError); }); }); + +describe("AgentRadar: an API key never takes the sign-ins' traffic (AGE-2278)", () => { + // Enriched from the registry like a loaded config, so default models and model lists are live. + const providers = (entries: Record): OcxConfig => { + for (const [name, prov] of Object.entries(entries)) enrichProviderFromRegistry(name, prov); + return { port: 10100, defaultProvider: "anthropic", providers: entries }; + }; + const anthropicKey = (): OcxProviderConfig => ({ adapter: "anthropic", baseUrl: "https://api.anthropic.com", apiKey: "sk-test" }); + const xaiKey = (): OcxProviderConfig => ({ adapter: "openai-chat", baseUrl: "https://api.x.ai/v1", apiKey: "xai-test" }); + const bare = ["claude-sonnet-5", "claude-opus-5", "grok-4.5", "grok-4.3"]; + + test("control: a key alone serves the bare ids", () => { + const keysOnly = providers({ "anthropic-apikey": anthropicKey(), "xai-apikey": xaiKey() }); + expect(bare.map(model => routeModel(keysOnly, model).providerName)) + .toEqual(["anthropic-apikey", "anthropic-apikey", "xai-apikey", "xai-apikey"]); + }); + + test("keys written before the sign-ins still leave every bare id on the sign-ins", () => { + const config = providers({ + "anthropic-apikey": anthropicKey(), + "xai-apikey": xaiKey(), + anthropic: { adapter: "anthropic", baseUrl: "https://api.anthropic.com" }, + xai: { adapter: "openai-chat", baseUrl: "https://api.x.ai/v1" }, + }); + expect(bare.map(model => routeModel(config, model).providerName)).toEqual(["anthropic", "anthropic", "xai", "xai"]); + expect(routeModel(config, "xai/grok-4.5").provider.authMode).toBe("oauth"); + // The key is reached only by its own namespace. + expect(routeModel(config, "anthropic-apikey/claude-opus-5").providerName).toBe("anthropic-apikey"); + const viaKey = routeModel(config, "xai-apikey/grok-4.5"); + expect([viaKey.providerName, viaKey.provider.authMode ?? "key", viaKey.modelId]).toEqual(["xai-apikey", "key", "grok-4.5"]); + }); +});