Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 364
feat(web): render mermaid diagrams in chat answers#1241
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
Closed
wilson101xx
wants to merge
3
commits into
sourcebot-dev:main
from
wilson101xx:adam.wilson/diagram-rendering
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 62 additions & 2 deletions
64 packages/web/src/features/chat/components/chatThread/answerCard.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3,10 +3,10 @@ | ||
| import { useExtractTOCItems } from "../../useTOCItems"; | ||
| import { TableOfContents } from "./tableOfContents"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { TableOfContentsIcon, ThumbsDown, ThumbsUp } from "lucide-react"; | ||
| import { Sparkles, TableOfContentsIcon, ThumbsDown, ThumbsUp } from "lucide-react"; | ||
| import { Separator } from "@/components/ui/separator"; | ||
| import { MarkdownRenderer } from "./markdownRenderer"; | ||
| import { forwardRef, memo, useCallback, useImperativeHandle, useRef, useState } from "react"; | ||
| import { forwardRef, memo, useCallback, useImperativeHandle, useMemo, useRef, useState } from "react"; | ||
| import { Toggle } from "@/components/ui/toggle"; | ||
| import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; | ||
| import { CopyIconButton } from "@/app/(app)/components/copyIconButton"; | ||
| @@ -19,15 +19,33 @@ import { LangfuseWeb } from "langfuse"; | ||
| import { env } from "@sourcebot/shared/client"; | ||
| import isEqual from "fast-deep-equal/react"; | ||
| import { FileSource } from "../../types"; | ||
| import { SUPPORTED_DIAGRAM_LANGUAGES } from "./diagramRenderer"; | ||
| interface AnswerCardProps { | ||
| answerText: string; | ||
| messageId: string; | ||
| chatId: string; | ||
| traceId?: string; | ||
| sources: FileSource[]; | ||
| /** | ||
| * When provided, a "Visualize" button is shown in the answer header. Clicking | ||
| * it sends a follow-up message asking the agent to render the answer as a | ||
| * diagram. If the answer already contains a diagram block, the button is | ||
| * relabeled to "New diagram" rather than hidden. The button is disabled | ||
| * while a stream is in flight. | ||
| */ | ||
coderabbitai[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| onVisualize?: () => void; | ||
| isVisualizeDisabled?: boolean; | ||
| } | ||
| const diagramFenceRegexes = SUPPORTED_DIAGRAM_LANGUAGES.map( | ||
| (lang) => new RegExp(`\`\`\`\\s*${lang}\\b`, 'i'), | ||
| ); | ||
| const answerContainsDiagram = (text: string): boolean => { | ||
| return diagramFenceRegexes.some((re) => re.test(text)); | ||
| }; | ||
| const langfuseWeb = env.NEXT_PUBLIC_LANGFUSE_PUBLIC_KEY ? new LangfuseWeb({ | ||
| publicKey: env.NEXT_PUBLIC_LANGFUSE_PUBLIC_KEY, | ||
| baseUrl: env.NEXT_PUBLIC_LANGFUSE_BASE_URL, | ||
| @@ -39,6 +57,8 @@ const AnswerCardComponent = forwardRef<HTMLDivElement, AnswerCardProps>(({ | ||
| chatId, | ||
| traceId, | ||
| sources, | ||
| onVisualize, | ||
| isVisualizeDisabled = false, | ||
| }, forwardedRef) => { | ||
| const markdownRendererRef = useRef<HTMLDivElement>(null); | ||
| // eslint-disable-next-line react-hooks/refs -- ref.current is passed to a custom hook, not used directly in render output | ||
| @@ -65,6 +85,26 @@ const AnswerCardComponent = forwardRef<HTMLDivElement, AnswerCardProps>(({ | ||
| return true; | ||
| }, [answerText, sources, chatId, captureEvent, toast]); | ||
| // Always show the Visualize button when the callback is wired up. The earlier | ||
| // "hide when a diagram is already present" heuristic was too aggressive — users | ||
| // often want a different angle on the same answer (e.g. a higher-level overview, | ||
| // or a sequence diagram instead of an architecture graph), and discoverability | ||
| // suffered when the button silently disappeared mid-answer. | ||
| const hasDiagram = useMemo(() => answerContainsDiagram(answerText), [answerText]); | ||
| const isVisualizeButtonVisible = !!onVisualize; | ||
| const visualizeButtonLabel = hasDiagram ? 'New diagram' : 'Visualize'; | ||
| const visualizeTooltip = hasDiagram | ||
| ? 'Generate another diagram (different angle or detail level)' | ||
| : 'Generate a diagram from this answer'; | ||
| const onVisualizeClick = useCallback(() => { | ||
| if (!onVisualize) { | ||
| return; | ||
| } | ||
| captureEvent('wa_chat_visualize_answer_pressed', { chatId, messageId }); | ||
| onVisualize(); | ||
| }, [onVisualize, captureEvent, chatId, messageId]); | ||
| const onFeedback = useCallback(async (feedbackType: 'like' | 'dislike') => { | ||
| setIsSubmittingFeedback(true); | ||
| @@ -114,6 +154,25 @@ const AnswerCardComponent = forwardRef<HTMLDivElement, AnswerCardProps>(({ | ||
| <div className="flex items-center justify-between mb-2"> | ||
| <p className="font-semibold text-muted-foreground">Answer</p> | ||
| <div className="flex items-center gap-2"> | ||
| {isVisualizeButtonVisible && ( | ||
| <Tooltip> | ||
| <TooltipTrigger asChild> | ||
| <Button | ||
| variant="ghost" | ||
| size="sm" | ||
| className="h-6 px-2 text-xs text-muted-foreground gap-1" | ||
| onClick={onVisualizeClick} | ||
| disabled={isVisualizeDisabled} | ||
| > | ||
| <Sparkles className="h-3.5 w-3.5" /> | ||
| {visualizeButtonLabel} | ||
| </Button> | ||
| </TooltipTrigger> | ||
| <TooltipContent side="bottom"> | ||
| {visualizeTooltip} | ||
| </TooltipContent> | ||
| </Tooltip> | ||
| )} | ||
| <Tooltip> | ||
| <TooltipTrigger asChild> | ||
| <CopyIconButton | ||
| @@ -155,6 +214,7 @@ const AnswerCardComponent = forwardRef<HTMLDivElement, AnswerCardProps>(({ | ||
| <MarkdownRenderer | ||
| ref={markdownRendererRef} | ||
| content={answerText} | ||
| chatId={chatId} | ||
| // scroll-mt offsets the scroll position for headings to take account | ||
| // of the sticky "answer" header. | ||
| className="prose prose-sm max-w-none prose-headings:scroll-mt-14" | ||
12 changes: 12 additions & 0 deletions
12 packages/web/src/features/chat/components/chatThread/chatThread.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11 packages/web/src/features/chat/components/chatThread/chatThreadListItem.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.