@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.
Files changed (159) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +273 -0
  3. package/bin/ttop.js +49 -0
  4. package/package.json +68 -0
  5. package/src/agents/aggregator.ts +176 -0
  6. package/src/agents/costing.ts +74 -0
  7. package/src/agents/index.ts +12 -0
  8. package/src/agents/types.ts +71 -0
  9. package/src/cli.ts +167 -0
  10. package/src/config/schema.test.ts +166 -0
  11. package/src/config/schema.ts +165 -0
  12. package/src/demo/simulator.ts +745 -0
  13. package/src/plugins/agents/index.ts +2 -0
  14. package/src/plugins/auth-sources.ts +186 -0
  15. package/src/plugins/lifecycle.ts +177 -0
  16. package/src/plugins/loader.test.ts +195 -0
  17. package/src/plugins/loader.ts +282 -0
  18. package/src/plugins/notification-bus.ts +153 -0
  19. package/src/plugins/notifications/index.ts +2 -0
  20. package/src/plugins/notifications/terminal-bell.ts +66 -0
  21. package/src/plugins/notifications/visual-flash.ts +57 -0
  22. package/src/plugins/npm-installer.ts +164 -0
  23. package/src/plugins/plugin-context-factory.ts +67 -0
  24. package/src/plugins/plugin-host.ts +205 -0
  25. package/src/plugins/providers/anthropic.ts +325 -0
  26. package/src/plugins/providers/antigravity.ts +309 -0
  27. package/src/plugins/providers/codex.ts +250 -0
  28. package/src/plugins/providers/gemini.ts +391 -0
  29. package/src/plugins/providers/github-copilot.ts +194 -0
  30. package/src/plugins/providers/index.ts +12 -0
  31. package/src/plugins/providers/minimax.ts +223 -0
  32. package/src/plugins/providers/openai-api.ts +250 -0
  33. package/src/plugins/providers/opencode-zen.ts +125 -0
  34. package/src/plugins/providers/perplexity.ts +177 -0
  35. package/src/plugins/providers/zai.ts +225 -0
  36. package/src/plugins/registry.ts +251 -0
  37. package/src/plugins/sandbox-guard.test.ts +191 -0
  38. package/src/plugins/sandbox-guard.ts +155 -0
  39. package/src/plugins/sandbox.ts +138 -0
  40. package/src/plugins/themes/catppuccin-latte.ts +45 -0
  41. package/src/plugins/themes/catppuccin-mocha.ts +45 -0
  42. package/src/plugins/themes/claude-code.ts +45 -0
  43. package/src/plugins/themes/dracula.ts +45 -0
  44. package/src/plugins/themes/github-light.ts +45 -0
  45. package/src/plugins/themes/gruvbox-dark.ts +45 -0
  46. package/src/plugins/themes/gruvbox-light.ts +45 -0
  47. package/src/plugins/themes/index.ts +15 -0
  48. package/src/plugins/themes/kanagawa.ts +45 -0
  49. package/src/plugins/themes/nord.ts +45 -0
  50. package/src/plugins/themes/one-dark.ts +45 -0
  51. package/src/plugins/themes/opencode.ts +47 -0
  52. package/src/plugins/themes/rose-pine-dawn.ts +45 -0
  53. package/src/plugins/themes/rose-pine.ts +45 -0
  54. package/src/plugins/themes/solarized-light.ts +45 -0
  55. package/src/plugins/themes/tokyo-night.ts +49 -0
  56. package/src/plugins/types/agent.ts +108 -0
  57. package/src/plugins/types/base.ts +267 -0
  58. package/src/plugins/types/index.ts +19 -0
  59. package/src/plugins/types/notification.ts +51 -0
  60. package/src/plugins/types/provider.ts +233 -0
  61. package/src/plugins/types/theme.ts +70 -0
  62. package/src/plugins/update-checker.ts +177 -0
  63. package/src/pricing/estimator.test.ts +181 -0
  64. package/src/pricing/estimator.ts +80 -0
  65. package/src/pricing/fallback.test.ts +71 -0
  66. package/src/pricing/fallback.ts +62 -0
  67. package/src/pricing/index.ts +66 -0
  68. package/src/pricing/models-dev.ts +130 -0
  69. package/src/storage/database.ts +613 -0
  70. package/src/storage/db.ts +64 -0
  71. package/src/storage/index.ts +66 -0
  72. package/src/storage/migrations/index.ts +189 -0
  73. package/src/storage/paths.ts +24 -0
  74. package/src/storage/repos/agentSessions.ts +210 -0
  75. package/src/storage/repos/providerSnapshots.ts +116 -0
  76. package/src/storage/repos/usageEvents.ts +334 -0
  77. package/src/storage/types.ts +288 -0
  78. package/src/tui/App.tsx +381 -0
  79. package/src/tui/components/CommandPalette.tsx +166 -0
  80. package/src/tui/components/DebugConsole.tsx +161 -0
  81. package/src/tui/components/DebugPanel.tsx +660 -0
  82. package/src/tui/components/FooterHints.tsx +22 -0
  83. package/src/tui/components/GhostProviderCard.tsx +45 -0
  84. package/src/tui/components/Header.tsx +125 -0
  85. package/src/tui/components/HelpOverlay.tsx +45 -0
  86. package/src/tui/components/InlineGauge.tsx +34 -0
  87. package/src/tui/components/InlineSparkline.tsx +78 -0
  88. package/src/tui/components/KpiStrip.tsx +246 -0
  89. package/src/tui/components/LimitGauge.tsx +186 -0
  90. package/src/tui/components/ModalBackdrop.tsx +42 -0
  91. package/src/tui/components/ProviderAggregateStrip.tsx +200 -0
  92. package/src/tui/components/ProviderCard.tsx +309 -0
  93. package/src/tui/components/ProviderDetailPanel.tsx +237 -0
  94. package/src/tui/components/ProviderLimitsPanel.tsx +308 -0
  95. package/src/tui/components/ProvidersList.tsx +380 -0
  96. package/src/tui/components/SessionDetailsDrawer.tsx +317 -0
  97. package/src/tui/components/SessionsTable.tsx +401 -0
  98. package/src/tui/components/SettingsModal.tsx +587 -0
  99. package/src/tui/components/SidebarBreakdown.tsx +87 -0
  100. package/src/tui/components/Skeleton.tsx +102 -0
  101. package/src/tui/components/SmartSidebar.tsx +644 -0
  102. package/src/tui/components/Sparkline.tsx +180 -0
  103. package/src/tui/components/Spinner.tsx +36 -0
  104. package/src/tui/components/StatusBar.tsx +96 -0
  105. package/src/tui/components/ThemePicker.tsx +234 -0
  106. package/src/tui/components/Toast.tsx +61 -0
  107. package/src/tui/components/UsageGauge.tsx +80 -0
  108. package/src/tui/components/index.ts +5 -0
  109. package/src/tui/contexts/AgentSessionContext.tsx +307 -0
  110. package/src/tui/contexts/ConfigContext.tsx +141 -0
  111. package/src/tui/contexts/DashboardRuntimeContext.tsx +102 -0
  112. package/src/tui/contexts/DemoModeContext.tsx +60 -0
  113. package/src/tui/contexts/DrawerContext.tsx +42 -0
  114. package/src/tui/contexts/InputContext.tsx +30 -0
  115. package/src/tui/contexts/LogContext.tsx +150 -0
  116. package/src/tui/contexts/PluginContext.tsx +424 -0
  117. package/src/tui/contexts/RealTimeActivityContext.tsx +119 -0
  118. package/src/tui/contexts/StorageContext.tsx +233 -0
  119. package/src/tui/contexts/ThemeContext.tsx +95 -0
  120. package/src/tui/contexts/TimeWindowContext.tsx +158 -0
  121. package/src/tui/contexts/ToastContext.tsx +40 -0
  122. package/src/tui/contexts/index.ts +5 -0
  123. package/src/tui/createApp.tsx +47 -0
  124. package/src/tui/debug/captureFrame.ts +262 -0
  125. package/src/tui/debug/snapshot.tsx +644 -0
  126. package/src/tui/driver/assertions.ts +279 -0
  127. package/src/tui/driver/cli.ts +717 -0
  128. package/src/tui/driver/coverage.ts +204 -0
  129. package/src/tui/driver/demo-snapshot.ts +66 -0
  130. package/src/tui/driver/diff.ts +166 -0
  131. package/src/tui/driver/driver.ts +270 -0
  132. package/src/tui/driver/index.ts +53 -0
  133. package/src/tui/driver/recorder.ts +303 -0
  134. package/src/tui/driver/test-workflow.ts +115 -0
  135. package/src/tui/driver/test.ts +47 -0
  136. package/src/tui/hooks/index.ts +20 -0
  137. package/src/tui/hooks/useAnimatedValue.ts +113 -0
  138. package/src/tui/hooks/useDashboardKeyboard.ts +341 -0
  139. package/src/tui/hooks/useDashboardState.ts +116 -0
  140. package/src/tui/hooks/useEmaActivity.ts +223 -0
  141. package/src/tui/hooks/useEntranceAnimation.ts +62 -0
  142. package/src/tui/hooks/useExitAnimation.ts +137 -0
  143. package/src/tui/hooks/usePulse.ts +138 -0
  144. package/src/tui/hooks/useSafeRenderer.ts +24 -0
  145. package/src/tui/hooks/useValueFlash.ts +111 -0
  146. package/src/tui/index.tsx +50 -0
  147. package/src/tui/utils/providerColor.ts +32 -0
  148. package/src/tui/views/Dashboard.tsx +340 -0
  149. package/src/tui/views/HistoricalTrendsView.tsx +1431 -0
  150. package/src/tui/views/ProjectsView.tsx +1277 -0
  151. package/src/tui/views/RealTimeDashboard.tsx +393 -0
  152. package/src/tui/views/SettingsView.tsx +465 -0
  153. package/src/tui/views/index.ts +2 -0
  154. package/src/types/global.d.ts +5 -0
  155. package/src/types/opentui-test-utils.d.ts +18 -0
  156. package/src/utils/clipboard.ts +118 -0
  157. package/src/utils/currency.test.ts +106 -0
  158. package/src/utils/currency.ts +79 -0
  159. package/src/version.ts +1 -0
@@ -0,0 +1,340 @@
1
+ import { useState, useEffect, useCallback, useRef, useMemo } from 'react';
2
+ import { useKeyboard, useTerminalDimensions } from '@opentui/react';
3
+ import type { ScrollBoxRenderable, BoxRenderable, InputRenderable } from '@opentui/core';
4
+ import { useColors } from '../contexts/ThemeContext.tsx';
5
+ import { usePlugins, type ProviderState } from '../contexts/PluginContext.tsx';
6
+ import { useInputFocus } from '../contexts/InputContext.tsx';
7
+ import { ProviderCard } from '../components/ProviderCard.tsx';
8
+ import { GhostProviderCard } from '../components/GhostProviderCard.tsx';
9
+ import { ProvidersList } from '../components/ProvidersList.tsx';
10
+ import { ProviderAggregateStrip } from '../components/ProviderAggregateStrip.tsx';
11
+ type SortMode = 'name' | 'usage' | 'status';
12
+ type ViewMode = 'cards' | 'list';
13
+
14
+ export function Dashboard() {
15
+ const colors = useColors();
16
+ const { width: termWidth } = useTerminalDimensions();
17
+ const { providers, isInitialized, refreshAllProviders, refreshProvider } = usePlugins();
18
+ const { setInputFocused } = useInputFocus();
19
+ const [focusedIndex, setFocusedIndex] = useState<number | null>(null);
20
+
21
+ const [filterQuery, setFilterQuery] = useState('');
22
+ const [isFiltering, setIsFiltering] = useState(false);
23
+ const [sortMode, setSortMode] = useState<SortMode>('status');
24
+ const [viewMode, setViewMode] = useState<ViewMode>('list');
25
+ const [showUnconfigured, setShowUnconfigured] = useState(true);
26
+ const [expandedIndex, setExpandedIndex] = useState<number | null>(null);
27
+ const [atRiskFocus, setAtRiskFocus] = useState(false);
28
+
29
+ const isFilteringRef = useRef(isFiltering);
30
+ const expandedIndexRef = useRef(expandedIndex);
31
+ const focusedIndexRef = useRef(focusedIndex);
32
+
33
+ useEffect(() => { isFilteringRef.current = isFiltering; }, [isFiltering]);
34
+ useEffect(() => { expandedIndexRef.current = expandedIndex; }, [expandedIndex]);
35
+ useEffect(() => { focusedIndexRef.current = focusedIndex; }, [focusedIndex]);
36
+
37
+ useEffect(() => {
38
+ setInputFocused(isFiltering);
39
+ return () => setInputFocused(false);
40
+ }, [isFiltering, setInputFocused]);
41
+
42
+ const scrollBoxRef = useRef<ScrollBoxRenderable>(null);
43
+ const containerRef = useRef<BoxRenderable>(null);
44
+ const cardRefs = useRef<(BoxRenderable | null)[]>([]);
45
+ const inputRef = useRef<InputRenderable>(null);
46
+
47
+ useEffect(() => {
48
+ if (isInitialized) {
49
+ refreshAllProviders();
50
+ }
51
+ }, [isInitialized]);
52
+
53
+ const getMaxUsage = useCallback((state: ProviderState) => {
54
+ if (!state.usage?.limits) return 0;
55
+
56
+ const items = state.usage.limits.items ?? [];
57
+ if (items.length > 0) {
58
+ return Math.max(...items.map((item) => item.usedPercent ?? 0));
59
+ }
60
+
61
+ const primary = state.usage.limits.primary?.usedPercent || 0;
62
+ const secondary = state.usage.limits.secondary?.usedPercent || 0;
63
+
64
+ return Math.max(primary, secondary);
65
+ }, []);
66
+
67
+ const providerList = Array.from(providers.values());
68
+
69
+ const filteredAndSortedProviders = useMemo(() => {
70
+ let result = providerList.filter((p) => p.configured);
71
+
72
+ if (atRiskFocus) {
73
+ result = result.filter(p => {
74
+ if (p.usage?.error) return true;
75
+ if (p.usage?.limitReached) return true;
76
+ const maxUsage = getMaxUsage(p);
77
+ return maxUsage >= 80;
78
+ });
79
+ }
80
+
81
+ if (filterQuery) {
82
+ const query = filterQuery.toLowerCase();
83
+ result = result.filter(p => p.plugin.name.toLowerCase().includes(query));
84
+ }
85
+
86
+ result.sort((a, b) => {
87
+ switch (sortMode) {
88
+ case 'name':
89
+ return a.plugin.name.localeCompare(b.plugin.name);
90
+ case 'usage':
91
+ return getMaxUsage(b) - getMaxUsage(a);
92
+ case 'status': {
93
+ if (a.usage?.limitReached !== b.usage?.limitReached) {
94
+ return a.usage?.limitReached ? -1 : 1;
95
+ }
96
+ if (!!a.usage?.error !== !!b.usage?.error) {
97
+ return a.usage?.error ? -1 : 1;
98
+ }
99
+ const usageDiff = getMaxUsage(b) - getMaxUsage(a);
100
+ if (Math.abs(usageDiff) > 0.01) return usageDiff;
101
+ return a.plugin.name.localeCompare(b.plugin.name);
102
+ }
103
+ default:
104
+ return 0;
105
+ }
106
+ });
107
+
108
+ return result;
109
+ }, [providerList, filterQuery, sortMode, getMaxUsage, atRiskFocus]);
110
+
111
+ const configured = filteredAndSortedProviders;
112
+ const unconfigured = useMemo(() =>
113
+ providerList.filter((p) => !p.configured),
114
+ [providerList]
115
+ );
116
+
117
+ const cycleFocus = useCallback((direction: 1 | -1) => {
118
+ const total = configured.length + (showUnconfigured ? unconfigured.length : 0);
119
+ if (total === 0) return;
120
+
121
+ setFocusedIndex((current) => {
122
+ if (current === null) {
123
+ return direction === 1 ? 0 : total - 1;
124
+ }
125
+ const next = current + direction;
126
+ if (next < 0) return null;
127
+ if (next >= total) return null;
128
+ return next;
129
+ });
130
+ }, [configured.length, unconfigured.length, showUnconfigured]);
131
+
132
+ const toggleExpanded = useCallback(() => {
133
+ const current = focusedIndexRef.current;
134
+ if (current === null) return;
135
+ setExpandedIndex(prev => prev === current ? null : current);
136
+ }, []);
137
+
138
+ const refreshSelected = useCallback(() => {
139
+ const idx = focusedIndexRef.current;
140
+ if (idx === null || idx >= configured.length) return;
141
+ const provider = configured[idx];
142
+ if (provider) {
143
+ refreshProvider(provider.plugin.id);
144
+ }
145
+ }, [configured, refreshProvider]);
146
+
147
+ useKeyboard((key) => {
148
+ if (isFilteringRef.current) {
149
+ if (key.name === 'escape') {
150
+ setIsFiltering(false);
151
+ setFilterQuery('');
152
+ return;
153
+ }
154
+ if (key.name === 'enter' || key.name === 'return') {
155
+ setIsFiltering(false);
156
+ return;
157
+ }
158
+ return;
159
+ }
160
+
161
+ if (key.name === 'down' || key.name === 'j') {
162
+ cycleFocus(1);
163
+ } else if (key.name === 'up' || key.name === 'k') {
164
+ cycleFocus(-1);
165
+ } else if (key.name === 'tab' && !key.shift) {
166
+ cycleFocus(1);
167
+ } else if (key.name === 'tab' && key.shift) {
168
+ cycleFocus(-1);
169
+ } else if (key.name === 'escape') {
170
+ if (expandedIndexRef.current !== null) {
171
+ setExpandedIndex(null);
172
+ } else if (focusedIndex !== null) {
173
+ setFocusedIndex(null);
174
+ } else if (filterQuery) {
175
+ setFilterQuery('');
176
+ }
177
+ } else if (key.name === 'enter' || key.name === 'return') {
178
+ toggleExpanded();
179
+ } else if (key.name === '/' || key.name === 'f') {
180
+ setIsFiltering(true);
181
+ } else if (key.name === 's') {
182
+ setSortMode(current => {
183
+ if (current === 'status') return 'usage';
184
+ if (current === 'usage') return 'name';
185
+ return 'status';
186
+ });
187
+ } else if (key.name === 'v') {
188
+ setViewMode(current => current === 'cards' ? 'list' : 'cards');
189
+ } else if (key.name === 'u') {
190
+ setShowUnconfigured(current => !current);
191
+ } else if (key.name === 'x') {
192
+ setAtRiskFocus(current => !current);
193
+ } else if (key.sequence === 'R') {
194
+ refreshAllProviders();
195
+ } else if (key.sequence === 'r' && focusedIndex !== null) {
196
+ refreshSelected();
197
+ }
198
+ });
199
+
200
+ if (!isInitialized) {
201
+ return (
202
+ <box flexGrow={1} justifyContent="center" alignItems="center">
203
+ <text fg={colors.textMuted}>Initializing plugins...</text>
204
+ </box>
205
+ );
206
+ }
207
+
208
+ const totalConfigured = configured.length;
209
+ const totalUnconfigured = unconfigured.length;
210
+
211
+ return (
212
+ <box flexDirection="column" flexGrow={1} padding={1} gap={0}>
213
+ <ProviderAggregateStrip providers={atRiskFocus ? configured : providerList.filter(p => p.configured)} />
214
+
215
+ <box flexDirection="row" gap={2} alignItems="center" height={1} justifyContent="space-between" paddingX={1}>
216
+ <box flexDirection="row" gap={2} alignItems="center">
217
+ {isFiltering ? (
218
+ <box flexDirection="row" gap={1} alignItems="center">
219
+ <text fg={colors.primary}>Filter:</text>
220
+ <input
221
+ ref={inputRef}
222
+ value={filterQuery}
223
+ onInput={(value: string) => setFilterQuery(value)}
224
+ focused={isFiltering}
225
+ width={20}
226
+ backgroundColor={colors.background}
227
+ textColor={colors.text}
228
+ cursorColor={colors.primary}
229
+ />
230
+ <text fg={colors.textSubtle}>(esc to clear)</text>
231
+ </box>
232
+ ) : (
233
+ <text fg={colors.textSubtle}>
234
+ {filterQuery ? `Filter: "${filterQuery}"` : '/ filter'}
235
+ </text>
236
+ )}
237
+
238
+ <text fg={colors.textSubtle}>|</text>
239
+
240
+ <text>
241
+ <span fg={colors.textSubtle}>Sort: </span>
242
+ <span fg={colors.primary}>{sortMode.toUpperCase()}</span>
243
+ </text>
244
+
245
+ <text fg={colors.textSubtle}>|</text>
246
+
247
+ <text>
248
+ <span fg={colors.textSubtle}>View: </span>
249
+ <span fg={colors.primary}>{viewMode === 'cards' ? 'Cards' : 'Console'}</span>
250
+ </text>
251
+
252
+ {atRiskFocus && (
253
+ <>
254
+ <text fg={colors.textSubtle}>|</text>
255
+ <text fg={colors.warning}>AT-RISK</text>
256
+ </>
257
+ )}
258
+ </box>
259
+
260
+ <text fg={colors.textMuted}>
261
+ {totalConfigured} configured{totalUnconfigured > 0 ? `, ${totalUnconfigured} unconfigured` : ''}
262
+ </text>
263
+ </box>
264
+
265
+ {viewMode === 'list' ? (
266
+ <box flexDirection="column" flexGrow={1}>
267
+ <ProvidersList
268
+ providers={configured}
269
+ selectedIndex={focusedIndex}
270
+ onSelect={setFocusedIndex}
271
+ expandedIndex={expandedIndex}
272
+ />
273
+ </box>
274
+ ) : (
275
+ <>
276
+ {configured.length > 0 ? (
277
+ <box flexDirection="column" gap={1} flexGrow={1}>
278
+ <scrollbox
279
+ ref={scrollBoxRef}
280
+ flexGrow={1}
281
+ focused={!isFiltering}
282
+ style={{
283
+ scrollbarOptions: {
284
+ visible: false,
285
+ },
286
+ }}
287
+ >
288
+ <box
289
+ ref={containerRef}
290
+ flexDirection="row"
291
+ flexWrap="wrap"
292
+ gap={1}
293
+ >
294
+ {configured.map((state, index) => (
295
+ <ProviderCard
296
+ ref={(el) => { cardRefs.current[index] = el; }}
297
+ key={state.plugin.id}
298
+ name={state.plugin.name}
299
+ configured={state.configured}
300
+ loading={state.loading}
301
+ usage={state.usage}
302
+ color={state.plugin.meta?.brandColor}
303
+ focused={focusedIndex === index && !isFiltering}
304
+ onFocus={() => setFocusedIndex(index)}
305
+ />
306
+ ))}
307
+
308
+ {showUnconfigured && unconfigured.map((state, index) => (
309
+ <GhostProviderCard
310
+ key={state.plugin.id}
311
+ name={state.plugin.name}
312
+ focused={focusedIndex === configured.length + index && !isFiltering}
313
+ onFocus={() => setFocusedIndex(configured.length + index)}
314
+ />
315
+ ))}
316
+ </box>
317
+ </scrollbox>
318
+ </box>
319
+ ) : (
320
+ <box flexGrow={1} justifyContent="center" alignItems="center">
321
+ <text fg={colors.textMuted}>
322
+ {filterQuery ? 'No matching providers found' : 'No configured providers'}
323
+ </text>
324
+ </box>
325
+ )}
326
+ </>
327
+ )}
328
+
329
+ <box flexDirection="row" paddingLeft={1} height={1}>
330
+ <text fg={colors.textSubtle} height={1}>
331
+ {isFiltering
332
+ ? 'Type to filter Esc cancel Enter apply'
333
+ : termWidth < 90
334
+ ? '↑↓ nav Enter detail / filter s sort v view u unconf x risk'
335
+ : '↑↓ navigate Enter detail / filter s sort v view u unconfigured x at-risk R refresh'}
336
+ </text>
337
+ </box>
338
+ </box>
339
+ );
340
+ }