Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
perf(mobile): reuse pending Markdown syntax searches
elfcv60b9fdf02b31
diff --git a/apps/mobile/src/__tests__/messageMarkdown.test.ts b/apps/mobile/src/__tests__/messageMarkdown.test.tsindex 7d57e6c7af..17805ac88a 100644--- a/apps/mobile/src/__tests__/messageMarkdown.test.ts+++ b/apps/mobile/src/__tests__/messageMarkdown.test.ts@@ -2065,3 +2065,43 @@ it('normalizes a pending bare URL once and still discovers the following URL', (expect(prefixScans).toBeLessThanOrEqual(2);} finally { spy.mockRestore(); }});++// #7088 follow-up: each formatted token rescanned all remaining syntax, blocking+// Hermes on a long paragraph. Count examined suffix characters through the real+// parser (unit, not wall-clock timing); existing semantic tests miss this cost.+it('bounds repeated suffix searches while keeping text, links and images', () => {+ const source = '**one** `two` '.repeat(30) + ' after src/end.ts';+ const exec = RegExp.prototype.exec;+ let searchedCharacters = 0;+ const spy = vi.spyOn(RegExp.prototype, 'exec').mockImplementation(function (this: RegExp, input: string) {+ const from = this.lastIndex;+ const match = Reflect.apply(exec, this, [input]);+ if (input === source && this.global) searchedCharacters += (match ? match.index + match[0].length : source.length) - from;+ return match;+ });+ try {+ const parsed = parseMobileMarkdownInlines(source);+ expect(parsed.filter(token => token.type === 'strong')).toHaveLength(30);+ expect(parsed.filter(token => token.type === 'code')).toHaveLength(30);+ expect(parsed.slice(-3)).toEqual([+ { type: 'image', alt: 'photo', url: 'https://example.invalid/a.png' },+ { type: 'text', text: ' after ' },+ { type: 'link', text: 'src/end.ts', url: 'src/end.ts', bare: true },+ ]);+ expect(searchedCharacters).toBeLessThan(source.length * 20);+ } finally { spy.mockRestore(); }+});++// The pending-search optimization must retain raw prefix boundaries and rejected+// spans. Through the public parser, protect rendering when another grammar wins+// before/inside a candidate; these cases distinguish unsafe token-level caching.+it.each([+ ['**a**_b_', [{ type: 'strong', text: 'a' }, { type: 'text', text: '_b_' }]],+ ['`a`$b$', [{ type: 'code', text: 'a' }, { type: 'text', text: '$b$' }]],+ [' then ', [+ { type: 'text', text: ' then ' }, { type: 'image', alt: 'ok', url: 'src/a.png' },+ ]],+])('keeps overlapping and consumed-prefix rendering for %s', (source, expected) => {+ expect(parseMobileMarkdownInlines(source as string)).toEqual(expected);+});diff --git a/apps/mobile/src/session/chatPathCandidate.ts b/apps/mobile/src/session/chatPathCandidate.tsindex a694ba2629..a61179a76e 100644--- a/apps/mobile/src/session/chatPathCandidate.ts+++ b/apps/mobile/src/session/chatPathCandidate.ts@@ -592,7 +592,12 @@ export interface BareFilePathMatch {* 候选 ≠ 点亮:`16/9.0`、`a/b.c` 这类形状达标的误命中同样返回,存在性由远端* stat(remotePathVerdict)兜底,解析不到就保持纯文本。*/-export function findBareFilePathMatch(input: string, from: number): BareFilePathMatch | null {+export function findBareFilePathMatch(+ input: string,+ from: number,+ // Optional paragraph-local exec cache, bound to this exact immutable input.+ search?: (regex: RegExp) => RegExpExecArray | null,+): BareFilePathMatch | null {// 不含任何路径分隔符的文本一定不是带分隔符路径。这条短路在渲染热路径上逐 token// 生效(见 messageMarkdown.findNextInlineToken),不能省。if (!input.includes('/') && !input.includes('\\')) return null;@@ -601,7 +606,7 @@ export function findBareFilePathMatch(input: string, from: number): BareFilePathconst re = new RegExp(BARE_PATH_RE_SOURCE, 'g');re.lastIndex = Math.max(0, from);let m: RegExpExecArray | null;- while ((m = re.exec(input)) !== null) {+ while ((m = search ? search(re) : re.exec(input)) !== null) {const value = m[1];if (!looksLikeFilePath(splitChatPathLineSuffix(value).href)) continue;const index = m.index + (m[0].length - value.length);diff --git a/apps/mobile/src/session/markdownRegexSearch.ts b/apps/mobile/src/session/markdownRegexSearch.tsnew file mode 100644index 0000000000..54b14d49fc--- /dev/null+++ b/apps/mobile/src/session/markdownRegexSearch.ts@@ -0,0 +1,32 @@+/** Search the parser's fixed, non-empty global regex grammars in one immutable+ * inline body, including a separate instance for recursive marks.+ * Retain only the next raw match per grammar, not a list of all tokens. A cursor+ * inside a previous match must search again: it can expose overlapping syntax.+ * Keep raw starts (including consumed prefix groups) and the search origin;+ * rejected candidates can advance a matcher beyond the next parser cursor.+ */+export function createMarkdownRegexSearch(input: string) {+ const pending = new Map<string, { from: number; match: RegExpExecArray | null }>();+ const searched = new Set<string>();+ return {+ begin: () => searched.clear(),+ exec: (regex: RegExp): RegExpExecArray | null => {+ const from = regex.lastIndex;+ const key = `${regex.source}/${regex.flags}`;+ // A grammar can reject its first hit and continue. Preserve that first hit+ // for the next parser cursor; caching the final miss would make an earlier+ // cursor rescan the entire suffix (or skip newly exposed overlapping syntax).+ if (searched.has(key)) return regex.exec(input);+ searched.add(key);+ let cached = pending.get(key);+ if (!cached || from < cached.from || (cached.match !== null && from > cached.match.index)) {+ cached = { from, match: regex.exec(input) };+ pending.set(key, cached);+ } else {+ // Match RegExp.exec's cursor contract, including a miss resetting to zero.+ regex.lastIndex = cached.match ? cached.match.index + cached.match[0].length : 0;+ }+ return cached.match;+ },+ };+}diff --git a/apps/mobile/src/session/messageMarkdown.ts b/apps/mobile/src/session/messageMarkdown.tsindex e44acf9eb2..eca63e85c5 100644--- a/apps/mobile/src/session/messageMarkdown.ts+++ b/apps/mobile/src/session/messageMarkdown.ts@@ -14,6 +14,7 @@ import {import { DEEP_LINK_SCHEME_GROUP } from '@/session/sessionLinks';// App bootstrap initializes this instance; parsing summaries must not initialize React bindings.import i18n from 'i18next';+import { createMarkdownRegexSearch } from './markdownRegexSearch';// 加粗 / 斜体 / 删除线里的内容会继续按行内语法解析(`**https://…**` 的链接照样可点,// 与桌面 remark 同口径),结果拍平进同一层 inline 列表:里面的纯文本就是该强调类型本身,@@ -1168,6 +1169,7 @@ function createInlineParseContext(input: string, startsInsideHtmlComment: booleaconst commentProbe = () => (probe ??= blankEscapedAngles(blankCodeSpans(input)));return {startsInsideHtmlComment,+ search: createMarkdownRegexSearch(input),needsCommentCheck: startsInsideHtmlComment || input.includes('<!--'),hasMarkdownImage: input.includes(' 的 "[alt](url)" 子串会被 link 规则命中,// 靠 image 的起点(`!`)更靠前在排序里胜出,这里的顺序只是可读性上的强调。@@ -1224,17 +1227,19 @@ function findNextInlineToken(...(url.startsWith('cindy-media://') ? { managedMediaKind: 'video' as const } : {}),};},+ undefined,+ context.search.exec,),// 本地路径链接:[README.md](/abs/path/README.md:17) 这类模型高频输出形态。// URL 经 classifyChatPathLinkTarget 判形状(http/session 之外的路径形态才收),// 渲染层再经被控端 stat 决定点亮 chip 还是保持纯文本标签。- matchLocalPathLink(input, from),+ matchLocalPathLink(input, from, context),// 正文纯文本里裸写的本地路径(`见 src/App.tsx 第 20 行`、`改的是 C:\proj\a.ts`):// 模型高频形态,桌面端由 remarkLocalPathLinks 切成 link 节点,这里补齐同一入口。// 产出 link inline 后与 `[label](path)` 形态共用 LinkPathChipSpan → 远端 stat →// 点亮 chip / 保持纯文本。matchBareFilePathLink(input, from, context),- matchRegex(input, from, /`([^`]+)`/g, (match) => ({ type: 'code' as const, text: match[1] })),+ matchRegex(input, from, /`([^`]+)`/g, (match) => ({ type: 'code' as const, text: match[1] }), undefined, context.search.exec),// inline math:$$x$$(双 dollar 行内形态)与 $x$。候选按起点排序,公式起点// 的 $ 早于公式体内的 * / _,强调规则不会拆走公式内容。单 dollar 采用// Pandoc 风格紧贴规则:内容首尾必须非空白、闭合 $ 后不能紧跟数字——@@ -1245,22 +1250,22 @@ function findNextInlineToken(matchRegex(input, from, /\$\$([^$`\n]+?)\$\$/g, (match) => ({type: 'math' as const,text: match[1].trim(),- })),+ }), undefined, context.search.exec),matchRegex(input, from, /(^|[^\\$])\$([^$`\n\s](?:[^$`\n]*[^$`\n\s])?)\$(?!\d)/g, (match) => ({type: 'math' as const,text: match[2],- }), 1),- matchRegex(input, from, /\*\*([^*\n]+)\*\*/g, (match) => ({ type: 'strong' as const, text: match[1] })),- matchRegex(input, from, /__([^_\n]+)__/g, (match) => ({ type: 'strong' as const, text: match[1] })),- matchRegex(input, from, /~~([^~\n]+)~~/g, (match) => ({ type: 'strikethrough' as const, text: match[1] })),+ }), 1, context.search.exec),+ matchRegex(input, from, /\*\*([^*\n]+)\*\*/g, (match) => ({ type: 'strong' as const, text: match[1] }), undefined, context.search.exec),+ matchRegex(input, from, /__([^_\n]+)__/g, (match) => ({ type: 'strong' as const, text: match[1] }), undefined, context.search.exec),+ matchRegex(input, from, /~~([^~\n]+)~~/g, (match) => ({ type: 'strikethrough' as const, text: match[1] }), undefined, context.search.exec),matchRegex(input, from, /(^|[^\w*])\*([^*\n]+)\*/g, (match) => ({type: 'emphasis' as const,text: match[2],- }), 1),+ }), 1, context.search.exec),matchRegex(input, from, /(^|[^\w_])_([^_\n]+)_/g, (match) => ({type: 'emphasis' as const,text: match[2],- }), 1),+ }), 1, context.search.exec),// 裸链接:http(s) 走共享 BARE_HTTP_URL_RE_SOURCE + clipBareHttpAutolink// (CJK/全角标点/包裹括号/尾部句读同一套);会话/项目深链仍是。project// 白名单与桌面 PROJECT_DEEP_LINK_RE_SOURCE 同口径,含尾部负向前瞻@@ -1322,6 +1327,7 @@ function matchBareUrl(input: string, from: number) {function matchLocalPathLink(input: string,from: number,+ context: InlineParseContext,): { index: number; end: number; inline: MobileMarkdownInline } | null {if (!input.includes('](')) return null;// 正则每次调用新建:g 标志的 lastIndex 是可变状态,模块级共享在提前 return /@@ -1329,7 +1335,7 @@ function matchLocalPathLink(const re = /\[([^\]]+)\]\((<[^>\n]+>|(?:[^()\n]|\([^()\n]*\))+)\)/g;re.lastIndex = from;let match: RegExpExecArray | null;- while ((match = re.exec(input)) !== null) {+ while ((match = context.search.exec(re)) !== null) {if (match.index > 0 && input[match.index - 1] === '!') continue;const url = parseLocalMarkdownDestination(match[2]);if (!url || !classifyChatPathLinkTarget(url)) continue;@@ -1366,7 +1372,7 @@ function matchBareFilePathLink(// 「注释内」。命中在注释里的跳过(桌面侧注释是 html 节点、插件根本看不到,压制// 才是同口径),注释外的照常识别。for (;;) {- const match = findBareFilePathMatch(input, cursor);+ const match = findBareFilePathMatch(input, cursor, context.search.exec);if (!match) return null;if (needsCommentCheck) {if (isInsideHtmlComment(context.commentProbe(), match.index, startsInsideHtmlComment)) {@@ -1396,9 +1402,10 @@ function matchRegex(regex: RegExp,build: (match: RegExpExecArray) => MobileMarkdownInline,prefixGroupIndex?: number,+ search?: ReturnType<typeof createMarkdownRegexSearch>['exec'],): { index: number; end: number; inline: MobileMarkdownInline } | null {regex.lastIndex = from;- const match = regex.exec(input);+ const match = search ? search(regex) : regex.exec(input);if (!match || match.index < from) return null;const prefixLength = prefixGroupIndex === undefined ? 0 : (match[prefixGroupIndex]?.length ?? 0);return {@@ -1542,7 +1549,7 @@ function matchHtmlImage(const { startsInsideHtmlComment } = context;HTML_IMG_TAG_RE.lastIndex = from;let match: RegExpExecArray | null;- while ((match = HTML_IMG_TAG_RE.exec(input)) !== null) {+ while ((match = context.search.exec(HTML_IMG_TAG_RE)) !== null) {// 注释里的 <img> 是被注释掉的内容,跳过;注释外的照常转换。if (isInsideHtmlComment(codeBlanked, match.index, startsInsideHtmlComment)) continue;// \<img ...> 转义的 < 使标签保持字面(与 Markdown 图片路径同口径,review 实捉)。@@ -1693,7 +1700,7 @@ function matchMarkdownImage(for (const matcher of matchers) {matcher.re.lastIndex = from;let candidate: RegExpExecArray | null;- while ((candidate = matcher.re.exec(input)) !== null) {+ while ((candidate = context.search.exec(matcher.re)) !== null) {const rawUrl = matcher.local ? parseLocalMarkdownDestination(candidate[2]) : candidate[2];// The destination parser also handles standard titles and angle brackets// for remote images, just as it does for local image paths.