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-default): 跨来源 Auto 两边列表联动刷新,按无档位登记切换

fmfsaisai1cd4a5966dd6
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/cindyAutoRouteFollow.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/cindyAutoRouteFollow.test.ts
index df0560564a..c9354b969b 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/cindyAutoRouteFollow.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/cindyAutoRouteFollow.test.ts
@@ -164,3 +164,46 @@ describe('followMovedCindyAutoRoutes', () => {
expect(stage).toHaveBeenCalledTimes(1);
});
});
+
+describe('Auto 没有推理档位时的跨来源跟随', () => {
+ // Auto 不带档位,任务库里 effort 存的是空串;登记切换只接受合法档位或 null。
+ const movedToPool = [provider('xd', 'builtin', ['auto']), provider('org-pool', 'organization', [AUTO])];
+
+ function follow(providers: ProviderView[], current: CindyAutoRoute) {
+ const stage = vi.fn(async () => {});
+ return {
+ stage,
+ run: () => followMovedCindyAutoRoutes({
+ listCandidates: async () => [{ sessionId: 'task', ...current }],
+ listProviders: async () => providers,
+ withSessionLock: async (_sessionId, run) => run(),
+ readRoute: async () => current,
+ stage,
+ assertCurrent: () => {},
+ }),
+ };
+ }
+
+ it('号池 → 平台:按无档位(null)登记,不把空串交给登记切换', async () => {
+ const { stage, run } = follow(movedToPlatform, route({ providerId: 'org-pool', effort: '' }));
+ await expect(run()).resolves.toBe(1);
+ expect(stage).toHaveBeenCalledWith('task', {
+ agentKind: 'pi', model: AUTO, providerId: 'xd', effort: null, fastMode: false,
+ });
+ });
+
+ it('平台 → 号池:同样按无档位(null)登记', async () => {
+ const { stage, run } = follow(movedToPool, route({ providerId: 'xd', effort: '' }));
+ await expect(run()).resolves.toBe(1);
+ expect(stage).toHaveBeenCalledWith('task', {
+ agentKind: 'pi', model: AUTO, providerId: 'org-pool', effort: null, fastMode: false,
+ });
+ });
+
+ it('负向:用户自己接的供应商上的 auto/... 不跟随,即使 Cindy 来源也有同名 Auto', async () => {
+ const providers = [provider('custom', 'user', ['other']), provider('xd', 'builtin', [AUTO])];
+ const { stage, run } = follow(providers, route({ providerId: 'custom', effort: '' }));
+ await expect(run()).resolves.toBe(0);
+ expect(stage).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/desktop/src/main/maker-ipc/cindyAutoRouteFollow.ts b/apps/desktop/src/main/maker-ipc/cindyAutoRouteFollow.ts
index 5e8293c0ed..d3def594a1 100644
--- a/apps/desktop/src/main/maker-ipc/cindyAutoRouteFollow.ts
+++ b/apps/desktop/src/main/maker-ipc/cindyAutoRouteFollow.ts
@@ -4,6 +4,8 @@ import {
type ProviderView,
} from '@cindy/model-providers';
+import { isSupportedRuntimeEffort } from './runtimeSelectionAxes.js';
+
/**
* 进行中任务跟随服务端挪动的 Auto。
*
@@ -61,7 +63,9 @@ export async function followMovedCindyAutoRoutes(deps: CindyAutoRouteFollowDeps)
const route = await deps.readRoute(candidate.sessionId);
const providerId = route ? movedCindyAutoProviderId(providers, route) : null;
if (!route || !providerId) return;
- await deps.stage(candidate.sessionId, { ...route, providerId });
+ // Auto 没有推理档位,任务里存的是空串;登记切换只接受合法档位或 null。
+ const effort = isSupportedRuntimeEffort(route.effort) ? route.effort : null;
+ await deps.stage(candidate.sessionId, { ...route, providerId, effort });
staged += 1;
deps.logger?.info('Cindy Auto moved provider; staged switch for next message', {
sessionId: candidate.sessionId,
diff --git a/apps/desktop/src/main/model-access/__tests__/byokRuntime.test.ts b/apps/desktop/src/main/model-access/__tests__/byokRuntime.test.ts
index 7483567b76..e8a10d154d 100644
--- a/apps/desktop/src/main/model-access/__tests__/byokRuntime.test.ts
+++ b/apps/desktop/src/main/model-access/__tests__/byokRuntime.test.ts
@@ -472,3 +472,38 @@ describe('BYOK Main runtime', () => {
expect(reloadCodex).toHaveBeenCalledTimes(5);
});
});
+describe('Cindy Auto moving between Cindy AI and the enterprise pool', () => {
+ function withModels(revision: string, ...ids: string[]) {
+ const next = structuredClone(directory);
+ next.revision = revision;
+ next.providers[0].models = ids.map((id) => ({ ...structuredClone(provider.models[0]), id, name: id }));
+ return next;
+ }
+ async function syncTwice(first: typeof directory, second: typeof directory) {
+ const onCindyAutoModelsChanged = vi.fn();
+ const runtime = createByokRuntime(undefined, undefined, undefined, onCindyAutoModelsChanged);
+ runtime.setOwner({ scope: 'a:cn:1', organizationId: 'org-a' });
+ state.fetch.mockResolvedValueOnce(first).mockResolvedValueOnce(credentials);
+ await runtime.sync();
+ onCindyAutoModelsChanged.mockClear();
+ state.fetch.mockResolvedValueOnce(second).mockResolvedValueOnce(credentials);
+ await runtime.sync();
+ return onCindyAutoModelsChanged;
+ }
+
+ it('platform → pool: a newly listed pool Auto asks Cindy AI to refresh too', async () => {
+ const notify = await syncTwice(withModels('1', 'byok/a/chat'), withModels('2', 'byok/a/chat', 'auto/group-a'));
+ expect(notify).toHaveBeenCalledTimes(1);
+ });
+
+ it('pool → platform: a pool Auto that disappeared asks Cindy AI to refresh too', async () => {
+ const notify = await syncTwice(withModels('1', 'byok/a/chat', 'auto/group-a'), withModels('2', 'byok/a/chat'));
+ expect(notify).toHaveBeenCalledTimes(1);
+ });
+
+ it('does not ask when only ordinary pool models change or nothing changes', async () => {
+ expect(await syncTwice(withModels('1', 'byok/a/chat', 'auto/group-a'), withModels('2', 'byok/a/other', 'auto/group-a')))
+ .not.toHaveBeenCalled();
+ expect(await syncTwice(withModels('1', 'auto/group-a'), withModels('1', 'auto/group-a'))).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/desktop/src/main/model-access/__tests__/modelsSyncRefresh.test.ts b/apps/desktop/src/main/model-access/__tests__/modelsSyncRefresh.test.ts
index 89ff8277b5..78751e8249 100644
--- a/apps/desktop/src/main/model-access/__tests__/modelsSyncRefresh.test.ts
+++ b/apps/desktop/src/main/model-access/__tests__/modelsSyncRefresh.test.ts
@@ -3,6 +3,7 @@ import type { ModelAccessStatus } from '../../../shared/modelAccess.js';
import {
buildModelsSyncRequest,
+ cindyAutoModelIdsChanged,
ensureCredentialsReadyForModelsRefresh,
modelsWithoutStalePaymentUpsell,
parseModelsSyncPayload,
@@ -440,3 +441,21 @@ describe('waitForModelsSyncRefresh', () => {
).resolves.toBe('failed');
});
});
+
+describe('cindyAutoModelIdsChanged', () => {
+ const ids = (...values: string[]) => values.map((id) => ({ id }));
+
+ it('号池 → 平台:Cindy AI 列表新增 Auto 算变化', () => {
+ expect(cindyAutoModelIdsChanged(ids('p1'), ids('p1', 'auto/group-a'))).toBe(true);
+ });
+
+ it('平台 → 号池:Cindy AI 列表移除 Auto 算变化', () => {
+ expect(cindyAutoModelIdsChanged(ids('auto/group-a', 'p1'), ids('p1'))).toBe(true);
+ });
+
+ it('负向:只有普通模型变化、顺序变化或形似 auto 的 ID 不算', () => {
+ expect(cindyAutoModelIdsChanged(ids('auto/group-a', 'p1'), ids('p2', 'auto/group-a'))).toBe(false);
+ expect(cindyAutoModelIdsChanged(ids('autopilot', 'vendor/auto'), ids('automation'))).toBe(false);
+ expect(cindyAutoModelIdsChanged([], [])).toBe(false);
+ });
+});
diff --git a/apps/desktop/src/main/model-access/byokRuntime.ts b/apps/desktop/src/main/model-access/byokRuntime.ts
index d1ef7f4847..06ecf9826e 100644
--- a/apps/desktop/src/main/model-access/byokRuntime.ts
+++ b/apps/desktop/src/main/model-access/byokRuntime.ts
@@ -13,6 +13,7 @@ import { getClientEndpoint } from '../clientEndpointsService.js';
import { createByokSync, type ByokConnection } from './byokSync.js';
import { buildByokProvider } from './byokProvider.js';
import { buildByokPricing, setByokPricing } from './byokPricing.js';
+import { cindyAutoModelIdsChanged } from './modelsSyncRefresh.js';
import { createLogger } from '../logger.js';
const log = createLogger('byokRuntime');
@@ -99,6 +100,8 @@ export function createByokRuntime(
onPricingChanged?: () => void,
cache?: ByokCache,
onCodexRoutesChanged?: () => void,
+ /** 号池目录里的 Cindy Auto 有增减:Auto 可能换了来源,请调用方让 Cindy AI 列表也刷新。 */
+ onCindyAutoModelsChanged?: () => void,
) {
let installed: readonly ByokConnection[] = [];
let installedDirectory: readonly ByokProvider[] = [];
@@ -183,6 +186,10 @@ export function createByokRuntime(
isDeepStrictEqual(installedDirectory, directory)
)
return;
+ const cindyAutoChanged = cindyAutoModelIdsChanged(
+ installedDirectory.flatMap((provider) => provider.models),
+ directory.flatMap((provider) => provider.models),
+ );
const previousDispatchState = dispatchState(installedDirectory, installed);
const nextDispatchState = dispatchState(directory, connections);
const previousCodexState = codexHostState(installedDirectory, installed);
@@ -249,6 +256,13 @@ export function createByokRuntime(
} catch {
log.warn('reference price notification failed');
}
+ if (cindyAutoChanged) {
+ try {
+ onCindyAutoModelsChanged?.();
+ } catch {
+ log.warn('Cindy Auto refresh notification failed');
+ }
+ }
},
});
}
diff --git a/apps/desktop/src/main/model-access/index.ts b/apps/desktop/src/main/model-access/index.ts
index a2029ae177..44a7793b06 100644
--- a/apps/desktop/src/main/model-access/index.ts
+++ b/apps/desktop/src/main/model-access/index.ts
@@ -45,6 +45,7 @@ import {
} from './credentialsSync.js';
import {
buildModelsSyncRequest,
+ cindyAutoModelIdsChanged,
ensureCredentialsReadyForModelsRefresh,
modelsWithoutStalePaymentUpsell,
parseModelsSyncPayload,
@@ -164,6 +165,11 @@ export const XD_MODELS_FOREGROUND_REFRESH_INTERVAL_MS = 5 * 60_000;
let modelsSyncGen = -1;
/** 旧世代请求在途时新账号的补发标记。 */
let modelsSyncRerunQueued = false;
+/**
+ * Cindy AI 列表里的 Auto 有增减时刷新企业号池(初始化时接上)。Auto 在两边之间换位置时,
+ * 只刷新一边会让进行中任务的跟随看到不一致的列表;另一边刷新后回头不再有增减,联动自然停止。
+ */
+let onXdCindyAutoModelsChanged: (() => void) | null = null;
/**
* 认证世代:登出或 userId / realm 变化时自增(与 credentialsSync 的 epoch 同语义)。
* 目录请求以发起时世代为闸——旧身份的在途 /models 响应在换号或同账号跨区后
@@ -268,14 +274,22 @@ async function runModelsSync(
return;
}
if (myGen !== authGeneration) return; // 响应归属旧账号,丢弃
+ const cindyAutoChanged = cindyAutoModelIdsChanged(getXdGatewayModels(), models);
+ const refreshPoolIfCindyAutoMoved = () => {
+ if (!cindyAutoChanged || !onXdCindyAutoModelsChanged) return;
+ log.info('Cindy Auto changed in Cindy AI models; refreshing the enterprise pool directory');
+ onXdCindyAutoModelsChanged();
+ };
if (models.length === 0) {
log.warn('xd gateway models fetch returned empty list; clearing current list');
applyGatewayModels([], { authenticatedUserId, authoritative: true });
lastModelsSyncSucceededAttempt = myAttempt;
+ refreshPoolIfCindyAutoMoved();
return;
}
log.info(`xd gateway models synced: ${models.length}`);
applyGatewayModels(models, { authenticatedUserId, authoritative: true });
+ refreshPoolIfCindyAutoMoved();
try {
const availability = await listExecutableMediaModels([], {
includeDisabled: true,
@@ -518,7 +532,14 @@ export function initModelAccess(): void {
broadcastReferenceModelPricing,
createByokCache(byokSnapshotSecretIo),
scheduleByokCodexHostRefresh,
+ () => {
+ // 号池里的 Auto 有增减:Auto 可能换到了 Cindy AI(或从那里搬来),让 Cindy AI 列表也刷新。
+ if (!lastAuthUserId || getSync().getStatus().state !== 'ok') return;
+ log.info('Cindy Auto changed in the enterprise pool directory; refreshing Cindy AI models');
+ scheduleModelsSync();
+ },
);
+ onXdCindyAutoModelsChanged = () => scheduleByokSync(byok, true);
const noteAuthState = (
isAuthenticated: boolean,
@@ -608,6 +629,7 @@ export function resetModelAccessForTest(): void {
foregroundRefreshListener = null;
}
installCindyGatewayKeyRejectionReporter(null);
+ onXdCindyAutoModelsChanged = null;
syncInstance = null;
modelsSyncInflight = null;
modelsSyncGen = -1;
diff --git a/apps/desktop/src/main/model-access/modelsSyncRefresh.ts b/apps/desktop/src/main/model-access/modelsSyncRefresh.ts
index 552fc78816..08ca5c00b2 100644
--- a/apps/desktop/src/main/model-access/modelsSyncRefresh.ts
+++ b/apps/desktop/src/main/model-access/modelsSyncRefresh.ts
@@ -1,4 +1,5 @@
import {
+ isCindyAutoModelId,
MODEL_ACCESS_CATALOG_V5_SCHEMA_VERSION,
parseListModelsResponse,
} from '@cindy/model-providers';
@@ -19,6 +20,21 @@ export type ModelsSyncPayloadParseResult =
| { ok: true; models: ModelAccessGatewayModel[]; accountTier: ModelAccessAccountTier }
| { ok: false; error: string };
+/**
+ * 同一个 Cindy Auto(预设 auto 与 auto/<组ID>)会随管理员改目标在 Cindy AI 与企业号池之间换位置;
+ * 任一边列表里的 Auto 有增减,说明另一边很可能也变了,需要一起刷新后再判断进行中任务的跟随。
+ */
+export function cindyAutoModelIdsChanged(
+ before: readonly { id: string }[],
+ after: readonly { id: string }[],
+): boolean {
+ const autoIds = (models: readonly { id: string }[]) =>
+ new Set(models.map((model) => model.id).filter(isCindyAutoModelId));
+ const previous = autoIds(before);
+ const next = autoIds(after);
+ return previous.size !== next.size || [...previous].some((id) => !next.has(id));
+}
+
/** 失败后的 LKG 只保留可执行模型;过期的付费差集不能继续作为升级营销事实。 */
export function modelsWithoutStalePaymentUpsell(
models: readonly ModelAccessGatewayModel[],
diff --git a/docs/dev-rules/model-catalog-maintenance.md b/docs/dev-rules/model-catalog-maintenance.md
index 9fb2e34714..fb7b6d565e 100644
--- a/docs/dev-rules/model-catalog-maintenance.md
+++ b/docs/dev-rules/model-catalog-maintenance.md
@@ -149,7 +149,10 @@ Auto 只按模型 ID 认(`isCindyAutoModelId`):服务端可以把同一个
记住的来源仍列着它(只是断连或停用)、或是用户自己接的供应商时不改道。
目录刷新后,仍记着旧来源的进行中任务登记一次「下一条消息前切换」:引擎与模型 ID 不变,只换
供应商与 Key(`apps/desktop/src/main/maker-ipc/cindyAutoRouteFollow.ts`);已有登记的切换或
-运行时改道不覆盖,远程主机与另一台电脑上运行的任务不在本机改道。请求被拒后刷新目录重试不在客户端处理。
+运行时改道不覆盖,远程主机与另一台电脑上运行的任务不在本机改道。Auto 没有推理档位(任务里 effort
+为空串)时按 null 登记。Cindy AI 列表或企业号池目录任一边刷新后 Auto ID 有增减,会顺带刷新另一边
+(`cindyAutoModelIdsChanged`),跟随在两边都更新后才看到一致的位置;只刷新一边会把旧位置当成仍有效。
+请求被拒后刷新目录重试不在客户端处理。
存下来的「来源 + Auto」(伙伴自己的模型链与用户自定义的伙伴默认、IM 渠道默认路由)在使用时套用
同一规则(`movedCindyAutoSourceId`,Pi 的 null 来源即 Cindy AI),不改写保存的配置;IM 跟随渠道
默认时把 Auto 的新旧位置视为同一路由,避免跟过去后被误判成用户单独改过。