rig_memory/migration/
testing.rs1use rig_core::conversation::{Aside, Author, Conversation};
18use rig_core::message::{ImageMediaType, Message, UserContent};
19use serde_json::json;
20
21use super::*;
22use crate::{BLOBS, Files};
23
24pub const SYNTHETIC: &str = "rig-session/synthetic";
27
28pub const SYNTHETIC_BLOBS: &str = "synthetic-blobs/";
30
31pub const UNINSTALLED: &str = "uninstalled.state";
34
35pub const UNINSTALLED_FILE: &str = "plugins/uninstalled/data";
37
38pub const CHAIN: Chain = Chain {
40 steps: &[Step {
41 from: SYNTHETIC,
42 to: FORMAT,
43 apply: from_synthetic,
44 #[cfg(test)]
45 fixture,
46 }],
47};
48
49fn from_synthetic(session: &mut Snapshot) -> Result<(), SessionError> {
52 rename_format(session, FORMAT)?;
53 move_files(session, SYNTHETIC_BLOBS, BLOBS);
54 Ok(())
55}
56
57pub fn write_fixture(store: &dyn Store) -> Result<(), SessionError> {
66 let snapshot = Snapshot::read(&fixture()?)?;
67 snapshot.write_to(store)
68}
69
70fn fixture() -> Result<MemoryStore, SessionError> {
71 let store = MemoryStore::default();
72 let mut conversation = Conversation::from_messages([
73 Message::User {
74 content: vec![
75 UserContent::text("a synthetic session"),
76 UserContent::image_base64("cG5n", Some(ImageMediaType::PNG), None),
77 ],
78 },
79 Message::assistant("seen"),
80 ]);
81 conversation.add_aside(Aside::new(
82 "uninstalled.aside",
83 Author::plugin("uninstalled"),
84 json!({"kept": [1, "as is"]}),
85 ));
86 crate::save(&store, "agent", &mut conversation)?;
87 let mut session = Session::read(&store)?;
88 session
89 .saved
90 .insert(UNINSTALLED.to_owned(), json!({"kept": [false, "as is"]}));
91 session.save(&store)?;
92 store.write_file(META, br#"{"title":"synthetic"}"#)?;
93 store.write_file(UNINSTALLED_FILE, b"opaque\0\xff bytes")?;
94 let mut snapshot = Snapshot::read(&store)?;
95 rename_format(&mut snapshot, SYNTHETIC)?;
96 move_files(&mut snapshot, BLOBS, SYNTHETIC_BLOBS);
97 let synthetic = MemoryStore::default();
98 snapshot.write_to(&synthetic)?;
99 Ok(synthetic)
100}
101
102fn rename_format(session: &mut Snapshot, to: &str) -> Result<(), SessionError> {
104 for bytes in session.logs.values_mut() {
105 let Some((first, rest)) =
106 memchr::memchr(b'\n', bytes).and_then(|at| bytes.split_at_checked(at))
107 else {
108 continue;
109 };
110 let mut header: Value = serde_json::from_slice(first).map_err(invalid_json)?;
111 if let Some(header) = header.as_object_mut() {
112 header.insert("format".to_owned(), to.into());
113 }
114 let mut renamed = serde_json::to_vec(&header).map_err(invalid_json)?;
115 renamed.extend_from_slice(rest);
116 *bytes = renamed;
117 }
118 Ok(())
119}
120
121fn move_files(session: &mut Snapshot, from: &str, to: &str) {
123 let moved: Vec<String> = session
124 .files
125 .keys()
126 .filter(|name| name.starts_with(from))
127 .cloned()
128 .collect();
129 for name in moved {
130 if let (Some(bytes), Some(rest)) = (session.files.remove(&name), name.strip_prefix(from)) {
131 session.files.insert(format!("{to}{rest}"), bytes);
132 }
133 }
134}