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): 保留队列图片标注编辑态

DavidShen89f2a03ed453
diff --git a/apps/desktop/src/renderer/__tests__/pendingQueueDefer.test.ts b/apps/desktop/src/renderer/__tests__/pendingQueueDefer.test.ts
index c8751b0d31..3c70bcf86d 100644
--- a/apps/desktop/src/renderer/__tests__/pendingQueueDefer.test.ts
+++ b/apps/desktop/src/renderer/__tests__/pendingQueueDefer.test.ts
@@ -80,7 +80,7 @@ vi.mock('@/lib/composerDraftStore', () => ({
}),
}));
-import { makerChatStore } from '@/lib/makerChatStore';
+import { makerChatStore, type QueuedMessage } from '@/lib/makerChatStore';
const MODEL = 'claude-opus-4-7';
const EFFORT = 'medium';
@@ -160,7 +160,7 @@ function projection(
};
}
-function queued(clientId: string, text: string): AgentInputQueuedMessage {
+function queued(clientId: string, text: string): QueuedMessage {
return {
clientId,
text,
@@ -616,6 +616,7 @@ describe('renderer input queue facade', () => {
category: 'image',
mimeType: 'image/png',
url: 'xdt-image://session/old.png',
+ annotated: true,
},
{
id: 'old-staged-file',
@@ -627,6 +628,13 @@ describe('renderer input queue facade', () => {
mimeType: 'application/octet-stream',
},
];
+ item.chatMessage.retryFiles = [
+ {
+ ...item.files[0],
+ annotationSourceUrl: 'xdt-image://session/old-source.png',
+ annotationStrokes: [{ points: [{ x: 0.25, y: 0.75 }] }],
+ },
+ ];
makerChatStore.initGlobalListeners();
projectionHandler?.(projection(sid, { pendingQueue: [item] }));
@@ -644,7 +652,10 @@ describe('renderer input queue facade', () => {
});
expect(saved).toBe(true);
- expect(cleanupCachedImages).toHaveBeenCalledWith(['xdt-image://session/old.png']);
+ expect(cleanupCachedImages).toHaveBeenCalledWith([
+ 'xdt-image://session/old.png',
+ 'xdt-image://session/old-source.png',
+ ]);
expect(cleanupStagedChatAttachments).toHaveBeenCalledWith(['C:\\cache\\old.bin']);
});
diff --git a/apps/desktop/src/renderer/lib/__tests__/queueComposerEdit.test.ts b/apps/desktop/src/renderer/lib/__tests__/queueComposerEdit.test.ts
index cbdb7b5c4b..86f0abda91 100644
--- a/apps/desktop/src/renderer/lib/__tests__/queueComposerEdit.test.ts
+++ b/apps/desktop/src/renderer/lib/__tests__/queueComposerEdit.test.ts
@@ -72,6 +72,43 @@ describe('queueMessageToComposerEditDraft', () => {
expect(prepared.draft.attachments).toHaveLength(1);
});
+ it('restores queued annotations as an editable source image plus vector strokes', () => {
+ const entry = queuedMessage();
+ const burnedUrl = 'xdt-image://session/annotated.png';
+ const sourceUrl = 'xdt-image://session/source.jpg';
+ const strokes = [{ points: [{ x: 0.25, y: 0.75 }] }];
+ entry.files = [
+ {
+ ...entry.files![0],
+ path: 'C:\\images\\annotated.png',
+ url: burnedUrl,
+ annotated: true,
+ },
+ ];
+ entry.chatMessage.retryFiles = [
+ {
+ ...entry.files[0],
+ annotationSourceUrl: sourceUrl,
+ annotationStrokes: strokes,
+ },
+ ];
+
+ const prepared = queueMessageToComposerEditDraft('session-1', entry);
+
+ expect(prepared.draft.attachments[0]).toMatchObject({
+ id: 'existing-image',
+ path: sourceUrl,
+ url: sourceUrl,
+ ext: '.jpg',
+ mimeType: 'image/jpeg',
+ annotationStrokes: strokes,
+ cacheUrlShared: true,
+ stagedPathShared: true,
+ });
+ expect(prepared.draft.attachments[0]).not.toHaveProperty('annotated');
+ expect(prepared.draft.attachments[0]?.annotationStrokes).not.toBe(strokes);
+ });
+
it('restores structured references, file mentions and pasted text chips', () => {
const entry = queuedMessage();
const messageHref = 'cindy://session/source?message=message-1';
diff --git a/apps/desktop/src/renderer/lib/makerChatStore.ts b/apps/desktop/src/renderer/lib/makerChatStore.ts
index dc8eb4061f..c71601c60d 100644
--- a/apps/desktop/src/renderer/lib/makerChatStore.ts
+++ b/apps/desktop/src/renderer/lib/makerChatStore.ts
@@ -13673,19 +13673,42 @@ function cleanupUnacceptedQueueEditMaterialization(
function cleanupAcceptedQueueEditReplacements(
originalFiles: readonly AttachedFile[],
+ originalRetryFiles: readonly AttachedFile[],
+ preparedFiles: readonly AttachedFile[],
acceptedFiles: readonly AttachedFile[],
): void {
const acceptedUrls = new Set(acceptedFiles.map((file) => file.url).filter(Boolean));
- const removedUrls = originalFiles
- .map((file) => file.url)
- .filter(
- (url): url is string =>
- Boolean(url?.startsWith('xdt-image://')) && !acceptedUrls.has(url),
- );
+ const retainedUrls = new Set(
+ [...preparedFiles, ...acceptedFiles].flatMap((file) =>
+ [file.url, file.annotationSourceUrl].filter((url): url is string => Boolean(url)),
+ ),
+ );
+ const originalFilesById = new Map(originalFiles.map((file) => [file.id, file]));
+ const removedUrls = [
+ ...originalFiles
+ .map((file) => file.url)
+ .filter(
+ (url): url is string => Boolean(url?.startsWith('xdt-image://')) && !acceptedUrls.has(url),
+ ),
+ ...originalRetryFiles
+ .filter((file) => {
+ const original = originalFilesById.get(file.id);
+ return (
+ original?.path === file.path &&
+ original.url === file.url &&
+ file.cacheUrlShared !== true &&
+ Boolean(file.annotationSourceUrl?.startsWith('xdt-image://')) &&
+ !retainedUrls.has(file.annotationSourceUrl!)
+ );
+ })
+ .map((file) => file.annotationSourceUrl!),
+ ];
if (removedUrls.length > 0) {
- void window.electronAPI.cleanupCachedImages(removedUrls).catch((error: unknown) => {
- log.warn('cleanup replaced queue edit images failed:', error);
- });
+ void window.electronAPI
+ .cleanupCachedImages([...new Set(removedUrls)])
+ .catch((error: unknown) => {
+ log.warn('cleanup replaced queue edit images failed:', error);
+ });
}
const acceptedPaths = new Set(acceptedFiles.map((file) => file.path));
@@ -13875,7 +13898,12 @@ async function updateQueueItemContent(
const accepted = projection.pendingQueue.find((item) => item.clientId === clientId);
const updated = queuedContentProjectionMatches(accepted, replacement);
if (updated && accepted) {
- cleanupAcceptedQueueEditReplacements(queued.files ?? [], accepted.files ?? []);
+ cleanupAcceptedQueueEditReplacements(
+ queued.files ?? [],
+ queued.chatMessage.retryFiles ?? [],
+ preparedFiles,
+ accepted.files ?? [],
+ );
cleanupAcceptedQueueEditMaterializationSources(
queued.files ?? [],
files,
diff --git a/apps/desktop/src/renderer/lib/queueComposerEdit.ts b/apps/desktop/src/renderer/lib/queueComposerEdit.ts
index daa3ace550..e8a6fbb13b 100644
--- a/apps/desktop/src/renderer/lib/queueComposerEdit.ts
+++ b/apps/desktop/src/renderer/lib/queueComposerEdit.ts
@@ -5,7 +5,7 @@ import type { ComposerDraft } from '@/lib/composerDraftStore';
import { formatQuoteForSend, parseChatQuoteSegments, type ChatQuoteSegment } from '@/lib/chatQuotes';
import { COMPOSER_QUOTE_NODE_TYPE } from '@/lib/composerQuoteDocument';
import { normalizeComposerDocumentJSON } from '@/lib/composerListDocument';
-import type { AttachedFile } from '@/lib/fileTypes';
+import { extractExt, getMimeType, type AttachedFile } from '@/lib/fileTypes';
import type { QueuedMessage } from '@/lib/makerChatStore';
import { formatMentionRef } from '@/lib/mentionRefFormat';
import type { AgentInputMention, AgentInputReference } from '../../shared/agentInputQueue';
@@ -313,11 +313,39 @@ export function queueMessageToComposerEditDraft(
entry: QueuedMessage,
): QueueComposerEditDraft {
const text = entry.chatMessage.content ?? entry.text;
- const attachments: AttachedFile[] = (entry.files ?? []).map(({ pathOrigin: _, ...file }) => ({
- ...file,
- cacheUrlShared: true,
- stagedPathShared: true,
- }));
+ const retryFilesById = new Map(
+ (entry.chatMessage.retryFiles ?? []).map((file) => [file.id, file]),
+ );
+ const attachments: AttachedFile[] = (entry.files ?? []).map(({ pathOrigin: _, ...file }) => {
+ const retryFile = retryFilesById.get(file.id);
+ const annotationSourceUrl =
+ file.annotated === true &&
+ retryFile?.annotated === true &&
+ retryFile.path === file.path &&
+ retryFile.url === file.url &&
+ retryFile.annotationSourceUrl &&
+ retryFile.annotationStrokes?.length
+ ? retryFile.annotationSourceUrl
+ : null;
+ if (!annotationSourceUrl || !retryFile?.annotationStrokes) {
+ return { ...file, cacheUrlShared: true, stagedPathShared: true };
+ }
+ const sourceExt = extractExt(annotationSourceUrl) || file.ext;
+ const editableFile = { ...file };
+ delete editableFile.annotated;
+ return {
+ ...editableFile,
+ path: annotationSourceUrl,
+ url: annotationSourceUrl,
+ ext: sourceExt,
+ mimeType: getMimeType(sourceExt, 'image'),
+ annotationStrokes: retryFile.annotationStrokes.map((stroke) => ({
+ points: stroke.points.map((point) => ({ ...point })),
+ })),
+ cacheUrlShared: true,
+ stagedPathShared: true,
+ };
+ });
const document = structuredQueueDocument(entry, text);
return {