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): 标注审查跟进——回退草稿保持同步写入、中止不留孤儿文件、降级提示后置

Dash51872903c335
diff --git a/apps/desktop/src/main/__tests__/agentInputQueue.test.ts b/apps/desktop/src/main/__tests__/agentInputQueue.test.ts
index 9d110d585d..987e2ed233 100644
--- a/apps/desktop/src/main/__tests__/agentInputQueue.test.ts
+++ b/apps/desktop/src/main/__tests__/agentInputQueue.test.ts
@@ -161,6 +161,11 @@ describe('agentInputQueue', () => {
'Note: the red freehand marks on the attached image(s) are annotations drawn by the user ' +
'to highlight the region(s) they are referring to; they are not part of the original image.',
);
+ // 编号只在本条消息内有效,说明里必须讲清楚。
+ expect(ANNOTATED_IMAGE_REGIONS_PREFIX).toBe(
+ 'Marked regions (normalized coordinates, origin top-left; ' +
+ 'images numbered in their order within this message): ',
+ );
});
it('appends marked regions indexed by image order when annotated images carry them', () => {
diff --git a/apps/desktop/src/renderer/__tests__/pendingQueueDefer.test.ts b/apps/desktop/src/renderer/__tests__/pendingQueueDefer.test.ts
index 3750766106..eb3c7cb853 100644
--- a/apps/desktop/src/renderer/__tests__/pendingQueueDefer.test.ts
+++ b/apps/desktop/src/renderer/__tests__/pendingQueueDefer.test.ts
@@ -1343,6 +1343,43 @@ describe('annotation burn-in in the renderer send / queue-edit paths', () => {
expect(errorToast).toHaveBeenCalledTimes(1);
});
+ it('does not claim "sent without annotations" when the fallback send is not accepted', async () => {
+ const sid = `annotated-send-fallback-rejected-${Math.random().toString(36).slice(2, 8)}`;
+ const warningToast = vi.spyOn(toast, 'warning');
+ (
+ annotationBurnInMocks.materialize as unknown as ReturnType<typeof vi.fn>
+ ).mockImplementationOnce(
+ async (
+ files: readonly AttachedFile[] | undefined,
+ _sessionId: string,
+ opts?: { onFallback?: (count: number) => void },
+ ) => {
+ opts?.onFallback?.(1);
+ return files ? files.map((f) => ({ ...f, annotationStrokes: undefined })) : undefined;
+ },
+ );
+ const file: AttachedFile = {
+ id: 'tray-image',
+ name: 'shot.png',
+ path: 'C:\\images\\shot.png',
+ ext: '.png',
+ size: 1,
+ category: 'image',
+ mimeType: 'image/png',
+ url: 'cindy-media://blobs/tray.png',
+ annotationStrokes: strokes,
+ };
+
+ const ok = await makerChatStore.sendMessage(sid, 'look here', MODEL, EFFORT, PERM, WD, [file], undefined, {
+ beforeEnqueue: async () => false,
+ });
+ await flushPromises();
+
+ expect(ok).toBe(false);
+ expect(input.enqueue).not.toHaveBeenCalled();
+ expect(warningToast).not.toHaveBeenCalled();
+ });
+
it('keeps the fallback (and warns) for sends that did not opt into aborting', async () => {
const sid = `annotated-send-fallback-${Math.random().toString(36).slice(2, 8)}`;
const warningToast = vi.spyOn(toast, 'warning');
diff --git a/apps/desktop/src/renderer/__tests__/rewindDraftAttachments.test.ts b/apps/desktop/src/renderer/__tests__/rewindDraftAttachments.test.ts
index 5e2efac636..19ff785cb7 100644
--- a/apps/desktop/src/renderer/__tests__/rewindDraftAttachments.test.ts
+++ b/apps/desktop/src/renderer/__tests__/rewindDraftAttachments.test.ts
@@ -14,6 +14,7 @@ import {
buildRewindDraftAttachments,
dropMissingAnnotationSources,
hasRestorableAnnotationSources,
+ startRewindSourceProbe,
} from '@/lib/rewindDraftAttachments';
describe('buildRewindDraftAttachments', () => {
@@ -160,10 +161,10 @@ describe('rewind draft attachment wiring', () => {
expect(userMessageSrc).toMatch(
/buildRewindDraftAttachments\(\{\s*images:\s*draftImages,\s*files\s*\}\)/,
);
- // 带可再编辑标注的历史图先探测原图,丢失的退回烧录图后再写草稿。
- expect(userMessageSrc).toMatch(
- /hasRestorableAnnotationSources\(images\)[\s\S]{0,120}dropMissingAnnotationSources\(images\)\.then\(saveRewindDraft\)/,
- );
+ // 原图探测在确认框打开时发起,提交时同步取结果写草稿——不存在迟到的二次写入。
+ expect(userMessageSrc).toMatch(/startRewindSourceProbe\(images\)[\s\S]{0,80}setRewindOpen\(true\)/);
+ expect(userMessageSrc).toMatch(/probe\.imagesForDraft\(\)/);
+ expect(userMessageSrc).not.toMatch(/dropMissingAnnotationSources\([^)]*\)\.then/);
expect(userMessageSrc).toMatch(
/saveComposerDraft\(sessionId,\s*\{\s*text:\s*draftText,\s*attachments:\s*draftAttachments/s,
);
@@ -290,3 +291,37 @@ describe('dropMissingAnnotationSources', () => {
).resolves.toEqual([annotated]);
});
});
+
+describe('startRewindSourceProbe (rewind draft timing)', () => {
+ const annotated = {
+ url: 'cindy-media://blobs/burned.png',
+ mimeType: 'image/png',
+ originalName: 'shot-annotated.png',
+ annotationSourceUrl: 'cindy-media://blobs/gone.png',
+ annotationStrokes: [{ points: [{ x: 0.5, y: 0.5 }] }],
+ };
+
+ it('never waits: before the probe settles the draft uses the editable images (old behavior)', async () => {
+ let settle: (exists: boolean) => void = () => {};
+ const probe = vi.fn(() => new Promise<boolean>((resolve) => { settle = resolve; }));
+ const handle = startRewindSourceProbe([annotated], probe);
+
+ // 提交时探测尚未完成:同步返回原列表,草稿照旧一次写完。
+ expect(handle.imagesForDraft()).toEqual([annotated]);
+
+ settle(false);
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ // 探测完成后(例如确认框还开着时)再提交:丢失的原图退回烧录图。
+ expect(handle.imagesForDraft()).toEqual([
+ { url: 'cindy-media://blobs/burned.png', mimeType: 'image/png', originalName: 'shot-annotated.png' },
+ ]);
+ });
+
+ it('does not probe at all when no image carries restorable annotations', () => {
+ const probe = vi.fn(async () => false);
+ const plain = [{ url: 'cindy-media://blobs/p.png', mimeType: 'image/png', originalName: 'p.png' }];
+ const handle = startRewindSourceProbe(plain, probe);
+ expect(probe).not.toHaveBeenCalled();
+ expect(handle.imagesForDraft()).toBe(plain);
+ });
+});
diff --git a/apps/desktop/src/renderer/components/chat/UserMessage.tsx b/apps/desktop/src/renderer/components/chat/UserMessage.tsx
index 13e694bf3c..c1fa100d74 100644
--- a/apps/desktop/src/renderer/components/chat/UserMessage.tsx
+++ b/apps/desktop/src/renderer/components/chat/UserMessage.tsx
@@ -58,8 +58,8 @@ import type { AgentInputReference } from '../../../shared/agentInputQueue';
import type { PersistedSessionReferenceMetadata } from '../../../shared/sessionReferenceMetadata';
import {
buildRewindDraftAttachments,
- dropMissingAnnotationSources,
- hasRestorableAnnotationSources,
+ startRewindSourceProbe,
+ type RewindSourceProbe,
} from '@/lib/rewindDraftAttachments';
import {
useAgentCapabilities,
@@ -1241,11 +1241,15 @@ export function UserMessage({
// dialog opens → preview dryRun → user confirm → commit → close, the whole
// span counts as "rewinding" for the action-bar Loader2). Reset on close.
const [rewindOpen, setRewindOpen] = useState(false);
+ // 带可再编辑标注的历史图:确认框打开时就预探测未烧录原图是否还在,提交时
+ // 同步取结果(丢失的退回烧录图),草稿仍在提交当下一次写完。
+ const rewindSourceProbeRef = useRef<RewindSourceProbe<UserImageItem> | null>(null);
const handleRewind = useCallback(() => {
if (!sessionId || !messageClientId) return;
+ rewindSourceProbeRef.current = images ? startRewindSourceProbe(images) : null;
setRewindOpen(true);
- }, [sessionId, messageClientId]);
+ }, [sessionId, messageClientId, images]);
const handleRewindCommitted = useCallback(
(session: Session) => {
@@ -1255,21 +1259,15 @@ export function UserMessage({
// reload it disappears from the list; the composer keeps the draft so
// the user can edit and re-send.
const draftText = quoteDraftDocument ?? textToTiptapDoc(bubbleBody);
- const saveRewindDraft = (draftImages: typeof images) => {
- const draftAttachments = buildRewindDraftAttachments({ images: draftImages, files });
- if (draftText || draftAttachments.length > 0) {
- saveComposerDraft(sessionId, {
- text: draftText,
- attachments: draftAttachments,
- });
- }
- };
- // 带可再编辑标注的历史图:先确认未烧录原图仍在,丢失的退回烧录图(否则
- // 草稿里是一张打不开的原图)。其余消息保持同步写草稿,时序与以往一致。
- if (images && hasRestorableAnnotationSources(images)) {
- void dropMissingAnnotationSources(images).then(saveRewindDraft);
- } else {
- saveRewindDraft(images);
+ const probe = rewindSourceProbeRef.current;
+ rewindSourceProbeRef.current = null;
+ const draftImages = probe && probe.images === images ? probe.imagesForDraft() : images;
+ const draftAttachments = buildRewindDraftAttachments({ images: draftImages, files });
+ if (draftText || draftAttachments.length > 0) {
+ saveComposerDraft(sessionId, {
+ text: draftText,
+ attachments: draftAttachments,
+ });
}
// Patch sidebar: tokens reset, sdkSessionId may have changed, bump
// updatedAt so this session sorts back to top.
diff --git a/apps/desktop/src/renderer/components/new-chat/__tests__/chatInputAnnotationAbort.test.tsx b/apps/desktop/src/renderer/components/new-chat/__tests__/chatInputAnnotationAbort.test.tsx
new file mode 100644
index 0000000000..b0dcc1b489
--- /dev/null
+++ b/apps/desktop/src/renderer/components/new-chat/__tests__/chatInputAnnotationAbort.test.tsx
@@ -0,0 +1,136 @@
+// @vitest-environment jsdom
+/**
+ * ChatInput × 标注烧录中止:已有任务的发送会请求 annotationBurnFailure: 'abort';
+ * 发送被中止(onSend 返回 false,makerChatStore 烧录失败时即如此)后,点击时的正文、
+ * 附件与矢量笔迹原样回到输入框,用户可直接重试。脚手架与 chatInputModelLoading
+ * 相同:外部宿主服务全部惰性,编辑器、输入框状态与发送派发真实运行。
+ */
+import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
+import { afterEach, beforeEach, expect, it, vi } from 'vitest';
+import type { ComponentProps, ReactNode } from 'react';
+import type { Editor } from '@tiptap/react';
+import type { ProviderView } from '@cindy/model-providers';
+import { sshNativeCodexProvider, sshModel } from '@/features/cc-agent/__tests__/sshModelFixtures';
+import type { AttachedFile } from '@/lib/fileTypes';
+import { ChatInput } from '../ChatInput';
+
+const h = vi.hoisted(() => ({ t: (key: string) => key, confirm: vi.fn(), editor: null as Editor | null, listening: false, stop: vi.fn().mockResolvedValue(undefined),
+ setModel: vi.fn(), selectModel: undefined as undefined | ((id: string) => Promise<void | boolean>), remoteProviders: [] as ProviderView[],
+ remoteStatus: 'ready' as 'ready' | 'loading' | 'error',
+}));
+
+vi.mock('react-i18next', async (original) => ({ ...await original<typeof import('react-i18next')>(), useTranslation: () => ({ t: h.t }) }));
+vi.mock('react-router-dom', () => ({ useNavigate: () => vi.fn() }));
+vi.mock('@/components/ui/confirm-dialog-provider', () => ({ useConfirmDialog: () => ({ confirm: h.confirm }) }));
+vi.mock('@/components/sidebar/SortableList', () => ({
+ SortableList: ({
+ items,
+ renderItem,
+ role,
+ ariaLabel,
+ className,
+ }: {
+ items: readonly unknown[];
+ renderItem: (item: unknown, index: number) => ReactNode;
+ role?: string;
+ ariaLabel?: string;
+ className?: string;
+ }) => (
+ <div role={role} aria-label={ariaLabel} className={className}>
+ {items.map((item, index) => (
+ <div key={index}>{renderItem(item, index)}</div>
+ ))}
+ </div>
+ ),
+}));
+vi.mock('../ModelSelector', async (original) => ({ ...await original<typeof import('../ModelSelector')>(), ModelSelector: ({ modelId, onModelChange }: { modelId: string; onModelChange: typeof h.selectModel }) => {
+ h.selectModel = onModelChange;
+ return <span data-testid="model-selector">{modelId}</span>;
+} }));
+vi.mock('@/hooks/useSshCodexProviders', () => ({ useSshCodexProviders: () => ({ providers: h.remoteProviders, status: h.remoteStatus, refresh: () => {} }) }));
+vi.mock('../ExtraDirsButton', () => ({ ExtraDirsButton: () => null }));
+vi.mock('../PermissionSelector', () => ({ PermissionSelector: () => <span data-testid="permission-selector" /> }));
+vi.mock('../NewGoalDialog', () => ({ NewGoalDialog: () => null }));
+vi.mock('../FolderPickerPopover', () => ({ FolderPickerPopover: () => null, addRecentFolder: vi.fn() }));
+vi.mock('../AtMentionPanel', () => ({ AtMentionPanel: () => null }));
+vi.mock('../SlashCommandPalette', () => ({ SlashCommandPalette: () => null }));
+vi.mock('@/voice-input/VoiceInputPointerHintLayer', () => ({ VoiceInputPointerHintLayer: ({ children }: { children: import('react').ReactNode }) => <>{children}</> }));
+vi.mock('@/voice-input/VoiceInputStatusNotice', () => ({ VoiceInputStatusNotice: () => null }));
+vi.mock('@/voice-input/useVoiceInput', () => ({ useVoiceInput: (editor: Editor | null) => {
+ h.editor = editor;
+ return { state: h.listening ? 'listening' : 'idle', isListening: h.listening, isBusy: h.listening, draftText: '', start: vi.fn(), stop: h.stop, cancel: vi.fn() };
+} }));
+vi.mock('@/hooks/useProviders', () => ({ useProviders: () => ({ providers: [], loading: false }) }));
+vi.mock('@/hooks/useDeviceProviders', () => ({ useDeviceProviders: () => ({ providers: [], loading: false, unsupported: false }) }));
+vi.mock('@/hooks/useConnectedSource', () => ({ useConnectedSource: () => ({ hasConnectedSource: true, loading: false }) }));
+vi.mock('@/hooks/useAvailableAgents', () => ({ useAvailableAgents: () => ({ agents: [], loading: false }) }));
+vi.mock('@/hooks/useAgentCapabilities', async (original) => ({ ...await original<typeof import('@/hooks/useAgentCapabilities')>(), useAgentCapabilities: () => ({ capabilities: null, loading: false }) }));
+
+const noOp = () => {};
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+const api: any = new Proxy({}, { get: (_obj, key) => {
+ if (key === 'setModel') return h.setModel;
+ if (key === 'listSync') return () => ({ ghosts: [] });
+ if (key === 'getDataSnapshot') return () => { throw new Error('test bridge unavailable'); };
+ if (key === 'setGlobalShortcut') return () => Promise.resolve({ ok: true });
+ if (key === 'platform') return 'darwin';
+ if (key === 'then') return undefined;
+ if (String(key).startsWith('on')) return () => noOp;
+ return new Proxy(() => Promise.resolve(undefined), { get: (_fn, nested) => api[nested] });
+} });
+
+const strokes = [{ points: [{ x: 0.1, y: 0.2 }, { x: 0.3, y: 0.4 }] }];
+const annotatedImage: AttachedFile = {
+ id: 'tray-annotated',
+ name: 'shot.png',
+ path: '/Users/sam/shot.png',
+ ext: '.png',
+ size: 10,
+ category: 'image',
+ mimeType: 'image/png',
+ url: 'cindy-media://blobs/tray.png',
+ annotationStrokes: strokes,
+};
+
+beforeEach(() => {
+ window.electronAPI = api;
+ vi.stubGlobal('ResizeObserver', class { observe() {} unobserve() {} disconnect() {} });
+});
+afterEach(() => { cleanup(); h.remoteProviders = []; vi.unstubAllGlobals(); vi.restoreAllMocks(); });
+
+it('restores text, attachments and strokes after an aborted annotated send', async () => {
+ h.remoteProviders = [sshNativeCodexProvider([sshModel('remote-only')])];
+ const restoreFiles = vi.fn((files: readonly AttachedFile[]) => [...files]);
+ const clearFiles = vi.fn();
+ const attachmentState: ComponentProps<typeof ChatInput>['attachmentState'] = {
+ attachments: [annotatedImage], hasAttachments: true, addFiles: vi.fn(), addClipboardImage: vi.fn(),
+ rejections: [], dismissRejection: noOp, clearRejections: noOp, addFolderPath: noOp,
+ pendingFoldersVersion: 0, consumePendingFolders: () => [], addFileMention: noOp,
+ pendingFileMentionsVersion: 0, consumePendingFileMentions: () => [],
+ removeFile: noOp, updateFile: noOp, discardFiles: noOp, clearFiles, restoreFiles,
+ };
+ // makerChatStore 在烧录失败时返回 false(见 pendingQueueDefer 的中止用例)。
+ const onSend = vi.fn().mockResolvedValue(false);
+ const view = render(
+ <ChatInput sessionId="ssh-annotated" initialWorkingDir="/workspace" runtimeAgentKind="codex"
+ vendorKey="codex" deviceLinkDeviceId={null} remoteHostId="builder" attachmentState={attachmentState}
+ hideRuntimeControls showFolderPicker={false} disableAutofocus
+ initialModel="remote-only" initialProviderId="openai" initialEffort="low" onSend={onSend} />,
+ );
+ await waitFor(() => expect(view.container.querySelector('[contenteditable]')).not.toBeNull());
+ await act(async () => { h.editor!.commands.setContent('<p>Look at the circled button</p>'); });
+
+ const send = screen.getByRole('button', { name: 'newChat.sendButton.send' }) as HTMLButtonElement;
+ await act(async () => { fireEvent.click(send); });
+ await waitFor(() => expect(onSend).toHaveBeenCalledTimes(1));
+
+ const [, , , , sentFiles, , sendOpts] = onSend.mock.calls[0];
+ expect(sendOpts).toEqual(expect.objectContaining({ annotationBurnFailure: 'abort' }));
+ expect(sentFiles).toEqual([expect.objectContaining({ id: 'tray-annotated', annotationStrokes: strokes })]);
+
+ // 恢复链路与其它发送失败共用(restoreRemoteOptimisticDraft),正文原样回来、不重复。
+ await waitFor(() => expect(h.editor!.getText().trim()).toBe('Look at the circled button'));
+ expect(clearFiles).toHaveBeenCalled();
+ const restored = restoreFiles.mock.calls.at(-1)?.[0] ?? [];
+ expect(restored).toEqual([expect.objectContaining({ id: 'tray-annotated', annotationStrokes: strokes })]);
+});
diff --git a/apps/desktop/src/renderer/i18n/locales/en/common.json b/apps/desktop/src/renderer/i18n/locales/en/common.json
index e784f0f5f6..a3d0dc796e 100644
--- a/apps/desktop/src/renderer/i18n/locales/en/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/en/common.json
@@ -9242,7 +9242,7 @@
"annotateSaveFailed": "Failed to save annotations",
"annotateRedo": "Redo",
"annotateDiscardConfirmTitle": "Discard annotation changes?",
- "annotateDiscardConfirmDescription": "Strokes you drew or undid since opening this image won't be kept.",
+ "annotateDiscardConfirmDescription": "The annotations will go back to how they were when you opened this image.",
"annotateDiscardConfirmAction": "Discard",
"annotateDiscardConfirmCancel": "Keep annotating",
"annotateBurnFailedNotSent": "Couldn't apply annotations to the image, so the message wasn't sent. Try again, or remove the annotations and send.",
diff --git a/apps/desktop/src/renderer/i18n/locales/ja/common.json b/apps/desktop/src/renderer/i18n/locales/ja/common.json
index 3c4e98256c..ee07126f52 100644
--- a/apps/desktop/src/renderer/i18n/locales/ja/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/ja/common.json
@@ -9236,7 +9236,7 @@
"annotateSaveFailed": "注釈の保存に失敗しました",
"annotateRedo": "やり直す",
"annotateDiscardConfirmTitle": "注釈の変更を破棄しますか?",
- "annotateDiscardConfirmDescription": "この画像を開いてから描いた線や取り消した線は保持されません。",
+ "annotateDiscardConfirmDescription": "注釈はこの画像を開いたときの状態に戻ります。",
"annotateDiscardConfirmAction": "破棄",
"annotateDiscardConfirmCancel": "注釈を続ける",
"annotateBurnFailedNotSent": "画像への注釈の合成に失敗したため、メッセージは送信されませんでした。再試行するか、注釈を削除してから送信してください。",
diff --git a/apps/desktop/src/renderer/i18n/locales/ko/common.json b/apps/desktop/src/renderer/i18n/locales/ko/common.json
index 9f3b8322e4..fdd1e96fbe 100644
--- a/apps/desktop/src/renderer/i18n/locales/ko/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/ko/common.json
@@ -9236,7 +9236,7 @@
"annotateSaveFailed": "주석 저장에 실패했습니다",
"annotateRedo": "다시 실행",
"annotateDiscardConfirmTitle": "주석 변경 사항을 버릴까요?",
- "annotateDiscardConfirmDescription": "이 이미지를 연 후 그리거나 실행 취소한 획은 유지되지 않습니다.",
+ "annotateDiscardConfirmDescription": "주석이 이 이미지를 열었을 때의 상태로 돌아갑니다.",
"annotateDiscardConfirmAction": "버리기",
"annotateDiscardConfirmCancel": "계속 주석 달기",
"annotateBurnFailedNotSent": "이미지에 주석을 합성하지 못해 메시지를 보내지 않았습니다. 다시 시도하거나 주석을 제거한 후 보내세요.",
diff --git a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
index 2b9073c00c..b166dafb38 100644
--- a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
@@ -9236,7 +9236,7 @@
"annotateSaveFailed": "保存标注失败",
"annotateRedo": "重做",
"annotateDiscardConfirmTitle": "放弃本次标注?",
- "annotateDiscardConfirmDescription": "打开后新画或撤销的笔迹不会保留。",
+ "annotateDiscardConfirmDescription": "将恢复到打开时的标注。",
"annotateDiscardConfirmAction": "放弃",
"annotateDiscardConfirmCancel": "继续标注",
"annotateBurnFailedNotSent": "标注图片合成失败,消息未发送。可重试,或移除标注后发送。",
diff --git a/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json b/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json
index 0e90bf6884..b70199f246 100644
--- a/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json
@@ -9235,7 +9235,7 @@
"annotateSaveFailed": "儲存標註失敗",
"annotateRedo": "重做",
"annotateDiscardConfirmTitle": "放棄本次標註?",
- "annotateDiscardConfirmDescription": "開啟後新畫或撤銷的筆跡不會保留。",
+ "annotateDiscardConfirmDescription": "將恢復到開啟時的標註。",
"annotateDiscardConfirmAction": "放棄",
"annotateDiscardConfirmCancel": "繼續標註",
"annotateBurnFailedNotSent": "標註圖片合成失敗,訊息未傳送。可重試,或移除標註後傳送。",
diff --git a/apps/desktop/src/renderer/lib/__tests__/annotationBurnInMaterialize.test.ts b/apps/desktop/src/renderer/lib/__tests__/annotationBurnInMaterialize.test.ts
index b8f26a53fe..b14df9f174 100644
--- a/apps/desktop/src/renderer/lib/__tests__/annotationBurnInMaterialize.test.ts
+++ b/apps/desktop/src/renderer/lib/__tests__/annotationBurnInMaterialize.test.ts
@@ -39,9 +39,11 @@ const api = {
readCachedImageAsBase64: vi.fn(),
cacheImageFromBuffer: vi.fn(),
cacheMediaForSession: vi.fn(),
+ cleanupCachedImages: vi.fn(),
};
beforeEach(() => {
+ api.cleanupCachedImages.mockResolvedValue(undefined);
api.readCachedImageAsBase64.mockResolvedValue({ base64: 'AAAA', mimeType: 'image/png' });
api.cacheImageFromBuffer.mockResolvedValue({
url: 'cindy-media://blobs/burned.png',
@@ -193,6 +195,58 @@ describe('materializeAnnotatedAttachmentsForSend', () => {
expect(api.cacheImageFromBuffer).not.toHaveBeenCalled();
});
+ it('abort mode burns the whole batch first: no cache writes or private copies if any burn fails', async () => {
+ api.readCachedImageAsBase64.mockImplementation(async ({ url }: { url: string }) => {
+ if (url.includes('broken')) throw new Error('decode failed');
+ return { base64: 'AAAA', mimeType: 'image/png' };
+ });
+ const files = [
+ imageFile({ id: 'ok', annotationStrokes: strokes }),
+ imageFile({ id: 'bad', url: 'cindy-media://blobs/broken.png', annotationStrokes: strokes }),
+ imageFile({ id: 'shared', url: 'cindy-media://blobs/history.png', cacheUrlShared: true }),
+ ];
+
+ await expect(
+ materializeAnnotatedAttachmentsForSend(files, 's1', { burnFailure: 'abort' }),
+ ).rejects.toBeInstanceOf(AnnotationBurnInError);
+ // 失败图片之外的图也已烧完(allSettled),但整批中止前不写媒体仓、不私有化。
+ expect(api.readCachedImageAsBase64).toHaveBeenCalledTimes(2);
+ expect(api.cacheImageFromBuffer).not.toHaveBeenCalled();
+ expect(api.cacheMediaForSession).not.toHaveBeenCalled();
+ });
+
+ it('abort mode cleans up files it already created when a later persist step fails', async () => {
+ api.cacheImageFromBuffer
+ .mockResolvedValueOnce({ url: 'cindy-media://blobs/burned-a.png', filename: 'a.png' })
+ .mockRejectedValueOnce(new Error('disk full'));
+ const files = [
+ imageFile({ id: 'a', annotationStrokes: strokes }),
+ imageFile({ id: 'b', annotationStrokes: strokes }),
+ ];
+
+ await expect(
+ materializeAnnotatedAttachmentsForSend(files, 's1', { burnFailure: 'abort' }),
+ ).rejects.toBeInstanceOf(AnnotationBurnInError);
+ await Promise.resolve();
+ await Promise.resolve();
+ expect(api.cleanupCachedImages).toHaveBeenCalledWith(['cindy-media://blobs/burned-a.png']);
+ });
+
+ it('abort mode succeeds like fallback mode when every image burns', async () => {
+ const result =
+ (await materializeAnnotatedAttachmentsForSend(
+ [
+ imageFile({ id: 'a', annotationStrokes: strokes }),
+ imageFile({ id: 'shared', url: 'cindy-media://blobs/history.png', cacheUrlShared: true }),
+ ],
+ 's1',
+ { burnFailure: 'abort' },
+ )) ?? [];
+ expect(result[0]).toMatchObject({ annotated: true, url: 'cindy-media://blobs/burned.png' });
+ expect(result[1]).toMatchObject({ url: 'cindy-media://blobs/private.png' });
+ expect(api.cleanupCachedImages).not.toHaveBeenCalled();
+ });
+
it('privatizes a shared history image whose strokes were all removed', async () => {
const [result] =
(await materializeAnnotatedAttachmentsForSend(
diff --git a/apps/desktop/src/renderer/lib/annotationBurnIn.ts b/apps/desktop/src/renderer/lib/annotationBurnIn.ts
index f494c1346d..21ee12216e 100644
--- a/apps/desktop/src/renderer/lib/annotationBurnIn.ts
+++ b/apps/desktop/src/renderer/lib/annotationBurnIn.ts
@@ -255,7 +255,9 @@ export async function materializeAnnotatedAttachmentsForSend(
if (!needsAnnotationMaterialize(files)) {
return files ? [...files] : undefined;
}
- const abortOnFailure = opts?.burnFailure === 'abort';
+ if (opts?.burnFailure === 'abort') {
+ return materializeOrAbort(files!, sessionId, opts);
+ }
let fallbackCount = 0;
const materialized = await Promise.all(
files!.map(async (f): Promise<AttachedFile> => {
@@ -273,58 +275,10 @@ export async function materializeAnnotatedAttachmentsForSend(
return f;
}
try {
- const source = f.url
- ? await loadImageSourceBase64(f.url)
- : f.base64
- ? { base64: f.base64, mimeType: f.mimeType }
- : null;
- if (!source) return f;
- const { blob, mimeType } = await burnInAnnotations(source, strokes);
- const ext = mimeType === 'image/jpeg' ? '.jpg' : '.png';
- const name = annotatedFileName(f.originalName ?? f.name, ext, Date.now());
- const regions = summarizeAnnotationRegions(strokes);
- const annotationRegions = regions.length > 0 ? { annotationRegions: regions } : {};
- if (f.url) {
- const cached = await window.electronAPI.cacheImageFromBuffer({
- sessionId,
- buffer: new Uint8Array(await blob.arrayBuffer()),
- mimeType,
- suggestedName: name,
- });
- return {
- ...f,
- url: cached.url,
- name,
- originalName: name,
- ext,
- mimeType,
- size: blob.size,
- annotated: true,
- ...annotationRegions,
- ...(opts?.stripAnnotationMeta
- ? { annotationSourceUrl: undefined, annotationStrokes: undefined }
- : { annotationSourceUrl: f.url }),
- };
- }
- return {
- ...f,
- base64: await blobToBase64Payload(blob),
- name,
- originalName: name,
- ext,
- mimeType,
- size: blob.size,
- annotated: true,
- ...annotationRegions,
- };
+ const burned = await burnAttachment(f, strokes);
+ if (!burned) return f;
+ return await persistBurnedAttachment(f, strokes, burned, sessionId, opts);
} catch (err) {
- if (abortOnFailure) {
- log.warn('burn-in failed, aborting send', {
- name: f.name,
- error: err instanceof Error ? err.message : String(err),
- });
- throw new AnnotationBurnInError(f.name, err);
- }
log.warn('burn-in failed, sending original image without annotations', {
name: f.name,
error: err instanceof Error ? err.message : String(err),
@@ -358,6 +312,147 @@ export async function materializeAnnotatedAttachmentsForSend(
return materialized;
}
+/** 需要按笔迹烧录的附件(未烧录过的图片且带笔迹)。 */
+function needsBurn(f: AttachedFile): boolean {
+ return f.category === 'image' && !f.annotated && (f.annotationStrokes?.length ?? 0) > 0;
+}
+
+/**
+ * 读取原图字节并烧录笔迹。只在渲染进程内计算,不产生任何持久化副作用;
+ * 没有可用字节源(既无 url 也无 base64)时返回 null。
+ */
+async function burnAttachment(
+ f: AttachedFile,
+ strokes: readonly AnnotationStroke[],
+): Promise<{ blob: Blob; mimeType: string } | null> {
+ const source = f.url
+ ? await loadImageSourceBase64(f.url)
+ : f.base64
+ ? { base64: f.base64, mimeType: f.mimeType }
+ : null;
+ if (!source) return null;
+ return burnInAnnotations(source, strokes);
+}
+
+/**
+ * 把烧录位图落成发送附件:缓存附件写入媒体仓成为新 url(原 url 记为
+ * annotationSourceUrl);base64 草稿附件就地替换字节。
+ */
+async function persistBurnedAttachment(
+ f: AttachedFile,
+ strokes: readonly AnnotationStroke[],
+ burned: { blob: Blob; mimeType: string },
+ sessionId: string,
+ opts: MaterializeAnnotatedOptions | undefined,
+): Promise<AttachedFile> {
+ const { blob, mimeType } = burned;
+ const ext = mimeType === 'image/jpeg' ? '.jpg' : '.png';
+ const name = annotatedFileName(f.originalName ?? f.name, ext, Date.now());
+ const regions = summarizeAnnotationRegions(strokes);
+ const annotationRegions = regions.length > 0 ? { annotationRegions: regions } : {};
+ if (f.url) {
+ const cached = await window.electronAPI.cacheImageFromBuffer({
+ sessionId,
+ buffer: new Uint8Array(await blob.arrayBuffer()),
+ mimeType,
+ suggestedName: name,
+ });
+ return {
+ ...f,
+ url: cached.url,
+ name,
+ originalName: name,
+ ext,
+ mimeType,
+ size: blob.size,
+ annotated: true,
+ ...annotationRegions,
+ ...(opts?.stripAnnotationMeta
+ ? { annotationSourceUrl: undefined, annotationStrokes: undefined }
+ : { annotationSourceUrl: f.url }),
+ };
+ }
+ return {
+ ...f,
+ base64: await blobToBase64Payload(blob),
+ name,
+ originalName: name,
+ ext,
+ mimeType,
+ size: blob.size,
+ annotated: true,
+ ...annotationRegions,
+ };
+}
+
+/**
+ * `burnFailure: 'abort'` 的物化:整批要么全部成功,要么不留副作用地中止。
+ * 1. 先烧录全部附件(纯计算,allSettled 等全部结束);任一失败即中止,此时
+ * 还没有写入媒体仓、也没有私有化复制。
+ * 2. 全部烧录成功后再写入 / 私有化;这一步若仍有失败,把本批已新建的文件经
+ * 既有清理接口回收后中止——重试不会累积孤儿文件。
+ */
+async function materializeOrAbort(
+ files: readonly AttachedFile[],
+ sessionId: string,
+ opts: MaterializeAnnotatedOptions,
+): Promise<AttachedFile[]> {
+ const burns = await Promise.allSettled(
+ files.map((f) => (needsBurn(f) ? burnAttachment(f, f.annotationStrokes!) : null)),
+ );
+ const burnFailure = burns.findIndex((result) => result.status === 'rejected');
+ if (burnFailure >= 0) {
+ const reason = (burns[burnFailure] as PromiseRejectedResult).reason;
+ log.warn('burn-in failed, aborting send', {
+ name: files[burnFailure].name,
+ error: reason instanceof Error ? reason.message : String(reason),
+ });
+ throw new AnnotationBurnInError(files[burnFailure].name, reason);
+ }
+ const persisted = await Promise.allSettled(
+ files.map(async (f, i): Promise<AttachedFile> => {
+ const burned = (burns[i] as PromiseFulfilledResult<{ blob: Blob; mimeType: string } | null>)
+ .value;
+ if (burned) return persistBurnedAttachment(f, f.annotationStrokes!, burned, sessionId, opts);
+ if (
+ f.category === 'image' &&
+ !f.annotated &&
+ !needsBurn(f) &&
+ f.cacheUrlShared &&
+ f.url
+ ) {
+ // 与 fallback 模式同一私有化语义(失败原样发送),只是推迟到整批烧录成功之后。
+ return (await privatizeSharedAttachment(f, sessionId)) ?? f;
+ }
+ return f;
+ }),
+ );
+ const persistFailure = persisted.findIndex((result) => result.status === 'rejected');
+ if (persistFailure < 0) {
+ return persisted.map((result) => (result as PromiseFulfilledResult<AttachedFile>).value);
+ }
+ const createdUrls = persisted.flatMap((result, i) =>
+ result.status === 'fulfilled' && result.value.url && result.value.url !== files[i].url
+ ? [result.value.url]
+ : [],
+ );
+ if (createdUrls.length > 0) {
+ void Promise.resolve()
+ .then(() => window.electronAPI.cleanupCachedImages(createdUrls))
+ .catch((cleanupError: unknown) => {
+ log.warn('cleanup aborted burn-in outputs failed', {
+ error: cleanupError instanceof Error ? cleanupError.message : String(cleanupError),
+ });
+ });
+ }
+ const reason = (persisted[persistFailure] as PromiseRejectedResult).reason;
+ log.warn('burn-in persist failed, aborting send', {
+ name: files[persistFailure].name,
+ error: reason instanceof Error ? reason.message : String(reason),
+ });
+ throw new AnnotationBurnInError(files[persistFailure].name, reason);
+}
+
/**
* 共享引用附件(cacheUrlShared)经 cache-for-session 复制出本会话私有副本,
* 避免新消息与历史消息共享同一缓存文件(历史侧被删除/清理时连带悬空)。
diff --git a/apps/desktop/src/renderer/lib/makerChatStore.ts b/apps/desktop/src/renderer/lib/makerChatStore.ts
index 6788372b33..e1b6b84d91 100644
--- a/apps/desktop/src/renderer/lib/makerChatStore.ts
+++ b/apps/desktop/src/renderer/lib/makerChatStore.ts
@@ -13389,18 +13389,19 @@ function buildQueuedMessage(
};
}
+/** 返回物化结果是否已交给 outbox(记录已被清除 / 撤销时为 false)。 */
function completeRemoteOptimisticMaterialization(
sessionId: string,
clientId: string,
buildMaterializedQueued: () => QueuedMessage,
-): void {
+): boolean {
const record = remoteOptimisticSendRecords(sessionId)?.get(clientId);
if (
!record ||
!record.materializationPending ||
!isRemoteOptimisticSendRegistered(sessionId, record)
) {
- return;
+ return false;
}
const previousQueued = record.queued;
@@ -13438,6 +13439,7 @@ function completeRemoteOptimisticMaterialization(
delete record.onMaterializationReady;
onMaterializationReady?.(queued);
pumpRemoteOptimisticSendsAfterCurrent(sessionId);
+ return true;
}
function extractSessionRefs(
@@ -14219,7 +14221,11 @@ function isRemoteMediaSession(sessionId: string): boolean {
return Boolean(getOrCreateState(sessionId).remoteHostId ?? getStickySessionDeviceId(sessionId));
}
-/** 烧录降级提示:无法安全中止的发送路径已发出不含标注的原图,必须让用户知道。 */
+/**
+ * 烧录降级提示:无法安全中止的发送路径发出了不含标注的原图,必须让用户知道。
+ * 只在消息确实交出后调用(本机:dispatch 受理;device-link:物化结果进入 outbox),
+ * 未发出的消息不能提示"已发送原图"。
+ */
function notifyAnnotationBurnFallback(): void {
toast.warning(i18n.t('chat.media.annotateBurnFailedSentOriginal'));
}
@@ -14235,14 +14241,21 @@ function runLocalAnnotatedSend(
burnFailure: 'abort' | undefined,
dispatch: (prepared: AttachedFile[] | undefined) => Promise<boolean>,
): Promise<boolean> {
+ let fellBack = false;
return runRemoteOptimisticMaterialization(
null,
materializeAnnotatedAttachmentsForSend(files, sessionId, {
stripAnnotationMeta: isRemoteMediaSession(sessionId),
burnFailure: burnFailure === 'abort' ? 'abort' : 'fallback',
- onFallback: notifyAnnotationBurnFallback,
+ onFallback: () => {
+ fellBack = true;
+ },
}),
- dispatch,
+ async (prepared) => {
+ const accepted = await dispatch(prepared);
+ if (accepted && fellBack) notifyAnnotationBurnFallback();
+ return accepted;
+ },
).catch((error: unknown) => {
if (!isAnnotationBurnInError(error)) throw error;
toast.error(i18n.t('chat.media.annotateBurnFailedNotSent'));
@@ -14354,26 +14367,34 @@ function sendMessage(
void accepted.then(
(optimisticallyAccepted) => {
if (!optimisticallyAccepted) return;
- // 气泡已上屏、输入框已清空:烧录失败不中止,降级发原图并提示。
+ // 气泡已上屏、输入框已清空:烧录失败不中止,降级发原图;物化结果进入
+ // outbox 后再提示(被 /clear 等撤销的消息不提示)。
+ let fellBack = false;
void materializeAnnotatedAttachmentsForSend(files, sessionId, {
stripAnnotationMeta,
- onFallback: notifyAnnotationBurnFallback,
+ onFallback: () => {
+ fellBack = true;
+ },
})
.then((prepared) => {
- completeRemoteOptimisticMaterialization(sessionId, identity.clientId, () =>
- buildQueuedMessage(
- sessionId,
- text,
- model,
- effort,
- permissionMode,
- workingDir,
- prepared,
- mentions,
- opts,
- identity,
- ),
+ const handedOff = completeRemoteOptimisticMaterialization(
+ sessionId,
+ identity.clientId,
+ () =>
+ buildQueuedMessage(
+ sessionId,
+ text,
+ model,
+ effort,
+ permissionMode,
+ workingDir,
+ prepared,
+ mentions,
+ opts,
+ identity,
+ ),
);
+ if (handedOff && fellBack) notifyAnnotationBurnFallback();
})
.catch((error) => {
const record = remoteOptimisticSendRecords(sessionId)?.get(identity.clientId);
@@ -14772,26 +14793,34 @@ function steerMessage(
void accepted.then(
(optimisticallyAccepted) => {
if (!optimisticallyAccepted) return;
- // 气泡已上屏、输入框已清空:烧录失败不中止,降级发原图并提示。
+ // 气泡已上屏、输入框已清空:烧录失败不中止,降级发原图;物化结果进入
+ // outbox 后再提示(被 /clear 等撤销的消息不提示)。
+ let fellBack = false;
void materializeAnnotatedAttachmentsForSend(files, sessionId, {
stripAnnotationMeta,
- onFallback: notifyAnnotationBurnFallback,
+ onFallback: () => {
+ fellBack = true;
+ },
})
.then((prepared) => {
- completeRemoteOptimisticMaterialization(sessionId, identity.clientId, () =>
- buildQueuedMessage(
- sessionId,
- text,
- model,
- effort,
- permissionMode,
- workingDir,
- prepared,
- mentions,
- opts,
- identity,
- ),
+ const handedOff = completeRemoteOptimisticMaterialization(
+ sessionId,
+ identity.clientId,
+ () =>
+ buildQueuedMessage(
+ sessionId,
+ text,
+ model,
+ effort,
+ permissionMode,
+ workingDir,
+ prepared,
+ mentions,
+ opts,
+ identity,
+ ),
);
+ if (handedOff && fellBack) notifyAnnotationBurnFallback();
})
.catch((error) => {
const record = remoteOptimisticSendRecords(sessionId)?.get(identity.clientId);
diff --git a/apps/desktop/src/renderer/lib/rewindDraftAttachments.ts b/apps/desktop/src/renderer/lib/rewindDraftAttachments.ts
index 68632e8b93..e1d18e81a9 100644
--- a/apps/desktop/src/renderer/lib/rewindDraftAttachments.ts
+++ b/apps/desktop/src/renderer/lib/rewindDraftAttachments.ts
@@ -186,3 +186,36 @@ export async function dropMissingAnnotationSources<T extends RewindDraftImage>(
}),
);
}
+
+/** 回退草稿的原图预探测句柄(见 {@link startRewindSourceProbe})。 */
+export interface RewindSourceProbe<T extends RewindDraftImage> {
+ /** 发起探测时的图片列表(调用方用它确认探测对象未变)。 */
+ readonly images: readonly T[];
+ /**
+ * 同步取写草稿用的图片:探测已完成则返回"丢失原图已退回烧录图"的结果;
+ * 尚未完成则原样返回发起时的列表(即旧行为:可编辑还原),绝不等待。
+ */
+ imagesForDraft(): readonly T[];
+}
+
+/**
+ * 在回退确认框打开时就开始探测历史标注图的原图是否还在,提交回退时同步取用
+ * 结果写草稿。这样草稿写入时序与旧版完全一致(提交后同步写一次),不会有迟到
+ * 的二次写入覆盖用户在输入框里刚输入 / 添加的内容;探测未完成时按旧行为还原。
+ * 无可还原标注图时不发起任何探测。
+ */
+export function startRewindSourceProbe<T extends RewindDraftImage>(
+ images: readonly T[],
+ probe: (url: string) => Promise<boolean> = probeImageSource,
+): RewindSourceProbe<T> {
+ let resolved: readonly T[] | null = null;
+ if (hasRestorableAnnotationSources(images)) {
+ void dropMissingAnnotationSources(images, probe).then((checked) => {
+ resolved = checked;
+ });
+ }
+ return {
+ images,
+ imagesForDraft: () => resolved ?? images,
+ };
+}
diff --git a/apps/desktop/src/shared/agentInputQueue.ts b/apps/desktop/src/shared/agentInputQueue.ts
index d8d3b81722..8b11304609 100644
--- a/apps/desktop/src/shared/agentInputQueue.ts
+++ b/apps/desktop/src/shared/agentInputQueue.ts
@@ -780,7 +780,8 @@ export const ANNOTATED_IMAGE_NOTE =
* 顺序从 1 编号,帮助模型在多张图 / 多处标注时对上用户所指。
*/
export const ANNOTATED_IMAGE_REGIONS_PREFIX =
- 'Marked regions (normalized image coordinates, origin top-left): ';
+ 'Marked regions (normalized coordinates, origin top-left; ' +
+ 'images numbered in their order within this message): ';
/** 一张标注图的区域描述,如 `image 2: x 0.31–0.46, y 0.12–0.20; x 0.70–0.90, y 0.55–0.61`。 */
function describeAnnotatedImageRegions(