Merge branch 'pr-57'

This commit is contained in:
James Babcock committed 2026-11-01 00:29:16 -07:00
commit 7f3e3f2305
25 files changed
+1888 -41

No files matched your search

+309
View File
@@ -0,0 +1,309 @@
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));
function assistant({ apiKey = 'fixture-key', baseUrl = 'https://provider.example/api/v1' } = {}) {
const requests = [], calls = [], deltas = [], errors = [], done = [];
const registry = {
listTools: () => [
{ name: 'timer.set', description: 'Set a timer', inputSchema: { type: 'object', properties: { seconds: { type: 'number' } } } },
{ name: 'glasses.show_alert', description: 'Show an alert', inputSchema: { type: 'object', properties: { text: { type: 'string' } } } },
],
callTool: async (name, args) => {
calls.push({ name, args });
return { ok: true, content: `${name} completed` };
},
};
const load = loader({ global: { isIOS: false, isAndroid: true }, setTimeout, clearTimeout }, {
'./sse': { openSseRequest: (url, body, headers, listener) => {
const request = { url, body: JSON.parse(body), headers, listener, cancelled: false };
requests.push(request);
return { cancel: () => { request.cancelled = true; } };
} },
'../native/local-llm': { streamLocalModel: () => assert.fail('unexpected local model'), isLocalModelReady: () => false, isLocalModelSupported: () => true, LOCAL_MODEL: { id: 'qwen', label: 'Qwen' } },
'./bridge-client': { assistantBridge: {} },
'./tool-registry': { toolRegistry: registry },
'../prompts': {
ASSISTANT_SYSTEM_PROMPT_BASE: 'test',
buildAssistantSystemPrompt: () => 'glasses system',
describeAssistantContext: () => '',
},
});
const { AssistantSession } = load('app/assistant/session.ts');
const session = new AssistantSession({ kind: 'direct', llm: {
selection: 'openai-compatible', provider: 'openai-compatible', model: 'fixture-model', apiKey, baseUrl,
} }, registry);
const send = text => session.sendUtterance(text, {}, {
onTextDelta: (_delta, full) => deltas.push(full),
onToolActivity() {},
onError: error => errors.push(error),
onTurnDone: result => done.push(result),
});
const event = (request, value) => request.listener.onLine('data: ' + JSON.stringify(value));
const chunk = (request, delta, finishReason = null) => event(request, { choices: [{ index: 0, delta, finish_reason: finishReason }] });
const finish = (request, reason = 'stop', close = 'done') => {
chunk(request, {}, reason);
if (close === 'done') request.listener.onLine('data: [DONE]');
else request.listener.onComplete();
};
return { load, session, send, event, chunk, finish, requests, calls, deltas, errors, done };
}
test('compatible provider sends full history, nested tools, the exact API root and optional auth', () => {
const h = assistant();
h.send('Remember the blue door');
const first = h.requests[0];
assert.equal(first.url, 'https://provider.example/api/v1/chat/completions');
assert.equal(first.headers.Authorization, 'Bearer fixture-key');
assert.equal(first.headers['Content-Type'], 'application/json');
assert.equal(first.body.model, 'fixture-model');
assert.equal(first.body.stream, true);
assert.equal('max_tokens' in first.body, false, 'provider default output limit is preserved');
assert.equal('reasoning_effort' in first.body, false, 'custom models default to provider reasoning');
assert.deepEqual(first.body.messages.map(m => [m.role, m.content]), [
['system', 'glasses system'], ['user', 'Remember the blue door'],
]);
assert.equal(first.body.tools[0].type, 'function');
assert.equal(first.body.tools[0].function.name, 'timer_set');
assert.equal(first.body.tools[0].function.parameters.type, 'object');
h.chunk(first, { content: 'Remembered.' }); h.finish(first);
h.send('What color?');
assert.deepEqual(h.requests[1].body.messages.slice(-3).map(m => [m.role, m.content]), [
['user', 'Remember the blue door'], ['assistant', 'Remembered.'], ['user', 'What color?'],
]);
h.session.cancel();
const noKey = assistant({ apiKey: '' });
noKey.send('Private request');
assert.equal('Authorization' in noKey.requests[0].headers, false);
noKey.session.cancel();
});
test('Venice web search is opt-in and never leaks to other compatible providers', () => {
const defaultOff = assistant({ baseUrl: 'https://api.venice.ai/api/v1' });
defaultOff.send('Current weather');
assert.equal('venice_parameters' in defaultOff.requests[0].body, false);
defaultOff.session.cancel();
const { isVeniceApiBaseUrl } = defaultOff.load('app/native/openai-chat.ts');
assert.equal(isVeniceApiBaseUrl(' https://api.venice.ai/api/v1/ '), true);
assert.equal(isVeniceApiBaseUrl('https://api.venice.ai/api/v1/chat/completions'), true);
assert.equal(isVeniceApiBaseUrl('http://api.venice.ai/api/v1'), false);
assert.equal(isVeniceApiBaseUrl('https://api.venice.ai.evil.example/api/v1'), false);
assert.equal(isVeniceApiBaseUrl('https://openwebui.example/api'), false);
const enabled = assistant({ baseUrl: 'https://api.venice.ai/api/v1' });
enabled.session.configure({ kind: 'direct', llm: {
selection: 'openai-compatible', provider: 'openai-compatible', model: 'fixture-model',
apiKey: 'fixture-key', baseUrl: 'https://api.venice.ai/api/v1', webSearch: true,
} });
enabled.send('Current weather');
assert.deepEqual(JSON.parse(JSON.stringify(enabled.requests[0].body.venice_parameters)), {
enable_web_search: 'on',
});
enabled.session.cancel();
const openWebUi = assistant({ baseUrl: 'https://openwebui.example/api' });
openWebUi.session.configure({ kind: 'direct', llm: {
selection: 'openai-compatible', provider: 'openai-compatible', model: 'fixture-model',
apiKey: 'fixture-key', baseUrl: 'https://openwebui.example/api', webSearch: true,
} });
openWebUi.send('Current weather');
assert.equal('venice_parameters' in openWebUi.requests[0].body, false);
openWebUi.session.cancel();
});
test('Venice and Open WebUI roots are preserved; only a complete endpoint suffix is removed', () => {
const { normalizeOpenAiCompatibleUrl } = assistant().load('app/native/openai-chat.ts');
assert.equal(normalizeOpenAiCompatibleUrl(' https://api.venice.ai/api/v1/ '), 'https://api.venice.ai/api/v1');
assert.equal(normalizeOpenAiCompatibleUrl('https://api.venice.ai/api/v1/chat/completions'), 'https://api.venice.ai/api/v1');
assert.equal(normalizeOpenAiCompatibleUrl('http://100.64.0.1:3000/api'), 'http://100.64.0.1:3000/api');
assert.equal(normalizeOpenAiCompatibleUrl('https://openwebui.example/api/chat/completions/'), 'https://openwebui.example/api');
assert.equal(normalizeOpenAiCompatibleUrl('https://provider.example/custom/root'), 'https://provider.example/custom/root');
assert.equal(normalizeOpenAiCompatibleUrl('provider.example/v1'), '');
assert.equal(normalizeOpenAiCompatibleUrl('https://provider.example/v1?key=secret'), '');
assert.equal(normalizeOpenAiCompatibleUrl('https://provider.example/v1#fragment'), '');
assert.equal(normalizeOpenAiCompatibleUrl('https://user:secret@provider.example/v1'), '', 'credentials never belong in saved URLs');
});
test('compatible model resolves only with URL and model, and Auto remains unchanged', () => {
const { ASSISTANT_MODEL_CHOICES, resolveAssistantModel } = assistant().load('app/assistant/models.ts');
assert.equal(ASSISTANT_MODEL_CHOICES.includes('openai-compatible'), true);
assert.equal(resolveAssistantModel('openai-compatible', { anthropic: '', openai: '' }), null);
assert.equal(resolveAssistantModel('openai-compatible', {
anthropic: '', openai: '', openAiCompatibleUrl: 'https://provider.example/api/v1', openAiCompatibleModel: '',
}), null);
const resolved = resolveAssistantModel('openai-compatible', {
anthropic: '', openai: '',
openAiCompatibleUrl: ' https://provider.example/api/v1 ',
openAiCompatibleKey: ' secret ', openAiCompatibleModel: ' model-id ',
});
assert.equal(resolved.provider, 'openai-compatible');
assert.equal(resolved.model, 'model-id');
assert.equal(resolved.apiKey, 'secret');
assert.equal(resolved.baseUrl, 'https://provider.example/api/v1');
assert.equal(resolved.effort, undefined);
assert.equal(resolveAssistantModel('auto', {
anthropic: '', openai: '', openAiCompatibleUrl: 'https://provider.example/api/v1', openAiCompatibleModel: 'model-id',
}), null, 'Auto never selects a custom provider');
});
test('streamed tool calls execute through Faceclaw, results continue the turn, and history survives follow-up', async () => {
const h = assistant();
h.send('Set a one-minute timer');
const first = h.requests[0];
h.chunk(first, { content: 'Setting it now.' });
h.chunk(first, {
reasoning_content: 'hidden reasoning',
reasoning_details: [{ type: 'reasoning.encrypted', data: 'opaque-fixture' }],
tool_calls: [{ index: 0, id: 'call-1', type: 'function', function: { name: 'timer_set', arguments: '{"seconds":' } }],
});
h.chunk(first, { tool_calls: [{ index: 0, function: { arguments: '60}' } }] });
h.finish(first, 'tool_calls');
await flush();
assert.equal(h.calls.length, 1);
assert.equal(h.calls[0].name, 'timer.set');
assert.equal(h.calls[0].args.seconds, 60);
assert.equal(h.requests.length, 2);
const second = h.requests[1];
const assistantMessage = second.body.messages.find(m => m.role === 'assistant' && m.tool_calls);
assert.equal(assistantMessage.content, 'Setting it now.');
assert.equal(assistantMessage.tool_calls[0].id, 'call-1');
assert.equal(assistantMessage.tool_calls[0].function.name, 'timer_set');
assert.equal(assistantMessage.tool_calls[0].function.arguments, '{"seconds":60}');
assert.equal(assistantMessage.reasoning_details[0].data, 'opaque-fixture');
const toolMessage = second.body.messages.find(m => m.role === 'tool');
assert.equal(toolMessage.tool_call_id, 'call-1');
assert.equal(toolMessage.content, 'timer.set completed');
assert.equal(JSON.stringify(second.body).includes('hidden reasoning'), false);
h.chunk(second, { content: 'Your timer is running.' });
h.finish(second, 'stop', 'close');
assert.equal(h.session.isTurnActive(), false);
assert.equal(h.done.length, 1);
assert.deepEqual(h.errors, []);
assert.equal(h.deltas.includes('hidden reasoning'), false);
h.send('What did you set?');
assert.match(JSON.stringify(h.requests[2].body), /Your timer is running/);
h.session.cancel();
assert.equal(h.requests[2].cancelled, true);
});
test('changing the API endpoint preserves visible history but drops provider-specific state', () => {
const h = assistant();
const original = h.session.config;
const config = (baseUrl) => ({ kind: 'direct', llm: {
selection: 'openai-compatible', provider: 'openai-compatible', model: 'fixture-model', apiKey: 'fixture-key', baseUrl,
} });
h.session.sendUtterance('Remember the blue door', {}, {
onTextDelta() {}, onToolActivity() {}, onError: error => assert.fail(error), onTurnDone() {},
});
const first = h.requests[0];
h.chunk(first, { reasoning_details: [{ type: 'opaque', data: 'server-one-only' }], content: 'Remembered.' });
h.finish(first);
const history = JSON.parse(JSON.stringify(h.session.history()));
assert.equal(JSON.stringify(history).includes('server-one-only'), true);
h.session.configure(config('https://provider-two.example/api/v1'));
h.send('What color?');
const second = h.requests[1];
assert.equal(second.url, 'https://provider-two.example/api/v1/chat/completions');
assert.equal(JSON.stringify(second.body).includes('server-one-only'), false);
assert.equal(JSON.stringify(second.body).includes('Remember the blue door'), true);
h.session.cancel();
const restored = new h.session.constructor(config('https://provider-two.example/api/v1'), undefined, history);
restored.sendUtterance('What color?', {}, {
onTextDelta() {}, onToolActivity() {}, onError: error => assert.fail(error), onTurnDone() {},
});
assert.equal(JSON.stringify(h.requests.at(-1).body).includes('server-one-only'), false);
restored.cancel();
});
test('multiple interleaved tool calls are reassembled independently and all results are returned', async () => {
const h = assistant();
h.send('Set a timer and show an alert');
const first = h.requests[0];
h.chunk(first, { tool_calls: [
{ index: 0, id: 'timer-id', function: { name: 'timer_set', arguments: '{"sec' } },
{ index: 1, id: 'alert-id', function: { name: 'glasses_show_alert', arguments: '{"text":' } },
] });
h.chunk(first, { tool_calls: [
{ index: 0, function: { arguments: 'onds":30}' } },
{ index: 1, function: { arguments: '"Done"}' } },
] });
h.finish(first, 'tool_calls');
await flush();
// Values produced inside the VM have a different Object prototype.
assert.equal(JSON.stringify(h.calls.map(c => [c.name, c.args])), JSON.stringify([
['timer.set', { seconds: 30 }], ['glasses.show_alert', { text: 'Done' }],
]));
const second = h.requests[1];
const tools = second.body.messages.filter(m => m.role === 'tool');
assert.equal(JSON.stringify(tools.map(m => [m.tool_call_id, m.content])), JSON.stringify([
['timer-id', 'timer.set completed'], ['alert-id', 'glasses.show_alert completed'],
]));
h.chunk(second, { content: 'Both actions completed.' });
h.finish(second);
assert.equal(h.session.isTurnActive(), false);
assert.deepEqual(h.errors, []);
});
test('content-only streams work with [DONE], while empty and in-band error streams fail clearly', () => {
const h = assistant();
h.send('Hello');
h.chunk(h.requests[0], { role: 'assistant' });
h.chunk(h.requests[0], { reasoning_content: 'not for the lenses' });
h.chunk(h.requests[0], { content: 'Hello ' });
h.chunk(h.requests[0], { content: 'there' });
h.requests[0].listener.onLine('data: [DONE]');
assert.equal(h.done[0].stopReason, 'end_turn');
assert.equal(h.session.transcript.at(-1).text, 'Hello there');
h.send('Empty'); h.finish(h.requests[1]);
assert.match(h.errors.at(-1), /without a reply/);
h.send('Failure');
h.event(h.requests[2], { error: { message: 'model overloaded' } });
assert.match(h.errors.at(-1), /model overloaded/);
});
test('HTTP errors, malformed tool JSON and premature EOF never execute incomplete tools', async () => {
const h = assistant();
h.send('Unauthorized');
h.requests[0].listener.onHttpError(401, '{"error":{"message":"bad key"}}');
assert.match(h.errors.at(-1), /invalid API key/);
assert.equal(h.session.isTurnActive(), false);
h.send('Malformed');
h.chunk(h.requests[1], { tool_calls: [{ index: 0, id: 'bad', function: { name: 'timer_set', arguments: '{broken' } }] });
h.finish(h.requests[1], 'tool_calls');
await flush();
assert.match(h.errors.at(-1), /malformed tool arguments/);
assert.equal(h.calls.length, 0);
h.send('Truncated');
h.chunk(h.requests[2], { tool_calls: [{ index: 0, id: 'partial', function: { name: 'timer_set', arguments: '{"seconds":60}' } }] });
h.requests[2].listener.onComplete();
await flush();
assert.match(h.errors.at(-1), /before the response completed/);
assert.equal(h.calls.length, 0);
h.send('Length limited');
h.chunk(h.requests[3], { tool_calls: [{ index: 0, id: 'partial', function: { name: 'timer_set', arguments: '{"seconds":60}' } }] });
h.finish(h.requests[3], 'length');
await flush();
assert.match(h.errors.at(-1), /before its tool calls completed/);
assert.equal(h.calls.length, 0);
});
test('cancellation suppresses late deltas and tool side effects', async () => {
const h = assistant();
h.send('Cancel this');
const request = h.requests[0];
h.session.cancel();
h.chunk(request, { content: 'late text', tool_calls: [{ index: 0, id: 'late', function: { name: 'timer_set', arguments: '{}' } }] });
h.finish(request, 'tool_calls');
await flush();
assert.equal(h.calls.length, 0);
assert.equal(h.deltas.length, 0);
assert.equal(h.done.length, 0);
assert.equal(request.cancelled, true);
});
@@ -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));
});
@@ -0,0 +1,86 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
function sharedEnv() {
const compatible = [], unexpected = [];
const localLlm = {
streamLocalModel: () => unexpected.push('local'),
isLocalModelReady: () => false,
isLocalModelSupported: () => true,
LOCAL_MODEL: { id: 'qwen', label: 'Qwen' },
};
const settings = {
anthropicApiKeySetting: { get: () => '' },
assistantModelSetting: { get: () => 'openai-compatible' },
assistantOpenAiCompatibleUrlSetting: { get: () => 'https://provider.example/api/v1' },
assistantOpenAiCompatibleKeySetting: { get: () => 'compatible-key' },
assistantOpenAiCompatibleModelSetting: { get: () => 'compatible-model' },
openAiApiKeySetting: { get: () => '' },
};
const fail = provider => () => { unexpected.push(provider); };
const load = loader({ global: { isIOS: false, isAndroid: true } }, {
'../../native/anthropic': { streamAnthropicMessage: fail('anthropic') },
'../../native/openai': { streamOpenAiResponse: fail('openai') },
'../../native/local-llm': localLlm,
'../native/local-llm': localLlm,
'../../native/openai-chat': {
streamOpenAiCompatibleChat: options => {
compatible.push(options);
return { cancel() {} };
},
},
'../../ui/dashboard-settings': settings,
'../../prompts': {
CALCULATOR_REWRITE_SYSTEM_PROMPT: 'calculator system',
buildCalculatorRewriteUserMessage: spoken => `rewrite: ${spoken}`,
TRANSLATE_SYSTEM_PROMPT: 'translate system',
buildTranslateUserMessage: speech => `translate: ${speech}`,
},
});
return { load, compatible, unexpected };
}
test('Calculator uses the configured compatible provider and preserves its small output cap', async () => {
const env = sharedEnv();
const { rewriteSpokenProblem } = env.load('app/apps/calculator/calculator-llm.ts');
const result = rewriteSpokenProblem('one plus one');
assert.equal(env.compatible.length, 1);
assert.deepEqual(env.unexpected, []);
const options = env.compatible[0];
assert.equal(options.baseUrl, 'https://provider.example/api/v1');
assert.equal(options.apiKey, 'compatible-key');
assert.equal(options.model, 'compatible-model');
assert.equal(options.maxTokens, 200);
assert.equal(options.messages[0].content, 'rewrite: one plus one');
options.onDone({ text: '1 + 1', content: [], stopReason: 'end_turn' });
assert.equal(await result, '1 + 1');
});
test('Translate resolves and streams through the compatible provider without changing its cancellation contract', () => {
const env = sharedEnv();
const { resolveTranslationModel, streamTranslation } = env.load('app/apps/translate/translate-llm.ts');
const model = resolveTranslationModel();
assert.equal(model.provider, 'openai-compatible');
assert.equal(model.baseUrl, 'https://provider.example/api/v1');
const progress = [], answers = [], errors = [];
const handle = streamTranslation(model, {
speech: 'Hola', context: [], language: 'Spanish',
onProgress: text => progress.push(text),
onDone: text => answers.push(text),
onError: error => errors.push(error),
});
assert.equal(typeof handle.cancel, 'function');
assert.equal(env.compatible.length, 1);
assert.deepEqual(env.unexpected, []);
const options = env.compatible[0];
assert.equal(options.baseUrl, 'https://provider.example/api/v1');
assert.equal(options.model, 'compatible-model');
assert.equal(options.maxTokens, 1024);
assert.equal(options.messages[0].content, 'translate: Hola');
options.onTextDelta('Hello', '<english>Hello</english>');
options.onDone({ text: '<english>Hello</english>', content: [], stopReason: 'end_turn' });
assert.deepEqual(progress, ['Hello']);
assert.deepEqual(answers, ['Hello']);
assert.deepEqual(errors, []);
});
+102
View File
@@ -0,0 +1,102 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
function settingsEnv(initialUrl = '') {
let url = initialUrl;
let webSearch = false;
const dummySetting = {
id: 'setting', label: 'Setting', get: () => '', set() {}, displayValue: () => '',
};
const urlSetting = {
id: 'compatible-url', label: 'Compatible API URL', get: () => url, set: value => { url = value; },
displayValue: () => url,
};
const searchSetting = {
id: 'venice-web-search', label: 'Venice web search', get: () => webSearch,
set: value => { webSearch = value; }, toggle: () => { webSearch = !webSearch; },
defaultValue: false, description: 'Venice-only web search.',
};
const row = setting => ({ label: setting.label, description: setting.description, onSelect() {} });
const dashboardSettings = new Proxy({
assistantOpenAiCompatibleUrlSetting: urlSetting,
veniceWebSearchSetting: searchSetting,
enumSettingMenuItem: row,
textSettingMenuItem: row,
toggleSettingMenuItem: row,
}, {
get(target, property) {
return property in target ? target[property] : dummySetting;
},
});
const load = loader({ global: { isIOS: false, isAndroid: true } }, {
'./remote-input-menu': { remoteInputMenuItem: () => ({ label: 'Remote input' }) },
'@nativescript/core': { knownFolders: {} },
'../../graphics/ui-fonts': { getDefaultSmallFont: () => ({}) },
'../../graphics/logo': { getDashboardLogo: () => null },
'../../graphics/textwrap': { wrapText: () => [] },
'../../version': { FACECLAW_VERSION: 'test' },
'../../native/local-llm': {
cancelLocalModelDownload() {}, deleteLocalModel() {}, startLocalModelDownload() {},
LOCAL_MODEL: { label: 'Qwen', sizeBytes: 1_000_000_000 },
localModelState: () => ({ status: 'missing' }), onLocalModelStateChanged: () => () => {},
},
'../../native/asr-model': {
ASR_MODELS: {
moonshine: { label: 'Moonshine', sizeBytes: 1 },
'whisper-base-en': { label: 'Whisper Base English', sizeBytes: 1 },
},
asrModelState: () => ({}), cancelAsrModelDownload() {},
deleteAsrModel() {}, startAsrModelDownload() {}, onAsrModelStateChanged: () => () => {},
},
'../../apps/files/text-viewer': { TextViewerLayer: class {} },
'../../analytics/privacy-disclosure': { IOS_PRIVACY_NOTICE: '' },
'../menu': {
drawRightValueMenuItem() {}, openModalMenu() {},
submenuItem: label => ({ label, onSelect() {} }),
},
'../shell/shell': { shell: { noteUserActivity() {} } },
'../dashboard-settings': dashboardSettings,
'../../native/wear-bridge': { wearBridge: {} },
'./settings-panel': {
SettingsPanelLayer: class { constructor(sections) { this.sections = sections; } },
openSettingsSubMenu() {},
},
'../font-picker': { uiFontPickerMenuItem: () => ({ label: 'Font' }) },
'../openai-compatible-model-picker': {
compatibleModelDiscoveryMenuItem: () => ({ label: 'Discover models' }),
},
'./sse': {},
});
const panel = load('app/ui/dashboard/settings-menus.ts').createSettingsPanelLayer();
const section = panel.sections.find(item => item.label === 'Assistant');
assert.ok(section);
return {
section,
labels: () => section.items.map(item => item.label),
setUrl: value => { url = value; },
searchSetting,
};
}
test('Venice web search row follows the current URL and never appears for other providers', () => {
const env = settingsEnv();
assert.equal(env.labels().includes('Venice web search'), false);
assert.equal(env.searchSetting.defaultValue, false);
assert.equal(env.searchSetting.get(), false);
env.setUrl(' https://api.venice.ai/api/v1/ ');
assert.equal(env.labels().includes('Venice web search'), true);
env.setUrl('https://openwebui.example/api');
assert.equal(env.labels().includes('Venice web search'), false);
env.setUrl('https://api.venice.ai/api/v1/chat/completions');
assert.equal(env.labels().includes('Venice web search'), true);
env.setUrl('https://api.venice.ai.evil.example/api/v1');
assert.equal(env.labels().includes('Venice web search'), false);
env.setUrl('http://api.venice.ai/api/v1');
assert.equal(env.labels().includes('Venice web search'), false);
});