agentmux_srv_core/backend/storage/
skill_source.rs1use rusqlite::{params, OptionalExtension};
9
10use super::error::StoreError;
11use super::store::Store;
12
13const SCHEMA: &str = "CREATE TABLE IF NOT EXISTS db_skill_source (
14 skill_id TEXT PRIMARY KEY,
15 kind TEXT NOT NULL,
16 location TEXT NOT NULL,
17 git_ref TEXT NOT NULL DEFAULT '',
18 item_key TEXT NOT NULL DEFAULT '',
19 imported_at INTEGER NOT NULL DEFAULT 0
20 );";
21
22#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, ts_rs::TS)]
24#[ts(export, export_to = "../../../frontend/types/rpc/")]
25pub struct SkillSource {
26 pub kind: String,
28 pub location: String,
30 #[serde(default)]
32 pub git_ref: String,
33 #[serde(default)]
35 pub item_key: String,
36}
37
38impl Store {
39 pub fn skill_source_set(&self, skill_id: &str, src: &SkillSource, now_ms: i64) -> Result<(), StoreError> {
41 let conn = self.conn.lock().unwrap();
42 conn.execute_batch(SCHEMA)?;
43 conn.execute(
44 "INSERT INTO db_skill_source (skill_id, kind, location, git_ref, item_key, imported_at)
45 VALUES (?1, ?2, ?3, ?4, ?5, ?6)
46 ON CONFLICT(skill_id) DO UPDATE SET kind = excluded.kind, location = excluded.location,
47 git_ref = excluded.git_ref, item_key = excluded.item_key, imported_at = excluded.imported_at",
48 params![skill_id, src.kind, src.location, src.git_ref, src.item_key, now_ms],
49 )?;
50 Ok(())
51 }
52
53 pub fn skill_source_get(&self, skill_id: &str) -> Result<Option<SkillSource>, StoreError> {
55 let conn = self.conn.lock().unwrap();
56 conn.execute_batch(SCHEMA)?;
57 Ok(conn
58 .query_row(
59 "SELECT kind, location, git_ref, item_key FROM db_skill_source WHERE skill_id = ?1",
60 params![skill_id],
61 |r| Ok(SkillSource { kind: r.get(0)?, location: r.get(1)?, git_ref: r.get(2)?, item_key: r.get(3)? }),
62 )
63 .optional()?)
64 }
65
66 pub fn skill_source_map(&self) -> Result<std::collections::HashMap<String, SkillSource>, StoreError> {
68 let conn = self.conn.lock().unwrap();
69 conn.execute_batch(SCHEMA)?;
70 let mut stmt = conn.prepare("SELECT skill_id, kind, location, git_ref, item_key FROM db_skill_source")?;
71 let rows = stmt.query_map([], |r| {
72 Ok((r.get::<_, String>(0)?, SkillSource { kind: r.get(1)?, location: r.get(2)?, git_ref: r.get(3)?, item_key: r.get(4)? }))
73 })?;
74 Ok(rows.collect::<Result<std::collections::HashMap<_, _>, _>>()?)
75 }
76
77 pub fn skill_source_forget(&self, skill_id: &str) -> Result<(), StoreError> {
79 let conn = self.conn.lock().unwrap();
80 conn.execute_batch(SCHEMA)?;
81 conn.execute("DELETE FROM db_skill_source WHERE skill_id = ?1", params![skill_id])?;
82 Ok(())
83 }
84}
85
86#[cfg(test)]
87mod source_tests {
88 use super::*;
89
90 fn catalog() -> Store {
91 let s = Store::open_in_memory().unwrap();
92 s.apply_identity_schema_for_tests().unwrap();
93 s
94 }
95
96 #[test]
97 fn a_source_is_remembered_replaced_and_forgotten_per_skill() {
98 let c = catalog();
99 assert_eq!(c.skill_source_get("s1").unwrap(), None);
100 let a = SkillSource { kind: "git".into(), location: "https://example.com/r.git".into(), git_ref: "main".into(), item_key: "skills/a".into() };
101 c.skill_source_set("s1", &a, 1).unwrap();
102 assert_eq!(c.skill_source_get("s1").unwrap(), Some(a.clone()));
103 let b = SkillSource { kind: "url".into(), location: "https://example.com/SKILL.md".into(), git_ref: String::new(), item_key: String::new() };
104 c.skill_source_set("s1", &b, 2).unwrap();
105 c.skill_source_set("s2", &a, 2).unwrap();
106 assert_eq!(c.skill_source_get("s1").unwrap(), Some(b));
107 assert_eq!(c.skill_source_map().unwrap().len(), 2);
108 c.skill_source_forget("s1").unwrap();
109 assert_eq!(c.skill_source_get("s1").unwrap(), None);
110 assert_eq!(c.skill_source_get("s2").unwrap(), Some(a));
111 }
112}