1pub mod output;
15pub mod patch;
16pub mod prepare;
17pub mod spec;
18pub use spec::UnhandledInvalidToolCall;
19pub mod transcript;
20
21pub use output::OutputMode;
22pub use patch::RequestPatch;
23pub use prepare::{PrepareError, PreparedRequest, prepare_request};
24pub use spec::RunSpec;
25
26use std::collections::{BTreeMap, BTreeSet};
27
28use serde::{Deserialize, Serialize};
29
30use rig_core::completion::{CompletionResponse, FinishReason, ToolDefinition};
31use rig_core::error::ProviderError;
32
33use rig_core::message::{
34 AssistantContent, AssistantMessage, ToolCall, ToolChoice, ToolName, ToolResult,
35 ToolResultContent, UserContent,
36};
37
38use rig_core::completion::{Message, ResponseIdentity, Usage};
39pub mod policy;
40pub mod response;
41pub mod streamed;
42
43pub use policy::{
44 InvalidToolCallAction, InvalidToolCallContext, InvalidToolCallReason, RetryRequest,
45};
46pub use response::{CompletionCall, MemoryAppend, PromptError, PromptResponse};
47use rig_core::completion::message::turn_failure;
48use rig_core::json_utils;
49use rig_core::structured_output;
50use transcript::{
51 TranscriptError, assistant_message, assistant_text_from_choice, assistant_turn,
52 build_full_history, build_history_for_request, invalid_tool_retry_user_message,
53 is_empty_assistant_turn, tool_result_message, validate_canonical,
54};
55
56pub use streamed::{
57 PartialStreamedTurn, StreamedInvalidToolCall, StreamedResolution, StreamedTurn,
58 StreamedTurnAssembler, StreamedTurnEvent,
59};
60
61fn unknown_tool_call_error(
63 tool_name: String,
64 available_tools: Vec<String>,
65 allowed_tools: Vec<String>,
66 chat_history: Vec<Message>,
67) -> PromptError {
68 PromptError::UnknownToolCall {
69 tool_name,
70 available_tools,
71 allowed_tools,
72 chat_history,
73 }
74}
75
76#[derive(Clone, Copy)]
77struct InvalidToolCallDiagnostic<'a> {
78 tool_call: &'a ToolCall,
79 executable_tool_names: &'a BTreeSet<String>,
80 allowed_tool_names: &'a BTreeSet<String>,
81 history: &'a [Message],
82}
83
84impl InvalidToolCallDiagnostic<'_> {
85 fn unknown(&self, tool_name: String) -> PromptError {
86 unknown_tool_call_error(
87 tool_name,
88 self.executable_tool_names.iter().cloned().collect(),
89 self.allowed_tool_names.iter().cloned().collect(),
90 self.history.to_vec(),
91 )
92 }
93
94 fn unknown_current(&self) -> PromptError {
96 self.unknown(self.tool_call.function.name.to_string())
97 }
98
99 fn cancelled(&self, reason: String) -> PromptError {
100 PromptError::cancelled(self.history.to_vec(), reason)
101 }
102}
103
104enum ValidatedInvalidToolCallAction {
105 Retry { feedback: String },
106 Repair { tool_name: String },
107 Skip { reason: String },
108}
109
110#[derive(Debug, Clone, Serialize, Deserialize)]
112pub enum AgentRunStep {
113 CallModel {
116 prompt: Message,
118 history: Vec<Message>,
121 turn: usize,
123 },
124 CallTools {
127 calls: Vec<PendingToolCall>,
129 },
130 Done(PromptResponse),
132}
133
134#[derive(Debug, Clone, Serialize, Deserialize)]
136pub struct PendingToolCall {
137 pub tool_call: ToolCall,
139 pub preresolved_result: Option<UserContent>,
143}
144
145#[derive(Debug, Clone, Serialize, Deserialize)]
147pub struct ModelTurn {
148 pub head: AssistantMessage,
150 pub response_id: Option<String>,
152 pub provider_request_id: Option<String>,
154 pub choice: Vec<AssistantContent>,
156 pub usage: Usage,
158 pub executable_tool_names: BTreeSet<String>,
160 pub allowed_tool_names: BTreeSet<String>,
162 pub finish_reason: Option<FinishReason>,
164 pub raw: serde_json::Value,
166}
167
168impl ModelTurn {
169 pub fn from_response(resp: &CompletionResponse, prepared: &prepare::PreparedRequest) -> Self {
172 Self::from_response_parts(
173 resp,
174 prepared.executable_tool_names.clone(),
175 prepared.allowed_tool_names.clone(),
176 )
177 }
178
179 pub fn from_response_parts(
184 resp: &CompletionResponse,
185 executable_tool_names: BTreeSet<String>,
186 allowed_tool_names: BTreeSet<String>,
187 ) -> Self {
188 Self::new(
189 resp.head(),
190 resp.choice.clone(),
191 resp.usage,
192 executable_tool_names,
193 allowed_tool_names,
194 resp.raw.clone(),
195 )
196 .with_identity(
197 resp.response_id().map(str::to_owned),
198 resp.provider_request_id.clone(),
199 )
200 .with_finish_reason(resp.finish_reason())
201 }
202
203 pub fn new(
207 head: AssistantMessage,
208 choice: Vec<AssistantContent>,
209 usage: Usage,
210 executable_tool_names: BTreeSet<String>,
211 allowed_tool_names: BTreeSet<String>,
212 raw: serde_json::Value,
213 ) -> Self {
214 Self {
215 head,
216 response_id: None,
217 provider_request_id: None,
218 choice,
219 usage,
220 executable_tool_names,
221 allowed_tool_names,
222 finish_reason: None,
223 raw,
224 }
225 }
226
227 pub fn with_identity(
229 mut self,
230 response_id: Option<String>,
231 provider_request_id: Option<String>,
232 ) -> Self {
233 self.response_id = response_id;
234 self.provider_request_id = provider_request_id;
235 self
236 }
237
238 pub fn with_finish_reason(mut self, finish_reason: Option<FinishReason>) -> Self {
240 self.finish_reason = finish_reason;
241 self
242 }
243}
244
245#[derive(Debug, Clone, Serialize, Deserialize)]
247pub enum ModelTurnOutcome {
248 Continue {
257 response_hook_suppressed: bool,
259 },
260 NeedsResolution(InvalidToolCallContext),
265 TurnRetried,
269}
270
271#[derive(Debug, Clone, Serialize, Deserialize)]
272struct ResolvingState {
273 head: AssistantMessage,
274 original_choice: Vec<AssistantContent>,
277 items: Vec<AssistantContent>,
279 next_index: usize,
281 executable_tool_names: BTreeSet<String>,
282 allowed_tool_names: BTreeSet<String>,
283 skipped: BTreeMap<usize, UserContent>,
286 recovered: bool,
287 any_skipped: bool,
288 has_tool_calls: bool,
289}
290
291fn pending_invalid_call(resolving: &ResolvingState) -> Option<&ToolCall> {
294 match resolving.items.get(resolving.next_index) {
295 Some(AssistantContent::ToolCall(tool_call))
296 if !resolving
297 .allowed_tool_names
298 .contains(tool_call.function.name.as_str()) =>
299 {
300 Some(tool_call)
301 }
302 _ => None,
303 }
304}
305
306fn name_reason(executable_tool_names: &BTreeSet<String>, name: &str) -> InvalidToolCallReason {
309 if executable_tool_names.contains(name) {
310 InvalidToolCallReason::DisallowedByToolChoice
311 } else {
312 InvalidToolCallReason::UnknownTool
313 }
314}
315
316fn has_tool_calls(items: &[AssistantContent]) -> bool {
317 items
318 .iter()
319 .any(|item| matches!(item, AssistantContent::ToolCall(_)))
320}
321
322#[derive(Debug, Clone, Serialize, Deserialize)]
323struct TurnState {
324 head: AssistantMessage,
325 items: Vec<AssistantContent>,
326 has_tool_calls: bool,
327 skipped: BTreeMap<usize, UserContent>,
329 tool_names: ToolNames,
330}
331
332#[derive(Debug, Clone, Serialize, Deserialize)]
335struct ToolNames {
336 executable: BTreeSet<String>,
337 allowed: BTreeSet<String>,
338}
339
340#[derive(Debug, Clone, Serialize, Deserialize)]
341enum RunState {
342 PreparingRequest,
344 AwaitingModel,
346 ResolvingToolCalls(ResolvingState),
349 AwaitingAdvance(TurnState),
352 ExecutingTools(Vec<PendingToolCall>, ToolNames),
356 Done(PromptResponse),
358 Failed,
360}
361
362#[derive(Debug, Clone, Serialize, Deserialize)]
369#[serde(deny_unknown_fields)]
370pub struct AgentRun {
371 #[serde(deserialize_with = "run_format")]
373 format: u32,
374 max_turns: usize,
375 max_invalid_tool_call_retries: usize,
376 max_consecutive_malformed_tool_calls: Option<usize>,
378 unhandled_invalid_tool_call: UnhandledInvalidToolCall,
380 tool_choice: Option<ToolChoice>,
381 output_tool_name: Option<String>,
384 output_schema: Option<serde_json::Value>,
386 max_output_retries: usize,
390 output_retries: usize,
391 chat_history: Option<Vec<Message>>,
392 new_messages: Vec<Message>,
393 current_turn: usize,
394 usage: Usage,
395 completion_calls: Vec<CompletionCall>,
396 completion_call_index: usize,
397 invalid_tool_call_retries: usize,
398 malformed_tool_call_retries: usize,
401 rollback_pending: bool,
404 streamed_completion_call_recorded: bool,
408 previous_model: Option<rig_core::completion::ModelRef>,
415 turn_tools: Option<TurnTools>,
419 entries: Vec<RunEntry>,
421 state: RunState,
422}
423
424pub const RUN_FORMAT: u32 = 2;
426
427fn run_format<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<u32, D::Error> {
431 let format = u32::deserialize(deserializer)?;
432 if format == RUN_FORMAT {
433 Ok(format)
434 } else {
435 Err(serde::de::Error::custom(format!(
436 "resume refused: the run is format {format}, this rig reads format {RUN_FORMAT}"
437 )))
438 }
439}
440
441#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
444pub struct RunEntry {
445 pub kind: String,
449 pub turn: usize,
452 pub value: serde_json::Value,
455}
456
457#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
460pub struct TurnTools {
461 pub turn: usize,
464 pub definitions: Vec<ToolDefinition>,
466}
467
468impl TurnTools {
469 pub fn contains(&self, tool_name: &str) -> bool {
471 self.definitions.iter().any(|d| d.name == tool_name)
472 }
473}
474
475impl AgentRun {
476 pub fn new(prompt: impl Into<Message>) -> Self {
479 Self {
480 format: RUN_FORMAT,
481 max_turns: 1,
482 max_invalid_tool_call_retries: 0,
483 max_consecutive_malformed_tool_calls: None,
484 unhandled_invalid_tool_call: UnhandledInvalidToolCall::Fail,
485 tool_choice: None,
486 output_tool_name: None,
487 output_schema: None,
488 max_output_retries: 0,
489 output_retries: 0,
490 chat_history: None,
491 new_messages: vec![prompt.into()],
492 current_turn: 0,
493 usage: Usage::default(),
494 completion_calls: Vec::new(),
495 completion_call_index: 0,
496 invalid_tool_call_retries: 0,
497 malformed_tool_call_retries: 0,
498 rollback_pending: false,
499 streamed_completion_call_recorded: false,
500 previous_model: None,
501 turn_tools: None,
502 entries: Vec::new(),
503 state: RunState::PreparingRequest,
504 }
505 }
506
507 pub fn initial_prompt(&self) -> Option<&Message> {
512 (self.current_turn == 0 && matches!(self.state, RunState::PreparingRequest))
513 .then(|| self.new_messages.last())
514 .flatten()
515 }
516
517 pub(crate) fn rewrite_initial_prompt(
525 &mut self,
526 prompt: impl Into<Message>,
527 ) -> Result<(), PromptError> {
528 let started = self.current_turn != 0 || !matches!(self.state, RunState::PreparingRequest);
529 match self.new_messages.last_mut() {
530 Some(slot) if !started => {
531 *slot = prompt.into();
532 Ok(())
533 }
534 _ => Err(PromptError::cancelled(
535 self.full_history(),
536 "the initial prompt can only be rewritten before the run starts",
537 )),
538 }
539 }
540
541 pub fn append_entry(&mut self, entry: RunEntry) {
543 self.entries.push(entry);
544 }
545
546 pub fn entries(&self) -> &[RunEntry] {
548 &self.entries
549 }
550
551 pub fn entries_of<'a>(&'a self, kind: &'a str) -> impl Iterator<Item = &'a RunEntry> {
553 self.entries.iter().filter(move |entry| entry.kind == kind)
554 }
555
556 pub fn last_entry_of(&self, kind: &str) -> Option<&RunEntry> {
558 self.entries.iter().rev().find(|entry| entry.kind == kind)
559 }
560
561 pub fn advertise_tools(&mut self, turn: usize, definitions: Vec<ToolDefinition>) {
565 self.turn_tools = Some(TurnTools { turn, definitions });
566 }
567
568 pub fn advertised_tools(&self) -> Option<&TurnTools> {
574 self.turn_tools.as_ref()
575 }
576
577 pub fn with_history(mut self, history: Vec<Message>) -> Self {
579 self.chat_history = Some(history);
580 self
581 }
582
583 pub fn max_turns(mut self, max_turns: usize) -> Self {
588 self.max_turns = max_turns;
589 self
590 }
591
592 pub fn with_output_validation(
598 mut self,
599 output_schema: Option<serde_json::Value>,
600 max_output_retries: usize,
601 ) -> Self {
602 self.output_schema = output_schema;
603 self.max_output_retries = max_output_retries;
604 self
605 }
606
607 pub fn input_chat_history(&self) -> &[Message] {
611 self.chat_history.as_deref().unwrap_or_default()
612 }
613
614 fn can_reprompt_for_output(&self) -> bool {
618 self.output_retries < self.max_output_retries && self.current_turn < self.max_turns
619 }
620
621 fn reprompt_for_output(&mut self) -> Result<AgentRunStep, PromptError> {
626 self.output_retries += 1;
627 self.state = RunState::PreparingRequest;
628 self.next_step()
629 }
630
631 pub fn max_invalid_tool_call_retries(mut self, retries: usize) -> Self {
635 self.max_invalid_tool_call_retries = retries;
636 self
637 }
638
639 pub fn max_consecutive_malformed_tool_calls(mut self, limit: impl Into<Option<usize>>) -> Self {
643 self.max_consecutive_malformed_tool_calls = limit.into();
644 self
645 }
646
647 pub fn with_unhandled_invalid_tool_call(mut self, policy: UnhandledInvalidToolCall) -> Self {
650 self.unhandled_invalid_tool_call = policy;
651 self
652 }
653
654 pub fn with_tool_choice(mut self, tool_choice: ToolChoice) -> Self {
658 self.tool_choice = Some(tool_choice);
659 self
660 }
661
662 pub fn with_output_tool_name(mut self, name: impl Into<String>) -> Self {
666 self.output_tool_name = Some(name.into());
667 self
668 }
669
670 #[must_use = "a refused commit means the run already pinned a name"]
678 pub fn commit_output_tool_name(&mut self, name: impl Into<String>) -> bool {
679 if self.output_tool_name.is_some() {
680 return false;
681 }
682 self.output_tool_name = Some(name.into());
683 true
684 }
685
686 pub fn output_tool_name(&self) -> Option<&str> {
690 self.output_tool_name.as_deref()
691 }
692
693 pub(crate) fn unhandled_invalid_tool_call(&self) -> UnhandledInvalidToolCall {
695 self.unhandled_invalid_tool_call
696 }
697
698 pub fn usage(&self) -> Usage {
700 self.usage
701 }
702
703 pub fn turn(&self) -> usize {
705 self.current_turn
706 }
707
708 pub fn previous_model(&self) -> Option<&rig_core::completion::ModelRef> {
710 self.previous_model.as_ref()
711 }
712
713 pub fn set_previous_model(&mut self, model: rig_core::completion::ModelRef) {
716 self.previous_model = Some(model);
717 }
718
719 pub fn completion_calls(&self) -> &[CompletionCall] {
721 &self.completion_calls
722 }
723
724 pub fn messages(&self) -> &[Message] {
727 &self.new_messages
728 }
729
730 pub fn accepted_turn_choice(&self) -> Option<Vec<AssistantContent>> {
732 let RunState::AwaitingAdvance(turn) = &self.state else {
733 return None;
734 };
735
736 if turn.items.is_empty() {
737 return None;
738 }
739 Some(turn.items.clone())
740 }
741
742 pub fn replace_accepted_turn_choice(
745 &mut self,
746 choice: Vec<AssistantContent>,
747 ) -> Result<(), PromptError> {
748 let replacement_has_tool_calls = choice
749 .iter()
750 .any(|item| matches!(item, AssistantContent::ToolCall(_)));
751 let parked_has_tool_calls = match &self.state {
752 RunState::AwaitingAdvance(turn) => turn.has_tool_calls,
753 _ => {
754 return Err(self.protocol_violation(
755 "replace_accepted_turn_choice called without an accepted turn awaiting advancement",
756 ));
757 }
758 };
759 if parked_has_tool_calls || replacement_has_tool_calls {
760 return Err(PromptError::cancelled(
761 self.full_history(),
762 "a completion outcome replacement does not support tool-bearing model turns; patch or deny the tool dispatches instead",
763 ));
764 }
765 if let RunState::AwaitingAdvance(turn) = &mut self.state {
766 turn.items = choice;
767 }
768 Ok(())
769 }
770
771 pub fn retry_model_turn(&mut self, request: RetryRequest) -> Result<(), PromptError> {
785 let turn = match std::mem::replace(&mut self.state, RunState::Failed) {
786 RunState::AwaitingAdvance(turn) => turn,
787 other => {
788 self.state = other;
789 return Err(self.protocol_violation(
790 "retry_model_turn called without an accepted turn awaiting advancement",
791 ));
792 }
793 };
794
795 if turn.has_tool_calls {
796 return Err(PromptError::cancelled(
797 self.full_history(),
798 "model-turn retry does not support tool-bearing model turns; use tool-call hooks instead",
799 ));
800 }
801
802 match request {
803 RetryRequest::Repeat => {}
804 RetryRequest::Feedback(feedback) => {
805 self.new_messages
808 .extend(assistant_turn(turn.head, turn.items));
809 self.new_messages.push(Message::user(feedback));
810 }
811 }
812
813 self.state = RunState::PreparingRequest;
814 Ok(())
815 }
816
817 pub fn full_history(&self) -> Vec<Message> {
819 build_full_history(self.chat_history.as_deref(), self.new_messages.clone())
820 }
821
822 pub fn is_done(&self) -> bool {
824 matches!(self.state, RunState::Done(_))
825 }
826
827 pub fn response(&self) -> Option<&PromptResponse> {
832 match &self.state {
833 RunState::Done(response) => Some(response),
834 _ => None,
835 }
836 }
837
838 pub fn cancel_error(&self, reason: impl Into<String>) -> PromptError {
841 PromptError::cancelled(self.full_history(), reason)
842 }
843
844 pub fn pending_invalid_tool_call(&self) -> Option<InvalidToolCallContext> {
848 let RunState::ResolvingToolCalls(resolving) = &self.state else {
849 return None;
850 };
851 let tool_call = pending_invalid_call(resolving)?;
852
853 Some(InvalidToolCallContext {
854 tool_name: tool_call.function.name.to_string(),
855 tool_call_id: Some(tool_call.id.clone()),
856 args: Some(tool_call.function.arguments_value().to_string()),
857 available_tools: resolving.executable_tool_names.iter().cloned().collect(),
858 allowed_tools: resolving.allowed_tool_names.iter().cloned().collect(),
859 tool_choice: self.tool_choice.clone(),
860 chat_history: self.diagnostic_history(resolving),
861 is_streaming: false,
862 reason: name_reason(
863 &resolving.executable_tool_names,
864 tool_call.function.name.as_str(),
865 ),
866 })
867 }
868
869 pub fn next_step(&mut self) -> Result<AgentRunStep, PromptError> {
877 match std::mem::replace(&mut self.state, RunState::Failed) {
878 RunState::PreparingRequest => {
879 let Some((prompt_ref, history_for_turn)) = self.new_messages.split_last() else {
880 return Err(PromptError::cancelled(
881 self.full_history(),
882 "prompt loop lost its pending prompt",
883 ));
884 };
885 let prompt = prompt_ref.clone();
886
887 if self.current_turn >= self.max_turns {
888 return Err(PromptError::MaxTurns {
889 max_turns: self.max_turns,
890 chat_history: self.full_history(),
891 prompt,
892 });
893 }
894
895 let history =
896 build_history_for_request(self.chat_history.as_deref(), history_for_turn);
897 self.current_turn += 1;
898 self.rollback_pending = false;
899 self.streamed_completion_call_recorded = false;
900 self.state = RunState::AwaitingModel;
901 Ok(AgentRunStep::CallModel {
902 prompt,
903 history,
904 turn: self.current_turn,
905 })
906 }
907 RunState::AwaitingAdvance(turn_state) => {
908 let TurnState {
909 head,
910 items,
911 has_tool_calls,
912 skipped,
913 tool_names,
914 } = turn_state;
915 let finish = self
920 .completion_calls
921 .last()
922 .and_then(|call| call.finish_reason.as_ref());
923 if let Some(message) = turn_failure(&items, head.stop.as_ref(), finish) {
924 if has_tool_calls {
925 self.new_messages.extend(assistant_turn(head, items));
926 }
927 return Err(ProviderError::Response(message).into());
928 }
929
930 if has_tool_calls
933 && let Some(output_tool_name) = self.output_tool_name.clone()
934 && let Some(tool_call) = items.iter().find_map(|item| match item {
935 AssistantContent::ToolCall(tc) if tc.function.name == output_tool_name => {
936 Some(tc)
937 }
938 _ => None,
939 })
940 {
941 let output_tool_calls = items
942 .iter()
943 .filter(|item| {
944 matches!(
945 item,
946 AssistantContent::ToolCall(tc)
947 if tc.function.name == output_tool_name
948 )
949 })
950 .count();
951 let args = tool_call.function.arguments_value();
952 let tool_call_id = tool_call.id.clone();
953 let output = json_utils::serialize_json_value(&args);
954
955 if let Some(raw) = &tool_call.function.invalid_arguments {
958 if self.can_reprompt_for_output() {
959 self.new_messages
960 .extend(assistant_message(head, items.clone()));
961 let feedback = rig_core::transcript::invalid_arguments_feedback(
962 &output_tool_name,
963 raw,
964 );
965 if let Some(user_message) =
966 invalid_tool_retry_user_message(&items, &tool_call_id, &feedback)
967 {
968 self.new_messages.push(user_message);
969 }
970 return self.reprompt_for_output();
971 }
972 return Err(ProviderError::Response(format!(
973 "the output tool `{output_tool_name}` was called with arguments \
974 that are not a JSON object: {raw}"
975 ))
976 .into());
977 }
978
979 let missing = self
980 .output_schema
981 .as_ref()
982 .map(|schema| structured_output::missing_required_fields(schema, &args))
983 .unwrap_or_default();
984 if !missing.is_empty() && self.can_reprompt_for_output() {
985 self.new_messages
986 .extend(assistant_message(head, items.clone()));
987 let feedback =
988 structured_output::reprompt_missing_fields(&output_tool_name, &missing);
989 if let Some(user_message) =
990 invalid_tool_retry_user_message(&items, &tool_call_id, &feedback)
991 {
992 self.new_messages.push(user_message);
993 }
994 return self.reprompt_for_output();
995 }
996
997 let mut final_items: Vec<AssistantContent> = items
1000 .iter()
1001 .filter(|item| !matches!(item, AssistantContent::ToolCall(_)))
1002 .cloned()
1003 .collect();
1004 final_items.push(AssistantContent::text(output.clone()));
1005 self.new_messages
1006 .extend(assistant_message(head, final_items.clone()));
1007
1008 let content = response::finalize_output_tool_choice(&items, &output)
1009 .unwrap_or_else(|| vec![AssistantContent::text(output)]);
1010 return Ok(self.finish(content, output_tool_calls));
1011 }
1012
1013 self.new_messages
1015 .extend(assistant_turn(head, items.clone()));
1016
1017 if has_tool_calls {
1018 self.output_retries = 0;
1020 let calls: Vec<PendingToolCall> = items
1021 .iter()
1022 .enumerate()
1023 .filter_map(|(index, item)| match item {
1024 AssistantContent::ToolCall(tool_call) => Some(PendingToolCall {
1025 tool_call: tool_call.clone(),
1026 preresolved_result: skipped.get(&index).cloned(),
1027 }),
1028 _ => None,
1029 })
1030 .collect();
1031 self.count_malformed_tool_calls(&calls)?;
1032 self.state = RunState::ExecutingTools(calls.clone(), tool_names);
1033 Ok(AgentRunStep::CallTools { calls })
1034 } else {
1035 if let Some(output_tool_name) = self.output_tool_name.clone()
1038 && !is_empty_assistant_turn(&items)
1039 && self.can_reprompt_for_output()
1040 && !structured_output::text_satisfies_schema(
1041 self.output_schema.as_ref(),
1042 &assistant_text_from_choice(&items),
1043 )
1044 {
1045 self.new_messages.push(Message::user(
1046 structured_output::reprompt_text_answer(&output_tool_name),
1047 ));
1048 return self.reprompt_for_output();
1049 }
1050
1051 Ok(self.finish(items, 0))
1052 }
1053 }
1054 RunState::ExecutingTools(calls, tool_names) => {
1055 let step = AgentRunStep::CallTools {
1058 calls: calls.clone(),
1059 };
1060 self.state = RunState::ExecutingTools(calls, tool_names);
1061 Ok(step)
1062 }
1063 RunState::Done(response) => {
1064 let step = AgentRunStep::Done(response.clone());
1065 self.state = RunState::Done(response);
1066 Ok(step)
1067 }
1068 state @ (RunState::AwaitingModel | RunState::ResolvingToolCalls(_)) => {
1069 let reason = match &state {
1070 RunState::AwaitingModel => {
1071 "next_step called while a model response is pending; feed it via model_response first"
1072 }
1073 _ => {
1074 "next_step called while an invalid tool-call resolution is pending; answer it via resolve_invalid_tool_call first"
1075 }
1076 };
1077 self.state = state;
1078 Err(self.protocol_violation(reason))
1079 }
1080 RunState::Failed => Err(self.protocol_violation(
1081 "next_step called after the run already failed or was misdriven",
1082 )),
1083 }
1084 }
1085
1086 pub fn model_response(&mut self, turn: ModelTurn) -> Result<ModelTurnOutcome, PromptError> {
1092 if !matches!(self.state, RunState::AwaitingModel) {
1093 return Err(
1094 self.protocol_violation("model_response called without a pending CallModel step")
1095 );
1096 }
1097 if self.streamed_completion_call_recorded {
1098 return Err(self.protocol_violation(
1099 "model_response called after record_streamed_completion_call for the same turn; feed streamed turns via streamed_turn",
1100 ));
1101 }
1102
1103 self.record_completion_call(
1104 turn.usage,
1105 ResponseIdentity {
1106 response_id: turn.response_id,
1107 provider_request_id: turn.provider_request_id,
1108 },
1109 turn.finish_reason,
1110 turn.raw,
1111 );
1112
1113 let items: Vec<AssistantContent> = turn.choice.clone();
1114 let has_tool_calls = has_tool_calls(&items);
1115
1116 self.state = RunState::ResolvingToolCalls(ResolvingState {
1117 head: turn.head,
1118 original_choice: turn.choice,
1119 items,
1120 next_index: 0,
1121 executable_tool_names: turn.executable_tool_names,
1122 allowed_tool_names: turn.allowed_tool_names,
1123 skipped: BTreeMap::new(),
1124 recovered: false,
1125 any_skipped: false,
1126 has_tool_calls,
1127 });
1128
1129 self.advance_resolution()
1130 }
1131
1132 fn record_completion_call(
1135 &mut self,
1136 usage: Usage,
1137 identity: ResponseIdentity,
1138 finish_reason: Option<FinishReason>,
1139 raw: serde_json::Value,
1140 ) -> CompletionCall {
1141 let call = CompletionCall::new(self.completion_call_index, usage, raw)
1142 .with_identity(identity)
1143 .with_finish_reason(finish_reason);
1144 self.completion_call_index += 1;
1145 self.completion_calls.push(call.clone());
1146 self.usage += usage;
1147 call
1148 }
1149
1150 fn finish(&mut self, content: Vec<AssistantContent>, output_tool_calls: usize) -> AgentRunStep {
1154 let response = PromptResponse::from_content(content, self.usage)
1155 .with_messages(self.new_messages.clone())
1156 .with_completion_calls(self.completion_calls.clone())
1157 .with_output_tool_calls(output_tool_calls);
1158 self.state = RunState::Done(response.clone());
1159 AgentRunStep::Done(response)
1160 }
1161
1162 fn finalize_turn(
1166 &mut self,
1167 head: AssistantMessage,
1168 items: Vec<AssistantContent>,
1169 has_tool_calls: bool,
1170 skipped: BTreeMap<usize, UserContent>,
1171 tool_names: ToolNames,
1172 ) {
1173 self.state = RunState::AwaitingAdvance(TurnState {
1174 head,
1175 items,
1176 has_tool_calls,
1177 skipped,
1178 tool_names,
1179 });
1180 }
1181
1182 fn count_malformed_tool_calls(&mut self, calls: &[PendingToolCall]) -> Result<(), PromptError> {
1187 let malformed = calls.iter().find_map(|call| {
1188 call.preresolved_result
1189 .is_none()
1190 .then_some(&call.tool_call)
1191 .and_then(|tool_call| {
1192 tool_call
1193 .function
1194 .invalid_arguments
1195 .as_deref()
1196 .map(|raw| (tool_call, raw))
1197 })
1198 });
1199 let Some((tool_call, raw)) = malformed else {
1200 self.malformed_tool_call_retries = 0;
1201 return Ok(());
1202 };
1203 self.malformed_tool_call_retries = self.malformed_tool_call_retries.saturating_add(1);
1204 let Some(limit) = self
1205 .max_consecutive_malformed_tool_calls
1206 .filter(|limit| self.malformed_tool_call_retries > *limit)
1207 else {
1208 return Ok(());
1209 };
1210 let error = policy::arguments_parse_error(raw);
1211 Err(ProviderError::Response(format!(
1212 "tool `{}` was called with arguments that are not a JSON object on {} consecutive \
1213 turns, more than the {} retries allowed: {error}",
1214 tool_call.function.name, self.malformed_tool_call_retries, limit,
1215 ))
1216 .into())
1217 }
1218
1219 pub fn malformed_tool_call_context(
1224 &self,
1225 tool_call: &ToolCall,
1226 is_streaming: bool,
1227 ) -> Option<InvalidToolCallContext> {
1228 let RunState::ExecutingTools(_, tool_names) = &self.state else {
1229 return None;
1230 };
1231 let raw = tool_call.function.invalid_arguments.as_ref()?;
1232 Some(InvalidToolCallContext {
1233 tool_name: tool_call.function.name.to_string(),
1234 tool_call_id: Some(tool_call.id.clone()),
1235 args: Some(raw.clone()),
1236 available_tools: tool_names.executable.iter().cloned().collect(),
1237 allowed_tools: tool_names.allowed.iter().cloned().collect(),
1238 tool_choice: self.tool_choice.clone(),
1239 chat_history: self.full_history(),
1240 is_streaming,
1241 reason: InvalidToolCallReason::malformed_arguments(raw),
1242 })
1243 }
1244
1245 fn validate_invalid_tool_call_action(
1250 &mut self,
1251 action: InvalidToolCallAction,
1252 diagnostic: InvalidToolCallDiagnostic<'_>,
1253 ) -> Result<ValidatedInvalidToolCallAction, PromptError> {
1254 let result = match action {
1255 InvalidToolCallAction::Fail => Err(diagnostic.unknown_current()),
1256 InvalidToolCallAction::Retry { feedback } => {
1257 if self.invalid_tool_call_retries >= self.max_invalid_tool_call_retries {
1258 Err(diagnostic.unknown_current())
1259 } else {
1260 self.invalid_tool_call_retries += 1;
1261 Ok(ValidatedInvalidToolCallAction::Retry { feedback })
1262 }
1263 }
1264 InvalidToolCallAction::Repair { tool_name } => {
1265 if diagnostic.allowed_tool_names.contains(&tool_name) {
1266 Ok(ValidatedInvalidToolCallAction::Repair { tool_name })
1267 } else {
1268 Err(diagnostic.unknown(tool_name))
1269 }
1270 }
1271 InvalidToolCallAction::Stop { reason } => Err(diagnostic.cancelled(reason)),
1272 InvalidToolCallAction::Skip { reason } => {
1273 if matches!(self.tool_choice, Some(ToolChoice::None)) {
1274 Err(diagnostic.unknown_current())
1275 } else {
1276 Ok(ValidatedInvalidToolCallAction::Skip { reason })
1277 }
1278 }
1279 };
1280
1281 if result.is_err() {
1282 self.state = RunState::Failed;
1283 }
1284 result
1285 }
1286
1287 pub fn resolve_invalid_tool_call(
1303 &mut self,
1304 action: InvalidToolCallAction,
1305 ) -> Result<ModelTurnOutcome, PromptError> {
1306 let mut resolving = self.take_resolving(
1307 "resolve_invalid_tool_call called without a pending invalid tool call",
1308 )?;
1309 let Some(tool_call) = pending_invalid_call(&resolving).cloned() else {
1310 self.state = RunState::ResolvingToolCalls(resolving);
1311 return Err(self.protocol_violation(
1312 "resolve_invalid_tool_call called without a pending invalid tool call",
1313 ));
1314 };
1315
1316 let diagnostic_history = self.diagnostic_history(&resolving);
1317 let action = self.validate_invalid_tool_call_action(
1318 action,
1319 InvalidToolCallDiagnostic {
1320 tool_call: &tool_call,
1321 executable_tool_names: &resolving.executable_tool_names,
1322 allowed_tool_names: &resolving.allowed_tool_names,
1323 history: &diagnostic_history,
1324 },
1325 )?;
1326
1327 match action {
1328 ValidatedInvalidToolCallAction::Retry { feedback } => {
1329 self.new_messages.extend(assistant_message(
1330 resolving.head.clone(),
1331 resolving.original_choice.clone(),
1332 ));
1333 let Some(user_message) = invalid_tool_retry_user_message(
1334 &resolving.original_choice,
1335 &tool_call.id,
1336 &feedback,
1337 ) else {
1338 return Err(PromptError::cancelled(
1339 diagnostic_history,
1340 "invalid tool call retry produced no retry messages",
1341 ));
1342 };
1343 self.new_messages.push(user_message);
1344 self.state = RunState::PreparingRequest;
1345 Ok(ModelTurnOutcome::TurnRetried)
1346 }
1347 ValidatedInvalidToolCallAction::Repair { tool_name } => {
1348 if let Some(AssistantContent::ToolCall(tool_call)) =
1349 resolving.items.get_mut(resolving.next_index)
1350 && let Ok(tool_name) = ToolName::new(tool_name)
1351 {
1352 tool_call.function.name = tool_name;
1353 }
1354 resolving.recovered = true;
1355 self.state = RunState::ResolvingToolCalls(resolving);
1356 self.advance_resolution()
1357 }
1358 ValidatedInvalidToolCallAction::Skip { reason } => {
1359 let user_content = tool_result_message(
1360 tool_call.id.clone(),
1361 tool_call.function.name.clone(),
1362 reason,
1363 );
1364 resolving.skipped.insert(resolving.next_index, user_content);
1368 resolving.recovered = true;
1369 resolving.any_skipped = true;
1370 resolving.next_index += 1;
1371 self.state = RunState::ResolvingToolCalls(resolving);
1372 self.advance_resolution()
1373 }
1374 }
1375 }
1376
1377 pub fn resolve_unhandled_invalid_tool_call(&mut self) -> Result<ModelTurnOutcome, PromptError> {
1381 match self.unhandled_invalid_tool_call {
1382 UnhandledInvalidToolCall::Fail => {
1383 self.resolve_invalid_tool_call(InvalidToolCallAction::fail())
1384 }
1385 UnhandledInvalidToolCall::Ignore => self.ignore_invalid_tool_call(),
1386 }
1387 }
1388
1389 pub fn ignore_invalid_tool_call(&mut self) -> Result<ModelTurnOutcome, PromptError> {
1392 let mut resolving = self.take_resolving(
1393 "ignore_invalid_tool_call called without a pending invalid tool call",
1394 )?;
1395
1396 if pending_invalid_call(&resolving).is_none() {
1397 self.state = RunState::ResolvingToolCalls(resolving);
1398 return Err(self.protocol_violation(
1399 "ignore_invalid_tool_call called without a pending invalid tool call",
1400 ));
1401 }
1402
1403 resolving.items.remove(resolving.next_index);
1404 resolving.has_tool_calls = has_tool_calls(&resolving.items);
1405 self.state = RunState::ResolvingToolCalls(resolving);
1406 self.advance_resolution()
1407 }
1408
1409 pub fn tool_results(&mut self, results: Vec<UserContent>) -> Result<(), PromptError> {
1415 let RunState::ExecutingTools(pending, _) = &self.state else {
1416 return Err(
1417 self.protocol_violation("tool_results called without a pending CallTools step")
1418 );
1419 };
1420 let mut unanswered: Vec<rig_core::message::CallId> = pending
1423 .iter()
1424 .map(|call| call.tool_call.id.clone())
1425 .collect();
1426
1427 if results.is_empty() {
1428 self.state = RunState::Failed;
1429 return Err(PromptError::cancelled(
1430 self.full_history(),
1431 "tool execution produced no tool results",
1432 ));
1433 }
1434 for result in &results {
1435 let UserContent::ToolResult(tool_result) = result else {
1436 return Err(self.protocol_violation(
1437 "tool_results received content that is not a tool result",
1438 ));
1439 };
1440 let Some(index) = unanswered.iter().position(|id| tool_result.call == *id) else {
1441 return Err(self.protocol_violation(&format!(
1442 "tool_results received a result for unknown or already-answered tool call id `{}`",
1443 tool_result.call
1444 )));
1445 };
1446 unanswered.swap_remove(index);
1447 }
1448 if !unanswered.is_empty() {
1449 return Err(self.protocol_violation(&format!(
1450 "tool_results left pending tool call id(s) unanswered: {unanswered:?}"
1451 )));
1452 }
1453
1454 self.new_messages.push(Message::User { content: results });
1456 self.state = RunState::PreparingRequest;
1457 Ok(())
1458 }
1459
1460 fn take_resolving(&mut self, violation: &str) -> Result<ResolvingState, PromptError> {
1464 match std::mem::replace(&mut self.state, RunState::Failed) {
1465 RunState::ResolvingToolCalls(resolving) => Ok(resolving),
1466 other => {
1467 self.state = other;
1468 Err(self.protocol_violation(violation))
1469 }
1470 }
1471 }
1472
1473 fn advance_resolution(&mut self) -> Result<ModelTurnOutcome, PromptError> {
1476 let mut resolving =
1477 self.take_resolving("internal: advance_resolution outside of tool-call resolution")?;
1478 while let Some(item) = resolving.items.get(resolving.next_index) {
1479 match item {
1480 AssistantContent::ToolCall(tool_call)
1481 if !resolving
1482 .allowed_tool_names
1483 .contains(tool_call.function.name.as_str()) =>
1484 {
1485 break;
1486 }
1487 _ => resolving.next_index += 1,
1488 }
1489 }
1490
1491 if resolving.next_index < resolving.items.len() {
1492 self.state = RunState::ResolvingToolCalls(resolving);
1493 return match self.pending_invalid_tool_call() {
1494 Some(context) => Ok(ModelTurnOutcome::NeedsResolution(context)),
1495 None => Err(self.protocol_violation(
1496 "internal: pending invalid tool call could not be derived",
1497 )),
1498 };
1499 }
1500
1501 let ResolvingState {
1502 head,
1503 items,
1504 mut skipped,
1505 recovered,
1506 any_skipped,
1507 has_tool_calls,
1508 executable_tool_names,
1509 allowed_tool_names,
1510 ..
1511 } = resolving;
1512
1513 if any_skipped {
1516 for (index, item) in items.iter().enumerate() {
1517 if let AssistantContent::ToolCall(tool_call) = item {
1518 skipped
1519 .entry(index)
1520 .or_insert_with(|| rig_core::transcript::not_executed(tool_call));
1521 }
1522 }
1523 }
1524
1525 self.finalize_turn(
1526 head,
1527 items,
1528 has_tool_calls,
1529 skipped,
1530 ToolNames {
1531 executable: executable_tool_names,
1532 allowed: allowed_tool_names,
1533 },
1534 );
1535 Ok(ModelTurnOutcome::Continue {
1536 response_hook_suppressed: recovered,
1537 })
1538 }
1539
1540 pub fn record_streamed_completion_call(
1548 &mut self,
1549 usage: Usage,
1550 identity: ResponseIdentity,
1551 finish_reason: Option<FinishReason>,
1552 raw: serde_json::Value,
1553 ) -> Result<CompletionCall, PromptError> {
1554 let recordable = matches!(self.state, RunState::AwaitingModel)
1555 || (matches!(self.state, RunState::PreparingRequest) && self.rollback_pending);
1556 if !recordable {
1557 return Err(self.protocol_violation(
1558 "record_streamed_completion_call called without a pending or rolled-back CallModel step",
1559 ));
1560 }
1561 if self.streamed_completion_call_recorded {
1562 return Err(self.protocol_violation(
1563 "record_streamed_completion_call called twice for the same model turn",
1564 ));
1565 }
1566 self.streamed_completion_call_recorded = true;
1567
1568 Ok(self.record_completion_call(usage, identity, finish_reason, raw))
1569 }
1570
1571 pub fn streamed_invalid_tool_call_context(
1574 &self,
1575 partial: &PartialStreamedTurn,
1576 invalid: &StreamedInvalidToolCall,
1577 ) -> InvalidToolCallContext {
1578 InvalidToolCallContext {
1579 tool_name: invalid.tool_call.function.name.to_string(),
1580 tool_call_id: Some(invalid.tool_call.id.clone()),
1581 args: invalid.args.clone(),
1582 available_tools: invalid.executable_tool_names.iter().cloned().collect(),
1583 allowed_tools: invalid.allowed_tool_names.iter().cloned().collect(),
1584 tool_choice: self.tool_choice.clone(),
1585 chat_history: self
1586 .streamed_diagnostic_history(partial, Some(invalid.tool_call.clone())),
1587 is_streaming: true,
1588 reason: name_reason(
1589 &invalid.executable_tool_names,
1590 invalid.tool_call.function.name.as_str(),
1591 ),
1592 }
1593 }
1594
1595 pub fn resolve_streamed_invalid_tool_call(
1601 &mut self,
1602 partial: &PartialStreamedTurn,
1603 invalid: &StreamedInvalidToolCall,
1604 action: InvalidToolCallAction,
1605 ) -> Result<StreamedResolution, PromptError> {
1606 if !matches!(self.state, RunState::AwaitingModel) {
1607 return Err(self.protocol_violation(
1608 "resolve_streamed_invalid_tool_call called without a pending CallModel step",
1609 ));
1610 }
1611
1612 let diagnostic_history =
1613 self.streamed_diagnostic_history(partial, Some(invalid.tool_call.clone()));
1614 let action = self.validate_invalid_tool_call_action(
1615 action,
1616 InvalidToolCallDiagnostic {
1617 tool_call: &invalid.tool_call,
1618 executable_tool_names: &invalid.executable_tool_names,
1619 allowed_tool_names: &invalid.allowed_tool_names,
1620 history: &diagnostic_history,
1621 },
1622 )?;
1623
1624 match action {
1625 ValidatedInvalidToolCallAction::Retry { feedback } => self.abandon_streamed_turn(
1626 partial,
1627 invalid,
1628 feedback,
1629 diagnostic_history,
1630 "invalid tool call retry produced no retry messages",
1631 None,
1632 ),
1633 ValidatedInvalidToolCallAction::Repair { tool_name } => {
1634 Ok(StreamedResolution::Repaired { tool_name })
1635 }
1636 ValidatedInvalidToolCallAction::Skip { reason } => {
1637 let skipped_tool_result = invalid
1641 .tool_call
1642 .result(vec![ToolResultContent::text(reason.as_str())]);
1643 self.abandon_streamed_turn(
1644 partial,
1645 invalid,
1646 reason,
1647 diagnostic_history,
1648 "invalid tool call skip produced no recovery messages",
1649 Some(skipped_tool_result),
1650 )
1651 }
1652 }
1653 }
1654
1655 pub fn ignore_streamed_invalid_tool_call(&mut self) -> Result<StreamedResolution, PromptError> {
1659 if !matches!(self.state, RunState::AwaitingModel) {
1660 return Err(self.protocol_violation(
1661 "ignore_streamed_invalid_tool_call called without a pending CallModel step",
1662 ));
1663 }
1664 Ok(StreamedResolution::Ignored)
1665 }
1666
1667 fn abandon_streamed_turn(
1671 &mut self,
1672 partial: &PartialStreamedTurn,
1673 invalid: &StreamedInvalidToolCall,
1674 feedback: String,
1675 diagnostic_history: Vec<Message>,
1676 no_messages_reason: &str,
1677 skipped_tool_result: Option<ToolResult>,
1678 ) -> Result<StreamedResolution, PromptError> {
1679 let Some((assistant_message, user_message)) =
1680 partial.rollback_messages(invalid.tool_call.clone(), feedback)
1681 else {
1682 self.state = RunState::Failed;
1683 return Err(PromptError::cancelled(
1684 diagnostic_history,
1685 no_messages_reason,
1686 ));
1687 };
1688 self.new_messages.push(assistant_message);
1689 self.new_messages.push(user_message);
1690 self.rollback_pending = true;
1691 self.state = RunState::PreparingRequest;
1692 Ok(StreamedResolution::TurnAbandoned {
1693 skipped_tool_result,
1694 })
1695 }
1696
1697 pub fn streamed_turn(&mut self, turn: StreamedTurn) -> Result<(), PromptError> {
1705 if !matches!(self.state, RunState::AwaitingModel) {
1706 return Err(
1707 self.protocol_violation("streamed_turn called without a pending CallModel step")
1708 );
1709 }
1710 if !self.streamed_completion_call_recorded {
1711 return Err(self.protocol_violation(
1712 "streamed_turn called before record_streamed_completion_call recorded the turn's completion call",
1713 ));
1714 }
1715
1716 let has_tool_calls = has_tool_calls(&turn.choice);
1717
1718 for item in &turn.choice {
1719 let AssistantContent::ToolCall(tool_call) = item else {
1720 continue;
1721 };
1722 if !turn
1723 .allowed_tool_names
1724 .contains(tool_call.function.name.as_str())
1725 {
1726 let mut diagnostic_messages = self.new_messages.clone();
1727 diagnostic_messages.extend(assistant_turn(turn.head.clone(), turn.choice.clone()));
1728 let diagnostic_history =
1729 build_full_history(self.chat_history.as_deref(), diagnostic_messages);
1730 self.state = RunState::Failed;
1731 return Err(unknown_tool_call_error(
1732 tool_call.function.name.to_string(),
1733 turn.executable_tool_names.iter().cloned().collect(),
1734 turn.allowed_tool_names.iter().cloned().collect(),
1735 diagnostic_history,
1736 ));
1737 }
1738 }
1739
1740 self.finalize_turn(
1741 turn.head,
1742 turn.choice,
1743 has_tool_calls,
1744 BTreeMap::new(),
1745 ToolNames {
1746 executable: turn.executable_tool_names,
1747 allowed: turn.allowed_tool_names,
1748 },
1749 );
1750 Ok(())
1751 }
1752
1753 fn streamed_diagnostic_history(
1756 &self,
1757 partial: &PartialStreamedTurn,
1758 current_tool_call: Option<ToolCall>,
1759 ) -> Vec<Message> {
1760 let mut messages = self.new_messages.clone();
1761 if let Some(assistant) = partial.assistant_message(current_tool_call) {
1762 messages.push(assistant);
1763 }
1764 build_full_history(self.chat_history.as_deref(), messages)
1765 }
1766
1767 fn diagnostic_history(&self, resolving: &ResolvingState) -> Vec<Message> {
1770 let mut diagnostic_messages = self.new_messages.clone();
1771 diagnostic_messages.extend(assistant_message(
1772 resolving.head.clone(),
1773 resolving.original_choice.clone(),
1774 ));
1775 build_full_history(self.chat_history.as_deref(), diagnostic_messages)
1776 }
1777
1778 fn protocol_violation(&self, reason: &str) -> PromptError {
1779 PromptError::cancelled(
1780 self.full_history(),
1781 format!("agent run driver protocol violation: {reason}"),
1782 )
1783 }
1784}
1785
1786impl AgentRun {
1787 pub fn from_spec(
1793 spec: &RunSpec,
1794 prompt: impl Into<Message>,
1795 history: Option<Vec<Message>>,
1796 ) -> Self {
1797 let mut run = AgentRun::new(prompt)
1798 .max_turns(spec.effective_max_turns())
1799 .max_invalid_tool_call_retries(spec.max_invalid_tool_call_retries)
1800 .with_unhandled_invalid_tool_call(spec.unhandled_invalid_tool_call)
1801 .with_output_validation(spec.output_schema.clone(), RunSpec::DEFAULT_OUTPUT_RETRIES);
1802 run.max_consecutive_malformed_tool_calls = spec.max_consecutive_malformed_tool_calls;
1803 if let Some(history) = history {
1804 run = run.with_history(history);
1805 }
1806 if let Some(tool_choice) = spec.tool_choice.clone() {
1807 run = run.with_tool_choice(tool_choice);
1808 }
1809 if let Some(name) = spec.output_tool_name.clone() {
1810 run = run.with_output_tool_name(name);
1811 }
1812 run
1813 }
1814}
1815
1816impl AgentRun {
1817 pub fn with_validated_history(self, history: Vec<Message>) -> Result<Self, TranscriptError> {
1823 validate_canonical(&history)?;
1824 Ok(self.with_history(history))
1825 }
1826}
1827
1828#[cfg(not(target_family = "wasm"))]
1831const _: fn() = || {
1832 fn assert_send_sync_static<T: Send + Sync + 'static>() {}
1833 assert_send_sync_static::<AgentRun>();
1834 assert_send_sync_static::<AgentRunStep>();
1835 assert_send_sync_static::<PendingToolCall>();
1836 assert_send_sync_static::<ModelTurn>();
1837 assert_send_sync_static::<StreamedTurn>();
1838 assert_send_sync_static::<StreamedTurnAssembler>();
1839 assert_send_sync_static::<PromptResponse>();
1840 assert_send_sync_static::<CompletionCall>();
1841 assert_send_sync_static::<PromptError>();
1842 assert_send_sync_static::<TurnTools>();
1843 assert_send_sync_static::<RunEntry>();
1844};
1845
1846#[cfg(test)]
1847mod tests;