forked from Blendman974/faceclaw
Merge branch 'pr-57'
This commit is contained in:
commit
7f3e3f2305
25 files changed
+1888
-41
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 { streamLocalModel } from "../../native/local-llm";
|
||||
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":
|
||||
streamLocalModel(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 { streamLocalModel } from "../../native/local-llm";
|
||||
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 streamLocalModel(options);
|
||||
default:
|
||||
|
||||
@@ -2,6 +2,7 @@ import { streamAnthropicMessage } from "../native/anthropic";
|
||||
import { streamHermesChat } from "../native/hermes";
|
||||
import { streamLocalModel } from "../native/local-llm";
|
||||
import { streamOpenAiResponse } from "../native/openai";
|
||||
import { streamOpenAiCompatibleChat } from "../native/openai-chat";
|
||||
import type {
|
||||
LlmContentBlock,
|
||||
LlmMessage,
|
||||
@@ -40,6 +41,8 @@ export type DirectTurnOptions = {
|
||||
effort?: "low" | "medium" | "high";
|
||||
/** Hermes: the API root, and the server-side session this conversation maps to. */
|
||||
baseUrl?: string;
|
||||
/** Venice-only web search for this interactive turn. */
|
||||
webSearch?: boolean;
|
||||
sessionId?: string;
|
||||
system: string;
|
||||
/** Owned by the session; this loop appends assistant + tool_result messages. */
|
||||
@@ -85,11 +88,12 @@ export class DirectAssistantBackend {
|
||||
return;
|
||||
}
|
||||
|
||||
const streamOptions: LlmStreamOptions = {
|
||||
const streamOptions: LlmStreamOptions & { webSearch?: boolean } = {
|
||||
apiKey: options.apiKey,
|
||||
model: options.model,
|
||||
system: options.system,
|
||||
effort: options.effort,
|
||||
webSearch: options.webSearch,
|
||||
messages: options.messages,
|
||||
// Hermes runs its own tools; Faceclaw's are not offered to it.
|
||||
tools: options.provider === "hermes" ? [] : options.buildTools(),
|
||||
@@ -143,6 +147,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";
|
||||
|
||||
+25
-2
@@ -1,4 +1,5 @@
|
||||
import { isLocalModelReady, isLocalModelSupported, LOCAL_MODEL } from "../native/local-llm";
|
||||
import { isVeniceApiBaseUrl } from "../native/openai-chat";
|
||||
|
||||
export const ASSISTANT_MODEL_VALUES = [
|
||||
"auto",
|
||||
@@ -11,6 +12,7 @@ export const ASSISTANT_MODEL_VALUES = [
|
||||
"sol",
|
||||
"qwen",
|
||||
"hermes",
|
||||
"openai-compatible",
|
||||
] as const;
|
||||
|
||||
/**
|
||||
@@ -26,7 +28,7 @@ export function supportedAssistantModel(value: AssistantModel): AssistantModel {
|
||||
return value === "qwen" && !isLocalModelSupported() ? "auto" : value;
|
||||
}
|
||||
|
||||
export type AssistantProvider = "anthropic" | "openai" | "local" | "hermes";
|
||||
export type AssistantProvider = "anthropic" | "openai" | "local" | "hermes" | "openai-compatible";
|
||||
|
||||
export type AssistantApiKeys = {
|
||||
anthropic: string;
|
||||
@@ -34,6 +36,11 @@ export type AssistantApiKeys = {
|
||||
/** A Hermes Agent API server; only the voice assistant passes these. */
|
||||
hermesUrl?: string;
|
||||
hermesKey?: string;
|
||||
openAiCompatibleUrl?: string;
|
||||
openAiCompatibleKey?: string;
|
||||
openAiCompatibleModel?: string;
|
||||
/** Venice web search, applied only when the resolved root is Venice. */
|
||||
veniceWebSearch?: boolean;
|
||||
};
|
||||
|
||||
export type ResolvedAssistantModel = {
|
||||
@@ -42,8 +49,10 @@ 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;
|
||||
/** Whether Venice web search is enabled for this interactive assistant turn. */
|
||||
webSearch?: boolean;
|
||||
};
|
||||
|
||||
type ModelDefinition = {
|
||||
@@ -69,6 +78,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 {
|
||||
@@ -112,6 +122,19 @@ 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,
|
||||
webSearch: isVeniceApiBaseUrl(baseUrl) && keys.veniceWebSearch === true,
|
||||
};
|
||||
}
|
||||
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 {
|
||||
@@ -192,6 +198,7 @@ export class AssistantSession {
|
||||
model: llm.model,
|
||||
effort: llm.effort,
|
||||
baseUrl: llm.baseUrl,
|
||||
webSearch: llm.webSearch,
|
||||
sessionId: this.hermesSessionId,
|
||||
system: llm.provider === "hermes" ? HERMES_SYSTEM_PROMPT
|
||||
: isLocal ? ASSISTANT_SYSTEM_PROMPT_BASE : buildAssistantSystemPrompt(ctx),
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
import { fetchTextWithUserAgent, type TextResponse } from "../util/http";
|
||||
import { normalizeOpenAiCompatibleUrl } from "./openai-chat";
|
||||
|
||||
export type DiscoveredCompatibleModel = {
|
||||
/** Exact identifier to send as model in Chat Completions. */
|
||||
id: string;
|
||||
/** Human-friendly catalog name, or id when the server supplies no name. */
|
||||
name: string;
|
||||
/** true/false when advertised; null means the server did not say. */
|
||||
supportsTools: boolean | null;
|
||||
offline: boolean;
|
||||
deprecated: boolean;
|
||||
beta: boolean;
|
||||
};
|
||||
|
||||
type DiscoveryOptions = {
|
||||
baseUrl: string;
|
||||
apiKey: string;
|
||||
/** Bypass the short-lived successful-response cache. */
|
||||
forceRefresh?: boolean;
|
||||
};
|
||||
|
||||
type DiscoveryResponse = Pick<TextResponse, "status" | "text">;
|
||||
type DiscoveryHttp = (url: string, init: {
|
||||
method: string;
|
||||
headers: Record<string, string>;
|
||||
timeoutMs: number;
|
||||
dontFollowRedirects: boolean;
|
||||
}) => Promise<DiscoveryResponse>;
|
||||
type DiscoveryDependencies = {
|
||||
http?: DiscoveryHttp;
|
||||
now?: () => number;
|
||||
};
|
||||
|
||||
type CacheEntry = {
|
||||
apiKey: string;
|
||||
fetchedAt: number;
|
||||
models: DiscoveredCompatibleModel[];
|
||||
};
|
||||
|
||||
export const COMPATIBLE_MODEL_DISCOVERY_TIMEOUT_MS = 10_000;
|
||||
const CACHE_TTL_MS = 60_000;
|
||||
const MAX_MODELS = 500;
|
||||
const MAX_DISCOVERY_BODY_CHARS = 2_000_000;
|
||||
const cache = new Map<string, CacheEntry>();
|
||||
|
||||
/**
|
||||
* Discover models from the configured server's own /models endpoint. No URL
|
||||
* probing, inference calls, disk persistence, or automatic model selection.
|
||||
*/
|
||||
export async function discoverOpenAiCompatibleModels(
|
||||
options: DiscoveryOptions,
|
||||
dependencies: DiscoveryDependencies = {},
|
||||
): Promise<DiscoveredCompatibleModel[]> {
|
||||
const baseUrl = normalizeOpenAiCompatibleUrl(options.baseUrl);
|
||||
if (!baseUrl) throw new Error("Set a valid compatible API URL first");
|
||||
const apiKey = options.apiKey.trim();
|
||||
const now = dependencies.now ?? Date.now;
|
||||
const cached = cache.get(baseUrl);
|
||||
if (!options.forceRefresh && cached && cached.apiKey === apiKey && now() - cached.fetchedAt < CACHE_TTL_MS) {
|
||||
return cached.models;
|
||||
}
|
||||
|
||||
const http = dependencies.http ?? defaultHttp;
|
||||
const headers: Record<string, string> = {};
|
||||
if (apiKey) headers.Authorization = `Bearer ${apiKey}`;
|
||||
let response: DiscoveryResponse;
|
||||
try {
|
||||
response = await http(`${baseUrl}/models`, {
|
||||
method: "GET",
|
||||
headers,
|
||||
timeoutMs: COMPATIBLE_MODEL_DISCOVERY_TIMEOUT_MS,
|
||||
// Do not forward a credential to a redirect target.
|
||||
dontFollowRedirects: true,
|
||||
});
|
||||
} catch {
|
||||
throw new Error("Could not reach the configured server");
|
||||
}
|
||||
if (response.status < 200 || response.status >= 300) {
|
||||
throw new Error(describeHttpError(response.status));
|
||||
}
|
||||
|
||||
let body: any;
|
||||
let rawBody: string;
|
||||
try {
|
||||
rawBody = await response.text();
|
||||
if (rawBody.length > MAX_DISCOVERY_BODY_CHARS) throw new Error("too large");
|
||||
body = JSON.parse(rawBody);
|
||||
} catch {
|
||||
throw new Error("The server returned an invalid model list");
|
||||
}
|
||||
if (!body || !Array.isArray(body.data)) {
|
||||
throw new Error("The server did not return an OpenAI-compatible model list");
|
||||
}
|
||||
|
||||
const models = parseModels(body.data);
|
||||
cache.set(baseUrl, { apiKey, fetchedAt: now(), models });
|
||||
return models;
|
||||
}
|
||||
|
||||
/** Parse OpenAI data[] rows, retaining richer Venice metadata when present. */
|
||||
export function parseDiscoveredCompatibleModels(rows: any[]): DiscoveredCompatibleModel[] {
|
||||
return parseModels(rows);
|
||||
}
|
||||
|
||||
function parseModels(rows: any[]): DiscoveredCompatibleModel[] {
|
||||
const byId = new Map<string, DiscoveredCompatibleModel>();
|
||||
for (const row of rows) {
|
||||
if (!row || typeof row.id !== "string" || !row.id.trim()) continue;
|
||||
const id = row.id.trim();
|
||||
if (byId.has(id)) continue;
|
||||
const spec = row.model_spec && typeof row.model_spec === "object" ? row.model_spec : null;
|
||||
const capabilities = spec?.capabilities && typeof spec.capabilities === "object" ? spec.capabilities : null;
|
||||
const catalogName = typeof spec?.name === "string" ? spec.name.trim() : "";
|
||||
const genericName = typeof row.name === "string" ? row.name.trim() : "";
|
||||
byId.set(id, {
|
||||
id,
|
||||
name: catalogName || genericName || id,
|
||||
supportsTools: typeof capabilities?.supportsFunctionCalling === "boolean"
|
||||
? capabilities.supportsFunctionCalling
|
||||
: null,
|
||||
offline: spec?.offline === true,
|
||||
deprecated: Boolean(spec?.deprecation),
|
||||
beta: spec?.betaModel === true || spec?.beta === true,
|
||||
});
|
||||
if (byId.size >= MAX_MODELS) break;
|
||||
}
|
||||
return Array.from(byId.values()).sort((a, b) => {
|
||||
if (a.offline !== b.offline) return a.offline ? 1 : -1;
|
||||
const aTools = a.supportsTools === true ? 0 : a.supportsTools === null ? 1 : 2;
|
||||
const bTools = b.supportsTools === true ? 0 : b.supportsTools === null ? 1 : 2;
|
||||
if (aTools !== bTools) return aTools - bTools;
|
||||
return (a.name || a.id).localeCompare(b.name || b.id) || a.id.localeCompare(b.id);
|
||||
});
|
||||
}
|
||||
|
||||
async function defaultHttp(url: string, init: {
|
||||
method: string;
|
||||
headers: Record<string, string>;
|
||||
timeoutMs: number;
|
||||
dontFollowRedirects: boolean;
|
||||
}): Promise<DiscoveryResponse> {
|
||||
return fetchTextWithUserAgent(url, init);
|
||||
}
|
||||
|
||||
function describeHttpError(status: number): string {
|
||||
if (status === 401) return "The server rejected the API key (HTTP 401)";
|
||||
if (status === 403) return "The API key cannot list these models (HTTP 403)";
|
||||
if (status === 404) return "Model discovery is not supported at this URL (HTTP 404)";
|
||||
if (status === 429) return "The server is rate limiting requests (HTTP 429)";
|
||||
if (status >= 500) return `The server is unavailable (HTTP ${status})`;
|
||||
if (status >= 300 && status < 400) return `The server redirected the request; use its direct API URL (HTTP ${status})`;
|
||||
return `Model discovery failed (HTTP ${status})`;
|
||||
}
|
||||
|
||||
/** Test hook; does not affect any saved setting. */
|
||||
export function clearCompatibleModelDiscoveryCache(): void {
|
||||
cache.clear();
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
import type {
|
||||
LlmContentBlock,
|
||||
LlmStreamHandle,
|
||||
LlmStreamOptions,
|
||||
} from "../assistant/llm-protocol";
|
||||
import { openSseRequest, type SseListener } from "./sse";
|
||||
|
||||
/** True only for Venice's official API root, never for lookalike hosts. */
|
||||
export function isVeniceApiBaseUrl(input: string): boolean {
|
||||
return normalizeOpenAiCompatibleUrl(input) === "https://api.venice.ai/api/v1";
|
||||
}
|
||||
|
||||
/** 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;
|
||||
/** Venice-only web search; never sent to other compatible providers. */
|
||||
webSearch?: boolean;
|
||||
};
|
||||
|
||||
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,
|
||||
},
|
||||
}));
|
||||
}
|
||||
// Provider-specific parameters must never leak to another endpoint, even
|
||||
// when a saved Venice preference is still enabled.
|
||||
if (options.webSearch === true && isVeniceApiBaseUrl(baseUrl)) {
|
||||
body.venice_parameters = { enable_web_search: "on" };
|
||||
}
|
||||
// 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/local-llm";
|
||||
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 ? "An LLM called from the phone: a cloud model with your OpenAI or Anthropic API key (Settings > API Keys), or Apple Intelligence's on-device model." : "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 ? "An LLM called from the phone: a cloud model with your OpenAI or Anthropic API key (Settings > API Keys) or your configured OpenAI-compatible API, or Apple Intelligence's on-device model." : "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,48 @@ 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 veniceWebSearchSetting = new ConfigSettingBoolean({
|
||||
id: "venice-web-search",
|
||||
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.",
|
||||
});
|
||||
|
||||
export const elevenLabsApiKeySetting = new ConfigSettingString({
|
||||
id: "elevenlabs-api-key",
|
||||
label: "ElevenLabs key",
|
||||
@@ -1112,11 +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;
|
||||
return false;
|
||||
},
|
||||
description:
|
||||
global.isIOS ? "Model used by the voice assistant. Auto prefers Terra with an OpenAI key, then Sonnet with an Anthropic key, then Apple Intelligence's on-device model." : "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. 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.",
|
||||
});
|
||||
|
||||
export const mapboxApiKeySetting = new ConfigSettingString({
|
||||
|
||||
@@ -42,7 +42,11 @@ import {
|
||||
assistantBridgePortSetting,
|
||||
assistantBridgeTokenSetting,
|
||||
assistantModelSetting,
|
||||
assistantOpenAiCompatibleKeySetting,
|
||||
assistantOpenAiCompatibleModelSetting,
|
||||
assistantOpenAiCompatibleUrlSetting,
|
||||
assistantSkipConfirmationSetting,
|
||||
veniceWebSearchSetting,
|
||||
batteryDisplayModeSetting,
|
||||
brightnessSetting,
|
||||
autoBrightnessMinSetting,
|
||||
@@ -94,6 +98,8 @@ import {
|
||||
import { wearBridge } from "../../native/wear-bridge";
|
||||
import { openSettingsSubMenu, SettingsPanelLayer, type SettingsSection } from "./settings-panel";
|
||||
import { uiFontPickerMenuItem } from "../font-picker";
|
||||
import { compatibleModelDiscoveryMenuItem } from "../openai-compatible-model-picker";
|
||||
import { isVeniceApiBaseUrl } from "../../native/openai-chat";
|
||||
|
||||
/** The Settings app's master-detail panel (sections on the left, contents on the right). */
|
||||
export function createSettingsPanelLayer(): SettingsPanelLayer {
|
||||
@@ -142,22 +148,36 @@ function settingsSections(): SettingsSection[] {
|
||||
},
|
||||
{
|
||||
label: "Assistant",
|
||||
items: [
|
||||
// iOS's on-phone model is Apple Intelligence; Android downloads one. Only Android has the bridge.
|
||||
enumSettingMenuItem(assistantBackendSetting),
|
||||
enumSettingMenuItem(assistantModelSetting),
|
||||
textSettingMenuItem(assistantHermesUrlSetting),
|
||||
textSettingMenuItem(assistantHermesKeySetting),
|
||||
global.isIOS ? systemModelMenuItem() : localModelMenuItem(),
|
||||
toggleSettingMenuItem(assistantSkipConfirmationSetting),
|
||||
// iOS has no agent bridge yet.
|
||||
...(!global.isIOS ? [
|
||||
textSettingMenuItem(assistantBridgeHostSetting),
|
||||
textSettingMenuItem(assistantBridgePortSetting),
|
||||
textSettingMenuItem(assistantBridgeTokenSetting),
|
||||
toggleSettingMenuItem(assistantAllowProactiveSetting),
|
||||
] : []),
|
||||
],
|
||||
// Re-read on every paint so the Venice-only row follows URL edits.
|
||||
get items() {
|
||||
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)] : []),
|
||||
global.isIOS ? systemModelMenuItem() : localModelMenuItem(),
|
||||
toggleSettingMenuItem(assistantSkipConfirmationSetting),
|
||||
// iOS has no agent bridge yet.
|
||||
...(!global.isIOS ? [
|
||||
textSettingMenuItem(assistantBridgeHostSetting),
|
||||
textSettingMenuItem(assistantBridgePortSetting),
|
||||
textSettingMenuItem(assistantBridgeTokenSetting),
|
||||
toggleSettingMenuItem(assistantAllowProactiveSetting),
|
||||
] : []),
|
||||
];
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "API Keys",
|
||||
|
||||
@@ -0,0 +1,232 @@
|
||||
import { MenuLayer, type MenuItem } from "./menu";
|
||||
import { EditTextSettingLayer } from "./dashboard-settings";
|
||||
import type { LayerContext } from "./layers";
|
||||
import {
|
||||
discoverOpenAiCompatibleModels,
|
||||
type DiscoveredCompatibleModel,
|
||||
} from "../native/openai-chat-models";
|
||||
|
||||
export type ModelDiscoverySettings = {
|
||||
getBaseUrl(): string;
|
||||
getApiKey(): string;
|
||||
getModel(): string;
|
||||
setModel(model: string): void;
|
||||
};
|
||||
|
||||
type Discover = typeof discoverOpenAiCompatibleModels;
|
||||
type PickerState = "loading" | "models" | "error";
|
||||
|
||||
const DISCOVERY_EXPLANATION =
|
||||
"Discover models queries only this server's /models endpoint. Choose an exact model ID, or keep using manual entry. This does not send an inference request.";
|
||||
|
||||
/** A cancellable, on-demand model picker embedded in Faceclaw's existing menus. */
|
||||
export class OpenAiCompatibleModelPicker extends MenuLayer {
|
||||
private state: PickerState = "loading";
|
||||
private models: DiscoveredCompatibleModel[] = [];
|
||||
private error = "";
|
||||
private showOffline = false;
|
||||
private requestGeneration = 0;
|
||||
private endpointKey = "";
|
||||
private removed = false;
|
||||
private readonly modelIdByRow = new Map<MenuItem, string>();
|
||||
|
||||
constructor(
|
||||
private readonly settings: ModelDiscoverySettings,
|
||||
private readonly modelSetting: ConstructorParameters<typeof EditTextSettingLayer>[0],
|
||||
private readonly discover: Discover = discoverOpenAiCompatibleModels,
|
||||
viewport: { width: number; height: number } = { width: 640, height: 480 },
|
||||
) {
|
||||
super("Discover models", [], discoveryLayout(viewport));
|
||||
}
|
||||
|
||||
open(ctx: LayerContext): void {
|
||||
ctx.stack.push(this);
|
||||
void this.refresh(ctx);
|
||||
}
|
||||
|
||||
private currentEndpoint(): string {
|
||||
return `${this.settings.getBaseUrl().trim()}\n${this.settings.getApiKey().trim()}`;
|
||||
}
|
||||
|
||||
private async refresh(ctx: LayerContext, forceRefresh = false): Promise<void> {
|
||||
const generation = ++this.requestGeneration;
|
||||
const endpointKey = this.currentEndpoint();
|
||||
this.endpointKey = endpointKey;
|
||||
this.state = "loading";
|
||||
this.error = "";
|
||||
this.updateRows();
|
||||
try {
|
||||
const models = await this.discover({
|
||||
baseUrl: this.settings.getBaseUrl(),
|
||||
apiKey: this.settings.getApiKey(),
|
||||
forceRefresh,
|
||||
});
|
||||
if (!this.isActive(generation)) return;
|
||||
if (endpointKey !== this.currentEndpoint()) {
|
||||
this.error = "API URL or key changed; discover again";
|
||||
this.state = "error";
|
||||
} else {
|
||||
this.models = models;
|
||||
this.state = "models";
|
||||
}
|
||||
} catch (error) {
|
||||
if (!this.isActive(generation)) return;
|
||||
this.error = endpointKey !== this.currentEndpoint()
|
||||
? "API URL or key changed; discover again"
|
||||
: error instanceof Error ? error.message : String(error);
|
||||
this.state = "error";
|
||||
}
|
||||
this.updateRows();
|
||||
ctx.actions.requestRender();
|
||||
}
|
||||
|
||||
private isActive(generation: number): boolean {
|
||||
// A temporary overlay may sit above this still-open picker. Removal, not
|
||||
// top-of-stack status, determines whether the pending result is obsolete.
|
||||
return !this.removed && generation === this.requestGeneration;
|
||||
}
|
||||
|
||||
private updateRows(): void {
|
||||
this.modelIdByRow.clear();
|
||||
const rows: MenuItem[] = [];
|
||||
if (this.state === "loading") {
|
||||
rows.push({ label: "Discovering models...", disabled: true, description: DISCOVERY_EXPLANATION, onSelect: () => {} });
|
||||
rows.push({
|
||||
label: "Cancel discovery",
|
||||
description: "Ignore the pending result and return to Assistant settings.",
|
||||
onSelect: (innerCtx) => innerCtx.stack.pop(),
|
||||
});
|
||||
} else if (this.state === "error") {
|
||||
rows.push({
|
||||
label: `Failed: ${truncateModelLabel(this.error || "Discovery failed", 40)}`,
|
||||
disabled: true,
|
||||
description: `${this.error}. Manual model entry is still available.`,
|
||||
onSelect: () => {},
|
||||
});
|
||||
rows.push({
|
||||
label: "Try again",
|
||||
description: "Query this server's /models endpoint again without changing the saved model.",
|
||||
onSelect: (innerCtx) => { void this.refresh(innerCtx, true); },
|
||||
});
|
||||
} else {
|
||||
const saved = this.settings.getModel().trim();
|
||||
const listed = this.models.filter((model) => this.showOffline || !model.offline || model.id === saved);
|
||||
const savedModel = saved ? this.models.find((model) => model.id === saved) : undefined;
|
||||
if (saved && !savedModel) {
|
||||
rows.push(this.modelRow({
|
||||
id: saved, name: saved, supportsTools: null, offline: false, deprecated: false, beta: false,
|
||||
}, "saved; not listed"));
|
||||
}
|
||||
if (!listed.length) {
|
||||
rows.push({
|
||||
label: this.models.length ? "No online models listed" : "No models listed",
|
||||
disabled: true,
|
||||
description: "Keep the saved model or use manual entry.",
|
||||
onSelect: () => {},
|
||||
});
|
||||
} else {
|
||||
rows.push(...listed.map((model) => this.modelRow(model)));
|
||||
}
|
||||
if (this.models.some((model) => model.offline)) {
|
||||
rows.push({
|
||||
label: this.showOffline ? "Hide offline models" : `Show offline (${this.models.filter((model) => model.offline).length})`,
|
||||
description: "Offline models normally fail inference until the provider restores them.",
|
||||
onSelect: (innerCtx) => {
|
||||
this.showOffline = !this.showOffline;
|
||||
this.updateRows();
|
||||
innerCtx.actions.requestRender();
|
||||
},
|
||||
});
|
||||
}
|
||||
rows.push({
|
||||
label: "Refresh models",
|
||||
description: "Fetch a fresh catalog. This never changes the selected model by itself.",
|
||||
onSelect: (innerCtx) => { void this.refresh(innerCtx, true); },
|
||||
});
|
||||
}
|
||||
rows.push({
|
||||
label: "Enter model manually",
|
||||
description: "Use an exact model ID when discovery is unavailable or the desired model is not listed.",
|
||||
onSelect: (innerCtx) => {
|
||||
this.requestGeneration++; // Any late discovery result belongs to the closed picker.
|
||||
void innerCtx.actions.startTextSettingEdit(this.modelSetting);
|
||||
innerCtx.stack.popThrough(this);
|
||||
innerCtx.stack.push(new EditTextSettingLayer(this.modelSetting));
|
||||
},
|
||||
});
|
||||
const current = this.settings.getModel().trim();
|
||||
const selected = Math.max(0, rows.findIndex((row) => this.modelIdByRow.get(row) === current));
|
||||
this.setItems(rows, selected);
|
||||
}
|
||||
|
||||
private modelRow(model: DiscoveredCompatibleModel, extraLabel?: string): MenuItem {
|
||||
const flags: string[] = [];
|
||||
if (model.supportsTools === true) flags.push("tools");
|
||||
else if (model.supportsTools === false) flags.push("no tools");
|
||||
else flags.push("tools unknown");
|
||||
if (model.offline) flags.push("offline");
|
||||
if (model.deprecated) flags.push("deprecated");
|
||||
if (model.beta) flags.push("beta");
|
||||
if (extraLabel) flags.push(extraLabel);
|
||||
const selected = this.settings.getModel().trim() === model.id ? " *" : "";
|
||||
const displayName = model.name === model.id ? model.id : `${model.name} (${model.id})`;
|
||||
const toolFlag = model.supportsTools === true ? "tools" : model.supportsTools === false ? "no tools" : "tools?";
|
||||
const visibleFlags = [toolFlag];
|
||||
if (model.offline) visibleFlags.push("offline");
|
||||
if (model.deprecated) visibleFlags.push("old");
|
||||
if (model.beta) visibleFlags.push("beta");
|
||||
if (extraLabel) visibleFlags.push("saved only");
|
||||
const flagSuffix = ` [${visibleFlags.join("/")}]`;
|
||||
const row: MenuItem = {
|
||||
label: `${truncateModelLabel(displayName, Math.max(12, 44 - flagSuffix.length))}${flagSuffix}${selected}`,
|
||||
description: `${model.name}: ${model.id} — ${flags.join(", ")}. Tool support unknown means the server did not advertise it; test before relying on glasses actions.`,
|
||||
onSelect: (innerCtx) => {
|
||||
// Re-read immediately before saving; a late catalog never overwrites a
|
||||
// selection made against a different endpoint.
|
||||
if (this.settings.getBaseUrl().trim() + "\n" + this.settings.getApiKey().trim() !== this.endpointKey) return;
|
||||
this.settings.setModel(model.id);
|
||||
innerCtx.stack.popThrough(this);
|
||||
},
|
||||
};
|
||||
this.modelIdByRow.set(row, model.id);
|
||||
return row;
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
this.removed = true;
|
||||
this.requestGeneration++; // Invalidate the pending result immediately.
|
||||
}
|
||||
}
|
||||
|
||||
/** Kept separate so tests can verify the normal Faceclaw modal sizing contract. */
|
||||
function discoveryLayout(viewport: { width: number; height: number }): ConstructorParameters<typeof MenuLayer>[2] {
|
||||
return {
|
||||
x: "center",
|
||||
y: 8,
|
||||
width: Math.min(320, viewport.width - 40),
|
||||
minHeight: Math.min(160, viewport.height - 40),
|
||||
maxHeight: Math.min(240, viewport.height - 40),
|
||||
depth: 4,
|
||||
};
|
||||
}
|
||||
|
||||
function truncateModelLabel(value: string, maxLength = 44): string {
|
||||
if (value.length <= maxLength) return value;
|
||||
const tailLength = Math.min(19, Math.max(4, maxLength - 8));
|
||||
const headLength = Math.max(1, maxLength - tailLength - 3);
|
||||
return `${value.slice(0, headLength)}...${value.slice(-tailLength)}`;
|
||||
}
|
||||
|
||||
/** One stable Assistant settings row; discovery state belongs to the pushed picker. */
|
||||
export function compatibleModelDiscoveryMenuItem(
|
||||
settings: ModelDiscoverySettings,
|
||||
modelSetting: ConstructorParameters<typeof EditTextSettingLayer>[0],
|
||||
discover: Discover = discoverOpenAiCompatibleModels,
|
||||
): MenuItem {
|
||||
return {
|
||||
label: "Discover models",
|
||||
description: DISCOVERY_EXPLANATION,
|
||||
disabled: () => !settings.getBaseUrl().trim(),
|
||||
onSelect: (ctx) => new OpenAiCompatibleModelPicker(settings, modelSetting, discover, ctx.stack.getBaseSize()).open(ctx),
|
||||
};
|
||||
}
|
||||
@@ -38,7 +38,11 @@ import {
|
||||
assistantBridgePortSetting,
|
||||
assistantBridgeTokenSetting,
|
||||
assistantModelSetting,
|
||||
assistantOpenAiCompatibleKeySetting,
|
||||
assistantOpenAiCompatibleModelSetting,
|
||||
assistantOpenAiCompatibleUrlSetting,
|
||||
assistantSkipConfirmationSetting,
|
||||
veniceWebSearchSetting,
|
||||
batteryIndicatorSettingsKey,
|
||||
brightnessSetting,
|
||||
onAnySettingChanged,
|
||||
@@ -1720,6 +1724,10 @@ class Shell {
|
||||
openai: openAiApiKeySetting.get(),
|
||||
hermesUrl: assistantHermesUrlSetting.get(),
|
||||
hermesKey: assistantHermesKeySetting.get(),
|
||||
openAiCompatibleUrl: assistantOpenAiCompatibleUrlSetting.get(),
|
||||
openAiCompatibleKey: assistantOpenAiCompatibleKeySetting.get(),
|
||||
openAiCompatibleModel: assistantOpenAiCompatibleModelSetting.get(),
|
||||
veniceWebSearch: veniceWebSearchSetting.get(),
|
||||
});
|
||||
if (llm && reasoning !== "default" && llm.effort) llm.effort = reasoning;
|
||||
return llm ? { kind: "direct", llm } : null;
|
||||
@@ -1749,7 +1757,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;
|
||||
}
|
||||
|
||||
+9
-1
@@ -33,7 +33,13 @@ export interface TextResponse {
|
||||
*/
|
||||
export async function fetchTextWithUserAgent(
|
||||
url: string,
|
||||
init?: { method?: string; headers?: HeadersInit; body?: string },
|
||||
init?: {
|
||||
method?: string;
|
||||
headers?: HeadersInit;
|
||||
body?: string;
|
||||
timeoutMs?: number;
|
||||
dontFollowRedirects?: boolean;
|
||||
},
|
||||
): Promise<TextResponse> {
|
||||
// Required lazily so the Node test harness can load this module (via
|
||||
// fetchWithUserAgent's users) without mocking @nativescript/core. Only the
|
||||
@@ -46,6 +52,8 @@ export async function fetchTextWithUserAgent(
|
||||
method: init?.method ?? "GET",
|
||||
headers: withUserAgent(init?.headers),
|
||||
content: init?.body,
|
||||
timeout: init?.timeoutMs,
|
||||
dontFollowRedirects: init?.dontFollowRedirects,
|
||||
});
|
||||
const status = response.statusCode;
|
||||
let body: string | undefined;
|
||||
|
||||
Reference in new issue
Block a user