Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree (570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base = the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main). Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details, conflict table and left-out files are in docs/upstream.md. Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61), docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out. scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run"). Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR macros on. No GPU run, no quality gate. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
109 lines
6.6 KiB
JavaScript
109 lines
6.6 KiB
JavaScript
/**
|
|
* Which window the person is using, and what only the app window can do.
|
|
*
|
|
* The Linux app starts the host itself (the "shell" process, which has no window) and shows the page in a separate window
|
|
* process. WebKitGTK can crash that process on some computers; the shell then keeps the host running and opens the same page
|
|
* in the default browser ("browser mode"). A person can also choose browser mode (`--browser`, STRATA_UI=browser).
|
|
*
|
|
* mode 'window' the app window shows the page; the window process registers the app updater here (Tauri's signed updater).
|
|
* mode 'browser' an ordinary browser tab shows the page. There is no window process, so the app cannot update itself, and
|
|
* closing the tab does not stop the engine: the menu has "Quit Strata" (POST /api/app/quit) and the command
|
|
* line has `--quit`.
|
|
* mode null not started by the app (the host run by hand, a test, a source checkout): nothing here applies.
|
|
*
|
|
* What needs the Tauri bridge and what happens without it (browser mode):
|
|
* folder picker works: the host runs zenity / kdialog / the desktop portal itself (folder-dialog.mjs), else the page asks for the path
|
|
* open a file / folder works: the host runs xdg-open itself (chat-files.mjs, artifacts.mjs)
|
|
* window controls none to offer: the browser has its own tab and window; the page never called the shell for them
|
|
* notifications the page does not use the shell for them; the host sends the one desktop notification it needs (notify-send)
|
|
* app updater NOT available: Tauri's updater lives in the window process. Settings says so in plain words and the
|
|
* engine and component updates (Strata Control) still work.
|
|
*/
|
|
import { execFile, spawn } from 'node:child_process';
|
|
import { externalEnvironment } from './external-env.mjs';
|
|
import { createAppUpdater } from './app-updater.mjs';
|
|
|
|
export const APP_UPDATE_UNAVAILABLE = 'app-update-unavailable';
|
|
export const BROWSER_UPDATE_MESSAGE = "The Strata app itself can't be updated in browser mode. Start Strata from its icon, without --browser, to update it.";
|
|
const BRIDGE_URL = /^http:\/\/127\.0\.0\.1:\d+$/;
|
|
const MODES = new Set(['window', 'browser']);
|
|
|
|
/** Browsers and openers to try, in order, when the address of the page is opened for the person. */
|
|
export const OPENERS = [['xdg-open'], ['gio', 'open'], ['sensible-browser'], ['x-www-browser'], ['firefox'], ['chromium'], ['google-chrome']];
|
|
|
|
/**
|
|
* Start one opener and say whether it took the address: false when the program is missing or ends with an error within a couple of
|
|
* seconds. xdg-open often stays running until the browser closes, so a program that is still running counts as success.
|
|
*/
|
|
export function startOpener(command, args, { spawnProcess = spawn, env = externalEnvironment(), settleMs = 2500 } = {}) {
|
|
return new Promise(resolve => {
|
|
let child;
|
|
try { child = spawnProcess(command, args, { detached: true, stdio: 'ignore', env }); } catch { return resolve(false); }
|
|
let done = false;
|
|
const finish = value => { if (!done) { done = true; clearTimeout(timer); resolve(value); } };
|
|
const timer = setTimeout(() => { try { child.unref?.(); } catch { /* ignore */ } finish(true); }, settleMs);
|
|
child.on('error', () => finish(false));
|
|
child.on('exit', code => finish(code === 0));
|
|
});
|
|
}
|
|
|
|
export async function openInBrowser(url, { openers = OPENERS, start = startOpener } = {}) {
|
|
for (const [command, ...rest] of openers) if (await start(command, [...rest, url])) return true;
|
|
return false;
|
|
}
|
|
|
|
/** One desktop notification (notify-send), best effort: no notification daemon or no notify-send is not an error. */
|
|
export function sendNotification(message, { run = execFile, env = externalEnvironment() } = {}) {
|
|
return new Promise(resolve => {
|
|
try { run('notify-send', ['--app-name=Strata', '--icon=dialog-information', 'Strata', message], { env, timeout: 5000 }, error => resolve(!error)); }
|
|
catch { resolve(false); }
|
|
});
|
|
}
|
|
|
|
export function createAppUi({ env = process.env, shutdown = () => {}, open = openInBrowser, notify = sendNotification, createUpdater = createAppUpdater } = {}) {
|
|
let mode = MODES.has(env.STRATA_UI_MODE) ? env.STRATA_UI_MODE : null;
|
|
let reason = mode === 'browser' ? (env.STRATA_UI_REASON || 'chosen') : null;
|
|
let bridge = null;
|
|
const originalBridge = createUpdater({ env }); // the older way: the shell passed the bridge in the environment
|
|
|
|
/** The shell's updater as the rest of the host sees it, or null when no app started this host. */
|
|
const updater = mode === null && !originalBridge ? null : {
|
|
get unavailable() { return mode === 'browser' ? BROWSER_UPDATE_MESSAGE : null; },
|
|
async check() { return (await current()).check(); },
|
|
async install(options) { return (await current()).install(options); },
|
|
};
|
|
async function current() {
|
|
if (bridge) return bridge;
|
|
if (originalBridge) return originalBridge;
|
|
throw Object.assign(new Error(mode === 'browser' ? BROWSER_UPDATE_MESSAGE : 'The app updater is not ready yet. Try again in a moment.'), { code: APP_UPDATE_UNAVAILABLE });
|
|
}
|
|
|
|
return {
|
|
updater,
|
|
get mode() { return mode; },
|
|
/** What the page reads to decide what to show. */
|
|
state: () => ({ mode, reason, canQuit: mode === 'browser', appUpdate: mode === 'browser' || mode === null ? 'unavailable' : 'available' }),
|
|
/** The window process hands over its updater bridge (a loopback address and a token). */
|
|
setBridge({ url, token } = {}) {
|
|
if (typeof url !== 'string' || !BRIDGE_URL.test(url) || typeof token !== 'string' || token.length < 16) throw new Error('That is not a bridge address.');
|
|
bridge = createUpdater({ env: { STRATA_SHELL_BRIDGE: url, STRATA_SHELL_TOKEN: token } });
|
|
return true;
|
|
},
|
|
/** Open the page in the default browser without changing the mode (a second start while Strata runs). */
|
|
openPage: url => open(url),
|
|
/** The shell moves everyone to a browser tab: the window could not start or has stopped. */
|
|
async switchToBrowser({ url, reason: why = 'window-failed', notice = '', openPage = true } = {}) {
|
|
mode = 'browser'; reason = String(why).slice(0, 40); bridge = null;
|
|
const opened = openPage && url ? await open(url) : false;
|
|
if (notice) void notify(notice);
|
|
return { mode, reason, opened };
|
|
},
|
|
/** The "Quit Strata" menu entry: only in browser mode, where closing the tab leaves the engine running. */
|
|
quit() {
|
|
if (mode !== 'browser') return { ok: false, status: 409, error: 'Close the Strata window to quit.' };
|
|
setImmediate(() => void shutdown());
|
|
return { ok: true, status: 202 };
|
|
},
|
|
};
|
|
}
|