@@ -93,7 +93,7 @@ def parse_respond_response(response)
9393
9494 Message . new (
9595 role : :assistant ,
96- content : all_output_text ( outputs ) ,
96+ content : all_output_content ( outputs ) ,
9797 tool_calls : parse_response_tool_calls ( outputs ) ,
9898 input_tokens : data [ 'usage' ] [ 'input_tokens' ] ,
9999 output_tokens : data [ 'usage' ] [ 'output_tokens' ] ,
@@ -103,6 +103,47 @@ def parse_respond_response(response)
103103 )
104104 end
105105
106+ def all_output_content ( outputs )
107+ text_content = extract_text_content ( outputs )
108+ image_outputs = outputs . select { |o | o [ 'type' ] == 'image_generation_call' }
109+
110+ return text_content unless image_outputs . any?
111+
112+ build_content_with_images ( text_content , image_outputs )
113+ end
114+
115+ private
116+
117+ def extract_text_content ( outputs )
118+ outputs . select { |o | o [ 'type' ] == 'message' } . flat_map do |o |
119+ o [ 'content' ] . filter_map do |c |
120+ c [ 'type' ] == 'output_text' && c [ 'text' ]
121+ end
122+ end . join ( "\n " )
123+ end
124+
125+ def build_content_with_images ( text_content , image_outputs )
126+ content = RubyLLM ::Content . new ( text_content )
127+ image_outputs . each do |output |
128+ attach_image_to_content ( content , output )
129+ end
130+ content
131+ end
132+
133+ def attach_image_to_content ( content , output )
134+ image_data = output [ 'result' ]
135+ output_format = output [ 'output_format' ] || 'png'
136+ mime_type = "image/#{ output_format } "
137+
138+ content . attach (
139+ RubyLLM ::ImageAttachment . new (
140+ data : image_data ,
141+ mime_type : mime_type ,
142+ model_id : nil
143+ )
144+ )
145+ end
146+
106147 def all_output_text ( outputs )
107148 outputs . select { |o | o [ 'type' ] == 'message' } . flat_map do |o |
108149 o [ 'content' ] . filter_map do |c |
0 commit comments