Files
strata/apps/host/server/open-text-file.mjs
Niko1221 9319219623 Expert settings: Open settings file (the model's run config in the system text editor), and a plain check of it before every start
The per-model settings already live in one text file (strata-<model>.json: args, env, sampling and the
named keys). Expert settings gets an Open settings file button (Notepad, open -t, or the system's
plain-text program) with the note that changes apply on the next start. The file is checked when the
panel opens, when the window comes back into focus, and before a load: bad JSON, a wrong value, an
unknown key (the nearest real name is offered) are said in plain words with the line, in 8 languages.
A mistake holds the start back instead of an engine that dies on it; an unknown key only warns.
2026-10-18 12:59:05 +02:00

39 lines
2.0 KiB
JavaScript

/** Open a file in the person's text editor (not the program a .json happens to belong to: that can be a browser). */
import { execFile } from 'node:child_process';
import { launch } from './chat-files.mjs';
import { externalEnvironment } from './external-env.mjs';
/** The command that opens `file` as text on this system, or null when only the file's own program is known. */
export async function textEditorCommand(file, { platform = process.platform, ask = askSystem } = {}) {
if (platform === 'win32') return ['notepad.exe', [file]];
if (platform === 'darwin') return ['open', ['-t', file]];
// Linux: the program the system opens plain text with (gedit, kate, mousepad, ...), through its desktop entry.
const entry = (await ask('xdg-mime', ['query', 'default', 'text/plain'])).trim();
if (/^[\w.+-]+\.desktop$/.test(entry)) {
const gtk = (await ask('sh', ['-c', 'command -v gtk-launch'])).trim();
if (gtk) return [gtk, [entry, file]];
const gio = (await ask('sh', ['-c', 'command -v gio'])).trim();
if (gio) {
for (const dir of ['/usr/share/applications', '/usr/local/share/applications', `${process.env.HOME ?? ''}/.local/share/applications`]) {
const path = `${dir}/${entry}`;
if ((await ask('test', ['-f', path], true)) === 'yes') return [gio, ['launch', path, file]];
}
}
}
return null;
}
function askSystem(command, args, yesNo = false) {
return new Promise(resolve => {
execFile(command, args, { timeout: 3000, env: externalEnvironment() }, (error, stdout) => resolve(yesNo ? (error ? 'no' : 'yes') : (error ? '' : String(stdout))));
});
}
/** Start the editor and return at once. Falls back to the file's own program (xdg-open) when no text editor is known. */
export async function openTextFile(file, { platform = process.platform, ask, recorder = null } = {}) {
const found = await textEditorCommand(file, { platform, ask });
const [command, args] = found ?? ['xdg-open', [file]];
launch(command, args, { recorder });
return { command, args };
}