feat: 添加 SessionFactory 模块,重构 SessionPool 以优化会话创建逻辑

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
ooodc 2026-04-28 12:08:34 +08:00
parent 65bcf34b75
commit 62b38eac73
4 changed files with 69 additions and 35 deletions

View File

@ -5,6 +5,7 @@ pub mod memory_maintenance;
pub mod processor; pub mod processor;
pub mod prompt; pub mod prompt;
pub mod session; pub mod session;
pub mod session_factory;
pub mod session_pool; pub mod session_pool;
pub mod ws; pub mod ws;

View File

@ -32,6 +32,7 @@ use super::memory_maintenance::{
MemoryMaintenanceModelOutput, MemoryMaintenanceScopeResult, MemoryMaintenanceService, MemoryMaintenanceModelOutput, MemoryMaintenanceScopeResult, MemoryMaintenanceService,
}; };
use super::prompt::load_agent_prompt; use super::prompt::load_agent_prompt;
use super::session_factory::SessionFactory;
use super::session_pool::SessionPool; use super::session_pool::SessionPool;
fn preview_text(content: &str, max_chars: usize) -> String { fn preview_text(content: &str, max_chars: usize) -> String {
@ -678,14 +679,14 @@ impl SessionManager {
known_agents, known_agents,
default_timezone, default_timezone,
)); ));
let session_pool = SessionPool::new( let session_factory = SessionFactory::new(
session_ttl_hours,
agent_prompt_reinject_every,
provider_config.clone(), provider_config.clone(),
tools.clone(), tools.clone(),
skills.clone(), skills.clone(),
store.clone(), store.clone(),
agent_prompt_reinject_every,
); );
let session_pool = SessionPool::new(session_ttl_hours, session_factory);
let cli_sessions = CliSessionService::new(store.clone()); let cli_sessions = CliSessionService::new(store.clone());
Ok(Self { Ok(Self {

View File

@ -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<ToolRegistry>,
skills: Arc<SkillRuntime>,
store: Arc<SessionStore>,
agent_prompt_reinject_every: u64,
}
impl SessionFactory {
pub(crate) fn new(
provider_config: LLMProviderConfig,
tools: Arc<ToolRegistry>,
skills: Arc<SkillRuntime>,
store: Arc<SessionStore>,
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<String>,
user_tx: mpsc::Sender<WsOutbound>,
) -> Result<Session, AgentError> {
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
}
}

View File

@ -5,22 +5,15 @@ use std::time::{Duration, Instant};
use tokio::sync::{Mutex, mpsc}; use tokio::sync::{Mutex, mpsc};
use crate::agent::AgentError; use crate::agent::AgentError;
use crate::config::LLMProviderConfig;
use crate::protocol::WsOutbound; use crate::protocol::WsOutbound;
use crate::skills::SkillRuntime;
use crate::storage::SessionStore;
use crate::tools::ToolRegistry;
use super::session::Session; use super::session::Session;
use super::session_factory::SessionFactory;
#[derive(Clone)] #[derive(Clone)]
pub(crate) struct SessionPool { pub(crate) struct SessionPool {
inner: Arc<Mutex<SessionPoolInner>>, inner: Arc<Mutex<SessionPoolInner>>,
provider_config: LLMProviderConfig, session_factory: SessionFactory,
tools: Arc<ToolRegistry>,
skills: Arc<SkillRuntime>,
store: Arc<SessionStore>,
agent_prompt_reinject_every: u64,
} }
struct SessionPoolInner { struct SessionPoolInner {
@ -30,25 +23,14 @@ struct SessionPoolInner {
} }
impl SessionPool { impl SessionPool {
pub(crate) fn new( pub(crate) fn new(session_ttl_hours: u64, session_factory: SessionFactory) -> Self {
session_ttl_hours: u64,
agent_prompt_reinject_every: u64,
provider_config: LLMProviderConfig,
tools: Arc<ToolRegistry>,
skills: Arc<SkillRuntime>,
store: Arc<SessionStore>,
) -> Self {
Self { Self {
inner: Arc::new(Mutex::new(SessionPoolInner { inner: Arc::new(Mutex::new(SessionPoolInner {
sessions: HashMap::new(), sessions: HashMap::new(),
session_timestamps: HashMap::new(), session_timestamps: HashMap::new(),
session_ttl: Duration::from_secs(session_ttl_hours * 3600), session_ttl: Duration::from_secs(session_ttl_hours * 3600),
})), })),
provider_config, session_factory,
tools,
skills,
store,
agent_prompt_reinject_every,
} }
} }
@ -74,16 +56,10 @@ impl SessionPool {
inner.sessions.remove(channel_name); inner.sessions.remove(channel_name);
let (user_tx, _rx) = mpsc::channel::<WsOutbound>(100); let (user_tx, _rx) = mpsc::channel::<WsOutbound>(100);
let session = Session::new( let session = self
channel_name.to_string(), .session_factory
self.provider_config.clone(), .create(channel_name.to_string(), user_tx)
user_tx, .await?;
self.tools.clone(),
self.skills.clone(),
self.store.clone(),
self.agent_prompt_reinject_every,
)
.await?;
inner inner
.sessions .sessions