77const { getMessageRefusal, getProviderRefusals, normalizeOpenAIChatUsage } = require ( "./provider_refusal.cjs" ) ;
88
99const USAGE_ALIASES = {
10- input_tokens : "inputTokens" ,
11- output_tokens : "outputTokens" ,
12- reasoning_output_tokens : "reasoningOutputTokens" ,
13- total_tokens : "totalTokens" ,
14- cache_creation_input_tokens : "cacheCreationInputTokens" ,
15- cache_read_input_tokens : "cacheReadInputTokens" ,
10+ input_tokens : [ "inputTokens" ] ,
11+ output_tokens : [ "outputTokens" ] ,
12+ reasoning_output_tokens : [ "reasoningOutputTokens" ] ,
13+ total_tokens : [ "totalTokens" ] ,
14+ cache_creation_input_tokens : [ "cacheCreationInputTokens" , "cacheWriteTokens" , "cache_write_tokens" ] ,
15+ cache_read_input_tokens : [ "cacheReadInputTokens" , "cacheReadTokens" , "cache_read_tokens" ] ,
1616} ;
1717const USAGE_BOOLEAN_ALIASES = {
1818 input_tokens_include_cache : "inputTokensIncludeCache" ,
@@ -40,7 +40,7 @@ function isMetric(value) {
4040 */
4141function sessionContext ( event ) {
4242 const data = event ?. data ?? { } ;
43- const sessionId = data . sessionId !== undefined ? data . sessionId : event ?. sessionId !== undefined ? event . sessionId : event ?. session_id !== undefined ? event . session_id : data . session_id ;
43+ const sessionId = data . sessionId !== undefined ? data . sessionId : event ?. sessionId !== undefined ? event . sessionId : event ?. session_id !== undefined ? event . session_id : data . session_id !== undefined ? data . session_id : event ?. sessionID ;
4444 const parentToolUseId =
4545 data . parentToolUseId !== undefined ? data . parentToolUseId : event ?. parentToolUseId !== undefined ? event . parentToolUseId : event ?. parent_tool_use_id !== undefined ? event . parent_tool_use_id : data . parent_tool_use_id ;
4646 const agentId = data . agentId ?? ( typeof event ?. type === "string" && event . type . startsWith ( "subagent." ) ? undefined : event ?. agentId ) ;
@@ -68,19 +68,33 @@ function sessionScopeKey(event) {
6868 return JSON . stringify ( [ sessionId , parentToolUseId , agentId ] ) ;
6969}
7070
71+ /** @param {any } value @returns {string[] } */
72+ function normalizeOverflowedTokens ( value ) {
73+ if ( ! Array . isArray ( value ) ) return [ ] ;
74+ return [
75+ ...new Set (
76+ value . flatMap ( key => {
77+ const match = Object . entries ( USAGE_ALIASES ) . find ( ( [ name , aliases ] ) => key === name || aliases . includes ( key ) ) ;
78+ return match ? [ match [ 0 ] ] : [ ] ;
79+ } )
80+ ) ,
81+ ] ;
82+ }
83+
7184/** @param {any } usage @returns {Record<string, any>|undefined } */
7285function normalizeSessionUsage ( usage ) {
7386 // Usage is a named-field object; arrays cannot represent its token fields.
7487 if ( ! usage || typeof usage !== "object" || Array . isArray ( usage ) ) return undefined ;
7588 const result = { ...usage } ;
7689 delete result . overflowedTokens ;
77- for ( const [ key , alias ] of Object . entries ( USAGE_ALIASES ) ) {
90+ for ( const [ key , aliases ] of Object . entries ( USAGE_ALIASES ) ) {
7891 const hasCanonical = Object . hasOwn ( usage , key ) ;
79- const value = hasCanonical ? usage [ key ] : usage [ alias ] ;
92+ const alias = aliases . find ( name => Object . hasOwn ( usage , name ) ) ;
93+ const value = hasCanonical ? usage [ key ] : alias === undefined ? undefined : usage [ alias ] ;
8094 if ( isTokenCount ( value ) ) result [ key ] = value ;
8195 else {
8296 delete result [ key ] ;
83- if ( hasCanonical ) delete result [ alias ] ;
97+ for ( const name of aliases ) delete result [ name ] ;
8498 }
8599 }
86100 for ( const [ key , alias ] of Object . entries ( USAGE_BOOLEAN_ALIASES ) ) {
@@ -94,10 +108,10 @@ function normalizeSessionUsage(usage) {
94108 }
95109 const overflowed = Object . hasOwn ( usage , "overflowed_tokens" ) ? usage . overflowed_tokens : usage . overflowedTokens ;
96110 if ( Array . isArray ( overflowed ) ) {
97- result . overflowed_tokens = [ ... new Set ( overflowed . map ( key => Object . entries ( USAGE_ALIASES ) . find ( ( [ name , alias ] ) => key === name || key === alias ) ?. [ 0 ] ) . filter ( Boolean ) ) ] ;
111+ result . overflowed_tokens = normalizeOverflowedTokens ( overflowed ) ;
98112 for ( const key of result . overflowed_tokens ) {
99113 delete result [ key ] ;
100- delete result [ USAGE_ALIASES [ key ] ] ;
114+ for ( const alias of USAGE_ALIASES [ key ] ) delete result [ alias ] ;
101115 }
102116 } else delete result . overflowed_tokens ;
103117 return result ;
@@ -113,14 +127,14 @@ function reconcileSessionUsage(previous, snapshot) {
113127 const normalized = normalizeSessionUsage ( snapshot ) ;
114128 if ( ! normalized ) return previous ;
115129 const result = { ...previous , ...normalized } ;
116- const overflowed = new Set ( Array . isArray ( previous ?. overflowed_tokens ) ? previous . overflowed_tokens : [ ] ) ;
130+ const overflowed = new Set ( normalizeOverflowedTokens ( previous ?. overflowed_tokens ) ) ;
117131 for ( const key of Object . keys ( USAGE_ALIASES ) ) {
118132 if ( isTokenCount ( normalized [ key ] ) ) overflowed . delete ( key ) ;
119133 }
120134 for ( const key of normalized . overflowed_tokens ?? [ ] ) {
121135 overflowed . add ( key ) ;
122136 delete result [ key ] ;
123- delete result [ USAGE_ALIASES [ key ] ] ;
137+ for ( const alias of USAGE_ALIASES [ key ] ) delete result [ alias ] ;
124138 }
125139 if ( overflowed . size ) result . overflowed_tokens = [ ...overflowed ] ;
126140 else delete result . overflowed_tokens ;
@@ -131,7 +145,7 @@ function reconcileSessionUsage(previous, snapshot) {
131145function accumulateSessionUsage ( target , usage ) {
132146 const normalized = normalizeSessionUsage ( usage ) ;
133147 if ( ! normalized ) return ;
134- const overflowed = new Set ( [ ...( Array . isArray ( target . overflowed_tokens ) ? target . overflowed_tokens : [ ] ) , ...( normalized . overflowed_tokens ?? [ ] ) ] ) ;
148+ const overflowed = new Set ( [ ...normalizeOverflowedTokens ( target . overflowed_tokens ) , ...( normalized . overflowed_tokens ?? [ ] ) ] ) ;
135149 for ( const key of Object . keys ( USAGE_ALIASES ) ) {
136150 if ( overflowed . has ( key ) || ! isTokenCount ( normalized [ key ] ) ) continue ;
137151 const sum = ( target [ key ] ?? 0 ) + normalized [ key ] ;
@@ -142,9 +156,9 @@ function accumulateSessionUsage(target, usage) {
142156 target . overflowed_tokens = [ ...overflowed ] ;
143157 for ( const key of overflowed ) {
144158 delete target [ key ] ;
145- delete target [ USAGE_ALIASES [ key ] ] ;
159+ for ( const alias of USAGE_ALIASES [ key ] ) delete target [ alias ] ;
146160 }
147- }
161+ } else delete target . overflowed_tokens ;
148162}
149163
150164/**
0 commit comments