use std::sync::Arc;
use crate::cancel::Token;
use crate::runtime::hook::{
self, CallbackResources, DispatchError, StepSnapshot, ToolCallDecision,
};
use crate::runtime::tool_batch::SettledDisposition;
use crate::value::tool_call::{ToolCall, ToolResult};
pub(crate) struct PreparedSettlement {
result: ToolResult,
disposition: SettledDisposition,
}
impl PreparedSettlement {
pub(crate) const fn new(result: ToolResult, disposition: SettledDisposition) -> Self {
Self {
result,
disposition,
}
}
#[cfg_attr(not(test), expect(dead_code, reason = "reached only by tests so far"))]
pub(crate) const fn result(&self) -> &ToolResult {
&self.result
}
#[cfg_attr(not(test), expect(dead_code, reason = "reached only by tests so far"))]
pub(crate) const fn disposition(&self) -> SettledDisposition {
self.disposition
}
pub(crate) fn into_parts(self) -> (ToolResult, SettledDisposition) {
(self.result, self.disposition)
}
}
pub(crate) struct PreparedCallPlan {
original: ToolCall,
effective: ToolCall,
settled: Option<PreparedSettlement>,
}
impl PreparedCallPlan {
#[cfg_attr(not(test), expect(dead_code, reason = "reached only by tests so far"))]
pub(crate) const fn original(&self) -> &ToolCall {
&self.original
}
pub(crate) const fn effective(&self) -> &ToolCall {
&self.effective
}
#[cfg_attr(not(test), expect(dead_code, reason = "reached only by tests so far"))]
pub(crate) const fn settled(&self) -> Option<&PreparedSettlement> {
self.settled.as_ref()
}
pub(crate) fn into_parts(self) -> (ToolCall, ToolCall, Option<PreparedSettlement>) {
(self.original, self.effective, self.settled)
}
}
pub(crate) struct ToolBatchPreparePlan {
entries: Vec<PreparedCallPlan>,
}
impl ToolBatchPreparePlan {
pub(crate) fn entries(&self) -> &[PreparedCallPlan] {
&self.entries
}
pub(crate) fn into_entries(self) -> Vec<PreparedCallPlan> {
self.entries
}
}
pub(crate) fn prepare_tool_calls(
calls: Vec<ToolCall>,
registry: &hook::RuntimeRegistry,
resources: &[CallbackResources<'_>],
snapshot: Arc<StepSnapshot>,
cancellation: &Token,
) -> Result<ToolBatchPreparePlan, DispatchError> {
if cancellation.is_cancelled() {
return Err(DispatchError::Cancelled);
}
let mut entries = Vec::with_capacity(calls.len());
for original in calls {
let mut effective = original.clone();
let mut settled = None;
for adapter in registry.tool_hooks() {
let callback_resources = *resources
.get(adapter.index())
.expect("before-tool-call resources cover every registry index");
let decision = adapter.invoke_before_tool_call(
callback_resources,
&snapshot,
&effective,
cancellation,
)?;
match decision {
ToolCallDecision::Continue => {}
ToolCallDecision::Modify(modified) => {
let (_, name, arguments) = modified.into_parts();
effective = ToolCall::new(original.id().clone(), name, arguments);
}
ToolCallDecision::Block(reason) => {
settled = Some(PreparedSettlement::new(
ToolResult::error_text(format!("blocked: {reason}")),
SettledDisposition::Blocked,
));
break;
}
ToolCallDecision::ShortCircuit(result) => {
settled = Some(PreparedSettlement::new(
result,
SettledDisposition::ShortCircuited,
));
break;
}
}
}
entries.push(PreparedCallPlan {
original,
effective,
settled,
});
}
Ok(ToolBatchPreparePlan { entries })
}