# ahwei/rn-ai-assistant architecture

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

- Interactive diagram: https://gitdiagram.com/ahwei/rn-ai-assistant
- Repository: https://github.com/ahwei/rn-ai-assistant

## Overview

##### Main Components

A. Client Layer:
- React Native Mobile Application (iOS & Android)
- Key frontend modules:
- Chat Interface (/src/components/chat)
- Expert Selection System
- Theme/UI Components
- Context Management (/src/contexts)

B. Backend Layer:
- FastAPI Server
- OpenAI Integration
- Database System
- API Endpoints

C. External Services:
- OpenAI API
- Mobile Platform Services (iOS/Android)

**[Mobile Clients]**

- ▼
**[Frontend Layer]**

- ▼
**[API Gateway]**

- ▼
**[Backend Services]**

- ▼
**[External Services]**

##### Specific Component Details

Frontend Layer (Blue):
- Main app structure (/src/app)
- Chat components
- Contexts and hooks
- State management flow

Backend Layer (Green):
- FastAPI application structure
- API endpoints (/app/api)
- Core configurations
- Database models

External Services (Purple):
- OpenAI API integration
- Platform-specific services

##### Key Flows to Highlight

A. Chat Flow:
**User Input → Chat Interface → API Gateway → Backend Processing → OpenAI API → Response Streaming → UI Update**

B. Expert Selection Flow:
**Expert Selection → Context Update → Chat Configuration → Backend Configuration → Response Adaptation**

##### Important Elements

- WebSocket connections for real-time chat
- State management flow
- API endpoint groupings
- Database interactions
- Authentication flow (if present)

##### Architectural Patterns to Emphasize

- Mobile-first architecture
- Microservices approach
- Real-time communication
- Stream processing
- Context-based state management

##### Implementation Details

- Frontend architecture (React Native)
- Backend services (FastAPI)
- Data persistence
- External service integration
- DevOps components (Docker)

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Mobile
        iOS["iOS Client"]
        Android["Android Client"]
    end

    subgraph Frontend
        ChatInterface["Chat Interface"]:::frontend
        click ChatInterface "https://github.com/ahwei/RN-AI-Assistant/tree/main/my-app/src/components/chat/"
        
        MessageDisplay["Message Display"]:::frontend
        click MessageDisplay "https://github.com/ahwei/RN-AI-Assistant/blob/main/my-app/src/components/chat/MessageBubble.tsx"
        
        ChatInput["Chat Input"]:::frontend
        click ChatInput "https://github.com/ahwei/RN-AI-Assistant/blob/main/my-app/src/components/chat/ChatInput.tsx"
        
        AIResponse["AI Response"]:::frontend
        click AIResponse "https://github.com/ahwei/RN-AI-Assistant/blob/main/my-app/src/components/chat/AIStreamingBubble.tsx"
        
        ExpertSelector["Expert Selection"]:::frontend
        click ExpertSelector "https://github.com/ahwei/RN-AI-Assistant/blob/main/my-app/src/components/chat/ExpertSelector.tsx"
        
        UI["Theme/UI Components"]:::frontend
        click UI "https://github.com/ahwei/RN-AI-Assistant/blob/main/my-app/src/components/ThemedView.tsx"
        
        ChatContext["Chat Context"]:::frontend
        click ChatContext "https://github.com/ahwei/RN-AI-Assistant/blob/main/my-app/src/contexts/ChatContext.tsx"
        
        ChatHooks["Chat Hooks"]:::frontend
        click ChatHooks "https://github.com/ahwei/RN-AI-Assistant/blob/main/my-app/src/hooks/useChat.tsx"
        
        Types["Type Definitions"]:::frontend
        click Types "https://github.com/ahwei/RN-AI-Assistant/blob/main/my-app/src/types/chat.ts"
        
        Routes["Frontend Routes"]:::frontend
        click Routes "https://github.com/ahwei/RN-AI-Assistant/blob/main/my-app/src/app/(chats)/_layout.tsx"
    end

    subgraph Backend
        FastAPI["FastAPI Server"]:::backend
        click FastAPI "https://github.com/ahwei/RN-AI-Assistant/blob/main/backend/app/main.py"
        
        APIEndpoints["API Endpoints"]:::backend
        click APIEndpoints "https://github.com/ahwei/RN-AI-Assistant/blob/main/backend/app/api/endpoints.py"
        
        OpenAIConfig["OpenAI Integration"]:::backend
        click OpenAIConfig "https://github.com/ahwei/RN-AI-Assistant/blob/main/backend/app/core/openai_config.py"
        
        DBSetup["Database Setup"]:::backend
        click DBSetup "https://github.com/ahwei/RN-AI-Assistant/blob/main/backend/app/database.py"
        
        Models["Database Models"]:::backend
        click Models "https://github.com/ahwei/RN-AI-Assistant/blob/main/backend/app/models/models.py"
        
        Config["Backend Config"]:::backend
        click Config "https://github.com/ahwei/RN-AI-Assistant/blob/main/backend/app/core/config.py"
    end

    subgraph External
        OpenAI["OpenAI API"]:::external
        PlatformServices["Platform Services"]:::external
    end

    iOS & Android --> ChatInterface
    ChatInterface --> MessageDisplay & ChatInput & AIResponse
    ExpertSelector --> ChatContext
    UI --> ChatInterface
    ChatContext --> ChatHooks
    Types --> ChatInterface
    Routes --> ChatInterface
    
    ChatInterface --> APIEndpoints
    APIEndpoints --> FastAPI
    FastAPI --> OpenAIConfig
    OpenAIConfig --> OpenAI
    FastAPI --> DBSetup
    DBSetup --> Models
    Config --> FastAPI
    
    iOS & Android --> PlatformServices

    classDef frontend fill:#3498db,stroke:#2980b9,color:white
    classDef backend fill:#2ecc71,stroke:#27ae60,color:white
    classDef external fill:#9b59b6,stroke:#8e44ad,color:white

    %% Legend
    subgraph Legend
        Frontend_L["Frontend Component"]:::frontend
        Backend_L["Backend Component"]:::backend
        External_L["External Service"]:::external
    end
```
