@@ -741,69 +741,70 @@ def _append_tool_call_result(tool_call_id: str, content: str) -> None:
741741 if isinstance (resp , CallToolResult ):
742742 res = resp
743743 _final_resp = resp
744- if isinstance ( res .content [ 0 ], TextContent ) :
744+ if not res .content :
745745 _append_tool_call_result (
746746 func_tool_id ,
747- res . content [ 0 ]. text ,
747+ "The tool returned no content." ,
748748 )
749- elif isinstance (res .content [0 ], ImageContent ):
750- # Cache the image instead of sending directly
751- cached_img = tool_image_cache .save_image (
752- base64_data = res .content [0 ].data ,
753- tool_call_id = func_tool_id ,
754- tool_name = func_tool_name ,
755- index = 0 ,
756- mime_type = res .content [0 ].mimeType or "image/png" ,
757- )
758- _append_tool_call_result (
759- func_tool_id ,
760- (
761- f"Image returned and cached at path='{ cached_img .file_path } '. "
762- f"Review the image below. Use send_message_to_user to send it to the user if satisfied, "
763- f"with type='image' and path='{ cached_img .file_path } '."
764- ),
765- )
766- # Yield image info for LLM visibility (will be handled in step())
767- yield _HandleFunctionToolsResult .from_cached_image (
768- cached_img
769- )
770- elif isinstance (res .content [0 ], EmbeddedResource ):
771- resource = res .content [0 ].resource
772- if isinstance (resource , TextResourceContents ):
773- _append_tool_call_result (
774- func_tool_id ,
775- resource .text ,
776- )
777- elif (
778- isinstance (resource , BlobResourceContents )
779- and resource .mimeType
780- and resource .mimeType .startswith ("image/" )
781- ):
749+ continue
750+
751+ result_parts : list [str ] = []
752+ for index , content_item in enumerate (res .content ):
753+ if isinstance (content_item , TextContent ):
754+ result_parts .append (content_item .text )
755+ elif isinstance (content_item , ImageContent ):
782756 # Cache the image instead of sending directly
783757 cached_img = tool_image_cache .save_image (
784- base64_data = resource . blob ,
758+ base64_data = content_item . data ,
785759 tool_call_id = func_tool_id ,
786760 tool_name = func_tool_name ,
787- index = 0 ,
788- mime_type = resource .mimeType ,
761+ index = index ,
762+ mime_type = content_item .mimeType or "image/png" ,
789763 )
790- _append_tool_call_result (
791- func_tool_id ,
792- (
793- f"Image returned and cached at path='{ cached_img .file_path } '. "
794- f"Review the image below. Use send_message_to_user to send it to the user if satisfied, "
795- f"with type='image' and path='{ cached_img .file_path } '."
796- ),
764+ result_parts .append (
765+ f"Image returned and cached at path='{ cached_img .file_path } '. "
766+ f"Review the image below. Use send_message_to_user to send it to the user if satisfied, "
767+ f"with type='image' and path='{ cached_img .file_path } '."
797768 )
798- # Yield image info for LLM visibility
769+ # Yield image info for LLM visibility (will be handled in step())
799770 yield _HandleFunctionToolsResult .from_cached_image (
800771 cached_img
801772 )
802- else :
803- _append_tool_call_result (
804- func_tool_id ,
805- "The tool has returned a data type that is not supported." ,
806- )
773+ elif isinstance (content_item , EmbeddedResource ):
774+ resource = content_item .resource
775+ if isinstance (resource , TextResourceContents ):
776+ result_parts .append (resource .text )
777+ elif (
778+ isinstance (resource , BlobResourceContents )
779+ and resource .mimeType
780+ and resource .mimeType .startswith ("image/" )
781+ ):
782+ # Cache the image instead of sending directly
783+ cached_img = tool_image_cache .save_image (
784+ base64_data = resource .blob ,
785+ tool_call_id = func_tool_id ,
786+ tool_name = func_tool_name ,
787+ index = index ,
788+ mime_type = resource .mimeType ,
789+ )
790+ result_parts .append (
791+ f"Image returned and cached at path='{ cached_img .file_path } '. "
792+ f"Review the image below. Use send_message_to_user to send it to the user if satisfied, "
793+ f"with type='image' and path='{ cached_img .file_path } '."
794+ )
795+ # Yield image info for LLM visibility
796+ yield _HandleFunctionToolsResult .from_cached_image (
797+ cached_img
798+ )
799+ else :
800+ result_parts .append (
801+ "The tool has returned a data type that is not supported."
802+ )
803+ if result_parts :
804+ _append_tool_call_result (
805+ func_tool_id ,
806+ "\n \n " .join (result_parts ),
807+ )
807808
808809 elif resp is None :
809810 # Tool 直接请求发送消息给用户
0 commit comments