honcho-ai 0.1.2

Rust SDK for Honcho — AI agent memory and social cognition infrastructure
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//! Wiremock tests for Peer methods (F5.5–F5.7).

#![allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::needless_borrows_for_generic_args,
    clippy::unused_async,
    missing_docs
)]

use futures_util::StreamExt;
use honcho_ai::Honcho;

use wiremock::matchers::{body_json, method, path, query_param};
use wiremock::{Mock, MockServer, ResponseTemplate};

fn peer_response() -> serde_json::Value {
    serde_json::json!({
        "id": "alice",
        "workspace_id": "ws1",
        "created_at": "2025-01-15T10:30:00Z",
        "metadata": {},
        "configuration": {}
    })
}

fn workspace_response() -> serde_json::Value {
    serde_json::json!({
        "id": "ws1",
        "metadata": {},
        "configuration": {},
        "created_at": "2025-01-15T10:30:00Z"
    })
}

fn make_honcho(server: &MockServer) -> Honcho {
    Honcho::from_params(
        Honcho::builder()
            .base_url(server.uri())
            .workspace_id("ws1")
            .build(),
    )
    .unwrap()
}

async fn mount_workspace_ensure(server: &MockServer) {
    Mock::given(method("POST"))
        .and(path("/v3/workspaces"))
        .and(body_json(&serde_json::json!({"id": "ws1"})))
        .respond_with(ResponseTemplate::new(200).set_body_json(workspace_response()))
        .mount(server)
        .await;
}

async fn mount_peer_create(server: &MockServer) {
    mount_workspace_ensure(server).await;
    Mock::given(method("POST"))
        .and(path("/v3/workspaces/ws1/peers"))
        .and(body_json(&serde_json::json!({"id": "alice"})))
        .respond_with(ResponseTemplate::new(200).set_body_json(peer_response()))
        .mount(server)
        .await;
}

// ── F5.5 Representation ────────────────────────────────────────────

#[tokio::test]
async fn peer_representation_basic() {
    let server = MockServer::start().await;
    let honcho = make_honcho(&server);
    mount_peer_create(&server).await;

    let repr_body = serde_json::json!({});
    Mock::given(method("POST"))
        .and(path("/v3/workspaces/ws1/peers/alice/representation"))
        .and(body_json(&repr_body))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "representation": "Alice likes cats and Rust."
        })))
        .mount(&server)
        .await;

    let peer = honcho.peer("alice", None, None).await.unwrap();
    let repr = peer.representation().await.unwrap();
    assert_eq!(repr, "Alice likes cats and Rust.");
}

#[tokio::test]
async fn peer_representation_with_options() {
    let server = MockServer::start().await;
    let honcho = make_honcho(&server);
    mount_peer_create(&server).await;

    Mock::given(method("POST"))
        .and(path("/v3/workspaces/ws1/peers/alice/representation"))
        .and(body_json(&serde_json::json!({
            "session_id": "sess1",
            "target": "bob",
            "search_query": "preferences",
            "search_top_k": 5,
            "search_max_distance": 0.8,
            "include_most_frequent": true,
            "max_conclusions": 20
        })))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "representation": "curated result"
        })))
        .mount(&server)
        .await;

    let peer = honcho.peer("alice", None, None).await.unwrap();
    let repr = peer
        .representation_builder()
        .session_id("sess1")
        .target("bob")
        .search_query("preferences")
        .search_top_k(5)
        .search_max_distance(0.8)
        .include_most_frequent(true)
        .max_conclusions(20)
        .send()
        .await
        .unwrap();
    assert_eq!(repr, "curated result");
}

#[tokio::test]
async fn peer_representation_validates_search_top_k() {
    let server = MockServer::start().await;
    let honcho = make_honcho(&server);
    mount_peer_create(&server).await;

    let peer = honcho.peer("alice", None, None).await.unwrap();

    let err = peer
        .representation_builder()
        .search_top_k(0)
        .send()
        .await
        .unwrap_err();
    assert!(
        matches!(err, honcho_ai::error::HonchoError::Validation(ref msg)
            if msg.contains("search_top_k")),
        "expected Validation error for search_top_k, got {err:?}"
    );

    let err = peer
        .representation_builder()
        .search_top_k(101)
        .send()
        .await
        .unwrap_err();
    assert!(
        matches!(err, honcho_ai::error::HonchoError::Validation(ref msg)
            if msg.contains("search_top_k")),
        "expected Validation error for search_top_k=101, got {err:?}"
    );
}

#[tokio::test]
async fn peer_representation_validates_search_max_distance() {
    let server = MockServer::start().await;
    let honcho = make_honcho(&server);
    mount_peer_create(&server).await;

    let peer = honcho.peer("alice", None, None).await.unwrap();

    let err = peer
        .representation_builder()
        .search_max_distance(1.5)
        .send()
        .await
        .unwrap_err();
    assert!(
        matches!(err, honcho_ai::error::HonchoError::Validation(ref msg)
            if msg.contains("search_max_distance")),
        "expected Validation error for search_max_distance, got {err:?}"
    );

    let err = peer
        .representation_builder()
        .search_max_distance(-0.1)
        .send()
        .await
        .unwrap_err();
    assert!(
        matches!(err, honcho_ai::error::HonchoError::Validation(ref msg)
            if msg.contains("search_max_distance")),
        "expected Validation error for negative search_max_distance, got {err:?}"
    );
}

#[tokio::test]
async fn peer_representation_validates_max_conclusions() {
    let server = MockServer::start().await;
    let honcho = make_honcho(&server);
    mount_peer_create(&server).await;

    let peer = honcho.peer("alice", None, None).await.unwrap();

    let err = peer
        .representation_builder()
        .max_conclusions(0)
        .send()
        .await
        .unwrap_err();
    assert!(
        matches!(err, honcho_ai::error::HonchoError::Validation(ref msg)
            if msg.contains("max_conclusions")),
        "expected Validation error for max_conclusions=0, got {err:?}"
    );

    let err = peer
        .representation_builder()
        .max_conclusions(101)
        .send()
        .await
        .unwrap_err();
    assert!(
        matches!(err, honcho_ai::error::HonchoError::Validation(ref msg)
            if msg.contains("max_conclusions")),
        "expected Validation error for max_conclusions=101, got {err:?}"
    );
}

// ── F5.6 Context ───────────────────────────────────────────────────

#[tokio::test]
async fn peer_context_returns_peer_context() {
    let server = MockServer::start().await;
    let honcho = make_honcho(&server);
    mount_peer_create(&server).await;

    Mock::given(method("GET"))
        .and(path("/v3/workspaces/ws1/peers/alice/context"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "peer_id": "alice",
            "target_id": "alice",
            "representation": "Alice is curious.",
            "peer_card": ["friendly", "inquisitive"]
        })))
        .mount(&server)
        .await;

    let peer = honcho.peer("alice", None, None).await.unwrap();
    let ctx = peer.context().await.unwrap();
    assert_eq!(ctx.peer_id, "alice");
    assert_eq!(ctx.target_id, "alice");
    assert_eq!(ctx.representation.as_deref(), Some("Alice is curious."));
    assert_eq!(
        ctx.peer_card.as_deref(),
        Some(&["friendly".to_owned(), "inquisitive".to_owned()][..])
    );
}

#[tokio::test]
async fn peer_context_with_target_sends_query() {
    let server = MockServer::start().await;
    let honcho = make_honcho(&server);
    mount_peer_create(&server).await;

    Mock::given(method("GET"))
        .and(path("/v3/workspaces/ws1/peers/alice/context"))
        .and(query_param("target", "bob"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "peer_id": "alice",
            "target_id": "bob",
            "representation": "Bob is helpful.",
            "peer_card": null
        })))
        .mount(&server)
        .await;

    let peer = honcho.peer("alice", None, None).await.unwrap();
    let ctx = peer.context_builder().target("bob").send().await.unwrap();
    assert_eq!(ctx.peer_id, "alice");
    assert_eq!(ctx.target_id, "bob");
    assert_eq!(ctx.representation.as_deref(), Some("Bob is helpful."));
    assert!(ctx.peer_card.is_none());
}

#[tokio::test]
async fn peer_context_with_options_sends_all_query_params() {
    let server = MockServer::start().await;
    let honcho = make_honcho(&server);
    mount_peer_create(&server).await;

    Mock::given(method("GET"))
        .and(path("/v3/workspaces/ws1/peers/alice/context"))
        .and(query_param("target", "bob"))
        .and(query_param("search_query", "preferences"))
        .and(query_param("search_top_k", "10"))
        .and(query_param("search_max_distance", "0.5"))
        .and(query_param("include_most_frequent", "true"))
        .and(query_param("max_conclusions", "20"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "peer_id": "alice",
            "target_id": "bob",
            "representation": "curated context",
            "peer_card": null
        })))
        .expect(1)
        .mount(&server)
        .await;

    let peer = honcho.peer("alice", None, None).await.unwrap();
    let ctx = peer
        .context_builder()
        .target("bob")
        .search_query("preferences")
        .search_top_k(10)
        .search_max_distance(0.5)
        .include_most_frequent(true)
        .max_conclusions(20)
        .send()
        .await
        .unwrap();
    assert_eq!(ctx.peer_id, "alice");
    assert_eq!(ctx.target_id, "bob");
    assert_eq!(ctx.representation.as_deref(), Some("curated context"));
}

#[tokio::test]
async fn peer_context_with_options_sends_only_set_params() {
    let server = MockServer::start().await;
    let honcho = make_honcho(&server);
    mount_peer_create(&server).await;

    Mock::given(method("GET"))
        .and(path("/v3/workspaces/ws1/peers/alice/context"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "peer_id": "alice",
            "target_id": "alice",
            "representation": "self context",
            "peer_card": null
        })))
        .expect(1)
        .mount(&server)
        .await;

    let peer = honcho.peer("alice", None, None).await.unwrap();
    let ctx = peer.context_builder().send().await.unwrap();
    assert_eq!(ctx.peer_id, "alice");
    assert_eq!(ctx.representation.as_deref(), Some("self context"));
}

// ── F5.7 Sessions ──────────────────────────────────────────────────

#[tokio::test]
async fn peer_sessions_returns_paginated() {
    let server = MockServer::start().await;
    let honcho = make_honcho(&server);
    mount_peer_create(&server).await;

    let session1 = serde_json::json!({
        "id": "s1",
        "is_active": true,
        "workspace_id": "ws1",
        "metadata": {},
        "configuration": {},
        "created_at": "2025-01-15T10:30:00Z"
    });
    let session2 = serde_json::json!({
        "id": "s2",
        "is_active": false,
        "workspace_id": "ws1",
        "metadata": {},
        "configuration": {},
        "created_at": "2025-01-16T10:30:00Z"
    });

    Mock::given(method("POST"))
        .and(path("/v3/workspaces/ws1/peers/alice/sessions"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "items": [session1, session2],
            "total": 2,
            "page": 1,
            "size": 50,
            "pages": 1
        })))
        .mount(&server)
        .await;

    let peer = honcho.peer("alice", None, None).await.unwrap();
    let page = peer.sessions().await.unwrap();
    assert_eq!(page.total(), 2);
    assert_eq!(page.pages(), 1);
    let items = page.items();
    assert_eq!(items.len(), 2);
    assert_eq!(items[0].id, "s1");
    assert!(items[0].is_active);
    assert_eq!(items[1].id, "s2");
    assert!(!items[1].is_active);
}

// ── F8.4 Streaming Chat ────────────────────────────────────────────

fn sse_chunk(json: &str) -> String {
    format!("data: {json}\n\n")
}

#[tokio::test]
async fn chat_stream_basic() {
    let server = MockServer::start().await;
    let honcho = make_honcho(&server);
    mount_peer_create(&server).await;

    let sse_body = format!(
        "{}{}{}",
        sse_chunk(r#"{"delta":{"content":"hello"}}"#),
        sse_chunk(r#"{"delta":{"content":" world"}}"#),
        sse_chunk(r#"{"done":true}"#),
    );

    Mock::given(method("POST"))
        .and(path("/v3/workspaces/ws1/peers/alice/chat"))
        .and(body_json(&serde_json::json!({
            "query": "hi",
            "stream": true,
        })))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_string(sse_body)
                .insert_header("content-type", "text/event-stream"),
        )
        .mount(&server)
        .await;

    let peer = honcho.peer("alice", None, None).await.unwrap();
    let mut stream = peer.chat_stream("hi").send().await.unwrap();

    let mut chunks = Vec::new();
    while let Some(item) = stream.next().await {
        chunks.push(item.unwrap());
    }
    assert_eq!(chunks, vec!["hello", " world"]);
}

#[tokio::test]
async fn chat_stream_with_target_session_reasoning_level() {
    let server = MockServer::start().await;
    let honcho = make_honcho(&server);
    mount_peer_create(&server).await;

    let expected_body = serde_json::json!({
        "query": "deep thought",
        "stream": true,
        "target": "bob",
        "session_id": "sess42",
        "reasoning_level": "high",
    });

    Mock::given(method("POST"))
        .and(path("/v3/workspaces/ws1/peers/alice/chat"))
        .and(body_json(&expected_body))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_string(
                    sse_chunk(r#"{"delta":{"content":"response"}}"#)
                        + &sse_chunk(r#"{"done":true}"#),
                )
                .insert_header("content-type", "text/event-stream"),
        )
        .mount(&server)
        .await;

    let peer = honcho.peer("alice", None, None).await.unwrap();
    let mut stream = peer
        .chat_stream("deep thought")
        .target("bob")
        .session("sess42")
        .reasoning_level(honcho_ai::types::dialectic::ReasoningLevel::High)
        .send()
        .await
        .unwrap();

    let mut chunks = Vec::new();
    while let Some(item) = stream.next().await {
        chunks.push(item.unwrap());
    }
    assert_eq!(chunks, vec!["response"]);
}

#[tokio::test]
async fn chat_stream_error_before_first_byte_returns_err() {
    let server = MockServer::start().await;
    let honcho = make_honcho(&server);
    mount_peer_create(&server).await;

    Mock::given(method("POST"))
        .and(path("/v3/workspaces/ws1/peers/alice/chat"))
        .respond_with(ResponseTemplate::new(500))
        .mount(&server)
        .await;

    let peer = honcho.peer("alice", None, None).await.unwrap();
    let result = peer.chat_stream("hi").send().await;
    assert!(result.is_err(), "expected error for 500 response");
    let err = result.err().unwrap();
    assert!(
        matches!(
            err,
            honcho_ai::error::HonchoError::Server { status: 500, .. }
        ),
        "expected Server(500), got {err:?}"
    );
}

#[tokio::test]
async fn chat_stream_validates_non_empty_query() {
    let server = MockServer::start().await;
    let honcho = make_honcho(&server);
    mount_peer_create(&server).await;

    let peer = honcho.peer("alice", None, None).await.unwrap();
    let result = peer.chat_stream("").send().await;
    assert!(result.is_err(), "expected error for empty query");
    let err = result.err().unwrap();
    assert!(
        matches!(err, honcho_ai::error::HonchoError::Validation(ref msg) if msg.contains("query")),
        "expected Validation error for empty query, got {err:?}"
    );
}