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): 修正远程队列附件保存确认

DavidShen278ce0a89e81
diff --git a/apps/desktop/src/renderer/__tests__/pendingQueueDefer.test.ts b/apps/desktop/src/renderer/__tests__/pendingQueueDefer.test.ts
index d8272903e6..c8751b0d31 100644
--- a/apps/desktop/src/renderer/__tests__/pendingQueueDefer.test.ts
+++ b/apps/desktop/src/renderer/__tests__/pendingQueueDefer.test.ts
@@ -12,12 +12,25 @@
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
-import type {
- AgentInputProjection,
- AgentInputQueuedMessage,
-} from '../../shared/agentInputQueue';
+import type { AgentInputProjection, AgentInputQueuedMessage } from '../../shared/agentInputQueue';
+import type { AttachedFile } from '@/lib/fileTypes';
import { remoteProjectsStore } from '@/features/device-link/remoteProjectsStore';
-import { __resetStickySessionOriginForTest, getStickySessionDeviceId } from '@/features/device-link/stickySessionOrigin';
+import {
+ __resetStickySessionOriginForTest,
+ getStickySessionDeviceId,
+} from '@/features/device-link/stickySessionOrigin';
+
+const annotationBurnInMocks = vi.hoisted(() => ({
+ materialize: vi.fn(
+ async (files: readonly AttachedFile[] | undefined): Promise<AttachedFile[] | undefined> =>
+ files ? [...files] : undefined,
+ ),
+}));
+
+vi.mock('@/lib/annotationBurnIn', async (importOriginal) => ({
+ ...(await importOriginal<typeof import('@/lib/annotationBurnIn')>()),
+ materializeAnnotatedAttachmentsForSend: annotationBurnInMocks.materialize,
+}));
vi.mock('@/lib/messageService', () => ({
list: vi.fn(async () => ({ items: [], hasMore: false, oldestId: null })),
@@ -223,6 +236,9 @@ const flushPromises = async () => {
beforeEach(() => {
vi.clearAllMocks();
+ annotationBurnInMocks.materialize.mockImplementation(async (files) =>
+ files ? [...files] : undefined,
+ );
remoteProjectsStore.clear();
__resetStickySessionOriginForTest();
makerChatStore.__teardownGlobalListeners();
@@ -439,6 +455,154 @@ describe('renderer input queue facade', () => {
);
});
+ it('accepts queue attachment projections with transport-only field changes', async () => {
+ const sid = `transport-fields-${Math.random().toString(36).slice(2, 8)}`;
+ const item = queued('q-transport-fields', 'keep transport fields out of intent');
+ makerChatStore.initGlobalListeners();
+ projectionHandler?.(projection(sid, { pendingQueue: [item] }));
+ input.updateContent.mockImplementationOnce(
+ async (sessionId: string, _clientId: string, replacement: AgentInputQueuedMessage) =>
+ projection(sessionId, {
+ pendingQueue: [
+ {
+ ...replacement,
+ files: (replacement.files ?? []).map((file) => ({
+ ...file,
+ path: 'C:\\remote-cache\\materialized.png',
+ url: 'xdt-image://remote/materialized.png',
+ size: 87,
+ sha256: 'a'.repeat(64),
+ })),
+ },
+ ],
+ }),
+ );
+
+ const saved = await makerChatStore.updateQueueItemContent(sid, item.clientId, {
+ content: {
+ text: item.text,
+ mentions: [],
+ hasQuotes: false,
+ agentReferences: [],
+ pastedTextRanges: [],
+ slashCommandRanges: [],
+ },
+ files: [
+ {
+ id: 'transport-image',
+ name: 'transport.png',
+ path: 'C:\\images\\transport.png',
+ ext: 'png',
+ size: 123,
+ category: 'image',
+ mimeType: 'image/png',
+ url: 'xdt-image://session/transport.png',
+ },
+ ],
+ });
+
+ expect(saved).toBe(true);
+ });
+
+ it('recycles a newly annotated remote queue edit source only after acceptance', async () => {
+ const sid = `remote-annotation-${Math.random().toString(36).slice(2, 8)}`;
+ const deviceId = 'dev-remote-annotation';
+ const item = queued('q-remote-annotation', 'annotated remote edit');
+ const sourceUrl = 'xdt-image://session/annotation-source.png';
+ const burnedUrl = 'xdt-image://session/annotation-burned.png';
+ const source: AttachedFile = {
+ id: 'remote-annotation',
+ name: 'annotation-source.png',
+ path: 'C:\\images\\annotation-source.png',
+ ext: 'png',
+ size: 123,
+ category: 'image',
+ mimeType: 'image/png',
+ url: sourceUrl,
+ annotationStrokes: [{ points: [{ x: 0.1, y: 0.2 }] }],
+ };
+ makerChatStore.initGlobalListeners();
+ projectionHandler?.(projection(sid, { pendingQueue: [item] }));
+ remoteProjectsStore.setDeviceSessions(deviceId, 'Remote Mac', [{ id: sid } as never]);
+ annotationBurnInMocks.materialize.mockResolvedValueOnce([
+ {
+ ...source,
+ url: burnedUrl,
+ annotated: true,
+ annotationStrokes: undefined,
+ annotationSourceUrl: undefined,
+ },
+ ]);
+ remoteInvoke.mockImplementation(async (_deviceId, channel, args) => {
+ expect(channel).toBe('maker:input:update-content');
+ const replacement = args[2] as AgentInputQueuedMessage;
+ return projection(sid, { pendingQueue: [replacement] });
+ });
+
+ const saved = await makerChatStore.updateQueueItemContent(sid, item.clientId, {
+ content: {
+ text: item.text,
+ mentions: [],
+ hasQuotes: false,
+ agentReferences: [],
+ pastedTextRanges: [],
+ slashCommandRanges: [],
+ },
+ files: [source],
+ });
+
+ expect(saved).toBe(true);
+ expect(cleanupCachedImages).toHaveBeenCalledWith([sourceUrl]);
+ });
+
+ it('keeps an annotated remote queue edit source when the replacement is rejected', async () => {
+ const sid = `remote-annotation-rejected-${Math.random().toString(36).slice(2, 8)}`;
+ const deviceId = 'dev-remote-annotation-rejected';
+ const item = queued('q-remote-annotation-rejected', 'annotated remote edit');
+ const sourceUrl = 'xdt-image://session/rejected-annotation-source.png';
+ const burnedUrl = 'xdt-image://session/rejected-annotation-burned.png';
+ const source: AttachedFile = {
+ id: 'remote-annotation-rejected',
+ name: 'rejected-annotation-source.png',
+ path: 'C:\\images\\rejected-annotation-source.png',
+ ext: 'png',
+ size: 123,
+ category: 'image',
+ mimeType: 'image/png',
+ url: sourceUrl,
+ annotationStrokes: [{ points: [{ x: 0.1, y: 0.2 }] }],
+ };
+ makerChatStore.initGlobalListeners();
+ projectionHandler?.(projection(sid, { pendingQueue: [item] }));
+ remoteProjectsStore.setDeviceSessions(deviceId, 'Remote Mac', [{ id: sid } as never]);
+ annotationBurnInMocks.materialize.mockResolvedValueOnce([
+ {
+ ...source,
+ url: burnedUrl,
+ annotated: true,
+ annotationStrokes: undefined,
+ annotationSourceUrl: undefined,
+ },
+ ]);
+ remoteInvoke.mockResolvedValue(projection(sid, { pendingQueue: [item] }));
+
+ const saved = await makerChatStore.updateQueueItemContent(sid, item.clientId, {
+ content: {
+ text: item.text,
+ mentions: [],
+ hasQuotes: false,
+ agentReferences: [],
+ pastedTextRanges: [],
+ slashCommandRanges: [],
+ },
+ files: [source],
+ });
+
+ expect(saved).toBe(false);
+ expect(cleanupCachedImages).toHaveBeenCalledTimes(1);
+ expect(cleanupCachedImages).toHaveBeenCalledWith([burnedUrl]);
+ });
+
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');
diff --git a/apps/desktop/src/renderer/lib/makerChatStore.ts b/apps/desktop/src/renderer/lib/makerChatStore.ts
index 0066fb79f6..dc8eb4061f 100644
--- a/apps/desktop/src/renderer/lib/makerChatStore.ts
+++ b/apps/desktop/src/renderer/lib/makerChatStore.ts
@@ -13631,7 +13631,18 @@ function queuedContentProjectionMatches(
): boolean {
if (!accepted || accepted.text !== replacement.text) return false;
const stableFiles = (files: QueuedMessage['files']) =>
- (files ?? []).map(({ path: _, url: __, pathOrigin: ___, ...file }) => file);
+ (files ?? []).map((file) => ({
+ id: file.id,
+ name: file.name,
+ ext: file.ext,
+ category: file.category,
+ mimeType: file.mimeType,
+ originalName: file.originalName ?? file.name,
+ base64: file.base64,
+ textContent: file.textContent,
+ truncated: file.truncated,
+ annotated: file.annotated,
+ }));
return (
JSON.stringify(stableFiles(accepted.files)) ===
JSON.stringify(stableFiles(replacement.files)) &&
@@ -13683,6 +13694,43 @@ function cleanupAcceptedQueueEditReplacements(
);
}
+function cleanupAcceptedQueueEditMaterializationSources(
+ queuedFiles: readonly AttachedFile[],
+ editedFiles: readonly AttachedFile[],
+ preparedFiles: readonly AttachedFile[],
+ acceptedFiles: readonly AttachedFile[],
+): void {
+ const queuedUrls = new Set(queuedFiles.map((file) => file.url).filter(Boolean));
+ const retainedUrls = new Set(
+ [...preparedFiles, ...acceptedFiles].flatMap((file) =>
+ [file.url, file.annotationSourceUrl].filter((url): url is string => Boolean(url)),
+ ),
+ );
+ const preparedById = new Map(preparedFiles.map((file) => [file.id, file]));
+ const removedSourceUrls = [
+ ...new Set(
+ editedFiles.flatMap((file) => {
+ const prepared = preparedById.get(file.id);
+ if (
+ file.cacheUrlShared === true ||
+ !file.url?.startsWith('xdt-image://') ||
+ queuedUrls.has(file.url) ||
+ retainedUrls.has(file.url) ||
+ prepared?.annotated !== true ||
+ prepared.url === file.url
+ ) {
+ return [];
+ }
+ return [file.url];
+ }),
+ ),
+ ];
+ if (removedSourceUrls.length === 0) return;
+ void window.electronAPI.cleanupCachedImages(removedSourceUrls).catch((error: unknown) => {
+ log.warn('cleanup accepted queue edit annotation sources failed:', error);
+ });
+}
+
function queueEditFilesMatch(
left: readonly AttachedFile[] | undefined,
right: readonly AttachedFile[] | undefined,
@@ -13828,6 +13876,12 @@ async function updateQueueItemContent(
const updated = queuedContentProjectionMatches(accepted, replacement);
if (updated && accepted) {
cleanupAcceptedQueueEditReplacements(queued.files ?? [], accepted.files ?? []);
+ cleanupAcceptedQueueEditMaterializationSources(
+ queued.files ?? [],
+ files,
+ preparedFiles,
+ accepted.files ?? [],
+ );
} else {
cleanupUnacceptedQueueEditMaterialization(files, preparedFiles);
}