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): 避免远端目录遮蔽内置预设 (#1194)

Dashb1c19aca5aea
diff --git a/packages/model-providers/catalog/providers.json b/packages/model-providers/catalog/providers.json
index f4205c2a83..4d9aa09f23 100644
--- a/packages/model-providers/catalog/providers.json
+++ b/packages/model-providers/catalog/providers.json
@@ -482,7 +482,8 @@
"models": [
{
"id": "MiniMax-M3",
- "name": "MiniMax M3"
+ "name": "MiniMax M3",
+ "contextWindow": 1000000
},
{
"id": "MiniMax-M2.5",
@@ -517,7 +518,8 @@
"models": [
{
"id": "MiniMax-M3",
- "name": "MiniMax M3"
+ "name": "MiniMax M3",
+ "contextWindow": 1000000
},
{
"id": "MiniMax-M2.5",
diff --git a/packages/model-providers/src/__tests__/presets.test.ts b/packages/model-providers/src/__tests__/presets.test.ts
index 65692762aa..a8ef2b007a 100644
--- a/packages/model-providers/src/__tests__/presets.test.ts
+++ b/packages/model-providers/src/__tests__/presets.test.ts
@@ -3,7 +3,7 @@
*
* 关键不变量:
* - presets 是纯 UI 模板数据,坏条目**逐条丢弃**,绝不让整份目录 parse 失败回退 bundled;
- * - mergeWithBundled:远端带 presets 用远端的,远端没带回落 bundled 的;
+ * - mergeWithBundled:远端与 bundled 按 id 合并,同 id 远端优先、bundled 补缺;
* - BUNDLED_CATALOG 自带的首批预设本身合法(每条至少一个 runtime、字段完整)。
*/
@@ -147,9 +147,15 @@ describe('parseCatalog presets 容错', () => {
});
describe('mergeWithBundled presets 兜底', () => {
- it('远端带 presets → 用远端的', () => {
+ it('远端 presets 与 bundled 按 id 合并:远端同 id 优先,bundled 缺项不丢', () => {
const merged = mergeWithBundled(minimalCatalog({ presets: [VALID_PRESET] }));
- expect(merged.presets?.map((p) => p.id)).toEqual(['openrouter']);
+ expect(merged.presets?.find((preset) => preset.id === 'openrouter')).toEqual(VALID_PRESET);
+ expect(merged.presets?.map((preset) => preset.id)).toEqual(
+ BUNDLED_CATALOG.presets?.map((preset) => preset.id),
+ );
+ expect(merged.presets?.map((preset) => preset.id)).toEqual(
+ expect.arrayContaining(['zhipu-coding-plan-cn', 'zai-coding-plan-global']),
+ );
});
it('远端没带 presets → 回落 bundled 的', () => {
@@ -157,6 +163,58 @@ describe('mergeWithBundled presets 兜底', () => {
expect(merged.presets).toEqual(BUNDLED_CATALOG.presets);
expect(merged.presets?.length ?? 0).toBeGreaterThan(0);
});
+
+ it('远端独有 preset 按远端原序追加在 bundled 之后', () => {
+ const remoteOnly = { ...VALID_PRESET, id: 'remote-only' };
+ const merged = mergeWithBundled(minimalCatalog({ presets: [remoteOnly] }));
+ expect(merged.presets?.at(-1)).toEqual(remoteOnly);
+ });
+
+ it('同 id 远端保留 runtime/model 时回填 bundled contextWindow,不复活被移除的 runtime/model', () => {
+ const remoteMiniMax = {
+ id: 'minimax-global',
+ name: 'Remote MiniMax',
+ runtimes: {
+ 'claude-code': {
+ baseUrl: 'https://remote.example/anthropic',
+ models: [{ id: 'MiniMax-M3', name: 'Remote M3' }],
+ },
+ },
+ };
+ const merged = mergeWithBundled(minimalCatalog({ presets: [remoteMiniMax] }));
+ const preset = merged.presets?.find((candidate) => candidate.id === 'minimax-global');
+ expect(preset).toEqual({
+ ...remoteMiniMax,
+ runtimes: {
+ 'claude-code': {
+ ...remoteMiniMax.runtimes['claude-code'],
+ models: [{ id: 'MiniMax-M3', name: 'Remote M3', contextWindow: 1_000_000 }],
+ },
+ },
+ });
+ expect(preset?.runtimes.codex).toBeUndefined();
+ });
+
+ it('远端显式 contextWindow 优先于 bundled', () => {
+ const remoteMiniMax = {
+ id: 'minimax-global',
+ name: 'Remote MiniMax',
+ runtimes: {
+ 'claude-code': {
+ baseUrl: 'https://remote.example/anthropic',
+ models: [{ id: 'MiniMax-M3', name: 'Remote M3', contextWindow: 512_000 }],
+ },
+ },
+ };
+ const merged = mergeWithBundled(minimalCatalog({ presets: [remoteMiniMax] }));
+ expect(
+ merged.presets
+ ?.find((candidate) => candidate.id === 'minimax-global')
+ ?.runtimes['claude-code']
+ ?.models[0]
+ ?.contextWindow,
+ ).toBe(512_000);
+ });
});
describe('presetDisplayName', () => {
@@ -375,6 +433,10 @@ describe('MiniMax OpenAI Responses 预设契约 (issue #345)', () => {
const preset = BUNDLED_CATALOG.presets?.find((candidate) => candidate.id === id);
expect(preset?.docsUrl).toBe(docsUrl);
expect(preset?.runtimes['claude-code']?.baseUrl).toMatch(/\/anthropic$/);
+ expect(preset?.runtimes['claude-code']?.models).toEqual([
+ { id: 'MiniMax-M3', name: 'MiniMax M3', contextWindow: 1_000_000 },
+ { id: 'MiniMax-M2.5', name: 'MiniMax M2.5' },
+ ]);
expect(preset?.runtimes.codex).toEqual({
baseUrl: codexBaseUrl,
models: [
@@ -426,6 +488,19 @@ describe('官方渠道预设契约', () => {
}));
});
+ it.each(['zhipu-coding-plan-cn', 'zai-coding-plan-global'])(
+ '%s 的 Claude Code GLM-5.2 1M 入口保留完整窗口元数据',
+ (id) => {
+ expect(
+ preset(id)?.runtimes['claude-code']?.models.find((model) => model.id === 'glm-5.2[1m]'),
+ ).toEqual({
+ id: 'glm-5.2[1m]',
+ name: 'GLM-5.2 (1M)',
+ contextWindow: 1_000_000,
+ });
+ },
+ );
+
it('小米按量与 Token Plan 凭证不会混用端点', () => {
expect(preset('xiaomi-mimo-api-cn')?.runtimes.codex?.baseUrl)
.toBe('https://api.xiaomimimo.com/v1');
diff --git a/packages/model-providers/src/source.ts b/packages/model-providers/src/source.ts
index b99bd65633..a19008c6b2 100644
--- a/packages/model-providers/src/source.ts
+++ b/packages/model-providers/src/source.ts
@@ -14,7 +14,7 @@
*/
import { BUNDLED_CATALOG, parseCatalog } from './catalog.js';
-import type { Catalog, Provider } from './types.js';
+import type { AgentKind, Catalog, Provider, ProviderPreset } from './types.js';
/** 公共模型目录 API 路径。发布版由 model-access-server 匿名提供完整 Catalog。 */
export const CATALOG_API_PATH = '/api/model-catalog/catalog';
@@ -97,6 +97,40 @@ function legacyAccessFor(primary: Provider, bundled: Provider): Provider['access
return sameRoutes ? bundled.access : undefined;
}
+/**
+ * 同 id preset 仍以远端为主;bundled 只给远端仍保留的同 runtime / 同 model
+ * 回填缺失的 contextWindow。这样旧远端不会把已核实的长上下文元数据降级,同时远端
+ * 仍可通过移除 runtime / model 停止新建,或用显式窗口覆盖 bundled。
+ */
+function backfillPresetContextWindows(
+ primary: ProviderPreset,
+ bundled: ProviderPreset,
+): ProviderPreset {
+ let changed = false;
+ const runtimes: ProviderPreset['runtimes'] = {};
+ for (const [agent, runtime] of Object.entries(primary.runtimes) as [
+ AgentKind,
+ NonNullable<ProviderPreset['runtimes'][AgentKind]>,
+ ][]) {
+ const bundledRuntime = bundled.runtimes[agent];
+ if (!bundledRuntime) {
+ runtimes[agent] = runtime;
+ continue;
+ }
+ const bundledModels = new Map(bundledRuntime.models.map((model) => [model.id, model]));
+ let runtimeChanged = false;
+ const models = runtime.models.map((model) => {
+ const bundledContextWindow = bundledModels.get(model.id)?.contextWindow;
+ if (model.contextWindow !== undefined || bundledContextWindow === undefined) return model;
+ runtimeChanged = true;
+ changed = true;
+ return { ...model, contextWindow: bundledContextWindow };
+ });
+ runtimes[agent] = runtimeChanged ? { ...runtime, models } : runtime;
+ }
+ return changed ? { ...primary, runtimes } : primary;
+}
+
/**
* 把远端 / 本地目录与内置 bundled 合并:以输入目录为主,bundled 补它缺失的
* provider(按 id),并给旧目录中同 id provider 补缺失的 access 元数据。primary
@@ -122,9 +156,20 @@ export function mergeWithBundled(primary: Catalog): Catalog {
for (const p of withAccess) {
if (!bundledById.has(p.id)) merged.push(p);
}
- // presets 同理兜底:远端带了用远端的,远端没带回落 bundled 的(预设是纯 UI 模板数据)。
- const presets = primary.presets ?? BUNDLED_CATALOG.presets;
- // cindyModelMeta 同 presets 兜底语义透传(消费点见 types.ts:服务端 + 客户端展示元数据基线)。
+ // presets 与 providers 同样按 id 合并:bundled 保序兜底,同 id 远端内容优先,
+ // 远端独有项按远端原序追加。避免旧远端的非空 presets 整段遮掉新版客户端内置条目。
+ const primaryPresets = primary.presets ?? [];
+ const bundledPresets = BUNDLED_CATALOG.presets ?? [];
+ const primaryPresetsById = new Map(primaryPresets.map((preset) => [preset.id, preset]));
+ const bundledPresetIds = new Set(bundledPresets.map((preset) => preset.id));
+ const presets = bundledPresets.map((bundled) => {
+ const remote = primaryPresetsById.get(bundled.id);
+ return remote ? backfillPresetContextWindows(remote, bundled) : bundled;
+ });
+ for (const preset of primaryPresets) {
+ if (!bundledPresetIds.has(preset.id)) presets.push(preset);
+ }
+ // cindyModelMeta 仍按整段缺省兜底透传(消费点见 types.ts:服务端 + 客户端展示元数据基线)。
const cindyModelMeta = primary.cindyModelMeta ?? BUNDLED_CATALOG.cindyModelMeta;
return {
version: primary.version,