dashboard-nanobot/design/database.md

131 lines
3.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# Dashboard Nanobot 数据库设计文档(同步到当前代码)
数据库默认使用 SQLite`data/nanobot_dashboard.db`。
## 1. ERD
```mermaid
erDiagram
BOTINSTANCE ||--o{ CHANNELROUTE : "路由"
BOTINSTANCE ||--o{ BOTMESSAGE : "消息"
NANOBOTIMAGE ||--o{ BOTINSTANCE : "被引用"
BOTINSTANCE {
string id PK
string name
text system_prompt
text soul_md
text agents_md
text user_md
text tools_md
text identity_md
text tools_config_json
string llm_provider
string llm_model
string api_key
string api_base
float temperature
float top_p
int max_tokens
float presence_penalty
float frequency_penalty
string workspace_dir UK
string docker_status
string image_tag
string current_state
text last_action
datetime created_at
datetime updated_at
}
BOTMESSAGE {
int id PK
string bot_id FK
string role
text text
text media_json
datetime created_at
}
NANOBOTIMAGE {
string tag PK
string image_id
string version
string status
string source_dir
datetime created_at
}
CHANNELROUTE {
int id PK
string bot_id FK
string channel_type
string external_app_id
string app_secret
text extra_config
int internal_port
boolean is_active
}
```
## 2. 表说明
### 2.1 `botinstance`
存储机器人实例核心配置与运行参数。
- 运行与镜像:`docker_status`、`image_tag`
- LLM 基础:`llm_provider`、`llm_model`、`api_key`、`api_base`
- LLM 参数:`temperature`、`top_p`、`max_tokens`
- Agent Bootstrap 内容:`soul_md`、`agents_md`、`user_md`、`tools_md`、`identity_md`
- 工具配置:`tools_config_json`
- 运行态:`current_state`、`last_action`
### 2.2 `botmessage`
保存 Dashboard 渠道持久化对话消息。
- `role``user | assistant`
- `text`:消息正文
- `media_json`附件路径列表JSON 字符串)
### 2.3 `nanobotimage`
镜像登记表(只记录显式登记镜像)。
- `tag`: 如 `nanobot-base:v0.1.4`
- `status`: `READY | BUILDING | ERROR | UNKNOWN`
- `source_dir`: 当前手工模式通常为 `manual`
### 2.4 `channelroute`
渠道路由映射表。
- `dashboard` 为内置渠道,不允许删除或禁用。
- `extra_config` 保存各渠道差异化字段。
## 3. 已移除的数据表
以下旧表已废弃,并在服务启动时自动删除:
- `skillregistry`
- `botskillmapping`
技能管理改为 **workspace 文件系统模式**
- 路径:`workspace/bots/{bot_id}/.nanobot/workspace/skills/`
- 管理 API`/api/bots/{bot_id}/skills`、`/api/bots/{bot_id}/skills/upload`、`/api/bots/{bot_id}/skills/{skill_name}`
## 4. 启动迁移策略(当前实现)
服务启动时自动执行:
1. `SQLModel.metadata.create_all(engine)`
2. 删除旧 skills 表(`DROP TABLE IF EXISTS botskillmapping/skillregistry`
3. 针对 `botinstance``botmessage` 做增量列补齐(`ALTER TABLE ADD COLUMN`
## 5. 安全与一致性
- `api_key`、`app_secret` 建议加密存储(当前代码为明文字段,生产需加密层)。
- 启动 Bot 前,以数据库字段 + 渠道路由重新生成 workspace 文件,确保配置一致性。