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(desktop): terminate invalid async media results

liugaowei614248c3772f
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 21d1357eea..1397fe6fb9 100644
--- a/apps/desktop/src/main/cindy-media/__tests__/invocationService.test.ts
+++ b/apps/desktop/src/main/cindy-media/__tests__/invocationService.test.ts
@@ -498,4 +498,53 @@ describe('Cindy Core media invocation state and security boundary', () => {
'https://gateway.example.com/video/tasks/task-1',
);
});
+
+ it('异步任务成功但媒体结果确定无效时终止 invocation,不诱导重复 poll', async () => {
+ const asyncOperation = {
+ ...operation({
+ mode: 'async',
+ taskIdPath: ['task_id'],
+ poll: {
+ method: 'GET',
+ path: '/video/tasks/{taskId}',
+ statusPath: ['status'],
+ successValues: ['succeeded'],
+ failureValues: ['failed'],
+ recommendedIntervalMs: 10,
+ timeoutMs: 5_000,
+ maxResponseBytes: 1_048_576,
+ media: [{ path: ['video'], encoding: 'base64', kind: 'video' }],
+ },
+ }, '/video/tasks'),
+ capability: 'video.generate',
+ };
+ mocks.models.mockResolvedValue([
+ { id: 'image-model', name: 'Video Model', mode: 'video_generation' },
+ ]);
+ mocks.guide.mockResolvedValue(resolvedGuide(asyncOperation));
+ mocks.outboundFetch
+ .mockResolvedValueOnce(new Response(JSON.stringify({ task_id: 'task-2' }), { status: 200 }))
+ .mockResolvedValueOnce(
+ new Response(JSON.stringify({ status: 'succeeded' }), { status: 200 }),
+ );
+
+ const prepared = await callCindyMedia({
+ action: 'prepare',
+ modelId: 'image-model',
+ capability: 'video.generate',
+ });
+ const invocationId = prepared.invocation_id as string;
+ await callCindyMedia({ action: 'request', invocationId, body: { content: [] } });
+
+ await expect(callCindyMedia({ action: 'poll', invocationId })).resolves.toMatchObject({
+ ok: false,
+ errorCode: 'MEDIA_RESULT_MISSING',
+ retryable: false,
+ });
+ expect(mocks.rows.get(invocationId)?.state).toBe('failed');
+ await expect(callCindyMedia({ action: 'poll', invocationId })).resolves.toMatchObject({
+ ok: false,
+ errorCode: 'INVOCATION_NOT_PENDING',
+ });
+ });
});
diff --git a/apps/desktop/src/main/cindy-media/invocationService.ts b/apps/desktop/src/main/cindy-media/invocationService.ts
index a9b0f7dc72..0658527e05 100644
--- a/apps/desktop/src/main/cindy-media/invocationService.ts
+++ b/apps/desktop/src/main/cindy-media/invocationService.ts
@@ -46,6 +46,12 @@ const MAX_MEDIA_RESULTS = 16;
const MAX_LOCAL_MEDIA_INPUTS = 16;
const MAX_LOCAL_MEDIA_INPUT_TOTAL_BYTES = 64 * 1024 * 1024;
const FORBIDDEN_PATH_SEGMENTS = new Set(['__proto__', 'prototype', 'constructor']);
+const TERMINAL_MEDIA_RESULT_ERRORS = new Set([
+ 'MEDIA_RESULT_INVALID',
+ 'MEDIA_RESULT_MISSING',
+ 'MEDIA_RESULT_TOO_LARGE',
+ 'RESPONSE_TOO_LARGE',
+]);
interface MediaConnection {
baseUrl: string;
@@ -783,7 +789,24 @@ async function pollInvocation(invocation: StoredMediaInvocation): Promise<Record
const rawStatus = valuesAtPath(response, guide.statusPath)[0];
const status = typeof rawStatus === 'string' ? rawStatus : '';
if (guide.successValues.includes(status)) {
- const media = await materializeResults(response, guide.media);
+ let media: Record<string, unknown>;
+ try {
+ media = await materializeResults(response, guide.media);
+ } catch (error) {
+ if (
+ error instanceof MediaInvocationError &&
+ TERMINAL_MEDIA_RESULT_ERRORS.has(error.code)
+ ) {
+ await transitionMediaInvocation({
+ id: invocation.id,
+ owner: invocation.owner,
+ from: 'pending',
+ to: 'failed',
+ });
+ return failure(error.code, error.message);
+ }
+ throw error;
+ }
await transitionMediaInvocation({
id: invocation.id,
owner: invocation.owner,