use crate::{
PathAccessRule, PermissionedAction, RequestedCapability,
tool::{
ToolExecutionOutcome, patch_evidence::WorkspacePatchProposal, proposal::ActionProposal,
},
};
use merry_core::PendingToolCall;
use std::{future::Future, pin::Pin};
use thiserror::Error;
use tokio_util::sync::CancellationToken;
pub type ToolExecutorFuture<'a> = Pin<Box<dyn Future<Output = ToolExecutionResult> + Send + 'a>>;
pub type ToolActionProposalFuture<'a> =
Pin<Box<dyn Future<Output = ToolActionProposalResult> + Send + 'a>>;
pub type ToolExecutionResult = Result<ToolExecutionOutcome, ToolExecutionError>;
pub type ToolActionProposalResult = Result<ToolActionPreflight, ToolExecutionError>;
#[derive(Debug, Clone)]
pub struct ToolExecutionContext {
pub(super) cancellation_token: CancellationToken,
pub(super) approved_apply_patch: Option<WorkspacePatchProposal>,
pub(super) approved_path_grants: Vec<PathAccessRule>,
pub(super) approved_network_domains: Vec<String>,
pub(super) output_sender: Option<tokio::sync::mpsc::Sender<crate::ProcessOutputChunk>>,
pub(super) plan_mode_read_only: bool,
}
impl ToolExecutionContext {
#[must_use]
pub fn new(cancellation_token: CancellationToken) -> Self {
Self {
cancellation_token,
approved_apply_patch: None,
approved_path_grants: Vec::new(),
approved_network_domains: Vec::new(),
output_sender: None,
plan_mode_read_only: false,
}
}
#[must_use]
pub fn cancellation_token(&self) -> &CancellationToken {
&self.cancellation_token
}
#[must_use]
pub fn approved_apply_patch(&self) -> Option<&WorkspacePatchProposal> {
self.approved_apply_patch.as_ref()
}
#[must_use]
pub fn approved_path_grants(&self) -> &[PathAccessRule] {
&self.approved_path_grants
}
#[must_use]
pub fn approved_network_domains(&self) -> &[String] {
&self.approved_network_domains
}
pub(crate) fn with_approved_apply_patch(mut self, patch: WorkspacePatchProposal) -> Self {
self.approved_apply_patch = Some(patch);
self
}
pub(crate) fn with_approved_path_grants(
mut self,
grants: impl IntoIterator<Item = PathAccessRule>,
) -> Self {
self.approved_path_grants.extend(grants);
self
}
pub(crate) fn with_approved_network_domains(
mut self,
domains: impl IntoIterator<Item = String>,
) -> Self {
self.approved_network_domains.extend(domains);
self
}
pub(crate) fn with_output_sender(
mut self,
sender: tokio::sync::mpsc::Sender<crate::ProcessOutputChunk>,
) -> Self {
self.output_sender = Some(sender);
self
}
pub(crate) fn output_sender(
&self,
) -> Option<&tokio::sync::mpsc::Sender<crate::ProcessOutputChunk>> {
self.output_sender.as_ref()
}
pub(crate) const fn with_plan_mode_read_only(mut self, active: bool) -> Self {
self.plan_mode_read_only = active;
self
}
pub(crate) const fn is_plan_mode_read_only(&self) -> bool {
self.plan_mode_read_only
}
}
impl Default for ToolExecutionContext {
fn default() -> Self {
Self {
cancellation_token: CancellationToken::new(),
approved_apply_patch: None,
approved_path_grants: Vec::new(),
approved_network_domains: Vec::new(),
output_sender: None,
plan_mode_read_only: false,
}
}
}
pub trait ToolExecutor: Send + Sync {
fn propose<'a>(
&'a self,
_call: PendingToolCall,
_context: ToolExecutionContext,
) -> ToolActionProposalFuture<'a> {
Box::pin(async { Ok(ToolActionPreflight::NoProposal) })
}
fn execute<'a>(
&'a self,
call: PendingToolCall,
context: ToolExecutionContext,
) -> ToolExecutorFuture<'a>;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ToolActionPreflight {
NoProposal,
Proposal(ActionProposal),
Outcome(ToolExecutionOutcome),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToolPermissionRequirement {
action: PermissionedAction,
requested: Vec<RequestedCapability>,
reason: Option<String>,
protected_target: bool,
}
impl ToolPermissionRequirement {
#[must_use]
pub fn new(
action: PermissionedAction,
requested: Vec<RequestedCapability>,
reason: Option<String>,
) -> Self {
Self {
action,
requested,
reason,
protected_target: false,
}
}
#[must_use]
pub const fn with_protected_target(mut self) -> Self {
self.protected_target = true;
self
}
#[must_use]
pub fn action(&self) -> &PermissionedAction {
&self.action
}
#[must_use]
pub fn requested(&self) -> &[RequestedCapability] {
&self.requested
}
#[must_use]
pub fn reason(&self) -> Option<&str> {
self.reason.as_deref()
}
pub(crate) fn into_parts(
self,
) -> (
PermissionedAction,
Vec<RequestedCapability>,
Option<String>,
bool,
) {
(
self.action,
self.requested,
self.reason,
self.protected_target,
)
}
}
#[derive(Debug, Error)]
pub enum ToolExecutionError {
#[error("tool execution cancelled")]
Cancelled,
#[error("tool execution requires permission review")]
PermissionRequired(Box<ToolPermissionRequirement>),
#[error("tool execution infrastructure error: {message}")]
Infrastructure {
message: String,
},
}
impl ToolExecutionError {
#[must_use]
pub fn infrastructure(message: impl Into<String>) -> Self {
Self::Infrastructure {
message: message.into(),
}
}
#[must_use]
pub fn permission_required(requirement: ToolPermissionRequirement) -> Self {
Self::PermissionRequired(Box::new(requirement))
}
}