diff --git a/apps/desktop/scripts/pics-e2e.mjs b/apps/desktop/scripts/pics-e2e.mjs new file mode 100644 index 00000000..7bd08f67 --- /dev/null +++ b/apps/desktop/scripts/pics-e2e.mjs @@ -0,0 +1,172 @@ +#!/usr/bin/env node +/** + * Attached pictures reaching a model, end to end on a test box, through the installed app layout (packaged host + bundled Control + an engine + * component that carries strata-vision): + * 1. the model catalog marks the picture-capable models (images: true) when the picture reader is installed; + * 2. API models: a llama-server with a small picture model (--mmproj) is saved as a Custom model API; the host works out that it reads pictures, + * and a real picture goes out as image_url and is answered. A server without a picture reader, a model set "not to read pictures", and an API + * of another style each get a plain refusal (409) that says how to fix it; ticking "This model reads pictures" lets the picture through; + * 3. a local model (unless --skip-local): Download and prepare with Read pictures ON, load, and a picture through the app's own chat. + * + * node pics-e2e.mjs --install --components --work --image shapes.png \ + * --vlm http://127.0.0.1:18090/v1 --vlm-model smolvlm --text-only http://127.0.0.1:18091/v1 [--skip-local --family qwen --model IQ3_XXS --data --device cpu] + */ +import { spawn } from 'node:child_process'; +import { existsSync, mkdirSync, readFileSync, readdirSync } from 'node:fs'; +import { createServer } from 'node:net'; +import { join, resolve } from 'node:path'; + +const args = Object.fromEntries(process.argv.slice(2).reduce((acc, a, i, all) => (a.startsWith('--') ? [...acc, [a.slice(2), all[i + 1]?.startsWith('--') || all[i + 1] === undefined ? true : all[i + 1]]] : acc), [])); +for (const need of ['install', 'components', 'work', 'image', 'vlm']) if (!args[need]) { console.error('missing --' + need); process.exit(2); } +const install = resolve(args.install), work = resolve(args.work), components = resolve(args.components); +const skipLocal = args['skip-local'] === true, device = String(args.device ?? 'cpu'), family = String(args.family ?? 'qwen'), modelId = String(args.model ?? 'IQ3_XXS'); +const node = join(install, 'node', 'node'), hostEntry = join(install, 'host', 'server', 'index.mjs'); +const dirs = { models: join(work, 'models'), profiles: join(work, 'profiles'), runtime: join(work, 'runtime'), home: join(work, 'home'), xdg: join(work, 'xdg'), appdata: join(work, 'appdata') }; +for (const d of [dirs.models, dirs.home, dirs.xdg, dirs.appdata]) mkdirSync(d, { recursive: true }); +const delay = ms => new Promise(r => setTimeout(r, ms)); +let failed = false; +const record = (name, ok, detail = '') => { if (!ok) failed = true; console.log(`${ok ? 'ok ' : 'FAIL'} ${name}${detail ? ' ' + String(detail).slice(0, 600) : ''}`); }; +const freePort = async () => { const s = createServer(); await new Promise(r => s.listen(0, '127.0.0.1', r)); const p = s.address().port; await new Promise(r => s.close(r)); return p; }; +const env = { ...process.env, STRATA_COMPONENTS_DIR: components, STRATA_PROFILE_DIR: dirs.profiles, STRATA_RUNTIME_DIR: dirs.runtime, STRATA_INSTALL_DIR: dirs.models, + STRATA_HOME: dirs.home, STRATA_MODELS_DIR: join(work, 'models-unused'), APPDATA: dirs.appdata, XDG_CONFIG_HOME: dirs.xdg, PYTHONDONTWRITEBYTECODE: '1' }; +delete env.STRATA_API_KEY; +const port = await freePort(); +const base = `http://127.0.0.1:${port}`; +const host = spawn(node, [hostEntry], { cwd: join(install, 'host'), stdio: ['ignore', 'pipe', 'pipe'], + env: { ...env, STRATA_PORT: String(port), STRATA_DIST: join(install, 'host', 'dist'), NODE_ENV: 'production', STRATA_PACKAGED: '1', STRATA_REAL_ENGINE: '1', STRATA_NO_ACQUIRE: '1', + STRATA_SHARE_PORT: String(await freePort()), STRATA_TUNNEL_PORT: String(await freePort()) } }); +let hostLog = ''; +host.stdout.on('data', c => { hostLog += c; }); host.stderr.on('data', c => { hostLog += c; }); +const call = async (path, method = 'GET', body) => { + const response = await fetch(base + path, { method, headers: { 'content-type': 'application/json' }, body: body === undefined ? undefined : JSON.stringify(body) }); + return { status: response.status, body: await response.json().catch(() => null) }; +}; +const png = 'data:image/png;base64,' + readFileSync(resolve(args.image)).toString('base64'); +const QUESTION = 'Name each shape in this picture with its colour, and read out any text.'; + +/** One chat turn through the app's own endpoint: the plain-text answer, or the refusal (status + message). */ +async function chat(extra, message = QUESTION) { + const response = await fetch(base + '/api/chat/stream', { method: 'POST', headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ message, agent: false, images: [{ url: png, name: 'shapes.png' }], ...extra }) }); + if (response.status !== 200) { const b = await response.json().catch(() => ({})); return { status: response.status, message: b.error ?? b.message ?? '' }; } + let text = '', error = ''; + for (const line of (await response.text()).split('\n')) { + if (!line.trim()) continue; + let f; try { f = JSON.parse(line); } catch { continue; } + if (f.type === 'error') error = f.message ?? 'error'; + else if (typeof f.text === 'string' && f.type !== 'reasoning' && f.type !== 'thinking') text += f.text; + } + return { status: 200, text, error }; +} +const saveApi = async (name, baseUrl, models, protocol = 'openai-chat') => { + const existing = (await call('/api/model-providers')).body.connections.find(c => c.name === name); + return (await call('/api/model-providers', 'POST', { ...(existing ? { id: existing.id } : {}), kind: 'custom', provider: null, name, baseUrl, protocol, apiKey: '', models })).body; +}; +async function readsAfter(connectionName, id, want, seconds = 20) { + for (let i = 0; i < seconds * 2; i++) { + const c = (await call('/api/model-providers')).body.connections.find(x => x.name === connectionName); + const m = c?.models.find(x => x.id === id); + if (m && m.reads === want) return { c, m }; + await delay(500); + } + const c = (await call('/api/model-providers')).body.connections.find(x => x.name === connectionName); + return { c, m: c?.models.find(x => x.id === id) }; +} +async function waitJob(id, seconds) { + let last = ''; + for (let i = 0; i < seconds * 2; i++) { + const state = (await call('/api/native')).body; + const job = state.jobs.find(j => j.id === id); + if (job && job.summary !== last) { last = job.summary; console.log(' ... ' + String(last).slice(0, 120)); } + if (job && job.state !== 'running') return { job, state }; + await delay(500); + } + throw new Error('operation did not finish'); +} +const names = low => /(red|blue|green|circle|square|triangle|strata|42)/.test(low); + +async function main() { + let up = false; + for (let i = 0; i < 200 && !up; i++) { try { up = (await fetch(base + '/api/health')).ok; } catch { /* not yet */ } if (!up) await delay(150); } + record('host healthy', up, up ? base : hostLog.slice(-500)); + if (!up) return; + + // ---- 1. the catalog + const packages = existsSync(join(components, 'engine')) ? readdirSync(join(components, 'engine')) : []; + const reader = packages.some(p => existsSync(join(components, 'engine', p, 'strata-vision'))); + const native = (await call('/api/native?refresh=1')).body; + record('the engine component carries the picture reader and the host knows it (visionReader)', reader && native.visionReader === true, `component=${reader} host=${native.visionReader}`); + const rows = (native.catalog?.families ?? []).flatMap(f => (f.sizes ?? []).map(s => ({ family: f.title ?? f.family, size: s.id, images: s.images, capable: s.imagesCapable }))); + console.log(' catalog (family / size / images / imagesCapable):'); + for (const r of rows) console.log(` ${String(r.family).padEnd(26)} ${String(r.size).padEnd(30)} images=${r.images} capable=${r.capable}`); + record('some models in the catalog read pictures', rows.some(r => r.images === true), `${rows.filter(r => r.images).length} of ${rows.length}`); + record('every picture-capable model reads pictures when the reader is installed', rows.every(r => r.capable === r.images), ''); + const flash = rows.filter(r => /flash-next/i.test(`${r.family} ${r.size}`) && !/gyro/i.test(`${r.family} ${r.size}`)); + record('the Qwen3.8-Flash-Next models are marked images: true', flash.length > 0 && flash.every(r => r.images === true), flash.map(r => r.size + '=' + r.images).join(' ')); + + // ---- 2. API models + const smol = args['vlm-model'] ?? 'smolvlm'; + await saveApi('Small picture model', args.vlm, [{ id: smol, name: 'SmolVLM' }]); + const found = await readsAfter('Small picture model', smol, true); + record('the host works out that the llama-server reads pictures (no tick needed)', found.m?.reads === true, JSON.stringify(found.m)); + const conn = found.c; + const sent = await chat({ remoteModel: { connectionId: conn?.id, modelId: smol } }); + console.log(` answer from the API model: ${JSON.stringify((sent.text ?? sent.message ?? '').slice(0, 300))}`); + record('the picture goes to the API model and is answered', sent.status === 200 && !sent.error && (sent.text ?? '').trim().length > 0 && names((sent.text ?? '').toLowerCase()), `status=${sent.status} err=${sent.error ?? ''}`); + + if (args['text-only']) { + await saveApi('Text only server', args['text-only'], [{ id: 'plain', name: 'Plain model' }]); + const t = await readsAfter('Text only server', 'plain', false); + record('a server without a picture reader is known not to read pictures', t.m?.reads === false, JSON.stringify(t.m)); + const refused = await chat({ remoteModel: { connectionId: t.c.id, modelId: 'plain' } }); + console.log(` refusal: ${JSON.stringify(refused.message)}`); + record('a picture for it is refused in plain words that say how to fix it', refused.status === 409 && /This model reads pictures/.test(refused.message ?? '') && /Settings/.test(refused.message ?? ''), `${refused.status}`); + const ticked = await saveApi('Text only server', args['text-only'], [{ id: 'plain', name: 'Plain model', vision: true }]); + const after = (await call('/api/model-providers')).body.connections.find(x => x.name === 'Text only server'); + record('ticking "This model reads pictures" is saved and makes the model read pictures', after?.models[0]?.vision === true && after?.models[0]?.reads === true, JSON.stringify(after?.models[0])); + const through = await chat({ remoteModel: { connectionId: after.id, modelId: 'plain' } }); + record('with the tick the picture is sent on (the server itself decides what to do with it)', through.status === 200, `${through.status} ${through.message ?? through.error ?? ''}`); + } + + await saveApi('Small picture model', args.vlm, [{ id: smol, name: 'SmolVLM', vision: false }]); + const off = await readsAfter('Small picture model', smol, false); + const notRead = await chat({ remoteModel: { connectionId: off.c.id, modelId: smol } }); + record('a model set not to read pictures refuses them and says where to change it', notRead.status === 409 && /set not to read pictures/.test(notRead.message ?? ''), `${notRead.status} ${notRead.message ?? ''}`); + await saveApi('Small picture model', args.vlm, [{ id: smol, name: 'SmolVLM', vision: null }]); + + await saveApi('Other style', 'https://api.anthropic.com/v1', [{ id: 'claude-x', name: 'Claude' }], 'anthropic'); + const other = (await call('/api/model-providers')).body.connections.find(x => x.name === 'Other style'); + const style = await chat({ remoteModel: { connectionId: other.id, modelId: 'claude-x' } }); + console.log(` other style: ${JSON.stringify(style.message)}`); + record('an API of another style says plainly why pictures stay in the preview', style.status === 409 && /OpenAI/.test(style.message ?? ''), `${style.status}`); + + if (skipLocal) return; + + // ---- 3. a local model + const values = { family, model: modelId, context: 32768, vision: 'yes', loadVision: device, ...(args.data ? { data_dir: resolve(args.data) } : {}) }; + const started = await call('/api/native/operations', 'POST', { kind: 'install', args: values }); + record('the install operation starts', started.status === 200 || started.status === 202, JSON.stringify(started.body).slice(0, 200)); + const done = await waitJob(started.body.id, 3600); + record('Download and prepare finishes', done.job.state === 'done', `${done.job.state} ${done.job.error ?? done.job.summary}`); + const refreshed = (await call('/api/native?refresh=1')).body; + const model = refreshed.installed?.models?.find(m => [m.id, m.model, m.tag].some(x => String(x ?? '').toLowerCase() === modelId.toLowerCase())); + record('the model is prepared with its picture reader', !!model?.visionPrepared, JSON.stringify({ ready: model?.ready, visionPrepared: model?.visionPrepared })); + const load = await call('/api/native/operations', 'POST', { kind: 'load', args: { model: model?.model ?? modelId.toLowerCase(), vision: device } }); + const loaded = await waitJob(load.body.id, 900); + const instance = loaded.state.instances[0]; + record('the model loads with the picture reader', loaded.job.state === 'done' && instance?.status === 'ready' && instance?.vision === true, `${loaded.job.state} ${loaded.job.error ?? ''} vision=${instance?.vision}`); + const t0 = Date.now(); + let local = { status: 0 }; + for (let i = 0; i < 2 && !(local.text ?? '').trim(); i++) local = await chat({ nativeModelId: instance?.id, maxTokens: 4096 }); + console.log(` answer from the local model (${Math.round((Date.now() - t0) / 1000)} s): ${JSON.stringify((local.text ?? local.message ?? '').slice(0, 300))}`); + const low = (local.text ?? '').toLowerCase(); + record('the local model answers about the picture (names the shapes)', local.status === 200 && /red|circle/.test(low) && /blue|square/.test(low) && /green|triangle/.test(low), `status=${local.status} err=${local.error ?? ''}`); + record('the local model reads STRATA 42', low.includes('42')); + const unloaded = await call(`/api/native/instances/${instance?.id}/unload`, 'POST', {}); + record('unload', unloaded.status === 200, unloaded.status); +} +try { await main(); } catch (e) { record('run', false, e.stack ?? e); } +host.kill(); +await delay(1500); +console.log(failed ? 'PICS-E2E FAIL' : 'PICS-E2E PASS'); +process.exit(failed ? 1 : 0); diff --git a/apps/host/server/agent/profile.mjs b/apps/host/server/agent/profile.mjs index 768bf0f2..b70fb63a 100644 --- a/apps/host/server/agent/profile.mjs +++ b/apps/host/server/agent/profile.mjs @@ -234,7 +234,8 @@ export function buildProfile({ local = [], connections = [], gate = true, tools id: m.id, name: m.name || m.id, contextWindow: m.contextMax ?? ASSUMED_API_CONTEXT, maxTokens: reply(m), // Pictures only when the server itself says it reads them (the host asks its model list): a server started without a vision encoder refuses the whole request when a picture is in it. - input: ownServer && m.vision === true ? ['text', 'image'] : ['text'], + // (any OpenAI-style API: a cloud model the person marked, or whose model list says it reads pictures, takes image_url the same way) + input: protocol === 'openai-completions' && m.vision === true ? ['text', 'image'] : ['text'], ...(ownServer ? { reasoning: true, reasoningEfforts: LOCAL_EFFORTS } : {}), })), }; diff --git a/apps/host/server/index.mjs b/apps/host/server/index.mjs index 63210c21..fd2ec429 100644 --- a/apps/host/server/index.mjs +++ b/apps/host/server/index.mjs @@ -48,8 +48,7 @@ import { CONTEXT_TOKENS, VARIANTS, localModels, variant } from './catalog.mjs'; import { browseLocalModels, inspectLocalModel } from './local-models.mjs'; import { EventHub, error, json, readBody, readRaw, uid } from './util.mjs'; import { createUploads } from './chat-uploads.mjs'; -import { createVisionProbe } from './vision-probe.mjs'; -import { isPrivateHost } from './agent/llm-shape.mjs'; +import { createVisionProbe, effectiveVision } from './vision-probe.mjs'; import { RequestQueue } from './request-queue.mjs'; import { selectSkills, skillInstructions } from './skill-selection.mjs'; import { createModelProviders } from './model-providers.mjs'; @@ -104,7 +103,7 @@ const WORKSPACE_NAME = process.env.STRATA_WORKSPACE ? basename(WORKSPACE) : 'Str const insideApp = path => { const rel = relative(ROOT, resolve(path)); return rel === '' || (!rel.startsWith('..') && !isAbsolute(rel)); }; const hub = new EventHub(); -const modelProviders = createModelProviders({ directory: store.DATA_DIR, changed: value => hub.send('providers', value) }); +const modelProviders = createModelProviders({ directory: store.DATA_DIR, changed: value => hub.send('providers', withPictureAnswers(value)) }); const imageModels = createImageModels({ dataDir: store.DATA_DIR, modelsDir: () => store.MODELS_DIR, real: models.REAL, changed: value => hub.send('image-models', value) }); /* The installed app ships Strata Control (python + strata_control) next to the host; a source checkout has none and keeps its older paths. */ const CONTROL = findControl(); @@ -127,6 +126,26 @@ const llmProxy = createLlmProxy({ targetOf: key => agent.llmTarget(key), samplin const agentTools = createAgentTools({ read: () => store.get().agentTools, write: value => store.update({ agentTools: value }), dataDir: store.DATA_DIR, browser: () => ({ ...findBrowser(), cli: findPlaywrightMcp(agent.harnessDir()) }) }); agentTools.ensureFile(); const visionProbe = createVisionProbe(); +/** The model APIs as the chat picker sees them: each model says whether it reads pictures (what the person ticked, else what the server answered). Unanswered models are asked in the background. */ +function withPictureAnswers(list) { + const secrets = new Map(modelProviders.withSecrets().map(c => [c.id, c])); + const connections = (list.connections ?? []).map(c => ({ ...c, models: c.models.map(m => { + const asked = visionProbe.known(c, m.id); + if (m.vision == null && asked === undefined && c.protocol === 'openai-chat' && secrets.has(c.id)) void visionProbe.check(secrets.get(c.id), m.id).catch(() => {}); + return { ...m, reads: effectiveVision(m, asked) ?? null }; + }) })); + return { ...list, connections }; +} +/** Why a picture cannot go to this API model, in plain words and with the way to fix it; null when it can. */ +async function pictureRefusal(connection, model) { + if (!connection || !model) return 'This API model is no longer available. Choose another model in the chat picker.'; + if (connection.protocol !== 'openai-chat') return `Pictures can only be sent to model APIs that use the OpenAI chat style. "${connection.name}" uses a different style, so the picture stays in the preview. Add the model through an OpenAI-compatible address (a /v1 address) to send pictures.`; + const answer = effectiveVision(model, await visionProbe.check(connection, model.id)); + if (answer === true) return null; + return answer === false + ? `"${model.name}" is set not to read pictures. If it does, open Settings \u2192 Models, edit "${connection.name}" and tick "This model reads pictures".` + : `Strata cannot tell that "${model.name}" reads pictures. If it is a picture-reading model, open Settings \u2192 Models, edit "${connection.name}" and tick "This model reads pictures". Otherwise choose a model that reads pictures.`; +} const agent = createAgentBridge({ tools: () => agentTools.profileView(), skillsDir: () => AGENT_SKILLS, @@ -148,7 +167,7 @@ const agent = createAgentBridge({ // The tool servers set up for this model (Settings, Tools): the agent connects to the same ones. mcpServers: i.mcpServers, })), - connections: () => modelProviders.withSecrets().map(c => ({ ...c, models: c.models.map(m => ({ ...m, vision: visionProbe.known(c, m.id) })) })), + connections: () => modelProviders.withSecrets().map(c => ({ ...c, models: c.models.map(m => ({ ...m, vision: effectiveVision(m, visionProbe.known(c, m.id)) })) })), log: line => console.log(line), }); let storageChanging = false; @@ -536,7 +555,7 @@ async function runAgentTurn({ res, controller, chatId, body, message, workspaceI choice = { connectionId: String(body.remoteModel.connectionId), modelId: String(body.remoteModel.modelId) }; // Does this server read pictures? Asked before the agent is built, so a picture is never sent to one that cannot. const own = modelProviders.withSecrets().find(c => c.id === choice.connectionId); - if (own && own.protocol === 'openai-chat' && isPrivateHost(own.baseUrl)) await visionProbe.check(own, choice.modelId); + if (own && own.protocol === 'openai-chat') await visionProbe.check(own, choice.modelId); } else { try { instance = native.resolveInstance(body.nativeModelId ?? native.state().defaultId); } @@ -959,7 +978,7 @@ async function api(req, res, path, method, url) { // Credentials are handled only on the host. Responses and events expose a key-set flag. return error(res,404,'Unknown native control action.');}catch(e){return error(res,/Finish|Unload|running|current operation/.test(e.message)?409:400,e.message);}} - if (path === '/api/model-providers' && method === 'GET') return json(res, 200, modelProviders.list()); + if (path === '/api/model-providers' && method === 'GET') return json(res, 200, withPictureAnswers(modelProviders.list())); if (path === '/api/model-providers/discover' && method === 'POST') { const body = await readBody(req); const controller = new AbortController(); @@ -968,11 +987,11 @@ async function api(req, res, path, method, url) { catch (err) { return error(res, 400, err.name === 'TimeoutError' ? 'The model API timed out. Check the address and try again.' : err.message); } } if (path === '/api/model-providers' && method === 'POST') { - try { return json(res, 200, modelProviders.save(await readBody(req))); } + try { return json(res, 200, withPictureAnswers(modelProviders.save(await readBody(req)))); } catch (err) { return error(res, 400, err.message); } } const providerMatch = /^\/api\/model-providers\/([a-zA-Z0-9-]+)$/.exec(path); - if (providerMatch && method === 'DELETE') return json(res, 200, modelProviders.remove(providerMatch[1])); + if (providerMatch && method === 'DELETE') return json(res, 200, withPictureAnswers(modelProviders.remove(providerMatch[1]))); if(path === '/api/artifacts/open' && method === 'POST') { const body=await readBody(req, 30*1024*1024); @@ -1047,7 +1066,7 @@ async function api(req, res, path, method, url) { if (measureFolder(cwd).tooBig) return error(res, 400, 'This folder is too big to try a change on a copy. Open a smaller folder for this chat, then try again.'); let choice, slots = 1; if (body.remoteModel?.connectionId && body.remoteModel?.modelId) { choice = { connectionId: String(body.remoteModel.connectionId), modelId: String(body.remoteModel.modelId) }; - { const own = modelProviders.withSecrets().find(c => c.id === choice.connectionId); if (own && own.protocol === 'openai-chat' && isPrivateHost(own.baseUrl)) await visionProbe.check(own, choice.modelId); } slots = Number(body.slots) >= 1 ? Math.floor(Number(body.slots)) : 1; } + { const own = modelProviders.withSecrets().find(c => c.id === choice.connectionId); if (own && own.protocol === 'openai-chat') await visionProbe.check(own, choice.modelId); } slots = Number(body.slots) >= 1 ? Math.floor(Number(body.slots)) : 1; } else { try { const inst = native.resolveInstance(native.state().defaultId); choice = { instanceId: inst.id }; slots = inst.metrics?.live?.slots?.length || inst.parallel || 1; } catch { return error(res, 503, 'Load a model first, then try Time-skip again.'); } } const chatId = typeof body.sessionId === 'string' && body.sessionId ? body.sessionId : null; if (chatId && forkTs.byChat(chatId)) return error(res, 409, 'A Time-skip is already running in this chat.'); @@ -1794,7 +1813,7 @@ async function api(req, res, path, method, url) { const body = await readBody(req,12*1024*1024); if (path === '/api/plan/start-goal') { body.message = '/plan start'; if (typeof body.planId !== 'string') return error(res, 400, 'planId is required'); } let images;try{const named=Array.isArray(body.imageIds)&&body.imageIds.length?uploads.dataUrls(typeof body.sessionId==='string'?body.sessionId:'',body.imageIds):[];images=validateChatImages([...(Array.isArray(body.images)?body.images:[]),...named]);}catch(e){return error(res,400,e.message);} - if(images.length&&body.remoteModel){const own=modelProviders.withSecrets().find(c=>c.id===body.remoteModel.connectionId);if(!(own&&own.protocol==='openai-chat'&&isPrivateHost(own.baseUrl)))return error(res,409,'Choose a local image-reading model before sending attached pictures.');} + if(images.length&&body.remoteModel){const own=modelProviders.withSecrets().find(c=>c.id===body.remoteModel.connectionId);const row=own?.models.find(m=>m.id===body.remoteModel.modelId);const refused=await pictureRefusal(own,row);if(refused)return error(res,409,refused);} else if(images.length){let selected;try{selected=native.resolveInstance(body.nativeModelId??native.state().defaultId);}catch(e){return error(res,409,e.message);}if(!selected.vision)return error(res,409,'Enable Read pictures for this model before sending images.');} // The goal bar's Resume has no words of its own: it is the same turn as "/goal resume". const message = body.goalAction === 'resume' ? '/goal resume' : String(body.message ?? '').trim(); diff --git a/apps/host/server/local-models.mjs b/apps/host/server/local-models.mjs index bef57a46..0bc6dc3f 100644 --- a/apps/host/server/local-models.mjs +++ b/apps/host/server/local-models.mjs @@ -14,7 +14,8 @@ export function browseLocalModels(input){ return {directory,parent:dirname(directory)===directory?null:dirname(directory),entries:entries.slice(0,1000),truncated:entries.length>1000}; } export function inspectLocalModel(input){ - let file=fullPath(input);if(extname(file).toLowerCase()!=='.gguf')throw new Error('Select a .gguf model file. Folders, adapters, and projectors cannot be loaded as a chat model.'); + let file=fullPath(input);if(extname(file).toLowerCase()!=='.gguf')throw new Error('Select a .gguf model file. Folders and adapters cannot be loaded as a chat model.'); + if(/^mmproj/i.test(basename(file)))throw new Error('This is a picture reader (mmproj), not a model. Choose the model’s own .gguf file; a model that reads pictures needs its reader in the same folder.'); if(!statSync(file).isFile())throw new Error('Select the model’s .gguf file, rather than its folder.'); accessSync(file,constants.R_OK); let selected=readGGUF(file);const split=basename(file).match(/^(.*)-(\d{5})-of-(\d{5})\.gguf$/i); @@ -25,9 +26,9 @@ export function inspectLocalModel(input){ try{selected=readGGUF(file);}catch{throw new Error('The first shard is missing or invalid. Keep all model shards in the same folder.');} } const m=selected.metadata,arch=m['general.architecture']; - if(!ARCHITECTURES.has(arch))throw new Error(`The declared architecture ${clean(arch)||'(missing)'} is not supported by this text-model importer. Choose a complete text GGUF model.`); + if(!ARCHITECTURES.has(arch))throw new Error(`The declared architecture ${clean(arch)||'(missing)'} is not supported by this importer. This model type isn’t supported by the Strata engine; add it as a Custom model API (llama-server) instead.`); if(m['general.type']&&m['general.type']!=='model')throw new Error('This is an adapter or auxiliary file. Choose the full model GGUF.'); - if(!m['tokenizer.ggml.model']||!m['tokenizer.ggml.tokens']?.count)throw new Error('This GGUF does not include its tokenizer. Choose the full model, not a projector or adapter.'); + if(!m['tokenizer.ggml.model']||!m['tokenizer.ggml.tokens']?.count)throw new Error('This GGUF does not include its tokenizer. Choose the full model, not an adapter. A picture reader (mmproj) goes next to its model, not in its place.'); const integer=(key,min,max)=>{const value=m[key];if(!Number.isInteger(value)||valuemax)throw new Error(`Missing or invalid ${key} metadata. This model cannot be configured safely.`);return value;}; const context=integer(`${arch}.context_length`,4096,100_000_000),layers=integer(`${arch}.block_count`,1,4096),embedding=integer(`${arch}.embedding_length`,1,1_000_000),heads=integer(`${arch}.attention.head_count`,1,8192); const kvHeads=m[`${arch}.attention.head_count_kv`]??heads,keyLength=m[`${arch}.attention.key_length`]??embedding/heads,valueLength=m[`${arch}.attention.value_length`]??keyLength,experts=m[`${arch}.expert_count`]??0; diff --git a/apps/host/server/model-providers.mjs b/apps/host/server/model-providers.mjs index ecd7acc8..cd4ea882 100644 --- a/apps/host/server/model-providers.mjs +++ b/apps/host/server/model-providers.mjs @@ -1,4 +1,5 @@ /** Saved API connections. Secrets never leave the host's private credentials file. */ +import { modelReadsPictures } from './vision-probe.mjs'; import { existsSync, readFileSync, writeFileSync, renameSync, mkdirSync } from 'node:fs'; import { join } from 'node:path'; import { randomUUID } from 'node:crypto'; @@ -75,7 +76,7 @@ export function createModelProviders({ directory, changed = () => {}, fetchImpl if (rows.length > 100) fail('Add up to 100 models to one connection.'); const models = rows.map(row => { const id = text(row.id, 256); if (!id || /[\r\n]/.test(id)) fail('Every model needs a model ID.'); - return { id, name: text(row.name) || id, contextMax: numeric(row.contextMax), maxOutput: numeric(row.maxOutput) }; + return { id, name: text(row.name) || id, contextMax: numeric(row.contextMax), maxOutput: numeric(row.maxOutput), vision: row.vision === true ? true : row.vision === false ? false : null }; }); if (new Set(models.map(m => m.id)).size !== models.length) fail('Each model ID must be unique within this connection.'); return { id: existing?.id ?? randomUUID(), kind, provider: catalog?.id ?? null, name, baseUrl, protocol, apiKey, models }; @@ -128,7 +129,7 @@ export function createModelProviders({ directory, changed = () => {}, fetchImpl if (connection.protocol !== 'anthropic' || !data.has_more) break; after = data.last_id; if (!after) break; } - const models = [...new Map(rows.filter(m => text(m.id, 256)).map(m => [m.id, { id: text(m.id, 256), name: text(m.display_name ?? m.name) || text(m.id, 256), contextMax: numeric(m.context_length ?? m.max_context_length ?? m.context_window), maxOutput: numeric(m.max_output_tokens ?? m.max_completion_tokens ?? m.top_provider?.max_completion_tokens) }])).values()]; + const models = [...new Map(rows.filter(m => text(m.id, 256)).map(m => [m.id, { id: text(m.id, 256), name: text(m.display_name ?? m.name) || text(m.id, 256), contextMax: numeric(m.context_length ?? m.max_context_length ?? m.context_window), maxOutput: numeric(m.max_output_tokens ?? m.max_completion_tokens ?? m.top_provider?.max_completion_tokens), vision: modelReadsPictures(m) }])).values()]; return { models: models.sort((a, b) => a.name.localeCompare(b.name)) }; } async function stream({ selection, message, history = [], systemPrompt = '', images = [], signal, onFrame }) { diff --git a/apps/host/server/native-control.mjs b/apps/host/server/native-control.mjs index f4b114c3..08254fc9 100644 --- a/apps/host/server/native-control.mjs +++ b/apps/host/server/native-control.mjs @@ -111,7 +111,7 @@ export function createNativeControl({directory,requests=()=>[],changed=()=>{},sp /* A read that is not forced never makes the window wait for the (slow) status of Control: it gets the last state at once, and a stale one is refreshed in the background. Only the first read, and a forced one after an operation, waits. */ if(refreshing)return !force&&snapshot.available?state():refreshing;if(!force&&Date.now()-lastRead<45000)return state(); - refreshing=(async()=>{try{const [catalog,status]=packaged?await (async()=>{const s=await tool('strata_status',{hardware:true});const c=await tool('strata_models').catch(error=>({families:[],contexts:[],recommendation:{family:null,model:null},context_note:'',error:error.message}));return [c,s];})():await Promise.all([tool('strata_models'),tool('strata_status',{hardware:true})]);const noImages=c=>c&&Array.isArray(c.families)?{...c,families:c.families.map(f=>({...f,images:false,sizes:(f.sizes??[]).map(s=>({...s,images:false}))}))}:c;snapshot={available:true,repo:packaged?status.installDir:repo(),version:packaged?(control.version??null):await execute('git',['describe','--tags','--always'],repo()).then(s=>s.trim()).catch(()=>null),summary:status.summary,catalog:packaged?noImages(catalog):catalog,installed:installedInfo(status.installed),hardware:status.hardware,error:null,install:status.install,external:status.running??null,...(packaged?{packaged:true,installDir:status.installDir,engine:status.engine,installerError:status.installerError}:{})};lastRead=Date.now();if(status.install?.running&&![...jobs.values()].some(j=>j.state==='running'))begin('install',j=>watchInstall(j,status.install));}catch(error){snapshot={...snapshot,available:false,error:error.message};lastRead=Date.now();}emit();return state();})().finally(()=>{refreshing=null;});const run=refreshing;if(!force&&snapshot.available){run.catch(()=>{});return state();}return run; + refreshing=(async()=>{try{const [catalog,status]=packaged?await (async()=>{const s=await tool('strata_status',{hardware:true});const c=await tool('strata_models').catch(error=>({families:[],contexts:[],recommendation:{family:null,model:null},context_note:'',error:error.message}));return [c,s];})():await Promise.all([tool('strata_models'),tool('strata_status',{hardware:true})]);let visionReader=false;if(packaged){try{const e=await control.engine();const root=e?.engine?.root??(e?.engine?.exe?dirname(e.engine.exe):'');visionReader=!!root&&['strata-vision','strata-vision.exe'].some(n=>existsSync(join(root,n)));}catch{visionReader=false;}}const markImages=c=>c&&Array.isArray(c.families)?{...c,families:c.families.map(f=>({...f,imagesCapable:!!f.images,images:!!f.images&&visionReader,sizes:(f.sizes??[]).map(s=>({...s,imagesCapable:!!s.images,images:!!s.images&&visionReader}))}))}:c;snapshot={available:true,repo:packaged?status.installDir:repo(),version:packaged?(control.version??null):await execute('git',['describe','--tags','--always'],repo()).then(s=>s.trim()).catch(()=>null),summary:status.summary,catalog:packaged?markImages(catalog):catalog,visionReader:packaged?visionReader:true,installed:installedInfo(status.installed),hardware:status.hardware,error:null,install:status.install,external:status.running??null,...(packaged?{packaged:true,installDir:status.installDir,engine:status.engine,installerError:status.installerError}:{})};lastRead=Date.now();if(status.install?.running&&![...jobs.values()].some(j=>j.state==='running'))begin('install',j=>watchInstall(j,status.install));}catch(error){snapshot={...snapshot,available:false,error:error.message};lastRead=Date.now();}emit();return state();})().finally(()=>{refreshing=null;});const run=refreshing;if(!force&&snapshot.available){run.catch(()=>{});return state();}return run; } function configure({repository}){if(packaged){if(instances.size||[...jobs.values()].some(j=>j.state==='running'))throw new Error('Unload models and finish operations before changing the installation.');const next=realpathSync(String(repository));if(!statSync(next).isDirectory())throw new Error('Choose the folder that holds your Strata model configurations.');preferences={...preferences,installDir:next};packagedInstall=null;disconnect();writeFileSync(settingsPath,JSON.stringify(preferences,null,2),{mode:0o600});lastRead=0;return refresh(true);}if(instances.size||[...jobs.values()].some(j=>j.state==='running'))throw new Error('Unload models and finish operations before changing the installation.');const next=realpathSync(String(repository));const old=preferences;preferences={...preferences,repo:next};try{repo();}catch(e){preferences=old;throw e;}disconnect();writeFileSync(settingsPath,JSON.stringify(preferences,null,2),{mode:0o600});lastRead=0;return refresh(true);} function setDownloadDirectory(input){if([...jobs.values()].some(j=>j.state==='running'))throw new Error('Finish the current operation before changing the folder.');if(typeof input!=='string'||!input.trim()||/[\r\n\0]/.test(input))throw new Error('Choose a model folder.');const path=realpathSync(input.trim());if(!statSync(path).isDirectory())throw new Error('Choose a folder, not a file.');accessSync(path,constants.R_OK|constants.W_OK);preferences={...preferences,downloadDirectory:path};writeFileSync(settingsPath,JSON.stringify(preferences,null,2),{mode:0o600});emit();return state();} diff --git a/apps/host/server/vision-probe.mjs b/apps/host/server/vision-probe.mjs index 9d66a51b..e81f79cc 100644 --- a/apps/host/server/vision-probe.mjs +++ b/apps/host/server/vision-probe.mjs @@ -1,9 +1,31 @@ /** - * Does a model server on this network read pictures? Asked of its own model list (`/models`, `architecture.input_modalities`), cached for a - * few minutes. Unknown or unreachable counts as "no": a picture sent to a server without a vision encoder makes the whole request fail. + * Does a model server read pictures? Asked of the server itself, cached for a few minutes: + * - its model list (`/models`): `architecture.input_modalities`, `input_modalities`, `modalities`, or a `capabilities` list that names image / vision / multimodal; + * - a llama-server's `/props` (`modalities.vision`), which is how `llama-server --mmproj` says it can read pictures (its `/v1/models` does not). + * Unknown or unreachable counts as "no" (a picture sent to a server without a vision encoder makes the whole request fail), but the person can + * always tick "This model reads pictures" in the model's settings: that answer wins over anything asked here. */ const TTL_MS = 5 * 60_000; +/** True / false / null (the row does not say) from one entry of a server's model list. */ +export function modelReadsPictures(row) { + if (!row || typeof row !== 'object') return null; + const lists = [row.architecture?.input_modalities, row.input_modalities, row.modalities?.input, Array.isArray(row.modalities) ? row.modalities : null]; + for (const list of lists) if (Array.isArray(list)) return list.some(x => /^(image|vision|images)$/i.test(String(x))); + if (row.modalities && typeof row.modalities === 'object' && typeof row.modalities.vision === 'boolean') return row.modalities.vision; + const caps = row.capabilities ?? row.meta?.capabilities; + if (Array.isArray(caps)) return caps.some(x => /vision|image|multimodal/i.test(String(x))); + if (caps && typeof caps === 'object' && typeof caps.vision === 'boolean') return caps.vision; + return null; +} + +/** The effective answer for a saved model: what the person ticked, else what the server said (undefined: never asked). */ +export function effectiveVision(model, asked) { + if (model?.vision === true) return true; + if (model?.vision === false) return false; + return asked; +} + export function createVisionProbe({ fetcher = fetch, now = Date.now } = {}) { const cache = new Map(); // key -> { at, value } const keyOf = (conn, modelId) => `${conn.id}|${modelId}`; @@ -15,13 +37,18 @@ export function createVisionProbe({ fetcher = fetch, now = Date.now } = {}) { const k = keyOf(conn, modelId); const hit = cache.get(k); if (hit && now() - hit.at < TTL_MS) return hit.value; let value = false; + const base = String(conn.baseUrl).replace(/\/+$/, ''); + const headers = conn.apiKey ? { authorization: `Bearer ${conn.apiKey}` } : {}; + const get = async path => { const res = await fetcher(base + path, { headers, signal: AbortSignal.timeout(4000) }); return res.ok ? res.json() : null; }; try { - const res = await fetcher(String(conn.baseUrl).replace(/\/+$/, '') + '/models', { headers: conn.apiKey ? { authorization: `Bearer ${conn.apiKey}` } : {}, signal: AbortSignal.timeout(4000) }); - if (res.ok) { - const list = (await res.json())?.data ?? []; - const row = list.find(m => m?.id === modelId) ?? (list.length === 1 ? list[0] : null); - const modes = row?.architecture?.input_modalities ?? row?.input_modalities ?? []; - value = Array.isArray(modes) && modes.includes('image'); + const list = (await get('/models'))?.data ?? []; + const row = list.find(m => m?.id === modelId) ?? (list.length === 1 ? list[0] : null); + value = modelReadsPictures(row) === true; + if (!value && modelReadsPictures(row) !== false) { // the list did not say: a llama-server tells in /props (next to /v1, not under it) + try { + const props = await (async () => { const res = await fetcher(base.replace(/\/v1$/, '') + '/props', { headers, signal: AbortSignal.timeout(4000) }); return res.ok ? res.json() : null; })(); + value = props?.modalities?.vision === true; + } catch { /* no /props: not a llama-server */ } } } catch { value = false; } cache.set(k, { at: now(), value }); diff --git a/apps/host/src/components/ComposerControls.tsx b/apps/host/src/components/ComposerControls.tsx index fd680d8e..b8bbe140 100644 --- a/apps/host/src/components/ComposerControls.tsx +++ b/apps/host/src/components/ComposerControls.tsx @@ -83,14 +83,14 @@ export function ComposerControls({ session, onPreview, onAttach, onError, onSlas skipped++; continue; } - if (!directory && photoTypes.test(file.name) && nativeModel?.vision && !remoteModel) { + if (!directory && photoTypes.test(file.name) && (remoteModel ? apiModel?.reads === true : nativeModel?.vision)) { await attachChatPhoto(file, session?.id); continue; } if (!directory && (photoTypes.test(file.name) || /\.pdf$/i.test(file.name))) { await openFile(file); if (photoTypes.test(file.name)) - onError(remoteModel ? 'Pictures are open in preview. Choose a local model with Read pictures enabled to send them.' : 'Enable Read pictures in Models to send this image to the model. It is open in preview.'); + onError(remoteModel ? `Strata cannot tell that "${apiModel?.name ?? 'this model'}" reads pictures, so the picture is only open in preview. If it does, open Settings > Models, edit its connection and tick "This model reads pictures". Otherwise choose a model that reads pictures.` : 'Enable Read pictures in Models to send this image to the model. It is open in preview.'); continue; } if (!textTypes.test(file.name) || bytes + file.size > 256 * 1024) { diff --git a/apps/host/src/components/NativeLocalModel.tsx b/apps/host/src/components/NativeLocalModel.tsx index 09f9750b..7c68d038 100644 --- a/apps/host/src/components/NativeLocalModel.tsx +++ b/apps/host/src/components/NativeLocalModel.tsx @@ -8,14 +8,16 @@ import { nativeCatalogModels, catalogVersionLabel } from '../lib/native-catalog' import { sizeGb } from '../lib/format'; import { NativeProgress } from './NativeModels'; -interface Preview { path: string; preview: { file_count: number; total_bytes: number; file_set_status: string; issues: string[]; candidate_selection: { family: string; model: string } | null } } +interface Preview { path: string; preview: { file_count: number; total_bytes: number; file_set_status: string; issues: string[]; messages?: string[]; projectors?: { name: string; status: string; pinned: boolean; plain: string }[]; reads_pictures?: boolean; candidate_selection: { family: string; model: string } | null } } /** The words for why a folder of GGUF files cannot be used: a sentence and what to do, the checker's own codes under Details. */ function problem(issues: string[]): { message: string; next: string } { const has = (re: RegExp) => issues.some(i => re.test(i)); if (has(/incomplete_or_invalid_shard_set|multiple_model_groups/)) return { message: 'Some parts of this model are missing, or the folder holds more than one model.', next: 'Put every part of one model in one folder, then check again.' }; - if (has(/unrecognized_or_unsupported_model_variant|unsupported_architecture|unsupported_or_missing_architecture|expert_quantization|tokenizer/)) + if (has(/unsupported_architecture|unsupported_or_missing_architecture/)) + return { message: 'This model type isn’t supported by the Strata engine.', next: 'Add it as a Custom model API instead: start it with llama-server, then open Settings > Models > Add model provider > Custom model API.' }; + if (has(/unrecognized_or_unsupported_model_variant|expert_quantization|tokenizer/)) return { message: 'Strata can’t run these files yet.', next: 'Strata runs the Qwen3.8-Flash-Next models. Download one from Models, or choose a folder with its files.' }; return { message: 'These files could not be checked.', next: 'Check that the folder holds complete GGUF files, then try again.' }; } @@ -51,7 +53,7 @@ export function NativeLocalModel({ onBack }: { onBack: () => void }) { setPending(true); setError(''); try { - const result = await nativeAction('operations', { kind: 'install', args: { family: selection.family, model: selection.model, gguf_dir: checked.path, context: native?.catalog?.recommendation?.context ?? 32768, vision: 'no', kv: 'int8', loadAfter: true } }); + const result = await nativeAction('operations', { kind: 'install', args: { family: selection.family, model: selection.model, gguf_dir: checked.path, context: native?.catalog?.recommendation?.context ?? 32768, vision: checked.preview.projectors?.some(p => p.status === 'ok' && p.pinned) && native?.visionReader ? 'yes' : 'no', kv: 'int8', loadAfter: true } }); setJobId(result.id); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } @@ -70,6 +72,7 @@ export function NativeLocalModel({ onBack }: { onBack: () => void }) {

{tr(family ? family.title : 'Model found')}

{tr(version ? catalogVersionLabel(version) : selection?.model ?? '')} · {tr(selection?.model ?? '')}

{tr("{0} files · {1}", [checked.preview.file_count, sizeGb(checked.preview.total_bytes / 1024 ** 3)])}

+ {tr(checked.preview.messages?.map(m =>

{tr(m)}

))}
void prepare()}>{tr(pending ? 'Checking…' : 'Prepare and load')}
: <>

{tr(found!.message)}

diff --git a/apps/host/src/components/NativeModels.tsx b/apps/host/src/components/NativeModels.tsx index 0e3489b7..0b9d01d9 100644 --- a/apps/host/src/components/NativeModels.tsx +++ b/apps/host/src/components/NativeModels.tsx @@ -135,6 +135,7 @@ export function NativeModels({ onOtherModels, onReady, onImport, onConnectEngine
{tr("Computer use")}
{tr(cardOptions ?
{tr("Graphics cards")}
{cardOptions.options.map(o => { const on = activeCards?.key === o.key; return ; })}{cardOptions.ignored.length ?

{`Not used: ${cardOptions.ignored.map(g => `${g.name ?? 'a card'} (${g.why_ignored})`).join('; ')}.`}

: null}
: null)} + {tr(selectedSize?.imagesCapable && !selectedSize.images ?

{tr("This model can read pictures, but the engine installed here does not include the picture reader yet, so Read pictures is off. Update the engine in Settings → About to turn it on.")}

: null)} {tr(selectedSize?.images ? <>
{tr(expert && vision !== 'no' ? setFilter(e.target.value)}/>
{tr(available.filter(m => `${m.name} ${m.id}`.toLowerCase().includes(filter.toLowerCase())).map(m => ))}
: null)} - {tr(draft.models.length ?
{tr(draft.models.map(m =>
{m.name}{m.id}
))}
:

{tr("No models selected yet. Fetch the list or add a model ID.")}

)} + {tr(draft.models.length ?
{tr(draft.models.map(m =>
{m.name}{m.id}{tr(draft.protocol === 'openai-chat' ? : null)}
))}
:

{tr("No models selected yet. Fetch the list or add a model ID.")}

)}