use crate::context::ids::BlockId;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ToolCallId<'a> {
pub tool_name: &'a str,
pub arguments: &'a serde_json::Value,
}
impl<'a> ToolCallId<'a> {
pub const fn new(tool_name: &'a str, arguments: &'a serde_json::Value) -> Self {
Self {
tool_name,
arguments,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ToolResultStatus {
Succeeded,
Failed,
Rejected,
Cancelled,
TimedOut,
UnknownOutcome,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ToolOutputMeta {
pub duration_ms: Option<u64>,
pub original_tokens: Option<usize>,
pub extra: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Truncation {
None,
Middle,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ToolOutput {
pub content: serde_json::Value,
pub truncation: Truncation,
pub meta: Option<ToolOutputMeta>,
pub artifact: Option<ArtifactRef>,
}
impl ToolOutput {
pub fn new(content: serde_json::Value) -> Self {
Self {
content,
truncation: Truncation::None,
meta: None,
artifact: None,
}
}
pub fn is_truncated(&self) -> bool {
!matches!(self.truncation, Truncation::None)
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ToolResultPayload {
pub call_block_id: BlockId,
pub status: ToolResultStatus,
pub output: ToolOutput,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub media: Vec<crate::context::block::MediaRef>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub notes: Vec<crate::context::block::TextPayload>,
}
impl ToolResultPayload {
pub fn new(call_block_id: BlockId, status: ToolResultStatus, output: ToolOutput) -> Self {
Self {
call_block_id,
status,
output,
media: Vec::new(),
notes: Vec::new(),
}
}
pub fn with_media(mut self, media: Vec<crate::context::block::MediaRef>) -> Self {
self.media = media;
self
}
pub fn with_notes(mut self, notes: Vec<crate::context::block::TextPayload>) -> Self {
self.notes = notes;
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ArtifactRef {
pub id: String,
pub size_bytes: usize,
pub kind: ArtifactKind,
pub persisted: bool,
}
impl ArtifactRef {
pub fn new(
id: impl Into<String>,
size_bytes: usize,
kind: ArtifactKind,
persisted: bool,
) -> Self {
Self {
id: id.into(),
size_bytes,
kind,
persisted,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ArtifactKind {
FullOutput,
PipeCache,
Binary,
}