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): 回收队列编辑旧附件

DavidShenc4ea97bb7383
diff --git a/apps/desktop/src/renderer/__tests__/pendingQueueDefer.test.ts b/apps/desktop/src/renderer/__tests__/pendingQueueDefer.test.ts
index 36fe2c35d8..d8272903e6 100644
--- a/apps/desktop/src/renderer/__tests__/pendingQueueDefer.test.ts
+++ b/apps/desktop/src/renderer/__tests__/pendingQueueDefer.test.ts
@@ -88,6 +88,8 @@ const cacheMediaForSession = vi.fn(async () => ({
mimeType: 'image/png',
size: 10,
}));
+const cleanupCachedImages = vi.fn(async () => undefined);
+const cleanupStagedChatAttachments = vi.fn(async () => undefined);
const input = {
getProjection: vi.fn(async (sessionId: string) => projection(sessionId)),
@@ -208,6 +210,8 @@ function installElectronBridge(): void {
},
deviceLink: { invoke: remoteInvoke },
cacheMediaForSession,
+ cleanupCachedImages,
+ cleanupStagedChatAttachments,
};
}
@@ -435,6 +439,51 @@ describe('renderer input queue facade', () => {
);
});
+ it('cleans original queue attachment artifacts only after an accepted replacement', async () => {
+ const sid = `cleanup-content-${Math.random().toString(36).slice(2, 8)}`;
+ const item = queued('q-cleanup-content', 'keep text');
+ item.files = [
+ {
+ id: 'old-image',
+ name: 'old.png',
+ path: 'C:\\images\\old.png',
+ ext: 'png',
+ size: 1,
+ category: 'image',
+ mimeType: 'image/png',
+ url: 'xdt-image://session/old.png',
+ },
+ {
+ id: 'old-staged-file',
+ name: 'old.exe',
+ path: 'C:\\cache\\old.bin',
+ ext: 'exe',
+ size: 1,
+ category: 'file',
+ mimeType: 'application/octet-stream',
+ },
+ ];
+
+ makerChatStore.initGlobalListeners();
+ projectionHandler?.(projection(sid, { pendingQueue: [item] }));
+
+ const saved = await makerChatStore.updateQueueItemContent(sid, item.clientId, {
+ content: {
+ text: item.text,
+ mentions: [],
+ hasQuotes: false,
+ agentReferences: [],
+ pastedTextRanges: [],
+ slashCommandRanges: [],
+ },
+ files: [],
+ });
+
+ expect(saved).toBe(true);
+ expect(cleanupCachedImages).toHaveBeenCalledWith(['xdt-image://session/old.png']);
+ expect(cleanupStagedChatAttachments).toHaveBeenCalledWith(['C:\\cache\\old.bin']);
+ });
+
it('uses the editor mentions when visible text is unchanged', async () => {
const sid = 'same-text-mentions-' + Math.random().toString(36).slice(2, 8);
const item = queued('q-same-text-mentions', 'keep @src/app.ts');
diff --git a/apps/desktop/src/renderer/lib/__tests__/queueComposerEdit.test.ts b/apps/desktop/src/renderer/lib/__tests__/queueComposerEdit.test.ts
index fb7b78fa2f..cbdb7b5c4b 100644
--- a/apps/desktop/src/renderer/lib/__tests__/queueComposerEdit.test.ts
+++ b/apps/desktop/src/renderer/lib/__tests__/queueComposerEdit.test.ts
@@ -133,6 +133,26 @@ describe('queueMessageToComposerEditDraft', () => {
]),
);
});
+
+ it('prefers the longest mention when directory and file references start together', () => {
+ const entry = queuedMessage();
+ entry.text = '@src/app.ts';
+ entry.chatMessage.content = entry.text;
+ entry.mentions = [
+ { type: 'dir', name: 'src', path: 'src' },
+ { type: 'file', name: 'app.ts', path: 'src/app.ts' },
+ ];
+
+ const prepared = queueMessageToComposerEditDraft('session-1', entry);
+ const inline = prepared.draft.text?.content?.[0]?.content ?? [];
+
+ expect(inline).toEqual([
+ expect.objectContaining({
+ type: 'mentionChip',
+ attrs: expect.objectContaining({ kind: 'file', path: 'src/app.ts', label: 'app.ts' }),
+ }),
+ ]);
+ });
});
describe('isQueueComposerEditCurrent', () => {
diff --git a/apps/desktop/src/renderer/lib/makerChatStore.ts b/apps/desktop/src/renderer/lib/makerChatStore.ts
index 245c4fbf49..0066fb79f6 100644
--- a/apps/desktop/src/renderer/lib/makerChatStore.ts
+++ b/apps/desktop/src/renderer/lib/makerChatStore.ts
@@ -172,6 +172,7 @@ import {
resetRemoteDataOwnerPushFence,
} from '@/lib/remoteDataOwnerPushFence';
import { buildUserMessageAttachmentPayload } from '@/lib/messageAttachmentPayload';
+import { cleanupStagedChatAttachmentFiles } from '@/lib/chatAttachmentStageCleanup';
import {
parseIssueEnvHarness,
parseIssueEnvModelId,
@@ -13659,6 +13660,29 @@ function cleanupUnacceptedQueueEditMaterialization(
});
}
+function cleanupAcceptedQueueEditReplacements(
+ originalFiles: 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),
+ );
+ if (removedUrls.length > 0) {
+ void window.electronAPI.cleanupCachedImages(removedUrls).catch((error: unknown) => {
+ log.warn('cleanup replaced queue edit images failed:', error);
+ });
+ }
+
+ const acceptedPaths = new Set(acceptedFiles.map((file) => file.path));
+ cleanupStagedChatAttachmentFiles(
+ originalFiles.filter((file) => !acceptedPaths.has(file.path)),
+ );
+}
+
function queueEditFilesMatch(
left: readonly AttachedFile[] | undefined,
right: readonly AttachedFile[] | undefined,
@@ -13802,7 +13826,11 @@ async function updateQueueItemContent(
}
const accepted = projection.pendingQueue.find((item) => item.clientId === clientId);
const updated = queuedContentProjectionMatches(accepted, replacement);
- if (!updated) cleanupUnacceptedQueueEditMaterialization(files, preparedFiles);
+ if (updated && accepted) {
+ cleanupAcceptedQueueEditReplacements(queued.files ?? [], accepted.files ?? []);
+ } else {
+ cleanupUnacceptedQueueEditMaterialization(files, preparedFiles);
+ }
return updated;
}
diff --git a/apps/desktop/src/renderer/lib/queueComposerEdit.ts b/apps/desktop/src/renderer/lib/queueComposerEdit.ts
index 782e12cb04..daa3ace550 100644
--- a/apps/desktop/src/renderer/lib/queueComposerEdit.ts
+++ b/apps/desktop/src/renderer/lib/queueComposerEdit.ts
@@ -195,7 +195,7 @@ function collectInlineReplacements(entry: QueuedMessage, text: string): InlineRe
}
replacements.sort((left, right) =>
- left.start - right.start || left.priority - right.priority || left.end - right.end,
+ left.start - right.start || left.priority - right.priority || right.end - left.end,
);
const accepted: InlineReplacement[] = [];
let previousEnd = 0;