-
-
Notifications
You must be signed in to change notification settings - Fork 23.1k
feat: Add Mermaid renderer support in chat window (#5402) #5410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Add mermaid.js dependency to render Mermaid diagrams - Modify CodeBlock component to detect and render Mermaid code blocks - Support theme-aware rendering (light/dark mode) - Add error handling with fallback to syntax highlighting - Preserve existing copy/download functionality
Summary of ChangesHello @aibysid, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the chat window's capabilities by introducing support for rendering Mermaid diagrams. By integrating the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces support for rendering Mermaid diagrams within code blocks, which is an excellent addition. The implementation correctly identifies Mermaid code, renders it, and gracefully handles various states such as loading and errors. It also supports theme-aware rendering for light and dark modes. My primary feedback concerns the performance of the rendering logic. Currently, it re-renders on every change to the code block's value. This can be inefficient for streaming responses, which are common in chat applications. I have provided a suggestion to debounce the render function to mitigate this potential performance issue.
| useEffect(() => { | ||
| if (!isMermaid || !value) return | ||
|
|
||
| mermaid.initialize({ | ||
| startOnLoad: false, | ||
| theme: theme.palette.mode === 'dark' ? 'dark' : 'default', | ||
| securityLevel: 'strict' | ||
| }) | ||
|
|
||
| const renderMermaid = async () => { | ||
| try { | ||
| setMermaidError(null) | ||
| const { svg } = await mermaid.render(diagramIdRef.current, value) | ||
| setMermaidSvg(svg) | ||
| } catch (error) { | ||
| setMermaidError(error.message) | ||
| setMermaidSvg(null) | ||
| } | ||
| } | ||
|
|
||
| renderMermaid() | ||
| }, [isMermaid, value, theme.palette.mode]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of the useEffect hook for rendering Mermaid diagrams will execute on every change to the value prop. In a chat application with streaming responses, this can lead to excessive re-renders and performance issues, as mermaid.initialize and mermaid.render would be called for each new token. This can also result in a poor user experience with flickering error messages for incomplete diagrams.
To optimize this, I recommend debouncing the rendering logic. This will ensure that the rendering only happens after the code has stopped changing for a brief period.
useEffect(() => {
if (!isMermaid || !value) return
const timer = setTimeout(() => {
mermaid.initialize({
startOnLoad: false,
theme: theme.palette.mode === 'dark' ? 'dark' : 'default',
securityLevel: 'strict'
})
const renderMermaid = async () => {
try {
setMermaidError(null)
const { svg } = await mermaid.render(diagramIdRef.current, value)
setMermaidSvg(svg)
} catch (error) {
setMermaidError(error.message)
setMermaidSvg(null)
}
}
renderMermaid()
}, 300)
return () => clearTimeout(timer)
}, [isMermaid, value, theme.palette.mode])|
+1 |
| useEffect(() => { | ||
| if (!isMermaid || !value) return | ||
|
|
||
| mermaid.initialize({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would be an issue, because when streaming, value changes, this will cause multiple initialization
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrapped the rendering logic in a setTimeout with a 300ms delay
…ers during streaming
Sample output:
