@tokentop/ttop 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +273 -0
- package/bin/ttop.js +49 -0
- package/package.json +68 -0
- package/src/agents/aggregator.ts +176 -0
- package/src/agents/costing.ts +74 -0
- package/src/agents/index.ts +12 -0
- package/src/agents/types.ts +71 -0
- package/src/cli.ts +167 -0
- package/src/config/schema.test.ts +166 -0
- package/src/config/schema.ts +165 -0
- package/src/demo/simulator.ts +745 -0
- package/src/plugins/agents/index.ts +2 -0
- package/src/plugins/auth-sources.ts +186 -0
- package/src/plugins/lifecycle.ts +177 -0
- package/src/plugins/loader.test.ts +195 -0
- package/src/plugins/loader.ts +282 -0
- package/src/plugins/notification-bus.ts +153 -0
- package/src/plugins/notifications/index.ts +2 -0
- package/src/plugins/notifications/terminal-bell.ts +66 -0
- package/src/plugins/notifications/visual-flash.ts +57 -0
- package/src/plugins/npm-installer.ts +164 -0
- package/src/plugins/plugin-context-factory.ts +67 -0
- package/src/plugins/plugin-host.ts +205 -0
- package/src/plugins/providers/anthropic.ts +325 -0
- package/src/plugins/providers/antigravity.ts +309 -0
- package/src/plugins/providers/codex.ts +250 -0
- package/src/plugins/providers/gemini.ts +391 -0
- package/src/plugins/providers/github-copilot.ts +194 -0
- package/src/plugins/providers/index.ts +12 -0
- package/src/plugins/providers/minimax.ts +223 -0
- package/src/plugins/providers/openai-api.ts +250 -0
- package/src/plugins/providers/opencode-zen.ts +125 -0
- package/src/plugins/providers/perplexity.ts +177 -0
- package/src/plugins/providers/zai.ts +225 -0
- package/src/plugins/registry.ts +251 -0
- package/src/plugins/sandbox-guard.test.ts +191 -0
- package/src/plugins/sandbox-guard.ts +155 -0
- package/src/plugins/sandbox.ts +138 -0
- package/src/plugins/themes/catppuccin-latte.ts +45 -0
- package/src/plugins/themes/catppuccin-mocha.ts +45 -0
- package/src/plugins/themes/claude-code.ts +45 -0
- package/src/plugins/themes/dracula.ts +45 -0
- package/src/plugins/themes/github-light.ts +45 -0
- package/src/plugins/themes/gruvbox-dark.ts +45 -0
- package/src/plugins/themes/gruvbox-light.ts +45 -0
- package/src/plugins/themes/index.ts +15 -0
- package/src/plugins/themes/kanagawa.ts +45 -0
- package/src/plugins/themes/nord.ts +45 -0
- package/src/plugins/themes/one-dark.ts +45 -0
- package/src/plugins/themes/opencode.ts +47 -0
- package/src/plugins/themes/rose-pine-dawn.ts +45 -0
- package/src/plugins/themes/rose-pine.ts +45 -0
- package/src/plugins/themes/solarized-light.ts +45 -0
- package/src/plugins/themes/tokyo-night.ts +49 -0
- package/src/plugins/types/agent.ts +108 -0
- package/src/plugins/types/base.ts +267 -0
- package/src/plugins/types/index.ts +19 -0
- package/src/plugins/types/notification.ts +51 -0
- package/src/plugins/types/provider.ts +233 -0
- package/src/plugins/types/theme.ts +70 -0
- package/src/plugins/update-checker.ts +177 -0
- package/src/pricing/estimator.test.ts +181 -0
- package/src/pricing/estimator.ts +80 -0
- package/src/pricing/fallback.test.ts +71 -0
- package/src/pricing/fallback.ts +62 -0
- package/src/pricing/index.ts +66 -0
- package/src/pricing/models-dev.ts +130 -0
- package/src/storage/database.ts +613 -0
- package/src/storage/db.ts +64 -0
- package/src/storage/index.ts +66 -0
- package/src/storage/migrations/index.ts +189 -0
- package/src/storage/paths.ts +24 -0
- package/src/storage/repos/agentSessions.ts +210 -0
- package/src/storage/repos/providerSnapshots.ts +116 -0
- package/src/storage/repos/usageEvents.ts +334 -0
- package/src/storage/types.ts +288 -0
- package/src/tui/App.tsx +381 -0
- package/src/tui/components/CommandPalette.tsx +166 -0
- package/src/tui/components/DebugConsole.tsx +161 -0
- package/src/tui/components/DebugPanel.tsx +660 -0
- package/src/tui/components/FooterHints.tsx +22 -0
- package/src/tui/components/GhostProviderCard.tsx +45 -0
- package/src/tui/components/Header.tsx +125 -0
- package/src/tui/components/HelpOverlay.tsx +45 -0
- package/src/tui/components/InlineGauge.tsx +34 -0
- package/src/tui/components/InlineSparkline.tsx +78 -0
- package/src/tui/components/KpiStrip.tsx +246 -0
- package/src/tui/components/LimitGauge.tsx +186 -0
- package/src/tui/components/ModalBackdrop.tsx +42 -0
- package/src/tui/components/ProviderAggregateStrip.tsx +200 -0
- package/src/tui/components/ProviderCard.tsx +309 -0
- package/src/tui/components/ProviderDetailPanel.tsx +237 -0
- package/src/tui/components/ProviderLimitsPanel.tsx +308 -0
- package/src/tui/components/ProvidersList.tsx +380 -0
- package/src/tui/components/SessionDetailsDrawer.tsx +317 -0
- package/src/tui/components/SessionsTable.tsx +401 -0
- package/src/tui/components/SettingsModal.tsx +587 -0
- package/src/tui/components/SidebarBreakdown.tsx +87 -0
- package/src/tui/components/Skeleton.tsx +102 -0
- package/src/tui/components/SmartSidebar.tsx +644 -0
- package/src/tui/components/Sparkline.tsx +180 -0
- package/src/tui/components/Spinner.tsx +36 -0
- package/src/tui/components/StatusBar.tsx +96 -0
- package/src/tui/components/ThemePicker.tsx +234 -0
- package/src/tui/components/Toast.tsx +61 -0
- package/src/tui/components/UsageGauge.tsx +80 -0
- package/src/tui/components/index.ts +5 -0
- package/src/tui/contexts/AgentSessionContext.tsx +307 -0
- package/src/tui/contexts/ConfigContext.tsx +141 -0
- package/src/tui/contexts/DashboardRuntimeContext.tsx +102 -0
- package/src/tui/contexts/DemoModeContext.tsx +60 -0
- package/src/tui/contexts/DrawerContext.tsx +42 -0
- package/src/tui/contexts/InputContext.tsx +30 -0
- package/src/tui/contexts/LogContext.tsx +150 -0
- package/src/tui/contexts/PluginContext.tsx +424 -0
- package/src/tui/contexts/RealTimeActivityContext.tsx +119 -0
- package/src/tui/contexts/StorageContext.tsx +233 -0
- package/src/tui/contexts/ThemeContext.tsx +95 -0
- package/src/tui/contexts/TimeWindowContext.tsx +158 -0
- package/src/tui/contexts/ToastContext.tsx +40 -0
- package/src/tui/contexts/index.ts +5 -0
- package/src/tui/createApp.tsx +47 -0
- package/src/tui/debug/captureFrame.ts +262 -0
- package/src/tui/debug/snapshot.tsx +644 -0
- package/src/tui/driver/assertions.ts +279 -0
- package/src/tui/driver/cli.ts +717 -0
- package/src/tui/driver/coverage.ts +204 -0
- package/src/tui/driver/demo-snapshot.ts +66 -0
- package/src/tui/driver/diff.ts +166 -0
- package/src/tui/driver/driver.ts +270 -0
- package/src/tui/driver/index.ts +53 -0
- package/src/tui/driver/recorder.ts +303 -0
- package/src/tui/driver/test-workflow.ts +115 -0
- package/src/tui/driver/test.ts +47 -0
- package/src/tui/hooks/index.ts +20 -0
- package/src/tui/hooks/useAnimatedValue.ts +113 -0
- package/src/tui/hooks/useDashboardKeyboard.ts +341 -0
- package/src/tui/hooks/useDashboardState.ts +116 -0
- package/src/tui/hooks/useEmaActivity.ts +223 -0
- package/src/tui/hooks/useEntranceAnimation.ts +62 -0
- package/src/tui/hooks/useExitAnimation.ts +137 -0
- package/src/tui/hooks/usePulse.ts +138 -0
- package/src/tui/hooks/useSafeRenderer.ts +24 -0
- package/src/tui/hooks/useValueFlash.ts +111 -0
- package/src/tui/index.tsx +50 -0
- package/src/tui/utils/providerColor.ts +32 -0
- package/src/tui/views/Dashboard.tsx +340 -0
- package/src/tui/views/HistoricalTrendsView.tsx +1431 -0
- package/src/tui/views/ProjectsView.tsx +1277 -0
- package/src/tui/views/RealTimeDashboard.tsx +393 -0
- package/src/tui/views/SettingsView.tsx +465 -0
- package/src/tui/views/index.ts +2 -0
- package/src/types/global.d.ts +5 -0
- package/src/types/opentui-test-utils.d.ts +18 -0
- package/src/utils/clipboard.ts +118 -0
- package/src/utils/currency.test.ts +106 -0
- package/src/utils/currency.ts +79 -0
- package/src/version.ts +1 -0
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import { useRef, useEffect } from 'react';
|
|
2
|
+
import { useKeyboard } from '@opentui/react';
|
|
3
|
+
import { useInputFocus } from '../contexts/InputContext.tsx';
|
|
4
|
+
import { useTimeWindow } from '../contexts/TimeWindowContext.tsx';
|
|
5
|
+
import { useToastContext } from '../contexts/ToastContext.tsx';
|
|
6
|
+
import { useAgentSessions } from '../contexts/AgentSessionContext.tsx';
|
|
7
|
+
import { copyToClipboard } from '@/utils/clipboard.ts';
|
|
8
|
+
import type { AgentSessionAggregate } from '../../agents/types.ts';
|
|
9
|
+
import type { DriverDimension, SidebarMode } from '../components/SmartSidebar.tsx';
|
|
10
|
+
|
|
11
|
+
function formatSessionSummary(session: AgentSessionAggregate): string {
|
|
12
|
+
const effectiveTokens = session.totals.input + session.totals.output;
|
|
13
|
+
const cost = session.totalCostUsd?.toFixed(4) ?? '0.00';
|
|
14
|
+
const primaryModel = session.streams[0]?.modelId ?? 'unknown';
|
|
15
|
+
const duration = Math.round((session.lastActivityAt - session.startedAt) / 1000);
|
|
16
|
+
const durationStr = duration > 3600
|
|
17
|
+
? `${Math.floor(duration / 3600)}h ${Math.floor((duration % 3600) / 60)}m`
|
|
18
|
+
: duration > 60
|
|
19
|
+
? `${Math.floor(duration / 60)}m ${duration % 60}s`
|
|
20
|
+
: `${duration}s`;
|
|
21
|
+
|
|
22
|
+
const cacheRead = session.totals.cacheRead ?? 0;
|
|
23
|
+
const cacheWrite = session.totals.cacheWrite ?? 0;
|
|
24
|
+
|
|
25
|
+
const lines = [
|
|
26
|
+
`Session: ${session.sessionId}`,
|
|
27
|
+
`Agent: ${session.agentName}`,
|
|
28
|
+
`Model: ${primaryModel}`,
|
|
29
|
+
`Status: ${session.status}`,
|
|
30
|
+
`Duration: ${durationStr}`,
|
|
31
|
+
`Tokens: ${effectiveTokens.toLocaleString()} (in: ${session.totals.input.toLocaleString()}, out: ${session.totals.output.toLocaleString()})`,
|
|
32
|
+
`Cost: $${cost}`,
|
|
33
|
+
`Requests: ${session.requestCount}`,
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
if (cacheRead > 0 || cacheWrite > 0) {
|
|
37
|
+
const totalInput = session.totals.input + cacheRead + cacheWrite;
|
|
38
|
+
const hitRate = totalInput > 0 ? Math.round((cacheRead / totalInput) * 100) : 0;
|
|
39
|
+
lines.push(`Cache: ${hitRate}% hit (read: ${cacheRead.toLocaleString()}, write: ${cacheWrite.toLocaleString()})`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (session.projectPath) lines.push(`Project: ${session.projectPath}`);
|
|
43
|
+
return lines.join('\n');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface DashboardKeyboardState {
|
|
47
|
+
showHelp: boolean;
|
|
48
|
+
showSessionDrawer: boolean;
|
|
49
|
+
selectedRow: number;
|
|
50
|
+
focusedPanel: 'sessions' | 'sidebar' | 'limits';
|
|
51
|
+
sidebarCollapsed: boolean;
|
|
52
|
+
filterQuery: string;
|
|
53
|
+
isFiltering: boolean;
|
|
54
|
+
sortField: 'cost' | 'tokens' | 'time';
|
|
55
|
+
pendingG: boolean;
|
|
56
|
+
scrollOffset: number;
|
|
57
|
+
limitSelectedIndex: number;
|
|
58
|
+
providerCount: number;
|
|
59
|
+
driverDimension: DriverDimension;
|
|
60
|
+
selectedDriverIndex: number;
|
|
61
|
+
activeDriverFilter: string | null;
|
|
62
|
+
sidebarMode: SidebarMode;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface DashboardKeyboardActions {
|
|
66
|
+
setShowHelp: (fn: (prev: boolean) => boolean) => void;
|
|
67
|
+
openSessionDrawer: () => void;
|
|
68
|
+
closeSessionDrawer: () => void;
|
|
69
|
+
setSelectedRow: (fn: (prev: number) => number) => void;
|
|
70
|
+
setFocusedPanel: (fn: (prev: 'sessions' | 'sidebar' | 'limits') => 'sessions' | 'sidebar' | 'limits') => void;
|
|
71
|
+
setSidebarCollapsed: (fn: (prev: boolean) => boolean) => void;
|
|
72
|
+
setFilterQuery: (fn: (prev: string) => string) => void;
|
|
73
|
+
setIsFiltering: (val: boolean) => void;
|
|
74
|
+
setSortField: (fn: (prev: 'cost' | 'tokens' | 'time') => 'cost' | 'tokens' | 'time') => void;
|
|
75
|
+
setPendingG: (val: boolean) => void;
|
|
76
|
+
setScrollOffset: (val: number) => void;
|
|
77
|
+
setLimitSelectedIndex: (fn: (prev: number) => number) => void;
|
|
78
|
+
setDriverDimension: (fn: (prev: DriverDimension) => DriverDimension) => void;
|
|
79
|
+
setSelectedDriverIndex: (fn: (prev: number) => number) => void;
|
|
80
|
+
setActiveDriverFilter: (val: string | null) => void;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
interface UseDashboardKeyboardProps {
|
|
84
|
+
state: DashboardKeyboardState;
|
|
85
|
+
actions: DashboardKeyboardActions;
|
|
86
|
+
processedSessions: AgentSessionAggregate[];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function useDashboardKeyboard({
|
|
90
|
+
state,
|
|
91
|
+
actions,
|
|
92
|
+
processedSessions,
|
|
93
|
+
}: UseDashboardKeyboardProps) {
|
|
94
|
+
const { setInputFocused } = useInputFocus();
|
|
95
|
+
const { cycleWindow, cycleWindowBack, cycleBudgetLock } = useTimeWindow();
|
|
96
|
+
const { showToast } = useToastContext();
|
|
97
|
+
const { refreshSessions } = useAgentSessions();
|
|
98
|
+
|
|
99
|
+
const isFilteringRef = useRef(state.isFiltering);
|
|
100
|
+
const modalOpenRef = useRef(false);
|
|
101
|
+
const pendingGRef = useRef(false);
|
|
102
|
+
const sessionsRef = useRef(processedSessions);
|
|
103
|
+
const focusedPanelRef = useRef(state.focusedPanel);
|
|
104
|
+
const providerCountRef = useRef(state.providerCount);
|
|
105
|
+
const filterQueryRef = useRef(state.filterQuery);
|
|
106
|
+
const driverDimensionRef = useRef(state.driverDimension);
|
|
107
|
+
const selectedDriverIndexRef = useRef(state.selectedDriverIndex);
|
|
108
|
+
const activeDriverFilterRef = useRef(state.activeDriverFilter);
|
|
109
|
+
const sidebarModeRef = useRef(state.sidebarMode);
|
|
110
|
+
|
|
111
|
+
useEffect(() => {
|
|
112
|
+
isFilteringRef.current = state.isFiltering;
|
|
113
|
+
modalOpenRef.current = state.showHelp || state.showSessionDrawer;
|
|
114
|
+
pendingGRef.current = state.pendingG;
|
|
115
|
+
sessionsRef.current = processedSessions;
|
|
116
|
+
focusedPanelRef.current = state.focusedPanel;
|
|
117
|
+
providerCountRef.current = state.providerCount;
|
|
118
|
+
filterQueryRef.current = state.filterQuery;
|
|
119
|
+
driverDimensionRef.current = state.driverDimension;
|
|
120
|
+
selectedDriverIndexRef.current = state.selectedDriverIndex;
|
|
121
|
+
activeDriverFilterRef.current = state.activeDriverFilter;
|
|
122
|
+
sidebarModeRef.current = state.sidebarMode;
|
|
123
|
+
}, [state.isFiltering, state.showHelp, state.showSessionDrawer, state.pendingG, processedSessions, state.focusedPanel, state.providerCount, state.filterQuery, state.driverDimension, state.selectedDriverIndex, state.activeDriverFilter, state.sidebarMode]);
|
|
124
|
+
|
|
125
|
+
const { isInputFocused } = useInputFocus();
|
|
126
|
+
|
|
127
|
+
useKeyboard((key) => {
|
|
128
|
+
// Let App.tsx handle global Ctrl+ shortcuts (Ctrl+P for capture, Ctrl+S for save, etc.)
|
|
129
|
+
if (key.ctrl) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Allow filter mode to handle its own input (no <input> element in RealTimeDashboard)
|
|
134
|
+
if (isInputFocused && !isFilteringRef.current) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (key.sequence === '?' || (key.shift && key.name === '/')) {
|
|
139
|
+
actions.setShowHelp(prev => !prev);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (modalOpenRef.current) {
|
|
144
|
+
if (key.name === 'escape' || key.name === 'q' || key.sequence === '?') {
|
|
145
|
+
actions.setShowHelp(() => false);
|
|
146
|
+
actions.closeSessionDrawer();
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const selectedSession = processedSessions[state.selectedRow];
|
|
151
|
+
if (state.showSessionDrawer && selectedSession) {
|
|
152
|
+
if (key.name === 'c') {
|
|
153
|
+
const summary = formatSessionSummary(selectedSession);
|
|
154
|
+
copyToClipboard(summary).then(() => {
|
|
155
|
+
showToast('Copied to clipboard');
|
|
156
|
+
}).catch(() => {
|
|
157
|
+
showToast('Copy failed', 'error');
|
|
158
|
+
});
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (key.name === 'x') {
|
|
162
|
+
showToast('Export not yet implemented', 'info');
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (isFilteringRef.current) {
|
|
170
|
+
if (key.name === 'enter' || key.name === 'return') {
|
|
171
|
+
actions.setIsFiltering(false);
|
|
172
|
+
setInputFocused(false);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (key.name === 'escape') {
|
|
176
|
+
actions.setFilterQuery(() => '');
|
|
177
|
+
actions.setIsFiltering(false);
|
|
178
|
+
setInputFocused(false);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (key.name === 'backspace') {
|
|
182
|
+
actions.setFilterQuery(q => q.slice(0, -1));
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
if (key.sequence && key.sequence.length === 1 && /^[a-zA-Z0-9\-_./]$/.test(key.sequence)) {
|
|
186
|
+
actions.setFilterQuery(q => q + key.sequence);
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (key.name === 'tab' && !key.shift) {
|
|
193
|
+
actions.setFocusedPanel(curr => {
|
|
194
|
+
if (curr === 'sessions') return 'limits';
|
|
195
|
+
if (curr === 'limits') return 'sidebar';
|
|
196
|
+
return 'sessions';
|
|
197
|
+
});
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
if (key.name === 'tab' && key.shift) {
|
|
201
|
+
actions.setFocusedPanel(curr => {
|
|
202
|
+
if (curr === 'sessions') return 'sidebar';
|
|
203
|
+
if (curr === 'sidebar') return 'limits';
|
|
204
|
+
return 'sessions';
|
|
205
|
+
});
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (key.name === 'l' && focusedPanelRef.current !== 'limits') {
|
|
210
|
+
actions.setFocusedPanel(() => 'limits');
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (key.name === 'i') {
|
|
215
|
+
actions.setSidebarCollapsed(curr => !curr);
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (key.name === '/' || key.sequence === '/') {
|
|
220
|
+
actions.setIsFiltering(true);
|
|
221
|
+
setInputFocused(true);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (key.name === 's') {
|
|
226
|
+
actions.setSortField(curr => curr === 'cost' ? 'tokens' : 'cost');
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Clear applied filter with Escape (when not in typing mode)
|
|
231
|
+
if (key.name === 'escape' && filterQueryRef.current) {
|
|
232
|
+
actions.setFilterQuery(() => '');
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (key.name === 't' && key.shift) {
|
|
237
|
+
cycleWindowBack();
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (key.name === 't') {
|
|
242
|
+
cycleWindow();
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (key.name === 'r') {
|
|
247
|
+
refreshSessions();
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (focusedPanelRef.current === 'sessions') {
|
|
252
|
+
const sessions = sessionsRef.current;
|
|
253
|
+
if (key.name === 'down' || key.name === 'j') {
|
|
254
|
+
actions.setPendingG(false);
|
|
255
|
+
actions.setSelectedRow(curr => Math.min(curr + 1, sessions.length - 1));
|
|
256
|
+
} else if (key.name === 'up' || key.name === 'k') {
|
|
257
|
+
actions.setPendingG(false);
|
|
258
|
+
actions.setSelectedRow(curr => Math.max(curr - 1, 0));
|
|
259
|
+
} else if (key.shift && key.name === 'g') {
|
|
260
|
+
actions.setPendingG(false);
|
|
261
|
+
actions.setSelectedRow(() => sessions.length - 1);
|
|
262
|
+
} else if (key.name === 'g') {
|
|
263
|
+
if (pendingGRef.current) {
|
|
264
|
+
actions.setSelectedRow(() => 0);
|
|
265
|
+
actions.setScrollOffset(0);
|
|
266
|
+
actions.setPendingG(false);
|
|
267
|
+
} else {
|
|
268
|
+
actions.setPendingG(true);
|
|
269
|
+
setTimeout(() => actions.setPendingG(false), 500);
|
|
270
|
+
}
|
|
271
|
+
} else if (key.name === 'return' && sessions.length > 0) {
|
|
272
|
+
actions.setPendingG(false);
|
|
273
|
+
actions.openSessionDrawer();
|
|
274
|
+
} else {
|
|
275
|
+
actions.setPendingG(false);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (focusedPanelRef.current === 'limits') {
|
|
280
|
+
const maxIndex = Math.max(0, providerCountRef.current - 1);
|
|
281
|
+
if (key.name === 'left' || key.name === 'h') {
|
|
282
|
+
actions.setLimitSelectedIndex(curr => Math.max(curr - 1, 0));
|
|
283
|
+
return;
|
|
284
|
+
} else if (key.name === 'right' || key.name === 'l') {
|
|
285
|
+
actions.setLimitSelectedIndex(curr => Math.min(curr + 1, maxIndex));
|
|
286
|
+
return;
|
|
287
|
+
} else if (key.name === 'escape') {
|
|
288
|
+
actions.setFocusedPanel(() => 'sessions');
|
|
289
|
+
actions.setLimitSelectedIndex(() => 0);
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (focusedPanelRef.current === 'sidebar') {
|
|
295
|
+
if (key.name === 'down' || key.name === 'j') {
|
|
296
|
+
actions.setSelectedDriverIndex(curr => curr + 1);
|
|
297
|
+
return;
|
|
298
|
+
} else if (key.name === 'up' || key.name === 'k') {
|
|
299
|
+
actions.setSelectedDriverIndex(curr => Math.max(curr - 1, 0));
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (key.name === 'return') {
|
|
304
|
+
if (activeDriverFilterRef.current !== null) {
|
|
305
|
+
actions.setActiveDriverFilter(null);
|
|
306
|
+
} else {
|
|
307
|
+
actions.setActiveDriverFilter('__TOGGLE_SELECTED__');
|
|
308
|
+
}
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (key.name === 'm') {
|
|
313
|
+
actions.setDriverDimension(() => 'model');
|
|
314
|
+
actions.setSelectedDriverIndex(() => 0);
|
|
315
|
+
return;
|
|
316
|
+
} else if (key.name === 'p') {
|
|
317
|
+
actions.setDriverDimension(() => 'project');
|
|
318
|
+
actions.setSelectedDriverIndex(() => 0);
|
|
319
|
+
return;
|
|
320
|
+
} else if (key.name === 'a') {
|
|
321
|
+
actions.setDriverDimension(() => 'agent');
|
|
322
|
+
actions.setSelectedDriverIndex(() => 0);
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (key.name === 'b') {
|
|
327
|
+
cycleBudgetLock();
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (key.name === 'escape') {
|
|
332
|
+
if (activeDriverFilterRef.current !== null) {
|
|
333
|
+
actions.setActiveDriverFilter(null);
|
|
334
|
+
} else {
|
|
335
|
+
actions.setFocusedPanel(() => 'sessions');
|
|
336
|
+
}
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { useState, useMemo, useRef, useEffect } from 'react';
|
|
2
|
+
import { totalTokenCount } from '../../agents/types.ts';
|
|
3
|
+
import type { AgentSessionAggregate, AgentSessionStream } from '../../agents/types.ts';
|
|
4
|
+
|
|
5
|
+
export interface DashboardDeltas {
|
|
6
|
+
cost: number;
|
|
7
|
+
tokens: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface UseDashboardStateResult {
|
|
11
|
+
showHelp: boolean;
|
|
12
|
+
setShowHelp: React.Dispatch<React.SetStateAction<boolean>>;
|
|
13
|
+
showDebugInspector: boolean;
|
|
14
|
+
setShowDebugInspector: React.Dispatch<React.SetStateAction<boolean>>;
|
|
15
|
+
selectedRow: number;
|
|
16
|
+
setSelectedRow: React.Dispatch<React.SetStateAction<number>>;
|
|
17
|
+
focusedPanel: 'sessions' | 'sidebar';
|
|
18
|
+
setFocusedPanel: React.Dispatch<React.SetStateAction<'sessions' | 'sidebar'>>;
|
|
19
|
+
sidebarCollapsed: boolean;
|
|
20
|
+
setSidebarCollapsed: React.Dispatch<React.SetStateAction<boolean>>;
|
|
21
|
+
filterQuery: string;
|
|
22
|
+
setFilterQuery: React.Dispatch<React.SetStateAction<string>>;
|
|
23
|
+
isFiltering: boolean;
|
|
24
|
+
setIsFiltering: React.Dispatch<React.SetStateAction<boolean>>;
|
|
25
|
+
sortField: 'cost' | 'tokens' | 'time';
|
|
26
|
+
setSortField: React.Dispatch<React.SetStateAction<'cost' | 'tokens' | 'time'>>;
|
|
27
|
+
deltas: DashboardDeltas;
|
|
28
|
+
processedSessions: AgentSessionAggregate[];
|
|
29
|
+
totalCost: number;
|
|
30
|
+
totalTokens: number;
|
|
31
|
+
totalRequests: number;
|
|
32
|
+
activeCount: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function useDashboardState(sessions: AgentSessionAggregate[]): UseDashboardStateResult {
|
|
36
|
+
const [showHelp, setShowHelp] = useState(false);
|
|
37
|
+
const [showDebugInspector, setShowDebugInspector] = useState(false);
|
|
38
|
+
const [selectedRow, setSelectedRow] = useState(0);
|
|
39
|
+
const [focusedPanel, setFocusedPanel] = useState<'sessions' | 'sidebar'>('sessions');
|
|
40
|
+
const [sidebarCollapsed, setSidebarCollapsed] = useState(false);
|
|
41
|
+
const [filterQuery, setFilterQuery] = useState('');
|
|
42
|
+
const [isFiltering, setIsFiltering] = useState(false);
|
|
43
|
+
const [sortField, setSortField] = useState<'cost' | 'tokens' | 'time'>('cost');
|
|
44
|
+
|
|
45
|
+
const historyRef = useRef<{time: number, cost: number, tokens: number}[]>([]);
|
|
46
|
+
const [deltas, setDeltas] = useState<DashboardDeltas>({ cost: 0, tokens: 0 });
|
|
47
|
+
|
|
48
|
+
const totalCost = sessions.reduce((acc, s) => acc + (s.totalCostUsd ?? 0), 0);
|
|
49
|
+
const totalTokens = sessions.reduce((acc, s) => acc + totalTokenCount(s.totals), 0);
|
|
50
|
+
const totalRequests = sessions.reduce((acc, s) => acc + s.requestCount, 0);
|
|
51
|
+
const activeCount = sessions.filter(s => s.status === 'active').length;
|
|
52
|
+
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
const currentTime = Date.now();
|
|
55
|
+
historyRef.current.push({ time: currentTime, cost: totalCost, tokens: totalTokens });
|
|
56
|
+
if (historyRef.current.length > 300) historyRef.current.shift();
|
|
57
|
+
|
|
58
|
+
const fiveMinAgo = historyRef.current[0];
|
|
59
|
+
if (fiveMinAgo) {
|
|
60
|
+
setDeltas({
|
|
61
|
+
cost: totalCost - fiveMinAgo.cost,
|
|
62
|
+
tokens: totalTokens - fiveMinAgo.tokens
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}, [sessions, totalCost, totalTokens]);
|
|
66
|
+
|
|
67
|
+
const processedSessions = useMemo(() => {
|
|
68
|
+
let result = [...sessions];
|
|
69
|
+
|
|
70
|
+
if (filterQuery) {
|
|
71
|
+
const q = filterQuery.toLowerCase();
|
|
72
|
+
result = result.filter(s =>
|
|
73
|
+
s.agentName.toLowerCase().includes(q) ||
|
|
74
|
+
s.streams.some((st: AgentSessionStream) => st.modelId.toLowerCase().includes(q)) ||
|
|
75
|
+
(s.projectPath?.toLowerCase().includes(q) ?? false)
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
result.sort((a, b) => {
|
|
80
|
+
const aActive = a.status === 'active' ? 1 : 0;
|
|
81
|
+
const bActive = b.status === 'active' ? 1 : 0;
|
|
82
|
+
if (bActive !== aActive) return bActive - aActive;
|
|
83
|
+
|
|
84
|
+
if (sortField === 'cost') return (b.totalCostUsd ?? 0) - (a.totalCostUsd ?? 0);
|
|
85
|
+
if (sortField === 'tokens') return (b.totals.input + b.totals.output) - (a.totals.input + a.totals.output);
|
|
86
|
+
return b.lastActivityAt - a.lastActivityAt;
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
return result;
|
|
90
|
+
}, [sessions, filterQuery, sortField]);
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
showHelp,
|
|
94
|
+
setShowHelp,
|
|
95
|
+
showDebugInspector,
|
|
96
|
+
setShowDebugInspector,
|
|
97
|
+
selectedRow,
|
|
98
|
+
setSelectedRow,
|
|
99
|
+
focusedPanel,
|
|
100
|
+
setFocusedPanel,
|
|
101
|
+
sidebarCollapsed,
|
|
102
|
+
setSidebarCollapsed,
|
|
103
|
+
filterQuery,
|
|
104
|
+
setFilterQuery,
|
|
105
|
+
isFiltering,
|
|
106
|
+
setIsFiltering,
|
|
107
|
+
sortField,
|
|
108
|
+
setSortField,
|
|
109
|
+
deltas,
|
|
110
|
+
processedSessions,
|
|
111
|
+
totalCost,
|
|
112
|
+
totalTokens,
|
|
113
|
+
totalRequests,
|
|
114
|
+
activeCount,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { useState, useEffect, useRef, useCallback } from 'react';
|
|
2
|
+
|
|
3
|
+
const BUCKET_COUNT = 60;
|
|
4
|
+
const AVG_WINDOW = 10;
|
|
5
|
+
|
|
6
|
+
export interface ActivityState {
|
|
7
|
+
instantRate: number;
|
|
8
|
+
avgRate: number;
|
|
9
|
+
isSpike: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ActivityDebugData {
|
|
13
|
+
lastDeltaTokens: number;
|
|
14
|
+
lastDt: number;
|
|
15
|
+
bucketsShifted: number;
|
|
16
|
+
currentBucketValue: number;
|
|
17
|
+
refreshCount: number;
|
|
18
|
+
lastRefreshTime: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface UseActivityRateResult {
|
|
22
|
+
activity: ActivityState;
|
|
23
|
+
sparkData: number[];
|
|
24
|
+
debugDataRef: React.MutableRefObject<ActivityDebugData>;
|
|
25
|
+
injectDelta: (delta: number) => void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function useEmaActivity(totalTokens: number): UseActivityRateResult {
|
|
29
|
+
const [buckets, setBuckets] = useState<number[]>(() => Array(BUCKET_COUNT).fill(0));
|
|
30
|
+
const [activity, setActivity] = useState<ActivityState>({
|
|
31
|
+
instantRate: 0, avgRate: 0, isSpike: false
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const stateRef = useRef<{
|
|
35
|
+
lastTokens: number;
|
|
36
|
+
lastTokenTime: number;
|
|
37
|
+
lastShiftTime: number;
|
|
38
|
+
initialized: boolean;
|
|
39
|
+
}>({
|
|
40
|
+
lastTokens: -1,
|
|
41
|
+
lastTokenTime: Date.now(),
|
|
42
|
+
lastShiftTime: Date.now(),
|
|
43
|
+
initialized: false,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const debugDataRef = useRef<ActivityDebugData>({
|
|
47
|
+
lastDeltaTokens: 0,
|
|
48
|
+
lastDt: 0,
|
|
49
|
+
bucketsShifted: 0,
|
|
50
|
+
currentBucketValue: 0,
|
|
51
|
+
refreshCount: 0,
|
|
52
|
+
lastRefreshTime: Date.now(),
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const calculateRates = useCallback((bucketData: number[]) => {
|
|
56
|
+
const recentBuckets = bucketData.slice(-AVG_WINDOW);
|
|
57
|
+
|
|
58
|
+
const instantRate = Math.max(...recentBuckets);
|
|
59
|
+
const avgRate = recentBuckets.reduce((a, b) => a + b, 0) / AVG_WINDOW;
|
|
60
|
+
|
|
61
|
+
const isSpike = instantRate >= 500 && instantRate >= avgRate * 3;
|
|
62
|
+
|
|
63
|
+
return { instantRate, avgRate, isSpike };
|
|
64
|
+
}, []);
|
|
65
|
+
|
|
66
|
+
const shiftBucketsWithDecay = useCallback((bucketData: number[], count: number): number[] => {
|
|
67
|
+
if (count <= 0) return bucketData;
|
|
68
|
+
const newBuckets = [...bucketData];
|
|
69
|
+
const shift = Math.min(count, BUCKET_COUNT);
|
|
70
|
+
const decayFactor = 0.7;
|
|
71
|
+
const zeroThreshold = 0.5; // Values below this become true zero (shows as dot)
|
|
72
|
+
|
|
73
|
+
for (let i = 0; i < BUCKET_COUNT - shift; i++) {
|
|
74
|
+
const val = bucketData[i + shift] ?? 0;
|
|
75
|
+
newBuckets[i] = val < zeroThreshold ? 0 : val;
|
|
76
|
+
}
|
|
77
|
+
for (let i = BUCKET_COUNT - shift; i < BUCKET_COUNT; i++) {
|
|
78
|
+
const prevValue = bucketData[BUCKET_COUNT - shift - 1] ?? 0;
|
|
79
|
+
const decaySteps = i - (BUCKET_COUNT - shift) + 1;
|
|
80
|
+
const decayed = prevValue * Math.pow(decayFactor, decaySteps);
|
|
81
|
+
newBuckets[i] = decayed < zeroThreshold ? 0 : decayed;
|
|
82
|
+
}
|
|
83
|
+
return newBuckets;
|
|
84
|
+
}, []);
|
|
85
|
+
|
|
86
|
+
const applyActivityRamp = useCallback((bucketData: number[], peakRate: number, fillCount: number): number[] => {
|
|
87
|
+
const newBuckets = [...bucketData];
|
|
88
|
+
const rampWidth = Math.min(fillCount + 2, 6);
|
|
89
|
+
|
|
90
|
+
for (let i = 0; i < rampWidth; i++) {
|
|
91
|
+
const bucketIdx = BUCKET_COUNT - 1 - i;
|
|
92
|
+
if (bucketIdx < 0) break;
|
|
93
|
+
|
|
94
|
+
const distanceFromPeak = i;
|
|
95
|
+
const rampMultiplier = Math.max(0.15, 1 - (distanceFromPeak * 0.25));
|
|
96
|
+
const rampedValue = peakRate * rampMultiplier;
|
|
97
|
+
|
|
98
|
+
newBuckets[bucketIdx] = Math.max(newBuckets[bucketIdx] ?? 0, rampedValue);
|
|
99
|
+
}
|
|
100
|
+
return newBuckets;
|
|
101
|
+
}, []);
|
|
102
|
+
|
|
103
|
+
const injectDelta = useCallback((delta: number) => {
|
|
104
|
+
if (delta <= 0 || delta >= 1000000) return;
|
|
105
|
+
|
|
106
|
+
const now = Date.now();
|
|
107
|
+
const rate = delta;
|
|
108
|
+
|
|
109
|
+
debugDataRef.current.lastDeltaTokens = delta;
|
|
110
|
+
debugDataRef.current.lastDt = 1;
|
|
111
|
+
debugDataRef.current.currentBucketValue = rate;
|
|
112
|
+
|
|
113
|
+
setBuckets(prev => {
|
|
114
|
+
const secSinceShift = (now - stateRef.current.lastShiftTime) / 1000;
|
|
115
|
+
const bucketsToShift = Math.floor(secSinceShift);
|
|
116
|
+
|
|
117
|
+
let newBuckets = shiftBucketsWithDecay(prev, bucketsToShift);
|
|
118
|
+
if (bucketsToShift > 0) {
|
|
119
|
+
stateRef.current.lastShiftTime = now;
|
|
120
|
+
debugDataRef.current.bucketsShifted = bucketsToShift;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
newBuckets = applyActivityRamp(newBuckets, rate, bucketsToShift);
|
|
124
|
+
|
|
125
|
+
const rates = calculateRates(newBuckets);
|
|
126
|
+
setActivity(rates);
|
|
127
|
+
return newBuckets;
|
|
128
|
+
});
|
|
129
|
+
}, [calculateRates, shiftBucketsWithDecay, applyActivityRamp]);
|
|
130
|
+
|
|
131
|
+
useEffect(() => {
|
|
132
|
+
const now = Date.now();
|
|
133
|
+
|
|
134
|
+
debugDataRef.current.refreshCount++;
|
|
135
|
+
debugDataRef.current.lastRefreshTime = now;
|
|
136
|
+
|
|
137
|
+
const prevTokens = stateRef.current.lastTokens;
|
|
138
|
+
const wasInitialized = stateRef.current.initialized;
|
|
139
|
+
|
|
140
|
+
if (prevTokens === -1 || prevTokens === 0 || !wasInitialized) {
|
|
141
|
+
stateRef.current = {
|
|
142
|
+
lastTokens: totalTokens,
|
|
143
|
+
lastTokenTime: now,
|
|
144
|
+
lastShiftTime: now,
|
|
145
|
+
initialized: totalTokens > 0,
|
|
146
|
+
};
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const deltaTokens = Math.max(0, totalTokens - prevTokens);
|
|
151
|
+
const rawDt = (now - stateRef.current.lastTokenTime) / 1000;
|
|
152
|
+
|
|
153
|
+
// Require minimum 0.5s between rate calculations
|
|
154
|
+
// DON'T update lastTokens here — that would silently consume the delta
|
|
155
|
+
// without ever writing it to buckets. Let the delta accumulate until
|
|
156
|
+
// enough time has passed for a proper rate calculation.
|
|
157
|
+
if (rawDt < 0.5) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Cap dt at 10s so long idle gaps don't dilute the rate when tokens finally arrive
|
|
162
|
+
// This shows activity as a "burst" rather than averaged over idle time
|
|
163
|
+
const dt = Math.min(Math.max(rawDt, 0.5), 10);
|
|
164
|
+
|
|
165
|
+
debugDataRef.current.lastDeltaTokens = deltaTokens;
|
|
166
|
+
debugDataRef.current.lastDt = dt;
|
|
167
|
+
|
|
168
|
+
if (deltaTokens > 0 && deltaTokens < 1000000) {
|
|
169
|
+
const rate = deltaTokens / dt;
|
|
170
|
+
debugDataRef.current.currentBucketValue = rate;
|
|
171
|
+
|
|
172
|
+
setBuckets(prev => {
|
|
173
|
+
const secSinceShift = (now - stateRef.current.lastShiftTime) / 1000;
|
|
174
|
+
const bucketsToShift = Math.floor(secSinceShift);
|
|
175
|
+
|
|
176
|
+
let newBuckets = shiftBucketsWithDecay(prev, bucketsToShift);
|
|
177
|
+
if (bucketsToShift > 0) {
|
|
178
|
+
stateRef.current.lastShiftTime = now;
|
|
179
|
+
debugDataRef.current.bucketsShifted = bucketsToShift;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
newBuckets = applyActivityRamp(newBuckets, rate, bucketsToShift);
|
|
183
|
+
|
|
184
|
+
const rates = calculateRates(newBuckets);
|
|
185
|
+
setActivity(rates);
|
|
186
|
+
return newBuckets;
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
stateRef.current.lastTokens = totalTokens;
|
|
190
|
+
stateRef.current.lastTokenTime = now;
|
|
191
|
+
}
|
|
192
|
+
}, [totalTokens, calculateRates, shiftBucketsWithDecay, applyActivityRamp]);
|
|
193
|
+
|
|
194
|
+
useEffect(() => {
|
|
195
|
+
const interval = setInterval(() => {
|
|
196
|
+
const now = Date.now();
|
|
197
|
+
const secSinceShift = (now - stateRef.current.lastShiftTime) / 1000;
|
|
198
|
+
|
|
199
|
+
if (secSinceShift >= 1) {
|
|
200
|
+
const bucketsToShift = Math.floor(secSinceShift);
|
|
201
|
+
debugDataRef.current.bucketsShifted = bucketsToShift;
|
|
202
|
+
|
|
203
|
+
setBuckets(prev => {
|
|
204
|
+
const newBuckets = shiftBucketsWithDecay(prev, bucketsToShift);
|
|
205
|
+
stateRef.current.lastShiftTime = now;
|
|
206
|
+
|
|
207
|
+
const rates = calculateRates(newBuckets);
|
|
208
|
+
setActivity(rates);
|
|
209
|
+
return newBuckets;
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}, 1000);
|
|
213
|
+
|
|
214
|
+
return () => clearInterval(interval);
|
|
215
|
+
}, [calculateRates, shiftBucketsWithDecay]);
|
|
216
|
+
|
|
217
|
+
return {
|
|
218
|
+
activity,
|
|
219
|
+
sparkData: buckets,
|
|
220
|
+
debugDataRef,
|
|
221
|
+
injectDelta,
|
|
222
|
+
};
|
|
223
|
+
}
|