From 62b38eac739344a428fdebda899da6b987f6f5a7 Mon Sep 17 00:00:00 2001 From: ooodc <549496103@qq.com> Date: Tue, 28 Apr 2026 12:08:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20SessionFactory=20?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=EF=BC=8C=E9=87=8D=E6=9E=84=20SessionPool=20?= =?UTF-8?q?=E4=BB=A5=E4=BC=98=E5=8C=96=E4=BC=9A=E8=AF=9D=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Copilot --- src/gateway/mod.rs | 1 + src/gateway/session.rs | 7 +++-- src/gateway/session_factory.rs | 56 ++++++++++++++++++++++++++++++++++ src/gateway/session_pool.rs | 40 +++++------------------- 4 files changed, 69 insertions(+), 35 deletions(-) create mode 100644 src/gateway/session_factory.rs diff --git a/src/gateway/mod.rs b/src/gateway/mod.rs index 1f98e5f..00c1991 100644 --- a/src/gateway/mod.rs +++ b/src/gateway/mod.rs @@ -5,6 +5,7 @@ pub mod memory_maintenance; pub mod processor; pub mod prompt; pub mod session; +pub mod session_factory; pub mod session_pool; pub mod ws; diff --git a/src/gateway/session.rs b/src/gateway/session.rs index 4a86d91..eccab60 100644 --- a/src/gateway/session.rs +++ b/src/gateway/session.rs @@ -32,6 +32,7 @@ use super::memory_maintenance::{ MemoryMaintenanceModelOutput, MemoryMaintenanceScopeResult, MemoryMaintenanceService, }; use super::prompt::load_agent_prompt; +use super::session_factory::SessionFactory; use super::session_pool::SessionPool; fn preview_text(content: &str, max_chars: usize) -> String { @@ -678,14 +679,14 @@ impl SessionManager { known_agents, default_timezone, )); - let session_pool = SessionPool::new( - session_ttl_hours, - agent_prompt_reinject_every, + let session_factory = SessionFactory::new( provider_config.clone(), tools.clone(), skills.clone(), store.clone(), + agent_prompt_reinject_every, ); + let session_pool = SessionPool::new(session_ttl_hours, session_factory); let cli_sessions = CliSessionService::new(store.clone()); Ok(Self { diff --git a/src/gateway/session_factory.rs b/src/gateway/session_factory.rs new file mode 100644 index 0000000..40a53ca --- /dev/null +++ b/src/gateway/session_factory.rs @@ -0,0 +1,56 @@ +use std::sync::Arc; + +use tokio::sync::mpsc; + +use crate::agent::AgentError; +use crate::config::LLMProviderConfig; +use crate::protocol::WsOutbound; +use crate::skills::SkillRuntime; +use crate::storage::SessionStore; +use crate::tools::ToolRegistry; + +use super::session::Session; + +#[derive(Clone)] +pub(crate) struct SessionFactory { + provider_config: LLMProviderConfig, + tools: Arc, + skills: Arc, + store: Arc, + agent_prompt_reinject_every: u64, +} + +impl SessionFactory { + pub(crate) fn new( + provider_config: LLMProviderConfig, + tools: Arc, + skills: Arc, + store: Arc, + agent_prompt_reinject_every: u64, + ) -> Self { + Self { + provider_config, + tools, + skills, + store, + agent_prompt_reinject_every, + } + } + + pub(crate) async fn create( + &self, + channel_name: impl Into, + user_tx: mpsc::Sender, + ) -> Result { + Session::new( + channel_name.into(), + self.provider_config.clone(), + user_tx, + self.tools.clone(), + self.skills.clone(), + self.store.clone(), + self.agent_prompt_reinject_every, + ) + .await + } +} diff --git a/src/gateway/session_pool.rs b/src/gateway/session_pool.rs index e0f0d1a..887ea95 100644 --- a/src/gateway/session_pool.rs +++ b/src/gateway/session_pool.rs @@ -5,22 +5,15 @@ use std::time::{Duration, Instant}; use tokio::sync::{Mutex, mpsc}; use crate::agent::AgentError; -use crate::config::LLMProviderConfig; use crate::protocol::WsOutbound; -use crate::skills::SkillRuntime; -use crate::storage::SessionStore; -use crate::tools::ToolRegistry; use super::session::Session; +use super::session_factory::SessionFactory; #[derive(Clone)] pub(crate) struct SessionPool { inner: Arc>, - provider_config: LLMProviderConfig, - tools: Arc, - skills: Arc, - store: Arc, - agent_prompt_reinject_every: u64, + session_factory: SessionFactory, } struct SessionPoolInner { @@ -30,25 +23,14 @@ struct SessionPoolInner { } impl SessionPool { - pub(crate) fn new( - session_ttl_hours: u64, - agent_prompt_reinject_every: u64, - provider_config: LLMProviderConfig, - tools: Arc, - skills: Arc, - store: Arc, - ) -> Self { + pub(crate) fn new(session_ttl_hours: u64, session_factory: SessionFactory) -> Self { Self { inner: Arc::new(Mutex::new(SessionPoolInner { sessions: HashMap::new(), session_timestamps: HashMap::new(), session_ttl: Duration::from_secs(session_ttl_hours * 3600), })), - provider_config, - tools, - skills, - store, - agent_prompt_reinject_every, + session_factory, } } @@ -74,16 +56,10 @@ impl SessionPool { inner.sessions.remove(channel_name); let (user_tx, _rx) = mpsc::channel::(100); - let session = Session::new( - channel_name.to_string(), - self.provider_config.clone(), - user_tx, - self.tools.clone(), - self.skills.clone(), - self.store.clone(), - self.agent_prompt_reinject_every, - ) - .await?; + let session = self + .session_factory + .create(channel_name.to_string(), user_tx) + .await?; inner .sessions