#![allow(
unused_imports,
dead_code,
non_camel_case_types,
unused_variables,
clippy::all
)]
use super::super::context::{LoadContext, SaveContext};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ReplayRecordKind {
Session,
Turn,
Summary,
}
impl Default for ReplayRecordKind {
fn default() -> Self {
Self::Session
}
}
impl std::fmt::Display for ReplayRecordKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Session => write!(f, "session"),
Self::Turn => write!(f, "turn"),
Self::Summary => write!(f, "summary"),
}
}
}
impl ReplayRecordKind {
pub fn from_str_opt(s: &str) -> Option<Self> {
match s {
"session" => Some(Self::Session),
"turn" => Some(Self::Turn),
"summary" => Some(Self::Summary),
_ => None,
}
}
pub fn as_str(&self) -> &str {
match self {
Self::Session => "session",
Self::Turn => "turn",
Self::Summary => "summary",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ReplayRecordStatus {
Success,
Error,
Cancelled,
}
impl Default for ReplayRecordStatus {
fn default() -> Self {
Self::Success
}
}
impl std::fmt::Display for ReplayRecordStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Success => write!(f, "success"),
Self::Error => write!(f, "error"),
Self::Cancelled => write!(f, "cancelled"),
}
}
}
impl ReplayRecordStatus {
pub fn from_str_opt(s: &str) -> Option<Self> {
match s {
"success" => Some(Self::Success),
"error" => Some(Self::Error),
"cancelled" => Some(Self::Cancelled),
_ => None,
}
}
pub fn as_str(&self) -> &str {
match self {
Self::Success => "success",
Self::Error => "error",
Self::Cancelled => "cancelled",
}
}
}
#[derive(Debug, Clone, Default)]
pub struct ReplayJournalRecord {
pub kind: ReplayRecordKind,
pub r#type: Option<String>,
pub session_id: Option<String>,
pub turn_id: Option<String>,
pub iteration: Option<i32>,
pub status: Option<ReplayRecordStatus>,
pub request_id: Option<String>,
pub tool_name: Option<String>,
pub success: Option<bool>,
pub error_kind: Option<String>,
pub turns: Option<i32>,
pub checkpoints: Option<i32>,
}
impl ReplayJournalRecord {
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 {
kind: value
.get("kind")
.and_then(|v| v.as_str())
.and_then(|s| ReplayRecordKind::from_str_opt(s))
.unwrap_or(ReplayRecordKind::Session),
r#type: value
.get("type")
.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()),
iteration: value
.get("iteration")
.and_then(|v| v.as_i64())
.map(|v| v as i32),
status: value
.get("status")
.and_then(|v| v.as_str())
.and_then(|s| ReplayRecordStatus::from_str_opt(s)),
request_id: value
.get("requestId")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
tool_name: value
.get("toolName")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
success: value.get("success").and_then(|v| v.as_bool()),
error_kind: value
.get("errorKind")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
turns: value
.get("turns")
.and_then(|v| v.as_i64())
.map(|v| v as i32),
checkpoints: value
.get("checkpoints")
.and_then(|v| v.as_i64())
.map(|v| v as i32),
}
}
pub fn to_value(&self, ctx: &SaveContext) -> serde_json::Value {
let mut result = serde_json::Map::new();
result.insert(
"kind".to_string(),
serde_json::Value::String(self.kind.to_string()),
);
if let Some(ref val) = self.r#type {
result.insert("type".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.iteration {
result.insert(
"iteration".to_string(),
serde_json::Value::Number(serde_json::Number::from(val)),
);
}
if let Some(ref val) = self.status {
result.insert(
"status".to_string(),
serde_json::Value::String(val.to_string()),
);
}
if let Some(ref val) = self.request_id {
result.insert(
"requestId".to_string(),
serde_json::Value::String(val.clone()),
);
}
if let Some(ref val) = self.tool_name {
result.insert(
"toolName".to_string(),
serde_json::Value::String(val.clone()),
);
}
if let Some(val) = self.success {
result.insert("success".to_string(), serde_json::Value::Bool(val));
}
if let Some(ref val) = self.error_kind {
result.insert(
"errorKind".to_string(),
serde_json::Value::String(val.clone()),
);
}
if let Some(val) = self.turns {
result.insert(
"turns".to_string(),
serde_json::Value::Number(serde_json::Number::from(val)),
);
}
if let Some(val) = self.checkpoints {
result.insert(
"checkpoints".to_string(),
serde_json::Value::Number(serde_json::Number::from(val)),
);
}
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))
}
}