@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,80 @@
|
|
|
1
|
+
import type { ModelPricing, CostBreakdown } from '@/plugins/types/provider.ts';
|
|
2
|
+
|
|
3
|
+
export interface TokenUsage {
|
|
4
|
+
input: number;
|
|
5
|
+
output: number;
|
|
6
|
+
cacheRead?: number;
|
|
7
|
+
cacheWrite?: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function estimateCost(
|
|
11
|
+
usage: TokenUsage,
|
|
12
|
+
pricing: ModelPricing
|
|
13
|
+
): CostBreakdown {
|
|
14
|
+
const inputCost = (usage.input / 1_000_000) * pricing.input;
|
|
15
|
+
const outputCost = (usage.output / 1_000_000) * pricing.output;
|
|
16
|
+
|
|
17
|
+
let cacheReadCost = 0;
|
|
18
|
+
if (usage.cacheRead && pricing.cacheRead) {
|
|
19
|
+
cacheReadCost = (usage.cacheRead / 1_000_000) * pricing.cacheRead;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let cacheWriteCost = 0;
|
|
23
|
+
if (usage.cacheWrite && pricing.cacheWrite) {
|
|
24
|
+
cacheWriteCost = (usage.cacheWrite / 1_000_000) * pricing.cacheWrite;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const total = inputCost + outputCost + cacheReadCost + cacheWriteCost;
|
|
28
|
+
|
|
29
|
+
const breakdown: CostBreakdown = {
|
|
30
|
+
total: roundCost(total),
|
|
31
|
+
input: roundCost(inputCost),
|
|
32
|
+
output: roundCost(outputCost),
|
|
33
|
+
currency: 'USD',
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
if (cacheReadCost > 0) breakdown.cacheRead = roundCost(cacheReadCost);
|
|
37
|
+
if (cacheWriteCost > 0) breakdown.cacheWrite = roundCost(cacheWriteCost);
|
|
38
|
+
|
|
39
|
+
return breakdown;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function estimateSessionCost(
|
|
43
|
+
sessions: TokenUsage[],
|
|
44
|
+
pricing: ModelPricing
|
|
45
|
+
): CostBreakdown {
|
|
46
|
+
const totals = sessions.reduce<TokenUsage>(
|
|
47
|
+
(acc, session) => ({
|
|
48
|
+
input: acc.input + session.input,
|
|
49
|
+
output: acc.output + session.output,
|
|
50
|
+
cacheRead: (acc.cacheRead ?? 0) + (session.cacheRead ?? 0),
|
|
51
|
+
cacheWrite: (acc.cacheWrite ?? 0) + (session.cacheWrite ?? 0),
|
|
52
|
+
}),
|
|
53
|
+
{ input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
return estimateCost(totals, pricing);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function formatCost(cost: number, currency = 'USD'): string {
|
|
60
|
+
if (currency === 'USD') {
|
|
61
|
+
if (cost < 0.01) {
|
|
62
|
+
return `$${cost.toFixed(4)}`;
|
|
63
|
+
}
|
|
64
|
+
if (cost < 1) {
|
|
65
|
+
return `$${cost.toFixed(3)}`;
|
|
66
|
+
}
|
|
67
|
+
return `$${cost.toFixed(2)}`;
|
|
68
|
+
}
|
|
69
|
+
return `${cost.toFixed(4)} ${currency}`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function formatTokenCount(tokens: number): string {
|
|
73
|
+
if (tokens < 1000) return tokens.toString();
|
|
74
|
+
if (tokens < 1_000_000) return `${(tokens / 1000).toFixed(1)}K`;
|
|
75
|
+
return `${(tokens / 1_000_000).toFixed(2)}M`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function roundCost(value: number): number {
|
|
79
|
+
return Math.round(value * 1_000_000) / 1_000_000;
|
|
80
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import {
|
|
3
|
+
FALLBACK_PRICING,
|
|
4
|
+
getFallbackPricing,
|
|
5
|
+
getFallbackProviderPricing,
|
|
6
|
+
} from './fallback.ts';
|
|
7
|
+
|
|
8
|
+
describe('getFallbackPricing()', () => {
|
|
9
|
+
test('returns pricing for an exact model match', () => {
|
|
10
|
+
expect(getFallbackPricing('openai', 'gpt-4.1')).toEqual({
|
|
11
|
+
input: 2,
|
|
12
|
+
output: 8,
|
|
13
|
+
cacheRead: 0.5,
|
|
14
|
+
source: 'fallback',
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('returns pricing when model id includes a known key', () => {
|
|
19
|
+
expect(getFallbackPricing('anthropic', 'claude-3-7-sonnet-20250219-thinking')).toEqual({
|
|
20
|
+
input: 3,
|
|
21
|
+
output: 15,
|
|
22
|
+
cacheRead: 0.3,
|
|
23
|
+
cacheWrite: 3.75,
|
|
24
|
+
source: 'fallback',
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('returns pricing when a known key includes the model id', () => {
|
|
29
|
+
expect(getFallbackPricing('google', 'gemini-2.5')).toEqual({
|
|
30
|
+
input: 1.25,
|
|
31
|
+
output: 10,
|
|
32
|
+
source: 'fallback',
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('returns null for unknown provider', () => {
|
|
37
|
+
expect(getFallbackPricing('unknown-provider', 'gpt-4.1')).toBeNull();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('returns null for unknown model in a known provider', () => {
|
|
41
|
+
expect(getFallbackPricing('openai', 'totally-unknown-model')).toBeNull();
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('getFallbackProviderPricing()', () => {
|
|
46
|
+
test('returns the full provider pricing map', () => {
|
|
47
|
+
const anthropicPricing = FALLBACK_PRICING['anthropic'];
|
|
48
|
+
if (!anthropicPricing) {
|
|
49
|
+
throw new Error('Expected anthropic fallback pricing to exist');
|
|
50
|
+
}
|
|
51
|
+
expect(getFallbackProviderPricing('anthropic')).toEqual(anthropicPricing);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('returns null for unknown provider', () => {
|
|
55
|
+
expect(getFallbackProviderPricing('does-not-exist')).toBeNull();
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe('FALLBACK_PRICING', () => {
|
|
60
|
+
test('includes anthropic provider pricing data', () => {
|
|
61
|
+
expect(FALLBACK_PRICING['anthropic']).toBeDefined();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('includes openai provider pricing data', () => {
|
|
65
|
+
expect(FALLBACK_PRICING['openai']).toBeDefined();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('includes google provider pricing data', () => {
|
|
69
|
+
expect(FALLBACK_PRICING['google']).toBeDefined();
|
|
70
|
+
});
|
|
71
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { ModelPricing } from '@/plugins/types/provider.ts';
|
|
2
|
+
|
|
3
|
+
//TODO: get more up to date fallback pricing
|
|
4
|
+
export const FALLBACK_PRICING: Record<string, Record<string, ModelPricing>> = {
|
|
5
|
+
anthropic: {
|
|
6
|
+
'claude-sonnet-4-20250514': { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75, source: 'fallback' },
|
|
7
|
+
'claude-3-7-sonnet-20250219': { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75, source: 'fallback' },
|
|
8
|
+
'claude-3-5-sonnet-20241022': { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75, source: 'fallback' },
|
|
9
|
+
'claude-3-5-haiku-20241022': { input: 0.8, output: 4, cacheRead: 0.08, cacheWrite: 1, source: 'fallback' },
|
|
10
|
+
'claude-3-opus-20240229': { input: 15, output: 75, cacheRead: 1.5, cacheWrite: 18.75, source: 'fallback' },
|
|
11
|
+
},
|
|
12
|
+
|
|
13
|
+
openai: {
|
|
14
|
+
'gpt-4.1': { input: 2, output: 8, cacheRead: 0.5, source: 'fallback' },
|
|
15
|
+
'gpt-4.1-mini': { input: 0.4, output: 1.6, cacheRead: 0.1, source: 'fallback' },
|
|
16
|
+
'gpt-4o': { input: 2.5, output: 10, cacheRead: 1.25, source: 'fallback' },
|
|
17
|
+
'gpt-4o-mini': { input: 0.15, output: 0.6, cacheRead: 0.075, source: 'fallback' },
|
|
18
|
+
'o1': { input: 15, output: 60, source: 'fallback' },
|
|
19
|
+
'o1-mini': { input: 1.1, output: 4.4, source: 'fallback' },
|
|
20
|
+
'o3': { input: 10, output: 40, source: 'fallback' },
|
|
21
|
+
'o3-mini': { input: 1.1, output: 4.4, source: 'fallback' },
|
|
22
|
+
'o4-mini': { input: 1.1, output: 4.4, source: 'fallback' },
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
google: {
|
|
26
|
+
'gemini-2.0-flash': { input: 0.1, output: 0.4, source: 'fallback' },
|
|
27
|
+
'gemini-2.0-flash-lite': { input: 0.075, output: 0.3, source: 'fallback' },
|
|
28
|
+
'gemini-2.5-pro': { input: 1.25, output: 10, source: 'fallback' },
|
|
29
|
+
'gemini-2.5-flash': { input: 0.15, output: 0.6, source: 'fallback' },
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
openrouter: {
|
|
33
|
+
'anthropic/claude-sonnet-4': { input: 3, output: 15, source: 'fallback' },
|
|
34
|
+
'openai/gpt-4.1': { input: 2, output: 8, source: 'fallback' },
|
|
35
|
+
'google/gemini-2.5-pro': { input: 1.25, output: 10, source: 'fallback' },
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export function getFallbackPricing(
|
|
40
|
+
providerId: string,
|
|
41
|
+
modelId: string
|
|
42
|
+
): ModelPricing | null {
|
|
43
|
+
const providerPricing = FALLBACK_PRICING[providerId];
|
|
44
|
+
if (!providerPricing) return null;
|
|
45
|
+
|
|
46
|
+
const exactMatch = providerPricing[modelId];
|
|
47
|
+
if (exactMatch) return exactMatch;
|
|
48
|
+
|
|
49
|
+
for (const [key, pricing] of Object.entries(providerPricing)) {
|
|
50
|
+
if (modelId.includes(key) || key.includes(modelId)) {
|
|
51
|
+
return pricing;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function getFallbackProviderPricing(
|
|
59
|
+
providerId: string
|
|
60
|
+
): Record<string, ModelPricing> | null {
|
|
61
|
+
return FALLBACK_PRICING[providerId] ?? null;
|
|
62
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { ModelPricing, ProviderPlugin, ProviderPricing } from '@/plugins/types/provider.ts';
|
|
2
|
+
import { getModelPricing, getProviderModels, normalizeProviderName, setProviderAliases, clearCache as clearModelsDevCache } from './models-dev.ts';
|
|
3
|
+
import { getFallbackPricing, getFallbackProviderPricing } from './fallback.ts';
|
|
4
|
+
|
|
5
|
+
export function initPricingFromPlugins(
|
|
6
|
+
plugins: readonly ProviderPlugin[],
|
|
7
|
+
additionalAliases?: Record<string, string>,
|
|
8
|
+
): void {
|
|
9
|
+
const aliases = new Map<string, string>();
|
|
10
|
+
|
|
11
|
+
for (const plugin of plugins) {
|
|
12
|
+
const pricing = plugin.pricing;
|
|
13
|
+
if (
|
|
14
|
+
pricing &&
|
|
15
|
+
'modelsDevProviderId' in pricing &&
|
|
16
|
+
typeof (pricing as ProviderPricing).modelsDevProviderId === 'string'
|
|
17
|
+
) {
|
|
18
|
+
aliases.set(plugin.id, (pricing as ProviderPricing).modelsDevProviderId!);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (additionalAliases) {
|
|
23
|
+
for (const [key, value] of Object.entries(additionalAliases)) {
|
|
24
|
+
aliases.set(key, value);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
setProviderAliases(aliases);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function getPricing(
|
|
32
|
+
providerId: string,
|
|
33
|
+
modelId: string
|
|
34
|
+
): Promise<ModelPricing | null> {
|
|
35
|
+
const normalizedProvider = normalizeProviderName(providerId);
|
|
36
|
+
|
|
37
|
+
const modelsDevPricing = await getModelPricing(normalizedProvider, modelId);
|
|
38
|
+
if (modelsDevPricing) return modelsDevPricing;
|
|
39
|
+
|
|
40
|
+
const fallbackPricing = getFallbackPricing(normalizedProvider, modelId);
|
|
41
|
+
if (fallbackPricing) return fallbackPricing;
|
|
42
|
+
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function getProviderPricing(
|
|
47
|
+
providerId: string
|
|
48
|
+
): Promise<Record<string, ModelPricing>> {
|
|
49
|
+
const normalizedProvider = normalizeProviderName(providerId);
|
|
50
|
+
|
|
51
|
+
const modelsDevPricing = await getProviderModels(normalizedProvider);
|
|
52
|
+
if (modelsDevPricing && Object.keys(modelsDevPricing).length > 0) {
|
|
53
|
+
return modelsDevPricing;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const fallbackPricing = getFallbackProviderPricing(normalizedProvider);
|
|
57
|
+
return fallbackPricing ?? {};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function clearPricingCache(): void {
|
|
61
|
+
clearModelsDevCache();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { estimateCost, estimateSessionCost, formatCost, formatTokenCount } from './estimator.ts';
|
|
65
|
+
export type { TokenUsage } from './estimator.ts';
|
|
66
|
+
export { FALLBACK_PRICING } from './fallback.ts';
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import type { ModelPricing } from '@/plugins/types/provider.ts';
|
|
2
|
+
|
|
3
|
+
const MODELS_DEV_API = 'https://models.dev/api.json';
|
|
4
|
+
const CACHE_TTL_MS = 3600000;
|
|
5
|
+
|
|
6
|
+
interface ModelsDevCost {
|
|
7
|
+
input: number;
|
|
8
|
+
output: number;
|
|
9
|
+
cache_read?: number;
|
|
10
|
+
cache_write?: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface ModelsDevModel {
|
|
14
|
+
id: string;
|
|
15
|
+
name: string;
|
|
16
|
+
family: string;
|
|
17
|
+
cost: ModelsDevCost;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface ModelsDevProvider {
|
|
21
|
+
id: string;
|
|
22
|
+
name: string;
|
|
23
|
+
models: Record<string, ModelsDevModel>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type ModelsDevResponse = Record<string, ModelsDevProvider>;
|
|
27
|
+
|
|
28
|
+
interface CachedData {
|
|
29
|
+
data: ModelsDevResponse;
|
|
30
|
+
fetchedAt: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let cache: CachedData | null = null;
|
|
34
|
+
|
|
35
|
+
export async function fetchModelsDevData(): Promise<ModelsDevResponse | null> {
|
|
36
|
+
if (cache && Date.now() - cache.fetchedAt < CACHE_TTL_MS) {
|
|
37
|
+
return cache.data;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
const response = await fetch(MODELS_DEV_API);
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
return cache?.data ?? null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const data = (await response.json()) as ModelsDevResponse;
|
|
47
|
+
cache = { data, fetchedAt: Date.now() };
|
|
48
|
+
return data;
|
|
49
|
+
} catch {
|
|
50
|
+
return cache?.data ?? null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let providerAliases: ReadonlyMap<string, string> = new Map();
|
|
55
|
+
|
|
56
|
+
export function setProviderAliases(aliases: ReadonlyMap<string, string>): void {
|
|
57
|
+
providerAliases = aliases;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function normalizeProviderName(providerId: string): string {
|
|
61
|
+
return providerAliases.get(providerId) ?? providerId;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function getModelPricing(
|
|
65
|
+
providerId: string,
|
|
66
|
+
modelId: string
|
|
67
|
+
): Promise<ModelPricing | null> {
|
|
68
|
+
const data = await fetchModelsDevData();
|
|
69
|
+
if (!data) return null;
|
|
70
|
+
|
|
71
|
+
const normalizedProvider = normalizeProviderName(providerId);
|
|
72
|
+
const provider = data[normalizedProvider];
|
|
73
|
+
if (!provider?.models) return null;
|
|
74
|
+
|
|
75
|
+
const model = provider.models[modelId];
|
|
76
|
+
if (!model?.cost) return null;
|
|
77
|
+
|
|
78
|
+
const pricing: ModelPricing = {
|
|
79
|
+
input: model.cost.input,
|
|
80
|
+
output: model.cost.output,
|
|
81
|
+
source: 'models.dev',
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
if (model.cost.cache_read !== undefined) {
|
|
85
|
+
pricing.cacheRead = model.cost.cache_read;
|
|
86
|
+
}
|
|
87
|
+
if (model.cost.cache_write !== undefined) {
|
|
88
|
+
pricing.cacheWrite = model.cost.cache_write;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return pricing;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export async function getProviderModels(
|
|
95
|
+
providerId: string
|
|
96
|
+
): Promise<Record<string, ModelPricing> | null> {
|
|
97
|
+
const data = await fetchModelsDevData();
|
|
98
|
+
if (!data) return null;
|
|
99
|
+
|
|
100
|
+
const normalizedProvider = normalizeProviderName(providerId);
|
|
101
|
+
const provider = data[normalizedProvider];
|
|
102
|
+
if (!provider?.models) return null;
|
|
103
|
+
|
|
104
|
+
const result: Record<string, ModelPricing> = {};
|
|
105
|
+
|
|
106
|
+
for (const [modelId, model] of Object.entries(provider.models)) {
|
|
107
|
+
if (!model.cost) continue;
|
|
108
|
+
|
|
109
|
+
const pricing: ModelPricing = {
|
|
110
|
+
input: model.cost.input,
|
|
111
|
+
output: model.cost.output,
|
|
112
|
+
source: 'models.dev',
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
if (model.cost.cache_read !== undefined) {
|
|
116
|
+
pricing.cacheRead = model.cost.cache_read;
|
|
117
|
+
}
|
|
118
|
+
if (model.cost.cache_write !== undefined) {
|
|
119
|
+
pricing.cacheWrite = model.cost.cache_write;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
result[modelId] = pricing;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function clearCache(): void {
|
|
129
|
+
cache = null;
|
|
130
|
+
}
|