# rutujakumbhar17/kai-the-companion architecture

> Architecture diagram of the GitHub repository rutujakumbhar17/kai-the-companion, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rutujakumbhar17/kai-the-companion
- Repository: https://github.com/rutujakumbhar17/kai-the-companion

## Overview

KAI is a multimodal emotional companion. The main user-to-result flow is camera frames and chat input from the browser, through Flask/Socket.IO, to emotion analysis and mood-aware LLM responses, with optional speech output; diary entries and emotion/chat records provide persistent reflection and dashboard views. The README documents dashboard, diary, and Faceography capabilities beyond the sampled code. The React app appears to be a separate marketing landing page. Relationships for unsampled routes and UI modules are based on README documentation, not inspected source.

Main entry points: [`app.py`](https://github.com/rutujakumbhar17/kai-the-companion/blob/main/app.py) (Flask SocketIO hub), [`camera_utils.py`](https://github.com/rutujakumbhar17/kai-the-companion/blob/main/camera_utils.py) (Frame emotion analysis), [`static/js/client.js`](https://github.com/rutujakumbhar17/kai-the-companion/blob/main/static/js/client.js) (Camera client), [`static/js/chat.js`](https://github.com/rutujakumbhar17/kai-the-companion/blob/main/static/js/chat.js) (Chat client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Web experience"]
  node_web_ui["Companion UI"]
  node_camera_client["Camera client<br/>[client.js]"]
  node_chat_client["Chat client<br/>[chat.js]"]
  node_diary_client["Diary client<br/>[diary.js]"]
  node_landing["Marketing landing<br/>[App.tsx]"]
end

subgraph group_server["Application hub"]
  node_server["Flask SocketIO hub<br/>[app.py]"]
  node_dashboard["Mood dashboard<br/>[app.py]"]
  node_diary_api["Diary API<br/>[app.py]"]
end

subgraph group_emotion["Emotion sensing"]
  node_emotion_engine["Frame emotion analysis<br/>[camera_utils.py]"]
  node_vision_model["Vision models<br/>[camera_utils.py]"]
  node_cascade["Face cascade"]
end

subgraph group_companion["Companion services"]
  node_groq["Groq LLM"]
  node_openrouter["OpenRouter fallback"]
  node_speech["Speech synthesis<br/>[app.py]"]
end

subgraph group_reflection["Reflection and records"]
  node_diary_db[("Diary SQLite<br/>[app.py]")]
  node_emotion_log[("Emotion CSV<br/>[camera_utils.py]")]
  node_chat_log[("Chat history JSON<br/>[app.py]")]
  node_gallery[("Faceography captures<br/>[camera_utils.py]")]
end

node_user(("User"))

node_user -->|"uses"| node_web_ui
node_user -->|"shares frames"| node_camera_client
node_user -->|"sends messages"| node_chat_client
node_user -->|"journals"| node_diary_client
node_camera_client -->|"streams frames"| node_server
node_chat_client -->|"sends chat"| node_server
node_diary_client -->|"calls diary API"| node_diary_api
node_server -->|"analyzes frames"| node_emotion_engine
node_emotion_engine -->|"classifies faces"| node_vision_model
node_emotion_engine -->|"detects faces"| node_cascade
node_emotion_engine -->|"logs moods"| node_emotion_log
node_emotion_engine -->|"buffers captures"| node_gallery
node_server -->|"requests response"| node_groq
node_server -.->|"falls back"| node_openrouter
node_server -->|"generates audio"| node_speech
node_server -->|"persists turns"| node_chat_log
node_server -->|"stores entries"| node_diary_db
node_server -->|"serves mood views"| node_dashboard
node_dashboard -->|"reads trends"| node_emotion_log
node_dashboard -->|"reads reflections"| node_diary_db
node_landing -->|"presents entry"| node_user

click node_web_ui "https://github.com/rutujakumbhar17/kai-the-companion/tree/main/templates"
click node_camera_client "https://github.com/rutujakumbhar17/kai-the-companion/blob/main/static/js/client.js"
click node_chat_client "https://github.com/rutujakumbhar17/kai-the-companion/blob/main/static/js/chat.js"
click node_diary_client "https://github.com/rutujakumbhar17/kai-the-companion/blob/main/static/js/diary.js"
click node_landing "https://github.com/rutujakumbhar17/kai-the-companion/blob/main/frontend/src/App.tsx"
click node_server "https://github.com/rutujakumbhar17/kai-the-companion/blob/main/app.py"
click node_dashboard "https://github.com/rutujakumbhar17/kai-the-companion/blob/main/app.py"
click node_diary_api "https://github.com/rutujakumbhar17/kai-the-companion/blob/main/app.py"
click node_emotion_engine "https://github.com/rutujakumbhar17/kai-the-companion/blob/main/camera_utils.py"
click node_vision_model "https://github.com/rutujakumbhar17/kai-the-companion/blob/main/camera_utils.py"
click node_cascade "https://github.com/rutujakumbhar17/kai-the-companion/blob/main/haarcascade_frontalface_default.xml"
click node_speech "https://github.com/rutujakumbhar17/kai-the-companion/blob/main/app.py"
click node_diary_db "https://github.com/rutujakumbhar17/kai-the-companion/blob/main/app.py"
click node_emotion_log "https://github.com/rutujakumbhar17/kai-the-companion/blob/main/camera_utils.py"
click node_chat_log "https://github.com/rutujakumbhar17/kai-the-companion/blob/main/app.py"
click node_gallery "https://github.com/rutujakumbhar17/kai-the-companion/blob/main/camera_utils.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_web_ui,node_camera_client,node_chat_client,node_diary_client,node_landing,node_user toneBlue
class node_server,node_dashboard,node_diary_api toneAmber
class node_emotion_engine,node_vision_model,node_cascade toneMint
class node_groq,node_openrouter,node_speech toneRose
class node_diary_db,node_emotion_log,node_chat_log,node_gallery toneIndigo
```

## Components

### Web experience

- [Companion UI](https://github.com/rutujakumbhar17/kai-the-companion/tree/main/templates): `templates`
- [Camera client](https://github.com/rutujakumbhar17/kai-the-companion/blob/main/static/js/client.js): `static/js/client.js`
- [Chat client](https://github.com/rutujakumbhar17/kai-the-companion/blob/main/static/js/chat.js): `static/js/chat.js`
- [Diary client](https://github.com/rutujakumbhar17/kai-the-companion/blob/main/static/js/diary.js): `static/js/diary.js`
- [Marketing landing](https://github.com/rutujakumbhar17/kai-the-companion/blob/main/frontend/src/App.tsx): `frontend/src/App.tsx`

### Application hub

- [Flask SocketIO hub](https://github.com/rutujakumbhar17/kai-the-companion/blob/main/app.py): `app.py`
- [Mood dashboard](https://github.com/rutujakumbhar17/kai-the-companion/blob/main/app.py): `app.py`
- [Diary API](https://github.com/rutujakumbhar17/kai-the-companion/blob/main/app.py): `app.py`

### Emotion sensing

- [Frame emotion analysis](https://github.com/rutujakumbhar17/kai-the-companion/blob/main/camera_utils.py): `camera_utils.py`
- [Vision models](https://github.com/rutujakumbhar17/kai-the-companion/blob/main/camera_utils.py): `camera_utils.py`
- [Face cascade](https://github.com/rutujakumbhar17/kai-the-companion/blob/main/haarcascade_frontalface_default.xml): `haarcascade_frontalface_default.xml`

### Companion services

- **Groq LLM**
- **OpenRouter fallback**
- [Speech synthesis](https://github.com/rutujakumbhar17/kai-the-companion/blob/main/app.py): `app.py`

### Reflection and records

- [Diary SQLite](https://github.com/rutujakumbhar17/kai-the-companion/blob/main/app.py): `app.py`
- [Emotion CSV](https://github.com/rutujakumbhar17/kai-the-companion/blob/main/camera_utils.py): `camera_utils.py`
- [Chat history JSON](https://github.com/rutujakumbhar17/kai-the-companion/blob/main/app.py): `app.py`
- [Faceography captures](https://github.com/rutujakumbhar17/kai-the-companion/blob/main/camera_utils.py): `camera_utils.py`

### Other components

- **User**

## Connections

- User → Companion UI: uses (evidence: `README.md`)
- User → Camera client: shares frames (evidence: `README.md`)
- User → Chat client: sends messages (evidence: `README.md`)
- User → Diary client: journals (evidence: `README.md`)
- Camera client → Flask SocketIO hub: streams frames (evidence: `static/js/client.js`)
- Chat client → Flask SocketIO hub: sends chat (evidence: `README.md`)
- Diary client → Diary API: calls diary API (evidence: `static/js/diary.js`)
- Flask SocketIO hub → Frame emotion analysis: analyzes frames (evidence: `app.py`)
- Frame emotion analysis → Vision models: classifies faces (evidence: `camera_utils.py`)
- Frame emotion analysis → Face cascade: detects faces (evidence: `camera_utils.py`)
- Frame emotion analysis → Emotion CSV: logs moods (evidence: `camera_utils.py`)
- Frame emotion analysis → Faceography captures: buffers captures (evidence: `camera_utils.py`)
- Flask SocketIO hub → Groq LLM: requests response (evidence: `README.md`)
- Flask SocketIO hub → OpenRouter fallback: falls back (evidence: `app.py`)
- Flask SocketIO hub → Speech synthesis: generates audio (evidence: `README.md`)
- Flask SocketIO hub → Chat history JSON: persists turns (evidence: `README.md`)
- Flask SocketIO hub → Diary SQLite: stores entries (evidence: `README.md`)
- Flask SocketIO hub → Mood dashboard: serves mood views (evidence: `README.md`)
- Mood dashboard → Emotion CSV: reads trends (evidence: `app.py`)
- Mood dashboard → Diary SQLite: reads reflections (evidence: `app.py`)
- Marketing landing → User: presents entry (evidence: `frontend/src/App.tsx`)
