@@ -222,6 +222,9 @@ const llmEventTagged = Schema.Union([
222222] ) . pipe ( Schema . toTaggedUnion ( "type" ) )
223223
224224type WithID < Event extends { readonly id : unknown } , ID > = Omit < Event , "type" | "id" > & { readonly id : ID | string }
225+ type WithUsage < Event extends { readonly usage ?: Usage } > = Omit < Event , "type" | "usage" > & {
226+ readonly usage ?: Usage | ConstructorParameters < typeof Usage > [ 0 ]
227+ }
225228
226229const responseID = ( value : ResponseID | string ) => ResponseID . make ( value )
227230const contentBlockID = ( value : ContentBlockID | string ) => ContentBlockID . make ( value )
@@ -252,8 +255,10 @@ export const LLMEvent = Object.assign(llmEventTagged, {
252255 toolCall : ( input : WithID < ToolCall , ToolCallID > ) => ToolCall . make ( { ...input , id : toolCallID ( input . id ) } ) ,
253256 toolResult : ( input : WithID < ToolResult , ToolCallID > ) => ToolResult . make ( { ...input , id : toolCallID ( input . id ) } ) ,
254257 toolError : ( input : WithID < ToolError , ToolCallID > ) => ToolError . make ( { ...input , id : toolCallID ( input . id ) } ) ,
255- stepFinish : StepFinish . make ,
256- requestFinish : RequestFinish . make ,
258+ stepFinish : ( input : WithUsage < StepFinish > ) =>
259+ StepFinish . make ( { ...input , usage : input . usage instanceof Usage ? input . usage : new Usage ( input . usage ?? { } ) } ) ,
260+ requestFinish : ( input : WithUsage < RequestFinish > ) =>
261+ RequestFinish . make ( { ...input , usage : input . usage instanceof Usage ? input . usage : new Usage ( input . usage ?? { } ) } ) ,
257262 providerError : ProviderErrorEvent . make ,
258263 is : {
259264 requestStart : llmEventTagged . guards [ "request-start" ] ,
0 commit comments