1use std::{
2 cell::{Cell, RefCell},
3 collections::{HashMap, HashSet},
4 time::Duration,
5};
6
7use futures_util::StreamExt;
8use infinity_provider_protocol::{
9 CompletionRequest, StreamChunk, ToolCallDeltaContent, ToolDefinition,
10 message::{AssistantContent, Message, ToolResult, ToolResultContent, UserContent},
11};
12use rap_protocol::ThreadId;
13use serde::Serialize;
14use tracing;
15
16use crate::message::{
17 InfinityMessage, InputMessage, InputMessageContent, SyntheticKind, TaggedSyntheticKind,
18};
19use crate::system::AgentEvent;
20use crate::tools::{Tool, ToolContext};
21use crate::traits::{ConversationStore, InputSender, StateStore};
22use infinity_provider_protocol::{FinalResponse, ModelProvider};
23
24type BoxError = Box<dyn std::error::Error + Send + Sync>;
27
28#[derive(Debug, Serialize)]
29pub struct OutputMessage {
30 pub text: String,
31 pub metadata: serde_json::Value,
32}
33
34#[derive(Debug, Serialize)]
35pub struct OAuthOutputMessage {
36 #[serde(rename = "type")]
37 pub message_type: String,
38 pub auth_url: String,
39 pub metadata: serde_json::Value,
40}
41
42#[derive(Debug, Serialize)]
43pub struct UserChoiceOutputMessage {
44 #[serde(rename = "type")]
45 pub message_type: String,
46 pub id: String,
47 pub prompt: String,
48 pub choices: Vec<String>,
49 pub default: usize,
50 pub metadata: serde_json::Value,
51}
52
53#[derive(Debug, Serialize)]
54pub struct UserChoiceCompleteOutputMessage {
55 #[serde(rename = "type")]
56 pub message_type: String,
57 pub choice_id: String,
58 pub metadata: serde_json::Value,
59}
60
61#[derive(Debug, PartialEq, Serialize)]
63pub enum PrepareResult {
64 Ready,
66 Handled,
68 OAuthRequired { auth_url: String },
70 UserChoiceRequired {
72 id: String,
73 prompt: String,
74 choices: Vec<String>,
75 default: usize,
76 response_url: String,
77 },
78 CompactionApplied,
80}
81
82pub enum CompletionAction {
84 Done(FinalResponse),
86 ExecuteToolCall {
90 tool_name: String,
91 tool_args: serde_json::Value,
92 tool_call_id: String,
93 call_id: Option<String>,
94 display_as: Option<String>,
95 },
96}
97
98pub enum CompletionEvent {
100 TextChunk(String),
102 Action(CompletionAction),
104 SyncToolCall {
106 tool_name: String,
107 tool_args: serde_json::Value,
108 display_as: Option<String>,
109 },
110 ThinkingStart,
112 ThinkingEnd,
114 ThinkingChunk(String),
116 SyncToolResult(ToolResult),
118 Info(String),
120}
121
122#[derive(Serialize, Clone)]
125pub struct PendingItem {
126 message: InfinityMessage,
127 message_id: String,
128}
129
130enum ToolResultMatch {
133 Unanswered,
135 AlreadyAnswered,
137 NoPendingCall,
139}
140
141pub struct HistoryManager<C: ConversationStore, S: StateStore> {
142 conversation_store: C,
143 state_store: S,
144 pub thread_id: ThreadId,
145 pub root_thread_id: ThreadId,
146 ancestor_chain: Vec<ThreadId>,
147 pub history: RefCell<Vec<InfinityMessage>>,
148 processed_message_ids: RefCell<HashSet<String>>,
149 metadata: RefCell<Option<serde_json::Value>>,
150 pending_items: RefCell<Vec<PendingItem>>,
151 turn_buffer: RefCell<Vec<PendingItem>>,
155 interrupted_tool_calls: RefCell<Vec<String>>,
159 compacted_up_to: RefCell<Option<i64>>,
163 ancestor_prefix_len: Cell<usize>,
168}
169
170impl<C: ConversationStore, S: StateStore> HistoryManager<C, S> {
171 pub async fn new_with_history(
172 conversation_store: C,
173 state_store: S,
174 thread_id: ThreadId,
175 ) -> Result<Self, BoxError> {
176 let _ = conversation_store.ensure_root_thread(&thread_id).await;
177
178 let ancestor_chain: Vec<ThreadId> = conversation_store
179 .get_ancestor_chain(&thread_id)
180 .await
181 .map(|links| links.iter().map(|(tid, _)| tid.clone()).collect())
182 .unwrap_or_default();
183 let root_thread_id = ancestor_chain
184 .first()
185 .cloned()
186 .unwrap_or_else(|| thread_id.clone());
187
188 let (history, compacted_up_to, ancestor_prefix_len) = conversation_store
189 .load_history_with_ancestors(&thread_id)
190 .await
191 .map_err(|e| Box::new(e) as BoxError)?;
192
193 let metadata = state_store
194 .get_metadata(&root_thread_id)
195 .await
196 .unwrap_or(None);
197
198 let processed_message_ids = state_store
199 .get_processed_ids(&thread_id)
200 .await
201 .unwrap_or_default();
202
203 Ok(Self {
204 conversation_store,
205 state_store,
206 thread_id,
207 root_thread_id,
208 ancestor_chain,
209 history: RefCell::new(history),
210 processed_message_ids: RefCell::new(processed_message_ids),
211 metadata: RefCell::new(metadata),
212 pending_items: RefCell::new(Vec::new()),
213 turn_buffer: RefCell::new(Vec::new()),
214 interrupted_tool_calls: RefCell::new(Vec::new()),
215 compacted_up_to: RefCell::new(compacted_up_to),
216 ancestor_prefix_len: Cell::new(ancestor_prefix_len),
217 })
218 }
219
220 pub fn handle_content(
221 &self,
222 message: InfinityMessage,
223 message_id: String,
224 ) -> Result<bool, BoxError> {
225 if self.processed_message_ids.borrow().contains(&message_id) {
226 tracing::info!("Message {} already processed, skipping", message_id);
227 return Ok(false);
228 }
229
230 let is_self_contained_subscription = matches!(
233 message,
234 InfinityMessage::SubscriptionEvent {
235 invocation: Some(_),
236 ..
237 }
238 );
239
240 if !is_self_contained_subscription {
241 if let Some(tool_result) = message.tool_result() {
242 match self.match_tool_result(&tool_result.id) {
243 ToolResultMatch::Unanswered => {}
244 ToolResultMatch::AlreadyAnswered => {
245 tracing::info!(
246 "Tool call {} already processed, ignoring duplicate",
247 tool_result.id
248 );
249 self.processed_message_ids.borrow_mut().insert(message_id);
250 return Ok(false);
251 }
252 ToolResultMatch::NoPendingCall => {
253 tracing::info!(
254 "Got tool call result for wrong call, ignoring {:?}",
255 tool_result
256 );
257 return Ok(false);
258 }
259 }
260 } else {
261 self.interrupt_pending_tool_call();
262 }
263 } else {
264 self.interrupt_pending_tool_call();
265 }
266
267 self.append_pending(message, message_id.clone());
268 self.processed_message_ids.borrow_mut().insert(message_id);
269 Ok(true)
270 }
271
272 fn match_tool_result(&self, result_id: &str) -> ToolResultMatch {
280 for msg in self.history.borrow().iter().rev() {
281 match msg {
282 InfinityMessage::ToolCall { call, .. } => {
283 if call.id == result_id {
284 return ToolResultMatch::Unanswered;
287 }
288 }
289 InfinityMessage::ToolResult { result, .. } => {
290 if result.id == result_id {
291 return ToolResultMatch::AlreadyAnswered;
292 }
293 }
294 _ => return ToolResultMatch::NoPendingCall,
295 }
296 }
297 ToolResultMatch::NoPendingCall
298 }
299
300 fn interrupt_pending_tool_call(&self) {
304 let last_call = self.history.borrow().last().and_then(|m| {
305 if let InfinityMessage::ToolCall { call, .. } = m {
306 Some(call.clone())
307 } else {
308 None
309 }
310 });
311 if let Some(tool_call) = last_call {
312 tracing::info!("Tool call {} interrupted by incoming message", tool_call.id);
313 self.interrupted_tool_calls
314 .borrow_mut()
315 .push(tool_call.id.clone());
316 let synthetic_result = InfinityMessage::ToolResult {
317 result: ToolResult {
318 id: tool_call.id.clone(),
319 call_id: tool_call.call_id.clone(),
320 content: vec![ToolResultContent::Text(
321 infinity_provider_protocol::message::Text {
322 text: "Tool call interrupted by user".to_owned(),
323 },
324 )],
325 },
326 display_segments: None,
327 };
328 self.append_pending(synthetic_result, format!("{}-interrupted", tool_call.id));
329 }
330 }
331
332 pub fn handle_completion(
338 &self,
339 completion: &StreamChunk,
340 completion_id: String,
341 display_as: Option<String>,
342 ) {
343 if self.processed_message_ids.borrow().contains(&completion_id) {
344 return;
345 }
346 if let StreamChunk::Text(text) = completion
350 && self.try_merge_buffer_text(text)
351 {
352 return;
353 }
354 let infinity_message = match completion {
355 StreamChunk::Text(text) => InfinityMessage::Assistant {
356 content: AssistantContent::text(text.clone()),
357 },
358 StreamChunk::Reasoning(r) => InfinityMessage::Assistant {
359 content: AssistantContent::Reasoning(r.clone()),
360 },
361 StreamChunk::ToolCall(call) => InfinityMessage::ToolCall {
362 call: call.clone(),
363 display_as,
364 },
365 StreamChunk::ToolCallDelta { .. }
366 | StreamChunk::ReasoningDelta { .. }
367 | StreamChunk::Final(_) => {
368 return;
369 }
370 };
371 self.turn_buffer.borrow_mut().push(PendingItem {
372 message: infinity_message,
373 message_id: completion_id,
374 });
375 }
376
377 pub fn flush_turn(&self) {
382 let drained = std::mem::take(&mut *self.turn_buffer.borrow_mut());
383 for item in drained {
384 self.append_pending(item.message, item.message_id);
385 }
386 }
387
388 pub fn flush_turn_trimming_reasoning(&self) {
392 {
393 let mut buffer = self.turn_buffer.borrow_mut();
394 while let Some(PendingItem {
398 message: InfinityMessage::Assistant { content },
399 ..
400 }) = buffer.last()
401 {
402 match content {
403 AssistantContent::Reasoning(_) => {}
404 AssistantContent::Text(text) if text.text.trim().is_empty() => {}
405 _ => break,
406 }
407 buffer.pop();
408 }
409 }
410 self.flush_turn();
411 }
412
413 pub fn discard_turn(&self) {
418 self.turn_buffer.borrow_mut().clear();
419 }
420
421 pub fn turn_buffer_is_empty(&self) -> bool {
422 self.turn_buffer.borrow().is_empty()
423 }
424
425 pub fn current_turn_view(&self) -> Vec<InfinityMessage> {
429 let history = self.history.borrow();
430 let buffer = self.turn_buffer.borrow();
431 history
432 .iter()
433 .cloned()
434 .chain(buffer.iter().map(|item| item.message.clone()))
435 .collect()
436 }
437
438 fn append_pending(&self, message: InfinityMessage, message_id: String) {
439 assert!(
440 self.turn_buffer.borrow().is_empty(),
441 "bug: append_pending called with un-flushed turn_buffer content"
442 );
443
444 self.history.borrow_mut().push(message.clone());
445 self.pending_items.borrow_mut().push(PendingItem {
446 message,
447 message_id,
448 });
449 }
450
451 fn try_merge_buffer_text(&self, text: &str) -> bool {
456 let mut buffer = self.turn_buffer.borrow_mut();
457 let Some(last) = buffer.last_mut() else {
458 return false;
459 };
460 let InfinityMessage::Assistant {
461 content: AssistantContent::Text(existing),
462 } = &mut last.message
463 else {
464 return false;
465 };
466 existing.text.push_str(text);
467 true
468 }
469
470 pub async fn sync(&self) -> Result<(), BoxError> {
471 assert!(
475 self.turn_buffer.borrow().is_empty(),
476 "bug: sync() called with un-flushed turn_buffer content"
477 );
478
479 let pending_items = std::mem::take(&mut *self.pending_items.borrow_mut());
480 if pending_items.is_empty() {
481 return Ok(());
482 }
483 let msgs: Vec<(InfinityMessage, String)> = pending_items
484 .iter()
485 .map(|item| (item.message.clone(), item.message_id.clone()))
486 .collect();
487 self.conversation_store
488 .append_messages(&self.thread_id, msgs)
489 .await
490 .map_err(|e| Box::new(e) as BoxError)?;
491 let msg_ids: Vec<String> = pending_items
497 .iter()
498 .filter(|i| {
499 matches!(
500 i.message,
501 InfinityMessage::User { .. } | InfinityMessage::SubscriptionEvent { .. }
502 )
503 })
504 .map(|i| i.message_id.clone())
505 .collect();
506 if !msg_ids.is_empty() {
507 let _ = self
508 .state_store
509 .add_processed_message_ids(&self.thread_id, msg_ids)
510 .await;
511 }
512 Ok(())
513 }
514
515 pub async fn update_metadata(&self, metadata: serde_json::Value) -> Result<(), BoxError> {
516 *self.metadata.borrow_mut() = Some(metadata.clone());
517 self.state_store
518 .set_metadata(&self.root_thread_id, metadata)
519 .await
520 .map_err(|e| Box::new(e) as BoxError)
521 }
522
523 pub fn get_metadata(&self) -> Option<serde_json::Value> {
524 self.metadata.borrow().clone()
525 }
526 pub fn get_history(&self, supports_image_input: bool) -> Vec<Message> {
530 self.history
531 .borrow()
532 .iter()
533 .flat_map(|m| m.clone().into_messages())
534 .map(|mut msg| {
535 if !supports_image_input {
536 strip_image_tool_results(&mut msg);
537 }
538 msg
539 })
540 .collect()
541 }
542
543 pub fn get_thread_stack(&self) -> Vec<ThreadId> {
546 let mut stack = self.ancestor_chain.clone();
547 stack.push(self.thread_id.clone());
548 stack
549 }
550
551 pub fn conversation_store(&self) -> &C {
552 &self.conversation_store
553 }
554
555 pub async fn apply_compaction(&self) -> Result<bool, BoxError> {
558 if let Ok(Some((summary, up_to_order))) = self
559 .conversation_store
560 .load_latest_compaction_summary_up_to(&self.thread_id, None)
561 .await
562 {
563 let offset = self
569 .compacted_up_to
570 .borrow()
571 .map_or(0, |prev| prev as usize - 1);
572 let up_to =
576 (up_to_order as usize).saturating_sub(offset) + self.ancestor_prefix_len.get();
577 let mut history = self.history.borrow_mut();
578 if up_to <= history.len() {
579 let remaining = history.split_off(up_to);
580 *history = vec![InfinityMessage::Assistant {
581 content: AssistantContent::text(format!(
582 "[Compacted conversation summary]\n{}",
583 summary
584 )),
585 }];
586 history.extend(remaining);
587 *self.compacted_up_to.borrow_mut() = Some(up_to_order);
588 self.ancestor_prefix_len.set(0);
590 return Ok(true);
591 }
592 }
593 Ok(false)
594 }
595
596 pub fn take_interrupted_tool_calls(&self) -> Vec<String> {
600 std::mem::take(&mut *self.interrupted_tool_calls.borrow_mut())
601 }
602
603 pub fn safe_spawn_point(&self) -> usize {
607 let history = self.history.borrow();
608 let mut answered: HashSet<&str> = HashSet::new();
615 let mut safe = history.len();
616 for (i, msg) in history.iter().enumerate().rev() {
617 match msg {
618 InfinityMessage::ToolCall { call, .. } => {
619 if !answered.contains(call.id.as_str()) {
620 safe = i;
621 }
622 }
623 InfinityMessage::ToolResult { result, .. } => {
624 answered.insert(result.id.as_str());
625 }
626 _ => break,
627 }
628 }
629 let offset = self
633 .compacted_up_to
634 .borrow()
635 .map_or(0, |prev| prev as usize - 1);
636 safe.saturating_sub(self.ancestor_prefix_len.get()) + offset
639 }
640
641 pub async fn track_subscription(&self, tool_call_id: &str) -> Result<(), BoxError> {
646 self.state_store
647 .add_active_subscription(&self.thread_id, tool_call_id)
648 .await
649 .map_err(|e| Box::new(e) as BoxError)
650 }
651
652 pub async fn remove_subscription(&self, tool_call_id: &str) -> Result<(), BoxError> {
654 self.state_store
655 .remove_active_subscription(&self.thread_id, tool_call_id)
656 .await
657 .map_err(|e| Box::new(e) as BoxError)
658 }
659}
660
661pub async fn prepare_input<C, S, M>(
667 input_msg: InputMessage,
668 message_id: String,
669 current_history: &HistoryManager<C, S>,
670 conversation_store: &C,
671 message_sender: &M,
672) -> Result<PrepareResult, BoxError>
673where
674 C: ConversationStore,
675 S: StateStore,
676 M: InputSender,
677{
678 if conversation_store
680 .is_thread_closed(&input_msg.group_id)
681 .await
682 .unwrap_or(false)
683 {
684 tracing::warn!(
685 "Received message for closed thread {}, skipping",
686 input_msg.group_id
687 );
688 return Ok(PrepareResult::Handled);
689 }
690
691 if input_msg
693 .synthetic
694 .as_ref()
695 .is_some_and(SyntheticKind::is_compaction_complete)
696 {
697 tracing::info!("Applying compaction to thread {}", input_msg.group_id);
698 current_history.apply_compaction().await?;
699 return Ok(PrepareResult::CompactionApplied);
700 }
701
702 if input_msg
704 .synthetic
705 .as_ref()
706 .is_some_and(SyntheticKind::is_compaction)
707 {
708 let spawn_call_id = uuid::Uuid::new_v4().to_string();
709
710 let safe_point = current_history.safe_spawn_point();
713
714 let sub_thread_id = conversation_store
715 .spawn_thread(&input_msg.group_id, &spawn_call_id, false, Some(safe_point))
716 .await
717 .map_err(|e| Box::new(e) as BoxError)?;
718 conversation_store
719 .mark_thread_as_compaction(&sub_thread_id)
720 .await
721 .map_err(|e| Box::new(e) as BoxError)?;
722
723 tracing::info!(
724 "Spawned compaction thread {} for parent {}",
725 sub_thread_id,
726 input_msg.group_id
727 );
728
729 let spawn_tool_call = InfinityMessage::ToolCall {
734 call: infinity_provider_protocol::message::ToolCall {
735 id: spawn_call_id.clone(),
736 call_id: None,
737 function: infinity_provider_protocol::message::ToolFunction {
738 name: "__harness_begin_compaction__".to_owned(),
739 arguments: serde_json::json!({}),
740 },
741 },
742 display_as: None,
743 };
744 conversation_store
745 .append_messages(
746 &sub_thread_id,
747 vec![(
748 spawn_tool_call,
749 format!("{}-compaction-call", spawn_call_id),
750 )],
751 )
752 .await
753 .map_err(|e| Box::new(e) as BoxError)?;
754
755 let child_msg = InputMessage {
757 content: InputMessageContent::User(UserContent::ToolResult(ToolResult {
758 id: spawn_call_id.clone(),
759 call_id: None,
760 content: vec![ToolResultContent::Text(
761 infinity_provider_protocol::message::Text {
762 text: format!(
763 "This tool call was synthetically injected by the harness. You are now INSIDE a compaction thread. You can see the full conversation history inherited from your parent thread, including all ancestor thread context. \
764 Summarize ALL of this content into a concise but comprehensive summary that preserves: all important context, decisions made, \
765 current task progress, relevant code changes and file paths, and any pending work. \
766 Then call close_thread with your thread ID ({}) and include the summary in report_to_parent.",
767 sub_thread_id
768 ),
769 },
770 )],
771 })),
772 group_id: sub_thread_id.clone(),
773 metadata: None,
774 synthetic: None,
775 display_as: None,
776 subscription: false,
777 };
778 message_sender
779 .send_to_input_queue(child_msg, &spawn_call_id)
780 .await
781 .map_err(|e| Box::new(e) as BoxError)?;
782
783 return Ok(PrepareResult::Handled);
784 }
785
786 if let Some(metadata) = input_msg.metadata {
788 current_history.update_metadata(metadata).await?;
789 }
790
791 if let InputMessageContent::OAuth(oauth) = &input_msg.content {
793 assert!(oauth.content_type == "oauth_required");
794 tracing::info!("Received OAuth required message, returning to caller");
795 return Ok(PrepareResult::OAuthRequired {
796 auth_url: oauth.auth_url.clone(),
797 });
798 }
799
800 if let InputMessageContent::UserChoice(choice) = &input_msg.content {
802 assert!(choice.content_type == "user_choice_required");
803 tracing::info!("Received user choice required message, returning to caller");
804 return Ok(PrepareResult::UserChoiceRequired {
805 id: choice.id.clone(),
806 prompt: choice.prompt.clone(),
807 choices: choice.choices.clone(),
808 default: choice.default,
809 response_url: choice.response_url.clone(),
810 });
811 }
812
813 let is_subscription = input_msg.subscription;
814
815 let user_content = match input_msg.content {
816 InputMessageContent::User(content) => content,
817 InputMessageContent::OAuth(_) | InputMessageContent::UserChoice(_) => {
818 return Ok(PrepareResult::Handled);
819 }
820 };
821
822 let subscription_event_meta: Option<(String, Option<ThreadId>)> =
825 input_msg.synthetic.as_ref().and_then(|s| {
826 if s.is_thread_report() || s.is_associative() || s.is_parent_message() {
827 let child_id = if let SyntheticKind::Tagged(TaggedSyntheticKind::ThreadReport {
828 child_thread_id,
829 ..
830 }) = s
831 {
832 Some(child_thread_id.clone())
833 } else {
834 None
835 };
836 Some((s.tool_call_id().to_owned(), child_id))
837 } else {
838 None
839 }
840 });
841
842 let mut subscription_invocation: Option<infinity_provider_protocol::message::ToolCall> = None;
844
845 let content = if let Some(synthetic_kind) = input_msg.synthetic {
846 let original_tool_call_id = synthetic_kind.tool_call_id().to_owned();
847 let is_final_subscription = synthetic_kind.is_final();
848 tracing::info!(
849 "Processing synthetic tool result for tool call: {}",
850 original_tool_call_id
851 );
852
853 let original_call = current_history.history.borrow().iter().find_map(|msg| {
854 if let InfinityMessage::ToolCall { call, .. } = msg
855 && call.id == original_tool_call_id
856 {
857 Some(call.clone())
858 } else {
859 None
860 }
861 });
862
863 let Some(original_call) = original_call else {
864 tracing::warn!(
865 "Could not find original tool call for synthetic message: {}, dropping",
866 original_tool_call_id
867 );
868 return Ok(PrepareResult::Handled);
869 };
870
871 if synthetic_kind.is_thread_report()
872 || synthetic_kind.is_associative()
873 || synthetic_kind.is_parent_message()
874 {
875 let new_tool_call_id = uuid::Uuid::new_v4().to_string();
876 if let UserContent::ToolResult(mut tool_result) = user_content {
877 subscription_invocation = Some(infinity_provider_protocol::message::ToolCall {
878 id: new_tool_call_id.clone(),
879 call_id: None,
880 function: infinity_provider_protocol::message::ToolFunction {
881 name: "receive_event__injected".to_owned(),
882 arguments: serde_json::json!({
883 "original_tool_name": original_call.function.name,
884 "original_tool_call_id": original_tool_call_id,
885 "original_args": original_call.function.arguments,
886 }),
887 },
888 });
889 tool_result.id = new_tool_call_id;
890 if is_final_subscription {
892 current_history
893 .remove_subscription(&original_tool_call_id)
894 .await
895 .ok();
896 }
897 UserContent::ToolResult(tool_result)
898 } else {
899 return Err("Synthetic message is not a tool result".into());
900 }
901 } else {
902 tracing::info!(
904 "Spawning subthread for subscription event from tool call: {}",
905 original_tool_call_id
906 );
907
908 let safe_point = current_history.safe_spawn_point();
911
912 let sub_thread_id = conversation_store
913 .spawn_thread(
914 &input_msg.group_id,
915 &original_tool_call_id,
916 true,
917 Some(safe_point),
918 )
919 .await
920 .map_err(|e| Box::new(e) as BoxError)?;
921
922 tracing::info!(
923 "Created subthread {} for subscription event in parent {}",
924 sub_thread_id,
925 input_msg.group_id
926 );
927
928 let event_call_id = uuid::Uuid::new_v4().to_string();
929 let spawn_call_id = uuid::Uuid::new_v4().to_string();
930
931 let event_content = if let UserContent::ToolResult(mut tool_result) = user_content {
932 tool_result.id = event_call_id.clone();
933 tool_result.call_id = None;
934 tool_result
935 } else {
936 return Err("Synthetic subscription event is not a tool result".into());
937 };
938
939 let mut child_messages: Vec<(InfinityMessage, String)> = Vec::new();
943
944 let event_tool_call = InfinityMessage::ToolCall {
946 call: infinity_provider_protocol::message::ToolCall {
947 id: event_call_id.clone(),
948 call_id: None,
949 function: infinity_provider_protocol::message::ToolFunction {
950 name: "receive_event__injected".to_owned(),
951 arguments: serde_json::json!({
952 "original_tool_name": original_call.function.name,
953 "original_tool_call_id": original_tool_call_id,
954 "original_args": original_call.function.arguments,
955 }),
956 },
957 },
958 display_as: None,
959 };
960 let spawn_tool_call = InfinityMessage::ToolCall {
961 call: infinity_provider_protocol::message::ToolCall {
962 id: spawn_call_id.clone(),
963 call_id: None,
964 function: infinity_provider_protocol::message::ToolFunction {
965 name: "spawn_thread".to_owned(),
966 arguments: serde_json::json!({
967 "instructions": "Spawning thread to process incoming event."
968 }),
969 },
970 },
971 display_as: None,
972 };
973 let spawn_tool_result = InfinityMessage::ToolResult {
974 result: ToolResult {
975 id: spawn_call_id.clone(),
976 call_id: None,
977 content: vec![ToolResultContent::Text(
978 infinity_provider_protocol::message::Text {
979 text: format!(
980 "You are now INSIDE the thread for processing the single event above. Your thread ID is {}, the parent which is still subscribing is {}. Process the single subscription event above, report to the parent if appropriate, then close the thread after processing this event. Your outputs are NOT VISIBLE to the user, if you want to show them something, send a report to your parent.",
981 sub_thread_id, input_msg.group_id
982 ),
983 },
984 )],
985 },
986 display_segments: None,
987 };
988 child_messages.extend(vec![
989 (spawn_tool_call, format!("{}-spawn-call", spawn_call_id)),
990 (spawn_tool_result, format!("{}-spawn-result", spawn_call_id)),
991 (event_tool_call, format!("{}-event-call", event_call_id)),
992 ]);
993 conversation_store
994 .append_messages(&sub_thread_id, child_messages)
995 .await
996 .map_err(|e| Box::new(e) as BoxError)?;
997
998 let child_msg = InputMessage {
1000 content: InputMessageContent::User(UserContent::ToolResult(event_content)),
1001 group_id: sub_thread_id.clone(),
1002 metadata: None,
1003 synthetic: None,
1004 display_as: None,
1005 subscription: false,
1006 };
1007 message_sender
1008 .send_to_input_queue(child_msg, &event_call_id)
1009 .await
1010 .map_err(|e| Box::new(e) as BoxError)?;
1011
1012 if is_final_subscription {
1014 current_history
1015 .remove_subscription(&original_tool_call_id)
1016 .await
1017 .ok();
1018 }
1019
1020 return Ok(PrepareResult::Handled);
1021 }
1022 } else {
1023 user_content
1024 };
1025
1026 let subscription_tool_call_id = if is_subscription {
1029 match &content {
1030 UserContent::ToolResult(result) => Some(result.id.clone()),
1031 _ => None,
1032 }
1033 } else {
1034 None
1035 };
1036
1037 let infinity_msg = if let Some((tool_call_id, child_thread_id)) = subscription_event_meta {
1038 if let UserContent::ToolResult(result) = content {
1039 InfinityMessage::SubscriptionEvent {
1040 result: Box::new(result),
1041 tool_call_id,
1042 child_thread_id,
1043 invocation: subscription_invocation.map(Box::new),
1044 }
1045 } else {
1046 InfinityMessage::User { content }
1047 }
1048 } else {
1049 match content {
1050 UserContent::ToolResult(result) => InfinityMessage::ToolResult {
1051 result,
1052 display_segments: input_msg.display_as.clone(),
1053 },
1054 other => InfinityMessage::User { content: other },
1055 }
1056 };
1057
1058 let is_new = current_history.handle_content(infinity_msg, message_id.clone())?;
1059
1060 if !is_new {
1061 tracing::info!("Message was duplicate or ignored, skipping agent processing");
1062 return Ok(PrepareResult::Handled);
1063 }
1064
1065 if let Some(ref tool_call_id) = subscription_tool_call_id {
1067 tracing::info!(
1068 "Tracking subscription {} in thread {}",
1069 tool_call_id,
1070 current_history.thread_id
1071 );
1072 current_history.track_subscription(tool_call_id).await?;
1073 }
1074
1075 Ok(PrepareResult::Ready)
1076}
1077
1078pub fn input_event<C, S>(
1082 current_history: &HistoryManager<C, S>,
1083 input_msg: &InputMessage,
1084) -> Option<AgentEvent>
1085where
1086 C: ConversationStore,
1087 S: StateStore,
1088{
1089 if let Some(synth) = input_msg.synthetic.as_ref() {
1090 if let InputMessageContent::User(UserContent::ToolResult(res)) = &input_msg.content
1091 && let Some(ToolResultContent::Text(text)) = res.content.first()
1092 {
1093 let orig_call = current_history.get_history(true).into_iter().find(|h| {
1094 if let Message::Assistant { content, .. } = h
1095 && let Some(AssistantContent::ToolCall(c)) = content.first()
1096 {
1097 c.id == synth.tool_call_id()
1098 } else {
1099 false
1100 }
1101 });
1102
1103 if let Some(Message::Assistant { content, .. }) = orig_call
1104 && let Some(AssistantContent::ToolCall(c)) = content.first()
1105 {
1106 let name = if let SyntheticKind::Tagged(TaggedSyntheticKind::ThreadReport {
1107 ref child_thread_id,
1108 ..
1109 }) = *synth
1110 {
1111 format!("Report from child thread {}", child_thread_id)
1112 } else {
1113 format!("{}({})", c.function.name, c.function.arguments)
1114 };
1115 return Some(AgentEvent::SubscriptionEvent {
1116 name,
1117 text: text.text.clone(),
1118 });
1119 }
1120 }
1121 None
1122 } else if let InputMessageContent::User(UserContent::ToolResult(res)) = &input_msg.content
1123 && let Some(ToolResultContent::Text(text)) = res.content.first()
1124 {
1125 Some(AgentEvent::ToolResult {
1126 segments: rap_protocol::build_display_segments(
1127 input_msg.display_as.as_deref(),
1128 &text.text,
1129 ),
1130 })
1131 } else if let InputMessageContent::User(UserContent::Text(ref text)) = input_msg.content {
1132 let display_text = text.text.strip_prefix("<interrupt>").unwrap_or(&text.text);
1133 Some(AgentEvent::UserInput {
1134 text: display_text.to_owned(),
1135 })
1136 } else {
1137 None
1138 }
1139}
1140
1141pub const IMAGE_OMITTED_PLACEHOLDER: &str =
1149 "[image omitted: the current model does not support image inputs]";
1150
1151fn strip_image_tool_results(msg: &mut Message) {
1155 if let Message::User { content } = msg {
1156 for c in content.iter_mut() {
1157 if let UserContent::ToolResult(result) = c {
1158 for item in result.content.iter_mut() {
1159 if matches!(item, ToolResultContent::Image(_)) {
1160 *item =
1161 ToolResultContent::Text(infinity_provider_protocol::message::Text {
1162 text: IMAGE_OMITTED_PLACEHOLDER.to_owned(),
1163 });
1164 }
1165 }
1166 }
1167 }
1168 }
1169}
1170
1171#[expect(
1172 clippy::too_many_arguments,
1173 reason = "completion orchestration requires many parameters"
1174)]
1175pub fn run_completion<'a: 'b, 'b, P, C, S, M>(
1176 provider: &'a P,
1177 model_id: &'a str,
1178 supports_image_input: bool,
1182 history: &'a HistoryManager<C, S>,
1183 tool_names: &'a HashSet<String>,
1184 tools: &'a [ToolDefinition],
1185 tool_registry: &'a HashMap<String, &'a dyn Tool<M>>,
1186 tool_context: &'a ToolContext<M>,
1187 group_id: &'a ThreadId,
1188 message_id: &'a str,
1189 extra_system_prompt: Option<&'a str>,
1190 cancel_rx: tokio::sync::oneshot::Receiver<()>,
1191) -> impl futures_util::Stream<Item = Result<CompletionEvent, BoxError>> + 'b
1192where
1193 P: ModelProvider + ?Sized,
1194 C: ConversationStore,
1195 S: StateStore,
1196 M: InputSender + 'static,
1197{
1198 async_stream::try_stream! {
1199 let mut cancel_rx = cancel_rx;
1200 let mut completion_counter: usize = 0;
1201 let mut is_thinking = false;
1202 let mut retry_count = 0;
1203
1204 let preamble = {
1205 let base = include_str!("default_prompt.md");
1206 let thread_info = format!("\n\nYour current thread ID is `{}`. The root thread ID is `{}`.", history.thread_id, history.root_thread_id);
1207 match extra_system_prompt {
1208 Some(extra) => format!("{}{}\n\n{}", base, thread_info, extra),
1209 None => format!("{}{}", base, thread_info),
1210 }
1211 };
1212
1213 'outer: loop {
1214 assert!(
1216 history.turn_buffer_is_empty(),
1217 "bug: entered completion loop with un-flushed turn buffer"
1218 );
1219
1220 let stream_result = provider
1221 .invoke_model(model_id, CompletionRequest {
1222 preamble: Some(preamble.clone()),
1223 chat_history: history.get_history(supports_image_input),
1224 tools: tools.to_vec(),
1225 max_tokens: None,
1226 additional_params: None,
1227 });
1228
1229 let stream_result = tokio::select! {
1230 r = stream_result => {
1231 Ok(r)
1232 }
1233 _ = &mut cancel_rx => {
1234 tracing::info!("Completion cancelled during request initiation");
1235 return;
1236 }
1237 _ = tokio::time::sleep(Duration::from_secs(60)) => {
1238 if retry_count < 10 {
1239 yield CompletionEvent::Info("Stream error (timeout initiating request), retrying...".to_owned());
1240 retry_count += 1;
1241 continue 'outer;
1242 } else {
1243 Err(Into::<BoxError>::into("Timed out initiating request"))
1244 }
1245 }
1246 }?;
1247
1248 let mut llm_stream = match stream_result {
1249 Ok(s) => s,
1250 Err(e) => {
1251 let err_str = format!("{}", e);
1252 tracing::error!(error = %e, "Completion stream initiation failed");
1253
1254 if (err_str.contains("please wait before trying again") || err_str.contains("please try again")) && retry_count < 10 {
1255 tracing::warn!("Stream error (rate limit), retrying...");
1256
1257 yield CompletionEvent::Info("Stream error (rate limit), retrying after 30 seconds...".to_owned());
1258 tokio::select! {
1259 _ = tokio::time::sleep(Duration::from_secs(30)) => {}
1260 _ = &mut cancel_rx => {
1261 tracing::info!("Completion cancelled during retry wait");
1262 return;
1263 }
1264 }
1265 retry_count += 1;
1266 continue 'outer;
1267 } else if (err_str.contains("unexpected end of stream") || err_str.contains("unexpected error when processing the request") || err_str.contains("is unable to process your request")) && retry_count < 10 {
1268 tracing::warn!("Stream error ({err_str}), retrying...");
1269
1270 yield CompletionEvent::Info(format!("Stream error ({err_str}), retrying..."));
1271 tokio::select! {
1272 _ = tokio::time::sleep(Duration::from_secs(5)) => {}
1273 _ = &mut cancel_rx => {
1274 tracing::info!("Completion cancelled during retry wait");
1275 return;
1276 }
1277 }
1278 retry_count += 1;
1279 continue 'outer;
1280 } else {
1281 Err(Into::<BoxError>::into(e))?;
1282 unreachable!()
1283 }
1284 }
1285 };
1286
1287 let mut has_emitted_tool_call = false;
1288 let mut should_loop_back = false;
1289
1290 loop {
1291 let cancelled;
1295 let llm_next = tokio::select! {
1296 res = llm_stream.next() => { cancelled = false; Ok(res) },
1297 _ = &mut cancel_rx => { cancelled = true; Ok(None) },
1298 _ = tokio::time::sleep(Duration::from_secs(120)) => {
1299 cancelled = false;
1300 if retry_count < 10 {
1301 yield CompletionEvent::Info("Stream error (timeout), retrying...".to_owned());
1302 tracing::warn!("Stream stalled, discarding partial turn and retrying...");
1303 history.discard_turn();
1306 if is_thinking {
1307 is_thinking = false;
1308 yield CompletionEvent::ThinkingEnd;
1309 }
1310 retry_count += 1;
1311 continue 'outer;
1312 } else {
1313 history.flush_turn_trimming_reasoning();
1318 Err(Into::<BoxError>::into("Stream timed out"))
1319 }
1320 },
1321 }?;
1322
1323 if cancelled {
1324 tracing::info!("Completion cancelled");
1325 history.flush_turn_trimming_reasoning();
1329 if is_thinking {
1330 yield CompletionEvent::ThinkingEnd;
1331 }
1332 return;
1333 }
1334
1335 let Some(res) = llm_next else {
1336 if is_thinking {
1337 is_thinking = false;
1338 yield CompletionEvent::ThinkingEnd;
1339 }
1340 if retry_count < 10 {
1341 history.discard_turn();
1344 yield CompletionEvent::Info("Stream error (unexpected end), retrying...".to_owned());
1345 tracing::warn!("Stream ended unexpectedly, discarding partial turn and retrying...");
1346 tokio::time::sleep(Duration::from_secs(1)).await;
1347 retry_count += 1;
1348 continue 'outer;
1349 } else {
1350 history.flush_turn_trimming_reasoning();
1353 Err(Into::<BoxError>::into("Stream timed out"))?;
1354 unreachable!()
1355 }
1356 };
1357
1358 let chunk = match res {
1359 Ok(c) => {
1360 retry_count = 0;
1361 c
1362 },
1363 Err(e) => {
1364 if is_thinking {
1365 is_thinking = false;
1366 yield CompletionEvent::ThinkingEnd;
1367 }
1368 let err_str = format!("{}", e);
1369 if (err_str.contains("unexpected end of stream") || err_str.contains("unexpected error when processing the request")) && retry_count < 10 {
1370 history.discard_turn();
1372 yield CompletionEvent::Info("Stream error (unexpected end), retrying...".to_owned());
1373 tracing::warn!("Stream error (unexpected end), discarding partial turn and retrying...");
1374 tokio::time::sleep(Duration::from_secs(1)).await;
1375 retry_count += 1;
1376 continue 'outer;
1377 } else {
1378 history.flush_turn_trimming_reasoning();
1381 Err(Into::<BoxError>::into(e))?;
1382 unreachable!()
1383 }
1384 }
1385 };
1386
1387 if let StreamChunk::Reasoning(ref r) = chunk
1389 && r.first_signature().is_none() { continue; }
1390
1391 let completion_id = format!("{}-{}-completion-{}", group_id, message_id, completion_counter);
1392 completion_counter += 1;
1393
1394 if has_emitted_tool_call && !matches!(chunk, StreamChunk::Final(_)) {
1410 tracing::info!("Ignoring post-tool-call stream content: {:?}", chunk);
1411 } else {
1412 let tool_display_as = if let StreamChunk::ToolCall(ref call) = chunk {
1414 let ds = tool_registry
1415 .get(call.function.name.as_str())
1416 .and_then(|t| t.display_script().map(String::from));
1417 crate::tools::eval_display_script(ds.as_deref(), &call.function.arguments)
1418 } else {
1419 None
1420 };
1421 history.handle_completion(&chunk, completion_id, tool_display_as.clone());
1422 match chunk {
1423 StreamChunk::Text(text) => {
1424 if is_thinking {
1425 is_thinking = false;
1426 yield CompletionEvent::ThinkingEnd;
1427 }
1428 tracing::info!("[Text] {}", &text);
1429 yield CompletionEvent::TextChunk(text);
1430 }
1431 StreamChunk::ToolCall(call) => {
1432 if is_thinking {
1433 is_thinking = false;
1434 yield CompletionEvent::ThinkingEnd;
1435 }
1436 tracing::info!("[Tool Call: {} with arguments {}]", &call.function.name, &call.function.arguments);
1437
1438 has_emitted_tool_call = true;
1439 history.flush_turn();
1447 if call.function.name == "receive_event__injected" {
1448 let tool_result = InfinityMessage::ToolResult {
1449 result: ToolResult {
1450 id: call.id.clone(),
1451 call_id: call.call_id.clone(),
1452 content: vec![ToolResultContent::Text(infinity_provider_protocol::message::Text {
1453 text: format!("Error: you cannot directly invoke {}, invocations will automatically be injected when events arrive.", call.function.name),
1454 })],
1455 },
1456 display_segments: None,
1457 };
1458 history.handle_content(tool_result, format!("{}-unknown-tool", call.id))?;
1459 should_loop_back = true;
1460 continue;
1461 } else if !tool_names.contains(call.function.name.as_str()) {
1462 tracing::warn!("Unknown tool '{}' called, injecting error and retrying", call.function.name);
1464 let tool_result = InfinityMessage::ToolResult {
1465 result: ToolResult {
1466 id: call.id.clone(),
1467 call_id: call.call_id.clone(),
1468 content: vec![ToolResultContent::Text(infinity_provider_protocol::message::Text {
1469 text: format!("Error: tool '{}' does not exist", call.function.name),
1470 })],
1471 },
1472 display_segments: None,
1473 };
1474 history.handle_content(tool_result, format!("{}-unknown-tool", call.id))?;
1475 should_loop_back = true;
1476 continue;
1477 }
1478
1479 let tool = tool_registry.get(call.function.name.as_str()).expect("bug: tool not found in registry after call");
1485 if tool.supports_sync() {
1486 history.sync().await?; let res = tool.execute_synchronous(
1489 &call.function.arguments,
1490 &call.id,
1491 call.call_id.as_deref(),
1492 tool_context,
1493 ).await.expect("bug: synchronous tool execution failed");
1494
1495 yield CompletionEvent::SyncToolCall {
1496 tool_name: call.function.name.clone(),
1497 tool_args: call.function.arguments.clone(),
1498 display_as: tool_display_as,
1499 };
1500 yield CompletionEvent::SyncToolResult(res.clone());
1501
1502 let sync_id = format!("{}-sync-result-{}", call.id, completion_counter);
1503 completion_counter += 1;
1504 history.handle_content(
1505 InfinityMessage::ToolResult {
1506 result: res,
1507 display_segments: None,
1508 },
1509 sync_id,
1510 )?;
1511 should_loop_back = true;
1512 } else {
1513 yield CompletionEvent::Action(CompletionAction::ExecuteToolCall {
1514 tool_name: call.function.name.clone(),
1515 tool_args: call.function.arguments.clone(),
1516 tool_call_id: call.id.clone(),
1517 call_id: call.call_id.clone(),
1518 display_as: tool_display_as,
1519 });
1520 }
1521 }
1522 StreamChunk::ToolCallDelta { content, .. } => {
1523 match content {
1524 ToolCallDeltaContent::Name(n) => {
1525 yield CompletionEvent::ThinkingChunk(format!("Invoking tool: {}", n));
1526 }
1527 ToolCallDeltaContent::Delta(d) => {
1528 yield CompletionEvent::ThinkingChunk(d)
1529 }
1530 }
1531 }
1532 StreamChunk::Reasoning(reasoning) => {
1533 if is_thinking {
1534 is_thinking = false;
1535 yield CompletionEvent::ThinkingEnd;
1536 }
1537 tracing::info!("[Reasoning: {:?}]", reasoning.first_text());
1538 }
1539 StreamChunk::ReasoningDelta { text: reasoning, .. } => {
1540 if !is_thinking {
1541 is_thinking = true;
1542 yield CompletionEvent::ThinkingStart;
1543 }
1544 yield CompletionEvent::ThinkingChunk(reasoning);
1545 }
1546 StreamChunk::Final(r) => {
1547 if is_thinking {
1548 yield CompletionEvent::ThinkingEnd;
1549 }
1550 tracing::info!("Received final message");
1551 history.flush_turn();
1553 yield CompletionEvent::Action(CompletionAction::Done(r));
1554
1555 if should_loop_back {
1556 continue 'outer;
1557 } else {
1558 return;
1559 }
1560 }
1561 }
1562 }
1563 }
1564 }
1565 }
1566}
1567
1568pub async fn execute_action<M>(
1574 action: CompletionAction,
1575 tool_registry: &HashMap<String, &dyn Tool<M>>,
1576 tool_context: &ToolContext<M>,
1577) -> Result<(), BoxError>
1578where
1579 M: InputSender + 'static,
1580{
1581 match action {
1582 CompletionAction::Done(_) => {}
1583 CompletionAction::ExecuteToolCall {
1584 tool_name,
1585 tool_args,
1586 tool_call_id,
1587 call_id,
1588 display_as: _,
1589 } => {
1590 let tool = tool_registry
1591 .get(&tool_name)
1592 .expect("tool must exist after run_completion");
1593 tool.execute(tool_args, tool_call_id, call_id, tool_context)
1594 .await?;
1595 }
1596 }
1597 Ok(())
1598}
1599
1600pub(crate) async fn execute_action_with_error_result<M>(
1604 action: CompletionAction,
1605 tool_registry: &HashMap<String, &dyn Tool<M>>,
1606 tool_context: &ToolContext<M>,
1607) -> Result<(), crate::tools::ToolError>
1608where
1609 M: InputSender + 'static,
1610{
1611 let failed_tool_call = match &action {
1612 CompletionAction::ExecuteToolCall {
1613 tool_call_id,
1614 call_id,
1615 ..
1616 } => Some((tool_call_id.clone(), call_id.clone())),
1617 CompletionAction::Done(_) => None,
1618 };
1619
1620 if let Err(error) = execute_action(action, tool_registry, tool_context).await {
1621 if let Some((tool_call_id, call_id)) = failed_tool_call
1622 && let Err(send_error) = crate::tools::send_tool_error(
1623 tool_context,
1624 &tool_call_id,
1625 call_id,
1626 "Tool call failed",
1627 )
1628 .await
1629 {
1630 tracing::error!(
1631 "failed to send fallback result after tool execution error: {}",
1632 send_error
1633 );
1634 }
1635 return Err(error);
1636 }
1637
1638 Ok(())
1639}
1640
1641#[cfg(test)]
1642#[expect(
1643 clippy::collapsible_if,
1644 clippy::type_complexity,
1645 reason = "test readability"
1646)]
1647mod tests {
1648 use super::*;
1649 use crate::message::{
1650 InputMessage, InputMessageContent, OAuthRequired, SyntheticKind, TaggedSyntheticKind,
1651 };
1652 use crate::stores::{InMemoryConversationStore, InMemoryStateStore};
1653 use crate::traits::{ConversationStore, InputSender};
1654 use async_trait::async_trait;
1655 use infinity_provider_protocol::message::{
1656 AssistantContent, Message, ToolCall, ToolFunction, ToolResult, ToolResultContent,
1657 UserContent,
1658 };
1659 use std::collections::HashSet;
1660
1661 #[derive(Clone)]
1664 struct StubSender;
1665
1666 #[async_trait]
1667 impl InputSender for StubSender {
1668 type Error = std::io::Error;
1669 async fn send_to_input_queue(
1670 &self,
1671 _message: crate::message::InputMessage,
1672 _dedup_id: &str,
1673 ) -> Result<(), std::io::Error> {
1674 Ok(())
1675 }
1676 }
1677
1678 async fn make_history(
1681 store: &InMemoryConversationStore,
1682 initial_history: Vec<Message>,
1683 ) -> HistoryManager<InMemoryConversationStore, InMemoryStateStore> {
1684 let hm = HistoryManager::new_with_history(
1685 store.clone(),
1686 InMemoryStateStore::new(),
1687 "thread-1".into(),
1688 )
1689 .await
1690 .expect("create history manager");
1691 *hm.history.borrow_mut() = initial_history
1692 .into_iter()
1693 .map(InfinityMessage::from_message)
1694 .collect();
1695 hm
1696 }
1697
1698 fn user_text_msg(group_id: &str, text: &str) -> InputMessage {
1699 InputMessage {
1700 content: InputMessageContent::User(UserContent::text(text)),
1701 group_id: group_id.into(),
1702 metadata: None,
1703 synthetic: None,
1704 display_as: None,
1705 subscription: false,
1706 }
1707 }
1708
1709 fn tool_call_msg(id: &str, name: &str, args: serde_json::Value) -> Message {
1710 Message::Assistant {
1711 content: vec![AssistantContent::ToolCall(ToolCall {
1712 id: id.to_owned(),
1713 call_id: None,
1714 function: ToolFunction {
1715 name: name.to_owned(),
1716 arguments: args,
1717 },
1718 })],
1719 }
1720 }
1721
1722 fn tool_result_input(
1723 group_id: &str,
1724 tool_call_id: &str,
1725 result_text: &str,
1726 synthetic: Option<SyntheticKind>,
1727 ) -> InputMessage {
1728 InputMessage {
1729 content: InputMessageContent::User(UserContent::ToolResult(ToolResult {
1730 id: tool_call_id.to_owned(),
1731 call_id: None,
1732 content: vec![ToolResultContent::Text(
1733 infinity_provider_protocol::message::Text {
1734 text: result_text.to_owned(),
1735 },
1736 )],
1737 })),
1738 group_id: group_id.into(),
1739 metadata: None,
1740 synthetic,
1741 display_as: None,
1742 subscription: false,
1743 }
1744 }
1745
1746 #[tokio::test]
1749 async fn simple_user_message_on_empty_history() {
1750 let store = InMemoryConversationStore::new();
1751 let hm = make_history(&store, vec![]).await;
1752
1753 let result = prepare_input(
1754 user_text_msg("thread-1", "hello"),
1755 "msg-1".to_owned(),
1756 &hm,
1757 &store,
1758 &StubSender,
1759 )
1760 .await
1761 .expect("prepare input");
1762
1763 assert_eq!(result, PrepareResult::Ready);
1764 insta::assert_json_snapshot!(hm.history.into_inner());
1765 }
1766
1767 #[tokio::test]
1768 async fn consecutive_text_chunks_are_coalesced() {
1769 let store = InMemoryConversationStore::new();
1770 let hm = make_history(&store, vec![]).await;
1771
1772 let text_chunk = |s: &str| StreamChunk::Text(s.to_owned());
1773
1774 hm.handle_completion(&text_chunk("Hello"), "c-1".to_owned(), None);
1775 hm.handle_completion(&text_chunk(", "), "c-2".to_owned(), None);
1776 hm.handle_completion(&text_chunk("world"), "c-3".to_owned(), None);
1777
1778 assert_eq!(hm.turn_buffer.borrow().len(), 1);
1781 assert!(hm.history.borrow().is_empty());
1782
1783 hm.flush_turn();
1786 insta::assert_json_snapshot!(hm.pending_items.borrow().clone());
1787 insta::assert_json_snapshot!(hm.history.borrow().clone());
1788 }
1789
1790 #[tokio::test]
1791 async fn text_chunks_not_coalesced_across_non_text_item() {
1792 let store = InMemoryConversationStore::new();
1793 let hm = make_history(&store, vec![]).await;
1794
1795 let text_chunk = |s: &str| StreamChunk::Text(s.to_owned());
1796 let tool_call = StreamChunk::ToolCall(ToolCall {
1797 id: "tc-1".to_owned(),
1798 call_id: None,
1799 function: ToolFunction {
1800 name: "some_tool".to_owned(),
1801 arguments: serde_json::json!({}),
1802 },
1803 });
1804
1805 hm.handle_completion(&text_chunk("before"), "c-1".to_owned(), None);
1806 hm.handle_completion(&tool_call, "c-2".to_owned(), None);
1808 hm.handle_completion(&text_chunk("after"), "c-3".to_owned(), None);
1809
1810 hm.flush_turn();
1814 insta::assert_json_snapshot!(hm.pending_items.borrow().clone());
1815 insta::assert_json_snapshot!(hm.history.borrow().clone());
1816 }
1817
1818 #[tokio::test]
1819 async fn buffered_turn_is_not_committed_until_flush() {
1820 let store = InMemoryConversationStore::new();
1821 let hm = make_history(
1822 &store,
1823 vec![Message::User {
1824 content: vec![UserContent::text("do the thing")],
1825 }],
1826 )
1827 .await;
1828
1829 let reasoning = StreamChunk::Reasoning(
1830 infinity_provider_protocol::message::Reasoning::new_with_signature(
1831 "thinking",
1832 Some("sig".to_owned()),
1833 ),
1834 );
1835 let text = StreamChunk::Text("partial answer".to_owned());
1836 hm.handle_completion(&reasoning, "c-1".to_owned(), None);
1837 hm.handle_completion(&text, "c-2".to_owned(), None);
1838
1839 assert_eq!(hm.history.borrow().len(), 1);
1843 assert!(hm.pending_items.borrow().is_empty());
1844 assert_eq!(hm.turn_buffer.borrow().len(), 2);
1845
1846 hm.flush_turn();
1847
1848 assert_eq!(hm.history.borrow().len(), 3);
1851 assert_eq!(hm.pending_items.borrow().len(), 2);
1852 assert!(hm.turn_buffer.borrow().is_empty());
1853 }
1854
1855 #[tokio::test]
1856 async fn discard_turn_drops_buffer_leaving_history_clean() {
1857 let store = InMemoryConversationStore::new();
1858 let hm = make_history(
1859 &store,
1860 vec![Message::User {
1861 content: vec![UserContent::text("do the thing")],
1862 }],
1863 )
1864 .await;
1865
1866 let text = StreamChunk::Text("partial answer".to_owned());
1867 hm.handle_completion(&text, "c-1".to_owned(), None);
1868 assert_eq!(hm.turn_buffer.borrow().len(), 1);
1869
1870 hm.discard_turn();
1871
1872 assert!(hm.turn_buffer.borrow().is_empty());
1875 assert_eq!(hm.history.borrow().len(), 1);
1876 assert!(matches!(
1877 hm.history.borrow().last(),
1878 Some(InfinityMessage::User { .. })
1879 ));
1880 }
1881
1882 #[tokio::test]
1883 async fn flush_turn_trimming_reasoning_keeps_text_drops_trailing_reasoning() {
1884 let store = InMemoryConversationStore::new();
1885 let hm = make_history(
1886 &store,
1887 vec![Message::User {
1888 content: vec![UserContent::text("do the thing")],
1889 }],
1890 )
1891 .await;
1892
1893 let text = StreamChunk::Text("here is the answer".to_owned());
1896 let reasoning = StreamChunk::Reasoning(
1897 infinity_provider_protocol::message::Reasoning::new_with_signature(
1898 "still thinking",
1899 Some("sig".to_owned()),
1900 ),
1901 );
1902 hm.handle_completion(&text, "c-1".to_owned(), None);
1903 hm.handle_completion(&reasoning, "c-2".to_owned(), None);
1904 assert_eq!(hm.turn_buffer.borrow().len(), 2);
1905
1906 hm.flush_turn_trimming_reasoning();
1907
1908 assert!(hm.turn_buffer.borrow().is_empty());
1912 let history = hm.history.borrow();
1913 assert_eq!(history.len(), 2);
1914 assert!(matches!(
1915 history.last(),
1916 Some(InfinityMessage::Assistant {
1917 content: AssistantContent::Text(t),
1918 }) if t.text == "here is the answer"
1919 ));
1920 }
1921
1922 #[tokio::test]
1923 async fn flush_turn_trimming_reasoning_trims_interleaved_empty_text() {
1924 let store = InMemoryConversationStore::new();
1925 let hm = make_history(
1926 &store,
1927 vec![Message::User {
1928 content: vec![UserContent::text("do the thing")],
1929 }],
1930 )
1931 .await;
1932
1933 let reasoning = |s: &str| {
1937 StreamChunk::Reasoning(
1938 infinity_provider_protocol::message::Reasoning::new_with_signature(
1939 s,
1940 Some("sig".to_owned()),
1941 ),
1942 )
1943 };
1944 let empty_text = StreamChunk::Text(" ".to_owned());
1945 hm.handle_completion(&reasoning("first"), "c-1".to_owned(), None);
1946 hm.handle_completion(&empty_text, "c-2".to_owned(), None);
1947 hm.handle_completion(&reasoning("second"), "c-3".to_owned(), None);
1948
1949 hm.flush_turn_trimming_reasoning();
1950
1951 assert!(hm.turn_buffer.borrow().is_empty());
1952 let history = hm.history.borrow();
1953 assert_eq!(history.len(), 1);
1954 assert!(matches!(history.last(), Some(InfinityMessage::User { .. })));
1955 }
1956
1957 #[tokio::test]
1958 async fn current_turn_view_appends_buffer_to_history() {
1959 let store = InMemoryConversationStore::new();
1960 let hm = make_history(
1961 &store,
1962 vec![Message::User {
1963 content: vec![UserContent::text("hi")],
1964 }],
1965 )
1966 .await;
1967
1968 assert_eq!(hm.current_turn_view().len(), 1);
1970
1971 let text = StreamChunk::Text("streaming...".to_owned());
1972 hm.handle_completion(&text, "c-1".to_owned(), None);
1973
1974 let view = hm.current_turn_view();
1977 assert_eq!(view.len(), 2);
1978 assert_eq!(hm.history.borrow().len(), 1);
1979 assert!(matches!(
1980 view.last(),
1981 Some(InfinityMessage::Assistant { .. })
1982 ));
1983 }
1984
1985 #[tokio::test]
1986 async fn closed_thread_ignores() {
1987 let store = InMemoryConversationStore::new();
1988 store
1989 .ensure_root_thread(ThreadId::from_ref("thread-1"))
1990 .await
1991 .expect("testing");
1992 store
1993 .close_thread(ThreadId::from_ref("thread-1"))
1994 .await
1995 .expect("testing");
1996 let hm = make_history(&store, vec![]).await;
1997
1998 let result = prepare_input(
1999 user_text_msg("thread-1", "hello"),
2000 "msg-1".to_owned(),
2001 &hm,
2002 &store,
2003 &StubSender,
2004 )
2005 .await
2006 .expect("prepare input");
2007
2008 assert_eq!(result, PrepareResult::Handled);
2009 assert!(hm.history.into_inner().is_empty());
2010 }
2011
2012 #[tokio::test]
2013 async fn oauth_required_returns_auth_url() {
2014 let store = InMemoryConversationStore::new();
2015 let hm = make_history(&store, vec![]).await;
2016
2017 let input = InputMessage {
2018 content: InputMessageContent::OAuth(OAuthRequired {
2019 content_type: "oauth_required".to_owned(),
2020 id: "oauth-1".to_owned(),
2021 call_id: None,
2022 auth_url: "https://example.com/auth".to_owned(),
2023 }),
2024 group_id: "thread-1".into(),
2025 metadata: None,
2026 synthetic: None,
2027 display_as: None,
2028 subscription: false,
2029 };
2030
2031 let result = prepare_input(input, "msg-1".to_owned(), &hm, &store, &StubSender)
2032 .await
2033 .expect("prepare input");
2034
2035 insta::assert_json_snapshot!(result);
2036 assert!(hm.history.into_inner().is_empty());
2037 }
2038
2039 #[tokio::test]
2040 async fn duplicate_message_returns_handled() {
2041 let store = InMemoryConversationStore::new();
2042 let hm = make_history(&store, vec![]).await;
2043
2044 let r1 = prepare_input(
2046 user_text_msg("thread-1", "hello"),
2047 "msg-1".to_owned(),
2048 &hm,
2049 &store,
2050 &StubSender,
2051 )
2052 .await
2053 .expect("prepare input");
2054 assert!(matches!(r1, PrepareResult::Ready));
2055
2056 let r2 = prepare_input(
2058 user_text_msg("thread-1", "hello"),
2059 "msg-1".to_owned(),
2060 &hm,
2061 &store,
2062 &StubSender,
2063 )
2064 .await
2065 .expect("prepare input");
2066
2067 assert_eq!(r2, PrepareResult::Handled);
2068 insta::assert_json_snapshot!(hm.history.into_inner());
2070 }
2071
2072 #[tokio::test]
2073 async fn user_message_interrupts_pending_tool_call() {
2074 let store = InMemoryConversationStore::new();
2075 let initial = vec![
2077 Message::User {
2078 content: vec![UserContent::text("do something")],
2079 },
2080 tool_call_msg("tc-1", "some_tool", serde_json::json!({"x": 1})),
2081 ];
2082 let hm = make_history(&store, initial).await;
2083
2084 let result = prepare_input(
2085 user_text_msg("thread-1", "actually, never mind"),
2086 "msg-2".to_owned(),
2087 &hm,
2088 &store,
2089 &StubSender,
2090 )
2091 .await
2092 .expect("prepare input");
2093
2094 assert_eq!(result, PrepareResult::Ready);
2095 insta::assert_json_snapshot!(hm.history.into_inner());
2097 }
2098
2099 #[tokio::test]
2100 async fn tool_result_appended_to_history() {
2101 let store = InMemoryConversationStore::new();
2102 let initial = vec![
2103 Message::User {
2104 content: vec![UserContent::text("do something")],
2105 },
2106 tool_call_msg("tc-1", "some_tool", serde_json::json!({"x": 1})),
2107 ];
2108 let hm = make_history(&store, initial).await;
2109
2110 let input = tool_result_input("thread-1", "tc-1", "tool output", None);
2111
2112 let result = prepare_input(input, "msg-2".to_owned(), &hm, &store, &StubSender)
2113 .await
2114 .expect("prepare input");
2115
2116 assert_eq!(result, PrepareResult::Ready);
2117 insta::assert_json_snapshot!(hm.history.into_inner());
2118 }
2119
2120 #[tokio::test]
2121 async fn thread_report_synthetic_event() {
2122 let store = InMemoryConversationStore::new();
2123 let initial = vec![
2125 Message::User {
2126 content: vec![UserContent::text("subscribe")],
2127 },
2128 tool_call_msg(
2129 "tc-sub",
2130 "subscribe_tool",
2131 serde_json::json!({"topic": "events"}),
2132 ),
2133 Message::User {
2134 content: vec![UserContent::ToolResult(ToolResult {
2135 id: "tc-sub".to_owned(),
2136 call_id: None,
2137 content: vec![ToolResultContent::Text(
2138 infinity_provider_protocol::message::Text {
2139 text: "subscribed successfully".to_owned(),
2140 },
2141 )],
2142 })],
2143 },
2144 ];
2145 let hm = make_history(&store, initial).await;
2146
2147 let input = tool_result_input(
2148 "thread-1",
2149 "tc-sub",
2150 "thread report data",
2151 Some(SyntheticKind::Tagged(TaggedSyntheticKind::ThreadReport {
2152 tool_call_id: "tc-sub".to_owned(),
2153 child_thread_id: "thread-1".into(),
2154 })),
2155 );
2156
2157 let result = prepare_input(input, "msg-2".to_owned(), &hm, &store, &StubSender)
2158 .await
2159 .expect("prepare input");
2160
2161 assert_eq!(result, PrepareResult::Ready);
2162 insta::assert_json_snapshot!(
2164 hm.history.into_inner(),
2165 { "[3].result.id" => "[uuid]", "[3].invocation.id" => "[uuid]" }
2166 );
2167 }
2168
2169 #[tokio::test]
2170 async fn thread_report_tool_interruption() {
2171 let store = InMemoryConversationStore::new();
2172 let initial = vec![
2174 Message::User {
2175 content: vec![UserContent::text("subscribe")],
2176 },
2177 tool_call_msg(
2178 "tc-sub",
2179 "subscribe_tool",
2180 serde_json::json!({"topic": "events"}),
2181 ),
2182 ];
2183 let hm = make_history(&store, initial).await;
2184
2185 let input = tool_result_input(
2186 "thread-1",
2187 "tc-sub",
2188 "thread report data",
2189 Some(SyntheticKind::Tagged(TaggedSyntheticKind::ThreadReport {
2190 tool_call_id: "tc-sub".to_owned(),
2191 child_thread_id: "thread-1".into(),
2192 })),
2193 );
2194
2195 let result = prepare_input(input, "msg-2".to_owned(), &hm, &store, &StubSender)
2196 .await
2197 .expect("prepare input");
2198
2199 assert_eq!(result, PrepareResult::Ready);
2200 insta::assert_json_snapshot!(
2201 hm.history.into_inner(),
2202 { "[3].result.id" => "[uuid]", "[3].invocation.id" => "[uuid]" }
2203 );
2204 }
2205
2206 #[tokio::test]
2207 async fn subscription_event_spawned_thread() {
2208 let store = InMemoryConversationStore::new();
2209 let initial = vec![
2211 Message::User {
2212 content: vec![UserContent::text("subscribe")],
2213 },
2214 tool_call_msg(
2215 "tc-sub",
2216 "subscribe_tool",
2217 serde_json::json!({"topic": "events"}),
2218 ),
2219 Message::User {
2220 content: vec![UserContent::ToolResult(ToolResult {
2221 id: "tc-sub".to_owned(),
2222 call_id: None,
2223 content: vec![ToolResultContent::Text(
2224 infinity_provider_protocol::message::Text {
2225 text: "subscribed successfully".to_owned(),
2226 },
2227 )],
2228 })],
2229 },
2230 ];
2231 let hm = make_history(&store, initial).await;
2232
2233 let input = tool_result_input(
2234 "thread-1",
2235 "tc-sub",
2236 "event payload",
2237 Some(SyntheticKind::Tagged(
2238 TaggedSyntheticKind::SubscriptionEvent {
2239 tool_call_id: "tc-sub".to_owned(),
2240 associative: false,
2241 r#final: false,
2242 },
2243 )),
2244 );
2245
2246 let result = prepare_input(input, "msg-2".to_owned(), &hm, &store, &StubSender)
2247 .await
2248 .expect("prepare input");
2249
2250 assert_eq!(result, PrepareResult::Handled);
2251 assert_eq!(hm.thread_id.as_str(), "thread-1");
2252 }
2253
2254 #[tokio::test]
2255 async fn subscription_event_tool_interruption() {
2256 let store = InMemoryConversationStore::new();
2257 let initial = vec![
2259 Message::User {
2260 content: vec![UserContent::text("subscribe")],
2261 },
2262 tool_call_msg(
2263 "tc-sub",
2264 "subscribe_tool",
2265 serde_json::json!({"topic": "events"}),
2266 ),
2267 ];
2268 let hm = make_history(&store, initial).await;
2269
2270 let input = tool_result_input(
2271 "thread-1",
2272 "tc-sub",
2273 "event payload",
2274 Some(SyntheticKind::Tagged(
2275 TaggedSyntheticKind::SubscriptionEvent {
2276 tool_call_id: "tc-sub".to_owned(),
2277 associative: false,
2278 r#final: false,
2279 },
2280 )),
2281 );
2282
2283 let result = prepare_input(input, "msg-2".to_owned(), &hm, &store, &StubSender)
2284 .await
2285 .expect("prepare input");
2286
2287 assert_eq!(result, PrepareResult::Handled);
2288 assert_eq!(hm.thread_id.as_str(), "thread-1");
2289 }
2290
2291 #[tokio::test]
2292 async fn synthetic_with_missing_tool_call_returns_handled() {
2293 let store = InMemoryConversationStore::new();
2294 let hm = make_history(&store, vec![]).await;
2296
2297 let input = tool_result_input(
2298 "thread-1",
2299 "nonexistent-tc",
2300 "some data",
2301 Some(SyntheticKind::Tagged(
2302 TaggedSyntheticKind::SubscriptionEvent {
2303 tool_call_id: "nonexistent-tc".to_owned(),
2304 associative: false,
2305 r#final: false,
2306 },
2307 )),
2308 );
2309
2310 let result = prepare_input(input, "msg-1".to_owned(), &hm, &store, &StubSender)
2311 .await
2312 .expect("prepare input");
2313
2314 assert_eq!(result, PrepareResult::Handled);
2315 assert!(hm.history.into_inner().is_empty());
2316 }
2317
2318 #[tokio::test]
2319 async fn metadata_is_updated_before_processing() {
2320 let store = InMemoryConversationStore::new();
2321 let hm = make_history(&store, vec![]).await;
2322 assert!(hm.get_metadata().is_none());
2323
2324 let input = InputMessage {
2325 content: InputMessageContent::User(UserContent::text("hi")),
2326 group_id: "thread-1".into(),
2327 metadata: Some(serde_json::json!({"user_id": "u-123"})),
2328 synthetic: None,
2329 display_as: None,
2330 subscription: false,
2331 };
2332
2333 let _ = prepare_input(input, "msg-1".to_owned(), &hm, &store, &StubSender)
2334 .await
2335 .expect("prepare input");
2336
2337 insta::assert_json_snapshot!(hm.get_metadata());
2338 }
2339
2340 #[tokio::test]
2341 async fn associative_subscription_event_inlined() {
2342 let store = InMemoryConversationStore::new();
2343 let initial = vec![
2345 Message::User {
2346 content: vec![UserContent::text("run command")],
2347 },
2348 tool_call_msg(
2349 "tc-cmd",
2350 "execute_command",
2351 serde_json::json!({"command": "make build"}),
2352 ),
2353 Message::User {
2354 content: vec![UserContent::ToolResult(ToolResult {
2355 id: "tc-cmd".to_owned(),
2356 call_id: None,
2357 content: vec![ToolResultContent::Text(infinity_provider_protocol::message::Text {
2358 text: "Command is still running. Output will be streamed via subscription events.".to_owned(),
2359 })],
2360 })],
2361 },
2362 ];
2363 let hm = make_history(&store, initial).await;
2364
2365 let input = tool_result_input(
2366 "thread-1",
2367 "tc-cmd",
2368 "build output chunk\n[exit code: 0]",
2369 Some(SyntheticKind::Tagged(
2370 TaggedSyntheticKind::SubscriptionEvent {
2371 tool_call_id: "tc-cmd".to_owned(),
2372 associative: true,
2373 r#final: false,
2374 },
2375 )),
2376 );
2377
2378 let result = prepare_input(input, "msg-2".to_owned(), &hm, &store, &StubSender)
2379 .await
2380 .expect("prepare input");
2381
2382 assert_eq!(result, PrepareResult::Ready);
2383 assert_eq!(hm.thread_id.as_str(), "thread-1");
2385 insta::assert_json_snapshot!(
2387 hm.history.into_inner(),
2388 { "[3].result.id" => "[uuid]", "[3].invocation.id" => "[uuid]" }
2389 );
2390 }
2391
2392 #[tokio::test]
2393 async fn associative_subscription_event_tool_interruption() {
2394 let store = InMemoryConversationStore::new();
2395 let initial = vec![
2397 Message::User {
2398 content: vec![UserContent::text("run command")],
2399 },
2400 tool_call_msg(
2401 "tc-cmd",
2402 "execute_command",
2403 serde_json::json!({"command": "make build"}),
2404 ),
2405 ];
2406 let hm = make_history(&store, initial).await;
2407
2408 let input = tool_result_input(
2409 "thread-1",
2410 "tc-cmd",
2411 "build output chunk\n[exit code: 0]",
2412 Some(SyntheticKind::Tagged(
2413 TaggedSyntheticKind::SubscriptionEvent {
2414 tool_call_id: "tc-cmd".to_owned(),
2415 associative: true,
2416 r#final: false,
2417 },
2418 )),
2419 );
2420
2421 let result = prepare_input(input, "msg-2".to_owned(), &hm, &store, &StubSender)
2422 .await
2423 .expect("prepare input");
2424
2425 assert_eq!(result, PrepareResult::Ready);
2426 assert_eq!(hm.thread_id.as_str(), "thread-1");
2428 insta::assert_json_snapshot!(
2429 hm.history.into_inner(),
2430 { "[3].result.id" => "[uuid]", "[3].invocation.id" => "[uuid]" }
2431 );
2432 }
2433
2434 use std::collections::HashMap;
2436
2437 use super::{CompletionAction, CompletionEvent, HistoryManager};
2438 use crate::test_helpers::{mock_provider, mock_provider_with_image_support};
2439 use crate::tools::{Tool, ToolContext};
2440 use futures_util::StreamExt;
2441 use infinity_provider_protocol::ToolDefinition;
2442 use rap_protocol::ThreadId;
2443
2444 fn tool_context() -> ToolContext<StubSender> {
2445 ToolContext {
2446 message_sender: StubSender,
2447 group_id: "thread-1".into(),
2448 callback_url: String::new(),
2449 user_id: None,
2450 thread_stack: vec!["thread-1".into()],
2451 }
2452 }
2453
2454 fn no_tools() -> (
2455 HashSet<String>,
2456 Vec<ToolDefinition>,
2457 HashMap<String, &'static dyn Tool<StubSender>>,
2458 ) {
2459 (HashSet::new(), vec![], HashMap::new())
2460 }
2461
2462 #[tokio::test(flavor = "current_thread")]
2465 async fn basic_text_completion() {
2466 let local = tokio::task::LocalSet::new();
2467 local
2468 .run_until(async {
2469 let (provider, mut ctrl) = mock_provider();
2470 let convo_store = InMemoryConversationStore::new();
2471 let hm = make_history(
2472 &convo_store,
2473 vec![Message::User {
2474 content: vec![UserContent::text("hello")],
2475 }],
2476 )
2477 .await;
2478 let (tool_names, tool_defs, tool_registry) = no_tools();
2479 let ctx = tool_context();
2480 let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
2481
2482 let handle = tokio::task::spawn_local(async move {
2484 let thread_id = ThreadId::from("thread-1");
2485 let stream = run_completion(
2486 &provider,
2487 "mock",
2488 false,
2489 &hm,
2490 &tool_names,
2491 &tool_defs,
2492 &tool_registry,
2493 &ctx,
2494 &thread_id,
2495 "msg-1",
2496 None,
2497 cancel_rx,
2498 );
2499 tokio::pin!(stream);
2500 let mut texts = Vec::new();
2501 let mut got_done = false;
2502 while let Some(ev) = stream.next().await {
2503 match ev.expect("receive stream event") {
2504 CompletionEvent::TextChunk(t) => texts.push(t),
2505 CompletionEvent::Action(CompletionAction::Done(_)) => {
2506 got_done = true;
2507 }
2508 _ => {}
2509 }
2510 }
2511 (texts, got_done)
2512 });
2513
2514 let _req = ctrl.next_request().await;
2516 ctrl.send_text("Hello ");
2517 ctrl.send_text("world!");
2518 ctrl.finish();
2519
2520 let (texts, got_done) = handle.await.expect("await task handle");
2521 assert_eq!(texts, vec!["Hello ", "world!"]);
2522 assert!(got_done);
2523 })
2524 .await;
2525 }
2526
2527 #[tokio::test(flavor = "current_thread")]
2528 async fn cancellation_mid_stream() {
2529 let local = tokio::task::LocalSet::new();
2530 local
2531 .run_until(async {
2532 let (provider, mut ctrl) = mock_provider();
2533 let convo_store = InMemoryConversationStore::new();
2534 let hm = make_history(
2535 &convo_store,
2536 vec![Message::User {
2537 content: vec![UserContent::text("hello")],
2538 }],
2539 )
2540 .await;
2541 let (tool_names, tool_defs, tool_registry) = no_tools();
2542 let ctx = tool_context();
2543 let (cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
2544
2545 let handle = tokio::task::spawn_local(async move {
2546 let thread_id = ThreadId::from("thread-1");
2547 let stream = run_completion(
2548 &provider,
2549 "mock",
2550 false,
2551 &hm,
2552 &tool_names,
2553 &tool_defs,
2554 &tool_registry,
2555 &ctx,
2556 &thread_id,
2557 "msg-1",
2558 None,
2559 cancel_rx,
2560 );
2561 tokio::pin!(stream);
2562 let mut texts = Vec::new();
2563 let mut got_done = false;
2564 while let Some(ev) = stream.next().await {
2565 match ev.expect("receive stream event") {
2566 CompletionEvent::TextChunk(t) => texts.push(t),
2567 CompletionEvent::Action(CompletionAction::Done(_)) => {
2568 got_done = true;
2569 }
2570 _ => {}
2571 }
2572 }
2573 (texts, got_done)
2574 });
2575
2576 let _req = ctrl.next_request().await;
2577 ctrl.send_text("partial");
2578 tokio::task::yield_now().await;
2580 tokio::task::yield_now().await;
2581 cancel_tx.send(()).expect("send cancel signal");
2583
2584 let (texts, got_done) = handle.await.expect("await task handle");
2585 assert_eq!(texts, vec!["partial"]);
2586 assert!(!got_done);
2588 })
2589 .await;
2590 }
2591
2592 #[tokio::test(flavor = "current_thread")]
2593 async fn unknown_tool_injects_error_and_retries() {
2594 let local = tokio::task::LocalSet::new();
2595 local
2596 .run_until(async {
2597 let (provider, mut ctrl) = mock_provider();
2598 let convo_store = InMemoryConversationStore::new();
2599 let hm = make_history(
2600 &convo_store,
2601 vec![Message::User {
2602 content: vec![UserContent::text("do it")],
2603 }],
2604 )
2605 .await;
2606 let (tool_names, tool_defs, tool_registry) = no_tools();
2607 let ctx = tool_context();
2608 let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
2609
2610 let handle = tokio::task::spawn_local(async move {
2611 let thread_id = ThreadId::from("thread-1");
2612 let stream = run_completion(
2613 &provider,
2614 "mock",
2615 false,
2616 &hm,
2617 &tool_names,
2618 &tool_defs,
2619 &tool_registry,
2620 &ctx,
2621 &thread_id,
2622 "msg-1",
2623 None,
2624 cancel_rx,
2625 );
2626 tokio::pin!(stream);
2627 let mut texts = Vec::new();
2628 let mut got_done = false;
2629 while let Some(ev) = stream.next().await {
2630 match ev.expect("receive stream event") {
2631 CompletionEvent::TextChunk(t) => texts.push(t),
2632 CompletionEvent::Action(CompletionAction::Done(_)) => {
2633 got_done = true;
2634 }
2635 _ => {}
2636 }
2637 }
2638 (texts, got_done)
2639 });
2640
2641 let _req = ctrl.next_request().await;
2643 ctrl.send_tool_call("tc-1", "nonexistent_tool", serde_json::json!({}));
2644 ctrl.finish();
2645
2646 let req2 = ctrl.next_request().await;
2648 let last_msg = req2
2650 .chat_history
2651 .into_iter()
2652 .last()
2653 .expect("bug: chat history is empty");
2654 if let Message::User { content } = &last_msg {
2655 if let Some(UserContent::ToolResult(res)) = content.first() {
2656 if let Some(infinity_provider_protocol::message::ToolResultContent::Text(
2657 t,
2658 )) = res.content.first()
2659 {
2660 assert!(
2661 t.text.contains("does not exist"),
2662 "Expected error about nonexistent tool, got: {}",
2663 t.text
2664 );
2665 }
2666 }
2667 }
2668 ctrl.send_text("ok, done");
2669 ctrl.finish();
2670
2671 let (texts, got_done) = handle.await.expect("await task handle");
2672 assert_eq!(texts, vec!["ok, done"]);
2673 assert!(got_done);
2674 })
2675 .await;
2676 }
2677
2678 #[tokio::test(flavor = "current_thread")]
2679 async fn receive_event_injected_tool_rejected() {
2680 let local = tokio::task::LocalSet::new();
2681 local
2682 .run_until(async {
2683 let (provider, mut ctrl) = mock_provider();
2684 let convo_store = InMemoryConversationStore::new();
2685 let hm = make_history(
2686 &convo_store,
2687 vec![Message::User {
2688 content: vec![UserContent::text("do it")],
2689 }],
2690 )
2691 .await;
2692 let (tool_names, tool_defs, tool_registry) = no_tools();
2693 let ctx = tool_context();
2694 let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
2695
2696 let handle = tokio::task::spawn_local(async move {
2697 let thread_id = ThreadId::from("thread-1");
2698 let stream = run_completion(
2699 &provider,
2700 "mock",
2701 false,
2702 &hm,
2703 &tool_names,
2704 &tool_defs,
2705 &tool_registry,
2706 &ctx,
2707 &thread_id,
2708 "msg-1",
2709 None,
2710 cancel_rx,
2711 );
2712 tokio::pin!(stream);
2713 let mut got_done = false;
2714 while let Some(ev) = stream.next().await {
2715 if let Ok(CompletionEvent::Action(CompletionAction::Done(_))) = ev {
2716 got_done = true;
2717 }
2718 }
2719 got_done
2720 });
2721
2722 let _req = ctrl.next_request().await;
2724 ctrl.send_tool_call("tc-1", "receive_event__injected", serde_json::json!({}));
2725 ctrl.finish();
2726
2727 let _req2 = ctrl.next_request().await;
2729 ctrl.send_text("understood");
2730 ctrl.finish();
2731
2732 let got_done = handle.await.expect("await task handle");
2733 assert!(got_done);
2734 })
2735 .await;
2736 }
2737
2738 struct EchoSyncTool;
2741
2742 #[async_trait]
2743 impl Tool<StubSender> for EchoSyncTool {
2744 fn name(&self) -> &str {
2745 "echo_sync"
2746 }
2747 fn description(&self) -> &str {
2748 "echoes args"
2749 }
2750 fn parameters(&self) -> serde_json::Value {
2751 serde_json::json!({"type": "object", "properties": {"text": {"type": "string"}}, "required": ["text"]})
2752 }
2753 async fn execute(
2754 &self,
2755 _: serde_json::Value,
2756 _: String,
2757 _: Option<String>,
2758 _: &ToolContext<StubSender>,
2759 ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
2760 Ok(())
2761 }
2762 fn supports_sync(&self) -> bool {
2763 true
2764 }
2765 async fn execute_synchronous(
2766 &self,
2767 args: &serde_json::Value,
2768 id: &str,
2769 call_id: Option<&str>,
2770 _ctx: &ToolContext<StubSender>,
2771 ) -> Option<ToolResult> {
2772 let text = args["text"].as_str().unwrap_or("?");
2773 Some(ToolResult {
2774 id: id.to_owned(),
2775 call_id: call_id.map(String::from),
2776 content: vec![ToolResultContent::Text(
2777 infinity_provider_protocol::message::Text {
2778 text: format!("echo: {}", text),
2779 },
2780 )],
2781 })
2782 }
2783 }
2784
2785 static ECHO_TOOL: EchoSyncTool = EchoSyncTool;
2786
2787 #[tokio::test(flavor = "current_thread")]
2788 async fn sync_tool_loops_back_without_new_stream() {
2789 let local = tokio::task::LocalSet::new();
2790 local
2791 .run_until(async {
2792 let (provider, mut ctrl) = mock_provider();
2793 let convo_store = InMemoryConversationStore::new();
2794 let hm = make_history(
2795 &convo_store,
2796 vec![Message::User {
2797 content: vec![UserContent::text("echo something")],
2798 }],
2799 )
2800 .await;
2801
2802 let mut tool_names = HashSet::new();
2803 tool_names.insert("echo_sync".to_owned());
2804 let tool_defs = vec![ToolDefinition {
2805 name: "echo_sync".into(),
2806 description: "echoes".into(),
2807 parameters: serde_json::json!({"type": "object", "properties": {"text": {"type": "string"}}}),
2808 }];
2809 let mut tool_registry: HashMap<String, &dyn Tool<StubSender>> = HashMap::new();
2810 tool_registry.insert("echo_sync".into(), &ECHO_TOOL);
2811 let ctx = tool_context();
2812 let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
2813
2814 let handle = tokio::task::spawn_local(async move {
2815 let thread_id = ThreadId::from("thread-1");
2816 let stream = run_completion(
2817 &provider,
2818 "mock",
2819 false,
2820 &hm,
2821 &tool_names,
2822 &tool_defs,
2823 &tool_registry,
2824 &ctx,
2825 &thread_id,
2826 "msg-1",
2827 None,
2828 cancel_rx,
2829 );
2830 tokio::pin!(stream);
2831 let mut sync_calls = Vec::new();
2832 let mut sync_results = Vec::new();
2833 let mut texts = Vec::new();
2834 while let Some(ev) = stream.next().await {
2835 match ev.expect("receive stream event") {
2836 CompletionEvent::SyncToolCall { tool_name, .. } => sync_calls.push(tool_name),
2837 CompletionEvent::SyncToolResult(res) => {
2838 if let Some(ToolResultContent::Text(t)) = res.content.first() {
2839 sync_results.push(t.text.clone());
2840 }
2841 }
2842 CompletionEvent::TextChunk(t) => texts.push(t),
2843 _ => {}
2844 }
2845 }
2846 (sync_calls, sync_results, texts)
2847 });
2848
2849 let _req = ctrl.next_request().await;
2851 ctrl.send_tool_call("tc-1", "echo_sync", serde_json::json!({"text": "hi"}));
2852 ctrl.finish();
2853
2854 let req2 = ctrl.next_request().await;
2856 let has_echo = req2.chat_history.into_iter().any(|m| {
2858 if let Message::User { content } = &m {
2859 if let Some(UserContent::ToolResult(res)) = content.first() {
2860 if let Some(ToolResultContent::Text(t)) = res.content.first() {
2861 return t.text.contains("echo: hi");
2862 }
2863 }
2864 }
2865 false
2866 });
2867 assert!(has_echo, "Tool result should be in history for round 2");
2868 ctrl.send_text("done");
2869 ctrl.finish();
2870
2871 let (sync_calls, sync_results, texts) = handle.await.expect("await task handle");
2872 assert_eq!(sync_calls, vec!["echo_sync"]);
2873 assert_eq!(sync_results, vec!["echo: hi"]);
2874 assert_eq!(texts, vec!["done"]);
2875 })
2876 .await;
2877 }
2878
2879 fn image_tool_result_history() -> Vec<Message> {
2883 vec![
2884 Message::User {
2885 content: vec![UserContent::text("show me the logo")],
2886 },
2887 tool_call_msg(
2888 "tc-img",
2889 "read_file",
2890 serde_json::json!({"path": "logo.png"}),
2891 ),
2892 Message::User {
2893 content: vec![UserContent::ToolResult(ToolResult {
2894 id: "tc-img".to_owned(),
2895 call_id: None,
2896 content: vec![
2897 ToolResultContent::text("Read image file \"logo.png\""),
2898 ToolResultContent::Image(infinity_provider_protocol::message::Image {
2899 data: infinity_provider_protocol::message::ImageSource::Base64(
2900 "aGVsbG8=".to_owned(),
2901 ),
2902 media_type: Some(
2903 infinity_provider_protocol::message::ImageMediaType::PNG,
2904 ),
2905 }),
2906 ],
2907 })],
2908 },
2909 ]
2910 }
2911
2912 fn image_result_content(req: &CompletionRequest) -> Vec<ToolResultContent> {
2915 req.chat_history
2916 .iter()
2917 .find_map(|m| {
2918 if let Message::User { content } = m
2919 && let Some(UserContent::ToolResult(res)) = content.first()
2920 && res.id == "tc-img"
2921 {
2922 Some(res.content.to_vec())
2923 } else {
2924 None
2925 }
2926 })
2927 .expect("tool result tc-img should be in chat history")
2928 }
2929
2930 #[tokio::test(flavor = "current_thread")]
2931 async fn image_tool_result_sent_to_image_capable_model() {
2932 let local = tokio::task::LocalSet::new();
2933 local
2934 .run_until(async {
2935 let (provider, mut ctrl) = mock_provider_with_image_support(true);
2936 let convo_store = InMemoryConversationStore::new();
2937 let hm = make_history(&convo_store, image_tool_result_history()).await;
2938 let (tool_names, tool_defs, tool_registry) = no_tools();
2939 let ctx = tool_context();
2940 let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
2941
2942 let thread_id_owned = ThreadId::from("thread-1");
2943 let handle = tokio::task::spawn_local(async move {
2944 let stream = run_completion(
2945 &provider,
2946 "mock",
2947 true,
2948 &hm,
2949 &tool_names,
2950 &tool_defs,
2951 &tool_registry,
2952 &ctx,
2953 &thread_id_owned,
2954 "msg-1",
2955 None,
2956 cancel_rx,
2957 );
2958 tokio::pin!(stream);
2959 while stream.next().await.is_some() {}
2960 });
2961
2962 let req = ctrl.next_request().await;
2963 let content = image_result_content(&req);
2964 assert_eq!(content.len(), 2);
2965 assert!(
2966 matches!(&content[1], ToolResultContent::Image(img)
2967 if img.data == infinity_provider_protocol::message::ImageSource::Base64("aGVsbG8=".to_owned())),
2968 "image content should be passed through unchanged, got {content:?}"
2969 );
2970
2971 ctrl.send_text("nice logo");
2972 ctrl.finish();
2973 handle.await.expect("await task handle");
2974 })
2975 .await;
2976 }
2977
2978 #[tokio::test(flavor = "current_thread")]
2979 async fn image_tool_result_stripped_for_non_image_model() {
2980 let local = tokio::task::LocalSet::new();
2981 local
2982 .run_until(async {
2983 let (provider, mut ctrl) = mock_provider();
2985 let convo_store = InMemoryConversationStore::new();
2986 let hm = make_history(&convo_store, image_tool_result_history()).await;
2987 let (tool_names, tool_defs, tool_registry) = no_tools();
2988 let ctx = tool_context();
2989 let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
2990
2991 let handle = tokio::task::spawn_local(async move {
2992 let thread_id = ThreadId::from("thread-1");
2993 let stream = run_completion(
2994 &provider,
2995 "mock",
2996 false,
2997 &hm,
2998 &tool_names,
2999 &tool_defs,
3000 &tool_registry,
3001 &ctx,
3002 &thread_id,
3003 "msg-1",
3004 None,
3005 cancel_rx,
3006 );
3007 tokio::pin!(stream);
3008 while stream.next().await.is_some() {}
3009 });
3010
3011 let req = ctrl.next_request().await;
3012 let content = image_result_content(&req);
3013 assert_eq!(content.len(), 2);
3014 match &content[0] {
3015 ToolResultContent::Text(t) => {
3016 assert_eq!(t.text, "Read image file \"logo.png\"")
3017 }
3018 other => panic!("expected text content, got {other:?}"),
3019 }
3020 match &content[1] {
3021 ToolResultContent::Text(t) => assert_eq!(t.text, IMAGE_OMITTED_PLACEHOLDER),
3022 other => panic!("image should be replaced with placeholder, got {other:?}"),
3023 }
3024
3025 ctrl.send_text("cannot see images");
3026 ctrl.finish();
3027 handle.await.expect("await task handle");
3028 })
3029 .await;
3030 }
3031
3032 #[tokio::test(flavor = "current_thread")]
3033 async fn thinking_chunks_emitted() {
3034 let local = tokio::task::LocalSet::new();
3035 local
3036 .run_until(async {
3037 let (provider, mut ctrl) = mock_provider();
3038 let convo_store = InMemoryConversationStore::new();
3039 let hm = make_history(
3040 &convo_store,
3041 vec![Message::User {
3042 content: vec![UserContent::text("think hard")],
3043 }],
3044 )
3045 .await;
3046 let (tool_names, tool_defs, tool_registry) = no_tools();
3047 let ctx = tool_context();
3048 let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3049
3050 let handle = tokio::task::spawn_local(async move {
3051 let thread_id = ThreadId::from("thread-1");
3052 let stream = run_completion(
3053 &provider,
3054 "mock",
3055 false,
3056 &hm,
3057 &tool_names,
3058 &tool_defs,
3059 &tool_registry,
3060 &ctx,
3061 &thread_id,
3062 "msg-1",
3063 None,
3064 cancel_rx,
3065 );
3066 tokio::pin!(stream);
3067 let mut events = Vec::new();
3068 while let Some(ev) = stream.next().await {
3069 match ev.expect("receive stream event") {
3070 CompletionEvent::ThinkingStart => events.push("start".to_owned()),
3071 CompletionEvent::ThinkingEnd => events.push("end".to_owned()),
3072 CompletionEvent::ThinkingChunk(c) => {
3073 events.push(format!("think:{}", c))
3074 }
3075 CompletionEvent::TextChunk(t) => events.push(format!("text:{}", t)),
3076 _ => {}
3077 }
3078 }
3079 events
3080 });
3081
3082 let _req = ctrl.next_request().await;
3083 ctrl.send_chunk(StreamChunk::ReasoningDelta {
3084 id: None,
3085 text: "hmm".into(),
3086 });
3087 ctrl.send_chunk(StreamChunk::ReasoningDelta {
3088 id: None,
3089 text: "...".into(),
3090 });
3091 ctrl.send_text("answer");
3092 ctrl.finish();
3093
3094 let events = handle.await.expect("await task handle");
3095 assert_eq!(
3096 events,
3097 vec!["start", "think:hmm", "think:...", "end", "text:answer"]
3098 );
3099 })
3100 .await;
3101 }
3102
3103 #[tokio::test(flavor = "current_thread")]
3104 async fn async_tool_call_yields_execute_action() {
3105 let local = tokio::task::LocalSet::new();
3106 local
3107 .run_until(async {
3108 let (provider, mut ctrl) = mock_provider();
3109 let convo_store = InMemoryConversationStore::new();
3110 let hm = make_history(
3111 &convo_store,
3112 vec![Message::User {
3113 content: vec![UserContent::text("run tool")],
3114 }],
3115 )
3116 .await;
3117
3118 struct AsyncTool;
3119 #[async_trait]
3120 impl Tool<StubSender> for AsyncTool {
3121 fn name(&self) -> &str {
3122 "async_tool"
3123 }
3124 fn description(&self) -> &str {
3125 "async"
3126 }
3127 fn parameters(&self) -> serde_json::Value {
3128 serde_json::json!({"type": "object", "properties": {}})
3129 }
3130 async fn execute(
3131 &self,
3132 _: serde_json::Value,
3133 _: String,
3134 _: Option<String>,
3135 _: &ToolContext<StubSender>,
3136 ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
3137 Ok(())
3138 }
3139 }
3140 static ASYNC_TOOL: AsyncTool = AsyncTool;
3141
3142 let mut tool_names = HashSet::new();
3143 tool_names.insert("async_tool".to_owned());
3144 let tool_defs = vec![ToolDefinition {
3145 name: "async_tool".into(),
3146 description: "async".into(),
3147 parameters: serde_json::json!({"type": "object"}),
3148 }];
3149 let mut tool_registry: HashMap<String, &dyn Tool<StubSender>> = HashMap::new();
3150 tool_registry.insert("async_tool".into(), &ASYNC_TOOL);
3151 let ctx = tool_context();
3152 let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3153
3154 let handle = tokio::task::spawn_local(async move {
3155 let thread_id = ThreadId::from("thread-1");
3156 let stream = run_completion(
3157 &provider,
3158 "mock",
3159 false,
3160 &hm,
3161 &tool_names,
3162 &tool_defs,
3163 &tool_registry,
3164 &ctx,
3165 &thread_id,
3166 "msg-1",
3167 None,
3168 cancel_rx,
3169 );
3170 tokio::pin!(stream);
3171 let mut tool_call_name = None;
3172 while let Some(ev) = stream.next().await {
3173 if let Ok(CompletionEvent::Action(CompletionAction::ExecuteToolCall {
3174 tool_name,
3175 ..
3176 })) = ev
3177 {
3178 tool_call_name = Some(tool_name);
3179 }
3180 }
3181 tool_call_name
3182 });
3183
3184 let _req = ctrl.next_request().await;
3185 ctrl.send_tool_call("tc-1", "async_tool", serde_json::json!({"x": 1}));
3186 ctrl.finish();
3187
3188 let name = handle.await.expect("await task handle");
3189 assert_eq!(name, Some("async_tool".to_owned()));
3190 })
3191 .await;
3192 }
3193
3194 #[tokio::test(flavor = "current_thread")]
3206 async fn concurrent_tool_calls_suppress_trailing_stream_content() {
3207 use infinity_provider_protocol::StreamChunk;
3208 let local = tokio::task::LocalSet::new();
3209 local
3210 .run_until(async {
3211 let (provider, mut ctrl) = mock_provider();
3212 let convo_store = InMemoryConversationStore::new();
3213 let hm = make_history(
3214 &convo_store,
3215 vec![Message::User {
3216 content: vec![UserContent::text("run tools")],
3217 }],
3218 )
3219 .await;
3220
3221 struct AsyncTool;
3222 #[async_trait]
3223 impl Tool<StubSender> for AsyncTool {
3224 fn name(&self) -> &str {
3225 "async_tool"
3226 }
3227 fn description(&self) -> &str {
3228 "async"
3229 }
3230 fn parameters(&self) -> serde_json::Value {
3231 serde_json::json!({"type": "object", "properties": {}})
3232 }
3233 async fn execute(
3234 &self,
3235 _: serde_json::Value,
3236 _: String,
3237 _: Option<String>,
3238 _: &ToolContext<StubSender>,
3239 ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
3240 Ok(())
3241 }
3242 }
3243 static ASYNC_TOOL2: AsyncTool = AsyncTool;
3244
3245 let mut tool_names = HashSet::new();
3246 tool_names.insert("async_tool".to_owned());
3247 let tool_defs = vec![ToolDefinition {
3248 name: "async_tool".into(),
3249 description: "async".into(),
3250 parameters: serde_json::json!({"type": "object"}),
3251 }];
3252 let mut tool_registry: HashMap<String, &dyn Tool<StubSender>> = HashMap::new();
3253 tool_registry.insert("async_tool".into(), &ASYNC_TOOL2);
3254 let ctx = tool_context();
3255 let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3256
3257 let handle = tokio::task::spawn_local(async move {
3258 let thread_id = ThreadId::from("thread-1");
3259 let stream = run_completion(
3260 &provider,
3261 "mock",
3262 false,
3263 &hm,
3264 &tool_names,
3265 &tool_defs,
3266 &tool_registry,
3267 &ctx,
3268 &thread_id,
3269 "msg-1",
3270 None,
3271 cancel_rx,
3272 );
3273 tokio::pin!(stream);
3274 let mut tags: Vec<&'static str> = Vec::new();
3275 while let Some(ev) = stream.next().await {
3276 tags.push(match ev.expect("receive stream event") {
3277 CompletionEvent::Info(_) => "Info",
3278 CompletionEvent::TextChunk(_) => "Text",
3279 CompletionEvent::ThinkingStart => "ThinkingStart",
3280 CompletionEvent::ThinkingEnd => "ThinkingEnd",
3281 CompletionEvent::ThinkingChunk(_) => "ThinkingChunk",
3282 CompletionEvent::SyncToolCall { .. } => "SyncToolCall",
3283 CompletionEvent::SyncToolResult(_) => "SyncToolResult",
3284 CompletionEvent::Action(CompletionAction::ExecuteToolCall {
3285 ..
3286 }) => "ExecuteToolCall",
3287 CompletionEvent::Action(CompletionAction::Done(_)) => "Done",
3288 });
3289 }
3290 let last = hm.history.borrow().last().cloned();
3293 (tags, last)
3294 });
3295
3296 let _req = ctrl.next_request().await;
3297 ctrl.send_chunk(StreamChunk::ReasoningDelta {
3300 id: None,
3301 text: "planning the calls".into(),
3302 });
3303 ctrl.send_chunk(StreamChunk::Reasoning(
3304 infinity_provider_protocol::message::Reasoning::new_with_signature(
3305 "planning the calls",
3306 Some("sig-1".to_owned()),
3307 ),
3308 ));
3309 ctrl.send_chunk(StreamChunk::ToolCallDelta {
3310 id: "tc-1".into(),
3311 content: ToolCallDeltaContent::Name("async_tool".into()),
3312 });
3313 ctrl.send_tool_call("tc-1", "async_tool", serde_json::json!({"x": 1}));
3314 ctrl.send_chunk(StreamChunk::ToolCallDelta {
3317 id: "tc-2".into(),
3318 content: ToolCallDeltaContent::Name("async_tool".into()),
3319 });
3320 ctrl.send_chunk(StreamChunk::ToolCallDelta {
3321 id: "tc-2".into(),
3322 content: ToolCallDeltaContent::Delta("{\"x\":2}".into()),
3323 });
3324 ctrl.send_chunk(StreamChunk::ReasoningDelta {
3326 id: None,
3327 text: "now the second call".into(),
3328 });
3329 ctrl.send_chunk(StreamChunk::Reasoning(
3330 infinity_provider_protocol::message::Reasoning::new_with_signature(
3331 "now the second call",
3332 Some("sig-2".to_owned()),
3333 ),
3334 ));
3335 ctrl.send_tool_call("tc-2", "async_tool", serde_json::json!({"x": 2}));
3336 ctrl.finish();
3337
3338 let (tags, last_history) = handle.await.expect("await task handle");
3339
3340 let action_idx = tags
3341 .iter()
3342 .position(|t| *t == "ExecuteToolCall")
3343 .expect("first tool call should be executed");
3344 assert_eq!(
3345 tags[action_idx + 1..]
3346 .iter()
3347 .filter(|t| **t != "Done")
3348 .count(),
3349 0,
3350 "no display events may be emitted after the executed tool call \
3351 (they would stream as thinking before the tool result); got {tags:?}"
3352 );
3353
3354 match last_history {
3357 Some(InfinityMessage::ToolCall { call, .. }) => {
3358 assert_eq!(call.id, "tc-1");
3359 }
3360 other => panic!("history must end with the executed tool call, got {other:?}"),
3361 }
3362 })
3363 .await;
3364 }
3365
3366 #[tokio::test(flavor = "current_thread")]
3367 async fn stream_drop_triggers_retry() {
3368 let local = tokio::task::LocalSet::new();
3369 local
3370 .run_until(async {
3371 let (provider, mut ctrl) = mock_provider();
3373 let convo_store = InMemoryConversationStore::new();
3374 let hm = make_history(
3375 &convo_store,
3376 vec![Message::User {
3377 content: vec![UserContent::text("go")],
3378 }],
3379 )
3380 .await;
3381 let (tool_names, tool_defs, tool_registry) = no_tools();
3382 let ctx = tool_context();
3383 let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3384
3385 let handle = tokio::task::spawn_local(async move {
3386 let thread_id = ThreadId::from("thread-1");
3387 let stream = run_completion(
3388 &provider,
3389 "mock",
3390 false,
3391 &hm,
3392 &tool_names,
3393 &tool_defs,
3394 &tool_registry,
3395 &ctx,
3396 &thread_id,
3397 "msg-1",
3398 None,
3399 cancel_rx,
3400 );
3401 tokio::pin!(stream);
3402 let mut texts = Vec::new();
3403 let mut info_count = 0;
3404 while let Some(ev) = stream.next().await {
3405 match ev.expect("receive stream event") {
3406 CompletionEvent::TextChunk(t) => texts.push(t),
3407 CompletionEvent::Info(_) => info_count += 1,
3408 _ => {}
3409 }
3410 }
3411 (texts, info_count)
3412 });
3413
3414 let _req = ctrl.next_request().await;
3416 ctrl.drop_stream();
3417
3418 let _req2 = ctrl.next_request().await;
3420 ctrl.send_text("recovered");
3421 ctrl.finish();
3422
3423 let (texts, info_count) = handle.await.expect("await task handle");
3424 assert_eq!(texts, vec!["recovered"]);
3425 assert!(
3426 info_count >= 1,
3427 "Should have emitted at least one Info about retry"
3428 );
3429 })
3430 .await;
3431 }
3432
3433 #[tokio::test(flavor = "current_thread")]
3434 async fn cancellation_during_thinking_emits_thinking_end() {
3435 let local = tokio::task::LocalSet::new();
3436 local
3437 .run_until(async {
3438 let (provider, mut ctrl) = mock_provider();
3439 let convo_store = InMemoryConversationStore::new();
3440 let hm = make_history(
3441 &convo_store,
3442 vec![Message::User {
3443 content: vec![UserContent::text("think")],
3444 }],
3445 )
3446 .await;
3447 let (tool_names, tool_defs, tool_registry) = no_tools();
3448 let ctx = tool_context();
3449 let (cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3450
3451 let handle = tokio::task::spawn_local(async move {
3452 let thread_id = ThreadId::from("thread-1");
3453 let stream = run_completion(
3454 &provider,
3455 "mock",
3456 false,
3457 &hm,
3458 &tool_names,
3459 &tool_defs,
3460 &tool_registry,
3461 &ctx,
3462 &thread_id,
3463 "msg-1",
3464 None,
3465 cancel_rx,
3466 );
3467 tokio::pin!(stream);
3468 let mut events = Vec::new();
3469 while let Some(ev) = stream.next().await {
3470 match ev.expect("receive stream event") {
3471 CompletionEvent::ThinkingStart => events.push("start"),
3472 CompletionEvent::ThinkingEnd => events.push("end"),
3473 CompletionEvent::ThinkingChunk(_) => events.push("chunk"),
3474 _ => {}
3475 }
3476 }
3477 events
3478 });
3479
3480 let _req = ctrl.next_request().await;
3481 ctrl.send_chunk(StreamChunk::ReasoningDelta {
3482 id: None,
3483 text: "deep thought".into(),
3484 });
3485 tokio::task::yield_now().await;
3486 tokio::task::yield_now().await;
3487 cancel_tx.send(()).expect("send cancel signal");
3488
3489 let events = handle.await.expect("await task handle");
3490 assert!(events.contains(&"start"));
3492 assert!(
3493 events.last() == Some(&"end"),
3494 "ThinkingEnd should be emitted on cancel, got: {:?}",
3495 events
3496 );
3497 })
3498 .await;
3499 }
3500
3501 #[tokio::test(flavor = "current_thread")]
3502 async fn cancellation_after_reasoning_does_not_commit_trailing_reasoning() {
3503 let local = tokio::task::LocalSet::new();
3508 local
3509 .run_until(async {
3510 let (provider, mut ctrl) = mock_provider();
3511 let convo_store = InMemoryConversationStore::new();
3512 let hm = std::rc::Rc::new(
3513 make_history(
3514 &convo_store,
3515 vec![Message::User {
3516 content: vec![UserContent::text("do the thing")],
3517 }],
3518 )
3519 .await,
3520 );
3521 let (tool_names, tool_defs, tool_registry) = no_tools();
3522 let ctx = tool_context();
3523 let (cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3524
3525 let hm_task = hm.clone();
3526 let thread_id_owned = ThreadId::from("thread-1");
3527 let handle = tokio::task::spawn_local(async move {
3528 let stream = run_completion(
3529 &provider,
3530 "mock",
3531 false,
3532 &hm_task,
3533 &tool_names,
3534 &tool_defs,
3535 &tool_registry,
3536 &ctx,
3537 &thread_id_owned,
3538 "msg-1",
3539 None,
3540 cancel_rx,
3541 );
3542 tokio::pin!(stream);
3543 while stream.next().await.is_some() {}
3544 });
3545
3546 let _req = ctrl.next_request().await;
3547 ctrl.send_text("here is the answer");
3548 ctrl.send_chunk(StreamChunk::Reasoning(
3550 infinity_provider_protocol::message::Reasoning::new_with_signature(
3551 "still thinking",
3552 Some("sig".to_owned()),
3553 ),
3554 ));
3555 tokio::task::yield_now().await;
3556 tokio::task::yield_now().await;
3557 cancel_tx.send(()).expect("send cancel signal");
3558
3559 handle.await.expect("await task handle");
3560
3561 let history = hm.history.borrow();
3564 assert!(
3565 !matches!(
3566 history.last(),
3567 Some(InfinityMessage::Assistant {
3568 content: AssistantContent::Reasoning(_),
3569 })
3570 ),
3571 "history must not end on a reasoning block, got: {:?}",
3572 history.last()
3573 );
3574 assert!(matches!(
3575 history.last(),
3576 Some(InfinityMessage::Assistant {
3577 content: AssistantContent::Text(t),
3578 }) if t.text == "here is the answer"
3579 ));
3580 })
3581 .await;
3582 }
3583
3584 #[tokio::test(flavor = "current_thread")]
3585 async fn multiple_sync_tool_calls_chain() {
3586 let local = tokio::task::LocalSet::new();
3587 local
3588 .run_until(async {
3589 let (provider, mut ctrl) = mock_provider();
3591 let convo_store = InMemoryConversationStore::new();
3592 let hm = make_history(
3593 &convo_store,
3594 vec![Message::User {
3595 content: vec![UserContent::text("echo twice")],
3596 }],
3597 )
3598 .await;
3599
3600 let mut tool_names = HashSet::new();
3601 tool_names.insert("echo_sync".to_owned());
3602 let tool_defs = vec![ToolDefinition {
3603 name: "echo_sync".into(),
3604 description: "echoes".into(),
3605 parameters: serde_json::json!({"type": "object", "properties": {"text": {"type": "string"}}}),
3606 }];
3607 let mut tool_registry: HashMap<String, &dyn Tool<StubSender>> = HashMap::new();
3608 tool_registry.insert("echo_sync".into(), &ECHO_TOOL);
3609 let ctx = tool_context();
3610 let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3611
3612 let handle = tokio::task::spawn_local(async move {
3613 let thread_id = ThreadId::from("thread-1");
3614 let stream = run_completion(
3615 &provider,
3616 "mock",
3617 false,
3618 &hm,
3619 &tool_names,
3620 &tool_defs,
3621 &tool_registry,
3622 &ctx,
3623 &thread_id,
3624 "msg-1",
3625 None,
3626 cancel_rx,
3627 );
3628 tokio::pin!(stream);
3629 let mut sync_calls = 0;
3630 let mut texts = Vec::new();
3631 while let Some(ev) = stream.next().await {
3632 match ev.expect("receive stream event") {
3633 CompletionEvent::SyncToolCall { .. } => sync_calls += 1,
3634 CompletionEvent::TextChunk(t) => texts.push(t),
3635 _ => {}
3636 }
3637 }
3638 (sync_calls, texts)
3639 });
3640
3641 let _req = ctrl.next_request().await;
3643 ctrl.send_tool_call("tc-1", "echo_sync", serde_json::json!({"text": "first"}));
3644 ctrl.finish();
3645
3646 let _req2 = ctrl.next_request().await;
3648 ctrl.send_tool_call("tc-2", "echo_sync", serde_json::json!({"text": "second"}));
3649 ctrl.finish();
3650
3651 let _req3 = ctrl.next_request().await;
3653 ctrl.send_text("all done");
3654 ctrl.finish();
3655
3656 let (sync_calls, texts) = handle.await.expect("await task handle");
3657 assert_eq!(sync_calls, 2);
3658 assert_eq!(texts, vec!["all done"]);
3659 })
3660 .await;
3661 }
3662
3663 #[tokio::test(flavor = "current_thread")]
3664 async fn broken_stream_discards_partial_turn_before_retry() {
3665 let local = tokio::task::LocalSet::new();
3672 local
3673 .run_until(async {
3674 let (provider, mut ctrl) = mock_provider();
3675 let convo_store = InMemoryConversationStore::new();
3676 let hm = make_history(
3677 &convo_store,
3678 vec![Message::User {
3679 content: vec![UserContent::text("do the thing")],
3680 }],
3681 )
3682 .await;
3683 let (tool_names, tool_defs, tool_registry) = no_tools();
3684 let ctx = tool_context();
3685 let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3686
3687 let handle = tokio::task::spawn_local(async move {
3688 let thread_id = ThreadId::from("thread-1");
3689 let stream = run_completion(
3690 &provider,
3691 "mock",
3692 false,
3693 &hm,
3694 &tool_names,
3695 &tool_defs,
3696 &tool_registry,
3697 &ctx,
3698 &thread_id,
3699 "msg-1",
3700 None,
3701 cancel_rx,
3702 );
3703 tokio::pin!(stream);
3704 let mut texts = Vec::new();
3705 let mut got_done = false;
3706 while let Some(ev) = stream.next().await {
3707 match ev.expect("receive stream event") {
3708 CompletionEvent::TextChunk(t) => texts.push(t),
3709 CompletionEvent::Action(CompletionAction::Done(_)) => got_done = true,
3710 _ => {}
3711 }
3712 }
3713 (texts, got_done)
3714 });
3715
3716 let _req = ctrl.next_request().await;
3718 ctrl.send_text("I'll take a look");
3719 tokio::task::yield_now().await;
3720 tokio::task::yield_now().await;
3721 ctrl.drop_stream();
3722
3723 let req2 = ctrl.next_request().await;
3725 let last_msg = req2
3726 .chat_history
3727 .into_iter()
3728 .last()
3729 .expect("bug: chat history is empty");
3730 assert!(
3731 matches!(last_msg, Message::User { .. }),
3732 "retry request must not end on an assistant message, got: {last_msg:?}"
3733 );
3734
3735 ctrl.send_text("done");
3736 ctrl.finish();
3737
3738 let (texts, got_done) = handle.await.expect("await task handle");
3739 assert!(got_done, "completion should finish after a clean retry");
3740 assert_eq!(texts, vec!["I'll take a look", "done"]);
3741 })
3742 .await;
3743 }
3744
3745 #[tokio::test(flavor = "current_thread")]
3746 async fn async_tool_call_flushes_tool_call_before_turn_ends() {
3747 let local = tokio::task::LocalSet::new();
3752 local
3753 .run_until(async {
3754 let (provider, mut ctrl) = mock_provider();
3755 let convo_store = InMemoryConversationStore::new();
3756 let hm = std::rc::Rc::new(
3757 make_history(
3758 &convo_store,
3759 vec![Message::User {
3760 content: vec![UserContent::text("use the tool")],
3761 }],
3762 )
3763 .await,
3764 );
3765
3766 struct AsyncTool;
3767 #[async_trait]
3768 impl Tool<StubSender> for AsyncTool {
3769 fn name(&self) -> &str {
3770 "do_async"
3771 }
3772 fn description(&self) -> &str {
3773 "async"
3774 }
3775 fn parameters(&self) -> serde_json::Value {
3776 serde_json::json!({"type": "object"})
3777 }
3778 async fn execute(
3779 &self,
3780 _: serde_json::Value,
3781 _: String,
3782 _: Option<String>,
3783 _: &ToolContext<StubSender>,
3784 ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
3785 Ok(())
3786 }
3787 }
3788 static ASYNC_TOOL: AsyncTool = AsyncTool;
3789
3790 let mut tool_names = HashSet::new();
3791 tool_names.insert("do_async".to_owned());
3792 let tool_defs = vec![ToolDefinition {
3793 name: "do_async".into(),
3794 description: "async".into(),
3795 parameters: serde_json::json!({"type": "object"}),
3796 }];
3797 let mut tool_registry: HashMap<String, &dyn Tool<StubSender>> = HashMap::new();
3798 tool_registry.insert("do_async".into(), &ASYNC_TOOL);
3799 let ctx = tool_context();
3800 let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3801
3802 let hm_task = hm.clone();
3803 let thread_id_owned = ThreadId::from("thread-1");
3804 let handle = tokio::task::spawn_local(async move {
3805 let stream = run_completion(
3806 &provider,
3807 "mock",
3808 false,
3809 &hm_task,
3810 &tool_names,
3811 &tool_defs,
3812 &tool_registry,
3813 &ctx,
3814 &thread_id_owned,
3815 "msg-1",
3816 None,
3817 cancel_rx,
3818 );
3819 tokio::pin!(stream);
3820 let mut got_action = false;
3821 while let Some(ev) = stream.next().await {
3822 if let CompletionEvent::Action(CompletionAction::ExecuteToolCall {
3823 ..
3824 }) = ev.expect("receive stream event")
3825 {
3826 got_action = true;
3827 }
3828 }
3829 got_action
3830 });
3831
3832 let _req = ctrl.next_request().await;
3833 ctrl.send_text("let me check");
3834 ctrl.send_tool_call("tc-1", "do_async", serde_json::json!({}));
3835 ctrl.finish();
3836
3837 let got_action = handle.await.expect("await task handle");
3838 assert!(got_action, "async tool call should yield ExecuteToolCall");
3839
3840 let history = hm.history.borrow();
3843 assert!(matches!(
3844 history.last(),
3845 Some(InfinityMessage::ToolCall { .. })
3846 ));
3847 assert!(
3848 history
3849 .iter()
3850 .any(|m| matches!(m, InfinityMessage::Assistant { .. })),
3851 "preceding assistant text should be committed too"
3852 );
3853 assert!(
3854 hm.turn_buffer.borrow().is_empty(),
3855 "buffer must be flushed when the turn ends"
3856 );
3857 })
3858 .await;
3859 }
3860
3861 #[tokio::test(flavor = "current_thread")]
3862 async fn sync_tool_loop_back_includes_flushed_tool_call() {
3863 let local = tokio::task::LocalSet::new();
3867 local
3868 .run_until(async {
3869 let (provider, mut ctrl) = mock_provider();
3870 let convo_store = InMemoryConversationStore::new();
3871 let hm = make_history(
3872 &convo_store,
3873 vec![Message::User {
3874 content: vec![UserContent::text("echo something")],
3875 }],
3876 )
3877 .await;
3878
3879 let mut tool_names = HashSet::new();
3880 tool_names.insert("echo_sync".to_owned());
3881 let tool_defs = vec![ToolDefinition {
3882 name: "echo_sync".into(),
3883 description: "echoes".into(),
3884 parameters: serde_json::json!({"type": "object", "properties": {"text": {"type": "string"}}}),
3885 }];
3886 let mut tool_registry: HashMap<String, &dyn Tool<StubSender>> = HashMap::new();
3887 tool_registry.insert("echo_sync".into(), &ECHO_TOOL);
3888 let ctx = tool_context();
3889 let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
3890
3891 let handle = tokio::task::spawn_local(async move {
3892 let thread_id = ThreadId::from("thread-1");
3893 let stream = run_completion(
3894 &provider, "mock", false, &hm, &tool_names, &tool_defs, &tool_registry,
3895 &ctx, &thread_id, "msg-1", None, cancel_rx,
3896 );
3897 tokio::pin!(stream);
3898 let mut got_done = false;
3899 while let Some(ev) = stream.next().await {
3900 if let CompletionEvent::Action(CompletionAction::Done(_)) =
3901 ev.expect("receive stream event")
3902 {
3903 got_done = true;
3904 }
3905 }
3906 got_done
3907 });
3908
3909 let _req = ctrl.next_request().await;
3911 ctrl.send_tool_call("tc-1", "echo_sync", serde_json::json!({"text": "hi"}));
3912 ctrl.finish();
3913
3914 let req2 = ctrl.next_request().await;
3917 let msgs: Vec<Message> = req2.chat_history.into_iter().collect();
3918 let has_tool_call = msgs.iter().any(|m| matches!(
3919 m,
3920 Message::Assistant { content, .. }
3921 if matches!(content.first(), Some(AssistantContent::ToolCall(_)))
3922 ));
3923 let has_tool_result = msgs.iter().any(|m| matches!(
3924 m,
3925 Message::User { content }
3926 if matches!(content.first(), Some(UserContent::ToolResult(_)))
3927 ));
3928 assert!(has_tool_call, "loop-back must include the assistant tool call");
3929 assert!(has_tool_result, "loop-back must include the injected tool result");
3930
3931 ctrl.send_text("all done");
3932 ctrl.finish();
3933
3934 let got_done = handle.await.expect("await task handle");
3935 assert!(got_done);
3936 })
3937 .await;
3938 }
3939
3940 #[derive(Clone)]
3945 struct CapturingSender(tokio::sync::mpsc::UnboundedSender<InputMessage>);
3946
3947 #[async_trait]
3948 impl InputSender for CapturingSender {
3949 type Error = std::io::Error;
3950 async fn send_to_input_queue(
3951 &self,
3952 message: InputMessage,
3953 _dedup_id: &str,
3954 ) -> Result<(), std::io::Error> {
3955 self.0.send(message).expect("testing");
3956 Ok(())
3957 }
3958 }
3959
3960 #[derive(Clone)]
3961 struct StubHttp;
3962
3963 #[async_trait]
3964 impl rap_client::http::HttpClient for StubHttp {
3965 type Error = std::io::Error;
3966 async fn post(&self, _: &str, _: &str) -> Result<u16, std::io::Error> {
3967 Ok(200)
3968 }
3969 async fn get(&self, _: &str) -> Result<(u16, Vec<u8>), std::io::Error> {
3970 Ok((200, Vec::new()))
3971 }
3972 }
3973
3974 struct FailingTool;
3976
3977 #[async_trait]
3978 impl Tool<CapturingSender> for FailingTool {
3979 fn name(&self) -> &str {
3980 "failing_tool"
3981 }
3982 fn description(&self) -> &str {
3983 "always fails"
3984 }
3985 fn parameters(&self) -> serde_json::Value {
3986 serde_json::json!({"type":"object","properties":{}})
3987 }
3988 async fn execute(
3989 &self,
3990 _: serde_json::Value,
3991 _: String,
3992 _: Option<String>,
3993 _: &ToolContext<CapturingSender>,
3994 ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
3995 Err("stub".into())
3996 }
3997 }
3998
3999 fn capturing_ctx(
4000 group_id: &str,
4001 thread_stack: Vec<ThreadId>,
4002 ) -> (
4003 ToolContext<CapturingSender>,
4004 tokio::sync::mpsc::UnboundedReceiver<InputMessage>,
4005 ) {
4006 let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
4007 (
4008 ToolContext {
4009 message_sender: CapturingSender(tx),
4010 group_id: group_id.into(),
4011 callback_url: String::new(),
4012 user_id: None,
4013 thread_stack,
4014 },
4015 rx,
4016 )
4017 }
4018
4019 fn captured_tool_result(
4021 rx: &mut tokio::sync::mpsc::UnboundedReceiver<InputMessage>,
4022 ) -> (String, Option<String>, String) {
4023 let message = rx.try_recv().expect("a tool result should be enqueued");
4024 let InputMessageContent::User(UserContent::ToolResult(result)) = message.content else {
4025 panic!("expected a tool result");
4026 };
4027 let Some(ToolResultContent::Text(text)) = result.content.first() else {
4028 panic!("expected text content");
4029 };
4030 (result.id.clone(), result.call_id.clone(), text.text.clone())
4031 }
4032
4033 #[tokio::test]
4036 async fn close_thread_missing_id_enqueues_error_result() {
4037 let (context, mut rx) = capturing_ctx("child", vec!["root".into(), "child".into()]);
4038 let tool = crate::tools::thread::CloseThreadTool::<_, StubHttp> {
4039 conversation_store: InMemoryConversationStore::new(),
4040 rap_notifier: None,
4041 };
4042
4043 tool.execute(
4044 serde_json::json!({}),
4045 "tc-close".into(),
4046 Some("call-close".into()),
4047 &context,
4048 )
4049 .await
4050 .expect("validation error should be returned as a tool result");
4051
4052 let (id, call_id, text) = captured_tool_result(&mut rx);
4053 assert_eq!(id, "tc-close");
4054 assert_eq!(call_id.as_deref(), Some("call-close"));
4055 assert_eq!(text, "Error: thread_id is required");
4056 }
4057
4058 #[tokio::test]
4061 async fn failed_tool_execution_enqueues_fallback_result() {
4062 let (context, mut rx) = capturing_ctx("t1", vec!["t1".into()]);
4063 let tool = FailingTool;
4064 let tools: HashMap<String, &dyn Tool<CapturingSender>> =
4065 HashMap::from([("failing_tool".into(), &tool as &dyn Tool<CapturingSender>)]);
4066 let action = CompletionAction::ExecuteToolCall {
4067 tool_name: "failing_tool".into(),
4068 tool_args: serde_json::json!({}),
4069 tool_call_id: "tc-1".into(),
4070 call_id: Some("call-1".into()),
4071 display_as: None,
4072 };
4073
4074 let error = execute_action_with_error_result(action, &tools, &context)
4075 .await
4076 .expect_err("tool execution should fail");
4077 assert_eq!(error.to_string(), "stub");
4078
4079 let (id, call_id, text) = captured_tool_result(&mut rx);
4080 assert_eq!(id, "tc-1");
4081 assert_eq!(call_id.as_deref(), Some("call-1"));
4082 assert_eq!(text, "Error: Tool call failed");
4083 }
4084
4085 #[tokio::test]
4088 async fn user_choice_input_surfaces_prompt() {
4089 let store = InMemoryConversationStore::new();
4090 let hm = make_history(&store, vec![]).await;
4091
4092 let input = InputMessage {
4093 content: InputMessageContent::UserChoice(crate::message::UserChoiceRequired {
4094 content_type: "user_choice_required".to_owned(),
4095 id: "choice-1".to_owned(),
4096 call_id: None,
4097 prompt: "pick one".to_owned(),
4098 choices: vec!["a".to_owned(), "b".to_owned()],
4099 default: 0,
4100 response_url: "https://example.com/choice".to_owned(),
4101 }),
4102 group_id: "thread-1".into(),
4103 metadata: None,
4104 synthetic: None,
4105 display_as: None,
4106 subscription: false,
4107 };
4108
4109 let result = prepare_input(input, "msg-1".to_owned(), &hm, &store, &StubSender)
4110 .await
4111 .expect("prepare input");
4112
4113 let PrepareResult::UserChoiceRequired {
4114 id,
4115 prompt,
4116 choices,
4117 default,
4118 response_url,
4119 } = result
4120 else {
4121 panic!("expected UserChoiceRequired, got {result:?}");
4122 };
4123 assert_eq!(id, "choice-1");
4124 assert_eq!(prompt, "pick one");
4125 assert_eq!(choices, vec!["a".to_owned(), "b".to_owned()]);
4126 assert_eq!(default, 0);
4127 assert_eq!(response_url, "https://example.com/choice");
4128 assert!(hm.history.into_inner().is_empty());
4129 }
4130}