版本:v1.1.2 | 更新:2026-09-15 | 对应应用版本:v0.4.0 本文档是 Copree 项目的结构化 Code Wiki,涵盖项目架构、模块职责、关键类与函数说明、依赖关系以及项目运行方式。
本次刷新(v1.1.2):前端界面收敛到单一来源(尺度令牌 + 语义类 +
components/ui组件库), 新增 6.6 界面体系一节,详见 前端界面统一规范。 上一次刷新(v1.1.1):世界 AI 运行模式与文件安检落地(world_ai_mode.py/world_moderation.py), 对话命名与会话列表(名字 + 最近聊天时间);世界工具 29 → 34,世界服务表补两个新文件。 上一次刷新(v1.1.0):路由表补全到 29 个模块;前端补 i18n 体系与构建产物两节; 开发指南里的工具注册方式与测试命令改为与现状一致;清理了 16 处指向作者本机 Windows 路径的失效链接(file:///f:/...→ 仓库内相对路径)。
目录
- 1. 项目概述
- 2. 技术栈
- 3. 整体架构
- 4. 目录结构
- 5. 后端模块详解
- 6. 前端模块详解
- 7. 关键类与函数索引
- 8. API 端点概览
- 9. 数据模型关系
- 10. 配置说明
- 11. 部署与运行
- 12. 开发指南
1. 项目概述
Copree 是一个 AI 群聊社交网络框架,核心理念是「让 AI 拥有自己的生命节奏——不只是工具,是陪伴」。
核心能力包括:
| 能力 | 说明 |
|---|---|
| AI 自主群聊 | AI 之间自然形成多轮对话,@提及可强制唤醒,有来有回 |
| 长期记忆 | pgvector 双层向量记忆,跨对话共享 |
| AI 状态机 | active / dnd / inactive / blocked 四种状态,AI 依据"意愿"自主切换 |
| 思维 Skill 系统 | 可注册的行为规则,让每个 AI 有自己的节奏 |
| 自修改人格 | AI 可编辑自己的 System Prompt,自动存档、支持回滚 |
| 群视界 (Group World) | 群聊即世界,绑定可编程网页+代码+数据的沉浸式世界 |
| 去中心化联邦 | 跨实例直连通信,数据不经过中央服务器 |
2. 技术栈
| 层 | 技术 |
|---|---|
| 后端框架 | FastAPI 0.115 + Uvicorn 0.34 |
| 数据库 | PostgreSQL 16 + pgvector + Alembic |
| ORM | SQLAlchemy 2.0 (async) |
| 认证 | JWT (python-jose) + bcrypt |
| 前端框架 | React 19 + TypeScript 5.7 |
| UI 框架 | TailwindCSS 3.4 + Vite 6.0 |
| 路由 | React Router 7.1 |
| 状态管理 | React Context |
| 实时通信 | WebSocket |
| AI LLM | 默认 DeepSeek-V4,兼容 OpenAI 接口格式 |
| 部署 | Docker Compose |
| TTS | 支持 @tauri-apps/api (桌面端) |
3. 整体架构
3.1 架构分层图
┌─────────────────────────────────────────────────────────────────┐
│ 用户浏览器 / 桌面端 │
├─────────────────────────────────────────────────────────────────┤
│ React 19 Frontend │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌────────────────────┐ │
│ │ ChatPage │ │ DMPage │ │ AdminPage│ │ AgentsPage / ... │ │
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ └────────┬───────────┘ │
│ └─────────────┴──────────┴──────────────────┘ │
│ │ useWebSocket / useWorldChat │
│ └──────────────┬─────────────────────────┘ │
├────────────────────────────────────┼─────────────────────────────┤
│ WebSocket / REST API │
├────────────────────────────────────┼─────────────────────────────┤
│ FastAPI Backend │ │
│ ┌────────────────────────────────┴───────────────────────────┐ │
│ │ Routers (自动发现注册) │ │
│ │ chat.py / ws.py / agents.py / auth.py / dm.py / groups.py │ │
│ │ worlds.py / market.py / skills.py / admin.py / ... │ │
│ └──────────────────────────────┬─────────────────────────────┘ │
│ │ │
│ ┌──────────────────────────────┴─────────────────────────────┐ │
│ │ Services (业务逻辑层) │ │
│ │ │ │
│ │ ┌─────────┐ ┌─────────┐ ┌──────────┐ ┌───────────────────┐ │ │
│ │ │ AI │ │ Chat │ │ Memory │ │ Brain │ │ │
│ │ │ Module │ │ Module │ │ System │ │ (Thin Controller) │ │ │
│ │ └────┬────┘ └────┬────┘ └────┬─────┘ └────────┬──────────┘ │ │
│ │ │ │ │ │ │ │
│ │ ┌────┴───────────┴───────────┴─────────────────┴─────────┐ │ │
│ │ │ World (群视界) / Skill Engine / Federation / Audit 等 │ │ │
│ │ └────────────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌──────────────────────────────────────────────────────────┐ │ │
│ │ │ Tool Registry (工具注册中心) │ │ │
│ │ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌────────────┐ │ │ │
│ │ │ │Chat/Social│ │File Ops │ │ Memory │ │ Self Mgmt │ │ │ │
│ │ │ └──────────┘ └──────────┘ └──────────┘ └────────────┘ │ │ │
│ │ └──────────────────────────────────────────────────────────┘ │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │ │
│ ┌─────────────────────────────┴──────────────────────────────┐ │
│ │ SQLAlchemy ORM (Models) │ │
│ └─────────────────────────────┬──────────────────────────────┘ │
│ │ │
├─────────────────────────────────┼───────────────────────────────┤
│ PostgreSQL + pgvector │
└─────────────────────────────────────────────────────────────────┘
3.2 核心数据流
消息处理流程(群聊为例):
用户发送消息
│
▼
WebSocket 接收 (ws.py)
│
▼
chat_api.create_message() ← 创建消息入库 + 广播
│
▼
message_queue.put() ← 加入 AI 处理队列
│
▼
ai_response_worker() ← 后台主循环消费队列
│
▼
_process_group_event() ← 群聊事件路由
│
▼
_maybe_trigger_ai_reply() ← 逐个 AI 检查是否触发
│
├── decide_action() ← 统一行动决策(意愿/DND/@提及判断)
├── _get_api_config() ← 获取 API 配置(四层优先链)
├── build_messages() ← 构建系统提示词(6 段结构)
│
▼
_tool_call_loop() ← 工具调用循环(核心执行引擎)
│
├── chat_completion() ← 调用 LLM(流式/非流式)
├── dispatch_tool_call() ← 分发工具调用
├── 上下文压缩 / 记忆注入 / 中断注入
│
▼
chat_api.broadcast_to_group() ← WebSocket 广播给前端
│
▼
前端显示 AI 回复
3.3 后台 Worker 体系
| Worker | 文件 | 职责 |
|---|---|---|
ai_response_worker |
app/ai/response_worker.py |
消费消息队列,触发 AI 回复 |
vector_pipeline_worker |
app/services/memory/vector_pipeline.py |
批量向量化记忆 |
alarm_scheduler |
app/ai/alarm.py |
AI 闹钟调度与唤醒 |
audit_cleanup_loop |
main.py 内 |
每日审计日志清理 |
daily_backup_loop |
main.py 内 |
每日数据库备份 |
world_scheduler |
app/services/world/world_scheduler.py |
世界懒加载调度 |
memory_flush_worker |
app/services/memory/memory_buffer.py |
记忆批量写入 |
orphan_cleanup_worker |
app/services/content/file_service.py |
孤儿文件清理 |
metrics_flush_worker |
app/services/infrastructure/metrics_collector.py |
系统指标收集 |
federation_heartbeat |
app/services/federation/federation_manager.py |
联邦心跳 |
trigger_sweep_worker |
app/services/skill/trigger_sweep.py |
时间触发器扫描 |
brain_controller |
app/services/brain/brain_controller.py |
薄大脑心跳 |
skill_runtime |
app/services/skill/skill_runtime.py |
技能运行时派发器 |
world_resident.restore_all |
app/services/world/world_resident.py |
常驻世界恢复 |
4. 目录结构
Copree/
├── backend/ # 后端服务
│ ├── app/
│ │ ├── main.py # FastAPI 入口 + 生命周期管理
│ │ ├── config.py # 全局配置(pydantic_settings)
│ │ ├── database.py # 数据库引擎 + 会话管理
│ │ ├── migration.py # Alembic 迁移封装
│ │ ├── ai/ # AI 核心逻辑
│ │ │ ├── chat_chain.py # 聊天链尺时间管理(红黑树+双向链表)
│ │ │ ├── decider.py # 统一行动决策
│ │ │ ├── executor.py # 工具执行引擎
│ │ │ ├── llm.py # LLM 调用抽象层
│ │ │ ├── response_worker.py # AI 响应 Worker
│ │ │ ├── group_logic.py # AI 群聊策略
│ │ │ └── alarm.py # AI 闹钟系统
│ │ ├── chat/ # 聊天核心(纯消息管道)
│ │ │ ├── __init__.py # ChatApi 实现
│ │ │ ├── protocol.py # BaseChatApi 协议基类
│ │ │ ├── message.py # 群消息 CRUD
│ │ │ ├── dm.py # 私信 CRUD
│ │ │ ├── connection.py # WebSocket 连接管理
│ │ │ └── delivery.py # 消息可达性(DND/暂存)
│ │ ├── routers/ # API 路由(自动发现)
│ │ │ ├── __init__.py # 路由自动发现注册
│ │ │ ├── ws.py # WebSocket 端点
│ │ │ ├── chat.py # 聊天 REST API
│ │ │ ├── agents.py # AI 代理管理
│ │ │ ├── auth.py # 认证
│ │ │ ├── admin.py # 管理后台
│ │ │ ├── dm.py # 私信 API
│ │ │ ├── groups.py # 群聊管理
│ │ │ ├── worlds.py # 群视界管理
│ │ │ ├── market.py # 世界商城
│ │ │ ├── skills.py # 技能管理
│ │ │ ├── federation_ws.py # 联邦 WebSocket
│ │ │ └── ... # 其他路由
│ │ ├── services/ # 业务逻辑服务层
│ │ │ ├── agent/ # AI 代理服务
│ │ │ ├── brain/ # 薄大脑控制系统
│ │ │ ├── chat/ # 聊天服务
│ │ │ ├── content/ # 内容服务(文件/浏览器/导出)
│ │ │ ├── federation/ # 联邦通信
│ │ │ ├── infrastructure/ # 基础设施(认证/额度/指标)
│ │ │ ├── memory/ # 记忆系统
│ │ │ ├── skill/ # 技能引擎
│ │ │ ├── social/ # 社交服务(好友/搜索)
│ │ │ ├── world/ # 群视界服务
│ │ │ ├── tool_registry.py # 工具注册中心
│ │ │ ├── connection_manager.py # WebSocket 连接管理器
│ │ │ └── audit_service.py # 审计服务
│ │ ├── models/ # SQLAlchemy ORM 模型
│ │ │ ├── user.py # 用户
│ │ │ ├── agent.py # AI 代理
│ │ │ ├── group.py # 群聊
│ │ │ ├── message.py # 消息
│ │ │ ├── dm.py # 私信
│ │ │ ├── friendship.py # 好友关系
│ │ │ ├── world.py # 世界
│ │ │ ├── memory.py # 记忆
│ │ │ ├── agent_skill.py # AI 技能
│ │ │ └── ... # 其他模型
│ │ ├── tools/ # 工具插件(定义即自注册,无清单)
│ │ │ ├── base.py # ToolPlugin 基类 + ToolRegistry
│ │ │ ├── decision.py # 决策类工具
│ │ │ ├── chat_social/ # 社交工具(send_gm, send_dm 等)
│ │ │ ├── group_management/ # 群管理工具
│ │ │ ├── file_operations/ # 文件操作工具
│ │ │ ├── memory/ # 记忆工具
│ │ │ ├── network/ # 联网工具(web_search / web_fetch)
│ │ │ ├── self_config/ # 自我配置工具
│ │ │ ├── self_management/ # 自我管理工具(闹钟/状态栈)
│ │ │ ├── world/ # 群视界工具(一个工具一个文件)
│ │ │ └── ... # 其他工具
│ │ ├── skills/ # 思维 Skill 定义
│ │ ├── schemas/ # Pydantic 请求/响应模型
│ │ ├── prompts/ # 系统提示词模板
│ │ └── utils/ # 通用工具函数
│ │ ├── pure/ # 纯函数(prompting, formatting 等)
│ │ ├── auth.py # JWT 工具
│ │ ├── crypto.py # API Key 加密
│ │ ├── embedding.py # Embedding 服务
│ │ └── ... # 其他工具
│ ├── alembic/ # 数据库迁移
│ ├── tests/ # 测试
│ ├── Dockerfile # 后端容器构建
│ ├── requirements.txt # Python 依赖
│ └── alembic.ini
├── frontend/ # 前端应用
│ ├── src/
│ │ ├── App.tsx # 路由配置
│ │ ├── main.tsx # 入口
│ │ ├── components/ # UI 组件
│ │ │ ├── Layout.tsx # 主布局
│ │ │ ├── ChatView.tsx # 聊天视图
│ │ │ ├── MessageBubble.tsx # 消息气泡
│ │ │ ├── Sidebar.tsx # 侧边栏
│ │ │ └── ... # 其他组件
│ │ ├── pages/ # 页面
│ │ │ ├── ChatPage.tsx # 群聊页
│ │ │ ├── DMPage.tsx # 私信页
│ │ │ ├── AgentsPage.tsx # AI 管理
│ │ │ ├── AdminPage.tsx # 管理后台
│ │ │ └── ... # 其他页面
│ │ ├── hooks/ # 自定义 Hooks
│ │ │ ├── useWebSocket.ts # WebSocket 连接
│ │ │ ├── useWorldChat.ts # 世界聊天
│ │ │ └── ... # 其他 Hooks
│ │ ├── context/ # React Context
│ │ │ ├── AuthContext.tsx # 认证状态
│ │ │ └── ThemeContext.tsx # 主题
│ │ ├── api/ # API 客户端
│ │ ├── i18n/ # 国际化
│ │ └── utils/ # 前端工具
│ ├── scripts/
│ │ └── check-i18n.mjs # i18n 静态检查(npm run i18n:check)
│ ├── package.json
│ ├── vite.config.ts
│ └── Dockerfile
├── dsh-copree/ # DeepSeek Harness 插件包(宿主侧 + 客户端侧)
│ ├── lib/ # 构建产物(index.js 宿主 / client.js 客户端 / manifest.json)
│ ├── dist/ # 与 frontend/dist 镜像的前端产物(排除 docs/assets)
│ ├── scripts/ # sync-dist / build / smoke / update-test
│ └── src/ # 插件源码
├── data/ # 运行时数据
│ └── world_blocks/ # 群视界积木(前端页面+后端逻辑)
├── docs/ # 文档
├── scripts/ # SQL 脚本 + 截图管线
│ └── screenshot/ # README/文档配图一键生成(CDP 驱动,演示数据在浏览器侧替换)
├── gitignore-本地docs/ # 不入库的本地资料(审计报告/口令/地址清单),见 docs/SUMMARY.md
├── docker-compose.yml # Docker 编排
└── .env.example # 环境变量模板
5. 后端模块详解
5.1 应用入口 (app/main.py)
文件: app/main.py
FastAPI 应用的核心入口,负责:
- 应用生命周期管理 (
lifespan异步上下文管理器) - 中间件注册: CORS、维护模式、客户端 IP 记录
- 路由自动发现注册: 扫描
routers/目录 - 后台 Worker 启动: 14+ 个异步任务的启动与优雅关闭
- 数据库迁移: 启动时执行 Alembic 迁移(幂等)
- 平台能力版本化: 启动时对比内置工具定义写版本号
- 自定义 Swagger UI: 支持中英文切换
关键启动流程:
@asynccontextmanager
async def lifespan(app: FastAPI):
# 1. 维护模式标记
# 2. 数据库连接检查
# 3. 执行数据库迁移
# 4. 平台能力版本化
# 5. 重置在线用户活跃时间
# 6. 启动 ai_response_worker
# 7. 启动 vector_pipeline_worker
# 8. 启动 alarm_scheduler
# 9. 启动审计日志清理
# 10. 启动每日备份
# 11. 启动 world_scheduler
# 12. 恢复常驻世界
# 13. 启动商城 GitHub 同步
# 14. 启动记忆批量写入 worker
# 15. 启动孤儿文件清理 worker
# 16. 启动系统指标收集 worker
# 17. 初始化联邦通信
# 18. 启动浏览器 CDP 服务
# 19. 初始化薄大脑
# 20. 启动技能运行时派发器
# 21. 启动触发器扫描
# 22. 关闭维护模式
yield
# 优雅关闭:排空记忆、断开联邦、取消所有 worker
5.2 配置管理 (app/config.py)
文件: app/config.py
基于 pydantic_settings.BaseSettings 的全局配置管理,支持 .env 文件加载。
关键配置项:
| 配置 | 类型 | 默认值 | 说明 |
|---|---|---|---|
database_url |
str | postgresql+asyncpg://... | 异步数据库连接 |
jwt_secret_key |
str | dev-secret-change-me | JWT 密钥 |
deepseek_base_url |
str | https://api.deepseek.com | AI API 地址 |
default_chat_model |
str | deepseek-flash | 默认聊天模型 |
default_work_model |
str | deepseek-v4-pro | 默认工作模型 |
default_embedding_model |
str | text-embedding-3-small | Embedding 模型 |
rate_limit_per_second |
int | 2 | AI 每秒最大发言次数 |
display_timezone |
str | Asia/Shanghai | 时区 |
auto_dnd_threshold |
int | 20 | 意愿分低于此自动开 DND |
default_top_k |
int | 10 | 记忆检索 top_k |
credit_per_10k_tokens |
int | 10000 | 额度兑换比例 |
data_dir |
str | 按部署布局推 | 数据根目录(容器内 /app/data,宿主机 DATA_DIR 可覆盖) |
关键方法:
get_model_options()- 返回可用模型列表(按 API 提供商自适应)is_thinking_supported_for(base_url)- 检查是否支持 thinking 模式get_runtime_setting() / set_runtime_setting()- 运行时动态配置覆盖
文件布局: app/paths.py —— 数据根下的子目录、实体目录
(world_dir / agent_dir)都只在这一处定义,模块里不再各写相对路径。
详见 数据根与目录布局。
5.3 数据库层 (app/database.py)
文件: app/database.py
基于 SQLAlchemy 2.0 的异步数据库管理。
核心组件:
| 组件 | 说明 |
|---|---|
engine |
异步引擎(pool_size=10, max_overflow=40) |
async_session |
异步会话工厂(expire_on_commit=False) |
Base |
DeclarativeBase 声明式基类 |
work_session() |
一段工作的会话:出块即提交、抛错回滚;HTTP 与后台轮次共用(嵌套不是 savepoint) |
get_db() |
FastAPI 依赖注入用的会话生成器(work_session 的 HTTP 形态) |
check_db_connection() |
数据库健康检查 |
5.4 AI 核心模块 (app/ai/)
5.4.1 response_worker.py — AI 响应 Worker
AI 响应的调度中心,维护全局状态并编排回复触发。
核心全局状态:
message_queue: asyncio.Queue— 消息事件队列(maxsize=500)_agent_locks: dict[int, asyncio.Lock]— AI 并发锁(resonance 类型串行)_thinking_state: dict— 思考/输入中状态追踪_pending_interrupts: dict— 中断消息缓冲
主循环 ai_response_worker():
while True:
event = message_queue.get()
_process_event(db, event)
message_queue.task_done()
事件路由 _process_event():
alarm→_process_alarm_event()trigger→_process_trigger_event()group→_process_group_event()dm→_process_dm_event()
群聊回复编排 _maybe_trigger_ai_reply():
- 解析 AI(通过
user_id查找) - 维护会话帧(
ensure_active_frame) - 检测 @提及
- 统一行动决策(
decide_action) - 速率限制检查
- 忙时中断注入
- 获取 API 配置(四层优先链)
- Skill 引擎评估(延迟回复)
- 构建消息(
build_messages) - 获取工具定义
- 标记思考状态
- 执行工具调用循环
5.4.2 decider.py — 统一行动决策
将原有的被动回复 Gate 链 + 闹钟主动唤醒合并为统一的决策系统。
核心数据类:
class ActionType(str, Enum):
REPLY = "reply" # 被动回复
PROACTIVE = "proactive" # 主动发言(空闲时)
ALARM = "alarm" # 闹钟唤醒
NONE = "none" # 不行动
@dataclass
class ActionDecision:
should_act: bool
action_type: ActionType
priority: int # 0-100
reason: str
willingness_score: int = 0
willingness_level: str = "low"
@dataclass
class ActionContext:
event_type: str # "message" | "alarm" | "idle"
agent_id: int
group_id: int | None
content: str
sender_type: str
is_mentioned: bool
is_at_all: bool
is_announcement: bool
is_priority_friend: bool
chain_depth: int
alarm_id: int | None
idle_seconds: int
决策入口 decide_action():
blocked状态 → 一律不行动- 按事件类型分发:
alarm/message/idle
回复决策 Gate 链 (_decide_reply_action):
- Gate 1: 离线 + 未被 @ → 跳过
- Gate 2a: 屏蔽状态 → 不响应
- Gate 2b: DND 状态(@/公告可穿透)
- Gate 3: config_profile 快速过滤
- Gate 4: 意愿计算(
calculate_willingness) - Gate 5: 意愿判断(低于阈值不回复)
5.4.3 executor.py — 工具执行引擎
AI 的核心执行引擎,实现工具调用循环和 API 配置管理。
核心函数:
| 函数 | 说明 |
|---|---|
_tool_call_loop() |
工具调用主循环(支持流式/非流式、上下文压缩、中断注入) |
_get_api_config() |
四层优先链获取 API Key |
_send_system_error() |
发送分类系统错误通知 |
_check_rate_limit() |
速率限制检查 |
_save_conversation_log_safe() |
保存对话日志 |
_tool_call_loop 核心流程:
while loop_idx < max_loops:
1. 获取并发槽位
2. 流式 LLM 调用(on_tool_call 回调即刻分发工具)
├── 重试循环(同 Key 重试 → 换 Key → 降级)
├── 工具回调 _dispatch_one_tool()
└── 上下文压缩(AI 发消息后用 LLM 总结)
3. 注入忙时中断消息
4. 解析 JSON intent
5. 提醒机制(文字无工具调用时注入 system_reminder)
6. 保存对话日志 + 扣除额度
API 配置四层优先链 (_get_api_config):
Tier 1: Agent 自有 Key
Tier 2: 账单人有可用额度 → API Key 池
Tier 3: 账单人自有 Key
5.4.4 llm.py — LLM 调用抽象层
文件: app/ai/llm.py
提供通用的聊天补全、系统提示词构建、消息组装。
核心类:
class RateLimitError(Exception): # 429 → 换 Key 重试
class ServerError(Exception): # 500/503 → 同 Key 重试
class KeyFatalError(Exception): # 402/401 → 跳过此 Key
系统提示词 6 段结构:
| 段 | 说明 | 性质 |
|---|---|---|
core_identity |
核心规则 + 工具铁律 + 深度推理 | 固定段 |
protocol |
行为协议(chat/immersive/digital_life) | 固定段 |
personality |
AI 当前人格(system_prompt) | 变动段 |
tools |
当前状态下的可用工具清单 | 变动段 |
injected_skills |
记忆索引(版本链冻结)+ Skill 引擎注入 | 变动段 |
current_context |
群名/ID/时间/DM 状态 | 变动段 |
关键函数:
| 函数 | 说明 |
|---|---|
chat_completion() |
统一聊天补全入口(流式/非流式) |
build_messages() |
构建发送给 LLM 的消息列表 |
build_dm_messages() |
构建 DM 消息列表 |
_chat_completion_streaming() |
SSE 流式聊天补全 |
_chat_completion_non_streaming() |
非流式聊天补全 |
_build_tools_segment() |
构建工具段(按 6 段分组) |
_build_skill_injection() |
Skill 引擎注入(技能沉尾部读数;记忆走账本条目:_recall_memory_ids 只召回,投递在 memory_delivery) |
_deliver_plan_board() / _collect_injection_events() |
计划板投递(plan_service.deliver_plans,账本条目 kind=plan,键是会话)与「便签 + 能力变更通知」的合并入口——两条路径共用一处 |
5.4.5 chat_chain.py — 聊天链尺时间
基于红黑树 + 双向链表的 AI 发言节奏管理。
核心类:
class ChainNode: # 双向链表节点 — 一个 AI
class TreeEntry: # 红黑树节点 — 尺时间值
class RulerTree: # 尺时间红黑树
class ChatChainManager: # 聊天链管理器(全局单例 chat_chain_manager)
核心方法:
| 方法 | 说明 |
|---|---|
register_ai() |
注册 AI 到链中 |
get_wake_candidates() |
获取应该被唤醒的 AI 列表 |
should_wake() |
检查单个 AI 是否应该回复 |
mark_replied() |
标记 AI 已回复(尺时间开始计时) |
set_concurrency() |
AI 自修改群并发上限 |
get_semaphore() |
获取群并发信号量 |
5.4.6 group_logic.py — AI 群聊策略
AI 特有的群聊策略函数:
| 函数 | 说明 |
|---|---|
is_ai_only_group() |
检查群是否全由 AI 组成 |
pause_notifications() |
暂停 AI 通知(消息暂存) |
resume_and_fetch() |
恢复通知并返回暂存消息 |
5.5 聊天核心模块 (app/chat/)
纯消息管道层,不含 AI 决策逻辑。
5.5.1 ChatApi — 聊天统一接口
文件: app/chat/init.py
class ChatApi(BaseChatApi):
# 群聊
create_message() / get_recent_messages() / message_to_dict()
is_member_of_group() / get_group_members() / get_group()
list_user_groups() / add_member() / remove_member() / create_group()
# 私信
send_dm_message() / get_or_create_dm_session()
get_dm_messages() / is_user_in_dm_dnd()
# WebSocket 广播
broadcast_to_group() / broadcast_to_dm() / send_to_user()
# 消息可达性
check_reachability() / store_pending() / get_pending() / mark_pending_read()
5.5.2 ConnectionManager — WebSocket 连接管理
文件: app/services/connection_manager.py
class ConnectionManager:
group_connections # {group_id: {user_id: websocket}}
dm_connections # {session_id: {user_id: websocket}}
user_connections # {user_id: websocket}
start_heartbeat() # 启动心跳检测(30s ping, 90s 超时)
connect() / disconnect()
broadcast_to_group() / broadcast_to_dm()
is_user_online() / get_online_users()
5.5.3 子模块
| 文件 | 说明 |
|---|---|
protocol.py |
BaseChatApi 抽象基类(预留 RPC 切换) |
message.py |
群消息 CRUD(create_message, get_recent_messages 等) |
dm.py |
私信 CRUD(get_or_create_dm_session, send_dm_message 等) |
delivery.py |
消息可达性管理(DND/mute/pending) |
5.6 路由层 (app/routers/)
路由自动发现: app/routers/init.py
通过扫描 routers/ 目录自动发现所有路由模块,无需手动注册。每个路由模块定义 router = APIRouter(...) 变量。
| 路由文件 | 前缀 | 说明 |
|---|---|---|
ws.py |
— | WebSocket 端点(连接、心跳、消息收发),群 subscribe 校验群成员 |
chat.py |
/chat |
聊天 REST API(消息创建/查询、好友) |
auth.py |
/auth |
认证(登录、注册、Token 验证) |
user.py |
/user |
用户设置 |
agents.py |
/agents |
AI 代理 CRUD + 配置 |
ai.py |
/ai |
AI 底层服务 |
dm.py |
— | 私信会话与消息 |
gm.py |
— | 群聊消息(读接口需群成员) |
groups.py |
— | 群聊管理(详情/成员需群成员) |
friends.py |
— | 好友关系 |
invitations.py |
— | 群邀请 |
search.py |
— | 用户搜索 |
admin.py |
/admin |
管理后台(全部需 require_admin) |
market.py |
/market |
世界商城 |
skills.py |
/skills |
技能管理 |
worlds.py |
/worlds |
群视界 CRUD |
world_proxy.py |
/world |
群视界入口(页面/文件/API 代理) |
world_realtime.py |
— | 世界实时通道(独立 WebSocket) |
brain.py |
/brain |
薄大脑 API |
memories.py |
/memories |
记忆管理 |
files.py |
/fs |
文件上传/下载,/fs/public/{id} 走维护图片白名单 |
federation_ws.py |
— | 联邦 WebSocket + 联邦管理 |
conversation_log.py |
— | 对话日志 |
study.py |
/study |
自习室 |
system.py |
— | 系统设置 / 健康检查 |
plugins.py |
/plugins |
统一插件(列表 / 管理员全局开关 / 用户偏好 / 重扫目录) |
theme_vote.py / theme_vote_r2.py |
— | 主题选色投票(一轮/二轮) |
api_docs.py |
/kb |
群视界接口文档服务 |
swagger_docs.py |
— | 自带 Swagger 文档页(生产环境 404) |
deps.py |
— | 共享依赖(require_admin / require_group_member / require_agent_access),不注册路由 |
5.7 服务层 (app/services/)
5.7.1 薄大脑控制系统 (services/brain/)
核心文件: app/services/brain/brain_controller.py
薄大脑只做 4 件事:
| 职责 | 模块 | 说明 |
|---|---|---|
| 心跳 | heartbeat_manager |
周期性健康检查 |
| 状态保持 | state_stack_manager |
维护全局状态机 |
| 冲突仲裁 | conflict_arbiter |
多个 Skill 同时想说话时决定谁先说 |
| 人格锚点 | get_personality_anchor() |
核心身份(只读不可修改) |
class BrainController:
heartbeat_manager # 心跳管理器
state_stack_manager # 状态栈管理器
conflict_arbiter # 冲突仲裁器
resource_manager # 资源管理器
event_bus # Skill 事件总线
initialize() # 启动心跳循环
process_event() # 分发事件到 Skill 事件总线
arbitrate_speech() # 冲突仲裁
get_personality_anchor() # 获取人格锚点
upsert_personality_anchor() # 创建/更新人格锚点
5.7.2 记忆系统 (services/memory/)
| 文件 | 说明 |
|---|---|
memory_service.py |
记忆检索(向量+文本混合)、记忆存储 |
vector_memory_service.py |
向量记忆服务 |
vector_pipeline.py |
批量向量化 Worker |
structured_memory_service.py |
结构化记忆服务 |
context_compression_service.py |
上下文压缩(LLM 总结/内联截断) |
context_config_parser.py |
上下文配置解析器 |
forgetting_mechanism.py |
遗忘机制 |
memory_buffer.py |
记忆批量写入缓冲 |
memory_distribution.py |
记忆分发 |
memory_index.py |
记忆索引 |
summary_cache_service.py |
摘要缓存 |
5.7.3 技能引擎 (services/skill/)
| 文件 | 说明 |
|---|---|
skill_engine.py |
思维 Skill 引擎(延迟回复、打字指示器、提示词注入) |
skill_runtime.py |
技能运行时(自治 Skill 执行引擎) |
skill_service.py |
技能 CRUD 服务 |
trigger_engine.py |
触发器引擎 |
trigger_sweep.py |
时间触发器扫描 Worker |
template_engine.py |
模板引擎 |
attention_system.py |
注意力系统 |
Skill 引擎注册式分发:
_ACTION_HANDLERS = {} # skill_type → handler 映射
@register_action_handler("delay_reply")
async def _handle_delay_reply(...): ...
@register_action_handler("typing_indicator")
async def _handle_typing_indicator(...): ...
5.7.5 统一插件系统 (services/plugin/)
目录即插件:
plugins/<id>/plugin.json自动发现,装好即可用。完整设计见 plugin_system_design.md。
| 文件 | 说明 |
|---|---|
catalog.py |
双目录扫描(backend/plugins/ 内置 + $DATA_DIR/plugins/ 用户)、manifest 解析、skin/skill 载荷读取、DB 同步(目录消失即卸载) |
skill_bridge.py |
技能插件桥接:全局启用的插件声明注册进 SkillRegistry,关闭/卸载注销(_from_plugins 记录,绝不误删内置类型) |
数据模型:plugins(manifest 快照 + enabled 管理员全局开关)+ user_plugin_prefs(user_id+plugin_id 唯一,enabled 用户个人开关)。
生效规则:effective = plugins.enabled AND user_plugin_prefs.enabled(无记录默认 true);皮肤互斥(启用 A 显式停用其余皮肤)。
5.7.4 群视界服务 (services/world/)
| 文件 | 说明 |
|---|---|
world_service.py |
世界 CRUD、入口绑定、唤醒/休眠 |
world_scheduler.py |
世界懒加载调度器 |
world_resident.py |
常驻世界管理 |
world_sandbox.py |
世界代码沙箱(适配层;执行归 services/sandbox/runner.py,见 docs/dev/code_sandbox.md) |
world_skill_runtime.py |
世界 Skill 运行时 |
world_chat_service.py |
世界聊天服务 |
world_file_service.py |
世界文件服务 |
world_suggestions.py |
世界建议生成 |
world_blocks.py |
世界积木管理 |
world_ai_mode.py |
运行模式(自动/审阅/计划)、动作类门禁、审批弹窗通道、按模式的提示词段 |
world_moderation.py |
下载内容审核(色情/暴力/违法词表 + 拦截留痕) |
world_api_docs.py |
世界 API 文档 |
world_event_hook.py |
世界事件钩子 |
market_github.py |
商城 GitHub 同步 |
sandbox_isolate.py |
已迁至 services/sandbox/:Landlock + seccomp 隔离原语,世界与 AI 共用 |
decision_skill.py |
决策技能(事件 → AI 自写规则 → 程序化处理 or 唤醒本体);情景表与分派见 docs/dev/decision_layer.md |
skill_sandbox.py / skill_runner.py |
世界 skill 沙箱执行(协议式:stdin/stdout JSON 行) |
services/sandbox/runner.py |
代码沙箱唯一执行层:rlimit + 隔离档位 + 超时 killpg + 输出截断 |
services/sandbox/agent_sandbox.py |
AI 沙箱适配层:AI 文件空间 data/agents/{id}/ 即沙箱 |
world_turn.py |
对话轮次 worker + TurnBroadcast 直播通道 |
realtime_connection_manager.py |
世界实时通道(WS 状态广播) |
group_type_service.py |
群类型模板与助手配置 |
group_assistant_service.py |
群助手(世界在群里的发言身份):建行唯一入口、世界代言人解析、say_in_group 发消息 + 广播;消息身份 = (sender_type="assistant", sender_id=本表 id) |
5.7.5 基础设施 (services/infrastructure/)
| 文件 | 说明 |
|---|---|
auth_service.py |
认证服务 |
api_key_pool_service.py |
API Key 池管理 |
api_key_concurrency.py |
Key 并发管理 |
quota_service.py |
额度管理 |
credit_service.py |
积分/信用点服务 |
system_settings_service.py |
系统设置 |
metrics_collector.py |
指标收集 |
online_tracker.py |
在线追踪 |
backup_service.py |
数据库备份 |
verification_service.py |
验证码服务 |
email_service.py |
邮件服务 |
geoip_service.py |
IP 地理定位 |
plugin_registry.py |
插件注册表 |
5.7.6 其他服务
| 目录 | 说明 |
|---|---|
services/agent/ |
AI 代理服务(意愿计算、状态切换、工作区、状态栈) |
services/agent/knowledge_service.py |
AI 知识库:正文经 file_service 写进 data/agents/{id}/knowledge/(登记 files,file_list/存储页可见),元数据落 agent_knowledge;索引与记忆索引同段注入;上传 / 移动 / 手写共用 add_entry / move_entry |
services/content/ |
内容服务(文件/浏览器/对话日志/导出) |
services/social/ |
社交服务(好友/搜索/DM 额度) |
services/federation/ |
联邦通信(实例管理、心跳、重连、资料同步) |
services/audit/ |
审计日志(PostgreSQL 后端) |
chat/ |
聊天服务实现(已在 5.5 节描述) |
5.8 工具系统 (app/tools/)
基于 ToolPlugin 基类的自动发现注册体系。
5.8.1 ToolPlugin 基类
class ToolPlugin:
name: str # 工具名(全局唯一)
description: str # 给 LLM 看的描述
segment: str # 所属技能段
parameters: dict # JSON Schema properties
required: list[str] # 必填参数
states: list[str] # 允许使用的 AI 状态
nullable: list[str] # 可空参数
admin_description: str # 管理员说明
async def execute(db, agent_id, group_id, arguments, context) -> dict
@classmethod
def to_definition() -> dict # OpenAI Function Calling 格式
5.8.2 技能段划分
| 段 Key | 段名称 | 包含工具 |
|---|---|---|
chat_social |
群聊社交 | send_gm, send_dm, enter_group, switch_state, set_dnd, ... |
file_operations |
文件操作 | file_read, file_write, file_edit, file_delete, file_list, file_share, execute_command, ... |
memory |
记忆系统 | store_memory(带 memory_id 则改已有那条), recall_memory, forget_memory, manage_records |
group_management |
群聊管理 | create_group, invite_to_group |
self_config |
自我配置 | update_self_config, toggle_thinking, update_emotion, manage_skills, set_status |
self_management |
自我管理 | set_alarm, list_alarms, cancel_alarm, push_state, pop_state, end_turn, compress_context, manage_workspace, ... |
5.8.3 工具注册中心
文件: app/services/tool_registry.py
class ToolRegistry:
register(plugin_cls) # 注册工具(自动发现)
get_plugin(name) # 获取插件实例
get_all_definitions() # 获取所有工具定义
get_segments() # 获取技能段信息
get_allowed_tools(state, thinking, delay) # 按状态过滤工具
dispatch(db, agent_id, group_id, name, args, context) # 分发工具调用
validate(name, arguments) # 校验工具调用格式
get_tools_info() # 管理面板信息
自动发现机制: app/tools/__init__.py 导入所有子模块时,ToolPlugin.__init_subclass__ 钩子自动注册子类。
5.9 模型层 (app/models/)
| 文件 | 核心模型 | 说明 |
|---|---|---|
user.py |
User |
用户(含 type=human/ai) |
agent.py |
Agent |
AI 代理(含状态机、API 配置、记忆配置) |
group.py |
Group, GroupMember |
群聊及成员 |
message.py |
Message |
群消息 |
dm.py |
DMSession, DMMessage |
私信会话与消息 |
friendship.py |
Friendship |
好友关系 |
world.py |
World, WorldAIS |
群视界及世界 AI |
memory.py |
RoughMemory, DetailMemory |
双层记忆 |
agent_skill.py |
AgentSkill |
AI 技能实例 |
agent_trigger.py |
AgentTrigger |
AI 触发器 |
agent_config.py |
AgentConfig |
AI 配置 |
agent_state_stack.py |
AgentStateStack |
AI 状态栈 |
agent_metrics.py |
AgentMetrics |
AI 指标 |
alarm.py |
Alarm |
AI 闹钟 |
system_settings.py |
SystemSettings |
系统设置 |
api_key_pool.py |
ApiKeyPool |
API Key 池 |
api_usage_log.py |
ApiUsageLog |
API 使用日志 |
conversation_log.py |
ConversationLog |
对话日志 |
file.py |
File |
文件记录 |
workspace.py |
Workspace |
工作区 |
personality_anchor.py |
PersonalityAnchor |
人格锚点 |
federation.py |
FederationInstance, FederationPeer |
联邦实例与对等端 |
redemption.py |
RedemptionCode |
兑换码 |
structured_record.py |
StructuredRecord |
结构化记录 |
vector_request.py |
VectorRequest |
向量化请求 |
market_item.py |
MarketItem |
商城商品 |
world_config.py |
WorldConfig |
世界配置 |
plugin.py |
Plugin, UserPluginPref |
统一插件(manifest 快照 + 管理员全局开关 / 用户个人开关) |
5.10 工具注册中心 (app/services/tool_registry.py)
作为薄封装层,对外提供兼容的模块级全局变量:
TOOL_DEFINITIONS # 工具定义列表
TOOL_HANDLERS # 工具 handler 映射
STATE_TOOL_WHITELIST # 状态白名单
SKILL_SEGMENTS # 技能段信息
get_allowed_tools(state, thinking_enabled, delay_reply_allowed)
dispatch_tool_call(db, agent_id, group_id, tool_name, arguments, context)
validate_tool_call(tool_name, arguments)
6. 前端模块详解
6.1 应用入口与路由
入口文件: frontend/src/App.tsx
路由结构:
├── 公共路由(无需认证)
│ ├── /login # 登录页
│ └── /demo-chat # Demo 聊天
├── 受保护路由(需认证)
│ ├── /chat # 群聊主页 (ChatPage)
│ ├── /dm # 私信 (DMPage)
│ ├── /agents # AI 管理 (AgentsPage)
│ ├── /agents/:id # AI 详情 (AgentDetailPage)
│ ├── /friends # 好友 (FriendsPage)
│ ├── /market # 世界商城 (MarketPage)
│ ├── /worlds # 我的世界 (WorldsPage)
│ ├── /worlds/:id # 世界详情 (WorldViewPage)
│ ├── /world-design # 世界设计 (WorldDesignPage)
│ ├── /me # 个人中心 (MePage)
│ ├── /settings # 设置 (SettingsPage)
│ ├── /usage # 用量统计 (UsagePage)
│ ├── /manual # 用户手册 (ManualPage)
│ ├── /admin # 管理后台 (AdminPage) — 需 admin 角色
│ ├── /setup # 新用户设置向导
│ └── /instance-setup # 桌面端实例配置
└── * # 404
6.2 核心组件
| 组件 | 文件 | 说明 |
|---|---|---|
Layout |
components/Layout.tsx |
主布局(侧边栏+内容区) |
Sidebar |
components/Sidebar.tsx |
侧边导航栏 |
ChatView |
components/ChatView.tsx |
聊天视图容器 |
ChatArea |
components/ChatArea.tsx |
消息列表区域 |
ChatInput |
components/ChatInput.tsx |
消息输入框 |
MessageBubble |
components/MessageBubble.tsx |
消息气泡(用户/AI/系统) |
DMChatView |
components/DMChatView.tsx |
私信视图 |
GroupSettingsPanel |
components/GroupSettingsPanel.tsx |
群设置面板 |
AgentSettingsModal |
components/AgentSettingsModal.tsx |
AI 设置弹窗 |
SkillBackpack |
components/SkillBackpack.tsx |
技能背包 |
PluginManager |
components/PluginManager.tsx |
插件管理 |
SearchOverlay |
components/SearchOverlay.tsx |
搜索遮罩 |
MarkdownContent |
components/MarkdownContent.tsx |
Markdown 渲染 |
CodeRenderer |
components/CodeRenderer.tsx |
代码高亮渲染 |
MermaidBlock |
components/MermaidBlock.tsx |
Mermaid 图表渲染 |
FilePreviewModal |
components/FilePreviewModal.tsx |
文件预览 |
Modal |
components/Modal.tsx |
通用弹窗 |
EmptyState |
components/EmptyState.tsx |
空状态提示 |
ErrorBoundary |
components/ErrorBoundary.tsx |
错误边界 |
6.3 Hooks 体系
| Hook | 文件 | 说明 |
|---|---|---|
useWebSocket |
hooks/useWebSocket.ts |
WebSocket 连接管理 |
useWorldChat |
hooks/useWorldChat.ts |
世界聊天 SSE 连接 |
useIsDark |
hooks/useIsDark.ts |
深色模式检测 |
usePendingFriendRequests |
hooks/usePendingFriendRequests.ts |
待处理好友请求 |
useDesktopNotification |
hooks/useDesktopNotification.ts |
桌面通知 |
useResizableSidebar |
hooks/useResizableSidebar.ts |
可拖拽侧边栏 |
useTimeTick |
hooks/useTimeTick.ts |
时间定时刷新 |
6.4 上下文管理
| Context | 文件 | 说明 |
|---|---|---|
AuthContext |
context/AuthContext.tsx |
认证状态(用户信息、登录/登出) |
ThemeContext |
context/ThemeContext.tsx |
主题(深色/浅色) |
I18nContext |
i18n/I18nContext.tsx |
国际化(中 / 英 / 日),见 6.7 |
6.5 页面模块
| 页面 | 文件 | 说明 |
|---|---|---|
ChatPage |
pages/ChatPage.tsx |
群聊主页面 |
DMPage |
pages/DMPage.tsx |
私信页面 |
AgentsPage |
pages/AgentsPage.tsx |
AI 列表 |
AgentDetailPage |
pages/AgentDetailPage.tsx |
AI 详情 |
FriendsPage |
pages/FriendsPage.tsx |
好友管理 |
MarketPage |
pages/MarketPage.tsx |
世界商城 |
WorldsPage |
pages/WorldsPage.tsx |
我的世界列表 |
WorldViewPage |
pages/WorldViewPage.tsx |
世界沉浸视图 |
WorldDesignPage |
pages/WorldDesignPage.tsx |
世界设计 |
SettingsPage |
pages/SettingsPage.tsx |
设置 |
AdminPage |
pages/AdminPage.tsx |
管理后台 |
MePage |
pages/MePage.tsx |
个人中心 |
UsagePage |
pages/UsagePage.tsx |
用量统计 |
LoginPage |
pages/LoginPage.tsx |
登录 |
SetupPage |
pages/SetupPage.tsx |
设置向导 |
InstanceSetupPage |
pages/InstanceSetupPage.tsx |
桌面端实例配置 |
DemoChat |
pages/DemoChat.tsx |
Demo 模式 |
组件/页面表为节选(当前 54 个组件、23 个页面);新增文件请顺手补进本表。
两张页面长这样(演示数据):


6.6 界面体系(单一来源)
写任何界面前先读 前端界面统一规范。三层结构,改一处全站生效:
| 层 | 文件 | 唯一负责 |
|---|---|---|
| 尺度令牌 | tailwind.config.js |
圆角 control/card/dialog、字号 3xs/2xs、层级 overlay/drawer/modal/toast/max |
| 语义类 | src/index.css |
.btn .btn-*、.icon-btn、.field、.card、.chip |
| React 壳 | src/components/ui/ |
PageShell PageHeader Button IconButton Input Select Card Badge Modal Dialog EmptyState |
约定:
- 页面只写布局,不写颜色/圆角/阴影;出现
bg-primary-500、rounded-2xl、px-3 py-1.5这类"自拼外观"就是漏网 - 弹窗一律
Modal(标准卡片)或Dialog(自定义卡片):ESC 关闭、锁背景滚动、点遮罩关闭由 Dialog 统一实现—— 手写fixed inset-0会丢掉这三样(2026-09 前 61 处手写遮罩里,多数没有 ESC 与滚动锁定) - 页面骨架一律
PageShell(标题栏 + 滚动 + 内容宽度四档narrow/content/wide/full) - 新增变体只改
index.css的@layer components,再在Button.tsx的VARIANT_CLASS登记; 不在页面里就地拼一套新外观 - 要整体调密度/观感就改令牌(2026-09 一次收敛:981 处圆角、398 处非标字号、78 处裸 z-index)
6.7 i18n 体系(三语 + 命名空间)
查找入口只有一个:i18n/translations.ts 的 getTranslation(lang, path, vars)。
path 形如 nav.chat(默认落 common 分区)或 tool:toolName.file_read(带分区前缀)。
| 文件 | 分区 | 内容 |
|---|---|---|
i18n/translations.ts |
common |
主字典,zh / en / ja 各一份(1700+ key) |
i18n/tool.ts |
tool |
工具名 toolName.*、状态模板 tool.*、world.reasoning |
i18n/admin_config.ts |
adminConfig |
配置组卡片文案,key 为裸名(save / embeddingLabel) |
约定与坑:
- 查不到 key 时
getTranslation原样返回 key——界面就会把admin.addProvider、adminConfig:sourceDb这样的源码串显示给用户。它不报错、不崩溃,所以必须靠npm run i18n:check兜住(2026-09 已按此修掉 48 个三语全缺 + 62 处单语缺的 key) - 分区内不重复写前缀:
adminConfig的 key 就是save,不是configGroup.save; 后端CONFIG_GROUPS的label_key/hint_key同样是裸名,ConfigGroupCard的tr()统一补adminConfig:前缀——三处必须同一个口径 - 语言解析优先级:设置向导临时覆盖 → 用户
language字段 → localStorage 缓存 → 默认语言 - 静态检查:
frontend/scripts/check-i18n.mjs(零依赖,纯 Node 标准库)扫源码里的t()/tr()调用与模板串,并读后端app_config_service.py核对下发的 key; 缺 key 即非 0 退出,--json供其它工具消费
6.8 构建产物与插件包
| 产物 | 生成方式 | 说明 |
|---|---|---|
frontend/dist |
容器内 vite build |
Web 前端产物,Docker 部署直接用它 |
dsh-copree/dist |
node dsh-copree/scripts/sync-dist.mjs |
与 frontend/dist 逐字节镜像,排除 docs/assets(README 配图不进包) |
dsh-copree/lib/*.js、lib/manifest.json |
node dsh-copree/scripts/build.mjs |
插件宿主/客户端产物 + 内容寻址清单,自更新按清单校验完整性 |
# 改了前端源码、要让 DSH 插件面板同步生效
docker exec -w /app ai_group_frontend sh -c "BASE_URL=/copree-ui/ node_modules/.bin/vite build"
node dsh-copree/scripts/sync-dist.mjs
node dsh-copree/scripts/build.mjs
改了后端 Python 记得重启容器:
docker restart ai_group_backend。 后端是挂载源码但不带 reload,改完不重启=线上还是旧代码(i18n 前缀改名踩过一次)。
English summary. All UI copy goes through a single lookup,
getTranslation(lang, path, vars), with three locales (zh / en / ja) and three namespaces
(common, tool, adminConfig). A missing key is returned verbatim, so the raw key
shows up in the UI instead of an error — npm run i18n:check is the guard for that.
Keys inside a namespace are bare (save, not configGroup.save); the backend
CONFIG_GROUPS schema uses the same bare names. Build artifacts:
frontend/dist (web) → dsh-copree/dist (plugin mirror, excluding docs/assets) →
dsh-copree/lib + lib/manifest.json (content-addressed, powers plugin self-update).
7. 关键类与函数索引
AI 核心
| 类/函数 | 文件路径 | 行号 | 说明 |
|---|---|---|---|
ChatChainManager |
app/ai/chat_chain.py |
L254 | 聊天链管理器(红黑树+双向链表) |
RulerTree |
app/ai/chat_chain.py |
L48 | 尺时间红黑树 |
ChainNode |
app/ai/chat_chain.py |
L25 | 双向链表节点 |
decide_action() |
app/ai/decider.py |
L70 | 统一行动决策入口 |
ActionDecision |
app/ai/decider.py |
L32 | 决策结果数据类 |
ActionContext |
app/ai/decider.py |
L45 | 决策上下文数据类 |
_tool_call_loop() |
app/ai/executor.py |
L289 | 工具调用主循环 |
_get_api_config() |
app/ai/executor.py |
L167 | API 配置四层优先链 |
chat_completion() |
app/ai/llm.py |
L89 | LLM 聊天补全统一入口 |
build_messages() |
app/ai/llm.py |
L886 | 构建 LLM 消息列表 |
ai_response_worker() |
app/ai/response_worker.py |
L74 | AI 响应 Worker 主循环 |
_maybe_trigger_ai_reply() |
app/ai/response_worker.py |
L403 | 群聊回复编排 |
_trigger_dm_ai_reply() |
app/ai/response_worker.py |
L727 | DM 回复编排 |
聊天核心
| 类/函数 | 文件路径 | 行号 | 说明 |
|---|---|---|---|
ChatApi |
app/chat/__init__.py |
L58 | 聊天统一接口实现 |
BaseChatApi |
app/chat/protocol.py |
L6 | 聊天协议抽象基类 |
ConnectionManager |
app/services/connection_manager.py |
L22 | WebSocket 连接管理器 |
薄大脑
| 类/函数 | 文件路径 | 行号 | 说明 |
|---|---|---|---|
BrainController |
app/services/brain/brain_controller.py |
L25 | 薄大脑控制器 |
heartbeat_manager |
app/services/brain/heartbeat_manager.py |
— | 心跳管理器 |
state_stack_manager |
app/services/brain/state_stack_manager.py |
— | 状态栈管理器 |
conflict_arbiter |
app/services/brain/conflict_arbiter.py |
— | 冲突仲裁器 |
工具系统
| 类/函数 | 文件路径 | 行号 | 说明 |
|---|---|---|---|
ToolPlugin |
app/tools/base.py |
L84 | 工具插件基类 |
ToolRegistry |
app/tools/base.py |
L152 | 工具注册中心 |
dispatch_tool_call() |
app/services/tool_registry.py |
L70 | 工具调用分发 |
get_allowed_tools() |
app/services/tool_registry.py |
L65 | 按状态获取工具 |
记忆系统
| 类/函数 | 文件路径 | 行号 | 说明 |
|---|---|---|---|
recall_relevant_memories() |
app/services/memory/memory_service.py |
— | 记忆检索(向量+文本混合) |
should_compress() |
app/services/memory/context_compression_service.py |
— | 压缩判断 |
inline_compress() |
app/services/memory/context_compression_service.py |
— | 内联压缩 |
群视界
| 类/函数 | 文件路径 | 行号 | 说明 |
|---|---|---|---|
create_world() |
app/services/world/world_service.py |
L52 | 创建世界 |
world_scheduler() |
app/services/world/world_scheduler.py |
— | 世界懒加载调度 |
skill_runtime |
app/services/world/world_skill_runtime.py |
— | 世界 Skill 运行时 |
gate_tool_call() |
app/services/world/world_ai_mode.py |
— | 工具门禁:按运行模式放行/弹窗/拦截;放行时把用户的补充要求一并交回(工具循环唯一入口调用) |
request_approval() |
app/services/world/world_ai_mode.py |
— | 审批弹窗唯一通道(AI 的 ask_user/present_plan 与平台门禁共用),返回 Approval(结论 / 用户原话 / 是否有人应答) |
resolve_approval() |
app/services/world/world_ai_mode.py |
— | 弹窗回执:收下用户写的理由(≤NOTE_MAX)并唤醒等在原地的工具调用 |
with_user_note() |
app/services/world/world_ai_mode.py |
— | 把用户的理由/补充要求并进工具结果(user_note 键的唯一入口) |
sweep_banned_files() |
app/services/world/world_file_service.py |
— | 禁用后缀兜底扫描强删(唤醒/导入/启动) |
inspect() |
app/services/world/world_moderation.py |
— | 下载内容审核唯一入口(链接/文件名/正文) |
8. API 端点概览
REST API
权限口径:群相关读接口需群成员(
require_group_member);/chat/user/*需登录;/admin/**需管理员且角色以 DB 为准;/fs/public/{id}只放行维护弹窗图片白名单。 详见 安全与权限模型。
| 模块 | 方法 | 路径 | 说明 |
|---|---|---|---|
| Auth | POST | /auth/register |
用户注册 |
| POST | /auth/login |
登录 | |
| GET | /auth/me |
获取当前用户 | |
| GM | GET | /gm/{group_id}/messages |
群聊消息历史(游标分页,需群成员) |
| POST | /gm/{group_id}/messages |
发送群聊消息 | |
| Chat | GET | /chat/user/{user_id} |
获取用户信息(需登录) |
| POST | /chat/friend/request |
发送好友请求 | |
| Agents | GET | /agents |
AI 列表 |
| POST | /agents |
创建 AI | |
| GET | /agents/{id} |
AI 详情 | |
| PUT | /agents/{id} |
更新 AI | |
| DELETE | /agents/{id} |
删除 AI | |
| POST | /agents/{id}/config |
更新 AI 配置 | |
| DM | GET | /dm/sessions |
私信会话列表 |
| GET | /dm/{session_id} |
私信消息 | |
| POST | /dm/{session_id} |
发送私信 | |
| Groups | GET | /groups |
群列表 |
| POST | /groups |
创建群 | |
| GET | /groups/{id} |
群详情 | |
| PUT | /groups/{id} |
更新群 | |
| POST | /groups/{id}/members |
添加成员 | |
| DELETE | /groups/{id}/members/{type}/{member_id} |
移除成员 | |
| Worlds | GET | /worlds |
世界列表 |
| POST | /worlds |
创建世界 | |
| GET | /worlds/{id} |
世界详情 | |
| POST | /worlds/{id}/bind |
绑定世界 | |
| Friends | GET | /friends |
好友列表 |
| POST | /friends/request |
发送好友申请 | |
| POST | /friends/{id}/accept |
接受好友 | |
| DELETE | /friends/{id} |
删除好友 | |
| Admin | GET | /admin/users |
用户列表 |
| PUT | /admin/users/{id} |
更新用户 | |
| GET | /admin/system-settings |
系统设置 | |
| PUT | /admin/system-settings |
更新系统设置 | |
| GET | /admin/metrics |
系统指标 | |
| Market | GET | /market/items |
商城列表 |
| POST | /market/publish |
发布世界 | |
| Skills | GET | /skills |
技能列表 |
| POST | /skills |
创建技能 | |
| GET | /skills/{id} |
技能详情 | |
| Files | POST | /files/upload |
上传文件 |
| GET | /files/{id} |
获取文件 | |
| GET | /files/{id}/download |
下载文件 | |
| System | GET | /system/settings |
系统设置 |
| GET | /system/health |
健康检查 | |
| Brain | GET | /brain/state |
薄大脑状态 |
| POST | /brain/arbitrate |
冲突仲裁 | |
| Plugins | GET | /plugins |
插件列表(全局开关 + 用户偏好) |
| POST | /plugins/rescan |
重扫目录(管理员) | |
| POST | /plugins/{id}/toggle |
全局开关(管理员) | |
| POST | /plugins/{id}/pref |
用户偏好 | |
| User | PUT | /user/settings |
用户设置 |
| POST | /user/redeem |
兑换额度 | |
| GET | /user/credit-status |
额度状态 | |
| Study | POST | /study/heartbeat |
自习室心跳 |
| GET | /study/summary |
自习统计 | |
| World | GET | /world/{id}/api/world |
世界信息 |
| GET/POST | /world/{id}/api/chat、/memories、/data/{key} |
世界页面调用的受控 API | |
| GET | /world/{id}/files/* |
世界静态文件(匿名可读,世界页面即可分享链接) | |
| ThemeVote | POST | /theme-vote |
主题选色投票 |
| Federation | — | /admin/federation/peers... |
联邦对端管理(管理员,见 federation_ws.py) |
上表是常读接口的概览,不是全集(29 个路由模块)。完整清单看运行实例的
/docs——生产环境(ENVIRONMENT=production)该页与/openapi.json会自动 404。
WebSocket
| 端点 | 说明 |
|---|---|
ws://host/ws?token=JWT |
主 WebSocket 端点 |
事件类型: new_message, new_dm_message, ai_thinking, ai_typing, ai_thinking_end, ai_typing_end |
|
online_update, friend_request, system_notification, world_event |
9. 数据模型关系
核心实体关系
User (用户)
├── Agent (AI 代理) [owner_id → User.id]
│ ├── AgentConfig (AI 配置)
│ ├── AgentSkill (AI 技能实例)
│ │ └── Skill (技能定义)
│ ├── AgentTrigger (AI 触发器)
│ ├── AgentStateStack (状态栈)
│ ├── AgentMetric (指标)
│ ├── Alarm (闹钟)
│ ├── RoughMemory → DetailMemory (双层记忆)
│ └── PersonalityAnchor (人格锚点)
├── GroupMember (群成员)
│ └── Group (群聊)
│ ├── Message (群消息)
│ ├── GroupType (群类型)
│ └── World (绑定的群视界)
├── Friendship (好友关系)
├── DMSession (私信会话)
│ └── DMMessage (私信消息)
├── World (群视界)
│ ├── WorldAIS (世界 AI)
│ ├── WorldConfig (世界配置)
│ └── WorldSkill (世界技能)
├── ApiKeyPool (API Key 池)
├── SystemSettings (系统设置,单例)
├── ConversationLog (对话日志)
├── AuditLog (审计日志)
├── MarketItem (商城商品)
└── RedemptionCode (兑换码)
10. 配置说明
环境变量 (.env)
# 数据库
DATABASE_URL=postgresql+asyncpg://ai_chat:password@localhost:5432/ai_group_chat
DATABASE_URL_SYNC=postgresql://ai_chat:password@localhost:5432/ai_group_chat
# JWT
JWT_SECRET_KEY=your-secret-key
# AI API
DEEPSEEK_BASE_URL=https://api.deepseek.com
EMBEDDING_MODEL=text-embedding-3-small
# 数据库密码
DB_PASSWORD=your-db-password
# 其他
DISPLAY_TIMEZONE=Asia/Shanghai
GITHUB_TOKEN=
REGISTRY_REPO=Coprexist/Copree
系统设置(管理员通过后台配置)
| 设置 | 说明 |
|---|---|
default_concurrent_ai_limit |
群默认 AI 并发上限 |
default_daily_backup_enabled |
每日备份开关 |
daily_backup_keep |
备份保留份数 |
auto_register_enabled |
公开注册开关 |
system_prompt_overrides |
管理员系统提示词覆盖 |
system_prompt_order |
提示词段拼接顺序 |
auto_dnd_threshold |
意愿分自动 DND 阈值 |
credit_per_10k_tokens |
额度兑换比例 |
agent_metrics_retention_days |
指标保留天数 |
11. 部署与运行
Docker Compose 方式(推荐)
# 1. 克隆仓库
git clone https://github.com/Coprexist/Copree.git
cd Copree
# 2. 配置环境变量
cp .env.example .env
# 编辑 .env 设置 DB_PASSWORD 和 JWT_SECRET_KEY
# 3. 启动服务
docker compose up -d
# 4. 访问
# 前端: http://localhost:5227
# 后端 API: http://localhost:5228
本地开发
# 后端
cd backend
pip install -r requirements.txt
uvicorn app.main:app --reload
# 前端
cd frontend
npm install
npm run dev
# Vite 将 /api/* 代理到 localhost:8000
数据库迁移
cd backend
# 自动迁移在应用启动时执行,也可手动:
alembic upgrade head
Docker 镜像
| 服务 | 镜像 | 端口映射 |
|---|---|---|
| PostgreSQL + pgvector | pgvector/pgvector:pg17 |
5432:5432 |
| Backend (FastAPI) | 自构建 (./backend) |
5228:8000 |
| Frontend (React) | 自构建 (./frontend) |
5227:3000 |
环境变量配置
# 数据库密码(务必修改)
DB_PASSWORD=your-secure-password
# JWT 密钥(务必修改)
JWT_SECRET_KEY=your-jwt-secret-key
# 数据目录(宿主机视角,默认 ./data;容器内的应用看到的是挂载点 /app/data)
DATA_DIR=./data
# AI API 配置
DEEPSEEK_BASE_URL=https://api.deepseek.com
# 可选:设置 API Key 池的默认 Key
持久化数据
| 路径 | 说明 |
|---|---|
./data/postgres/ |
PostgreSQL 数据持久化 |
./data/uploads/ |
用户上传文件 |
./data/world_blocks/ |
群视界积木(前端页面+后端逻辑) |
./data/backups/ |
数据库备份(如有启用) |
健康检查
| 服务 | 端点 | 说明 |
|---|---|---|
| Backend | GET /health |
返回 {"status": "ok"} |
| PostgreSQL | Docker healthcheck | pg_isready -U ai_chat |
12. 开发指南
项目结构约定
| 目录 | 约定 |
|---|---|
app/ai/ |
AI 核心逻辑(不可与业务逻辑混淆) |
app/chat/ |
纯消息管道(不含 AI 决策) |
app/services/ |
业务逻辑服务层 |
app/tools/ |
工具插件(自动发现注册) |
app/routers/ |
API 路由(自动发现注册) |
app/models/ |
SQLAlchemy ORM 模型 |
app/schemas/ |
Pydantic 请求/响应模型 |
app/prompts/ |
系统提示词模板 |
app/utils/ |
通用工具函数 |
新增 AI 工具
- 在
app/tools/下创建子目录(如my_tool/) - 实现
ToolPlugin基类:
from app.tools.base import ToolPlugin
class MyTool(ToolPlugin):
name = "my_tool"
description = "给 LLM 看的工具描述"
segment = "chat_social" # 所属技能段
parameters = {
"type": "object",
"properties": {
"param1": {"type": "string", "description": "参数说明"}
}
}
required = ["param1"]
async def execute(self, db, agent_id, group_id, arguments, context):
# 实现工具逻辑
return {"result": "success"}
- 不需要登记清单:
app/tools/__init__.py扫描目录自动导入,子类在定义时自注册。 缺少label(卡片一行文案)/segment(技能段)等契约字段会在导入时直接抛TypeError——这是故意的:不允许"默默兜底成一句工具执行成功"
新增 API 路由
- 在
app/routers/下创建路由文件(如my_router.py) - 定义
router = APIRouter(prefix="/my-api", tags=["my-api"]) - 路由会被自动发现注册,无需手动配置
新增数据库模型
- 在
app/models/下创建模型文件 - 继承
Base,使用 SQLAlchemy 2.0 声明式语法 - 在
app/models/__init__.py中导入模型 - 运行
alembic revision --autogenerate -m "my_new_model"生成迁移
代码风格
| 约定 | 说明 |
|---|---|
| 异步优先 | 所有 I/O 操作使用 async/await |
| 依赖注入 | FastAPI Depends(get_db) 获取数据库会话 |
| 类型标注 | 全面使用 Python 类型标注 |
| Pydantic 校验 | 请求/响应数据使用 Pydantic 模型校验 |
| 错误处理 | 使用 HTTPException + 统一错误响应格式 |
测试
后端用例自带连接串推导,跑之前给一个测试库即可(不要指向主库):
# 后端:整套 / 单个文件(容器内运行,无需装依赖)
PROD=$(docker exec ai_group_backend printenv DATABASE_URL)
TEST=$(printf "%s" "$PROD" | sed "s|/ai_group_chat|/ai_group_chat_test|")
docker exec -w /app -e TEST_DATABASE_URL="$TEST" \
-e TEST_DATABASE_URL_SYNC="$(printf "%s" "$TEST" | sed "s|+asyncpg||")" \
ai_group_backend python tests/run_without_pytest.py # 或 tests/test_security_guards.py
# 前端:类型检查 + i18n key 完整性(都在容器里跑,不装依赖)
docker exec -w /app ai_group_frontend node_modules/.bin/tsc --noEmit
node frontend/scripts/check-i18n.mjs
| 守卫 | 拦住什么 |
|---|---|
backend/tests/test_security_guards.py |
路由漏挂权限依赖(require_admin / require_group_member)、角色信 JWT、白名单失守 |
backend/tests/test_world_tool_summaries.py |
世界工具缺 label / summary 契约 |
frontend/scripts/check-i18n.mjs |
界面会显示裸 i18n key(三语缺 key / 后缀口径不一致) |
文档约定
| 约定 | 说明 |
|---|---|
| 同步 CHANGELOG | 任何用户可见改动都写进 CHANGELOG.md 的 Unreleased |
| 中英双语 | 指南类文档保留中英标题/副标题,关键操作段落给英文摘要 |
| 命令双份 | 依赖网络的安装命令给「A. 官方源」与「B. 国内镜像」两个各自整段可复制的块 |
| 私有资料不入库 | 审计报告、口令、公网域名/入口 IP 一律放 gitignore-本地docs/(见 docs/SUMMARY.md) |
常用 Git 分支策略
| 分支 | 说明 |
|---|---|
main |
生产稳定版本 |
develop |
开发主分支 |
feature/* |
功能开发 |
hotfix/* |
紧急修复 |
附录 A:依赖关系图
后端模块依赖关系
main.py (入口)
├── config.py (全局配置)
│ └── pydantic_settings
├── database.py (数据库层)
│ └── sqlalchemy + asyncpg
├── routers/ (路由层)
│ ├── ws.py → connection_manager.py
│ ├── chat.py → chat/__init__.py
│ ├── agents.py → services/agent/
│ ├── groups.py → models/group.py
│ └── ...
├── ai/ (AI 核心)
│ ├── response_worker.py
│ │ ├── decider.py (决策)
│ │ │ └── services/agent/ (意愿计算)
│ │ ├── executor.py (执行引擎)
│ │ │ ├── llm.py (LLM 调用)
│ │ │ └── tool_registry.py (工具分发)
│ │ ├── group_logic.py (群聊策略)
│ │ └── alarm.py (闹钟)
│ ├── chat_chain.py (聊天链)
│ └── llm.py
├── chat/ (聊天核心)
│ ├── __init__.py (ChatApi)
│ ├── protocol.py (BaseChatApi)
│ ├── message.py → models/message.py
│ ├── dm.py → models/dm.py
│ └── delivery.py (可达性)
├── services/ (业务逻辑)
│ ├── brain/ (薄大脑)
│ ├── memory/ (记忆系统)
│ ├── skill/ (技能引擎)
│ ├── world/ (群视界)
│ ├── federation/ (联邦)
│ ├── infrastructure/ (基础设施)
│ └── ...
├── tools/ (工具插件)
│ ├── base.py (ToolPlugin + ToolRegistry)
│ └── */ (各工具实现)
└── models/ (ORM 模型)
数据流依赖
用户消息
→ WebSocket (ws.py)
→ ChatApi.create_message()
→ message_queue.put()
→ ai_response_worker()
→ decide_action()
→ _get_api_config()
→ build_messages()
→ _tool_call_loop()
→ chat_completion() (LLM)
→ dispatch_tool_call() (工具)
→ ToolPlugin.execute()
→ 上下文压缩 / 记忆注入
→ ChatApi.broadcast_to_group()
→ ConnectionManager.broadcast_to_group()
→ WebSocket → 前端
外部服务依赖
| 服务 | 用途 | 配置 |
|---|---|---|
| PostgreSQL + pgvector | 关系数据 + 向量搜索 | DATABASE_URL |
| AI API (DeepSeek 等) | LLM 推理 | DEEPSEEK_BASE_URL + API Key |
| Embedding API | 文本向量化 | EMBEDDING_MODEL |
| SMTP 邮件服务 | 邮件验证(可选) | SMTP_* |
| GitHub API | 商城同步(可选) | GITHUB_TOKEN |
附录 B:AI 状态机
┌──────────┐
│ active │ ← 正常活跃
└────┬─────┘
│ 用户/AI 设为 DND
▼
┌──────────┐
│ dnd │ ← 勿扰(仅 @提及/公告可穿透)
└────┬─────┘
│ 空闲超时 / 手动切换
▼
┌──────────┐
│ inactive │ ← 休眠(不主动回复)
└────┬─────┘
│ 被管理员/系统封禁
▼
┌──────────┐
│ blocked │ ← 屏蔽(完全不响应)
└──────────┘
状态转换规则:
active → dnd: AI 主动设置或意愿分低于auto_dnd_thresholddnd → active: 手动恢复或收到 @提及active → inactive: 空闲超过阈值inactive → active: 收到消息或被唤醒* → blocked: 管理员操作blocked → active: 管理员解除
文档版本: v1.1.0 | 更新日期: 2026-08-11 本文档随项目演进持续更新,如发现不一致请以实际代码为准。