@@ -7,12 +7,6 @@ import {
77TooltipProvider ,
88TooltipTrigger ,
99} from "components/Tooltip/Tooltip" ;
10- import every from "lodash/every" ;
11- import keys from "lodash/keys" ;
12- import mapValues from "lodash/mapValues" ;
13- import some from "lodash/some" ;
14- import sum from "lodash/sum" ;
15- import uniq from "lodash/uniq" ;
1610import {
1711ArrowDownIcon ,
1812ArrowUpIcon ,
@@ -37,35 +31,43 @@ type RequestLogsRowProps = {
3731function tokenUsageMetadataMerge (
3832...objects :Array < AIBridgeInterception [ "token_usages" ] [ number ] [ "metadata" ] >
3933) :unknown {
40- // TODO: Where possible, use native JS functions instead of lodash functions
41-
4234// Filter out empty objects
43- const nonEmptyObjects = objects . filter ( ( obj ) => keys ( obj ) . length > 0 ) ;
35+ const nonEmptyObjects = objects . filter ( ( obj ) => Object . keys ( obj ) . length > 0 ) ;
4436
4537// If all objects were empty, return null
4638if ( nonEmptyObjects . length === 0 ) return null ;
4739
4840// Check if all objects have the same keys
49- const keySets = nonEmptyObjects . map ( ( obj ) => keys ( obj ) . sort ( ) . join ( "," ) ) ;
41+ const keySets = nonEmptyObjects . map ( ( obj ) =>
42+ Object . keys ( obj ) . sort ( ) . join ( "," ) ,
43+ ) ;
5044// If the keys are different, just instantly return the objects
51- if ( uniq ( keySets ) . length > 1 ) return nonEmptyObjects ;
45+ if ( new Set ( keySets ) . size > 1 ) return nonEmptyObjects ;
5246
5347// Group the objects by key
54- const grouped = mapValues ( nonEmptyObjects [ 0 ] , ( _ , key ) =>
55- nonEmptyObjects . map ( ( obj ) => obj [ key ] ) ,
48+ const grouped = Object . fromEntries (
49+ Object . keys ( nonEmptyObjects [ 0 ] ) . map ( ( key ) => [
50+ key ,
51+ nonEmptyObjects . map ( ( obj ) => obj [ key ] ) ,
52+ ] ) ,
5653) ;
5754
5855// Map the grouped values to a new object
59- const result = mapValues ( grouped , ( values :unknown [ ] ) => {
60- const allNumeric = every ( values , ( v :unknown ) => typeof v === "number" ) ;
61- const allSame = uniq ( values ) . length === 1 ;
56+ const result = Object . fromEntries (
57+ Object . entries ( grouped ) . map ( ( [ key , values ] :[ string , unknown [ ] ] ) => {
58+ const allNumeric = values . every ( ( v :unknown ) => typeof v === "number" ) ;
59+ const allSame = new Set ( values ) . size === 1 ;
6260
63- if ( allNumeric ) return sum ( values ) ;
64- if ( allSame ) return values [ 0 ] ;
65- return null ; // Mark conflict
66- } ) ;
61+ if ( allNumeric )
62+ return [ key , values . reduce ( ( acc , v ) => acc + ( v as number ) , 0 ) ] ;
63+ if ( allSame ) return [ key , values [ 0 ] ] ;
64+ return [ key , null ] ; // Mark conflict
65+ } ) ,
66+ ) ;
6767
68- return some ( result , ( v :unknown ) => v === null ) ?nonEmptyObjects :result ;
68+ return Object . values ( result ) . some ( ( v :unknown ) => v === null )
69+ ?nonEmptyObjects
70+ :result ;
6971}
7072
7173export const RequestLogsRow :FC < RequestLogsRowProps > = ( { interception} ) => {