How infiolab/infio-copilot works
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).