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 management operations

Steven Enamakel3c56e2f3b059
diff --git a/crates/openhuman-core/src/tools/ops.rs b/crates/openhuman-core/src/tools/ops.rs
index 51220b5cad..a0145a2f11 100644
--- a/crates/openhuman-core/src/tools/ops.rs
+++ b/crates/openhuman-core/src/tools/ops.rs
@@ -489,23 +489,13 @@ pub fn all_tools_with_runtime(
tracing::debug!("[lsp] capability gate off (set OPENHUMAN_LSP_ENABLED=1 to register)");
}
- // Registered tool sources (`tools::sources`): tools an outside crate plugs
- // in, appended in registration order after every built-in and before the
- // filters below, so they are gated exactly like built-ins. Nothing
- // registered => nothing appended.
+ // Registered tool sources placed at the end of the catalogue
+ // (`tools::sources`), after every built-in and before the filters below,
+ // so they are gated exactly like built-ins. Nothing registered => nothing
+ // appended.
tools.extend(crate::tools::sources::build_registered_tool_sources(
- &crate::tools::sources::ToolSourceCtx {
- config: &config,
- root_config,
- security,
- runtime: &runtime,
- audit: &audit,
- browser_config,
- http_config,
- action_dir,
- agents,
- approval_workspace_root,
- },
+ &source_ctx,
+ None,
));
// Post-filters over the assembled list:
diff --git a/crates/openhuman-core/src/tools/sources.rs b/crates/openhuman-core/src/tools/sources.rs
index 14c5a2f6cf..91124979a1 100644
--- a/crates/openhuman-core/src/tools/sources.rs
+++ b/crates/openhuman-core/src/tools/sources.rs
@@ -2,10 +2,15 @@
//! 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.
+//! built-in catalogue, splicing every registered source in at its
+//! [`ToolSourcePlacement`], 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 catalogue order is part of the prompt-cache layout, so a source can ask
+//! to sit after a named anchor point instead of at the end. Sources sharing a
+//! placement keep registration order.
//!
//! The registry is generic: it knows nothing about what a source builds.
@@ -42,18 +47,61 @@ pub struct ToolSourceCtx<'a> {
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");
+/// Anchor point: right after the search tools, before hosting.
+pub const ANCHOR_AFTER_SEARCH: &str = "after_search";
+/// Anchor point: right after the delegation tool, before the Composio tools.
+pub const ANCHOR_AFTER_DELEGATE: &str = "after_delegate";
+/// Every anchor `all_tools_with_runtime` splices sources into.
+pub const ANCHORS: [&str; 2] = [ANCHOR_AFTER_SEARCH, ANCHOR_AFTER_DELEGATE];
+
+/// Where in the catalogue a source's tools go.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+pub enum ToolSourcePlacement {
+ /// After every built-in tool (the default).
+ #[default]
+ End,
+ /// Right after the named anchor point (one of [`ANCHORS`]). An unknown
+ /// anchor falls back to [`ToolSourcePlacement::End`] and logs a warning.
+ After(&'static str),
+}
-/// Registers `factory` as the tool source `id` for the whole process.
-/// Re-registering an id replaces its factory and keeps its position.
+#[derive(Clone)]
+struct ToolSourceEntry {
+ placement: ToolSourcePlacement,
+ factory: ToolSourceFactory,
+}
+
+static TOOL_SOURCES: HookSlot<ToolSourceEntry> = HookSlot::new("tools::sources");
+
+/// Registers `factory` as the tool source `id` for the whole process, at the
+/// end of the catalogue. 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));
+ register_tool_source_at(id, ToolSourcePlacement::End, factory);
+}
+
+/// Registers a tool source for the whole process at `placement`.
+pub fn register_tool_source_at<F>(
+ id: impl Into<String>,
+ placement: ToolSourcePlacement,
+ factory: F,
+) where
+ F: for<'a> Fn(&ToolSourceCtx<'a>) -> Vec<Box<dyn Tool>> + Send + Sync + 'static,
+{
+ TOOL_SOURCES.register(
+ id,
+ ToolSourceEntry {
+ placement,
+ factory: Arc::new(factory),
+ },
+ );
}
-/// Registers a tool source for the context built from `extensions`.
+/// Registers a tool source for the context built from `extensions`, at the end
+/// of the catalogue.
pub fn register_tool_source_in<F>(
extensions: &crate::core::slots::Extensions,
id: impl Into<String>,
@@ -61,7 +109,14 @@ pub fn register_tool_source_in<F>(
) where
F: for<'a> Fn(&ToolSourceCtx<'a>) -> Vec<Box<dyn Tool>> + Send + Sync + 'static,
{
- TOOL_SOURCES.register_in(extensions, id, Arc::new(factory));
+ TOOL_SOURCES.register_in(
+ extensions,
+ id,
+ ToolSourceEntry {
+ placement: ToolSourcePlacement::End,
+ factory: Arc::new(factory),
+ },
+ );
}
/// Removes the process-wide tool source `id`.
@@ -69,13 +124,31 @@ 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>> {
+/// Builds the tools of every registered source placed at `anchor`
+/// (`Some(name)`) or at the end (`None`, which also collects sources whose
+/// anchor is unknown), in registration order.
+pub(crate) fn build_registered_tool_sources(
+ ctx: &ToolSourceCtx<'_>,
+ anchor: Option<&str>,
+) -> Vec<Box<dyn Tool>> {
let mut tools = Vec::new();
- for (id, factory) in TOOL_SOURCES.entries() {
- let built = factory(ctx);
+ for (id, entry) in TOOL_SOURCES.entries() {
+ let applies = match (entry.placement, anchor) {
+ (ToolSourcePlacement::After(a), Some(wanted)) => a == wanted,
+ (ToolSourcePlacement::After(a), None) if !ANCHORS.contains(&a) => {
+ tracing::warn!(source = %id, anchor = a, "[tools::sources] unknown anchor; appending at the end");
+ true
+ }
+ (ToolSourcePlacement::End, None) => true,
+ _ => false,
+ };
+ if !applies {
+ continue;
+ }
+ let built = (entry.factory)(ctx);
tracing::debug!(
source = %id,
+ anchor = ?anchor,
count = built.len(),
"[tools::sources] registered tool source built"
);