@@ -26,9 +26,8 @@ import { createRuntime } from "@/node/runtime/runtimeFactory";
2626import { MessageQueue } from "./messageQueue" ;
2727import type { StreamEndEvent , StreamAbortEvent } from "@/common/types/stream" ;
2828import { sumUsageHistory } from "@/common/utils/tokens/usageAggregator" ;
29- import type { ChatUsageDisplay } from "@/common/utils/tokens/usageAggregator" ;
30- import { createDisplayUsage } from "@/common/utils/tokens/tokenStatsCalculator" ;
3129import type { LanguageModelV2Usage } from "@ai-sdk/provider" ;
30+ import { accUsageHistory } from "@/common/utils/tokens/displayUsage" ;
3231
3332export interface AgentSessionChatEvent {
3433 workspaceId : string ;
@@ -702,36 +701,7 @@ export class AgentSession {
702701 return Err ( `Failed to get history for usage calculation: ${ historyResult . error } ` ) ;
703702 }
704703
705- // Calculate cumulative historical usage from all messages
706- const usageHistory : ChatUsageDisplay [ ] = [ ] ;
707- let cumulativeHistorical : ChatUsageDisplay | undefined ;
708-
709- for ( const msg of historyResult . data ) {
710- if ( msg . role === "assistant" ) {
711- // Accumulate historical usage from previous compactions
712- if ( msg . metadata ?. historicalUsage ) {
713- cumulativeHistorical = msg . metadata . historicalUsage ;
714- }
715-
716- // Add current message's usage
717- if ( msg . metadata ?. usage && msg . metadata . model ) {
718- const displayUsage = createDisplayUsage (
719- msg . metadata . usage ,
720- msg . metadata . model ,
721- msg . metadata . providerMetadata
722- ) ;
723- if ( displayUsage ) {
724- usageHistory . push ( displayUsage ) ;
725- }
726- }
727- }
728- }
729-
730- // If we have historical usage from a compaction, prepend it to history
731- // This ensures costs from pre-compaction messages are included in totals
732- if ( cumulativeHistorical ) {
733- usageHistory . unshift ( cumulativeHistorical ) ;
734- }
704+ const usageHistory = accUsageHistory ( historyResult . data , undefined ) ;
735705
736706 const historicalUsage = usageHistory . length > 0 ? sumUsageHistory ( usageHistory ) : undefined ;
737707
0 commit comments