use crate::kernel::{GraphId, RunId};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use svix_ksuid::KsuidLike;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Checkpoint {
pub id: String,
pub run_id: RunId,
pub graph_id: Option<GraphId>,
pub current_node: Option<String>,
pub state: Value,
pub messages: Vec<MessageRecord>,
pub tool_results: HashMap<String, Value>,
pub created_at: chrono::DateTime<chrono::Utc>,
pub metadata: HashMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MessageRecord {
pub role: String,
pub content: String,
}
impl Checkpoint {
pub fn new(run_id: RunId) -> Self {
Self {
id: format!("ckpt_{}", svix_ksuid::Ksuid::new(None, None)),
run_id,
graph_id: None,
current_node: None,
state: Value::Null,
messages: Vec::new(),
tool_results: HashMap::new(),
created_at: chrono::Utc::now(),
metadata: HashMap::new(),
}
}
pub fn with_state(mut self, state: Value) -> Self {
self.state = state;
self
}
pub fn with_node(mut self, node: impl Into<String>) -> Self {
self.current_node = Some(node.into());
self
}
pub fn add_message(&mut self, role: impl Into<String>, content: impl Into<String>) {
self.messages.push(MessageRecord {
role: role.into(),
content: content.into(),
});
}
pub fn add_tool_result(&mut self, tool_name: impl Into<String>, result: Value) {
self.tool_results.insert(tool_name.into(), result);
}
}