use merry_core::ToolCallId;
use merry_runtime::{
AgentLoopConfigError, AgentLoopError, FinalOutputContractError, InteractiveError, RuntimeError,
};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum AgentProfileError {
#[error("agent profile configuration context is unavailable")]
ContextUnavailable,
#[error("agent profile runtime configuration failed: {source}")]
Runtime {
#[source]
source: RuntimeError,
},
#[error("agent profile loop configuration is invalid: {source}")]
LoopConfig {
#[source]
source: AgentLoopConfigError,
},
}
#[derive(Debug, Error)]
pub enum AgentBuildError {
#[error("a primary model provider is required")]
MissingPrimaryProvider,
#[error("runtime build failed: {source}")]
Runtime {
#[source]
source: RuntimeError,
},
#[error("agent profile loop configuration is invalid: {source}")]
LoopConfig {
#[source]
source: AgentLoopConfigError,
},
#[error("agent profile configuration failed: {source}")]
Profile {
#[source]
source: AgentProfileError,
},
#[error("tool configuration is invalid: {source}")]
Tool {
#[source]
source: crate::tools::ToolBuildError,
},
}
impl AgentBuildError {
#[must_use]
pub fn diagnostic_code(&self) -> &'static str {
match self {
Self::MissingPrimaryProvider => "missing_primary_provider",
Self::Runtime { source } => source.diagnostic_code(),
Self::LoopConfig { .. } => "agent_loop_config",
Self::Profile { source } => match source {
AgentProfileError::ContextUnavailable => "profile_context_unavailable",
AgentProfileError::Runtime { source } => source.diagnostic_code(),
AgentProfileError::LoopConfig { .. } => "agent_loop_config",
},
Self::Tool { source } => match source {
crate::tools::ToolBuildError::Core(_) => "tool_core_error",
crate::tools::ToolBuildError::ReservedName { .. } => "reserved_tool_name",
},
}
}
}
impl From<RuntimeError> for AgentBuildError {
fn from(source: RuntimeError) -> Self {
Self::Runtime { source }
}
}
impl From<AgentLoopConfigError> for AgentBuildError {
fn from(source: AgentLoopConfigError) -> Self {
Self::LoopConfig { source }
}
}
impl From<AgentProfileError> for AgentBuildError {
fn from(source: AgentProfileError) -> Self {
Self::Profile { source }
}
}
#[derive(Debug, Error)]
pub enum AgentError {
#[error("agent input or runtime operation failed: {source}")]
Runtime {
#[source]
source: RuntimeError,
},
#[error("agent loop failed: {source}")]
Loop {
#[source]
source: AgentLoopError,
},
#[error("structured output contract is invalid: {source}")]
FinalOutputContract {
#[source]
source: FinalOutputContractError,
},
#[error("structured output was not recorded by the agent loop")]
StructuredOutputNotRecorded {
run: Box<crate::RunResult>,
},
#[error("structured output could not be decoded: {source}")]
StructuredOutputDecode {
run: Box<crate::RunResult>,
#[source]
source: serde_json::Error,
},
#[error("interactive agent operation failed: {source}")]
Interactive {
#[source]
source: InteractiveError,
},
#[error(
"tool invocation results do not match active calls: expected {expected_call_ids:?}, received {received_call_ids:?}"
)]
ToolInvocationBatchMismatch {
expected_call_ids: Vec<ToolCallId>,
received_call_ids: Vec<ToolCallId>,
},
#[error("tool invocation batch has already been resolved")]
ToolInvocationBatchResolved,
#[error("tool invocation batch must be submitted or cancelled before the run advances")]
ToolInvocationBatchPending,
#[error("tool invocation batch is not pending")]
ToolInvocationBatchNotPending,
#[error("hook invocation must be resolved before the run advances")]
HookInvocationPending,
#[error("hook invocation is not pending")]
HookInvocationNotPending,
#[error(
"event-only agent stream encountered a host tool handoff; use stream_with_tool_handoff"
)]
ToolHandoffRequired,
#[error("agent run has not reached its terminal boundary")]
AgentRunNotFinished,
#[error("agent run terminal result was already consumed")]
AgentRunResultConsumed,
#[error("agent run reached EOF without a terminal result")]
AgentRunResultMissing,
#[error("agent run protocol error: {message}")]
AgentRunProtocol {
message: &'static str,
},
#[error("interactive protocol error: {message}")]
InteractiveProtocol {
message: &'static str,
},
}
impl AgentError {
#[must_use]
pub fn diagnostic_code(&self) -> &'static str {
match self {
Self::Runtime { source } => source.diagnostic_code(),
Self::Loop { source } => source.runtime_error().diagnostic_code(),
Self::FinalOutputContract { .. } => "final_output_contract",
Self::StructuredOutputNotRecorded { .. } => "structured_output_not_recorded",
Self::StructuredOutputDecode { .. } => "structured_output_decode",
Self::Interactive { .. } => "interactive_error",
Self::ToolInvocationBatchMismatch { .. } => "tool_batch_mismatch",
Self::ToolInvocationBatchResolved => "tool_batch_resolved",
Self::ToolInvocationBatchPending => "tool_batch_pending",
Self::ToolInvocationBatchNotPending => "tool_batch_not_pending",
Self::HookInvocationPending => "hook_invocation_pending",
Self::HookInvocationNotPending => "hook_invocation_not_pending",
Self::ToolHandoffRequired => "tool_handoff_required",
Self::AgentRunNotFinished => "agent_run_not_finished",
Self::AgentRunResultConsumed => "agent_run_result_consumed",
Self::AgentRunResultMissing => "agent_run_result_missing",
Self::AgentRunProtocol { .. } => "agent_run_protocol",
Self::InteractiveProtocol { .. } => "interactive_protocol",
}
}
#[must_use]
pub fn structured_run(&self) -> Option<&crate::RunResult> {
match self {
Self::StructuredOutputNotRecorded { run }
| Self::StructuredOutputDecode { run, .. } => Some(run),
_ => None,
}
}
#[must_use]
pub fn into_structured_run(self) -> Option<crate::RunResult> {
match self {
Self::StructuredOutputNotRecorded { run }
| Self::StructuredOutputDecode { run, .. } => Some(*run),
_ => None,
}
}
}
impl From<RuntimeError> for AgentError {
fn from(source: RuntimeError) -> Self {
Self::Runtime { source }
}
}
impl From<AgentLoopError> for AgentError {
fn from(source: AgentLoopError) -> Self {
Self::Loop { source }
}
}
impl From<InteractiveError> for AgentError {
fn from(source: InteractiveError) -> Self {
Self::Interactive { source }
}
}