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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use std::sync::Arc;
use serde_json::Value;
use tokio::sync::RwLock;
use crate::engine::approval::ApprovalHandler;
use crate::engine::pipeline::DefaultPipeline;
use crate::engine::recovery::{InvalidArgsHandler, ReissueMissingFields, ToolErrorRecovery};
use crate::engine::runtime::event_bus::EventBus;
use crate::tool::{ActivationContext, ToolPolicy, ToolRegistry};
use super::ToolEngine;
impl ToolEngine {
pub fn new(
tools: ToolRegistry,
approval_handler: Option<Arc<dyn ApprovalHandler>>,
tool_policy: Option<Arc<dyn ToolPolicy>>,
error_recovery: Arc<dyn ToolErrorRecovery>,
event_bus: EventBus,
) -> Self {
let pipeline = DefaultPipeline::new(tool_policy.clone(), None, None);
Self {
tools: Arc::new(RwLock::new(tools)),
approval_handler,
tool_policy,
error_recovery,
invalid_args_handler: Arc::new(ReissueMissingFields),
event_bus,
pipeline,
}
}
#[allow(dead_code)]
pub async fn definitions(&self) -> Vec<Value> {
self.tools.read().await.definitions()
}
/// Return tool definitions filtered by [`ToolExposure`].
///
/// Respects `Direct` / `Deferred` / `Hidden` visibility levels.
pub async fn definitions_filtered(&self, ctx: &ActivationContext) -> Vec<Value> {
self.tools.read().await.definitions_filtered(ctx)
}
/// Shared handle to the tool registry (test-only).
///
/// Lets a test fixture hold the same `Arc` the engine executes against, so
/// it can register tools from inside `call` — the self-registration pattern
/// that deadlocked when the engine held the registry lock across a call.
#[cfg(test)]
pub(crate) fn tools_handle(&self) -> Arc<RwLock<ToolRegistry>> {
Arc::clone(&self.tools)
}
/// Does the named tool's JSON schema declare at least one required
/// parameter?
///
/// Used by the react truncation guard to tell a *legitimate* empty-arguments
/// call (`{}` for a no-arg tool such as `list_agents`) apart from a
/// *degenerate* one: an empty `spawn_agent {}` the model echoed back from
/// sanitized history. A call to a required-field tool with `{}` args can
/// never succeed, so the guard re-issues it instead of letting it fall
/// through to a `ToolArgsInvalid` execution failure. Unknown tools return
/// `false` so they take the normal schema-validation path.
pub async fn tool_requires_params(&self, name: &str) -> bool {
let guard = self.tools.read().await;
guard
.get(name)
.map(|t| {
t.schema()
.get("required")
.and_then(Value::as_array)
.is_some_and(|a| !a.is_empty())
})
.unwrap_or(false)
}
/// The named tool's input schema, or `None` when the tool is unknown.
/// Read-only sibling of [`Self::tool_requires_params`] for callers that
/// need the full schema — e.g. the react truncation guard's markup
/// salvage, which validates required fields and coerces recovered
/// argument values by declared type.
pub async fn tool_schema(&self, name: &str) -> Option<Value> {
let guard = self.tools.read().await;
guard.get(name).map(|t| t.schema())
}
pub fn error_recovery(&self) -> &Arc<dyn ToolErrorRecovery> {
&self.error_recovery
}
/// Replace the strategy used when a tool call omits required arguments.
pub fn with_invalid_args_handler(mut self, handler: Arc<dyn InvalidArgsHandler>) -> Self {
self.invalid_args_handler = handler;
self
}
pub fn invalid_args_handler(&self) -> &Arc<dyn InvalidArgsHandler> {
&self.invalid_args_handler
}
pub fn approval_handler(&self) -> Option<&Arc<dyn ApprovalHandler>> {
self.approval_handler.as_ref()
}
pub fn tool_policy(&self) -> Option<&Arc<dyn ToolPolicy>> {
self.tool_policy.as_ref()
}
pub fn tools_arc(&self) -> Arc<RwLock<ToolRegistry>> {
self.tools.clone()
}
}