# martinrojas/chatbot-playground architecture

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

- Interactive diagram: https://gitdiagram.com/martinrojas/chatbot-playground
- Repository: https://github.com/martinrojas/chatbot-playground

## Overview

##### Client-Side Layer
- Components Module (src/lib/components)
- State Management (nanostores)
- Token Management System
- Use rounded rectangles for components

##### Server-Side Layer
- API Routes module (/api/chat)
- SSE handling mechanism

##### External Services
- OpenAI's ChatGPT API

Component Relationships:

- ChatMessage
- IOPanel
- SystemPanel
- SystemPrompts
- TokenCalculator
- MockWindow

##### State Management
- Message Stores
- Token Store

##### API Layer
- Chat Endpoint
- SSE Handler

Implementation Guidelines:

##### Use standardized symbols
- Rectangles with rounded corners for UI components
- Solid for direct calls

##### Components in layers from top to bottom
- UI Layer (Components)
- State Management Layer (Stores)
- API Layer (Server Routes)
- External Services Layer (ChatGPT API)

##### Use clear spacing and alignment
- Related components
- Maintain consistent spacing
- Align components vertically when they're in the same layer

Additional Notes:

##### Environment indicators
- Development (localhost:3000)
- Production (chat.martinrojas.dev)

##### Build/deployment flow (optional)
- Build process
- Adapter requirements for deployment

Remember to:
- Use consistent styling
- Version information
- A title and description
- Document any assumptions or important notes

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Frontend
        UI[User Interface]
        subgraph Components
            CM["ChatMessage"]:::component
            IP["IOPanel"]:::component
            SP["SystemPanel"]:::component
            SPR["SystemPrompts"]:::component
            TC["TokenCalculator"]:::component
            MW["MockWindow"]:::component
        end
        subgraph StateManagement
            MS["Message Store"]:::store
            TS["Token Store"]:::store
            TMS["Token Management System"]:::util
        end
    end

    subgraph ServerLayer
        API["API Routes"]:::server
        SSE["SSE Handler"]:::server
    end

    subgraph ExternalServices
        GPT["ChatGPT API"]:::external
    end

    UI <--> Components
    Components <--> StateManagement
    StateManagement --> API
    API <--> SSE
    SSE <-.->|Stream| GPT
    GPT <.->|Response Stream| UI

    %% Click Events
    click CM "https://github.com/martinrojas/chatbot-playground/blob/main/src/lib/components/ChatMessage.svelte"
    click IP "https://github.com/martinrojas/chatbot-playground/blob/main/src/lib/components/IOPanel.svelte"
    click SP "https://github.com/martinrojas/chatbot-playground/blob/main/src/lib/components/SystemPanel.svelte"
    click SPR "https://github.com/martinrojas/chatbot-playground/blob/main/src/lib/components/SystemPrompts.svelte"
    click TC "https://github.com/martinrojas/chatbot-playground/blob/main/src/lib/components/TokenCalculator.svelte"
    click MW "https://github.com/martinrojas/chatbot-playground/blob/main/src/lib/components/MockWindow.svelte"
    click MS "https://github.com/martinrojas/chatbot-playground/blob/main/src/lib/nanostores/messagesStores.ts"
    click TS "https://github.com/martinrojas/chatbot-playground/blob/main/src/lib/nanostores/tokenStore.ts"
    click TMS "https://github.com/martinrojas/chatbot-playground/blob/main/src/lib/tokenizer.ts"
    click API "https://github.com/martinrojas/chatbot-playground/blob/main/src/routes/api/chat/+server.ts"
    click SSE "https://github.com/martinrojas/chatbot-playground/blob/main/src/routes/api/chat/+server.ts"

    %% Styles
    classDef component fill:#a5d8ff,stroke:#0077cc
    classDef store fill:#ffd8a5,stroke:#cc7700
    classDef server fill:#a5ffa5,stroke:#00cc00
    classDef external fill:#d8a5ff,stroke:#7700cc
    classDef util fill:#f5f5f5,stroke:#666666

    %% Legend
    subgraph Legend
        L1["UI Component"]:::component
        L2["Store"]:::store
        L3["Server Component"]:::server
        L4["External Service"]:::external
        L5["Utility"]:::util
    end
```
