agentai 0.3.0

AgentAI is a Rust library designed to simplify the creation of AI agents.
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
use super::*;
use crate::message::Message;
use crate::tool::{Tool, ToolResult};
use async_trait::async_trait;
use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::{Value, json};
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;

/// Minimal OpenAI-compatible server that replays canned responses and records request bodies.
struct MockLlm {
    base_url: String,
    requests: Arc<Mutex<Vec<String>>>,
}

impl MockLlm {
    async fn start(responses: Vec<(u16, String)>) -> Self {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let base_url = format!("http://{}/v1/", listener.local_addr().unwrap());
        let queue = Arc::new(Mutex::new(VecDeque::from(responses)));
        let requests = Arc::new(Mutex::new(Vec::new()));
        let recorded = requests.clone();
        tokio::spawn(async move {
            loop {
                let (mut socket, _) = listener.accept().await.unwrap();
                let queue = queue.clone();
                let recorded = recorded.clone();
                tokio::spawn(async move {
                    let mut buf = Vec::new();
                    let mut chunk = [0u8; 4096];
                    let body = loop {
                        let n = socket.read(&mut chunk).await.unwrap();
                        if n == 0 {
                            return;
                        }
                        buf.extend_from_slice(&chunk[..n]);
                        let text = String::from_utf8_lossy(&buf).to_string();
                        if let Some((head, body)) = text.split_once("\r\n\r\n") {
                            let len = head
                                .lines()
                                .find_map(|l| {
                                    l.to_ascii_lowercase()
                                        .strip_prefix("content-length:")
                                        .map(|v| v.trim().parse::<usize>().unwrap())
                                })
                                .unwrap_or(0);
                            if body.len() >= len {
                                break body.to_string();
                            }
                        }
                    };
                    recorded.lock().unwrap().push(body);
                    let (status, payload) = queue
                        .lock()
                        .unwrap()
                        .pop_front()
                        .expect("mock LLM has no more responses");
                    let content_type = if payload.starts_with("data:") {
                        "text/event-stream"
                    } else {
                        "application/json"
                    };
                    let response = format!(
                        "HTTP/1.1 {status} Mock\r\nContent-Type: {content_type}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{payload}",
                        payload.len()
                    );
                    socket.write_all(response.as_bytes()).await.unwrap();
                    let _ = socket.shutdown().await;
                });
            }
        });
        Self { base_url, requests }
    }

    fn agent(&self) -> Agent {
        Agent::new_with_url(&self.base_url, "test-key", "system")
            .unwrap()
            .with_model("m")
    }

    fn requests(&self) -> Vec<String> {
        self.requests.lock().unwrap().clone()
    }
}

fn text_reply(text: &str) -> (u16, String) {
    let body = json!({
        "id": "1", "object": "chat.completion", "created": 0, "model": "m",
        "choices": [{
            "index": 0,
            "message": { "role": "assistant", "content": text },
            "finish_reason": "stop"
        }],
        "usage": { "prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2 }
    });
    (200, body.to_string())
}

fn tool_call_reply() -> (u16, String) {
    let body = json!({
        "id": "1", "object": "chat.completion", "created": 0, "model": "m",
        "choices": [{
            "index": 0,
            "message": {
                "role": "assistant",
                "content": null,
                "tool_calls": [{
                    "id": "call_1",
                    "type": "function",
                    "function": { "name": "echo", "arguments": "{}" }
                }]
            },
            "finish_reason": "tool_calls"
        }],
        "usage": { "prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2 }
    });
    (200, body.to_string())
}

/// Toolbox with one tool, `echo`, that always fails with the error produced by `make_error`.
struct FailingToolBox {
    make_error: fn() -> ToolError,
}

#[async_trait]
impl ToolBox for FailingToolBox {
    fn tools_definitions(&self) -> Result<Vec<Tool>, ToolError> {
        Ok(vec![Tool::new("echo")])
    }

    async fn call_tool(&self, _tool_name: String, _arguments: Value) -> ToolResult {
        Err((self.make_error)())
    }
}

#[tokio::test]
async fn recoverable_tool_error_lets_the_model_retry() {
    let llm = MockLlm::start(vec![tool_call_reply(), text_reply("recovered")]).await;
    let toolbox = FailingToolBox {
        make_error: || ToolError::InvalidArguments("timezone is unknown".to_string()),
    };

    let result: AgentResult<String> = llm
        .agent()
        .with_toolbox(toolbox)
        .run("question")
        .await
        .unwrap();

    assert_eq!(result.answer, "recovered");
    assert_eq!(result.iterations, 2);
    let requests = llm.requests();
    assert_eq!(requests.len(), 2);
    // The second request carries the tool error as the tool result
    assert!(requests[1].contains("invalid arguments: timezone is unknown"));
}

#[tokio::test]
async fn fatal_tool_error_ends_the_run() {
    let llm = MockLlm::start(vec![tool_call_reply()]).await;
    let toolbox = FailingToolBox {
        make_error: || ToolError::Configuration("missing API key".to_string()),
    };

    let err = llm
        .agent()
        .with_toolbox(toolbox)
        .run::<String>("question")
        .await
        .unwrap_err();

    match err {
        AgentError::Tool { tool, source } => {
            assert_eq!(tool, "echo");
            assert!(matches!(source, ToolError::Configuration(_)));
            assert!(!source.is_recoverable());
        }
        other => panic!("expected AgentError::Tool, got {other:?}"),
    }
    // The run stopped: the model was not asked again
    assert_eq!(llm.requests().len(), 1);
}

#[tokio::test]
async fn invalid_answer_keeps_the_raw_text() {
    #[derive(Deserialize, JsonSchema, Debug)]
    struct Answer {
        #[allow(dead_code)]
        number: u32,
    }
    let llm = MockLlm::start(vec![text_reply("not json at all")]).await;

    let err = llm.agent().run::<Answer>("question").await.unwrap_err();

    match err {
        AgentError::InvalidAnswer { raw, .. } => assert_eq!(raw, "not json at all"),
        other => panic!("expected AgentError::InvalidAnswer, got {other:?}"),
    }
}

#[tokio::test]
async fn running_out_of_iterations_is_reported() {
    let llm = MockLlm::start(vec![tool_call_reply(); 5]).await;
    let toolbox = FailingToolBox {
        make_error: || ToolError::Failed("still broken".to_string()),
    };

    let err = llm
        .agent()
        .with_toolbox(toolbox)
        .run::<String>("question")
        .await
        .unwrap_err();

    assert!(matches!(err, AgentError::MaxIterationsReached(5)));
}

#[tokio::test]
async fn provider_errors_know_if_they_are_transient() {
    let llm = MockLlm::start(vec![
        (502, r#"{"error":"bad gateway"}"#.to_string()),
        (401, r#"{"error":"unauthorized"}"#.to_string()),
    ])
    .await;

    let err = llm.agent().run::<String>("q").await.unwrap_err();
    assert!(matches!(err, AgentError::Llm(_)));
    assert!(err.is_transient());

    let err = llm.agent().run::<String>("q").await.unwrap_err();
    assert!(matches!(err, AgentError::Llm(_)));
    assert!(!err.is_transient());
}

#[test]
fn recoverable_and_fatal_tool_errors() {
    assert!(ToolError::NotFound("x".into()).is_recoverable());
    assert!(ToolError::InvalidArguments("x".into()).is_recoverable());
    assert!(ToolError::Failed("x".into()).is_recoverable());
    assert!(!ToolError::Configuration("x".into()).is_recoverable());
    assert!(!ToolError::fatal("x").is_recoverable());
}

fn request_json(llm: &MockLlm, index: usize) -> Value {
    serde_json::from_str(&llm.requests()[index]).unwrap()
}

#[tokio::test]
async fn max_iterations_can_be_configured() {
    let llm = MockLlm::start(vec![tool_call_reply(); 2]).await;
    let toolbox = FailingToolBox {
        make_error: || ToolError::Failed("still broken".to_string()),
    };

    let err = llm
        .agent()
        .with_max_iterations(2)
        .with_toolbox(toolbox)
        .run::<String>("question")
        .await
        .unwrap_err();

    assert!(matches!(err, AgentError::MaxIterationsReached(2)));
    assert_eq!(llm.requests().len(), 2);
}

#[tokio::test]
async fn zero_iterations_do_not_call_the_model() {
    let llm = MockLlm::start(vec![]).await;

    let err = llm
        .agent()
        .with_max_iterations(0)
        .run::<String>("question")
        .await
        .unwrap_err();

    assert!(matches!(err, AgentError::MaxIterationsReached(0)));
    assert!(llm.requests().is_empty());
}

#[tokio::test]
async fn default_options_send_the_default_temperature() {
    let llm = MockLlm::start(vec![text_reply("hi")]).await;

    let agent = llm.agent();
    assert_eq!(agent.max_iterations(), DEFAULT_MAX_ITERATIONS);
    let _: AgentResult<String> = agent.run("question").await.unwrap();

    assert_eq!(
        request_json(&llm, 0)["temperature"],
        json!(DEFAULT_TEMPERATURE)
    );
}

#[tokio::test]
async fn chat_options_are_sent_with_every_request() {
    let llm = MockLlm::start(vec![text_reply("hi")]).await;
    let options = ChatOptions::default()
        .with_temperature(0.7)
        .with_max_tokens(123)
        .with_tool_choice(ToolChoice::Required);

    let _: AgentResult<String> = llm
        .agent()
        .with_chat_options(options)
        .run("question")
        .await
        .unwrap();

    let request = request_json(&llm, 0);
    assert_eq!(request["temperature"], json!(0.7));
    let max_tokens = request
        .get("max_tokens")
        .or_else(|| request.get("max_completion_tokens"));
    assert_eq!(max_tokens, Some(&json!(123)));
    assert_eq!(request["tool_choice"], json!("required"));
}

#[tokio::test]
async fn structured_output_keeps_custom_options() {
    #[derive(Deserialize, JsonSchema)]
    #[allow(dead_code)]
    struct Answer {
        value: u32,
    }

    let llm = MockLlm::start(vec![text_reply(r#"{"value": 7}"#)]).await;

    let result: AgentResult<Answer> = llm
        .agent()
        .with_chat_options(ChatOptions::default().with_temperature(0.0))
        .run("question")
        .await
        .unwrap();

    assert_eq!(result.answer.value, 7);
    let request = request_json(&llm, 0);
    assert_eq!(request["temperature"], json!(0.0));
    assert!(request.get("response_format").is_some());
}

struct StrictTools;

#[crate::tool::toolbox]
impl StrictTools {
    /// Strict echo.
    #[tool(strict)]
    fn strict_echo(
        &self,
        /// Text to echo
        text: String,
    ) -> ToolResult {
        Ok(text)
    }

    /// Plain echo.
    #[tool]
    fn plain_echo(
        &self,
        /// Text to echo
        text: String,
    ) -> ToolResult {
        Ok(text)
    }
}

#[tokio::test]
async fn strict_tools_are_sent_as_strict() {
    let llm = MockLlm::start(vec![text_reply("hi")]).await;

    let _: AgentResult<String> = llm
        .agent()
        .with_toolbox(StrictTools)
        .run("question")
        .await
        .unwrap();

    let request = request_json(&llm, 0);
    let tools = request["tools"].as_array().unwrap();
    let function = |name: &str| {
        tools
            .iter()
            .map(|t| &t["function"])
            .find(|f| f["name"] == json!(name))
            .unwrap()
            .clone()
    };

    let strict = function("strict_echo");
    assert_eq!(strict["strict"], json!(true));
    assert_eq!(strict["parameters"]["additionalProperties"], json!(false));
    assert_eq!(strict["parameters"]["required"], json!(["text"]));
    assert_eq!(function("plain_echo")["strict"], json!(false));
}

/// Toolbox with one tool, `echo`, that returns "echoed".
struct EchoToolBox;

#[async_trait]
impl ToolBox for EchoToolBox {
    fn tools_definitions(&self) -> Result<Vec<Tool>, ToolError> {
        Ok(vec![Tool::new("echo")])
    }

    async fn call_tool(&self, _tool_name: String, _arguments: Value) -> ToolResult {
        Ok("echoed".to_string())
    }
}

fn sse(chunks: Vec<Value>) -> (u16, String) {
    let mut body = String::new();
    for chunk in chunks {
        body.push_str(&format!("data: {chunk}\n\n"));
    }
    body.push_str("data: [DONE]\n\n");
    (200, body)
}

fn sse_chunk(delta: Value, finish_reason: Option<&str>) -> Value {
    json!({
        "id": "1", "object": "chat.completion.chunk", "created": 0, "model": "m",
        "choices": [{ "index": 0, "delta": delta, "finish_reason": finish_reason }]
    })
}

fn sse_usage() -> Value {
    json!({
        "id": "1", "object": "chat.completion.chunk", "created": 0, "model": "m",
        "choices": [],
        "usage": { "prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2 }
    })
}

fn streamed_text_reply(parts: &[&str]) -> (u16, String) {
    let mut chunks: Vec<Value> = parts
        .iter()
        .map(|part| sse_chunk(json!({ "role": "assistant", "content": part }), None))
        .collect();
    chunks.push(sse_chunk(json!({}), Some("stop")));
    chunks.push(sse_usage());
    sse(chunks)
}

fn streamed_tool_call_reply() -> (u16, String) {
    sse(vec![
        sse_chunk(
            json!({ "role": "assistant", "tool_calls": [{
                "index": 0, "id": "call_1", "type": "function",
                "function": { "name": "echo", "arguments": "" }
            }]}),
            None,
        ),
        sse_chunk(
            json!({ "tool_calls": [{ "index": 0, "function": { "arguments": "{}" } }]}),
            None,
        ),
        sse_chunk(json!({}), Some("tool_calls")),
        sse_usage(),
    ])
}

fn truncated_reply(text: &str) -> (u16, String) {
    let body = json!({
        "id": "1", "object": "chat.completion", "created": 0, "model": "m",
        "choices": [{
            "index": 0,
            "message": { "role": "assistant", "content": text },
            "finish_reason": "length"
        }],
        "usage": { "prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2 }
    });
    (200, body.to_string())
}

/// Messages as JSON, to compare them.
fn messages_json(messages: &[Message]) -> Value {
    serde_json::to_value(messages).unwrap()
}

#[tokio::test]
async fn run_returns_only_the_new_messages() {
    let llm = MockLlm::start(vec![tool_call_reply(), text_reply("done")]).await;
    let history = vec![
        Message::user("earlier question"),
        Message::assistant("earlier answer"),
    ];

    let result: AgentResult<String> = llm
        .agent()
        .with_toolbox(EchoToolBox)
        .run("question")
        .with_history(&history)
        .await
        .unwrap();

    assert_eq!(result.answer, "done");
    // prompt, assistant tool call, tool response, final answer
    assert_eq!(result.new_messages.len(), 4);
    let new = messages_json(&result.new_messages);
    assert!(!new.to_string().contains("earlier"));
    assert!(!new.to_string().contains("\"system\""));
    assert_eq!(result.usage.total_tokens, 4);
    assert_eq!(result.iterations, 2);
    assert!(matches!(result.stop_reason, Some(StopReason::Completed)));

    // The request carries the system prompt and the history before the prompt
    let messages = request_json(&llm, 0)["messages"].clone();
    assert_eq!(messages[0]["role"], json!("system"));
    assert_eq!(messages[0]["content"], json!("system"));
    assert_eq!(messages[1]["content"], json!("earlier question"));
    assert_eq!(messages[2]["content"], json!("earlier answer"));
    assert_eq!(messages[3]["content"], json!("question"));
}

#[tokio::test]
async fn appended_messages_continue_the_conversation() {
    let llm = MockLlm::start(vec![text_reply("first"), text_reply("second")]).await;
    let agent = llm.agent();
    let mut history = Vec::new();

    let result: AgentResult<String> = agent.run("one").with_history(&history).await.unwrap();
    history.extend(result.new_messages);
    let result: AgentResult<String> = agent.run("two").with_history(&history).await.unwrap();
    history.extend(result.new_messages);

    assert_eq!(history.len(), 4);
    let contents: Vec<Value> = request_json(&llm, 1)["messages"]
        .as_array()
        .unwrap()
        .iter()
        .map(|m| m["content"].clone())
        .collect();
    assert_eq!(
        contents,
        vec![json!("system"), json!("one"), json!("first"), json!("two")]
    );
}

#[tokio::test]
async fn run_options_replace_the_agent_settings() {
    let llm = MockLlm::start(vec![tool_call_reply(), text_reply("unused")]).await;
    let toolbox = FailingToolBox {
        make_error: || ToolError::Failed("still broken".to_string()),
    };

    let err = llm
        .agent()
        .run::<String>("question")
        .with_options(
            RunOptions::default()
                .with_model("other-model")
                .with_max_iterations(1)
                .with_toolbox(toolbox),
        )
        .await
        .unwrap_err();

    assert!(matches!(err, AgentError::MaxIterationsReached(1)));
    let request = request_json(&llm, 0);
    assert_eq!(request["model"], json!("other-model"));
    assert_eq!(request["tools"][0]["function"]["name"], json!("echo"));
}

#[tokio::test]
async fn a_run_without_a_model_fails_before_calling_the_model() {
    let llm = MockLlm::start(vec![]).await;
    let agent = Agent::new_with_url(&llm.base_url, "test-key", "system").unwrap();

    let err = agent.run::<String>("question").await.unwrap_err();
    assert!(matches!(err, AgentError::MissingModel));
    let err = agent.run_stream::<String>("question").await.err().unwrap();
    assert!(matches!(err, AgentError::MissingModel));
    assert!(llm.requests().is_empty());

    // A model set for the run is enough
    let llm = MockLlm::start(vec![text_reply("hi")]).await;
    let agent = Agent::new_with_url(&llm.base_url, "test-key", "system").unwrap();
    let result: AgentResult<String> = agent.run("question").with_model("m").await.unwrap();
    assert_eq!(result.answer, "hi");
}

#[tokio::test]
async fn truncated_answer_is_reported() {
    let llm = MockLlm::start(vec![truncated_reply(r#"{"value":"#)]).await;

    let err = llm.agent().run::<String>("question").await.unwrap_err();

    match err {
        AgentError::MaxTokens { raw } => assert_eq!(raw, r#"{"value":"#),
        other => panic!("expected AgentError::MaxTokens, got {other:?}"),
    }
}

#[tokio::test]
async fn stream_sends_deltas_tool_events_and_the_result() {
    use futures_util::StreamExt;

    let llm = MockLlm::start(vec![
        streamed_tool_call_reply(),
        streamed_text_reply(&["Hel", "lo"]),
    ])
    .await;

    let mut stream = llm
        .agent()
        .with_toolbox(EchoToolBox)
        .run_stream::<String>("question")
        .await
        .unwrap();
    let mut events = Vec::new();
    let mut result = None;
    while let Some(event) = stream.next().await {
        match event.unwrap() {
            AgentEvent::TextDelta(text) => events.push(format!("text:{text}")),
            AgentEvent::ToolCall(call) => events.push(format!("call:{}", call.name)),
            AgentEvent::ToolResult {
                tool,
                content,
                is_error,
                ..
            } => events.push(format!("result:{tool}:{content}:{is_error}")),
            AgentEvent::Finished(res) => result = Some(res),
            other => panic!("unexpected event {other:?}"),
        }
    }

    assert_eq!(
        events,
        vec![
            "call:echo",
            "result:echo:echoed:false",
            "text:Hel",
            "text:lo"
        ]
    );
    let result = result.expect("the stream ends with Finished");
    assert_eq!(result.answer, "Hello");
    assert_eq!(result.new_messages.len(), 4);
    assert_eq!(result.usage.total_tokens, 4);
    assert_eq!(request_json(&llm, 0)["stream"], json!(true));
}

#[tokio::test]
async fn streamed_and_awaited_runs_produce_the_same_messages() {
    let awaited_llm = MockLlm::start(vec![tool_call_reply(), text_reply("Hello")]).await;
    let awaited: AgentResult<String> = awaited_llm
        .agent()
        .with_toolbox(EchoToolBox)
        .run("question")
        .await
        .unwrap();

    let streamed_llm = MockLlm::start(vec![
        streamed_tool_call_reply(),
        streamed_text_reply(&["Hel", "lo"]),
    ])
    .await;
    let streamed = streamed_llm
        .agent()
        .with_toolbox(EchoToolBox)
        .run_stream::<String>("question")
        .await
        .unwrap()
        .finish()
        .await
        .unwrap();

    assert_eq!(
        messages_json(&streamed.new_messages),
        messages_json(&awaited.new_messages)
    );
    // Both send the same conversation to the model in the second call
    assert_eq!(
        request_json(&streamed_llm, 1)["messages"],
        request_json(&awaited_llm, 1)["messages"]
    );
}

#[tokio::test]
async fn streamed_structured_output_is_parsed_at_the_end() {
    #[derive(Deserialize, JsonSchema, Debug)]
    struct Answer {
        value: u32,
    }
    let llm = MockLlm::start(vec![streamed_text_reply(&[r#"{"val"#, r#"ue": 7}"#])]).await;

    let result = llm
        .agent()
        .run_stream::<Answer>("question")
        .await
        .unwrap()
        .finish()
        .await
        .unwrap();

    assert_eq!(result.answer.value, 7);
}

#[tokio::test]
async fn fatal_tool_error_ends_the_stream() {
    use futures_util::StreamExt;

    let llm = MockLlm::start(vec![streamed_tool_call_reply()]).await;
    let toolbox = FailingToolBox {
        make_error: || ToolError::Configuration("missing API key".to_string()),
    };

    let mut stream = llm
        .agent()
        .with_toolbox(toolbox)
        .run_stream::<String>("q")
        .await
        .unwrap();
    assert!(matches!(
        stream.next().await,
        Some(Ok(AgentEvent::ToolCall(_)))
    ));
    assert!(matches!(
        stream.next().await,
        Some(Ok(AgentEvent::ToolResult { is_error: true, .. }))
    ));
    assert!(matches!(
        stream.next().await,
        Some(Err(AgentError::Tool { .. }))
    ));
    assert!(stream.next().await.is_none());
}

#[test]
fn runs_and_streams_can_be_moved_to_other_tasks() {
    fn assert_send_static<T: Send + 'static>(_: &T) {}
    fn assert_send_static_type<T: Send + 'static>() {}
    let agent = Agent::new_with_url("http://localhost/", "key", "system").unwrap();
    let history = vec![Message::user("hi")];
    let future =
        std::future::IntoFuture::into_future(agent.run::<String>("q").with_history(&history));
    assert_send_static(&future);
    let future = std::future::IntoFuture::into_future(
        agent.run_stream::<String>("q").with_history(&history),
    );
    assert_send_static(&future);
    assert_send_static_type::<AgentStream<String>>();
    fn assert_send_sync_clone<T: Send + Sync + Clone>() {}
    assert_send_sync_clone::<Agent>();
}

/// Toolbox that counts its calls.
struct CountingToolBox(Arc<Mutex<usize>>);

#[async_trait]
impl ToolBox for CountingToolBox {
    fn tools_definitions(&self) -> Result<Vec<Tool>, ToolError> {
        Ok(vec![Tool::new("echo")])
    }

    async fn call_tool(&self, _tool_name: String, _arguments: Value) -> ToolResult {
        *self.0.lock().unwrap() += 1;
        Ok("echoed".to_string())
    }
}

fn truncated_tool_call_reply() -> (u16, String) {
    let body = json!({
        "id": "1", "object": "chat.completion", "created": 0, "model": "m",
        "choices": [{
            "index": 0,
            "message": {
                "role": "assistant",
                "content": null,
                "tool_calls": [{
                    "id": "call_1",
                    "type": "function",
                    "function": { "name": "echo", "arguments": "{}" }
                }]
            },
            "finish_reason": "length"
        }],
        "usage": { "prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2 }
    });
    (200, body.to_string())
}

#[tokio::test]
async fn truncated_tool_calls_are_not_run() {
    let llm = MockLlm::start(vec![truncated_tool_call_reply()]).await;
    let calls = Arc::new(Mutex::new(0));

    let err = llm
        .agent()
        .with_toolbox(CountingToolBox(calls.clone()))
        .run::<String>("question")
        .await
        .unwrap_err();

    match err {
        AgentError::MaxTokens { raw } => assert_eq!(raw, ""),
        other => panic!("expected AgentError::MaxTokens, got {other:?}"),
    }
    assert_eq!(*calls.lock().unwrap(), 0);
    assert_eq!(llm.requests().len(), 1);
}

#[tokio::test]
async fn truncated_answer_without_text_does_not_use_up_iterations() {
    let body = json!({
        "id": "1", "object": "chat.completion", "created": 0, "model": "m",
        "choices": [{
            "index": 0,
            "message": { "role": "assistant", "content": "" },
            "finish_reason": "length"
        }],
        "usage": { "prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2 }
    });
    let llm = MockLlm::start(vec![(200, body.to_string())]).await;

    let err = llm.agent().run::<String>("question").await.unwrap_err();

    assert!(matches!(err, AgentError::MaxTokens { .. }));
    assert_eq!(llm.requests().len(), 1);
}