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: files changed crates/openhuman-core/src/integrations/composio/contract/curated.rs,crates/open
Steven Enamakel9fbc3507313e
diff --git a/crates/openhuman-core/src/integrations/composio/contract/curated.rs b/crates/openhuman-core/src/integrations/composio/contract/curated.rsnew file mode 100644index 0000000000..f9a7c417ea--- /dev/null+++ b/crates/openhuman-core/src/integrations/composio/contract/curated.rs@@ -0,0 +1,248 @@+//! The curated Composio catalogs, read from the connector module.+//!+//! The tables live in `tinyconnectors` and are served over its bus contract+//! (`ListCuratedCatalog`, `GetVocabulary`). The host's lookups are synchronous+//! and sit on hot paths (tool visibility, flow gates), so this keeps one+//! process-wide snapshot of the module's answer. The snapshot is filled by+//! [`prime`] (an async call to the module) and dropped by [`invalidate`] when+//! the module is re-initialised or its pinned version changes.+//!+//! Until the snapshot is filled — the module is not loaded yet, or this build+//! has no module loader — every toolkit reads as uncurated, which routes the+//! caller to its classify-by-verb fallback rather than hiding every action.++use std::collections::{HashMap, HashSet};+use std::sync::{Arc, Mutex, RwLock};++use tinyconnectors_bus::names::methods;+use tinyconnectors_bus::vocabulary::{+ CuratedCatalogRequest, CuratedTool as BusCuratedTool, ToolScope as BusScope, VocabularyRequest,+ VocabularyResponse,+};++use super::scopes::{CuratedTool, ToolScope};++const LOG: &str = "[composio][curated]";++/// Description for a toolkit the module has no blurb for, used while the+/// snapshot is empty.+pub const FALLBACK_DESCRIPTION: &str = "Interact with this connected service via its available actions";++/// Spellings callers use that are not the module's canonical toolkit slug.+/// They are resolved by the module (`GetVocabulary` matches aliases), not here;+/// listing them only tells [`prime`] which extra names to ask about.+const ALIAS_SPELLINGS: &[&str] = &[+ "google_calendar",+ "google_drive",+ "google_docs",+ "google_sheets",+ "microsoft",+ "one",+ "onedrive",+ "larksuite",+];++#[derive(Default)]+struct Snapshot {+ /// Module version the snapshot was read from.+ module_version: String,+ /// Lowercased toolkit name or alias -> curated catalog.+ catalogs: HashMap<String, Arc<[CuratedTool]>>,+ descriptions: HashMap<String, &'static str>,+ native: HashSet<String>,+ /// Canonical toolkits with a catalog, in the module's order.+ toolkits: Vec<String>,+}++fn store() -> &'static RwLock<Option<Arc<Snapshot>>> {+ static STORE: RwLock<Option<Arc<Snapshot>>> = RwLock::new(None);+ &STORE+}++fn current() -> Option<Arc<Snapshot>> {+ store()+ .read()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .clone()+}++/// Leak each distinct string once: `CuratedTool` carries `&'static str`, and a+/// re-read after reinit must not grow the heap by another copy of the tables.+fn intern(text: &str) -> &'static str {+ static POOL: Mutex<Option<HashSet<&'static str>>> = Mutex::new(None);+ let mut pool = POOL.lock().unwrap_or_else(std::sync::PoisonError::into_inner);+ let pool = pool.get_or_insert_with(HashSet::new);+ if let Some(found) = pool.get(text) {+ return found;+ }+ let leaked: &'static str = Box::leak(text.to_owned().into_boxed_str());+ pool.insert(leaked);+ leaked+}++fn scope(bus: BusScope) -> ToolScope {+ match bus {+ BusScope::Read => ToolScope::Read,+ BusScope::Write => ToolScope::Write,+ BusScope::Admin => ToolScope::Admin,+ }+}++fn key(toolkit: &str) -> String {+ toolkit.trim().to_ascii_lowercase()+}++/// Build a snapshot from the module's two replies. Pure, so it is tested+/// without a loaded module.+fn build(+ module_version: &str,+ listed: &[BusCuratedTool],+ vocabulary: &VocabularyResponse,+) -> Snapshot {+ let mut snapshot = Snapshot {+ module_version: module_version.to_owned(),+ ..Snapshot::default()+ };+ for tool in listed {+ if !snapshot.toolkits.contains(&tool.toolkit) {+ snapshot.toolkits.push(tool.toolkit.clone());+ }+ }+ for entry in &vocabulary.toolkits {+ let name = key(&entry.toolkit);+ snapshot+ .descriptions+ .insert(name.clone(), intern(&entry.description));+ if entry.native_provider {+ snapshot.native.insert(name.clone());+ }+ if let Some(actions) = &entry.actions {+ let catalog: Arc<[CuratedTool]> = actions+ .iter()+ .map(|action| CuratedTool {+ slug: intern(&action.slug),+ scope: scope(action.scope),+ })+ .collect();+ snapshot.catalogs.insert(name, catalog);+ }+ }+ snapshot+}++/// Read the catalogs from the connector module into the process snapshot.+/// A snapshot read from the currently pinned module version is kept as is.+///+/// # Errors+///+/// The module could not be reached; the previous snapshot (if any) stays.+pub async fn prime(config: &crate::config::Config) -> Result<(), String> {+ let version = crate::modules::registry::find(crate::modules::connectors::MODULE_ID)+ .map(|record| record.version.to_owned())+ .unwrap_or_default();+ if current().is_some_and(|snapshot| snapshot.module_version == version) {+ return Ok(());+ }+ let listed: Vec<BusCuratedTool> = crate::modules::connectors::call_stateless(+ config,+ methods::LIST_CURATED_CATALOG,+ CuratedCatalogRequest { toolkit: None },+ )+ .await?;+ let mut names: Vec<String> = Vec::new();+ for tool in &listed {+ if !names.contains(&tool.toolkit) {+ names.push(tool.toolkit.clone());+ }+ }+ names.extend(ALIAS_SPELLINGS.iter().map(|alias| (*alias).to_owned()));+ let vocabulary: VocabularyResponse = crate::modules::connectors::call_stateless(+ config,+ methods::GET_VOCABULARY,+ VocabularyRequest { toolkits: names },+ )+ .await?;+ let snapshot = build(&version, &listed, &vocabulary);+ tracing::debug!(+ "{LOG} primed toolkits={} module_version={version}",+ snapshot.toolkits.len()+ );+ *store()+ .write()+ .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(Arc::new(snapshot));+ Ok(())+}++/// Drop the snapshot so the next [`prime`] re-reads it (module reinit).+pub fn invalidate() {+ *store()+ .write()+ .unwrap_or_else(std::sync::PoisonError::into_inner) = None;+ tracing::debug!("{LOG} invalidated");+}++/// Replace the snapshot directly. Test seam for callers that exercise the+/// host adapters without a loaded module.+#[cfg(test)]+pub(crate) fn install_for_test(+ listed: &[BusCuratedTool],+ vocabulary: &VocabularyResponse,+) {+ *store()+ .write()+ .unwrap_or_else(std::sync::PoisonError::into_inner) =+ Some(Arc::new(build("test", listed, vocabulary)));+}++/// The curated catalog for `toolkit` (an action prefix or any known alias).+#[must_use]+pub fn catalog_for_toolkit(toolkit: &str) -> Option<Arc<[CuratedTool]>> {+ current()?.catalogs.get(&key(toolkit)).cloned()+}++/// Canonical toolkits that have a curated catalog.+#[must_use]+pub fn curated_toolkits() -> Vec<String> {+ current().map(|s| s.toolkits.clone()).unwrap_or_default()+}++/// Does `toolkit` have a native identity provider?+#[must_use]+pub fn has_native_provider(toolkit: &str) -> bool {+ current().is_some_and(|s| s.native.contains(&key(toolkit)))+}++/// What the module says `toolkit` can do, or [`FALLBACK_DESCRIPTION`].+#[must_use]+pub fn toolkit_description(toolkit: &str) -> &'static str {+ current()+ .and_then(|s| s.descriptions.get(&key(toolkit)).copied())+ .unwrap_or(FALLBACK_DESCRIPTION)+}++/// Should this action slug appear in the agent's tool surface, given an+/// already-loaded user scope preference? See `ListTools`: the module applies+/// the same rule when it lists tools.+#[must_use]+pub fn is_action_visible_with_pref(slug: &str, pref: &super::scopes::UserScopePref) -> bool {+ use super::scopes::{classify_unknown, find_curated, toolkit_from_slug};+ let Some(toolkit) = toolkit_from_slug(slug) else {+ return true;+ };+ match catalog_for_toolkit(&toolkit) {+ Some(catalog) => find_curated(&catalog, slug).is_some_and(|c| pref.allows(c.scope)),+ None => pref.allows(classify_unknown(slug)),+ }+}++/// The curated scope `slug` requires, if it appears in a catalog.+#[must_use]+pub fn curated_scope_for(slug: &str) -> Option<ToolScope> {+ use super::scopes::{find_curated, toolkit_from_slug};+ let catalog = catalog_for_toolkit(&toolkit_from_slug(slug)?)?;+ find_curated(&catalog, slug).map(|c| c.scope)+}++#[cfg(test)]+#[path = "curated_tests.rs"]+mod tests;diff --git a/crates/openhuman-core/src/integrations/composio/contract/curated_tests.rs b/crates/openhuman-core/src/integrations/composio/contract/curated_tests.rsnew file mode 100644index 0000000000..effaf3ef22--- /dev/null+++ b/crates/openhuman-core/src/integrations/composio/contract/curated_tests.rs@@ -0,0 +1,75 @@+use super::*;+use tinyconnectors_bus::vocabulary::{ToolkitVocabulary, VocabularyAction};++fn reply() -> (Vec<BusCuratedTool>, VocabularyResponse) {+ let listed = vec![BusCuratedTool {+ toolkit: "gmail".into(),+ slug: "GMAIL_SEND_EMAIL".into(),+ scope: BusScope::Write,+ }];+ let vocabulary = VocabularyResponse {+ toolkits: vec![+ ToolkitVocabulary {+ toolkit: "gmail".into(),+ description: "Mail".into(),+ native_provider: true,+ actions: Some(vec![+ VocabularyAction { slug: "GMAIL_FETCH_EMAILS".into(), scope: BusScope::Read },+ VocabularyAction { slug: "GMAIL_SEND_EMAIL".into(), scope: BusScope::Write },+ ]),+ },+ ToolkitVocabulary {+ toolkit: "Google_Calendar".into(),+ description: "Calendar".into(),+ native_provider: false,+ actions: Some(vec![VocabularyAction {+ slug: "GOOGLECALENDAR_CREATE_EVENT".into(),+ scope: BusScope::Write,+ }]),+ },+ ToolkitVocabulary {+ toolkit: "unknownkit".into(),+ description: "generic".into(),+ native_provider: false,+ actions: None,+ },+ ],+ agent_ready_toolkits: vec![],+ };+ (listed, vocabulary)+}++#[test]+fn snapshot_serves_catalogs_aliases_descriptions_and_native_flags() {+ let (listed, vocabulary) = reply();+ let snapshot = build("1", &listed, &vocabulary);+ assert_eq!(snapshot.toolkits, vec!["gmail".to_string()]);+ assert_eq!(snapshot.catalogs["gmail"].len(), 2);+ assert_eq!(snapshot.catalogs["gmail"][1].scope, ToolScope::Write);+ assert!(snapshot.catalogs.contains_key("google_calendar"));+ assert!(!snapshot.catalogs.contains_key("unknownkit"));+ assert!(snapshot.native.contains("gmail"));+ assert_eq!(snapshot.descriptions["gmail"], "Mail");+}++#[test]+fn intern_returns_one_allocation_per_distinct_string() {+ let a = intern("SOME_SLUG_FOR_INTERN_TEST");+ let b = intern("SOME_SLUG_FOR_INTERN_TEST");+ assert!(std::ptr::eq(a, b));+}++#[test]+fn lookups_use_the_installed_snapshot_and_invalidate_clears_it() {+ let (listed, vocabulary) = reply();+ install_for_test(&listed, &vocabulary);+ assert!(catalog_for_toolkit(" GMAIL ").is_some());+ assert!(has_native_provider("gmail"));+ assert_eq!(toolkit_description("gmail"), "Mail");+ assert_eq!(toolkit_description("nope"), FALLBACK_DESCRIPTION);+ assert_eq!(curated_scope_for("GMAIL_SEND_EMAIL"), Some(ToolScope::Write));+ invalidate();+ assert!(catalog_for_toolkit("gmail").is_none());+ assert_eq!(toolkit_description("gmail"), FALLBACK_DESCRIPTION);+ assert!(curated_toolkits().is_empty());+}