# wang-yifan666/qq_chatbot architecture

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

- Interactive diagram: https://gitdiagram.com/wang-yifan666/qq_chatbot
- Repository: https://github.com/wang-yifan666/qq_chatbot

## Overview

This is a QQ group-chat AI bot. QQ message and notice events enter distinct DIRECT, AMBIENT, and POKE handlers; scheduled tasks provide a separate proactive entry point. Authorized requests converge on message perception and prompt construction, then call an LLM provider and return or proactively send a response. Context, personal memory, relationships, persona and knowledge retrieval, and optional web search support that workflow. The README documents the major stages and boundaries; several service implementations were not sampled, so their internal links are omitted where the documented flow does not establish them.

Main entry points: [`plugins/ai_chat.py`](https://github.com/wang-yifan666/qq_chatbot/blob/main/plugins/ai_chat.py) (Direct Chat), [`services/context_store.py`](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/context_store.py) (Chat Context), [`services/scheduled_tasks.py`](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/scheduled_tasks.py) (Scheduled Tasks), [`services/llm_client.py`](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/llm_client.py) (LLM Fallback)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Events and Access"]
  node_direct["Direct Chat<br/>[ai_chat.py]"]
  node_recorder["Context Recorder"]
  node_ambient["Ambient Chat<br/>[ambient.py]"]
  node_poke["Poke Handler<br/>[poke.py]"]
  node_scheduled["Scheduled Tasks<br/>[scheduled_tasks.py]"]
  node_access["Group Access<br/>[group_access.py]"]
end

subgraph group_conversation["Conversation Pipeline"]
  node_resolver["Message Resolver"]
  node_multimodal["Multimodal Builder"]
  node_file_reader["File Reader<br/>[file_reader.py]"]
  node_prompt["Prompt Builder<br/>[prompt_builder.py]"]
end

subgraph group_personalization["Context and Personalization"]
  node_arbitration["Context Arbitration"]
  node_profile["Interaction Profile"]
  node_intensity["Trigger Intensity"]
  node_relationship["Relationship Service"]
  node_memory_retriever["Personal Memory Retrieval"]
  node_persona["Persona RAG<br/>[persona_rag.py]"]
  node_knowledge["Knowledge RAG<br/>[knowledge_rag.py]"]
end

subgraph group_integrations["Models and Features"]
  node_llm["LLM Fallback<br/>[llm_client.py]"]
  node_web["Web Search<br/>[web_search.py]"]
  node_paint["Paint Command<br/>[paint.py]"]
  node_image_gen["Image Generation<br/>[image_gen.py]"]
  node_embedding["Embedding Backend"]
end

subgraph group_storage["Persistence"]
  node_context_store["Chat Context<br/>[context_store.py]"]
  node_personal_store["Personal Memory Store"]
  node_user_store["User Store<br/>[user_store.py]"]
  node_database[("SQLite Database<br/>[database.py]")]
end

node_qq(("QQ Events"))
node_scheduler(("Cron Scheduler"))

node_qq -->|"triggers"| node_direct
node_qq -->|"triggers"| node_recorder
node_qq -->|"triggers"| node_ambient
node_qq -->|"triggers"| node_poke
node_scheduler -->|"triggers"| node_scheduled
node_direct -->|"checks access"| node_access
node_recorder -->|"checks access"| node_access
node_ambient -->|"checks access"| node_access
node_poke -->|"checks access"| node_access
node_scheduled -->|"checks access"| node_access
node_direct -->|"resolves message"| node_resolver
node_resolver -->|"builds blocks"| node_multimodal
node_resolver -->|"reads files"| node_file_reader
node_direct -->|"builds request"| node_prompt
node_direct -->|"arbitrates context"| node_arbitration
node_direct -->|"derives profile"| node_profile
node_direct -->|"sets intensity"| node_intensity
node_direct -->|"reads and updates"| node_relationship
node_direct -->|"retrieves memory"| node_memory_retriever
node_direct -->|"retrieves style"| node_persona
node_direct -->|"retrieves knowledge"| node_knowledge
node_scheduled -->|"retrieves style"| node_persona
node_prompt -->|"reads context"| node_context_store
node_direct -->|"stores messages"| node_context_store
node_recorder -->|"records messages"| node_context_store
node_ambient -->|"stores reply"| node_context_store
node_poke -->|"stores reply"| node_context_store
node_scheduled -->|"stores reply"| node_context_store
node_direct -->|"uses personal data"| node_personal_store
node_direct -->|"upserts identity"| node_user_store
node_context_store -->|"reads and writes"| node_database
node_user_store -->|"reads and writes"| node_database
node_personal_store -->|"stores memories"| node_database
node_persona -->|"encodes queries"| node_embedding
node_knowledge -->|"encodes queries"| node_embedding
node_direct -->|"requests response"| node_llm
node_ambient -->|"requests decision"| node_llm
node_scheduled -->|"requests response"| node_llm
node_poke -.->|"generates reply"| node_llm
node_llm -->|"runs tool"| node_web
node_paint -->|"generates image"| node_image_gen
node_paint -->|"stores reply"| node_context_store

click node_direct "https://github.com/wang-yifan666/qq_chatbot/blob/main/plugins/ai_chat.py"
click node_recorder "https://github.com/wang-yifan666/qq_chatbot/blob/main/plugins/context_recorder.py"
click node_ambient "https://github.com/wang-yifan666/qq_chatbot/blob/main/plugins/ambient.py"
click node_poke "https://github.com/wang-yifan666/qq_chatbot/blob/main/plugins/poke.py"
click node_scheduled "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/scheduled_tasks.py"
click node_access "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/group_access.py"
click node_resolver "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/perception/message_resolver.py"
click node_multimodal "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/perception/multimodal_builder.py"
click node_file_reader "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/perception/file_reader.py"
click node_prompt "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/prompt_builder.py"
click node_arbitration "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/context_arbitration.py"
click node_profile "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/interaction_profile.py"
click node_intensity "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/trigger_intensity.py"
click node_relationship "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/relationship_service.py"
click node_memory_retriever "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/memory_retriever.py"
click node_persona "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/persona_rag.py"
click node_knowledge "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/knowledge_rag.py"
click node_llm "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/llm_client.py"
click node_web "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/web_search.py"
click node_paint "https://github.com/wang-yifan666/qq_chatbot/blob/main/plugins/paint.py"
click node_image_gen "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/image_gen.py"
click node_context_store "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/context_store.py"
click node_personal_store "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/personal_memory_store.py"
click node_user_store "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/user_store.py"
click node_database "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/database.py"
click node_embedding "https://github.com/wang-yifan666/qq_chatbot/blob/main/services/embedding_backend.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_direct,node_recorder,node_ambient,node_poke,node_scheduled,node_access toneBlue
class node_resolver,node_multimodal,node_file_reader,node_prompt toneAmber
class node_arbitration,node_profile,node_intensity,node_relationship,node_memory_retriever,node_persona,node_knowledge toneMint
class node_llm,node_web,node_paint,node_image_gen,node_embedding,node_scheduler toneRose
class node_context_store,node_personal_store,node_user_store,node_database,node_qq toneIndigo
```

## Components

### Events and Access

- [Direct Chat](https://github.com/wang-yifan666/qq_chatbot/blob/main/plugins/ai_chat.py): `plugins/ai_chat.py`
- [Context Recorder](https://github.com/wang-yifan666/qq_chatbot/blob/main/plugins/context_recorder.py): `plugins/context_recorder.py`
- [Ambient Chat](https://github.com/wang-yifan666/qq_chatbot/blob/main/plugins/ambient.py): `plugins/ambient.py`
- [Poke Handler](https://github.com/wang-yifan666/qq_chatbot/blob/main/plugins/poke.py): `plugins/poke.py`
- [Scheduled Tasks](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/scheduled_tasks.py): `services/scheduled_tasks.py`
- [Group Access](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/group_access.py): `services/group_access.py`

### Conversation Pipeline

- [Message Resolver](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/perception/message_resolver.py): `services/perception/message_resolver.py`
- [Multimodal Builder](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/perception/multimodal_builder.py): `services/perception/multimodal_builder.py`
- [File Reader](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/perception/file_reader.py): `services/perception/file_reader.py`
- [Prompt Builder](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/prompt_builder.py): `services/prompt_builder.py`

### Context and Personalization

- [Context Arbitration](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/context_arbitration.py): `services/context_arbitration.py`
- [Interaction Profile](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/interaction_profile.py): `services/interaction_profile.py`
- [Trigger Intensity](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/trigger_intensity.py): `services/trigger_intensity.py`
- [Relationship Service](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/relationship_service.py): `services/relationship_service.py`
- [Personal Memory Retrieval](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/memory_retriever.py): `services/memory_retriever.py`
- [Persona RAG](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/persona_rag.py): `services/persona_rag.py`
- [Knowledge RAG](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/knowledge_rag.py): `services/knowledge_rag.py`

### Models and Features

- [LLM Fallback](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/llm_client.py): `services/llm_client.py`
- [Web Search](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/web_search.py): `services/web_search.py`
- [Paint Command](https://github.com/wang-yifan666/qq_chatbot/blob/main/plugins/paint.py): `plugins/paint.py`
- [Image Generation](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/image_gen.py): `services/image_gen.py`
- [Embedding Backend](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/embedding_backend.py): `services/embedding_backend.py`

### Persistence

- [Chat Context](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/context_store.py): `services/context_store.py`
- [Personal Memory Store](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/personal_memory_store.py): `services/personal_memory_store.py`
- [User Store](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/user_store.py): `services/user_store.py`
- [SQLite Database](https://github.com/wang-yifan666/qq_chatbot/blob/main/services/database.py): `services/database.py`

### Other components

- **QQ Events**
- **Cron Scheduler**

## Connections

- QQ Events → Direct Chat: triggers
- QQ Events → Context Recorder: triggers
- QQ Events → Ambient Chat: triggers
- QQ Events → Poke Handler: triggers
- Cron Scheduler → Scheduled Tasks: triggers
- Direct Chat → Group Access: checks access (evidence: `plugins/ai_chat.py`)
- Context Recorder → Group Access: checks access (evidence: `plugins/context_recorder.py`)
- Ambient Chat → Group Access: checks access (evidence: `plugins/ambient.py`)
- Poke Handler → Group Access: checks access (evidence: `plugins/poke.py`)
- Scheduled Tasks → Group Access: checks access (evidence: `services/scheduled_tasks.py`)
- Direct Chat → Message Resolver: resolves message (evidence: `plugins/ai_chat.py`)
- Message Resolver → Multimodal Builder: builds blocks (evidence: `services/perception/message_resolver.py`)
- Message Resolver → File Reader: reads files (evidence: `services/perception/message_resolver.py`)
- Direct Chat → Prompt Builder: builds request (evidence: `plugins/ai_chat.py`)
- Direct Chat → Context Arbitration: arbitrates context (evidence: `plugins/ai_chat.py`)
- Direct Chat → Interaction Profile: derives profile (evidence: `plugins/ai_chat.py`)
- Direct Chat → Trigger Intensity: sets intensity (evidence: `plugins/ai_chat.py`)
- Direct Chat → Relationship Service: reads and updates (evidence: `plugins/ai_chat.py`)
- Direct Chat → Personal Memory Retrieval: retrieves memory (evidence: `plugins/ai_chat.py`)
- Direct Chat → Persona RAG: retrieves style (evidence: `plugins/ai_chat.py`)
- Direct Chat → Knowledge RAG: retrieves knowledge (evidence: `plugins/ai_chat.py`)
- Scheduled Tasks → Persona RAG: retrieves style (evidence: `services/scheduled_tasks.py`)
- Prompt Builder → Chat Context: reads context (evidence: `services/prompt_builder.py`)
- Direct Chat → Chat Context: stores messages (evidence: `plugins/ai_chat.py`)
- Context Recorder → Chat Context: records messages (evidence: `plugins/context_recorder.py`)
- Ambient Chat → Chat Context: stores reply
- Poke Handler → Chat Context: stores reply
- Scheduled Tasks → Chat Context: stores reply (evidence: `services/scheduled_tasks.py`)
- Direct Chat → Personal Memory Store: uses personal data
- Direct Chat → User Store: upserts identity (evidence: `plugins/ai_chat.py`)
- Chat Context → SQLite Database: reads and writes (evidence: `services/context_store.py`)
- User Store → SQLite Database: reads and writes (evidence: `services/user_store.py`)
- Personal Memory Store → SQLite Database: stores memories
- Persona RAG → Embedding Backend: encodes queries (evidence: `services/persona_rag.py`)
- Knowledge RAG → Embedding Backend: encodes queries (evidence: `services/knowledge_rag.py`)
- Direct Chat → LLM Fallback: requests response (evidence: `plugins/ai_chat.py`)
- Ambient Chat → LLM Fallback: requests decision
- Scheduled Tasks → LLM Fallback: requests response (evidence: `services/scheduled_tasks.py`)
- Poke Handler → LLM Fallback: generates reply
- LLM Fallback → Web Search: runs tool (evidence: `services/llm_client.py`)
- Paint Command → Image Generation: generates image (evidence: `plugins/paint.py`)
- Paint Command → Chat Context: stores reply (evidence: `plugins/paint.py`)
