tilo_actions/remote/content_get.rs
1//! Content gets: how a caller reads bytes back out of its workspace's
2//! content store, a piece at a time, by the reference a put or an action
3//! answered with (`tilo.content:<hash>`).
4//!
5//! A put is how bytes get in ([`super::ContentPuts`]); a get is how they
6//! come out, for a caller that keeps no copy of the workspace — an agent's
7//! MCP session through its own device. Reads are bounded like puts: one
8//! request answers one piece a frame carries, and the caller asks again at
9//! the offset the piece ends at.
10//!
11//! Only the store of the workspace the caller is in answers: a hash held by
12//! another workspace, or by no workspace, reads as no such content.
13
14use super::{ActionCallError, Caller};
15
16/// What a provider without a content store answers.
17pub const CONTENT_GETS_UNAVAILABLE: &str = "reading content is not available here";
18
19/// One piece of content, as a provider answers it.
20#[derive(Clone, Debug, PartialEq, Eq)]
21pub struct ContentPiece {
22 /// The whole content's length.
23 pub size: u64,
24 /// Where this piece starts.
25 pub offset: u64,
26 /// The piece.
27 pub bytes: Vec<u8>,
28}
29
30/// A workspace's content store as a caller reads it.
31pub trait ContentGets: Send + Sync {
32 /// The bytes of `content` (`tilo.content:<hash>`) from `offset`, at most
33 /// `length` of them; `length` of zero asks for as many as one piece
34 /// carries.
35 ///
36 /// # Errors
37 ///
38 /// [`ActionCallError::InvalidRequest`] for a reference that is not one,
39 /// content the caller's workspace does not hold, an offset past its end,
40 /// or a piece longer than [`super::MAX_CONTENT_FRAME_BYTES`].
41 fn get(
42 &self,
43 caller: &Caller,
44 content: &str,
45 offset: u64,
46 length: u64,
47 ) -> Result<ContentPiece, ActionCallError>;
48}
49
50/// What a request for content answers: the whole content's size, where the
51/// piece starts, and the piece itself.
52#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
53#[serde(deny_unknown_fields)]
54pub struct ContentGetAnswer {
55 /// The whole content's length.
56 pub size: u64,
57 /// Where this piece starts.
58 pub offset: u64,
59 /// The piece, hex-encoded so the frame stays self-describing text.
60 bytes: String,
61}
62
63impl ContentGetAnswer {
64 /// The answer for `piece`.
65 ///
66 /// # Errors
67 ///
68 /// A piece longer than [`super::MAX_CONTENT_FRAME_BYTES`], which no
69 /// frame carries.
70 pub fn new(piece: ContentPiece) -> Result<Self, ActionCallError> {
71 if piece.bytes.len() > super::MAX_CONTENT_FRAME_BYTES {
72 return Err(ActionCallError::InvalidRequest {
73 reason: format!(
74 "a piece carries at most {} bytes",
75 super::MAX_CONTENT_FRAME_BYTES
76 ),
77 });
78 }
79 Ok(Self {
80 size: piece.size,
81 offset: piece.offset,
82 bytes: hex::encode(piece.bytes),
83 })
84 }
85
86 /// The piece's bytes; `None` when they are not well-formed.
87 #[must_use]
88 pub fn bytes(&self) -> Option<Vec<u8>> {
89 if !self.bytes.len().is_multiple_of(2)
90 || self.bytes.len() > super::MAX_CONTENT_FRAME_BYTES * 2
91 {
92 return None;
93 }
94 hex::decode(&self.bytes).ok()
95 }
96
97 /// Where a next piece starts, or `None` when this one ends the content.
98 #[must_use]
99 pub fn next_offset(&self) -> Option<u64> {
100 let end = self
101 .offset
102 .saturating_add(self.bytes.len().div_ceil(2) as u64);
103 (end < self.size).then_some(end)
104 }
105}
106
107#[cfg(test)]
108mod tests {
109
110 use super::*;
111
112 #[test]
113 fn a_piece_names_where_the_next_one_starts() {
114 let answer = ContentGetAnswer::new(ContentPiece {
115 size: 10,
116 offset: 0,
117 bytes: b"abcd".to_vec(),
118 })
119 .unwrap();
120 assert_eq!(answer.bytes(), Some(b"abcd".to_vec()));
121 assert_eq!(answer.next_offset(), Some(4));
122
123 let last = ContentGetAnswer::new(ContentPiece {
124 size: 6,
125 offset: 4,
126 bytes: b"ef".to_vec(),
127 })
128 .unwrap();
129 assert_eq!(last.next_offset(), None, "the end of the content");
130 assert_eq!(last.bytes(), Some(b"ef".to_vec()));
131
132 let empty = ContentGetAnswer::new(ContentPiece {
133 size: 0,
134 offset: 0,
135 bytes: Vec::new(),
136 })
137 .unwrap();
138 assert_eq!(empty.bytes(), Some(Vec::new()));
139 assert_eq!(empty.next_offset(), None);
140 }
141}