wauldo 0.10.0

Official Rust SDK for Wauldo — Verified AI answers from your documents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Memory API client — Wauldo Deploy long-term memory.
//!
//! Tenant-scoped key-value store with namespaces and lexical search.
//! Standalone like `AgentsClient` — no coupling to `HttpClient`.
//!
//! # Example
//! ```no_run
//! use wauldo::memory::{MemoryClient, SearchOptions};
//!
//! # async fn run() -> Result<(), Box<dyn std::error::Error>> {
//! let mem = MemoryClient::new("http://localhost:3000").with_api_key("sk-...");
//! mem.set("support", "ticket-123", "Customer asked about pricing", &["urgent", "sales"], None)
//!     .await?;
//! let hits = mem.search("support", SearchOptions { query: Some("pricing".into()), tags: vec!["urgent".into()], limit: None }).await?;
//! println!("{} hits", hits.results.len());
//! # Ok(())
//! # }
//! ```

use std::time::Duration;

use reqwest::{header::HeaderMap, Client, Method, StatusCode};
use serde::{Deserialize, Serialize};

use crate::agents::{bounded_read, AgentsError, AgentsResult, MAX_RESPONSE_SIZE};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryEntry {
    pub id: String,
    pub tenant_id: String,
    pub namespace: String,
    pub key: String,
    pub value: String,
    #[serde(default)]
    pub tags: Vec<String>,
    #[serde(default)]
    pub embedding: Option<Vec<f32>>,
    pub created_at: u64,
    pub updated_at: u64,
}

#[derive(Debug, Clone, Deserialize)]
pub struct MemoryListResponse {
    pub entries: Vec<MemoryEntry>,
    pub pagination: MemoryPagination,
}

#[derive(Debug, Clone, Deserialize)]
pub struct MemoryPagination {
    pub total: usize,
    pub limit: usize,
    pub offset: usize,
}

#[derive(Debug, Clone, Deserialize)]
pub struct MemorySearchResponse {
    pub results: Vec<MemorySearchResult>,
    pub total_matched: usize,
    pub mode: String,
}

#[derive(Debug, Clone, Deserialize)]
pub struct MemorySearchResult {
    pub entry: MemoryEntry,
    pub score: f32,
    pub matched_fields: Vec<String>,
}

#[derive(Debug, Clone, Default)]
pub struct SearchOptions {
    pub query: Option<String>,
    pub tags: Vec<String>,
    pub limit: Option<usize>,
}

pub struct MemoryClient {
    base_url: String,
    api_key: Option<String>,
    tenant: Option<String>,
    client: Client,
}

impl MemoryClient {
    pub fn new(base_url: impl Into<String>) -> Self {
        Self {
            base_url: base_url.into().trim_end_matches('/').to_string(),
            api_key: None,
            tenant: None,
            client: Client::builder()
                .timeout(Duration::from_secs(60))
                .build()
                .expect("reqwest client"),
        }
    }

    pub fn with_api_key(mut self, key: impl Into<String>) -> Self {
        self.api_key = Some(key.into());
        self
    }

    pub fn with_tenant(mut self, tenant: impl Into<String>) -> Self {
        self.tenant = Some(tenant.into());
        self
    }

    fn headers(&self) -> HeaderMap {
        let mut h = HeaderMap::new();
        h.insert("Content-Type", "application/json".parse().unwrap());
        if let Some(key) = &self.api_key {
            if let Ok(val) = format!("Bearer {key}").parse() {
                h.insert("Authorization", val);
            }
        }
        if let Some(t) = &self.tenant {
            if let Ok(val) = t.parse() {
                h.insert("x-rapidapi-user", val);
            }
        }
        h
    }

    async fn request<T: for<'de> Deserialize<'de>>(
        &self,
        method: Method,
        path: &str,
        body: Option<&serde_json::Value>,
    ) -> AgentsResult<Option<T>> {
        let url = format!("{}{}", self.base_url, path);
        let mut req = self.client.request(method, &url).headers(self.headers());
        if let Some(b) = body {
            req = req.json(b);
        }
        let resp = req.send().await?;
        let status = resp.status();
        if status == StatusCode::NO_CONTENT {
            return Ok(None);
        }
        let bytes = bounded_read(resp, MAX_RESPONSE_SIZE).await?;
        if !status.is_success() {
            let body = String::from_utf8_lossy(&bytes).into_owned();
            return Err(AgentsError::Status {
                status: status.as_u16(),
                body,
            });
        }
        if bytes.is_empty() {
            return Ok(None);
        }
        Ok(Some(serde_json::from_slice(&bytes)?))
    }

    // ── CRUD ─────────────────────────────────────────────────────

    pub async fn set(
        &self,
        namespace: &str,
        key: &str,
        value: &str,
        tags: &[&str],
        embedding: Option<Vec<f32>>,
    ) -> AgentsResult<MemoryEntry> {
        let mut body = serde_json::json!({ "key": key, "value": value });
        if !tags.is_empty() {
            body["tags"] = serde_json::json!(tags);
        }
        if let Some(emb) = embedding {
            body["embedding"] = serde_json::json!(emb);
        }
        self.request::<MemoryEntry>(
            Method::POST,
            &format!("/v1/memory/{namespace}"),
            Some(&body),
        )
        .await
        .map(|o| o.expect("server returned empty body for memory.set"))
    }

    pub async fn get(&self, namespace: &str, key: &str) -> AgentsResult<MemoryEntry> {
        self.request::<MemoryEntry>(Method::GET, &format!("/v1/memory/{namespace}/{key}"), None)
            .await
            .map(|o| o.expect("server returned empty body for memory.get"))
    }

    pub async fn delete(&self, namespace: &str, key: &str) -> AgentsResult<()> {
        let _: Option<serde_json::Value> = self
            .request(
                Method::DELETE,
                &format!("/v1/memory/{namespace}/{key}"),
                None,
            )
            .await?;
        Ok(())
    }

    pub async fn list(
        &self,
        namespace: &str,
        limit: usize,
        offset: usize,
    ) -> AgentsResult<MemoryListResponse> {
        self.request::<MemoryListResponse>(
            Method::GET,
            &format!("/v1/memory/{namespace}?limit={limit}&offset={offset}"),
            None,
        )
        .await
        .map(|o| o.expect("server returned empty body for memory.list"))
    }

    pub async fn search(
        &self,
        namespace: &str,
        options: SearchOptions,
    ) -> AgentsResult<MemorySearchResponse> {
        let query = options.query.unwrap_or_default();
        if query.is_empty() && options.tags.is_empty() {
            return Err(AgentsError::InvalidInput(
                "search requires query or tags (or both)".into(),
            ));
        }
        let mut body = serde_json::json!({ "query": query });
        if !options.tags.is_empty() {
            body["tags"] = serde_json::json!(options.tags);
        }
        if let Some(limit) = options.limit {
            body["limit"] = serde_json::json!(limit);
        }
        self.request::<MemorySearchResponse>(
            Method::POST,
            &format!("/v1/memory/{namespace}/search"),
            Some(&body),
        )
        .await
        .map(|o| o.expect("server returned empty body for memory.search"))
    }

    // ── Namespace sugar ──────────────────────────────────────────
    //
    // Bound views so callers can write `client.short_term().set(...)`
    // instead of `client.set("short_term", ...)`. Pure sugar — the
    // base CRUD methods above remain unchanged.

    /// Sugar for namespace `short_term` (session/transient state).
    pub fn short_term(&self) -> NamespacedMemory<'_> {
        NamespacedMemory {
            client: self,
            namespace: "short_term",
        }
    }

    /// Sugar for namespace `long_term` (durable user/agent facts).
    pub fn long_term(&self) -> NamespacedMemory<'_> {
        NamespacedMemory {
            client: self,
            namespace: "long_term",
        }
    }

    /// Sugar for namespace `entity` (per-entity profiles/state).
    pub fn entity(&self) -> NamespacedMemory<'_> {
        NamespacedMemory {
            client: self,
            namespace: "entity",
        }
    }

    /// Sugar for namespace `contextual` (per-context attachments).
    pub fn contextual(&self) -> NamespacedMemory<'_> {
        NamespacedMemory {
            client: self,
            namespace: "contextual",
        }
    }
}

/// Namespace-bound view over a [`MemoryClient`].
///
/// Returned by [`MemoryClient::short_term`], [`MemoryClient::long_term`],
/// [`MemoryClient::entity`], [`MemoryClient::contextual`]. Every method
/// forwards to the parent client with the namespace prefilled.
pub struct NamespacedMemory<'a> {
    client: &'a MemoryClient,
    pub namespace: &'static str,
}

impl<'a> NamespacedMemory<'a> {
    pub async fn set(
        &self,
        key: &str,
        value: &str,
        tags: &[&str],
        embedding: Option<Vec<f32>>,
    ) -> AgentsResult<MemoryEntry> {
        self.client
            .set(self.namespace, key, value, tags, embedding)
            .await
    }

    pub async fn get(&self, key: &str) -> AgentsResult<MemoryEntry> {
        self.client.get(self.namespace, key).await
    }

    pub async fn delete(&self, key: &str) -> AgentsResult<()> {
        self.client.delete(self.namespace, key).await
    }

    pub async fn list(&self, limit: usize, offset: usize) -> AgentsResult<MemoryListResponse> {
        self.client.list(self.namespace, limit, offset).await
    }

    pub async fn search(&self, options: SearchOptions) -> AgentsResult<MemorySearchResponse> {
        self.client.search(self.namespace, options).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use wiremock::matchers::{body_partial_json, header, method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    fn sample_entry_json() -> serde_json::Value {
        serde_json::json!({
            "id": "m1",
            "tenant_id": "t",
            "namespace": "support",
            "key": "k1",
            "value": "hello",
            "tags": [],
            "created_at": 0u64,
            "updated_at": 0u64,
        })
    }

    #[tokio::test]
    async fn test_set_basic_posts_key_and_value() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/memory/support"))
            .and(body_partial_json(serde_json::json!({
                "key": "k1",
                "value": "hello",
            })))
            .respond_with(ResponseTemplate::new(200).set_body_json(sample_entry_json()))
            .mount(&server)
            .await;
        let client = MemoryClient::new(server.uri());
        let out = client
            .set("support", "k1", "hello", &[], None)
            .await
            .unwrap();
        assert_eq!(out.id, "m1");
    }

    #[tokio::test]
    async fn test_set_with_tags_and_embedding() {
        use wiremock::Request;
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/memory/ns"))
            .respond_with(ResponseTemplate::new(200).set_body_json(sample_entry_json()))
            .mount(&server)
            .await;
        let client = MemoryClient::new(server.uri());
        client
            .set("ns", "k", "v", &["urgent"], Some(vec![0.1, 0.2]))
            .await
            .unwrap();

        // Verify via inspecting the captured request — body_partial_json is
        // fussy with mixed f32/f64 literals, so we parse the body ourselves.
        let requests = server.received_requests().await.unwrap();
        let req: &Request = &requests[0];
        let body: serde_json::Value = serde_json::from_slice(&req.body).unwrap();
        assert_eq!(body["key"], "k");
        assert_eq!(body["value"], "v");
        assert_eq!(body["tags"], serde_json::json!(["urgent"]));
        assert!(body["embedding"].is_array());
        let emb = body["embedding"].as_array().unwrap();
        assert_eq!(emb.len(), 2);
    }

    #[tokio::test]
    async fn test_get() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1/memory/ns/k"))
            .respond_with(ResponseTemplate::new(200).set_body_json(sample_entry_json()))
            .mount(&server)
            .await;
        let client = MemoryClient::new(server.uri());
        let out = client.get("ns", "k").await.unwrap();
        assert_eq!(out.value, "hello");
    }

    #[tokio::test]
    async fn test_delete_returns_unit() {
        let server = MockServer::start().await;
        Mock::given(method("DELETE"))
            .and(path("/v1/memory/ns/k"))
            .respond_with(ResponseTemplate::new(204))
            .mount(&server)
            .await;
        let client = MemoryClient::new(server.uri());
        client.delete("ns", "k").await.unwrap();
    }

    #[tokio::test]
    async fn test_list_returns_paginated_response() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1/memory/ns"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "entries": [],
                "pagination": { "total": 0, "limit": 20, "offset": 0 },
            })))
            .mount(&server)
            .await;
        let client = MemoryClient::new(server.uri());
        let out = client.list("ns", 20, 0).await.unwrap();
        assert_eq!(out.pagination.limit, 20);
    }

    #[tokio::test]
    async fn test_search_query_only_sends_query() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/memory/ns/search"))
            .and(body_partial_json(serde_json::json!({"query": "hello"})))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "results": [],
                "total_matched": 0,
                "mode": "lexical",
            })))
            .mount(&server)
            .await;
        let client = MemoryClient::new(server.uri());
        client
            .search(
                "ns",
                SearchOptions {
                    query: Some("hello".into()),
                    ..Default::default()
                },
            )
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn test_search_rejects_empty_query_and_tags() {
        let client = MemoryClient::new("http://localhost:1");
        let err = client
            .search("ns", SearchOptions::default())
            .await
            .unwrap_err();
        assert!(matches!(err, AgentsError::InvalidInput(_)));
    }

    #[tokio::test]
    async fn test_tenant_header_injected() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/memory/ns"))
            .and(header("authorization", "Bearer k"))
            .and(header("x-rapidapi-user", "tenant-x"))
            .respond_with(ResponseTemplate::new(200).set_body_json(sample_entry_json()))
            .mount(&server)
            .await;
        let client = MemoryClient::new(server.uri())
            .with_api_key("k")
            .with_tenant("tenant-x");
        client.set("ns", "k", "v", &[], None).await.unwrap();
    }
}