重构: 整合客户端代码到 client 目录

- 将 cli/ 目录合并到 client/
- InputEvent::Message 改为简单 String 类型,移除对 ChatMessage 的依赖
- cli 模块从 lib.rs 移除
- client/mod.rs 添加 mod channel 和 mod input 声明
- 添加设计文档 docs/plans/2026-04-26-client-refactor-design.md
This commit is contained in:
xiaoxixi 2026-04-26 17:18:13 +08:00
parent dfe0fad61e
commit 72c888a41f
6 changed files with 48 additions and 13 deletions

View File

@ -0,0 +1,40 @@
# 客户端代码整合设计
## 目标
将分散在 `src/cli/``src/client/` 的客户端代码整合到 `src/client/` 目录。
## 变更
### 目录结构
```
src/
├── client/ # 整合后的客户端模块
│ ├── mod.rs # 主程序入口 (run 函数)
│ ├── input.rs # InputHandler + InputCommand (从 cli/input.rs 合并)
│ └── channel.rs # CliChannel (从 cli/channel.rs 合并)
├── cli/ # 删除
└── protocol.rs # 保留
```
### 关键变更
| 变更 | 说明 |
|------|------|
| `InputEvent::Message(String)` | 简化为只携带文本内容,不再使用 `ChatMessage` |
| `cli` 模块删除 | 代码合并到 `client` |
| 解耦 | `client` 不再依赖 `bus::ChatMessage` |
## 实施步骤
1. 创建 `src/client/input.rs` - 从 `cli/input.rs` 合并,修改 `InputEvent::Message``String`
2. 创建 `src/client/channel.rs` - 从 `cli/channel.rs` 直接复制
3. 更新 `src/client/mod.rs` - 更新 import
4. 更新 `src/lib.rs` - 删除 `pub mod cli;`
5. 删除 `src/cli/` 目录
## 验证
- `cargo build` 通过
- 功能保持不变

View File

@ -1,5 +0,0 @@
pub mod channel;
pub mod input;
pub use channel::CliChannel;
pub use input::{InputCommand, InputEvent, InputHandler};

View File

@ -1,9 +1,7 @@
use crate::bus::ChatMessage;
use super::channel::CliChannel; use super::channel::CliChannel;
pub enum InputEvent { pub enum InputEvent {
Message(ChatMessage), Message(String),
Command(InputCommand), Command(InputCommand),
} }
@ -41,7 +39,7 @@ impl InputHandler {
return Ok(Some(InputEvent::Command(cmd))); return Ok(Some(InputEvent::Command(cmd)));
} }
Ok(Some(InputEvent::Message(ChatMessage::user(line)))) Ok(Some(InputEvent::Message(line)))
} }
Ok(None) => Ok(None), Ok(None) => Ok(None),
Err(e) => Err(InputError::IoError(e)), Err(e) => Err(InputError::IoError(e)),

View File

@ -1,9 +1,12 @@
pub use crate::protocol::{WsInbound, WsOutbound, serialize_inbound, serialize_outbound}; pub use crate::protocol::{WsInbound, WsOutbound, serialize_inbound, serialize_outbound};
mod channel;
mod input;
use futures_util::{SinkExt, StreamExt}; use futures_util::{SinkExt, StreamExt};
use tokio_tungstenite::{connect_async, tungstenite::Message}; use tokio_tungstenite::{connect_async, tungstenite::Message};
use crate::cli::{InputCommand, InputEvent, InputHandler}; use input::{InputCommand, InputEvent, InputHandler};
fn format_session_list(sessions: &[crate::protocol::SessionSummary], current_session_id: Option<&str>) -> String { fn format_session_list(sessions: &[crate::protocol::SessionSummary], current_session_id: Option<&str>) -> String {
if sessions.is_empty() { if sessions.is_empty() {
@ -181,9 +184,9 @@ pub async fn run(gateway_url: &str) -> Result<(), Box<dyn std::error::Error>> {
} }
continue; continue;
} }
InputEvent::Message(msg) => { InputEvent::Message(content) => {
let inbound = WsInbound::UserInput { let inbound = WsInbound::UserInput {
content: msg.content, content,
channel: None, channel: None,
chat_id: current_session_id.clone(), chat_id: current_session_id.clone(),
sender_id: None, sender_id: None,

View File

@ -1,7 +1,6 @@
pub mod config; pub mod config;
pub mod providers; pub mod providers;
pub mod bus; pub mod bus;
pub mod cli;
pub mod agent; pub mod agent;
pub mod gateway; pub mod gateway;
pub mod client; pub mod client;