forked from Blendman974/faceclaw
Add OpenAI-compatible assistant provider
This commit is contained in:
1 parent
a5eda29368
commit
77e8a86196
19 files changed
+807
-23
No files matched your search
@@ -4,7 +4,7 @@ import { truncateText, wrapText } from "../../graphics/textwrap";
|
||||
import { ASSISTANT_MODEL_CHOICES, assistantModelLabel, resolveAssistantModel } from "../../assistant/models";
|
||||
import type { AssistantTranscriptEntry } from "../../assistant/session";
|
||||
import type { AssistantConversations, ReasoningLevel } from "../../assistant/conversations";
|
||||
import { anthropicApiKeySetting, assistantBackendSetting, openAiApiKeySetting } from "../../ui/dashboard-settings";
|
||||
import { anthropicApiKeySetting, assistantBackendSetting, assistantOpenAiCompatibleKeySetting, assistantOpenAiCompatibleModelSetting, assistantOpenAiCompatibleUrlSetting, openAiApiKeySetting } from "../../ui/dashboard-settings";
|
||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_PRESS, gestureHints, type InputEvent } from "../../ui/gestures";
|
||||
import type { Layer, LayerContext } from "../../ui/layers";
|
||||
import type { MenuItem } from "../../ui/menu";
|
||||
@@ -102,7 +102,7 @@ class AiChatLayer implements Layer {
|
||||
}
|
||||
case "long-press":
|
||||
if (!this.conversations.current().session?.isTurnActive()) {
|
||||
if (!shell.isAssistantAvailable()) { shell.showAlert(global.isIOS ? "Set an OpenAI or Anthropic API key in Settings." : "Set an API key or download the on-phone model in Settings."); return; }
|
||||
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; }
|
||||
void this.draft.start();
|
||||
}
|
||||
return;
|
||||
@@ -133,7 +133,12 @@ class AiChatLayer implements Layer {
|
||||
onSelect: (pickCtx) => { if (this.conversations.configure(model, record.reasoning)) pickCtx.stack.clearToBase(); },
|
||||
}))) },
|
||||
{ label: `Reasoning: ${record.reasoning}`, disabled: () => {
|
||||
const llm = resolveAssistantModel(record.model, { anthropic: anthropicApiKeySetting.get(), openai: openAiApiKeySetting.get() });
|
||||
const llm = resolveAssistantModel(record.model, {
|
||||
anthropic: anthropicApiKeySetting.get(), openai: openAiApiKeySetting.get(),
|
||||
openAiCompatibleUrl: assistantOpenAiCompatibleUrlSetting.get(),
|
||||
openAiCompatibleKey: assistantOpenAiCompatibleKeySetting.get(),
|
||||
openAiCompatibleModel: assistantOpenAiCompatibleModelSetting.get(),
|
||||
});
|
||||
return external || busy() || !llm?.effort;
|
||||
}, onSelect: (ctx) => submenu(ctx, "Reasoning", (["default", "low", "medium", "high"] as ReasoningLevel[]).map((reasoning) => ({
|
||||
label: `${reasoning === record.reasoning ? "✓ " : ""}${reasoning === "default" ? "Model default" : reasoning}`,
|
||||
|
||||
@@ -9,11 +9,15 @@
|
||||
import { streamAnthropicMessage } from "../../native/anthropic";
|
||||
import { streamLocalQwen } from "../../native/llama";
|
||||
import { streamOpenAiResponse } from "../../native/openai";
|
||||
import { streamOpenAiCompatibleChat } from "../../native/openai-chat";
|
||||
import { resolveAssistantModel } from "../../assistant/models";
|
||||
import { type LlmStreamOptions } from "../../assistant/llm-protocol";
|
||||
import {
|
||||
anthropicApiKeySetting,
|
||||
assistantModelSetting,
|
||||
assistantOpenAiCompatibleKeySetting,
|
||||
assistantOpenAiCompatibleModelSetting,
|
||||
assistantOpenAiCompatibleUrlSetting,
|
||||
openAiApiKeySetting,
|
||||
} from "../../ui/dashboard-settings";
|
||||
import { CALCULATOR_REWRITE_SYSTEM_PROMPT, buildCalculatorRewriteUserMessage } from "../../prompts";
|
||||
@@ -22,6 +26,9 @@ export function rewriteSpokenProblem(spoken: string): Promise<string | null> {
|
||||
const resolved = resolveAssistantModel(assistantModelSetting.get(), {
|
||||
anthropic: anthropicApiKeySetting.get(),
|
||||
openai: openAiApiKeySetting.get(),
|
||||
openAiCompatibleUrl: assistantOpenAiCompatibleUrlSetting.get(),
|
||||
openAiCompatibleKey: assistantOpenAiCompatibleKeySetting.get(),
|
||||
openAiCompatibleModel: assistantOpenAiCompatibleModelSetting.get(),
|
||||
});
|
||||
if (!resolved) return Promise.resolve(null);
|
||||
|
||||
@@ -40,6 +47,9 @@ export function rewriteSpokenProblem(spoken: string): Promise<string | null> {
|
||||
case "openai":
|
||||
streamOpenAiResponse(options);
|
||||
break;
|
||||
case "openai-compatible":
|
||||
streamOpenAiCompatibleChat({ ...options, baseUrl: resolved.baseUrl ?? "" });
|
||||
break;
|
||||
case "local":
|
||||
streamLocalQwen(options);
|
||||
break;
|
||||
|
||||
@@ -28,8 +28,8 @@ const TRAY_ICON_ID = "translate";
|
||||
const NO_PROVIDER_PROBLEM =
|
||||
"Translate needs a transcription provider that understands any language: add an ElevenLabs, OpenAI, or Soniox API key (Settings > API Keys), or a Whisper server URL (Settings > Voice).";
|
||||
const NO_MODEL_PROBLEM = global.isIOS
|
||||
? "Translate needs a language model: add an Anthropic or OpenAI API key (Settings > API Keys)."
|
||||
: "Translate needs a language model: add an Anthropic or OpenAI API key (Settings > API Keys), or download the on-phone model (Settings > Assistant).";
|
||||
? "Translate needs a language model: configure a cloud or OpenAI-compatible model in Settings > Assistant."
|
||||
: "Translate needs a language model: configure one in Settings > Assistant, or download the on-phone model.";
|
||||
|
||||
export type TranslateAppOptions = InProcessAppOptions & {
|
||||
/** Begin (or retarget) continuous mic capture, kept on while the window is open. */
|
||||
|
||||
@@ -6,9 +6,13 @@
|
||||
import { streamAnthropicMessage } from "../../native/anthropic";
|
||||
import { streamLocalQwen } from "../../native/llama";
|
||||
import { streamOpenAiResponse } from "../../native/openai";
|
||||
import { streamOpenAiCompatibleChat } from "../../native/openai-chat";
|
||||
import { resolveAssistantModel, type ResolvedAssistantModel } from "../../assistant/models";
|
||||
import { type LlmStreamHandle, type LlmStreamOptions } from "../../assistant/llm-protocol";
|
||||
import { anthropicApiKeySetting, assistantModelSetting, openAiApiKeySetting } from "../../ui/dashboard-settings";
|
||||
import {
|
||||
anthropicApiKeySetting, assistantModelSetting, assistantOpenAiCompatibleKeySetting,
|
||||
assistantOpenAiCompatibleModelSetting, assistantOpenAiCompatibleUrlSetting, openAiApiKeySetting,
|
||||
} from "../../ui/dashboard-settings";
|
||||
import { TRANSLATE_SYSTEM_PROMPT, buildTranslateUserMessage } from "../../prompts";
|
||||
import { type TranslationTurn } from "./translation-model";
|
||||
|
||||
@@ -23,6 +27,9 @@ export function resolveTranslationModel(): ResolvedAssistantModel | null {
|
||||
return resolveAssistantModel(selection === "hermes" ? "auto" : selection, {
|
||||
anthropic: anthropicApiKeySetting.get(),
|
||||
openai: openAiApiKeySetting.get(),
|
||||
openAiCompatibleUrl: assistantOpenAiCompatibleUrlSetting.get(),
|
||||
openAiCompatibleKey: assistantOpenAiCompatibleKeySetting.get(),
|
||||
openAiCompatibleModel: assistantOpenAiCompatibleModelSetting.get(),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -54,6 +61,8 @@ export function streamTranslation(model: ResolvedAssistantModel, request: Transl
|
||||
switch (model.provider) {
|
||||
case "openai":
|
||||
return streamOpenAiResponse(options);
|
||||
case "openai-compatible":
|
||||
return streamOpenAiCompatibleChat({ ...options, baseUrl: model.baseUrl ?? "" });
|
||||
case "local":
|
||||
return streamLocalQwen(options);
|
||||
default:
|
||||
|
||||
@@ -2,6 +2,7 @@ import { streamAnthropicMessage } from "../native/anthropic";
|
||||
import { streamHermesChat } from "../native/hermes";
|
||||
import { streamLocalQwen } from "../native/llama";
|
||||
import { streamOpenAiResponse } from "../native/openai";
|
||||
import { streamOpenAiCompatibleChat } from "../native/openai-chat";
|
||||
import type {
|
||||
LlmContentBlock,
|
||||
LlmMessage,
|
||||
@@ -143,6 +144,8 @@ export class DirectAssistantBackend {
|
||||
streamHandle =
|
||||
options.provider === "hermes"
|
||||
? streamHermesChat({ ...streamOptions, baseUrl: options.baseUrl ?? "", sessionId: options.sessionId ?? "" })
|
||||
: options.provider === "openai-compatible"
|
||||
? streamOpenAiCompatibleChat({ ...streamOptions, baseUrl: options.baseUrl ?? "" })
|
||||
: options.provider === "openai"
|
||||
? streamOpenAiResponse(streamOptions)
|
||||
: options.provider === "local"
|
||||
|
||||
@@ -15,7 +15,7 @@ export type LlmContentBlock =
|
||||
}
|
||||
| { type: "tool_result"; tool_use_id: string; content: string; is_error?: boolean }
|
||||
/** Opaque state required to continue a provider's stateless conversation. */
|
||||
| { type: "provider_item"; provider: "openai"; item: any };
|
||||
| { type: "provider_item"; provider: "openai" | "openai-compatible"; item: any };
|
||||
|
||||
export type LlmMessage = {
|
||||
role: "user" | "assistant";
|
||||
|
||||
+19
-2
@@ -11,6 +11,7 @@ export const ASSISTANT_MODEL_VALUES = [
|
||||
"sol",
|
||||
"qwen",
|
||||
"hermes",
|
||||
"openai-compatible",
|
||||
] as const;
|
||||
|
||||
export type AssistantModel = (typeof ASSISTANT_MODEL_VALUES)[number];
|
||||
@@ -21,7 +22,7 @@ export function supportedAssistantModel(value: AssistantModel): AssistantModel {
|
||||
return global.isIOS && value === "qwen" ? "auto" : value;
|
||||
}
|
||||
|
||||
export type AssistantProvider = "anthropic" | "openai" | "local" | "hermes";
|
||||
export type AssistantProvider = "anthropic" | "openai" | "local" | "hermes" | "openai-compatible";
|
||||
|
||||
export type AssistantApiKeys = {
|
||||
anthropic: string;
|
||||
@@ -29,6 +30,9 @@ export type AssistantApiKeys = {
|
||||
/** A Hermes Agent API server; only the voice assistant passes these. */
|
||||
hermesUrl?: string;
|
||||
hermesKey?: string;
|
||||
openAiCompatibleUrl?: string;
|
||||
openAiCompatibleKey?: string;
|
||||
openAiCompatibleModel?: string;
|
||||
};
|
||||
|
||||
export type ResolvedAssistantModel = {
|
||||
@@ -37,7 +41,7 @@ export type ResolvedAssistantModel = {
|
||||
model: string;
|
||||
apiKey: string;
|
||||
effort?: "low" | "medium" | "high";
|
||||
/** API root for providers that are not fixed services (Hermes). */
|
||||
/** API root for Hermes or an OpenAI-compatible provider. */
|
||||
baseUrl?: string;
|
||||
};
|
||||
|
||||
@@ -63,6 +67,7 @@ const MODEL_DEFINITIONS: Record<Exclude<AssistantModel, "auto">, ModelDefinition
|
||||
qwen: { label: `${LOCAL_MODEL.label} (on-phone)`, provider: "local", model: LOCAL_MODEL.id },
|
||||
// Your own Hermes Agent (its API server), with its own tools and memory.
|
||||
hermes: { label: "Hermes agent", provider: "hermes", model: "hermes-agent" },
|
||||
"openai-compatible": { label: "OpenAI-compatible", provider: "openai-compatible", model: "custom" },
|
||||
};
|
||||
|
||||
export function assistantModelLabel(value: AssistantModel): string {
|
||||
@@ -106,6 +111,18 @@ export function resolveAssistantModel(
|
||||
if (!baseUrl) return null;
|
||||
return { selection, provider: "hermes", model: definition.model, apiKey: keys.hermesKey?.trim() ?? "", baseUrl };
|
||||
}
|
||||
if (definition.provider === "openai-compatible") {
|
||||
const baseUrl = keys.openAiCompatibleUrl?.trim() ?? "";
|
||||
const model = keys.openAiCompatibleModel?.trim() ?? "";
|
||||
if (!baseUrl || !model) return null;
|
||||
return {
|
||||
selection,
|
||||
provider: definition.provider,
|
||||
model,
|
||||
apiKey: keys.openAiCompatibleKey?.trim() ?? "",
|
||||
baseUrl,
|
||||
};
|
||||
}
|
||||
if (definition.provider === "local") {
|
||||
if (!isLocalModelReady()) return null;
|
||||
return {
|
||||
|
||||
@@ -95,14 +95,20 @@ export class AssistantSession {
|
||||
// original model. Preserve visible conversation when switching engines.
|
||||
if (this.config.kind !== config.kind ||
|
||||
(this.config.kind === "direct" && config.kind === "direct" &&
|
||||
(this.config.llm.model !== config.llm.model || this.config.llm.provider !== config.llm.provider))) {
|
||||
(this.config.llm.model !== config.llm.model || this.config.llm.provider !== config.llm.provider ||
|
||||
(this.config.llm.baseUrl ?? "") !== (config.llm.baseUrl ?? "")))) {
|
||||
this.rebuildTextHistory();
|
||||
}
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
private engine(): string {
|
||||
return this.config.kind === "direct" ? `${this.config.llm.provider}:${this.config.llm.model}` : "external";
|
||||
if (this.config.kind !== "direct") return "external";
|
||||
const llm = this.config.llm;
|
||||
// Keep the legacy engine ID for built-in providers so upgrading does not
|
||||
// discard their opaque history. Custom endpoints need endpoint isolation.
|
||||
const endpoint = llm.provider === "openai-compatible" ? `:${llm.baseUrl ?? ""}` : "";
|
||||
return `${llm.provider}:${llm.model}${endpoint}`;
|
||||
}
|
||||
|
||||
private rebuildTextHistory(): void {
|
||||
|
||||
@@ -0,0 +1,272 @@
|
||||
import type {
|
||||
LlmContentBlock,
|
||||
LlmStreamHandle,
|
||||
LlmStreamOptions,
|
||||
} from "../assistant/llm-protocol";
|
||||
import { openSseRequest, type SseListener } from "./sse";
|
||||
|
||||
/** Streaming client for OpenAI-compatible Chat Completions APIs. */
|
||||
export type OpenAiCompatibleStreamOptions = LlmStreamOptions & {
|
||||
/** Explicit API root, for example https://api.venice.ai/api/v1 or https://host/api. */
|
||||
baseUrl: string;
|
||||
};
|
||||
|
||||
type PendingToolCall = {
|
||||
id: string;
|
||||
name: string;
|
||||
inputJson: string;
|
||||
};
|
||||
|
||||
/** Remove an optional endpoint suffix, but never guess a provider's /v1 or /api prefix. */
|
||||
export function normalizeOpenAiCompatibleUrl(input: string): string {
|
||||
let url = input.trim().replace(/\/+$/, "");
|
||||
url = url.replace(/\/chat\/completions$/i, "").replace(/\/+$/, "");
|
||||
return /^https?:\/\/[^/\s?#@\\]+(?:\/[^\s?#\\]*)?$/i.test(url) ? url : "";
|
||||
}
|
||||
|
||||
export function streamOpenAiCompatibleChat(options: OpenAiCompatibleStreamOptions): LlmStreamHandle {
|
||||
const apiKey = options.apiKey.trim();
|
||||
const baseUrl = normalizeOpenAiCompatibleUrl(options.baseUrl);
|
||||
let request: { cancel(): void } | null = null;
|
||||
let settled = false;
|
||||
let text = "";
|
||||
let finishReason: string | null = null;
|
||||
const toolCalls = new Map<number, PendingToolCall>();
|
||||
const providerMetadata: Record<string, any> = {};
|
||||
|
||||
const fail = (message: string) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
request?.cancel();
|
||||
options.onError(message);
|
||||
};
|
||||
|
||||
const done = () => {
|
||||
if (settled) return;
|
||||
const hasToolCalls = toolCalls.size > 0;
|
||||
// Never execute calls from a truncated stream or a length-limited response.
|
||||
if (hasToolCalls && finishReason !== "tool_calls" && finishReason !== "stop") {
|
||||
fail("OpenAI-compatible stream ended before its tool calls completed");
|
||||
return;
|
||||
}
|
||||
if (!text && !hasToolCalls) {
|
||||
fail("OpenAI-compatible provider ended the turn without a reply");
|
||||
return;
|
||||
}
|
||||
|
||||
const content: LlmContentBlock[] = [];
|
||||
if (text) content.push({ type: "text", text });
|
||||
if (Object.keys(providerMetadata).length) {
|
||||
content.push({ type: "provider_item", provider: "openai-compatible", item: providerMetadata });
|
||||
}
|
||||
for (const index of Array.from(toolCalls.keys()).sort((a, b) => a - b)) {
|
||||
const call = toolCalls.get(index)!;
|
||||
if (!call.name) {
|
||||
fail("OpenAI-compatible provider returned a tool call without a name");
|
||||
return;
|
||||
}
|
||||
let input: any = {};
|
||||
if (call.inputJson.trim()) {
|
||||
try {
|
||||
input = JSON.parse(call.inputJson);
|
||||
} catch {
|
||||
fail("OpenAI-compatible provider returned malformed tool arguments");
|
||||
return;
|
||||
}
|
||||
}
|
||||
content.push({
|
||||
type: "tool_use",
|
||||
id: call.id || `call_${index + 1}`,
|
||||
name: call.name,
|
||||
input,
|
||||
});
|
||||
}
|
||||
|
||||
settled = true;
|
||||
request?.cancel();
|
||||
options.onDone({
|
||||
text,
|
||||
content,
|
||||
stopReason: finishReason === "content_filter" ? "refusal"
|
||||
: hasToolCalls ? "tool_use"
|
||||
: finishReason === "length" ? "max_tokens" : "end_turn",
|
||||
});
|
||||
};
|
||||
|
||||
if (!baseUrl || !options.model.trim()) {
|
||||
setTimeout(() => fail(!baseUrl
|
||||
? "Set a valid OpenAI-compatible API URL"
|
||||
: "Set an OpenAI-compatible model ID"), 0);
|
||||
return { cancel: () => { settled = true; } };
|
||||
}
|
||||
|
||||
const body: any = {
|
||||
model: options.model.trim(),
|
||||
messages: buildChatMessages(options),
|
||||
stream: true,
|
||||
};
|
||||
// Let the provider choose its default unless a one-shot feature supplies a cap.
|
||||
if (options.maxTokens !== undefined) body.max_tokens = options.maxTokens;
|
||||
if (options.tools?.length) {
|
||||
body.tools = options.tools.map((tool) => ({
|
||||
type: "function",
|
||||
function: {
|
||||
name: tool.name,
|
||||
description: tool.description,
|
||||
parameters: tool.input_schema,
|
||||
},
|
||||
}));
|
||||
}
|
||||
// Do not send provider-specific reasoning fields by default. Compatible
|
||||
// servers vary widely; advanced reasoning controls can be added separately.
|
||||
|
||||
const headers: Record<string, string> = { "Content-Type": "application/json" };
|
||||
if (apiKey) headers.Authorization = `Bearer ${apiKey}`;
|
||||
|
||||
const listener: SseListener = {
|
||||
onLine: (line: string) => {
|
||||
if (settled || !line.startsWith("data:")) return;
|
||||
const payload = line.slice(5).trim();
|
||||
if (!payload) return;
|
||||
if (payload === "[DONE]") {
|
||||
if (finishReason || (!toolCalls.size && text)) done();
|
||||
else fail("OpenAI-compatible stream ended before the response completed");
|
||||
return;
|
||||
}
|
||||
let event: any;
|
||||
try {
|
||||
event = JSON.parse(payload);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
if (event?.error) {
|
||||
fail(`OpenAI-compatible: ${String(event.error.message ?? event.error)}`);
|
||||
return;
|
||||
}
|
||||
const choice = event?.choices?.[0];
|
||||
if (!choice) return; // Usage and provider-metadata chunks may have no choices.
|
||||
const delta = choice.delta ?? {};
|
||||
if (typeof delta.content === "string" && delta.content) {
|
||||
text += delta.content;
|
||||
options.onTextDelta?.(delta.content, text);
|
||||
}
|
||||
// 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 (delta.thought_signature !== undefined) providerMetadata.thought_signature = delta.thought_signature;
|
||||
if (Array.isArray(delta.tool_calls)) {
|
||||
delta.tool_calls.forEach((call: any, position: number) => {
|
||||
const index = Number.isInteger(call?.index) ? Number(call.index) : position;
|
||||
const pending = toolCalls.get(index) ?? { id: "", name: "", inputJson: "" };
|
||||
if (call?.id) pending.id = String(call.id);
|
||||
if (call?.function?.name) pending.name = String(call.function.name);
|
||||
if (typeof call?.function?.arguments === "string") pending.inputJson += call.function.arguments;
|
||||
toolCalls.set(index, pending);
|
||||
});
|
||||
}
|
||||
if (choice.finish_reason) finishReason = String(choice.finish_reason);
|
||||
},
|
||||
onHttpError: (code: number, errorBody: string) => fail(describeHttpError(Number(code), String(errorBody))),
|
||||
onComplete: () => {
|
||||
// Some compatible servers close after the finish chunk without [DONE].
|
||||
if (finishReason) done();
|
||||
else fail("OpenAI-compatible stream ended before the response completed");
|
||||
},
|
||||
onFailure: (message: string) => fail(`OpenAI-compatible connection failed: ${String(message)}`),
|
||||
};
|
||||
|
||||
try {
|
||||
request = openSseRequest(`${baseUrl}/chat/completions`, JSON.stringify(body), headers, listener);
|
||||
} catch (error) {
|
||||
setTimeout(() => fail(`OpenAI-compatible request failed: ${String((error as Error)?.message ?? error)}`), 0);
|
||||
return { cancel: () => { settled = true; } };
|
||||
}
|
||||
|
||||
return {
|
||||
cancel: () => {
|
||||
settled = true;
|
||||
try { request?.cancel(); } catch { /* ignore */ }
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** Convert neutral conversation history to Chat Completions messages. */
|
||||
function buildChatMessages(options: OpenAiCompatibleStreamOptions): any[] {
|
||||
const messages: any[] = [];
|
||||
if (options.system) messages.push({ role: "system", content: options.system });
|
||||
|
||||
for (const message of options.messages) {
|
||||
if (typeof message.content === "string") {
|
||||
messages.push({ role: message.role, content: message.content });
|
||||
continue;
|
||||
}
|
||||
|
||||
if (message.role === "assistant") {
|
||||
let text = "";
|
||||
const toolCalls: any[] = [];
|
||||
let providerMetadata: Record<string, any> = {};
|
||||
for (const block of message.content) {
|
||||
if (block.type === "text") text += block.text;
|
||||
else if (block.type === "tool_use") {
|
||||
toolCalls.push({
|
||||
id: block.id,
|
||||
type: "function",
|
||||
function: { name: block.name, arguments: JSON.stringify(block.input ?? {}) },
|
||||
});
|
||||
} else if (block.type === "provider_item" && block.provider === "openai-compatible") {
|
||||
providerMetadata = { ...providerMetadata, ...block.item };
|
||||
}
|
||||
}
|
||||
if (text || toolCalls.length) {
|
||||
messages.push({
|
||||
role: "assistant",
|
||||
content: text || (toolCalls.length ? null : ""),
|
||||
...(toolCalls.length ? { tool_calls: toolCalls } : {}),
|
||||
...providerMetadata,
|
||||
});
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
let pendingText = "";
|
||||
const flushText = () => {
|
||||
if (!pendingText) return;
|
||||
messages.push({ role: "user", content: pendingText });
|
||||
pendingText = "";
|
||||
};
|
||||
for (const block of message.content) {
|
||||
if (block.type === "text") pendingText += block.text;
|
||||
else if (block.type === "tool_result") {
|
||||
flushText();
|
||||
messages.push({
|
||||
role: "tool",
|
||||
tool_call_id: block.tool_use_id,
|
||||
content: block.is_error ? `Error: ${block.content}` : block.content,
|
||||
});
|
||||
}
|
||||
}
|
||||
flushText();
|
||||
}
|
||||
return messages;
|
||||
}
|
||||
|
||||
function describeHttpError(code: number, body: string): string {
|
||||
let detail = "";
|
||||
try {
|
||||
const parsed = JSON.parse(body);
|
||||
detail = String(parsed?.error?.message ?? parsed?.error ?? parsed?.message ?? "");
|
||||
} catch {
|
||||
// Non-JSON error body.
|
||||
}
|
||||
switch (code) {
|
||||
case 401: return "OpenAI-compatible: invalid API key";
|
||||
case 403: return "OpenAI-compatible: API key lacks permission";
|
||||
case 429: return "OpenAI-compatible: rate limited, try again shortly";
|
||||
case 500:
|
||||
case 502:
|
||||
case 503:
|
||||
case 504:
|
||||
return "OpenAI-compatible: service unavailable, try again shortly";
|
||||
default: return `OpenAI-compatible: HTTP ${code}${detail ? ` (${detail})` : ""}`;
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
} from "~/assistant/models";
|
||||
import { isLocalModelReady } from "../native/llama";
|
||||
import { normalizeWhisperServerUrl } from "../native/whisper-server-stt";
|
||||
import { normalizeOpenAiCompatibleUrl } from "../native/openai-chat";
|
||||
import { isValidSpotifyClientId, SPOTIFY_CLIENT_ID_KEY } from "../native/spotify-auth";
|
||||
import { drawRightValueMenuItem, drawToggleMenuItem, MenuItem, openModalMenu } from "./menu";
|
||||
import { ANIMATION_SPEEDS, animationSpeedLabel, scaleAnimationDuration, type AnimationSpeed } from "./animation-speed";
|
||||
@@ -942,7 +943,7 @@ export const assistantBackendSetting = new ConfigSettingEnum<AssistantBackendKin
|
||||
values: global.isIOS ? ["direct"] : ["direct", "external"],
|
||||
formatValue: (value) => assistantBackendLabels[value] ?? value,
|
||||
description:
|
||||
global.isIOS ? "Cloud models called with your OpenAI or Anthropic API key. Add your key in Settings > API Keys." : "Who answers assistant queries: an LLM called from the phone (a cloud API with your key, or the downloaded on-phone model), or your own long-running agent (e.g. OpenClaw) reached through the faceclaw-agent-bridge plugin.",
|
||||
global.isIOS ? "Cloud models called through OpenAI, Anthropic, or your configured OpenAI-compatible API." : "Who answers assistant queries: an LLM called from the phone (a cloud API with your key, or the downloaded on-phone model), or your own long-running agent (e.g. OpenClaw) reached through the faceclaw-agent-bridge plugin.",
|
||||
});
|
||||
|
||||
export const assistantHermesUrlSetting = new ConfigSettingString({
|
||||
@@ -1008,6 +1009,40 @@ export const assistantAllowProactiveSetting = new ConfigSettingBoolean({
|
||||
"Let the external agent use glasses tools outside of a conversation, e.g. showing an alert when a long-running job finishes. Rate-limited; only tools marked proactive-safe are allowed.",
|
||||
});
|
||||
|
||||
export const assistantOpenAiCompatibleUrlSetting = new ConfigSettingString({
|
||||
id: "assistant-openai-compatible-url",
|
||||
label: "Compatible API URL",
|
||||
storageKey: "assistant.openAiCompatibleUrl",
|
||||
defaultValue: "",
|
||||
editorTitle: "OpenAI-compatible API root",
|
||||
glassesEditTitle: "Edit compatible API URL",
|
||||
formatValue: (value) => value || "(not set)",
|
||||
validate: (value) => (!value.trim() || normalizeOpenAiCompatibleUrl(value) ? null : "Enter an http:// or https:// API root."),
|
||||
description: "Explicit API root for the OpenAI-compatible Chat Completions assistant. Venice: https://api.venice.ai/api/v1. Open WebUI: https://your-host/api. The provider's path is not guessed.",
|
||||
});
|
||||
|
||||
export const assistantOpenAiCompatibleKeySetting = new ConfigSettingString({
|
||||
id: "assistant-openai-compatible-key",
|
||||
label: "Compatible API key",
|
||||
storageKey: "assistant.openAiCompatibleKey",
|
||||
defaultValue: "",
|
||||
editorTitle: "OpenAI-compatible API key (optional)",
|
||||
glassesEditTitle: "Edit compatible API key",
|
||||
formatValue: (value) => (value ? `${value.slice(0, 6)}...` : "(none)"),
|
||||
description: "Bearer key sent only to the compatible assistant URL above. Optional for private servers that do not require authentication.",
|
||||
});
|
||||
|
||||
export const assistantOpenAiCompatibleModelSetting = new ConfigSettingString({
|
||||
id: "assistant-openai-compatible-model",
|
||||
label: "Compatible model ID",
|
||||
storageKey: "assistant.openAiCompatibleModel",
|
||||
defaultValue: "",
|
||||
editorTitle: "OpenAI-compatible model ID",
|
||||
glassesEditTitle: "Edit compatible model ID",
|
||||
formatValue: (value) => value || "(not set)",
|
||||
description: "Exact model ID sent to the compatible provider. For glasses tools, choose a model that supports function calling.",
|
||||
});
|
||||
|
||||
export const elevenLabsApiKeySetting = new ConfigSettingString({
|
||||
id: "elevenlabs-api-key",
|
||||
label: "ElevenLabs key",
|
||||
@@ -1112,11 +1147,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;
|
||||
return false;
|
||||
},
|
||||
description:
|
||||
global.isIOS ? "Model used by the voice assistant. Auto prefers Terra with an OpenAI key, then Sonnet with an Anthropic key." : "Model used by the voice assistant. Auto prefers Terra when an OpenAI key is set, then Sonnet when an Anthropic key is set, then the downloaded on-phone model.",
|
||||
global.isIOS ? "Model used by the assistant. OpenAI-compatible uses the URL, model ID, and optional key below. Auto only considers the built-in providers." : "Model used by the assistant. OpenAI-compatible uses the URL, model ID, and optional key below. Auto only considers the built-in providers.",
|
||||
});
|
||||
|
||||
export const mapboxApiKeySetting = new ConfigSettingString({
|
||||
|
||||
@@ -42,6 +42,9 @@ import {
|
||||
assistantBridgePortSetting,
|
||||
assistantBridgeTokenSetting,
|
||||
assistantModelSetting,
|
||||
assistantOpenAiCompatibleKeySetting,
|
||||
assistantOpenAiCompatibleModelSetting,
|
||||
assistantOpenAiCompatibleUrlSetting,
|
||||
assistantSkipConfirmationSetting,
|
||||
batteryDisplayModeSetting,
|
||||
brightnessSetting,
|
||||
@@ -148,6 +151,9 @@ function settingsSections(): SettingsSection[] {
|
||||
enumSettingMenuItem(assistantModelSetting),
|
||||
textSettingMenuItem(assistantHermesUrlSetting),
|
||||
textSettingMenuItem(assistantHermesKeySetting),
|
||||
textSettingMenuItem(assistantOpenAiCompatibleUrlSetting),
|
||||
textSettingMenuItem(assistantOpenAiCompatibleModelSetting),
|
||||
textSettingMenuItem(assistantOpenAiCompatibleKeySetting),
|
||||
...(!global.isIOS ? [localModelMenuItem()] : []),
|
||||
toggleSettingMenuItem(assistantSkipConfirmationSetting),
|
||||
// iOS has no local LLM or agent bridge yet.
|
||||
|
||||
@@ -38,6 +38,9 @@ import {
|
||||
assistantBridgePortSetting,
|
||||
assistantBridgeTokenSetting,
|
||||
assistantModelSetting,
|
||||
assistantOpenAiCompatibleKeySetting,
|
||||
assistantOpenAiCompatibleModelSetting,
|
||||
assistantOpenAiCompatibleUrlSetting,
|
||||
assistantSkipConfirmationSetting,
|
||||
batteryIndicatorSettingsKey,
|
||||
brightnessSetting,
|
||||
@@ -1720,6 +1723,9 @@ class Shell {
|
||||
openai: openAiApiKeySetting.get(),
|
||||
hermesUrl: assistantHermesUrlSetting.get(),
|
||||
hermesKey: assistantHermesKeySetting.get(),
|
||||
openAiCompatibleUrl: assistantOpenAiCompatibleUrlSetting.get(),
|
||||
openAiCompatibleKey: assistantOpenAiCompatibleKeySetting.get(),
|
||||
openAiCompatibleModel: assistantOpenAiCompatibleModelSetting.get(),
|
||||
});
|
||||
if (llm && reasoning !== "default" && llm.effort) llm.effort = reasoning;
|
||||
return llm ? { kind: "direct", llm } : null;
|
||||
@@ -1749,7 +1755,7 @@ class Shell {
|
||||
this.showAlert(
|
||||
assistantBackendSetting.get() === "external"
|
||||
? "Configure the agent bridge host and token in Settings."
|
||||
: global.isIOS ? "Set an OpenAI or Anthropic API key in Settings." : "Set an API key or download the on-phone model in Settings.",
|
||||
: global.isIOS ? "Configure an assistant model in Settings." : "Set an API key or download the on-phone model in Settings.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user