use super::invocation::{
ToolInvocation, ToolInvocationBatch, ToolInvocationResult, ToolInvocationSubmission,
order_tool_invocation_results,
};
use crate::{errors::AgentError, run_result::RunResult};
use merry_core::{PendingToolCall, RuntimeEvent, ToolCallBatchId};
use merry_runtime::{AgentRun as RuntimeAgentRun, Runtime};
#[derive(Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum AgentRunMessage<'a> {
Event(Box<RuntimeEvent>),
ToolInvocations {
batch: ToolInvocationBatch<'a>,
},
}
impl<'a> AgentRunMessage<'a> {
#[must_use]
pub fn as_event(&self) -> Option<&RuntimeEvent> {
match self {
Self::Event(event) => Some(event.as_ref()),
Self::ToolInvocations { .. } => None,
}
}
#[must_use]
pub fn as_tool_invocations(&self) -> Option<&ToolInvocationBatch<'a>> {
match self {
Self::Event(_) => None,
Self::ToolInvocations { batch } => Some(batch),
}
}
#[must_use]
pub fn as_tool_invocations_mut(&mut self) -> Option<&mut ToolInvocationBatch<'a>> {
match self {
Self::Event(_) => None,
Self::ToolInvocations { batch } => Some(batch),
}
}
}
pub struct AgentRun {
runtime: Runtime,
inner: RuntimeAgentRun,
ended: bool,
terminal_result: Option<RunResult>,
terminal_error: Option<AgentError>,
terminal_error_observed: bool,
result_consumed: bool,
}
impl AgentRun {
pub(crate) fn new(runtime: Runtime, inner: RuntimeAgentRun) -> Self {
Self {
runtime,
inner,
ended: false,
terminal_result: None,
terminal_error: None,
terminal_error_observed: false,
result_consumed: false,
}
}
pub(crate) fn request_cancel(&mut self) {
self.inner.request_cancel();
}
pub(crate) fn into_owned(self) -> crate::binding::OwnedAgentRun {
crate::binding::OwnedAgentRun::new(self)
}
pub async fn next_message(&mut self) -> Result<Option<AgentRunMessage<'_>>, AgentError> {
let Some(message) = self.next_runtime_message().await? else {
return Ok(None);
};
match message {
merry_runtime::AgentRunMessage::Event(event) => {
Ok(Some(AgentRunMessage::Event(Box::new(event))))
}
merry_runtime::AgentRunMessage::ToolInvocations { batch } => {
self.tool_invocation_message(batch.id().clone(), batch.calls().to_vec())
}
_ => Err(AgentError::AgentRunProtocol {
message: "runtime emitted an unsupported agent run message",
}),
}
}
pub(crate) async fn next_owned_message(
&mut self,
) -> Result<Option<crate::binding::OwnedAgentRunMessage>, AgentError> {
let Some(message) = self.next_runtime_message().await? else {
return Ok(None);
};
match message {
merry_runtime::AgentRunMessage::Event(event) => Ok(Some(
crate::binding::OwnedAgentRunMessage::Event(Box::new(event)),
)),
merry_runtime::AgentRunMessage::ToolInvocations { batch } => {
let Some(batch) = crate::binding::OwnedToolInvocationBatch::new(
batch.id().clone(),
batch.calls().to_vec(),
) else {
return Err(AgentError::AgentRunProtocol {
message: "runtime emitted an empty tool invocation batch",
});
};
Ok(Some(
crate::binding::OwnedAgentRunMessage::ToolInvocations { batch },
))
}
merry_runtime::AgentRunMessage::HookInvocation { invocation } => {
Ok(Some(crate::binding::OwnedAgentRunMessage::HookInvocation {
invocation: Box::new(invocation),
}))
}
_ => Err(AgentError::AgentRunProtocol {
message: "runtime emitted an unsupported agent run message",
}),
}
}
async fn next_runtime_message(
&mut self,
) -> Result<Option<merry_runtime::AgentRunMessage>, AgentError> {
if self.ended {
return Ok(None);
}
let Some(message) = self.inner.next_message().await.map_err(AgentError::from)? else {
self.ended = true;
match self.finish_inner().await {
Ok(result) => self.terminal_result = Some(result),
Err(error) => {
self.terminal_error = Some(error);
self.terminal_error_observed = true;
return match self.terminal_error.take() {
Some(error) => Err(error),
None => Err(AgentError::AgentRunResultMissing),
};
}
}
return Ok(None);
};
Ok(Some(message))
}
pub async fn next(&mut self) -> Result<Option<AgentRunMessage<'_>>, AgentError> {
self.next_message().await
}
pub(crate) async fn submit_tool_invocation_results(
&mut self,
batch_id: &ToolCallBatchId,
invocations: &[ToolInvocation],
results: Vec<ToolInvocationResult>,
) -> Result<ToolInvocationSubmission, AgentError> {
let ordered_results = order_tool_invocation_results(invocations, results)?;
match self
.inner
.submit_bridge_tool_outcomes(batch_id, ordered_results)
.await
{
Ok(()) => Ok(ToolInvocationSubmission::Accepted),
Err(merry_runtime::RuntimeError::BridgeToolResultRejected { .. }) => {
Ok(ToolInvocationSubmission::RejectedAndRecorded)
}
Err(error) => Err(AgentError::from(error)),
}
}
pub(crate) async fn submit_hook_decision(
&mut self,
invocation_id: &merry_runtime::HookInvocationId,
decision: merry_runtime::HookDecision,
) -> Result<(), AgentError> {
self.inner
.submit_hook_decision(invocation_id, decision)
.await
.map_err(AgentError::from)
}
pub(crate) async fn fail_hook_invocation(
&mut self,
invocation_id: &merry_runtime::HookInvocationId,
message: String,
) -> Result<(), AgentError> {
self.inner
.fail_hook_invocation(invocation_id, message)
.await
.map_err(AgentError::from)
}
pub async fn result(&mut self) -> Result<RunResult, AgentError> {
if !self.ended {
return Err(AgentError::AgentRunNotFinished);
}
self.take_terminal_result()
}
pub async fn cancel(&mut self) -> Result<RunResult, AgentError> {
if !self.ended {
self.inner.cancel_and_wait().await;
self.ended = true;
match self.finish_inner().await {
Ok(result) => self.terminal_result = Some(result),
Err(error) => self.terminal_error = Some(error),
}
}
self.take_terminal_result()
}
async fn finish_inner(&mut self) -> Result<RunResult, AgentError> {
let result = self.inner.result().await.map_err(AgentError::from)?;
RunResult::from_runtime(&self.runtime, result).await
}
fn tool_invocation_message(
&mut self,
batch_id: ToolCallBatchId,
calls: Vec<PendingToolCall>,
) -> Result<Option<AgentRunMessage<'_>>, AgentError> {
let Some(batch) = ToolInvocationBatch::from_batch(self, batch_id, calls) else {
return Err(AgentError::AgentRunProtocol {
message: "runtime emitted an empty tool invocation batch",
});
};
Ok(Some(AgentRunMessage::ToolInvocations { batch }))
}
fn take_terminal_result(&mut self) -> Result<RunResult, AgentError> {
if self.result_consumed {
return Err(AgentError::AgentRunResultConsumed);
}
self.result_consumed = true;
if let Some(result) = self.terminal_result.take() {
return Ok(result);
}
if let Some(error) = self.terminal_error.take() {
return Err(error);
}
if self.terminal_error_observed {
return Err(AgentError::AgentRunResultConsumed);
}
Err(AgentError::AgentRunResultMissing)
}
}