Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(im): include closing tags in Telegram text budget
Zhao Dengfeng9f15b27f614b
diff --git a/packages/lizi-im/src/telegram/__tests__/htmlCap.test.ts b/packages/lizi-im/src/telegram/__tests__/htmlCap.test.tsindex a2165d38d7..32375a7f1c 100644--- a/packages/lizi-im/src/telegram/__tests__/htmlCap.test.ts+++ b/packages/lizi-im/src/telegram/__tests__/htmlCap.test.ts@@ -28,10 +28,16 @@ describe('capRenderedText', () => {if (amp !== -1) expect(out.slice(amp)).toContain(';');});- it('嵌套标签按栈序逆序闭合', () => {+ it.each([+ [10, '<b>…</b>'],+ [15, '<b><i>…</i></b>'],+ [20, '<b><i>zzzzz…</i></b>'],+ [30, '<b><i>zzzzzzzzzzzzzzz…</i></b>'],+ ])('嵌套标签按栈序逆序闭合且不超过上限: %s', (maxChars, expected) => {const html = `<b><i>${'z'.repeat(40)}</i></b>`;- const out = capRenderedText(html, 15);- // 先闭合内层 i 再闭合外层 b- expect(out.endsWith('…</i></b>')).toBe(true);+ const out = capRenderedText(html, maxChars);+ // 省略号和闭合标签都必须计入长度预算;嵌套标签按栈序逆序闭合。+ expect(out).toBe(expected);+ expect(out.length).toBeLessThanOrEqual(maxChars);});});diff --git a/packages/lizi-im/src/telegram/htmlCap.ts b/packages/lizi-im/src/telegram/htmlCap.tsindex 9cb8779d2f..11d57233f4 100644--- a/packages/lizi-im/src/telegram/htmlCap.ts+++ b/packages/lizi-im/src/telegram/htmlCap.ts@@ -14,9 +14,8 @@* 把 `html` 安全截断到不超过 `maxChars`(含尾部省略号与补闭合标签)。* `html.length <= maxChars` 时原样返回。*/-export function capRenderedText(html: string, maxChars: number): string {- if (html.length <= maxChars) return html;- let cut = html.slice(0, maxChars - 1);+function renderCandidate(html: string, cutLength: number): { cut: string; closers: string } {+ let cut = html.slice(0, cutLength);const lastOpen = cut.lastIndexOf('<');if (lastOpen > cut.lastIndexOf('>')) cut = cut.slice(0, lastOpen);const lastAmp = cut.lastIndexOf('&');@@ -39,5 +38,19 @@ export function capRenderedText(html: string, maxChars: number): string {.reverse().map((name) => `</${name}>`).join('');- return `${cut}…${closers}`;+ return { cut, closers };+}++export function capRenderedText(html: string, maxChars: number): string {+ if (html.length <= maxChars) return html;+ let cutLength = maxChars - 1;+ while (cutLength > 0) {+ const { cut, closers } = renderCandidate(html, cutLength);+ const rendered = `${cut}…${closers}`;+ if (rendered.length <= maxChars) return rendered;+ // 截点和当前标签栈的闭合后缀共用同一长度预算;缩短后需重新计算标签栈。+ // 逐字符回退并重新计算标签栈,直到完整结果不超过上限。+ cutLength -= 1;+ }+ return '…';}