Fix reasoning_details accumulation; filter assistant settings to settings relevant to the selected model

This commit is contained in:
James Babcock committed 2026-11-01 02:20:37 -08:00
1 parent 571c4c84df
commit 41ff1d2733
8 files changed
+170 -37

No files matched your search

+13 -11
View File
@@ -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 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. 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. 1. **Assistant model** — choose **OpenAI-compatible**. Auto never selects it.
2. **Compatible model ID** — the exact model identifier accepted by the server. 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 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. 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. 4. **Compatible model ID** — the exact model identifier accepted by the server.
The picker shows tool capability when advertised and hides offline models by 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. default. Refresh never changes the saved model by itself.
5. **Assistant model** — choose **OpenAI-compatible**. Auto never selects it.
Provider examples: 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 is no background discovery or automatic model selection. Manual model entry
remains available for servers without a compatible model-list endpoint. remains available for servers without a compatible model-list endpoint.
For the official Venice URL, **Settings > Assistant** also shows **Venice web When **OpenAI-compatible** is the assistant model and its URL is the official
search**. It is off by default and visible only for that URL. When enabled, Venice URL, **Settings > Assistant** also shows **Venice web search**. It is off
supported Venice models may search the web for current information; the search by default and hidden otherwise. When enabled, supported Venice models may
model must support the feature. Open WebUI and other compatible servers never search the web for current information; the search model must support the
receive Venice-specific parameters, including after an endpoint switch. 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. The same provider can supply Calculator's rewrite fallback and Translate.
Actual tool calling depends on the selected model and server. Use HTTPS where Actual tool calling depends on the selected model and server. Use HTTPS where
+1 -1
View File
@@ -102,7 +102,7 @@ class AiChatLayer implements Layer {
} }
case "long-press": case "long-press":
if (!this.conversations.current().session?.isTurnActive()) { 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(); void this.draft.start();
} }
return; return;
+47 -1
View File
@@ -40,6 +40,7 @@ export function streamOpenAiCompatibleChat(options: OpenAiCompatibleStreamOption
let finishReason: string | null = null; let finishReason: string | null = null;
const toolCalls = new Map<number, PendingToolCall>(); const toolCalls = new Map<number, PendingToolCall>();
const providerMetadata: Record<string, any> = {}; const providerMetadata: Record<string, any> = {};
const reasoningDetails = new ReasoningDetailsAccumulator();
const fail = (message: string) => { const fail = (message: string) => {
if (settled) return; if (settled) return;
@@ -63,6 +64,8 @@ export function streamOpenAiCompatibleChat(options: OpenAiCompatibleStreamOption
const content: LlmContentBlock[] = []; const content: LlmContentBlock[] = [];
if (text) content.push({ type: "text", text }); if (text) content.push({ type: "text", text });
const details = reasoningDetails.entries();
if (details.length) providerMetadata.reasoning_details = details;
if (Object.keys(providerMetadata).length) { if (Object.keys(providerMetadata).length) {
content.push({ type: "provider_item", provider: "openai-compatible", item: providerMetadata }); 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 // Reasoning is not displayed. Preserve provider metadata for replay in
// multi-turn tool loops when the service supplies it. // 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 (delta.thought_signature !== undefined) providerMetadata.thought_signature = delta.thought_signature;
if (Array.isArray(delta.tool_calls)) { if (Array.isArray(delta.tool_calls)) {
delta.tool_calls.forEach((call: any, position: number) => { 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<string, any>[] = [];
private readonly byIndex = new Map<number, Record<string, any>>();
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<string, any>[] {
return this.ordered;
}
}
/** Convert neutral conversation history to Chat Completions messages. */ /** Convert neutral conversation history to Chat Completions messages. */
function buildChatMessages(options: OpenAiCompatibleStreamOptions): any[] { function buildChatMessages(options: OpenAiCompatibleStreamOptions): any[] {
const messages: any[] = []; const messages: any[] = [];
+4 -4
View File
@@ -1048,7 +1048,7 @@ export const veniceWebSearchSetting = new ConfigSettingBoolean({
label: "Venice web search", label: "Venice web search",
storageKey: "assistant.veniceWebSearch", storageKey: "assistant.veniceWebSearch",
defaultValue: false, 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({ export const elevenLabsApiKeySetting = new ConfigSettingString({
@@ -1155,12 +1155,12 @@ export const assistantModelSetting = new ConfigSettingEnum<AssistantModel>({
if (provider === "anthropic") return anthropicApiKeySetting.get().trim().length === 0; if (provider === "anthropic") return anthropicApiKeySetting.get().trim().length === 0;
if (provider === "openai") return openAiApiKeySetting.get().trim().length === 0; if (provider === "openai") return openAiApiKeySetting.get().trim().length === 0;
if (provider === "local") return !isLocalModelReady(); if (provider === "local") return !isLocalModelReady();
if (provider === "openai-compatible") return !assistantOpenAiCompatibleUrlSetting.get().trim() || !assistantOpenAiCompatibleModelSetting.get().trim(); // Hermes and OpenAI-compatible stay selectable before they're configured:
if (provider === "hermes") return assistantHermesUrlSetting.get().trim().length === 0; // their rows only appear in Settings once one is the selected model.
return false; return false;
}, },
description: 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({ export const mapboxApiKeySetting = new ConfigSettingString({
+20 -14
View File
@@ -148,25 +148,31 @@ function settingsSections(): SettingsSection[] {
}, },
{ {
label: "Assistant", 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() { get items() {
const selectedModel = assistantModelSetting.get();
return [ return [
// iOS's on-phone model is Apple Intelligence; Android downloads one. Only Android has the bridge. // iOS's on-phone model is Apple Intelligence; Android downloads one. Only Android has the bridge.
enumSettingMenuItem(assistantBackendSetting), enumSettingMenuItem(assistantBackendSetting),
enumSettingMenuItem(assistantModelSetting), enumSettingMenuItem(assistantModelSetting),
textSettingMenuItem(assistantHermesUrlSetting), ...(selectedModel === "hermes" ? [
textSettingMenuItem(assistantHermesKeySetting), textSettingMenuItem(assistantHermesUrlSetting),
textSettingMenuItem(assistantOpenAiCompatibleUrlSetting), textSettingMenuItem(assistantHermesKeySetting),
textSettingMenuItem(assistantOpenAiCompatibleKeySetting), ] : []),
compatibleModelDiscoveryMenuItem({ ...(selectedModel === "openai-compatible" ? [
getBaseUrl: () => assistantOpenAiCompatibleUrlSetting.get(), textSettingMenuItem(assistantOpenAiCompatibleUrlSetting),
getApiKey: () => assistantOpenAiCompatibleKeySetting.get(), textSettingMenuItem(assistantOpenAiCompatibleKeySetting),
getModel: () => assistantOpenAiCompatibleModelSetting.get(), compatibleModelDiscoveryMenuItem({
setModel: (model) => assistantOpenAiCompatibleModelSetting.set(model), getBaseUrl: () => assistantOpenAiCompatibleUrlSetting.get(),
}, assistantOpenAiCompatibleModelSetting), getApiKey: () => assistantOpenAiCompatibleKeySetting.get(),
textSettingMenuItem(assistantOpenAiCompatibleModelSetting), getModel: () => assistantOpenAiCompatibleModelSetting.get(),
...(isVeniceApiBaseUrl(assistantOpenAiCompatibleUrlSetting.get()) setModel: (model) => assistantOpenAiCompatibleModelSetting.set(model),
? [toggleSettingMenuItem(veniceWebSearchSetting)] : []), }, assistantOpenAiCompatibleModelSetting),
textSettingMenuItem(assistantOpenAiCompatibleModelSetting),
...(isVeniceApiBaseUrl(assistantOpenAiCompatibleUrlSetting.get())
? [toggleSettingMenuItem(veniceWebSearchSetting)] : []),
] : []),
global.isIOS ? systemModelMenuItem() : localModelMenuItem(), global.isIOS ? systemModelMenuItem() : localModelMenuItem(),
toggleSettingMenuItem(assistantSkipConfirmationSetting), toggleSettingMenuItem(assistantSkipConfirmationSetting),
// iOS has no agent bridge yet. // iOS has no agent bridge yet.
+10 -5
View File
@@ -1670,6 +1670,15 @@ class Shell {
return this.resolveAssistantConfiguration(current.model, current.reasoning) !== null; 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). */ /** Observe assistant turns (see AssistantActivityEvent). */
onAssistantActivity(listener: (event: AssistantActivityEvent) => void): () => void { onAssistantActivity(listener: (event: AssistantActivityEvent) => void): () => void {
this.assistantActivityListeners.add(listener); this.assistantActivityListeners.add(listener);
@@ -1754,11 +1763,7 @@ class Shell {
if (!text) return; if (!text) return;
const session = this.ensureAssistantSession(); const session = this.ensureAssistantSession();
if (!session) { if (!session) {
this.showAlert( this.showAlert(this.assistantSetupHint());
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.",
);
return; return;
} }
if (session.isTurnActive()) { if (session.isTurnActive()) {
@@ -2,8 +2,9 @@ const test = require('node:test');
const assert = require('node:assert/strict'); const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs'); const { loader } = require('./helpers/load-typescript.cjs');
function settingsEnv(initialUrl = '') { function settingsEnv(initialUrl = '', initialModel = 'openai-compatible') {
let url = initialUrl; let url = initialUrl;
let model = initialModel;
let webSearch = false; let webSearch = false;
const dummySetting = { const dummySetting = {
id: 'setting', label: 'Setting', get: () => '', set() {}, displayValue: () => '', 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; }, id: 'compatible-url', label: 'Compatible API URL', get: () => url, set: value => { url = value; },
displayValue: () => url, displayValue: () => url,
}; };
const modelSetting = {
id: 'assistant-model', label: 'Assistant model', get: () => model, set: value => { model = value; },
displayValue: () => model,
};
const searchSetting = { const searchSetting = {
id: 'venice-web-search', label: 'Venice web search', get: () => webSearch, id: 'venice-web-search', label: 'Venice web search', get: () => webSearch,
set: value => { webSearch = value; }, toggle: () => { webSearch = !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 row = setting => ({ label: setting.label, description: setting.description, onSelect() {} });
const dashboardSettings = new Proxy({ const dashboardSettings = new Proxy({
assistantModelSetting: modelSetting,
assistantOpenAiCompatibleUrlSetting: urlSetting, 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, veniceWebSearchSetting: searchSetting,
enumSettingMenuItem: row, enumSettingMenuItem: row,
textSettingMenuItem: row, textSettingMenuItem: row,
@@ -75,6 +85,7 @@ function settingsEnv(initialUrl = '') {
section, section,
labels: () => section.items.map(item => item.label), labels: () => section.items.map(item => item.label),
setUrl: value => { url = value; }, setUrl: value => { url = value; },
setModel: value => { model = value; },
searchSetting, 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'); env.setUrl('http://api.venice.ai/api/v1');
assert.equal(env.labels().includes('Venice web search'), false); 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');
});
@@ -189,6 +189,32 @@ test('streamed tool calls execute through Faceclaw, results continue the turn, a
assert.equal(h.requests[2].cancelled, true); 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', () => { test('changing the API endpoint preserves visible history but drops provider-specific state', () => {
const h = assistant(); const h = assistant();
const original = h.session.config; const original = h.session.config;