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(media): preserve legacy provider compatibility

liugaoweicee4320a4e30
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/cindySlot.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/cindySlot.test.ts
index 1be490d421..79d47cc0e7 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/cindySlot.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/cindySlot.test.ts
@@ -221,16 +221,43 @@ describe('载荷校验', () => {
).toMatchObject({ ok: false });
});
- it('模型白名单:名单内放行并透传,名单外拒且不触发生成', async () => {
+ it('旧插件模型名:名单内透传,唯一 basename 升级,失效值回落当前默认', async () => {
const { slot, generateImage } = makeSlot();
const ok = await slot.handleModelRequest('art', { ...REQ, model: 'gemini-3-pro-image' });
expect(ok).toMatchObject({ ok: true });
- expect(generateImage).toHaveBeenCalledWith({ prompt: '一只猫', model: 'gemini-3-pro-image' });
+ expect(generateImage).toHaveBeenLastCalledWith({
+ prompt: '一只猫',
+ model: 'gemini-3-pro-image',
+ });
- const bad = await slot.handleModelRequest('art', { ...REQ, model: 'dall-e-9' });
- expect(bad).toMatchObject({ ok: false });
- expect((bad as { message: string }).message).toContain('白名单');
- expect(generateImage).toHaveBeenCalledTimes(1);
+ const namespaced = makeSlot({
+ getImageConfig: vi.fn(() => ({
+ models: [
+ {
+ id: 'openai/gpt-image-2',
+ label: 'GPT Image 2',
+ supportsEdit: true,
+ },
+ ],
+ defaults: {
+ standard: 'openai/gpt-image-2',
+ draft: 'openai/gpt-image-2',
+ best: 'openai/gpt-image-2',
+ },
+ })) as unknown as CindySlotDeps['getImageConfig'],
+ });
+ expect(await namespaced.slot.handleModelRequest('art', { ...REQ, model: 'gpt-image-2' })).toMatchObject({
+ ok: true,
+ model: 'openai/gpt-image-2',
+ });
+ expect(namespaced.generateImage).toHaveBeenCalledWith({
+ prompt: '一只猫',
+ model: 'openai/gpt-image-2',
+ });
+
+ const fallback = await slot.handleModelRequest('art', { ...REQ, model: 'dall-e-9' });
+ expect(fallback).toMatchObject({ ok: true, model: 'gpt-image-2' });
+ expect(generateImage).toHaveBeenLastCalledWith({ prompt: '一只猫', model: 'gpt-image-2' });
});
it('缺省模型 = gpt-image-2', async () => {
@@ -812,13 +839,16 @@ describe('视频代办(gen_video / edit_video)', () => {
expect(saveGhostMedia).toHaveBeenCalledWith(expect.objectContaining({ mimeType: 'video/mp4' }));
});
- it('tier 档位查视频翻译表(best → seedance-pro);白名单外点名拒', async () => {
+ it('tier 档位查视频翻译表(best → seedance-pro);失效点名回落默认', async () => {
const { slot, generateVideo } = makeSlot();
await slot.handleModelRequest('art', { ...VREQ, tier: 'best' });
expect(generateVideo).toHaveBeenLastCalledWith({ prompt: '一只猫奔跑', model: 'seedance-pro' });
- // 图像白名单里的型号点给视频代办 → 拒(两类目白名单独立)。
- const bad = await slot.handleModelRequest('art', { ...VREQ, model: 'gpt-image-2' });
- expect(bad).toMatchObject({ ok: false });
+ const fallback = await slot.handleModelRequest('art', { ...VREQ, model: 'gpt-image-2' });
+ expect(fallback).toMatchObject({ ok: true, model: 'seedance-fast' });
+ expect(generateVideo).toHaveBeenLastCalledWith({
+ prompt: '一只猫奔跑',
+ model: 'seedance-fast',
+ });
});
it('edit_video:参考图归属校验后按路径注入;上限 2 张,超限拒', async () => {
diff --git a/apps/desktop/src/main/cindy-brain/cindySlot.ts b/apps/desktop/src/main/cindy-brain/cindySlot.ts
index 9cc57ba773..1dbf8e0d27 100644
--- a/apps/desktop/src/main/cindy-brain/cindySlot.ts
+++ b/apps/desktop/src/main/cindy-brain/cindySlot.ts
@@ -830,7 +830,8 @@ export class GhostCindySlot {
// 选型优先级(低 → 高逐层覆盖):出厂默认 → 档位(意识意图,主机翻译)
// → 意识专属覆盖(用户在详情页钉的)→ 调用显式点名(用户当场说的)。
- // 意识报了白名单外的名字 = 拒,不静默降级。配置按类目取(图像/视频
+ // 旧插件报的裸 ID 会在唯一时升级为完整 ID;已失效或歧义值保留前面
+ // 已解析的用户配置/当前默认。配置按类目取(图像/视频
// 各一份白名单与默认,同来自 providers.json 目录)。
const cfg = info.category === 'image' ? this.deps.getImageConfig() : this.deps.getVideoConfig();
// 目录没给该类目任何模型 = 能力暂不可用:早拒并说清原因,不落回任何写死型号
@@ -873,19 +874,35 @@ export class GhostCindySlot {
}
}
if (p.model !== undefined) {
- if (typeof p.model !== 'string' || !whitelist.has(p.model)) {
- // 拒绝话术带上当前可用清单:插件侧不再维护模型枚举(白名单单源执法,
- // 2026-07),AI 点名失败时靠这份清单自愈,不用猜主机认哪些 id。
- return {
- ok: false,
- message: `不支持的模型(不在主机白名单内)。当前可用:${cfg.models.length > 0 ? cfg.models.map((m) => m.id).join(' / ') : '(暂无可用型号)'}`,
- };
+ if (typeof p.model !== 'string') {
+ return { ok: false, message: 'model 不合法(必须是字符串)' };
}
- if (p.model !== model) {
- providerId = undefined;
- providerModelLabel = undefined;
+ const requestedModel = p.model;
+ const exact = cfg.models.find((candidate) => candidate.id === requestedModel);
+ const basenameMatches = requestedModel.includes('/')
+ ? []
+ : cfg.models.filter(
+ (candidate) =>
+ candidate.id.slice(candidate.id.lastIndexOf('/') + 1) === requestedModel,
+ );
+ const selected = exact ?? (basenameMatches.length === 1 ? basenameMatches[0] : undefined);
+ if (selected) {
+ if (selected.id !== model) {
+ providerId = undefined;
+ providerModelLabel = undefined;
+ }
+ model = selected.id;
+ } else {
+ // 旧插件会携带发布时写进说明的裸 ID;目录升级后该 ID 可能已 namespaced
+ // 或下架。此时保留上面已经解析出的用户配置/当前默认,不让旧插件版本把
+ // 整项媒体能力卡死,也不把一个歧义裸名猜成第三方计费来源。
+ this.deps.log?.warn('ghost cindy explicit legacy model unavailable, using resolved fallback', {
+ ghostId,
+ requestedModel,
+ fallbackModel: model,
+ ...(providerId ? { fallbackProviderId: providerId } : {}),
+ });
}
- model = p.model;
}
// 画面参数按**解析出的型号**二次校验:协议层值域是所有 provider 的
diff --git a/apps/desktop/src/main/cindy-brain/index.ts b/apps/desktop/src/main/cindy-brain/index.ts
index c1daa639ee..27279487e2 100644
--- a/apps/desktop/src/main/cindy-brain/index.ts
+++ b/apps/desktop/src/main/cindy-brain/index.ts
@@ -3347,6 +3347,54 @@ function resolveMediaPreferenceOrDefault(
);
}
+/**
+ * Art 1.12.5–1.13.2 已经走 Core media,但还不会在 prepare 时传
+ * providerId。这段兼容窗口只能稳定表达 Cindy AI 来源;1.13.3 起
+ * 插件会传完整来源,恢复全部 Provider 选择。
+ */
+function isProviderBlindCoreArt(ghost: InstalledGhost): boolean {
+ return (
+ ghost.manifest.id === 'cindy-art' &&
+ supportsCindyVersion(ghost.manifest.version, '1.12.5') &&
+ !supportsCindyVersion(ghost.manifest.version, '1.13.3')
+ );
+}
+
+function getGhostMediaPreferenceConfig(
+ ghostId: string,
+ capability: GhostMediaCapability,
+): CindyMediaPreferenceConfig {
+ const config = getMediaPreferenceConfig(capability);
+ const ghost = getGhostManager().list().find((candidate) => candidate.manifest.id === ghostId);
+ if (!ghost || !isProviderBlindCoreArt(ghost)) return config;
+ const models = config.models.filter((model) => model.providerId === 'xd');
+ const standard = models[0]?.id;
+ return {
+ models,
+ defaults: standard ? { standard, draft: standard, best: standard } : null,
+ };
+}
+
+/** 存量钉定已失效或旧插件无法表达时,一次性升级为当前可用选型。 */
+function resolveAndMigrateGhostMediaPreference(
+ ghostId: string,
+ capability: GhostMediaCapability,
+ config: CindyMediaPreferenceConfig,
+ preference: string | undefined,
+): CindyMediaPreferenceModel | null {
+ const selected = resolveMediaPreferenceOrDefault(config, preference);
+ if (preference && selected && preference !== selected.id) {
+ log.info('migrating stale ghost media preference to available model', {
+ ghostId,
+ capability,
+ previousPreference: preference,
+ nextPreference: selected.id,
+ });
+ writeGhostCindyOverride(ghostId, capability, selected.id);
+ }
+ return selected;
+}
+
/**
* 插件配置页的模型目录;这里只读目录,不提供任何生成入口。Host 只按 Gateway mode
* 切图片/视频大类并透传 modalities,具体动作支持度由插件自行解释。
@@ -3372,7 +3420,10 @@ async function getGhostConfigurableMediaModels(
// Gateway modalities 透传给插件判断,不能把插件声明的多个动作取交集。
const availability = await listExecutableMediaModels();
const mode = type === 'image' ? 'image_generation' : 'video_generation';
- const models = availability.models.filter((model) => model.mode === mode);
+ const models = availability.models.filter(
+ (model) =>
+ model.mode === mode && (!isProviderBlindCoreArt(ghost) || model.providerId === 'xd'),
+ );
if (
models.length === 0 &&
availability.unavailable.some((model) => model.retryable)
@@ -3424,8 +3475,8 @@ async function getGhostConfigurableMediaModels(
/**
* 读取插件在 Host「Cindy 能力」中的现有媒体选型。
- * 这里只投影当前有效值,不复制或迁移配置;已配置模型失效时,
- * 统一回落到该能力当前的默认(第一个可用)模型。
+ * 已配置模型失效,或旧插件无法表达完整来源时,回落到当前第一个
+ * 可用模型,并一次性迁移存量配置,保证页面展示与实际执行一致。
*/
function getGhostConfiguredMediaModel(
ghostId: string,
@@ -3452,9 +3503,14 @@ function getGhostConfiguredMediaModel(
};
}
- const config = getMediaPreferenceConfig(mediaCapability);
+ const config = getGhostMediaPreferenceConfig(ghostId, mediaCapability);
const override = readGhostCindyOverrides(ghostId)[mediaCapability];
- const selected = resolveMediaPreferenceOrDefault(config, override);
+ const selected = resolveAndMigrateGhostMediaPreference(
+ ghostId,
+ mediaCapability,
+ config,
+ override,
+ );
if (!selected) {
return {
ok: false,
@@ -3931,8 +3987,11 @@ export function getGhostCindySlot(): GhostCindySlot {
getMediaOverride: (ghostId, capability) => {
const value = readGhostCindyOverrides(ghostId)[capability as CindyCapabilityKey] ?? null;
if (!value) return null;
- const selected = resolveMediaPreferenceOrDefault(
- getMediaPreferenceConfig(capability as GhostMediaCapability),
+ const mediaCapability = capability as GhostMediaCapability;
+ const selected = resolveAndMigrateGhostMediaPreference(
+ ghostId,
+ mediaCapability,
+ getGhostMediaPreferenceConfig(ghostId, mediaCapability),
value,
);
return selected
@@ -6172,8 +6231,10 @@ export function registerGhostIpc(): void {
for (const capability of GHOST_MEDIA_CAPABILITIES) {
const value = storedOverrides[capability];
if (!value) continue;
- const selected = resolveMediaPreferenceOrDefault(
- getMediaPreferenceConfig(capability),
+ const selected = resolveAndMigrateGhostMediaPreference(
+ ghostId as string,
+ capability,
+ getGhostMediaPreferenceConfig(ghostId as string, capability),
value,
);
if (selected) overrides[capability] = selected.id;
@@ -6223,10 +6284,10 @@ export function registerGhostIpc(): void {
: null;
event.returnValue = {
overrides,
- image: byKind(getMediaPreferenceConfig('image.generate')),
- imageEdit: byKind(getMediaPreferenceConfig('image.edit')),
- video: byKind(getMediaPreferenceConfig('video.generate')),
- videoEdit: byKind(getMediaPreferenceConfig('video.edit')),
+ image: byKind(getGhostMediaPreferenceConfig(ghostId as string, 'image.generate')),
+ imageEdit: byKind(getGhostMediaPreferenceConfig(ghostId as string, 'image.edit')),
+ video: byKind(getGhostMediaPreferenceConfig(ghostId as string, 'video.generate')),
+ videoEdit: byKind(getGhostMediaPreferenceConfig(ghostId as string, 'video.edit')),
text: {
options: textOptions,
defaultModel:
@@ -6299,10 +6360,12 @@ export function registerGhostIpc(): void {
// fail-closed 兜底,不在这层把「暂时没配 key」当成非法值。
if (
!isCindyOverrideModelAllowed(capability as string, model, {
- image: getMediaPreferenceConfig(
+ image: getGhostMediaPreferenceConfig(
+ ghostId,
capability === 'image.edit' ? 'image.edit' : 'image.generate',
).models,
- video: getMediaPreferenceConfig(
+ video: getGhostMediaPreferenceConfig(
+ ghostId,
capability === 'video.edit' ? 'video.edit' : 'video.generate',
).models,
embed: getCatalogEmbedConfig().models,
diff --git a/apps/desktop/src/main/cindy-media/__tests__/invocationService.test.ts b/apps/desktop/src/main/cindy-media/__tests__/invocationService.test.ts
index 5519644be4..1b43b73e68 100644
--- a/apps/desktop/src/main/cindy-media/__tests__/invocationService.test.ts
+++ b/apps/desktop/src/main/cindy-media/__tests__/invocationService.test.ts
@@ -372,7 +372,7 @@ describe('Cindy Core media invocation state and security boundary', () => {
});
});
- it('旧调用未传 providerId 时不在同名 Provider 中静默选择来源', async () => {
+ it('旧调用未传 providerId 时同名来源优先回落 Cindy AI', async () => {
const model = {
id: 'openai/gpt-image-2',
name: 'GPT Image 2',
@@ -382,20 +382,21 @@ describe('Cindy Core media invocation state and security boundary', () => {
officialDocs: 'https://platform.openai.com/docs/guides/image-generation',
};
mocks.models.mockResolvedValue([{ ...model, providerId: 'xd' }, model]);
+ mocks.guide.mockResolvedValue(
+ resolvedGuide(operation({ mode: 'sync', media: [] })),
+ );
- await expect(
- callCindyMedia({
- action: 'prepare',
- modelId: model.id,
- capability: 'image.generate',
- }),
- ).resolves.toMatchObject({
- ok: false,
- errorCode: 'MODEL_NOT_AVAILABLE',
- retryable: false,
- message: expect.stringContaining('provider_id'),
+ const prepared = await callCindyMedia({
+ action: 'prepare',
+ modelId: model.id,
+ capability: 'image.generate',
});
- expect(mocks.guide).not.toHaveBeenCalled();
+ expect(prepared).toMatchObject({
+ ok: true,
+ provider_id: 'xd',
+ model_id: model.id,
+ });
+ expect(mocks.guide).toHaveBeenCalledWith(model.id);
expect(mocks.providerModel).not.toHaveBeenCalled();
});
diff --git a/apps/desktop/src/main/cindy-media/invocationService.ts b/apps/desktop/src/main/cindy-media/invocationService.ts
index eaf5668f79..3b8ace1411 100644
--- a/apps/desktop/src/main/cindy-media/invocationService.ts
+++ b/apps/desktop/src/main/cindy-media/invocationService.ts
@@ -1130,22 +1130,32 @@ async function prepareInvocation(
const matchingModels = models.filter(
(candidate) => candidate.id === modelId && (!providerId || candidate.providerId === providerId),
);
- if (!providerId && matchingModels.length > 1) {
- return failure(
- 'MODEL_NOT_AVAILABLE',
- '该模型同时来自多个 Provider,请从模型目录选择精确来源并在 prepare 时传入 provider_id',
- );
+ const model = providerId
+ ? matchingModels[0]
+ : (matchingModels.find((candidate) => candidate.providerId === 'xd') ??
+ matchingModels[0]);
+ if (!providerId && model && matchingModels.length !== 1) {
+ log.warn('legacy media prepare resolved to available model', {
+ requestedModelId: modelId,
+ resolvedProviderId: model.providerId,
+ resolvedModelId: model.id,
+ matchingProviderCount: matchingModels.length,
+ });
}
- const model = matchingModels[0];
if (!model) {
return failure('MODEL_NOT_AVAILABLE', '该模型或指定 Provider 当前不可见,或不是请求的媒体类型');
}
+ const resolvedModelId = model.id;
let preparedGuide: PreparedMediaInvocationGuide;
if (model.providerId !== 'xd') {
if (capability !== 'image.generate' && capability !== 'image.edit') {
return failure('CAPABILITY_NOT_SUPPORTED', '该第三方 Provider 当前不支持请求的媒体能力');
}
- const providerModel = resolveProviderMediaModel(model.providerId, modelId, capability);
+ const providerModel = resolveProviderMediaModel(
+ model.providerId,
+ resolvedModelId,
+ capability,
+ );
if (!providerModel) {
return failure('MODEL_NOT_AVAILABLE', '该第三方媒体模型或执行来源当前不可用');
}
@@ -1153,7 +1163,7 @@ async function prepareInvocation(
} else {
let resolvedGuide: ResolvedMediaInvocationGuide;
try {
- resolvedGuide = await fetchMediaInvocationGuide(modelId);
+ resolvedGuide = await fetchMediaInvocationGuide(resolvedModelId);
assertAuthScope(scope);
} catch (error) {
if (error instanceof ServerApiError && error.code === 'MEDIA_INVOCATION_GUIDE_NOT_FOUND') {
@@ -1164,7 +1174,7 @@ async function prepareInvocation(
}
if (error instanceof MediaGuideCompatibilityError) {
log.warn('media Guide rejected by current client', {
- modelId,
+ modelId: resolvedModelId,
code: error.code,
detail: error.detail,
});
@@ -1199,7 +1209,7 @@ async function prepareInvocation(
void _operations;
preparedGuide = {
// Guide 查询键不参与调用身份;配置、持久化和 Gateway 请求始终使用完整 modelId。
- modelId,
+ modelId: resolvedModelId,
...guideProtocol,
...operation,
};
@@ -1227,7 +1237,7 @@ async function prepareInvocation(
status: 'prepared',
invocation_id: id,
provider_id: model.providerId,
- model_id: modelId,
+ model_id: resolvedModelId,
model_name: model.name ?? model.id,
capability,
guide_revision: preparedGuide.revision,