1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//! The SQLite [`DriverStore`](crate::drivers::DriverStore) (spec 3.2 §7.7, §13 Q8; features
//! `drivers`, `sqlite`).
use super::driver_sql::{REGISTRY_TABLE, sql_driver_store};
use crate::error::{KernelError, Result};
/// Migrations of [`SqliteDriverStore`], recorded in `_adk_kernel_driver_migrations`.
const MIGRATIONS: &[(i64, &str, &str)] = &[(
1,
"create kernel driver and parked-call tables",
"\
CREATE TABLE IF NOT EXISTS adk_kernel_drivers (\
id TEXT PRIMARY KEY, \
record TEXT NOT NULL\
);\
CREATE TABLE IF NOT EXISTS adk_kernel_driver_parks (\
app_name TEXT NOT NULL, \
user_id TEXT NOT NULL, \
session_id TEXT NOT NULL, \
call_id TEXT NOT NULL, \
driver_id TEXT NOT NULL, \
park TEXT NOT NULL, \
PRIMARY KEY (app_name, user_id, session_id, call_id)\
);\
CREATE INDEX IF NOT EXISTS adk_kernel_driver_parks_driver ON adk_kernel_driver_parks (driver_id);",
)];
/// A [`DriverStore`](crate::drivers::DriverStore) in SQLite (spec 3.2 §7.7, features
/// `drivers`, `sqlite`).
///
/// In the CLI it lives in `harness.db` on the same pool as the harness and process stores:
/// its tables are `adk_kernel_drivers` and `adk_kernel_driver_parks`, and
/// [`migrate`](Self::migrate) records its steps in `_adk_kernel_driver_migrations`. Call
/// `migrate` once before use.
///
/// # Example
///
/// ```rust
/// use adk_kernel::drivers::{DriverManifest, DriverRecord, DriverStore};
/// use adk_kernel::stores::SqliteDriverStore;
///
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> adk_kernel::Result<()> {
/// let pool = sqlx::SqlitePool::connect("sqlite::memory:").await.unwrap();
/// let store = SqliteDriverStore::new(pool);
/// store.migrate().await?;
/// let manifest = DriverManifest::from_toml("id = \"fs\"\ncommand = \"mcp-fs\"\n")?;
/// store.put(DriverRecord::new(manifest, "alice", chrono::Utc::now())).await?;
/// assert!(store.get("fs").await?.is_some());
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct SqliteDriverStore {
pool: sqlx::SqlitePool,
}
impl SqliteDriverStore {
/// A store over `pool`, such as the pool of `harness.db`.
pub fn new(pool: sqlx::SqlitePool) -> Self {
Self { pool }
}
/// The pool the store writes to.
pub fn pool(&self) -> &sqlx::SqlitePool {
&self.pool
}
/// Creates or upgrades the store's tables. Idempotent.
///
/// # Errors
///
/// Returns [`KernelError::Store`] when a statement fails, or when the database was
/// migrated by a newer release.
pub async fn migrate(&self) -> Result<()> {
adk_session::migration::sqlite_runner::run_sql_migrations(
&self.pool,
REGISTRY_TABLE,
MIGRATIONS,
|| async { Ok(false) },
)
.await
.map_err(|error| KernelError::Store { backend: "sqlite", reason: error.message })
}
}
sql_driver_store!(SqliteDriverStore, "sqlite");