修复飞书渠道会话创建时机:执行斜杠命令前先确保会话存在

在 Feishu 收到斜杠命令时,先通过 CreateDialog 创建会话,
再执行实际的命令。这样 /dump 等命令就能正确获取对话历史。
This commit is contained in:
xiaoxixi 2026-04-28 21:06:25 +08:00
parent c52461055d
commit e787203e94
2 changed files with 40 additions and 26 deletions

View File

@ -2017,16 +2017,36 @@ impl Channel for FeishuChannel {
// Check for slash command // Check for slash command
if let Some((cmd_name, cmd_args)) = parse_slash_command(&msg.content) { if let Some((cmd_name, cmd_args)) = parse_slash_command(&msg.content) {
tracing::info!(cmd = %cmd_name, "Feishu slash command detected"); tracing::info!(cmd = %cmd_name, "Feishu slash command detected");
// For slash commands, we need to ensure a session exists first
// Get or create session via control plane
let (reply_tx, mut reply_rx) = mpsc::channel(1); let (reply_tx, mut reply_rx) = mpsc::channel(1);
bus.publish_control(crate::bus::ControlMessage {
op: SessionCommand::CreateDialog {
channel: msg.channel.clone(),
chat_id: msg.chat_id.clone(),
title: None,
},
reply_tx,
}).await?;
// Wait for session creation
let session_id = match reply_rx.recv().await {
Some(Ok(SessionEvent::DialogCreated { session_id, .. })) => Some(session_id),
_ => None,
};
// Now execute the slash command with the session
let (cmd_reply_tx, mut cmd_reply_rx) = mpsc::channel(1);
bus.publish_control(crate::bus::ControlMessage { bus.publish_control(crate::bus::ControlMessage {
op: SessionCommand::ExecuteSlashCommand { op: SessionCommand::ExecuteSlashCommand {
command: cmd_name.to_string(), command: cmd_name.to_string(),
args: if cmd_args.is_empty() { None } else { Some(cmd_args.to_string()) }, args: if cmd_args.is_empty() { None } else { Some(cmd_args.to_string()) },
channel: msg.channel.clone(), channel: msg.channel.clone(),
chat_id: msg.chat_id.clone(), chat_id: msg.chat_id.clone(),
current_session_id: None, current_session_id: session_id,
}, },
reply_tx, reply_tx: cmd_reply_tx,
}).await?; }).await?;
// Handle response // Handle response

View File

@ -370,14 +370,8 @@ impl SessionManager {
} }
} }
"info" => { "info" => {
let sid = if let Some(s) = current_session_id { if let Some(sid) = current_session_id {
s.clone() let session = self.get_or_create_session(sid).await?;
} else {
// Create a new session if none exists (e.g., Feishu first message)
let (new_id, _) = self.create_session(channel, chat_id, None).await?;
new_id
};
let session = self.get_or_create_session(&sid).await?;
let session_guard = session.lock().await; let session_guard = session.lock().await;
let message_count = session_guard.get_history().len(); let message_count = session_guard.get_history().len();
let session_id_str = session_guard.session_id(); let session_id_str = session_guard.session_id();
@ -385,19 +379,19 @@ impl SessionManager {
"Session ID: {}\nMessage count: {}", "Session ID: {}\nMessage count: {}",
session_id_str, message_count session_id_str, message_count
))) )))
} else {
Ok((None, "No active session.".to_string()))
}
} }
"dump" => { "dump" => {
let sid = if let Some(s) = current_session_id { if let Some(sid) = current_session_id {
s.clone() let session = self.get_or_create_session(sid).await?;
} else {
// Create a new session if none exists (e.g., Feishu first message)
let (new_id, _) = self.create_session(channel, chat_id, None).await?;
new_id
};
let session = self.get_or_create_session(&sid).await?;
let session_guard = session.lock().await; let session_guard = session.lock().await;
let md = session_guard.dump_as_markdown(); let md = session_guard.dump_as_markdown();
Ok((None, md)) Ok((None, md))
} else {
Ok((None, "No active session.".to_string()))
}
} }
_ => Err(AgentError::Other(format!("Command not implemented: {}", cmd.name))), _ => Err(AgentError::Other(format!("Command not implemented: {}", cmd.name))),
} }