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
//! Step-by-step iteration types for the agentic loop.
//!
//! [`StepIterator`] lets you drive the loop one turn at a time, inspect
//! intermediate state, inject messages, and modify the tool registry
//! between turns.
use futures::StreamExt;
use neuron_tool::ToolRegistry;
use neuron_types::{
CompletionRequest, ContentBlock, ContentItem, ContextStrategy, HookAction, LoopError, Message,
Provider, Role, StopReason, StreamError, StreamEvent, TokenUsage, ToolContext, ToolOutput,
};
use crate::loop_impl::{
AgentLoop, AgentResult, DEFAULT_ACTIVITY_TIMEOUT, accumulate_usage, check_request_limit,
check_token_limits, check_tool_call_limit, extract_text, fire_compaction_hooks,
fire_loop_iteration_hooks, fire_post_llm_hooks, fire_post_tool_hooks, fire_pre_llm_hooks,
fire_pre_tool_hooks,
};
/// The result of a single turn in the agentic loop.
#[derive(Debug)]
pub enum TurnResult {
/// Tool calls were executed and results appended.
ToolsExecuted {
/// The tool calls made by the model.
calls: Vec<(String, String, serde_json::Value)>,
/// The tool outputs.
results: Vec<ToolOutput>,
},
/// The model returned a final text response.
FinalResponse(AgentResult),
/// Context compaction occurred.
CompactionOccurred {
/// Token count before compaction.
old_tokens: usize,
/// Token count after compaction.
new_tokens: usize,
},
/// The turn limit was reached.
MaxTurnsReached,
/// An error occurred.
Error(LoopError),
}
/// Step-by-step iterator over the agentic loop.
///
/// Allows driving the loop one turn at a time with full control
/// between turns: inspect messages, inject new messages, modify
/// tools.
///
/// Created via [`AgentLoop::run_step`].
pub struct StepIterator<'a, P: Provider, C: ContextStrategy> {
loop_ref: &'a mut AgentLoop<P, C>,
tool_ctx: &'a ToolContext,
total_usage: TokenUsage,
turns: usize,
request_count: usize,
tool_call_count: usize,
finished: bool,
}
impl<'a, P: Provider, C: ContextStrategy> StepIterator<'a, P, C> {
/// Advance the loop by one turn.
///
/// Returns `None` if the loop has already completed (final response
/// was returned or an error occurred).
pub async fn next(&mut self) -> Option<TurnResult> {
if self.finished {
return None;
}
// Check cancellation
if self.tool_ctx.cancellation_token.is_cancelled() {
self.finished = true;
return Some(TurnResult::Error(LoopError::Cancelled));
}
// Check max turns
if let Some(max) = self.loop_ref.config.max_turns
&& self.turns >= max
{
self.finished = true;
return Some(TurnResult::MaxTurnsReached);
}
// Check request limit (pre-request)
if let Some(ref limits) = self.loop_ref.config.usage_limits
&& let Err(e) = check_request_limit(limits, self.request_count)
{
self.finished = true;
return Some(TurnResult::Error(e));
}
// Fire LoopIteration hooks
match fire_loop_iteration_hooks(&self.loop_ref.hooks, self.turns).await {
Ok(Some(HookAction::Terminate { reason })) => {
self.finished = true;
return Some(TurnResult::Error(LoopError::HookTerminated(reason)));
}
Err(e) => {
self.finished = true;
return Some(TurnResult::Error(e));
}
_ => {}
}
// Check context compaction
let token_count = self
.loop_ref
.context
.token_estimate(&self.loop_ref.messages);
if self
.loop_ref
.context
.should_compact(&self.loop_ref.messages, token_count)
{
let old_tokens = token_count;
match self
.loop_ref
.context
.compact(self.loop_ref.messages.clone())
.await
{
Ok(compacted) => {
self.loop_ref.messages = compacted;
let new_tokens = self
.loop_ref
.context
.token_estimate(&self.loop_ref.messages);
// Fire compaction hooks
match fire_compaction_hooks(&self.loop_ref.hooks, old_tokens, new_tokens).await
{
Ok(Some(HookAction::Terminate { reason })) => {
self.finished = true;
return Some(TurnResult::Error(LoopError::HookTerminated(reason)));
}
Err(e) => {
self.finished = true;
return Some(TurnResult::Error(e));
}
_ => {}
}
return Some(TurnResult::CompactionOccurred {
old_tokens,
new_tokens,
});
}
Err(e) => {
self.finished = true;
return Some(TurnResult::Error(e.into()));
}
}
}
// Build completion request
let request = CompletionRequest {
model: String::new(),
messages: self.loop_ref.messages.clone(),
system: Some(self.loop_ref.config.system_prompt.clone()),
tools: self.loop_ref.tools.definitions(),
..Default::default()
};
// Fire PreLlmCall hooks
match fire_pre_llm_hooks(&self.loop_ref.hooks, &request).await {
Ok(Some(HookAction::Terminate { reason })) => {
self.finished = true;
return Some(TurnResult::Error(LoopError::HookTerminated(reason)));
}
Err(e) => {
self.finished = true;
return Some(TurnResult::Error(e));
}
_ => {}
}
// Call provider (via durability if set)
let response = if let Some(ref durable) = self.loop_ref.durability {
let options = neuron_types::ActivityOptions {
start_to_close_timeout: DEFAULT_ACTIVITY_TIMEOUT,
heartbeat_timeout: None,
retry_policy: None,
};
match durable.0.erased_execute_llm_call(request, options).await {
Ok(r) => r,
Err(e) => {
self.finished = true;
return Some(TurnResult::Error(
neuron_types::ProviderError::Other(Box::new(e)).into(),
));
}
}
} else {
match self.loop_ref.provider.complete(request).await {
Ok(r) => r,
Err(e) => {
self.finished = true;
return Some(TurnResult::Error(e.into()));
}
}
};
// Fire PostLlmCall hooks
match fire_post_llm_hooks(&self.loop_ref.hooks, &response).await {
Ok(Some(HookAction::Terminate { reason })) => {
self.finished = true;
return Some(TurnResult::Error(LoopError::HookTerminated(reason)));
}
Err(e) => {
self.finished = true;
return Some(TurnResult::Error(e));
}
_ => {}
}
// Accumulate usage
accumulate_usage(&mut self.total_usage, &response.usage);
self.request_count += 1;
self.turns += 1;
// Check token limits (post-response)
if let Some(ref limits) = self.loop_ref.config.usage_limits
&& let Err(e) = check_token_limits(limits, &self.total_usage)
{
self.finished = true;
return Some(TurnResult::Error(e));
}
// Check for tool calls
let tool_calls: Vec<_> = response
.message
.content
.iter()
.filter_map(|block| {
if let ContentBlock::ToolUse { id, name, input } = block {
Some((id.clone(), name.clone(), input.clone()))
} else {
None
}
})
.collect();
// Append assistant message
self.loop_ref.messages.push(response.message.clone());
// Server-side compaction: the provider paused to compact context.
// Report as a compaction event so the caller can continue stepping.
if response.stop_reason == StopReason::Compaction {
return Some(TurnResult::CompactionOccurred {
old_tokens: 0,
new_tokens: 0,
});
}
if tool_calls.is_empty() || response.stop_reason == StopReason::EndTurn {
self.finished = true;
let response_text = extract_text(&response.message);
return Some(TurnResult::FinalResponse(AgentResult {
response: response_text,
messages: self.loop_ref.messages.clone(),
usage: self.total_usage.clone(),
turns: self.turns,
}));
}
// Check cancellation before tool execution
if self.tool_ctx.cancellation_token.is_cancelled() {
self.finished = true;
return Some(TurnResult::Error(LoopError::Cancelled));
}
// Check tool call limit (pre-tool-call)
if let Some(ref limits) = self.loop_ref.config.usage_limits
&& let Err(e) = check_tool_call_limit(limits, self.tool_call_count, tool_calls.len())
{
self.finished = true;
return Some(TurnResult::Error(e));
}
self.tool_call_count += tool_calls.len();
// Execute tool calls (parallel or sequential)
let mut tool_result_blocks = Vec::new();
let mut tool_outputs = Vec::new();
if self.loop_ref.config.parallel_tool_execution && tool_calls.len() > 1 {
let futs = tool_calls.iter().map(|(call_id, tool_name, input)| {
self.loop_ref
.execute_single_tool(call_id, tool_name, input, self.tool_ctx)
});
let results = futures::future::join_all(futs).await;
for result in results {
match result {
Ok(block) => {
// Extract ToolOutput from the ContentBlock for TurnResult
if let ContentBlock::ToolResult {
content, is_error, ..
} = &block
{
tool_outputs.push(ToolOutput {
content: content.clone(),
structured_content: None,
is_error: *is_error,
});
}
tool_result_blocks.push(block);
}
Err(e) => {
self.finished = true;
return Some(TurnResult::Error(e));
}
}
}
} else {
for (call_id, tool_name, input) in &tool_calls {
match self
.loop_ref
.execute_single_tool(call_id, tool_name, input, self.tool_ctx)
.await
{
Ok(block) => {
if let ContentBlock::ToolResult {
content, is_error, ..
} = &block
{
tool_outputs.push(ToolOutput {
content: content.clone(),
structured_content: None,
is_error: *is_error,
});
}
tool_result_blocks.push(block);
}
Err(e) => {
self.finished = true;
return Some(TurnResult::Error(e));
}
}
}
}
// Append tool results
self.loop_ref.messages.push(Message {
role: Role::User,
content: tool_result_blocks,
});
Some(TurnResult::ToolsExecuted {
calls: tool_calls,
results: tool_outputs,
})
}
/// Returns a reference to the current messages.
#[must_use]
pub fn messages(&self) -> &[Message] {
&self.loop_ref.messages
}
/// Inject a message into the conversation between turns.
pub fn inject_message(&mut self, message: Message) {
self.loop_ref.messages.push(message);
}
/// Returns a mutable reference to the tool registry.
#[must_use]
pub fn tools_mut(&mut self) -> &mut ToolRegistry {
&mut self.loop_ref.tools
}
}
impl<P: Provider, C: ContextStrategy> AgentLoop<P, C> {
/// Create a step-by-step iterator over the loop.
///
/// Unlike [`run`](AgentLoop::run) which drives to completion, this
/// lets you advance one turn at a time, inspect state, inject messages,
/// and modify tools between turns.
///
/// The user message is appended immediately. Call
/// [`StepIterator::next`] to advance.
#[must_use]
pub fn run_step<'a>(
&'a mut self,
user_message: Message,
tool_ctx: &'a ToolContext,
) -> StepIterator<'a, P, C> {
self.messages.push(user_message);
StepIterator {
loop_ref: self,
tool_ctx,
total_usage: TokenUsage::default(),
turns: 0,
request_count: 0,
tool_call_count: 0,
finished: false,
}
}
/// Run the loop with streaming, forwarding [`StreamEvent`]s through a channel.
///
/// Uses `provider.complete_stream()` instead of `provider.complete()` for
/// each LLM turn. When durability is set, falls back to `DurableContext::execute_llm_call`
/// (full response) and synthesizes stream events from the result.
///
/// Tool execution is handled identically to [`run`](AgentLoop::run).
/// Fires the same hook events as `run()`: `LoopIteration`, `PreLlmCall`,
/// `PostLlmCall`, `PreToolExecution`, `PostToolExecution`, and
/// `ContextCompaction`.
///
/// Returns a receiver that yields `StreamEvent`s. The final
/// `StreamEvent::MessageComplete` on the last turn signals the loop
/// has finished.
///
/// # Errors
///
/// Errors are sent as `StreamEvent::Error` on the channel.
pub async fn run_stream(
&mut self,
user_message: Message,
tool_ctx: &ToolContext,
) -> tokio::sync::mpsc::Receiver<StreamEvent> {
let (tx, rx) = tokio::sync::mpsc::channel(64);
self.messages.push(user_message);
let mut turns: usize = 0;
let mut request_count: usize = 0;
let mut tool_call_count: usize = 0;
let mut total_usage = TokenUsage::default();
loop {
// Check cancellation
if tool_ctx.cancellation_token.is_cancelled() {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable("cancelled")))
.await;
break;
}
// Check max turns
if let Some(max) = self.config.max_turns
&& turns >= max
{
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"max turns reached ({max})"
))))
.await;
break;
}
// Check request limit (pre-request)
if let Some(ref limits) = self.config.usage_limits
&& let Err(e) = check_request_limit(limits, request_count)
{
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"{e}"
))))
.await;
break;
}
// Fire LoopIteration hooks
match fire_loop_iteration_hooks(&self.hooks, turns).await {
Ok(Some(HookAction::Terminate { reason })) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"hook terminated: {reason}"
))))
.await;
break;
}
Err(e) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"hook error: {e}"
))))
.await;
break;
}
_ => {}
}
// Check context compaction
let token_count = self.context.token_estimate(&self.messages);
if self.context.should_compact(&self.messages, token_count) {
let old_tokens = token_count;
match self.context.compact(self.messages.clone()).await {
Ok(compacted) => {
self.messages = compacted;
let new_tokens = self.context.token_estimate(&self.messages);
// Fire ContextCompaction hooks
match fire_compaction_hooks(&self.hooks, old_tokens, new_tokens).await {
Ok(Some(HookAction::Terminate { reason })) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"hook terminated: {reason}"
))))
.await;
break;
}
Err(e) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"hook error: {e}"
))))
.await;
break;
}
_ => {}
}
}
Err(e) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"compaction error: {e}"
))))
.await;
break;
}
}
}
// Build completion request
let request = CompletionRequest {
model: String::new(),
messages: self.messages.clone(),
system: Some(self.config.system_prompt.clone()),
tools: self.tools.definitions(),
..Default::default()
};
// Fire PreLlmCall hooks
match fire_pre_llm_hooks(&self.hooks, &request).await {
Ok(Some(HookAction::Terminate { reason })) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"hook terminated: {reason}"
))))
.await;
break;
}
Err(e) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"hook error: {e}"
))))
.await;
break;
}
_ => {}
}
// Call provider: durable path uses complete() with synthesized events,
// non-durable path uses complete_stream() for real streaming.
// Returns (Message, TokenUsage) so usage limits can be checked afterwards.
let (message, turn_usage) = if let Some(ref durable) = self.durability {
// Durable path: use execute_llm_call for journaling/replay
let options = neuron_types::ActivityOptions {
start_to_close_timeout: DEFAULT_ACTIVITY_TIMEOUT,
heartbeat_timeout: None,
retry_policy: None,
};
let response = match durable.0.erased_execute_llm_call(request, options).await {
Ok(r) => r,
Err(e) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"durable error: {e}"
))))
.await;
break;
}
};
// Synthesize stream events from the durable response
for block in &response.message.content {
if let ContentBlock::Text(text) = block
&& tx.send(StreamEvent::TextDelta(text.clone())).await.is_err()
{
return rx;
}
}
if tx
.send(StreamEvent::Usage(response.usage.clone()))
.await
.is_err()
{
return rx;
}
if tx
.send(StreamEvent::MessageComplete(response.message.clone()))
.await
.is_err()
{
return rx;
}
// Fire PostLlmCall hooks
match fire_post_llm_hooks(&self.hooks, &response).await {
Ok(Some(HookAction::Terminate { reason })) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"hook terminated: {reason}"
))))
.await;
break;
}
Err(e) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"hook error: {e}"
))))
.await;
break;
}
_ => {}
}
(response.message, response.usage)
} else {
// Non-durable path: use streaming provider call
let stream_handle = match self.provider.complete_stream(request).await {
Ok(h) => h,
Err(e) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"provider error: {e}"
))))
.await;
break;
}
};
// Forward all stream events to the channel, collect the assembled message
let mut assembled_message: Option<Message> = None;
let mut assembled_response: Option<neuron_types::CompletionResponse> = None;
let mut stream_usage = TokenUsage::default();
let mut stream = stream_handle.receiver;
while let Some(event) = stream.next().await {
match &event {
StreamEvent::MessageComplete(msg) => {
assembled_message = Some(msg.clone());
}
StreamEvent::Usage(u) => {
stream_usage = u.clone();
// Build a partial CompletionResponse for PostLlmCall
assembled_response = Some(neuron_types::CompletionResponse {
id: String::new(),
model: String::new(),
message: assembled_message.clone().unwrap_or(Message {
role: Role::Assistant,
content: vec![],
}),
usage: u.clone(),
stop_reason: StopReason::EndTurn,
});
}
_ => {}
}
// Forward event to caller
if tx.send(event).await.is_err() {
// Receiver dropped, stop
return rx;
}
}
// Process the assembled message
let msg = match assembled_message {
Some(m) => m,
None => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(
"stream ended without MessageComplete",
)))
.await;
break;
}
};
// Fire PostLlmCall hooks with the assembled response
if let Some(mut resp) = assembled_response {
resp.message = msg.clone();
match fire_post_llm_hooks(&self.hooks, &resp).await {
Ok(Some(HookAction::Terminate { reason })) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"hook terminated: {reason}"
))))
.await;
break;
}
Err(e) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"hook error: {e}"
))))
.await;
break;
}
_ => {}
}
} else {
// No usage event received; construct a minimal response for the hook
let resp = neuron_types::CompletionResponse {
id: String::new(),
model: String::new(),
message: msg.clone(),
usage: TokenUsage::default(),
stop_reason: StopReason::EndTurn,
};
match fire_post_llm_hooks(&self.hooks, &resp).await {
Ok(Some(HookAction::Terminate { reason })) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"hook terminated: {reason}"
))))
.await;
break;
}
Err(e) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"hook error: {e}"
))))
.await;
break;
}
_ => {}
}
}
(msg, stream_usage)
};
request_count += 1;
turns += 1;
accumulate_usage(&mut total_usage, &turn_usage);
// Check token limits (post-response)
if let Some(ref limits) = self.config.usage_limits
&& let Err(e) = check_token_limits(limits, &total_usage)
{
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"{e}"
))))
.await;
break;
}
// Check for tool calls
let tool_calls: Vec<_> = message
.content
.iter()
.filter_map(|block| {
if let ContentBlock::ToolUse { id, name, input } = block {
Some((id.clone(), name.clone(), input.clone()))
} else {
None
}
})
.collect();
self.messages.push(message.clone());
// Server-side compaction: continue the loop for the next iteration.
// The compacted context is already in the message history.
// (In streaming mode we don't have a StopReason directly, but
// compaction blocks in the content signal this condition.)
if tool_calls.is_empty() {
// Done — final response was already streamed
break;
}
// Check cancellation before tool execution
if tool_ctx.cancellation_token.is_cancelled() {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable("cancelled")))
.await;
break;
}
// Check tool call limit (pre-tool-call)
if let Some(ref limits) = self.config.usage_limits
&& let Err(e) = check_tool_call_limit(limits, tool_call_count, tool_calls.len())
{
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"{e}"
))))
.await;
break;
}
tool_call_count += tool_calls.len();
// Execute tool calls with hooks and durability
let mut tool_result_blocks = Vec::new();
for (call_id, tool_name, input) in &tool_calls {
// Fire PreToolExecution hooks
match fire_pre_tool_hooks(&self.hooks, tool_name, input).await {
Ok(Some(HookAction::Terminate { reason })) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"hook terminated: {reason}"
))))
.await;
return rx;
}
Ok(Some(HookAction::Skip { reason })) => {
tool_result_blocks.push(ContentBlock::ToolResult {
tool_use_id: call_id.clone(),
content: vec![ContentItem::Text(format!(
"Tool call skipped: {reason}"
))],
is_error: true,
});
continue;
}
Err(e) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"hook error: {e}"
))))
.await;
return rx;
}
_ => {}
}
// Execute tool (via durability wrapper if present)
let result = if let Some(ref durable) = self.durability {
let options = neuron_types::ActivityOptions {
start_to_close_timeout: DEFAULT_ACTIVITY_TIMEOUT,
heartbeat_timeout: None,
retry_policy: None,
};
match durable
.0
.erased_execute_tool(tool_name, input.clone(), tool_ctx, options)
.await
{
Ok(r) => r,
Err(e) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"durable tool error: {e}"
))))
.await;
return rx;
}
}
} else {
match self.tools.execute(tool_name, input.clone(), tool_ctx).await {
Ok(r) => r,
Err(neuron_types::ToolError::ModelRetry(hint)) => {
// Convert ModelRetry into an error tool result so the
// model can self-correct on the next iteration.
ToolOutput {
content: vec![ContentItem::Text(hint)],
structured_content: None,
is_error: true,
}
}
Err(e) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"tool error: {e}"
))))
.await;
return rx;
}
}
};
// Fire PostToolExecution hooks
match fire_post_tool_hooks(&self.hooks, tool_name, &result).await {
Ok(Some(HookAction::Terminate { reason })) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"hook terminated: {reason}"
))))
.await;
return rx;
}
Err(e) => {
let _ = tx
.send(StreamEvent::Error(StreamError::non_retryable(format!(
"hook error: {e}"
))))
.await;
return rx;
}
_ => {}
}
tool_result_blocks.push(ContentBlock::ToolResult {
tool_use_id: call_id.clone(),
content: result.content,
is_error: result.is_error,
});
}
self.messages.push(Message {
role: Role::User,
content: tool_result_blocks,
});
}
rx
}
}