Skip to content

Commit 8c194c3

Browse files
committed
feat(tui): display total context tokens and reverse history order
- Change token display from separate input/output to total context tokens (input + cached) - Format large token counts with K/M suffixes for better readability - Reverse agent tree display to show latest entries first - Update column header from "Tokens" to "Context" to reflect new meaning - Update telemetry logging to clarify total context calculation
1 parent 7a19a8e commit 8c194c3

File tree

5 files changed

+16
-9
lines changed

5 files changed

+16
-9
lines changed

src/cli/tui/routes/workflow/components/modals/history/history-row.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ export function HistoryRow(props: HistoryRowProps) {
6868

6969
const tokens = () => {
7070
if (agent.telemetry) {
71-
return formatTokens(agent.telemetry.tokensIn, agent.telemetry.tokensOut)
71+
const total = agent.telemetry.tokensIn + agent.telemetry.tokensOut
72+
return formatTokens(total)
7273
}
7374
return "-"
7475
}

src/cli/tui/routes/workflow/components/modals/history/history-tree.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ export function flattenTree(tree: AgentTreeNode[]): FlattenedAgent[] {
2424
const result: FlattenedAgent[] = []
2525

2626
function traverse(nodes: AgentTreeNode[], depth: number, parentIsLast: boolean[]) {
27-
nodes.forEach((node, index) => {
28-
const isLast = index === nodes.length - 1
27+
// Reverse nodes so latest appears first
28+
const reversedNodes = [...nodes].reverse()
29+
reversedNodes.forEach((node, index) => {
30+
const isLast = index === reversedNodes.length - 1
2931

3032
result.push({
3133
agent: node.agent,

src/cli/tui/routes/workflow/components/modals/history/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export function HistoryView(props: HistoryViewProps) {
128128
<box width={28}><text fg={themeCtx.theme.textMuted} attributes={1}>Engine/Model</text></box>
129129
<box width={12}><text fg={themeCtx.theme.textMuted} attributes={1}>Status</text></box>
130130
<box width={12}><text fg={themeCtx.theme.textMuted} attributes={1}>Duration</text></box>
131-
<box width={22}><text fg={themeCtx.theme.textMuted} attributes={1}>Tokens</text></box>
131+
<box width={22}><text fg={themeCtx.theme.textMuted} attributes={1}>Context</text></box>
132132
</box>
133133

134134
{/* Agent Rows - using scrollbox for native scrolling */}

src/cli/tui/routes/workflow/state/formatters.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ export function formatNumber(num: number): string {
99
return num.toLocaleString()
1010
}
1111

12-
export function formatTokens(tokensIn: number, tokensOut: number): string {
13-
return `${formatNumber(tokensIn)}in/${formatNumber(tokensOut)}out`
12+
export function formatTokens(total: number): string {
13+
if (total >= 1000000) return `${(total / 1000000).toFixed(1)}M`
14+
if (total >= 1000) return `${(total / 1000).toFixed(1)}K`
15+
return `${total}`
1416
}
1517

1618
export function formatCost(cost: number): string {

src/infra/engines/providers/opencode/execution/runner.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,17 @@ export async function runOpenCode(options: RunOpenCodeOptions): Promise<RunOpenC
199199

200200
// Only emit when NEW telemetry is captured (not on every line)
201201
if (onTelemetry && newCaptured?.tokens && newCaptured !== prevCaptured) {
202+
// Context window = input + cached (cache.read + cache.write)
203+
const totalContextIn = (newCaptured.tokens.input ?? 0) + (newCaptured.tokens.cached ?? 0);
202204
const telemetryPayload = {
203-
tokensIn: newCaptured.tokens.input ?? 0,
205+
tokensIn: totalContextIn,
204206
tokensOut: newCaptured.tokens.output ?? 0,
205207
cached: newCaptured.tokens.cached,
206208
cost: newCaptured.cost,
207209
duration: newCaptured.duration,
208210
};
209-
logger.debug('[TELEMETRY:2-RUNNER] Emitting onTelemetry → tokensIn=%d, tokensOut=%d, cached=%s',
210-
telemetryPayload.tokensIn, telemetryPayload.tokensOut, telemetryPayload.cached ?? 'undefined');
211+
logger.debug('[TELEMETRY:2-RUNNER] Emitting onTelemetry → tokensIn=%d (input=%d + cached=%d), tokensOut=%d',
212+
totalContextIn, newCaptured.tokens.input ?? 0, newCaptured.tokens.cached ?? 0, telemetryPayload.tokensOut);
211213
onTelemetry(telemetryPayload);
212214
}
213215

0 commit comments

Comments
 (0)