Skip to content

Commit 4cfdee5

Browse files
committed
refactor(telemetry): simplify token calculation and remove total telemetry
remove total telemetry aggregation and simplify token context calculation move agent telemetry display to output window clean up telemetry bar component
1 parent 4bdfbe7 commit 4cfdee5

File tree

5 files changed

+29
-103
lines changed

5 files changed

+29
-103
lines changed

src/cli/tui/routes/workflow/components/output/output-window.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ export function OutputWindow(props: OutputWindowProps) {
399399
</box>
400400
</Show>
401401

402-
{/* Narrow layout: name on one line, status on next line */}
402+
{/* Narrow layout: name on one line, status on next line, telemetry on third */}
403403
<Show when={!isWideLayout()}>
404404
<box flexDirection="row">
405405
<text fg={themeCtx.theme.border}></text>
@@ -417,11 +417,21 @@ export function OutputWindow(props: OutputWindowProps) {
417417
</box>
418418
</Show>
419419

420-
<box flexDirection="row">
421-
<text fg={themeCtx.theme.border}></text>
422-
<Show when={derivedActivity() !== "idle"} fallback={<text fg={themeCtx.theme.textMuted}>{currentPhrase()}</text>}>
423-
<TypingText text={`↳ ${currentPhrase()}`} speed={30} />
424-
</Show>
420+
<box flexDirection="row" justifyContent="space-between" paddingRight={2}>
421+
<box flexDirection="row">
422+
<text fg={themeCtx.theme.border}></text>
423+
<Show when={derivedActivity() !== "idle"} fallback={<text fg={themeCtx.theme.textMuted}>{currentPhrase()}</text>}>
424+
<TypingText text={`↳ ${currentPhrase()}`} speed={30} />
425+
</Show>
426+
</box>
427+
<text fg={themeCtx.theme.textMuted}>
428+
context: {(() => {
429+
const total = (props.currentAgent?.telemetry.tokensIn ?? 0) + (props.currentAgent?.telemetry.tokensOut ?? 0)
430+
if (total >= 1000000) return `${(total / 1000000).toFixed(1)}M`
431+
if (total >= 1000) return `${(total / 1000).toFixed(1)}K`
432+
return `${total}`
433+
})()}
434+
</text>
425435
</box>
426436
<text fg={themeCtx.theme.border}>╰─</text>
427437
</box>

src/cli/tui/routes/workflow/components/output/telemetry-bar.tsx

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,21 @@
77
*/
88

99
import { Show } from "solid-js"
10-
import { useTerminalDimensions } from "@opentui/solid"
1110
import { useTheme } from "@tui/shared/context/theme"
12-
import { formatTokens, formatNumber } from "../../state/formatters"
1311
import type { WorkflowStatus, AutonomousMode } from "../../state/types"
1412

1513
export interface TelemetryBarProps {
1614
workflowName: string
1715
runtime: string
1816
status: WorkflowStatus
19-
total: {
20-
tokensIn: number
21-
tokensOut: number
22-
cached?: number
23-
}
2417
autonomousMode?: AutonomousMode
2518
}
2619

2720
/**
2821
* Show workflow info, status, and total telemetry in footer
2922
*/
30-
// Compact threshold - below this width, use compact layout
31-
const COMPACT_WIDTH = 80
32-
3323
export function TelemetryBar(props: TelemetryBarProps) {
3424
const themeCtx = useTheme()
35-
const dimensions = useTerminalDimensions()
36-
37-
const isCompact = () => (dimensions()?.width ?? 80) < COMPACT_WIDTH
38-
39-
const totalText = () => {
40-
const cached = props.total.cached ?? 0
41-
const base = formatTokens(props.total.tokensIn, props.total.tokensOut)
42-
return cached > 0 ? `${base} (${formatNumber(cached)} cached)` : base
43-
}
44-
45-
// Compact token display - no "cached" info
46-
const compactTokenText = () => {
47-
return formatTokens(props.total.tokensIn, props.total.tokensOut)
48-
}
4925

5026
const showStatus = () => props.status === "awaiting" || props.status === "paused" || props.status === "stopped"
5127

@@ -75,12 +51,17 @@ export function TelemetryBar(props: TelemetryBarProps) {
7551
borderStyle="rounded"
7652
borderColor={themeCtx.theme.border}
7753
>
78-
{/* Left side: workflow name, runtime, status, autonomous mode */}
54+
{/* Left side: runtime */}
55+
<box flexDirection="row" flexShrink={0}>
56+
<text fg={themeCtx.theme.textMuted}>Runtime: </text>
57+
<text fg={themeCtx.theme.text}>{props.runtime}</text>
58+
</box>
59+
60+
{/* Right side: workflow name, status, autonomous mode */}
7961
<box flexDirection="row" flexShrink={1}>
8062
<text fg={themeCtx.theme.text} attributes={1}>
8163
{props.workflowName}
8264
</text>
83-
<text fg={themeCtx.theme.textMuted}>{props.runtime}</text>
8465
<Show when={showStatus()}>
8566
<text fg={themeCtx.theme.text}></text>
8667
<text fg={statusColor()}>{statusText()}</text>
@@ -90,17 +71,6 @@ export function TelemetryBar(props: TelemetryBarProps) {
9071
<text fg={themeCtx.theme.primary}>{props.autonomousMode === 'always' ? 'AUTO (LOCKED)' : 'AUTO'}</text>
9172
</Show>
9273
</box>
93-
94-
{/* Right side: token counts */}
95-
<box flexDirection="row" flexShrink={0}>
96-
<Show when={!isCompact()}>
97-
<text fg={themeCtx.theme.textMuted}>Tokens: </text>
98-
<text fg={themeCtx.theme.text}>{totalText()}</text>
99-
</Show>
100-
<Show when={isCompact()}>
101-
<text fg={themeCtx.theme.text}>{compactTokenText()}</text>
102-
</Show>
103-
</box>
10474
</box>
10575
)
10676
}

src/cli/tui/routes/workflow/components/shells/shared-layout.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export function SharedLayout(props: SharedLayoutProps) {
4848
workflowName={shell.state().workflowName}
4949
runtime={shell.timer.workflowRuntime()}
5050
status={shell.state().workflowStatus}
51-
total={shell.totalTelemetry()}
5251
autonomousMode={shell.state().autonomousMode}
5352
/>
5453
<StatusFooter

src/cli/tui/routes/workflow/hooks/use-workflow-computed.ts

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,12 @@
55
*/
66

77
import { createMemo, type Accessor } from "solid-js"
8-
import { debug } from "../../../../../shared/logging/logger.js"
98
import type { WorkflowState, AgentState, SubAgentState } from "../state/types"
109

1110
export interface UseWorkflowComputedOptions {
1211
getState: Accessor<WorkflowState>
1312
}
1413

15-
export interface TotalTelemetry {
16-
tokensIn: number
17-
tokensOut: number
18-
cached?: number
19-
}
20-
2114
export interface UseWorkflowComputedResult {
2215
// View checks
2316
isControllerView: Accessor<boolean>
@@ -34,9 +27,6 @@ export interface UseWorkflowComputedResult {
3427
// Checkpoint
3528
isCheckpointActive: Accessor<boolean>
3629

37-
// Telemetry
38-
totalTelemetry: Accessor<TotalTelemetry>
39-
4030
// Layout
4131
isTimelineCollapsed: Accessor<boolean>
4232
}
@@ -99,49 +89,6 @@ export function useWorkflowComputed(options: UseWorkflowComputedOptions): UseWor
9989
// Checkpoint state
10090
const isCheckpointActive = () => getState().checkpointState?.active ?? false
10191

102-
// Total telemetry - memoized
103-
const totalTelemetry = createMemo((prev: TotalTelemetry | undefined) => {
104-
const agents = getState().agents
105-
const subAgents = getState().subAgents
106-
const controller = getState().controllerState
107-
let tokensIn = 0, tokensOut = 0, cached = 0
108-
109-
for (const agent of agents) {
110-
tokensIn += agent.telemetry.tokensIn
111-
tokensOut += agent.telemetry.tokensOut
112-
cached += agent.telemetry.cached ?? 0
113-
}
114-
115-
for (const subs of subAgents.values()) {
116-
for (const sub of subs) {
117-
tokensIn += sub.telemetry.tokensIn
118-
tokensOut += sub.telemetry.tokensOut
119-
cached += sub.telemetry.cached ?? 0
120-
}
121-
}
122-
123-
// Include controller telemetry
124-
if (controller?.telemetry) {
125-
tokensIn += controller.telemetry.tokensIn
126-
tokensOut += controller.telemetry.tokensOut
127-
cached += controller.telemetry.cached ?? 0
128-
}
129-
130-
const result: TotalTelemetry = {
131-
tokensIn,
132-
tokensOut,
133-
cached: cached > 0 ? cached : undefined
134-
}
135-
136-
// Only log when values actually change
137-
if (!prev || prev.tokensIn !== tokensIn || prev.tokensOut !== tokensOut || prev.cached !== result.cached) {
138-
debug('[TELEMETRY:6-TOTAL] totalTokensIn=%d, totalTokensOut=%d, totalCached=%s',
139-
tokensIn, tokensOut, result.cached)
140-
}
141-
142-
return result
143-
})
144-
14592
// Timeline collapsed state
14693
const isTimelineCollapsed = () => getState().timelineCollapsed
14794

@@ -153,7 +100,6 @@ export function useWorkflowComputed(options: UseWorkflowComputedOptions): UseWor
153100
currentAgent,
154101
isShowingRunningAgent,
155102
isCheckpointActive,
156-
totalTelemetry,
157103
isTimelineCollapsed
158104
}
159105
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ export function updateAgentTelemetryInList(
99
return agents.map((agent) => {
1010
if (agent.id !== agentId) return agent
1111

12-
// Context = input (uncached) + cached tokens
13-
// The API reports uncached input separately from cached, we need the sum for total context
14-
const currentContext = (telemetry.tokensIn ?? 0) + (telemetry.cached ?? 0)
12+
// Context = input tokens (total)
13+
// Note: cached tokens are already INCLUDED in tokensIn, not separate
14+
// cached is just metadata showing how many of those tokens were served from cache
15+
const currentContext = telemetry.tokensIn ?? 0
1516
const newTokensIn = currentContext > 0 ? currentContext : agent.telemetry.tokensIn
1617
const newTokensOut = telemetry.tokensOut ?? agent.telemetry.tokensOut
1718
const newCached = telemetry.cached ?? agent.telemetry.cached
1819
// Cost accumulates
1920
const newCost = (agent.telemetry.cost ?? 0) + (telemetry.cost ?? 0) || undefined
2021

2122
debug('[TELEMETRY:5-UTILS] [STEP-AGENT] updateAgentTelemetryInList → agentId=%s', agentId)
22-
debug('[TELEMETRY:5-UTILS] [STEP-AGENT] INPUT: uncached=%d + cached=%d = context=%d, output=%d',
23-
telemetry.tokensIn ?? 0, telemetry.cached ?? 0, currentContext, telemetry.tokensOut ?? 0)
23+
debug('[TELEMETRY:5-UTILS] [STEP-AGENT] INPUT: context=%d (cached=%d), output=%d',
24+
telemetry.tokensIn ?? 0, telemetry.cached ?? 0, telemetry.tokensOut ?? 0)
2425
debug('[TELEMETRY:5-UTILS] [STEP-AGENT] RESULT: context=%d, output=%d (REPLACES previous)',
2526
newTokensIn, newTokensOut)
2627

0 commit comments

Comments
 (0)