# koishijs/koishi architecture

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

- Interactive diagram: https://gitdiagram.com/koishijs/koishi
- Repository: https://github.com/koishijs/koishi

## Overview

##### Main Components
- Core Engine (packages/core): This is the backbone. It includes the bot implementation, command handling (see files like bot.ts, command/*, parser.ts, validate.ts), context management, database interactions, middleware, i18n support (locales and i18n.ts), permissions, and schema definitions.
- CLI/Main Application (packages/koishi): This package contains the command-line interface (under src/cli), the main entry point (packages/koishi/src/index.ts), and worker-related modules (under packages/koishi/src/worker such as daemon.ts and logger.ts). It likely acts as the runtime that bootstraps the bot by integrating the core.
- Plugin System (plugins/*): There’s an extensive directory of plugins (e.g., common plugins like bind, broadcast, callme, echo, help, inspect and others such as hmr, http, mock, proxy-agent, server). These modules are designed to extend or complement the core functionality. They each maintain their own locales, tests, and configuration.
- Loader and Utilities (packages/loader and packages/utils): The loader package (e.g., packages/loader/src/index.ts) is likely responsible for dynamically loading and managing plugins/extensions. The utils package provides helper functions which support common operations across the system.
- Internationalization (packages/i18n-utils): Although seemingly a small package, it provides utilities to support the multiple locale files distributed across the core and plugin modules.

##### Out the Relationships and Data Flow
- A central “Core Engine” block that exposes fundamental services (bot logic, command parsing, context, database, etc.).
- “Loader” as an intermediary that dynamically imports or activates plugins. It should be shown between the core and the plugins.
- “Utilities” and “i18n” as supporting modules that are linked to both core and plugins. They provide cross-cutting functions and localization support.

##### Architectural Patterns and Design Principles
- Modular Architecture: Each package (core, CLI, plugins, loader, utilities) is self-contained, making the system highly extensible.
- Plugin-Based Extension: The clear separation into a core engine and separate plugins (each with its own locale support and tests) emphasizes an architecture where additional functionality can be “plugged in” without altering the core.
- Separation of Concerns: Core handles business logic, CLI/worker handles runtime interactions, and plugins encapsulate additional features.
- Use of standardized configuration/testing tools (see the various tsconfig.json files, ESLint configurations, GitHub workflows) which reinforces a robust, multi-package development practice.

##### Be Detailed
- You might also want to add a section for “Testing & Configuration” to show the presence of test suites within each package and the various config files (GitHub workflows, ESLint, etc.), indicating robust development practices.

Happy diagramming!

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Modular Bot Framework"
        direction TB
        cli["CLI & Worker (packages/koishi)"]:::app
        core["Core Engine (packages/core)"]:::core
        loader["Plugin Loader (packages/loader)"]:::loader
        plugins["Plugin Modules (plugins)"]:::plugins
        utils["Utilities (packages/utils)"]:::utils
        i18n["i18n Utilities (packages/i18n-utils)"]:::i18n
    end

    cli -->|"initializes"| core
    core -->|"injectsPlugins"| loader
    loader -->|"discovers"| plugins
    plugins -->|"extends"| core
    core -->|"utilizes"| utils
    core -->|"supportsLocalization"| i18n

    click cli "https://github.com/koishijs/koishi/tree/master/packages/koishi"
    click core "https://github.com/koishijs/koishi/tree/master/packages/core"
    click loader "https://github.com/koishijs/koishi/tree/master/packages/loader"
    click plugins "https://github.com/koishijs/koishi/tree/master/plugins"
    click utils "https://github.com/koishijs/koishi/tree/master/packages/utils"
    click i18n "https://github.com/koishijs/koishi/tree/master/packages/i18n-utils"

    classDef app fill:#F9D0C4,stroke:#333,stroke-width:2px;
    classDef core fill:#D0E6F2,stroke:#333,stroke-width:2px;
    classDef loader fill:#D0F2D0,stroke:#333,stroke-width:2px;
    classDef plugins fill:#F2E6D0,stroke:#333,stroke-width:2px;
    classDef utils fill:#E6D0F2,stroke:#333,stroke-width:2px;
    classDef i18n fill:#F2D0E6,stroke:#333,stroke-width:2px;
```
