dakera-client 0.11.90

Rust client SDK for Dakera AI Agent Memory Platform
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
//! Integration tests against a real Dakera server (Docker service in CI).
//!
//! Requires DAKERA_TEST_URL env var pointing to a running Dakera instance.
//! Auth is enabled — set DAKERA_API_KEY to a valid key (default: test-key).
//!
//! Run locally: DAKERA_TEST_URL=http://localhost:3000 DAKERA_API_KEY=test-key cargo test --test integration_test

use std::env;

use dakera_client::memory::{
    BatchMemoryFilter, BatchRecallRequest, ConsolidateRequest, ForgetRequest, RecallRequest,
    StoreMemoryRequest, UpdateImportanceRequest,
};
use dakera_client::{CreateNamespaceRequest, DakeraClient, Document, HybridSearchRequest};

fn get_client() -> Option<DakeraClient> {
    let url = env::var("DAKERA_TEST_URL").ok()?;
    let api_key = env::var("DAKERA_API_KEY").unwrap_or_else(|_| "test-key".to_string());
    Some(
        DakeraClient::builder(&url)
            .api_key(&api_key)
            .build()
            .expect("Failed to create client"),
    )
}

fn random_hex() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};
    let nanos = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .subsec_nanos();
    format!("{:08x}", nanos)
}

fn test_namespace() -> String {
    format!("integ-{}", random_hex())
}

fn test_agent() -> String {
    format!("integ-agent-{}", random_hex())
}

// ---------------------------------------------------------------------------
// Health
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_health() {
    let Some(client) = get_client() else {
        eprintln!("DAKERA_TEST_URL not set — skipping");
        return;
    };
    let health = client.health().await.unwrap();
    assert!(health.healthy);
}

#[test]
fn test_health_response_build_sha_present() {
    let json = r#"{"healthy":true,"version":"0.11.84","build_sha":"abc1234def5678"}"#;
    let h: dakera_client::HealthResponse = serde_json::from_str(json).unwrap();
    assert!(h.healthy);
    assert_eq!(h.build_sha.as_deref(), Some("abc1234def5678"));
}

#[test]
fn test_health_response_build_sha_absent() {
    let json = r#"{"healthy":true,"version":"0.11.83"}"#;
    let h: dakera_client::HealthResponse = serde_json::from_str(json).unwrap();
    assert!(h.healthy);
    assert!(h.build_sha.is_none());
}

// ---------------------------------------------------------------------------
// Namespaces
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_create_namespace() {
    let Some(client) = get_client() else {
        eprintln!("DAKERA_TEST_URL not set — skipping");
        return;
    };
    let ns = test_namespace();
    let req = CreateNamespaceRequest {
        dimensions: Some(1024),
        ..Default::default()
    };
    let result = client.create_namespace(&ns, req).await.unwrap();
    assert_eq!(result.name, ns);
    client.delete_namespace_admin(&ns).await.unwrap();
}

#[tokio::test]
async fn test_list_namespaces() {
    let Some(client) = get_client() else {
        eprintln!("DAKERA_TEST_URL not set — skipping");
        return;
    };
    let ns = test_namespace();
    let req = CreateNamespaceRequest {
        dimensions: Some(1024),
        ..Default::default()
    };
    client.create_namespace(&ns, req).await.unwrap();
    let namespaces = client.list_namespaces().await.unwrap();
    assert!(namespaces.contains(&ns));
    client.delete_namespace_admin(&ns).await.unwrap();
}

#[tokio::test]
async fn test_get_namespace() {
    let Some(client) = get_client() else {
        eprintln!("DAKERA_TEST_URL not set — skipping");
        return;
    };
    let ns = test_namespace();
    let req = CreateNamespaceRequest {
        dimensions: Some(1024),
        ..Default::default()
    };
    client.create_namespace(&ns, req).await.unwrap();
    let info = client.get_namespace(&ns).await.unwrap();
    assert_eq!(info.name, ns);
    assert_eq!(info.dimensions, Some(1024));
    client.delete_namespace_admin(&ns).await.unwrap();
}

// ---------------------------------------------------------------------------
// Memory CRUD
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_store_memory() {
    let Some(client) = get_client() else {
        eprintln!("DAKERA_TEST_URL not set — skipping");
        return;
    };
    let agent = test_agent();
    let req = StoreMemoryRequest::new(&agent, "The user prefers dark mode")
        .with_importance(0.8)
        .with_tags(vec!["preference".to_string(), "ui".to_string()]);
    let result = client.store_memory(req).await.unwrap();
    assert!(!result.memory_id.is_empty());
}

#[tokio::test]
async fn test_recall_semantic() {
    let Some(client) = get_client() else {
        eprintln!("DAKERA_TEST_URL not set — skipping");
        return;
    };
    let agent = test_agent();
    let req = StoreMemoryRequest::new(&agent, "Python is my primary programming language")
        .with_importance(0.9);
    client.store_memory(req).await.unwrap();
    tokio::time::sleep(std::time::Duration::from_millis(500)).await;

    let recall_req = RecallRequest::new(&agent, "programming language").with_top_k(5);
    let result = client.recall(recall_req).await.unwrap();
    assert!(!result.memories.is_empty());
}

#[tokio::test]
async fn test_batch_recall() {
    let Some(client) = get_client() else {
        eprintln!("DAKERA_TEST_URL not set — skipping");
        return;
    };
    let agent = test_agent();
    let req = StoreMemoryRequest::new(&agent, "Batch recall test memory").with_importance(0.8);
    client.store_memory(req).await.unwrap();

    let filter = BatchMemoryFilter::default().with_min_importance(0.5);
    let batch_req = BatchRecallRequest::new(&agent).with_filter(filter);
    let result = client.batch_recall(batch_req).await.unwrap();
    assert!(!result.memories.is_empty());
}

#[tokio::test]
async fn test_get_memory() {
    let Some(client) = get_client() else {
        eprintln!("DAKERA_TEST_URL not set — skipping");
        return;
    };
    let agent = test_agent();
    let req = StoreMemoryRequest::new(&agent, "Memory for get test").with_importance(0.7);
    let stored = client.store_memory(req).await.unwrap();
    let memory = client.get_memory(&agent, &stored.memory_id).await.unwrap();
    assert_eq!(memory.content, "Memory for get test");
}

#[tokio::test]
async fn test_update_importance() {
    let Some(client) = get_client() else {
        eprintln!("DAKERA_TEST_URL not set — skipping");
        return;
    };
    let agent = test_agent();
    let req = StoreMemoryRequest::new(&agent, "Importance update test").with_importance(0.5);
    let stored = client.store_memory(req).await.unwrap();
    let update_req = UpdateImportanceRequest {
        memory_ids: vec![stored.memory_id],
        importance: 0.95,
    };
    client.update_importance(&agent, update_req).await.unwrap();
}

#[tokio::test]
async fn test_forget() {
    let Some(client) = get_client() else {
        eprintln!("DAKERA_TEST_URL not set — skipping");
        return;
    };
    let agent = test_agent();
    let req = StoreMemoryRequest::new(&agent, "Memory to forget").with_importance(0.3);
    let stored = client.store_memory(req).await.unwrap();
    let forget_req = ForgetRequest::by_ids(&agent, vec![stored.memory_id]);
    client.forget(forget_req).await.unwrap();
}

// ---------------------------------------------------------------------------
// Sessions
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_session_lifecycle() {
    let Some(client) = get_client() else {
        eprintln!("DAKERA_TEST_URL not set — skipping");
        return;
    };
    let agent = test_agent();
    let session = client.start_session(&agent).await.unwrap();
    assert!(!session.id.is_empty());

    let sessions = client.list_sessions(&agent).await.unwrap();
    assert!(!sessions.is_empty());

    client.end_session(&session.id, None).await.unwrap();
}

// ---------------------------------------------------------------------------
// Vectors / Text
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_index_and_search() {
    let Some(client) = get_client() else {
        eprintln!("DAKERA_TEST_URL not set — skipping");
        return;
    };
    let ns = test_namespace();
    let req = CreateNamespaceRequest {
        dimensions: Some(1024),
        ..Default::default()
    };
    client.create_namespace(&ns, req).await.unwrap();

    client
        .index_document(
            &ns,
            Document::new("doc-1", "Machine learning transforms data"),
        )
        .await
        .unwrap();
    client
        .index_document(
            &ns,
            Document::new("doc-2", "Natural language processing understands text"),
        )
        .await
        .unwrap();
    client
        .index_document(
            &ns,
            Document::new("doc-3", "Deep learning uses neural networks"),
        )
        .await
        .unwrap();

    tokio::time::sleep(std::time::Duration::from_secs(1)).await;

    let results = client.search_text(&ns, "neural networks", 3).await.unwrap();
    let _ = results;

    client.delete_namespace_admin(&ns).await.unwrap();
}

#[tokio::test]
async fn test_hybrid_search() {
    let Some(client) = get_client() else {
        eprintln!("DAKERA_TEST_URL not set — skipping");
        return;
    };
    let ns = test_namespace();
    let req = CreateNamespaceRequest {
        dimensions: Some(1024),
        ..Default::default()
    };
    client.create_namespace(&ns, req).await.unwrap();

    client
        .index_document(&ns, Document::new("h-1", "Machine learning data analysis"))
        .await
        .unwrap();
    tokio::time::sleep(std::time::Duration::from_secs(1)).await;

    let search_req = HybridSearchRequest::text_only("machine learning", 3);
    let _results = client.hybrid_search(&ns, search_req).await;

    client.delete_namespace_admin(&ns).await.unwrap();
}

// ---------------------------------------------------------------------------
// Knowledge Graph
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_memory_graph() {
    let Some(client) = get_client() else {
        eprintln!("DAKERA_TEST_URL not set — skipping");
        return;
    };
    let agent = test_agent();
    let req = StoreMemoryRequest::new(&agent, "Knowledge graph test memory").with_importance(0.8);
    let stored = client.store_memory(req).await.unwrap();
    tokio::time::sleep(std::time::Duration::from_millis(500)).await;
    let opts = dakera_client::GraphOptions::new().depth(1);
    let _graph = client.memory_graph(&stored.memory_id, opts).await;
}

// ---------------------------------------------------------------------------
// Consolidate
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_consolidate() {
    let Some(client) = get_client() else {
        eprintln!("DAKERA_TEST_URL not set — skipping");
        return;
    };
    let agent = test_agent();
    for i in 0..3 {
        let req = StoreMemoryRequest::new(
            &agent,
            format!("Consolidation test variation {i}: similar content"),
        )
        .with_importance(0.6);
        client.store_memory(req).await.unwrap();
    }
    tokio::time::sleep(std::time::Duration::from_millis(500)).await;

    let consolidate_req = ConsolidateRequest::default();
    let _result = client.consolidate(&agent, consolidate_req).await;
}

// ---------------------------------------------------------------------------
// Error Handling
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_nonexistent_namespace() {
    let Some(client) = get_client() else {
        eprintln!("DAKERA_TEST_URL not set — skipping");
        return;
    };
    let result = client.get_namespace("nonexistent-ns-xyz-99999").await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_nonexistent_memory() {
    let Some(client) = get_client() else {
        eprintln!("DAKERA_TEST_URL not set — skipping");
        return;
    };
    let result = client
        .get_memory("test-agent", "nonexistent-memory-id")
        .await;
    assert!(result.is_err());
}

// ---------------------------------------------------------------------------
// Authentication
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_auth_rejects_invalid_key() {
    let url = match env::var("DAKERA_TEST_URL") {
        Ok(u) => u,
        Err(_) => {
            eprintln!("DAKERA_TEST_URL not set — skipping");
            return;
        }
    };
    let bad_client = DakeraClient::builder(&url)
        .api_key("invalid-key-xxx")
        .build()
        .expect("Failed to create client");
    let result = bad_client.list_namespaces().await;
    assert!(result.is_err(), "expected auth error with invalid key");
    let err = result.unwrap_err();
    assert!(err.is_auth_error(), "expected auth error, got: {err:?}");
}

#[tokio::test]
async fn test_auth_accepts_valid_key() {
    let Some(client) = get_client() else {
        eprintln!("DAKERA_TEST_URL not set — skipping");
        return;
    };
    let namespaces = client.list_namespaces().await.unwrap();
    assert!(namespaces.len() >= 0);
}