use std::error;
use std::fmt::{self, Display, Formatter};
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::Poll;
use crate::cancel::Token;
use crate::runtime::event::ToolEmitter;
use crate::runtime::tool::{CallDisposition, ExecutionError, Invocation, StepToolSnapshot};
use crate::value::tool_call::{ToolCall, ToolResult};
#[derive(Clone, Debug)]
pub struct Outcome {
result: ToolResult,
disposition: CallDisposition,
}
impl Outcome {
#[must_use]
pub const fn result(&self) -> &ToolResult {
&self.result
}
#[must_use]
pub const fn disposition(&self) -> CallDisposition {
self.disposition
}
#[must_use]
pub fn into_parts(self) -> (ToolResult, CallDisposition) {
(self.result, self.disposition)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum SettledDisposition {
ShortCircuited,
Blocked,
Truncated,
NotExecuted,
}
impl SettledDisposition {
#[must_use]
pub const fn disposition(self) -> CallDisposition {
match self {
Self::ShortCircuited => CallDisposition::ShortCircuited,
Self::Blocked => CallDisposition::Blocked,
Self::Truncated => CallDisposition::Truncated,
Self::NotExecuted => CallDisposition::NotExecuted,
}
}
}
struct Executable {
snapshot: Arc<StepToolSnapshot>,
invocation: Invocation,
call: ToolCall,
}
type PreparedRun = Pin<Box<dyn Future<Output = Result<Outcome, ExecutionError>> + Send>>;
impl Executable {
fn into_run(self, cancellation: Token) -> PreparedRun {
Box::pin(async move {
let outcome = self
.snapshot
.execute(&self.invocation, &self.call, &cancellation)
.await?;
let (result, disposition) = outcome.into_parts();
Ok(Outcome {
result,
disposition,
})
})
}
}
enum Slot {
Settled(Outcome),
Executable(Executable),
}
pub struct PreparedToolCall {
slot: Slot,
}
impl fmt::Debug for PreparedToolCall {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut prepared = formatter.debug_struct("PreparedToolCall");
match &self.slot {
Slot::Settled(outcome) => prepared
.field("settled", &true)
.field("disposition", &outcome.disposition),
Slot::Executable(executable) => prepared
.field("settled", &false)
.field("call", &executable.call),
}
.finish()
}
}
impl PreparedToolCall {
#[must_use]
pub const fn settled(result: ToolResult, disposition: SettledDisposition) -> Self {
Self {
slot: Slot::Settled(Outcome {
result,
disposition: disposition.disposition(),
}),
}
}
#[must_use]
pub fn executable(
snapshot: Arc<StepToolSnapshot>,
session_id: Option<String>,
call: ToolCall,
emitter: ToolEmitter,
) -> Self {
let invocation = snapshot.invocation(session_id, call.id().clone(), emitter);
Self {
slot: Slot::Executable(Executable {
snapshot,
invocation,
call,
}),
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ErrorOrigin {
Slot(usize),
Batch,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Error {
origin: ErrorOrigin,
error: ExecutionError,
}
impl Error {
#[must_use]
pub const fn origin(&self) -> ErrorOrigin {
self.origin
}
#[must_use]
pub const fn execution(&self) -> &ExecutionError {
&self.error
}
#[must_use]
pub fn into_execution(self) -> ExecutionError {
self.error
}
#[deprecated(since = "0.3.0", note = "use `execution` instead")]
#[must_use]
pub const fn error(&self) -> &ExecutionError {
self.execution()
}
#[deprecated(since = "0.3.0", note = "use `into_execution` instead")]
#[must_use]
pub fn into_error(self) -> ExecutionError {
self.into_execution()
}
const fn batch(error: ExecutionError) -> Self {
Self {
origin: ErrorOrigin::Batch,
error,
}
}
pub(crate) const fn cancelled_batch() -> Self {
Self::batch(ExecutionError::Cancelled)
}
}
impl Display for Error {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
match self.origin {
ErrorOrigin::Slot(index) => write!(
formatter,
"prepared tool batch failed at source index {index}: {}",
self.error
),
ErrorOrigin::Batch => write!(
formatter,
"prepared tool batch failed with no source position: {}",
self.error
),
}
}
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
Some(&self.error)
}
}
pub(super) fn latch(latched: &mut Option<Error>, index: usize, error: ExecutionError) {
let outranks = match latched {
None => true,
Some(existing) => {
existing.error == ExecutionError::Cancelled && error != ExecutionError::Cancelled
}
};
if outranks {
*latched = Some(Error {
origin: ErrorOrigin::Slot(index),
error,
});
}
}
struct Worker {
index: usize,
run: Option<PreparedRun>,
}
struct OwnedRuns {
siblings: Token,
workers: Vec<Worker>,
}
impl Drop for OwnedRuns {
fn drop(&mut self) {
self.siblings.cancel();
self.workers.clear();
}
}
pub async fn execute_prepared(
entries: Vec<PreparedToolCall>,
cancellation: &Token,
) -> Result<Vec<Outcome>, Error> {
if cancellation.is_cancelled() {
return Err(Error::batch(ExecutionError::Cancelled));
}
let mut outcomes: Vec<Option<Outcome>> = Vec::with_capacity(entries.len());
let mut runs = OwnedRuns {
siblings: cancellation.child_token(),
workers: Vec::new(),
};
for (index, entry) in entries.into_iter().enumerate() {
match entry.slot {
Slot::Settled(outcome) => outcomes.push(Some(outcome)),
Slot::Executable(executable) => {
outcomes.push(None);
let run = executable.into_run(runs.siblings.clone());
runs.workers.push(Worker {
index,
run: Some(run),
});
}
}
}
let mut latched: Option<Error> = None;
let siblings = runs.siblings.clone();
std::future::poll_fn(|context| {
let mut joined = true;
for worker in &mut runs.workers {
let Some(run) = worker.run.as_mut() else {
continue;
};
match run.as_mut().poll(context) {
Poll::Pending => joined = false,
Poll::Ready(Ok(outcome)) => {
worker.run = None;
outcomes[worker.index] = Some(outcome);
}
Poll::Ready(Err(error)) => {
worker.run = None;
latch(&mut latched, worker.index, error);
}
}
}
if latched.is_some() {
siblings.cancel();
}
if joined {
Poll::Ready(())
} else {
Poll::Pending
}
})
.await;
if let Some(failure) = latched {
return Err(failure);
}
if cancellation.is_cancelled() {
return Err(Error::batch(ExecutionError::Cancelled));
}
Ok(outcomes
.into_iter()
.map(|slot| slot.expect("every joined slot of a non-failing batch materialized an outcome"))
.collect())
}