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] #[async_trait]
impl Channel for CliChannel { impl Channel for CliChannel {
fn name(&self) -> &str { fn name(&self) -> &str {
"cli" "websocket"
} }
fn is_running(&self) -> bool { fn is_running(&self) -> bool {

View File

@ -15,19 +15,19 @@ use crate::protocol::Channel as ProtocolChannel;
pub struct ChannelManager { pub struct ChannelManager {
channels: Arc<RwLock<HashMap<String, Arc<dyn Channel + Send + Sync>>>>, channels: Arc<RwLock<HashMap<String, Arc<dyn Channel + Send + Sync>>>>,
bus: Arc<MessageBus>, bus: Arc<MessageBus>,
cli_channel: Arc<CliChannel>, websocket_channel: Arc<CliChannel>,
} }
impl ChannelManager { impl ChannelManager {
pub fn new() -> Self { 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(); 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 { Self {
channels: Arc::new(RwLock::new(channels)), channels: Arc::new(RwLock::new(channels)),
bus: MessageBus::new(100), bus: MessageBus::new(100),
cli_channel, websocket_channel,
} }
} }
@ -36,8 +36,8 @@ impl ChannelManager {
self.bus.clone() self.bus.clone()
} }
pub fn cli_channel(&self) -> Arc<CliChannel> { pub fn websocket_channel(&self) -> Arc<CliChannel> {
self.cli_channel.clone() self.websocket_channel.clone()
} }
/// Initialize all Channel instances from config /// Initialize all Channel instances from config
@ -142,16 +142,7 @@ impl ChannelManager {
let mut seen = HashSet::new(); let mut seen = HashSet::new();
let mut channels: Vec<ProtocolChannel> = Vec::new(); let mut channels: Vec<ProtocolChannel> = Vec::new();
// 1. WebSocket 通道 — Web 前端自己的连接,始终存在 // 所有注册的通道websocket, feishu, wechat 等)
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 等)
for (name, _channel) in self.channels().await { for (name, _channel) in self.channels().await {
if seen.contains(&name) { if seen.contains(&name) {
continue; continue;
@ -172,7 +163,6 @@ impl ChannelManager {
fn channel_display_name(name: &str) -> String { fn channel_display_name(name: &str) -> String {
match name { match name {
"websocket" => "WebSocket".to_string(), "websocket" => "WebSocket".to_string(),
"cli" => "命令行".to_string(),
"feishu" => "飞书".to_string(), "feishu" => "飞书".to_string(),
"wechat" => "微信".to_string(), "wechat" => "微信".to_string(),
other => other.to_string(), other => other.to_string(),
@ -262,7 +252,7 @@ mod tests {
.collect::<Vec<_>>(); .collect::<Vec<_>>();
names.sort(); 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("primary").await.unwrap().name(), "primary");
assert_eq!(manager.get_channel("backup").await.unwrap().name(), "backup"); assert_eq!(manager.get_channel("backup").await.unwrap().name(), "backup");
} }
@ -323,7 +313,7 @@ mod tests {
.collect::<Vec<_>>(); .collect::<Vec<_>>();
names.sort(); 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"); 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()) .map(|m| m.clone())
.unwrap_or_default()) .unwrap_or_default())
} }
None => Err(CommandError::new( None => {
"SESSION_NOT_FOUND", tracing::warn!(
format!("Session not found for channel: {}", channel_name), 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 std::sync::Arc;
use tokio::sync::mpsc; use tokio::sync::mpsc;
const CLI_CHANNEL_NAME: &str = "cli"; const WS_CHANNEL_NAME: &str = "websocket";
/// Default media directory for WebSocket uploads /// Default media directory for WebSocket uploads
fn default_ws_media_dir() -> PathBuf { 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; let mut current_topic_id: Option<String> = None;
state state
.channel_manager .channel_manager
.cli_channel() .websocket_channel()
.register_connection( .register_connection(
current_session_id.clone(), current_session_id.clone(),
runtime_session_id.clone(), runtime_session_id.clone(),
@ -273,7 +273,7 @@ async fn handle_socket(ws: WebSocket, state: Arc<GatewayState>) {
state state
.channel_manager .channel_manager
.cli_channel() .websocket_channel()
.unregister_connection(&runtime_session_id) .unregister_connection(&runtime_session_id)
.await; .await;
tracing::info!(session_id = %runtime_session_id, current_session_id = %current_session_id, "CLI session ended"); 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 state
.channel_manager .channel_manager
.cli_channel() .websocket_channel()
.register_connection( .register_connection(
chat_id.clone(), chat_id.clone(),
runtime_session_id.to_string(), runtime_session_id.to_string(),
@ -315,7 +315,7 @@ async fn handle_inbound(
state state
.bus .bus
.publish_inbound(InboundMessage { .publish_inbound(InboundMessage {
channel: CLI_CHANNEL_NAME.to_string(), channel: WS_CHANNEL_NAME.to_string(),
sender_id, sender_id,
chat_id, chat_id,
content, content,
@ -450,7 +450,7 @@ async fn handle_inbound(
current_topic_id = ?current_topic_id, current_topic_id = ?current_topic_id,
"Building CommandContext for WebSocket command" "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_session_id(current_session_id.as_str())
.with_chat_id(current_session_id.as_str()); .with_chat_id(current_session_id.as_str());
// 只在有 topic_id 时才设置 // 只在有 topic_id 时才设置
@ -473,7 +473,7 @@ async fn handle_inbound(
*current_session_id = session_id.clone(); *current_session_id = session_id.clone();
state state
.channel_manager .channel_manager
.cli_channel() .websocket_channel()
.register_connection( .register_connection(
session_id.clone(), session_id.clone(),
runtime_session_id.to_string(), runtime_session_id.to_string(),

View File

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