askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
site icons (favicons) in tabs, address bar suggestions and the bookmarks panel
7249579
15 hours, 50 minutes ago
History
chat-model-choice.ts · 3.8 KB · 85 lines
| 1 | /** |
| 2 | * Which model the assistant (the chat) uses: picked in the panel, remembered across restarts. Electron-free, so it is |
| 3 | * unit-tested. |
| 4 | * |
| 5 | * The chat and the AI Reader are different jobs. A TL;DR is one quick call, where the smallest model does fine; the chat |
| 6 | * reads pages, picks tools and acts over many steps, where a small model asks needless questions and misses the obvious. |
| 7 | * So the chat has its own choice and a stronger default, and the reader keeps the env file's model. |
| 8 | * |
| 9 | * Only models of a provider that is set up (a key, or a local endpoint) are offered. A saved choice for a provider that is |
| 10 | * no longer set up is ignored, never half-used. |
| 11 | */ |
| 12 | |
| 13 | import { PRICES } from "./ai.js"; |
| 14 | import type { AiConfig, Provider } from "./ai-config.js"; |
| 15 | |
| 16 | export interface ModelChoice { |
| 17 | /** "provider/model": what is saved and sent back from the panel. */ |
| 18 | id: string; |
| 19 | provider: Provider; |
| 20 | model: string; |
| 21 | label: string; |
| 22 | } |
| 23 | |
| 24 | /** Cheapest first. Labels are short: the panel's picker is narrow. */ |
| 25 | const ANTHROPIC: Array<{ model: string; name: string }> = [ |
| 26 | { model: "claude-haiku-5-5", name: "Haiku 5.5" }, |
| 27 | { model: "claude-sonnet-5-5", name: "Sonnet 5.5" }, |
| 28 | { model: "claude-opus-5-5", name: "Opus 5.5" }, |
| 29 | { model: "claude-fable-5-2", name: "Fable 5.2" }, |
| 30 | ]; |
| 31 | /** The chat's default when Anthropic is set up and nothing else is chosen. */ |
| 32 | export const CHAT_DEFAULT_ANTHROPIC = "claude-sonnet-5-5"; |
| 33 | |
| 34 | /** "· 20×": the cost per message compared with Haiku, from the prices we know (never a guess). */ |
| 35 | function costNote(model: string): string { |
| 36 | const base = PRICES["claude-haiku-5-5"]; |
| 37 | const p = PRICES[model]; |
| 38 | if (!base || !p) return ""; |
| 39 | return model === "claude-haiku-5-5" ? " · cheapest" : ` · ${Math.round(p.in / base.in)}× cost`; |
| 40 | } |
| 41 | |
| 42 | /** The models the panel offers, for the providers that are set up. */ |
| 43 | export function chatModelChoices(cfg: AiConfig | null): ModelChoice[] { |
| 44 | if (!cfg) return []; |
| 45 | const out: ModelChoice[] = []; |
| 46 | const add = (provider: Provider, model: string, label: string) => { |
| 47 | const id = `${provider}/${model}`; |
| 48 | if (!out.some((c) => c.id === id)) out.push({ id, provider, model, label }); |
| 49 | }; |
| 50 | for (const provider of cfg.available) { |
| 51 | if (provider === "anthropic") { |
| 52 | for (const m of ANTHROPIC) add("anthropic", m.model, `${m.name}${costNote(m.model)}`); |
| 53 | // a model named in the env file that is not in the list above still shows, under its own name |
| 54 | if (cfg.provider === "anthropic" && !ANTHROPIC.some((m) => m.model === cfg.model)) add("anthropic", cfg.model, cfg.model); |
| 55 | } else { |
| 56 | // OpenAI, or a model on this machine or network (DBB_AI_BASE_URL): the one the env file names |
| 57 | const model = cfg.provider === "openai" ? cfg.model : "gpt-5.4-mini"; |
| 58 | add("openai", model, cfg.baseUrl ? `${model} (${new URL(cfg.baseUrl).host})` : model); |
| 59 | } |
| 60 | } |
| 61 | return out; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * The config the chat runs with: the saved choice if it is still on offer; otherwise a model the env file names |
| 66 | * explicitly for the chat's provider; otherwise Sonnet when Anthropic is the provider; otherwise the env's model. |
| 67 | */ |
| 68 | export function chatAi(cfg: AiConfig | null, saved: string | null): AiConfig | null { |
| 69 | if (!cfg) return null; |
| 70 | const choices = chatModelChoices(cfg); |
| 71 | const pick = saved ? choices.find((c) => c.id === saved) : undefined; |
| 72 | if (pick) return { ...cfg, provider: pick.provider, model: pick.model }; |
| 73 | if (cfg.provider === "anthropic" && !cfg.modelExplicit) return { ...cfg, model: CHAT_DEFAULT_ANTHROPIC }; |
| 74 | return cfg; |
| 75 | } |
| 76 | |
| 77 | /** A saved file's choice: "provider/model" with sane characters, or null. */ |
| 78 | export function parseSavedChoice(text: string): string | null { |
| 79 | try { |
| 80 | const v = (JSON.parse(text) as { model?: unknown }).model; |
| 81 | return typeof v === "string" && /^(anthropic|openai)\/[A-Za-z0-9._:-]{1,80}$/.test(v) ? v : null; |
| 82 | } catch { |
| 83 | return null; |
| 84 | } |
| 85 | } |