use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::JsonObject;
use crate::error::{Error, Result};
use crate::ids::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>,
}
impl Interrupt {
pub fn new(id: impl Into<String>, reason: impl Into<String>) -> Self {
Self {
id: id.into(),
reason: reason.into(),
..Default::default()
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub enum RunOutcome {
Success,
Interrupt {
interrupts: Vec<Interrupt>,
},
}
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>,
}
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()),
}
}
pub fn cancelled(interrupt_id: impl Into<String>) -> Self {
Self {
interrupt_id: interrupt_id.into(),
status: ResumeStatus::Cancelled,
payload: None,
}
}
}