@@ -207,6 +207,10 @@ struct ChatContext<'o, W> {
207207 tool_use_recursions : u32 ,
208208 current_user_input_id : Option < String > ,
209209 tool_use_events : Vec < ToolUseEventBuilder > ,
210+
211+ /// Whether or not an unexpected end of chat stream was encountered while consuming the model
212+ /// response's tool use data. Pair of (tool_use_id, name) for the tool that was being received.
213+ encountered_tool_use_eos : Option < ( String , String ) > ,
210214}
211215
212216impl < ' o , W > ChatContext < ' o , W >
@@ -228,6 +232,7 @@ where
228232 tool_use_recursions : 0 ,
229233 current_user_input_id : None ,
230234 tool_use_events : vec ! [ ] ,
235+ encountered_tool_use_eos : None ,
231236 }
232237 }
233238
@@ -381,19 +386,43 @@ Hi, I'm <g>Amazon Q</g>. Ask me anything.
381386 cursor:: Show
382387 ) ?;
383388 }
384- execute ! (
385- self . output,
386- style:: SetAttribute ( Attribute :: Bold ) ,
387- style:: SetForegroundColor ( Color :: Red ) ,
388- style:: Print ( format!(
389- "We're having trouble responding right now, please try again later: {:?}" ,
390- err
391- ) ) ,
392- style:: SetForegroundColor ( Color :: Reset ) ,
393- style:: SetAttribute ( Attribute :: Reset ) ,
394- ) ?;
395- if self . conversation_state . next_message . is_none ( ) {
396- self . conversation_state . history . pop_back ( ) ;
389+ match err {
390+ parser:: RecvError :: UnexpectedToolUseEos {
391+ tool_use_id,
392+ name,
393+ message,
394+ } => {
395+ error ! (
396+ tool_use_id,
397+ name, "The response stream ended before the entire tool use was received"
398+ ) ;
399+ self . conversation_state . push_assistant_message ( * message) ;
400+ self . encountered_tool_use_eos = Some ( ( tool_use_id, name) ) ;
401+ if self . is_interactive {
402+ execute ! ( self . output, cursor:: Hide ) ?;
403+ self . spinner = Some ( Spinner :: new (
404+ Spinners :: Dots ,
405+ "The generated tool use was too large, trying to divide up the work..."
406+ . to_string ( ) ,
407+ ) ) ;
408+ }
409+ } ,
410+ err => {
411+ execute ! (
412+ self . output,
413+ style:: SetAttribute ( Attribute :: Bold ) ,
414+ style:: SetForegroundColor ( Color :: Red ) ,
415+ style:: Print ( format!(
416+ "We're having trouble responding right now, please try again later: {:?}" ,
417+ err
418+ ) ) ,
419+ style:: SetForegroundColor ( Color :: Reset ) ,
420+ style:: SetAttribute ( Attribute :: Reset ) ,
421+ ) ?;
422+ if self . conversation_state . next_message . is_none ( ) {
423+ self . conversation_state . history . pop_back ( ) ;
424+ }
425+ } ,
397426 }
398427 break ;
399428 } ,
@@ -490,6 +519,23 @@ Hi, I'm <g>Amazon Q</g>. Ask me anything.
490519 sigint_recver : & mut tokio:: sync:: mpsc:: Receiver < ( ) > ,
491520 should_terminate : & Arc < AtomicBool > ,
492521 ) -> Result < Option < SendMessageOutput > , error:: PromptAndSendError > {
522+ if let Some ( ( tool_use_id, _) ) = self . encountered_tool_use_eos . take ( ) {
523+ let tool_results = vec ! [ ToolResult {
524+ tool_use_id,
525+ content: vec![ ToolResultContentBlock :: Text (
526+ "The generated tool was too large, try again but this time split up the work between multiple tool uses" . to_string( ) ,
527+ ) ] ,
528+ status: ToolResultStatus :: Error ,
529+ } ] ;
530+ self . conversation_state . add_tool_results ( tool_results) ;
531+ self . send_tool_use_telemetry ( ) . await ;
532+ return Ok ( Some (
533+ self . client
534+ . send_message ( self . conversation_state . as_sendable_conversation_state ( ) )
535+ . await ?,
536+ ) ) ;
537+ }
538+
493539 loop {
494540 // Tool uses that need to be executed.
495541 let mut queued_tools: Vec < ( String , Tool ) > = Vec :: new ( ) ;
0 commit comments