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.
This commit is contained in:
olivier committed 2026-10-12 16:28:59 -04:00
1 parent b4d962047b
commit 564e3ea8b3
4 files changed
+186 -82

No files matched your search

+3 -1
View File
@@ -133,10 +133,12 @@ This is useful for automation tools, Stream Deck, or scripting.
## Model Selection ## 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` - Models are fetched live from `openrouter.ai/api/v1/models`
- Free models appear first (marked with a **Free** badge) - 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 - Type to filter by name, ID, or description
- You can still type a model ID manually if you prefer - You can still type a model ID manually if you prefer
+149 -22
View File
@@ -4,9 +4,69 @@ interface OpenRouterModel {
id: string; id: string;
name: string; name: string;
description?: string; description?: string;
/** USD per token, as decimal strings (e.g. "0.0000001"); "-1" means variable. */
pricing?: { pricing?: {
prompt: number; prompt: string;
completion: number; 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<string, { at: number; perf: ModelPerf | null }>();
/** "$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<ModelPerf | null> {
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<OpenRouterModel> {
private allModels: OpenRouterModel[] = []; private allModels: OpenRouterModel[] = [];
private cached = false; private cached = false;
private onChoose: (modelId: string) => void; 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<string, HTMLElement>();
private perfTimer: number | null = null;
constructor(app: App, onChoose: (modelId: string) => void) { constructor(app: App, apiKey: string, onChoose: (modelId: string) => void) {
super(app); super(app);
this.apiKey = apiKey;
this.onChoose = onChoose; this.onChoose = onChoose;
this.setPlaceholder("Search OpenRouter models..."); this.setPlaceholder("Search OpenRouter models...");
this.setInstructions([ this.setInstructions([
@@ -35,18 +101,34 @@ export class ModelSuggestModal extends SuggestModal<OpenRouterModel> {
this.allModels = (data.data || []) this.allModels = (data.data || [])
.filter((m: OpenRouterModel) => m.id && !m.id.includes(":")) .filter((m: OpenRouterModel) => m.id && !m.id.includes(":"))
.sort((a: OpenRouterModel, b: OpenRouterModel) => { .sort((a: OpenRouterModel, b: OpenRouterModel) => {
const aFree = a.pricing?.prompt === 0 ? 0 : 1; const aFree = this.isFree(a) ? 0 : 1;
const bFree = b.pricing?.prompt === 0 ? 0 : 1; const bFree = this.isFree(b) ? 0 : 1;
if (aFree !== bFree) return aFree - bFree; if (aFree !== bFree) return aFree - bFree;
return (a.name || a.id).localeCompare(b.name || b.id); return (a.name || a.id).localeCompare(b.name || b.id);
}); });
this.cached = true; this.cached = true;
// The modal rendered its (empty) list before the fetch finished.
this.inputEl.dispatchEvent(new Event("input"));
} catch (e) { } catch (e) {
// Silently fail — the text input still works for manual entry // 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[] { 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(); const q = query.toLowerCase().trim();
if (!q) return this.allModels.slice(0, 50); if (!q) return this.allModels.slice(0, 50);
return this.allModels return this.allModels
@@ -64,30 +146,75 @@ export class ModelSuggestModal extends SuggestModal<OpenRouterModel> {
const nameEl = container.createDiv({ cls: "model-suggestion-name" }); const nameEl = container.createDiv({ cls: "model-suggestion-name" });
nameEl.setText(model.name || model.id); nameEl.setText(model.name || model.id);
const idEl = container.createDiv({ cls: "model-suggestion-id" }); if (this.isFree(model)) {
idEl.setText(model.id); nameEl.createSpan({ cls: "model-suggestion-badge", text: "Free" });
idEl.style.fontSize = "0.8em"; }
idEl.style.opacity = "0.6";
if (model.pricing?.prompt === 0 && model.pricing?.completion === 0) { container.createDiv({ cls: "model-suggestion-id", text: model.id });
const badge = container.createSpan({ cls: "model-suggestion-badge" });
badge.setText("Free"); const stats = container.createDiv({ cls: "model-suggestion-stats" });
badge.style.marginLeft = "8px"; const price = model.pricing;
badge.style.padding = "0 6px"; if (price && !this.isFree(model)) {
badge.style.borderRadius = "4px"; stats.createSpan({
badge.style.backgroundColor = "var(--color-green)"; text:
badge.style.color = "var(--text-on-accent)"; parseFloat(price.prompt) < 0
badge.style.fontSize = "0.75em"; ? "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) { if (model.description) {
const descEl = container.createDiv({ cls: "model-suggestion-desc" }); container.createDiv({
descEl.setText(model.description.slice(0, 120)); cls: "model-suggestion-desc",
descEl.style.fontSize = "0.75em"; text: model.description.slice(0, 120),
descEl.style.opacity = "0.5"; });
} }
} }
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) { onChooseSuggestion(model: OpenRouterModel) {
this.onChoose(model.id); this.onChoose(model.id);
} }
+11 -22
View File
@@ -1,5 +1,5 @@
import NibbleAI from "main"; import NibbleAI from "main";
import { App, PluginSettingTab, SecretComponent, Setting, setIcon } from "obsidian"; import { App, PluginSettingTab, SecretComponent, Setting } from "obsidian";
import { import {
SettingsManager, SettingsManager,
PostProcessingProvider, PostProcessingProvider,
@@ -21,17 +21,16 @@ type TabId =
interface TabInfo { interface TabInfo {
id: TabId; id: TabId;
icon: string;
label: string; label: string;
} }
const TABS: TabInfo[] = [ const TABS: TabInfo[] = [
{ id: "api-keys", icon: "key", label: "API Keys" }, { id: "api-keys", label: "API Keys" },
{ id: "transcription", icon: "mic", label: "Transcription" }, { id: "transcription", label: "Transcription" },
{ id: "proofreading", icon: "wand", label: "Proofreading" }, { id: "proofreading", label: "Proofreading" },
{ id: "title-generation", icon: "heading", label: "Title Generation" }, { id: "title-generation", label: "Title Generation" },
{ id: "property-filler", icon: "pencil", label: "Property Filler" }, { id: "property-filler", label: "Property Filler" },
{ id: "advanced", icon: "settings", label: "Advanced" }, { id: "advanced", label: "Advanced" },
]; ];
export class NibbleAISettingsTab extends PluginSettingTab { export class NibbleAISettingsTab extends PluginSettingTab {
@@ -61,12 +60,9 @@ export class NibbleAISettingsTab extends PluginSettingTab {
for (const tab of TABS) { for (const tab of TABS) {
const tabBtn = tabBar.createEl("button", { const tabBtn = tabBar.createEl("button", {
cls: `nibbleai-tab ${this.currentTab === tab.id ? "nibbleai-tab-active" : ""}`, cls: `nibbleai-tab${this.currentTab === tab.id ? " is-active" : ""}`,
attr: { "data-tab": tab.id }, 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", () => { tabBtn.addEventListener("click", () => {
this.currentTab = tab.id; this.currentTab = tab.id;
this.display(); this.display();
@@ -102,7 +98,6 @@ export class NibbleAISettingsTab extends PluginSettingTab {
// ── API Keys Tab ── // ── API Keys Tab ──
private renderApiKeysTab(containerEl: HTMLElement): void { private renderApiKeysTab(containerEl: HTMLElement): void {
containerEl.createEl("h2", { text: "API Keys" });
containerEl.createEl("p", { 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.", 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", cls: "setting-item-description",
@@ -172,8 +167,6 @@ export class NibbleAISettingsTab extends PluginSettingTab {
// ── Transcription Tab ── // ── Transcription Tab ──
private renderTranscriptionTab(containerEl: HTMLElement): void { private renderTranscriptionTab(containerEl: HTMLElement): void {
containerEl.createEl("h2", { text: "Transcription" });
new Setting(containerEl) new Setting(containerEl)
.setName("Enable transcription") .setName("Enable transcription")
.setDesc("General settings for the audio-to-text feature") .setDesc("General settings for the audio-to-text feature")
@@ -431,7 +424,6 @@ export class NibbleAISettingsTab extends PluginSettingTab {
} }
private renderProofreadingTab(containerEl: HTMLElement): void { private renderProofreadingTab(containerEl: HTMLElement): void {
containerEl.createEl("h2", { text: "Proofreading" });
containerEl.createEl("p", { containerEl.createEl("p", {
text: "Proofread selected text with AI. Right-click any selection or use the command palette.", text: "Proofread selected text with AI. Right-click any selection or use the command palette.",
cls: "setting-item-description", cls: "setting-item-description",
@@ -553,7 +545,6 @@ export class NibbleAISettingsTab extends PluginSettingTab {
// ── Title Generation Tab ── // ── Title Generation Tab ──
private renderTitleGenTab(containerEl: HTMLElement): void { private renderTitleGenTab(containerEl: HTMLElement): void {
containerEl.createEl("h2", { text: "Title Generation" });
containerEl.createEl("p", { containerEl.createEl("p", {
text: "Generate a title for the current note \u2014 either rename the file or set a frontmatter property.", text: "Generate a title for the current note \u2014 either rename the file or set a frontmatter property.",
cls: "setting-item-description", cls: "setting-item-description",
@@ -630,7 +621,6 @@ export class NibbleAISettingsTab extends PluginSettingTab {
// ── Property Filler Tab ── // ── Property Filler Tab ──
private renderPropertyFillerTab(containerEl: HTMLElement): void { private renderPropertyFillerTab(containerEl: HTMLElement): void {
containerEl.createEl("h2", { text: "Property Filler" });
containerEl.createEl("p", { containerEl.createEl("p", {
text: "Fill any frontmatter property with AI-generated content.", text: "Fill any frontmatter property with AI-generated content.",
cls: "setting-item-description", cls: "setting-item-description",
@@ -703,8 +693,6 @@ export class NibbleAISettingsTab extends PluginSettingTab {
// ── Advanced Tab ── // ── Advanced Tab ──
private renderAdvancedTab(containerEl: HTMLElement): void { private renderAdvancedTab(containerEl: HTMLElement): void {
containerEl.createEl("h2", { text: "Advanced" });
new Setting(containerEl) new Setting(containerEl)
.setName("Debug mode") .setName("Debug mode")
.setDesc("Increase verbosity for troubleshooting") .setDesc("Increase verbosity for troubleshooting")
@@ -884,7 +872,8 @@ export class NibbleAISettingsTab extends PluginSettingTab {
.setButtonText("Browse") .setButtonText("Browse")
.setIcon("search") .setIcon("search")
.onClick(() => { .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); onChange(modelId);
textComponent.setValue(modelId); textComponent.setValue(modelId);
}).open(); }).open();
+23 -37
View File
@@ -113,6 +113,14 @@
color: var(--text-on-accent); color: var(--text-on-accent);
font-size: 0.75em; 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 { .model-suggestion-desc {
font-size: 0.75em; font-size: 0.75em;
opacity: 0.5; opacity: 0.5;
@@ -138,62 +146,40 @@
font-size: 14px; font-size: 14px;
} }
/* ── Settings Tab Bar ── */ /* ── Settings Tab Bar (matches Waypoint's settings tabs) ── */
.nibbleai-tab-bar { .nibbleai-tab-bar {
display: flex; display: flex;
flex-wrap: wrap;
gap: 4px; gap: 4px;
padding: 4px 0 12px 0; margin-bottom: 20px;
border-bottom: 1px solid var(--background-modifier-border); border-bottom: 1px solid var(--background-modifier-border);
margin-bottom: 16px; padding-bottom: 8px;
} }
.nibbleai-tab { .nibbleai-tab {
display: inline-flex; background: none;
align-items: center;
gap: 6px;
padding: 6px 14px;
border-radius: 6px;
border: none; border: none;
background: transparent; cursor: var(--cursor-link, pointer);
color: var(--text-muted); color: var(--text-muted);
cursor: pointer; padding: 6px 14px;
border-radius: 6px 6px 0 0;
font-size: var(--font-ui-small); font-size: var(--font-ui-small);
transition: background 0.15s, color 0.15s; font-weight: var(--font-medium);
white-space: nowrap; transition: color 100ms, background 100ms;
user-select: none;
} }
.nibbleai-tab:hover { .nibbleai-tab:hover {
background: var(--background-modifier-hover);
color: var(--text-normal); color: var(--text-normal);
background: var(--background-modifier-hover);
} }
.nibbleai-tab-active { .nibbleai-tab.is-active {
background: var(--interactive-accent); color: var(--text-accent);
color: var(--text-on-accent); background: var(--background-modifier-active-hover);
} border-bottom: 2px solid var(--text-accent);
margin-bottom: -9px;
.nibbleai-tab-active:hover {
background: var(--interactive-accent-hover);
color: var(--text-on-accent);
}
.nibbleai-tab-icon svg {
width: 16px;
height: 16px;
display: block;
} }
/* ── Tab Content ── */ /* ── 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 { .nibbleai-tab-content h3 {
margin-top: 28px; margin-top: 28px;
margin-bottom: 10px; margin-bottom: 10px;