Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(tools): add source listing tool

Steven Enamakelc75743d55067
diff --git a/crates/openhuman-core/src/core/slots_tests.rs b/crates/openhuman-core/src/core/slots_tests.rs
new file mode 100644
index 0000000000..b29dd5309f
--- /dev/null
+++ b/crates/openhuman-core/src/core/slots_tests.rs
@@ -0,0 +1,77 @@
+use super::*;
+
+static SLOT: HookSlot<u32> = HookSlot::new("slots_tests::numbers");
+
+#[test]
+fn an_empty_slot_has_no_entries() {
+ let slot: HookSlot<u32> = HookSlot::new("slots_tests::empty");
+ assert!(slot.is_empty());
+ assert!(slot.entries().is_empty());
+ assert_eq!(slot.get("missing"), None);
+ assert_eq!(slot.first(), None);
+}
+
+#[test]
+fn entries_keep_registration_order_and_re_registering_keeps_position() {
+ SLOT.clear();
+ SLOT.register("b", 2);
+ SLOT.register("a", 1);
+ SLOT.register("c", 3);
+ SLOT.register("b", 20);
+ let entries = SLOT.entries();
+ assert_eq!(
+ entries,
+ vec![("b".into(), 20), ("a".into(), 1), ("c".into(), 3)]
+ );
+ assert_eq!(SLOT.first(), Some(20));
+ assert_eq!(SLOT.get("a"), Some(1));
+ SLOT.clear();
+}
+
+#[test]
+fn unregister_removes_only_the_named_entry() {
+ SLOT.clear();
+ SLOT.register("a", 1);
+ SLOT.register("b", 2);
+ assert!(SLOT.unregister("a"));
+ assert!(!SLOT.unregister("a"));
+ assert_eq!(SLOT.entries(), vec![("b".to_string(), 2)]);
+ SLOT.clear();
+}
+
+#[test]
+fn context_registrations_are_isolated_until_an_extensions_set_is_bound() {
+ SLOT.clear();
+ let extensions = Extensions::new();
+ SLOT.register_in(&extensions, "ctx", 7);
+ // Nothing is bound to an ambient context here, so the process view is empty.
+ assert!(SLOT.entries().is_empty());
+ let table = extensions.existing::<u32>("slots_tests::numbers").unwrap();
+ assert_eq!(table.get("ctx"), Some(7));
+}
+
+#[test]
+fn merging_extensions_shares_tables_without_overwriting_existing_ones() {
+ let first = Extensions::new();
+ let second = Extensions::new();
+ SLOT.register_in(&first, "one", 1);
+ SLOT.register_in(&second, "two", 2);
+ let target = Extensions::new();
+ target.merge_from(&first);
+ target.merge_from(&second);
+ assert_eq!(target.len(), 1, "the first table for a slot name wins");
+ let table = target.existing::<u32>("slots_tests::numbers").unwrap();
+ assert_eq!(table.get("one"), Some(1));
+ assert_eq!(table.get("two"), None);
+}
+
+#[test]
+fn a_slot_name_reused_with_another_type_yields_a_detached_table() {
+ let extensions = Extensions::new();
+ SLOT.register_in(&extensions, "n", 1);
+ let other: HookSlot<String> = HookSlot::new("slots_tests::numbers");
+ other.register_in(&extensions, "s", "x".to_string());
+ let table = extensions.existing::<u32>("slots_tests::numbers").unwrap();
+ assert_eq!(table.get("n"), Some(1));
+ assert_eq!(table.get("s"), None);
+}
diff --git a/crates/openhuman-core/src/tools/mod.rs b/crates/openhuman-core/src/tools/mod.rs
index 0fbbdb0191..9557ba3a9c 100644
--- a/crates/openhuman-core/src/tools/mod.rs
+++ b/crates/openhuman-core/src/tools/mod.rs
@@ -9,6 +9,7 @@ pub mod registry;
pub mod rules;
pub(crate) mod schema_cache;
mod schemas;
+pub mod sources;
pub mod timeout;
pub mod toolpacks;
pub(crate) mod user_filter;
diff --git a/crates/openhuman-core/src/tools/sources.rs b/crates/openhuman-core/src/tools/sources.rs
new file mode 100644
index 0000000000..14c5a2f6cf
--- /dev/null
+++ b/crates/openhuman-core/src/tools/sources.rs
@@ -0,0 +1,89 @@
+//! Tool sources: id-keyed factories an outside crate registers so its tools
+//! join the agent catalogue.
+//!
+//! [`all_tools_with_runtime`](super::ops::all_tools_with_runtime) builds the
+//! built-in catalogue, then appends every registered source in registration
+//! order, then applies the capability filters (`DomainSet`, memory capability,
+//! `ToolGroups`). A registered tool is therefore filtered exactly like a
+//! built-in one. With nothing registered the catalogue is unchanged.
+//!
+//! The registry is generic: it knows nothing about what a source builds.
+
+use std::collections::HashMap;
+use std::path::Path;
+use std::sync::Arc;
+
+use tinytools::Tool;
+
+use crate::agent::host_runtime::RuntimeAdapter;
+use crate::config::{BrowserConfig, Config, DelegateAgentConfig, HttpRequestConfig};
+use crate::core::slots::HookSlot;
+use crate::security::{AuditLogger, SecurityPolicy};
+
+/// What a tool source can see while the catalogue is assembled: the same
+/// handles `all_tools_with_runtime` was given.
+pub struct ToolSourceCtx<'a> {
+ /// The session's configuration snapshot.
+ pub config: &'a Arc<Config>,
+ /// The root configuration the catalogue is assembled under.
+ pub root_config: &'a Config,
+ pub security: &'a Arc<SecurityPolicy>,
+ pub runtime: &'a Arc<dyn RuntimeAdapter>,
+ pub audit: &'a Arc<AuditLogger>,
+ pub browser_config: &'a BrowserConfig,
+ pub http_config: &'a HttpRequestConfig,
+ /// The agent's permitted read/write root.
+ pub action_dir: &'a Path,
+ pub agents: &'a HashMap<String, DelegateAgentConfig>,
+ pub approval_workspace_root: Option<&'a Path>,
+}
+
+/// A registered tool source.
+pub type ToolSourceFactory =
+ Arc<dyn for<'a> Fn(&ToolSourceCtx<'a>) -> Vec<Box<dyn Tool>> + Send + Sync>;
+
+static TOOL_SOURCES: HookSlot<ToolSourceFactory> = HookSlot::new("tools::sources");
+
+/// Registers `factory` as the tool source `id` for the whole process.
+/// Re-registering an id replaces its factory and keeps its position.
+pub fn register_tool_source<F>(id: impl Into<String>, factory: F)
+where
+ F: for<'a> Fn(&ToolSourceCtx<'a>) -> Vec<Box<dyn Tool>> + Send + Sync + 'static,
+{
+ TOOL_SOURCES.register(id, Arc::new(factory));
+}
+
+/// Registers a tool source for the context built from `extensions`.
+pub fn register_tool_source_in<F>(
+ extensions: &crate::core::slots::Extensions,
+ id: impl Into<String>,
+ factory: F,
+) where
+ F: for<'a> Fn(&ToolSourceCtx<'a>) -> Vec<Box<dyn Tool>> + Send + Sync + 'static,
+{
+ TOOL_SOURCES.register_in(extensions, id, Arc::new(factory));
+}
+
+/// Removes the process-wide tool source `id`.
+pub fn unregister_tool_source(id: &str) -> bool {
+ TOOL_SOURCES.unregister(id)
+}
+
+/// Builds every registered source's tools, in registration order.
+pub(crate) fn build_registered_tool_sources(ctx: &ToolSourceCtx<'_>) -> Vec<Box<dyn Tool>> {
+ let mut tools = Vec::new();
+ for (id, factory) in TOOL_SOURCES.entries() {
+ let built = factory(ctx);
+ tracing::debug!(
+ source = %id,
+ count = built.len(),
+ "[tools::sources] registered tool source built"
+ );
+ tools.extend(built);
+ }
+ tools
+}
+
+#[cfg(test)]
+#[path = "sources_tests.rs"]
+mod tests;