@@ -167,6 +167,9 @@ const llmEventTagged = Schema.Union([
167167] ) . pipe ( Schema . toTaggedUnion ( "type" ) )
168168
169169type WithID < Event extends { readonly id : unknown } , ID > = Omit < Event , "type" | "id" > & { readonly id : ID | string }
170+ type WithUsage < Event extends { readonly usage ?: Usage } > = Omit < Event , "type" | "usage" > & {
171+ readonly usage ?: Usage | ConstructorParameters < typeof Usage > [ 0 ]
172+ }
170173
171174const responseID = ( value : ResponseID | string ) => ResponseID . make ( value )
172175const contentBlockID = ( value : ContentBlockID | string ) => ContentBlockID . make ( value )
@@ -197,8 +200,10 @@ export const LLMEvent = Object.assign(llmEventTagged, {
197200 toolCall : ( input : WithID < ToolCall , ToolCallID > ) => ToolCall . make ( { ...input , id : toolCallID ( input . id ) } ) ,
198201 toolResult : ( input : WithID < ToolResult , ToolCallID > ) => ToolResult . make ( { ...input , id : toolCallID ( input . id ) } ) ,
199202 toolError : ( input : WithID < ToolError , ToolCallID > ) => ToolError . make ( { ...input , id : toolCallID ( input . id ) } ) ,
200- stepFinish : StepFinish . make ,
201- requestFinish : RequestFinish . make ,
203+ stepFinish : ( input : WithUsage < StepFinish > ) =>
204+ StepFinish . make ( { ...input , usage : input . usage instanceof Usage ? input . usage : new Usage ( input . usage ?? { } ) } ) ,
205+ requestFinish : ( input : WithUsage < RequestFinish > ) =>
206+ RequestFinish . make ( { ...input , usage : input . usage instanceof Usage ? input . usage : new Usage ( input . usage ?? { } ) } ) ,
202207 providerError : ProviderErrorEvent . make ,
203208 is : {
204209 requestStart : llmEventTagged . guards [ "request-start" ] ,
0 commit comments