feat: 更新通道名称为 websocket,调整相关引用和逻辑

This commit is contained in:
oudecheng 2026-07-03 10:58:24 +08:00
parent f3365f8d3a
commit d2af01eb30
5 changed files with 26 additions and 31 deletions

View File

@ -65,7 +65,7 @@ impl Default for CliChannel {
#[async_trait]
impl Channel for CliChannel {
fn name(&self) -> &str {
"cli"
"websocket"
}
fn is_running(&self) -> bool {

View File

@ -15,19 +15,19 @@ use crate::protocol::Channel as ProtocolChannel;
pub struct ChannelManager {
channels: Arc<RwLock<HashMap<String, Arc<dyn Channel + Send + Sync>>>>,
bus: Arc<MessageBus>,
cli_channel: Arc<CliChannel>,
websocket_channel: Arc<CliChannel>,
}
impl ChannelManager {
pub fn new() -> Self {
let cli_channel = Arc::new(CliChannel::new());
let websocket_channel = Arc::new(CliChannel::new());
let mut channels: HashMap<String, Arc<dyn Channel + Send + Sync>> = HashMap::new();
channels.insert("cli".to_string(), cli_channel.clone());
channels.insert("websocket".to_string(), websocket_channel.clone());
Self {
channels: Arc::new(RwLock::new(channels)),
bus: MessageBus::new(100),
cli_channel,
websocket_channel,
}
}
@ -36,8 +36,8 @@ impl ChannelManager {
self.bus.clone()
}
pub fn cli_channel(&self) -> Arc<CliChannel> {
self.cli_channel.clone()
pub fn websocket_channel(&self) -> Arc<CliChannel> {
self.websocket_channel.clone()
}
/// Initialize all Channel instances from config
@ -142,16 +142,7 @@ impl ChannelManager {
let mut seen = HashSet::new();
let mut channels: Vec<ProtocolChannel> = Vec::new();
// 1. WebSocket 通道 — Web 前端自己的连接,始终存在
seen.insert("websocket".to_string());
channels.push(ProtocolChannel {
id: "websocket".to_string(),
name: "WebSocket".to_string(),
description: Some("Web 前端通道".to_string()),
is_writable: true,
});
// 2. 所有动态注册的通道cli, feishu, wechat 等)
// 所有注册的通道websocket, feishu, wechat 等)
for (name, _channel) in self.channels().await {
if seen.contains(&name) {
continue;
@ -172,7 +163,6 @@ impl ChannelManager {
fn channel_display_name(name: &str) -> String {
match name {
"websocket" => "WebSocket".to_string(),
"cli" => "命令行".to_string(),
"feishu" => "飞书".to_string(),
"wechat" => "微信".to_string(),
other => other.to_string(),
@ -262,7 +252,7 @@ mod tests {
.collect::<Vec<_>>();
names.sort();
assert_eq!(names, vec!["backup", "cli", "primary"]);
assert_eq!(names, vec!["backup", "primary", "websocket"]);
assert_eq!(manager.get_channel("primary").await.unwrap().name(), "primary");
assert_eq!(manager.get_channel("backup").await.unwrap().name(), "backup");
}
@ -323,7 +313,7 @@ mod tests {
.collect::<Vec<_>>();
names.sort();
assert_eq!(names, vec!["cli", "wechat_main"]);
assert_eq!(names, vec!["websocket", "wechat_main"]);
assert_eq!(manager.get_channel("wechat_main").await.unwrap().name(), "wechat_main");
}
}

View File

@ -51,9 +51,13 @@ pub async fn get_messages_from_session(
.map(|m| m.clone())
.unwrap_or_default())
}
None => Err(CommandError::new(
"SESSION_NOT_FOUND",
format!("Session not found for channel: {}", channel_name),
)),
None => {
tracing::warn!(
channel = %channel_name,
chat_id = %chat_id,
"No in-memory session, returning empty message list"
);
Ok(Vec::new())
}
}
}

View File

@ -40,7 +40,7 @@ use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::mpsc;
const CLI_CHANNEL_NAME: &str = "cli";
const WS_CHANNEL_NAME: &str = "websocket";
/// Default media directory for WebSocket uploads
fn default_ws_media_dir() -> PathBuf {
@ -157,7 +157,7 @@ async fn handle_socket(ws: WebSocket, state: Arc<GatewayState>) {
let mut current_topic_id: Option<String> = None;
state
.channel_manager
.cli_channel()
.websocket_channel()
.register_connection(
current_session_id.clone(),
runtime_session_id.clone(),
@ -273,7 +273,7 @@ async fn handle_socket(ws: WebSocket, state: Arc<GatewayState>) {
state
.channel_manager
.cli_channel()
.websocket_channel()
.unregister_connection(&runtime_session_id)
.await;
tracing::info!(session_id = %runtime_session_id, current_session_id = %current_session_id, "CLI session ended");
@ -301,7 +301,7 @@ async fn handle_inbound(
state
.channel_manager
.cli_channel()
.websocket_channel()
.register_connection(
chat_id.clone(),
runtime_session_id.to_string(),
@ -315,7 +315,7 @@ async fn handle_inbound(
state
.bus
.publish_inbound(InboundMessage {
channel: CLI_CHANNEL_NAME.to_string(),
channel: WS_CHANNEL_NAME.to_string(),
sender_id,
chat_id,
content,
@ -450,7 +450,7 @@ async fn handle_inbound(
current_topic_id = ?current_topic_id,
"Building CommandContext for WebSocket command"
);
let mut cmd_ctx = CommandContext::new("websocket", "cli")
let mut cmd_ctx = CommandContext::new("websocket", "websocket")
.with_session_id(current_session_id.as_str())
.with_chat_id(current_session_id.as_str());
// 只在有 topic_id 时才设置
@ -473,7 +473,7 @@ async fn handle_inbound(
*current_session_id = session_id.clone();
state
.channel_manager
.cli_channel()
.websocket_channel()
.register_connection(
session_id.clone(),
runtime_session_id.to_string(),

View File

@ -126,6 +126,7 @@ fn test_tool_call_outbound_serialization() {
topic_id: None,
timestamp: None,
reasoning_content: None,
user_message_id: None,
};
let json = serde_json::to_string(&msg).unwrap();