@@ -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
@@ -421,9 +416,7 @@ const messageStart = (
421416        usage : { 
422417          input_tokens : inputTokens , 
423418          output_tokens : 0 , 
424-           ...( inputCachedTokens  !==  undefined  &&  { 
425-             cache_creation_input_tokens : inputCachedTokens , 
426-           } ) , 
419+           cache_read_input_tokens : inputCachedTokens  ??  0 , 
427420        } , 
428421      } , 
429422    } , 
@@ -449,6 +442,7 @@ const openTextBlockIfNeeded = (
449442  } 
450443
451444  if  ( ! state . openBlocks . has ( blockIndex ) )  { 
445+     closeOpenBlocks ( state ,  events ) 
452446    events . push ( { 
453447      type : "content_block_start" , 
454448      index : blockIndex , 
@@ -480,6 +474,7 @@ const openThinkingBlockIfNeeded = (
480474  } 
481475
482476  if  ( ! state . openBlocks . has ( blockIndex ) )  { 
477+     closeOpenBlocks ( state ,  events ) 
483478    events . push ( { 
484479      type : "content_block_start" , 
485480      index : blockIndex , 
@@ -508,13 +503,20 @@ const closeBlockIfOpen = (
508503  state . blockHasDelta . delete ( blockIndex ) 
509504} 
510505
511- const  closeAllOpenBlocks  =  ( 
506+ const  closeOpenBlocks  =  ( 
512507  state : ResponsesStreamState , 
513508  events : Array < AnthropicStreamEventData > , 
514509)  =>  { 
515510  for  ( const  blockIndex  of  state . openBlocks )  { 
516511    closeBlockIfOpen ( state ,  blockIndex ,  events ) 
517512  } 
513+ } 
514+ 
515+ const  closeAllOpenBlocks  =  ( 
516+   state : ResponsesStreamState , 
517+   events : Array < AnthropicStreamEventData > , 
518+ )  =>  { 
519+   closeOpenBlocks ( state ,  events ) 
518520
519521  state . functionCallStateByOutputIndex . clear ( ) 
520522} 
@@ -562,6 +564,7 @@ const openFunctionCallBlock = (
562564  const  {  blockIndex }  =  functionCallState 
563565
564566  if  ( ! state . openBlocks . has ( blockIndex ) )  { 
567+     closeOpenBlocks ( state ,  events ) 
565568    events . push ( { 
566569      type : "content_block_start" , 
567570      index : blockIndex , 
0 commit comments