#![allow(
unused_imports,
dead_code,
non_camel_case_types,
unused_variables,
clippy::all
)]
use super::super::context::{LoadContext, SaveContext};
use super::redaction_metadata::RedactionMetadata;
#[derive(Debug, Clone, Default)]
pub struct Checkpoint {
pub id: Option<String>,
pub session_id: Option<String>,
pub turn_id: Option<String>,
pub checkpoint_number: Option<i32>,
pub title: String,
pub overview: Option<String>,
pub state: serde_json::Value,
pub summary: Option<String>,
pub metadata: serde_json::Value,
pub created_at: Option<String>,
pub redaction: Option<RedactionMetadata>,
}
impl Checkpoint {
pub fn new() -> Self {
Self::default()
}
pub fn from_json(json: &str, ctx: &LoadContext) -> Result<Self, serde_json::Error> {
let value: serde_json::Value = serde_json::from_str(json)?;
Ok(Self::load_from_value(&value, ctx))
}
pub fn from_yaml(yaml: &str, ctx: &LoadContext) -> Result<Self, serde_yaml::Error> {
let value: serde_json::Value = serde_yaml::from_str(yaml)?;
Ok(Self::load_from_value(&value, ctx))
}
pub fn load_from_value(value: &serde_json::Value, ctx: &LoadContext) -> Self {
let value = ctx.process_input(value.clone());
Self {
id: value
.get("id")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
session_id: value
.get("sessionId")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
turn_id: value
.get("turnId")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
checkpoint_number: value
.get("checkpointNumber")
.and_then(|v| v.as_i64())
.map(|v| v as i32),
title: value
.get("title")
.and_then(|v| v.as_str())
.unwrap_or_default()
.to_string(),
overview: value
.get("overview")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
state: value
.get("state")
.cloned()
.unwrap_or(serde_json::Value::Null),
summary: value
.get("summary")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
metadata: value
.get("metadata")
.cloned()
.unwrap_or(serde_json::Value::Null),
created_at: value
.get("createdAt")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
redaction: value
.get("redaction")
.filter(|v| v.is_object() || v.is_array() || v.is_string())
.map(|v| RedactionMetadata::load_from_value(v, ctx)),
}
}
pub fn to_value(&self, ctx: &SaveContext) -> serde_json::Value {
let mut result = serde_json::Map::new();
if let Some(ref val) = self.id {
result.insert("id".to_string(), serde_json::Value::String(val.clone()));
}
if let Some(ref val) = self.session_id {
result.insert(
"sessionId".to_string(),
serde_json::Value::String(val.clone()),
);
}
if let Some(ref val) = self.turn_id {
result.insert("turnId".to_string(), serde_json::Value::String(val.clone()));
}
if let Some(val) = self.checkpoint_number {
result.insert(
"checkpointNumber".to_string(),
serde_json::Value::Number(serde_json::Number::from(val)),
);
}
if !self.title.is_empty() {
result.insert(
"title".to_string(),
serde_json::Value::String(self.title.clone()),
);
}
if let Some(ref val) = self.overview {
result.insert(
"overview".to_string(),
serde_json::Value::String(val.clone()),
);
}
if !self.state.is_null() {
result.insert("state".to_string(), self.state.clone());
}
if let Some(ref val) = self.summary {
result.insert(
"summary".to_string(),
serde_json::Value::String(val.clone()),
);
}
if !self.metadata.is_null() {
result.insert("metadata".to_string(), self.metadata.clone());
}
if let Some(ref val) = self.created_at {
result.insert(
"createdAt".to_string(),
serde_json::Value::String(val.clone()),
);
}
if let Some(ref val) = self.redaction {
let nested = val.to_value(ctx);
if !nested.is_null() {
result.insert("redaction".to_string(), nested);
}
}
ctx.process_dict(serde_json::Value::Object(result))
}
pub fn to_json(&self, ctx: &SaveContext) -> Result<String, serde_json::Error> {
serde_json::to_string_pretty(&self.to_value(ctx))
}
pub fn to_yaml(&self, ctx: &SaveContext) -> Result<String, serde_yaml::Error> {
serde_yaml::to_string(&self.to_value(ctx))
}
pub fn as_state_dict(&self) -> Option<&serde_json::Map<String, serde_json::Value>> {
self.state.as_object()
}
pub fn as_metadata_dict(&self) -> Option<&serde_json::Map<String, serde_json::Value>> {
self.metadata.as_object()
}
}