You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(web): render mermaid diagrams in chat answers
Add inline mermaid diagram rendering to chat answers with a "Visualize"
button on the answer card to generate diagrams on demand, plus a
fullscreen pan/zoom modal.
The renderer is registry-based so a future React Flow engine can be
plugged in without changing the markdown wiring or the agent prompt.
Refs #1240
Co-authored-by: Cursor <cursoragent@cursor.com>
Copy file name to clipboardExpand all lines: packages/web/src/features/chat/agent.ts
+73Lines changed: 73 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -317,6 +317,79 @@ const createPrompt = ({
317
317
Authentication in Sourcebot is built on NextAuth.js with a session-based approach using JWT tokens and Prisma as the database adapter ${fileReferenceToString({repo: 'github.com/sourcebot-dev/sourcebot',path: 'auth.ts',range: {startLine: 135,endLine: 140}})}. The system supports multiple authentication providers and implements organization-based authorization with role-defined permissions.
318
318
\`\`\`
319
319
</answer_instructions>
320
+
321
+
<diagram_instructions>
322
+
When the user asks for a diagram, visual, workflow, architecture, sequence, flow, "show me a visual", "draw", or similar visual request, you MUST include a \`mermaid\` fenced code block in your answer. You may also include a diagram on your own initiative when the answer involves a multi-step workflow, a request/response sequence, or relationships between services or modules that are clearer visually than in prose.
323
+
324
+
**Diagram type selection** — pick the one that best fits the question:
325
+
- \`sequenceDiagram\` — request flows, API call chains, message passing between services or actors
- \`graph TD\` — service topology, architecture overviews, component dependencies
328
+
- \`classDiagram\` — model / class relationships, inheritance hierarchies
329
+
- \`erDiagram\` — database schemas and entity relationships
330
+
- \`stateDiagram-v2\` — state machines and lifecycle transitions
331
+
332
+
**Diagram authoring rules:**
333
+
- ONLY include nodes and edges that are supported by code you have actually read via tool calls. Do NOT invent components, services, or relationships. If you are uncertain about a connection, omit it and say so in the surrounding prose.
334
+
- Keep diagrams focused. Prefer two small diagrams (e.g. "high level" + "request flow detail") over one giant one with twenty nodes.
335
+
- For every meaningful node in the diagram, cite its source location in the surrounding prose using the existing \`${FILE_REFERENCE_PREFIX}\` format, e.g. "The \`AuthService\` node is implemented in ${fileReferenceToString({repo: 'repository',path: 'services/auth.ts',range: {startLine: 1,endLine: 30}})}".
336
+
- Use short, descriptive labels for nodes — function names, service names, or short phrases. Avoid full sentences inside nodes.
337
+
- Make sure the mermaid syntax parses. If unsure about a particular feature, use the simplest form (e.g. \`A --> B\` rather than complex edge styling).
338
+
339
+
**Label hygiene — CRITICAL for mermaid to parse correctly:**
340
+
Mermaid's tokenizer is fragile. If a label contains anything other than letters, digits, underscores, hyphens, and single spaces, follow these rules — otherwise the diagram will fail to render and the user will see a parse error.
341
+
342
+
1. **For flowchart, graph, classDiagram, erDiagram, and stateDiagram-v2 NODE LABELS**: ALWAYS wrap the label in double quotes when it contains ANY of these characters: parentheses, commas, angle brackets, ampersands, slashes, colons, semicolons, pipes, braces, brackets, backticks, periods, dollar signs, hashes. The quotes make the content opaque to the parser. Examples:
- OK without quotes: \`C[ProcessRequest]\`, \`D[handle response]\`
348
+
349
+
2. **For sequenceDiagram MESSAGE LABELS** (the text after \`:\`): keep them plain English. The message text is freeform but MUST NOT contain double quotes, and should avoid parentheses with arguments. Describe the call instead of writing its signature:
0 commit comments