@tokscale/cli 1.0.5 → 1.0.7

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 (50) hide show
  1. package/dist/cli.js +14 -3
  2. package/dist/cli.js.map +1 -1
  3. package/dist/native.d.ts.map +1 -1
  4. package/dist/native.js +3 -2
  5. package/dist/native.js.map +1 -1
  6. package/package.json +6 -4
  7. package/src/auth.ts +211 -0
  8. package/src/cli.ts +1040 -0
  9. package/src/credentials.ts +123 -0
  10. package/src/cursor.ts +558 -0
  11. package/src/graph-types.ts +188 -0
  12. package/src/graph.ts +485 -0
  13. package/src/native-runner.ts +105 -0
  14. package/src/native.ts +938 -0
  15. package/src/pricing.ts +309 -0
  16. package/src/sessions/claudecode.ts +119 -0
  17. package/src/sessions/codex.ts +227 -0
  18. package/src/sessions/gemini.ts +108 -0
  19. package/src/sessions/index.ts +126 -0
  20. package/src/sessions/opencode.ts +94 -0
  21. package/src/sessions/reports.ts +475 -0
  22. package/src/sessions/types.ts +59 -0
  23. package/src/spinner.ts +283 -0
  24. package/src/submit.ts +175 -0
  25. package/src/table.ts +233 -0
  26. package/src/tui/App.tsx +339 -0
  27. package/src/tui/components/BarChart.tsx +198 -0
  28. package/src/tui/components/DailyView.tsx +113 -0
  29. package/src/tui/components/DateBreakdownPanel.tsx +79 -0
  30. package/src/tui/components/Footer.tsx +225 -0
  31. package/src/tui/components/Header.tsx +68 -0
  32. package/src/tui/components/Legend.tsx +39 -0
  33. package/src/tui/components/LoadingSpinner.tsx +82 -0
  34. package/src/tui/components/ModelRow.tsx +47 -0
  35. package/src/tui/components/ModelView.tsx +145 -0
  36. package/src/tui/components/OverviewView.tsx +108 -0
  37. package/src/tui/components/StatsView.tsx +225 -0
  38. package/src/tui/components/TokenBreakdown.tsx +46 -0
  39. package/src/tui/components/index.ts +15 -0
  40. package/src/tui/config/settings.ts +130 -0
  41. package/src/tui/config/themes.ts +115 -0
  42. package/src/tui/hooks/useData.ts +518 -0
  43. package/src/tui/index.tsx +44 -0
  44. package/src/tui/opentui.d.ts +137 -0
  45. package/src/tui/types/index.ts +165 -0
  46. package/src/tui/utils/cleanup.ts +65 -0
  47. package/src/tui/utils/colors.ts +65 -0
  48. package/src/tui/utils/format.ts +36 -0
  49. package/src/tui/utils/responsive.ts +8 -0
  50. package/src/types.d.ts +28 -0
@@ -0,0 +1,165 @@
1
+ import type { ColorPaletteName } from "../config/themes.js";
2
+
3
+ export type TabType = "overview" | "model" | "daily" | "stats";
4
+ export type SortType = "cost" | "tokens";
5
+ export type SourceType = "opencode" | "claude" | "codex" | "cursor" | "gemini";
6
+
7
+ export type { ColorPaletteName };
8
+
9
+ export interface ModelEntry {
10
+ source: string;
11
+ model: string;
12
+ input: number;
13
+ output: number;
14
+ cacheWrite: number;
15
+ cacheRead: number;
16
+ reasoning: number;
17
+ total: number;
18
+ cost: number;
19
+ }
20
+
21
+ export interface DailyEntry {
22
+ date: string;
23
+ input: number;
24
+ output: number;
25
+ cache: number;
26
+ total: number;
27
+ cost: number;
28
+ }
29
+
30
+ export interface ContributionDay {
31
+ date: string;
32
+ cost: number;
33
+ level: number;
34
+ }
35
+
36
+ export interface GridCell {
37
+ date: string | null;
38
+ level: number;
39
+ }
40
+
41
+ export interface TotalBreakdown {
42
+ input: number;
43
+ output: number;
44
+ cacheWrite: number;
45
+ cacheRead: number;
46
+ reasoning: number;
47
+ total: number;
48
+ cost: number;
49
+ }
50
+
51
+ export interface Stats {
52
+ favoriteModel: string;
53
+ totalTokens: number;
54
+ sessions: number;
55
+ longestSession: string;
56
+ currentStreak: number;
57
+ longestStreak: number;
58
+ activeDays: number;
59
+ totalDays: number;
60
+ peakHour: string;
61
+ }
62
+
63
+ export interface ModelWithPercentage {
64
+ modelId: string;
65
+ percentage: number;
66
+ inputTokens: number;
67
+ outputTokens: number;
68
+ cacheReadTokens: number;
69
+ cacheWriteTokens: number;
70
+ totalTokens: number;
71
+ cost: number;
72
+ }
73
+
74
+ export interface ChartModelData {
75
+ modelId: string;
76
+ tokens: number;
77
+ color: string;
78
+ }
79
+
80
+ export interface ChartDataPoint {
81
+ date: string;
82
+ models: ChartModelData[];
83
+ total: number;
84
+ }
85
+
86
+ export interface TUIData {
87
+ modelEntries: ModelEntry[];
88
+ dailyEntries: DailyEntry[];
89
+ contributions: ContributionDay[];
90
+ contributionGrid: GridCell[][];
91
+ stats: Stats;
92
+ totalCost: number;
93
+ totals: TotalBreakdown;
94
+ modelCount: number;
95
+ chartData: ChartDataPoint[];
96
+ topModels: ModelWithPercentage[];
97
+ dailyBreakdowns: Map<string, DailyModelBreakdown>;
98
+ }
99
+
100
+ export interface TUISettings {
101
+ colorPalette: string;
102
+ }
103
+
104
+ export type LoadingPhase =
105
+ | "idle"
106
+ | "loading-pricing"
107
+ | "syncing-cursor"
108
+ | "parsing-sources"
109
+ | "finalizing-report"
110
+ | "complete";
111
+
112
+ export interface DailyModelBreakdown {
113
+ date: string;
114
+ cost: number;
115
+ totalTokens: number;
116
+ models: Array<{
117
+ modelId: string;
118
+ source: string;
119
+ tokens: {
120
+ input: number;
121
+ output: number;
122
+ cacheRead: number;
123
+ cacheWrite: number;
124
+ reasoning: number;
125
+ };
126
+ cost: number;
127
+ messages: number;
128
+ }>;
129
+ }
130
+
131
+ export interface TUIOptions {
132
+ initialTab?: TabType;
133
+ enabledSources?: SourceType[];
134
+ sortBy?: SortType;
135
+ sortDesc?: boolean;
136
+ since?: string;
137
+ until?: string;
138
+ year?: string;
139
+ colorPalette?: ColorPaletteName;
140
+ }
141
+
142
+
143
+
144
+ export const LAYOUT = {
145
+ HEADER_HEIGHT: 2,
146
+ FOOTER_HEIGHT: 4,
147
+ MIN_CONTENT_HEIGHT: 12,
148
+ CHART_HEIGHT_RATIO: 0.35,
149
+ MIN_CHART_HEIGHT: 5,
150
+ MIN_LIST_HEIGHT: 4,
151
+ CHART_AXIS_WIDTH: 8,
152
+ MIN_CHART_WIDTH: 20,
153
+ MAX_VISIBLE_BARS: 52,
154
+ } as const;
155
+
156
+ export const SOURCE_LABELS: Record<SourceType, string> = {
157
+ opencode: "OC",
158
+ claude: "CC",
159
+ codex: "CX",
160
+ cursor: "CR",
161
+ gemini: "GM",
162
+ } as const;
163
+
164
+ export const TABS: readonly TabType[] = ["overview", "model", "daily", "stats"] as const;
165
+ export const ALL_SOURCES: readonly SourceType[] = ["opencode", "claude", "codex", "cursor", "gemini"] as const;
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Terminal cleanup utility for restoring terminal state when TUI exits.
3
+ * Provides fallback cleanup for crash scenarios where OpenTUI's destroy() may not run.
4
+ */
5
+
6
+ /**
7
+ * Complete terminal state restoration sequences.
8
+ * Based on research from xterm, kitty keyboard protocol, and popular TUI libraries.
9
+ */
10
+ export const TERMINAL_CLEANUP_SEQUENCES = [
11
+ // Disable all mouse tracking modes
12
+ '\x1b[?1016l', // SGR Pixel Mode
13
+ '\x1b[?1015l', // URXVT Mouse
14
+ '\x1b[?1006l', // SGR Mouse (produces "51;77;17M" sequences)
15
+ '\x1b[?1005l', // UTF-8 Mouse
16
+ '\x1b[?1004l', // Focus Events
17
+ '\x1b[?1003l', // Any Event Mouse (motion tracking)
18
+ '\x1b[?1002l', // Button Event Mouse (drag tracking)
19
+ '\x1b[?1001l', // Highlight Mouse
20
+ '\x1b[?1000l', // VT200 Mouse
21
+ '\x1b[?9l', // X10 Mouse
22
+
23
+ // Disable kitty keyboard protocol (produces "9;5u" sequences)
24
+ '\x1b[<u', // Disable kitty keyboard progressive enhancement
25
+ '\x1b[>4;0m', // Disable modifyOtherKeys (xterm)
26
+
27
+ // Disable synchronized updates
28
+ '\x1b[?2026l',
29
+
30
+ // Restore cursor and attributes
31
+ '\x1b[?25h', // Show cursor (DECTCEM)
32
+ '\x1b[0m', // Reset all text attributes (SGR 0)
33
+
34
+ // Exit alternate screen buffer
35
+ '\x1b[?1049l', // Exit alt screen + restore cursor
36
+ '\x1b[?47l', // Legacy: exit alt screen
37
+ '\x1b[?1047l', // Legacy: another alt screen mode
38
+ ].join('');
39
+
40
+ let hasCleanedUp = false;
41
+
42
+ /**
43
+ * Write terminal restoration sequences to stdout.
44
+ * Idempotent - safe to call multiple times.
45
+ *
46
+ * This is a FALLBACK for crash scenarios. Normal exit should use
47
+ * renderer.destroy() which handles cleanup properly.
48
+ */
49
+ export function restoreTerminalState(): void {
50
+ if (hasCleanedUp) return;
51
+ hasCleanedUp = true;
52
+
53
+ try {
54
+ process.stdout.write(TERMINAL_CLEANUP_SEQUENCES);
55
+ } catch {
56
+ // Ignore write errors (stdout may be closed)
57
+ }
58
+ }
59
+
60
+ /**
61
+ * Reset cleanup state (for testing purposes).
62
+ */
63
+ export function resetCleanupState(): void {
64
+ hasCleanedUp = false;
65
+ }
@@ -0,0 +1,65 @@
1
+ import type { SourceType } from "../types/index.js";
2
+
3
+ export const PROVIDER_COLORS = {
4
+ anthropic: "#FF6B35",
5
+ openai: "#10B981",
6
+ google: "#3B82F6",
7
+ cursor: "#8B5CF6",
8
+ opencode: "#6B7280",
9
+ deepseek: "#06B6D4",
10
+ xai: "#EAB308",
11
+ meta: "#6366F1",
12
+ unknown: "#FFFFFF",
13
+ } as const;
14
+
15
+ export type ProviderType = keyof typeof PROVIDER_COLORS;
16
+
17
+ const PROVIDER_PATTERNS: readonly [RegExp, ProviderType][] = [
18
+ [/claude|sonnet|opus|haiku/i, "anthropic"],
19
+ [/gpt|^o1|^o3|codex|text-embedding|dall-e|whisper|tts/i, "openai"],
20
+ [/gemini/i, "google"],
21
+ [/deepseek/i, "deepseek"],
22
+ [/grok/i, "xai"],
23
+ [/llama|mixtral/i, "meta"],
24
+ [/^auto$|cursor/i, "cursor"],
25
+ ] as const;
26
+
27
+ const providerCache = new Map<string, ProviderType>();
28
+ const colorCache = new Map<string, string>();
29
+
30
+ export function getProviderFromModel(modelId: string): ProviderType {
31
+ const cached = providerCache.get(modelId);
32
+ if (cached) return cached;
33
+
34
+ let provider: ProviderType = "unknown";
35
+ for (const [pattern, type] of PROVIDER_PATTERNS) {
36
+ if (pattern.test(modelId)) {
37
+ provider = type;
38
+ break;
39
+ }
40
+ }
41
+
42
+ providerCache.set(modelId, provider);
43
+ return provider;
44
+ }
45
+
46
+ export function getModelColor(modelId: string): string {
47
+ const cached = colorCache.get(modelId);
48
+ if (cached) return cached;
49
+
50
+ const color = PROVIDER_COLORS[getProviderFromModel(modelId)];
51
+ colorCache.set(modelId, color);
52
+ return color;
53
+ }
54
+
55
+ export const SOURCE_COLORS: Record<SourceType, string> = {
56
+ opencode: "#22c55e",
57
+ claude: "#f97316",
58
+ codex: "#3b82f6",
59
+ cursor: "#a855f7",
60
+ gemini: "#06b6d4",
61
+ };
62
+
63
+ export function getSourceColor(source: SourceType | string): string {
64
+ return SOURCE_COLORS[source as SourceType] || "#888888";
65
+ }
@@ -0,0 +1,36 @@
1
+ export function formatTokens(n: number): string {
2
+ if (!Number.isFinite(n) || n < 0) return "0";
3
+ return n.toLocaleString();
4
+ }
5
+
6
+ export function formatTokensShort(n: number): string {
7
+ if (!Number.isFinite(n) || n < 0) return "0";
8
+ if (n >= 1_000_000_000) return `${(n / 1_000_000_000).toFixed(1)}B`;
9
+ if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`;
10
+ if (n >= 1_000) return `${(n / 1_000).toFixed(1)}K`;
11
+ return n.toLocaleString();
12
+ }
13
+
14
+ export function formatTokensCompact(n: number): string {
15
+ if (!Number.isFinite(n) || n < 0) return "0";
16
+ if (n >= 1_000_000_000) return `${(n / 1_000_000_000).toFixed(1)}B`;
17
+ if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`;
18
+ if (n >= 1_000) return `${(n / 1_000).toFixed(0)}K`;
19
+ return n.toLocaleString();
20
+ }
21
+
22
+ export function formatCost(cost: number): string {
23
+ if (!Number.isFinite(cost) || cost < 0) return "$0.00";
24
+ if (cost >= 1000) return `$${(cost / 1000).toFixed(1)}K`;
25
+ return `$${cost.toFixed(2)}`;
26
+ }
27
+
28
+ export function formatCostFull(cost: number): string {
29
+ if (!Number.isFinite(cost) || cost < 0) return "$0.00";
30
+ return `$${cost.toFixed(2)}`;
31
+ }
32
+
33
+ export function formatPercentage(value: number): string {
34
+ if (!Number.isFinite(value)) return "0.0%";
35
+ return `${value.toFixed(1)}%`;
36
+ }
@@ -0,0 +1,8 @@
1
+ export const NARROW_TERMINAL_WIDTH = 80;
2
+ export const VERY_NARROW_TERMINAL_WIDTH = 60;
3
+
4
+ export const isNarrow = (width: number | undefined): boolean =>
5
+ (width ?? 100) < NARROW_TERMINAL_WIDTH;
6
+
7
+ export const isVeryNarrow = (width: number | undefined): boolean =>
8
+ (width ?? 100) < VERY_NARROW_TERMINAL_WIDTH;
package/src/types.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ declare module "string-width" {
2
+ export default function stringWidth(str: string): number;
3
+ }
4
+
5
+ declare module "./tui-bundle.js" {
6
+ import type { TUIOptions } from "./tui/types/index.js";
7
+ export function launchTUI(options?: TUIOptions): Promise<void>;
8
+ export type { TUIOptions };
9
+ }
10
+
11
+ declare module "bun" {
12
+ export interface BunSubprocess {
13
+ stdin: any;
14
+ stdout: any;
15
+ stderr: any;
16
+ exited: Promise<number>;
17
+ kill(signal?: string): void;
18
+ }
19
+
20
+ export function spawn(options: {
21
+ cmd: string[];
22
+ stdin?: "pipe";
23
+ stdout?: "pipe";
24
+ stderr?: "pipe";
25
+ timeout?: number;
26
+ killSignal?: string;
27
+ }): BunSubprocess;
28
+ }