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。

feat(model-default): 存下来的 Auto 路由在使用时跟随服务端挪动

fmfsaisai954387dfd7bd
diff --git a/apps/desktop/src/main/im/__tests__/defaultSessionSettings.test.ts b/apps/desktop/src/main/im/__tests__/defaultSessionSettings.test.ts
index 4d57a9ae1f..f0310f9b36 100644
--- a/apps/desktop/src/main/im/__tests__/defaultSessionSettings.test.ts
+++ b/apps/desktop/src/main/im/__tests__/defaultSessionSettings.test.ts
@@ -237,6 +237,33 @@ describe('resolveImSessionDefaults', () => {
});
});
+ it.each([
+ ['号池 → Cindy AI(显式来源)', 'org-pool', 'xd', ['auto', 'auto/group-1'], ['pool-model']],
+ ['Cindy AI → 号池(Pi 默认来源 null 即 Cindy AI)', null, 'org-pool', ['auto'], ['pool-model', 'auto/group-1']],
+ ] as const)('保存的 Auto 默认在使用时跟随服务端挪动:%s', async (_name, saved, expected, xdModels, poolModels) => {
+ const piModel = (id: string) => ({ id, name: id, contextWindow: 200_000, efforts: [], defaultEffort: null });
+ const piProvider = (id: string, source: string, models: readonly string[]) => ({
+ id, name: id, source, connected: true, agents: ['pi'],
+ auth: { method: 'managed' }, access: { kind: 'managed' },
+ models: { pi: models.map(piModel) },
+ routing: { pi: { upstream: 'https://example.invalid', authStrategy: 'api-key-header' } },
+ });
+ mocks.listProviders.mockResolvedValue([
+ piProvider('xd', 'builtin', xdModels),
+ piProvider('org-pool', 'organization', poolModels),
+ ]);
+ mocks.readImDefaultSettings.mockReturnValue({
+ agentKind: 'pi',
+ agents: { pi: { providerId: saved, model: 'auto/group-1', effort: 'high' } },
+ });
+
+ await expect(resolveImSessionDefaults(config)).resolves.toMatchObject({
+ agentKind: 'pi',
+ model: 'auto/group-1',
+ providerId: expected,
+ });
+ });
+
it('reroutes an implicit default whose native-default copy is disabled', async () => {
// providerId 留空(隐式路由):原生默认落点(codex → openai)的拷贝被停用而 xd
// 有启用拷贝时必须显式改道 —— turnRunner 直建不过路由守卫(第十六轮)。
diff --git a/apps/desktop/src/main/im/defaultSessionSettings.ts b/apps/desktop/src/main/im/defaultSessionSettings.ts
index 61c261aebf..b6a00b8d4a 100644
--- a/apps/desktop/src/main/im/defaultSessionSettings.ts
+++ b/apps/desktop/src/main/im/defaultSessionSettings.ts
@@ -13,6 +13,7 @@ import {
effectiveSourceIdForModel,
getModel,
isModelSelectableForNewRoute,
+ movedCindyAutoSourceId,
type ProviderView,
} from '@cindy/model-providers';
@@ -289,6 +290,12 @@ function resolveProviderId(
modelId: string,
providerId: string | null,
): string | null {
+ // 保存的 Auto 路由在使用时跟随服务端:Auto 被挪到另一家 Cindy 来源后改用新来源,
+ // 与新建任务同一规则;渠道设置里存的值不改写。
+ const movedAuto = providers
+ ? movedCindyAutoSourceId(providers, { agent: agentKind, model: modelId, providerId })
+ : null;
+ if (movedAuto) return movedAuto;
if (!providerId) {
if (!providers) return null;
// 隐式默认(未选来源)同样过裁决:原生默认落点的拷贝被停用而有启用替代时,
diff --git a/apps/desktop/src/main/im/shared/__tests__/channelDefaultRouteSync.test.ts b/apps/desktop/src/main/im/shared/__tests__/channelDefaultRouteSync.test.ts
index 76def5f744..7029f970d1 100644
--- a/apps/desktop/src/main/im/shared/__tests__/channelDefaultRouteSync.test.ts
+++ b/apps/desktop/src/main/im/shared/__tests__/channelDefaultRouteSync.test.ts
@@ -211,6 +211,29 @@ beforeEach(() => {
});
describe('syncUnderLock', () => {
+ it('Auto 被服务端挪到另一家 Cindy 来源、任务已跟过去时,仍算在跟随渠道默认', async () => {
+ const auto = 'auto/group-1';
+ const view = (id: string, source: string, models: string[]) => ({
+ id, name: id, source, connected: true, agents: ['pi'],
+ auth: { method: 'managed' }, access: { kind: 'managed' },
+ routing: { pi: { upstream: 'https://example.invalid', authStrategy: 'api-key-header' } },
+ models: { pi: models.map((model) => ({ id: model, name: model, contextWindow: 200_000, efforts: [], defaultEffort: null })) },
+ });
+ // 管理员把 Auto 从号池挪到 Cindy AI;任务已在下一条消息前跟着换到了 xd。
+ mocks.listProviders.mockResolvedValue([view('xd', 'builtin', [auto]), view('org-pool', 'organization', ['pool-model'])]);
+ const saved: ImDefaultRoute = { agentKind: 'pi', model: auto, providerId: 'org-pool', effort: 'high' };
+ const moved: ImDefaultRoute = { ...saved, providerId: 'xd' };
+ await insertTask('t1', moved, { record: buildImDefaultRouteRecord('fp-old', saved) });
+ // 之后用户改了渠道默认的其它设置,解析出的目标仍是这个 Auto 的新位置。
+ mocks.resolveDefaults.mockResolvedValue({ ...moved, permissionMode: 'auto', fastMode: false, fingerprint: 'fp-new' });
+ await sync().syncUnderLock('t1');
+
+ expect(mocks.applyRoute).not.toHaveBeenCalled();
+ const record = parseImDefaultRouteRecord((await rowOf('t1')).imDefaultRoute);
+ expect(record?.manual).toBeFalsy();
+ expect(record).toMatchObject({ fp: 'fp-new', route: moved });
+ });
+
it('moves a following task to a share and back even when provider and model are identical', async () => {
await insertTask('t1', OLD);
const shared = { ...OLD, agentDeviceId: 'share:owner' };
diff --git a/apps/desktop/src/main/im/shared/channelDefaultRouteSync.ts b/apps/desktop/src/main/im/shared/channelDefaultRouteSync.ts
index 056961d3c2..c6280d0d68 100644
--- a/apps/desktop/src/main/im/shared/channelDefaultRouteSync.ts
+++ b/apps/desktop/src/main/im/shared/channelDefaultRouteSync.ts
@@ -8,7 +8,7 @@
*/
import { and, eq, inArray, isNull } from 'drizzle-orm';
import type { AgentKind } from '@cindy/maker-core';
-import { effectiveSourceIdForModel, type ProviderView } from '@cindy/model-providers';
+import { effectiveSourceIdForModel, movedCindyAutoSourceId, type ProviderView } from '@cindy/model-providers';
import type { ImDefaultSettingsChannel } from '../../../shared/imDefaultSettings.js';
import { getDbClient } from '../../localDb/client/current';
@@ -133,11 +133,18 @@ function currentRouteOf(row: SessionRouteRow): ImDefaultRoute {
/**
* 隐式来源按「新路由选择」口径落到具体来源 —— 系统建会话钉住 / 切换时独占改道
- * 用的就是这个口径, 与它钉出的具体 id 视为同一条路由。显式来源原样比较。
+ * 用的就是这个口径, 与它钉出的具体 id 视为同一条路由。显式来源原样比较;
+ * 唯一例外是被服务端挪到另一家 Cindy 来源的 Auto, 新旧位置视为同一条路由, 否则
+ * 任务跟着 Auto 换了来源后会被误判成用户单独改过, 从此不再跟随渠道默认。
*/
function providerNormalizer(providers: ProviderView[]) {
- return (route: ImDefaultRoute): string | null =>
- route.agentDeviceId ? route.providerId : route.providerId ?? effectiveSourceIdForModel(providers, null, route.model, route.agentKind as AgentKind);
+ return (route: ImDefaultRoute): string | null => {
+ if (route.agentDeviceId) return route.providerId;
+ const agent = route.agentKind as AgentKind;
+ return movedCindyAutoSourceId(providers, { agent, model: route.model, providerId: route.providerId })
+ ?? route.providerId
+ ?? effectiveSourceIdForModel(providers, null, route.model, agent);
+ };
}
async function listProviders(): Promise<ProviderView[] | null> {
diff --git a/apps/desktop/src/main/maker-host/__tests__/botModelChainSettingsStore.test.ts b/apps/desktop/src/main/maker-host/__tests__/botModelChainSettingsStore.test.ts
index 5cdc3108b9..f79334046f 100644
--- a/apps/desktop/src/main/maker-host/__tests__/botModelChainSettingsStore.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/botModelChainSettingsStore.test.ts
@@ -228,4 +228,38 @@ describe('bot model chain settings store', () => {
expect((await readBotModelChainSettingsState({ rootPath })).isCustomized).toBe(true);
});
+ describe('模型组 Auto 被服务端挪到另一家 Cindy 来源', () => {
+ const AUTO = 'auto/group-1';
+ const view = (id: string, source: ProviderView['source'], modelIds: string[]) => ({
+ id, name: id, source, connected: true, agents: ['pi'],
+ auth: { method: 'managed' }, access: { kind: 'managed' },
+ routing: { pi: { upstream: 'https://example.invalid', authStrategy: 'api-key-header' } },
+ models: { pi: modelIds.map((id) => ({ id, name: id, contextWindow: 200_000, efforts: [], defaultEffort: null })) },
+ }) as unknown as ProviderView;
+ // 管理员把 Auto 的目标从号池模型切到平台模型。
+ const providers = [view('xd', 'builtin', ['auto', AUTO]), view('org-pool', 'organization', ['pool-model'])];
+ const route = (providerId: string | null, model = AUTO) =>
+ ({ harness: 'pi' as const, model, providerId, effort: '', fastMode: false });
+
+ it('伙伴自己存的路由在使用时改用新来源,不改写资料里的值', async () => {
+ const rootPath = await testRoot();
+ const config = { modelChainOverride: [route('org-pool'), route('org-pool', 'pool-model')] };
+ expect(await readEffectiveBotModelChain(config, { rootPath, providers })).toEqual([
+ route('xd'), route('org-pool', 'pool-model'),
+ ]);
+ expect(config.modelChainOverride[0].providerId).toBe('org-pool');
+ });
+
+ it('用户自定义的伙伴默认同样跟随;没挪动、预设 auto 与普通模型原样', async () => {
+ const rootPath = await testRoot();
+ await writeBotModelChainSettings([route('org-pool'), route('xd', 'auto')], { rootPath });
+ expect(await readEffectiveBotModelChain({ modelChainOverride: null }, { rootPath, providers }))
+ .toEqual([route('xd'), route('xd', 'auto')]);
+ // 保存的设置文件仍是原值。
+ expect((await readBotModelChainSettingsState({ rootPath, providers: [] })).value.modelChain[0])
+ .toMatchObject({ providerId: 'org-pool' });
+ expect(await readEffectiveBotModelChain({ modelChainOverride: [route('xd')] }, { rootPath, providers }))
+ .toEqual([route('xd')]);
+ });
+ });
});
diff --git a/apps/desktop/src/main/maker-host/bot-model-chain-settings-store.ts b/apps/desktop/src/main/maker-host/bot-model-chain-settings-store.ts
index bcf074b4ee..ca21c98c07 100644
--- a/apps/desktop/src/main/maker-host/bot-model-chain-settings-store.ts
+++ b/apps/desktop/src/main/maker-host/bot-model-chain-settings-store.ts
@@ -1,6 +1,11 @@
import { app } from 'electron';
import path from 'node:path';
-import { isModelVisible, type ProviderView } from '@cindy/model-providers';
+import {
+ isCindyAutoModelId,
+ isModelVisible,
+ movedCindyAutoSourceId,
+ type ProviderView,
+} from '@cindy/model-providers';
import { defaultBotModelChain } from '../../shared/botDefaultModelChain.js';
import {
@@ -148,15 +153,53 @@ export function readExplicitBotModelChain(config: Record<string, unknown>): BotM
return null;
}
+/**
+ * 保存下来的 Auto 路由在使用时跟随服务端:Auto 被挪到另一家 Cindy 来源后改用新来源,
+ * 不改写伙伴资料或设置里存的值。目录读不到时原样返回,交给既有的路由校验。
+ */
+async function followMovedCindyAuto(
+ chain: BotModelRoute[],
+ providers: readonly ProviderView[] | undefined,
+): Promise<BotModelRoute[]> {
+ // 预设 auto 只在 Cindy AI 上、不会被挪动;只有模型组 Auto 才值得为此读一次目录。
+ if (!chain.some((route) => route.model !== 'auto' && isCindyAutoModelId(route.model))) return chain;
+ let views = providers;
+ if (!views) {
+ try {
+ views = await readConnectedProviders(activeOwnerScopeKey());
+ } catch (error) {
+ log.warn('Bot model chain Auto follow skipped: providers unavailable', {
+ error: error instanceof Error ? error.message : String(error),
+ });
+ return chain;
+ }
+ }
+ const catalog = views;
+ return chain.map((route) => {
+ const providerId = movedCindyAutoSourceId(catalog, {
+ agent: route.harness === 'claude' ? 'claude-code' : route.harness,
+ model: route.model,
+ providerId: route.providerId,
+ });
+ return providerId ? { ...route, providerId } : route;
+ });
+}
+
/** A null override follows owner settings; explicit per-Bot routes stay in its Profile. */
export async function readEffectiveBotModelSelection(
config: Record<string, unknown>,
options?: Parameters<typeof readBotModelChainSettingsState>[0],
): Promise<{ chain: BotModelRoute[]; followsCindyDefault: boolean }> {
const explicit = readExplicitBotModelChain(config);
- if (explicit !== null) return { chain: explicit, followsCindyDefault: false };
+ if (explicit !== null) {
+ return { chain: await followMovedCindyAuto(explicit, options?.providers), followsCindyDefault: false };
+ }
const state = await readBotModelChainSettingsState(options);
- return { chain: state.value.modelChain, followsCindyDefault: !state.isCustomized };
+ // 跟随 Cindy 默认时链路由实时目录推导,已经是当前位置;只有用户自定义的设置是存下来的。
+ const chain = state.isCustomized
+ ? await followMovedCindyAuto(state.value.modelChain, options?.providers)
+ : state.value.modelChain;
+ return { chain, followsCindyDefault: !state.isCustomized };
}
export async function readEffectiveBotModelChain(
diff --git a/apps/desktop/src/main/maker-ipc/cindyAutoRouteFollow.ts b/apps/desktop/src/main/maker-ipc/cindyAutoRouteFollow.ts
index 80e68fa95f..5e8293c0ed 100644
--- a/apps/desktop/src/main/maker-ipc/cindyAutoRouteFollow.ts
+++ b/apps/desktop/src/main/maker-ipc/cindyAutoRouteFollow.ts
@@ -1,6 +1,5 @@
import {
- effectiveSourceIdForModel,
- isCindyAutoModelId,
+ movedCindyAutoSourceId,
type AgentKind,
type ProviderView,
} from '@cindy/model-providers';
@@ -22,21 +21,16 @@ export interface CindyAutoRoute {
fastMode: boolean;
}
-/** Pi 的 providerId=null 就是 Cindy AI 网关;其它引擎的 null 不一定落在 Cindy AI,不推断。 */
-function rememberedProviderId(route: CindyAutoRoute): string | null {
- return route.providerId ?? (route.agentKind === 'pi' ? 'xd' : null);
-}
-
/** 该路由的 Auto 已被服务端挪到另一家 Cindy 来源时返回新来源,否则 null。 */
export function movedCindyAutoProviderId(
providers: readonly ProviderView[],
route: CindyAutoRoute,
): string | null {
- if (!isCindyAutoModelId(route.model)) return null;
- const remembered = rememberedProviderId(route);
- if (!remembered) return null;
- const effective = effectiveSourceIdForModel([...providers], remembered, route.model, route.agentKind);
- return effective && effective !== remembered ? effective : null;
+ return movedCindyAutoSourceId(providers, {
+ agent: route.agentKind,
+ model: route.model,
+ providerId: route.providerId,
+ });
}
export interface CindyAutoRouteFollowDeps {
diff --git a/docs/dev-rules/model-catalog-maintenance.md b/docs/dev-rules/model-catalog-maintenance.md
index 800f8ed422..9fb2e34714 100644
--- a/docs/dev-rules/model-catalog-maintenance.md
+++ b/docs/dev-rules/model-catalog-maintenance.md
@@ -150,6 +150,9 @@ Auto 只按模型 ID 认(`isCindyAutoModelId`):服务端可以把同一个
目录刷新后,仍记着旧来源的进行中任务登记一次「下一条消息前切换」:引擎与模型 ID 不变,只换
供应商与 Key(`apps/desktop/src/main/maker-ipc/cindyAutoRouteFollow.ts`);已有登记的切换或
运行时改道不覆盖,远程主机与另一台电脑上运行的任务不在本机改道。请求被拒后刷新目录重试不在客户端处理。
+存下来的「来源 + Auto」(伙伴自己的模型链与用户自定义的伙伴默认、IM 渠道默认路由)在使用时套用
+同一规则(`movedCindyAutoSourceId`,Pi 的 null 来源即 Cindy AI),不改写保存的配置;IM 跟随渠道
+默认时把 Auto 的新旧位置视为同一路由,避免跟过去后被误判成用户单独改过。
设置页管理列表组内与选择器同序:组内每项都带 `sortOrder` 时按它排;只要有一项缺失,退回
按系列名 A–Z、同系列版本号降序,避免局部权重把新型号压到旧策展位置之后。
diff --git a/packages/model-providers/src/__tests__/cindyAutoRelocation.test.ts b/packages/model-providers/src/__tests__/cindyAutoRelocation.test.ts
index 47b8aa5be7..9877768ebc 100644
--- a/packages/model-providers/src/__tests__/cindyAutoRelocation.test.ts
+++ b/packages/model-providers/src/__tests__/cindyAutoRelocation.test.ts
@@ -5,7 +5,7 @@
import { describe, expect, it } from 'vitest';
import { isCindyAutoModelId, isCindyDistributedProvider } from '../provider-identity.js';
-import { effectiveSourceIdForModel, type ProviderView } from '../registry.js';
+import { effectiveSourceIdForModel, movedCindyAutoSourceId, type ProviderView } from '../registry.js';
import type { CatalogModel, ProviderSource } from '../types.js';
function model(id: string): CatalogModel {
@@ -88,3 +88,18 @@ describe('effectiveSourceIdForModel:Auto 跨 Cindy 来源改道', () => {
expect(effectiveSourceIdForModel([provider('xd', 'builtin', [AUTO])], 'gone-pool', AUTO, 'pi')).toBeNull();
});
});
+
+describe('movedCindyAutoSourceId:已保存 / 运行中的路由', () => {
+ const toPool = [provider('xd', 'builtin', ['auto']), provider('org-pool', 'organization', [AUTO])];
+
+ it('挪动后给出新来源,没挪动给 null', () => {
+ expect(movedCindyAutoSourceId(toPool, { agent: 'pi', model: AUTO, providerId: 'xd' })).toBe('org-pool');
+ expect(movedCindyAutoSourceId(toPool, { agent: 'pi', model: AUTO, providerId: 'org-pool' })).toBeNull();
+ expect(movedCindyAutoSourceId(toPool, { agent: 'pi', model: 'auto', providerId: 'xd' })).toBeNull();
+ });
+
+ it('Pi 的 null 就是 Cindy AI;其它引擎的 null 不推断', () => {
+ expect(movedCindyAutoSourceId(toPool, { agent: 'pi', model: AUTO, providerId: null })).toBe('org-pool');
+ expect(movedCindyAutoSourceId(toPool, { agent: 'codex', model: AUTO, providerId: null })).toBeNull();
+ });
+});
diff --git a/packages/model-providers/src/index.ts b/packages/model-providers/src/index.ts
index e5943c4180..5a4a578cfd 100644
--- a/packages/model-providers/src/index.ts
+++ b/packages/model-providers/src/index.ts
@@ -148,6 +148,7 @@ export {
connectedProvidersForAgent,
nativeDefaultSourceId,
effectiveSourceIdForModel,
+ movedCindyAutoSourceId,
actualSourceIdForModel,
providerOffersModel,
getModel,
diff --git a/packages/model-providers/src/registry.ts b/packages/model-providers/src/registry.ts
index ef6e016091..1ee093378f 100644
--- a/packages/model-providers/src/registry.ts
+++ b/packages/model-providers/src/registry.ts
@@ -364,6 +364,22 @@ function relocatedAutoSourceId(
return nativeDefaultSourceId(sources.filter(isCindyDistributedProvider), agent);
}
+/**
+ * 已保存或运行中的路由(任务、伙伴模型链、IM 默认):服务端把其中的 Auto 挪到另一家
+ * Cindy 来源后,现在该走的来源;没挪动或不适用时返回 null。只在使用时解析,不改写保存的值。
+ * Pi 的 providerId=null 就是 Cindy AI 网关;其它引擎的 null 不一定落在 Cindy AI,不推断。
+ */
+export function movedCindyAutoSourceId(
+ views: readonly ProviderView[],
+ route: { agent: AgentKind; model: string; providerId: string | null },
+): string | null {
+ if (!isCindyAutoModelId(route.model)) return null;
+ const remembered = route.providerId ?? (route.agent === 'pi' ? 'xd' : null);
+ if (!remembered) return null;
+ const effective = effectiveSourceIdForModel([...views], remembered, route.model, route.agent);
+ return effective && effective !== remembered ? effective : null;
+}
+
/**
* 「实际路由口径」的来源解析:选择规则与 effectiveSourceIdForModel 相同,但**不**
* 剔除停用拷贝(includeDisabled rail)。给「展示一个已在运行的会话」用:运行中的