Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy
Rust
move model routing helpers into registry
Raine Virta3f23988a34b8
diff --git a/src/cli.ts b/src/cli.tsindex 6351306..b3c3b00 100755--- a/src/cli.ts+++ b/src/cli.ts@@ -13,7 +13,12 @@ import {import { configDir } from "./paths.ts";import { existsSync } from "node:fs";import { join } from "node:path";-import { allSupportedModels, getProvider, listProviders } from "./providers/registry.ts";+import {+ allSupportedModels,+ getProvider,+ groupSupportedModelsByProvider,+ listProviders,+} from "./providers/registry.ts";import type { CliHandlers } from "./providers/types.ts";declare const BUILD_VERSION: string | undefined;@@ -113,12 +118,7 @@ Models: ${models}}function printSupportedModels(): void {- const groups = new Map<string, string[]>();- for (const { model, provider } of allSupportedModels()) {- const models = groups.get(provider) ?? [];- models.push(model);- groups.set(provider, models);- }+ const groups = groupSupportedModelsByProvider();for (const provider of listProviders()) {const models = groups.get(provider) ?? [];console.log(` ${provider}: ${models.join(", ")}`);diff --git a/src/providers/registry.test.ts b/src/providers/registry.test.tsindex 9beedd4..85a821e 100644--- a/src/providers/registry.test.ts+++ b/src/providers/registry.test.ts@@ -2,9 +2,14 @@ import { afterEach, describe, expect, it } from "bun:test";import { mkdtempSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";-import { allSupportedModels, ANTHROPIC_STYLE_ALIASES, providerForModel } from "./registry.ts";+import {+ allSupportedModels,+ ANTHROPIC_STYLE_ALIASES,+ groupSupportedModelsByProvider,+ normalizeIncomingModel,+ providerForModel,+} from "./registry.ts";import { loadConfig } from "../config.ts";-import { normalizeIncomingModel } from "../server.ts";afterEach(() => {loadConfig({ forceReload: true });@@ -18,10 +23,8 @@ function providersFor(model: string): string[] {describe("provider routing", () => {it("routes fast Codex model aliases after context suffix normalization", () => {- const model = normalizeIncomingModel("gpt-5.4-fast[1m]");-- expect(model).toBe("gpt-5.4-fast");- expect(providerForModel(model)?.name).toBe("codex");+ expect(normalizeIncomingModel("gpt-5.4-fast[1m]")).toBe("gpt-5.4-fast");+ expect(providerForModel("gpt-5.4-fast[1m]")?.name).toBe("codex");});it("routes Anthropic-style aliases to Codex by default", () => {@@ -76,6 +79,23 @@ describe("provider routing", () => {expect(providersFor("sonnet")).toEqual(["codex"]);});+ it("groups supported models without changing provider or model order", () => {+ loadConfig({ env: { CCP_ALIAS_PROVIDER: "kimi" }, forceReload: true });++ const grouped = groupSupportedModelsByProvider();+ const expected = new Map<string, string[]>();+ for (const { model, provider } of allSupportedModels()) {+ const models = expected.get(provider) ?? [];+ models.push(model);+ expected.set(provider, models);+ }++ expect([...grouped.keys()]).toEqual([...expected.keys()]);+ expect([...grouped.entries()]).toEqual([...expected.entries()]);+ expect(grouped.get("kimi")).toEqual(expect.arrayContaining([...ANTHROPIC_STYLE_ALIASES]));+ expect(grouped.get("codex") ?? []).not.toEqual(expect.arrayContaining(["sonnet"]));+ });+it("does not duplicate supported model entries", () => {for (const env of [{}, { CCP_ALIAS_PROVIDER: "codex" }]) {loadConfig({ env, forceReload: true });diff --git a/src/providers/registry.ts b/src/providers/registry.tsindex d4b0a98..ca35a73 100644--- a/src/providers/registry.ts+++ b/src/providers/registry.ts@@ -37,19 +37,34 @@ export function allProviders(): Provider[] {return Object.values(PROVIDERS);}+export function normalizeIncomingModel(model: string): string {+ return model.replace(/\[1m\]$/i, "");+}+export function providerForModel(model: string,aliasProviderOverride?: AliasProvider,): Provider | undefined {- if (ANTHROPIC_STYLE_ALIASES.has(model))+ const normalizedModel = normalizeIncomingModel(model);+ if (ANTHROPIC_STYLE_ALIASES.has(normalizedModel))return getProvider(aliasProviderOverride ?? aliasProvider());- if (isCursorModel(model)) return cursorProvider;+ if (isCursorModel(normalizedModel)) return cursorProvider;for (const p of allProviders()) {- if (p.supportedModels.has(model)) return p;+ if (p.supportedModels.has(normalizedModel)) return p;}return undefined;}+export function groupSupportedModelsByProvider(): Map<string, string[]> {+ const groups = new Map<string, string[]>();+ for (const { model, provider } of allSupportedModels()) {+ const models = groups.get(provider) ?? [];+ models.push(model);+ groups.set(provider, models);+ }+ return groups;+}+export function allSupportedModels(): Array<{ model: string; provider: string }> {const out: Array<{ model: string; provider: string }> = [];const activeAliasProvider = aliasProvider();diff --git a/src/server.test.ts b/src/server.test.tsindex 71b1546..8da0f46 100644--- a/src/server.test.ts+++ b/src/server.test.ts@@ -2,6 +2,7 @@ import { afterEach, describe, expect, it } from "bun:test";import { loadConfig } from "./config.ts";import type { createLogger } from "./log.ts";import { startServer, normalizeIncomingModel, wrapStreamResponse } from "./server.ts";+import { groupSupportedModelsByProvider } from "./providers/registry.ts";import { startTestServer } from "./test/server.ts";const servers: Array<{ stop: () => void }> = [];@@ -76,6 +77,36 @@ describe("server error responses", () => {expect(resp.headers.get("content-type")).toBe("application/json");expect(body.error.type).toBe("invalid_request_error");});++ it("uses active alias-provider grouping in unknown-model errors", async () => {+ loadConfig({ env: { CCP_ALIAS_PROVIDER: "kimi" }, forceReload: true });+ const server = startTestServer(startServer);+ servers.push(server);++ const resp = await fetch(`http://127.0.0.1:${server.port}/v1/messages`, {+ method: "POST",+ headers: { "content-type": "application/json" },+ body: JSON.stringify({+ model: "not-a-real-model",+ messages: [{ role: "user", content: "hello" }],+ }),+ });+ const body = (await resp.json()) as { error: { message: string } };++ const parts: string[] = [];+ for (const [provider, models] of groupSupportedModelsByProvider()) {+ parts.push(`${provider}: ${models.join(", ")}`);+ }++ expect(resp.status).toBe(400);+ expect(body.error.message).toBe(+ `Unknown model "not-a-real-model". Supported: ${parts.join("; ")}.`,+ );+ expect([...groupSupportedModelsByProvider().keys()]).toEqual(["codex", "kimi", "cursor"]);+ expect(groupSupportedModelsByProvider().get("kimi")).toEqual(+ expect.arrayContaining(["haiku", "sonnet", "opus"]),+ );+ });});describe("stream wrapper", () => {diff --git a/src/server.ts b/src/server.tsindex cab4a73..8b61671 100644--- a/src/server.ts+++ b/src/server.ts@@ -6,11 +6,14 @@ import type { AnthropicRequest } from "./anthropic/schema.ts";import type { AliasProvider } from "./config.ts";import type { Provider, RequestContext } from "./providers/types.ts";import {- allSupportedModels,ANTHROPIC_STYLE_ALIASES,+ groupSupportedModelsByProvider,+ normalizeIncomingModel,providerForModel,} from "./providers/registry.ts";+export { normalizeIncomingModel };+const rootLog = createLogger("server");export interface ServeOptions {@@ -190,12 +193,6 @@ function captureInboundTraffic(ctx.traffic.writeJson("010-anthropic-request", body);}-// Claude Code may append a [1m] suffix to model names as a local compaction-// hint. The suffix is not part of the upstream model id.-export function normalizeIncomingModel(model: string): string {- return model.replace(/\[1m\]$/i, "");-}-function routeProvider(body: AnthropicRequest,reqId: string,@@ -222,14 +219,8 @@ function routeProvider(}function knownModelsMessage(): string {- const groups = new Map<string, string[]>();- for (const { model, provider } of allSupportedModels()) {- const list = groups.get(provider) ?? [];- list.push(model);- groups.set(provider, list);- }const parts: string[] = [];- for (const [provider, models] of groups) {+ for (const [provider, models] of groupSupportedModelsByProvider()) {parts.push(`${provider}: ${models.join(", ")}`);}return `Supported: ${parts.join("; ")}.`;