# アーキテクチャ構成図

## システム全体構成図

```mermaid
graph TB
    subgraph "External Services"
        GitHub[GitHub API]
        OpenAI[OpenAI API]
        Sentry[Sentry]
    end

    subgraph "Client Layer"
        Browser[Web Browser]
        CLI[@liam-hq/cli]
    end

    subgraph "Edge Layer"
        Vercel[Vercel Edge Network]
        Middleware[Next.js Middleware]
    end

    subgraph "Application Layer - @liam-hq/app"
        AppRouter[App Router]
        ServerComponents[Server Components]
        ClientComponents[Client Components]
        ServerActions[Server Actions]
        APIRoutes[API Routes]
    end

    subgraph "Domain Layer"
        ERDCore[@liam-hq/erd-core]
        Schema[@liam-hq/schema]
        Agent[@liam-hq/agent]
    end

    subgraph "Infrastructure Layer"
        DB[@liam-hq/db]
        GitHubPkg[@liam-hq/github]
        Security[@liam-hq/security]
        MCPServer[@liam-hq/mcp-server]
    end

    subgraph "Data Layer"
        Supabase[(Supabase PostgreSQL)]
        SupabaseAuth[Supabase Auth]
        SupabaseRealtime[Supabase Realtime]
    end

    Browser --> Vercel
    CLI --> Schema
    Vercel --> Middleware
    Middleware --> AppRouter
    AppRouter --> ServerComponents
    AppRouter --> ClientComponents
    ServerComponents --> ServerActions
    ServerComponents --> APIRoutes
    ClientComponents --> ERDCore
    ServerActions --> Agent
    ServerActions --> DB
    APIRoutes --> Agent
    ERDCore --> Schema
    Agent --> DB
    Agent --> OpenAI
    DB --> Supabase
    DB --> SupabaseAuth
    DB --> SupabaseRealtime
    GitHubPkg --> GitHub
    ServerActions --> GitHubPkg
    ServerComponents --> Sentry
    Agent --> Sentry
```

## モノレポパッケージ依存関係図

```mermaid
graph LR
    subgraph "Applications"
        App[@liam-hq/app]
        Docs[@liam-hq/docs]
    end

    subgraph "Public Packages"
        CLI[@liam-hq/cli]
        ERDCore[@liam-hq/erd-core]
        Schema[@liam-hq/schema]
        UI[@liam-hq/ui]
    end

    subgraph "Internal Packages"
        Agent[@liam-hq/agent]
        DB[@liam-hq/db]
        GitHub[@liam-hq/github]
        MCPServer[@liam-hq/mcp-server]
        PGLite[@liam-hq/pglite-server]
        Security[@liam-hq/security]
        Neverthrow[@liam-hq/neverthrow]
    end

    App --> Agent
    App --> DB
    App --> ERDCore
    App --> Schema
    App --> UI
    App --> GitHub
    App --> Security
    App --> Neverthrow

    Docs --> UI

    CLI --> ERDCore
    CLI --> Schema

    ERDCore --> UI
    ERDCore --> Schema
    ERDCore --> Neverthrow

    Agent --> DB
    Agent --> Schema
    Agent --> PGLite
    Agent --> Neverthrow

    DB --> Neverthrow
```

## AIエージェントワークフロー図

```mermaid
stateDiagram-v2
    [*] --> ValidateInitialSchema: Start
    ValidateInitialSchema --> LeadAgent: Schema Valid
    ValidateInitialSchema --> LeadAgent: Empty Schema

    LeadAgent --> PMAgent: Analyze Requirements
    LeadAgent --> DBAgent: Direct DB Modification
    LeadAgent --> [*]: End Conversation

    PMAgent --> DBAgent: Requirements Analyzed

    DBAgent --> QAAgent: Schema Generated

    QAAgent --> LeadAgent: Tests Complete

    state LeadAgent {
        [*] --> AnalyzeInput
        AnalyzeInput --> RouteToAgent
        RouteToAgent --> [*]
    }

    state PMAgent {
        [*] --> AnalyzeRequirements
        AnalyzeRequirements --> ProcessRequirements
        ProcessRequirements --> SaveArtifact
        SaveArtifact --> [*]
    }

    state DBAgent {
        [*] --> GenerateSchema
        GenerateSchema --> ValidateSchema
        ValidateSchema --> [*]
    }

    state QAAgent {
        [*] --> GenerateTestCases
        GenerateTestCases --> RunTests
        RunTests --> AnalyzeFailures
        AnalyzeFailures --> [*]
    }
```

## レイヤーアーキテクチャ図

```mermaid
graph TB
    subgraph "Presentation Layer"
        UI[@liam-hq/ui]
        ERDCore[@liam-hq/erd-core]
        AppPages[App Pages & Components]
        DocsPages[Docs Pages]
    end

    subgraph "Application Layer"
        ServerActions[Server Actions]
        APIRoutes[API Routes]
        Agent[@liam-hq/agent]
        Middleware[Next.js Middleware]
    end

    subgraph "Domain Layer"
        Schema[@liam-hq/schema]
        ValibotSchemas[Valibot Schemas]
        DomainTypes[Domain Types]
    end

    subgraph "Infrastructure Layer"
        DB[@liam-hq/db]
        GitHub[@liam-hq/github]
        Security[@liam-hq/security]
        SupabaseClient[Supabase Client]
        LangGraph[LangGraph]
        LangChain[LangChain]
    end

    AppPages --> UI
    AppPages --> ERDCore
    DocsPages --> UI
    ERDCore --> UI

    AppPages --> ServerActions
    AppPages --> APIRoutes
    ServerActions --> Agent
    APIRoutes --> Agent
    Middleware --> DB

    Agent --> Schema
    ServerActions --> Schema
    ERDCore --> Schema

    Agent --> DB
    Agent --> LangGraph
    Agent --> LangChain
    DB --> SupabaseClient
    ServerActions --> GitHub
    Middleware --> Security
```

## データフロー図

```mermaid
sequenceDiagram
    participant User
    participant Browser
    participant Middleware
    participant ServerComponent
    participant ServerAction
    participant Agent
    participant Supabase
    participant OpenAI

    User->>Browser: Access Application
    Browser->>Middleware: HTTP Request
    Middleware->>Middleware: Check Session (Supabase Auth)

    alt Not Authenticated
        Middleware-->>Browser: Redirect to /login
    else Authenticated
        Middleware->>ServerComponent: Forward Request
        ServerComponent->>Supabase: Fetch Data (RLS Applied)
        Supabase-->>ServerComponent: Return Data
        ServerComponent-->>Browser: Render Page
    end

    User->>Browser: Send Chat Message
    Browser->>ServerAction: Submit Message
    ServerAction->>Agent: Invoke LangGraph Workflow
    Agent->>OpenAI: LLM Request
    OpenAI-->>Agent: LLM Response
    Agent->>Supabase: Save Schema Changes
    Supabase-->>Agent: Confirmation
    Agent-->>ServerAction: Workflow Result
    ServerAction-->>Browser: Stream Response
    Browser-->>User: Display Result
```

## 認証フロー図

```mermaid
sequenceDiagram
    participant User
    participant Browser
    participant NextJS as Next.js App
    participant Middleware
    participant SupabaseAuth as Supabase Auth
    participant GitHub as GitHub OAuth
    participant DB as PostgreSQL

    User->>Browser: Click Login
    Browser->>NextJS: GET /login
    NextJS-->>Browser: Render Login Page

    User->>Browser: Click GitHub Login
    Browser->>SupabaseAuth: Initiate OAuth
    SupabaseAuth->>GitHub: Redirect to GitHub
    GitHub-->>User: Show Authorization Page
    User->>GitHub: Authorize
    GitHub->>SupabaseAuth: Callback with Code
    SupabaseAuth->>GitHub: Exchange Code for Token
    GitHub-->>SupabaseAuth: Access Token
    SupabaseAuth->>DB: Create/Update User Record
    SupabaseAuth-->>Browser: Redirect to /auth/callback/github

    Browser->>NextJS: GET /auth/callback/github
    NextJS->>SupabaseAuth: Verify Session
    SupabaseAuth-->>NextJS: Session Valid
    NextJS->>Middleware: Set Session Cookie
    NextJS-->>Browser: Redirect to Dashboard

    Note over Browser,DB: Subsequent Requests
    Browser->>Middleware: Request with Cookie
    Middleware->>SupabaseAuth: Validate Session
    SupabaseAuth-->>Middleware: User Info
    Middleware->>NextJS: Forward (Authenticated)
```

## デプロイメントアーキテクチャ図

```mermaid
graph TB
    subgraph "Development"
        LocalDev[Local Development]
        PreviewBranch[Preview Branch]
    end

    subgraph "Vercel Platform"
        VercelEdge[Vercel Edge Network]
        VercelFunctions[Vercel Serverless Functions]
        VercelCDN[Vercel CDN]
    end

    subgraph "Supabase Platform"
        SupabaseProduction[Production Database]
        SupabasePreview[Preview Branches]
        SupabaseAuth[Auth Service]
        SupabaseRealtime[Realtime Service]
    end

    subgraph "External APIs"
        GitHubAPI[GitHub API]
        OpenAIAPI[OpenAI API]
        SentryAPI[Sentry API]
    end

    LocalDev --> SupabasePreview
    PreviewBranch --> VercelEdge
    PreviewBranch --> SupabasePreview

    VercelEdge --> VercelFunctions
    VercelEdge --> VercelCDN
    VercelFunctions --> SupabaseProduction
    VercelFunctions --> SupabaseAuth
    VercelFunctions --> SupabaseRealtime
    VercelFunctions --> GitHubAPI
    VercelFunctions --> OpenAIAPI
    VercelFunctions --> SentryAPI
```
