配置: - rustfmt.toml: 固化 max_width=100 / 4 空格缩进,cargo fmt 全量格式化 - Cargo.toml: 配置 [lints.rust] 与 [lints.clippy] 渐进式规则 - .github/workflows/ci.yml: Rust(fmt+clippy+test) + 前端(eslint+tsc+test) 双平台 CI - Makefile: 新增 check/fmt/fix 目标,clippy 对齐 --all-targets --all-features - web: eslint flat config + prettier 配置 + package.json 脚本与依赖 - src/main.rs: loop→while 修复 clippy::never_loop 对抗性审查发现并修复: - eslint 缺 caughtErrorsIgnorePattern 导致 catch(_) 误报为 error - 前端 lint 未接入 CI,现已补上 Lint 步骤 - Makefile 与 CI 的 clippy flags 不一致,已对齐
69 lines
2.1 KiB
Rust
69 lines
2.1 KiB
Rust
use std::sync::Arc;
|
||
|
||
use tokio::sync::Mutex;
|
||
|
||
use crate::agent::AgentError;
|
||
|
||
use super::session::Session;
|
||
use super::session_factory::SessionFactory;
|
||
use super::session_pool::SessionPool;
|
||
|
||
#[derive(Clone)]
|
||
pub(crate) struct SessionLifecycleService {
|
||
session_pool: SessionPool,
|
||
}
|
||
|
||
impl SessionLifecycleService {
|
||
pub(crate) fn new(session_factory: SessionFactory, session_ttl_hours: Option<u64>) -> Self {
|
||
Self {
|
||
session_pool: SessionPool::new(session_factory, session_ttl_hours),
|
||
}
|
||
}
|
||
|
||
pub(crate) async fn ensure_session(&self, channel_name: &str) -> Result<(), AgentError> {
|
||
self.session_pool.ensure_session(channel_name).await
|
||
}
|
||
|
||
pub(crate) async fn get(&self, channel_name: &str) -> Option<Arc<Mutex<Session>>> {
|
||
self.session_pool.get(channel_name).await
|
||
}
|
||
|
||
pub(crate) async fn touch(&self, channel_name: &str) {
|
||
self.session_pool.touch(channel_name).await;
|
||
}
|
||
|
||
/// 获取活跃的主 Session(用于用户消息)
|
||
pub(crate) async fn active_session(
|
||
&self,
|
||
channel_name: &str,
|
||
) -> Result<Arc<Mutex<Session>>, AgentError> {
|
||
self.ensure_session(channel_name).await?;
|
||
self.touch(channel_name).await;
|
||
self.get(channel_name)
|
||
.await
|
||
.ok_or_else(|| AgentError::Other("Session not found".to_string()))
|
||
}
|
||
|
||
/// 根据 chat_id 自动选择并获取 Session
|
||
/// - scheduler/ 开头:返回定时任务专用 Session
|
||
/// - 其他:返回主 Session
|
||
pub(crate) async fn active_session_for_chat_id(
|
||
&self,
|
||
channel_name: &str,
|
||
chat_id: &str,
|
||
) -> Result<Arc<Mutex<Session>>, AgentError> {
|
||
self.session_pool
|
||
.ensure_session_for_chat_id(channel_name, chat_id)
|
||
.await?;
|
||
self.touch(channel_name).await;
|
||
self.session_pool
|
||
.get_for_chat_id(channel_name, chat_id)
|
||
.await
|
||
.ok_or_else(|| AgentError::Other("Session not found".to_string()))
|
||
}
|
||
|
||
pub(crate) async fn cleanup_expired_sessions(&self) -> usize {
|
||
self.session_pool.cleanup_expired_sessions().await
|
||
}
|
||
}
|