agent-base 0.15.0

A lightweight Agent Runtime Kernel for building AI agents in Rust
Documentation
//! `activate_tool`: the model opens a tool that was named in the roster but not
//! mounted.
//!
//! One call to open, and it stays open for the session (see [`ToolActivation`]).
//! The tool hands back the full wire definition — description and schema — so
//! the next call is made against the real shape and not a guess from the name.
//!
//! **Why it holds its own definitions instead of the registry.** Looking the
//! tool up live would mean taking the registry lock from inside a call, which
//! is the pattern that deadlocked before (`ToolEngine::tools_handle`'s docs).
//! The candidate list is fixed at build time and is the same list the roster is
//! rendered from, so the two cannot drift.

use std::collections::HashMap;
use std::sync::Arc;

use async_trait::async_trait;
use serde_json::{Value, json};

use crate::types::{AgentError, AgentResult};

use super::{Content, Tool, ToolActivation, ToolContext, ToolExposure};

/// Opens one of the tools the roster names but does not mount.
pub struct ActivateTool {
    activation: ToolActivation,
    /// Tool name -> the definition the model will get once it is open.
    candidates: HashMap<String, (ToolExposure, Value)>,
}

impl ActivateTool {
    /// `candidates` is every tool this can open — the same list the roster is
    /// rendered from. Keeping one list is what stops the two from drifting.
    pub fn new(
        activation: ToolActivation,
        candidates: Vec<(String, ToolExposure, Value)>,
    ) -> Arc<Self> {
        Arc::new(Self {
            activation,
            candidates: candidates
                .into_iter()
                .map(|(name, exposure, def)| (name, (exposure, def)))
                .collect(),
        })
    }

    /// Names in sorted order, for the error message a wrong guess deserves.
    fn candidate_names(&self) -> Vec<&str> {
        let mut names: Vec<&str> = self.candidates.keys().map(|s| s.as_str()).collect();
        names.sort_unstable();
        names
    }
}

#[async_trait]
impl Tool for ActivateTool {
    fn name(&self) -> &'static str {
        "activate_tool"
    }

    fn description(&self) -> &'static str {
        "Mount a tool that the roster names but the session has not opened yet, \
         so it can be called from the next turn on. Pass the tool's name exactly \
         as the roster spells it; the result is that tool's full description and \
         argument schema. Opening is once per session — it stays mounted, do not \
         re-open it to use it again. Reach for this when the task needs a \
         capability the roster lists as unmounted; do NOT use it for tools that \
         are already mounted, and do not open a group on the chance it comes in \
         handy — every open tool is sent on every later request."
    }

    fn schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "name": {
                    "type": "string",
                    "description": "The tool name exactly as the roster spells it."
                }
            },
            "required": ["name"],
            "additionalProperties": false,
        })
    }

    /// Exposure is `Direct`: this one must always be on the wire, or nothing can
    /// ever be opened.
    fn exposure(&self) -> ToolExposure {
        ToolExposure::Direct
    }

    async fn call(&self, args: &Value, _ctx: &ToolContext) -> AgentResult<Vec<Content>> {
        let name = args
            .get("name")
            .and_then(Value::as_str)
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .ok_or_else(|| AgentError::ToolArgsInvalid {
                name: self.name().to_string(),
                raw: "missing field `name` — pass the tool name exactly as the roster spells it"
                    .to_string(),
            })?;

        let Some((exposure, definition)) = self.candidates.get(name) else {
            return Ok(vec![Content::text(format!(
                "No tool named `{name}` is mountable. Mountable names: {}.",
                self.candidate_names().join(", ")
            ))]);
        };

        match exposure {
            ToolExposure::Hidden => Ok(vec![Content::text(format!(
                "`{name}` is internal and is not mountable. It is not in the roster."
            ))]),
            ToolExposure::Direct => Ok(vec![Content::text(format!(
                "`{name}` is already mounted — call it directly."
            ))]),
            ToolExposure::Deferred => {
                let first_open = self.activation.activate(name);
                let status = if first_open {
                    "mounted"
                } else {
                    "already mounted"
                };
                Ok(vec![Content::text(format!(
                    "`{name}` {status}. Call it from the next turn on, with these arguments:\n{}",
                    serde_json::to_string_pretty(definition).unwrap_or_default()
                ))])
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::AtomicBool;

    use super::*;
    use crate::engine::EventBus;
    use crate::tool::ExecClock;
    use crate::types::SessionId;
    use serde_json::json;

    fn test_context() -> ToolContext {
        let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
        ToolContext {
            session_id: SessionId::new(0),
            user_event_tx: tx,
            llm_client: None,
            session_store: None,
            language: crate::types::Language::En,
            cancel_token: tokio_util::sync::CancellationToken::new(),
            max_output_chars: None,
            exec_clock: ExecClock::default(),
            waiting_for_human: Arc::new(AtomicBool::new(false)),
            event_bus: EventBus::new(1),
        }
    }

    fn registry_free_tool() -> (ActivateTool, ToolActivation) {
        let activation = ToolActivation::new();
        let tool = ActivateTool::new(
            activation.clone(),
            vec![
                (
                    "browser_navigate".to_string(),
                    ToolExposure::Deferred,
                    json!({
                        "name": "browser_navigate",
                        "description": "Open a URL in the browser.",
                        "parameters": {"type": "object", "properties": {"url": {"type": "string"}}},
                    }),
                ),
                (
                    "always_here".to_string(),
                    ToolExposure::Direct,
                    json!({"name": "always_here"}),
                ),
                (
                    "internal".to_string(),
                    ToolExposure::Hidden,
                    json!({"name": "internal"}),
                ),
            ],
        );
        // `ActivateTool::new` returns Arc; unwrap for the tests to own the value.
        let tool = Arc::try_unwrap(tool).ok().expect("single owner in tests");
        (tool, activation)
    }

    async fn call(tool: &ActivateTool, args: Value) -> String {
        let ctx = test_context();
        let out = tool.call(&args, &ctx).await.expect("call succeeds");
        out.iter()
            .map(|c| match c {
                Content::Text { text } => text.clone(),
                _ => String::new(),
            })
            .collect()
    }

    /// Value: protects=the one thing the tool exists for — an opened tool must
    /// be callable afterwards and its shape must come from us, not from the
    /// model's reading of its name; fails_when=opening changes nothing, or
    /// returns something other than the real definition. why_new=the tool is
    /// new. seam=roster -> request; no_claim=that the model will read it.
    #[tokio::test]
    async fn opening_a_tool_mounts_it_and_hands_back_its_shape() {
        let (tool, activation) = registry_free_tool();
        let out = call(&tool, json!({"name": "browser_navigate"})).await;
        assert!(
            activation.is_active("browser_navigate"),
            "opening must stick for the session"
        );
        assert!(out.contains("mounted"), "say what happened: {out}");
        assert!(
            out.contains("\"url\""),
            "and hand back the real arguments, not just the name: {out}"
        );
    }

    /// Value: protects=the model against a confidently wrong guess — a name
    /// that is not mountable must come back with the list, because a silent
    /// success leaves it calling a tool that never arrived; fails_when=an
    /// unknown name is accepted. why_new=the tool is new. seam=roster; no_claim=
    /// that the list is the only way to discover names.
    #[tokio::test]
    async fn an_unknown_name_is_refused_with_the_real_ones() {
        let (tool, _activation) = registry_free_tool();
        let out = call(&tool, json!({"name": "browser_nav"})).await;
        assert!(out.contains("No tool named `browser_nav`"), "{out}");
        assert!(
            out.contains("browser_navigate"),
            "the answer must name the tool it meant: {out}"
        );
    }

    /// Value: protects=the reason `Hidden` exists — internal plumbing must stay
    /// internal, and a roster-shaped hole must not become a way in;
    /// fails_when=an internal tool can be mounted. why_new=the tool is new.
    /// seam=exposure policy; no_claim=that no other path reaches a hidden tool.
    #[tokio::test]
    async fn an_internal_tool_cannot_be_mounted() {
        let (tool, activation) = registry_free_tool();
        let out = call(&tool, json!({"name": "internal"})).await;
        assert!(out.contains("not mountable"), "{out}");
        assert!(!activation.is_active("internal"), "and nothing was opened");
    }

    /// Value: protects=a turn the model could have spent working — a mounted
    /// tool is already on the wire, so asking again must not read as success
    /// and cost another round; fails_when=re-opening reports a fresh mount.
    /// why_new=the tool is new. seam=session state; no_claim=about a session
    /// where the tool was unmounted by other means.
    #[tokio::test]
    async fn opening_twice_is_reported_as_already_mounted() {
        let (tool, _activation) = registry_free_tool();
        call(&tool, json!({"name": "browser_navigate"})).await;
        let out = call(&tool, json!({"name": "browser_navigate"})).await;
        assert!(out.contains("already mounted"), "{out}");
    }

    /// Value: protects=the budget the roster exists to save — `Direct` tools
    /// are already on every request, so "opening" one must not read as a win
    /// and must not change the request; fails_when=a Direct tool is reported
    /// mounted. why_new=the tool is new. seam=exposure policy.
    #[tokio::test]
    async fn an_already_mounted_tool_is_said_to_be() {
        let (tool, activation) = registry_free_tool();
        let out = call(&tool, json!({"name": "always_here"})).await;
        assert!(out.contains("already mounted"), "{out}");
        assert!(
            !activation.is_active("always_here"),
            "a Direct tool needs no activation entry"
        );
    }

    /// Value: protects=the arg contract — a call with no name is a mistake the
    /// model can correct if we say so, and an empty name is not a name;
    /// fails_when=both are accepted and quietly do nothing.
    /// why_new=the tool is new. seam=args.
    #[tokio::test]
    async fn a_missing_name_says_what_is_missing() {
        let (tool, _activation) = registry_free_tool();
        let err = tool.call(&json!({}), &test_context()).await;
        let msg = format!("{:?}", err.expect_err("no name is an error"));
        assert!(
            msg.contains("`name`"),
            "the error must name the field: {msg}"
        );
    }
}