# mattermost/mattermost-plugin-ai architecture

> Architecture diagram of the GitHub repository mattermost/mattermost-plugin-ai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mattermost/mattermost-plugin-ai
- Repository: https://github.com/mattermost/mattermost-plugin-ai

## Overview

##### Main Components
- Plugin Core: The central plugin configuration file (plugin.json) and entry point (plugin.go) that registers the plugin with Mattermost.
- Backend Server: The “server” directory contains much of the Go code responsible for processing API requests, handling bot behaviors, managing LLM communications, conversation context, message processing, and administrative tasks.
- Subcomponents include:
▪ API endpoints (api.go, api_admin.go, etc.)
▪ LLM integration modules (server/llm, server/openai, server/anthropic)
▪ Enterprise-related functionality (server/enterprise)
▪ Metrics and logging (server/metrics, server/llm_log.go)
▪ Additional tools for text splitting, conversation analysis, and transcriptions.
- Frontend Web Application: The “webapp” folder contains the React/TypeScript code that builds the user interface shown in Mattermost (e.g., chat panels, bot selectors, RHS panels). Key files such as index.tsx, components, and Redux management indicate a rich client-side experience.
- Build and Deployment Tools: The “build” directory, along with Makefile, CI configurations (.github/workflows), and other scripts, orchestrate the build process, plugin packaging, and testing.
- End-to-End and Unit Test Framework: The “e2e” directory (with Playwright configurations and tests) and various test files across server directories ensure quality and include integration tests.
- Documentation & Configuration: The “docs” folder and configuration files (e.g., .golangci.yml, .editorconfig, and various config files in .config) add context for development and deployment.

##### Relationships and Interactions
- The Plugin Core acts as the entry point into Mattermost. When installed, Mattermost loads the plugin, which then sets up both the backend and frontend components.
- The Frontend Web Application communicates with the backend server through API endpoints (defined in the server folder) to handle user actions, manage bot interactions, and perform administrative tasks.
- The Backend interacts directly with external LLM providers – separate modules for OpenAI and Anthropic indicate that it routes and processes AI-related requests.
- Build and test pipelines (via Makefile, GitHub workflows, and test directories) should be noted, especially as they impact deployment and development.

##### Architectural Patterns/Design Principles
- Full-stack separation: Clear delineation between backend (server) and frontend (webapp) components.
- Modular Design: Subdirectories such as server/llm, server/api, and webapp/components highlight a modular approach, enabling easier maintenance and potential extension points (especially for integrating additional LLM providers).
- Dependency Isolation: The presence of separate test suites (unit tests in server, E2E tests in “e2e”) and build scripts suggest adherence to CI/CD practices.
- Integration with External Systems: Emphasize that the plugin not only integrates with Mattermost’s internal APIs but also with external LLM providers.

##### Relevant Technologies
- Backend: Go (Go modules such as go.mod, go.sum), relevant frameworks/libraries for HTTP routing, LLM integrations, and message processing.
- Frontend: React/TypeScript, Redux, Webpack, ESLint, Babel.
- Testing: Playwright for E2E tests, Go testing frameworks for back-end tests.
- CI/CD: GitHub workflows and Makefile.

##### Instructed Elements
- Data flow from the webapp to the backend.
- API interactions between the backend and Mattermost server.
- Calls out to external LLM provider services.
- Internal communication between various backend modules.
- UI components vs. server-side logic.
- Core business logic vs. configuration and utility modules.
- Testing/build pipelines as auxiliary but essential components.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Plugin Core
    subgraph "Plugin Core"
        Plugin_JSON["Plugin JSON"]:::plugin
        Plugin_GO["Plugin Go"]:::plugin
    end

    %% Backend Server
    subgraph "Backend Server"
        API_Endpoints["API Endpoints"]:::backend
        LLM_Gen["Generic LLM"]:::backend
        OpenAI["OpenAI Module"]:::backend
        Anthropic["Anthropic Module"]:::backend
        Enterprise["Enterprise Functionality"]:::backend
        Metrics["Metrics & Logging"]:::backend
        Utilities["Utilities & Tools"]:::backend
    end

    %% Frontend UI
    subgraph "Frontend UI"
        Frontend["Frontend UI"]:::frontend
    end

    %% Build & Deployment
    subgraph "Build & Deployment"
        Build_Tools["Build Tools"]:::build
        CICD["CI/CD"]:::build
        Makefile["Makefile"]:::build
    end

    %% Testing
    subgraph "Testing"
        E2E["E2E Tests"]:::test
        Backend_Tests["Backend Unit Tests"]:::test
    end

    %% Documentation & Configuration
    subgraph "Docs & Config"
        Documentation["Documentation"]:::doc
        Config["Configuration Files"]:::doc
    end

    %% External Systems
    subgraph "External Systems"
        Mattermost["Mattermost Server"]:::external
        LLM_Providers["LLM Providers"]:::external
    end

    %% Relationships
    %% Plugin Core initiates the Plugin and ties to Backend and Frontend
    Plugin_JSON -->|"registers"| API_Endpoints
    Plugin_GO -->|"initializes"| Frontend

    %% Frontend calls backend API endpoints
    Frontend -->|"API call"| API_Endpoints

    %% Backend API interacts with Mattermost and supports modules
    API_Endpoints -->|"communicates"| Mattermost
    API_Endpoints -->|"invokes"| LLM_Gen
    API_Endpoints -->|"manages"| Enterprise
    API_Endpoints -->|"logs"| Metrics
    API_Endpoints -->|"utilizes"| Utilities

    %% LLM Integration details
    LLM_Gen -->|"routes to"| OpenAI
    LLM_Gen -->|"routes to"| Anthropic
    OpenAI -->|"queries"| LLM_Providers
    Anthropic -->|"queries"| LLM_Providers

    %% Supporting Processes: Build, Testing, Documentation & Config
    Build_Tools -->|"builds"| Plugin_JSON
    CICD -->|"deploys"| Plugin_GO
    Makefile -->|"orchestrates"| Plugin_JSON

    CICD -->|"triggers"| E2E
    CICD -->|"triggers"| Backend_Tests

    Documentation ---|"guides"| Plugin_JSON
    Config ---|"configures"| Plugin_JSON

    %% Styles
    classDef plugin fill:#FFEB3B,stroke:#333,stroke-width:2px;
    classDef backend fill:#03A9F4,stroke:#333,stroke-width:2px;
    classDef frontend fill:#8BC34A,stroke:#333,stroke-width:2px;
    classDef build fill:#9E9E9E,stroke:#333,stroke-width:2px;
    classDef test fill:#FF9800,stroke:#333,stroke-width:2px;
    classDef doc fill:#673AB7,stroke:#333,stroke-width:2px,color:#fff;
    classDef external fill:#E91E63,stroke:#333,stroke-width:2px,color:#fff;

    %% Click Events
    click Plugin_JSON "https://github.com/mattermost/mattermost-plugin-ai/blob/master/plugin.json"
    click Plugin_GO "https://github.com/mattermost/mattermost-plugin-ai/blob/master/plugin.go"
    click API_Endpoints "https://github.com/mattermost/mattermost-plugin-ai/blob/master/server/api.go"
    click LLM_Gen "https://github.com/mattermost/mattermost-plugin-ai/tree/master/server/llm"
    click OpenAI "https://github.com/mattermost/mattermost-plugin-ai/blob/master/server/openai/openai.go"
    click Anthropic "https://github.com/mattermost/mattermost-plugin-ai/blob/master/server/anthropic/anthropic.go"
    click Enterprise "https://github.com/mattermost/mattermost-plugin-ai/tree/master/server/enterprise"
    click Metrics "https://github.com/mattermost/mattermost-plugin-ai/tree/master/server/metrics"
    click Frontend "https://github.com/mattermost/mattermost-plugin-ai/tree/master/webapp"
    click Build_Tools "https://github.com/mattermost/mattermost-plugin-ai/tree/master/build"
    click Makefile "https://github.com/mattermost/mattermost-plugin-ai/tree/master/Makefile"
    click CICD "https://github.com/mattermost/mattermost-plugin-ai/tree/master/.github/workflows"
    click E2E "https://github.com/mattermost/mattermost-plugin-ai/tree/master/e2e"
    click Documentation "https://github.com/mattermost/mattermost-plugin-ai/tree/master/docs"
    click Config "https://github.com/mattermost/mattermost-plugin-ai/blob/master/.config"
```
