# infiolab/infio-copilot architecture

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

- Interactive diagram: https://gitdiagram.com/infiolab/infio-copilot
- Repository: https://github.com/infiolab/infio-copilot

## Overview

##### Project Type and Purpose
- Infio-Copilot is an Obsidian community plugin (open-source tool) written in TypeScript.
- It provides two main user-facing features: context-aware autocomplete and an interactive chat/edit flow over your vault.
- It also supports RAG (retrieval-augmented generation) by embedding your notes.

##### High-Level Architectural Layers
Break the system into four logical layers:
A. Host & Plugin Entry
- Obsidian Plugin Host (the host application)
- main.ts – registers commands, views, settings tab, render plugins, event listeners
B. UI Layer (React + Lexical)
- Views: ChatView, ApplyView, PreviewView (in src/)
- Components under src/components/**
- Settings UI under src/settings/**
- React Contexts (AppContext, LLMContext, DatabaseContext, etc.) for state management
- Lexical editor plugins for rich input (in src/components/chat-view/chat-input/plugins)
C. Core Business Logic
- core/llm: LLMManager & provider adapters (OpenAI, Anthropic, Gemini, etc.)
- core/prompts: prompt templates & tool definitions
- core/autocomplete: context detection, state machine, preprocessors/post-processors
- core/diff: diff strategies for “apply” flow
- core/rag: embedding generation & retrieval engine
- core/mcp: multi-processing coordination (if using multiple LLMs)
D. Data & Infrastructure
- Database Layer:
– JSON storage managers (src/database/json/…)
– SQL/PG Lite modules & schema (src/database/modules/…, src/pgworker)
- Web Worker (pgworker/pglite.worker.ts) hosts the PG Lite engine on a background thread
- External Services: LLM API endpoints, embedding endpoints, file system (vault)
- Utility modules for FS, API calls, prompt generation, path, etc. (src/utils/**)

##### Main Components & Their Interactions
1. Obsidian Host
– Loads the plugin (main.ts).
– Invokes plugin callbacks: commands, settings, event listeners.
2. Plugin Entrypoint (main.ts)
– Registers React views & settings tab.
– Initializes Context Providers (wraps the UI).
– Spawns PG Lite web worker.
3. UI Components
– Render chat/autocomplete/edit UI.
– Consume React Contexts to read/write state.
– On user action, call Hooks (src/hooks/**) which drive core logic.
4. Core Engine
– Hooks invoke core modules: e.g. useChatHistory → database; useAutocomplete → core/autocomplete.
– Core modules call LLMManager, passing prompts from core/prompts.
– LLMManager uses provider adapters to hit external APIs.
5. Database & Storage
– Database managers store chat history, commands, custom modes.
– If using vectors, embeddings are produced by core/rag and stored in PG Lite.
– PG Lite runs in a Worker; communication via postMessage from main thread.
6. External
– HTTP calls to LLM providers, search-web, image sources.

##### Architectural Patterns & Design Principles
- Layered Architecture: UI → Core → Data → External services
- MVVM-like: React components (View), Context/Hooks (ViewModel), Core modules (Model)
- Strategy Pattern: core/diff and core/autocomplete let you plug in different strategies
- Adapter Pattern: core/llm/provider adapters unify different LLM APIs
- Worker Offloading: PG Lite in a Web Worker for non-blocking storage

##### Details
- Core flow for both “Chat/Edit” and “Autocomplete.”
- Don’t forget the Settings UI and localization modules under Data/Infrastructure (they load JSON locale files).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Host & Plugin Entry
    ObsidianHost["Obsidian Host"]:::host
    main["main.ts (Plugin Entrypoint)"]:::host
    manifest["manifest.json"]:::host

    ObsidianHost -->|"loads plugin"| main
    main -->|uses| manifest

    click main "https://github.com/infiolab/infio-copilot/blob/master/src/main.ts"
    click manifest "https://github.com/infiolab/infio-copilot/blob/master/manifest.json"

    %% UI Layer
    subgraph "UI Layer (React + Lexical)"
        direction TB
        subgraph "Views"
            direction TB
            ChatView["Chat View"]:::ui
            ApplyView["Apply View"]:::ui
            PreviewView["Preview View"]:::ui
        end
        subgraph "View Roots"
            direction TB
            ChatViewRoot["ChatViewRoot"]:::ui
            ApplyViewRoot["ApplyViewRoot"]:::ui
            PreviewViewRoot["PreviewViewRoot"]:::ui
        end
        subgraph "Settings UI"
            direction TB
            SettingsTab["Settings Tab"]:::ui
        end
        subgraph "React Contexts"
            direction TB
            AppContext["AppContext"]:::ui
            LLMContext["LLMContext"]:::ui
            DatabaseContext["DatabaseContext"]:::ui
            RAGContext["RAGContext"]:::ui
            DiffStrategyContext["DiffStrategyContext"]:::ui
            SettingsContext["SettingsContext"]:::ui
            DialogContext["DialogContext"]:::ui
            DarkModeContext["DarkModeContext"]:::ui
        end
        subgraph "Lexical Editor Plugins"
            direction TB
            LexicalPlugins["Chat Input Plugins"]:::ui
        end
    end

    click ChatView "https://github.com/infiolab/infio-copilot/blob/master/src/ChatView.tsx"
    click ApplyView "https://github.com/infiolab/infio-copilot/blob/master/src/ApplyView.tsx"
    click PreviewView "https://github.com/infiolab/infio-copilot/blob/master/src/PreviewView.tsx"
    click ChatViewRoot "https://github.com/infiolab/infio-copilot/blob/master/src/components/chat-view/ChatView.tsx"
    click ApplyViewRoot "https://github.com/infiolab/infio-copilot/blob/master/src/components/apply-view/ApplyViewRoot.tsx"
    click PreviewViewRoot "https://github.com/infiolab/infio-copilot/blob/master/src/components/preview-view/PreviewViewRoot.tsx"
    click SettingsTab "https://github.com/infiolab/infio-copilot/blob/master/src/settings/SettingTab.tsx"
    click AppContext "https://github.com/infiolab/infio-copilot/blob/master/src/contexts/AppContext.tsx"
    click LLMContext "https://github.com/infiolab/infio-copilot/blob/master/src/contexts/LLMContext.tsx"
    click DatabaseContext "https://github.com/infiolab/infio-copilot/blob/master/src/contexts/DatabaseContext.tsx"
    click RAGContext "https://github.com/infiolab/infio-copilot/blob/master/src/contexts/RAGContext.tsx"
    click DiffStrategyContext "https://github.com/infiolab/infio-copilot/blob/master/src/contexts/DiffStrategyContext.tsx"
    click SettingsContext "https://github.com/infiolab/infio-copilot/blob/master/src/contexts/SettingsContext.tsx"
    click DialogContext "https://github.com/infiolab/infio-copilot/blob/master/src/contexts/DialogContext.tsx"
    click DarkModeContext "https://github.com/infiolab/infio-copilot/blob/master/src/contexts/DarkModeContext.tsx"
    click LexicalPlugins "https://github.com/infiolab/infio-copilot/tree/master/src/components/chat-view/chat-input/plugins/"

    %% ViewModel (Hooks)
    subgraph "ViewModel (Hooks)"
        direction TB
        useChatHistory["useChatHistory"]:::core
        useCommands["useCommands"]:::core
        useCustomMode["useCustomMode"]:::core
    end

    click useChatHistory "https://github.com/infiolab/infio-copilot/blob/master/src/hooks/use-chat-history.ts"
    click useCommands "https://github.com/infiolab/infio-copilot/blob/master/src/hooks/use-commands.ts"
    click useCustomMode "https://github.com/infiolab/infio-copilot/blob/master/src/hooks/use-custom-mode.ts"

    %% Core Business Logic
    subgraph "Core Engine"
        direction TB
        LLMMgr["LLMManager & Adapters"]:::core
        Prompts["Prompt Templates & Tools"]:::core
        Autocomplete["Autocomplete Engine"]:::core
        DiffStrategies["Diff Strategies"]:::core
        InlineEdit["Inline Edit Processor"]:::core
        RAGEngine["RAG Engine & Embeddings"]:::core
        MCP["Multi-Processing Coordination"]:::core
    end

    click LLMMgr "https://github.com/infiolab/infio-copilot/tree/master/src/core/llm/"
    click Prompts "https://github.com/infiolab/infio-copilot/tree/master/src/core/prompts/"
    click Autocomplete "https://github.com/infiolab/infio-copilot/tree/master/src/core/autocomplete/"
    click DiffStrategies "https://github.com/infiolab/infio-copilot/tree/master/src/core/diff/"
    click InlineEdit "https://github.com/infiolab/infio-copilot/blob/master/src/core/edit/inline-edit-processor.ts"
    click RAGEngine "https://github.com/infiolab/infio-copilot/tree/master/src/core/rag/"
    click MCP "https://github.com/infiolab/infio-copilot/tree/master/src/core/mcp/"

    %% Data & Infrastructure
    subgraph "Data & Infrastructure"
        direction TB
        subgraph "Database Layer"
            direction TB
            JSONStorage["JSON Storage Managers"]:::data
            SQLLayer["SQL/PGLite Modules"]:::data
        end
        Worker["Web Worker (PG Lite)"]:::data
        subgraph "Utilities"
            direction TB
            FS["File System & Path"]:::data
            APIUtils["API & HTTP"]:::data
            PromptGen["Prompt Generation Helpers"]:::data
            MiscUtils["Miscellaneous Utils"]:::data
        end
        Lang["Language & Localization"]:::data
        Styles["Stylesheet"]:::data
    end

    click JSONStorage "https://github.com/infiolab/infio-copilot/tree/master/src/database/json/"
    click SQLLayer "https://github.com/infiolab/infio-copilot/blob/master/src/database/schema.ts"
    click SQLLayer "https://github.com/infiolab/infio-copilot/blob/master/src/database/sql.ts"
    click SQLLayer "https://github.com/infiolab/infio-copilot/tree/master/src/database/modules/"
    click Worker "https://github.com/infiolab/infio-copilot/blob/master/src/pgworker/pglite.worker.ts"
    click FS "https://github.com/infiolab/infio-copilot/blob/master/src/utils/fs.ts"
    click APIUtils "https://github.com/infiolab/infio-copilot/blob/master/src/utils/api.ts"
    click APIUtils "https://github.com/infiolab/infio-copilot/blob/master/src/utils/web-search.ts"
    click PromptGen "https://github.com/infiolab/infio-copilot/blob/master/src/utils/prompt-generator.ts"
    click MiscUtils "https://github.com/infiolab/infio-copilot/tree/master/src/utils/"
    click Lang "https://github.com/infiolab/infio-copilot/tree/master/src/lang/locale/"
    click Styles "https://github.com/infiolab/infio-copilot/blob/master/styles.css"

    %% External Services
    subgraph "External Services"
        direction TB
        ExternalLLM["External LLM APIs"]:::external
        WebSearch["Search/Web Sources"]:::external
        ImageSrc["Image Sources"]:::external
        VaultFS["Vault File System"]:::external
    end

    %% Connections
    main -->|"registers views"| ChatView
    main -->|"initializes contexts"| AppContext
    main -->|"spawns"| Worker

    ChatView -->|uses| useChatHistory
    ApplyView -->|uses| DiffStrategies
    ChatView -->|uses| Autocomplete
    ChatView -->|renders| ChatViewRoot
    ApplyView -->|renders| ApplyViewRoot
    PreviewView -->|renders| PreviewViewRoot
    SettingsTab -->|uses| SettingsContext

    useChatHistory -->|reads/writes| JSONStorage
    useCommands -->|reads/writes| JSONStorage
    useCustomMode -->|reads/writes| SQLLayer

    useChatHistory -->|calls| RAGEngine
    Autocomplete -->|calls| LLMMgr
    DiffStrategies -->|calls| InlineEdit
    InlineEdit -->|calls| LLMMgr
    RAGEngine -->|stores vectors in| SQLLayer

    LLMMgr -->|HTTP| ExternalLLM
    ExternalLLM -->|responds| LLMMgr

    Worker -->|postMessage| SQLLayer
    SQLLayer -->|responses| Worker

    ChatView -->|FS access| VaultFS
    useChatHistory -->|FS access| VaultFS

    AppContext --> ChatView
    AppContext --> ApplyView
    AppContext --> PreviewView

    %% Styles
    classDef host fill:#cccccc,stroke:#333,stroke-width:1px
    classDef ui fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef core fill:#98FB98,stroke:#333,stroke-width:1px
    classDef data fill:#FFA500,stroke:#333,stroke-width:1px
    classDef external fill:#D3D3D3,stroke:#333,stroke-width:1px
```
