use serde::{Deserialize, Serialize};
use serde_json::Value;
pub type Map = serde_json::Map<String, Value>;
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Trace {
#[serde(default)]
pub steps: Vec<Step>,
#[serde(default, skip_serializing_if = "Map::is_empty")]
pub meta: Map,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum StepKind {
Message,
ToolCall,
ToolResult,
Event,
#[serde(untagged)]
Other(String),
}
impl StepKind {
pub fn tag(&self) -> &str {
match self {
StepKind::Message => "message",
StepKind::ToolCall => "tool_call",
StepKind::ToolResult => "tool_result",
StepKind::Event => "event",
StepKind::Other(s) => s,
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Step {
pub kind: StepKind,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub role: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub args: Option<Value>,
#[serde(default, skip_serializing_if = "Map::is_empty")]
pub meta: Map,
}
impl Step {
pub fn new(kind: StepKind) -> Self {
Step {
kind,
role: None,
name: None,
content: None,
args: None,
meta: Map::new(),
}
}
pub fn with_role(mut self, role: impl Into<String>) -> Self {
self.role = Some(role.into());
self
}
pub fn with_name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn with_content(mut self, content: impl Into<String>) -> Self {
self.content = Some(content.into());
self
}
pub fn with_args(mut self, args: Value) -> Self {
self.args = Some(args);
self
}
pub fn label(&self) -> String {
let mut s = self.kind.tag().to_string();
if let Some(role) = &self.role {
s.push(' ');
s.push_str(role);
}
if let Some(name) = &self.name {
s.push_str(" ·");
s.push(' ');
s.push_str(name);
}
s
}
}
impl Trace {
pub fn new() -> Self {
Trace::default()
}
pub fn from_steps(steps: Vec<Step>) -> Self {
Trace {
steps,
meta: Map::new(),
}
}
pub fn len(&self) -> usize {
self.steps.len()
}
pub fn is_empty(&self) -> bool {
self.steps.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn step_label_includes_kind_role_and_name() {
let step = Step::new(StepKind::ToolCall).with_name("get_weather");
assert_eq!(step.label(), "tool_call · get_weather");
let msg = Step::new(StepKind::Message).with_role("assistant");
assert_eq!(msg.label(), "message assistant");
}
#[test]
fn kind_roundtrips_through_json_including_other() {
for kind in [
StepKind::Message,
StepKind::ToolCall,
StepKind::ToolResult,
StepKind::Event,
StepKind::Other("reasoning".into()),
] {
let json = serde_json::to_string(&kind).unwrap();
let back: StepKind = serde_json::from_str(&json).unwrap();
assert_eq!(kind, back);
}
}
#[test]
fn other_kind_tag_is_the_inner_string() {
assert_eq!(StepKind::Other("reasoning".into()).tag(), "reasoning");
}
}