Skip to main content

rig_memory/
blobs.rs

1//! Images kept as blobs: a saved message names each image's blob,
2//! `blob:<sha256>.<ext>`, in place of its data, and the store keeps the
3//! blob as the file `blobs/<sha256>.<ext>` ([`BLOBS`]). A message is saved
4//! in a push or a splice.
5
6use std::borrow::Cow;
7use std::io;
8use std::sync::Arc;
9
10use base64::Engine;
11use base64::engine::general_purpose::STANDARD;
12use rig_core::completion::Message;
13use rig_core::conversation::{Author, Change, ChangeRecord};
14use rig_core::message::DocumentSourceKind::{Base64, Raw, Unknown, Url};
15use rig_core::message::{Image, ImageMediaType};
16use rig_core::tool::content_hash;
17
18use crate::Files;
19
20/// How a saved message names an image stored as a blob, in place of its
21/// data: `blob:<sha256>.<ext>`.
22const BLOB: &str = "blob:";
23
24/// Where a store keeps the blobs its messages name: files under `blobs/`,
25/// each named by a hash of its content.
26pub const BLOBS: &str = "blobs/";
27
28/// Stores the blob `name` in `files`: a name is a hash of the content, so
29/// writing it again changes nothing.
30pub(crate) fn put_blob(files: &(impl Files + ?Sized), name: &str, bytes: &[u8]) -> io::Result<()> {
31    files.write_file(&format!("{BLOBS}{name}"), bytes)
32}
33
34/// The blob `name` of `files`; [`io::ErrorKind::NotFound`] when there is
35/// none.
36pub(crate) fn read_blob(files: &(impl Files + ?Sized), name: &str) -> io::Result<Vec<u8>> {
37    files.read_file(&format!("{BLOBS}{name}"))
38}
39
40/// The names of the blobs `files` keeps.
41pub(crate) fn blob_names(files: &(impl Files + ?Sized)) -> io::Result<Vec<String>> {
42    let names = files.files()?.into_iter();
43    Ok(names
44        .filter_map(|name| Some(name.strip_prefix(BLOBS)?.to_owned()))
45        .collect())
46}
47
48fn inline(image: &Image) -> bool {
49    matches!(image.data, Base64(_) | Raw(_))
50}
51
52/// Where a save puts each blob it stores, by name.
53pub(crate) type PutBlob<'a> = dyn FnMut(String, Vec<u8>) -> io::Result<()> + 'a;
54
55/// `message` as it is saved: the data of each image in it given to
56/// `put` once per content, and named there instead. Borrowed when it has
57/// no image data to store; data that is not valid base64 stays inline.
58///
59/// # Errors
60///
61/// When `put` fails.
62pub(crate) fn store_images_with<'m>(
63    message: &'m Message,
64    put: &mut PutBlob<'_>,
65) -> io::Result<Cow<'m, Message>> {
66    if !message.images().any(inline) {
67        return Ok(Cow::Borrowed(message));
68    }
69    let mut message = message.clone();
70    for image in message.images_mut() {
71        let bytes = match &image.data {
72            Base64(data) => STANDARD.decode(data).ok(),
73            Raw(bytes) => Some(bytes.clone()),
74            _ => None,
75        };
76        if let Some(bytes) = bytes {
77            let extension = image.media_type.as_ref();
78            let extension = extension.map_or("bin", ImageMediaType::extension);
79            let name = format!("{}.{extension}", content_hash(&bytes).0);
80            image.data = Url(format!("{BLOB}{name}"));
81            put(name, bytes)?;
82        }
83    }
84    Ok(Cow::Owned(message))
85}
86
87/// Puts the data of each image `message` names back in place, from the
88/// blobs `blob` reads.
89/// An image whose blob cannot be read has no data any more ([`Unknown`]),
90/// which a request sends as a placeholder; the first failure is returned,
91/// unless a later one is other than a missing blob, so a missing blob
92/// never hides a failing store.
93///
94/// # Errors
95///
96/// The first blob that could not be read for a reason other than that it
97/// is gone, else the first that is gone.
98pub(crate) fn load_images_with(
99    message: &mut Message,
100    blob: &dyn Fn(&str) -> io::Result<Vec<u8>>,
101) -> io::Result<()> {
102    let mut gone = None;
103    for image in message.images_mut() {
104        if let Url(url) = &image.data
105            && let Some(name) = url.strip_prefix(BLOB)
106        {
107            image.data = match blob(name) {
108                Ok(bytes) => Base64(STANDARD.encode(bytes)),
109                Err(failure) => {
110                    let missing = |failure: &io::Error| failure.kind() == io::ErrorKind::NotFound;
111                    match &gone {
112                        Some(first) if !missing(first) || missing(&failure) => {}
113                        _ => gone = Some(failure),
114                    }
115                    Unknown
116                }
117            };
118        }
119    }
120    gone.map_or(Ok(()), Err)
121}
122
123/// Adds the name of every blob `bytes` names (`blob:<name>`) to `named`.
124pub(crate) fn named_blobs(bytes: &[u8], named: &mut std::collections::BTreeSet<String>) {
125    let name_byte = |byte: &u8| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b'-');
126    let mut rest = bytes;
127    while let Some(at) = memchr::memmem::find(rest, BLOB.as_bytes()) {
128        let after = rest.get(at + BLOB.len()..).unwrap_or_default();
129        let len = after.iter().take_while(|byte| name_byte(byte)).count();
130        if let Some(name) = after
131            .get(..len)
132            .and_then(|name| std::str::from_utf8(name).ok())
133            && !name.is_empty()
134        {
135            named.insert(name.to_owned());
136        }
137        rest = after.get(len..).unwrap_or_default();
138    }
139}
140
141/// Calls `f` on each message `change` saves: the entries of a push or a
142/// splice, and those of a reset's whole value. It stops at `f`'s first
143/// error.
144pub(crate) fn messages_mut(
145    change: &mut Change,
146    f: &mut dyn FnMut(&mut Message) -> io::Result<()>,
147) -> io::Result<()> {
148    match change {
149        Change::Push { entries } | Change::Splice { with: entries, .. } => entries
150            .iter_mut()
151            .try_for_each(|entry| f(Arc::make_mut(&mut entry.message))),
152        Change::Reset { state } => {
153            let (mut done, halted) = (Ok(()), state.halted());
154            state.edit_each(Author::rig(), |entry| {
155                done.as_ref().ok()?;
156                let mut message = entry.message.clone();
157                done = f(&mut message);
158                (done.is_ok() && message != *entry.message).then_some(message)
159            });
160            // A splice that reaches the end clears the halt the value keeps.
161            let halt = ChangeRecord {
162                change: Change::Halt { halt: halted },
163                by: None,
164                at: None,
165            };
166            let put_back = state.apply(&halt).map_err(io::Error::other);
167            // The edits are of the value itself, not changes of it to save.
168            **state = state.view();
169            done.and(put_back)
170        }
171        _ => Ok(()),
172    }
173}
174
175/// `change` as it is saved, its messages' images stored as blobs
176/// ([`store_images_with`], [`messages_mut`]); `None` when it has none to store.
177/// The whole value of a [`Change::Reset`] keeps its images inline.
178pub(crate) fn store_change(change: &Change, put: &mut PutBlob<'_>) -> io::Result<Option<Change>> {
179    let has_inline = match change {
180        Change::Push { entries } | Change::Splice { with: entries, .. } => entries
181            .iter()
182            .any(|entry| entry.message.images().any(inline)),
183        _ => false,
184    };
185    if !has_inline {
186        return Ok(None);
187    }
188    let mut change = change.clone();
189    messages_mut(&mut change, &mut |message| {
190        if let Cow::Owned(stored) = store_images_with(message, put)? {
191            *message = stored;
192        }
193        Ok(())
194    })?;
195    Ok(Some(change))
196}