Add compatible assistant model discovery

This commit is contained in:
reidellawfirm committed 2026-10-31 16:01:25 -05:00
1 parent 77e8a86196
commit cc8dc544be
9 files changed
+880 -6

No files matched your search

+3 -1
View File
@@ -131,7 +131,9 @@ assistant system prompt, conversation history, available tool definitions, and
tool results needed to answer. Its URL, model ID, and optional API key are
stored on the phone. Faceclaw does not operate that server and cannot make
representations about its logging or retention; for a private Open WebUI
server, the server's operator controls those policies.
server, the server's operator controls those policies. If you choose Discover
models, Faceclaw also requests that server's model catalog using the same
configured URL and optional API key.
AI conversation histories and memory files may be stored on your phone.
Faceclaw does not transmit voice-assistant prompts or responses to any party
+7 -2
View File
@@ -109,7 +109,10 @@ In **Settings > Assistant**, set:
2. **Compatible model ID** — the exact model identifier accepted by the server.
3. **Compatible API key** — optional; when set, it is sent only to the configured
compatible endpoint as a Bearer token, not to OpenAI's Whisper service.
4. **Assistant model** — choose **OpenAI-compatible**. Auto never selects it.
4. Optional: choose **Discover models**, then pick a model from the server catalog.
The picker shows tool capability when advertised and hides offline models by
default. Refresh never changes the saved model by itself.
5. **Assistant model** — choose **OpenAI-compatible**. Auto never selects it.
Provider examples:
@@ -120,7 +123,9 @@ Provider examples:
You may paste the full `/chat/completions` endpoint instead; Faceclaw removes
that suffix. It otherwise preserves the path you enter and does not guess `/v1`
or `/api`. There is no automatic model discovery in this version.
or `/api`. Discovery requests `{API root}/models` only when you choose it; there
is no background discovery or automatic model selection. Manual model entry
remains available for servers without a compatible model-list endpoint.
The same provider can supply Calculator's rewrite fallback and Translate.
Actual tool calling depends on the selected model and server. Use HTTPS where
+159
View File
@@ -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();
}
+8 -1
View File
@@ -97,6 +97,7 @@ 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";
/** The Settings app's master-detail panel (sections on the left, contents on the right). */
export function createSettingsPanelLayer(): SettingsPanelLayer {
@@ -152,8 +153,14 @@ function settingsSections(): SettingsSection[] {
textSettingMenuItem(assistantHermesUrlSetting),
textSettingMenuItem(assistantHermesKeySetting),
textSettingMenuItem(assistantOpenAiCompatibleUrlSetting),
textSettingMenuItem(assistantOpenAiCompatibleModelSetting),
textSettingMenuItem(assistantOpenAiCompatibleKeySetting),
compatibleModelDiscoveryMenuItem({
getBaseUrl: () => assistantOpenAiCompatibleUrlSetting.get(),
getApiKey: () => assistantOpenAiCompatibleKeySetting.get(),
getModel: () => assistantOpenAiCompatibleModelSetting.get(),
setModel: (model) => assistantOpenAiCompatibleModelSetting.set(model),
}, assistantOpenAiCompatibleModelSetting),
textSettingMenuItem(assistantOpenAiCompatibleModelSetting),
...(!global.isIOS ? [localModelMenuItem()] : []),
toggleSettingMenuItem(assistantSkipConfirmationSetting),
// iOS has no local LLM or agent bridge yet.
+232
View File
@@ -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),
};
}
+9 -1
View File
@@ -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;
@@ -0,0 +1,160 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
function discoveryEnv(responder) {
const requests = [];
const load = loader({}, {
'../util/http': {
fetchTextWithUserAgent: async (url, init) => {
requests.push({ url, init });
return responder(url, init, requests.length);
},
},
'./sse': {},
});
const api = load('app/native/openai-chat-models.ts');
api.clearCompatibleModelDiscoveryCache();
return { api, requests };
}
const response = (body, status = 200) => ({ status, text: async () => typeof body === 'string' ? body : JSON.stringify(body) });
const veniceModel = (id, name, tools, extra = {}) => ({
id,
model_spec: {
name,
capabilities: { supportsFunctionCalling: tools },
...extra,
},
});
const options = {
baseUrl: 'https://api.venice.ai/api/v1',
apiKey: 'venice-key',
};
test('Venice discovery uses its exact API root, auth, timeout, no redirects, and capability metadata', async () => {
const env = discoveryEnv(async () => response({ data: [
veniceModel('chat-only', 'Chat only', false),
veniceModel('offline-tools', 'Offline tools', true, { offline: true }),
veniceModel('tools-model', 'Tools model', true, { betaModel: true }),
{ id: 'generic-model', name: 'Generic model' },
veniceModel('old-model', 'Old model', true, { deprecation: { date: '2027-01-01' } }),
] }));
const models = await env.api.discoverOpenAiCompatibleModels(options);
assert.equal(env.requests.length, 1);
assert.equal(env.requests[0].url, 'https://api.venice.ai/api/v1/models');
assert.equal(env.requests[0].init.method, 'GET');
assert.equal(env.requests[0].init.headers.Authorization, 'Bearer venice-key');
assert.equal(env.requests[0].init.timeoutMs, 10_000);
assert.equal(env.requests[0].init.dontFollowRedirects, true);
assert.equal(models.map(model => model.id).join(','), 'old-model,tools-model,generic-model,chat-only,offline-tools');
const tools = models.find(model => model.id === 'tools-model');
assert.equal(tools.name, 'Tools model');
assert.equal(tools.supportsTools, true);
assert.equal(tools.beta, true);
assert.equal(models.find(model => model.id === 'old-model').deprecated, true);
assert.equal(models.find(model => model.id === 'generic-model').supportsTools, null);
assert.equal(models.find(model => model.id === 'chat-only').supportsTools, false);
assert.equal(models.find(model => model.id === 'offline-tools').offline, true);
});
test('Open WebUI discovery uses /api/models, supports optional auth, and marks tool support unknown', async () => {
const env = discoveryEnv(async () => response({ data: [
{ id: 'qwen-local', name: 'Qwen local' },
{ id: 'second-model' },
] }));
const models = await env.api.discoverOpenAiCompatibleModels({
baseUrl: 'http://100.64.0.1:3000/api', apiKey: '',
});
assert.equal(env.requests[0].url, 'http://100.64.0.1:3000/api/models');
assert.equal('Authorization' in env.requests[0].init.headers, false);
assert.equal(JSON.stringify(models.map(model => [model.id, model.supportsTools])), JSON.stringify([
['qwen-local', null], ['second-model', null],
]));
});
test('successful catalogs are cached briefly per endpoint and credential; refresh bypasses the cache', async () => {
let now = 1000;
const env = discoveryEnv(async (_url, _init, count) => response({ data: [{ id: `model-${count}` }] }));
const deps = { now: () => now };
const first = await env.api.discoverOpenAiCompatibleModels(options, deps);
const cached = await env.api.discoverOpenAiCompatibleModels(options, deps);
assert.equal(first, cached);
assert.equal(env.requests.length, 1);
await env.api.discoverOpenAiCompatibleModels({ ...options, apiKey: 'other-key' }, deps);
assert.equal(env.requests.length, 2, 'a different credential cannot reuse the old response');
await env.api.discoverOpenAiCompatibleModels({ ...options, baseUrl: 'https://other.example/api' }, deps);
assert.equal(env.requests.length, 3, 'a different endpoint has its own catalog');
await env.api.discoverOpenAiCompatibleModels({ ...options, forceRefresh: true }, deps);
assert.equal(env.requests.length, 4, 'explicit refresh always fetches');
now += 61_000;
await env.api.discoverOpenAiCompatibleModels(options, deps);
assert.equal(env.requests.length, 5, 'the short-lived entry expires');
});
test('invalid URLs and unsupported or malformed responses fail without querying alternate endpoints', async () => {
const env = discoveryEnv(async (_url, _init, count) => {
if (count === 1) return response('<html>login</html>');
if (count === 2) return response({ models: [] });
return response({ data: [] });
});
await assert.rejects(env.api.discoverOpenAiCompatibleModels({ baseUrl: 'not-a-url', apiKey: '' }), /valid compatible API URL/);
assert.equal(env.requests.length, 0);
await assert.rejects(env.api.discoverOpenAiCompatibleModels(options), /invalid model list/);
await assert.rejects(env.api.discoverOpenAiCompatibleModels({ ...options, forceRefresh: true }), /did not return an OpenAI-compatible model list/);
assert.deepEqual(env.requests.map(request => request.url), [
'https://api.venice.ai/api/v1/models', 'https://api.venice.ai/api/v1/models',
]);
});
test('HTTP and reachability errors are actionable and never fall back to inference', async () => {
const statuses = [401, 403, 404, 429, 503, 302];
for (const status of statuses) {
const env = discoveryEnv(async () => response({}, status));
await assert.rejects(env.api.discoverOpenAiCompatibleModels(options), error => {
assert.match(error.message, new RegExp(String(status)));
return true;
});
assert.equal(env.requests.length, 1);
assert.equal(env.requests[0].init.method, 'GET');
}
const unreachable = discoveryEnv(async () => { throw new Error('socket details'); });
await assert.rejects(unreachable.api.discoverOpenAiCompatibleModels(options), /Could not reach the configured server/);
});
test('model parsing skips invalid rows, deduplicates IDs, and caps catalog size', () => {
const env = discoveryEnv(async () => response({ data: [] }));
const rows = [null, {}, { id: '' }, { id: 'same', name: 'First' }, { id: 'same', name: 'Duplicate' }];
for (let index = 0; index < 510; index++) rows.push({ id: `model-${String(index).padStart(3, '0')}` });
const parsed = env.api.parseDiscoveredCompatibleModels(rows);
assert.equal(parsed.length, 500);
assert.equal(parsed.filter(model => model.id === 'same').length, 1);
assert.equal(parsed.find(model => model.id === 'same').name, 'First');
});
test('native text HTTP forwards discovery timeout and redirect protections with the app User-Agent', async () => {
const requests = [];
const load = loader({ global: { isAndroid: false, isIOS: true } }, {
'../version': { USER_AGENT: 'Faceclaw/test' },
'@nativescript/core/http': {
request: async init => {
requests.push(init);
return { statusCode: 200, content: { toString: () => '{"data":[]}' } };
},
},
});
const { fetchTextWithUserAgent } = load('app/util/http.ts');
const result = await fetchTextWithUserAgent('https://provider.example/api/models', {
headers: { Authorization: 'Bearer secret' },
timeoutMs: 10_000,
dontFollowRedirects: true,
});
assert.equal(requests[0].timeout, 10_000);
assert.equal(requests[0].dontFollowRedirects, true);
assert.equal(requests[0].headers.Authorization, 'Bearer secret');
assert.equal(requests[0].headers['User-Agent'], 'Faceclaw/test');
assert.equal(JSON.stringify(await result.json()), JSON.stringify({ data: [] }));
});
@@ -0,0 +1,301 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
const flush = () => new Promise(resolve => setImmediate(resolve));
class FakeMenuLayer {
constructor(title, items, layout) {
this.title = title;
this.layout = layout;
this.setItems(items);
}
setItems(items, selectedIndex = 0) {
this.items = items;
this.selectedIndex = items.length
? Math.max(0, Math.min(selectedIndex ?? 0, items.length - 1))
: null;
}
get selectedItem() {
return this.selectedIndex === null ? null : this.items[this.selectedIndex];
}
}
class FakeEditTextSettingLayer {
constructor(setting) { this.setting = setting; }
}
function deferred() {
let resolve, reject;
const promise = new Promise((res, rej) => { resolve = res; reject = rej; });
return { promise, resolve, reject };
}
function model(id, extra = {}) {
return {
id,
name: extra.name ?? id,
supportsTools: extra.supportsTools ?? null,
offline: extra.offline ?? false,
deprecated: extra.deprecated ?? false,
beta: extra.beta ?? false,
};
}
function pickerEnv({ models = [], saved = '', discover } = {}) {
const calls = [];
const settings = {
baseUrl: 'https://provider.example/api/v1',
apiKey: 'secret',
model: saved,
getBaseUrl() { return this.baseUrl; },
getApiKey() { return this.apiKey; },
getModel() { return this.model; },
setModel(value) { this.model = value; },
};
const modelSetting = { id: 'compatible-model' };
const renders = [];
const edits = [];
const layers = [{ id: 'settings-base' }];
const remove = layer => layer.onRemoved?.();
const stack = {
getBaseSize: () => ({ width: 320, height: 240 }),
push(layer) { layers.push(layer); },
topMatches(predicate) { return layers.length > 1 && predicate(layers.at(-1)); },
pop() { if (layers.length > 1) remove(layers.pop()); },
popThrough(layer) {
const index = layers.indexOf(layer);
if (index <= 0) return false;
for (const removed of layers.splice(index).reverse()) remove(removed);
return true;
},
};
const ctx = {
stack,
actions: {
requestRender: () => renders.push('render'),
startTextSettingEdit: setting => { edits.push(setting); },
},
};
const load = loader({}, {
'./menu': { MenuLayer: FakeMenuLayer },
'./dashboard-settings': { EditTextSettingLayer: FakeEditTextSettingLayer },
'../native/openai-chat-models': {},
});
const api = load('app/ui/openai-compatible-model-picker.ts');
const discoverImpl = discover ?? (async options => {
calls.push(options);
return models;
});
const picker = new api.OpenAiCompatibleModelPicker(
settings, modelSetting, discoverImpl, stack.getBaseSize(),
);
return {
api, picker, settings, modelSetting, discoverImpl,
calls, renders, edits, layers, ctx,
};
}
function row(picker, text) {
const found = picker.items.find(item => item.label.includes(text));
assert.ok(found, `Missing row ${text}; got: ${picker.items.map(item => item.label).join(' | ')}`);
return found;
}
test('catalog shows capabilities, hides offline models, and saves only an explicit selection', async () => {
const env = pickerEnv({
saved: 'generic-model',
models: [
model('tools-model', { name: 'Tools Model', supportsTools: true }),
model('generic-model', { name: 'Generic Model' }),
model('offline-model', { supportsTools: true, offline: true }),
],
});
env.picker.open(env.ctx);
assert.equal(row(env.picker, 'Discovering').disabled, true);
await flush();
assert.equal(env.settings.model, 'generic-model');
assert.equal(env.calls.length, 1);
assert.equal(env.calls[0].baseUrl, 'https://provider.example/api/v1');
assert.equal(env.calls[0].apiKey, 'secret');
assert.equal(env.calls[0].forceRefresh, false);
assert.equal(env.picker.items.some(item => item.label.includes('offline-model')), false);
assert.match(row(env.picker, 'Tools Model').label, /\[tools\]/);
assert.match(row(env.picker, 'Generic Model').label, /\[tools\?\]/);
assert.ok(row(env.picker, 'Generic Model').label.endsWith(' *'));
assert.equal(env.picker.selectedItem.label, row(env.picker, 'Generic Model').label);
assert.equal(env.picker.layout.width, 280);
assert.equal(env.picker.layout.maxHeight, 200);
await row(env.picker, 'Tools Model').onSelect(env.ctx, env.picker);
assert.equal(env.settings.model, 'tools-model');
assert.equal(env.layers.length, 1);
});
test('refresh bypasses cache and preserves an unlisted saved model without auto-selecting', async () => {
const env = pickerEnv({
saved: 'keep-model',
discover: async options => {
env.calls.push(options);
return [model(env.calls.length === 1 ? 'first-model' : 'second-model')];
},
});
env.picker.open(env.ctx);
await flush();
assert.match(row(env.picker, 'keep-model').label, /saved only/);
assert.equal(env.picker.selectedItem.label, row(env.picker, 'keep-model').label);
await row(env.picker, 'Refresh models').onSelect(env.ctx, env.picker);
await flush();
assert.equal(env.calls.length, 2);
assert.equal(env.calls[1].forceRefresh, true);
assert.equal(env.settings.model, 'keep-model');
assert.ok(row(env.picker, 'second-model'));
assert.ok(row(env.picker, 'keep-model'));
});
test('cancel invalidates a pending result and manual entry remains available while loading', async () => {
const pending = deferred();
const env = pickerEnv({
saved: 'original-model',
discover: options => { env.calls.push(options); return pending.promise; },
});
env.picker.open(env.ctx);
await row(env.picker, 'Cancel discovery').onSelect(env.ctx, env.picker);
assert.equal(env.layers.length, 1);
const renderCount = env.renders.length;
pending.resolve([model('late-model')]);
await flush();
assert.equal(env.settings.model, 'original-model');
assert.equal(env.renders.length, renderCount);
const second = deferred();
const manual = pickerEnv({
saved: 'original-model',
discover: () => second.promise,
});
manual.picker.open(manual.ctx);
await row(manual.picker, 'Enter model manually').onSelect(manual.ctx, manual.picker);
assert.equal(manual.edits.length, 1);
assert.equal(manual.edits[0], manual.modelSetting);
assert.equal(manual.layers.length, 2);
assert.ok(manual.layers[1] instanceof FakeEditTextSettingLayer);
second.resolve([model('late-model')]);
await flush();
assert.equal(manual.settings.model, 'original-model');
assert.ok(manual.layers[1] instanceof FakeEditTextSettingLayer);
});
test('offline models are optional but a saved offline model remains selectable', async () => {
const env = pickerEnv({
saved: 'saved-offline',
models: [
model('online-model'),
model('saved-offline', { offline: true }),
model('other-offline', { offline: true }),
],
});
env.picker.open(env.ctx);
await flush();
assert.ok(row(env.picker, 'saved-offline'));
assert.equal(env.picker.items.some(item => item.label.includes('other-offline')), false);
await row(env.picker, 'Show offline (2)').onSelect(env.ctx, env.picker);
assert.ok(row(env.picker, 'other-offline'));
assert.match(row(env.picker, 'other-offline').label, /offline/);
await row(env.picker, 'Hide offline models').onSelect(env.ctx, env.picker);
assert.equal(env.picker.items.some(item => item.label.includes('other-offline')), false);
assert.equal(env.settings.model, 'saved-offline');
});
test('errors retain the saved model and Try again reloads the current endpoint', async () => {
const first = deferred();
const env = pickerEnv({
saved: 'original-model',
discover: options => {
env.calls.push(options);
if (env.calls.length === 1) return first.promise;
return Promise.resolve([model('new-endpoint-model')]);
},
});
env.picker.open(env.ctx);
env.settings.baseUrl = 'https://other.example/api';
first.resolve([model('old-endpoint-model')]);
await flush();
assert.match(row(env.picker, 'Failed:').description, /URL or key changed/);
assert.equal(env.picker.items.some(item => item.label.includes('old-endpoint-model')), false);
assert.equal(env.settings.model, 'original-model');
await row(env.picker, 'Try again').onSelect(env.ctx, env.picker);
await flush();
assert.equal(env.calls[1].baseUrl, 'https://other.example/api');
assert.equal(env.calls[1].forceRefresh, true);
assert.ok(row(env.picker, 'new-endpoint-model'));
assert.equal(env.settings.model, 'original-model');
});
test('a stale catalog cannot be selected after the endpoint changes', async () => {
const env = pickerEnv({ models: [model('old-endpoint-model')] });
env.picker.open(env.ctx);
await flush();
const staleRow = row(env.picker, 'old-endpoint-model');
env.settings.baseUrl = 'https://other.example/api';
await staleRow.onSelect(env.ctx, env.picker);
assert.equal(env.settings.model, '');
assert.equal(env.layers.length, 2);
});
test('empty catalogs retain recovery actions and settings row requires a URL', async () => {
const env = pickerEnv({ saved: 'keep-model', models: [] });
env.picker.open(env.ctx);
await flush();
assert.ok(row(env.picker, 'No models listed'));
assert.ok(row(env.picker, 'Refresh models'));
assert.ok(row(env.picker, 'Enter model manually'));
env.ctx.stack.pop();
const item = env.api.compatibleModelDiscoveryMenuItem(
env.settings, env.modelSetting, async () => [],
);
env.settings.baseUrl = '';
assert.equal(item.disabled(), true);
env.settings.baseUrl = 'https://provider.example/api/v1';
assert.equal(item.disabled(), false);
item.onSelect(env.ctx);
assert.equal(env.layers.length, 2);
assert.equal(env.layers[1].layout.width, 280);
env.layers[1].onRemoved();
});
test('a temporary overlay does not invalidate a pending result while the picker remains open', async () => {
const pending = deferred();
const env = pickerEnv({ discover: () => pending.promise });
env.picker.open(env.ctx);
env.layers.push({ id: 'temporary-overlay' });
pending.resolve([model('available-model')]);
await flush();
assert.equal(env.picker.items.some(item => item.label.includes('available-model')), true);
assert.equal(env.layers.length, 3);
assert.ok(env.renders.length > 0);
});
test('a discovery error is visible in the modal row and retains manual recovery', async () => {
const env = pickerEnv({
discover: async () => { throw new Error('Model discovery is not supported at this URL (HTTP 404)'); },
});
env.picker.open(env.ctx);
await flush();
assert.match(row(env.picker, 'Failed:').label, /404/);
assert.match(row(env.picker, 'Failed:').description, /Manual model entry/);
assert.ok(row(env.picker, 'Try again'));
assert.ok(row(env.picker, 'Enter model manually'));
env.ctx.stack.pop();
});
test('long labels stay bounded while descriptions retain the exact model ID', async () => {
const longId = `provider/${'x'.repeat(90)}-model`;
const longName = `A very long model name ${'y'.repeat(70)}`;
const env = pickerEnv({
models: [model(longId, { name: longName, supportsTools: true })],
});
env.picker.open(env.ctx);
await flush();
const longRow = row(env.picker, '[tools]');
assert.ok(longRow.label.length <= 54);
assert.ok(longRow.description.includes(longId));
});
+1 -1
View File
@@ -76,7 +76,7 @@
<li>Anthropic: <a href="https://www.anthropic.com/legal/privacy">https://www.anthropic.com/legal/privacy</a></li>
<li>Venice: <a href="https://venice.ai/privacy">https://venice.ai/privacy</a></li>
</ul>
<p>With an OpenAI-compatible provider, the configured server receives the assistant system prompt, conversation history, available tool definitions, and tool results needed to answer. Its URL, model ID, and optional API key are stored on the phone. Faceclaw does not operate that server and cannot make representations about its logging or retention; for a private Open WebUI server, the server's operator controls those policies.</p>
<p>With an OpenAI-compatible provider, the configured server receives the assistant system prompt, conversation history, available tool definitions, and tool results needed to answer. Its URL, model ID, and optional API key are stored on the phone. Faceclaw does not operate that server and cannot make representations about its logging or retention; for a private Open WebUI server, the server's operator controls those policies. If you choose Discover models, Faceclaw also requests that server's model catalog using the same configured URL and optional API key.</p>
<p>AI conversation histories and memory files may be stored on your phone. Faceclaw does not transmit voice-assistant prompts or responses to any party other than a user-selected AI model provider. The voice assistant is given tools with which it is able to access most things on the user's phone, including all content displayed on the glasses, location, notifications, and content accessible through the Terminal app.</p>
<p>The AI assistant can take actions on the user's behalf, including sharing any information it has access to. The user is responsible for any instructions they give it.</p>
<h2>Location</h2>