Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(model-providers): strip common prefixes when matching custom models to registry (#3086)
Battlepluse1f3b13342ee
diff --git a/packages/model-providers/src/__tests__/user-provider.test.ts b/packages/model-providers/src/__tests__/user-provider.test.tsindex 4814dd252f..dbe31bed02 100644--- a/packages/model-providers/src/__tests__/user-provider.test.ts+++ b/packages/model-providers/src/__tests__/user-provider.test.ts@@ -17,6 +17,7 @@ import {storedCustomProviderId,} from "../user-provider.js";import type { CustomProviderConfig } from "../types.js";+import type { ModelRegistry } from "../modelAccessBean.js";import { BUNDLED_CATALOG } from "../catalog.js";const codexOnly: CustomProviderConfig = {@@ -208,7 +209,99 @@ describe("buildUserProvider (per-runtime)", () => {});});- it("falls back safely for ambiguous matches, missing target routes and invalid defaults", () => {+it('strips xd/ prefix to match registry effort metadata (entry.id ≠ custom id)', () => {+ const p = buildUserProvider(+ {+ id: 'my-provider',+ name: 'My Provider',+ runtimes: {+ codex: {+ baseUrl: 'https://my-provider.example/v1',+ models: [{ id: 'xd/codex/gpt-5.6-sol', name: 'GPT-5.6-Sol' }],+ },+ },+ },+ { modelRegistry: BUNDLED_CATALOG.modelRegistry },+ );+ // xd/codex/gpt-5.6-sol → strips to codex/gpt-5.6-sol → no exact match+ // → further strips? no, only openai/xd/chatgpt/ are stripped.+ // Actually xd/codex/gpt-5.6-sol starts with xd/ → stripped to codex/gpt-5.6-sol+ // which matches route.modelId for codex agent.+ expect(p.models.codex?.[0]).toMatchObject({+ id: 'xd/codex/gpt-5.6-sol',+ efforts: expect.arrayContaining(['ultra']),+ defaultEffort: 'high',+ });+ });++ it('strips xd/ prefix to match registry effort metadata', () => {+ const p = buildUserProvider(+ {+ id: 'xd-relay',+ name: 'XD Relay',+ runtimes: {+ codex: {+ baseUrl: 'https://xd-relay.example/v1',+ models: [{ id: 'xd/gpt-5.6-sol', name: 'GPT-5.6-Sol' }],+ },+ },+ },+ { modelRegistry: BUNDLED_CATALOG.modelRegistry },+ );+ // xd/gpt-5.6-sol → strips to gpt-5.6-sol → matches registry entry+ expect(p.models.codex?.[0]).toMatchObject({+ id: 'xd/gpt-5.6-sol',+ efforts: expect.arrayContaining(['ultra']),+ defaultEffort: 'high',+ });+ });++ it('strips openai/ prefix to match registry effort metadata (entry.id ≠ custom id)', () => {+ const p = buildUserProvider(+ {+ id: 'openai-relay',+ name: 'OpenAI Relay',+ runtimes: {+ codex: {+ baseUrl: 'https://openai-relay.example/v1',+ models: [{ id: 'openai/gpt-5.6-sol', name: 'GPT-5.6-Sol' }],+ },+ },+ },+ { modelRegistry: BUNDLED_CATALOG.modelRegistry },+ );+ // openai/gpt-5.6-sol → strips to gpt-5.6-sol → matches registry entry+ // entry.id = 'gpt-5.6-sol' ≠ custom id 'openai/gpt-5.6-sol'+ expect(p.models.codex?.[0]).toMatchObject({+ id: 'openai/gpt-5.6-sol',+ efforts: expect.arrayContaining(['ultra']),+ defaultEffort: 'high',+ });+ });++ it('unregistered prefix falls back to CUSTOM_EFFORTS', () => {+ const p = buildUserProvider(+ {+ id: 'unknown-relay',+ name: 'Unknown Relay',+ runtimes: {+ codex: {+ baseUrl: 'https://unknown.example/v1',+ models: [{ id: 'custom/my-model', name: 'My Model' }],+ },+ },+ },+ { modelRegistry: BUNDLED_CATALOG.modelRegistry },+ );+ // custom/my-model → no registry match → CUSTOM_EFFORTS for codex+ expect(p.models.codex?.[0]).toMatchObject({+ id: 'custom/my-model',+ efforts: ['low', 'medium', 'high', 'xhigh', 'max'],+ defaultEffort: 'high',+ });+ });++ it('falls back safely for ambiguous matches, missing target routes and invalid defaults', () => {const registry = structuredClone(BUNDLED_CATALOG.modelRegistry);if (!registry) throw new Error("missing bundled model registry");const baseEntry = registry.models.find(@@ -590,7 +683,189 @@ describe("buildUserProvider (per-runtime)", () => {});});- it("exports only the explicitly supported effort levels for a Pi reasoning model", () => {+ it('strips xd/ prefix to match registry effort metadata for claude-code', () => {+ const p = buildUserProvider(+ {+ id: 'my-provider',+ name: 'My Provider',+ runtimes: {+ 'claude-code': {+ baseUrl: 'https://my-provider.example/v1',+ models: [{ id: 'xd/codex/gpt-5.6-sol', name: 'GPT-5.6-Sol' }],+ },+ },+ },+ { modelRegistry: BUNDLED_CATALOG.modelRegistry },+ );+ // xd/codex/gpt-5.6-sol → strips xd/ → codex/gpt-5.6-sol → matches route for claude-code+ // without prefix-stripping, the model ID wouldn't match any registry entry+ // and efforts would fall back to CUSTOM_EFFORTS (no 'xhigh' from registry).+ const model = p.models['claude-code']?.[0];+ expect(model?.efforts?.length).toBeGreaterThan(0);+ expect(model?.efforts).toContain('xhigh');+ });++ it('strips xd/ prefix to match registry effort metadata for codex', () => {+ const p = buildUserProvider(+ {+ id: 'xd-relay',+ name: 'XD Relay',+ runtimes: {+ codex: {+ baseUrl: 'https://xd-relay.example/v1',+ models: [{ id: 'xd/gpt-5.6-sol', name: 'GPT-5.6-Sol' }],+ },+ },+ },+ { modelRegistry: BUNDLED_CATALOG.modelRegistry },+ );+ const model = p.models.codex?.[0];+ expect(model?.efforts?.length).toBeGreaterThan(0);+ expect(model?.efforts).toContain('xhigh');+ });++ it('synthetic registry: prefix stripping is required for openai/xd/chatgpt/ prefixes', () => {+ // Synthetic registry where entry id = 'synthetic-gpt' with 'ultra' effort.+ // Custom model id = 'openai/synthetic-gpt' can only match via prefix stripping.+ // If strip-prefix code is removed, efforts would remain empty.+ const syntheticRegistry: ModelRegistry = {+ updatedAt: '2026-01-01T00:00:00Z',+ schemaVersion: 2,+ models: [+ {+ id: 'synthetic-gpt',+ name: 'Synthetic GPT',+ efforts: ['low', 'medium', 'high', 'xhigh', 'max', 'ultra'],+ defaultEffort: 'high',+ routes: [+ { providerId: 'test-provider', modelId: 'synthetic-gpt', agents: ['claude-code', 'codex'] },+ ],+ },+ ],+ };++ // openai/ prefix: should strip and match synthetic-gpt+ const pOpenai = buildUserProvider(+ { id: 'relay', name: 'R', runtimes: { 'claude-code': { baseUrl: 'https://x/v1', models: [{ id: 'openai/synthetic-gpt', name: 'G' }] } } },+ { modelRegistry: syntheticRegistry },+ );+ expect(pOpenai.models['claude-code']?.[0]?.efforts).toContain('ultra');++ // xd/ prefix: should strip and match+ const pXd = buildUserProvider(+ { id: 'relay', name: 'R', runtimes: { codex: { baseUrl: 'https://x/v1', models: [{ id: 'xd/synthetic-gpt', name: 'G' }] } } },+ { modelRegistry: syntheticRegistry },+ );+ expect(pXd.models.codex?.[0]?.efforts).toContain('ultra');++ // chatgpt/ prefix: should strip and match+ const pChatgpt = buildUserProvider(+ { id: 'relay', name: 'R', runtimes: { 'claude-code': { baseUrl: 'https://x/v1', models: [{ id: 'chatgpt/synthetic-gpt', name: 'G' }] } } },+ { modelRegistry: syntheticRegistry },+ );+ expect(pChatgpt.models['claude-code']?.[0]?.efforts).toContain('ultra');++ // unknown prefix: should NOT match registry, gets default CUSTOM_EFFORTS (no 'ultra')+ const pUnknown = buildUserProvider(+ { id: 'relay', name: 'R', runtimes: { 'claude-code': { baseUrl: 'https://x/v1', models: [{ id: 'unknown/synthetic-gpt', name: 'G' }] } } },+ { modelRegistry: syntheticRegistry },+ );+ expect(pUnknown.models['claude-code']?.[0]?.efforts).not.toContain('ultra');++ // no prefix: should match directly+ const pDirect = buildUserProvider(+ { id: 'relay', name: 'R', runtimes: { 'claude-code': { baseUrl: 'https://x/v1', models: [{ id: 'synthetic-gpt', name: 'G' }] } } },+ { modelRegistry: syntheticRegistry },+ );+ expect(pDirect.models['claude-code']?.[0]?.efforts).toContain('ultra');+ });++ it('Case A: exact match takes priority over prefix-stripped match (no ambiguity)', () => {+ // Registry has two entries:+ // A: id='openai/foo', route.modelId='openai/foo', efforts=['ultra']+ // B: id='other', route.modelId='foo', efforts=['low']+ // Custom model: openai/foo+ // Stage 1 exact: matches A (entry.id='openai/foo') → unique → use A's efforts+ // Without two-stage: both A and B match → ambiguous → fallback+ const reg: ModelRegistry = {+ updatedAt: '2026-01-01T00:00:00Z',+ schemaVersion: 2,+ models: [+ {+ id: 'openai/foo', name: 'OpenAI Foo',+ efforts: ['low', 'medium', 'high', 'xhigh', 'max', 'ultra'],+ defaultEffort: 'high',+ routes: [{ providerId: 'openai', modelId: 'openai/foo', agents: ['codex'] }],+ },+ {+ id: 'other', name: 'Other Foo',+ efforts: ['low', 'medium', 'high'],+ defaultEffort: 'low',+ routes: [{ providerId: 'other', modelId: 'foo', agents: ['codex'] }],+ },+ ],+ };+ const p = buildUserProvider(+ { id: 'relay', name: 'R', runtimes: { codex: { baseUrl: 'https://x/v1', models: [{ id: 'openai/foo', name: 'F' }] } } },+ { modelRegistry: reg },+ );+ // Must select entry A (exact match), not ambiguous fallback+ expect(p.models.codex?.[0]?.efforts).toContain('ultra');+ expect(p.models.codex?.[0]?.defaultEffort).toBe('high');+ });++ it('Case B: no exact match → prefix fallback finds unique entry', () => {+ // No entry with id='openai/bar' or route.modelId='openai/bar'+ // But route.modelId='bar' exists → strip openai/ to find it+ const reg: ModelRegistry = {+ updatedAt: '2026-01-01T00:00:00Z',+ schemaVersion: 2,+ models: [+ {+ id: 'registry-bar', name: 'Registry Bar',+ efforts: ['low', 'medium', 'high', 'xhigh', 'max', 'ultra'],+ defaultEffort: 'max',+ routes: [{ providerId: 'test', modelId: 'bar', agents: ['claude-code'] }],+ },+ ],+ };+ const p = buildUserProvider(+ { id: 'relay', name: 'R', runtimes: { 'claude-code': { baseUrl: 'https://x/v1', models: [{ id: 'openai/bar', name: 'B' }] } } },+ { modelRegistry: reg },+ );+ // Stage 1: no exact match for 'openai/bar'+ // Stage 2: strip openai/ → 'bar' → matches route.modelId → unique+ expect(p.models['claude-code']?.[0]?.efforts).toContain('ultra');+ expect(p.models['claude-code']?.[0]?.defaultEffort).toBe('max');+ });++ it('Case C: no exact match → prefix fallback yields ambiguity → CUSTOM_EFFORTS', () => {+ // Two entries both have route.modelId='baz' after stripping openai/+ const reg: ModelRegistry = {+ updatedAt: '2026-01-01T00:00:00Z',+ schemaVersion: 2,+ models: [+ {+ id: 'entry-1', name: 'E1',+ efforts: ['low', 'ultra'],+ routes: [{ providerId: 'p1', modelId: 'baz', agents: ['codex'] }],+ },+ {+ id: 'entry-2', name: 'E2',+ efforts: ['low', 'high'],+ routes: [{ providerId: 'p2', modelId: 'baz', agents: ['codex'] }],+ },+ ],+ };+ const p = buildUserProvider(+ { id: 'relay', name: 'R', runtimes: { codex: { baseUrl: 'https://x/v1', models: [{ id: 'openai/baz', name: 'B' }] } } },+ { modelRegistry: reg },+ );+ // Ambiguous → falls back to CUSTOM_EFFORTS (no 'ultra')+ expect(p.models.codex?.[0]?.efforts).not.toContain('ultra');+ });++ it('exports only the explicitly supported effort levels for a Pi reasoning model', () => {const p = buildUserProvider({id: "reasoning-pi",name: "Reasoning Pi",diff --git a/packages/model-providers/src/user-provider.ts b/packages/model-providers/src/user-provider.tsindex 76a8b27d5c..0e23e2e1c8 100644--- a/packages/model-providers/src/user-provider.ts+++ b/packages/model-providers/src/user-provider.ts@@ -68,6 +68,23 @@ interface RegistryEffortMetadata {defaultEffort: Effort | null;}+function toRegistryEffortMetadata(+ entry: { efforts?: readonly Effort[]; defaultEffort?: Effort | null; perAgent?: Partial<Record<string, { efforts?: readonly Effort[]; defaultEffort?: Effort | null }>> },+ agent: AgentKind,+): RegistryEffortMetadata | undefined {+ const perAgent = entry.perAgent?.[agent];+ const efforts = perAgent?.efforts ?? entry.efforts;+ if (!efforts) return undefined;+ const declaredDefault = perAgent?.defaultEffort ?? entry.defaultEffort;+ const defaultEffort =+ declaredDefault && efforts.includes(declaredDefault)+ ? declaredDefault+ : efforts.includes(DEFAULT_CUSTOM_EFFORT)+ ? DEFAULT_CUSTOM_EFFORT+ : (efforts[efforts.length - 1] ?? null);+ return { efforts: [...efforts], defaultEffort };+}+/*** 仅在模型能由当前 agent 的 Registry route 唯一识别时复用 effort 元数据。* 自定义 provider 的 id、model id 与路由保持原值;Pi 能力继续只认逐模型显式配置。@@ -79,34 +96,41 @@ function registryEffortMetadata(): RegistryEffortMetadata | undefined {if (agent === "pi" || !registry) return undefined;- const candidates = new Set([modelId]);- if (modelId.startsWith("chatgpt/")) {- candidates.add(modelId.slice("chatgpt/".length));- }- const matches = registry.models.filter((entry) =>+ // Stage 1 — exact lookup: only the original modelId.+ const exactMatches = registry.models.filter((entry) =>entry.routes.some((route) =>route.agents.includes(agent) &&- (candidates.has(entry.id) || candidates.has(route.modelId)),+ (entry.id === modelId || route.modelId === modelId),),);- const uniqueEntries = [- ...new Map(matches.map((entry) => [entry.id, entry])).values(),+ const exactUnique = [+ ...new Map(exactMatches.map((entry) => [entry.id, entry])).values(),];- if (uniqueEntries.length !== 1) return undefined;+ if (exactUnique.length === 1) return toRegistryEffortMetadata(exactUnique[0]!, agent);+ if (exactUnique.length > 1) return undefined; // ambiguous, don't strip- const entry = uniqueEntries[0]!;- const perAgent = entry.perAgent?.[agent];- const efforts = perAgent?.efforts ?? entry.efforts;- if (!efforts) return undefined;- const declaredDefault = perAgent?.defaultEffort ?? entry.defaultEffort;- const defaultEffort =- declaredDefault && efforts.includes(declaredDefault)- ? declaredDefault- : efforts.includes(DEFAULT_CUSTOM_EFFORT)- ? DEFAULT_CUSTOM_EFFORT- : (efforts[efforts.length - 1] ?? null);- return { efforts: [...efforts], defaultEffort };+ // Stage 2 — prefix fallback: only when Stage 1 found nothing.+ // Strip common provider prefixes so third-party custom API models+ // (e.g. "openai/gpt-5.6-sol") can match registry entries whose+ // route.modelId is just "gpt-5.6-sol".+ const stripped = new Set<string>();+ for (const prefix of ['openai/', 'xd/', 'chatgpt/']) {+ if (modelId.startsWith(prefix)) stripped.add(modelId.slice(prefix.length));+ }+ if (stripped.size === 0) return undefined;+ const fallbackMatches = registry.models.filter((entry) =>+ entry.routes.some(+ (route) =>+ route.agents.includes(agent) &&+ (stripped.has(entry.id) || stripped.has(route.modelId)),+ ),+ );+ const fallbackUnique = [+ ...new Map(fallbackMatches.map((entry) => [entry.id, entry])).values(),+ ];+ if (fallbackUnique.length !== 1) return undefined;+ return toRegistryEffortMetadata(fallbackUnique[0]!, agent);}/** 固定 agent 顺序:保证派生出的 provider.agents / routing / models 顺序稳定。 */