@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,393 @@
|
|
|
1
|
+
import { useState, useEffect, useMemo, useRef, useCallback } from 'react';
|
|
2
|
+
import { useTerminalDimensions } from '@opentui/react';
|
|
3
|
+
import type { ScrollBoxRenderable } from '@opentui/core';
|
|
4
|
+
import { useColors } from '../contexts/ThemeContext.tsx';
|
|
5
|
+
import { usePlugins } from '../contexts/PluginContext.tsx';
|
|
6
|
+
import { useAgentSessions } from '../contexts/AgentSessionContext.tsx';
|
|
7
|
+
import { useTimeWindow } from '../contexts/TimeWindowContext.tsx';
|
|
8
|
+
import { useConfig } from '../contexts/ConfigContext.tsx';
|
|
9
|
+
import { useDashboardRuntime } from '../contexts/DashboardRuntimeContext.tsx';
|
|
10
|
+
import { useDrawer } from '../contexts/DrawerContext.tsx';
|
|
11
|
+
import { useDashboardKeyboard } from '../hooks/useDashboardKeyboard.ts';
|
|
12
|
+
import { getProviderColor } from '../utils/providerColor.ts';
|
|
13
|
+
import { notificationBus } from '@/plugins/notification-bus.ts';
|
|
14
|
+
|
|
15
|
+
import { KpiStrip } from '../components/KpiStrip.tsx';
|
|
16
|
+
import { SessionsTable } from '../components/SessionsTable.tsx';
|
|
17
|
+
import { SmartSidebar, getSidebarMode, type DriverDimension } from '../components/SmartSidebar.tsx';
|
|
18
|
+
import { ProviderLimitsPanel } from '../components/ProviderLimitsPanel.tsx';
|
|
19
|
+
import { HelpOverlay } from '../components/HelpOverlay.tsx';
|
|
20
|
+
|
|
21
|
+
export function RealTimeDashboard() {
|
|
22
|
+
const colors = useColors();
|
|
23
|
+
const { providers } = usePlugins();
|
|
24
|
+
const { sessions: agentSessions, isLoading } = useAgentSessions();
|
|
25
|
+
const { windowMs, windowLabel, budgetType, budgetTypeLabel } = useTimeWindow();
|
|
26
|
+
const { height: terminalHeight, width: terminalWidth } = useTerminalDimensions();
|
|
27
|
+
const { config } = useConfig();
|
|
28
|
+
const { activity, sparkData, deltas } = useDashboardRuntime();
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
const sidebarMode = getSidebarMode(terminalWidth);
|
|
32
|
+
const showLargeHeader = terminalHeight >= 35;
|
|
33
|
+
const showProviderLimitsPanel = terminalHeight >= 24;
|
|
34
|
+
|
|
35
|
+
// Visible-row count for scroll tracking — every non-session-data line must be counted.
|
|
36
|
+
const visibleRows = (() => {
|
|
37
|
+
const appChrome = (showLargeHeader ? 7 : 1) + 1; // App.tsx: Header + StatusBar
|
|
38
|
+
const outerPadding = 2; // padding={1} top + bottom
|
|
39
|
+
const kpi = 5; // KpiStrip fragment: h4 cards + h1 rule
|
|
40
|
+
const limits = !showProviderLimitsPanel ? 0 // ProviderLimitsPanel
|
|
41
|
+
: terminalHeight < 30 ? (sidebarMode === 'hidden' ? 2 : 1) // compact (+budget bar)
|
|
42
|
+
: 4; // normal/wide bordered box
|
|
43
|
+
const tableChrome = 4 + (terminalHeight >= 30 ? 1 : 0); // borders + header + columns + inspector
|
|
44
|
+
const footer = 1; // keyboard shortcut bar
|
|
45
|
+
// gap={1} between rendered children; KpiStrip fragment injects 2 children
|
|
46
|
+
const children = 2 + (showProviderLimitsPanel ? 1 : 0) + 1 + 1;
|
|
47
|
+
const gaps = children - 1;
|
|
48
|
+
return Math.max(1, terminalHeight - appChrome - outerPadding - kpi - limits - tableChrome - footer - gaps);
|
|
49
|
+
})();
|
|
50
|
+
|
|
51
|
+
const { showDrawer, isOpen: showSessionDrawer } = useDrawer();
|
|
52
|
+
|
|
53
|
+
const [showHelp, setShowHelp] = useState(false);
|
|
54
|
+
const [selectedRow, setSelectedRow] = useState(0);
|
|
55
|
+
const [focusedPanel, setFocusedPanel] = useState<'sessions' | 'sidebar' | 'limits'>('sessions');
|
|
56
|
+
const [sidebarCollapsed, setSidebarCollapsed] = useState(config.display.sidebarCollapsed);
|
|
57
|
+
const [filterQuery, setFilterQuery] = useState('');
|
|
58
|
+
const [isFiltering, setIsFiltering] = useState(false);
|
|
59
|
+
const [sortField, setSortField] = useState<'cost' | 'tokens' | 'time'>('cost');
|
|
60
|
+
const [pendingG, setPendingG] = useState(false);
|
|
61
|
+
const [scrollOffset, setScrollOffset] = useState(0);
|
|
62
|
+
const [limitSelectedIndex, setLimitSelectedIndex] = useState(0);
|
|
63
|
+
const [driverDimension, setDriverDimension] = useState<DriverDimension>('model');
|
|
64
|
+
const [selectedDriverIndex, setSelectedDriverIndex] = useState(0);
|
|
65
|
+
const [activeDriverFilter, setActiveDriverFilter] = useState<string | null>(null);
|
|
66
|
+
|
|
67
|
+
const effectiveSidebarCollapsed = sidebarCollapsed || sidebarMode === 'hidden';
|
|
68
|
+
const showBudgetInLimits = sidebarMode === 'hidden';
|
|
69
|
+
|
|
70
|
+
const sessionsScrollboxRef = useRef<ScrollBoxRenderable>(null);
|
|
71
|
+
|
|
72
|
+
const configuredProviders = useMemo(() => {
|
|
73
|
+
return Array.from(providers.values())
|
|
74
|
+
.filter(p => p.configured)
|
|
75
|
+
.sort((a, b) => getMaxUsedPercent(b) - getMaxUsedPercent(a));
|
|
76
|
+
}, [providers]);
|
|
77
|
+
|
|
78
|
+
function getMaxUsedPercent(provider: any): number {
|
|
79
|
+
if (!provider.usage?.limits) return 0;
|
|
80
|
+
const items = provider.usage.limits.items ?? [];
|
|
81
|
+
if (items.length > 0) return Math.max(...items.map((i: any) => i.usedPercent ?? 0));
|
|
82
|
+
const primary = provider.usage.limits.primary?.usedPercent ?? 0;
|
|
83
|
+
const secondary = provider.usage.limits.secondary?.usedPercent ?? 0;
|
|
84
|
+
return Math.max(primary, secondary);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const providerColorOf = useCallback(
|
|
88
|
+
(id: string) => getProviderColor(id, providers, colors.primary),
|
|
89
|
+
[providers, colors.primary],
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
const baseFilteredSessions = useMemo(() => {
|
|
93
|
+
let result = [...agentSessions];
|
|
94
|
+
|
|
95
|
+
if (windowMs !== null) {
|
|
96
|
+
const cutoff = Date.now() - windowMs;
|
|
97
|
+
result = result.filter(s => s.lastActivityAt >= cutoff);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (filterQuery) {
|
|
101
|
+
const q = filterQuery.toLowerCase();
|
|
102
|
+
result = result.filter(s =>
|
|
103
|
+
s.agentName.toLowerCase().includes(q) ||
|
|
104
|
+
s.streams.some(st => st.modelId.toLowerCase().includes(q)) ||
|
|
105
|
+
(s.projectPath?.toLowerCase().includes(q) ?? false) ||
|
|
106
|
+
(s.sessionName?.toLowerCase().includes(q) ?? false)
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return result;
|
|
111
|
+
}, [agentSessions, filterQuery, windowMs]);
|
|
112
|
+
|
|
113
|
+
const drivers = useMemo(() => {
|
|
114
|
+
const stats: Record<string, { cost: number }> = {};
|
|
115
|
+
baseFilteredSessions.forEach(s => {
|
|
116
|
+
s.streams.forEach(st => {
|
|
117
|
+
let key: string;
|
|
118
|
+
switch (driverDimension) {
|
|
119
|
+
case 'model': key = st.modelId; break;
|
|
120
|
+
case 'project': key = s.projectPath?.split('/').pop() ?? 'unknown'; break;
|
|
121
|
+
case 'agent': key = s.agentName; break;
|
|
122
|
+
}
|
|
123
|
+
if (!stats[key]) stats[key] = { cost: 0 };
|
|
124
|
+
stats[key]!.cost += st.costUsd ?? 0;
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
return Object.entries(stats)
|
|
128
|
+
.map(([id, data]) => ({ id, cost: data.cost }))
|
|
129
|
+
.sort((a, b) => b.cost - a.cost)
|
|
130
|
+
.slice(0, 5);
|
|
131
|
+
}, [baseFilteredSessions, driverDimension]);
|
|
132
|
+
|
|
133
|
+
useEffect(() => {
|
|
134
|
+
const maxIndex = Math.max(0, drivers.length - 1);
|
|
135
|
+
if (selectedDriverIndex > maxIndex) {
|
|
136
|
+
setSelectedDriverIndex(maxIndex);
|
|
137
|
+
}
|
|
138
|
+
}, [drivers.length, selectedDriverIndex]);
|
|
139
|
+
|
|
140
|
+
useEffect(() => {
|
|
141
|
+
if (activeDriverFilter === '__TOGGLE_SELECTED__') {
|
|
142
|
+
const driver = drivers[selectedDriverIndex];
|
|
143
|
+
setActiveDriverFilter(driver?.id ?? null);
|
|
144
|
+
}
|
|
145
|
+
}, [activeDriverFilter, drivers, selectedDriverIndex]);
|
|
146
|
+
|
|
147
|
+
const processedSessions = useMemo(() => {
|
|
148
|
+
let result = [...baseFilteredSessions];
|
|
149
|
+
|
|
150
|
+
if (activeDriverFilter && activeDriverFilter !== '__TOGGLE_SELECTED__') {
|
|
151
|
+
result = result.filter(s => {
|
|
152
|
+
switch (driverDimension) {
|
|
153
|
+
case 'model':
|
|
154
|
+
return s.streams.some(st => st.modelId === activeDriverFilter);
|
|
155
|
+
case 'project':
|
|
156
|
+
return (s.projectPath?.split('/').pop() ?? 'unknown') === activeDriverFilter;
|
|
157
|
+
case 'agent':
|
|
158
|
+
return s.agentName === activeDriverFilter;
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
result.sort((a, b) => {
|
|
164
|
+
const aActive = a.status === 'active' ? 1 : 0;
|
|
165
|
+
const bActive = b.status === 'active' ? 1 : 0;
|
|
166
|
+
if (bActive !== aActive) return bActive - aActive;
|
|
167
|
+
|
|
168
|
+
if (sortField === 'cost') return (b.totalCostUsd ?? 0) - (a.totalCostUsd ?? 0);
|
|
169
|
+
if (sortField === 'tokens') return (b.totals.input + b.totals.output) - (a.totals.input + a.totals.output);
|
|
170
|
+
return b.lastActivityAt - a.lastActivityAt;
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
return result;
|
|
174
|
+
}, [baseFilteredSessions, activeDriverFilter, driverDimension, sortField]);
|
|
175
|
+
|
|
176
|
+
useEffect(() => {
|
|
177
|
+
if (processedSessions.length === 0) {
|
|
178
|
+
if (selectedRow !== 0) setSelectedRow(0);
|
|
179
|
+
if (scrollOffset !== 0) setScrollOffset(0);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const maxRow = processedSessions.length - 1;
|
|
184
|
+
if (selectedRow > maxRow) {
|
|
185
|
+
setSelectedRow(maxRow);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
}, [processedSessions.length, selectedRow, scrollOffset]);
|
|
189
|
+
|
|
190
|
+
useEffect(() => {
|
|
191
|
+
if (!sessionsScrollboxRef.current || processedSessions.length === 0) return;
|
|
192
|
+
|
|
193
|
+
let newOffset = scrollOffset;
|
|
194
|
+
|
|
195
|
+
if (selectedRow < scrollOffset) {
|
|
196
|
+
newOffset = selectedRow;
|
|
197
|
+
} else if (selectedRow >= scrollOffset + visibleRows) {
|
|
198
|
+
newOffset = selectedRow - visibleRows + 1;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (newOffset !== scrollOffset) {
|
|
202
|
+
setScrollOffset(newOffset);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
sessionsScrollboxRef.current.scrollTo(newOffset);
|
|
206
|
+
}, [selectedRow, processedSessions.length, scrollOffset, visibleRows]);
|
|
207
|
+
|
|
208
|
+
const openSessionDrawer = useCallback(() => {
|
|
209
|
+
const session = processedSessions[selectedRow];
|
|
210
|
+
if (session) {
|
|
211
|
+
showDrawer(session);
|
|
212
|
+
}
|
|
213
|
+
}, [processedSessions, selectedRow, showDrawer]);
|
|
214
|
+
|
|
215
|
+
const { hideDrawer } = useDrawer();
|
|
216
|
+
|
|
217
|
+
useDashboardKeyboard({
|
|
218
|
+
state: {
|
|
219
|
+
showHelp,
|
|
220
|
+
showSessionDrawer,
|
|
221
|
+
selectedRow,
|
|
222
|
+
focusedPanel,
|
|
223
|
+
sidebarCollapsed,
|
|
224
|
+
filterQuery,
|
|
225
|
+
isFiltering,
|
|
226
|
+
sortField,
|
|
227
|
+
pendingG,
|
|
228
|
+
scrollOffset,
|
|
229
|
+
limitSelectedIndex,
|
|
230
|
+
providerCount: configuredProviders.length,
|
|
231
|
+
driverDimension,
|
|
232
|
+
selectedDriverIndex,
|
|
233
|
+
activeDriverFilter,
|
|
234
|
+
sidebarMode,
|
|
235
|
+
},
|
|
236
|
+
actions: {
|
|
237
|
+
setShowHelp,
|
|
238
|
+
openSessionDrawer,
|
|
239
|
+
closeSessionDrawer: hideDrawer,
|
|
240
|
+
setSelectedRow,
|
|
241
|
+
setFocusedPanel,
|
|
242
|
+
setSidebarCollapsed,
|
|
243
|
+
setFilterQuery,
|
|
244
|
+
setIsFiltering,
|
|
245
|
+
setSortField,
|
|
246
|
+
setPendingG,
|
|
247
|
+
setScrollOffset,
|
|
248
|
+
setLimitSelectedIndex,
|
|
249
|
+
setDriverDimension,
|
|
250
|
+
setSelectedDriverIndex,
|
|
251
|
+
setActiveDriverFilter,
|
|
252
|
+
},
|
|
253
|
+
processedSessions,
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
const windowedKpis = useMemo(() => ({
|
|
257
|
+
cost: processedSessions.reduce((acc, s) => acc + (s.totalCostUsd ?? 0), 0),
|
|
258
|
+
tokens: processedSessions.reduce((acc, s) => acc + s.totals.input + s.totals.output, 0),
|
|
259
|
+
requests: processedSessions.reduce((acc, s) => acc + s.requestCount, 0),
|
|
260
|
+
}), [processedSessions]);
|
|
261
|
+
|
|
262
|
+
const budgetPeriodCost = useMemo(() => {
|
|
263
|
+
|
|
264
|
+
let dailyCost = 0;
|
|
265
|
+
let weeklyCost = 0;
|
|
266
|
+
let monthlyCost = 0;
|
|
267
|
+
|
|
268
|
+
for (const session of agentSessions) {
|
|
269
|
+
dailyCost += session.costInDay;
|
|
270
|
+
weeklyCost += session.costInWeek;
|
|
271
|
+
monthlyCost += session.costInMonth;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return { daily: dailyCost, weekly: weeklyCost, monthly: monthlyCost };
|
|
275
|
+
}, [agentSessions]);
|
|
276
|
+
|
|
277
|
+
useEffect(() => {
|
|
278
|
+
const checks: Array<{ cost: number; limit: number | null; type: 'daily' | 'weekly' | 'monthly' }> = [
|
|
279
|
+
{ cost: budgetPeriodCost.daily, limit: config.budgets.daily, type: 'daily' },
|
|
280
|
+
{ cost: budgetPeriodCost.weekly, limit: config.budgets.weekly, type: 'weekly' },
|
|
281
|
+
{ cost: budgetPeriodCost.monthly, limit: config.budgets.monthly, type: 'monthly' },
|
|
282
|
+
];
|
|
283
|
+
for (const { cost, limit, type } of checks) {
|
|
284
|
+
if (limit !== null && limit > 0) {
|
|
285
|
+
notificationBus.checkBudget(cost, limit, type, config);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}, [budgetPeriodCost, config]);
|
|
289
|
+
|
|
290
|
+
const getBudgetCost = () => {
|
|
291
|
+
switch (budgetType) {
|
|
292
|
+
case 'daily': return budgetPeriodCost.daily;
|
|
293
|
+
case 'weekly': return budgetPeriodCost.weekly;
|
|
294
|
+
case 'monthly': return budgetPeriodCost.monthly;
|
|
295
|
+
default: return windowedKpis.cost;
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
const activeCount = agentSessions.filter(s => s.status === 'active').length;
|
|
300
|
+
|
|
301
|
+
return (
|
|
302
|
+
<box flexDirection="column" flexGrow={1} padding={1} gap={1} overflow="hidden">
|
|
303
|
+
{showHelp && <HelpOverlay />}
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
<KpiStrip
|
|
307
|
+
totalCost={windowedKpis.cost}
|
|
308
|
+
totalTokens={windowedKpis.tokens}
|
|
309
|
+
totalRequests={windowedKpis.requests}
|
|
310
|
+
activeCount={activeCount}
|
|
311
|
+
deltaCost={deltas.cost}
|
|
312
|
+
deltaTokens={deltas.tokens}
|
|
313
|
+
windowSec={deltas.windowSec}
|
|
314
|
+
activity={activity}
|
|
315
|
+
sparkData={sparkData}
|
|
316
|
+
budget={{
|
|
317
|
+
limit: budgetType === 'daily' ? config.budgets.daily
|
|
318
|
+
: budgetType === 'weekly' ? config.budgets.weekly
|
|
319
|
+
: budgetType === 'monthly' ? config.budgets.monthly
|
|
320
|
+
: null,
|
|
321
|
+
budgetCost: getBudgetCost(),
|
|
322
|
+
budgetType,
|
|
323
|
+
budgetTypeLabel,
|
|
324
|
+
warningPercent: config.alerts.warningPercent,
|
|
325
|
+
criticalPercent: config.alerts.criticalPercent,
|
|
326
|
+
}}
|
|
327
|
+
/>
|
|
328
|
+
|
|
329
|
+
<ProviderLimitsPanel
|
|
330
|
+
providers={configuredProviders.map(p => ({
|
|
331
|
+
id: p.plugin.id,
|
|
332
|
+
name: p.plugin.name,
|
|
333
|
+
usedPercent: getMaxUsedPercent(p),
|
|
334
|
+
color: providerColorOf(p.plugin.id),
|
|
335
|
+
...(p.usage?.error ? { error: p.usage.error } : {}),
|
|
336
|
+
}))}
|
|
337
|
+
focused={focusedPanel === 'limits'}
|
|
338
|
+
selectedIndex={limitSelectedIndex}
|
|
339
|
+
showBudgetBar={showBudgetInLimits}
|
|
340
|
+
budget={{
|
|
341
|
+
totalCost: windowedKpis.cost,
|
|
342
|
+
budgetCost: getBudgetCost(),
|
|
343
|
+
limit: budgetType === 'daily' ? config.budgets.daily
|
|
344
|
+
: budgetType === 'weekly' ? config.budgets.weekly
|
|
345
|
+
: budgetType === 'monthly' ? config.budgets.monthly
|
|
346
|
+
: null,
|
|
347
|
+
budgetType,
|
|
348
|
+
budgetTypeLabel,
|
|
349
|
+
warningPercent: config.alerts.warningPercent,
|
|
350
|
+
criticalPercent: config.alerts.criticalPercent,
|
|
351
|
+
}}
|
|
352
|
+
/>
|
|
353
|
+
|
|
354
|
+
<box flexDirection="row" gap={1} flexGrow={1} minHeight={1}>
|
|
355
|
+
<SessionsTable
|
|
356
|
+
ref={sessionsScrollboxRef}
|
|
357
|
+
sessions={processedSessions}
|
|
358
|
+
selectedRow={selectedRow}
|
|
359
|
+
isLoading={isLoading}
|
|
360
|
+
isFiltering={isFiltering}
|
|
361
|
+
filterQuery={filterQuery}
|
|
362
|
+
focusedPanel={focusedPanel}
|
|
363
|
+
windowLabel={windowLabel}
|
|
364
|
+
getProviderColor={providerColorOf}
|
|
365
|
+
/>
|
|
366
|
+
|
|
367
|
+
{!effectiveSidebarCollapsed && (
|
|
368
|
+
<SmartSidebar
|
|
369
|
+
sessions={processedSessions}
|
|
370
|
+
budgetCost={getBudgetCost()}
|
|
371
|
+
focusedPanel={focusedPanel}
|
|
372
|
+
dimension={driverDimension}
|
|
373
|
+
selectedDriverIndex={selectedDriverIndex}
|
|
374
|
+
activeDriverFilter={activeDriverFilter}
|
|
375
|
+
getProviderColor={providerColorOf}
|
|
376
|
+
/>
|
|
377
|
+
)}
|
|
378
|
+
</box>
|
|
379
|
+
|
|
380
|
+
<box flexDirection="row" paddingLeft={1} height={1} flexShrink={0}>
|
|
381
|
+
<text fg={colors.textSubtle} height={1} flexGrow={1}>
|
|
382
|
+
{isFiltering ? 'Type to filter Esc cancel Enter apply' :
|
|
383
|
+
filterQuery ? `Esc clear / edit filter ↑↓ navigate s sort` :
|
|
384
|
+
activeDriverFilter ? `Filter: ${activeDriverFilter} Esc clear` :
|
|
385
|
+
focusedPanel === 'sessions' ? '/ filter ↑↓ navigate Enter details s sort l limits Tab next ⇧Tab prev' :
|
|
386
|
+
focusedPanel === 'limits' ? '←→ select provider Tab next ⇧Tab prev Esc back' :
|
|
387
|
+
focusedPanel === 'sidebar' ? '↑↓ select Enter filter m/p/a dimension b budget Tab next ⇧Tab prev' :
|
|
388
|
+
'/ filter i sidebar Tab/⇧Tab switch ? help'}
|
|
389
|
+
</text>
|
|
390
|
+
</box>
|
|
391
|
+
</box>
|
|
392
|
+
);
|
|
393
|
+
}
|