Make Codex and Claude Code subscriptions go further: a local model router that gives routine agent work to economical models and keeps your strongest model for the hard parts.
Rust ai-agentsai-gatewayclaude-codecodexcoding-agentcost-optimizationdeepseekllm-gatewayllm-routerlocal-firstmodel-routingrusttask-routingtauri
fix: show every built-in API in custom prefill (#18)
澄潭22ab3c7b14cc
diff --git a/apps/desktop/src/features/model-connections/ModelConnectionForm.tsx b/apps/desktop/src/features/model-connections/ModelConnectionForm.tsxindex 2fbf47c4..3a9f8d3f 100644--- a/apps/desktop/src/features/model-connections/ModelConnectionForm.tsx+++ b/apps/desktop/src/features/model-connections/ModelConnectionForm.tsx@@ -35,7 +35,7 @@ import type {} from './types';import { checkFailureCode, modelAvailabilityMessage } from './copy';import { metadataModelPrefill } from './metadata-prefill';-import { metadataProviderCandidates, metadataProviderOption, registeredModelCandidates, sortMetadataProviders, sortRegisteredOptions, templateEndpointForProtocol } from './registered-metadata';+import { customApiPrefillOptions, metadataProviderCandidates, metadataProviderOption, registeredModelCandidates, sortMetadataProviders, sortRegisteredOptions, templateEndpointForProtocol } from './registered-metadata';import {connectionErrorMessage,ManualModelEditor,@@ -73,7 +73,7 @@ export function ModelConnectionForm(props: ModelConnectionFormProps) {const [connectionOptions, setConnectionOptions] = useState<ComputeConnectionOptions | null>(null);const [optionsLoading, setOptionsLoading] = useState(true);const [registeredOptionId, setRegisteredOptionId] = useState('');- const [metadataProviderKey, setMetadataProviderKey] = useState('');+ const [metadataPrefillKey, setMetadataPrefillKey] = useState('');const password = useRef<HTMLInputElement>(null);const errorFeedback = useRef<HTMLDivElement>(null);const draftRef = useRef(draft);@@ -98,10 +98,13 @@ export function ModelConnectionForm(props: ModelConnectionFormProps) {&& provider.base_url_candidates.length > 0&& provider.protocol_candidates.length > 0) ?? []), [connectionOptions]);- const metadataProvider = metadataProviders.find(provider => provider.provider_record_key === metadataProviderKey) ?? null;- const metadataCandidates = useMemo(() => metadataProvider && connectionOptions?.metadata_catalog- ? metadataProviderCandidates(metadataProvider, connectionOptions.metadata_catalog, registeredOptions)- : [], [metadataProvider, connectionOptions, registeredOptions]);+ const metadataPrefills = useMemo(() => customApiPrefillOptions(registeredOptions, metadataProviders), [registeredOptions, metadataProviders]);+ const selectedPrefill = metadataPrefills.find(prefill => prefill.key === metadataPrefillKey) ?? null;+ const metadataCandidates = useMemo(() => selectedPrefill?.kind === 'template'+ ? registeredModelCandidates(selectedPrefill.option, connectionOptions?.metadata_catalog)+ : selectedPrefill?.kind === 'provider' && connectionOptions?.metadata_catalog+ ? metadataProviderCandidates(selectedPrefill.provider, connectionOptions.metadata_catalog, registeredOptions)+ : [], [selectedPrefill, connectionOptions, registeredOptions]);useDiscardGuard('models', () => dirty && !saveCompletedRef.current, language, confirmReplacement);const releaseProtectedInput = useCallback(() => {@@ -132,7 +135,6 @@ export function ModelConnectionForm(props: ModelConnectionFormProps) {}, [backend, custom, free]);function selectMetadataProvider(provider: ProviderMetadataRecord | null) {- setMetadataProviderKey(provider?.provider_record_key ?? '');if (!provider) return;const catalog = connectionOptions?.metadata_catalog;const option = catalog ? metadataProviderOption(provider, registeredOptions) : null;@@ -178,6 +180,39 @@ export function ModelConnectionForm(props: ModelConnectionFormProps) {}), true, 'connection');}+ function selectRegisteredTemplate(option: ComputeConnectionOptions['options'][number], keepExistingModels = false) {+ const endpoint = [...(option.endpoints ?? [])].sort((a, b) => a.stable_preference - b.stable_preference)[0];+ if (!endpoint) return;+ const candidates = registeredModelCandidates(option, connectionOptions?.metadata_catalog);+ const otherProtocols = new Set<UpstreamProtocol>();+ const additional_endpoints = [...(option.endpoints ?? [])]+ .sort((a, b) => a.stable_preference - b.stable_preference)+ .filter(other => other.protocol !== endpoint.protocol && !otherProtocols.has(other.protocol) && Boolean(otherProtocols.add(other.protocol)))+ .map(other => ({ base_url: other.base_url, base_kind: 'api_root' as const,+ request_path_override: other.request_path, inventory_path_override: other.inventory_path ?? null,+ protocol: other.protocol, protocol_profile_id: `profile/custom/${other.protocol}`,+ protocol_profile_revision: 1, authentication: other.authentication_semantics ?? { kind: 'bearer' as const } }));+ setCustom(true);+ setMetadataPrefillKey(`template:${option.connection_option_id}`);+ invalidate(current => ({ ...current,+ entry_kind: 'custom_api', display_template_id: option.connection_option_id,+ display_name: connectionName(option.connection_option_id, language, option.display_name),+ base_url: endpoint.base_url, base_kind: 'api_root',+ request_path_override: endpoint.request_path, inventory_path_override: endpoint.inventory_path ?? null,+ protocol: endpoint.protocol, protocol_profile_id: `profile/custom/${endpoint.protocol}`,+ protocol_profile_revision: current.protocol_profile_revision + 1,+ authentication: endpoint.authentication_semantics ?? { kind: 'bearer' },+ additional_endpoints, models: withRegisteredModels(keepExistingModels ? current.models : [], candidates),+ }), true, 'connection');+ }++ function selectMetadataPrefill(key: string) {+ setMetadataPrefillKey(key);+ const prefill = metadataPrefills.find(value => value.key === key);+ if (prefill?.kind === 'template') selectRegisteredTemplate(prefill.option);+ else if (prefill?.kind === 'provider') selectMetadataProvider(prefill.provider);+ }+useEffect(() => { draftRef.current = draft; }, [draft]);useEffect(() => {if (!error || phase !== 'editing') return;@@ -541,12 +576,17 @@ export function ModelConnectionForm(props: ModelConnectionFormProps) {{!custom && !free && <button className="btn btn-quiet" type="button" onClick={() => { setCustom(true); invalidate(current => ({ ...current, entry_kind: 'custom_api' }), true, 'connection'); }}>{zh ? '自定义 API' : 'Custom API'}</button>}{custom ? <><ProviderIcon optionId={draft.display_template_id} language={language} />- {!optionsLoading && metadataProviders.length > 0 && <div className="connection-fields metadata-prefill-fields">- <label className="field"><span className="field-label">{zh ? '供应商元数据预填(可选)' : 'Provider metadata prefill (optional)'}</span><select className="select" value={metadataProviderKey} onChange={event => selectMetadataProvider(metadataProviders.find(provider => provider.provider_record_key === event.target.value) ?? null)}>+ {!optionsLoading && metadataPrefills.length > 0 && <div className="connection-fields metadata-prefill-fields">+ <label className="field"><span className="field-label">{zh ? '接入资料预填(可选)' : 'Connection prefill (optional)'}</span><select className="select" value={metadataPrefillKey} onChange={event => selectMetadataPrefill(event.target.value)}><option value="">{zh ? '不使用预填' : 'No prefill'}</option>- {metadataProviders.map(provider => <option key={provider.provider_record_key} value={provider.provider_record_key}>{provider.display_name}</option>)}+ <optgroup label={zh ? '内置接入' : 'Built-in connections'}>+ {metadataPrefills.filter(prefill => prefill.kind === 'template').map(prefill => <option key={prefill.key} value={prefill.key}>{connectionName(prefill.option.connection_option_id, language, prefill.option.display_name)}</option>)}+ </optgroup>+ {metadataProviders.length > 0 && <optgroup label={zh ? '供应商资料记录' : 'Provider metadata records'}>+ {metadataPrefills.filter(prefill => prefill.kind === 'provider').map(prefill => <option key={prefill.key} value={prefill.key}>{prefill.provider.display_name}</option>)}+ </optgroup>}</select></label>- {metadataProvider && <p className="field-help">{zh+ {selectedPrefill && <p className="field-help">{zh? `已收录 ${metadataCandidates.length} 个模型,检查接入后统一选择;未知能力保持空白,凭据不会从资料推断。`: `${metadataCandidates.length} models in this catalog. Choose after checking the connection; unknown capabilities remain blank and credentials are never inferred.`}</p>}</div>}@@ -604,19 +644,7 @@ export function ModelConnectionForm(props: ModelConnectionFormProps) {<p className="field-help">{zh? `已收录 ${registeredCandidates.length} 个模型,填写 Key 后统一选择。具体可用范围取决于账号和套餐。`: `${registeredCandidates.length} models in this catalog. Enter your key, then choose models. Availability depends on your account and plan.`}</p>- {registeredOption.endpoints?.length && <button className="btn btn-quiet" type="button" onClick={() => {- const endpoint = [...registeredOption.endpoints!].sort((a, b) => a.stable_preference - b.stable_preference)[0];- setCustom(true);- const otherProtocols = new Set<UpstreamProtocol>();- const additional_endpoints = [...(registeredOption.endpoints ?? [])]- .sort((a, b) => a.stable_preference - b.stable_preference)- .filter(other => other.protocol !== endpoint.protocol && !otherProtocols.has(other.protocol) && Boolean(otherProtocols.add(other.protocol)))- .map(other => ({ base_url: other.base_url, base_kind: 'api_root' as const,- request_path_override: other.request_path, inventory_path_override: other.inventory_path ?? null,- protocol: other.protocol, protocol_profile_id: `profile/custom/${other.protocol}`,- protocol_profile_revision: 1, authentication: other.authentication_semantics ?? { kind: 'bearer' as const } }));- invalidate(current => ({ ...current, entry_kind: 'custom_api', display_template_id: registeredOption.connection_option_id, display_name: connectionName(registeredOption.connection_option_id, language, registeredOption.display_name), base_url: endpoint.base_url, request_path_override: endpoint.request_path, inventory_path_override: endpoint.inventory_path ?? null, protocol: endpoint.protocol, protocol_profile_id: `profile/custom/${endpoint.protocol}`, authentication: endpoint.authentication_semantics ?? { kind: 'bearer' }, additional_endpoints, models: withRegisteredModels(current.models, registeredCandidates) }), true, 'connection');- }}>{zh ? '自定义此模板的连接配置' : 'Customize this template connection'}</button>}+ {registeredOption.endpoints?.length && <button className="btn btn-quiet" type="button" onClick={() => selectRegisteredTemplate(registeredOption, true)}>{zh ? '自定义此模板的连接配置' : 'Customize this template connection'}</button>}</section>}{custom && <><label className="field"><span className="field-label">{zh ? '请求路径(可选覆盖)' : 'Request path (optional override)'}</span><input className="input" value={draft.request_path_override ?? ''} onChange={event => invalidate(current => ({ ...current, request_path_override: event.target.value || null }), true, 'connection')} /></label>diff --git a/apps/desktop/src/features/model-connections/registered-metadata.ts b/apps/desktop/src/features/model-connections/registered-metadata.tsindex 6818aaff..323834d2 100644--- a/apps/desktop/src/features/model-connections/registered-metadata.ts+++ b/apps/desktop/src/features/model-connections/registered-metadata.ts@@ -43,6 +43,18 @@ export function sortMetadataProviders(providers: ProviderMetadataRecord[]): Provreturn [...providers].sort((left, right) => rank(left.provider_record_key) - rank(right.provider_record_key));}+/** Offer every built-in product even when no provider record carries prefill evidence. */+export function customApiPrefillOptions(options: ComputeConnectionOption[], providers: ProviderMetadataRecord[]) {+ return [+ ...sortRegisteredOptions(options).map(option => ({+ kind: 'template' as const, key: `template:${option.connection_option_id}`, option,+ })),+ ...sortMetadataProviders(providers).map(provider => ({+ kind: 'provider' as const, key: `provider:${provider.provider_record_key}`, provider,+ })),+ ];+}+const metadataProtocol: Record<UpstreamProtocol, string> = {chat_completions: 'openai-chat',messages: 'anthropic-messages',diff --git a/apps/desktop/tests/registered-metadata.test.mjs b/apps/desktop/tests/registered-metadata.test.mjsindex 97574749..fc2d70dc 100644--- a/apps/desktop/tests/registered-metadata.test.mjs+++ b/apps/desktop/tests/registered-metadata.test.mjs@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';import { readFileSync } from 'node:fs';import test from 'node:test';-import { metadataProviderCandidates, metadataProviderOption, registeredModelCandidates, sortMetadataProviders, sortRegisteredOptions, templateEndpointForProtocol } from '../src/features/model-connections/registered-metadata.ts';+import { customApiPrefillOptions, metadataProviderCandidates, metadataProviderOption, registeredModelCandidates, sortMetadataProviders, sortRegisteredOptions, templateEndpointForProtocol } from '../src/features/model-connections/registered-metadata.ts';const bundle = JSON.parse(readFileSync(new URL('../../../assets/release-facts/current/bundle/model-data.json', import.meta.url)));const registry = JSON.parse(readFileSync(new URL('../../../assets/release-facts/current/bundle/connector-registry.json', import.meta.url)));@@ -165,6 +165,24 @@ test('Bailian Token Plan is first among custom API metadata prefills', () => {]);});+test('direct custom API prefill offers every built-in product with its scoped model catalog', () => {+ const options = registry.connection_options.filter(value => value.origin !== 'agent_subscription')+ .map(value => option(value.connection_option_id));+ const providers = bundle.metadata_catalog.provider_records.filter(value =>+ value.usable_for.includes('custom-api-endpoint-prefill')+ && value.base_url_candidates.length > 0 && value.protocol_candidates.length > 0);+ const prefills = customApiPrefillOptions(options, providers);+ const templates = prefills.filter(value => value.kind === 'template');+ assert.equal(templates.length, options.length);+ assert.deepEqual(new Set(templates.map(value => value.option.connection_option_id)),+ new Set(options.map(value => value.connection_option_id)));+ assert.equal(templates[0].option.connection_option_id, 'bailian.token-plan.cn.v1');+ assert.deepEqual(registeredModelCandidates(templates[0].option, bundle.metadata_catalog)+ .map(value => value.upstream_model_id).sort(), [...teamTokenModels].sort());+ assert.deepEqual(prefills.filter(value => value.kind === 'provider').map(value => value.provider.provider_record_key),+ sortMetadataProviders(providers).map(value => value.provider_record_key));+});+test('priority providers expose their current scoped model IDs', () => {const expected = new Map([['bailian.payg.cn.v1', ['qwen3.8-max', 'deepseek-v4.1-flash', 'deepseek-v4-pro', 'glm-5.3', 'kimi-k3']],