1use std::sync::{Arc, Mutex, OnceLock};
19use std::time::Duration;
20
21use async_trait::async_trait;
22
23use crate::core::learn::{self, SkillCurator, SuccessfulRun};
24use crate::prelude::*;
25
26static SUCCESS_POOL: OnceLock<Arc<Mutex<Vec<SuccessfulRun>>>> = OnceLock::new();
28
29pub fn success_pool() -> &'static Arc<Mutex<Vec<SuccessfulRun>>> {
31 SUCCESS_POOL.get_or_init(|| Arc::new(Mutex::new(Vec::new())))
32}
33
34pub struct AgentPlugin {
36 handles: Mutex<Vec<tokio::task::JoinHandle<()>>>,
38}
39
40impl AgentPlugin {
41 pub fn new() -> Self {
42 Self {
43 handles: Mutex::new(Vec::new()),
44 }
45 }
46}
47
48impl Default for AgentPlugin {
49 fn default() -> Self {
50 Self::new()
51 }
52}
53
54#[async_trait]
55impl Plugin for AgentPlugin {
56 fn name(&self) -> &str {
57 "agent"
58 }
59
60 fn priority(&self) -> i32 {
61 80
62 }
63
64 async fn start(&self) -> Result<()> {
65 success_pool();
67
68 let curator = SkillCurator::shared();
70 learn::register_default_tasks(curator, success_pool().clone());
71 let mut slot = self.handles.lock().unwrap();
73 for h in slot.drain(..) {
74 h.abort();
75 }
76 *slot = learn::scheduler().start();
77
78 tracing::info!(
79 "agent 插件启动:技能沉淀任务已注册(skill-distill,间隔 {}s)",
80 Duration::from_secs(600).as_secs()
81 );
82 Ok(())
83 }
84
85 async fn stop(&self) -> Result<()> {
86 for h in self.handles.lock().unwrap().drain(..) {
87 h.abort();
88 }
89 tracing::info!("agent 插件停止:后台任务已中止");
90 Ok(())
91 }
92}