Files
leleka/engine/strata/apps/host/server/agent/tools-settings.mjs
T
Pavlo RatushnyiandClaude Sonnet 5.5 87e2633afe engine/strata: bring the Strata subtree to upstream 41cf05a0 (engine 0.1.50) with the local patches re-applied
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>
2026-10-25 12:49:15 +01:00

219 lines
12 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* What the person chose in Settings -> Tools, and the single place that validates it.
*
* Saved (inside the host's settings file): which tool groups are off, which web search service to use (and its key, which never leaves
* this file: the screen only learns whether one is saved), whether the browser tool is on, and the person's own tools.
*
* The profile builder reads `profileView()`; the screen reads `view()`. Custom command and instruction tools are served by the app's
* own small tool server (custom-tools-mcp.mjs) from a JSON file this module writes, so they appear to the assistant as ordinary tools:
* `mcp__strata-instructions__<name>` (look-only: it just returns the person's instructions) and `mcp__strata-commands__<name>`
* (runs the person's command; asks first in Manual and is refused in Plan). A person's own tool server (a web address or a program)
* is added as `mcp__custom-<name>__...` and asks first in Manual like any connected tool.
*/
import { mkdirSync, renameSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { GROUP_IDS, TOOL_GROUPS } from './tools-catalog.mjs';
import { SHELLS, clampTimeoutSec } from './custom-command-run.mjs';
export const SEARCH_PROVIDERS = ['builtin', 'exa', 'perplexity', 'deepseek'];
const KINDS = ['instructions', 'command', 'server'];
const NAME = /^[A-Za-z][A-Za-z0-9 _-]{0,39}$/;
const MAX_CUSTOM = 40;
export const INSTRUCTIONS_SERVER = 'strata-instructions';
export const COMMANDS_SERVER = 'strata-commands';
const slug = value => String(value).toLowerCase().replace(/[^a-z0-9]+/g, '_').replace(/^_+|_+$/g, '').slice(0, 40) || 'tool';
const clean = (value, max) => String(value ?? '').replace(/\0/g, '').slice(0, max);
function normalize(raw) {
const r = raw && typeof raw === 'object' ? raw : {};
const search = r.search && typeof r.search === 'object' ? r.search : {};
return {
off: [...new Set((Array.isArray(r.off) ? r.off : []).filter(id => GROUP_IDS.includes(id)))],
search: { provider: SEARCH_PROVIDERS.includes(search.provider) ? search.provider : 'builtin', key: typeof search.key === 'string' ? search.key : '' },
browser: { enabled: r.browser?.enabled === true },
// Manual mode asks once per chat before the first web search or page. This switch (off unless the person turned it on) skips that question.
webWithoutAsking: r.webWithoutAsking === true,
custom: (Array.isArray(r.custom) ? r.custom : []).filter(t => t && NAME.test(t.name ?? '') && KINDS.includes(t.kind)).slice(0, MAX_CUSTOM).map(t => ({
id: String(t.id), name: String(t.name), about: clean(t.about, 300), kind: t.kind, enabled: t.enabled !== false,
instructions: clean(t.instructions, 20000), command: clean(t.command, 2000), address: clean(t.address, 2000),
args: (Array.isArray(t.args) ? t.args : []).slice(0, 50).map(a => clean(a, 1000)),
// a command tool's folder, shell and time allowed (custom-command-run.mjs); '' / 'auto' / 120 when not set
cwd: clean(t.cwd, 1000).trim(), shell: SHELLS.includes(t.shell) ? t.shell : 'auto', timeoutSec: clampTimeoutSec(t.timeoutSec),
})),
};
}
function validateCustom(body, existing) {
const name = String(body?.name ?? '').trim();
if (!NAME.test(name)) throw new Error('Give the tool a short name: letters, numbers, spaces or hyphens.');
const about = clean(body?.about, 300).trim();
if (!about) throw new Error('Say what the tool does, in a sentence. The assistant reads this to decide when to use it.');
const kind = body?.kind;
if (!KINDS.includes(kind)) throw new Error('Choose how the tool works.');
const out = { name, about, kind, instructions: '', command: '', address: '', args: [], cwd: '', shell: 'auto', timeoutSec: clampTimeoutSec(undefined) };
if (kind === 'instructions') {
out.instructions = clean(body.instructions, 20000).trim();
if (!out.instructions) throw new Error('Write the instructions the assistant should follow when it uses this tool.');
} else if (kind === 'command') {
out.command = clean(body.command, 2000).trim();
if (!out.command) throw new Error('Enter the command to run.');
if (/[\r\n]/.test(out.command)) throw new Error('Put the command on one line.');
// The edit form of Settings does not send these three: an edit keeps what the tool had.
const before = existing.find(t => t.id === body.id && t.kind === 'command');
out.cwd = body.cwd !== undefined ? clean(body.cwd, 1000).trim() : before?.cwd ?? '';
if (/[\r\n]/.test(out.cwd)) throw new Error('Put the folder on one line.');
if (body.shell !== undefined && !SHELLS.includes(body.shell)) throw new Error('Choose how the command runs: automatic, cmd, PowerShell, bash or sh.');
out.shell = body.shell !== undefined ? body.shell : before?.shell ?? 'auto';
if (body.timeoutSec !== undefined && !(Number(body.timeoutSec) >= 1 && Number(body.timeoutSec) <= 3600)) throw new Error('Give the time allowed in seconds, from 1 to 3600.');
out.timeoutSec = body.timeoutSec !== undefined ? clampTimeoutSec(body.timeoutSec) : before?.timeoutSec ?? clampTimeoutSec(undefined);
} else {
out.address = clean(body.address, 2000).trim();
if (!out.address) throw new Error('Enter the tool server’s web address, or the program that starts it.');
if (/[\r\n]/.test(out.address)) throw new Error('Put the address or program on one line.');
if (/^https?:\/\//i.test(out.address)) {
let url; try { url = new URL(out.address); } catch { throw new Error('That web address is not valid.'); }
if (url.username || url.password) throw new Error('Use a web address without a user name or password in it.');
}
out.args = (Array.isArray(body.args) ? body.args : []).slice(0, 50).map(a => clean(a, 1000)).filter(Boolean);
}
if (existing.some(t => t.name.toLowerCase() === name.toLowerCase() && t.id !== body.id)) throw new Error('You already have a tool with that name.');
return out;
}
const idFor = (name, existing) => {
const base = slug(name);
let id = base, n = 2;
while (existing.some(t => t.id === id)) id = `${base}_${n++}`;
return id;
};
/**
* @param {{read: () => object|undefined, write: (value: object) => void, dataDir: string, browser?: () => {found: boolean, name: string|null, path?: string|null, cli?: string|null}}} deps
*/
export function createAgentTools({ read, write, dataDir, browser = () => ({ found: false, name: null }) }) {
const state = () => normalize(read());
const filePath = join(dataDir, 'agent', 'custom-tools.json');
/** The file the app's own tool server reads: instruction tools and command tools that are on. */
function writeToolFile(s) {
const instructions = {}, commands = {};
for (const t of s.custom.filter(t => t.enabled)) {
const key = t.id;
if (t.kind === 'instructions') instructions[key] = { description: t.about, text: t.instructions };
else if (t.kind === 'command') commands[key] = { description: t.about, command: t.command, cwd: t.cwd, shell: t.shell, timeoutSec: t.timeoutSec };
}
mkdirSync(join(dataDir, 'agent'), { recursive: true });
const tmp = `${filePath}.tmp`;
writeFileSync(tmp, JSON.stringify({ instructions, commands }, null, 1));
renameSync(tmp, filePath);
return { instructions: Object.keys(instructions).length, commands: Object.keys(commands).length };
}
const save = s => { write(s); writeToolFile(s); };
function view() {
const s = state();
const probe = browser();
return {
groups: TOOL_GROUPS.map(g => ({ id: g.id, name: g.name, about: g.about, area: g.area, asks: g.asks, enabled: !s.off.includes(g.id) })),
search: { provider: s.search.provider, keySet: !!s.search.key },
webWithoutAsking: s.webWithoutAsking,
// `found` means the browser tool can run: a browser on this computer AND the tool server in the agent component.
browser: { enabled: s.browser.enabled, found: !!(probe.found && probe.cli), name: probe.name ?? null, ...(probe.found && !probe.cli ? { missing: 'component' } : {}) },
custom: s.custom.map(t => ({
id: t.id, name: t.name, about: t.about, kind: t.kind, enabled: t.enabled,
summary: t.kind === 'instructions' ? t.instructions.slice(0, 80) : t.kind === 'command' ? t.command : t.address,
instructions: t.instructions, command: t.command, address: t.address, args: t.args,
...(t.kind === 'command' ? { cwd: t.cwd, shell: t.shell, timeoutSec: t.timeoutSec } : {}),
})),
};
}
return {
view,
filePath,
/** For the profile builder: plain values, secrets included. Never sent to the screen. */
profileView() {
const s = state();
const custom = s.custom.filter(t => t.enabled);
const servers = {};
for (const t of custom.filter(t => t.kind === 'server')) {
const name = `custom-${t.id.replace(/_/g, "-")}`.slice(0, 32);
servers[name] = /^https?:\/\//i.test(t.address) ? { url: t.address } : { command: t.address, args: t.args };
}
return {
off: new Set(s.off),
search: s.search,
webWithoutAsking: s.webWithoutAsking,
browserEnabled: s.browser.enabled && !!(browser().found && browser().cli),
browserPath: browser().path ?? null,
browserCli: browser().cli ?? null,
servers,
hasInstructionTools: custom.some(t => t.kind === 'instructions'),
hasCommandTools: custom.some(t => t.kind === 'command'),
toolFile: filePath,
};
},
update(change) {
const s = state();
if (change?.groups) {
for (const [id, on] of Object.entries(change.groups)) {
if (!GROUP_IDS.includes(id)) throw new Error('That tool is not known.');
s.off = on === false ? [...new Set([...s.off, id])] : s.off.filter(x => x !== id);
}
}
if (change?.search) {
const provider = change.search.provider;
if (provider !== undefined) {
if (!SEARCH_PROVIDERS.includes(provider)) throw new Error('Choose one of the listed search services.');
if (provider !== s.search.provider) s.search = { provider, key: '' }; // a key belongs to its service
}
if (change.search.key !== undefined) {
const key = String(change.search.key).trim();
if (!key || key.length > 500 || /[\s\0]/.test(key)) throw new Error('That key does not look right. Paste it again without spaces.');
s.search = { ...s.search, key };
}
}
if (typeof change?.webWithoutAsking === 'boolean') s.webWithoutAsking = change.webWithoutAsking;
if (change?.browser && typeof change.browser.enabled === 'boolean') s.browser = { enabled: change.browser.enabled };
if (change?.custom) {
for (const [id, on] of Object.entries(change.custom)) {
const t = s.custom.find(x => x.id === id);
if (!t) throw new Error('That tool is not in your list.');
t.enabled = on !== false;
}
}
save(s);
return view();
},
addCustom(body) {
const s = state();
if (s.custom.length >= MAX_CUSTOM) throw new Error(`You can have up to ${MAX_CUSTOM} tools of your own.`);
const t = validateCustom(body, s.custom);
s.custom.push({ id: idFor(t.name, s.custom), enabled: true, ...t });
save(s);
return view();
},
editCustom(id, body) {
const s = state();
const at = s.custom.findIndex(t => t.id === id);
if (at < 0) throw new Error('That tool is not in your list.');
const t = validateCustom({ ...body, id }, s.custom);
s.custom[at] = { ...s.custom[at], ...t };
save(s);
return view();
},
removeCustom(id) {
const s = state();
if (!s.custom.some(t => t.id === id)) throw new Error('That tool is not in your list.');
s.custom = s.custom.filter(t => t.id !== id);
save(s);
return view();
},
/** Make sure the tool server's file exists (host start). */
ensureFile() { writeToolFile(state()); },
};
}