rho-coding-agent 3.10.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The browser tools: `navigate` and the tools that read pages, act on
//! them and manage tabs, named and shaped after Anthropic's browser toolset.
//!
//! They drive the daemon's shared headless browser through the session's
//! [`SessionBrowser`], in a browser context of the session's own. Only a
//! daemon session gets one, so only it gets these tools. `navigate` is
//! declared to the model, and its description names the rest, which load
//! through `tool_search`: a session that never browses pays for one tool's
//! schema, not twenty-two.

mod actions;
mod calls;
mod inputs;
mod keys;
mod opt_in;
mod person;
mod reads;
mod refs;
mod schema;
mod snapshot;
mod tabs;
mod text;
mod view;

use super::code_mode::ContextExposure;
use super::sdk_registry::ToolBundle;
use crate::browser::config::BrowserConfig;
use crate::browser::SessionBrowser;
use futures_util::future::BoxFuture;
use rho_sdk::tool::Tool;
use schema::Action;
use std::sync::Arc;
use tabs::SessionTabs;

/// The names of the browser tools.
pub(crate) fn tool_names() -> impl Iterator<Item = &'static str> {
    Action::ALL.into_iter().map(Action::name)
}

/// The session's browser tools that `[browser]` as `config` offers, in two
/// bundles since exposure is per bundle: `navigate`, declared, and the
/// rest, which `tool_search` loads. The declared bundle's shutdown closes
/// the session's browser context.
pub(super) fn bundles(
    browser: SessionBrowser,
    max_output_bytes: usize,
    config: &BrowserConfig,
) -> [BrowserBundle; 2] {
    let offer = Arc::new(calls::Offer::new(config));
    let tabs = Arc::new(SessionTabs::new(browser.clone()));
    let person: Arc<dyn crate::browser::drive::PersonTabs> = tabs.clone();
    browser.drive_with(Arc::downgrade(&person));
    let tools = |declared: bool| -> Vec<Arc<dyn Tool>> {
        offer
            .actions
            .iter()
            .copied()
            .filter(|action| (*action == Action::Navigate) == declared)
            .map(|action| {
                Arc::new(calls::BrowserTool {
                    tabs: Arc::clone(&tabs),
                    action,
                    max_output_bytes,
                    offer: Arc::clone(&offer),
                }) as Arc<dyn Tool>
            })
            .collect()
    };
    [
        BrowserBundle {
            tools: tools(/*declared*/ true),
            releases: Some(Arc::clone(&tabs)),
            exposure: ContextExposure::Direct,
        },
        BrowserBundle {
            tools: tools(/*declared*/ false),
            releases: None,
            exposure: ContextExposure::Hidden,
        },
    ]
}

pub(super) struct BrowserBundle {
    tools: Vec<Arc<dyn Tool>>,
    /// The tabs this bundle closes at shutdown: one bundle does, so the
    /// context is released once.
    releases: Option<Arc<SessionTabs>>,
    exposure: ContextExposure,
}

impl ToolBundle for BrowserBundle {
    fn tools(&self) -> &[Arc<dyn Tool>] {
        &self.tools
    }

    fn context_exposure(&self) -> ContextExposure {
        self.exposure
    }

    fn shutdown(&self) -> BoxFuture<'_, ()> {
        Box::pin(async move {
            if let Some(tabs) = &self.releases {
                tabs.release().await;
            }
        })
    }
}