@@ -176,8 +176,6 @@ const handleOutputItemDone = (
176176 state . blockHasDelta . add ( blockIndex )
177177 }
178178
179- closeBlockIfOpen ( state , blockIndex , events )
180-
181179 return events
182180}
183181
@@ -232,7 +230,6 @@ const handleFunctionCallArgumentsDone = (
232230 state . blockHasDelta . add ( blockIndex )
233231 }
234232
235- closeBlockIfOpen ( state , blockIndex , events )
236233 state . functionCallStateByOutputIndex . delete ( outputIndex )
237234 return events
238235}
@@ -340,8 +337,6 @@ const handleOutputTextDone = (
340337 } )
341338 }
342339
343- closeBlockIfOpen ( state , blockIndex , events )
344-
345340 return events
346341}
347342
@@ -447,6 +442,7 @@ const openTextBlockIfNeeded = (
447442 }
448443
449444 if ( ! state . openBlocks . has ( blockIndex ) ) {
445+ closeOpenBlocks ( state , events )
450446 events . push ( {
451447 type : "content_block_start" ,
452448 index : blockIndex ,
@@ -478,6 +474,7 @@ const openThinkingBlockIfNeeded = (
478474 }
479475
480476 if ( ! state . openBlocks . has ( blockIndex ) ) {
477+ closeOpenBlocks ( state , events )
481478 events . push ( {
482479 type : "content_block_start" ,
483480 index : blockIndex ,
@@ -506,13 +503,20 @@ const closeBlockIfOpen = (
506503 state . blockHasDelta . delete ( blockIndex )
507504}
508505
509- const closeAllOpenBlocks = (
506+ const closeOpenBlocks = (
510507 state : ResponsesStreamState ,
511508 events : Array < AnthropicStreamEventData > ,
512509) => {
513510 for ( const blockIndex of state . openBlocks ) {
514511 closeBlockIfOpen ( state , blockIndex , events )
515512 }
513+ }
514+
515+ const closeAllOpenBlocks = (
516+ state : ResponsesStreamState ,
517+ events : Array < AnthropicStreamEventData > ,
518+ ) => {
519+ closeOpenBlocks ( state , events )
516520
517521 state . functionCallStateByOutputIndex . clear ( )
518522}
@@ -560,6 +564,7 @@ const openFunctionCallBlock = (
560564 const { blockIndex } = functionCallState
561565
562566 if ( ! state . openBlocks . has ( blockIndex ) ) {
567+ closeOpenBlocks ( state , events )
563568 events . push ( {
564569 type : "content_block_start" ,
565570 index : blockIndex ,
0 commit comments