Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): preserve media submission outcomes
liugaowei6c351f81811b
diff --git a/apps/desktop/src/main/cindy-media/__tests__/invocationService.test.ts b/apps/desktop/src/main/cindy-media/__tests__/invocationService.test.tsindex ce2c83babc..8606d92faf 100644--- a/apps/desktop/src/main/cindy-media/__tests__/invocationService.test.ts+++ b/apps/desktop/src/main/cindy-media/__tests__/invocationService.test.ts@@ -30,6 +30,7 @@ const mocks = vi.hoisted(() => ({},dbOwnerId: 'media-user-0',transitionDbs: [] as unknown[],+ failTransitionTo: null as string | null,recover: vi.fn<(owner: string, db: unknown) => Promise<number>>(async () => 0),prune: vi.fn(async () => undefined),rows: new Map<string, Record<string, unknown>>(),@@ -138,6 +139,7 @@ vi.mock('../mediaInvocationStore.js', () => ({db: unknown,) => {mocks.transitionDbs.push(db);+ if (mocks.failTransitionTo === to) throw new Error(`transition to ${to} failed`);const row = mocks.rows.get(id);if (!row || row.owner !== owner || row.state !== from) return false;row.state = to;@@ -211,6 +213,7 @@ describe('Cindy Core media invocation state and security boundary', () => {drizzle: { owner: mocks.currentUserId },};mocks.transitionDbs.length = 0;+ mocks.failTransitionTo = null;mocks.rows.clear();mocks.models.mockReset().mockResolvedValue([{ id: 'image-model', name: 'Image Model', mode: 'image_generation' },@@ -527,6 +530,58 @@ describe('Cindy Core media invocation state and security boundary', () => {expect(mocks.outboundFetch).toHaveBeenCalledTimes(1);});+ it('建连失败发生在派发前时恢复 prepared,不把 invocation 卡在 submitting', async () => {+ const resolved = resolvedGuide(+ operation({+ mode: 'sync',+ media: [{ path: ['data'], encoding: 'base64', kind: 'image' }],+ }),+ );+ resolved.guide.connection.providerId = 'unsupported';+ mocks.guide.mockResolvedValue(resolved);++ const invocationId = await prepare();+ await expect(+ callCindyMedia({ action: 'request', invocationId, body: { prompt: 'cat' } }),+ ).resolves.toMatchObject({+ ok: false,+ errorCode: 'CONNECTION_NOT_SUPPORTED',+ outcomeKnown: true,+ retryable: false,+ });+ expect(mocks.rows.get(invocationId)?.state).toBe('prepared');+ expect(mocks.outboundFetch).not.toHaveBeenCalled();+ });++ it('上游成功后响应持久化抛错时保留 unknown,禁止自动重提', async () => {+ mocks.guide.mockResolvedValue(+ resolvedGuide(+ operation({+ mode: 'sync',+ media: [{ path: ['data'], encoding: 'base64', kind: 'image' }],+ }),+ ),+ );+ mocks.outboundFetch.mockResolvedValue(+ new Response(JSON.stringify({ data: PNG.toString('base64') }), { status: 200 }),+ );+ mocks.failTransitionTo = 'pending';++ const invocationId = await prepare();+ await expect(+ callCindyMedia({ action: 'request', invocationId, body: { prompt: 'cat' } }),+ ).resolves.toMatchObject({+ ok: false,+ errorCode: 'SUBMISSION_OUTCOME_UNKNOWN',+ outcomeKnown: false,+ });+ expect(mocks.rows.get(invocationId)?.state).toBe('unknown');+ await expect(+ callCindyMedia({ action: 'request', invocationId, body: { prompt: 'cat' } }),+ ).resolves.toMatchObject({ ok: false, errorCode: 'INVOCATION_ALREADY_USED' });+ expect(mocks.outboundFetch).toHaveBeenCalledTimes(1);+ });+it('付费提交准备参数期间账号切换时不发出上游请求', async () => {mocks.guide.mockResolvedValue(resolvedGuide(diff --git a/apps/desktop/src/main/cindy-media/invocationService.ts b/apps/desktop/src/main/cindy-media/invocationService.tsindex 67c90df84a..cccf60cbf3 100644--- a/apps/desktop/src/main/cindy-media/invocationService.ts+++ b/apps/desktop/src/main/cindy-media/invocationService.ts@@ -977,6 +977,17 @@ async function submitInvocation(}const requestBody = await prepareRequestBody(body, invocation.guide);assertAuthScope(scope, invocation.owner);+ let connection: MediaConnection;+ try {+ connection = resolveConnection(invocation.guide.connection.providerId);+ } catch (error) {+ if (error instanceof MediaInvocationError) {+ return failure(error.code, error.message, error.code === 'CONNECTION_UNAVAILABLE', {+ outcomeKnown: true,+ });+ }+ throw error;+ }const claimed = await transitionMediaInvocation({id: invocation.id,@@ -997,7 +1008,6 @@ async function submitInvocation(// claim 之后再过一次认证代次闸;通过后到 outboundFetch 发起之间没有异步让出点,// 因而不会拿切换后账号的 endpoint / key 提交旧账号的付费请求。assertAuthScope(scope, invocation.owner);- const connection = resolveConnection(invocation.guide.connection.providerId);try {const response = await dispatchRequest({connection,@@ -1013,16 +1023,35 @@ async function submitInvocation(});if (invocation.guide.response.mode === 'sync') {const responseJson = JSON.stringify(response);- const persisted = await transitionMediaInvocation(- {- id: invocation.id,- owner: invocation.owner,- from: 'submitting',- to: 'pending',- responseJson,- },- db,- );+ let persisted = false;+ try {+ persisted = await transitionMediaInvocation(+ {+ id: invocation.id,+ owner: invocation.owner,+ from: 'submitting',+ to: 'pending',+ responseJson,+ },+ db,+ );+ } catch (error) {+ log.warn('persist sync media response failed', {+ error: error instanceof Error ? error.message : String(error),+ });+ await transitionMediaInvocation(+ {+ id: invocation.id,+ owner: invocation.owner,+ from: 'submitting',+ to: 'unknown',+ },+ db,+ ).catch(() => false);+ return submissionOutcomeUnknown(+ '上游结果已生成,但本地未能保存结果;不要自动重提',+ );+ }if (!persisted) {return submissionOutcomeUnknown('上游结果已生成,但本地未能保存结果;不要自动重提',@@ -1053,16 +1082,35 @@ async function submitInvocation('上游响应没有任务 id,无法确认任务状态;不要自动重提',);}- const persisted = await transitionMediaInvocation(- {- id: invocation.id,- owner: invocation.owner,- from: 'submitting',- to: 'pending',- taskId,- },- db,- );+ let persisted = false;+ try {+ persisted = await transitionMediaInvocation(+ {+ id: invocation.id,+ owner: invocation.owner,+ from: 'submitting',+ to: 'pending',+ taskId,+ },+ db,+ );+ } catch (error) {+ log.warn('persist async media task id failed', {+ error: error instanceof Error ? error.message : String(error),+ });+ await transitionMediaInvocation(+ {+ id: invocation.id,+ owner: invocation.owner,+ from: 'submitting',+ to: 'unknown',+ },+ db,+ ).catch(() => false);+ return submissionOutcomeUnknown(+ '上游任务已创建,但本地未能保存任务 id;不要自动重提',+ );+ }if (!persisted) {return submissionOutcomeUnknown('上游任务已创建,但本地未能保存任务 id;不要自动重提',