From 831832664d002379df36609d4c46b653d0deda5e Mon Sep 17 00:00:00 2001 From: ooodc <549496103@qq.com> Date: Sat, 16 May 2026 20:19:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=87=8D=E6=9E=84=E4=BC=9A=E8=AF=9D?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=BD=93=E5=89=8D=E8=AF=9D=E9=A2=98=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=8C=E7=AE=80=E5=8C=96=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E5=A4=84=E7=90=86=E4=B8=AD=E7=9A=84=E4=BC=9A=E8=AF=9D=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/command/handlers/switch_session.rs | 20 -------------------- src/gateway/cli_session.rs | 8 ++++++++ src/gateway/processor.rs | 11 ++++------- src/gateway/runtime.rs | 3 +-- src/gateway/session.rs | 11 +++++++++++ src/gateway/tool_registry_factory.rs | 5 +---- src/tools/task/runtime.rs | 2 +- 7 files changed, 26 insertions(+), 34 deletions(-) diff --git a/src/command/handlers/switch_session.rs b/src/command/handlers/switch_session.rs index 2f34d39..c8a75b3 100644 --- a/src/command/handlers/switch_session.rs +++ b/src/command/handlers/switch_session.rs @@ -107,24 +107,4 @@ async fn handle_switch_session( .with_metadata("topic_id", &topic.id) .with_metadata("title", &topic.title) .with_metadata("message_count", &topic.message_count.to_string())) -} - -fn format_time_ago(timestamp_ms: i64) -> String { - let now = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap() - .as_millis() as i64; - - let diff_ms = now - timestamp_ms; - let diff_secs = diff_ms / 1000; - - if diff_secs < 60 { - "just now".to_string() - } else if diff_secs < 3600 { - format!("{} mins ago", diff_secs / 60) - } else if diff_secs < 86400 { - format!("{} hours ago", diff_secs / 3600) - } else { - format!("{} days ago", diff_secs / 86400) - } } \ No newline at end of file diff --git a/src/gateway/cli_session.rs b/src/gateway/cli_session.rs index b5338dc..9fda499 100644 --- a/src/gateway/cli_session.rs +++ b/src/gateway/cli_session.rs @@ -20,6 +20,7 @@ impl CliSessionService { } /// 创建指定通道的会话 + #[allow(dead_code)] pub(crate) fn create_with_channel( &self, channel_name: &str, @@ -30,12 +31,14 @@ impl CliSessionService { .map_err(|err| AgentError::Other(format!("create session error: {}", err))) } + #[allow(dead_code)] pub(crate) fn get(&self, session_id: &str) -> Result, AgentError> { self.store .get_session(session_id) .map_err(|err| AgentError::Other(format!("get session error: {}", err))) } + #[allow(dead_code)] pub(crate) fn list(&self, include_archived: bool) -> Result, AgentError> { self.store .list_sessions("cli", include_archived) @@ -43,6 +46,7 @@ impl CliSessionService { } /// 列出指定通道的会话 + #[allow(dead_code)] pub(crate) fn list_by_channel( &self, channel_name: &str, @@ -53,24 +57,28 @@ impl CliSessionService { .map_err(|err| AgentError::Other(format!("list sessions error: {}", err))) } + #[allow(dead_code)] pub(crate) fn rename(&self, session_id: &str, title: &str) -> Result<(), AgentError> { self.store .rename_session(session_id, title) .map_err(|err| AgentError::Other(format!("rename session error: {}", err))) } + #[allow(dead_code)] pub(crate) fn archive(&self, session_id: &str) -> Result<(), AgentError> { self.store .archive_session(session_id) .map_err(|err| AgentError::Other(format!("archive session error: {}", err))) } + #[allow(dead_code)] pub(crate) fn delete(&self, session_id: &str) -> Result<(), AgentError> { self.store .delete_session(session_id) .map_err(|err| AgentError::Other(format!("delete session error: {}", err))) } + #[allow(dead_code)] pub(crate) fn clear_messages(&self, session_id: &str) -> Result<(), AgentError> { self.store .clear_messages(session_id) diff --git a/src/gateway/processor.rs b/src/gateway/processor.rs index 8ddcd16..fa62f0a 100644 --- a/src/gateway/processor.rs +++ b/src/gateway/processor.rs @@ -145,13 +145,10 @@ impl InboundProcessor { // 计算正确的 session_id(根据 channel_name 和 chat_id) let session_id = persistent_session_id(&inbound.channel, &inbound.chat_id); - // 获取当前话题(如果有 Session) - let current_topic = if let Some(session) = self.session_manager.get(&inbound.channel).await { - let guard = session.lock().await; - guard.current_topic(&inbound.chat_id).map(|s| s.to_string()) - } else { - None - }; + // 获取当前话题(封装了 session 创建逻辑) + let current_topic = self.session_manager + .get_current_topic(&inbound.channel, &inbound.chat_id) + .await?; // 使用 ChannelInputAdapter 尝试解析命令 let adapter = ChannelInputAdapter::new(); diff --git a/src/gateway/runtime.rs b/src/gateway/runtime.rs index a38188b..b43a5f8 100644 --- a/src/gateway/runtime.rs +++ b/src/gateway/runtime.rs @@ -11,7 +11,7 @@ use crate::storage::{ }; use crate::tools::{ DefaultSubAgentRuntime, InMemoryTaskRepository, NoopSessionMessageSender, - SessionMessageSender, SubAgentRuntime, SubAgentRuntimeConfig, ToolRegistry, + SessionMessageSender, SubAgentRuntimeConfig, ToolRegistry, }; use super::agent_factory::AgentFactory; @@ -89,7 +89,6 @@ pub(crate) fn build_session_manager_with_sender( scheduler_jobs, skill_events.clone(), session_message_sender.clone(), - conversations.clone(), known_agents, default_timezone, disabled_tools, diff --git a/src/gateway/session.rs b/src/gateway/session.rs index d45bdf0..63c8248 100644 --- a/src/gateway/session.rs +++ b/src/gateway/session.rs @@ -547,6 +547,17 @@ impl SessionManager { self.lifecycle.get(channel_name).await } + /// 获取指定 chat 的当前话题(确保 session 存在) + pub async fn get_current_topic(&self, channel_name: &str, chat_id: &str) -> Result, AgentError> { + self.ensure_session(channel_name).await?; + if let Some(session) = self.get(channel_name).await { + let guard = session.lock().await; + Ok(guard.current_topic(chat_id).map(|s| s.to_string())) + } else { + Ok(None) + } + } + /// 更新最后活跃时间 pub async fn touch(&self, channel_name: &str) { self.lifecycle.touch(channel_name).await; diff --git a/src/gateway/tool_registry_factory.rs b/src/gateway/tool_registry_factory.rs index 0f984ce..2740e34 100644 --- a/src/gateway/tool_registry_factory.rs +++ b/src/gateway/tool_registry_factory.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use crate::config::TaskConfig; use crate::skills::SkillRuntime; -use crate::storage::{ConversationRepository, MemoryRepository, SchedulerJobRepository, SkillEventRepository}; +use crate::storage::{MemoryRepository, SchedulerJobRepository, SkillEventRepository}; use crate::tools::{ BashTool, CalculatorTool, FileEditTool, FileReadTool, FileWriteTool, HttpRequestTool, MemoryManageTool, MemorySearchTool, @@ -18,7 +18,6 @@ pub(crate) struct ToolRegistryFactory { scheduler_jobs: Arc, skill_events: Arc, session_message_sender: Arc, - conversations: Arc, known_agents: HashSet, default_timezone: String, disabled_tools: HashSet, @@ -33,7 +32,6 @@ impl ToolRegistryFactory { scheduler_jobs: Arc, skill_events: Arc, session_message_sender: Arc, - conversations: Arc, known_agents: HashSet, default_timezone: String, disabled_tools: HashSet, @@ -45,7 +43,6 @@ impl ToolRegistryFactory { scheduler_jobs, skill_events, session_message_sender, - conversations, known_agents, default_timezone, disabled_tools, diff --git a/src/tools/task/runtime.rs b/src/tools/task/runtime.rs index 3cea5fd..d0f1da6 100644 --- a/src/tools/task/runtime.rs +++ b/src/tools/task/runtime.rs @@ -13,7 +13,7 @@ use crate::tools::{ToolContext, ToolRegistry}; use super::error::TaskError; use super::prompt::{extract_summary, SubagentPromptBuilder}; use super::repository::TaskRepository; -use super::types::{SubagentType, TaskDefinition, TaskHandle, TaskSession, TaskSessionState, TaskToolResult}; +use super::types::{SubagentType, TaskDefinition, TaskSession, TaskToolResult}; /// 子代理运行时配置 #[derive(Debug, Clone)]