1+ import type { ReasoningDetailUnion } from '@/src/schemas/reasoning-details' ;
2+ import type { OpenRouterUsageAccounting } from '@/src/types/index' ;
13import type {
24 LanguageModelV2 ,
35 LanguageModelV2CallOptions ,
@@ -12,13 +14,12 @@ import type {
1214import type { ParseResult } from '@ai-sdk/provider-utils' ;
1315import type { FinishReason } from 'ai' ;
1416import type { z } from 'zod/v4' ;
15- import type { ReasoningDetailUnion } from '@/src/schemas/reasoning-details' ;
16- import type { OpenRouterUsageAccounting } from '@/src/types/index' ;
1717import type {
1818 OpenRouterChatModelId ,
1919 OpenRouterChatSettings ,
2020} from '../types/openrouter-chat-settings' ;
2121
22+ import { ReasoningDetailType } from '@/src/schemas/reasoning-details' ;
2223import { InvalidResponseDataError } from '@ai-sdk/provider' ;
2324import {
2425 combineHeaders ,
@@ -28,7 +29,6 @@ import {
2829 isParsableJson ,
2930 postJsonToApi ,
3031} from '@ai-sdk/provider-utils' ;
31- import { ReasoningDetailType } from '@/src/schemas/reasoning-details' ;
3232import { openrouterFailedResponseHandler } from '../schemas/error-response' ;
3333import { mapOpenRouterFinishReason } from '../utils/map-finish-reason' ;
3434import { convertToOpenRouterChatMessages } from './convert-to-openrouter-chat-messages' ;
@@ -398,7 +398,7 @@ export class OpenRouterChatLanguageModel implements LanguageModelV2 {
398398 name : string ;
399399 arguments : string ;
400400 } ;
401-
401+ inputStarted : boolean ;
402402 sent : boolean ;
403403 } > = [ ] ;
404404
@@ -419,7 +419,7 @@ export class OpenRouterChatLanguageModel implements LanguageModelV2 {
419419 let textId : string | undefined ;
420420 let reasoningId : string | undefined ;
421421 let openrouterResponseId : string | undefined ;
422-
422+
423423 return {
424424 stream : response . pipeThrough (
425425 new TransformStream <
@@ -604,6 +604,7 @@ export class OpenRouterChatLanguageModel implements LanguageModelV2 {
604604 name : toolCallDelta . function . name ,
605605 arguments : toolCallDelta . function . arguments ?? '' ,
606606 } ,
607+ inputStarted : false ,
607608 sent : false ,
608609 } ;
609610
@@ -619,6 +620,8 @@ export class OpenRouterChatLanguageModel implements LanguageModelV2 {
619620 toolCall . function ?. arguments != null &&
620621 isParsableJson ( toolCall . function . arguments )
621622 ) {
623+ toolCall . inputStarted = true ;
624+
622625 controller . enqueue ( {
623626 type : 'tool-input-start' ,
624627 id : toolCall . id ,
@@ -658,7 +661,8 @@ export class OpenRouterChatLanguageModel implements LanguageModelV2 {
658661 throw new Error ( 'Tool call is missing' ) ;
659662 }
660663
661- if ( toolCallDelta . function ?. name != null ) {
664+ if ( ! toolCall . inputStarted ) {
665+ toolCall . inputStarted = true ;
662666 controller . enqueue ( {
663667 type : 'tool-input-start' ,
664668 id : toolCall . id ,
@@ -717,10 +721,16 @@ export class OpenRouterChatLanguageModel implements LanguageModelV2 {
717721 }
718722
719723 if ( textStarted ) {
720- controller . enqueue ( { type : 'text-end' , id : textId || generateId ( ) } ) ;
724+ controller . enqueue ( {
725+ type : 'text-end' ,
726+ id : textId || generateId ( ) ,
727+ } ) ;
721728 }
722729 if ( reasoningStarted ) {
723- controller . enqueue ( { type : 'reasoning-end' , id : reasoningId || generateId ( ) } ) ;
730+ controller . enqueue ( {
731+ type : 'reasoning-end' ,
732+ id : reasoningId || generateId ( ) ,
733+ } ) ;
724734 }
725735
726736 controller . enqueue ( {
0 commit comments