feat: 添加 Web 前端及服务端 SSE 流式支持,扩展多模型兼容

后端:
  - server: 实现完整的 HTTP 会话管理(CRUD)+ SSE 事件流推送,
    支持双通道架构(POST 发消息 + GET SSE 接收流式响应)
  - runtime: ContentBlock 新增 Thinking / RedactedThinking 变体,
    支持思考过程和已编辑思考的序列化/反序列化
  - api: 注册 GLM 系列模型(glm-4/5 等)到模型注册表,
    扩展 XAI/OpenAI 兼容提供商的请求构建逻辑

  前端:
  - 基于 Ant Design X 构建完整聊天界面:Bubble.List 消息列表、
    Sender 输入框、Conversations 会话管理、Think 思考过程折叠、
    ThoughtChain 工具调用链展示
  - XMarkdown 集成:代码高亮、Mermaid 图表、LaTeX 公式、
    自定义脚注、流式渲染(incomplete 占位符)
  - SSE Hook 对接服务端事件流,手动管理 AssistantBuffer 累积 delta
  - 深色/浅色主题切换,会话侧边栏(新建/切换/删除)
This commit is contained in:
fengmengqi
2026-04-10 16:29:27 +08:00
parent 0782159ecd
commit 4a04faf926
56 changed files with 8611 additions and 172 deletions
+51
View File
@@ -0,0 +1,51 @@
# 工具规范模块 (tools)
本模块定义了 AI 代理可以使用的所有内置工具的规范 (Schema)、权限要求以及分发逻辑。
## 概览
`tools` 模块充当了 AI 认知能力与物理操作之间的桥梁,其主要职责包括:
- **工具定义**:使用 JSON Schema 定义每个工具的输入参数结构,以便 AI 正确调用。
- **权限映射**:为每个工具分配安全等级(如只读、工作区写入、完全访问)。
- **工具注册表 (GlobalToolRegistry)**:统一管理内置工具和由插件提供的动态工具。
- **分发执行**:将 AI 生成的 JSON 调用分发到 `runtime` 模块中的具体实现。
## 关键特性
- **内置工具集 (MVP Tools)**
- **系统交互**`bash`, `PowerShell`, `REPL`
- **文件操作**`read_file`, `write_file`, `edit_file`
- **搜索与发现**`glob_search`, `grep_search`, `ToolSearch`
- **网络与辅助**`WebSearch`, `WebFetch`, `Sleep`
- **高级调度**`Agent`(启动子代理), `Skill`(加载专用技能), `TodoWrite`(任务管理)。
- **名称归一化**:支持工具别名(例如将 `grep` 映射为 `grep_search`),提高 AI 调用的稳健性。
- **插件集成**:允许 `plugins` 模块注册自定义工具,并确保它们与内置工具不发生命名冲突。
## 实现逻辑
### 核心结构
- **`ToolSpec`**: 核心配置结构,存储工具的元数据(名称、描述、Schema、权限)。
- **`GlobalToolRegistry`**: 负责维护工具列表,并提供 `definitions` 方法生成供 LLM 使用的工具 API 声明。
- **`execute_tool`**: 顶级分发函数,负责将反序列化后的输入传递给底层的执行函数。
### 工作流程
1. 系统初始化时,根据用户配置和加载的插件,构建 `GlobalToolRegistry`
2. 将工具定义转换为 AI 模型可理解的格式(由 `api` 模块处理)。
3. 当接收到 AI 的工具调用请求时,`runtime::ConversationRuntime` 调用 `ToolExecutor`
4. `ToolExecutor` 委托给本模块的 `execute_tool` 函数。
5. 本模块验证输入格式,并调用 `runtime` 提供的底层文件或进程操作 API。
## 使用示例 (工具定义)
```rust
use tools::{ToolSpec, mvp_tool_specs};
use serde_json::json;
// 获取所有 MVP 工具的规范
let specs = mvp_tool_specs();
for spec in specs {
println!("工具: {}, 权限级别: {:?}", spec.name, spec.required_permission);
}
```
+15 -4
View File
@@ -1628,7 +1628,7 @@ fn build_agent_runtime(
.clone()
.unwrap_or_else(|| DEFAULT_AGENT_MODEL.to_string());
let allowed_tools = job.allowed_tools.clone();
let api_client = ProviderRuntimeClient::new(model, allowed_tools.clone())?;
let api_client = ProviderRuntimeClient::new(&model, allowed_tools.clone())?;
let tool_executor = SubagentToolExecutor::new(allowed_tools);
Ok(ConversationRuntime::new(
Session::new(),
@@ -1805,8 +1805,8 @@ struct ProviderRuntimeClient {
}
impl ProviderRuntimeClient {
fn new(model: String, allowed_tools: BTreeSet<String>) -> Result<Self, String> {
let model = resolve_model_alias(&model).to_string();
fn new(model: &str, allowed_tools: BTreeSet<String>) -> Result<Self, String> {
let model = resolve_model_alias(model).clone();
let client = ProviderClient::from_model(&model).map_err(|error| error.to_string())?;
Ok(Self {
runtime: tokio::runtime::Runtime::new().map_err(|error| error.to_string())?,
@@ -1818,6 +1818,7 @@ impl ProviderRuntimeClient {
}
impl ApiClient for ProviderRuntimeClient {
#[allow(clippy::too_many_lines)]
fn stream(&mut self, request: ApiRequest) -> Result<Vec<AssistantEvent>, RuntimeError> {
let tools = tool_specs_for_allowed_tools(Some(&self.allowed_tools))
.into_iter()
@@ -1991,6 +1992,13 @@ fn convert_messages(messages: &[ConversationMessage]) -> Vec<InputMessage> {
}],
is_error: *is_error,
},
ContentBlock::Thinking { thinking, signature } => InputContentBlock::Thinking {
thinking: thinking.clone(),
signature: signature.clone(),
},
ContentBlock::RedactedThinking { data } => InputContentBlock::RedactedThinking {
data: serde_json::from_str(&data.render()).unwrap_or(serde_json::Value::Null),
},
})
.collect::<Vec<_>>();
(!content.is_empty()).then(|| InputMessage {
@@ -2061,7 +2069,10 @@ fn final_assistant_text(summary: &runtime::TurnSummary) -> String {
.iter()
.filter_map(|block| match block {
ContentBlock::Text { text } => Some(text.as_str()),
_ => None,
ContentBlock::Thinking { .. }
| ContentBlock::RedactedThinking { .. }
| ContentBlock::ToolUse { .. }
| ContentBlock::ToolResult { .. } => None,
})
.collect::<Vec<_>>()
.join("")