iicp-client 0.7.78

Use the open IICP AI mesh from Rust without running a node
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// SPDX-License-Identifier: Apache-2.0
//! Unit tests for the openai_compat backend helper. Uses mockito to mock
//! the upstream provider so the tests don't need a real Ollama / vLLM.

use std::time::Duration;

use iicp_client::backends::openai_compat::{invoke, OpenAiCompatOptions};
use iicp_client::backends::{invoke_backend, llamacpp, vllm, BACKEND_TYPES};
use serde_json::json;

fn opts(base_url: String, model: Option<&str>) -> OpenAiCompatOptions {
    OpenAiCompatOptions {
        base_url,
        model: model.map(String::from),
        api_key: None,
        timeout: Duration::from_secs(5),
    }
}

#[tokio::test]
async fn test_chat_completion_happy_path() {
    let mut server = mockito::Server::new_async().await;
    let _m = server
        .mock("POST", "/chat/completions")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"id":"chatcmpl-test","choices":[{"message":{"content":"PONG"}}]}"#)
        .create_async()
        .await;

    let result = invoke(
        &opts(server.url(), Some("qwen2.5:0.5b")),
        "urn:iicp:intent:llm:chat:v1",
        &json!({"messages": [{"role":"user","content":"hi"}]}),
    )
    .await;
    assert!(
        result.get("error_code").is_none(),
        "unexpected error: {result}"
    );
    assert_eq!(
        result["result"]["id"].as_str().unwrap_or(""),
        "chatcmpl-test"
    );
}

#[tokio::test]
async fn test_factory_model_is_injected_when_payload_missing() {
    let mut server = mockito::Server::new_async().await;
    let _m = server
        .mock("POST", "/chat/completions")
        .match_body(mockito::Matcher::PartialJson(json!({
            "model": "qwen2.5:0.5b"
        })))
        .with_status(200)
        .with_body("{}")
        .create_async()
        .await;

    let result = invoke(
        &opts(server.url(), Some("qwen2.5:0.5b")),
        "urn:iicp:intent:llm:chat:v1",
        &json!({"messages": []}),
    )
    .await;
    assert!(result.get("error_code").is_none(), "unexpected: {result}");
}

#[tokio::test]
async fn test_task_payload_model_overrides_factory() {
    let mut server = mockito::Server::new_async().await;
    let _m = server
        .mock("POST", "/chat/completions")
        .match_body(mockito::Matcher::PartialJson(json!({
            "model": "llama-3-8b"
        })))
        .with_status(200)
        .with_body("{}")
        .create_async()
        .await;

    let result = invoke(
        &opts(server.url(), Some("qwen2.5:0.5b")),
        "urn:iicp:intent:llm:chat:v1",
        &json!({"messages": [], "model": "llama-3-8b"}),
    )
    .await;
    assert!(result.get("error_code").is_none(), "unexpected: {result}");
}

#[tokio::test]
async fn test_completion_intent_routes_to_completions_path() {
    let mut server = mockito::Server::new_async().await;
    let _m = server
        .mock("POST", "/completions")
        .with_status(200)
        .with_body(r#"{"choices":[{"text":"PONG"}]}"#)
        .create_async()
        .await;

    let result = invoke(
        &opts(server.url(), Some("q")),
        "urn:iicp:intent:llm:completion:v1",
        &json!({"prompt":"ping"}),
    )
    .await;
    assert_eq!(
        result["result"]["choices"][0]["text"].as_str(),
        Some("PONG")
    );
}

#[tokio::test]
async fn test_embedding_intent_routes_to_embeddings_path() {
    let mut server = mockito::Server::new_async().await;
    let _m = server
        .mock("POST", "/embeddings")
        .with_status(200)
        .with_body(r#"{"data":[{"embedding":[0.1,0.2]}]}"#)
        .create_async()
        .await;

    let result = invoke(
        &opts(server.url(), Some("text-embedding-3-small")),
        "urn:iicp:intent:llm:embedding:v1",
        &json!({"input":"hello"}),
    )
    .await;
    assert!(result["result"]["data"][0]["embedding"].is_array());
}

#[tokio::test]
async fn test_unsupported_intent_returns_400() {
    let server = mockito::Server::new_async().await;
    let result = invoke(
        &opts(server.url(), Some("q")),
        "urn:iicp:intent:llm:fancy:v1",
        &json!({}),
    )
    .await;
    assert_eq!(result["error_code"].as_u64(), Some(400));
    assert!(result["error_message"]
        .as_str()
        .unwrap_or("")
        .contains("unsupported intent"));
}

#[tokio::test]
async fn test_no_model_returns_400() {
    let server = mockito::Server::new_async().await;
    let result = invoke(
        &opts(server.url(), None),
        "urn:iicp:intent:llm:chat:v1",
        &json!({"messages": []}),
    )
    .await;
    assert_eq!(result["error_code"].as_u64(), Some(400));
    assert!(result["error_message"]
        .as_str()
        .unwrap_or("")
        .contains("no model"));
}

#[tokio::test]
async fn test_non_object_payload_returns_400() {
    let server = mockito::Server::new_async().await;
    let result = invoke(
        &opts(server.url(), Some("q")),
        "urn:iicp:intent:llm:chat:v1",
        &json!("string-not-object"),
    )
    .await;
    assert_eq!(result["error_code"].as_u64(), Some(400));
    assert!(result["error_message"]
        .as_str()
        .unwrap_or("")
        .contains("must be a JSON object"));
}

#[tokio::test]
async fn test_upstream_500_is_surfaced() {
    let mut server = mockito::Server::new_async().await;
    let _m = server
        .mock("POST", "/chat/completions")
        .with_status(500)
        .with_body("model not loaded")
        .create_async()
        .await;

    let result = invoke(
        &opts(server.url(), Some("q")),
        "urn:iicp:intent:llm:chat:v1",
        &json!({"messages": []}),
    )
    .await;
    assert_eq!(result["error_code"].as_u64(), Some(500));
    assert!(result["error_message"]
        .as_str()
        .unwrap_or("")
        .contains("model not loaded"));
}

#[tokio::test]
async fn test_upstream_429_rate_limit_surfaced() {
    let mut server = mockito::Server::new_async().await;
    let _m = server
        .mock("POST", "/chat/completions")
        .with_status(429)
        .with_body("rate limit exceeded")
        .create_async()
        .await;

    let result = invoke(
        &opts(server.url(), Some("q")),
        "urn:iicp:intent:llm:chat:v1",
        &json!({"messages": []}),
    )
    .await;
    assert_eq!(result["error_code"].as_u64(), Some(429));
}

#[tokio::test]
async fn test_api_key_sets_bearer_auth() {
    let mut server = mockito::Server::new_async().await;
    let _m = server
        .mock("POST", "/chat/completions")
        .match_header("authorization", "Bearer sk-test-1234")
        .with_status(200)
        .with_body("{}")
        .create_async()
        .await;

    let mut o = opts(server.url(), Some("q"));
    o.api_key = Some("sk-test-1234".into());
    let result = invoke(&o, "urn:iicp:intent:llm:chat:v1", &json!({"messages": []})).await;
    assert!(result.get("error_code").is_none(), "unexpected: {result}");
}

#[tokio::test]
async fn test_base_url_trailing_slash_normalized() {
    let mut server = mockito::Server::new_async().await;
    let _m = server
        .mock("POST", "/chat/completions")
        .with_status(200)
        .with_body("{}")
        .create_async()
        .await;

    // server.url() ends without a trailing slash; we add one to test normalization.
    let result = invoke(
        &opts(format!("{}/", server.url()), Some("q")),
        "urn:iicp:intent:llm:chat:v1",
        &json!({"messages": []}),
    )
    .await;
    assert!(result.get("error_code").is_none(), "unexpected: {result}");
}

// ── Dedicated backends (vLLM / llama.cpp) + selector — parity Block B ────────

#[tokio::test]
async fn test_vllm_invoke_happy_path() {
    let mut server = mockito::Server::new_async().await;
    let _m = server
        .mock("POST", "/chat/completions")
        .with_status(200)
        .with_body(r#"{"id":"ok"}"#)
        .create_async()
        .await;
    let result = vllm::invoke(
        &opts(server.url(), Some("mistral-7b")),
        "urn:iicp:intent:llm:chat:v1",
        &json!({"messages": []}),
    )
    .await;
    assert!(result.get("error_code").is_none(), "unexpected: {result}");
    assert_eq!(result["result"]["id"].as_str(), Some("ok"));
}

#[tokio::test]
async fn test_vllm_error_message_uses_engine_label() {
    let server = mockito::Server::new_async().await;
    let result = vllm::invoke(
        &opts(server.url(), Some("m")),
        "urn:iicp:intent:bogus:v1",
        &json!({}),
    )
    .await;
    assert_eq!(result["error_code"].as_u64(), Some(400));
    assert!(result["error_message"]
        .as_str()
        .unwrap_or("")
        .starts_with("vllm:"));
}

#[tokio::test]
async fn test_llamacpp_invoke_happy_path() {
    let mut server = mockito::Server::new_async().await;
    let _m = server
        .mock("POST", "/chat/completions")
        .with_status(200)
        .with_body(r#"{"id":"ok"}"#)
        .create_async()
        .await;
    let result = llamacpp::invoke(
        &opts(server.url(), Some("gguf")),
        "urn:iicp:intent:llm:chat:v1",
        &json!({"messages": []}),
    )
    .await;
    assert!(result.get("error_code").is_none(), "unexpected: {result}");
}

#[test]
fn test_default_options_ports() {
    assert!(vllm::default_options().base_url.contains(":8000"));
    assert!(llamacpp::default_options().base_url.contains(":8080"));
}

#[tokio::test]
async fn test_invoke_backend_dispatches_and_rejects_unknown() {
    let server = mockito::Server::new_async().await;
    let o = opts(server.url(), Some("m"));
    for t in BACKEND_TYPES {
        // unsupported intent → 400 from the engine, but dispatch itself is Ok(_)
        let r = invoke_backend(t, &o, "urn:iicp:intent:bogus:v1", &json!({})).await;
        assert!(r.is_ok(), "dispatch for {t} should be Ok");
    }
    let bad = invoke_backend("nope", &o, "urn:iicp:intent:llm:chat:v1", &json!({})).await;
    assert!(bad.is_err());
    assert!(bad.unwrap_err().contains("unknown backend_type"));
}

// ── #414 audio:transcribe (STT) — multipart file upload ──────────────────────

#[tokio::test]
async fn test_audio_transcribe_posts_multipart_and_returns_text() {
    use base64::{engine::general_purpose::STANDARD, Engine};
    let mut server = mockito::Server::new_async().await;
    let _m = server
        .mock("POST", "/audio/transcriptions")
        .match_header(
            "content-type",
            mockito::Matcher::Regex("multipart/form-data".into()),
        )
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"text":"hello world"}"#)
        .create_async()
        .await;

    let payload = json!({
        "audio": STANDARD.encode(b"RIFF....fake-wav-bytes"),
        "filename": "clip.wav",
        "language": "en",
    });
    let result = invoke(
        &opts(server.url(), Some("whisper-1")),
        "urn:iicp:intent:audio:transcribe:v1",
        &payload,
    )
    .await;
    assert!(
        result.get("error_code").is_none(),
        "unexpected error: {result}"
    );
    assert_eq!(
        result["result"]["text"].as_str().unwrap_or(""),
        "hello world"
    );
}

#[tokio::test]
async fn test_audio_transcribe_rejects_invalid_base64() {
    let result = invoke(
        &opts("http://127.0.0.1:1".into(), Some("whisper-1")),
        "urn:iicp:intent:audio:transcribe:v1",
        &json!({"audio": "!!not-base64!!"}),
    )
    .await;
    assert_eq!(result["error_code"].as_u64(), Some(400));
    assert!(result["error_message"]
        .as_str()
        .unwrap_or("")
        .contains("base64"));
}

#[tokio::test]
async fn test_audio_transcribe_requires_audio_field() {
    let result = invoke(
        &opts("http://127.0.0.1:1".into(), Some("whisper-1")),
        "urn:iicp:intent:audio:transcribe:v1",
        &json!({}),
    )
    .await;
    assert_eq!(result["error_code"].as_u64(), Some(400));
    assert!(result["error_message"]
        .as_str()
        .unwrap_or("")
        .contains("audio"));
}

/// Live end-to-end ratification (#414, #408 discipline). Requires a running
/// whisper.cpp `whisper-server` on :8090 serving /v1/audio/transcriptions:
///   whisper-server -m ggml-tiny.en.bin --port 8090 \
///     --inference-path /v1/audio/transcriptions
/// Run with: cargo test --test backends_tests -- --ignored audio_transcribe_live
#[tokio::test]
#[ignore]
async fn test_audio_transcribe_live_whisper_server() {
    use base64::{engine::general_purpose::STANDARD, Engine};
    let wav = std::fs::read("/opt/homebrew/Cellar/whisper-cpp/1.8.6/share/whisper-cpp/jfk.wav")
        .expect("jfk.wav sample");
    let payload = json!({"audio": STANDARD.encode(&wav), "filename": "jfk.wav"});
    let result = invoke(
        &opts("http://127.0.0.1:8090/v1".into(), None),
        "urn:iicp:intent:audio:transcribe:v1",
        &payload,
    )
    .await;
    let text = result["result"]["text"]
        .as_str()
        .unwrap_or("")
        .to_lowercase();
    assert!(
        text.contains("country"),
        "expected transcription, got {result}"
    );
}

// ── #414 audio:speech (TTS) — JSON request, binary audio response ────────────

#[tokio::test]
async fn test_audio_speech_returns_base64_audio() {
    use base64::{engine::general_purpose::STANDARD, Engine};
    let mut server = mockito::Server::new_async().await;
    let _m = server
        .mock("POST", "/audio/speech")
        .match_body(mockito::Matcher::PartialJson(
            json!({"input": "hello world"}),
        ))
        .with_status(200)
        .with_header("content-type", "audio/wav")
        .with_body(b"RIFF....fake-wav-audio")
        .create_async()
        .await;

    let result = invoke(
        &opts(server.url(), Some("tts-1")),
        "urn:iicp:intent:audio:speech:v1",
        &json!({"input": "hello world", "voice": "alloy"}),
    )
    .await;
    assert!(
        result.get("error_code").is_none(),
        "unexpected error: {result}"
    );
    assert_eq!(
        result["result"]["content_type"].as_str().unwrap_or(""),
        "audio/wav"
    );
    let decoded = STANDARD
        .decode(result["result"]["audio"].as_str().unwrap_or(""))
        .unwrap();
    assert_eq!(decoded, b"RIFF....fake-wav-audio");
}

#[tokio::test]
async fn test_audio_speech_requires_input_field() {
    let result = invoke(
        &opts("http://127.0.0.1:1".into(), Some("tts-1")),
        "urn:iicp:intent:audio:speech:v1",
        &json!({}),
    )
    .await;
    assert_eq!(result["error_code"].as_u64(), Some(400));
    assert!(result["error_message"]
        .as_str()
        .unwrap_or("")
        .contains("input"));
}

/// Live ratification (#414): requires an OpenAI-compat /v1/audio/speech backend on
/// :8091 (e.g. the espeak-ng shim documented in FORGE_STATE iter-2026).
/// Run with: cargo test --test backends_tests -- --ignored audio_speech_live
#[tokio::test]
#[ignore]
async fn test_audio_speech_live_espeak_server() {
    use base64::{engine::general_purpose::STANDARD, Engine};
    let result = invoke(
        &opts("http://127.0.0.1:8091/v1".into(), None),
        "urn:iicp:intent:audio:speech:v1",
        &json!({"input": "Ask not what your country can do for you.", "voice": "en"}),
    )
    .await;
    let audio = STANDARD
        .decode(result["result"]["audio"].as_str().unwrap_or(""))
        .expect("base64 audio");
    assert_eq!(&audio[..4], b"RIFF", "expected wav audio, got {result}");
    assert!(audio.len() > 1000, "expected real audio bytes");
}

// ── #414 safety:moderate (content moderation) — JSON in/out, model-optional ──

#[tokio::test]
async fn test_safety_moderate_routes_without_model() {
    let mut server = mockito::Server::new_async().await;
    let _m = server
        .mock("POST", "/moderations")
        .match_body(mockito::Matcher::PartialJson(json!({"input": "bad text"})))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"results":[{"flagged":true,"categories":{"harassment":true},"category_scores":{"harassment":0.99}}]}"#)
        .create_async()
        .await;

    // NO model configured — moderation must still route (model-optional).
    let result = invoke(
        &opts(server.url(), None),
        "urn:iicp:intent:safety:moderate:v1",
        &json!({"input": "bad text"}),
    )
    .await;
    assert!(
        result.get("error_code").is_none(),
        "unexpected error: {result}"
    );
    assert_eq!(
        result["result"]["results"][0]["flagged"].as_bool(),
        Some(true)
    );
}

/// Live ratification (#414): requires an OpenAI-compat /v1/moderations backend on
/// :8092 (e.g. the unitary/toxic-bert shim documented in FORGE_STATE iter-2027).
/// Run with: cargo test --test backends_tests -- --ignored safety_moderate_live
#[tokio::test]
#[ignore]
async fn test_safety_moderate_live_toxicbert() {
    let toxic = invoke(
        &opts("http://127.0.0.1:8092/v1".into(), None),
        "urn:iicp:intent:safety:moderate:v1",
        &json!({"input": "I will hurt you, you idiot"}),
    )
    .await;
    let benign = invoke(
        &opts("http://127.0.0.1:8092/v1".into(), None),
        "urn:iicp:intent:safety:moderate:v1",
        &json!({"input": "the weather is lovely today"}),
    )
    .await;
    assert_eq!(
        toxic["result"]["results"][0]["flagged"].as_bool(),
        Some(true),
        "{toxic}"
    );
    assert_eq!(
        benign["result"]["results"][0]["flagged"].as_bool(),
        Some(false),
        "{benign}"
    );
}