Skip to main content

base/interface/
tool_dispatch.rs

1//! How a tool calls other tools of its session — through the same dispatch a
2//! model's own tool call takes (validation, hooks, middleware, telemetry,
3//! permission), so a sub-call can do nothing its caller could not have done
4//! directly. Handed to tools that ask for it (`ToolSpec::needs_dispatch`);
5//! a skill's inline command takes the same dispatch through
6//! [`crate::interface::inline_command::InlineCommands`].
7
8use async_trait::async_trait;
9use serde_json::Value;
10use tokio_util::sync::CancellationToken;
11
12/// A tool another tool can call by name.
13#[derive(Debug, Clone)]
14pub struct CallableTool {
15    pub name: String,
16    pub description: String,
17    pub input_schema: Value,
18    /// See `ToolSpec::code_result`.
19    pub result: Option<String>,
20}
21
22/// What a sub-call produced: the result's text, its structured content when
23/// the tool gave one, and whether the tool called it a failure. `Err` is the
24/// call not running at all — unknown, not callable, refused, invalid.
25#[derive(Debug, Clone)]
26pub struct SubCallAnswer {
27    pub text: String,
28    pub structured: Option<Value>,
29    pub reported_failure: bool,
30}
31
32/// What the sub-calls of one plugin command's run are held to: they are that
33/// command's, a read-only run writes nothing, and the files a write run may
34/// change are `writes`, globs relative to the project root.
35#[derive(Debug, Clone, PartialEq, Eq)]
36pub struct CommandScope {
37    pub command: String,
38    /// The command line as run, for a question about one of its sub-calls.
39    pub line: String,
40    pub plugin: String,
41    pub read_only: bool,
42    pub writes: Vec<String>,
43}
44
45#[async_trait]
46pub trait ToolDispatch: Send + Sync {
47    /// The tools a sub-call may name.
48    fn callable(&self) -> Vec<CallableTool>;
49
50    /// Whether this call may run beside others of its kind.
51    fn concurrency_safe(&self, name: &str, input: &Value) -> bool;
52
53    /// Run one sub-call. A call that would need a person's approval is
54    /// refused rather than asked, except one of a plugin command that the
55    /// model or a person ran in a session someone attends.
56    async fn call(
57        &self,
58        name: &str,
59        input: Value,
60        cancel: CancellationToken,
61    ) -> Result<SubCallAnswer, String>;
62
63    /// The same dispatch for one plugin command's sub-calls. `None` where
64    /// this dispatch cannot run a command's calls.
65    fn for_command(&self, _scope: CommandScope) -> Option<std::sync::Arc<dyn ToolDispatch>> {
66        None
67    }
68}