feat(saas): extend agent templates with soul_content, add /available endpoint, key pool cleanup, and industry seed templates

- Add 9 extended fields to AgentTemplateInfo: soul_content, scenarios,
  welcome_message, quick_commands, personality, communication_style,
  emoji, version, source_id
- Refactor service.rs to use sqlx::Row (manual column extraction) to
  avoid the 16-element tuple FromRow limit
- Add /api/v1/agent-templates/available (lightweight public listing)
  and /api/v1/agent-templates/:id/full endpoints
- Add 24h key_usage_window cleanup task in scheduler
- Update seed data with extended fields for all 5 existing templates
  plus new Medical Assistant template (healthcare category)
This commit is contained in:
iven
2026-03-31 02:58:09 +08:00
parent eb956d0dce
commit 3e57fadfc9
7 changed files with 340 additions and 59 deletions

View File

@@ -74,6 +74,9 @@ pub fn start_scheduler(config: &SchedulerConfig, db: PgPool, dispatcher: WorkerD
/// 内置的 DB 清理任务(不通过 Worker直接执行 SQL
pub fn start_db_cleanup_tasks(db: PgPool) {
let db_devices = db.clone();
let db_key_pool = db.clone();
// 每 24 小时清理不活跃设备
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_secs(86400));
@@ -86,7 +89,7 @@ pub fn start_db_cleanup_tasks(db: PgPool) {
let cutoff = (chrono::Utc::now() - chrono::Duration::days(90)).to_rfc3339();
cutoff
})
.execute(&db)
.execute(&db_devices)
.await
{
Ok(result) => {
@@ -98,6 +101,27 @@ pub fn start_db_cleanup_tasks(db: PgPool) {
}
}
});
// 每 24 小时清理过期的 key_usage_window 记录
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_secs(86400));
loop {
interval.tick().await;
match sqlx::query(
"DELETE FROM key_usage_window WHERE window_minute < to_char(NOW() - INTERVAL '24 hours', 'YYYY-MM-DD\"T\"HH24:MI')"
)
.execute(&db_key_pool)
.await
{
Ok(result) => {
if result.rows_affected() > 0 {
tracing::info!("Cleaned up {} expired key_usage_window records (24h)", result.rows_affected());
}
}
Err(e) => tracing::error!("Key usage window cleanup failed: {}", e),
}
}
});
}
/// 启动用户定时任务调度循环