Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

feat: auto-fix chart response and rendering

Louisdcf2206a227b
diff --git a/web-app/src/components/MermaidError.tsx b/web-app/src/components/MermaidError.tsx
new file mode 100644
index 000000000..4b33738e5
--- /dev/null
+++ b/web-app/src/components/MermaidError.tsx
@@ -0,0 +1,179 @@
+import { memo, useEffect, useState } from 'react'
+import { useAppState } from '@/hooks/useAppState'
+import { useMessages } from '@/hooks/useMessages'
+import { useThreads } from '@/hooks/useThreads'
+import { useModelProvider } from '@/hooks/useModelProvider'
+import { sendCompletion } from '@/lib/completion'
+import { ContentType } from '@janhq/core'
+
+interface MermaidErrorComponentProps {
+ error: string
+ chart: string
+ retry: () => void
+ messageId?: string
+}
+
+/**
+ * Animated running cat component
+ * Displays a cute cat running animation when mermaid diagram errors
+ * Automatically attempts to fix the diagram when streaming finishes
+ */
+function MermaidErrorComponent({
+ error,
+ chart,
+ retry,
+ messageId,
+}: MermaidErrorComponentProps) {
+ const [isFixing, setIsFixing] = useState(false)
+ const [hasAttemptedFix, setHasAttemptedFix] = useState(false)
+
+ const streamingContent = useAppState((state) => state.streamingContent)
+ const isStreaming =
+ streamingContent !== undefined && streamingContent !== null
+ const getCurrentThread = useThreads((state) => state.getCurrentThread)
+ const getMessages = useMessages((state) => state.getMessages)
+ const updateMessage = useMessages((state) => state.updateMessage)
+ const selectedModel = useModelProvider((state) => state.selectedModel)
+ const getProviderByName = useModelProvider((state) => state.getProviderByName)
+ const selectedProvider = useModelProvider((state) => state.selectedProvider)
+
+ useEffect(() => {
+ // Only attempt to fix once when streaming finishes
+ if (!isStreaming && !hasAttemptedFix && !isFixing && chart) {
+ fixMermaidDiagram()
+ }
+ }, [isStreaming, hasAttemptedFix, isFixing, chart])
+
+ const fixMermaidDiagram = async () => {
+ const thread = getCurrentThread()
+ if (!thread || !selectedModel || !selectedProvider) {
+ return
+ }
+
+ const provider = getProviderByName(selectedProvider)
+ if (!provider) {
+ return
+ }
+
+ setIsFixing(true)
+ setHasAttemptedFix(true)
+
+ try {
+ // Get the last assistant message from the current thread
+ const messages = getMessages(thread.id)
+ const toEditMessage = messages.find((m) => m.id === messageId)
+
+ if (!toEditMessage) {
+ return
+ }
+ const currentContent = toEditMessage.content?.[0]?.text?.value || ''
+
+ // Create a prompt to fix the mermaid diagram
+ const fixPrompt = `The following mermaid diagram has an error:
+
+\`\`\`mermaid
+${chart}
+\`\`\`
+
+Error: ${error}
+
+Please fix the mermaid diagram syntax. Here is the format of linechart
+xychart
+ title "Sales Revenue"
+ x-axis [jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec]
+ y-axis "Revenue (in $)" 4000 --> 11000
+ bar [5000, 6000, 7500, 8200, 9500, 10500, 11000, 10200, 9200, 8500, 7000, 6000]
+ line [5000, 6000, 7500, 8200, 9500, 10500, 11000, 10200, 9200, 8500, 7000, 6000]
+
+Make sure no brackets in the chart title or x-axis or y-axis, string should be wrapped in double quotes. Remember line and bar are arrays and items should be wrapped in square brackets.
+Don't put any extra fields or syntax that is not part of mermaid syntax such as series or markers.
+
+Return ONLY the corrected mermaid code block in the following format:
+\`\`\`mermaid
+[corrected diagram code here]
+\`\`\`
+
+
+
+Do not include explanations or additional text.`
+
+ // Call the model to fix the diagram
+ const abortController = new AbortController()
+
+ const completion = await sendCompletion(
+ thread,
+ provider,
+ [
+ {
+ role: 'user',
+ content: fixPrompt,
+ },
+ ],
+ abortController,
+ [],
+ false,
+ )
+ // Extract the fixed mermaid code
+ if (
+ completion &&
+ 'choices' in completion &&
+ completion.choices?.[0]?.message?.content
+ ) {
+ const fixedContent = completion.choices[0].message.content as string
+
+ // Extract mermaid code from markdown code block
+ const mermaidMatch = fixedContent.match(/```mermaid\s*([\s\S]*?)\s*```/)
+ const fixedMermaid = (
+ mermaidMatch ? mermaidMatch[1].trim() : fixedContent.trim()
+ ).replace('\"', '"')
+
+ // Replace the broken mermaid in the message content
+ const updatedContent = currentContent.replace(
+ /```mermaid\s*[\s\S]*?\s*```/,
+ `\`\`\`mermaid\n${fixedMermaid}\n\`\`\``
+ )
+ // Update the message with the fixed mermaid
+ updateMessage({
+ ...toEditMessage,
+ content: [
+ {
+ type: ContentType.Text,
+ text: {
+ value: updatedContent,
+ annotations: [],
+ },
+ },
+ ],
+ })
+
+ // Retry rendering the diagram after a brief delay
+ setTimeout(() => {
+ retry()
+ }, 100)
+ }
+ } catch (err) {
+ console.error('Failed to fix mermaid diagram:', err)
+ } finally {
+ setIsFixing(false)
+ }
+ }
+ return (
+ <div className="flex flex-col items-center justify-center p-6 gap-3">
+ <img src="/images/jan-logo.png" alt="Jan Logo" className='h-12 w-12' />
+ <p className="text-sm text-muted-foreground text-center">
+ {isFixing
+ ? 'Meowing the diagram...'
+ : isStreaming
+ ? 'Error detected, fixing soon...'
+ : 'Diagram error detected'}
+ </p>
+ {!isStreaming && hasAttemptedFix && !isFixing && (
+ <p className="text-xs text-muted-foreground/60 text-center mt-1">
+ Auto-fix attempted. Check the updated diagram.
+ </p>
+ )}
+ </div>
+ )
+}
+
+export const MermaidError = memo(MermaidErrorComponent)
diff --git a/web-app/src/containers/RenderMarkdown.tsx b/web-app/src/containers/RenderMarkdown.tsx
index 754a705b6..696582e9c 100644
--- a/web-app/src/containers/RenderMarkdown.tsx
+++ b/web-app/src/containers/RenderMarkdown.tsx
@@ -5,7 +5,7 @@ import { cn } from '@/lib/utils'
import { defaultRehypePlugins, Streamdown } from 'streamdown'
import remarkGfm from 'remark-gfm'
import remarkMath from 'remark-math'
-import { AnimatedRunningCat } from '@/components/AnimatedRunningCat'
+import { MermaidError } from '@/components/MermaidError'
interface MarkdownProps {
content: string
@@ -106,6 +106,15 @@ function RenderMarkdownComponent({
defaultRehypePlugins.harden,
]}
components={components}
+ mermaid={
+ messageId
+ ? {
+ errorComponent: (props) => (
+ <MermaidError messageId={messageId} {...props} />
+ ),
+ }
+ : {}
+ }
>
{normalizedContent}
</Streamdown>
diff --git a/web-app/src/lib/completion.ts b/web-app/src/lib/completion.ts
index 76a7dc86f..b049cc36b 100644
--- a/web-app/src/lib/completion.ts
+++ b/web-app/src/lib/completion.ts
@@ -270,7 +270,7 @@ export const sendCompletion = async (
thread_id: thread.id,
tools: normalizeTools(usableTools),
tool_choice: usableTools.length ? 'auto' : undefined,
- stream: true,
+ stream,
...params,
},
abortController