Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): strip the tags a tool-call preview reassembles

Elian Doran347043b1d8a0
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.spec.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.spec.tsx
index 4d5a904a58..447d47dca6 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.spec.tsx
@@ -65,4 +65,8 @@ describe("markdownToPlainPreview", () => {
it("decodes numeric and named entities, and keeps the ones it cannot", () => {
expect(markdownToPlainPreview("ABC & � &bogus;")).toBe("ABC & � &bogus;");
});
+
+ it("strips a tag that removing another one leaves behind", () => {
+ expect(markdownToPlainPreview("<<b>script>alert(1)<</b>/script>")).toBe("alert(1)");
+ });
});
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
index bf2f128b18..a6a61972f2 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
@@ -796,8 +796,7 @@ export function markdownToPlainPreview(markdown: string): string {
// The Markdown export keeps what it cannot express (reference links, `<kbd>`, icon spans) as
// HTML. Entities are decoded last, so an escaped `&lt;b&gt;` stays text.
- return markdown
- .replace(/<\/?[a-z][^>]*>/gi, "")
+ return stripTags(markdown)
.replace(/!\[[^\]]*\]\([^)]*\)/g, "")
.replace(/\[([^\]]*)\]\([^)]*\)/g, "$1")
.replace(/^\s*\|?[\s:|-]+\|?\s*$/gm, "")
@@ -808,6 +807,19 @@ export function markdownToPlainPreview(markdown: string): string {
.trim();
}
+/**
+ * Removes HTML tags until none is left, since removing one can join the text around it into another.
+ */
+function stripTags(html: string): string {
+ let previous: string;
+ let text = html;
+ do {
+ previous = text;
+ text = text.replace(/<\/?[a-z][^>]*>/gi, "");
+ } while (text !== previous);
+ return text;
+}
+
const NAMED_ENTITIES: Record<string, string> = { nbsp: " ", amp: "&", lt: "<", gt: ">", quot: "\"", apos: "'" };
function decodeEntity(entity: string, name: string): string {