From 39509478b66842a6853319b670ea1c278ca49517 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 7 Oct 2025 23:45:16 -0500 Subject: [PATCH] Add model pricing to model selector --- src/components/ModelPricing.tsx | 50 +++++++++++++++++++++++++++++ src/components/ModelSelector.tsx | 48 ++++++++++++++++----------- src/components/OnboardingDialog.tsx | 44 ++----------------------- 3 files changed, 81 insertions(+), 61 deletions(-) create mode 100644 src/components/ModelPricing.tsx diff --git a/src/components/ModelPricing.tsx b/src/components/ModelPricing.tsx new file mode 100644 index 00000000..9507bf08 --- /dev/null +++ b/src/components/ModelPricing.tsx @@ -0,0 +1,50 @@ +import { Decimal } from 'decimal.js'; +import { cn } from '@/lib/utils'; + +interface ModelPricingProps { + pricing: { prompt: Decimal; completion: Decimal }; + className?: string; +} + +/** + * Get a single normalized "price per token" for a model, + * combining input and output pricing with agentic-coding-aware weights. + * + * Default behavior: + * - Uses inputWeight = 0.20 (based on typical 3–5x output premium seen in practice). + * + * Optional behavior: + * - If you pass observed token usage, the function will compute the weight + * as the observed share of spend from inputs over total spend. + */ +function getNormalizedModelPrice( + pricing: { prompt: Decimal; completion: Decimal }, + inputWeight = 0.2, +): Decimal { + if (!(inputWeight >= 0 && inputWeight <= 1)) { + throw new Error("Computed inputWeight is out of range [0,1]. Check inputs."); + } + if (pricing.prompt.equals(0) && pricing.completion.equals(0)) { + return new Decimal(0); + } + const outputWeight = 1 - inputWeight; + return pricing.prompt.times(inputWeight).plus(pricing.completion.times(outputWeight)); +} + +export function ModelPricing({ pricing, className }: ModelPricingProps) { + const normalizedPrice = getNormalizedModelPrice(pricing).times(1_000_000).toNumber(); + + if (normalizedPrice === 0) { + return Free; + } else if (normalizedPrice < 10) { + return $; + } else if (normalizedPrice >= 10 && normalizedPrice < 15) { + return $$; + } else if (normalizedPrice >= 15 && normalizedPrice < 50) { + return $$$; + } else if (normalizedPrice >= 50) { + return $$$$; + } + + return null; +} diff --git a/src/components/ModelSelector.tsx b/src/components/ModelSelector.tsx index 3e992d01..efacd615 100644 --- a/src/components/ModelSelector.tsx +++ b/src/components/ModelSelector.tsx @@ -11,6 +11,7 @@ import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/comp import { useAISettings } from '@/hooks/useAISettings'; import { useProviderModels } from '@/hooks/useProviderModels'; import { useIsMobile } from '@/hooks/useIsMobile'; +import { ModelPricing } from '@/components/ModelPricing'; import { cn } from '@/lib/utils'; interface ModelSelectorProps { @@ -277,7 +278,7 @@ export function ModelSelector({ role="combobox" aria-expanded={open} className={cn( - "h-8 p-0 gap-0.5 text-xs border-0 bg-transparent hover:bg-transparent hover:text-foreground focus-visible:ring-0 focus-visible:ring-offset-0 text-muted-foreground text-right min-w-0" + "h-8 p-0 gap-0.5 text-xs border-0 bg-transparent hover:bg-transparent hover:text-foreground focus-visible:ring-0 focus-visible:ring-offset-0 text-muted-foreground text-right min-w-0 max-w-[280px]" )} disabled={disabled} > @@ -287,12 +288,12 @@ export function ModelSelector({ - + {(isConfigured && models.length >= 5) && ( )} - +

{t('noModelsFound')}

@@ -302,22 +303,28 @@ export function ModelSelector({ {shouldShowRecentlyUsed && ( - {recentlyUsedModels.map((model) => ( - handleSelect(model)} - className="cursor-pointer" - > - { + const modelData = models.find(m => m.fullId === modelId); + return ( + handleSelect(modelId)} + className="cursor-pointer" + > + + {modelId} + {modelData?.pricing && ( + )} - /> - {model} - - ))} + + ); + })} )} @@ -377,7 +384,10 @@ export function ModelSelector({ value === model.fullId ? "opacity-100" : "opacity-0" )} /> - {model.fullId} + {model.fullId} + {model.pricing && ( + + )} ))} diff --git a/src/components/OnboardingDialog.tsx b/src/components/OnboardingDialog.tsx index bd576a85..4788a413 100644 --- a/src/components/OnboardingDialog.tsx +++ b/src/components/OnboardingDialog.tsx @@ -1,4 +1,3 @@ -import { Decimal } from 'decimal.js'; import { useState, useEffect, useRef } from 'react'; import { generateSecretKey } from 'nostr-tools'; import { nip19 } from 'nostr-tools'; @@ -16,6 +15,7 @@ import { useProviderModels } from '@/hooks/useProviderModels'; import { CreditsDialog } from '@/components/CreditsDialog'; import { OnboardingCreditsBadge } from '@/components/OnboardingCreditsBadge'; import { ShakespeareLogo } from '@/components/ShakespeareLogo'; +import { ModelPricing } from '@/components/ModelPricing'; import { AI_PROVIDER_PRESETS, type PresetProvider } from '@/lib/aiProviderPresets'; import { cn } from '@/lib/utils'; @@ -138,21 +138,6 @@ export function OnboardingDialog({ open, onOpenChange }: OnboardingDialogProps) onOpenChange(false); }; - const renderPrice = (pricing: { prompt: Decimal; completion: Decimal }) => { - const normalizedPrice = getNormalizedModelPrice(pricing).times(1_000_000).toNumber(); - if (normalizedPrice === 0) { - return Free; - } else if (normalizedPrice < 10) { - return $; - } else if (normalizedPrice >= 10 && normalizedPrice < 15) { - return $$; - } else if (normalizedPrice >= 15 && normalizedPrice < 50) { - return $$$; - } else if (normalizedPrice >= 50) { - return $$$$; - } - } - // Reset state when dialog opens/closes useEffect(() => { if (open) { @@ -533,7 +518,7 @@ export function OnboardingDialog({ open, onOpenChange }: OnboardingDialogProps)
{modelName} - {model.pricing && renderPrice(model.pricing)} + {model.pricing && } {model.description && (

@@ -608,29 +593,4 @@ export function OnboardingDialog({ open, onOpenChange }: OnboardingDialogProps) )} ); -} - -/** - * Get a single normalized "price per token" for a model, - * combining input and output pricing with agentic-coding-aware weights. - * - * Default behavior: - * - Uses inputWeight = 0.20 (based on typical 3–5x output premium seen in practice). - * - * Optional behavior: - * - If you pass observed token usage, the function will compute the weight - * as the observed share of spend from inputs over total spend. - */ -function getNormalizedModelPrice( - pricing: { prompt: Decimal; completion: Decimal }, - inputWeight = 0.2, -): Decimal { - if (!(inputWeight >= 0 && inputWeight <= 1)) { - throw new Error("Computed inputWeight is out of range [0,1]. Check inputs."); - } - if (pricing.prompt.equals(0) && pricing.completion.equals(0)) { - return new Decimal(0); - } - const outputWeight = 1 - inputWeight; - return pricing.prompt.times(inputWeight).plus(pricing.completion.times(outputWeight)); } \ No newline at end of file