strands/types/
snapshot.rs1use serde::{Deserialize, Serialize};
4
5use super::json::JsonObject;
6
7pub const SNAPSHOT_SCHEMA_VERSION: &str = "1.0";
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
12#[serde(rename_all = "camelCase")]
13#[non_exhaustive]
14pub enum Scope {
15 Agent,
17 MultiAgent,
19}
20
21#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
23#[serde(rename_all = "camelCase")]
24#[non_exhaustive]
25pub struct Snapshot {
26 pub scope: Scope,
28 pub schema_version: String,
30 pub created_at: String,
32 pub data: JsonObject,
34 pub app_data: JsonObject,
36}
37
38impl Snapshot {
39 pub fn new(scope: Scope, created_at: impl Into<String>, data: JsonObject) -> Self {
41 Self {
42 scope,
43 schema_version: SNAPSHOT_SCHEMA_VERSION.to_owned(),
44 created_at: created_at.into(),
45 data,
46 app_data: JsonObject::new(),
47 }
48 }
49
50 #[must_use]
52 pub fn with_app_data(mut self, app_data: JsonObject) -> Self {
53 self.app_data = app_data;
54 self
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use serde_json::json;
61
62 use super::*;
63
64 #[test]
65 fn snapshot_serializes_in_ts_key_order() {
66 let mut data = JsonObject::new();
67 data.insert("messages".into(), json!([]));
68 let snapshot = Snapshot::new(Scope::Agent, "2026-11-11T12:00:00.000Z", data)
69 .with_app_data(json!({"k": 1}).as_object().unwrap().clone());
70 assert_eq!(
71 serde_json::to_string(&snapshot).unwrap(),
72 r#"{"scope":"agent","schemaVersion":"1.0","createdAt":"2026-11-11T12:00:00.000Z","data":{"messages":[]},"appData":{"k":1}}"#
73 );
74 }
75
76 #[test]
77 fn multi_agent_scope_uses_camel_case() {
78 assert_eq!(serde_json::to_value(Scope::MultiAgent).unwrap(), json!("multiAgent"));
79 }
80}