# アーキテクチャ構成図

## システム全体構成図

```mermaid
graph TB
    subgraph "Client Layer"
        CLI[CLI/TUI<br/>packages/opencode]
        WebApp[Web App<br/>packages/app]
        Desktop[Desktop App<br/>packages/desktop<br/>Tauri]
    end

    subgraph "Core Engine"
        Server[HTTP Server<br/>Hono]
        Session[Session Manager]
        Agent[Agent System]
        Provider[Provider Layer<br/>AI SDK]
        Tool[Tool Registry]
    end

    subgraph "Infrastructure"
        Storage[(SQLite<br/>Drizzle ORM)]
        Bus[Event Bus]
        Config[Config Manager]
    end

    subgraph "External Services"
        LLM[LLM Providers<br/>Anthropic/OpenAI/Google]
        LSP[LSP Servers<br/>TypeScript/Python/Rust]
        MCP[MCP Servers]
        VCS[VCS<br/>Git]
    end

    subgraph "Cloud Services"
        API[API Worker<br/>Cloudflare]
        Console[Console<br/>SolidStart]
        Auth[Auth Worker<br/>OpenAuth]
        DB[(PlanetScale)]
    end

    CLI --> Server
    WebApp --> Server
    Desktop --> Server

    Server --> Session
    Session --> Agent
    Session --> Provider
    Agent --> Tool

    Provider --> LLM
    Tool --> LSP
    Tool --> MCP
    Tool --> VCS

    Session --> Storage
    Session --> Bus
    Agent --> Config

    Console --> Auth
    Console --> DB
    API --> Auth
```

## パッケージ構成図

```mermaid
graph LR
    subgraph "packages/"
        opencode[opencode<br/>Core CLI]
        app[app<br/>Web UI]
        ui[ui<br/>UI Library]
        desktop[desktop<br/>Tauri App]
        console[console<br/>Admin Console]
        enterprise[enterprise<br/>Enterprise]
        function[function<br/>API Workers]
        sdk[sdk<br/>JS SDK]
        plugin[plugin<br/>Plugin System]
        util[util<br/>Utilities]
    end

    app --> ui
    app --> sdk
    app --> util
    desktop --> app
    desktop --> ui
    console --> ui
    enterprise --> ui
    opencode --> sdk
    opencode --> plugin
    opencode --> util
    sdk --> util
```

## Coreパッケージ内部構成図

```mermaid
graph TB
    subgraph "packages/opencode/src"
        subgraph "CLI Layer"
            index[index.ts<br/>Entry Point]
            cli[cli/<br/>Commands]
        end

        subgraph "Application Layer"
            session[session/<br/>Session Management]
            agent[agent/<br/>Agent System]
            skill[skill/<br/>Skills]
        end

        subgraph "Domain Layer"
            config[config/<br/>Configuration]
            permission[permission/<br/>Permissions]
            project[project/<br/>Project]
        end

        subgraph "Infrastructure Layer"
            server[server/<br/>HTTP Server]
            storage[storage/<br/>Database]
            bus[bus/<br/>Event Bus]
            provider[provider/<br/>LLM Providers]
            tool[tool/<br/>Tools]
            lsp[lsp/<br/>LSP Client]
            mcp[mcp/<br/>MCP Client]
            auth[auth/<br/>Authentication]
        end

        subgraph "Utilities"
            util[util/<br/>Helpers]
            format[format/<br/>Formatters]
        end
    end

    index --> cli
    cli --> session
    session --> agent
    session --> provider
    agent --> permission
    agent --> skill
    agent --> tool
    session --> storage
    session --> bus
    server --> session
    tool --> lsp
    tool --> mcp
    provider --> auth
    config --> project
```

## データフロー図

```mermaid
sequenceDiagram
    participant User as User
    participant CLI as CLI/TUI
    participant Server as HTTP Server
    participant Session as Session
    participant Agent as Agent
    participant Provider as Provider
    participant LLM as LLM API
    participant Tool as Tool
    participant Storage as Storage

    User->>CLI: Input Prompt
    CLI->>Server: POST /session/chat
    Server->>Session: chat()
    Session->>Agent: get agent config
    Session->>Session: build prompt
    Session->>Provider: streamText()
    Provider->>LLM: API Request
    LLM-->>Provider: Stream Response

    loop Tool Calls
        Provider-->>Session: Tool Call
        Session->>Tool: execute()
        Tool-->>Session: Result
        Session->>Provider: Continue with result
        Provider->>LLM: Send result
        LLM-->>Provider: Continue stream
    end

    Provider-->>Session: Complete
    Session->>Storage: Save messages
    Session-->>Server: Response
    Server-->>CLI: Stream/SSE
    CLI-->>User: Display output
```

## イベントバス構成図

```mermaid
graph TB
    subgraph "Publishers"
        Session[Session]
        Message[Message]
        Provider[Provider]
        Tool[Tool]
        LSP[LSP]
        MCP[MCP]
    end

    subgraph "Event Bus"
        Bus[Bus.publish]
        GlobalBus[GlobalBus]
    end

    subgraph "Subscribers"
        Server[Server/SSE]
        TUI[TUI]
        Storage[Storage]
        Plugins[Plugins]
    end

    Session --> Bus
    Message --> Bus
    Provider --> Bus
    Tool --> Bus
    LSP --> Bus
    MCP --> Bus

    Bus --> GlobalBus
    Bus --> Server
    Bus --> TUI
    Bus --> Storage
    Bus --> Plugins
```

## クラウドインフラ構成図

```mermaid
graph TB
    subgraph "Cloudflare"
        subgraph "Workers"
            API[API Worker<br/>packages/function]
            AuthWorker[Auth Worker<br/>OpenAuth]
            LogProcessor[Log Processor]
        end

        subgraph "Storage"
            KV[(Cloudflare KV)]
            R2[(R2 Bucket)]
        end

        subgraph "Static"
            WebApp[Web App<br/>packages/app]
            Docs[Docs<br/>Astro]
        end
    end

    subgraph "PlanetScale"
        MySQL[(MySQL)]
    end

    subgraph "SolidStart"
        Console[Console App<br/>packages/console]
        Enterprise[Enterprise<br/>packages/enterprise]
    end

    subgraph "External"
        GitHub[GitHub OAuth]
        Google[Google OAuth]
        Stripe[Stripe]
    end

    Console --> AuthWorker
    Console --> MySQL
    Console --> Stripe
    Enterprise --> MySQL
    API --> KV
    API --> R2
    AuthWorker --> KV
    AuthWorker --> GitHub
    AuthWorker --> Google
    LogProcessor --> KV
```

## ツール実行フロー図

```mermaid
flowchart TD
    A[LLM Response] --> B{Tool Call?}
    B -->|No| Z[Return Response]
    B -->|Yes| C[Parse Tool Call]
    C --> D{Permission Check}
    D -->|Denied| E[Return Denied]
    D -->|Ask| F[User Prompt]
    F -->|Allow| G[Execute Tool]
    F -->|Deny| E
    D -->|Allow| G
    G --> H{Tool Type}
    H -->|edit| I[File Edit]
    H -->|bash| J[Shell Command]
    H -->|read| K[File Read]
    H -->|grep| L[Code Search]
    H -->|task| M[Subagent]
    I --> N[Return Result]
    J --> N
    K --> N
    L --> N
    M --> N
    N --> O[Send to LLM]
    O --> A
```

## プロバイダー構成図

```mermaid
graph TB
    subgraph "Provider Layer"
        ProviderNS[Provider Namespace]
        Auth[Auth Module]
        Transform[Transform Module]
    end

    subgraph "AI SDK Providers"
        Anthropic[@ai-sdk/anthropic]
        OpenAI[@ai-sdk/openai]
        Google[@ai-sdk/google]
        Azure[@ai-sdk/azure]
        Bedrock[@ai-sdk/amazon-bedrock]
        Vertex[@ai-sdk/google-vertex]
        OpenRouter[@openrouter/ai-sdk-provider]
        XAI[@ai-sdk/xai]
        Mistral[@ai-sdk/mistral]
        Groq[@ai-sdk/groq]
        Custom[Custom Providers]
    end

    subgraph "Models Database"
        ModelsDev[models.dev]
    end

    ProviderNS --> Auth
    ProviderNS --> Transform
    ProviderNS --> Anthropic
    ProviderNS --> OpenAI
    ProviderNS --> Google
    ProviderNS --> Azure
    ProviderNS --> Bedrock
    ProviderNS --> Vertex
    ProviderNS --> OpenRouter
    ProviderNS --> XAI
    ProviderNS --> Mistral
    ProviderNS --> Groq
    ProviderNS --> Custom
    ProviderNS --> ModelsDev
```

## ストレージ構成図

```mermaid
erDiagram
    PROJECT ||--o{ SESSION : contains
    SESSION ||--o{ MESSAGE : contains
    MESSAGE ||--o{ PART : contains

    PROJECT {
        string id PK
        string name
        string worktree
        timestamp time_created
    }

    SESSION {
        string id PK
        string project_id FK
        string parent_id FK
        string slug
        string directory
        string title
        string version
        string share_url
        json permission
        json revert
        int summary_additions
        int summary_deletions
        int summary_files
        timestamp time_created
        timestamp time_updated
        timestamp time_compacting
        timestamp time_archived
    }

    MESSAGE {
        string id PK
        string session_id FK
        json data
        timestamp time_created
    }

    PART {
        string id PK
        string message_id FK
        string session_id FK
        json data
        timestamp time_created
    }
```
