Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 完善结构化列表边界处理
zhangyongjie2bfc215bc08f
diff --git a/apps/desktop/src/renderer/__tests__/composerStructuredLists.test.ts b/apps/desktop/src/renderer/__tests__/composerStructuredLists.test.tsindex b6f962b3c9..b52d1cb522 100644--- a/apps/desktop/src/renderer/__tests__/composerStructuredLists.test.ts+++ b/apps/desktop/src/renderer/__tests__/composerStructuredLists.test.ts@@ -5,6 +5,7 @@ import Document from '@tiptap/extension-document';import Paragraph from '@tiptap/extension-paragraph';import Text from '@tiptap/extension-text';import HardBreak from '@tiptap/extension-hard-break';+import { Fragment, Slice } from '@tiptap/pm/model';import { TextSelection } from '@tiptap/pm/state';import {@@ -15,6 +16,7 @@ import {handleStructuredListBreak,promoteTrailingPlainListParagraph,} from '@/components/new-chat/ComposerListNodes';+import { plainTextToComposerDocument } from '@/lib/composerListDocument';import {serializeEditorContent,serializeEditorSlice,@@ -170,6 +172,38 @@ describe('composer structured list input rules', () => {expect(editor.state.doc.firstChild?.textContent).toBe(' - ');});+ it.each(['0. ', '0001. '])('keeps %s as plain text', (typed) => {+ const editor = makeEditor();++ typeThroughInputRules(editor, typed);++ expect(editor.state.doc.firstChild?.type.name).toBe('paragraph');+ expect(editor.state.doc.firstChild?.textContent).toBe(typed);+ });++ it('does not join an explicitly non-contiguous ordered marker with the preceding list', () => {+ const editor = makeEditor({+ type: 'doc',+ content: [+ {+ type: 'orderedList',+ attrs: { start: 1, marker: '.' },+ content: [{ type: 'listItem', content: [{ type: 'paragraph', content: [{ type: 'text', text: 'first' }] }] }],+ },+ { type: 'paragraph' },+ ],+ });+ selectDocumentEnd(editor);++ typeThroughInputRules(editor, '3. ');++ expect(editor.state.doc.childCount).toBe(2);+ expect(editor.state.doc.firstChild?.type.name).toBe('orderedList');+ expect(editor.state.doc.firstChild?.childCount).toBe(1);+ expect(editor.state.doc.lastChild?.type.name).toBe('orderedList');+ expect(editor.state.doc.lastChild?.attrs.start).toBe(3);+ });+it('reserves enough marker width for long ordered-list numbers', () => {const editor = makeEditor();@@ -824,6 +858,105 @@ describe('composer structured list serialization', () => {]);});+ it('does not duplicate restored continuation indentation after a list quote', () => {+ const editor = makeEditor({+ type: 'doc',+ content: [+ {+ type: 'bulletList',+ content: [+ {+ type: 'listItem',+ content: [+ {+ type: 'paragraph',+ content: [+ { type: COMPOSER_QUOTE_NODE_TYPE, attrs: { text: 'quoted' } },+ { type: 'text', text: ' after' },+ ],+ },+ ],+ },+ ],+ },+ ],+ });++ expect(serializeEditorContent(editor).text).toBe(+ '- \n > <!-- cindy-composer-quote -->\n > quoted\n after',+ );+ });++ it('keeps adjacent list quote chips as separate quote segments', () => {+ const editor = makeEditor({+ type: 'doc',+ content: [+ {+ type: 'bulletList',+ content: [+ {+ type: 'listItem',+ content: [+ {+ type: 'paragraph',+ content: [+ { type: COMPOSER_QUOTE_NODE_TYPE, attrs: { text: 'first' } },+ { type: COMPOSER_QUOTE_NODE_TYPE, attrs: { text: 'second' } },+ ],+ },+ ],+ },+ ],+ },+ ],+ });++ const serialized = serializeEditorContent(editor).text;+ expect(parseChatQuoteSegments(serialized)).toEqual([+ { kind: 'text', text: '- ' },+ { kind: 'quote', quote: { text: 'first' } },+ { kind: 'quote', quote: { text: 'second' } },+ ]);+ });++ it('preserves an empty structured list item separator at the end of serialized text', () => {+ const editor = makeEditor({+ type: 'doc',+ content: [+ {+ type: 'orderedList',+ attrs: { start: 2, marker: '.' },+ content: [{ type: 'listItem', content: [{ type: 'paragraph' }] }],+ },+ ],+ });++ expect(serializeEditorContent(editor).text).toBe('2. ');+ });++ it('inserts normalized list blocks at a non-terminal selection', () => {+ const editor = makeEditor({+ type: 'doc',+ content: [+ { type: 'paragraph', content: [{ type: 'text', text: 'before' }] },+ { type: 'paragraph', content: [{ type: 'text', text: 'after' }] },+ ],+ });+ editor.commands.setTextSelection(2);+ const replacement = plainTextToComposerDocument('1. first\n2. second').content!.map((node) =>+ editor.state.schema.nodeFromJSON(node),+ );+ const tr = editor.state.tr.replaceSelection(new Slice(Fragment.from(replacement), 0, 0));+ editor.view.dispatch(tr);++ expect(editor.state.doc.textContent).toContain('first');+ expect(editor.state.doc.textContent).toContain('second');+ expect(editor.state.doc.childCount).toBeGreaterThan(2);+ expect(editor.state.doc.content.content.some((node) => node.type.name === 'orderedList')).toBe(+ true,+ );+ });+it('preserves nested markers and projects atom ranges into wire offsets', () => {const href = 'cindy://session/session-a?message=message-a';const editor = makeEditor({diff --git a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsxindex a9a53bfb59..bee11c2ae9 100644--- a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx+++ b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx@@ -1554,25 +1554,23 @@ export function ChatInput({$from.parent.firstChild?.type.name === 'hardBreak')) &&$from.parentOffset === $from.parent.content.size;- if (- state.selection.empty &&- paragraphEndsDocument &&- paragraphIsEmptyLine &&- composerDocumentContainsList(normalizedPaste)- ) {+ if (composerDocumentContainsList(normalizedPaste)) {event.preventDefault();const replacement = (normalizedPaste.content ?? []).map((node) =>state.schema.nodeFromJSON(node),);- if ($from.parent.content.size > 0) {+ if (paragraphEndsDocument && paragraphIsEmptyLine && $from.parent.content.size > 0) {replacement.unshift(state.schema.nodes.paragraph.create());}const fragment = Fragment.from(replacement);- const tr = state.tr.replaceWith(- paragraphPosition,- paragraphPosition + $from.parent.nodeSize,- fragment,- );+ const tr =+ paragraphEndsDocument && paragraphIsEmptyLine+ ? state.tr.replaceWith(+ paragraphPosition,+ paragraphPosition + $from.parent.nodeSize,+ fragment,+ )+ : state.tr.replaceSelection(new Slice(fragment, 0, 0));tr.setSelection(TextSelection.atEnd(tr.doc));view.dispatch(tr.scrollIntoView());return true;diff --git a/apps/desktop/src/renderer/components/new-chat/ComposerListNodes.ts b/apps/desktop/src/renderer/components/new-chat/ComposerListNodes.tsindex 9da33c3b00..1c6f8a7aac 100644--- a/apps/desktop/src/renderer/components/new-chat/ComposerListNodes.ts+++ b/apps/desktop/src/renderer/components/new-chat/ComposerListNodes.ts@@ -13,8 +13,8 @@ import { Selection, TextSelection } from '@tiptap/pm/state';import type { EditorView } from '@tiptap/pm/view';const BULLET_MARKER_RE = /^([-+*•])([ \t]+)$/;-const ORDERED_MARKER_RE = /^(\d{1,6})([.)])([ \t]+)$/;-const CJK_ORDERED_MARKER_RE = /^(\d{1,6})(、)$/;+const ORDERED_MARKER_RE = /^([1-9]\d{0,5})([.)])([ \t]+)$/;+const CJK_ORDERED_MARKER_RE = /^([1-9]\d{0,5})(、)$/;type BulletMarker = '-' | '+' | '*' | '•';type OrderedMarker = '.' | ')' | '、';@@ -48,7 +48,7 @@ function plainListParagraphMarker(text: string): PlainListParagraphMarker | nullattrs: { marker: bullet[1], separator: bullet[2] },};}- const ordered = text.match(/^(\d{1,6})([.)])([ \t]+)/);+ const ordered = text.match(/^([1-9]\d{0,5})([.)])([ \t]+)/);if (ordered) {return {kind: 'ordered',@@ -56,7 +56,7 @@ function plainListParagraphMarker(text: string): PlainListParagraphMarker | nullattrs: { start: Number(ordered[1]), marker: ordered[2], separator: ordered[3] },};}- const cjkOrdered = text.match(/^(\d{1,6})(、)([ \t]*)/);+ const cjkOrdered = text.match(/^([1-9]\d{0,5})(、)([ \t]*)/);if (cjkOrdered) {return {kind: 'ordered',@@ -172,6 +172,8 @@ export const ComposerBulletList = Node.create({wrappingInputRule({find: BULLET_MARKER_RE,type: this.type,+ joinPredicate: (match, before) =>+ before.attrs.marker === match[1] && before.attrs.separator === match[2],getAttributes: (match) => ({marker: match[1] as BulletMarker,separator: match[2],@@ -194,6 +196,15 @@ function orderedAttrs(match: RegExpMatchArray): OrderedListAttrs {};}+function canJoinOrderedList(match: RegExpMatchArray, before: PMNode): boolean {+ const attrs = orderedAttrs(match);+ return (+ before.attrs.marker === attrs.marker &&+ (before.attrs.separator ?? (attrs.marker === '、' ? '' : ' ')) === attrs.separator &&+ before.attrs.start + before.childCount === attrs.start+ );+}+export const ComposerOrderedList = Node.create({name: 'orderedList',group: 'block',@@ -252,11 +263,13 @@ export const ComposerOrderedList = Node.create({wrappingInputRule({find: ORDERED_MARKER_RE,type: this.type,+ joinPredicate: canJoinOrderedList,getAttributes: orderedAttrs,}),wrappingInputRule({find: CJK_ORDERED_MARKER_RE,type: this.type,+ joinPredicate: canJoinOrderedList,getAttributes: orderedAttrs,}),hardBreakListInputRule(ORDERED_MARKER_RE, this.type, orderedAttrs),diff --git a/apps/desktop/src/renderer/components/new-chat/composerContentSerialization.ts b/apps/desktop/src/renderer/components/new-chat/composerContentSerialization.tsindex ad39af0aeb..8e2e8b6714 100644--- a/apps/desktop/src/renderer/components/new-chat/composerContentSerialization.ts+++ b/apps/desktop/src/renderer/components/new-chat/composerContentSerialization.ts@@ -37,6 +37,9 @@ export interface SerializedComposerContent {type OrderedMarker = '.' | ')' | '、';+const EMPTY_LIST_ITEM_MARKER_RE =+ /(?:^|\n)(?:[1-9]\d{0,5}[.)][ \t]+|[1-9]\d{0,5}、[ \t]*|[-+*•][ \t]+)$/;+function orderedListMarker(node: ProseMirrorNode): OrderedMarker {return node.attrs.marker === ')' || node.attrs.marker === '、' ? node.attrs.marker : '.';}@@ -142,7 +145,8 @@ function serializeComposerDocument(// can still recognize the private marker and source metadata.const continuationPrefix = ' '.repeat(prefix.length);const quoteLines = quoteText.split('\n');- buffer += `\n${quoteLines+ const quoteSeparator = continuationAfterQuote === null ? '' : '\n';+ buffer += `${quoteSeparator}\n${quoteLines.map((line) => `${continuationPrefix}${line}`).join('\n')}`;continuationAfterQuote = continuationPrefix;@@ -155,7 +159,9 @@ function serializeComposerDocument(}if (continuationAfterQuote !== null) {- buffer += `\n${continuationAfterQuote}`;+ const alreadyIndented =+ child.isText && (child.text ?? '').startsWith(continuationAfterQuote);+ buffer += `\n${alreadyIndented ? '' : continuationAfterQuote}`;continuationAfterQuote = null;}@@ -314,8 +320,11 @@ function serializeComposerDocument(}});+ const lastTextBlock = blocks.at(-1);+ const preserveTrailingWhitespace =+ lastTextBlock?.kind === 'text' && EMPTY_LIST_ITEM_MARKER_RE.test(lastTextBlock.text);return {- ...serializeComposerContentBlocksWithRanges(blocks),+ ...serializeComposerContentBlocksWithRanges(blocks, { preserveTrailingWhitespace }),mentions,hasQuotes,};diff --git a/apps/desktop/src/renderer/lib/composerListDocument.ts b/apps/desktop/src/renderer/lib/composerListDocument.tsindex 5eaf5ccbca..24bc6608a9 100644--- a/apps/desktop/src/renderer/lib/composerListDocument.ts+++ b/apps/desktop/src/renderer/lib/composerListDocument.ts@@ -17,7 +17,7 @@ interface ComposerLine {}const BULLET_RE = /^([-+*•])([ \t]+)/;-const ORDERED_RE = /^(\d{1,6})([.)、])([ \t]*)/;+const ORDERED_RE = /^([1-9]\d{0,5})([.)、])([ \t]*)/;function inlineNodeText(node: JSONContent): string {if (node.type === 'text') return node.text ?? '';diff --git a/apps/desktop/src/renderer/lib/composerQuoteDocument.ts b/apps/desktop/src/renderer/lib/composerQuoteDocument.tsindex a79ca25232..f5a1766f56 100644--- a/apps/desktop/src/renderer/lib/composerQuoteDocument.ts+++ b/apps/desktop/src/renderer/lib/composerQuoteDocument.ts@@ -213,6 +213,7 @@ export function serializeComposerContentBlocks(blocks: readonly ComposerSerializ/** Serialize blocks and project block-relative presentation ranges into wire offsets. */export function serializeComposerContentBlocksWithRanges(blocks: readonly ComposerSerializedBlock[],+ options: { preserveTrailingWhitespace?: boolean } = {},): {text: string;agentReferences: AgentInputReference[];@@ -275,7 +276,7 @@ export function serializeComposerContentBlocksWithRanges(});const leadingTrim = serialized.length - serialized.trimStart().length;- const text = serialized.trim();+ const text = options.preserveTrailingWhitespace ? serialized.trimStart() : serialized.trim();return {text,agentReferences: agentReferences