@@ -133,18 +133,19 @@ function client.process_stream(stream_response: StreamInput, callbacks: StreamCa
133133 table.insert (tool_calls , part )
134134 on_tool_call (part )
135135 elseif part .text then
136+ local text = tostring (part .text )
136137 if part .thought == true then
137- on_thinking (part . text )
138+ on_thinking (text )
138139 else
139- full_content = full_content .. part . text
140- on_content (part . text )
140+ full_content = full_content .. text
141+ on_content (text )
141142 end
142143 end
143144 end
144145 end
145146
146147 if candidate .finishReason then
147- finish_reason = candidate .finishReason
148+ finish_reason = tostring ( candidate .finishReason )
148149 end
149150 end
150151
173174--- Process a streaming response and send chunks via output.streamer.
174175--- Returns an aggregated Google-like response compatible with map_success_response().
175176local function handle_stream_response (response , http_options )
176- local streamer = output .streamer (
177- http_options .stream_reply_to ,
178- http_options .stream_topic ,
179- http_options .stream_buffer_size or 10
180- )
177+ local reply_to : string ? = http_options .stream_reply_to
178+ local topic : string ? = http_options .stream_topic
179+ local streamer = output .streamer (reply_to , topic , http_options .stream_buffer_size or 10 )
181180 if not streamer then
182181 return nil , {
183182 status_code = 500 ,
@@ -191,40 +190,42 @@ local function handle_stream_response(response, http_options)
191190 local usage_metadata : any = nil
192191 local response_metadata : table = {}
193192
193+ local callbacks : StreamCallbacks = {
194+ on_content = function (chunk : string )
195+ full_content = full_content .. chunk
196+ streamer :buffer_content (chunk )
197+ end ,
198+
199+ on_tool_call = function (tool_part : any )
200+ table.insert (tool_call_parts , tool_part )
201+ if tool_part .functionCall then
202+ streamer :send_tool_call (
203+ tostring (tool_part .functionCall .name ),
204+ tostring (tool_part .functionCall .args or " {}" ),
205+ tostring (tool_part .functionCall .name )
206+ )
207+ end
208+ end ,
209+
210+ on_thinking = function (text : string )
211+ streamer :send_thinking (text )
212+ end ,
213+
214+ on_error = function (error_info : any )
215+ streamer :send_error (" server_error" , tostring (error_info .message ), nil )
216+ end ,
217+
218+ on_done = function (result : StreamResult )
219+ streamer :flush ()
220+ finish_reason = result .finish_reason
221+ usage_metadata = result .usage
222+ response_metadata = result .metadata
223+ end ,
224+ }
225+
194226 local _ , stream_err = client .process_stream (
195227 { stream = response .stream , metadata = {} },
196- {
197- on_content = function (chunk : string )
198- full_content = full_content .. chunk
199- streamer :buffer_content (chunk )
200- end ,
201-
202- on_tool_call = function (tool_part : any )
203- table.insert (tool_call_parts , tool_part )
204- if tool_part .functionCall then
205- streamer :send_tool_call (
206- tool_part .functionCall .name ,
207- tool_part .functionCall .args or {},
208- tool_part .functionCall .name
209- )
210- end
211- end ,
212-
213- on_thinking = function (text : string )
214- streamer :send_thinking (text )
215- end ,
216-
217- on_error = function (error_info : any )
218- streamer :send_error (" server_error" , tostring (error_info .message ))
219- end ,
220-
221- on_done = function (result : StreamResult )
222- streamer :flush ()
223- finish_reason = result .finish_reason
224- usage_metadata = result .usage
225- response_metadata = result .metadata
226- end
227- }
228+ callbacks
228229 )
229230
230231 if stream_err then
0 commit comments