Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): prevent Markdown previews from hanging the main process (#5207)
Praiseb2f98143234f
diff --git a/apps/desktop/src/main/__tests__/notificationPreview.test.ts b/apps/desktop/src/main/__tests__/notificationPreview.test.tsindex 0229825aba..5e40b62de1 100644--- a/apps/desktop/src/main/__tests__/notificationPreview.test.ts+++ b/apps/desktop/src/main/__tests__/notificationPreview.test.ts@@ -9,8 +9,15 @@ describe('notification plain text', () => {['`a_b * 2`\n\n```ts\nconst a_b = 2 * 3;\n```', 'a_b * 2 const a_b = 2 * 3;'],['> 已完成\n\n- 第一项\n- 第二项\n\n~~旧内容~~', '已完成 第一项 第二项 旧内容'],['', ''],+ ['![private filename][image]\n\n[image]: https://example.com/image.png', ''],+ ['<img src="https://example.com/image.png" alt="private filename">', ''],+ ['& 😀 <tag>', '& 😀 <tag>'],['**日本語** / _한국어_ / [繁體中文](url)', '日本語 / 한국어 / 繁體中文'],])('converts %s', (markdown, expected) => expect(notificationPreview(markdown)).toBe(expected));+ it('does not stall on bracketed repeated code spans', () => {+ const markdown = '[' + '`````a`````'.repeat(15) + ']';+ expect(notificationPreview(markdown)).toBe('[' + ('a' + '`'.repeat(10)).repeat(14) + 'a]');+ });it('shortens after parsing, without splitting Unicode characters', () => {expect(notificationPreview('**😀中文abc**', 4)).toBe('😀中文');expect(notificationPreview('abc😀', 4)).toBe('abc');diff --git a/apps/desktop/src/main/localDb/__tests__/mapperMarkdownPreview.test.ts b/apps/desktop/src/main/localDb/__tests__/mapperMarkdownPreview.test.tsindex 7cd284c6dd..7b6d44d302 100644--- a/apps/desktop/src/main/localDb/__tests__/mapperMarkdownPreview.test.ts+++ b/apps/desktop/src/main/localDb/__tests__/mapperMarkdownPreview.test.ts@@ -14,6 +14,13 @@ describe('sidebar Markdown preview', () => {expect(extractMessagePreview(JSON.stringify({ text: markdown }), 'user')).toBe(plain);});+ it('handles bracketed code spans that made the old link lexer backtrack', () => {+ const markdown = '[' + '`````a`````'.repeat(15) + ']';+ const expected = ('[' + ('a' + '`'.repeat(10)).repeat(14) + 'a]').slice(0, 140);+ expect(finalizePlainPreview(markdown, 'user')).toBe(expected);+ expect(extractMessagePreview(JSON.stringify({ text: markdown }), 'user')).toBe(expected);+ });+it('normalizes existing raw cached previews on read', () => {const session = sessionToCamel({createdAt: 1,@@ -59,6 +66,8 @@ describe('sidebar Markdown preview', () => {['<img src="javascript:alert(1)" alt="不支持的图片">', null],['<img src="https://example.com/image.png">', null],['<script><img src="https://example.com/image.png" alt="隐藏内容"></script>', null],+ ['![reference alt][image]\n\n[image]: https://example.com/image.png', 'reference alt'],+ ['& 😀 <tag>', '& 😀 <tag>'],['---', null],])('extracts readable content from %s', (text, expected) => {expect(finalizePlainPreview(text, 'assistant')).toBe(expected);diff --git a/apps/desktop/src/main/notificationPreview.ts b/apps/desktop/src/main/notificationPreview.tsindex 32559f3b55..836f23e812 100644--- a/apps/desktop/src/main/notificationPreview.ts+++ b/apps/desktop/src/main/notificationPreview.ts@@ -1,24 +1,8 @@-import { Lexer, type Tokens, type Token } from 'marked';+import { markdownPreviewText } from '../shared/markdownPreviewText';-/** Parse Markdown before shortening it: punctuation in ordinary text/code is content. */+/** Parse before shortening, without leaking image filenames into notifications. */export function notificationPlainText(markdown: string): string {- const render = (tokens: Token[]): string => tokens.map((token): string => {- switch (token.type) {- case 'space': case 'br': return ' ';- case 'html': case 'image': case 'def': case 'hr': return '';- case 'code': case 'codespan': return token.text;- case 'list': return (token as Tokens.List).items.map((item) => render(item.tokens)).join(' ');- case 'table': return [(token as Tokens.Table).header, ...(token as Tokens.Table).rows]- .map((row) => row.map((cell) => render(cell.tokens)).join(' ')).join(' ');- default: {- const value = token as Token & { tokens?: Token[]; text?: string };- const text = value.tokens ? render(value.tokens) : (value.text ?? '');- return ['paragraph', 'heading', 'blockquote', 'list_item'].includes(token.type)- ? ` ${text} ` : text;- }- }- }).join('');- return render(Lexer.lex(markdown)).replace(/\s+/gu, ' ').trim();+ return markdownPreviewText(markdown, { includeImageAlt: false });}export function notificationPreview(markdown: string, limit = 240): string {diff --git a/apps/desktop/src/shared/markdownPreviewText.ts b/apps/desktop/src/shared/markdownPreviewText.tsindex 66d4e79edd..75a9660cb8 100644--- a/apps/desktop/src/shared/markdownPreviewText.ts+++ b/apps/desktop/src/shared/markdownPreviewText.ts@@ -1,45 +1,62 @@-import { Lexer, type Token, type Tokens } from 'marked';+import type { Nodes } from 'mdast';+import remarkGfm from 'remark-gfm';+import remarkParse from 'remark-parse';+import { unified } from 'unified';import { htmlImgToImageNode } from './htmlImage';-/** Extract display text without generating HTML or counting link destinations. */-export function markdownPreviewText(markdown: string): string {- return previewTokens(Lexer.lex(markdown, { gfm: true }))- .replace(/\s+/g, ' ')- .trim();-}+const previewParser = unified().use(remarkParse).use(remarkGfm);-function previewTokens(tokens: Token[]): string {- return tokens- .map((token): string => {- switch (token.type) {- case 'space':- case 'br':- case 'hr':- case 'def':- return ' ';- case 'html':- return `${htmlImgToImageNode({ type: 'html', value: token.text })?.alt ?? ''} `;- case 'list':- return `${token.items.map((item: Tokens.ListItem) => previewTokens(item.tokens)).join(' ')} `;- case 'table':- return (- [token.header, ...token.rows]- .map((row: { tokens: Token[] }[]) =>- row.map((cell) => previewTokens(cell.tokens)).join(' '),- )- .join(' ') + ' '- );- case 'heading':- case 'paragraph':- case 'blockquote':- return `${previewTokens(token.tokens ?? [])} `;- case 'code':- return `${token.text} `;- default:- if ('tokens' in token && token.tokens) return previewTokens(token.tokens);- return 'text' in token ? token.text : '';- }- })- .join('');+/** Extract readable text without HTML output or link destinations. */+export function markdownPreviewText(+ markdown: string,+ options: { includeImageAlt?: boolean } = {},+): string {+ // Marked's link regex can backtrack for minutes on short bracketed code spans.+ // These previews run on Main after DB replies, so use the existing remark parser.+ const stack: Array<Nodes | string> = [previewParser.parse(markdown)];+ const parts: string[] = [];+ const includeImageAlt = options.includeImageAlt !== false;+ while (stack.length > 0) {+ const node = stack.pop()!;+ if (typeof node === 'string') {+ parts.push(node);+ continue;+ }+ switch (node.type) {+ case 'text':+ case 'inlineCode':+ parts.push(node.value);+ break;+ case 'code':+ parts.push(' ', node.value, ' ');+ break;+ case 'image':+ case 'imageReference':+ if (includeImageAlt) parts.push(node.alt ?? '');+ break;+ case 'html':+ if (includeImageAlt) parts.push(htmlImgToImageNode(node)?.alt ?? '', ' ');+ break;+ case 'definition':+ break;+ case 'break':+ case 'thematicBreak':+ parts.push(' ');+ break;+ default:+ if ('children' in node) {+ const block = node.type !== 'emphasis' && node.type !== 'strong'+ && node.type !== 'delete' && node.type !== 'link' && node.type !== 'linkReference';+ if (block) {+ parts.push(' ');+ stack.push(' ');+ }+ for (let index = node.children.length - 1; index >= 0; index--) {+ stack.push(node.children[index]);+ }+ }+ }+ }+ return parts.join('').replace(/\s+/gu, ' ').trim();}