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