File size: 18,455 Bytes
d5c104e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
import React, { useEffect, useRef, useState } from 'react';
import Markdown from 'markdown-to-jsx';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { vscDarkPlus } from 'react-syntax-highlighter/dist/esm/styles/prism';
import { FcCheckmark } from 'react-icons/fc';
import { BiSolidCopy } from 'react-icons/bi';
import { LuDownload } from 'react-icons/lu';
import katex from 'katex';
import 'katex/dist/katex.min.css';
import './Streaming.css';
import './SourceRef.css';
// Math rendering components
const InlineMath = ({ children }) => {
const mathRef = useRef(null);
useEffect(() => {
if (mathRef.current && children) {
try {
katex.render(children.toString(), mathRef.current, {
throwOnError: false,
displayMode: false
});
} catch (e) {
mathRef.current.innerHTML = `<span style="color: red;">Error: ${e.message}</span>`;
}
}
}, [children]);
return <span ref={mathRef} className="inline-math" />;
};
const BlockMath = ({ children }) => {
const mathRef = useRef(null);
useEffect(() => {
if (mathRef.current && children) {
try {
katex.render(children.toString(), mathRef.current, {
throwOnError: false,
displayMode: true
});
} catch (e) {
mathRef.current.innerHTML = `<div style="color: red;">Error: ${e.message}</div>`;
}
}
}, [children]);
return <div ref={mathRef} className="block-math" />;
};
// Helper function to normalize various citation formats (e.g., [1,2], [1, 2]) into the standard [1][2] format
const normalizeCitations = (text) => {
if (!text) return '';
// First, temporarily replace math expressions to protect them from citation processing
const mathPlaceholders = [];
let mathIndex = 0;
// Replace block math
text = text.replace(/\$\$([\s\S]*?)\$\$/g, (match) => {
const placeholder = `__BLOCK_MATH_${mathIndex}__`;
mathPlaceholders[mathIndex] = match;
mathIndex++;
return placeholder;
});
// Replace inline math
text = text.replace(/\$([^\$\n]+?)\$/g, (match) => {
const placeholder = `__INLINE_MATH_${mathIndex}__`;
mathPlaceholders[mathIndex] = match;
mathIndex++;
return placeholder;
});
// Process citations
const citationRegex = /\[(\d+(?:,\s*\d+)+)\]/g;
text = text.replace(citationRegex, (match, capturedNumbers) => {
const numbers = capturedNumbers
.split(/,\s*/)
.map(numStr => numStr.trim())
.filter(Boolean);
if (numbers.length <= 1) {
return match;
}
return numbers.map(num => `[${num}]`).join('');
});
// Restore math expressions
mathPlaceholders.forEach((math, index) => {
const placeholder = math.startsWith('$$') ? `__BLOCK_MATH_${index}__` : `__INLINE_MATH_${index}__`;
text = text.replace(placeholder, math);
});
return text;
};
// Streaming component for rendering markdown content
const Streaming = ({ content, isStreaming, onContentRef, showSourcePopup, hideSourcePopup }) => {
const contentRef = useRef(null);
useEffect(() => {
if (contentRef.current && onContentRef) {
onContentRef(contentRef.current);
}
}, [content, onContentRef]);
const displayContent = isStreaming ? `${content}▌` : (content || '');
const normalizedContent = normalizeCitations(displayContent);
// CodeBlock component with copy functionality
const CodeBlock = ({ language, codeString }) => {
const [copied, setCopied] = useState(false);
const [showCopyTooltip, setShowCopyTooltip] = useState(true);
const handleCopy = () => {
const textToCopy = String(codeString).replace(/\n$/, '');
navigator.clipboard.writeText(textToCopy).then(() => {
setCopied(true);
setShowCopyTooltip(false);
setTimeout(() => setCopied(false), 2000);
}).catch(err => {
console.error('Failed to copy:', err);
});
};
return (
<div className="code-block-container">
<div className="code-block-header">
<span>{language}</span>
<button className={`code-copy-button ${copied ? 'copied' : ''}`} onClick={handleCopy}>
{copied ? <FcCheckmark /> : <BiSolidCopy />}
{showCopyTooltip && <span className="tooltip">Copy Code</span>}
</button>
</div>
<SyntaxHighlighter
style={vscDarkPlus}
language={language}
PreTag="div"
>
{String(codeString).replace(/\n$/, '')}
</SyntaxHighlighter>
</div>
);
};
// TableWithCopy component with copy and download functionality
const TableWithCopy = ({ children, ...props }) => {
const [copied, setCopied] = useState(false);
const [fadingCopy, setFadingCopy] = useState(false);
const [showCopyTooltip, setShowCopyTooltip] = useState(true);
const [downloaded, setDownloaded] = useState(false);
const [fadingDownload, setFadingDownload] = useState(false);
const [showDownloadTooltip, setShowDownloadTooltip] = useState(true);
const tableRef = useRef(null);
const handleCopy = () => {
if (tableRef.current) {
const rows = Array.from(tableRef.current.querySelectorAll('tr'));
const csvData = rows.map(row => {
const cells = Array.from(row.querySelectorAll('th, td'));
return cells.map(cell => {
let text = cell.innerText.trim();
if (text.includes('"') || text.includes(',') || text.includes('\n')) {
text = '"' + text.replace(/"/g, '""') + '"';
}
return text;
}).join(',');
}).join('\n');
navigator.clipboard.writeText(csvData).then(() => {
setCopied(true);
setShowCopyTooltip(false);
setTimeout(() => {
setFadingCopy(true);
setTimeout(() => {
setCopied(false);
setFadingCopy(false);
}, 200);
}, 2000);
}).catch(err => {
console.error('Failed to copy table:', err);
});
}
};
const handleDownload = () => {
if (tableRef.current) {
const rows = Array.from(tableRef.current.querySelectorAll('tr'));
const tsvData = rows.map(row => {
const cells = Array.from(row.querySelectorAll('th, td'));
return cells.map(cell => {
let text = cell.innerText.trim();
if (text.includes('"') || text.includes('\t') || text.includes('\n')) {
text = '"' + text.replace(/"/g, '""') + '"';
}
return text;
}).join('\t');
}).join('\n');
const blob = new Blob([tsvData], { type: 'application/vnd.ms-excel' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'table.xls';
a.click();
URL.revokeObjectURL(url);
setDownloaded(true);
setShowDownloadTooltip(false);
setTimeout(() => {
setFadingDownload(true);
setTimeout(() => {
setDownloaded(false);
setFadingDownload(false);
}, 200);
}, 2000);
}
};
return (
<div className="table-container">
<button className={`table-copy-button ${copied ? 'copied' : ''} ${fadingCopy ? 'fading' : ''}`} onClick={handleCopy}>
{copied ? <FcCheckmark /> : <BiSolidCopy />}
{showCopyTooltip && <span className="tooltip">Copy Table</span>}
</button>
<table ref={tableRef} {...props}>{children}</table>
<button className={`table-download-button ${downloaded ? 'downloaded' : ''} ${fadingDownload ? 'fading' : ''}`} onClick={handleDownload}>
{downloaded ? <FcCheckmark /> : <LuDownload />}
{showDownloadTooltip && <span className="tooltip">Download Table</span>}
</button>
</div>
);
};
// Custom renderer for text nodes to handle source references and math
const renderWithSourceRefsAndMath = (elementType) => {
const ElementComponent = elementType; // e.g., 'p', 'li'
// Helper to gather plain text
const getFullText = (something) => {
if (typeof something === 'string') return something;
if (Array.isArray(something)) return something.map(getFullText).join('');
if (React.isValidElement(something) && something.props?.children)
return getFullText(React.Children.toArray(something.props.children));
return '';
};
return ({ children, ...props }) => {
// Plain‑text version of this block (paragraph / list‑item)
const fullText = getFullText(children);
// Same regex the backend used
const sentenceRegex = /[^.!?\n]+[.!?]+[\])'"`'"]*|[^.!?\n]+$/g;
const sentencesArr = fullText.match(sentenceRegex) || [fullText];
// Helper function to find the sentence that contains position `pos`
const sentenceByPos = (pos) => {
let run = 0;
for (const s of sentencesArr) {
const end = run + s.length;
if (pos >= run && pos < end) return s.trim();
run = end;
}
return fullText.trim();
};
// Cursor that advances through fullText so each subsequent
// indexOf search starts AFTER the previous match
let searchCursor = 0;
// Recursive renderer that preserves existing markup and adds math support
const processNode = (node, keyPrefix = 'node') => {
if (typeof node === 'string') {
const parts = [];
let lastIndex = 0;
// First process block math
const blockMathRegex = /\$\$([\s\S]*?)\$\$/g;
let blockMatch;
while ((blockMatch = blockMathRegex.exec(node))) {
// Process text before the math expression for citations
const textBefore = node.slice(lastIndex, blockMatch.index);
if (textBefore) {
parts.push(...processCitations(textBefore, keyPrefix, lastIndex));
}
// Add the block math component
parts.push(
<BlockMath key={`${keyPrefix}-block-math-${blockMatch.index}`}>
{blockMatch[1]}
</BlockMath>
);
lastIndex = blockMatch.index + blockMatch[0].length;
}
// Process remaining text for inline math and citations
const remainingText = node.slice(lastIndex);
if (remainingText) {
parts.push(...processInlineMathAndCitations(remainingText, keyPrefix, lastIndex));
}
return parts;
}
// For non‑string children, recurse (preserves <em>, <strong>, links, etc.)
if (React.isValidElement(node) && node.props?.children) {
const processed = React.Children.map(node.props.children, (child, i) =>
processNode(child, `${keyPrefix}-${i}`)
);
return React.cloneElement(node, { children: processed });
}
return node; // element without children or unknown type
};
// Helper function to process inline math and citations
const processInlineMathAndCitations = (text, keyPrefix, offset) => {
const parts = [];
let lastIndex = 0;
// Combined regex for inline math and citations
const combinedRegex = /\$([^\$\n]+?)\$|\[(\d+)\]/g;
let match;
while ((match = combinedRegex.exec(text))) {
// Add text before the match
if (match.index > lastIndex) {
parts.push(text.slice(lastIndex, match.index));
}
if (match[1] !== undefined) {
// It's inline math
parts.push(
<InlineMath key={`${keyPrefix}-inline-math-${match.index}`}>
{match[1]}
</InlineMath>
);
} else if (match[2] !== undefined) {
// It's a citation
const num = parseInt(match[2], 10);
const absIdx = fullText.indexOf(match[0], searchCursor);
if (absIdx !== -1) searchCursor = absIdx + match[0].length;
const sentenceForPopup = sentenceByPos(absIdx);
parts.push(
<sup
key={`${keyPrefix}-ref-${num}-${match.index}`}
className="source-reference"
onMouseEnter={(e) =>
showSourcePopup &&
showSourcePopup(num - 1, e.target, sentenceForPopup)
}
onMouseLeave={hideSourcePopup}
>
{num}
</sup>
);
}
lastIndex = match.index + match[0].length;
}
// Add remaining text
if (lastIndex < text.length) {
parts.push(text.slice(lastIndex));
}
return parts;
};
// Helper function to process only citations
const processCitations = (text, keyPrefix, offset) => {
const citationRegex = /\[(\d+)\]/g;
let last = 0;
let parts = [];
let m;
while ((m = citationRegex.exec(text))) {
const sliceBefore = text.slice(last, m.index);
if (sliceBefore) parts.push(sliceBefore);
const localIdx = m.index;
const num = parseInt(m[1], 10);
const citStr = m[0];
// Find this specific occurrence in fullText, starting at searchCursor
const absIdx = fullText.indexOf(citStr, searchCursor);
if (absIdx !== -1) searchCursor = absIdx + citStr.length;
const sentenceForPopup = sentenceByPos(absIdx);
parts.push(
<sup
key={`${keyPrefix}-ref-${num}-${localIdx}`}
className="source-reference"
onMouseEnter={(e) =>
showSourcePopup &&
showSourcePopup(num - 1, e.target, sentenceForPopup)
}
onMouseLeave={hideSourcePopup}
>
{num}
</sup>
);
last = localIdx + citStr.length;
}
if (last < text.length) parts.push(text.slice(last));
return parts;
};
const processedChildren = React.Children.map(children, (child, i) =>
processNode(child, `root-${i}`)
);
// Render original element (p, li, …) with processed children
return <ElementComponent {...props}>{processedChildren}</ElementComponent>;
};
};
return (
<div className="streaming-content" ref={contentRef}>
<Markdown
options={{
wrapper: React.Fragment, // Use Fragment to avoid wrapper div
forceBlock: false,
forceInline: false,
overrides: {
p: {
component: renderWithSourceRefsAndMath('p')
},
li: {
component: renderWithSourceRefsAndMath('li')
},
h1: {
component: renderWithSourceRefsAndMath('h1')
},
h2: {
component: renderWithSourceRefsAndMath('h2')
},
h3: {
component: renderWithSourceRefsAndMath('h3')
},
h4: {
component: renderWithSourceRefsAndMath('h4')
},
h5: {
component: renderWithSourceRefsAndMath('h5')
},
h6: {
component: renderWithSourceRefsAndMath('h6')
},
pre: {
component: ({ children, ...props }) => {
let codeElement = null;
let codeString = '';
let language = '';
React.Children.forEach(children, (child) => {
if (React.isValidElement(child)) { // Check for element, no type restriction
codeElement = child;
const className = child.props?.className || '';
const match = /(?:lang|language)-(\w+)/.exec(className);
if (match) {
language = match[1];
}
// Flatten children safely
codeString = React.Children.toArray(child.props?.children).join('');
}
});
// Always start code blocks on a new line
return (
<>
<div style={{ display: 'block', width: '100%', height: 0, margin: 0, padding: 0 }} />
{codeElement ? (
<CodeBlock
language={language || 'plaintext'} // Default for no-lang blocks
codeString={codeString}
/>
) : (
<pre {...props}>{children}</pre>
)}
</>
);
}
},
code: {
component: ({ className, children, ...props }) => {
// This handles inline code only
// Block code is handled by the pre component above
return (
<code className={className} {...props}>
{children}
</code>
);
}
},
table: {
component: TableWithCopy
},
a: {
component: ({ children, href, ...props }) => {
return (
<a
href={href}
target="_blank"
rel="noopener noreferrer"
className="markdown-link"
{...props}
>
{children}
</a>
);
}
},
blockquote: {
component: ({ children, ...props }) => {
return (
<blockquote className="markdown-blockquote" {...props}>
{children}
</blockquote>
);
}
}
}
}}
>
{normalizedContent}
</Markdown>
</div>
);
};
export default Streaming; |