Skip to main content

clickhouse_loom/
models.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4/// A memory write. Unset options use the server's defaults.
5#[derive(Debug, Serialize)]
6#[non_exhaustive]
7pub struct SetRequest {
8    pub memory_key: String,
9    pub value: Value,
10    #[serde(skip_serializing_if = "Option::is_none")]
11    pub memory_type: Option<String>,
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub tags: Option<Vec<String>>,
14    #[serde(skip_serializing_if = "Option::is_none")]
15    pub ttl_seconds: Option<i64>,
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub confidence: Option<f64>,
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub if_match_sha256: Option<String>,
20}
21
22impl SetRequest {
23    pub fn new(memory_key: impl Into<String>, value: impl Into<Value>) -> Self {
24        Self {
25            memory_key: memory_key.into(),
26            value: value.into(),
27            memory_type: None,
28            tags: None,
29            ttl_seconds: None,
30            confidence: None,
31            if_match_sha256: None,
32        }
33    }
34}
35
36/// A search addressed to the client's namespace, across agents by default.
37#[derive(Debug, Serialize)]
38#[non_exhaustive]
39pub struct SearchRequest {
40    pub query: String,
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub top_k: Option<u32>,
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub tags: Option<Vec<String>>,
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub memory_type: Option<String>,
47    #[serde(skip_serializing_if = "Option::is_none")]
48    pub search_mode: Option<String>,
49    #[serde(rename = "agent", skip_serializing_if = "Option::is_none")]
50    pub agent_id: Option<String>,
51    #[serde(skip_serializing_if = "Option::is_none")]
52    pub namespace_scope: Option<String>,
53    #[serde(skip_serializing_if = "Option::is_none")]
54    pub user_id: Option<String>,
55    #[serde(skip_serializing_if = "Option::is_none")]
56    pub session_id: Option<String>,
57}
58
59impl SearchRequest {
60    pub fn new(query: impl Into<String>) -> Self {
61        Self {
62            query: query.into(),
63            top_k: None,
64            tags: None,
65            memory_type: None,
66            search_mode: None,
67            agent_id: None,
68            namespace_scope: None,
69            user_id: None,
70            session_id: None,
71        }
72    }
73}
74
75#[derive(Debug, Deserialize)]
76#[non_exhaustive]
77pub struct SetResponse {
78    pub version: u64,
79    #[serde(default)]
80    pub memory_id: String,
81    #[serde(default)]
82    pub content_sha256: String,
83    #[serde(default)]
84    pub status: String,
85    /// A merge or dry run can succeed without writing a row.
86    #[serde(default)]
87    pub noop: bool,
88    #[serde(default)]
89    pub merged_into_key: String,
90}
91
92/// A missing memory is a successful response with `found == false`.
93#[derive(Debug, Deserialize)]
94#[non_exhaustive]
95pub struct GetResponse {
96    pub found: bool,
97    #[serde(default)]
98    pub memory_id: String,
99    #[serde(default)]
100    pub value: Value,
101    pub version: Option<u64>,
102    pub memory_type: Option<String>,
103    pub confidence: Option<f64>,
104    pub pinned: Option<bool>,
105}
106
107#[derive(Debug, Deserialize)]
108#[non_exhaustive]
109pub struct SearchHit {
110    pub memory_key: String,
111    pub memory_type: String,
112    pub content_excerpt: String,
113    pub distance: f64,
114    pub similarity: f64,
115    pub version: Option<u64>,
116    pub namespace: Option<String>,
117    pub tags: Option<Vec<String>>,
118    /// None means the server did not disclose the per-hit threshold decision.
119    pub below_threshold: Option<bool>,
120    /// True on a governed definition; None when the server does not disclose it.
121    pub governed: Option<bool>,
122    pub reinforcement_count: Option<u64>,
123    pub used_count: Option<u64>,
124    pub used_cited_count: Option<u64>,
125    pub harm_count: Option<u64>,
126    pub evidence_count: Option<u64>,
127    pub acted_count: Option<u64>,
128    pub pinned: Option<bool>,
129    #[serde(default)]
130    pub rank_score: f64,
131    #[serde(default)]
132    pub temporal_anchor: String,
133    #[serde(default)]
134    pub temporal_confidence: f64,
135    #[serde(default)]
136    pub valid_at: String,
137    #[serde(default)]
138    pub via_path: Vec<String>,
139    #[serde(default)]
140    pub entities: Vec<String>,
141    /// Writer of the returned row, empty when no row identity is available.
142    #[serde(default)]
143    pub agent: String,
144    #[serde(default)]
145    pub user_id: String,
146    #[serde(default = "default_source")]
147    pub source: String,
148    /// A chunk's parent key, usable with Client::get and for recall credit.
149    #[serde(default)]
150    pub parent_memory_key: String,
151    #[serde(default)]
152    pub status: String,
153    pub superseded_by: Option<String>,
154    pub superseded_at: Option<String>,
155    #[serde(default)]
156    pub created_at: String,
157    #[serde(default)]
158    pub code_state: String,
159    #[serde(default)]
160    pub body_truncated: bool,
161    #[serde(default)]
162    pub next: String,
163    #[serde(default)]
164    pub source_resource_id: String,
165    #[serde(default)]
166    pub source_class: String,
167    #[serde(default)]
168    pub verified: bool,
169    #[serde(default)]
170    pub explanation: String,
171    #[serde(default)]
172    pub session_id: String,
173    #[serde(default)]
174    pub speaker: String,
175    #[serde(default)]
176    pub about_other: bool,
177    #[serde(default)]
178    pub derived_from: Vec<String>,
179    #[serde(default)]
180    pub score_components: std::collections::HashMap<String, f64>,
181    pub resolved_from: Option<String>,
182}
183
184#[derive(Debug, Deserialize)]
185#[non_exhaustive]
186pub struct SearchResponse {
187    pub results: Vec<SearchHit>,
188    #[serde(default)]
189    pub namespaces_searched: Vec<String>,
190    #[serde(default)]
191    pub candidates_total: u64,
192    #[serde(default)]
193    pub next_page: String,
194    /// True when the returned hits do not support an answer.
195    #[serde(default)]
196    pub abstain: bool,
197    #[serde(default)]
198    pub abstain_reason: String,
199    #[serde(default)]
200    pub abstain_code: String,
201    /// Empty when the response omits the risk label.
202    #[serde(default)]
203    pub retrieval_risk: String,
204    /// Status of the governed-definition retrieval route, empty when undisclosed.
205    #[serde(default)]
206    pub term_leg: String,
207    /// None means recall credit was not disclosed.
208    pub reinforcement: Option<String>,
209    /// Number of saves whose extraction is still pending.
210    #[serde(default)]
211    pub pending_extraction: u64,
212    #[serde(default)]
213    pub degraded: Vec<String>,
214    #[serde(default)]
215    pub next: String,
216}
217
218/// Deletion writes a tombstone; it does not physically erase stored data.
219#[derive(Debug, Deserialize)]
220#[non_exhaustive]
221pub struct DeleteResponse {
222    pub tombstoned: bool,
223    pub version: u64,
224    #[serde(default = "default_true")]
225    pub found: bool,
226}
227
228fn default_true() -> bool {
229    true
230}
231
232fn default_source() -> String {
233    "fact".into()
234}
235
236macro_rules! option_setter {
237    ($name:ident, $ty:ty) => {
238        pub fn $name(mut self, value: $ty) -> Self {
239            self.$name = Some(value);
240            self
241        }
242    };
243}
244
245macro_rules! string_setter {
246    ($name:ident) => {
247        pub fn $name(mut self, value: impl Into<String>) -> Self {
248            self.$name = Some(value.into());
249            self
250        }
251    };
252}
253
254macro_rules! tags_setter {
255    () => {
256        pub fn tags(mut self, tags: impl IntoIterator<Item = impl Into<String>>) -> Self {
257            self.tags = Some(tags.into_iter().map(Into::into).collect());
258            self
259        }
260    };
261}
262
263impl SetRequest {
264    string_setter!(memory_type);
265    tags_setter!();
266    option_setter!(ttl_seconds, i64);
267    option_setter!(confidence, f64);
268    string_setter!(if_match_sha256);
269
270    pub async fn send(
271        self,
272        client: &crate::Client,
273        options: crate::CallOptions,
274    ) -> Result<crate::CallResult<SetResponse>, crate::CallFailure> {
275        client.set_with_options(self, options).await
276    }
277}
278
279impl SearchRequest {
280    option_setter!(top_k, u32);
281    tags_setter!();
282    string_setter!(memory_type);
283    string_setter!(search_mode);
284    string_setter!(agent_id);
285    string_setter!(namespace_scope);
286    string_setter!(user_id);
287    string_setter!(session_id);
288
289    pub async fn send(
290        self,
291        client: &crate::Client,
292        options: crate::CallOptions,
293    ) -> Result<crate::CallResult<SearchResponse>, crate::CallFailure> {
294        client.search_with_options(self, options).await
295    }
296}