PicoBot/src/gateway/session_lifecycle.rs
oudecheng cda14360af chore: 建立工程化基线(rustfmt + clippy + CI + eslint + prettier)
配置:
- 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 不一致,已对齐
2026-08-03 23:24:02 +08:00

69 lines
2.1 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}
}