mirror of
https://github.com/makecindy/cindy.git
synced 2026-12-02 14:40:44 +08:00
Merge pull request #9571 from Br1efx/fix/provider-probe-model-selection
fix(provider): 测试连接优先探测用户可见的聊天模型
This commit is contained in:
@@ -29,10 +29,16 @@ import {
|
||||
} from '../provider-diagnostics.js';
|
||||
import { probeProviderProtocols } from '../provider-protocol-probe.js';
|
||||
import { setCustomProviders } from '../active-catalog.js';
|
||||
import {
|
||||
__resetModelVisibilityMirrorForTest,
|
||||
clearModelVisibilityMirror,
|
||||
setModelVisibilityMirror,
|
||||
} from '../model-visibility-mirror.js';
|
||||
|
||||
afterEach(() => {
|
||||
setCustomProviders([]);
|
||||
setDiagnosticsKeyReader(() => null);
|
||||
__resetModelVisibilityMirrorForTest();
|
||||
probeLog.warn.mockClear();
|
||||
});
|
||||
|
||||
@@ -796,6 +802,171 @@ describe('resolveSavedProbeSpec / testProviderConnection(saved)', () => {
|
||||
expect(resolveSavedProbeSpec('my-relay', 'claude-code').modelId).toBe('glm-5.2');
|
||||
});
|
||||
|
||||
it('用户明确开启的不是列表第一个聊天模型时,探测用该可见模型(issue #9470)', () => {
|
||||
const provider = buildUserProvider(config);
|
||||
setCustomProviders([
|
||||
{
|
||||
...provider,
|
||||
models: {
|
||||
...provider.models,
|
||||
'claude-code': [
|
||||
{
|
||||
id: 'glm-5.2',
|
||||
name: 'GLM',
|
||||
contextWindow: 128_000,
|
||||
efforts: [],
|
||||
defaultEffort: null,
|
||||
},
|
||||
{
|
||||
id: 'glm-4.5',
|
||||
name: 'GLM 4.5',
|
||||
contextWindow: 128_000,
|
||||
efforts: [],
|
||||
defaultEffort: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
]);
|
||||
setModelVisibilityMirror({ 'claude-code:my-relay:glm-4.5': true });
|
||||
setDiagnosticsKeyReader(() => 'sk-saved');
|
||||
expect(resolveSavedProbeSpec('my-relay', 'claude-code').modelId).toBe('glm-4.5');
|
||||
});
|
||||
|
||||
// 测试价值:生产入口 resolveSavedProbeSpec,可观察结果是探测请求用的 modelId。
|
||||
// Bug #9470 / PR #9571 P1:镜像只存显式开关,关掉写 false、没拨过无条目。
|
||||
// 原兜底只认 === true,会把显式隐藏的第一个坏模型当成探测目标。
|
||||
// 最小失败输入:候选 [glm-5.2, glm-4.5],只把 glm-5.2 标成 false。tier: unit。
|
||||
it('显式隐藏第一个聊天模型、第二个未拨过时,探测跳过隐藏项选第二个', () => {
|
||||
const provider = buildUserProvider(config);
|
||||
setCustomProviders([
|
||||
{
|
||||
...provider,
|
||||
models: {
|
||||
...provider.models,
|
||||
'claude-code': [
|
||||
{
|
||||
id: 'glm-5.2',
|
||||
name: 'GLM',
|
||||
contextWindow: 128_000,
|
||||
efforts: [],
|
||||
defaultEffort: null,
|
||||
},
|
||||
{
|
||||
id: 'glm-4.5',
|
||||
name: 'GLM 4.5',
|
||||
contextWindow: 128_000,
|
||||
efforts: [],
|
||||
defaultEffort: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
]);
|
||||
setModelVisibilityMirror({ 'claude-code:my-relay:glm-5.2': false });
|
||||
setDiagnosticsKeyReader(() => 'sk-saved');
|
||||
expect(resolveSavedProbeSpec('my-relay', 'claude-code').modelId).toBe('glm-4.5');
|
||||
});
|
||||
|
||||
// 全部显式隐藏时仍要能测连接:退回第一个可选模型,不要因为没有可见模型而抛空。
|
||||
it('全部聊天模型都被显式隐藏时,探测仍退回第一个可选模型', () => {
|
||||
const provider = buildUserProvider(config);
|
||||
setCustomProviders([
|
||||
{
|
||||
...provider,
|
||||
models: {
|
||||
...provider.models,
|
||||
'claude-code': [
|
||||
{
|
||||
id: 'glm-5.2',
|
||||
name: 'GLM',
|
||||
contextWindow: 128_000,
|
||||
efforts: [],
|
||||
defaultEffort: null,
|
||||
},
|
||||
{
|
||||
id: 'glm-4.5',
|
||||
name: 'GLM 4.5',
|
||||
contextWindow: 128_000,
|
||||
efforts: [],
|
||||
defaultEffort: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
]);
|
||||
setModelVisibilityMirror({
|
||||
'claude-code:my-relay:glm-5.2': false,
|
||||
'claude-code:my-relay:glm-4.5': false,
|
||||
});
|
||||
setDiagnosticsKeyReader(() => 'sk-saved');
|
||||
expect(resolveSavedProbeSpec('my-relay', 'claude-code').modelId).toBe('glm-5.2');
|
||||
});
|
||||
|
||||
// 镜像未就绪时 getModelVisibilityOverride 抛 MODEL_VISIBILITY_NOT_READY。
|
||||
// 探测不能因此失败,必须走原来的第一个可选模型。
|
||||
it('可见性镜像未就绪时走兜底且不抛错', () => {
|
||||
const provider = buildUserProvider(config);
|
||||
setCustomProviders([
|
||||
{
|
||||
...provider,
|
||||
models: {
|
||||
...provider.models,
|
||||
'claude-code': [
|
||||
{
|
||||
id: 'glm-5.2',
|
||||
name: 'GLM',
|
||||
contextWindow: 128_000,
|
||||
efforts: [],
|
||||
defaultEffort: null,
|
||||
},
|
||||
{
|
||||
id: 'glm-4.5',
|
||||
name: 'GLM 4.5',
|
||||
contextWindow: 128_000,
|
||||
efforts: [],
|
||||
defaultEffort: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
]);
|
||||
clearModelVisibilityMirror();
|
||||
setDiagnosticsKeyReader(() => 'sk-saved');
|
||||
expect(resolveSavedProbeSpec('my-relay', 'claude-code').modelId).toBe('glm-5.2');
|
||||
});
|
||||
|
||||
it('没有显式可见偏好时,探测仍退回第一个聊天模型', () => {
|
||||
const provider = buildUserProvider(config);
|
||||
setCustomProviders([
|
||||
{
|
||||
...provider,
|
||||
models: {
|
||||
...provider.models,
|
||||
'claude-code': [
|
||||
{
|
||||
id: 'glm-5.2',
|
||||
name: 'GLM',
|
||||
contextWindow: 128_000,
|
||||
efforts: [],
|
||||
defaultEffort: null,
|
||||
},
|
||||
{
|
||||
id: 'glm-4.5',
|
||||
name: 'GLM 4.5',
|
||||
contextWindow: 128_000,
|
||||
efforts: [],
|
||||
defaultEffort: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
]);
|
||||
setModelVisibilityMirror({});
|
||||
setDiagnosticsKeyReader(() => 'sk-saved');
|
||||
expect(resolveSavedProbeSpec('my-relay', 'claude-code').modelId).toBe('glm-5.2');
|
||||
});
|
||||
|
||||
it('全是非聊天模型时抛「无聊天模型」错误,而不是静默探测一个 embedding 模型', () => {
|
||||
const provider = buildUserProvider(config);
|
||||
setCustomProviders([
|
||||
|
||||
@@ -34,6 +34,7 @@ import {
|
||||
type ProviderErrorCode,
|
||||
} from '../../shared/providerErrors.js';
|
||||
import { getActiveCatalog } from './active-catalog.js';
|
||||
import { getModelVisibilityOverride } from './model-visibility-mirror.js';
|
||||
import { withOpenCodeGoSessionHeader } from './opencode-go-session.js';
|
||||
import { withProviderAppAttribution } from './provider-app-attribution.js';
|
||||
import { createMakerLogger } from './logger-adapter.js';
|
||||
@@ -462,9 +463,27 @@ export function resolveSavedProbeSpec(providerId: string, agent: AgentKind): Pro
|
||||
// 探测发的是聊天形状的最小请求(见下方 requestPath/body 组装);挑第一个非聊天模型
|
||||
// (image/embedding/...)会把探测发给一个本来就不接受聊天请求的端点,得到的失败结论
|
||||
// 和"这个供应商配置是坏的"完全无关(2026-07 review,与 issue #882 第 3 点同一类问题)。
|
||||
const model = (provider.models[agent] ?? []).find((m) =>
|
||||
isAgentSelectableModel(m, { userProvider: provider.source === 'user' }),
|
||||
);
|
||||
// 列表第一个聊天模型若与上游协议不兼容,也会把整个供应商误报为连不上(issue #9470)。
|
||||
// 用户明确打开的模型优先。镜像只存显式开关:true 是打开,false 是关掉,没拨过则无条目。
|
||||
// 兜底先排除显式隐藏,再按原顺序取第一个可选模型;全部显式隐藏时仍退回第一个可选模型,
|
||||
// 避免测试连接因「没有可见模型」变成空失败。镜像未就绪或读取失败时忽略可见性,走原兜底。
|
||||
const candidates = provider.models[agent] ?? [];
|
||||
const selectable = (m: (typeof candidates)[number]): boolean =>
|
||||
isAgentSelectableModel(m, { userProvider: provider.source === 'user' });
|
||||
let explicitlyShown: (typeof candidates)[number] | undefined;
|
||||
let notHidden: (typeof candidates)[number] | undefined;
|
||||
try {
|
||||
for (const candidate of candidates) {
|
||||
if (!selectable(candidate)) continue;
|
||||
const override = getModelVisibilityOverride(agent, providerId, candidate.id);
|
||||
if (override === true && !explicitlyShown) explicitlyShown = candidate;
|
||||
if (override !== false && !notHidden) notHidden = candidate;
|
||||
}
|
||||
} catch {
|
||||
explicitlyShown = undefined;
|
||||
notHidden = undefined;
|
||||
}
|
||||
const model = explicitlyShown ?? notHidden ?? candidates.find(selectable);
|
||||
if (!model) throw new Error(`provider '${providerId}' has no chat models for '${agent}'`);
|
||||
// Pi's HTTP-only route helper intentionally excludes SDK transports. A native
|
||||
// probe must keep the SDK route instead of treating that exclusion as failure.
|
||||
|
||||
Reference in New Issue
Block a user