From 41ff1d27337d67921a958ba647f9cd2f168a2131 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Sun, 1 Nov 2026 02:20:37 -0800 Subject: [PATCH] Fix reasoning_details accumulation; filter assistant settings to settings relevant to the selected model --- README.md | 24 +++++---- app/apps/ai-chat/ai-chat-app.ts | 2 +- app/native/openai-chat.ts | 48 +++++++++++++++++- app/ui/dashboard-settings.ts | 8 +-- app/ui/dashboard/settings-menus.ts | 34 +++++++------ app/ui/shell/shell.ts | 15 ++++-- ...=> assistant-settings-visibility.test.cjs} | 50 ++++++++++++++++++- tests/openai-compatible-assistant.test.cjs | 26 ++++++++++ 8 files changed, 170 insertions(+), 37 deletions(-) rename tests/{venice-web-search-settings.test.cjs => assistant-settings-visibility.test.cjs} (61%) diff --git a/README.md b/README.md index f2a8e01a..39655e62 100644 --- a/README.md +++ b/README.md @@ -104,16 +104,17 @@ The assistant can call an OpenAI-compatible **Chat Completions** endpoint direct from the phone. This uses the same conversation history and glasses tool loop as the built-in cloud providers; it does not require different glasses firmware. -In **Settings > Assistant**, set: +In **Settings > Assistant**: -1. **Compatible API URL** — the explicit API root. -2. **Compatible model ID** — the exact model identifier accepted by the server. +1. **Assistant model** — choose **OpenAI-compatible**. Auto never selects it. + The settings in steps 2–4 appear only while it is selected. +2. **Compatible API URL** — the explicit API root. 3. **Compatible API key** — optional; when set, it is sent only to the configured compatible endpoint as a Bearer token, not to OpenAI's Whisper service. -4. Optional: choose **Discover models**, then pick a model from the server catalog. - The picker shows tool capability when advertised and hides offline models by +4. **Compatible model ID** — the exact model identifier accepted by the server. + Or choose **Discover models** and pick one from the server catalog. The + picker shows tool capability when advertised and hides offline models by default. Refresh never changes the saved model by itself. -5. **Assistant model** — choose **OpenAI-compatible**. Auto never selects it. Provider examples: @@ -128,11 +129,12 @@ or `/api`. Discovery requests `{API root}/models` only when you choose it; there is no background discovery or automatic model selection. Manual model entry remains available for servers without a compatible model-list endpoint. -For the official Venice URL, **Settings > Assistant** also shows **Venice web -search**. It is off by default and visible only for that URL. When enabled, -supported Venice models may search the web for current information; the search -model must support the feature. Open WebUI and other compatible servers never -receive Venice-specific parameters, including after an endpoint switch. +When **OpenAI-compatible** is the assistant model and its URL is the official +Venice URL, **Settings > Assistant** also shows **Venice web search**. It is off +by default and hidden otherwise. When enabled, supported Venice models may +search the web for current information; the search model must support the +feature. Open WebUI and other compatible servers never receive Venice-specific +parameters, including after an endpoint switch. The same provider can supply Calculator's rewrite fallback and Translate. Actual tool calling depends on the selected model and server. Use HTTPS where diff --git a/app/apps/ai-chat/ai-chat-app.ts b/app/apps/ai-chat/ai-chat-app.ts index 740f617c..2c897fd6 100644 --- a/app/apps/ai-chat/ai-chat-app.ts +++ b/app/apps/ai-chat/ai-chat-app.ts @@ -102,7 +102,7 @@ class AiChatLayer implements Layer { } case "long-press": if (!this.conversations.current().session?.isTurnActive()) { - if (!shell.isAssistantAvailable()) { shell.showAlert(global.isIOS ? "Configure an assistant model in Settings." : "Set an API key or download the on-phone model in Settings."); return; } + if (!shell.isAssistantAvailable()) { shell.showAlert(shell.assistantSetupHint()); return; } void this.draft.start(); } return; diff --git a/app/native/openai-chat.ts b/app/native/openai-chat.ts index 08d061df..745de2f6 100644 --- a/app/native/openai-chat.ts +++ b/app/native/openai-chat.ts @@ -40,6 +40,7 @@ export function streamOpenAiCompatibleChat(options: OpenAiCompatibleStreamOption let finishReason: string | null = null; const toolCalls = new Map(); const providerMetadata: Record = {}; + const reasoningDetails = new ReasoningDetailsAccumulator(); const fail = (message: string) => { if (settled) return; @@ -63,6 +64,8 @@ export function streamOpenAiCompatibleChat(options: OpenAiCompatibleStreamOption const content: LlmContentBlock[] = []; if (text) content.push({ type: "text", text }); + const details = reasoningDetails.entries(); + if (details.length) providerMetadata.reasoning_details = details; if (Object.keys(providerMetadata).length) { content.push({ type: "provider_item", provider: "openai-compatible", item: providerMetadata }); } @@ -164,7 +167,7 @@ export function streamOpenAiCompatibleChat(options: OpenAiCompatibleStreamOption } // Reasoning is not displayed. Preserve provider metadata for replay in // multi-turn tool loops when the service supplies it. - if (delta.reasoning_details !== undefined) providerMetadata.reasoning_details = delta.reasoning_details; + if (Array.isArray(delta.reasoning_details)) reasoningDetails.add(delta.reasoning_details); if (delta.thought_signature !== undefined) providerMetadata.thought_signature = delta.thought_signature; if (Array.isArray(delta.tool_calls)) { delta.tool_calls.forEach((call: any, position: number) => { @@ -202,6 +205,49 @@ export function streamOpenAiCompatibleChat(options: OpenAiCompatibleStreamOption }; } +/** String fields of a reasoning_details entry that stream in pieces. */ +const REASONING_DETAIL_TEXT_FIELDS = ["text", "summary", "data"]; + +/** + * Rebuilds the complete reasoning_details array (OpenRouter's format) from + * streamed deltas. Each delta carries fragments; fragments with the same + * `index` belong to one entry, so their text, summary, and data concatenate + * and their other fields (e.g. a signature that arrives after the text) fill + * in. Entries without an index are kept as sent. The replayed array must + * match what the model produced, so it is reassembled whole rather than + * replaying only the latest chunk. + */ +class ReasoningDetailsAccumulator { + private readonly ordered: Record[] = []; + private readonly byIndex = new Map>(); + + add(fragments: any[]): void { + for (const fragment of fragments) { + if (!fragment || typeof fragment !== "object" || Array.isArray(fragment)) continue; + const index = Number.isInteger(fragment.index) ? Number(fragment.index) : null; + const existing = index === null ? undefined : this.byIndex.get(index); + if (!existing) { + const entry = { ...fragment }; + this.ordered.push(entry); + if (index !== null) this.byIndex.set(index, entry); + continue; + } + for (const [key, value] of Object.entries(fragment)) { + if (value === undefined || value === null) continue; + if (REASONING_DETAIL_TEXT_FIELDS.includes(key) && typeof value === "string" && typeof existing[key] === "string") { + existing[key] += value; + } else if (existing[key] === undefined || existing[key] === null) { + existing[key] = value; + } + } + } + } + + entries(): Record[] { + return this.ordered; + } +} + /** Convert neutral conversation history to Chat Completions messages. */ function buildChatMessages(options: OpenAiCompatibleStreamOptions): any[] { const messages: any[] = []; diff --git a/app/ui/dashboard-settings.ts b/app/ui/dashboard-settings.ts index 49978282..0e94e5af 100644 --- a/app/ui/dashboard-settings.ts +++ b/app/ui/dashboard-settings.ts @@ -1048,7 +1048,7 @@ export const veniceWebSearchSetting = new ConfigSettingBoolean({ label: "Venice web search", storageKey: "assistant.veniceWebSearch", defaultValue: false, - description: "Allow supported Venice models to search the web for current information. Shown only for the official Venice API URL. Search queries are processed by Venice's search service and may incur additional charges.", + description: "Allow supported Venice models to search the web for current information. Shown only when OpenAI-compatible is the assistant model and its URL is the official Venice API. Search queries are processed by Venice's search service and may incur additional charges.", }); export const elevenLabsApiKeySetting = new ConfigSettingString({ @@ -1155,12 +1155,12 @@ export const assistantModelSetting = new ConfigSettingEnum({ if (provider === "anthropic") return anthropicApiKeySetting.get().trim().length === 0; if (provider === "openai") return openAiApiKeySetting.get().trim().length === 0; if (provider === "local") return !isLocalModelReady(); - if (provider === "openai-compatible") return !assistantOpenAiCompatibleUrlSetting.get().trim() || !assistantOpenAiCompatibleModelSetting.get().trim(); - if (provider === "hermes") return assistantHermesUrlSetting.get().trim().length === 0; + // Hermes and OpenAI-compatible stay selectable before they're configured: + // their rows only appear in Settings once one is the selected model. return false; }, description: - global.isIOS ? "Model used by the assistant. Auto prefers Terra with an OpenAI key, then Sonnet with an Anthropic key, then Apple Intelligence's on-device model. OpenAI-compatible uses the URL, model ID, and optional key below; Auto never picks it." : "Model used by the assistant. Auto prefers Terra when an OpenAI key is set, then Sonnet when an Anthropic key is set, then the downloaded on-phone model. OpenAI-compatible uses the URL, model ID, and optional key below; Auto never picks it.", + global.isIOS ? "Model used by the assistant. Auto prefers Terra with an OpenAI key, then Sonnet with an Anthropic key, then Apple Intelligence's on-device model; it never picks OpenAI-compatible or Hermes agent. Choosing either of those shows its settings below." : "Model used by the assistant. Auto prefers Terra when an OpenAI key is set, then Sonnet when an Anthropic key is set, then the downloaded on-phone model; it never picks OpenAI-compatible or Hermes agent. Choosing either of those shows its settings below.", }); export const mapboxApiKeySetting = new ConfigSettingString({ diff --git a/app/ui/dashboard/settings-menus.ts b/app/ui/dashboard/settings-menus.ts index b2f13fed..d75edd82 100644 --- a/app/ui/dashboard/settings-menus.ts +++ b/app/ui/dashboard/settings-menus.ts @@ -148,25 +148,31 @@ function settingsSections(): SettingsSection[] { }, { label: "Assistant", - // Re-read on every paint so the Venice-only row follows URL edits. + // Re-read on every paint: provider-specific rows follow the selected + // model (and, for Venice, the compatible URL). get items() { + const selectedModel = assistantModelSetting.get(); return [ // iOS's on-phone model is Apple Intelligence; Android downloads one. Only Android has the bridge. enumSettingMenuItem(assistantBackendSetting), enumSettingMenuItem(assistantModelSetting), - textSettingMenuItem(assistantHermesUrlSetting), - textSettingMenuItem(assistantHermesKeySetting), - textSettingMenuItem(assistantOpenAiCompatibleUrlSetting), - textSettingMenuItem(assistantOpenAiCompatibleKeySetting), - compatibleModelDiscoveryMenuItem({ - getBaseUrl: () => assistantOpenAiCompatibleUrlSetting.get(), - getApiKey: () => assistantOpenAiCompatibleKeySetting.get(), - getModel: () => assistantOpenAiCompatibleModelSetting.get(), - setModel: (model) => assistantOpenAiCompatibleModelSetting.set(model), - }, assistantOpenAiCompatibleModelSetting), - textSettingMenuItem(assistantOpenAiCompatibleModelSetting), - ...(isVeniceApiBaseUrl(assistantOpenAiCompatibleUrlSetting.get()) - ? [toggleSettingMenuItem(veniceWebSearchSetting)] : []), + ...(selectedModel === "hermes" ? [ + textSettingMenuItem(assistantHermesUrlSetting), + textSettingMenuItem(assistantHermesKeySetting), + ] : []), + ...(selectedModel === "openai-compatible" ? [ + textSettingMenuItem(assistantOpenAiCompatibleUrlSetting), + textSettingMenuItem(assistantOpenAiCompatibleKeySetting), + compatibleModelDiscoveryMenuItem({ + getBaseUrl: () => assistantOpenAiCompatibleUrlSetting.get(), + getApiKey: () => assistantOpenAiCompatibleKeySetting.get(), + getModel: () => assistantOpenAiCompatibleModelSetting.get(), + setModel: (model) => assistantOpenAiCompatibleModelSetting.set(model), + }, assistantOpenAiCompatibleModelSetting), + textSettingMenuItem(assistantOpenAiCompatibleModelSetting), + ...(isVeniceApiBaseUrl(assistantOpenAiCompatibleUrlSetting.get()) + ? [toggleSettingMenuItem(veniceWebSearchSetting)] : []), + ] : []), global.isIOS ? systemModelMenuItem() : localModelMenuItem(), toggleSettingMenuItem(assistantSkipConfirmationSetting), // iOS has no agent bridge yet. diff --git a/app/ui/shell/shell.ts b/app/ui/shell/shell.ts index bfc04cfd..10338fab 100644 --- a/app/ui/shell/shell.ts +++ b/app/ui/shell/shell.ts @@ -1670,6 +1670,15 @@ class Shell { return this.resolveAssistantConfiguration(current.model, current.reasoning) !== null; } + /** What to set up when isAssistantAvailable() is false. */ + assistantSetupHint(): string { + if (assistantBackendSetting.get() === "external") return "Configure the agent bridge host and token in Settings."; + const model = this.getAssistantConversations().current().model; + if (model === "hermes") return "Set the Hermes URL in Settings > Assistant."; + if (model === "openai-compatible") return "Set the compatible API URL and model ID in Settings > Assistant."; + return global.isIOS ? "Configure an assistant model in Settings." : "Set an API key or download the on-phone model in Settings."; + } + /** Observe assistant turns (see AssistantActivityEvent). */ onAssistantActivity(listener: (event: AssistantActivityEvent) => void): () => void { this.assistantActivityListeners.add(listener); @@ -1754,11 +1763,7 @@ class Shell { if (!text) return; const session = this.ensureAssistantSession(); if (!session) { - this.showAlert( - assistantBackendSetting.get() === "external" - ? "Configure the agent bridge host and token in Settings." - : global.isIOS ? "Configure an assistant model in Settings." : "Set an API key or download the on-phone model in Settings.", - ); + this.showAlert(this.assistantSetupHint()); return; } if (session.isTurnActive()) { diff --git a/tests/venice-web-search-settings.test.cjs b/tests/assistant-settings-visibility.test.cjs similarity index 61% rename from tests/venice-web-search-settings.test.cjs rename to tests/assistant-settings-visibility.test.cjs index b5f348ed..43e96713 100644 --- a/tests/venice-web-search-settings.test.cjs +++ b/tests/assistant-settings-visibility.test.cjs @@ -2,8 +2,9 @@ const test = require('node:test'); const assert = require('node:assert/strict'); const { loader } = require('./helpers/load-typescript.cjs'); -function settingsEnv(initialUrl = '') { +function settingsEnv(initialUrl = '', initialModel = 'openai-compatible') { let url = initialUrl; + let model = initialModel; let webSearch = false; const dummySetting = { id: 'setting', label: 'Setting', get: () => '', set() {}, displayValue: () => '', @@ -12,6 +13,10 @@ function settingsEnv(initialUrl = '') { id: 'compatible-url', label: 'Compatible API URL', get: () => url, set: value => { url = value; }, displayValue: () => url, }; + const modelSetting = { + id: 'assistant-model', label: 'Assistant model', get: () => model, set: value => { model = value; }, + displayValue: () => model, + }; const searchSetting = { id: 'venice-web-search', label: 'Venice web search', get: () => webSearch, set: value => { webSearch = value; }, toggle: () => { webSearch = !webSearch; }, @@ -19,7 +24,12 @@ function settingsEnv(initialUrl = '') { }; const row = setting => ({ label: setting.label, description: setting.description, onSelect() {} }); const dashboardSettings = new Proxy({ + assistantModelSetting: modelSetting, assistantOpenAiCompatibleUrlSetting: urlSetting, + assistantOpenAiCompatibleKeySetting: { ...dummySetting, id: 'compatible-key', label: 'Compatible API key' }, + assistantOpenAiCompatibleModelSetting: { ...dummySetting, id: 'compatible-model', label: 'Compatible model ID' }, + assistantHermesUrlSetting: { ...dummySetting, id: 'assistant-hermes-url', label: 'Hermes URL' }, + assistantHermesKeySetting: { ...dummySetting, id: 'assistant-hermes-key', label: 'Hermes key' }, veniceWebSearchSetting: searchSetting, enumSettingMenuItem: row, textSettingMenuItem: row, @@ -75,6 +85,7 @@ function settingsEnv(initialUrl = '') { section, labels: () => section.items.map(item => item.label), setUrl: value => { url = value; }, + setModel: value => { model = value; }, searchSetting, }; } @@ -100,3 +111,40 @@ test('Venice web search row follows the current URL and never appears for other env.setUrl('http://api.venice.ai/api/v1'); assert.equal(env.labels().includes('Venice web search'), false); }); + +test('Venice web search row also requires the OpenAI-compatible model to be selected', () => { + const env = settingsEnv('https://api.venice.ai/api/v1', 'sonnet'); + assert.equal(env.labels().includes('Venice web search'), false); + env.setModel('hermes'); + assert.equal(env.labels().includes('Venice web search'), false); + env.setModel('openai-compatible'); + assert.equal(env.labels().includes('Venice web search'), true); +}); + +test('Hermes URL and key rows appear only while Hermes agent is the selected model', () => { + const env = settingsEnv('', 'auto'); + // Labels come from the module's VM realm; compare by value. + const hermesRows = () => JSON.stringify(env.labels().filter(label => label.startsWith('Hermes'))); + assert.equal(hermesRows(), '[]'); + env.setModel('openai-compatible'); + assert.equal(hermesRows(), '[]'); + env.setModel('hermes'); + assert.equal(hermesRows(), JSON.stringify(['Hermes URL', 'Hermes key'])); + const labels = env.labels(); + assert.equal(labels.indexOf('Hermes URL'), labels.indexOf('Assistant model') + 1, 'Hermes rows follow the model row'); +}); + +test('OpenAI-compatible rows appear only while OpenAI-compatible is the selected model', () => { + const env = settingsEnv('https://api.venice.ai/api/v1', 'auto'); + const compatibleRows = () => JSON.stringify(env.labels().filter(label => + label.startsWith('Compatible') || label === 'Discover models' || label === 'Venice web search')); + assert.equal(compatibleRows(), '[]'); + env.setModel('hermes'); + assert.equal(compatibleRows(), '[]'); + env.setModel('openai-compatible'); + assert.equal(compatibleRows(), JSON.stringify([ + 'Compatible API URL', 'Compatible API key', 'Discover models', 'Compatible model ID', 'Venice web search', + ])); + const labels = env.labels(); + assert.equal(labels.indexOf('Compatible API URL'), labels.indexOf('Assistant model') + 1, 'compatible rows follow the model row'); +}); diff --git a/tests/openai-compatible-assistant.test.cjs b/tests/openai-compatible-assistant.test.cjs index c59d7459..795f5c56 100644 --- a/tests/openai-compatible-assistant.test.cjs +++ b/tests/openai-compatible-assistant.test.cjs @@ -189,6 +189,32 @@ test('streamed tool calls execute through Faceclaw, results continue the turn, a assert.equal(h.requests[2].cancelled, true); }); +test('reasoning_details streamed in fragments are reassembled by index before replay', async () => { + const h = assistant(); + h.send('Set a one-minute timer'); + const first = h.requests[0]; + h.chunk(first, { reasoning_details: [ + { type: 'reasoning.text', text: 'Need a ', signature: null, format: 'anthropic-claude-v1', index: 0 }, + ] }); + h.chunk(first, { reasoning_details: [{ type: 'reasoning.text', text: 'timer.', index: 0 }] }); + h.chunk(first, { reasoning_details: [ + { type: 'reasoning.text', signature: 'sig-after-text', index: 0 }, + { type: 'reasoning.encrypted', data: 'abc', index: 1 }, + ] }); + h.chunk(first, { reasoning_details: [{ type: 'reasoning.encrypted', data: 'def', index: 1 }] }); + h.chunk(first, { tool_calls: [{ index: 0, id: 'call-1', function: { name: 'timer_set', arguments: '{"seconds":60}' } }] }); + h.finish(first, 'tool_calls'); + await flush(); + const assistantMessage = h.requests[1].body.messages.find(m => m.role === 'assistant' && m.tool_calls); + assert.deepEqual(assistantMessage.reasoning_details, [ + { type: 'reasoning.text', text: 'Need a timer.', signature: 'sig-after-text', format: 'anthropic-claude-v1', index: 0 }, + { type: 'reasoning.encrypted', data: 'abcdef', index: 1 }, + ]); + h.chunk(h.requests[1], { content: 'Timer set.' }); + h.finish(h.requests[1]); + assert.deepEqual(h.errors, []); +}); + test('changing the API endpoint preserves visible history but drops provider-specific state', () => { const h = assistant(); const original = h.session.config;