rho-coding-agent 3.12.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. Its tabs
//! ([`session_tabs`]) exist whether or not its agent has the tools, so a
//! person can drive them from a client either way. `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 cursor;
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 tabs, kept for as long as the session's tool set: a person
/// drives them from a client whether or not the agent has the browser
/// tools, and the agent's tools, when it has them, act on the same tabs.
/// The bundle offers no tool; its shutdown closes the session's browser
/// context.
pub(super) fn session_tabs(browser: SessionBrowser) -> TabsBundle {
    let tabs = Arc::new(SessionTabs::new(browser.clone()));
    let person: Arc<dyn crate::browser::drive::PersonTabs> = tabs.clone();
    browser.drive_with(Arc::downgrade(&person));
    TabsBundle { tabs }
}

/// The session's tabs, from [`session_tabs`].
pub(super) struct TabsBundle {
    tabs: Arc<SessionTabs>,
}

impl TabsBundle {
    /// The agent's browser tools that `[browser]` as `config` offers, on
    /// these tabs, in two bundles since exposure is per bundle: `navigate`,
    /// declared, and the rest, which `tool_search` loads.
    pub(super) fn agent_bundles(
        &self,
        max_output_bytes: usize,
        config: &BrowserConfig,
    ) -> [BrowserBundle; 2] {
        let offer = Arc::new(calls::Offer::new(config));
        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(&self.tabs),
                        action,
                        max_output_bytes,
                        offer: Arc::clone(&offer),
                    }) as Arc<dyn Tool>
                })
                .collect()
        };
        [
            BrowserBundle {
                tools: tools(/*declared*/ true),
                exposure: ContextExposure::Direct,
            },
            BrowserBundle {
                tools: tools(/*declared*/ false),
                exposure: ContextExposure::Hidden,
            },
        ]
    }
}

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

    fn shutdown(&self) -> BoxFuture<'_, ()> {
        Box::pin(self.tabs.release())
    }
}

pub(super) struct BrowserBundle {
    tools: Vec<Arc<dyn Tool>>,
    exposure: ContextExposure,
}

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

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