# microsoft/vscode-mermaid architecture

> Architecture diagram of the GitHub repository microsoft/vscode-mermaid, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/microsoft/vscode-mermaid
- Repository: https://github.com/microsoft/vscode-mermaid

## Overview

##### Project type and purpose
- This is a VS Code extension written in TypeScript.

##### File structure
Top-level directories and files:
- build/ → CI pipelines (Azure YAML files for pre-release, stable, autobaselining)
- src/ → extension source code
- / → logic for the outline-view webview
- commands.ts, extension.ts → VS Code activation, command registration
- codeLensProvider.ts → in-editor code lens hooks
- groqHandler.ts → optional groq API calls
- media/ → CSS/JS assets for webviews
- assets/ → demo GIFs used in README
- webpack.config.js, tsconfig.json → build configuration
- .config/, package.json, etc. → standard extension metadata and settings

##### README for additional insights
- There’s an experimental groq integration behind a setting and stored API key.

##### Main components and interactions
a. Components:
- VS Code Host
- Extension Host Process
– Activation & lifecycle (extension.ts)
– Command Handlers (commands.ts)
– Chat Participant Service (src/chat/*)
– Outline Service (src/outline/*)
– CodeLens Provider
– groq API Client
- GitHub Copilot Chat Service (external)
- groq API Service (external, optional)
- Webviews
– Outline View Panel
- File System & User Settings (for storing the groq key, extension settings)

b. Relationships & Data Flow:
1. User invokes a slash command or clicks the outline view refresh button.
2. The Extension Host routes the request to either the Chat Participant or Outline Service.
3. Chat Participant Service calls GitHub Copilot Chat API, passing the user prompt + context.
– Optionally the service invokes groqHandler to pre-process the code outline.
5. Extension processes the DSL (mermaidHelpers.ts), creates/updates a DiagramDocument.
8. Settings and secrets (groq key) are read from VS Code’s secret storage.

c. Architectural Patterns & Design Principles:
- **Extension (Plugin) Architecture**: clear separation between VS Code APIs and your services.
- **Command Pattern**: commands.ts registers named commands that delegate to handlers.
- **MVC-like Webviews**: DiagramDocument (model), webview panels (view), extension code (controller).
- **Facade over External APIs**: chatParticipant.ts abstracts the GitHub Copilot Chat Participant API; groqHandler.ts abstracts groq calls.

d. Key technologies/frameworks:
- VS Code Extension API (vscode module)
- GitHub Copilot Chat Participant API
- webview panels (HTML/CSS/JS)
- TypeScript, webpack

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% VS Code UI Layer
    subgraph "VS Code UI"
        direction TB
        User["User"]:::user
        CommandsPalette["Command Palette / Slash Commands"]:::ui
        OutlineViewButton["Outline View Refresh"]:::ui
    end

    %% Extension Host Process
    subgraph "Extension Host Process"
        direction TB
        Activation["Extension Activation & Lifecycle"]:::service
        Commands["Command Handlers"]:::service
        ChatService["Chat Participant Service"]:::service
        OutlineService["Outline Service"]:::service
        CodeLens["CodeLens Provider"]:::service
        GroqClient["groq API Client"]:::service
        MermaidHelpers["Mermaid Helpers"]:::service
        DiagramDoc["Diagram Model"]:::service
        DiagramPanelCtrl["Diagram Renderer Controller"]:::service
        DiagramCore["Core Mermaid Renderer"]:::service
    end

    %% Webview Panels
    subgraph "Webview Panels"
        direction TB
        EditorPanel["Diagram Editor Panel"]:::webview
        OutlinePanel["Outline View Panel"]:::webview
    end

    %% External Services & Storage
    subgraph "External & Storage"
        direction TB
        CopilotAPI["GitHub Copilot Chat API"]:::external
        GroqAPI["groq API Service (optional)"]:::external
        SecretStore["VS Code Secret Storage"]:::storage
        FileSystem["File System"]:::storage
    end

    %% Build & Config & Tests
    subgraph "Build, Config & Tests"
        direction TB
        Webpack["webpack.config.js"]:::config
        TSConfig["tsconfig.json"]:::config
        Pipelines["build/azure-pipelines*.yml"]:::config
        DemoAssets["assets/gifs"]:::config
        TestSuite["Extension Tests"]:::test
    end

    %% Flows - Chat Participant Flow
    User -->|invoke slash command| CommandsPalette
    CommandsPalette -->|executes| Commands
    Commands -->|routes to| ChatService
    ChatService -->|calls| CopilotAPI
    ChatService -->|optional call| GroqClient
    GroqClient -->|requests outline| GroqAPI
    ChatService -->|returns DSL| MermaidHelpers
    MermaidHelpers -->|builds doc| DiagramDoc
    DiagramDoc -->|update| DiagramPanelCtrl
    DiagramPanelCtrl -->|renders via| DiagramCore
    DiagramCore -->|display in| EditorPanel
    EditorPanel -->|shows diagram to| User

    %% Flows - Outline View Flow
    User -->|click refresh| OutlineViewButton
    OutlineViewButton -->|executes| OutlineService
    OutlineService -->|optional call| GroqClient
    GroqClient -->|requests outline| GroqAPI
    OutlineService -->|returns DSL| MermaidHelpers
    MermaidHelpers -->|builds doc| DiagramDoc
    DiagramDoc -->|update| OutlinePanel
    OutlinePanel -->|renders| DiagramCore
    OutlinePanel -->|shows outline to| User

    %% Settings & Storage
    OutlineService -->|reads key| SecretStore
    GroqClient -->|reads key| SecretStore
    Commands -->|reads/writes| FileSystem

    %% Build & Config Integration
    Activation -->|packaged by| Webpack
    Activation -->|compiled via| TSConfig
    Pipelines -->|CI for| Activation
    DemoAssets -->|used in| README

    %% Tests
    TestSuite -->|tests| Activation
    TestSuite -->|tests| Commands

    %% Click Events
    click Activation "https://github.com/microsoft/vscode-mermaid/blob/main/src/extension.ts"
    click Commands "https://github.com/microsoft/vscode-mermaid/blob/main/src/commands.ts"
    click ChatService "https://github.com/microsoft/vscode-mermaid/blob/main/src/chat/chatParticipant.ts"
    click ChatService "https://github.com/microsoft/vscode-mermaid/blob/main/src/chat/chatHelpers.ts"
    click ChatService "https://github.com/microsoft/vscode-mermaid/blob/main/src/chat/additionalTools.ts"
    click ChatService "https://github.com/microsoft/vscode-mermaid/blob/main/src/chat/chatExamples.ts"
    click ChatService "https://github.com/microsoft/vscode-mermaid/blob/main/src/chat/mermaidPrompt.tsx"
    click OutlineService "https://github.com/microsoft/vscode-mermaid/blob/main/src/outline/outlineView.ts"
    click OutlineService "https://github.com/microsoft/vscode-mermaid/blob/main/src/outline/outlinePrompt.tsx"
    click CodeLens "https://github.com/microsoft/vscode-mermaid/blob/main/src/codeLensProvider.ts"
    click GroqClient "https://github.com/microsoft/vscode-mermaid/blob/main/src/groqHandler.ts"
    click MermaidHelpers "https://github.com/microsoft/vscode-mermaid/blob/main/src/mermaidHelpers.ts"
    click DiagramDoc "https://github.com/microsoft/vscode-mermaid/blob/main/src/diagramDocument.ts"
    click DiagramPanelCtrl "https://github.com/microsoft/vscode-mermaid/blob/main/src/diagramEditorPanel.ts"
    click DiagramCore "https://github.com/microsoft/vscode-mermaid/blob/main/src/diagram.ts"
    click Webpack "https://github.com/microsoft/vscode-mermaid/blob/main/webpack.config.js"
    click TSConfig "https://github.com/microsoft/vscode-mermaid/blob/main/tsconfig.json"
    click Pipelines "https://github.com/microsoft/vscode-mermaid/blob/main/build/azure-pipelines.yml"
    click Pipelines "https://github.com/microsoft/vscode-mermaid/blob/main/build/azure-pipelines-pre.yml"
    click Pipelines "https://github.com/microsoft/vscode-mermaid/blob/main/build/azure-pipelines-stable.yml"
    click DemoAssets "https://github.com/microsoft/vscode-mermaid/tree/main/assets/gifs"
    click TestSuite "https://github.com/microsoft/vscode-mermaid/blob/main/src/test/extension.test.ts"

    %% Styles
    classDef user fill:#FFE082,stroke:#B8860B
    classDef ui fill:#FFF3E0,stroke:#FB8C00
    classDef service fill:#E3F2FD,stroke:#1976D2
    classDef webview fill:#E8F5E9,stroke:#388E3C
    classDef external fill:#FFEBEE,stroke:#D32F2F
    classDef storage fill:#FFFDE7,stroke:#FBC02D
    classDef config fill:#ECEFF1,stroke:#607D8B
    classDef test fill:#FCE4EC,stroke:#C2185B
```
