# インフラ構成図

## システム全体構成図

```mermaid
graph TB
    subgraph Users["Users"]
        Browser["Browser"]
        CLI["OpenCode CLI"]
        Desktop["Desktop App"]
        VSCode["VS Code Extension"]
    end

    subgraph CloudflareEdge["Cloudflare Edge"]
        subgraph Workers["Cloudflare Workers"]
            ApiWorker["API Worker<br/>api.{domain}"]
            AuthWorker["Auth Worker<br/>auth.{domain}"]
            ConsoleWorker["Console Worker<br/>{domain}"]
            TeamsWorker["Teams Worker<br/>{shortDomain}"]
            LogProcessor["Log Processor Worker"]
        end

        subgraph StaticSites["Cloudflare Pages"]
            DocsAstro["Docs (Astro)<br/>docs.{domain}"]
            WebApp["Web App (Static)<br/>app.{domain}"]
        end

        subgraph Storage["Cloudflare Storage"]
            R2Bucket["R2 Bucket"]
            R2ZenData["R2 ZenData"]
            R2ZenDataNew["R2 ZenDataNew"]
            R2Enterprise["R2 EnterpriseStorage"]
            KVAuth["KV AuthStorage"]
            KVGateway["KV GatewayKv"]
        end

        DurableObjects["Durable Objects<br/>SyncServer"]
    end

    subgraph ExternalServices["External Services"]
        PlanetScale["PlanetScale<br/>(MySQL)"]
        Stripe["Stripe<br/>(Payments)"]
        GitHub["GitHub<br/>(OAuth/CI)"]
        Google["Google<br/>(OAuth)"]
        Discord["Discord<br/>(Notifications)"]
        Feishu["Feishu<br/>(Notifications)"]
        EmailOctopus["EmailOctopus<br/>(Email)"]
        Honeycomb["Honeycomb<br/>(Observability)"]
        AWSSES["AWS SES<br/>(Email)"]
    end

    Browser --> DocsAstro
    Browser --> WebApp
    Browser --> ConsoleWorker
    CLI --> ApiWorker
    Desktop --> ApiWorker
    VSCode --> ApiWorker

    ApiWorker --> R2Bucket
    ApiWorker --> DurableObjects
    ApiWorker --> Discord
    ApiWorker --> Feishu
    ApiWorker --> GitHub

    AuthWorker --> KVAuth
    AuthWorker --> PlanetScale
    AuthWorker --> GitHub
    AuthWorker --> Google

    ConsoleWorker --> R2ZenData
    ConsoleWorker --> R2ZenDataNew
    ConsoleWorker --> PlanetScale
    ConsoleWorker --> Stripe
    ConsoleWorker --> KVGateway
    ConsoleWorker --> AWSSES
    ConsoleWorker --> EmailOctopus

    TeamsWorker --> R2Enterprise

    LogProcessor --> Honeycomb
    ConsoleWorker -.-> LogProcessor
```

## デプロイメントパイプライン

```mermaid
graph LR
    subgraph SourceControl["Source Control"]
        GitHubRepo["GitHub Repository<br/>anomalyco/opencode"]
    end

    subgraph CIJobs["GitHub Actions"]
        subgraph VersionJob["Version Job"]
            Version["Determine Version"]
        end

        subgraph BuildJobs["Build Jobs"]
            BuildCLI["Build CLI<br/>(Linux x64/ARM64)"]
            BuildTauriMac["Build Tauri<br/>(macOS x64/ARM64)"]
            BuildTauriWin["Build Tauri<br/>(Windows x64)"]
            BuildTauriLinux["Build Tauri<br/>(Linux x64/ARM64)"]
        end

        subgraph PublishJob["Publish Job"]
            PublishNPM["Publish to NPM"]
            PublishGHCR["Publish to GHCR"]
            PublishAUR["Publish to AUR"]
            PublishRelease["Create GitHub Release"]
        end

        subgraph DeployJob["Deploy Job"]
            DeploySST["SST Deploy"]
        end
    end

    subgraph Artifacts["Artifact Destinations"]
        NPM["npm Registry<br/>opencode-ai"]
        GHCR["GitHub Container Registry<br/>ghcr.io/anomalyco"]
        AUR["Arch User Repository"]
        GHRelease["GitHub Releases"]
        Cloudflare["Cloudflare<br/>(Workers/Pages)"]
    end

    GitHubRepo -->|push dev/production| DeployJob
    GitHubRepo -->|workflow_dispatch| VersionJob

    VersionJob --> BuildCLI
    BuildCLI --> BuildTauriMac
    BuildCLI --> BuildTauriWin
    BuildCLI --> BuildTauriLinux
    BuildTauriMac --> PublishJob
    BuildTauriWin --> PublishJob
    BuildTauriLinux --> PublishJob

    PublishNPM --> NPM
    PublishGHCR --> GHCR
    PublishAUR --> AUR
    PublishRelease --> GHRelease

    DeploySST --> Cloudflare
```

## コンテナイメージ階層

```mermaid
graph TB
    subgraph BaseImages["Base Images"]
        Ubuntu["Ubuntu 24.04"]
    end

    subgraph BuildImages["Build Images"]
        Base["base<br/>(build-essential, git, etc.)"]
        BunNode["bun-node<br/>(Node.js 24.4.0, Bun 1.3.5)"]
        Publish["publish<br/>(Docker, pacman)"]
        Rust["rust<br/>(Rust stable)"]
        TauriLinux["tauri-linux<br/>(WebKit, GTK)"]
    end

    subgraph RuntimeImages["Runtime Images"]
        OpenCodeCLI["opencode<br/>(Alpine, CLI binary)"]
    end

    Ubuntu --> Base
    Base --> BunNode
    BunNode --> Publish
    BunNode --> Rust
    Rust --> TauriLinux

    Alpine["Alpine Linux"] --> OpenCodeCLI
```

## ネットワークアーキテクチャ

```mermaid
graph TB
    subgraph Internet["Internet"]
        Users["Users"]
    end

    subgraph CloudflareDNS["Cloudflare DNS"]
        DNS["DNS Zone<br/>opencode.ai"]
    end

    subgraph Domains["Domain Routing"]
        ProdDomain["opencode.ai"]
        DevDomain["dev.opencode.ai"]
        StageDomain["{stage}.dev.opencode.ai"]
        ShortDomain["opncd.ai"]
    end

    subgraph Subdomains["Subdomain Routing"]
        Api["api.*"]
        Docs["docs.*"]
        App["app.*"]
        Auth["auth.*"]
        Root["Root (Console)"]
    end

    subgraph Workers["Cloudflare Workers"]
        ApiWorker["API Worker"]
        DocsWorker["Docs (Astro)"]
        AppWorker["App (Static)"]
        AuthWorker["Auth Worker"]
        ConsoleWorker["Console Worker"]
        TeamsWorker["Teams Worker"]
    end

    Users --> DNS
    DNS --> ProdDomain
    DNS --> DevDomain
    DNS --> StageDomain
    DNS --> ShortDomain

    ProdDomain --> Subdomains
    DevDomain --> Subdomains
    StageDomain --> Subdomains

    Api --> ApiWorker
    Docs --> DocsWorker
    App --> AppWorker
    Auth --> AuthWorker
    Root --> ConsoleWorker
    ShortDomain --> TeamsWorker
```

## データフロー

```mermaid
sequenceDiagram
    participant User as User
    participant CF as Cloudflare Edge
    participant API as API Worker
    participant Auth as Auth Worker
    participant DO as Durable Objects
    participant PS as PlanetScale
    participant R2 as R2 Storage
    participant Stripe as Stripe

    User->>CF: HTTPS Request
    CF->>Auth: Authenticate
    Auth->>PS: Verify Session
    PS-->>Auth: Session Data
    Auth-->>CF: Auth Token

    CF->>API: API Request + Token
    API->>DO: Sync State
    DO-->>API: Current State
    API->>R2: Read/Write Data
    R2-->>API: Data Response
    API-->>CF: API Response
    CF-->>User: HTTPS Response

    Note over User,Stripe: Payment Flow
    User->>CF: Checkout Request
    CF->>Stripe: Create Session
    Stripe-->>CF: Checkout URL
    CF-->>User: Redirect to Stripe
    Stripe->>CF: Webhook Event
    CF->>PS: Update Subscription
```

## 環境分離

```mermaid
graph TB
    subgraph Production["Production Environment"]
        ProdWorkers["Workers<br/>(opencode.ai)"]
        ProdDB["PlanetScale<br/>(production branch)"]
        ProdStorage["R2 Buckets<br/>(production)"]
    end

    subgraph Development["Development Environment"]
        DevWorkers["Workers<br/>(dev.opencode.ai)"]
        DevDB["PlanetScale<br/>(dev branch)"]
        DevStorage["R2 Buckets<br/>(dev)"]
    end

    subgraph Staging["Staging Environments"]
        StageWorkers["Workers<br/>({stage}.dev.opencode.ai)"]
        StageDB["PlanetScale<br/>({stage} branch)"]
        StageStorage["R2 Buckets<br/>({stage})"]
    end

    subgraph CI["CI/CD"]
        GHA["GitHub Actions"]
    end

    GHA -->|"push production"| Production
    GHA -->|"push dev"| Development
    GHA -->|"manual/PR"| Staging

    ProdDB -.->|"parent branch"| DevDB
    ProdDB -.->|"parent branch"| StageDB
```
