diff --git a/src/public/app/widgets/llm_chat_panel.ts b/src/public/app/widgets/llm_chat_panel.ts index 4aac66096..21133039c 100644 --- a/src/public/app/widgets/llm_chat_panel.ts +++ b/src/public/app/widgets/llm_chat_panel.ts @@ -7,6 +7,7 @@ import { t } from "../services/i18n.js"; import libraryLoader from "../services/library_loader.js"; import { applySyntaxHighlight } from "../services/syntax_highlight.js"; import options from "../services/options.js"; +import { marked } from "marked"; // Import the LLM Chat CSS (async function() { @@ -429,13 +430,6 @@ export default class LlmChatPanel extends BasicWidget { private formatMarkdown(content: string): string { if (!content) return ''; - // Check if content contains HTML sections for thinking visualization - if (content.includes('
') || - content.includes('
')) { - console.log('Detected thinking process visualization in response'); - // For content with HTML thinking visualizations, we need to protect them - } - // First, extract HTML thinking visualization to protect it from replacements const thinkingBlocks: string[] = []; let processedContent = content.replace(/
/g, (match) => { @@ -444,26 +438,21 @@ export default class LlmChatPanel extends BasicWidget { return placeholder; }); - // Then extract code blocks to protect them from other replacements - const codeBlocks: string[] = []; - processedContent = processedContent.replace(/```(\w+)?\n([\s\S]+?)\n```/gs, (match, language, code) => { - const placeholder = `__CODE_BLOCK_${codeBlocks.length}__`; - const languageClass = language ? ` language-${language}` : ''; - codeBlocks.push(`
${code}
`); - return placeholder; + // Use marked library to parse the markdown + const markedContent = marked(processedContent, { + breaks: true, // Convert line breaks to
+ gfm: true, // Enable GitHub Flavored Markdown + silent: true // Ignore errors }); - // Apply other markdown formatting - processedContent = processedContent - .replace(/\*\*(.*?)\*\*/g, '$1') - .replace(/\*(.*?)\*/g, '$1') - .replace(/`([^`]+)`/g, '$1') - .replace(/\n/g, '
'); - - // Restore code blocks - codeBlocks.forEach((block, index) => { - processedContent = processedContent.replace(`__CODE_BLOCK_${index}__`, block); - }); + // Handle potential promise (though it shouldn't be with our options) + if (typeof markedContent === 'string') { + processedContent = markedContent; + } else { + console.warn('Marked returned a promise unexpectedly'); + // Use the original content as fallback + processedContent = content; + } // Restore thinking visualization blocks thinkingBlocks.forEach((block, index) => {