@@ -248,14 +248,6 @@ def configured_model
248248 end
249249
250250 describe "#talk" do
251- before do
252- transport . stub (
253- "POST" , "/v1/chat/completions" ,
254- fixture : "openai/chat_completions.sse" ,
255- headers : { "content-type" => "text/event-stream" }
256- )
257- end
258-
259251 context "when configured with a class-level stream object" do
260252 let ( :stream ) { StringIO . new }
261253 let ( :agent_class ) do
@@ -267,6 +259,11 @@ def configured_model
267259 let ( :agent ) { agent_class . new ( llm , model : "gpt-4.1" ) }
268260
269261 before do
262+ transport . stub (
263+ "POST" , "/v1/chat/completions" ,
264+ fixture : "openai/chat_completions.sse" ,
265+ headers : { "content-type" => "text/event-stream" }
266+ )
270267 agent . talk ( "Say hello" )
271268 end
272269
@@ -287,6 +284,11 @@ def configured_model
287284 let ( :agent ) { agent_class . new ( llm , model : "gpt-4.1" , stream : override_stream ) }
288285
289286 before do
287+ transport . stub (
288+ "POST" , "/v1/chat/completions" ,
289+ fixture : "openai/chat_completions.sse" ,
290+ headers : { "content-type" => "text/event-stream" }
291+ )
290292 agent . talk ( "Say hello" )
291293 end
292294
@@ -298,6 +300,102 @@ def configured_model
298300 expect ( default_stream . string ) . must_equal ""
299301 end
300302 end
303+
304+ context "when auto-executing streamed tool calls" do
305+ let ( :stream ) do
306+ Class . new ( LLM ::Stream ) do
307+ attr_reader :returns
308+ def initialize
309+ @returns = [ ]
310+ end
311+ def on_tool_return ( tool , result )
312+ @returns << [ tool . name , result . name , result . value ]
313+ end
314+ end . new
315+ end
316+ let ( :tool ) do
317+ Class . new ( LLM ::Tool ) do
318+ name "system"
319+ parameter :command , String , "The command"
320+ required %i[ command ]
321+ def call ( command :)
322+ { "success" => command == "date" ? "2025-08-24" : false }
323+ end
324+ end
325+ end
326+ let ( :agent ) { LLM ::Agent . new ( llm , model : "gpt-4.1" , stream :, tools : [ tool ] ) }
327+
328+ before do
329+ transport
330+ . stub (
331+ "POST" , "/v1/chat/completions" ,
332+ fixture : "openai/chat_completions_tool.sse" ,
333+ headers : { "content-type" => "text/event-stream" }
334+ )
335+ . stub (
336+ "POST" , "/v1/chat/completions" ,
337+ fixture : "openai/chat_completions_tool_result.sse" ,
338+ headers : { "content-type" => "text/event-stream" }
339+ )
340+ agent . talk ( "Run date" )
341+ end
342+
343+ it "emits tool returns without manual stream queueing" do
344+ expect ( stream . returns ) . must_equal [ [ "system" , "system" , { "success" => "2025-08-24" } ] ]
345+ end
346+ end
347+
348+ context "when confirming streamed tool calls" do
349+ let ( :stream ) do
350+ Class . new ( LLM ::Stream ) do
351+ attr_reader :returns
352+ def initialize
353+ @returns = [ ]
354+ end
355+ def on_tool_return ( tool , result )
356+ @returns << [ tool . name , result . name , result . value ]
357+ end
358+ end . new
359+ end
360+ let ( :tool ) do
361+ Class . new ( LLM ::Tool ) do
362+ name "system"
363+ parameter :command , String , "The command"
364+ required %i[ command ]
365+ def call ( command :)
366+ { "success" => command == "date" ? "2025-08-24" : false }
367+ end
368+ end
369+ end
370+ let ( :agent_class ) do
371+ Class . new ( LLM ::Agent ) do
372+ confirm "system"
373+ def on_tool_confirmation ( fn , strategy )
374+ fn . spawn ( strategy ) . wait
375+ end
376+ end
377+ end
378+ let ( :agent ) { agent_class . new ( llm , model : "gpt-4.1" , stream :, tools : [ tool ] ) }
379+
380+ before do
381+ transport
382+ . stub (
383+ "POST" , "/v1/chat/completions" ,
384+ fixture : "openai/chat_completions_tool.sse" ,
385+ headers : { "content-type" => "text/event-stream" }
386+ )
387+ . stub (
388+ "POST" , "/v1/chat/completions" ,
389+ fixture : "openai/chat_completions_tool_result.sse" ,
390+ headers : { "content-type" => "text/event-stream" }
391+ )
392+ agent . talk ( "Run date" )
393+ end
394+
395+ it "emits confirmed tool returns" do
396+ expect ( stream . returns ) . must_equal [ [ "system" , "system" , { "success" => "2025-08-24" } ] ]
397+ end
398+ end
301399 end
302400end
303401
0 commit comments