Skip to main content

base/interface/
plugin_call.rs

1//! A host's call of a tool object outside any turn, through the session's
2//! whole tool pipeline — permission, `PreToolUse`, `tool.around`, the call,
3//! `PostToolUse`, telemetry — the way the model's calls go, beside a turn
4//! that may be running. The tool is handed over, so it need not be one the model can call.
5//!
6//! The call itself is the host's approval: what the session would ask about
7//! runs, what its rules deny or its mode refuses does not. Nobody is asked
8//! anything, neither about it nor about the calls it makes. `HostCallLimits::rules`
9//! hold after the session's own for this call and the calls it makes: a deny
10//! refuses in any mode, an allow answers only what the mode would ask.
11
12use std::sync::Arc;
13
14use serde_json::Value;
15use tokio_util::sync::CancellationToken;
16
17use crate::interface::tool::Tool;
18use crate::interface::tool_middleware::ToolOutcome;
19use crate::permission::PermissionRule;
20
21#[async_trait::async_trait]
22pub trait HostToolRunner: Send + Sync {
23    async fn run(
24        &self,
25        tool: Arc<dyn Tool>,
26        input: Value,
27        limits: HostCallLimits,
28        cancel: CancellationToken,
29    ) -> HostCallOutcome;
30    /// The session's tool `name`, if its tool policy lets it have one.
31    fn tool(&self, name: &str) -> Option<Arc<dyn Tool>>;
32}
33
34/// What the host holds one call and every call it makes to, over the
35/// session's rules and sandbox: they only tighten.
36#[derive(Debug, Clone, Default, PartialEq)]
37pub struct HostCallLimits {
38    /// Held after the session's or the scene's rules.
39    pub rules: Vec<PermissionRule>,
40    /// No sandboxed command reaches the network; a command the sandbox cannot
41    /// hold to that is refused, not run.
42    pub cut_network: bool,
43}
44
45/// `outcome` `Err`: no answer — refused, invalid, failed, the session gone. `by_middleware`: the
46/// answer is not the tool's own — a `tool.around` gave or changed it, or a `tool.result` rewrote
47/// it. `stopped`: the host or the session's end told the call to stop.
48#[derive(Debug, Clone, PartialEq)]
49pub struct HostCallOutcome {
50    pub outcome: ToolOutcome,
51    pub by_middleware: bool,
52    pub stopped: bool,
53}