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
+64
View File
@@ -0,0 +1,64 @@
# Rusty Claude CLI 模块 (rusty-claude-cli)
本模块提供了 Claw 命令行界面的另一个功能完整的实现。它集成了对话、工具执行、插件扩展以及身份验证等核心功能。
## 概览
`rusty-claude-cli` 是一个全功能的 CLI 应用程序,其主要职责包括:
- **用户交互**:提供交互式 REPL 和非交互式命令执行(`prompt` 子命令)。
- **环境初始化**:处理项目初始化 (`init`) 和配置加载。
- **身份验证**:通过本地回环服务器处理 OAuth 登录流程。
- **状态渲染**:实现丰富的终端 UI 效果,如 Markdown 渲染、语法高亮和动态加载动画 (Spinner)。
- **会话管理**:支持从保存的文件中恢复会话并执行追加的斜杠命令。
## 与 `claw-cli` 的关系
虽然 `rusty-claude-cli``claw-cli` 都生成名为 `claw` 的二进制文件,但 `rusty-claude-cli` 包含更复杂的集成逻辑:
- 它直接引用了几乎所有的核心 crate(`runtime`, `api`, `tools`, `plugins`, `commands`)。
- 它的 `main.rs` 实现非常庞大,包含了大量的业务编排逻辑。
- 它可以作为一个独立的、集成度极高的 CLI 参考实现。
## 关键特性
- **多功能子命令**
- `prompt`:快速运行单次推理。
- `login`/`logout`OAuth 身份验证管理。
- `init`:项目环境自举。
- `bootstrap-plan`:查看系统的启动阶段。
- `dump-manifests`:从上游源码中提取并显示功能清单。
- **增强的 REPL**
- 支持多行输入和历史记录。
- 集成了斜杠命令处理引擎。
- 提供详细的消耗统计和权限模式切换报告。
- **灵活的权限控制**:支持通过命令行参数 `--permission-mode` 或环境变量动态调整权限级别。
## 实现逻辑
### 核心子模块
- **`main.rs`**: 程序的入口,包含了复杂的参数解析逻辑和 REPL 循环。
- **`render.rs`**: 封装了 `TerminalRenderer``Spinner`,负责所有的终端输出美化。
- **`input.rs`**: 处理从标准输入读取数据及命令解析。
- **`init.rs`**: 专注于仓库的初始化和 `.claw.md` 文件的生成。
- **`app.rs`**: 可能包含应用程序级别的高层状态管理(取决于具体实现)。
### 工作流程
1. 程序启动,解析命令行参数。
2. 根据参数决定是执行单次任务还是进入 REPL 模式。
3. 在 REPL 模式下,初始化 `ConversationRuntime`
4. 进入循环:读取用户输入 -> 处理斜杠命令或发送给 AI -> 渲染响应 -> 执行工具 -> 循环。
5. 会话数据根据需要保存或恢复。
## 使用示例
```bash
# 启动交互模式
cargo run -p rusty-claude-cli --bin claw
# 直接运行 Prompt
cargo run -p rusty-claude-cli --bin claw prompt "检查代码中的内存泄漏"
# 恢复之前的会话并执行压缩
cargo run -p rusty-claude-cli --bin claw --resume session.json /compact
```
+17 -1
View File
@@ -35,7 +35,7 @@ use runtime::{
};
use serde_json::json;
use tools::{execute_tool, mvp_tool_specs, ToolSpec};
use plugins::{self, PluginManager, PluginManagerConfig};
use plugins::{self};
const DEFAULT_MODEL: &str = "claude-opus-4-6";
const DEFAULT_MAX_TOKENS: u32 = 32;
@@ -1870,6 +1870,12 @@ fn render_export_text(session: &Session) -> String {
for block in &message.blocks {
match block {
ContentBlock::Text { text } => lines.push(text.clone()),
ContentBlock::Thinking { thinking, .. } => {
lines.push(format!("[thinking] {thinking}"));
}
ContentBlock::RedactedThinking { .. } => {
lines.push("[thinking] <redacted>".to_string());
}
ContentBlock::ToolUse { id, name, input } => {
lines.push(format!("[tool_use id={id} name={name}] {input}"));
}
@@ -2352,6 +2358,16 @@ fn convert_messages(messages: &[ConversationMessage]) -> Vec<InputMessage> {
.iter()
.map(|block| match block {
ContentBlock::Text { text } => InputContentBlock::Text { text: text.clone() },
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),
},
ContentBlock::ToolUse { id, name, input } => InputContentBlock::ToolUse {
id: id.clone(),
name: name.clone(),