Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(model-providers): honor active media metadata in BYOK settings

JiangJiahao1c647df80815
diff --git a/apps/desktop/src/main/maker-host/__tests__/modelMetadataLayers.integration.test.ts b/apps/desktop/src/main/maker-host/__tests__/modelMetadataLayers.integration.test.ts
index e31dba8ae7..a27b2a5970 100644
--- a/apps/desktop/src/main/maker-host/__tests__/modelMetadataLayers.integration.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/modelMetadataLayers.integration.test.ts
@@ -50,6 +50,7 @@ import {
getActiveCatalog,
getActiveLocalModelCatalog,
setActiveCatalog,
+ setCustomProviderConfigs,
setCustomProviders,
setLocalCatalogOverrides,
setXdGatewayModels,
@@ -113,6 +114,35 @@ afterEach(() => {
setActiveCatalog(BUNDLED_CATALOG);
});
describe('metadata layers through the active catalog', () => {
+ it.each([undefined, null, 'openai-audio-speech'] as const)(
+ 'refreshes media protocol declarations without changing explicit user override %s', (nativeApi) => {
+ const config: CustomProviderConfig = { id: 'speech-refresh', name: 'Speech', runtimes: {
+ codex: { baseUrl: 'https://speech.example.test/v1', wireProtocol: 'openai-chat', models: [{
+ id: 'new-server-voice', name: 'Voice',
+ discoveredMetadata: { mode: 'audio_speech', nativeApi: 'openai-audio-speech' },
+ ...(nativeApi !== undefined ? { nativeApi } : {}),
+ }] },
+ } };
+ const original = structuredClone(config);
+ const publish = (declaration: 'openai-audio-speech' | null) => setActiveCatalog({
+ ...BUNDLED_CATALOG,
+ modelRegistry: { schemaVersion: 5, updatedAt: declaration ? '2026-10-12T06:00:00.000Z' : '2026-10-12T07:00:00.000Z',
+ baseModels: [{ id: 'new-server-voice', aliases: [], defaults: { mode: 'audio_speech' } }],
+ models: [{ id: 'new-server-voice', name: 'Voice', modelRef: 'new-server-voice', nativeApi: declaration,
+ routes: [{ providerId: 'xd', modelId: 'new-server-voice', agents: [] }] }],
+ },
+ });
+ publish('openai-audio-speech');
+ setCustomProviderConfigs([config]);
+ const activeProvider = () => getActiveCatalog().providers.find(p => p.id === config.id)!;
+ expect(activeProvider().audioModels?.[0].nativeApi).toBe(nativeApi === null ? null : 'openai-audio-speech');
+ publish(null);
+ expect(activeProvider().models.codex![0].nativeApi).toBe(nativeApi === undefined ? null : nativeApi);
+ expect(activeProvider().audioModels?.[0].nativeApi).toBe(nativeApi === undefined ? null : nativeApi);
+ expect(activeProvider().models.codex![0].userModelConfig).toEqual(original.runtimes.codex!.models[0]);
+ expect(config).toEqual(original);
+ });
+
it('keeps BYOK speech metadata through config validation, JSON persistence and active catalog refresh', () => {
const discovered = parseModelsListResponse({ data: [{ id: 'gpt-private-voice',
mode: 'audio_speech', native_api: 'openai-audio-speech' }] })!;
diff --git a/apps/desktop/src/renderer/components/settings/ProviderConnectionDialog.tsx b/apps/desktop/src/renderer/components/settings/ProviderConnectionDialog.tsx
index f16b7bb2c1..7a8af183e4 100644
--- a/apps/desktop/src/renderer/components/settings/ProviderConnectionDialog.tsx
+++ b/apps/desktop/src/renderer/components/settings/ProviderConnectionDialog.tsx
@@ -71,6 +71,7 @@ import {
restoreHydratedApiKey,
stripCredentialHeaders,
type CustomProviderAuthMode,
+ type ProviderConnectionTestSignatureFields,
type SavedProviderProbeBaseline,
} from '@/lib/providerModelFetch';
import {
@@ -102,6 +103,7 @@ import type {
CustomProviderConfig,
ProviderPreset,
ProviderRuntimeModelConfig,
+ ProviderView,
ProviderWireProtocol,
} from '@cindy/model-providers';
import { SettingsTextInput } from './SettingsTextInput';
@@ -154,6 +156,8 @@ function defaultWireFor(agent: DialogAgentKind): ProviderWireProtocol {
interface ProviderConnectionDialogProps {
initial?: CustomProviderConfig;
+ /** Effective modes from the same connection's active catalog, separate from editable overrides. */
+ catalogModels?: ProviderView['models'];
/** 已占用的全部 provider id(内置 anthropic/openai/xd + 全部自定义);新建时自动生成 id 时避让,防撞内置保留 id。 */
existingIds?: string[];
/** Stable fallback for transitions whose immediate opener unmounts before this dialog mounts. */
@@ -201,9 +205,14 @@ interface RuntimeFields extends RuntimeFillDraft {
videoApi?: 'xai-videos';
}
-function runtimeProbeFields(agent: DialogAgentKind, runtime: RuntimeFields): RuntimeFields {
+function runtimeProbeFields(
+ agent: DialogAgentKind,
+ runtime: RuntimeFields,
+ catalogModels?: ProviderConnectionTestSignatureFields['catalogModels'],
+): RuntimeFields & Pick<ProviderConnectionTestSignatureFields, 'catalogModels'> {
return {
...runtime,
+ catalogModels,
requestPath: agent === 'pi' ? '' : runtime.requestPath,
models: modelsAfterProviderEndpointEdit(runtime.models, runtime.modelRouteBaseUrl, runtime.baseUrl),
};
@@ -403,6 +412,7 @@ function PresetDropdown({
export function ProviderConnectionDialog({
initial,
+ catalogModels,
existingIds,
returnFocusRef,
focusAgent,
@@ -414,6 +424,7 @@ export function ProviderConnectionDialog({
const onClose = useCallback(() => dialog.close(notifyClosed), [dialog.close, notifyClosed]);
const onSaved = useCallback(() => dialog.close(notifySaved), [dialog.close, notifySaved]);
const editing = !!initial;
+ const catalogModelsRef = useRef(catalogModels);
const initialOAuth = initial?.auth?.method === 'oauth' ? initial.auth.oauth : undefined;
const formId = useId();
@@ -676,6 +687,18 @@ export function ProviderConnectionDialog({
// (见 setRtSynced),不用被动 useEffect——effect 在 commit 后才跑,IPC 响应若落在
// 状态更新与 effect 之间会读到旧值。
const rtRef = useRef(rt);
+ useLayoutEffect(() => {
+ const previousCatalogModels = catalogModelsRef.current;
+ catalogModelsRef.current = catalogModels;
+ const changedAgents = AGENTS.filter(agent =>
+ firstProviderChatModel(rtRef.current[agent].models, previousCatalogModels?.[agent])?.id !==
+ firstProviderChatModel(rtRef.current[agent].models, catalogModels?.[agent])?.id);
+ if (changedAgents.length) setTest(previous => {
+ const next = { ...previous };
+ for (const agent of changedAgents) next[agent] = IDLE_TEST;
+ return next;
+ });
+ }, [catalogModels]);
/** 唯一的 rt 写入口:状态更新的同时同步镜像进 rtRef(updater 幂等,StrictMode 双调无害)。 */
const setRtSynced = useCallback(
(
@@ -747,10 +770,10 @@ export function ProviderConnectionDialog({
authMode: savedAuthMode,
apiKey: loadedKeyRef.current[agent] ?? '',
...(agent === 'pi'
- ? { modelPiApi: firstProviderChatModel(rc.models)?.piApi }
+ ? { modelPiApi: firstProviderChatModel(rc.models, catalogModels?.[agent])?.piApi }
: {}),
- modelRoute: firstProviderChatModel(rc.models)?.route,
- modelApi: firstProviderChatModel(rc.models)?.api,
+ modelRoute: firstProviderChatModel(rc.models, catalogModels?.[agent])?.route,
+ modelApi: firstProviderChatModel(rc.models, catalogModels?.[agent])?.api,
catalogPresetId: rc.catalogPresetId,
headers:
rc.headers && Object.keys(rc.headers).length > 0
@@ -758,7 +781,7 @@ export function ProviderConnectionDialog({
: [],
};
},
- [editing, initial],
+ [editing, initial, catalogModels],
);
// URL edits temporarily clear an untouched hydrated key so it cannot be
@@ -1175,7 +1198,7 @@ export function ProviderConnectionDialog({
);
const f = rt[activeTab];
- const mediaOnly = f.models.some(model => model.id.trim()) && !firstProviderChatModel(f.models);
+ const mediaOnly = f.models.some(model => model.id.trim()) && !firstProviderChatModel(f.models, catalogModels?.[activeTab]);
const boundPreset = presets.find(preset => preset.id === f.catalogPresetId);
const templateBound = Boolean(f.catalogPresetId);
const endpointTemplate = boundPreset?.runtimes[activeTab]?.baseUrl;
@@ -1200,9 +1223,9 @@ export function ProviderConnectionDialog({
const handleTest = useCallback(async () => {
const agent = activeTab;
const rf = rt[agent];
- const probeFields = runtimeProbeFields(agent, rf);
+ const probeFields = runtimeProbeFields(agent, rf, catalogModels?.[agent]);
const defaultBaseUrl = rf.baseUrl.trim();
- const firstModelConfig = firstProviderChatModel(rf.models);
+ const firstModelConfig = firstProviderChatModel(rf.models, catalogModels?.[agent]);
const firstModel = firstModelConfig?.id.trim();
if (!matchesEndpointTemplate(agent, rf)) {
toast.error(t('settings.providers.custom.errors.baseUrlInvalid'));
@@ -1270,7 +1293,7 @@ export function ProviderConnectionDialog({
);
if (
providerConnectionTestRequestSignature(
- runtimeProbeFields(agent, rtRef.current[agent]),
+ runtimeProbeFields(agent, rtRef.current[agent], catalogModelsRef.current?.[agent]),
authModeRef.current,
) !== requestSig
)
@@ -1284,7 +1307,7 @@ export function ProviderConnectionDialog({
} catch (e) {
if (
providerConnectionTestRequestSignature(
- runtimeProbeFields(agent, rtRef.current[agent]),
+ runtimeProbeFields(agent, rtRef.current[agent], catalogModelsRef.current?.[agent]),
authModeRef.current,
) !== requestSig
)
@@ -1293,7 +1316,7 @@ export function ProviderConnectionDialog({
setTest((prev) => ({ ...prev, [agent]: { status: 'fail', code: 'UNKNOWN' } }));
if (ipc?.message) toast.error(ipc.message);
}
- }, [activeTab, authMode, rt, t, savedBaselineFor, initial, presets, matchesEndpointTemplate]);
+ }, [activeTab, authMode, rt, t, savedBaselineFor, initial, presets, matchesEndpointTemplate, catalogModels]);
// 拉取单飞:任一 runtime(含 Pi)在途时所有 Tab 的拉取按钮都禁用——两个并发请求会竞争
// 同一个勾选弹层(后到的覆盖先开的、确认还会写进另一个 runtime),单飞直接消掉这类竞态。
diff --git a/apps/desktop/src/renderer/components/settings/ProvidersSection.tsx b/apps/desktop/src/renderer/components/settings/ProvidersSection.tsx
index c29bbdef3a..3a672791e2 100644
--- a/apps/desktop/src/renderer/components/settings/ProvidersSection.tsx
+++ b/apps/desktop/src/renderer/components/settings/ProvidersSection.tsx
@@ -135,7 +135,7 @@ import { localCliDisplayName, type LocalCliDetection } from '../../../shared/loc
import { isBuiltinRefreshableProviderId } from '../../../shared/providerModelRefresh';
import { applyProviderOrder } from '../../../shared/providerOrder';
import type { AgentKind, CustomProviderConfig, ProviderView } from '@cindy/model-providers';
-import { isCustomRoutedProvider, isOrganizationManagedProvider } from '@cindy/model-providers';
+import { isCustomRoutedProvider, isOrganizationManagedProvider, storedCustomProviderId } from '@cindy/model-providers';
// ---------------------------------------------------------------------------
// 工具
@@ -3471,6 +3471,8 @@ export function ProvidersSection() {
{dialog && (
<ProviderConnectionDialog
initial={dialog.mode === 'edit' ? dialog.config : undefined}
+ catalogModels={dialog.mode === 'edit' ? providers.find(p =>
+ p.source === 'user' && storedCustomProviderId(p.id) === dialog.config.id)?.models : undefined}
focusAgent={dialog.mode === 'edit' ? dialog.focusAgent : undefined}
existingIds={providers.map((p) => p.id)}
returnFocusRef={dialog.mode === 'create' ? addProviderButtonRef : undefined}
diff --git a/apps/desktop/src/renderer/components/settings/__tests__/CustomProviderDialogAccessibility.test.tsx b/apps/desktop/src/renderer/components/settings/__tests__/CustomProviderDialogAccessibility.test.tsx
index fc5964cdd9..ab60e23631 100644
--- a/apps/desktop/src/renderer/components/settings/__tests__/CustomProviderDialogAccessibility.test.tsx
+++ b/apps/desktop/src/renderer/components/settings/__tests__/CustomProviderDialogAccessibility.test.tsx
@@ -3,7 +3,8 @@ import { useRef, useState } from 'react';
import { act, cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
-import type { CustomProviderConfig } from '@cindy/model-providers';
+import { buildUserProvider, type CustomProviderConfig, type ModelRegistry } from '@cindy/model-providers';
+import { customProviderModelConfigFromCatalogModel } from '@/lib/customProviders';
import { ProviderConnectionDialog } from '../ProviderConnectionDialog';
@@ -59,6 +60,66 @@ describe('BYOK speech catalog settings', () => {
codex: { baseUrl: 'https://speech.example.test/v1', wireProtocol: 'openai-chat', models },
} };
}
+ const activeSpeechRegistry: ModelRegistry = { schemaVersion: 5, updatedAt: '2026-10-12T06:00:00.000Z',
+ baseModels: [{ id: 'new-server-voice', aliases: [], defaults: { mode: 'audio_speech' } }], models: [],
+ };
+
+ it.each([false, true])('keeps active-only speech models out of chat tests with edited key: %s', async (editKey) => {
+ const provider = buildUserProvider(speechConnection([{ id: 'new-server-voice', name: 'Voice' }]), { modelRegistry: activeSpeechRegistry });
+ const initial = speechConnection(provider.models.codex!.map(model => customProviderModelConfigFromCatalogModel(model, 'codex')));
+ customProviderMocks.readCustomProviderKey.mockResolvedValue('old-secret');
+ const user = userEvent.setup();
+ render(<ProviderConnectionDialog initial={initial} catalogModels={provider.models} onSaved={vi.fn()} onClose={vi.fn()} />);
+ await screen.findByText('settings.providers.custom.fields.apiKeySaved');
+ if (editKey) {
+ const key = screen.getByPlaceholderText('settings.providers.custom.fields.apiKeyEditPlaceholder');
+ await user.clear(key);
+ await user.type(key, 'new-secret');
+ }
+ expect(screen.queryByRole('button', { name: 'settings.providers.custom.test.button' })).toBeNull();
+ expect(screen.getByText('settings.providers.connection.mediaOnlyTestHint')).toBeTruthy();
+ await user.click(screen.getByRole('button', { name: 'settings.providers.custom.save' }));
+ await waitFor(() => expect(customProviderMocks.updateCustomProvider).toHaveBeenCalledOnce());
+ expect(customProviderMocks.updateCustomProvider.mock.calls[0][0].runtimes.codex.models).toEqual(initial.runtimes.codex!.models);
+ expect(window.electronAPI.maker.testProviderConnection).not.toHaveBeenCalled();
+ });
+
+ it.each(['saved', 'adhoc'] as const)('uses the chat member in a mixed active catalog for %s probes', async (kind) => {
+ const initial = speechConnection([{ id: 'new-server-voice', name: 'Voice' }, { id: 'private-chat', name: 'Chat' }]);
+ const provider = buildUserProvider(initial, { modelRegistry: activeSpeechRegistry });
+ customProviderMocks.readCustomProviderKey.mockResolvedValue('old-secret');
+ const user = userEvent.setup();
+ render(<ProviderConnectionDialog initial={initial} catalogModels={provider.models} onSaved={vi.fn()} onClose={vi.fn()} />);
+ await screen.findByText('settings.providers.custom.fields.apiKeySaved');
+ if (kind === 'adhoc') {
+ const key = screen.getByPlaceholderText('settings.providers.custom.fields.apiKeyEditPlaceholder');
+ await user.clear(key);
+ await user.type(key, 'new-secret');
+ }
+ await user.click(screen.getByRole('button', { name: 'settings.providers.custom.test.button' }));
+ expect(window.electronAPI.maker.testProviderConnection).toHaveBeenCalledWith(kind === 'saved'
+ ? { kind: 'saved', providerId: initial.id, agent: 'codex' }
+ : { kind: 'adhoc', spec: expect.objectContaining({ modelId: 'private-chat', apiKey: 'new-secret' }) });
+ });
+
+ it('discards an in-flight chat result when the active catalog identifies its model as speech', async () => {
+ const initial = speechConnection([{ id: 'new-server-voice', name: 'Voice' }]);
+ const provider = buildUserProvider(initial, { modelRegistry: activeSpeechRegistry });
+ let finish!: (result: { ok: true; latencyMs: number }) => void;
+ window.electronAPI.maker.testProviderConnection = vi.fn().mockImplementation(() => new Promise(resolve => { finish = resolve; }));
+ customProviderMocks.readCustomProviderKey.mockResolvedValue('old-secret');
+ const user = userEvent.setup();
+ const props = { initial, onSaved: vi.fn(), onClose: vi.fn() };
+ const { rerender } = render(<ProviderConnectionDialog {...props} />);
+ await screen.findByText('settings.providers.custom.fields.apiKeySaved');
+ await user.click(screen.getByRole('button', { name: 'settings.providers.custom.test.button' }));
+ await act(async () => { rerender(<ProviderConnectionDialog {...props} catalogModels={provider.models} />); });
+ await act(async () => { finish({ ok: true, latencyMs: 1 }); });
+ expect(screen.queryByRole('button', { name: 'settings.providers.custom.test.button' })).toBeNull();
+ await act(async () => { rerender(<ProviderConnectionDialog {...props} />); });
+ expect(screen.getByRole('button', { name: 'settings.providers.custom.test.button' }).hasAttribute('disabled')).toBe(false);
+ expect(screen.queryByText('settings.providers.custom.test.ok')).toBeNull();
+ });
it('manually marks an unknown model as speech and preserves that choice and its protocol through refresh and save', async () => {
customProviderMocks.readCustomProviderKey.mockResolvedValue(null);
diff --git a/apps/desktop/src/renderer/lib/__tests__/providerModelFetch.test.ts b/apps/desktop/src/renderer/lib/__tests__/providerModelFetch.test.ts
index 179d157da0..8080852288 100644
--- a/apps/desktop/src/renderer/lib/__tests__/providerModelFetch.test.ts
+++ b/apps/desktop/src/renderer/lib/__tests__/providerModelFetch.test.ts
@@ -1,4 +1,6 @@
import { describe, expect, it } from 'vitest';
+import { buildUserProvider, type ModelRegistry, type ProviderRuntimeModelConfig } from '@cindy/model-providers';
+import { customProviderModelConfigFromCatalogModel } from '../customProviders';
import {
firstProviderChatModel,
@@ -33,6 +35,53 @@ it('excludes exact public speech identities from chat probes without guessing un
expect(firstProviderChatModel([{ ...speech, mode: 'chat' }])?.id).toBe(speech.id);
});
+describe('active catalog modes in connection probes', () => {
+ const registry: ModelRegistry = { schemaVersion: 5, updatedAt: '2026-10-12T06:00:00.000Z',
+ baseModels: [{ id: 'new-server-voice', aliases: [], defaults: { mode: 'audio_speech' } }], models: [],
+ };
+ function project(models: ProviderRuntimeModelConfig[]) {
+ return buildUserProvider({ id: 'speech', name: 'Speech', runtimes: { codex: {
+ baseUrl: fields.baseUrl.trim(), models,
+ } } }, { modelRegistry: registry }).models.codex!;
+ }
+
+ it('uses the effective mode after restoring an ID-only saved config without materializing it', () => {
+ const catalogModels = project([{ id: 'new-server-voice', name: 'Voice' }]);
+ const models = catalogModels.map(model => customProviderModelConfigFromCatalogModel(model, 'codex'));
+ expect(catalogModels[0].mode).toBe('audio_speech');
+ expect(models[0].mode).toBeUndefined();
+ expect(firstProviderChatModel(models, catalogModels)).toBeUndefined();
+ expect(firstProviderChatModel([{ ...models[0], mode: 'chat' }], catalogModels)?.id).toBe(models[0].id);
+ expect(firstProviderChatModel([{ ...models[0], discoveredMetadata: { mode: 'chat' } }], catalogModels)?.id).toBe(models[0].id);
+ expect(models).toEqual([{ id: 'new-server-voice', name: 'Voice' }]);
+ });
+
+ it('does not restore a saved explicit type after the user resets it to automatic', () => {
+ const catalogModels = project([{ id: 'private-model', name: 'Private', mode: 'audio_speech' }]);
+ const automatic = { id: 'private-model', name: 'Private' };
+ expect(firstProviderChatModel([automatic], catalogModels)).toBe(automatic);
+ });
+
+ it('uses the same active classification for route, saved-probe eligibility and request signatures', () => {
+ const models: ProviderRuntimeModelConfig[] = [
+ { id: 'new-server-voice', name: 'Voice', route: { baseUrl: 'https://speech.example.test/v1', wireProtocol: 'openai-chat' } },
+ { id: 'chat-model', name: 'Chat', route: { baseUrl: 'https://chat.example.test/v1', wireProtocol: 'openai-chat' } },
+ ];
+ const catalogModels = project(models);
+ const probe = { ...fields, wireProtocol: 'openai-responses' as const, models, catalogModels };
+ expect(firstProviderChatModel(models, catalogModels)?.id).toBe('chat-model');
+ expect(resolveProviderConnectionProbeRoute('codex', probe)).toMatchObject({
+ baseUrl: 'https://chat.example.test/v1', wireProtocol: 'openai-chat',
+ });
+ expect(connectionTestCanUseSaved(probe, { ...fields, wireProtocol: 'openai-responses',
+ authMode: 'apiKey', modelRoute: models[1].route }, 'apiKey')).toBe(true);
+ expect(JSON.parse(providerConnectionTestRequestSignature(probe, 'apiKey')).modelId).toBe('chat-model');
+ expect(providerConnectionTestRequestSignature(probe, 'apiKey')).not.toBe(
+ providerConnectionTestRequestSignature({ ...probe, catalogModels: [] }, 'apiKey'),
+ );
+ });
+});
+
describe('providerModelFetchRequestSignature', () => {
it('invalidates an in-flight result when the auth mode changes', () => {
expect(providerModelFetchRequestSignature(fields, 'apiKey')).not.toBe(
diff --git a/apps/desktop/src/renderer/lib/providerModelFetch.ts b/apps/desktop/src/renderer/lib/providerModelFetch.ts
index 6fba60b01b..aba9c5654b 100644
--- a/apps/desktop/src/renderer/lib/providerModelFetch.ts
+++ b/apps/desktop/src/renderer/lib/providerModelFetch.ts
@@ -8,6 +8,7 @@ import {
resolvePiModelRoute,
providerWireProtocolForApi,
type AgentKind,
+ type CatalogModel,
type PiModelApi,
type ProviderModelRouteConfig,
type ProviderWireProtocol,
@@ -27,11 +28,18 @@ export interface ProviderConnectionTestSignatureFields extends ProviderModelFetc
catalogPresetId?: string;
wireProtocol: ProviderWireProtocol;
models: ReadonlyArray<{ id: string; mode?: string; discoveredMetadata?: { mode?: string }; api?: PiModelApi; piApi?: PiModelApi; route?: ProviderModelRouteConfig }>;
+ /** Current provider projection, used only for probing; never persisted as user overrides. */
+ catalogModels?: ReadonlyArray<Pick<CatalogModel, 'id' | 'mode' | 'userModelConfig'>>;
}
-export function firstProviderChatModel<T extends { id: string; mode?: string; discoveredMetadata?: { mode?: string } }>(models: readonly T[]): T | undefined {
+export function firstProviderChatModel<T extends { id: string; mode?: string; discoveredMetadata?: { mode?: string } }>(
+ models: readonly T[],
+ catalogModels: ProviderConnectionTestSignatureFields['catalogModels'] = [],
+): T | undefined {
return models.find((model) => model.id.trim().length > 0 && isAgentSelectableModel(
{ id: model.id, group: 'custom', mode: model.mode ?? model.discoveredMetadata?.mode ??
+ // Restoring automatic detection must not reuse a saved explicit mode.
+ catalogModels.find(current => current.id === model.id.trim() && current.userModelConfig?.mode === undefined)?.mode ??
findBaseModel(BUNDLED_CATALOG.modelRegistry, model.id.trim())?.defaults.mode },
{ userProvider: true },
));
@@ -51,7 +59,7 @@ export function resolveProviderConnectionProbeRoute(
agent: ProviderProbeAgent,
fields: Pick<
ProviderConnectionTestSignatureFields,
- 'baseUrl' | 'requestPath' | 'wireProtocol' | 'models' | 'catalogPresetId'
+ 'baseUrl' | 'requestPath' | 'wireProtocol' | 'models' | 'catalogPresetId' | 'catalogModels'
>,
presets: readonly ProviderPreset[] = [],
): ProviderConnectionProbeRoute | null {
@@ -62,7 +70,7 @@ export function resolveProviderConnectionProbeRoute(
models: fields.models.map(model => ({ ...model, name: model.id })),
} },
}, { presets }).models[agent] : undefined;
- const firstModel = firstProviderChatModel(projectedModels ?? fields.models);
+ const firstModel = firstProviderChatModel(projectedModels ?? fields.models, fields.catalogModels);
const api = firstModel?.api ?? firstModel?.piApi;
if (api && ['google-generative-ai', 'google-vertex', 'azure-openai-responses', 'bedrock-converse-stream', 'mistral-conversations'].includes(api)) {
return { api, baseUrl: firstModel?.route?.baseUrl ?? fields.baseUrl,
@@ -267,7 +275,7 @@ export function connectionTestCanUseSaved(
if (form.requestPath.trim() !== baseline.requestPath.trim()) return false;
if (form.wireProtocol !== baseline.wireProtocol) return false;
if ((form.catalogPresetId ?? null) !== (baseline.catalogPresetId ?? null)) return false;
- const firstModel = firstProviderChatModel(form.models);
+ const firstModel = firstProviderChatModel(form.models, form.catalogModels);
if ((firstModel?.piApi ?? null) !== (baseline.modelPiApi ?? null)) return false;
if ((firstModel?.api ?? null) !== (baseline.modelApi ?? null)) return false;
if (
@@ -284,15 +292,14 @@ export function providerConnectionTestRequestSignature(
fields: ProviderConnectionTestSignatureFields,
authMode: CustomProviderAuthMode,
): string {
+ const firstModel = firstProviderChatModel(fields.models, fields.catalogModels);
return JSON.stringify({
request: providerModelFetchRequestSignature(fields, authMode),
wireProtocol: fields.wireProtocol,
- modelId: firstProviderChatModel(fields.models)?.id.trim() ?? null,
- modelPiApi: firstProviderChatModel(fields.models)?.piApi ?? null,
- modelApi: firstProviderChatModel(fields.models)?.api ?? null,
+ modelId: firstModel?.id.trim() ?? null,
+ modelPiApi: firstModel?.piApi ?? null,
+ modelApi: firstModel?.api ?? null,
catalogPresetId: fields.catalogPresetId ?? null,
- modelRoute: normalizedModelRoute(
- firstProviderChatModel(fields.models)?.route,
- ),
+ modelRoute: normalizedModelRoute(firstModel?.route),
});
}
diff --git a/packages/model-providers/src/__tests__/speechModelDiscovery.test.ts b/packages/model-providers/src/__tests__/speechModelDiscovery.test.ts
index c319847d75..ad795ded87 100644
--- a/packages/model-providers/src/__tests__/speechModelDiscovery.test.ts
+++ b/packages/model-providers/src/__tests__/speechModelDiscovery.test.ts
@@ -4,6 +4,7 @@ import { isAgentSelectableModel } from '../classification.js';
import { parseModelsListResponse } from '../modelDiscovery.js';
import { mergeDiscoveredRuntimeModels, pickModelMetadata, validModelMetadata } from '../modelMetadataLayers.js';
import { resolveCatalogModelNativeApi, resolveModelNativeApi } from '../modelRegistry.js';
+import { projectProviderMediaModels } from '../providerMediaModels.js';
import { buildUserProvider } from '../user-provider.js';
import type { ModelRegistry } from '../modelAccessBean.js';
import type { ProviderRuntimeModelConfig } from '../types.js';
@@ -16,6 +17,34 @@ function provider(models: ProviderRuntimeModelConfig[], id = 'speech-account') {
}
describe('BYOK speech discovery and projection', () => {
+ it.each([
+ [null, undefined, null],
+ ['openai-completions', undefined, 'openai-completions'],
+ [undefined, undefined, 'openai-audio-speech'],
+ [null, 'openai-audio-speech', 'openai-audio-speech'],
+ ['openai-audio-speech', null, null],
+ ] as const)('preserves media protocol precedence through projection: declaration %s, user %s',
+ (declaration, override, expected) => {
+ for (const providerId of ['speech-account', 'xd']) {
+ const activeRegistry: ModelRegistry = {
+ schemaVersion: 5, updatedAt: '2026-10-12T06:00:00.000Z',
+ baseModels: [{ id: 'new-voice', aliases: [], defaults: { mode: 'audio_speech' } }],
+ models: [{ id: 'new-voice', name: 'Voice', modelRef: 'new-voice', nativeApi: declaration,
+ routes: [{ providerId, modelId: 'new-voice', agents: [] }] }],
+ };
+ const model: ProviderRuntimeModelConfig = { id: 'new-voice', name: 'Voice',
+ discoveredMetadata: { mode: 'audio_speech', nativeApi: 'openai-audio-speech' },
+ ...(override !== undefined ? { nativeApi: override } : {}),
+ };
+ const p = buildUserProvider({ id: 'speech-account', name: 'Speech', runtimes: {
+ codex: { baseUrl: 'https://speech.example.test/v1', wireProtocol: 'openai-chat', models: [model] },
+ } }, { modelRegistry: activeRegistry });
+ expect(p.models.codex![0].nativeApi).toBe(expected);
+ expect(projectProviderMediaModels(p, activeRegistry).audioModels?.[0].nativeApi).toBe(expected);
+ expect(p.models.codex![0].userModelConfig).toEqual(model);
+ }
+ });
+
it.each(['nativeApi', 'native_api'])('retains explicit speech metadata from %s through refresh and catalog parsing', key => {
const discovered = parseModelsListResponse({ data: [{ id: 'custom-voice',
name: 'Custom Voice', mode: 'audio_speech', [key]: 'openai-audio-speech',
diff --git a/packages/model-providers/src/providerMediaModels.ts b/packages/model-providers/src/providerMediaModels.ts
index 40ac28d376..0c312ccee8 100644
--- a/packages/model-providers/src/providerMediaModels.ts
+++ b/packages/model-providers/src/providerMediaModels.ts
@@ -99,17 +99,23 @@ export function projectProviderMediaModels(
),
)
.map((model): ProviderMediaModel => {
+ const userMetadata = options.userMetadata?.(model.id, model);
const metadata = resolveModelMetadata(
registry,
catalogId,
catalogModelId(model.id),
options.live ? pickModelMetadata(model) : model.discoveredMetadata,
- options.userMetadata?.(model.id, model),
+ userMetadata,
);
const declaredMatch = declared.find(
({ route }) => route.modelId === catalogModelId(model.id),
);
- const nativeApi = declaredMatch?.entry.nativeApi;
+ // Custom runtime media already has declaration/discovery precedence resolved
+ // by buildUserProvider. Reapplying discovery here would undo that decision.
+ const customNativeApi = provider.source === "user" && model.sourceAgent
+ ? userMetadata?.nativeApi !== undefined ? userMetadata.nativeApi : model.nativeApi
+ : undefined;
+ const nativeApi = customNativeApi !== undefined ? customNativeApi : declaredMatch?.entry.nativeApi;
const defaultEnabled = declaredMatch?.entry.defaultEnabled;
// Preserve the source's real ID, payment state and disable flag.
return {
diff --git a/packages/model-providers/src/user-provider.ts b/packages/model-providers/src/user-provider.ts
index 6940c49136..ffa8b56301 100644
--- a/packages/model-providers/src/user-provider.ts
+++ b/packages/model-providers/src/user-provider.ts
@@ -633,10 +633,10 @@ export function buildUserProvider(
const declaration = currentDeclaration !== undefined ? currentDeclaration
: projected.nativeApi !== undefined ? projected.nativeApi
: resolveDeclaration(BUNDLED_CATALOG.modelRegistry);
- // Media declarations are metadata too; dropping them here loses the connection's
- // speech protocol before audioModels is projected. This does not enable execution.
- const nativeApi = providerMediaField(projected.mode) && projected.nativeApi !== undefined
- ? projected.nativeApi : declaration;
+ // Only an explicit user value may override the current media declaration.
+ // projected also contains discovery metadata, including stale protocol claims.
+ const nativeApi = providerMediaField(projected.mode) && storedModel.nativeApi !== undefined
+ ? storedModel.nativeApi : declaration;
return {
...(imported?.cost ? { cost: imported.cost } : {}),
...projected,