From 564e3ea8b3cdeb7a2a1e87732c289646e892f0c4 Mon Sep 17 00:00:00 2001 From: Olivier Date: Mon, 12 Oct 2026 16:28:59 -0400 Subject: [PATCH] feat: model prices and latency in browser, Waypoint-style settings tabs - Model browser: per-million input/output price on every row; time to first token (p50) and tokens/s for the busiest OpenRouter provider, fetched lazily for visible rows and cached 10 minutes. - Fix: Free badge and free-first sorting (prices are strings), and the list was empty until typing. - Settings: text-only tabs styled like Waypoint's; drop the heading that repeated the tab name. --- README.md | 4 +- src/ModelBrowser.ts | 173 ++++++++++++++++++++++++++++++++----- src/NibbleAISettingsTab.ts | 33 +++---- styles.css | 60 +++++-------- 4 files changed, 187 insertions(+), 83 deletions(-) diff --git a/README.md b/README.md index a2ad740..cd01d70 100644 --- a/README.md +++ b/README.md @@ -133,10 +133,12 @@ This is useful for automation tools, Stream Deck, or scripting. ## Model Selection -Wherever you need to select an AI model (post-processing, proofreading, title generation), NibbleAI provides a **Browse models** button that opens a searchable dialog of available OpenRouter models: +Wherever you need to select an AI model (post-processing, proofreading, title generation, property filler), NibbleAI provides a **Browse** button that opens a searchable dialog of available OpenRouter models: - Models are fetched live from `openrouter.ai/api/v1/models` - Free models appear first (marked with a **Free** badge) +- Each row shows the price per million input/output tokens +- With an OpenRouter key configured, each visible row also shows time to first token (p50) and output speed (tokens/s) over the last 30 minutes, for the provider that served the most requests. Loaded once you stop typing; cached for 10 minutes - Type to filter by name, ID, or description - You can still type a model ID manually if you prefer diff --git a/src/ModelBrowser.ts b/src/ModelBrowser.ts index c619587..a8f9742 100644 --- a/src/ModelBrowser.ts +++ b/src/ModelBrowser.ts @@ -4,9 +4,69 @@ interface OpenRouterModel { id: string; name: string; description?: string; + /** USD per token, as decimal strings (e.g. "0.0000001"); "-1" means variable. */ pricing?: { - prompt: number; - completion: number; + prompt: string; + completion: string; + }; +} + +interface Percentiles { + p50: number; +} + +interface OpenRouterEndpoint { + provider_name: string; + latency_last_30m: Percentiles | null; + throughput_last_30m: Percentiles | null; + perf_last_30m_by_workload?: { + text_generation?: { request_count?: number }; + }; +} + +interface ModelPerf { + provider: string; + /** Time to first token, p50, in ms. */ + latencyMs: number; + /** Output tokens per second, p50. */ + tokensPerSecond: number | null; +} + +/** OpenRouter's stats cover the last 30 minutes; reuse them for a while across modal openings. */ +const PERF_TTL_MS = 10 * 60 * 1000; +const PERF_DEBOUNCE_MS = 300; +const PERF_CONCURRENCY = 6; +const perfCache = new Map(); + +/** "$0.10" for a per-token price string, scaled to per million tokens. */ +function formatPerMillion(perToken: string): string { + const perMillion = parseFloat(perToken) * 1_000_000; + if (perMillion >= 1) return `$${perMillion.toFixed(2)}`; + return `$${perMillion.toPrecision(2)}`; +} + +/** + * Picks the endpoint that served the most requests, the one OpenRouter + * most likely routes to, among those reporting latency. + */ +async function fetchPerf(modelId: string, apiKey: string): Promise { + const resp = await fetch(`https://openrouter.ai/api/v1/models/${modelId}/endpoints`, { + headers: { Authorization: `Bearer ${apiKey}` }, + }); + if (!resp.ok) return null; + const endpoints: OpenRouterEndpoint[] = (await resp.json()).data?.endpoints ?? []; + const busiest = endpoints + .filter((e) => e.latency_last_30m) + .sort( + (a, b) => + (b.perf_last_30m_by_workload?.text_generation?.request_count ?? 0) - + (a.perf_last_30m_by_workload?.text_generation?.request_count ?? 0) + )[0]; + if (!busiest?.latency_last_30m) return null; + return { + provider: busiest.provider_name, + latencyMs: busiest.latency_last_30m.p50, + tokensPerSecond: busiest.throughput_last_30m?.p50 ?? null, }; } @@ -14,9 +74,15 @@ export class ModelSuggestModal extends SuggestModal { private allModels: OpenRouterModel[] = []; private cached = false; private onChoose: (modelId: string) => void; + /** Latency needs an OpenRouter key; without one, rows show price only. */ + private apiKey: string; + /** Perf elements of the latest render pass, filled in once stats arrive. */ + private perfEls = new Map(); + private perfTimer: number | null = null; - constructor(app: App, onChoose: (modelId: string) => void) { + constructor(app: App, apiKey: string, onChoose: (modelId: string) => void) { super(app); + this.apiKey = apiKey; this.onChoose = onChoose; this.setPlaceholder("Search OpenRouter models..."); this.setInstructions([ @@ -35,18 +101,34 @@ export class ModelSuggestModal extends SuggestModal { this.allModels = (data.data || []) .filter((m: OpenRouterModel) => m.id && !m.id.includes(":")) .sort((a: OpenRouterModel, b: OpenRouterModel) => { - const aFree = a.pricing?.prompt === 0 ? 0 : 1; - const bFree = b.pricing?.prompt === 0 ? 0 : 1; + const aFree = this.isFree(a) ? 0 : 1; + const bFree = this.isFree(b) ? 0 : 1; if (aFree !== bFree) return aFree - bFree; return (a.name || a.id).localeCompare(b.name || b.id); }); this.cached = true; + // The modal rendered its (empty) list before the fetch finished. + this.inputEl.dispatchEvent(new Event("input")); } catch (e) { // Silently fail — the text input still works for manual entry } } + onClose() { + if (this.perfTimer !== null) window.clearTimeout(this.perfTimer); + this.perfEls.clear(); + } + + private isFree(model: OpenRouterModel): boolean { + return ( + parseFloat(model.pricing?.prompt ?? "") === 0 && + parseFloat(model.pricing?.completion ?? "") === 0 + ); + } + getSuggestions(query: string): OpenRouterModel[] { + // A new render pass starts; stats for rows no longer shown aren't needed. + this.perfEls.clear(); const q = query.toLowerCase().trim(); if (!q) return this.allModels.slice(0, 50); return this.allModels @@ -64,31 +146,76 @@ export class ModelSuggestModal extends SuggestModal { const nameEl = container.createDiv({ cls: "model-suggestion-name" }); nameEl.setText(model.name || model.id); - const idEl = container.createDiv({ cls: "model-suggestion-id" }); - idEl.setText(model.id); - idEl.style.fontSize = "0.8em"; - idEl.style.opacity = "0.6"; + if (this.isFree(model)) { + nameEl.createSpan({ cls: "model-suggestion-badge", text: "Free" }); + } - if (model.pricing?.prompt === 0 && model.pricing?.completion === 0) { - const badge = container.createSpan({ cls: "model-suggestion-badge" }); - badge.setText("Free"); - badge.style.marginLeft = "8px"; - badge.style.padding = "0 6px"; - badge.style.borderRadius = "4px"; - badge.style.backgroundColor = "var(--color-green)"; - badge.style.color = "var(--text-on-accent)"; - badge.style.fontSize = "0.75em"; + container.createDiv({ cls: "model-suggestion-id", text: model.id }); + + const stats = container.createDiv({ cls: "model-suggestion-stats" }); + const price = model.pricing; + if (price && !this.isFree(model)) { + stats.createSpan({ + text: + parseFloat(price.prompt) < 0 + ? "Variable price" + : `${formatPerMillion(price.prompt)} in · ${formatPerMillion(price.completion)} out per M tokens`, + }); + } + if (this.apiKey) { + const perfEl = stats.createSpan({ cls: "model-suggestion-perf" }); + const hit = perfCache.get(model.id); + if (hit && Date.now() - hit.at < PERF_TTL_MS) { + this.showPerf(perfEl, hit.perf); + } else { + perfEl.setText("latency…"); + this.perfEls.set(model.id, perfEl); + this.schedulePerfFetch(); + } } if (model.description) { - const descEl = container.createDiv({ cls: "model-suggestion-desc" }); - descEl.setText(model.description.slice(0, 120)); - descEl.style.fontSize = "0.75em"; - descEl.style.opacity = "0.5"; + container.createDiv({ + cls: "model-suggestion-desc", + text: model.description.slice(0, 120), + }); } } + private showPerf(el: HTMLElement, perf: ModelPerf | null) { + if (!perf) { + el.setText("no latency data"); + return; + } + const firstToken = `first token ${(perf.latencyMs / 1000).toFixed(1)} s`; + const speed = perf.tokensPerSecond ? ` · ${Math.round(perf.tokensPerSecond)} tok/s` : ""; + el.setText(`${firstToken}${speed} (${perf.provider})`); + } + + /** Waits for typing to pause so each keystroke doesn't fire ~50 requests. */ + private schedulePerfFetch() { + if (this.perfTimer !== null) window.clearTimeout(this.perfTimer); + this.perfTimer = window.setTimeout(() => { + this.perfTimer = null; + void this.fetchVisiblePerf(); + }, PERF_DEBOUNCE_MS); + } + + private async fetchVisiblePerf() { + const queue = [...this.perfEls.keys()]; + const worker = async () => { + for (let id = queue.shift(); id !== undefined; id = queue.shift()) { + if (!this.perfEls.has(id)) continue; + const perf = await fetchPerf(id, this.apiKey).catch(() => null); + perfCache.set(id, { at: Date.now(), perf }); + const el = this.perfEls.get(id); + if (el?.isConnected) this.showPerf(el, perf); + } + }; + await Promise.all(Array.from({ length: PERF_CONCURRENCY }, worker)); + } + onChooseSuggestion(model: OpenRouterModel) { this.onChoose(model.id); } -} \ No newline at end of file +} diff --git a/src/NibbleAISettingsTab.ts b/src/NibbleAISettingsTab.ts index b893c6e..d325591 100644 --- a/src/NibbleAISettingsTab.ts +++ b/src/NibbleAISettingsTab.ts @@ -1,5 +1,5 @@ import NibbleAI from "main"; -import { App, PluginSettingTab, SecretComponent, Setting, setIcon } from "obsidian"; +import { App, PluginSettingTab, SecretComponent, Setting } from "obsidian"; import { SettingsManager, PostProcessingProvider, @@ -21,17 +21,16 @@ type TabId = interface TabInfo { id: TabId; - icon: string; label: string; } const TABS: TabInfo[] = [ - { id: "api-keys", icon: "key", label: "API Keys" }, - { id: "transcription", icon: "mic", label: "Transcription" }, - { id: "proofreading", icon: "wand", label: "Proofreading" }, - { id: "title-generation", icon: "heading", label: "Title Generation" }, - { id: "property-filler", icon: "pencil", label: "Property Filler" }, - { id: "advanced", icon: "settings", label: "Advanced" }, + { id: "api-keys", label: "API Keys" }, + { id: "transcription", label: "Transcription" }, + { id: "proofreading", label: "Proofreading" }, + { id: "title-generation", label: "Title Generation" }, + { id: "property-filler", label: "Property Filler" }, + { id: "advanced", label: "Advanced" }, ]; export class NibbleAISettingsTab extends PluginSettingTab { @@ -61,12 +60,9 @@ export class NibbleAISettingsTab extends PluginSettingTab { for (const tab of TABS) { const tabBtn = tabBar.createEl("button", { - cls: `nibbleai-tab ${this.currentTab === tab.id ? "nibbleai-tab-active" : ""}`, - attr: { "data-tab": tab.id }, + cls: `nibbleai-tab${this.currentTab === tab.id ? " is-active" : ""}`, + text: tab.label, }); - const iconEl = tabBtn.createSpan({ cls: "nibbleai-tab-icon" }); - setIcon(iconEl, tab.icon); - tabBtn.createSpan({ cls: "nibbleai-tab-label", text: tab.label }); tabBtn.addEventListener("click", () => { this.currentTab = tab.id; this.display(); @@ -102,7 +98,6 @@ export class NibbleAISettingsTab extends PluginSettingTab { // ── API Keys Tab ── private renderApiKeysTab(containerEl: HTMLElement): void { - containerEl.createEl("h2", { text: "API Keys" }); containerEl.createEl("p", { text: "Pick a secret from Obsidian's keychain (Settings → Keychain) for each provider. NibbleAI stores only the secret's name; the key itself stays in the keychain.", cls: "setting-item-description", @@ -172,8 +167,6 @@ export class NibbleAISettingsTab extends PluginSettingTab { // ── Transcription Tab ── private renderTranscriptionTab(containerEl: HTMLElement): void { - containerEl.createEl("h2", { text: "Transcription" }); - new Setting(containerEl) .setName("Enable transcription") .setDesc("General settings for the audio-to-text feature") @@ -431,7 +424,6 @@ export class NibbleAISettingsTab extends PluginSettingTab { } private renderProofreadingTab(containerEl: HTMLElement): void { - containerEl.createEl("h2", { text: "Proofreading" }); containerEl.createEl("p", { text: "Proofread selected text with AI. Right-click any selection or use the command palette.", cls: "setting-item-description", @@ -553,7 +545,6 @@ export class NibbleAISettingsTab extends PluginSettingTab { // ── Title Generation Tab ── private renderTitleGenTab(containerEl: HTMLElement): void { - containerEl.createEl("h2", { text: "Title Generation" }); containerEl.createEl("p", { text: "Generate a title for the current note \u2014 either rename the file or set a frontmatter property.", cls: "setting-item-description", @@ -630,7 +621,6 @@ export class NibbleAISettingsTab extends PluginSettingTab { // ── Property Filler Tab ── private renderPropertyFillerTab(containerEl: HTMLElement): void { - containerEl.createEl("h2", { text: "Property Filler" }); containerEl.createEl("p", { text: "Fill any frontmatter property with AI-generated content.", cls: "setting-item-description", @@ -703,8 +693,6 @@ export class NibbleAISettingsTab extends PluginSettingTab { // ── Advanced Tab ── private renderAdvancedTab(containerEl: HTMLElement): void { - containerEl.createEl("h2", { text: "Advanced" }); - new Setting(containerEl) .setName("Debug mode") .setDesc("Increase verbosity for troubleshooting") @@ -884,7 +872,8 @@ export class NibbleAISettingsTab extends PluginSettingTab { .setButtonText("Browse") .setIcon("search") .onClick(() => { - new ModelSuggestModal(this.app, (modelId: string) => { + const openRouterKey = getApiKey(this.app, this.plugin.settings.openRouterSecret); + new ModelSuggestModal(this.app, openRouterKey, (modelId: string) => { onChange(modelId); textComponent.setValue(modelId); }).open(); diff --git a/styles.css b/styles.css index 2db53fa..78dd744 100644 --- a/styles.css +++ b/styles.css @@ -113,6 +113,14 @@ color: var(--text-on-accent); font-size: 0.75em; } +.model-suggestion-stats { + display: flex; + flex-wrap: wrap; + gap: 0 12px; + font-size: 0.8em; + color: var(--text-muted); + font-variant-numeric: tabular-nums; +} .model-suggestion-desc { font-size: 0.75em; opacity: 0.5; @@ -138,62 +146,40 @@ font-size: 14px; } -/* ── Settings Tab Bar ── */ +/* ── Settings Tab Bar (matches Waypoint's settings tabs) ── */ .nibbleai-tab-bar { display: flex; - flex-wrap: wrap; gap: 4px; - padding: 4px 0 12px 0; + margin-bottom: 20px; border-bottom: 1px solid var(--background-modifier-border); - margin-bottom: 16px; + padding-bottom: 8px; } .nibbleai-tab { - display: inline-flex; - align-items: center; - gap: 6px; - padding: 6px 14px; - border-radius: 6px; + background: none; border: none; - background: transparent; + cursor: var(--cursor-link, pointer); color: var(--text-muted); - cursor: pointer; + padding: 6px 14px; + border-radius: 6px 6px 0 0; font-size: var(--font-ui-small); - transition: background 0.15s, color 0.15s; - white-space: nowrap; - user-select: none; + font-weight: var(--font-medium); + transition: color 100ms, background 100ms; } .nibbleai-tab:hover { - background: var(--background-modifier-hover); color: var(--text-normal); + background: var(--background-modifier-hover); } -.nibbleai-tab-active { - background: var(--interactive-accent); - color: var(--text-on-accent); -} - -.nibbleai-tab-active:hover { - background: var(--interactive-accent-hover); - color: var(--text-on-accent); -} - -.nibbleai-tab-icon svg { - width: 16px; - height: 16px; - display: block; +.nibbleai-tab.is-active { + color: var(--text-accent); + background: var(--background-modifier-active-hover); + border-bottom: 2px solid var(--text-accent); + margin-bottom: -9px; } /* ── Tab Content ── */ -.nibbleai-tab-content h2 { - margin-top: 0; - margin-bottom: 16px; - font-size: var(--font-ui-large); - font-weight: 700; - color: var(--text-normal); -} - .nibbleai-tab-content h3 { margin-top: 28px; margin-bottom: 10px;