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
/// Streaming implementation for agent loop.
///
/// pi-mono pattern: the provider accumulates content into a single `output`
/// message. Each event carries a snapshot (`partial`) of this message.
/// Done carries the complete accumulated message.
///
/// TTSR integration: when a [`TtsrEngine`](super::ttsr::TtsrEngine) is
/// provided, every [`ProviderEvent::TextDelta`] is checked against
/// registered rules. A match aborts the stream and returns
/// [`StreamOutcome::RuleInterrupt`].
use futures::StreamExt;
use oxi_ai::{
ContentBlock, Context, Message, ProviderEvent, StopReason, StreamOptions, Tool as OxTool,
};
use std::collections::{HashMap, HashSet};
use super::helpers::sanitize_orphaned_tool_results;
use super::stream_outcome::StreamOutcome;
use super::ttsr::{MatchSource, TtsrEngine, TtsrMatchContext};
pub(crate) async fn stream_assistant_response(
loop_ref: &super::AgentLoop,
messages: &mut Vec<Message>,
emit: &super::EmitFn,
ttsr: Option<&TtsrEngine>,
) -> StreamOutcome {
let model = match loop_ref.resolve_model() {
Ok(m) => m,
Err(_) => {
return StreamOutcome::Error {
message: oxi_ai::AssistantMessage::new(
oxi_ai::Api::OpenAiCompletions,
"agent",
&loop_ref.config.model_id,
),
detail: "Failed to resolve model".to_string(),
};
}
};
// Proactively sanitize orphaned tool results to prevent provider
// errors like "Messages with role 'tool' must be a response to a
// preceding message with 'tool_calls'".
let removed = sanitize_orphaned_tool_results(messages);
if removed > 0 {
tracing::warn!(
session_id = ?loop_ref.session_id,
removed,
"Sanitized orphaned tool results before streaming"
);
}
let mut context = Context::new();
if let Some(ref system_prompt) = loop_ref.config.system_prompt {
context.set_system_prompt(system_prompt.clone());
}
for msg in messages.iter() {
context.add_message(msg.clone());
}
let tool_defs = loop_ref.tools.definitions();
if !tool_defs.is_empty() {
let mut oxi_tools = Vec::with_capacity(tool_defs.len());
for def in &tool_defs {
let schema = serde_json::to_value(&def.input_schema)
.unwrap_or_else(|_| serde_json::json!({"type": "object", "properties": {}}));
oxi_tools.push(OxTool::new(&def.name, &def.description, schema));
}
context.set_tools(oxi_tools);
}
let stream_options = StreamOptions {
temperature: Some(loop_ref.config.temperature as f64),
max_tokens: Some(loop_ref.config.max_tokens as usize),
provider_options: loop_ref.config.provider_options.clone(),
..Default::default()
};
let stream = match super::retry::stream_with_retry(
loop_ref,
&model,
&context,
Some(stream_options),
emit,
)
.await
{
Ok(s) => s,
Err(e) => {
return StreamOutcome::Error {
message: oxi_ai::AssistantMessage::new(
oxi_ai::Api::OpenAiCompletions,
"agent",
&loop_ref.config.model_id,
),
detail: e.to_string(),
};
}
};
let mut added_partial = false;
let mut event_count = 0u32;
// content_index → resolved tool-call id, populated from `ToolCallStart`
// and reconciled at `ToolCallEnd`. Lets `ToolCallDelta` forward the
// correct id even though providers (Anthropic, OpenAI) keep the id in a
// private pending map and do not embed a `ContentBlock::ToolCall` in the
// streaming partial until the call finalizes.
let mut tool_call_ids: HashMap<usize, String> = HashMap::new();
// Reset the thinking-loop detector so each stream attempt is guarded
// independently. Without this, the detector's `fired` flag stays
// sticky after the first hit and retries run unguarded (issue
// flagged in advisory review). The retry layer may resample several
// times; each attempt deserves a fresh detector.
if let Some(detector) = loop_ref.thinking_loop_detector.lock().as_mut() {
detector.reset();
}
let mut rx = stream;
let stream_idle_timeout = std::time::Duration::from_secs(30);
let cancel_check_interval = std::time::Duration::from_millis(500);
let mut last_event_at = std::time::Instant::now();
loop {
let next_event = tokio::select! {
event = rx.next() => event,
_ = tokio::time::sleep(cancel_check_interval) => {
if loop_ref.is_cancelled() {
tracing::info!(
"Stream cancelled (detected in periodic check)"
);
if added_partial {
let last_idx = messages.len() - 1;
if let Message::Assistant(ref mut m) = messages[last_idx] {
m.stop_reason = StopReason::Aborted;
}
let last_msg = messages.last().expect("non-empty").clone();
emit(super::AgentEvent::MessageEnd {
message: last_msg.clone(),
});
if let Message::Assistant(m) = &last_msg {
return StreamOutcome::Cancelled(m.clone());
}
}
return StreamOutcome::Cancelled(oxi_ai::AssistantMessage::new(
oxi_ai::Api::OpenAiCompletions,
"agent",
&loop_ref.config.model_id,
));
}
if last_event_at.elapsed() >= stream_idle_timeout {
tracing::warn!(
"Stream idle timeout ({:?}) reached after {} events",
stream_idle_timeout, event_count
);
let mut err_asst = oxi_ai::AssistantMessage::new(
oxi_ai::Api::OpenAiCompletions,
"agent",
&loop_ref.config.model_id,
);
err_asst.stop_reason = StopReason::Error;
err_asst.error_message = Some(format!(
"Stream timed out after {:?} of inactivity",
stream_idle_timeout
));
if added_partial {
let last_idx = messages.len() - 1;
if let Message::Assistant(ref mut m) = messages[last_idx] {
m.stop_reason = StopReason::Error;
}
}
emit(super::AgentEvent::MessageEnd {
message: Message::Assistant(err_asst.clone()),
});
emit(super::AgentEvent::Error {
message: format!(
"Stream timed out after {:?} of inactivity",
stream_idle_timeout
),
session_id: loop_ref.session_id.clone(),
});
return StreamOutcome::Error { message: err_asst, detail: format!("Stream timed out after {:?} of inactivity", stream_idle_timeout) };
}
continue;
}
};
let event = match next_event {
Some(e) => e,
None => break,
};
last_event_at = std::time::Instant::now();
if loop_ref.is_cancelled() {
tracing::info!("Stream cancelled after {} events", event_count);
if added_partial {
let last_idx = messages.len() - 1;
if let Message::Assistant(ref mut m) = messages[last_idx] {
m.stop_reason = StopReason::Aborted;
}
let last_msg = messages.last().expect("non-empty").clone();
emit(super::AgentEvent::MessageEnd {
message: last_msg.clone(),
});
if let Message::Assistant(m) = &last_msg {
return StreamOutcome::Cancelled(m.clone());
}
}
return StreamOutcome::Cancelled(oxi_ai::AssistantMessage::new(
oxi_ai::Api::OpenAiCompletions,
"agent",
&loop_ref.config.model_id,
));
}
event_count += 1;
match event {
ProviderEvent::Start { partial } => {
tracing::info!("Stream event #{}: Start", event_count);
messages.push(Message::Assistant((*partial).clone()));
added_partial = true;
emit(super::AgentEvent::MessageStart {
message: messages.last().expect("non-empty after push").clone(),
});
}
ProviderEvent::FallbackStart {
from_model,
to_model,
..
} => {
tracing::info!(
"Stream event #{}: Fallback from {} to {}",
event_count,
from_model,
to_model
);
emit(super::AgentEvent::Fallback {
from_model,
to_model,
});
}
ProviderEvent::FallbackExhausted {
models_tried,
final_error,
} => {
tracing::warn!(
"Stream event #{}: All fallback models exhausted. Tried: {:?}, error: {}",
event_count,
models_tried,
final_error
);
if let Some(last_model) = models_tried.last() {
emit(super::AgentEvent::Fallback {
from_model: last_model.clone(),
to_model: "none".to_string(),
});
}
}
ProviderEvent::TextDelta { delta, partial, .. } => {
if added_partial {
let last_idx = messages.len() - 1;
if let Message::Assistant(ref mut m) = messages[last_idx] {
*m = (*partial).clone();
}
}
let last_msg = messages.last().expect("non-empty").clone();
let delta_clone = delta.clone();
emit(super::AgentEvent::MessageUpdate {
message: last_msg,
delta: Some(delta),
});
// ── TTSR check ──
if let Some(engine) = ttsr {
let ctx = TtsrMatchContext {
source: MatchSource::Text,
file_paths: vec![],
tool_name: None,
};
let violations = engine.check_delta(&delta_clone, &ctx);
if !violations.is_empty() {
let mut partial_msg = messages
.last()
.and_then(|m| match m {
Message::Assistant(a) => Some(a.clone()),
_ => None,
})
.unwrap_or_else(|| {
oxi_ai::AssistantMessage::new(
oxi_ai::Api::OpenAiCompletions,
"agent",
&loop_ref.config.model_id,
)
});
partial_msg.stop_reason = StopReason::Aborted;
return StreamOutcome::RuleInterrupt {
partial: partial_msg,
rule: violations.into_iter().next().expect("non-empty"),
};
}
}
}
ProviderEvent::ThinkingStart { partial, .. } if added_partial => {
let last_idx = messages.len() - 1;
if let Message::Assistant(ref mut m) = messages[last_idx] {
*m = (*partial).clone();
}
emit(super::AgentEvent::Thinking);
}
ProviderEvent::ThinkingDelta { delta, partial, .. } => {
// Feed the thinking-loop detector if enabled. On detection
// we surface an error event so the retry layer resamples;
// matches omp's "transient stream stall" classification.
if let Some(detector) = loop_ref.thinking_loop_detector.lock().as_mut()
&& let Some(reason) = detector.push(&delta)
{
tracing::warn!(
session_id = ?loop_ref.session_id,
reason = %reason,
"thinking-loop detected; aborting stream"
);
// Reuse the circuit-breaker failure path: emit
// a transient error and stop. The retry layer
// will resample.
loop_ref.circuit_breaker.record_failure();
emit(super::AgentEvent::Error {
message: reason,
session_id: loop_ref.session_id.clone(),
});
// Break out of the stream loop — the upstream
// retry policy treats transient errors as
// resample candidates.
break;
}
if added_partial {
let last_idx = messages.len() - 1;
if let Message::Assistant(ref mut m) = messages[last_idx] {
*m = (*partial).clone();
}
}
let last_msg = messages.last().expect("non-empty").clone();
emit(super::AgentEvent::ThinkingDelta {
text: delta.clone(),
});
emit(super::AgentEvent::MessageUpdate {
message: last_msg,
delta: Some(delta),
});
}
ProviderEvent::ThinkingEnd { partial, .. } if added_partial => {
let last_idx = messages.len() - 1;
if let Message::Assistant(ref mut m) = messages[last_idx] {
*m = (*partial).clone();
}
emit(super::AgentEvent::ThinkingEnd);
}
ProviderEvent::ToolCallStart {
content_index,
tool_call_id,
partial,
..
} if added_partial => {
let last_idx = messages.len() - 1;
if let Message::Assistant(ref mut m) = messages[last_idx] {
*m = (*partial).clone();
}
// Register the provider id so later ToolCallDelta events can
// forward it. OpenAI re-emits ToolCallStart on id-bearing
// deltas, so this also fills the map when the first start
// lacked an id.
if let Some(id) = tool_call_id
&& !id.is_empty()
{
tool_call_ids.insert(content_index, id);
}
}
ProviderEvent::ToolCallDelta {
content_index,
delta,
partial,
..
} if added_partial => {
let last_idx = messages.len() - 1;
if let Message::Assistant(ref mut m) = messages[last_idx] {
*m = (*partial).clone();
}
// Forward the streamed argument fragment to downstream
// consumers (live tool-arg construction UIs, Oxios kernel).
// Resolve the id from the ToolCallStart registration; fall
// back to the finalized block in the accumulated partial for
// any provider that embeds it there. If neither resolves we
// skip this delta rather than emit an unverified id.
let resolved_id = tool_call_ids
.get(&content_index)
.cloned()
.or_else(|| extract_tool_call_id(messages, content_index));
if let Some(id) = resolved_id {
emit(super::AgentEvent::ToolCallDelta {
tool_call_id: id,
args_delta: delta,
});
}
}
ProviderEvent::ToolCallEnd {
content_index,
tool_call,
..
} if added_partial => {
// Reconcile the id map with the finalized call so any
// trailing deltas (and the map itself) stay authoritative.
tool_call_ids.insert(content_index, tool_call.id.clone());
let last_idx = messages.len() - 1;
if let Message::Assistant(ref mut m) = messages[last_idx] {
m.content.push(ContentBlock::ToolCall(tool_call));
}
let last_msg = messages.last().expect("non-empty").clone();
emit(super::AgentEvent::MessageUpdate {
message: last_msg,
delta: None,
});
}
ProviderEvent::Done { message, .. } => {
loop_ref.circuit_breaker.record_success();
let (input, output) = (message.usage.input, message.usage.output);
if input > 0 || output > 0 {
// Snapshot the heuristic estimate of what was *just
// sent* so we can compare it to the provider's
// reported input_tokens on the same snapshot. This
// is the drift metric referenced by issue #28:
// `bytes/4` can undercount by 3-4× on token-dense
// content (base64, JSON, CJK), and the legacy
// compaction path used that heuristic directly.
//
// The slice we estimate over is the *prompt* the
// provider tokenized, NOT the prompt + the
// assistant turn we just streamed. At
// `ProviderEvent::Done`, `messages` ends with the
// just-completed assistant message (pushed on
// `Start` at the start of the stream, or — in the
// no-partial-Start path — on `Done` after this
// branch; we record before that push). We slice
// off the trailing assistant message so the
// heuristic matches what `usage.input` actually
// covers; otherwise the drift metric would
// *understate* #28's bytes/4 underestimate.
//
// The compaction decision itself is unaffected —
// it reads `Real(last_input_tokens)` =
// `usage.input`, which is correct.
let prompt_len = messages.len().saturating_sub(1);
let estimate_at_report = estimate_tokens_from_messages(&messages[..prompt_len]);
loop_ref.state.update(|s| {
s.record_usage(input, output);
s.record_provider_turn(input, estimate_at_report);
});
emit(super::AgentEvent::Usage {
input_tokens: input,
output_tokens: output,
});
}
tracing::info!(
"Stream event #{}: Done (stop_reason={:?})",
event_count,
message.stop_reason
);
if added_partial {
let last_idx = messages.len() - 1;
if let Message::Assistant(ref mut m) = messages[last_idx] {
let mut seen_ids: HashSet<String> = message
.content
.iter()
.filter_map(|b| match b {
ContentBlock::ToolCall(tc) => Some(tc.id.clone()),
_ => None,
})
.collect();
let extra_tool_calls: Vec<ContentBlock> = m
.content
.iter()
.filter(|b| match b {
ContentBlock::ToolCall(tc) => seen_ids.insert(tc.id.clone()),
_ => false,
})
.cloned()
.collect();
let tc_count = extra_tool_calls.len();
*m = message.clone();
m.content.extend(extra_tool_calls);
tracing::info!(
"Done: merged {} extra tool_calls, final has {} content blocks, stop_reason={:?}",
tc_count,
m.content.len(),
m.stop_reason
);
}
} else {
messages.push(Message::Assistant(message.clone()));
}
let last_msg = messages.last().expect("non-empty").clone();
emit(super::AgentEvent::MessageEnd {
message: last_msg.clone(),
});
if let Message::Assistant(m) = &last_msg {
return StreamOutcome::Complete(m.clone());
} else {
return StreamOutcome::Complete(message);
}
}
ProviderEvent::Error { mut error, .. } => {
loop_ref.circuit_breaker.record_failure();
tracing::info!("Stream event #{}: Error", event_count);
let raw_msg = error.text_content();
let friendly = if raw_msg.is_empty() {
"Unknown provider error".to_string()
} else {
raw_msg
};
tracing::error!(
session_id = ?loop_ref.session_id,
"Provider stream error: {}", friendly
);
error.stop_reason = StopReason::Error;
if added_partial {
let last_idx = messages.len() - 1;
if let Message::Assistant(ref mut m) = messages[last_idx] {
*m = error.clone();
}
} else {
messages.push(Message::Assistant(error.clone()));
}
emit(super::AgentEvent::MessageEnd {
message: Message::Assistant(error.clone()),
});
emit(super::AgentEvent::Error {
message: format!("âš {}", friendly),
session_id: loop_ref.session_id.clone(),
});
return StreamOutcome::Error {
message: error,
detail: format!("âš {}", friendly),
};
}
_ => {}
}
}
tracing::info!("Stream ended after {} events", event_count);
let final_message = match messages.last().and_then(|m| match m {
Message::Assistant(a) => Some(a.clone()),
_ => None,
}) {
Some(m) => m,
None => {
return StreamOutcome::Error {
message: oxi_ai::AssistantMessage::new(
oxi_ai::Api::OpenAiCompletions,
"agent",
&loop_ref.config.model_id,
),
detail: "No final assistant message in stream".to_string(),
};
}
};
if !added_partial {
tracing::warn!("Stream ended without Start event, emitting synthetic MessageStart");
emit(super::AgentEvent::MessageStart {
message: Message::Assistant(final_message.clone()),
});
}
emit(super::AgentEvent::MessageEnd {
message: Message::Assistant(final_message.clone()),
});
StreamOutcome::Complete(final_message)
}
/// Heuristic token estimate for a messages slice, mirroring
/// `AgentState::estimate_tokens` (serialized JSON length / 4).
///
/// Used in [`stream_assistant_response`] at the moment the provider
/// reports `usage.input_tokens` to record the divergence between
/// the legacy heuristic and the ground-truth provider count (see
/// issue #28 gap 2). The result is cached on
/// `AgentState::last_estimate_at_report` / `last_estimate_divergence`
/// so the operator can see how badly `bytes/4` is undercounting on
/// token-dense content.
///
/// Kept local (not a method on `AgentState`) so we can call it with
/// a borrowed slice of the loop's working `messages` buffer without
/// cloning the whole history.
fn estimate_tokens_from_messages(messages: &[Message]) -> usize {
let json = serde_json::to_string(messages).unwrap_or_default();
json.len() / 4
}
/// Best-effort extraction of a tool-call id from the accumulated assistant
/// message's content block at `content_index`.
///
/// This is a fallback for the `tool_call_ids` map used in
/// [`stream_assistant_response`]: most providers (Anthropic, OpenAI) surface
/// the id at `ToolCallStart` and keep it in a private pending map, so the
/// `ToolCall` block is *not* present in the streaming partial during deltas.
/// The lookup only resolves for providers that embed the block early, but it
/// costs nothing and is forward-compatible.
fn extract_tool_call_id(messages: &[Message], content_index: usize) -> Option<String> {
let last = messages.last()?;
let Message::Assistant(m) = last else {
return None;
};
m.content.get(content_index).and_then(|b| match b {
ContentBlock::ToolCall(tc) => Some(tc.id.clone()),
_ => None,
})
}
#[cfg(test)]
mod streaming_lifecycle_tests {
//! Verifies `stream_assistant_response` forwards provider streaming
//! lifecycle events as `AgentEvent`s:
//! - `ProviderEvent::ThinkingEnd` → `AgentEvent::ThinkingEnd`
//! - `ProviderEvent::ToolCallDelta` → `AgentEvent::ToolCallDelta { tool_call_id, args_delta }`
//!
//! The `tool_call_id` is resolved from a content_index→id map populated
//! at `ToolCallStart`/`ToolCallEnd`, because providers keep the id in a
//! private pending map and never embed a `ContentBlock::ToolCall` in the
//! streaming partial until the call finalizes.
use super::stream_assistant_response;
use crate::ProviderResolver;
use crate::config::ToolExecutionMode;
use crate::events::AgentEvent;
use crate::state::SharedState;
use crate::tools::ToolRegistry;
use crate::{AgentLoop, AgentLoopConfig};
use futures::Stream;
use oxi_ai::{
Api, AssistantMessage, CompactionStrategy, ContentBlock, Context, Message, Model, Provider,
ProviderEvent, StopReason, StreamOptions, StreamResult, ToolCall, UserMessage,
};
use std::collections::VecDeque;
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context as TaskContext, Poll};
/// Provider that replays a fixed script of `ProviderEvent`s.
struct ScriptedProvider {
events: Arc<Vec<ProviderEvent>>,
}
impl ScriptedProvider {
fn new(events: Vec<ProviderEvent>) -> Self {
Self {
events: Arc::new(events),
}
}
}
impl Provider for ScriptedProvider {
fn stream<'a>(
&'a self,
_model: &'a Model,
_context: &'a Context,
_options: Option<StreamOptions>,
) -> Pin<Box<dyn Future<Output = StreamResult> + Send + 'a>> {
let events = Arc::clone(&self.events);
Box::pin(async move {
Ok(Box::pin(ScriptedStream {
events: VecDeque::from((*events).clone()),
})
as Pin<Box<dyn Stream<Item = ProviderEvent> + Send>>)
})
}
fn name(&self) -> &str {
"scripted"
}
}
struct ScriptedStream {
events: VecDeque<ProviderEvent>,
}
impl Stream for ScriptedStream {
type Item = ProviderEvent;
fn poll_next(
mut self: Pin<&mut Self>,
_cx: &mut TaskContext<'_>,
) -> Poll<Option<Self::Item>> {
Poll::Ready(self.events.pop_front())
}
}
struct DummyResolver;
impl ProviderResolver for DummyResolver {
fn resolve_provider(&self, _name: &str) -> Option<Arc<dyn Provider>> {
None
}
fn resolve_model(&self, _model_id: &str) -> Option<Model> {
Some(Model::new(
"test/model",
"Test",
Api::AnthropicMessages,
"mock",
"https://mock.test",
))
}
}
fn empty_partial() -> Arc<AssistantMessage> {
Arc::new(AssistantMessage::new(
Api::AnthropicMessages,
"mock",
"test/model",
))
}
fn make_loop(provider: Arc<dyn Provider>) -> AgentLoop {
let config = AgentLoopConfig {
model_id: "test/model".to_string(),
system_prompt: None,
temperature: 1.0,
max_tokens: 4096,
tool_execution: ToolExecutionMode::Sequential,
compaction_strategy: CompactionStrategy::Disabled,
context_window: 128_000,
compact_on_start: false,
auto_retry_enabled: false,
auto_retry_max_attempts: 1,
thinking_loop_detection: false,
..Default::default()
};
AgentLoop::new_with_resolver(
provider,
config,
Arc::new(ToolRegistry::new()),
SharedState::new(),
Arc::new(DummyResolver),
)
}
/// Runs a scripted provider through `stream_assistant_response` and
/// returns every emitted `AgentEvent` in order.
async fn run_script(events: Vec<ProviderEvent>) -> Vec<AgentEvent> {
let provider: Arc<dyn Provider> = Arc::new(ScriptedProvider::new(events));
let agent_loop = make_loop(provider);
let collected: Arc<Mutex<Vec<AgentEvent>>> = Arc::new(Mutex::new(Vec::new()));
let sink = Arc::clone(&collected);
let emit: Arc<dyn Fn(AgentEvent) + Send + Sync> =
Arc::new(move |e| sink.lock().unwrap().push(e));
let mut messages: Vec<Message> = vec![Message::User(UserMessage::new("hi".to_string()))];
let _ = stream_assistant_response(&agent_loop, &mut messages, &emit, None).await;
collected.lock().unwrap().clone()
}
/// Anthropic-style: the tool-call id is known up front at `ToolCallStart`.
#[tokio::test]
async fn thinking_end_and_tool_call_delta_forwarded() {
let finalized = ToolCall::new("tc_abc", "bash", serde_json::json!({"command":"ls"}));
let mut done_msg = AssistantMessage::new(Api::AnthropicMessages, "mock", "test/model");
done_msg
.content
.push(ContentBlock::ToolCall(finalized.clone()));
let events = vec![
ProviderEvent::Start {
partial: empty_partial(),
},
ProviderEvent::ThinkingStart {
content_index: 0,
partial: empty_partial(),
},
ProviderEvent::ThinkingDelta {
content_index: 0,
delta: "reasoning...".to_string(),
partial: empty_partial(),
},
ProviderEvent::ThinkingEnd {
content_index: 0,
content: "reasoning...".to_string(),
partial: empty_partial(),
},
ProviderEvent::ToolCallStart {
content_index: 1,
tool_call_id: Some("tc_abc".to_string()),
tool_name: Some("bash".to_string()),
partial: empty_partial(),
},
ProviderEvent::ToolCallDelta {
content_index: 1,
delta: "{\"command\":".to_string(),
partial: empty_partial(),
},
ProviderEvent::ToolCallDelta {
content_index: 1,
delta: "\"ls\"}".to_string(),
partial: empty_partial(),
},
ProviderEvent::ToolCallEnd {
content_index: 1,
tool_call: finalized,
partial: empty_partial(),
},
ProviderEvent::Done {
reason: StopReason::Stop,
message: done_msg,
},
];
let emitted = run_script(events).await;
let thinking_end_at = emitted
.iter()
.position(|e| matches!(e, AgentEvent::ThinkingEnd));
assert!(
thinking_end_at.is_some(),
"AgentEvent::ThinkingEnd must be emitted"
);
let deltas: Vec<(&str, &str)> = emitted
.iter()
.filter_map(|e| match e {
AgentEvent::ToolCallDelta {
tool_call_id,
args_delta,
} => Some((tool_call_id.as_str(), args_delta.as_str())),
_ => None,
})
.collect();
assert_eq!(deltas.len(), 2, "expected exactly two ToolCallDelta events");
assert_eq!(deltas[0], ("tc_abc", "{\"command\":"));
assert_eq!(deltas[1], ("tc_abc", "\"ls\"}"));
let first_delta_at = emitted
.iter()
.position(|e| matches!(e, AgentEvent::ToolCallDelta { .. }))
.expect("at least one ToolCallDelta");
assert!(
thinking_end_at.unwrap() < first_delta_at,
"ThinkingEnd must precede ToolCallDelta"
);
}
/// OpenAI-style: the first `ToolCallStart` carries no id (name only),
/// then a second id-bearing delta re-emits `ToolCallStart`. The map must
/// pick up the id so later `ToolCallDelta`s resolve.
#[tokio::test]
async fn tool_call_delta_resolves_late_id() {
let finalized = ToolCall::new("tc_late", "grep", serde_json::json!({"pattern":"foo"}));
let mut done_msg = AssistantMessage::new(Api::AnthropicMessages, "mock", "test/model");
done_msg
.content
.push(ContentBlock::ToolCall(finalized.clone()));
let events = vec![
ProviderEvent::Start {
partial: empty_partial(),
},
ProviderEvent::ToolCallStart {
content_index: 0,
tool_call_id: None,
tool_name: Some("grep".to_string()),
partial: empty_partial(),
},
ProviderEvent::ToolCallStart {
content_index: 0,
tool_call_id: Some("tc_late".to_string()),
tool_name: Some("grep".to_string()),
partial: empty_partial(),
},
ProviderEvent::ToolCallDelta {
content_index: 0,
delta: "{\"pattern\":".to_string(),
partial: empty_partial(),
},
ProviderEvent::ToolCallEnd {
content_index: 0,
tool_call: finalized,
partial: empty_partial(),
},
ProviderEvent::Done {
reason: StopReason::Stop,
message: done_msg,
},
];
let emitted = run_script(events).await;
let ids: Vec<String> = emitted
.iter()
.filter_map(|e| match e {
AgentEvent::ToolCallDelta { tool_call_id, .. } => Some(tool_call_id.clone()),
_ => None,
})
.collect();
assert_eq!(
ids,
vec!["tc_late".to_string()],
"ToolCallDelta must resolve the id from the second ToolCallStart"
);
}
}