Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.
feat(web): plain-language model names, renaming, and a reasoning picker
Frank Arena7ca4d13a39b6
diff --git a/src/providers/chatgpt-web.ts b/src/providers/chatgpt-web.tsindex 219ecdef..c6917c19 100644--- a/src/providers/chatgpt-web.ts+++ b/src/providers/chatgpt-web.ts@@ -6,6 +6,7 @@ import type { LLMResponse, LLMStreamChunk } from './base.js';import { getValidChatGPTSession, getChatGPTSession } from '../auth/chatgpt-auth.js';import { CHATGPT_BACKEND_API } from '../auth/chatgpt-session.js';import { logger } from '../utils/logger.js';+import { applyReasoningToBody, getModelPref } from '../utils/model-prefs.js';// Fields the codex endpoint does not acceptconst UNSUPPORTED_FIELDS = [@@ -17,9 +18,12 @@ const UNSUPPORTED_FIELDS = [/*** Sanitise an outgoing request body for the ChatGPT codex/responses endpoint.*/-function sanitiseBody(raw: string): string {+function sanitiseBody(raw: string, providerName: string): string {const body = JSON.parse(raw);+ // User-chosen reasoning level for this model (dashboard reasoning picker).+ if (typeof body.model === 'string') applyReasoningToBody(body, getModelPref(providerName, body.model).reasoning);+// Requiredbody.store = false;body.stream = true;@@ -176,7 +180,7 @@ export class ChatGPTWebProvider extends BaseProvider {} catch { /* ignore */ }try {- modifiedInit.body = sanitiseBody(init.body);+ modifiedInit.body = sanitiseBody(init.body, this.name);} catch { /* pass through */ }}diff --git a/src/providers/openai-compat.reasoning.test.ts b/src/providers/openai-compat.reasoning.test.tsnew file mode 100644index 00000000..d13f73a0--- /dev/null+++ b/src/providers/openai-compat.reasoning.test.ts@@ -0,0 +1,75 @@+import { mkdtempSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { generateText } from 'ai';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import { OpenAICompatProvider } from './openai-compat.js';+import { resetModelPrefsCache, setModelPref } from '../utils/model-prefs.js';+import type { ProviderConfig } from '../utils/config.js';++const config = (name: string): ProviderConfig => ({+ name,+ apiKey: '',+ baseUrl: 'http://ollama.test/v1',+ model: 'qwen3.8:27b',+ enabled: true,+});++/** Minimal Chat Completions reply so generateText resolves. */+function completion(): Response {+ return new Response(JSON.stringify({+ id: 'c1', object: 'chat.completion', created: 0, model: 'qwen3.8:27b',+ choices: [{ index: 0, message: { role: 'assistant', content: 'ok' }, finish_reason: 'stop' }],+ usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 },+ }), { status: 200, headers: { 'Content-Type': 'application/json' } });+}++describe('OpenAICompatProvider reasoning preference', () => {+ let home: string;+ const previousHome = process.env.MERCURY_HOME;+ let sent: Record<string, unknown>[];++ beforeEach(() => {+ home = mkdtempSync(join(tmpdir(), 'mercury-reasoning-'));+ process.env.MERCURY_HOME = home;+ resetModelPrefsCache();+ sent = [];+ vi.stubGlobal('fetch', vi.fn(async (_url: unknown, init?: RequestInit) => {+ sent.push(JSON.parse(String(init?.body)));+ return completion();+ }));+ });++ afterEach(() => {+ vi.unstubAllGlobals();+ resetModelPrefsCache();+ if (previousHome === undefined) delete process.env.MERCURY_HOME;+ else process.env.MERCURY_HOME = previousHome;+ rmSync(home, { recursive: true, force: true });+ });++ async function send(name: string) {+ const provider = new OpenAICompatProvider(config(name), { useChatApi: true });+ await generateText({ model: provider.getModelInstance(), prompt: 'hi' });+ return sent[0]!;+ }++ it('sends reasoning_effort "none" when thinking is turned off for an Ollama model', async () => {+ setModelPref('ollamaLocal', 'qwen3.8:27b', { reasoning: 'off' });+ expect((await send('ollamaLocal')).reasoning_effort).toBe('none');+ });++ it('sends the chosen level even for model ids the SDK does not know are reasoning models', async () => {+ setModelPref('ollamaLocal', 'qwen3.8:27b', { reasoning: 'high' });+ expect((await send('ollamaLocal')).reasoning_effort).toBe('high');+ });++ it('sends nothing on auto', async () => {+ expect(await send('ollamaLocal')).not.toHaveProperty('reasoning_effort');+ });++ it('leaves providers without a reasoning control untouched', async () => {+ setModelPref('lmStudio', 'qwen3.8:27b', { reasoning: 'high' });+ expect(await send('lmStudio')).not.toHaveProperty('reasoning_effort');+ });+});diff --git a/src/providers/openai-compat.ts b/src/providers/openai-compat.tsindex ff9530ab..f080dd8f 100644--- a/src/providers/openai-compat.ts+++ b/src/providers/openai-compat.ts@@ -5,6 +5,28 @@ import type { ProviderConfig } from '../utils/config.js';import type { LLMResponse, LLMStreamChunk } from './base.js';import { logger } from '../utils/logger.js';import { aimlapiHeaders } from './aimlapi-attribution.js';+import { applyReasoningToBody, getModelPref, reasoningLevelsFor } from '../utils/model-prefs.js';++/**+ * fetch that applies the user's reasoning level for the requested model.+ * Done on the wire because the AI SDK drops reasoningEffort for model ids it+ * doesn't recognise as reasoning models (every Ollama model, for one).+ */+function withReasoningPref(providerName: string): typeof fetch {+ return async (input, init) => {+ if (typeof init?.body === 'string') {+ try {+ const body = JSON.parse(init.body) as Record<string, unknown>;+ const level = typeof body.model === 'string' ? getModelPref(providerName, body.model).reasoning : undefined;+ if (level) {+ applyReasoningToBody(body, level);+ init = { ...init, body: JSON.stringify(body) };+ }+ } catch { /* not JSON — pass through */ }+ }+ return globalThis.fetch(input, init);+ };+}export class OpenAICompatProvider extends BaseProvider {readonly name: string;@@ -23,6 +45,9 @@ export class OpenAICompatProvider extends BaseProvider {// `undefined` for every host but AI/ML API's, so no other provider// reached through this class sends anything extra.headers: aimlapiHeaders(config.baseUrl),+ // Only providers with a reasoning control get the hook; the rest keep+ // the SDK's default fetch untouched.+ fetch: reasoningLevelsFor(config.name).length ? withReasoningPref(config.name) : undefined,});this.modelInstance = useChatApi? this.client.chat(config.model)diff --git a/src/utils/model-prefs.test.ts b/src/utils/model-prefs.test.tsnew file mode 100644index 00000000..129833dc--- /dev/null+++ b/src/utils/model-prefs.test.ts@@ -0,0 +1,114 @@+import { mkdtempSync, readFileSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import {+ applyReasoningToBody,+ displayModelName,+ getModelPref,+ humanizeModelName,+ modelGroupLabel,+ reasoningLevelsFor,+ resetModelPrefsCache,+ setModelPref,+} from './model-prefs.js';++describe('humanizeModelName', () => {+ it.each([+ ['qwen3.8:27b-nvfp4-5090', 'Qwen 3.8 27B (NVFP4 · 5090)'],+ ['gpt-6-sol', 'GPT-6 Sol'],+ ['gpt-5.6-sol', 'GPT-5.6 Sol'],+ ['gpt-oss:120b-cloud', 'GPT OSS 120B'],+ ['kimi-k2.7-code:cloud', 'Kimi K2.7 Code'],+ ['gemma4:12b', 'Gemma 4 12B'],+ ['deepseek-v4-pro:0813-cloud', 'DeepSeek V4 Pro (0813)'],+ ['orcarouter/Qwen3.8-27B-Uncensored:q6_K', 'Qwen 3.8 27B Uncensored (Q6_K)'],+ ['hf.co/mradermacher/qwen3.5-9b-nsfw-captioning-v5-GGUF:Q8_0', 'Qwen 3.5 9B NSFW Captioning V5 (Q8_0)'],+ ['mistral-small-24b-huihui-ablit-q4ks:latest', 'Mistral Small 24B Huihui Abliterated (Q4KS)'],+ ['codex-auto-review', 'Codex Auto Review'],+ ['hf.co/BennyDaBall/Qwopus3.8-27B-Flash-NVFP4:NVFP4', 'Qwopus 3.8 27B Flash (NVFP4)'],+ ])('%s → %s', (raw, expected) => {+ expect(humanizeModelName(raw)).toBe(expected);+ });++ it('never returns an empty name', () => {+ expect(humanizeModelName(':latest')).not.toBe('');+ });+});++describe('modelGroupLabel', () => {+ it('separates Ollama cloud models served through the local daemon', () => {+ expect(modelGroupLabel('ollamaLocal', 'glm-5.3:cloud')).toBe('Ollama Cloud · via this PC');+ expect(modelGroupLabel('ollamaLocal', 'gpt-oss:20b-cloud')).toBe('Ollama Cloud · via this PC');+ expect(modelGroupLabel('ollamaLocal', 'gemma4:12b')).toBe('Ollama · this PC');+ expect(modelGroupLabel('chatgptWeb', 'gpt-6-sol')).toBe('ChatGPT');+ });+});++describe('reasoning', () => {+ it('offers Off only where it is known to work', () => {+ expect(reasoningLevelsFor('ollamaLocal')).toContain('off');+ expect(reasoningLevelsFor('chatgptWeb')).toEqual(['low', 'medium', 'high']);+ expect(reasoningLevelsFor('deepseek')).toEqual([]);+ });++ it('sets reasoning.effort on Responses API bodies, keeping other reasoning fields', () => {+ const body: Record<string, unknown> = { input: [], reasoning: { summary: 'auto' } };+ applyReasoningToBody(body, 'high');+ expect(body.reasoning).toEqual({ summary: 'auto', effort: 'high' });+ });++ it('sets reasoning_effort on Chat Completions bodies, mapping off to none', () => {+ const body: Record<string, unknown> = { messages: [] };+ applyReasoningToBody(body, 'off');+ expect(body.reasoning_effort).toBe('none');+ });++ it('leaves the body alone on auto', () => {+ const body: Record<string, unknown> = { messages: [] };+ applyReasoningToBody(body, undefined);+ expect(body).toEqual({ messages: [] });+ });+});++describe('model prefs store', () => {+ let home: string;+ const previousHome = process.env.MERCURY_HOME;++ beforeEach(() => {+ home = mkdtempSync(join(tmpdir(), 'mercury-prefs-'));+ process.env.MERCURY_HOME = home;+ resetModelPrefsCache();+ });++ afterEach(() => {+ resetModelPrefsCache();+ if (previousHome === undefined) delete process.env.MERCURY_HOME;+ else process.env.MERCURY_HOME = previousHome;+ rmSync(home, { recursive: true, force: true });+ });++ it('stores a label and reasoning level and persists them to disk', () => {+ setModelPref('ollamaLocal', 'qwen3.8:27b', { label: ' My Qwen ', reasoning: 'off' });+ expect(getModelPref('ollamaLocal', 'qwen3.8:27b')).toEqual({ label: 'My Qwen', reasoning: 'off' });+ expect(displayModelName('ollamaLocal', 'qwen3.8:27b')).toBe('My Qwen');++ resetModelPrefsCache();+ const onDisk = JSON.parse(readFileSync(join(home, 'model-prefs.json'), 'utf-8'));+ expect(onDisk['ollamaLocal::qwen3.8:27b']).toEqual({ label: 'My Qwen', reasoning: 'off' });+ });++ it('clears fields with an empty label or auto, and drops empty entries', () => {+ setModelPref('chatgptWeb', 'gpt-6-sol', { label: 'Sol', reasoning: 'high' });+ setModelPref('chatgptWeb', 'gpt-6-sol', { label: '' });+ expect(getModelPref('chatgptWeb', 'gpt-6-sol')).toEqual({ reasoning: 'high' });+ setModelPref('chatgptWeb', 'gpt-6-sol', { reasoning: 'auto' });+ expect(getModelPref('chatgptWeb', 'gpt-6-sol')).toEqual({});+ expect(displayModelName('chatgptWeb', 'gpt-6-sol')).toBe('GPT-6 Sol');+ });++ it('ignores unknown reasoning values', () => {+ setModelPref('ollamaLocal', 'm', { reasoning: 'turbo' as never });+ expect(getModelPref('ollamaLocal', 'm')).toEqual({});+ });+});diff --git a/src/utils/model-prefs.ts b/src/utils/model-prefs.tsnew file mode 100644index 00000000..01b7d04b--- /dev/null+++ b/src/utils/model-prefs.ts@@ -0,0 +1,227 @@+/**+ * Per-model user preferences: a display name and a reasoning level.+ *+ * Kept in ~/.mercury/model-prefs.json rather than mercury.yaml because the+ * agent holds its own copy of the config and rewrites the whole file on+ * provider switches, which would clobber prefs saved from the dashboard.+ * Everything runs in the daemon process, so the in-memory copy here is the+ * source of truth once loaded.+ */+import { existsSync, readFileSync, writeFileSync, renameSync } from 'node:fs';+import { join } from 'node:path';+import { getMercuryHome } from './config.js';++export type ReasoningLevel = 'off' | 'low' | 'medium' | 'high';++export interface ModelPref {+ /** User-chosen display name; absent means use the generated one. */+ label?: string;+ /** Absent means "auto": send nothing and let the model decide. */+ reasoning?: ReasoningLevel;+}++const REASONING_LEVELS: readonly ReasoningLevel[] = ['off', 'low', 'medium', 'high'];+const MAX_LABEL_LENGTH = 80;++let prefs: Record<string, ModelPref> | null = null;++function prefsPath(): string {+ return join(getMercuryHome(), 'model-prefs.json');+}++function key(provider: string, model: string): string {+ return `${provider}::${model}`;+}++function load(): Record<string, ModelPref> {+ if (prefs) return prefs;+ try {+ const path = prefsPath();+ prefs = existsSync(path) ? (JSON.parse(readFileSync(path, 'utf-8')) as Record<string, ModelPref>) : {};+ } catch {+ prefs = {};+ }+ return prefs;+}++function save(): void {+ const path = prefsPath();+ const tmp = `${path}.tmp`;+ writeFileSync(tmp, JSON.stringify(load(), null, 2), 'utf-8');+ renameSync(tmp, path);+}++export function isReasoningLevel(value: unknown): value is ReasoningLevel {+ return typeof value === 'string' && (REASONING_LEVELS as readonly string[]).includes(value);+}++export function getModelPref(provider: string, model: string): ModelPref {+ return load()[key(provider, model)] ?? {};+}++/**+ * Update a model's prefs. `null` (or an empty label / 'auto') clears a field;+ * `undefined` leaves it unchanged.+ */+export function setModelPref(+ provider: string,+ model: string,+ update: { label?: string | null; reasoning?: ReasoningLevel | 'auto' | null },+): ModelPref {+ const all = load();+ const next: ModelPref = { ...all[key(provider, model)] };++ if (update.label !== undefined) {+ const label = update.label?.trim().slice(0, MAX_LABEL_LENGTH);+ if (label) next.label = label;+ else delete next.label;+ }+ if (update.reasoning !== undefined) {+ if (isReasoningLevel(update.reasoning)) next.reasoning = update.reasoning;+ else delete next.reasoning;+ }++ if (next.label === undefined && next.reasoning === undefined) delete all[key(provider, model)];+ else all[key(provider, model)] = next;+ save();+ return next;+}++/** Test hook: forget the in-memory copy so the next read hits disk. */+export function resetModelPrefsCache(): void {+ prefs = null;+}++// ─── Reasoning ───────────────────────────────────────────────────────────────++/**+ * Levels a provider can honour. ChatGPT/OpenAI models may reject "none", so+ * they get no Off; Ollama maps Off to reasoning_effort "none" (thinking off).+ * Providers not listed have no reasoning control.+ */+export function reasoningLevelsFor(provider: string): ReasoningLevel[] {+ if (provider === 'chatgptWeb' || provider === 'openai') return ['low', 'medium', 'high'];+ if (provider === 'ollamaLocal' || provider === 'ollamaCloud') return ['off', 'low', 'medium', 'high'];+ return [];+}++/**+ * Apply a reasoning level to an outgoing OpenAI-style request body, in place.+ * Done at the HTTP layer because the AI SDK drops reasoningEffort for model+ * ids it doesn't recognise as reasoning models (anything not o1/o3/o4/gpt-5*).+ */+export function applyReasoningToBody(body: Record<string, unknown>, level: ReasoningLevel | undefined): void {+ if (!level) return;+ if (Array.isArray(body.input)) {+ // Responses API+ const reasoning = (body.reasoning && typeof body.reasoning === 'object' ? body.reasoning : {}) as Record<string, unknown>;+ body.reasoning = { ...reasoning, effort: level === 'off' ? 'none' : level };+ } else if (Array.isArray(body.messages)) {+ // Chat Completions (Ollama's /v1 maps "none" to thinking off)+ body.reasoning_effort = level === 'off' ? 'none' : level;+ }+}++// ─── Display names ───────────────────────────────────────────────────────────++const PROVIDER_LABELS: Record<string, string> = {+ chatgptWeb: 'ChatGPT',+ openai: 'OpenAI API',+ anthropic: 'Claude (API)',+ deepseek: 'DeepSeek',+ grok: 'Grok',+ ollamaLocal: 'Ollama · this PC',+ ollamaCloud: 'Ollama Cloud',+ lmStudio: 'LM Studio',+ atomicChat: 'Atomic Chat',+ openaiCompat: 'OpenAI-compatible',+ litellm: 'LiteLLM',+ aimlapi: 'AI/ML API',+ atlascloud: 'Atlas Cloud',+ mimo: 'MiMo',+ mimoTokenPlan: 'MiMo Token Plan',+ githubCopilot: 'GitHub Copilot',+ mercuryCloud: 'Mercury Cloud',+};++/** True for Ollama models that run on ollama.com but are served through the local daemon. */+export function isOllamaCloudModel(model: string): boolean {+ return /(^|[:-])cloud$/i.test(model);+}++/** The heading a model is grouped under in pickers. */+export function modelGroupLabel(provider: string, model: string): string {+ if (provider === 'ollamaLocal' && isOllamaCloudModel(model)) return 'Ollama Cloud · via this PC';+ return PROVIDER_LABELS[provider] ?? provider;+}++const WORD_CASE: Record<string, string> = {+ gpt: 'GPT', oss: 'OSS', glm: 'GLM', qwen: 'Qwen', gemma: 'Gemma', kimi: 'Kimi',+ deepseek: 'DeepSeek', mistral: 'Mistral', minimax: 'MiniMax', nemotron: 'Nemotron',+ llama: 'Llama', phi: 'Phi', nsfw: 'NSFW', ai: 'AI', vl: 'VL', moe: 'MoE', it: 'IT',+ qwopus: 'Qwopus', ablit: 'Abliterated', abliterated: 'Abliterated', huihui: 'Huihui',+};++const QUANT_RE = /^(i?q\d[a-z0-9_]*|f16|fp16|bf16|fp8|int8|int4|nvfp4|mxfp4|awq|gptq)$/i;++function titleWord(word: string): string {+ const lower = word.toLowerCase();+ if (WORD_CASE[lower]) return WORD_CASE[lower];+ // Sizes and versions: 27b → 27B, 120b → 120B, a3b → A3B, v4.1 → V4.1, k2.7 → K2.7+ if (/^\d+(\.\d+)?[bmk]$/i.test(word)) return word.toUpperCase();+ if (/^[a-z]\d+(\.\d+)?[a-z]?$/i.test(word)) return word.toUpperCase();+ if (/^\d/.test(word)) return word;+ // A brand fused with a version: qwen3.8 → Qwen 3.8, gemma4 → Gemma 4+ const fused = /^([a-z]+?)(\d+(?:\.\d+)*)$/i.exec(word);+ if (fused && WORD_CASE[fused[1].toLowerCase()]) return `${WORD_CASE[fused[1].toLowerCase()]} ${fused[2]}`;+ return word.charAt(0).toUpperCase() + word.slice(1);+}++/**+ * Turn a raw model id into something readable, e.g.+ * qwen3.8:27b-nvfp4-5090 → Qwen 3.8 27B (NVFP4 · 5090)+ * gpt-6-sol → GPT-6 Sol+ * hf.co/user/Foo-27B-GGUF:Q4_K_M → Foo 27B (Q4_K_M)+ */+export function humanizeModelName(model: string): string {+ let id = model.trim();+ if (!id) return model;+ id = id.replace(/^hf\.co\/[^/]+\//i, '');+ if (id.includes('/')) id = id.slice(id.lastIndexOf('/') + 1);++ const [rawName, rawTag = ''] = id.split(':', 2);+ const name = rawName.replace(/[-_.]gguf$/i, '');+ const nameWords: string[] = [];+ const extras: string[] = [];++ // Hyphens separate words; underscores belong to quant tags like Q4_K_M.+ const parts = name.split(/[-\s]+/).filter(Boolean);+ for (let i = 0; i < parts.length; i++) {+ const part = parts[i];+ // Keep GPT-style "GPT-6" / "GPT-5.6" joined.+ if (/^gpt$/i.test(part) && parts[i + 1] && /^\d/.test(parts[i + 1])) {+ nameWords.push(`GPT-${parts[++i]}`);+ } else if (QUANT_RE.test(part)) {+ extras.push(part.toUpperCase());+ } else {+ nameWords.push(titleWord(part));+ }+ }++ for (const part of rawTag.split(/-+/).filter(Boolean)) {+ if (/^(latest|cloud)$/i.test(part)) continue;+ if (QUANT_RE.test(part)) extras.push(part.toUpperCase());+ else if (/^\d+(\.\d+)?[bmk]$/i.test(part)) nameWords.push(part.toUpperCase());+ else if (/^\d{4}$/.test(part)) extras.push(part);+ else nameWords.push(titleWord(part));+ }++ const base = nameWords.join(' ').replace(/\s+/g, ' ').trim() || model;+ const tags = [...new Set(extras)];+ return tags.length ? `${base} (${tags.join(' · ')})` : base;+}++/** The name to show for a model: the user's label, else the generated one. */+export function displayModelName(provider: string, model: string): string {+ return getModelPref(provider, model).label ?? humanizeModelName(model);+}diff --git a/src/web/api/chat.ts b/src/web/api/chat.tsindex ba7f8c20..5cb4577f 100644--- a/src/web/api/chat.ts+++ b/src/web/api/chat.ts@@ -5,6 +5,10 @@ import { existsSync, readFileSync, writeFileSync, readdirSync, statSync } from 'import { join, extname, basename, relative, resolve } from 'node:path';import { getMercuryHome, loadConfig, getActiveProviders } from '../../utils/config.js';import type { SessionRepository } from '../../sessions/index.js';+import {+ getModelPref, setModelPref, humanizeModelName, modelGroupLabel,+ reasoningLevelsFor, isReasoningLevel, type ReasoningLevel,+} from '../../utils/model-prefs.js';let webChannel: WebChannel | null = null;let programmingMode: ProgrammingMode | null = null;@@ -257,11 +261,24 @@ async function listProviderModels(p: { name: string; baseUrl: string }): Promisereturn [];}+/** What the picker needs to show a model in plain language. */+function describeModel(provider: string, model: string) {+ const pref = getModelPref(provider, model);+ return {+ label: pref.label ?? humanizeModelName(model),+ autoLabel: humanizeModelName(model),+ customLabel: pref.label ?? null,+ group: modelGroupLabel(provider, model),+ reasoning: pref.reasoning ?? 'auto',+ reasoningLevels: reasoningLevelsFor(provider),+ };+}+chat.get('/api/chat/models', async (c) => {const config = loadConfig();const active = getActiveProviders(config);const current = currentProviderFn ? currentProviderFn() : { name: config.providers.default || '', model: '' };- const entries: { name: string; model: string; available: boolean; isCurrent: boolean }[] = [];+ const entries: ({ name: string; model: string; available: boolean; isCurrent: boolean } & ReturnType<typeof describeModel>)[] = [];const perProvider = await Promise.all(active.map((p) => listProviderModels(p)));for (const [i, p] of active.entries()) {const models = perProvider[i];@@ -272,10 +289,31 @@ chat.get('/api/chat/models', async (c) => {model,available: true,isCurrent: p.name === current.name && model === current.model,+ ...describeModel(p.name, model),});}}- return c.json({ current, providers: entries });+ return c.json({ current: { ...current, ...describeModel(current.name, current.model) }, providers: entries });+});++/** Rename a model (label) and/or set its reasoning level. null / '' / 'auto' resets. */+chat.post('/api/chat/models/prefs', async (c) => {+ const body = await c.req.json<{ provider?: string; model?: string; label?: string | null; reasoning?: string | null }>();+ const provider = body.provider?.trim();+ const model = body.model?.trim();+ if (!provider || !model) {+ return c.json({ error: 'provider and model are required' }, 400);+ }+ if (body.reasoning != null && body.reasoning !== 'auto') {+ if (!isReasoningLevel(body.reasoning) || !reasoningLevelsFor(provider).includes(body.reasoning)) {+ return c.json({ error: `Reasoning level '${body.reasoning}' is not supported for ${provider}` }, 400);+ }+ }+ setModelPref(provider, model, {+ label: body.label,+ reasoning: body.reasoning === undefined ? undefined : (body.reasoning as ReasoningLevel | 'auto' | null),+ });+ return c.json({ ok: true, ...describeModel(provider, model) });});chat.post('/api/chat/models/switch', async (c) => {diff --git a/ui/src/components/chat/ModelSwitcher.tsx b/ui/src/components/chat/ModelSwitcher.tsxindex 4af25d6d..aa5b0151 100644--- a/ui/src/components/chat/ModelSwitcher.tsx+++ b/ui/src/components/chat/ModelSwitcher.tsx@@ -1,15 +1,15 @@-import { useState, useCallback } from "react";-import { ChevronDown, Loader2, Check } from "lucide-react";+import { useState, useCallback, useEffect, useMemo } from "react";+import { Brain, Check, ChevronDown, Loader2, Pencil, RotateCcw, Search, X } from "lucide-react";import { cn } from "@/lib/utils";import { Button } from "@/components/ui/button";-import { Badge } from "@/components/ui/badge";+import { Input } from "@/components/ui/input";import {Popover,PopoverContent,PopoverTrigger,} from "@/components/ui/popover";import api from "@/lib/api";-import type { ModelsResponse } from "@/lib/api";+import type { ModelsResponse, ReasoningChoice } from "@/lib/api";import { useChatStore } from "@/stores/chat";interface ModelSwitcherProps {@@ -17,24 +17,37 @@ interface ModelSwitcherProps {currentModel: string;}-export function ModelSwitcher({- currentProvider,- currentModel,-}: ModelSwitcherProps) {+type ModelEntry = ModelsResponse["providers"][number];++const REASONING_COPY: Record<ReasoningChoice, { title: string; hint: string }> = {+ auto: { title: "Auto", hint: "Let the model decide (its default)" },+ off: { title: "Off", hint: "Fastest: answers without thinking first" },+ low: { title: "Light", hint: "A quick think: good for simple tasks" },+ medium: { title: "Balanced", hint: "A good mix of speed and depth" },+ high: { title: "Deep", hint: "Thinks hardest: slower, best for tricky work" },+};++const keyOf = (p: { name: string; model: string }) => `${p.name}::${p.model}`;++export function ModelSwitcher({ currentProvider, currentModel }: ModelSwitcherProps) {const [open, setOpen] = useState(false);+ const [reasoningOpen, setReasoningOpen] = useState(false);const [models, setModels] = useState<ModelsResponse | null>(null);const [loading, setLoading] = useState(false);const [switching, setSwitching] = useState<string | null>(null);+ const [query, setQuery] = useState("");+ const [editing, setEditing] = useState<string | null>(null);+ const [draft, setDraft] = useState("");+ const [saving, setSaving] = useState(false);const setProvider = useChatStore((s) => s.setProvider);const setModel = useChatStore((s) => s.setModel);const loadModels = useCallback(async () => {- // Always refetch: the Ollama model list changes as models are pulled.+ // Always refetch: model lists and names change (pulls, renames).setLoading(!models);try {- const data = await api.chat.models.list();- setModels(data);+ setModels(await api.chat.models.list());} catch {// silently fail} finally {@@ -42,16 +55,21 @@ export function ModelSwitcher({}}, [models]);- async function handleSwitch(providerName: string, model: string) {- setSwitching(`${providerName}::${model}`);+ // Load once up front so the button can show the friendly name.+ useEffect(() => {+ loadModels();+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [currentProvider, currentModel]);++ async function handleSwitch(entry: ModelEntry) {+ setSwitching(keyOf(entry));try {- const res = await api.chat.models.switch(providerName, model);+ const res = await api.chat.models.switch(entry.name, entry.model);if (!res.ok) return;- // Refresh model list to get new currentconst data = await api.chat.models.list();setModels(data);- setProvider(data.current?.name || providerName);- setModel(data.current?.model || model);+ setProvider(data.current?.name || entry.name);+ setModel(data.current?.model || entry.model);setOpen(false);} catch {// silently fail@@ -60,92 +78,237 @@ export function ModelSwitcher({}}- const displayName =- currentModel.length > 24- ? currentModel.slice(0, 22) + "..."- : currentModel || currentProvider || "Model";+ function startEditing(entry: ModelEntry) {+ setEditing(keyOf(entry));+ setDraft(entry.customLabel ?? entry.label);+ }++ async function saveLabel(entry: ModelEntry, label: string | null) {+ setSaving(true);+ try {+ await api.chat.models.setPrefs(entry.name, entry.model, { label });+ setModels(await api.chat.models.list());+ setEditing(null);+ // The new name may no longer match the search; show the full list.+ setQuery("");+ } catch {+ // keep the editor open so nothing typed is lost+ } finally {+ setSaving(false);+ }+ }++ async function setReasoning(choice: ReasoningChoice) {+ if (!models) return;+ const { name, model } = models.current;+ try {+ await api.chat.models.setPrefs(name, model, { reasoning: choice });+ setModels(await api.chat.models.list());+ setReasoningOpen(false);+ } catch {+ // silently fail+ }+ }++ const groups = useMemo(() => {+ const q = query.trim().toLowerCase();+ const map = new Map<string, ModelEntry[]>();+ for (const p of models?.providers ?? []) {+ if (q && ![p.label, p.model, p.group].some((s) => s.toLowerCase().includes(q))) continue;+ const list = map.get(p.group) ?? [];+ list.push(p);+ map.set(p.group, list);+ }+ // The current model's group first, then local before cloud-hosted ones.+ const rank = (group: string) =>+ group === models?.current.group ? 0 : /cloud/i.test(group) ? 2 : 1;+ return [...map.entries()].sort(([a], [b]) => rank(a) - rank(b));+ }, [models, query]);++ const current = models?.current;+ const displayName = current?.label || currentModel || currentProvider || "Model";+ const reasoningLevels = current?.reasoningLevels ?? [];+ const reasoning = current?.reasoning ?? "auto";+ const isLocalOllama = current?.name === "ollamaLocal" || current?.name === "ollamaCloud";return (- <Popover- open={open}- onOpenChange={(o) => {- setOpen(o);- if (o) loadModels();- }}- >- <PopoverTrigger asChild>- <Button- variant="ghost"- size="sm"- className="h-7 gap-1 px-2 text-xs font-medium text-muted-foreground hover:text-foreground"- >- <span className="max-w-[140px] truncate">{displayName}</span>- <ChevronDown className="h-3 w-3" />- </Button>- </PopoverTrigger>- <PopoverContent align="start" className="w-72 p-2">- <p className="px-2 pb-2 text-xs font-semibold text-muted-foreground uppercase tracking-wider">- Models- </p>-- {loading ? (- <div className="flex items-center justify-center py-4">- <Loader2 className="h-4 w-4 animate-spin text-primary" />+ <div className="flex items-center gap-1">+ <Popover+ open={open}+ onOpenChange={(o) => {+ setOpen(o);+ if (o) loadModels();+ else {+ setEditing(null);+ setQuery("");+ }+ }}+ >+ <PopoverTrigger asChild>+ <Button+ variant="ghost"+ size="sm"+ className="h-7 gap-1 px-2 text-xs font-medium text-muted-foreground hover:text-foreground"+ title={current ? `${current.group} · ${current.model}` : undefined}+ >+ <span className="max-w-[180px] truncate">{displayName}</span>+ <ChevronDown className="h-3 w-3" />+ </Button>+ </PopoverTrigger>+ <PopoverContent align="start" className="w-80 p-2">+ <div className="relative mb-2">+ <Search className="pointer-events-none absolute left-2 top-1/2 h-3.5 w-3.5 -translate-y-1/2 text-muted-foreground" />+ <Input+ value={query}+ onChange={(e) => setQuery(e.target.value)}+ placeholder="Search models..."+ className="h-8 pl-7 text-xs"+ /></div>- ) : models ? (- <div className="flex max-h-[60vh] flex-col gap-0.5 overflow-y-auto">- {models.providers.map((p) => {- const key = `${p.name}::${p.model}`;- const isActive = p.name === currentProvider && p.model === currentModel;- const isSwitching = switching === key;- return (++ {loading && !models ? (+ <div className="flex items-center justify-center py-4">+ <Loader2 className="h-4 w-4 animate-spin text-primary" />+ </div>+ ) : groups.length ? (+ <div className="flex max-h-[60vh] flex-col gap-2 overflow-y-auto">+ {groups.map(([group, entries]) => (+ <div key={group}>+ <p className="px-2 pb-1 text-[10px] font-semibold uppercase tracking-wider text-muted-foreground">+ {group}+ </p>+ <div className="flex flex-col gap-0.5">+ {entries.map((p) => {+ const key = keyOf(p);+ const isActive = p.name === currentProvider && p.model === currentModel;+ const isSwitching = switching === key;++ if (editing === key) {+ return (+ <div key={key} className="flex items-center gap-1 rounded-md bg-muted/60 px-2 py-1.5">+ <Input+ autoFocus+ value={draft}+ maxLength={80}+ onChange={(e) => setDraft(e.target.value)}+ onKeyDown={(e) => {+ if (e.key === "Enter") saveLabel(p, draft);+ if (e.key === "Escape") setEditing(null);+ }}+ className="h-7 text-xs"+ aria-label={`New name for ${p.model}`}+ />+ <Button size="icon" variant="ghost" className="h-7 w-7 shrink-0" disabled={saving}+ onClick={() => saveLabel(p, draft)} title="Save name">+ {saving ? <Loader2 className="h-3.5 w-3.5 animate-spin" /> : <Check className="h-3.5 w-3.5" />}+ </Button>+ {p.customLabel && (+ <Button size="icon" variant="ghost" className="h-7 w-7 shrink-0" disabled={saving}+ onClick={() => saveLabel(p, null)} title={`Reset to "${p.autoLabel}"`}>+ <RotateCcw className="h-3.5 w-3.5" />+ </Button>+ )}+ <Button size="icon" variant="ghost" className="h-7 w-7 shrink-0"+ onClick={() => setEditing(null)} title="Cancel">+ <X className="h-3.5 w-3.5" />+ </Button>+ </div>+ );+ }++ return (+ <div+ key={key}+ className={cn(+ "group flex items-center gap-1 rounded-md transition-colors",+ p.available ? "hover:bg-muted/60" : "opacity-40",+ isActive && "bg-primary/10",+ )}+ >+ <button+ disabled={!p.available || isSwitching}+ onClick={() => {+ if (!isActive && p.available) handleSwitch(p);+ }}+ className="min-w-0 flex-1 px-2 py-1.5 text-left text-sm"+ >+ <div className="flex items-center gap-1.5">+ <span className="truncate font-medium text-foreground">{p.label}</span>+ {isActive && <Check className="h-3.5 w-3.5 shrink-0 text-primary" />}+ {isSwitching && <Loader2 className="h-3.5 w-3.5 shrink-0 animate-spin text-primary" />}+ </div>+ <p className="truncate font-mono text-[10px] text-muted-foreground">{p.model}</p>+ </button>+ <Button+ size="icon"+ variant="ghost"+ className="mr-1 h-7 w-7 shrink-0 opacity-0 transition-opacity focus:opacity-100 group-hover:opacity-100"+ onClick={() => startEditing(p)}+ title="Rename"+ aria-label={`Rename ${p.label}`}+ >+ <Pencil className="h-3.5 w-3.5" />+ </Button>+ </div>+ );+ })}+ </div>+ </div>+ ))}+ </div>+ ) : (+ <p className="px-2 py-4 text-center text-sm text-muted-foreground">+ {query ? "No models match your search" : "No providers found"}+ </p>+ )}+ </PopoverContent>+ </Popover>++ {reasoningLevels.length > 0 && (+ <Popover open={reasoningOpen} onOpenChange={setReasoningOpen}>+ <PopoverTrigger asChild>+ <Button+ variant="ghost"+ size="sm"+ className="h-7 gap-1 px-2 text-xs font-medium text-muted-foreground hover:text-foreground"+ title="How hard the model thinks before answering"+ >+ <Brain className="h-3.5 w-3.5" />+ <span>{REASONING_COPY[reasoning].title}</span>+ <ChevronDown className="h-3 w-3" />+ </Button>+ </PopoverTrigger>+ <PopoverContent align="start" className="w-72 p-2">+ <p className="px-2 pb-2 text-[10px] font-semibold uppercase tracking-wider text-muted-foreground">+ Reasoning · {displayName}+ </p>+ <div className="flex flex-col gap-0.5">+ {(["auto", ...reasoningLevels] as ReasoningChoice[]).map((choice) => (<button- key={key}- disabled={!p.available || isSwitching}- onClick={() => {- if (!isActive && p.available) handleSwitch(p.name, p.model);- }}+ key={choice}+ onClick={() => setReasoning(choice)}className={cn(- "flex items-center gap-2 rounded-md px-2 py-2 text-left text-sm transition-colors",- p.available- ? "hover:bg-muted/60"- : "cursor-not-allowed opacity-40",- isActive && "bg-primary/10"+ "flex items-start gap-2 rounded-md px-2 py-1.5 text-left transition-colors hover:bg-muted/60",+ reasoning === choice && "bg-primary/10",)}><div className="min-w-0 flex-1">- <div className="flex items-center gap-1.5">- <span className="truncate font-medium text-foreground">- {p.model}- </span>- {isActive && (- <Check className="h-3.5 w-3.5 shrink-0 text-primary" />- )}- </div>- <p className="truncate text-xs text-muted-foreground">- {p.name}- </p>+ <div className="text-sm font-medium text-foreground">{REASONING_COPY[choice].title}</div>+ <div className="text-xs text-muted-foreground">{REASONING_COPY[choice].hint}</div></div>- {isSwitching ? (- <Loader2 className="h-3.5 w-3.5 shrink-0 animate-spin text-primary" />- ) : (- <Badge- variant={p.available ? "success" : "secondary"}- className="shrink-0 text-[10px] px-1.5 py-0"- >- {p.available ? "Ready" : "Unavailable"}- </Badge>- )}+ {reasoning === choice && <Check className="mt-0.5 h-3.5 w-3.5 shrink-0 text-primary" />}</button>- );- })}- </div>- ) : (- <p className="px-2 py-4 text-center text-sm text-muted-foreground">- No providers found- </p>- )}- </PopoverContent>- </Popover>+ ))}+ </div>+ {isLocalOllama && (+ <p className="px-2 pt-2 text-[11px] text-muted-foreground">+ Most local models only switch thinking on or off. Light, Balanced and Deep differ+ on models that support levels, like GPT-OSS.+ </p>+ )}+ </PopoverContent>+ </Popover>+ )}+ </div>);}diff --git a/ui/src/lib/api.ts b/ui/src/lib/api.tsindex c2aad6df..c47d965d 100644--- a/ui/src/lib/api.ts+++ b/ui/src/lib/api.ts@@ -122,6 +122,9 @@ export const chat = {provider,model,}),+ /** Rename a model and/or set its reasoning level; null / '' / 'auto' resets. */+ setPrefs: (provider: string, model: string, prefs: { label?: string | null; reasoning?: ReasoningChoice | null }) =>+ post<{ ok: boolean } & ModelDescription>("/api/chat/models/prefs", { provider, model, ...prefs }),},threads: {list: () => get<{ threads: ChatThread[] }>("/api/chat/threads"),@@ -483,9 +486,25 @@ export interface ToolStep {result?: string;}+export type ReasoningLevel = "off" | "low" | "medium" | "high";+export type ReasoningChoice = ReasoningLevel | "auto";++/** Plain-language view of a model (see src/utils/model-prefs.ts). */+export interface ModelDescription {+ /** Custom label if set, else the generated friendly name. */+ label: string;+ autoLabel: string;+ customLabel: string | null;+ /** Heading the model is grouped under, e.g. "ChatGPT". */+ group: string;+ reasoning: ReasoningChoice;+ /** Levels this provider honours; empty means no reasoning control. */+ reasoningLevels: ReasoningLevel[];+}+export interface ModelsResponse {- current: { name: string; model: string };- providers: { name: string; model: string; available: boolean; isCurrent?: boolean }[];+ current: { name: string; model: string } & ModelDescription;+ providers: ({ name: string; model: string; available: boolean; isCurrent?: boolean } & ModelDescription)[];}export interface CodeStatus {