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): preserve plan mode composer drafts (#3236)

Tsai Yuan83bc242ff427
diff --git a/apps/desktop/src/renderer/__tests__/planModeComposerCommand.test.ts b/apps/desktop/src/renderer/__tests__/planModeComposerCommand.test.ts
index d6e641f40f..9eac783642 100644
--- a/apps/desktop/src/renderer/__tests__/planModeComposerCommand.test.ts
+++ b/apps/desktop/src/renderer/__tests__/planModeComposerCommand.test.ts
@@ -6,6 +6,7 @@ import {
addPlanModeComposerCommand,
consumePlanModeComposerCommand,
isPlanModeComposerCommandText,
+ planModeComposerDraftText,
shouldPreservePlanModeComposerDraft,
} from '../components/new-chat/planModeComposerCommand';
import type { UnifiedCommand } from '../lib/slashCommands';
@@ -84,4 +85,25 @@ describe('/plan composer command', () => {
expect(shouldPreservePlanModeComposerDraft(0, 1)).toBe(true);
expect(shouldPreservePlanModeComposerDraft(0, 0)).toBe(false);
});
+
+ it('captures editor text before the plan command clears the editor', () => {
+ const editorText = {
+ type: 'doc',
+ content: [{ type: 'paragraph', content: [{ type: 'text', text: 'keep me' }] }],
+ };
+ const storedText = {
+ type: 'doc',
+ content: [{ type: 'paragraph', content: [{ type: 'text', text: 'stored' }] }],
+ };
+
+ expect(planModeComposerDraftText(true, editorText, storedText)).toBe(editorText);
+ expect(planModeComposerDraftText(false, null, storedText)).toBe(storedText);
+ expect(planModeComposerDraftText(true, null, storedText)).toBeNull();
+ });
+
+ it('does not persist the consumed /plan command as draft text', () => {
+ const editorText = { type: 'doc', content: [{ type: 'paragraph', content: [{ type: 'text', text: '/plan' }] }] };
+ expect(planModeComposerDraftText(true, editorText, null, true)).toBeNull();
+ expect(planModeComposerDraftText(false, null, editorText, true)).toBeNull();
+ });
});
diff --git a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
index 44453fb1fd..99740a6c09 100644
--- a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
+++ b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
@@ -158,6 +158,7 @@ import {
addPlanModeComposerCommand,
consumePlanModeComposerCommand,
isPlanModeComposerCommandText,
+ planModeComposerDraftText,
shouldPreservePlanModeComposerDraft,
} from './planModeComposerCommand';
import { PendingQueuePanel } from './PendingQueuePanel';
@@ -4997,6 +4998,14 @@ export function ChatInput({
editorStorageKey: storageKeyForDraftRef.current,
sourceStorageKey,
});
+ const existingDraft = sourceStorageKey ? getComposerDraft(sourceStorageKey) : undefined;
+ // Capture this before consuming `/plan`, which clears the live editor.
+ const draftText = planModeComposerDraftText(
+ editorOwnsSource,
+ editorOwnsSource ? editor.getJSON() : null,
+ existingDraft?.text,
+ true,
+ );
if (editorOwnsSource) {
planModeEntry?.onToggle(!planModeEntry.enabled);
isRestoringRef.current = true;
@@ -5013,11 +5022,10 @@ export function ChatInput({
if (
shouldPreservePlanModeComposerDraft(attachmentsForSend.length, commentsForSend.length)
) {
- const existingDraft = getComposerDraft(sourceStorageKey);
saveComposerDraft(
sourceStorageKey,
{
- text: editorOwnsSource ? editor.getJSON() : (existingDraft?.text ?? null),
+ text: draftText,
attachments: attachmentsForSend,
quotes: existingDraft?.quotes ?? [],
browserComments: commentsForSend,
diff --git a/apps/desktop/src/renderer/components/new-chat/planModeComposerCommand.ts b/apps/desktop/src/renderer/components/new-chat/planModeComposerCommand.ts
index d143059a96..ee8636407a 100644
--- a/apps/desktop/src/renderer/components/new-chat/planModeComposerCommand.ts
+++ b/apps/desktop/src/renderer/components/new-chat/planModeComposerCommand.ts
@@ -1,4 +1,5 @@
import type { Transaction } from '@tiptap/pm/state';
+import type { JSONContent } from '@tiptap/core';
import type { UnifiedCommand } from '@/lib/slashCommands';
@@ -56,3 +57,14 @@ export function shouldPreservePlanModeComposerDraft(
): boolean {
return attachmentCount > 0 || browserCommentCount > 0;
}
+
+/** Capture text before consuming `/plan` clears the live editor. */
+export function planModeComposerDraftText(
+ editorOwnsSource: boolean,
+ editorText: JSONContent | null | undefined,
+ storedText: JSONContent | null | undefined,
+ consumedCommand = false,
+): JSONContent | null {
+ if (consumedCommand) return null;
+ return (editorOwnsSource ? editorText : storedText) ?? null;
+}