use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ToolCallId(pub String);
impl ToolCallId {
pub fn new(s: impl Into<String>) -> Self {
Self(s.into())
}
pub fn generate(
round_id: crate::context::ids::RoundId,
tool_name: &str,
arguments: &serde_json::Value,
position: usize,
) -> Self {
let json = serde_json::to_string(arguments)
.unwrap_or_else(|_| "<unserializable-arguments>".to_string());
let preimage = format!("{}|{}|{}", round_id.0, tool_name, json);
let hash = blake3::hash(preimage.as_bytes());
let hex = hash.to_hex();
Self(format!("{}:{}:{}", tool_name, &hex[..8], position))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ToolResultStatus {
Succeeded,
Failed,
Rejected,
Cancelled,
TimedOut,
UnknownOutcome,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
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)]
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)]
pub struct ToolResultPayload {
pub call_id: ToolCallId,
pub status: ToolResultStatus,
pub output: ToolOutput,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub media: Vec<crate::context::block::MediaRef>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ArtifactRef {
pub id: String,
pub size_bytes: usize,
pub kind: ArtifactKind,
pub persisted: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ArtifactKind {
FullOutput,
PipeCache,
Binary,
}