Merge pull request #2155 from danielaskdd/latex-loading
Feat(webui): Prevent LaTeX Parsing Errors During Streaming
This commit is contained in:
commit
37bc012273
2 changed files with 31 additions and 3 deletions
|
|
@ -27,6 +27,11 @@ export type MessageWithError = Message & {
|
||||||
* Used to persist the rendering state across updates and prevent flickering.
|
* Used to persist the rendering state across updates and prevent flickering.
|
||||||
*/
|
*/
|
||||||
mermaidRendered?: boolean
|
mermaidRendered?: boolean
|
||||||
|
/**
|
||||||
|
* Indicates if the LaTeX formulas in this message are complete and ready for rendering.
|
||||||
|
* Used to prevent red error text during streaming of incomplete LaTeX formulas.
|
||||||
|
*/
|
||||||
|
latexRendered?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restore original component definition and export
|
// Restore original component definition and export
|
||||||
|
|
@ -138,7 +143,7 @@ export const ChatMessage = ({ message }: { message: MessageWithError }) => { //
|
||||||
remarkPlugins={[remarkGfm, remarkFootnotes, remarkMath]}
|
remarkPlugins={[remarkGfm, remarkFootnotes, remarkMath]}
|
||||||
rehypePlugins={[
|
rehypePlugins={[
|
||||||
rehypeRaw,
|
rehypeRaw,
|
||||||
...(katexPlugin ? [[katexPlugin, {
|
...((katexPlugin && (message.latexRendered ?? true)) ? [[katexPlugin, {
|
||||||
errorColor: theme === 'dark' ? '#ef4444' : '#dc2626',
|
errorColor: theme === 'dark' ? '#ef4444' : '#dc2626',
|
||||||
throwOnError: false,
|
throwOnError: false,
|
||||||
displayMode: false,
|
displayMode: false,
|
||||||
|
|
@ -168,7 +173,7 @@ export const ChatMessage = ({ message }: { message: MessageWithError }) => { //
|
||||||
remarkPlugins={[remarkGfm, remarkFootnotes, remarkMath]}
|
remarkPlugins={[remarkGfm, remarkFootnotes, remarkMath]}
|
||||||
rehypePlugins={[
|
rehypePlugins={[
|
||||||
rehypeRaw,
|
rehypeRaw,
|
||||||
...(katexPlugin ? [[
|
...((katexPlugin && (message.latexRendered ?? true)) ? [[
|
||||||
katexPlugin,
|
katexPlugin,
|
||||||
{
|
{
|
||||||
errorColor: theme === 'dark' ? '#ef4444' : '#dc2626',
|
errorColor: theme === 'dark' ? '#ef4444' : '#dc2626',
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,22 @@ const generateUniqueId = () => {
|
||||||
return `id-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
return `id-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// LaTeX completeness detection function
|
||||||
|
const detectLatexCompleteness = (content: string): boolean => {
|
||||||
|
// Check for unclosed block-level LaTeX formulas ($$...$$)
|
||||||
|
const blockLatexMatches = content.match(/\$\$/g) || []
|
||||||
|
const hasUnclosedBlock = blockLatexMatches.length % 2 !== 0
|
||||||
|
|
||||||
|
// Check for unclosed inline LaTeX formulas ($...$, but not $$)
|
||||||
|
// Remove all block formulas first to avoid interference
|
||||||
|
const contentWithoutBlocks = content.replace(/\$\$[\s\S]*?\$\$/g, '')
|
||||||
|
const inlineLatexMatches = contentWithoutBlocks.match(/(?<!\$)\$(?!\$)/g) || []
|
||||||
|
const hasUnclosedInline = inlineLatexMatches.length % 2 !== 0
|
||||||
|
|
||||||
|
// LaTeX is complete if there are no unclosed formulas
|
||||||
|
return !hasUnclosedBlock && !hasUnclosedInline
|
||||||
|
}
|
||||||
|
|
||||||
// Robust COT parsing function to handle multiple think blocks and edge cases
|
// Robust COT parsing function to handle multiple think blocks and edge cases
|
||||||
const parseCOTContent = (content: string) => {
|
const parseCOTContent = (content: string) => {
|
||||||
const thinkStartTag = '<think>'
|
const thinkStartTag = '<think>'
|
||||||
|
|
@ -97,7 +113,8 @@ export default function RetrievalTesting() {
|
||||||
return {
|
return {
|
||||||
...msg,
|
...msg,
|
||||||
id: msgWithError.id || `hist-${Date.now()}-${index}`, // Add ID if missing
|
id: msgWithError.id || `hist-${Date.now()}-${index}`, // Add ID if missing
|
||||||
mermaidRendered: msgWithError.mermaidRendered ?? true // Assume historical mermaid is rendered
|
mermaidRendered: msgWithError.mermaidRendered ?? true, // Assume historical mermaid is rendered
|
||||||
|
latexRendered: msgWithError.latexRendered ?? true // Assume historical LaTeX is rendered
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error processing message:', error)
|
console.error('Error processing message:', error)
|
||||||
|
|
@ -203,6 +220,7 @@ export default function RetrievalTesting() {
|
||||||
content: '',
|
content: '',
|
||||||
role: 'assistant',
|
role: 'assistant',
|
||||||
mermaidRendered: false,
|
mermaidRendered: false,
|
||||||
|
latexRendered: false, // Explicitly initialize to false
|
||||||
thinkingTime: null, // Explicitly initialize to null
|
thinkingTime: null, // Explicitly initialize to null
|
||||||
thinkingContent: undefined, // Explicitly initialize to undefined
|
thinkingContent: undefined, // Explicitly initialize to undefined
|
||||||
displayContent: undefined, // Explicitly initialize to undefined
|
displayContent: undefined, // Explicitly initialize to undefined
|
||||||
|
|
@ -282,6 +300,10 @@ export default function RetrievalTesting() {
|
||||||
}
|
}
|
||||||
assistantMessage.mermaidRendered = mermaidRendered
|
assistantMessage.mermaidRendered = mermaidRendered
|
||||||
|
|
||||||
|
// Detect if the assistant message contains complete LaTeX formulas
|
||||||
|
const latexRendered = detectLatexCompleteness(assistantMessage.content)
|
||||||
|
assistantMessage.latexRendered = latexRendered
|
||||||
|
|
||||||
// Single unified update to avoid race conditions
|
// Single unified update to avoid race conditions
|
||||||
setMessages((prev) => {
|
setMessages((prev) => {
|
||||||
const newMessages = [...prev]
|
const newMessages = [...prev]
|
||||||
|
|
@ -295,6 +317,7 @@ export default function RetrievalTesting() {
|
||||||
isThinking: assistantMessage.isThinking,
|
isThinking: assistantMessage.isThinking,
|
||||||
isError: isError,
|
isError: isError,
|
||||||
mermaidRendered: assistantMessage.mermaidRendered,
|
mermaidRendered: assistantMessage.mermaidRendered,
|
||||||
|
latexRendered: assistantMessage.latexRendered,
|
||||||
thinkingTime: assistantMessage.thinkingTime
|
thinkingTime: assistantMessage.thinkingTime
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue