Skip to content

Commit e0be9f3

Browse files
committed
fixup
1 parent 60cf5e6 commit e0be9f3

File tree

3 files changed

+5
-36
lines changed

3 files changed

+5
-36
lines changed

src/browser/stores/WorkspaceStore.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
isRestoreToInput,
1818
} from "@/common/types/ipc";
1919
import { MapStore } from "./MapStore";
20-
import { getUsageHistory } from "@/common/utils/tokens/displayUsage";
20+
import { accUsageHistory } from "@/common/utils/tokens/displayUsage";
2121
import { WorkspaceConsumerManager } from "./WorkspaceConsumerManager";
2222
import type { ChatUsageDisplay } from "@/common/utils/tokens/usageAggregator";
2323
import type { TokenConsumer } from "@/common/types/chatStats";
@@ -432,7 +432,7 @@ export class WorkspaceStore {
432432

433433
const messages = aggregator.getAllMessages();
434434
const model = aggregator.getCurrentModel();
435-
const usageHistory = getUsageHistory(messages, model);
435+
const usageHistory = accUsageHistory(messages, model);
436436

437437
// Calculate total from usage history (now includes historical)
438438
const totalTokens = usageHistory.reduce(

src/common/utils/tokens/displayUsage.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export function createDisplayUsage(
9292
};
9393
}
9494

95-
export function getUsageHistory(
95+
export function accUsageHistory(
9696
messages: MuxMessage[],
9797
fallbackModel?: string
9898
): ChatUsageDisplay[] {
@@ -112,7 +112,6 @@ export function getUsageHistory(
112112
if (msg.metadata?.usage) {
113113
// Use the model from this specific message (not global)
114114
const model = msg.metadata.model ?? fallbackModel ?? "unknown";
115-
116115
const usage = createDisplayUsage(msg.metadata.usage, model, msg.metadata.providerMetadata);
117116

118117
if (usage) {

src/node/services/agentSession.ts

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ import { createRuntime } from "@/node/runtime/runtimeFactory";
2626
import { MessageQueue } from "./messageQueue";
2727
import type { StreamEndEvent, StreamAbortEvent } from "@/common/types/stream";
2828
import { sumUsageHistory } from "@/common/utils/tokens/usageAggregator";
29-
import type { ChatUsageDisplay } from "@/common/utils/tokens/usageAggregator";
30-
import { createDisplayUsage } from "@/common/utils/tokens/tokenStatsCalculator";
3129
import type { LanguageModelV2Usage } from "@ai-sdk/provider";
30+
import { accUsageHistory } from "@/common/utils/tokens/displayUsage";
3231

3332
export 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

Comments
 (0)