feat: add AI endpoint manager plugin with configuration and routing capabilities
Some checks failed
CI / Determine matrix (push) Has been cancelled
CI / Sanitizer (ASan+UBSan) / ubuntu-24.04 (push) Has been cancelled
CI / Coverage (gcovr) / ubuntu-24.04 (push) Has been cancelled
CI / ${{ matrix.os }} / ${{ matrix.build_type }} (push) Has been cancelled

- Introduced `ai_endpoint_mgr` plugin to manage multiple AI provider endpoints.
- Added configuration reference documentation for `config.toml`.
- Implemented endpoint loading, active endpoint switching, and model mutation.
- Included error handling for missing endpoints and configuration failures.
- Developed unit tests covering various scenarios including error paths and concurrency.
This commit is contained in:
2026-06-03 21:07:25 +08:00
parent 28ae90a6cc
commit 4745ce1f1c
18 changed files with 1570 additions and 34 deletions

View File

@@ -24,6 +24,7 @@
| 文档 | 说明 |
|------|------|
| [CLI 命令速查](reference/commands.md) | 全部 CLI 命令的别名、作用与示例 |
| [配置参考](reference/config.md) | config.toml 单 provider 与多 endpoint 全部字段说明、默认值与安全提示 |
| [Plugin ABI 契约](reference/plugin-abi.md) | 跨 DLL 通信的 C ABI 规范:内存所有权、堆纪律、回调线程安全 |
---
@@ -53,7 +54,7 @@
### 参考
- [ ] **API 参考** (`reference/api.md`) — TODO: dstalk_host.h 完整 API 说明与调用示例
- [ ] **配置参考** (`reference/config.md`) — TODO: config.toml 所有字段的详细说明
- [x] **配置参考** (`reference/config.md`) — config.toml 所有字段的详细说明、默认值与安全提示
- [ ] **服务接口参考** (`reference/services.md`) — TODO: dstalk_services.h 中所有 vtable 接口定义
---
@@ -63,6 +64,7 @@
- 命令以 `$ ` 前缀表示, 在终端中运行
- 代码块标注语言 (```c, ```toml, ```bash 等)
- [ ] 表示计划中未完成的文档
- 配置文件模板见项目根目录 `config.example.toml`
---

156
docs/reference/config.md Normal file
View File

@@ -0,0 +1,156 @@
# 配置参考 / Configuration Reference
`config.toml` 是 dstalk 的唯一配置文件,放在项目根目录。本文档列出所有支持字段、类型、默认值与使用说明。
---
## 配置概览
配置分为两种模式:
| 模式 | 适用场景 | 核心 key |
|------|----------|----------|
| **单 Provider** | 只需一个 AI 后端 | `ai.provider`, `api.*` |
| **多 Endpoint** | 同时配置多个 AI 后端,运行时切换 | `endpoints.names`, `endpoint.<name>.*` |
两种模式可以同时配置,但 `ai_endpoint_mgr``chat` / `chat_stream` 调用优先走 `endpoints.*`
---
## 一、单 Provider 模式 (Legacy)
直接在 `[global]` 下声明。这些 key 无默认值——不配置则无法启动 AI 对话。
### ai.provider
- **类型**: string
- **默认**: 无(必填)
- **值**: `"ai_openai"``"ai_anthropic"`
- **说明**: 指定要加载的 AI provider 插件。该名称对应插件 DLL 注册的服务名。
### api.base_url
- **类型**: string
- **默认**: 无(必填)
- **说明**: AI API 的基础 URL。例如:
- OpenAI: `https://api.openai.com/v1`
- Anthropic: `https://api.anthropic.com`
- DeepSeek (OpenAI 兼容): `https://api.deepseek.com/v1`
### api.api_key
- **类型**: string
- **默认**: 无(必填)
- **说明**: API 密钥。注意保管,不要提交到版本控制。
### api.model
- **类型**: string
- **默认**: 无(必填)
- **说明**: 要使用的模型名称。例如 `gpt-4o``claude-sonnet-4-20250514``deepseek-chat`
---
## 二、多 Endpoint 模式 (推荐)
`ai_endpoint_mgr` 插件管理。每个 endpoint 是一个命名的 AI 后端配置,运行时可通过 `/endpoint` 命令切换。
### endpoints.names
- **类型**: string (逗号分隔)
- **默认**: 无
- **说明**: 所有要启用的 endpoint 名称列表。例如 `"openai_main, anthropic_alt"`。重复名称会被跳过并输出警告。
### endpoints.active
- **类型**: string
- **默认**: 取 `endpoints.names` 中第一个成功加载的 endpoint
- **说明**: 启动时默认使用的 endpoint 名称。如果指定的名称不在 `endpoints.names` 中或对应配置无效,则回退到第一个成功加载的 endpoint。
---
### endpoint.\<name\>.provider
- **类型**: string
- **默认**: 无(必填)
- **值**: `"ai_openai"``"ai_anthropic"`
- **说明**: 该 endpoint 使用的 AI provider 服务名称。
### endpoint.\<name\>.base_url
- **类型**: string
- **默认**: 按 provider 自动推导
- **说明**:
- 未配置时,已知 provider 自动使用默认 base_url:
- `ai_anthropic``https://api.anthropic.com`
- `ai_openai``https://api.openai.com/v1`
- 如果 provider 不在已知列表中,**必须**显式配置 `base_url`,否则该 endpoint 加载失败。
- 显式配置的值优先于默认值可用于自定义端点如代理、DeepSeek 兼容 API 等)。
### endpoint.\<name\>.api_key
- **类型**: string
- **默认**: 无(必填)
- **说明**: 该 endpoint 的 API 密钥。
> **安全提示**: `api_key` **不会**出现在 `list_json()` 的输出中,也不进入日志。这是有意为之的安全策略。
### endpoint.\<name\>.model
- **类型**: string
- **默认**: 无(必填)
- **说明**: 该 endpoint 默认使用的模型名称。运行时可通过 `set_model()` 修改并同步回 `config.toml`
### endpoint.\<name\>.max_tokens
- **类型**: integer
- **默认**: `4096`
- **范围**: `1` ~ `1000000`(超出范围的值视为无效,回退到默认值)
- **说明**: 每次请求的最大输出 token 数。
### endpoint.\<name\>.temperature
- **类型**: double
- **默认**: `0.7`
- **范围**: `0.0` ~ `2.0`(超出范围的值视为无效,回退到默认值)
- **说明**: 生成温度。越高随机性越强,越低越确定性。
---
## 三、ui 配置 (可选 / optional)
以下 key 目前为 CLI 前端预留,后续 GUI/Web 前端也会使用。
### ui.prompt
- **类型**: string
- **默认**: `"> "`
- **说明**: 命令行提示符字符串。
### ui.multiline
- **类型**: string
- **默认**: `"/"` (即不支持多行输入)
- **说明**: 多行输入结束符。发送该字符串结束多行输入模式。
---
## 四、完整配置示例
见项目根目录 `config.example.toml`
---
## 五、运行时 endpoint 操作
`ai_endpoint_mgr` 服务提供以下接口(通过 C ABI供前端调用:
| 操作 | 说明 |
|------|------|
| `count()` | 返回已加载的 endpoint 数量 |
| `list_json()` | 返回 JSON 数组,每项含 `name`, `provider`, `base_url`, `model`, `active` (不含 `api_key` |
| `get_active()` | 返回当前激活的 endpoint 名称 |
| `set_active(name)` | 切换激活 endpoint返回 0 成功,-2 表示不存在 |
| `set_model(name, model)` | 修改 endpoint 的模型并同步到 configname 为 null 时作用于当前 active endpoint |
| `chat(ep, history, len, input, tools)` | 路由对话到指定 endpointep 为 null 时用 active |
| `chat_stream(ep, history, len, input, cb, userdata)` | 流式路由对话 |

View File

@@ -96,6 +96,8 @@ api.model = "gpt-4o"
> **关键**: 修改 `ai.provider` 字段即可在不同后端间切换, 无需改动代码。
>
> API Key 可从 [OpenAI-compatible 开放平台](https://platform.openai.com/) 或 [Anthropic Console](https://console.anthropic.com/) 获取。
>
> **多 Endpoint 配置** (同时使用多个 AI 后端并在运行时通过 `/endpoint` 切换): 参见 [配置参考](../reference/config.md) 和项目根目录 `config.example.toml`。
---
@@ -172,5 +174,6 @@ dstalk v0.1.0 | dstalk AI | /help 查看帮助 | /quit 退出
## 下一步
- 查看 [CLI 命令速查表](../reference/commands.md) 了解全部命令
- 查看 [配置参考](../reference/config.md) 了解 config.toml 所有字段
- 输入 `/help` 在 dstalk 内查看命令列表
- 输入 `/status` 查看当前运行状态