use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::JsonObject;
use crate::error::{Error, Result};
use crate::ids::{SubagentRunId, ToolCallId};
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct Interrupt {
pub id: String,
pub reason: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tool_call_id: Option<ToolCallId>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(
feature = "schemars",
schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
)]
#[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
pub response_schema: Option<JsonObject>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub expires_at: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(
feature = "schemars",
schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
)]
#[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
pub metadata: Option<JsonObject>,
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
pub subagent_run_id: Option<SubagentRunId>,
}
impl Interrupt {
pub fn new(id: impl Into<String>, reason: impl Into<String>) -> Self {
Self {
id: id.into(),
reason: reason.into(),
..Default::default()
}
}
#[must_use]
pub fn with_subagent_run_id(mut self, subagent_run_id: impl Into<SubagentRunId>) -> Self {
self.subagent_run_id = Some(subagent_run_id.into());
self
}
}
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(tag = "type", rename_all = "lowercase", deny_unknown_fields)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub enum RunOutcome {
Success,
Interrupt {
interrupts: Vec<Interrupt>,
},
}
impl<'de> Deserialize<'de> for RunOutcome {
fn deserialize<D: serde::Deserializer<'de>>(
deserializer: D,
) -> std::result::Result<Self, D::Error> {
#[derive(Deserialize)]
#[serde(tag = "type", rename_all = "lowercase", deny_unknown_fields)]
enum Wire {
Success {},
Interrupt { interrupts: Vec<Interrupt> },
}
Ok(match Wire::deserialize(deserializer)? {
Wire::Success {} => Self::Success,
Wire::Interrupt { interrupts } => Self::Interrupt { interrupts },
})
}
}
impl RunOutcome {
pub fn interrupt(interrupts: impl Into<Vec<Interrupt>>) -> Self {
Self::Interrupt {
interrupts: interrupts.into(),
}
}
pub const fn is_interrupt(&self) -> bool {
matches!(self, Self::Interrupt { .. })
}
pub fn interrupts(&self) -> &[Interrupt] {
match self {
Self::Success => &[],
Self::Interrupt { interrupts } => interrupts,
}
}
pub fn validate(&self) -> Result<()> {
match self {
Self::Interrupt { interrupts } if interrupts.is_empty() => Err(Error::Protocol(
"RUN_FINISHED outcome `interrupt` requires at least one interrupt".to_owned(),
)),
_ => Ok(()),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub enum ResumeStatus {
Resolved,
Cancelled,
}
impl ResumeStatus {
pub const fn as_str(&self) -> &'static str {
match self {
Self::Resolved => "resolved",
Self::Cancelled => "cancelled",
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct ResumeEntry {
pub interrupt_id: String,
pub status: ResumeStatus,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub payload: Option<Value>,
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
#[cfg_attr(
feature = "schemars",
schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
)]
#[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
pub metadata: Option<JsonObject>,
}
impl ResumeEntry {
pub fn resolved(interrupt_id: impl Into<String>, payload: impl Into<Value>) -> Self {
Self {
interrupt_id: interrupt_id.into(),
status: ResumeStatus::Resolved,
payload: Some(payload.into()),
metadata: None,
}
}
pub fn cancelled(interrupt_id: impl Into<String>) -> Self {
Self {
interrupt_id: interrupt_id.into(),
status: ResumeStatus::Cancelled,
payload: None,
metadata: None,
}
}
#[must_use]
pub fn with_metadata(mut self, metadata: JsonObject) -> Self {
self.metadata = Some(metadata);
self
}
}