Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(chat-input): keep composer text verbatim so pasted tokens survive export (#22915)

Arvin Xu966b3237aaf3
diff --git a/src/features/AgentGoals/CreateGoalModal/CreateGoalContent.tsx b/src/features/AgentGoals/CreateGoalModal/CreateGoalContent.tsx
index eee83fd9eb7..901ba8f9189 100644
--- a/src/features/AgentGoals/CreateGoalModal/CreateGoalContent.tsx
+++ b/src/features/AgentGoals/CreateGoalModal/CreateGoalContent.tsx
@@ -288,7 +288,7 @@ const CreateGoalContent = memo<CreateGoalContentProps>((props) => {
const handleContentChange = useCallback(() => {
if (!canCreate || !editor) return;
- instructionRef.current = String(editor.getDocument('markdown') ?? '');
+ instructionRef.current = String(editor.getDocument('markdown', { format: false }) ?? '');
setPlan((current) => ({ ...current, instruction: instructionRef.current }));
}, [canCreate, editor]);
diff --git a/src/features/AgentGoals/GoalRequirement.tsx b/src/features/AgentGoals/GoalRequirement.tsx
index 28a715d1912..8711c7e2ce8 100644
--- a/src/features/AgentGoals/GoalRequirement.tsx
+++ b/src/features/AgentGoals/GoalRequirement.tsx
@@ -53,7 +53,7 @@ const GoalRequirement = memo<GoalRequirementProps>(({ goalId, requirement }) =>
try {
// On the unmount flush the child editor may already be disposed; a dead
// instance means nothing newer than the last change event to save.
- markdown = String(editor.getDocument('markdown') ?? '').trim();
+ markdown = String(editor.getDocument('markdown', { format: false }) ?? '').trim();
} catch {
return;
}
diff --git a/src/features/AgentTasks/AgentTaskDetail/useTaskInstructionAutosave.test.ts b/src/features/AgentTasks/AgentTaskDetail/useTaskInstructionAutosave.test.ts
index f2e8d3c4afa..451ce7fd7fc 100644
--- a/src/features/AgentTasks/AgentTaskDetail/useTaskInstructionAutosave.test.ts
+++ b/src/features/AgentTasks/AgentTaskDetail/useTaskInstructionAutosave.test.ts
@@ -99,4 +99,41 @@ describe('useTaskInstructionAutosave', () => {
expect(editor.getDocument).not.toHaveBeenCalled();
expect(updateTask).not.toHaveBeenCalled();
});
+
+ it('saves a pasted token verbatim instead of letting the markdown export escape it', async () => {
+ // The editor's default markdown export runs a remark round-trip that escapes an
+ // underscore next to `-` or `.` (abc-_def -> abc-\\_def), so a pasted JWT reaches
+ // the executor mangled. Autosave must read the writer's output verbatim; this mock
+ // mirrors that escaping, so dropping `format: false` fails here.
+ const token = 'eyJhbGciOiJFZERTQSJ9.abc-_def.ghi_jkl';
+ const escapingEditor = {
+ getDocument: vi.fn((type: string, options?: { format?: boolean }) =>
+ type === 'json'
+ ? { root: { children: [{ text: token }] } }
+ : options?.format === false
+ ? token
+ : 'eyJhbGciOiJFZERTQSJ9.abc-\\_def.ghi_jkl',
+ ),
+ } as unknown as IEditor;
+
+ const { result } = renderHook(() =>
+ useTaskInstructionAutosave({
+ contentRevision: 0,
+ editable: true,
+ editor: escapingEditor,
+ onEdit,
+ taskId: 'T-1',
+ updateTask,
+ }),
+ );
+
+ act(() => result.current());
+ await act(async () => vi.advanceTimersByTimeAsync(300));
+
+ expect(updateTask).toHaveBeenCalledWith(
+ 'T-1',
+ { editorData: { root: { children: [{ text: token }] } }, instruction: token },
+ { source: 'editor' },
+ );
+ });
});
diff --git a/src/features/AgentTasks/AgentTaskDetail/useTaskInstructionAutosave.ts b/src/features/AgentTasks/AgentTaskDetail/useTaskInstructionAutosave.ts
index fb906f81606..ea752c69d04 100644
--- a/src/features/AgentTasks/AgentTaskDetail/useTaskInstructionAutosave.ts
+++ b/src/features/AgentTasks/AgentTaskDetail/useTaskInstructionAutosave.ts
@@ -71,7 +71,7 @@ export const useTaskInstructionAutosave = ({
if (jsonSignature === lastSavedJsonRef.current) return;
lastSavedJsonRef.current = jsonSignature;
- const markdown = String(editor.getDocument('markdown') ?? '');
+ const markdown = String(editor.getDocument('markdown', { format: false }) ?? '');
updateTask(taskId, { editorData: json, instruction: markdown }, { source: 'editor' }).catch(
(error) => {
console.error('[TaskInstruction] Failed to save:', error);
diff --git a/src/features/AgentTasks/CreateTaskModal/CreateTaskContent.tsx b/src/features/AgentTasks/CreateTaskModal/CreateTaskContent.tsx
index 84c3616257f..bd8c3970b50 100644
--- a/src/features/AgentTasks/CreateTaskModal/CreateTaskContent.tsx
+++ b/src/features/AgentTasks/CreateTaskModal/CreateTaskContent.tsx
@@ -200,7 +200,7 @@ const CreateTaskContent = memo<CreateTaskContentProps>(
const handleContentChange = useCallback(() => {
if (!canCreateTask) return;
if (!editor) return;
- instructionRef.current = String(editor.getDocument('markdown') ?? '');
+ instructionRef.current = String(editor.getDocument('markdown', { format: false }) ?? '');
}, [canCreateTask, editor]);
const handleAttach = useCallback(() => {
diff --git a/src/features/Channels/Composer.tsx b/src/features/Channels/Composer.tsx
index 1775301bc44..795973b60b2 100644
--- a/src/features/Channels/Composer.tsx
+++ b/src/features/Channels/Composer.tsx
@@ -160,7 +160,7 @@ export function Composer({
);
};
const send = async () => {
- const value = String(editor.getDocument('markdown') || '').trim();
+ const value = String(editor.getDocument('markdown', { format: false }) || '').trim();
const fileIds = attachments.readyFileIds();
if (sending.current || !fileIds || (!value && !fileIds.length)) return;
const document = editor.getDocument('json');
@@ -376,7 +376,7 @@ export function Composer({
const document = JSON.stringify(json);
if (document !== previousDocument.current) {
previousDocument.current = document;
- setContent(String(editor.getDocument('markdown') || ''));
+ setContent(String(editor.getDocument('markdown', { format: false }) || ''));
draft.save();
}
}}
diff --git a/src/features/EditorModal/index.tsx b/src/features/EditorModal/index.tsx
index 4ab95d9b79b..b2e1474c3ab 100644
--- a/src/features/EditorModal/index.tsx
+++ b/src/features/EditorModal/index.tsx
@@ -31,7 +31,7 @@ const EditorModalFooter = memo<EditorModalFooterProps>((props) => {
setConfirmLoading(true);
try {
- const finalValue = (editor.getDocument('markdown') as unknown as string) || '';
+ const finalValue = (editor.getDocument('markdown', { format: false }) as unknown as string) || '';
const editorData = editor.getDocument('json');
await onConfirm?.(finalValue, editorData);
close();