use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Trace {
pub id: String,
pub session_id: String,
pub role: String,
pub content: String,
#[serde(default)]
pub metadata: HashMap<String, Value>,
pub created_at: DateTime<Utc>,
#[serde(skip_serializing_if = "Option::is_none")]
pub embedding: Option<String>,
}
impl Trace {
pub fn new(session_id: String, role: String, content: String) -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
session_id,
role,
content,
metadata: HashMap::new(),
created_at: Utc::now(),
embedding: None,
}
}
pub fn with_metadata(mut self, metadata: HashMap<String, Value>) -> Self {
self.metadata = metadata;
self
}
pub fn add_metadata(&mut self, key: String, value: Value) {
self.metadata.insert(key, value);
}
pub fn get_metadata(&self, key: &str) -> Option<&Value> {
self.metadata.get(key)
}
pub fn is_success(&self) -> bool {
self.metadata.get("success").and_then(|v| v.as_bool()).unwrap_or(true)
}
}