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

+1 -1
View File
@@ -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;
+47 -1
View File
@@ -40,6 +40,7 @@ export function streamOpenAiCompatibleChat(options: OpenAiCompatibleStreamOption
let finishReason: string | null = null;
const toolCalls = new Map<number, PendingToolCall>();
const providerMetadata: Record<string, any> = {};
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<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. */
function buildChatMessages(options: OpenAiCompatibleStreamOptions): any[] {
const messages: any[] = [];
+4 -4
View File
@@ -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<AssistantModel>({
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({
+20 -14
View File
@@ -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.
+10 -5
View File
@@ -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()) {