langchain-rust 7.0.0

LangChain for Rust, the easiest way to write LLM-based programs in Rust
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
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
//! Offline end-to-end tests for agents + `AgentExecutor`, driven by a scripted fake LLM.
use std::{
    error::Error,
    sync::{Arc, Mutex as StdMutex},
};

use async_trait::async_trait;
use serde_json::{json, Value};
use tokio::sync::Mutex;

use crate::{
    agent::{AgentExecutor, ConversationalAgentBuilder, OpenAiToolAgentBuilder},
    chain::{Chain, ChainError},
    memory::SimpleMemory,
    prompt_args,
    schemas::{memory::BaseMemory, MessageType},
    test_utils::FakeLLM,
    tools::Tool,
};

/// Tool that records the inputs it receives and echoes a fixed answer (or fails).
#[derive(Clone)]
struct RecordingTool {
    name: String,
    answer: Result<String, String>,
    inputs: Arc<StdMutex<Vec<Value>>>,
}

impl RecordingTool {
    fn ok(name: &str, answer: &str) -> Self {
        Self {
            name: name.into(),
            answer: Ok(answer.into()),
            inputs: Default::default(),
        }
    }
    fn failing(name: &str, err: &str) -> Self {
        Self {
            name: name.into(),
            answer: Err(err.into()),
            inputs: Default::default(),
        }
    }
    fn inputs(&self) -> Vec<Value> {
        self.inputs.lock().unwrap().clone()
    }
}

#[async_trait]
impl Tool for RecordingTool {
    fn name(&self) -> String {
        self.name.clone()
    }
    fn description(&self) -> String {
        format!("{} tool", self.name)
    }
    async fn run(&self, input: Value) -> Result<String, Box<dyn Error>> {
        self.inputs.lock().unwrap().push(input);
        self.answer.clone().map_err(|e| e.into())
    }
}

fn action(tool: &str, input: &str) -> String {
    format!("```json\n{{\"action\": \"{tool}\", \"action_input\": \"{input}\"}}\n```")
}

fn final_answer(answer: &str) -> String {
    action("Final Answer", answer)
}

#[tokio::test]
async fn conversational_agent_uses_tool_then_answers() {
    let calc = RecordingTool::ok("Calculator", "4");
    let llm = FakeLLM::new([action("Calculator", "2+2"), final_answer("It is 4")]);
    let agent = ConversationalAgentBuilder::new()
        .tools(&[Arc::new(calc.clone())])
        .build(llm.clone())
        .unwrap();
    let memory = Arc::new(Mutex::new(SimpleMemory::new()));
    let executor = AgentExecutor::from_agent(agent).with_memory(memory.clone());

    let out = executor
        .invoke(prompt_args! {"input" => "what is 2+2?"})
        .await
        .unwrap();
    assert_eq!(out, "It is 4");
    assert_eq!(calc.inputs(), vec![json!("2+2")]);

    // Prompt contains the tool description and the user input
    let calls = llm.calls();
    assert_eq!(calls.len(), 2);
    let first_prompt: String = calls[0].iter().map(|m| m.content.clone()).collect();
    assert!(first_prompt.contains("> Calculator: Calculator tool"));
    assert!(first_prompt.contains("what is 2+2?"));
    // Second call carries the tool observation in the scratchpad
    let second_prompt: String = calls[1].iter().map(|m| m.content.clone()).collect();
    assert!(second_prompt.contains("TOOL RESPONSE"));
    assert!(second_prompt.contains('4'));

    // Default chain options (max_tokens = 1000) are pushed to the LLM
    let last_max_tokens = llm.options.lock().unwrap().last().unwrap().max_tokens;
    assert_eq!(last_max_tokens, Some(1000));

    let messages = memory.lock().await.messages().await.unwrap();
    assert_eq!(messages.len(), 2);
    assert_eq!(messages[0].message_type, MessageType::HumanMessage);
    assert_eq!(messages[0].content, "what is 2+2?");
    assert_eq!(messages[1].content, "It is 4");
}

#[tokio::test]
async fn tool_names_with_spaces_are_matched() {
    let tool = RecordingTool::ok("Web Search", "result");
    let llm = FakeLLM::new([action("Web Search", "rust"), final_answer("done")]);
    let agent = ConversationalAgentBuilder::new()
        .tools(&[Arc::new(tool.clone())])
        .build(llm)
        .unwrap();
    let out = AgentExecutor::from_agent(agent)
        .invoke(prompt_args! {"input" => "q"})
        .await
        .unwrap();
    assert_eq!(out, "done");
    assert_eq!(tool.inputs(), vec![json!("rust")]);
}

#[tokio::test]
async fn unknown_tool_is_an_error() {
    let llm = FakeLLM::new([action("Nope", "x")]);
    let agent = ConversationalAgentBuilder::new().build(llm).unwrap();
    let err = AgentExecutor::from_agent(agent)
        .invoke(prompt_args! {"input" => "q"})
        .await
        .unwrap_err();
    assert!(
        matches!(&err, ChainError::AgentError(m) if m.contains("Tool Nope not found")),
        "{err:?}"
    );
}

#[tokio::test]
async fn max_iterations_stops_the_loop() {
    let tool = RecordingTool::ok("Echo", "again");
    let llm = FakeLLM::new([
        action("Echo", "1"),
        action("Echo", "2"),
        action("Echo", "3"),
    ]);
    let agent = ConversationalAgentBuilder::new()
        .tools(&[Arc::new(tool.clone())])
        .build(llm)
        .unwrap();
    let out = AgentExecutor::from_agent(agent)
        .with_max_iterations(2)
        .invoke(prompt_args! {"input" => "q"})
        .await
        .unwrap();
    assert_eq!(out, "Max iterations reached");
    assert_eq!(tool.inputs().len(), 2);
}

#[tokio::test]
async fn tool_errors_are_observations_unless_break_if_error() {
    let make = || {
        let tool = RecordingTool::failing("Broken", "boom");
        let llm = FakeLLM::new([action("Broken", "x"), final_answer("recovered")]);
        let agent = ConversationalAgentBuilder::new()
            .tools(&[Arc::new(tool)])
            .build(llm.clone())
            .unwrap();
        (agent, llm)
    };

    let (agent, llm) = make();
    let out = AgentExecutor::from_agent(agent)
        .invoke(prompt_args! {"input" => "q"})
        .await
        .unwrap();
    assert_eq!(out, "recovered");
    let second: String = llm.calls()[1].iter().map(|m| m.content.clone()).collect();
    assert!(second.contains("The tool return the following error: boom"));

    let (agent, _) = make();
    let err = AgentExecutor::from_agent(agent)
        .with_break_if_error(true)
        .invoke(prompt_args! {"input" => "q"})
        .await
        .unwrap_err();
    assert!(
        matches!(&err, ChainError::AgentError(m) if m.contains("boom")),
        "{err:?}"
    );
}

#[tokio::test]
async fn memory_history_is_sent_as_chat_history() {
    let llm = FakeLLM::new([final_answer("second answer")]);
    let agent = ConversationalAgentBuilder::new()
        .build(llm.clone())
        .unwrap();
    let mut mem = SimpleMemory::new();
    mem.add_user_message(&"earlier question").await.unwrap();
    mem.add_ai_message(&"earlier answer").await.unwrap();
    let memory = Arc::new(Mutex::new(mem));

    AgentExecutor::from_agent(agent)
        .with_memory(memory.clone())
        .invoke(prompt_args! {"input" => "new question"})
        .await
        .unwrap();

    let prompt = &llm.calls()[0];
    assert!(prompt
        .iter()
        .any(|m| m.content == "earlier question" && m.message_type == MessageType::HumanMessage));
    assert!(prompt
        .iter()
        .any(|m| m.content == "earlier answer" && m.message_type == MessageType::AIMessage));
    assert_eq!(memory.lock().await.messages().await.unwrap().len(), 4);
}

fn tool_calls_json(calls: &[(&str, &str, &str)]) -> String {
    json!(calls
        .iter()
        .map(|(id, name, args)| json!({
            "id": id, "type": "function",
            "function": {"name": name, "arguments": args}
        }))
        .collect::<Vec<_>>())
    .to_string()
}

#[tokio::test]
async fn openai_tools_agent_executes_parallel_tool_calls() {
    let weather = RecordingTool::ok("weather", "sunny");
    let time = RecordingTool::ok("time", "noon");
    let llm = FakeLLM::new([
        tool_calls_json(&[
            ("call_1", "weather", r#"{"input":"Lima"}"#),
            ("call_2", "time", "now"),
        ]),
        "Sunny at noon".to_string(),
    ]);
    // Tools collected at runtime in a Vec<Arc<dyn Tool>> work too (#323): &Vec derefs to a slice.
    let tools: Vec<Arc<dyn Tool>> = vec![Arc::new(weather.clone()), Arc::new(time.clone())];
    let agent = OpenAiToolAgentBuilder::new()
        .tools(&tools)
        .build(llm.clone())
        .unwrap();
    let memory = Arc::new(Mutex::new(SimpleMemory::new()));
    let out = AgentExecutor::from_agent(agent)
        .with_memory(memory.clone())
        .invoke(prompt_args! {"input" => "weather and time?"})
        .await
        .unwrap();

    assert_eq!(out, "Sunny at noon");
    // default Tool::parse_input unwraps {"input": ...}
    assert_eq!(weather.inputs(), vec![json!("Lima")]);
    assert_eq!(time.inputs(), vec![json!("now")]);

    // Tools were advertised as functions to the LLM
    let fn_names: Vec<String> = llm
        .options
        .lock()
        .unwrap()
        .iter()
        .flat_map(|o| o.functions.clone().unwrap_or_default())
        .map(|f| f.name)
        .collect();
    assert_eq!(fn_names, vec!["weather", "time"]);

    // Scratchpad on 2nd call: one AI message with tool_calls, then two tool messages
    let second = &llm.calls()[1];
    let ai_with_calls: Vec<_> = second.iter().filter(|m| m.tool_calls.is_some()).collect();
    assert_eq!(ai_with_calls.len(), 1);
    let tool_msgs: Vec<_> = second
        .iter()
        .filter(|m| m.message_type == MessageType::ToolMessage)
        .map(|m| (m.id.clone().unwrap(), m.content.clone()))
        .collect();
    assert_eq!(
        tool_msgs,
        vec![
            ("call_1".to_string(), "sunny".to_string()),
            ("call_2".to_string(), "noon".to_string())
        ]
    );

    // Memory: human, ai(tool_calls), tool, tool, ai(final)
    let types: Vec<_> = memory
        .lock()
        .await
        .messages()
        .await
        .unwrap()
        .into_iter()
        .map(|m| m.message_type)
        .collect();
    assert_eq!(
        types,
        vec![
            MessageType::HumanMessage,
            MessageType::AIMessage,
            MessageType::ToolMessage,
            MessageType::ToolMessage,
            MessageType::AIMessage
        ]
    );
}

#[tokio::test]
async fn openai_tools_agent_plain_text_is_final() {
    let llm = FakeLLM::new(["hello"]);
    let agent = OpenAiToolAgentBuilder::new().build(llm).unwrap();
    let out = AgentExecutor::from_agent(agent)
        .invoke(prompt_args! {"input" => "hi"})
        .await
        .unwrap();
    assert_eq!(out, "hello");
}

#[tokio::test]
async fn agents_send_input_images_and_save_them_to_memory() {
    // Tools agent: the images ride on the human message, also on the step after a tool.
    let tools: Vec<Arc<dyn Tool>> = vec![Arc::new(RecordingTool::ok("Lookup", "an orange cat"))];
    let llm = FakeLLM::new([
        tool_calls_json(&[("call_1", "Lookup", "{}")]),
        "It's a cat".to_string(),
    ]);
    let agent = OpenAiToolAgentBuilder::new()
        .tools(&tools)
        .build(llm.clone())
        .unwrap();
    let memory = Arc::new(Mutex::new(SimpleMemory::new()));
    let out = AgentExecutor::from_agent(agent)
        .with_memory(memory.clone())
        .invoke(prompt_args! {
            "input" => "What is this?",
            "images" => ["https://example.com/cat.png", "iVBORw0KGgo"],
        })
        .await
        .unwrap();
    assert_eq!(out, "It's a cat");

    assert_eq!(llm.calls().len(), 2);
    for call in llm.calls() {
        let human: Vec<_> = call
            .iter()
            .filter(|m| m.message_type == MessageType::HumanMessage)
            .collect();
        assert_eq!(human.len(), 1);
        assert_eq!(human[0].content, "What is this?");
        let urls: Vec<_> = human[0]
            .images
            .iter()
            .flatten()
            .map(|i| i.image_url.as_str())
            .collect();
        assert_eq!(urls, ["https://example.com/cat.png", "iVBORw0KGgo"]);
    }
    let saved = memory.lock().await.messages().await.unwrap();
    assert_eq!(saved[0].message_type, MessageType::HumanMessage);
    assert_eq!(saved[0].content, "What is this?");
    assert_eq!(saved[0].images.as_ref().unwrap().len(), 2);

    // Conversational (ReAct-style) agent.
    let llm = FakeLLM::new([final_answer("A cat")]);
    let agent = ConversationalAgentBuilder::new()
        .build(llm.clone())
        .unwrap();
    AgentExecutor::from_agent(agent)
        .invoke(prompt_args! {"input" => "What is this?", "images" => "https://a/b.png"})
        .await
        .unwrap();
    let human = llm.calls()[0]
        .iter()
        .find(|m| m.message_type == MessageType::HumanMessage)
        .cloned()
        .unwrap();
    assert!(human.content.contains("What is this?"));
    assert_eq!(human.images.unwrap()[0].image_url, "https://a/b.png");
}

#[tokio::test]
async fn openai_tools_agent_uses_typed_tool_calls() {
    use crate::{language_models::GenerateResult, schemas::ToolCall};

    let weather = RecordingTool::ok("weather", "sunny");
    // `generation` is plain text: only the typed `tool_calls` can drive the action.
    let calls = vec![ToolCall::new("toolu_1", "weather", r#"{"input":"Lima"}"#)];
    let llm = FakeLLM::with_results([
        GenerateResult {
            generation: "Let me check".into(),
            tool_calls: calls.clone(),
            ..Default::default()
        },
        GenerateResult {
            generation: "Sunny in Lima".into(),
            ..Default::default()
        },
    ]);
    let tools: Vec<Arc<dyn Tool>> = vec![Arc::new(weather.clone())];
    let agent = OpenAiToolAgentBuilder::new()
        .tools(&tools)
        .build(llm.clone())
        .unwrap();
    let memory = Arc::new(Mutex::new(SimpleMemory::new()));
    let out = AgentExecutor::from_agent(agent)
        .with_memory(memory.clone())
        .invoke(prompt_args! {"input" => "weather?"})
        .await
        .unwrap();

    assert_eq!(out, "Sunny in Lima");
    assert_eq!(weather.inputs(), vec![json!("Lima")]);

    // The scratchpad replays the typed calls in the shared wire format.
    let second = &llm.calls()[1];
    let ai = second.iter().find(|m| m.tool_calls.is_some()).unwrap();
    assert_eq!(ai.parse_tool_calls(), calls);
    let tool_msg = second
        .iter()
        .find(|m| m.message_type == MessageType::ToolMessage)
        .unwrap();
    assert_eq!(
        (tool_msg.id.as_deref(), tool_msg.content.as_str()),
        (Some("toolu_1"), "sunny")
    );

    // ...and so does memory.
    let stored = memory.lock().await.messages().await.unwrap();
    assert_eq!(stored[1].parse_tool_calls(), calls);
}

#[tokio::test]
async fn openai_tools_agent_empty_or_invalid_json_list_is_final() {
    // A JSON array that is not tool calls must not be treated as an action.
    let llm = FakeLLM::new([r#"[1, 2, 3]"#, "[]"]);
    let agent = OpenAiToolAgentBuilder::new().build(llm).unwrap();
    let executor = AgentExecutor::from_agent(agent);
    let out = executor
        .invoke(prompt_args! {"input" => "list?"})
        .await
        .unwrap();
    assert_eq!(out, "[1, 2, 3]");
    let out = executor
        .invoke(prompt_args! {"input" => "empty?"})
        .await
        .unwrap();
    assert_eq!(out, "[]");
}

// ---- streaming + cancellation ----

use crate::{agent::AgentStreamEvent, language_models::GenerateResult, schemas::ToolCall};
use futures::StreamExt;
use tokio_util::sync::CancellationToken;

async fn collect_events(
    executor: &AgentExecutor<impl crate::agent::Agent>,
    input: &str,
) -> Vec<Result<AgentStreamEvent, ChainError>> {
    executor
        .stream_events(prompt_args! {"input" => input})
        .collect()
        .await
}

fn text(s: &str) -> AgentStreamEvent {
    AgentStreamEvent::Text(s.into())
}

/// Drops `Usage` events (FakeLLM reports 1+1 tokens per call) for tests about the rest.
fn without_usage<E>(
    events: impl IntoIterator<Item = Result<AgentStreamEvent, E>>,
) -> Vec<Result<AgentStreamEvent, E>> {
    events
        .into_iter()
        .filter(|e| !matches!(e, Ok(AgentStreamEvent::Usage(_))))
        .collect()
}

#[tokio::test]
async fn stream_events_tools_agent_streams_tools_then_answer_tokens() {
    let weather = RecordingTool::ok("weather", "sunny");
    let llm = FakeLLM::with_results([
        GenerateResult::default().with_tool_calls(vec![ToolCall::new(
            "call_1",
            "weather",
            r#"{"input":"Lima"}"#,
        )]),
        GenerateResult {
            generation: "Sunny in Lima".into(),
            ..Default::default()
        },
    ]);
    let tools: Vec<Arc<dyn Tool>> = vec![Arc::new(weather.clone())];
    let agent = OpenAiToolAgentBuilder::new()
        .tools(&tools)
        .build(llm.clone())
        .unwrap();
    let memory = Arc::new(Mutex::new(SimpleMemory::new()));
    let executor = AgentExecutor::from_agent(agent).with_memory(memory.clone());

    let events: Vec<_> = collect_events(&executor, "weather?")
        .await
        .into_iter()
        .map(Result::unwrap)
        .collect();
    assert_eq!(
        events,
        vec![
            AgentStreamEvent::ToolStart {
                tool: "weather".into(),
                input: r#"{"input":"Lima"}"#.into()
            },
            AgentStreamEvent::ToolEnd {
                tool: "weather".into(),
                output: "sunny".into()
            },
            // The final answer arrives token by token, not as one chunk.
            text("Sunny "),
            text("in "),
            text("Lima"),
            AgentStreamEvent::Finish {
                output: "Sunny in Lima".into()
            },
        ]
    );
    assert_eq!(weather.inputs(), vec![json!("Lima")]);

    // Memory is saved exactly like `call` does: human, ai(tool_calls), tool, ai(final).
    let types: Vec<_> = memory
        .lock()
        .await
        .messages()
        .await
        .unwrap()
        .into_iter()
        .map(|m| m.message_type)
        .collect();
    assert_eq!(
        types,
        vec![
            MessageType::HumanMessage,
            MessageType::AIMessage,
            MessageType::ToolMessage,
            MessageType::AIMessage
        ]
    );
}

#[tokio::test]
async fn stream_events_default_plan_stream_works_for_any_agent() {
    // ConversationalAgent has no plan_stream override: the answer comes as one chunk.
    let calc = RecordingTool::ok("Calculator", "4");
    let llm = FakeLLM::new([action("Calculator", "2+2"), final_answer("It is 4")]);
    let agent = ConversationalAgentBuilder::new()
        .tools(&[Arc::new(calc.clone())])
        .build(llm)
        .unwrap();
    let events: Vec<_> =
        without_usage(collect_events(&AgentExecutor::from_agent(agent), "2+2?").await)
            .into_iter()
            .map(Result::unwrap)
            .collect();
    assert_eq!(
        events,
        vec![
            AgentStreamEvent::ToolStart {
                tool: "Calculator".into(),
                input: "2+2".into()
            },
            AgentStreamEvent::ToolEnd {
                tool: "Calculator".into(),
                output: "4".into()
            },
            text("It is 4"),
            AgentStreamEvent::Finish {
                output: "It is 4".into()
            },
        ]
    );
}

#[tokio::test]
async fn stream_events_tool_error_is_an_observation_event() {
    let broken = RecordingTool::failing("broken", "boom");
    let llm = FakeLLM::new([tool_calls_json(&[("c1", "broken", "x")]), "ok".into()]);
    let agent = OpenAiToolAgentBuilder::new()
        .tools(&[Arc::new(broken)])
        .build(llm)
        .unwrap();
    let events: Vec<_> = collect_events(&AgentExecutor::from_agent(agent), "go")
        .await
        .into_iter()
        .map(Result::unwrap)
        .collect();
    assert!(
        events.contains(&AgentStreamEvent::ToolEnd {
            tool: "broken".into(),
            output: "The tool return the following error: boom".into()
        }),
        "{events:?}"
    );
    assert_eq!(
        events.last(),
        Some(&AgentStreamEvent::Finish {
            output: "ok".into()
        })
    );
}

#[tokio::test]
async fn stream_events_errors_end_the_stream_with_err() {
    // Unknown tool: ToolStart is never emitted, the stream ends with the error.
    let llm = FakeLLM::new([tool_calls_json(&[("c1", "nope", "x")])]);
    let agent = OpenAiToolAgentBuilder::new().build(llm).unwrap();
    let events = without_usage(collect_events(&AgentExecutor::from_agent(agent), "go").await);
    assert_eq!(events.len(), 1, "{events:?}");
    let err = events[0].as_ref().unwrap_err().to_string();
    assert!(err.contains("Tool nope not found"), "{err}");

    // LLM failure (script exhausted) surfaces as an Err item too, not an empty stream.
    let agent = OpenAiToolAgentBuilder::new()
        .build(FakeLLM::new(Vec::<String>::new()))
        .unwrap();
    let events = collect_events(&AgentExecutor::from_agent(agent), "go").await;
    assert_eq!(events.len(), 1, "{events:?}");
    let err = events[0].as_ref().unwrap_err().to_string();
    assert!(err.contains("no scripted response left"), "{err}");
}

#[tokio::test]
async fn stream_events_max_iterations_finishes() {
    let tool = RecordingTool::ok("t", "again");
    let llm = FakeLLM::new([
        tool_calls_json(&[("c1", "t", "1")]),
        tool_calls_json(&[("c2", "t", "2")]),
    ]);
    let agent = OpenAiToolAgentBuilder::new()
        .tools(&[Arc::new(tool)])
        .build(llm)
        .unwrap();
    let executor = AgentExecutor::from_agent(agent).with_max_iterations(2);
    let events: Vec<_> = collect_events(&executor, "loop")
        .await
        .into_iter()
        .map(Result::unwrap)
        .collect();
    let starts = events
        .iter()
        .filter(|e| matches!(e, AgentStreamEvent::ToolStart { .. }))
        .count();
    assert_eq!(starts, 2);
    assert_eq!(
        events.last(),
        Some(&AgentStreamEvent::Finish {
            output: "Max iterations reached".into()
        })
    );
}

#[tokio::test]
async fn cancellation_stops_a_running_agent_and_skips_memory() {
    // The 2nd LLM call never answers; cancelling must still end the run.
    let weather = RecordingTool::ok("weather", "sunny");
    let llm = FakeLLM::new([tool_calls_json(&[("c1", "weather", "Lima")])]).hang_when_exhausted();
    let agent = OpenAiToolAgentBuilder::new()
        .tools(&[Arc::new(weather.clone())])
        .build(llm.clone())
        .unwrap();
    let token = CancellationToken::new();
    let memory = Arc::new(Mutex::new(SimpleMemory::new()));
    let executor = AgentExecutor::from_agent(agent)
        .with_memory(memory.clone())
        .with_cancellation(token.clone());

    let mut events = executor.stream_events(prompt_args! {"input" => "weather?"});
    let mut seen = Vec::new();
    let drain = async {
        while let Some(event) = events.next().await {
            let done_with_tool = matches!(event, Ok(AgentStreamEvent::ToolEnd { .. }));
            seen.push(event);
            if done_with_tool {
                token.cancel();
            }
        }
    };
    tokio::time::timeout(std::time::Duration::from_secs(5), drain)
        .await
        .expect("cancel must end the stream");
    let err = seen.pop().unwrap().unwrap_err().to_string();
    assert!(err.contains("agent run cancelled"), "{err}");
    assert!(matches!(
        without_usage(seen).as_slice(),
        [
            Ok(AgentStreamEvent::ToolStart { .. }),
            Ok(AgentStreamEvent::ToolEnd { .. })
        ]
    ));
    assert_eq!(
        llm.calls().len(),
        2,
        "second plan was in flight when cancelled"
    );
    assert!(memory.lock().await.messages().await.unwrap().is_empty());
}

#[tokio::test]
async fn cancellation_applies_to_call_too() {
    let llm = FakeLLM::new(Vec::<String>::new()).hang_when_exhausted();
    let agent = OpenAiToolAgentBuilder::new().build(llm).unwrap();
    let token = CancellationToken::new();
    let executor = AgentExecutor::from_agent(agent).with_cancellation(token.clone());

    let canceller = {
        let token = token.clone();
        tokio::spawn(async move {
            tokio::time::sleep(std::time::Duration::from_millis(20)).await;
            token.cancel();
        })
    };
    let err = tokio::time::timeout(
        std::time::Duration::from_secs(5),
        executor.invoke(prompt_args! {"input" => "hi"}),
    )
    .await
    .expect("cancel must end the run")
    .unwrap_err();
    assert!(err.to_string().contains("agent run cancelled"), "{err}");
    canceller.await.unwrap();

    // An already-cancelled token stops the run before the LLM is called.
    let llm = FakeLLM::new(["never"]);
    let agent = OpenAiToolAgentBuilder::new().build(llm.clone()).unwrap();
    let executor = AgentExecutor::from_agent(agent).with_cancellation(token);
    assert!(executor
        .invoke(prompt_args! {"input" => "hi"})
        .await
        .is_err());
    assert!(llm.calls().is_empty());
}

#[tokio::test]
async fn dropping_the_stream_stops_the_run() {
    let tool = RecordingTool::ok("t", "x");
    let llm = FakeLLM::new([
        tool_calls_json(&[("c1", "t", "1")]),
        tool_calls_json(&[("c2", "t", "2")]),
        "done".into(),
    ]);
    let agent = OpenAiToolAgentBuilder::new()
        .tools(&[Arc::new(tool.clone())])
        .build(llm.clone())
        .unwrap();
    let executor = AgentExecutor::from_agent(agent);
    {
        let mut events = executor.stream_events(prompt_args! {"input" => "go"});
        // Take the first tool start and walk away.
        let first = loop {
            match events.next().await.unwrap().unwrap() {
                AgentStreamEvent::Usage(_) => continue,
                other => break other,
            }
        };
        assert!(matches!(first, AgentStreamEvent::ToolStart { .. }));
    }
    // Lazily polled: once dropped, the run made no further LLM calls.
    assert_eq!(llm.calls().len(), 1);
    assert!(tool.inputs().len() <= 1);
}

#[tokio::test]
async fn call_and_stream_events_agree_on_the_answer() {
    let make = || {
        let llm = FakeLLM::new([tool_calls_json(&[("c1", "t", "q")]), "final answer".into()]);
        let agent = OpenAiToolAgentBuilder::new()
            .tools(&[Arc::new(RecordingTool::ok("t", "obs"))])
            .build(llm)
            .unwrap();
        AgentExecutor::from_agent(agent)
    };
    let via_call = make().invoke(prompt_args! {"input" => "q"}).await.unwrap();
    let streamed = collect_events(&make(), "q").await;
    let Some(Ok(AgentStreamEvent::Finish { output })) = streamed.last() else {
        panic!("{streamed:?}");
    };
    assert_eq!(&via_call, output);
    // Concatenated text chunks equal the final output.
    let joined: String = streamed
        .iter()
        .filter_map(|e| match e {
            Ok(AgentStreamEvent::Text(t)) => Some(t.as_str()),
            _ => None,
        })
        .collect();
    assert_eq!(joined, "final answer");
}

#[tokio::test]
async fn stream_events_never_leaks_text_tool_call_json_split_across_chunks() {
    // LLMs without native tool calling write the calls as JSON text; FakeLLM streams it
    // word by word, so the JSON arrives in many chunks and must still be held back.
    let spaced =
        r#"[{"id": "c1", "type": "function", "function": {"name": "t", "arguments": "q"}}]"#;
    let llm = FakeLLM::new([spaced, "the answer"]);
    let agent = OpenAiToolAgentBuilder::new()
        .tools(&[Arc::new(RecordingTool::ok("t", "obs"))])
        .build(llm)
        .unwrap();
    let events: Vec<_> =
        without_usage(collect_events(&AgentExecutor::from_agent(agent), "q").await)
            .into_iter()
            .map(Result::unwrap)
            .collect();
    assert_eq!(
        events,
        vec![
            AgentStreamEvent::ToolStart {
                tool: "t".into(),
                input: "q".into()
            },
            AgentStreamEvent::ToolEnd {
                tool: "t".into(),
                output: "obs".into()
            },
            text("the "),
            text("answer"),
            AgentStreamEvent::Finish {
                output: "the answer".into()
            },
        ]
    );
}

#[tokio::test]
async fn stream_events_answer_starting_with_bracket_is_still_streamed() {
    // Held back as a possible tool call, but it is not one: the text must not be lost.
    let llm = FakeLLM::new(["[1, 2, 3] are the numbers"]);
    let agent = OpenAiToolAgentBuilder::new().build(llm).unwrap();
    let events: Vec<_> = collect_events(&AgentExecutor::from_agent(agent), "q")
        .await
        .into_iter()
        .map(Result::unwrap)
        .collect();
    let joined: String = events
        .iter()
        .filter_map(|e| match e {
            AgentStreamEvent::Text(t) => Some(t.as_str()),
            _ => None,
        })
        .collect();
    assert_eq!(joined, "[1, 2, 3] are the numbers");
    assert_eq!(
        events.last(),
        Some(&AgentStreamEvent::Finish {
            output: "[1, 2, 3] are the numbers".into()
        })
    );
}

// ---- tracing spans ----

use crate::observability::testing::capture;

fn tools_agent_run() -> (RecordingTool, AgentExecutor<impl crate::agent::Agent>) {
    let weather = RecordingTool::ok("weather", "sunny");
    let llm = FakeLLM::with_results([
        GenerateResult::default().with_tool_calls(vec![ToolCall::new(
            "call_1",
            "weather",
            r#"{"input":"Lima"}"#,
        )]),
        GenerateResult {
            generation: "Sunny".into(),
            ..Default::default()
        },
    ]);
    let tools: Vec<Arc<dyn Tool>> = vec![Arc::new(weather.clone())];
    let agent = OpenAiToolAgentBuilder::new()
        .tools(&tools)
        .build(llm)
        .unwrap();
    (weather, AgentExecutor::from_agent(agent))
}

#[tokio::test]
async fn agent_run_emits_run_plan_and_tool_spans() {
    let (spans, _guard) = capture();
    let (_, executor) = tools_agent_run();
    executor
        .invoke(prompt_args! {"input" => "weather?"})
        .await
        .unwrap();

    let run = spans.named("agent.run");
    assert_eq!(run.len(), 1);
    assert_eq!(run[0].field("max_iterations"), Some("10"));
    assert_eq!(run[0].field("steps"), Some("1"));
    assert_eq!(run[0].field("error"), None);

    let plans = spans.named("agent.plan");
    let steps: Vec<_> = plans.iter().map(|p| p.field("step").unwrap()).collect();
    assert_eq!(steps, vec!["0", "1"], "one plan span per iteration");
    assert!(plans.iter().all(|p| p.parent == Some("agent.run")));

    let tool = &spans.named("tool.call")[0];
    assert_eq!(tool.field("tool"), Some("weather"));
    assert_eq!(tool.parent, Some("agent.run"));
    assert_eq!(tool.field("error"), None);

    // Each LLM call belongs to the step that made it (through the agent's LLMChain).
    let llm = spans.named("llm.generate");
    assert_eq!(llm.len(), 2);
    assert!(
        llm.iter().all(|s| s.parent == Some("chain.call")),
        "{llm:?}"
    );
    let chains = spans.named("chain.call");
    assert!(
        chains.iter().all(|s| s.parent == Some("agent.plan")),
        "{chains:?}"
    );
}

#[tokio::test]
async fn agent_spans_are_the_same_when_streaming() {
    let (spans, _guard) = capture();
    let (_, executor) = tools_agent_run();
    let events = collect_events(&executor, "weather?").await;
    assert!(events.iter().all(Result::is_ok));

    assert_eq!(spans.named("agent.run")[0].field("steps"), Some("1"));
    assert_eq!(spans.named("agent.plan").len(), 2);
    assert_eq!(spans.named("tool.call")[0].parent, Some("agent.run"));
    let llm = spans.named("llm.generate");
    assert_eq!(llm.len(), 2);
    assert!(llm.iter().all(|s| s.field("stream") == Some("true")));
    assert!(
        llm.iter().all(|s| s.parent == Some("agent.plan")),
        "{llm:?}"
    );
}

#[tokio::test]
async fn failing_tool_records_error_on_tool_span() {
    let (spans, _guard) = capture();
    let llm = FakeLLM::new([action("Broken", "x"), final_answer("recovered")]);
    let agent = ConversationalAgentBuilder::new()
        .tools(&[Arc::new(RecordingTool::failing("Broken", "boom"))])
        .build(llm)
        .unwrap();
    let out = AgentExecutor::from_agent(agent)
        .invoke(prompt_args! {"input" => "q"})
        .await
        .unwrap();
    assert_eq!(out, "recovered");

    let tool = &spans.named("tool.call")[0];
    assert_eq!(tool.field("tool"), Some("Broken"));
    assert_eq!(tool.field("error"), Some("boom"));
    // The tool error became an observation, so the run itself succeeded.
    assert_eq!(spans.named("agent.run")[0].field("error"), None);
}

#[tokio::test]
async fn failed_run_records_error_on_run_span() {
    let (spans, _guard) = capture();
    let llm = FakeLLM::new([action("Nope", "x")]);
    let agent = ConversationalAgentBuilder::new().build(llm).unwrap();
    AgentExecutor::from_agent(agent)
        .invoke(prompt_args! {"input" => "q"})
        .await
        .unwrap_err();
    let run = &spans.named("agent.run")[0];
    assert!(run.field("error").unwrap().contains("Tool Nope not found"));
}

#[tokio::test]
async fn max_iterations_records_steps() {
    let (spans, _guard) = capture();
    let llm = FakeLLM::new([action("Echo", "1"), action("Echo", "2")]);
    let agent = ConversationalAgentBuilder::new()
        .tools(&[Arc::new(RecordingTool::ok("Echo", "again"))])
        .build(llm)
        .unwrap();
    AgentExecutor::from_agent(agent)
        .with_max_iterations(2)
        .invoke(prompt_args! {"input" => "q"})
        .await
        .unwrap();
    assert_eq!(spans.named("agent.run")[0].field("steps"), Some("2"));
    assert_eq!(spans.named("tool.call").len(), 2);
}

// ---- token usage ----

use crate::language_models::TokenUsage;

fn with_usage(result: GenerateResult, prompt: u32, completion: u32) -> GenerateResult {
    GenerateResult {
        tokens: Some(TokenUsage::new(prompt, completion)),
        ..result
    }
}

/// Tools agent over a 2-call run: a tool call (10+2 tokens), then the answer (30+5).
fn usage_tools_run() -> AgentExecutor<impl crate::agent::Agent> {
    let llm = FakeLLM::with_results([
        with_usage(
            GenerateResult::default().with_tool_calls(vec![ToolCall::new("c1", "t", "q")]),
            10,
            2,
        ),
        with_usage(
            GenerateResult {
                generation: "done".into(),
                ..Default::default()
            },
            30,
            5,
        ),
    ]);
    let agent = OpenAiToolAgentBuilder::new()
        .tools(&[Arc::new(RecordingTool::ok("t", "obs"))])
        .build(llm)
        .unwrap();
    AgentExecutor::from_agent(agent)
}

#[tokio::test]
async fn call_sums_token_usage_over_every_plan() {
    let result = usage_tools_run()
        .call(prompt_args! {"input" => "q"})
        .await
        .unwrap();
    assert_eq!(result.generation, "done");
    assert_eq!(result.tokens, Some(TokenUsage::new(40, 7)));
}

#[tokio::test]
async fn run_span_records_total_tokens() {
    let (spans, _guard) = capture();
    usage_tools_run()
        .invoke(prompt_args! {"input" => "q"})
        .await
        .unwrap();
    assert_eq!(
        spans.named("agent.run")[0].field("total_tokens"),
        Some("47")
    );
}

#[tokio::test]
async fn streaming_run_span_records_total_tokens_too() {
    let (spans, _guard) = capture();
    let events = collect_events(&usage_tools_run(), "q").await;
    assert!(events.iter().all(Result::is_ok), "{events:?}");
    assert_eq!(
        spans.named("agent.run")[0].field("total_tokens"),
        Some("47")
    );
}

#[tokio::test]
async fn conversational_agent_reports_token_usage() {
    let llm = FakeLLM::with_results([
        with_usage(
            GenerateResult {
                generation: action("Calculator", "2+2"),
                ..Default::default()
            },
            7,
            1,
        ),
        with_usage(
            GenerateResult {
                generation: final_answer("4"),
                ..Default::default()
            },
            9,
            3,
        ),
    ]);
    let agent = ConversationalAgentBuilder::new()
        .tools(&[Arc::new(RecordingTool::ok("Calculator", "4"))])
        .build(llm)
        .unwrap();
    let executor = AgentExecutor::from_agent(agent);
    let result = executor
        .call(prompt_args! {"input" => "2+2"})
        .await
        .unwrap();
    assert_eq!(result.tokens, Some(TokenUsage::new(16, 4)));
}

#[tokio::test]
async fn stream_events_reports_usage_per_plan_before_its_actions() {
    let events: Vec<_> = collect_events(&usage_tools_run(), "q")
        .await
        .into_iter()
        .map(Result::unwrap)
        .collect();
    assert_eq!(
        events,
        vec![
            AgentStreamEvent::Usage(TokenUsage::new(10, 2)),
            AgentStreamEvent::ToolStart {
                tool: "t".into(),
                input: "q".into()
            },
            AgentStreamEvent::ToolEnd {
                tool: "t".into(),
                output: "obs".into()
            },
            text("done"),
            AgentStreamEvent::Usage(TokenUsage::new(30, 5)),
            AgentStreamEvent::Finish {
                output: "done".into()
            },
        ]
    );
}

#[tokio::test]
async fn default_plan_stream_forwards_usage() {
    // ConversationalAgent uses the trait's default plan_stream.
    let llm = FakeLLM::with_results([with_usage(
        GenerateResult {
            generation: final_answer("hi"),
            ..Default::default()
        },
        4,
        2,
    )]);
    let agent = ConversationalAgentBuilder::new().build(llm).unwrap();
    let events: Vec<_> = collect_events(&AgentExecutor::from_agent(agent), "q")
        .await
        .into_iter()
        .map(Result::unwrap)
        .collect();
    assert_eq!(
        events,
        vec![
            text("hi"),
            AgentStreamEvent::Usage(TokenUsage::new(4, 2)),
            AgentStreamEvent::Finish {
                output: "hi".into()
            },
        ]
    );
}

#[tokio::test]
async fn missing_usage_stays_none() {
    // An LLM that reports no usage: no Usage events and `tokens` is None, not zero.
    let make = || {
        let llm = FakeLLM::with_results([GenerateResult {
            generation: "plain".into(),
            ..Default::default()
        }]);
        AgentExecutor::from_agent(OpenAiToolAgentBuilder::new().build(llm).unwrap())
    };
    let result = make().call(prompt_args! {"input" => "q"}).await.unwrap();
    assert_eq!(result.tokens, None);
    let events = collect_events(&make(), "q").await;
    assert!(
        !events
            .iter()
            .any(|e| matches!(e, Ok(AgentStreamEvent::Usage(_)))),
        "{events:?}"
    );
}

#[tokio::test]
async fn max_iterations_still_reports_usage() {
    let llm = FakeLLM::with_results([
        with_usage(
            GenerateResult::default().with_tool_calls(vec![ToolCall::new("c1", "t", "1")]),
            5,
            1,
        ),
        with_usage(
            GenerateResult::default().with_tool_calls(vec![ToolCall::new("c2", "t", "2")]),
            6,
            1,
        ),
    ]);
    let agent = OpenAiToolAgentBuilder::new()
        .tools(&[Arc::new(RecordingTool::ok("t", "again"))])
        .build(llm)
        .unwrap();
    let result = AgentExecutor::from_agent(agent)
        .with_max_iterations(2)
        .call(prompt_args! {"input" => "q"})
        .await
        .unwrap();
    assert_eq!(result.generation, "Max iterations reached");
    assert_eq!(result.tokens, Some(TokenUsage::new(11, 2)));
}

/// Live: `OLLAMA_MODEL=qwen3:14b cargo test --lib agent_usage_live -- --ignored`
#[tokio::test]
#[ignore]
async fn agent_usage_live_ollama() {
    use crate::{
        language_models::options::CallOptions,
        llm::{ollama::openai::OllamaConfig, openai::OpenAI},
    };
    let model = std::env::var("OLLAMA_MODEL").unwrap_or_else(|_| "qwen3:14b".into());
    let make = || {
        let llm = OpenAI::new(OllamaConfig::default())
            .with_model(&model)
            .with_options(CallOptions::new().with_stream_usage(true));
        let agent = OpenAiToolAgentBuilder::new()
            .tools(&[Arc::new(RecordingTool::ok("weather", "sunny, 24C"))])
            .build(llm)
            .unwrap();
        AgentExecutor::from_agent(agent)
    };
    let input = "Use the weather tool to check Lima, then answer in one sentence.";

    let result = make().call(prompt_args! {"input" => input}).await.unwrap();
    let total = result.tokens.expect("usage from a non-streaming run");
    println!("call: {:?} -> {}", total, result.generation);
    assert!(total.prompt_tokens > 0 && total.completion_tokens > 0);

    let events = collect_events(&make(), input).await;
    let usages: Vec<_> = events
        .iter()
        .filter_map(|e| match e {
            Ok(AgentStreamEvent::Usage(u)) => Some(u.clone()),
            _ => None,
        })
        .collect();
    println!("stream usages: {usages:?}");
    assert!(!usages.is_empty(), "{events:?}");
}