The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
chore(modules): add tinyconnectors and tinyconnectors core modules
Steven Enamakelce512cf65c3e
diff --git a/crates/openhuman-core/src/modules/storage_import_tinyconnectors.rs b/crates/openhuman-core/src/modules/storage_import_tinyconnectors.rsnew file mode 100644index 0000000000..7f9fa4eb71--- /dev/null+++ b/crates/openhuman-core/src/modules/storage_import_tinyconnectors.rs@@ -0,0 +1,214 @@+//! One-time, read-only import of tinyconnectors' legacy state files into the+//! module's storage.+//!+//! Before the storage contract the module kept two things under+//! `<workspace>/state/`: the user's scope preferences as one JSON file per+//! toolkit (`composio-user-scopes/<toolkit>.json`, the bare preference value)+//! and the webhook trigger archive as one JSONL file per UTC day+//! (`triggers/<YYYY-MM-DD>.jsonl`). From v0.18.0 it keeps them in its own scope+//! of the host's storage, as `composio-user-scopes` documents and+//! `triggers/<day>` streams, and no longer reads the files, so an install+//! upgraded with preferences set (a denied write, say) must not lose them.+//!+//! * **Legacy files are never modified.** They are only read.+//! * **Nothing is overwritten.** A preference document that already exists is+//! kept, and a day's stream that already holds records is not appended to,+//! so a retry cannot duplicate and records the module wrote since are never+//! replaced.+//! * **Runs once per state directory.** A marker document (in a collection the+//! module's grant does not include) records it. An unreadable file writes no+//! marker, so the next start retries. A file whose content is not valid JSON+//! is skipped, as the module itself would have refused it.++use std::path::Path;++use anyhow::{anyhow, Context, Result};+use serde_json::{json, Value};+use tinystoragedrivers::{CollectionSpec, ScopedStorage};++use super::storage_grant_tinyconnectors::{+ MODULE_ID, PREFS_COLLECTION, TRIGGER_STREAM_PREFIX,+};+use super::storage_import_support as support;++const LOG_PREFIX: &str = "[connectors-import]";+const PREFS_DIR: &str = "composio-user-scopes";+const TRIGGERS_DIR: &str = "triggers";+/// Host-only: not in the module's grant, so the module cannot see it.+const MARKER_COLLECTION: &str = "tinyconnectors_host_import";++/// How one import attempt ended.+#[derive(Debug, PartialEq, Eq)]+pub(crate) enum Outcome {+ /// No storage backend is installed; the module is not using this storage.+ NoBackend,+ /// The marker says this state directory was already imported.+ AlreadyDone,+ /// Neither legacy directory exists.+ NoSource,+ /// The files were read and their records written.+ Imported {+ prefs: usize,+ triggers: usize,+ skipped: usize,+ },+}++/// Imports the legacy files under `state_dir` into the module's scope of the+/// installed backend.+pub(crate) async fn import_legacy(state_dir: &Path) -> Result<Outcome> {+ let Some(scoped) = support::module_scope(MODULE_ID)? else {+ return Ok(Outcome::NoBackend);+ };+ import_into(&scoped, state_dir).await+}++/// A preference file: the toolkit key and its bare JSON value.+struct LegacyPref {+ key: String,+ value: Value,+}++/// One day's archive: the stream it goes to and its parsed records.+struct LegacyDay {+ stream: String,+ records: Vec<Value>,+}++struct Legacy {+ prefs: Vec<LegacyPref>,+ days: Vec<LegacyDay>,+ skipped: usize,+}++async fn import_into(scoped: &ScopedStorage, state_dir: &Path) -> Result<Outcome> {+ let docs = scoped.documents();+ let marker = support::marker_id("connectors", state_dir);+ if support::already_imported(docs, MARKER_COLLECTION, &marker).await? {+ return Ok(Outcome::AlreadyDone);+ }+ let prefs_dir = state_dir.join(PREFS_DIR);+ let triggers_dir = state_dir.join(TRIGGERS_DIR);+ if !prefs_dir.is_dir() && !triggers_dir.is_dir() {+ return Ok(Outcome::NoSource);+ }++ let legacy = tokio::task::spawn_blocking(move || read_legacy(&prefs_dir, &triggers_dir))+ .await+ .context("the legacy reader panicked")??;++ docs.ensure_collection(&CollectionSpec::new(PREFS_COLLECTION))+ .await+ .map_err(|error| anyhow!("declare {PREFS_COLLECTION}: {:?}", error.kind()))?;+ let mut prefs = 0usize;+ for pref in legacy.prefs {+ let doc = json!({ "value": pref.value });+ if support::put_absent(docs, PREFS_COLLECTION, &pref.key, doc).await? {+ prefs += 1;+ }+ }+ let mut triggers = 0usize;+ for day in legacy.days {+ triggers += support::append_if_empty(scoped, &day.stream, day.records).await?;+ }++ support::mark_imported(+ docs,+ MARKER_COLLECTION,+ &marker,+ json!({ "prefs": prefs, "triggers": triggers, "skipped": legacy.skipped }),+ )+ .await?;+ tracing::info!(+ "{LOG_PREFIX} imported prefs={prefs} triggers={triggers} skipped={}",+ legacy.skipped+ );+ Ok(Outcome::Imported {+ prefs,+ triggers,+ skipped: legacy.skipped,+ })+}++/// The files of `dir` with `extension`, sorted by name; a missing directory has+/// none.+fn files_with_extension(dir: &Path, extension: &str) -> Result<Vec<std::path::PathBuf>> {+ let entries = match std::fs::read_dir(dir) {+ Ok(entries) => entries,+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),+ Err(error) => return Err(error).with_context(|| format!("list {}", dir.display())),+ };+ let mut files = Vec::new();+ for entry in entries {+ let path = entry.with_context(|| format!("list {}", dir.display()))?.path();+ if path.is_file() && path.extension().is_some_and(|found| found == extension) {+ files.push(path);+ }+ }+ files.sort();+ Ok(files)+}++fn read_legacy(prefs_dir: &Path, triggers_dir: &Path) -> Result<Legacy> {+ let mut skipped = 0usize;+ let mut prefs = Vec::new();+ for path in files_with_extension(prefs_dir, "json")? {+ let Some(key) = path.file_stem().and_then(|stem| stem.to_str()) else {+ skipped += 1;+ continue;+ };+ let text = std::fs::read_to_string(&path)+ .with_context(|| format!("read {}", path.display()))?;+ match serde_json::from_str::<Value>(&text) {+ Ok(value) => prefs.push(LegacyPref {+ key: key.to_owned(),+ value,+ }),+ Err(_) => {+ tracing::warn!("{LOG_PREFIX} skipped a preference file that is not valid JSON");+ skipped += 1;+ }+ }+ }+ let mut days = Vec::new();+ for path in files_with_extension(triggers_dir, "jsonl")? {+ let Some(day) = path+ .file_stem()+ .and_then(|stem| stem.to_str())+ .filter(|stem| is_day(stem))+ else {+ skipped += 1;+ continue;+ };+ let text = std::fs::read_to_string(&path)+ .with_context(|| format!("read {}", path.display()))?;+ let (records, bad) = support::parse_jsonl(&text);+ skipped += bad;+ days.push(LegacyDay {+ stream: format!("{TRIGGER_STREAM_PREFIX}{day}"),+ records,+ });+ }+ Ok(Legacy {+ prefs,+ days,+ skipped,+ })+}++/// `YYYY-MM-DD`, the only name the archive ever wrote.+fn is_day(name: &str) -> bool {+ let bytes = name.as_bytes();+ bytes.len() == 10+ && bytes+ .iter()+ .enumerate()+ .all(|(index, byte)| match index {+ 4 | 7 => *byte == b'-',+ _ => byte.is_ascii_digit(),+ })+}++#[cfg(test)]+#[path = "storage_import_tinyconnectors_tests.rs"]+mod tests;diff --git a/crates/openhuman-core/src/modules/storage_import_tinywallet.rs b/crates/openhuman-core/src/modules/storage_import_tinywallet.rsnew file mode 100644index 0000000000..a615833920--- /dev/null+++ b/crates/openhuman-core/src/modules/storage_import_tinywallet.rs@@ -0,0 +1,93 @@+//! One-time, read-only import of the x402 payments ledger into the wallet+//! module's storage.+//!+//! Before the storage contract the module's ledger kept every payment attempt+//! as one JSON line in `<workspace>/x402/payments.jsonl`; a payment that+//! changed status was appended again with the same id. From v0.22.0 the same+//! records live in the `x402.payments` stream of the module's own scope, and+//! the module no longer reads the file. Dropping it would forget payments that+//! count against the daily and monthly budget, so an upgraded install would+//! silently get its spending allowance back.+//!+//! * **The legacy file is never modified.** It is only read.+//! * **Nothing is duplicated.** The records are appended in one batch and only+//! to an empty stream, so a retry after a failed attempt cannot repeat them+//! and records the module wrote since are never preceded by old ones.+//! * **Runs once per workspace.** A marker document (in a collection the+//! module's grant does not include) records it. An unreadable file writes no+//! marker, so the next start retries; a line that does not parse is skipped.+//! * **What is not carried over.** Budget holds. The legacy ledger held+//! reservations only in memory, so there is nothing on disk to import; the+//! `x402_budget` document starts empty.++use std::path::Path;++use anyhow::{anyhow, Context, Result};+use serde_json::json;+use tinystoragedrivers::ScopedStorage;++use super::storage_grant_tinywallet::{MODULE_ID, PAYMENTS_STREAM};+use super::storage_import_support as support;++const LOG_PREFIX: &str = "[x402-import]";+const LEDGER_FILE: &str = "x402/payments.jsonl";+/// Host-only: not in the module's grant, so the module cannot see it.+const MARKER_COLLECTION: &str = "tinywallet_host_import";++/// How one import attempt ended.+#[derive(Debug, PartialEq, Eq)]+pub(crate) enum Outcome {+ /// No storage backend is installed; the module is not using this storage.+ NoBackend,+ /// The marker says this workspace was already imported.+ AlreadyDone,+ /// There is no legacy ledger file.+ NoSource,+ /// The file was read and its records written.+ Imported { payments: usize, skipped: usize },+}++/// Imports the legacy ledger under `workspace_dir` into the module's scope of+/// the installed backend.+pub(crate) async fn import_legacy(workspace_dir: &Path) -> Result<Outcome> {+ let Some(scoped) = support::module_scope(MODULE_ID)? else {+ return Ok(Outcome::NoBackend);+ };+ import_into(&scoped, workspace_dir).await+}++async fn import_into(scoped: &ScopedStorage, workspace_dir: &Path) -> Result<Outcome> {+ let docs = scoped.documents();+ let marker = support::marker_id("x402", workspace_dir);+ if support::already_imported(docs, MARKER_COLLECTION, &marker).await? {+ return Ok(Outcome::AlreadyDone);+ }+ let ledger = workspace_dir.join(LEDGER_FILE);+ if !ledger.is_file() {+ return Ok(Outcome::NoSource);+ }+ let (records, skipped) = tokio::task::spawn_blocking(move || -> Result<_> {+ let text = std::fs::read_to_string(&ledger)+ .with_context(|| format!("read {}", ledger.display()))?;+ Ok(support::parse_jsonl(&text))+ })+ .await+ .context("the legacy reader panicked")??;++ let payments = support::append_if_empty(scoped, PAYMENTS_STREAM, records)+ .await+ .map_err(|error| anyhow!("import the payments: {error:#}"))?;+ support::mark_imported(+ docs,+ MARKER_COLLECTION,+ &marker,+ json!({ "payments": payments, "skipped": skipped }),+ )+ .await?;+ tracing::info!("{LOG_PREFIX} imported payments={payments} skipped={skipped}");+ Ok(Outcome::Imported { payments, skipped })+}++#[cfg(test)]+#[path = "storage_import_tinywallet_tests.rs"]+mod tests;