agentmux_srv_core/backend/
builtin_skills.rs1use std::sync::Arc;
20
21use super::storage::store::Store;
22use super::storage::{Skill, StoreError};
23
24pub struct Builtin {
26 pub id: &'static str,
28 pub name: &'static str,
29 pub trigger: &'static str,
30 pub description: &'static str,
31 pub content: &'static str,
33}
34
35const SKILL_TYPE: &str = crate::backend::agent_config::SKILL_TYPE_AGENT_SKILL;
37
38pub const BUILTINS: &[Builtin] = &[Builtin {
39 id: "7c1e5d2a-3b64-4f0e-9a58-0d15c4b0d1a2",
40 name: "disk-cleanup",
41 trigger: "disk-cleanup",
42 description: "Free disk space safely: measure AgentMux's own leftovers (old version folders, dev homes, instances, caches), say what is safe to remove, and ask the user before deleting anything.",
43 content: include_str!("builtin_skills/disk-cleanup.md"),
44}];
45
46fn seed_key(id: &str) -> String {
48 format!("skill:{id}")
49}
50
51pub fn seed_on_startup(store: &Arc<Store>) {
53 let now = agentmux_common::time::now_ms();
54 let seeded = seed(store, BUILTINS, now);
55 if seeded > 0 {
56 tracing::info!(seeded, "built-in skills: seeded");
57 }
58}
59
60pub(crate) fn seed(store: &Store, list: &[Builtin], now_ms: i64) -> usize {
62 let mut created = 0;
63 for b in list {
64 let key = seed_key(b.id);
65 match store.builtin_seed_done(&key) {
66 Ok(true) => continue,
67 Ok(false) => {}
68 Err(e) => {
69 tracing::warn!(skill = b.name, "built-in skills: could not read the seed record: {e}");
70 continue;
71 }
72 }
73 match seed_one(store, b, now_ms) {
74 Ok(made) => {
75 if let Err(e) = store.builtin_seed_mark(&key, now_ms) {
76 tracing::warn!(skill = b.name, "built-in skills: could not record the seed: {e}");
77 continue;
78 }
79 created += usize::from(made);
80 }
81 Err(e) => tracing::warn!(skill = b.name, "built-in skills: not seeded, will retry at the next start: {e}"),
82 }
83 }
84 created
85}
86
87fn seed_one(store: &Store, b: &Builtin, now_ms: i64) -> Result<bool, StoreError> {
90 if store.skill_get(b.id)?.is_some() {
91 if store.skill_reach_map()?.contains_key(b.id) {
94 return Ok(false);
95 }
96 store.skill_set_reach(b.id, true, true, now_ms)?;
97 return Ok(true);
98 }
99 let skill = Skill {
100 id: b.id.to_string(),
101 name: b.name.to_string(),
102 trigger: b.trigger.to_string(),
103 skill_type: SKILL_TYPE.to_string(),
104 description: b.description.to_string(),
105 content: b.content.to_string(),
106 is_global: true,
107 created_at: now_ms,
108 updated_at: now_ms,
109 };
110 match store.skill_upsert_unique_global(&skill) {
111 Ok(()) => {}
112 Err(StoreError::Other(msg)) if msg.contains("already exists") => {
113 tracing::info!(skill = b.name, "built-in skills: a global skill with this name exists; leaving it");
114 return Ok(false);
115 }
116 Err(e) => return Err(e),
117 }
118 store.skill_set_reach(b.id, true, true, now_ms)?;
119 Ok(true)
120}
121
122#[cfg(test)]
123#[path = "builtin_skills_tests.rs"]
124mod tests;