Skip to content

Commit a1bbc27

Browse files
committed
codex: fix displaying used context
1 parent e82c8a7 commit a1bbc27

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/packages/frontend/chat/codex-activity.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ function createEventEntry({
501501
function formatSummaryDetail(message: AcpStreamMessage & { type: "summary" }) {
502502
const parts: string[] = [];
503503
if (message.finalResponse) {
504-
parts.push(truncate(message.finalResponse, 200));
504+
parts.push(truncate(message.finalResponse, 60));
505505
}
506506
if (message.usage) {
507507
parts.push(`Usage: ${formatUsage(message.usage)}`);
@@ -512,11 +512,14 @@ function formatSummaryDetail(message: AcpStreamMessage & { type: "summary" }) {
512512
function formatUsage(usage?: {
513513
input_tokens?: number;
514514
output_tokens?: number;
515+
reasoning_output_tokens?: number;
515516
}) {
516517
if (!usage) return "";
517518
const parts: string[] = [];
518519
if (usage.input_tokens != null) parts.push(`${usage.input_tokens} in`);
519520
if (usage.output_tokens != null) parts.push(`${usage.output_tokens} out`);
521+
if (usage.reasoning_output_tokens)
522+
parts.push(`${usage.reasoning_output_tokens} reasoning`);
520523
return parts.join(", ");
521524
}
522525

src/packages/frontend/chat/codex.tsx

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,18 @@ export function CodexConfigButton({
189189

190190
const contextWindow =
191191
usageSummary?.contextWindow ?? getModelContextWindow(selectedModelValue);
192-
const usageTotal =
193-
usageSummary?.totalTokens != null ? usageSummary.totalTokens : 0;
192+
const usedTokens =
193+
usageSummary?.usedTokens ??
194+
(usageSummary?.totalTokens != null ? usageSummary.totalTokens : 0);
195+
const cappedUsedTokens =
196+
contextWindow != null ? Math.min(usedTokens, contextWindow) : usedTokens;
194197
const remainingPercent =
195198
usageSummary != null && contextWindow != null
196199
? Math.max(
197200
0,
198-
Math.round(((contextWindow - usageTotal) / contextWindow) * 100),
201+
Math.round(
202+
((contextWindow - cappedUsedTokens) / contextWindow) * 100,
203+
),
199204
)
200205
: null;
201206

@@ -453,6 +458,7 @@ export default CodexConfigButton;
453458
type UsageSummary = {
454459
latest?: any;
455460
totalTokens: number;
461+
usedTokens?: number;
456462
contextWindow?: number;
457463
};
458464

@@ -485,6 +491,7 @@ function getCodexUsageSummary(
485491
});
486492
let latest;
487493
let totalTokens = 0;
494+
let usedTokens: number | undefined;
488495
let contextWindow: number | undefined;
489496
let hasAggregate = false;
490497
for (const entry of sortedMessages) {
@@ -501,12 +508,16 @@ function getCodexUsageSummary(
501508
if (usageData?.model_context_window != null) {
502509
contextWindow = usageData.model_context_window;
503510
}
511+
const turnUsed = calcUsedTokens(usageData);
512+
if (turnUsed != null) {
513+
usedTokens = turnUsed;
514+
}
504515
latest = usageData;
505516
}
506517
if (!latest && totalTokens === 0) {
507518
return undefined;
508519
}
509-
return { latest, totalTokens, contextWindow };
520+
return { latest, totalTokens, usedTokens, contextWindow };
510521
}
511522

512523
function getMessageByKey(map, key: string): ChatMessageTyped | undefined {
@@ -586,6 +597,24 @@ function renderOptionWithDescription({
586597
);
587598
}
588599

600+
function calcUsedTokens(usage: any): number | undefined {
601+
if (!usage || typeof usage !== "object") return undefined;
602+
const keys = [
603+
"input_tokens",
604+
"cached_input_tokens",
605+
"output_tokens",
606+
"reasoning_output_tokens",
607+
] as const;
608+
let total = 0;
609+
for (const key of keys) {
610+
const value = usage[key];
611+
if (typeof value === "number" && Number.isFinite(value)) {
612+
total += value;
613+
}
614+
}
615+
return total > 0 ? total : undefined;
616+
}
617+
589618
function toMsSafe(value: any): number {
590619
if (value instanceof Date) {
591620
const ms = value.valueOf();

0 commit comments

Comments
 (0)