# idootop/mi-gpt architecture

> Architecture diagram of the GitHub repository idootop/mi-gpt, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/idootop/mi-gpt
- Repository: https://github.com/idootop/mi-gpt

## Overview

MiGPT turns messages received through a Xiaomi XiaoAI speaker into streamed, context-aware AI replies played back through the speaker. The main runtime starts the speaker and conversation manager, gathers profile, room, history, and short-/long-term memory context, calls an OpenAI-compatible model, then streams the answer to speaker playback. The README also documents persona changes and custom TTS. Edges reflect the sampled entry point, bot, memory, database, OpenAI, and speaker code; some conversation and speaker internals are represented by their exact enclosing modules because their source was not sampled.

Main entry points: [`src/services/bot/index.ts`](https://github.com/idootop/mi-gpt/blob/main/src/services/bot/index.ts) (Bot workflow), [`src/services/bot/memory/index.ts`](https://github.com/idootop/mi-gpt/blob/main/src/services/bot/memory/index.ts) (Memory manager), [`src/index.ts`](https://github.com/idootop/mi-gpt/blob/main/src/index.ts) (MiGPT client), [`src/services/bot/conversation.ts`](https://github.com/idootop/mi-gpt/blob/main/src/services/bot/conversation.ts) (Conversation manager)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime orchestration"]
  node_migpt["MiGPT client<br/>[index.ts]"]
end

subgraph group_conversation["Conversation and memory"]
  node_bot["Bot workflow<br/>[index.ts]"]
  node_conversation_manager["Conversation manager<br/>[conversation.ts]"]
  node_memory_manager["Memory manager<br/>[index.ts]"]
  node_short_memory["Short-term memory<br/>[short-term.ts]"]
  node_long_memory["Long-term memory<br/>[long-term.ts]"]
end

subgraph group_speaker["Speaker integration"]
  node_speaker_ai["AI speaker<br/>[ai.ts]"]
  node_speaker_loop["Speaker message loop<br/>[speaker.ts]"]
  node_speaker_base["Speaker playback<br/>[base.ts]"]
  node_stream["Stream response<br/>[stream.ts]"]
end

subgraph group_persistence["Persistence and AI"]
  node_openai_client["OpenAI client<br/>[openai.ts]"]
  node_db_runtime["Database runtime<br/>[index.ts]"]
  node_prisma_store[("Application database<br/>[schema.prisma]")]
  node_memory_crud["Memory persistence<br/>[memory.ts]"]
  node_memory_tables["Memory records"]
  node_message_store["Message persistence<br/>[message.ts]"]
end

node_user(("Speaker user"))
node_xiaomi{{"Xiaomi speaker services"}}
node_model_service{{"LLM service"}}
node_custom_tts{{"Custom TTS endpoint"}}

node_user -->|"speaks to"| node_xiaomi
node_migpt -->|"constructs"| node_speaker_ai
node_migpt -->|"constructs"| node_bot
node_migpt -->|"initializes"| node_db_runtime
node_migpt -->|"starts"| node_bot
node_migpt -->|"runs with"| node_db_runtime
node_bot -->|"loads context"| node_conversation_manager
node_bot -->|"starts speaker"| node_speaker_ai
node_speaker_ai -->|"runs"| node_speaker_loop
node_speaker_loop -->|"polls messages"| node_xiaomi
node_speaker_ai -->|"uses playback"| node_speaker_base
node_speaker_loop -->|"dispatches query"| node_bot
node_bot -->|"reads history"| node_conversation_manager
node_bot -->|"reads memories"| node_memory_manager
node_memory_manager -->|"queries memories"| node_memory_crud
node_memory_manager -->|"generates summaries"| node_short_memory
node_memory_manager -->|"generates summaries"| node_long_memory
node_conversation_manager -->|"stores messages"| node_message_store
node_conversation_manager -->|"records messages"| node_memory_manager
node_memory_crud -->|"reads and writes"| node_prisma_store
node_memory_tables -->|"reads and writes"| node_prisma_store
node_message_store -->|"reads and writes"| node_prisma_store
node_bot -->|"requests stream"| node_openai_client
node_memory_manager -->|"requests summaries"| node_openai_client
node_openai_client -->|"streams completion"| node_model_service
node_openai_client -->|"forwards chunks"| node_stream
node_stream -->|"supplies response"| node_speaker_base
node_speaker_base -->|"plays speech"| node_xiaomi
node_speaker_base -.->|"requests speech"| node_custom_tts

click node_migpt "https://github.com/idootop/mi-gpt/blob/main/src/index.ts"
click node_bot "https://github.com/idootop/mi-gpt/blob/main/src/services/bot/index.ts"
click node_conversation_manager "https://github.com/idootop/mi-gpt/blob/main/src/services/bot/conversation.ts"
click node_memory_manager "https://github.com/idootop/mi-gpt/blob/main/src/services/bot/memory/index.ts"
click node_short_memory "https://github.com/idootop/mi-gpt/blob/main/src/services/bot/memory/short-term.ts"
click node_long_memory "https://github.com/idootop/mi-gpt/blob/main/src/services/bot/memory/long-term.ts"
click node_speaker_ai "https://github.com/idootop/mi-gpt/blob/main/src/services/speaker/ai.ts"
click node_speaker_loop "https://github.com/idootop/mi-gpt/blob/main/src/services/speaker/speaker.ts"
click node_speaker_base "https://github.com/idootop/mi-gpt/blob/main/src/services/speaker/base.ts"
click node_stream "https://github.com/idootop/mi-gpt/blob/main/src/services/speaker/stream.ts"
click node_openai_client "https://github.com/idootop/mi-gpt/blob/main/src/services/openai.ts"
click node_db_runtime "https://github.com/idootop/mi-gpt/blob/main/src/services/db/index.ts"
click node_prisma_store "https://github.com/idootop/mi-gpt/blob/main/prisma/schema.prisma"
click node_memory_crud "https://github.com/idootop/mi-gpt/blob/main/src/services/db/memory.ts"
click node_memory_tables "https://github.com/idootop/mi-gpt/blob/main/src/services/db/memory-long-term.ts"
click node_message_store "https://github.com/idootop/mi-gpt/blob/main/src/services/db/message.ts"

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_migpt,node_user toneBlue
class node_bot,node_conversation_manager,node_memory_manager,node_short_memory,node_long_memory toneAmber
class node_speaker_ai,node_speaker_loop,node_speaker_base,node_stream toneMint
class node_openai_client,node_db_runtime,node_prisma_store,node_memory_crud,node_memory_tables,node_message_store toneRose
class node_xiaomi,node_model_service,node_custom_tts toneIndigo
```

## Components

### Runtime orchestration

- [MiGPT client](https://github.com/idootop/mi-gpt/blob/main/src/index.ts): `src/index.ts`

### Conversation and memory

- [Bot workflow](https://github.com/idootop/mi-gpt/blob/main/src/services/bot/index.ts): `src/services/bot/index.ts`
- [Conversation manager](https://github.com/idootop/mi-gpt/blob/main/src/services/bot/conversation.ts): `src/services/bot/conversation.ts`
- [Memory manager](https://github.com/idootop/mi-gpt/blob/main/src/services/bot/memory/index.ts): `src/services/bot/memory/index.ts`
- [Short-term memory](https://github.com/idootop/mi-gpt/blob/main/src/services/bot/memory/short-term.ts): `src/services/bot/memory/short-term.ts`
- [Long-term memory](https://github.com/idootop/mi-gpt/blob/main/src/services/bot/memory/long-term.ts): `src/services/bot/memory/long-term.ts`

### Speaker integration

- [AI speaker](https://github.com/idootop/mi-gpt/blob/main/src/services/speaker/ai.ts): `src/services/speaker/ai.ts`
- [Speaker message loop](https://github.com/idootop/mi-gpt/blob/main/src/services/speaker/speaker.ts): `src/services/speaker/speaker.ts`
- [Speaker playback](https://github.com/idootop/mi-gpt/blob/main/src/services/speaker/base.ts): `src/services/speaker/base.ts`
- [Stream response](https://github.com/idootop/mi-gpt/blob/main/src/services/speaker/stream.ts): `src/services/speaker/stream.ts`

### Persistence and AI

- [OpenAI client](https://github.com/idootop/mi-gpt/blob/main/src/services/openai.ts): `src/services/openai.ts`
- [Database runtime](https://github.com/idootop/mi-gpt/blob/main/src/services/db/index.ts): `src/services/db/index.ts`
- [Application database](https://github.com/idootop/mi-gpt/blob/main/prisma/schema.prisma): `prisma/schema.prisma`
- [Memory persistence](https://github.com/idootop/mi-gpt/blob/main/src/services/db/memory.ts): `src/services/db/memory.ts`
- [Memory records](https://github.com/idootop/mi-gpt/blob/main/src/services/db/memory-long-term.ts): `src/services/db/memory-long-term.ts`
- [Message persistence](https://github.com/idootop/mi-gpt/blob/main/src/services/db/message.ts): `src/services/db/message.ts`

### Other components

- **Speaker user**
- **Xiaomi speaker services**
- **LLM service**
- **Custom TTS endpoint**

## Connections

- Speaker user → Xiaomi speaker services: speaks to
- MiGPT client → AI speaker: constructs
- MiGPT client → Bot workflow: constructs
- MiGPT client → Database runtime: initializes
- MiGPT client → Bot workflow: starts
- MiGPT client → Database runtime: runs with
- Bot workflow → Conversation manager: loads context
- Bot workflow → AI speaker: starts speaker
- AI speaker → Speaker message loop: runs
- Speaker message loop → Xiaomi speaker services: polls messages
- AI speaker → Speaker playback: uses playback
- Speaker message loop → Bot workflow: dispatches query
- Bot workflow → Conversation manager: reads history
- Bot workflow → Memory manager: reads memories
- Memory manager → Memory persistence: queries memories
- Memory manager → Short-term memory: generates summaries
- Memory manager → Long-term memory: generates summaries
- Conversation manager → Message persistence: stores messages
- Conversation manager → Memory manager: records messages
- Memory persistence → Application database: reads and writes
- Memory records → Application database: reads and writes
- Message persistence → Application database: reads and writes
- Bot workflow → OpenAI client: requests stream
- Memory manager → OpenAI client: requests summaries
- OpenAI client → LLM service: streams completion
- OpenAI client → Stream response: forwards chunks
- Stream response → Speaker playback: supplies response
- Speaker playback → Xiaomi speaker services: plays speech
- Speaker playback → Custom TTS endpoint: requests speech
