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 prompt;
pub mod session;
pub mod session_factory;
pub mod session_pool;
pub mod ws;

View File

@ -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 {

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 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<Mutex<SessionPoolInner>>,
provider_config: LLMProviderConfig,
tools: Arc<ToolRegistry>,
skills: Arc<SkillRuntime>,
store: Arc<SessionStore>,
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<ToolRegistry>,
skills: Arc<SkillRuntime>,
store: Arc<SessionStore>,
) -> 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,15 +56,9 @@ impl SessionPool {
inner.sessions.remove(channel_name);
let (user_tx, _rx) = mpsc::channel::<WsOutbound>(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,
)
let session = self
.session_factory
.create(channel_name.to_string(), user_tx)
.await?;
inner