feat: 重构会话管理逻辑,添加获取当前话题的方法,简化命令处理中的会话获取逻辑
This commit is contained in:
parent
3591822145
commit
831832664d
@ -107,24 +107,4 @@ async fn handle_switch_session(
|
|||||||
.with_metadata("topic_id", &topic.id)
|
.with_metadata("topic_id", &topic.id)
|
||||||
.with_metadata("title", &topic.title)
|
.with_metadata("title", &topic.title)
|
||||||
.with_metadata("message_count", &topic.message_count.to_string()))
|
.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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -20,6 +20,7 @@ impl CliSessionService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 创建指定通道的会话
|
/// 创建指定通道的会话
|
||||||
|
#[allow(dead_code)]
|
||||||
pub(crate) fn create_with_channel(
|
pub(crate) fn create_with_channel(
|
||||||
&self,
|
&self,
|
||||||
channel_name: &str,
|
channel_name: &str,
|
||||||
@ -30,12 +31,14 @@ impl CliSessionService {
|
|||||||
.map_err(|err| AgentError::Other(format!("create session error: {}", err)))
|
.map_err(|err| AgentError::Other(format!("create session error: {}", err)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub(crate) fn get(&self, session_id: &str) -> Result<Option<SessionRecord>, AgentError> {
|
pub(crate) fn get(&self, session_id: &str) -> Result<Option<SessionRecord>, AgentError> {
|
||||||
self.store
|
self.store
|
||||||
.get_session(session_id)
|
.get_session(session_id)
|
||||||
.map_err(|err| AgentError::Other(format!("get session error: {}", err)))
|
.map_err(|err| AgentError::Other(format!("get session error: {}", err)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub(crate) fn list(&self, include_archived: bool) -> Result<Vec<SessionRecord>, AgentError> {
|
pub(crate) fn list(&self, include_archived: bool) -> Result<Vec<SessionRecord>, AgentError> {
|
||||||
self.store
|
self.store
|
||||||
.list_sessions("cli", include_archived)
|
.list_sessions("cli", include_archived)
|
||||||
@ -43,6 +46,7 @@ impl CliSessionService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 列出指定通道的会话
|
/// 列出指定通道的会话
|
||||||
|
#[allow(dead_code)]
|
||||||
pub(crate) fn list_by_channel(
|
pub(crate) fn list_by_channel(
|
||||||
&self,
|
&self,
|
||||||
channel_name: &str,
|
channel_name: &str,
|
||||||
@ -53,24 +57,28 @@ impl CliSessionService {
|
|||||||
.map_err(|err| AgentError::Other(format!("list sessions error: {}", err)))
|
.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> {
|
pub(crate) fn rename(&self, session_id: &str, title: &str) -> Result<(), AgentError> {
|
||||||
self.store
|
self.store
|
||||||
.rename_session(session_id, title)
|
.rename_session(session_id, title)
|
||||||
.map_err(|err| AgentError::Other(format!("rename session error: {}", err)))
|
.map_err(|err| AgentError::Other(format!("rename session error: {}", err)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub(crate) fn archive(&self, session_id: &str) -> Result<(), AgentError> {
|
pub(crate) fn archive(&self, session_id: &str) -> Result<(), AgentError> {
|
||||||
self.store
|
self.store
|
||||||
.archive_session(session_id)
|
.archive_session(session_id)
|
||||||
.map_err(|err| AgentError::Other(format!("archive session error: {}", err)))
|
.map_err(|err| AgentError::Other(format!("archive session error: {}", err)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub(crate) fn delete(&self, session_id: &str) -> Result<(), AgentError> {
|
pub(crate) fn delete(&self, session_id: &str) -> Result<(), AgentError> {
|
||||||
self.store
|
self.store
|
||||||
.delete_session(session_id)
|
.delete_session(session_id)
|
||||||
.map_err(|err| AgentError::Other(format!("delete session error: {}", err)))
|
.map_err(|err| AgentError::Other(format!("delete session error: {}", err)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub(crate) fn clear_messages(&self, session_id: &str) -> Result<(), AgentError> {
|
pub(crate) fn clear_messages(&self, session_id: &str) -> Result<(), AgentError> {
|
||||||
self.store
|
self.store
|
||||||
.clear_messages(session_id)
|
.clear_messages(session_id)
|
||||||
|
|||||||
@ -145,13 +145,10 @@ impl InboundProcessor {
|
|||||||
// 计算正确的 session_id(根据 channel_name 和 chat_id)
|
// 计算正确的 session_id(根据 channel_name 和 chat_id)
|
||||||
let session_id = persistent_session_id(&inbound.channel, &inbound.chat_id);
|
let session_id = persistent_session_id(&inbound.channel, &inbound.chat_id);
|
||||||
|
|
||||||
// 获取当前话题(如果有 Session)
|
// 获取当前话题(封装了 session 创建逻辑)
|
||||||
let current_topic = if let Some(session) = self.session_manager.get(&inbound.channel).await {
|
let current_topic = self.session_manager
|
||||||
let guard = session.lock().await;
|
.get_current_topic(&inbound.channel, &inbound.chat_id)
|
||||||
guard.current_topic(&inbound.chat_id).map(|s| s.to_string())
|
.await?;
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
// 使用 ChannelInputAdapter 尝试解析命令
|
// 使用 ChannelInputAdapter 尝试解析命令
|
||||||
let adapter = ChannelInputAdapter::new();
|
let adapter = ChannelInputAdapter::new();
|
||||||
|
|||||||
@ -11,7 +11,7 @@ use crate::storage::{
|
|||||||
};
|
};
|
||||||
use crate::tools::{
|
use crate::tools::{
|
||||||
DefaultSubAgentRuntime, InMemoryTaskRepository, NoopSessionMessageSender,
|
DefaultSubAgentRuntime, InMemoryTaskRepository, NoopSessionMessageSender,
|
||||||
SessionMessageSender, SubAgentRuntime, SubAgentRuntimeConfig, ToolRegistry,
|
SessionMessageSender, SubAgentRuntimeConfig, ToolRegistry,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::agent_factory::AgentFactory;
|
use super::agent_factory::AgentFactory;
|
||||||
@ -89,7 +89,6 @@ pub(crate) fn build_session_manager_with_sender(
|
|||||||
scheduler_jobs,
|
scheduler_jobs,
|
||||||
skill_events.clone(),
|
skill_events.clone(),
|
||||||
session_message_sender.clone(),
|
session_message_sender.clone(),
|
||||||
conversations.clone(),
|
|
||||||
known_agents,
|
known_agents,
|
||||||
default_timezone,
|
default_timezone,
|
||||||
disabled_tools,
|
disabled_tools,
|
||||||
|
|||||||
@ -547,6 +547,17 @@ impl SessionManager {
|
|||||||
self.lifecycle.get(channel_name).await
|
self.lifecycle.get(channel_name).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 获取指定 chat 的当前话题(确保 session 存在)
|
||||||
|
pub async fn get_current_topic(&self, channel_name: &str, chat_id: &str) -> Result<Option<String>, 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) {
|
pub async fn touch(&self, channel_name: &str) {
|
||||||
self.lifecycle.touch(channel_name).await;
|
self.lifecycle.touch(channel_name).await;
|
||||||
|
|||||||
@ -3,7 +3,7 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
use crate::config::TaskConfig;
|
use crate::config::TaskConfig;
|
||||||
use crate::skills::SkillRuntime;
|
use crate::skills::SkillRuntime;
|
||||||
use crate::storage::{ConversationRepository, MemoryRepository, SchedulerJobRepository, SkillEventRepository};
|
use crate::storage::{MemoryRepository, SchedulerJobRepository, SkillEventRepository};
|
||||||
use crate::tools::{
|
use crate::tools::{
|
||||||
BashTool, CalculatorTool, FileEditTool, FileReadTool, FileWriteTool,
|
BashTool, CalculatorTool, FileEditTool, FileReadTool, FileWriteTool,
|
||||||
HttpRequestTool, MemoryManageTool, MemorySearchTool,
|
HttpRequestTool, MemoryManageTool, MemorySearchTool,
|
||||||
@ -18,7 +18,6 @@ pub(crate) struct ToolRegistryFactory {
|
|||||||
scheduler_jobs: Arc<dyn SchedulerJobRepository>,
|
scheduler_jobs: Arc<dyn SchedulerJobRepository>,
|
||||||
skill_events: Arc<dyn SkillEventRepository>,
|
skill_events: Arc<dyn SkillEventRepository>,
|
||||||
session_message_sender: Arc<dyn SessionMessageSender>,
|
session_message_sender: Arc<dyn SessionMessageSender>,
|
||||||
conversations: Arc<dyn ConversationRepository>,
|
|
||||||
known_agents: HashSet<String>,
|
known_agents: HashSet<String>,
|
||||||
default_timezone: String,
|
default_timezone: String,
|
||||||
disabled_tools: HashSet<String>,
|
disabled_tools: HashSet<String>,
|
||||||
@ -33,7 +32,6 @@ impl ToolRegistryFactory {
|
|||||||
scheduler_jobs: Arc<dyn SchedulerJobRepository>,
|
scheduler_jobs: Arc<dyn SchedulerJobRepository>,
|
||||||
skill_events: Arc<dyn SkillEventRepository>,
|
skill_events: Arc<dyn SkillEventRepository>,
|
||||||
session_message_sender: Arc<dyn SessionMessageSender>,
|
session_message_sender: Arc<dyn SessionMessageSender>,
|
||||||
conversations: Arc<dyn ConversationRepository>,
|
|
||||||
known_agents: HashSet<String>,
|
known_agents: HashSet<String>,
|
||||||
default_timezone: String,
|
default_timezone: String,
|
||||||
disabled_tools: HashSet<String>,
|
disabled_tools: HashSet<String>,
|
||||||
@ -45,7 +43,6 @@ impl ToolRegistryFactory {
|
|||||||
scheduler_jobs,
|
scheduler_jobs,
|
||||||
skill_events,
|
skill_events,
|
||||||
session_message_sender,
|
session_message_sender,
|
||||||
conversations,
|
|
||||||
known_agents,
|
known_agents,
|
||||||
default_timezone,
|
default_timezone,
|
||||||
disabled_tools,
|
disabled_tools,
|
||||||
|
|||||||
@ -13,7 +13,7 @@ use crate::tools::{ToolContext, ToolRegistry};
|
|||||||
use super::error::TaskError;
|
use super::error::TaskError;
|
||||||
use super::prompt::{extract_summary, SubagentPromptBuilder};
|
use super::prompt::{extract_summary, SubagentPromptBuilder};
|
||||||
use super::repository::TaskRepository;
|
use super::repository::TaskRepository;
|
||||||
use super::types::{SubagentType, TaskDefinition, TaskHandle, TaskSession, TaskSessionState, TaskToolResult};
|
use super::types::{SubagentType, TaskDefinition, TaskSession, TaskToolResult};
|
||||||
|
|
||||||
/// 子代理运行时配置
|
/// 子代理运行时配置
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user