use alloc::borrow::Cow;
use alloc::boxed::Box;
use alloc::string::String;
use alloc::sync::Arc;
use core::fmt::{self, Debug, Display};
use core::future::Future;
use core::pin::Pin;
use super::CallIdentity;
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct ApprovalKind(Cow<'static, str>);
impl ApprovalKind {
#[must_use]
pub fn new(name: impl Into<Cow<'static, str>>) -> Self {
Self(name.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Display for ApprovalKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ApprovalRequest {
kind: ApprovalKind,
subject: String,
reason: Option<String>,
call: Option<CallIdentity>,
}
impl ApprovalRequest {
#[must_use]
pub fn new(kind: ApprovalKind, subject: impl Into<String>) -> Self {
Self {
kind,
subject: subject.into(),
reason: None,
call: None,
}
}
#[must_use]
pub fn with_reason(mut self, reason: impl Into<String>) -> Self {
self.reason = Some(reason.into());
self
}
#[must_use]
pub fn with_call(mut self, call: CallIdentity) -> Self {
self.call = Some(call);
self
}
#[must_use]
pub const fn kind(&self) -> &ApprovalKind {
&self.kind
}
#[must_use]
pub fn subject(&self) -> &str {
&self.subject
}
#[must_use]
pub fn reason(&self) -> Option<&str> {
self.reason.as_deref()
}
#[must_use]
pub const fn call(&self) -> Option<&CallIdentity> {
self.call.as_ref()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Approval {
Approved,
Denied {
reason: String,
},
}
impl Approval {
#[must_use]
pub const fn is_approved(&self) -> bool {
matches!(self, Self::Approved)
}
}
pub trait Approver: Send + Sync + 'static {
fn approve(
&self,
request: ApprovalRequest,
) -> impl Future<Output = Result<Approval, ApproverFailure>> + Send;
}
#[derive(Debug, thiserror::Error)]
#[error("the approver could not decide")]
pub struct ApproverFailure(#[source] Box<dyn core::error::Error + Send + Sync + 'static>);
impl ApproverFailure {
#[must_use]
pub fn new(error: impl core::error::Error + Send + Sync + 'static) -> Self {
Self(Box::new(error))
}
}
type ApproveFuture<'a> =
Pin<Box<dyn Future<Output = Result<Approval, ApproverFailure>> + Send + 'a>>;
trait ApproverImpl: Send + Sync {
fn approve(&self, request: ApprovalRequest) -> ApproveFuture<'_>;
}
impl<A: Approver> ApproverImpl for A {
fn approve(&self, request: ApprovalRequest) -> ApproveFuture<'_> {
Box::pin(Approver::approve(self, request))
}
}
#[derive(Clone)]
pub struct AnyApprover(Arc<dyn ApproverImpl>);
impl AnyApprover {
#[must_use]
pub fn new(approver: impl Approver) -> Self {
Self(Arc::new(approver))
}
}
impl Approver for AnyApprover {
fn approve(
&self,
request: ApprovalRequest,
) -> impl Future<Output = Result<Approval, ApproverFailure>> + Send {
self.0.approve(request)
}
}
impl Debug for AnyApprover {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AnyApprover").finish_non_exhaustive()
}
}
#[derive(Debug, thiserror::Error)]
pub enum ApprovalError {
#[error(
"approval for `{kind}` was requested, but this tool call's context has no approver \
(its dispatcher did not call `ToolContext::with_approver`)"
)]
NoApprover {
kind: ApprovalKind,
},
#[error("the approver failed on the request for `{kind}`")]
Failed {
kind: ApprovalKind,
#[source]
failure: ApproverFailure,
},
#[error("the tool call was cancelled while waiting for approval")]
Cancelled,
}