Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
Settings Tools behind the screen: tool groups on/off reach the agent (restart on change), web search with no key (DuckDuckGo, polite, cached) or the person's own Exa/Perplexity/DeepSeek key in the environment only, the person's own tools (instructions, commands, tool servers) through the app's tool server, Plan refuses connected tools, keyless API connections work in the agent; browser finder
Niko122124e0157b5327
diff --git a/apps/host/scripts/agent2-flow.mjs b/apps/host/scripts/agent2-flow.mjsnew file mode 100644index 000000000..01f82ec2d--- /dev/null+++ b/apps/host/scripts/agent2-flow.mjs@@ -0,0 +1,180 @@+/**+ * The three slash commands through the real app on a real model, with screenshots (playwright-core + installed Chrome).+ *+ * STRATA_URL=http://127.0.0.1:8899 SHOTS=./shots WORKDIR=<the host's workspace> MODEL='Qwen test' node scripts/agent2-flow.mjs+ *+ * Steps (each prints ok/FAIL):+ * 1. typing "/" lists /time-skip, /goal and /loop with plain descriptions;+ * 2. /loop 1m <message>: the first answer arrives, the loop repeats it a minute later, /loop shows it, /loop stop ends it;+ * 3. /goal <objective>: the assistant works until the goal is complete (Manual mode: Approve cards are clicked), /goal shows it;+ * 4. /time-skip <request>: a job runs on a copy, the sidebar lists the changed file, Keep the update changes the file in WORKDIR,+ * Put the original back restores it.+ * Environment: STRATA_URL (required), SHOTS, WORKDIR, MODEL (picker text), CHROME, SKIP (comma list of steps to skip: menu,loop,goal,timeskip),+ * LOOP_WAIT_MS (how long to wait for the second loop run, default 150000).+ */+import { chromium } from 'playwright-core';+import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';+import { join, resolve } from 'node:path';++const BASE = process.env.STRATA_URL;+if (!BASE) { console.error('STRATA_URL is required'); process.exit(2); }+const SHOTS = resolve(process.env.SHOTS ?? 'agent2-shots');+mkdirSync(SHOTS, { recursive: true });+const WORKDIR = process.env.WORKDIR;+const MODEL = process.env.MODEL ?? 'Qwen test';+const SKIP = new Set((process.env.SKIP ?? '').split(',').filter(Boolean));+const CHROME = process.env.CHROME ?? ['C:/Program Files/Google/Chrome/Application/chrome.exe', '/usr/bin/google-chrome'].find(existsSync);++let failed = false;+const check = (ok, label) => { console.log(`${ok ? 'ok ' : 'FAIL'} ${label}`); if (!ok) failed = true; };+const browser = await chromium.launch({ executablePath: CHROME, headless: true });+const page = await browser.newPage({ viewport: { width: 1512, height: 1000 } });+const problems = [];+page.on('pageerror', e => problems.push(`pageerror: ${e.message}`));+page.on('console', m => { if (m.type() === 'error') problems.push(`console: ${m.text().slice(0, 200)}`); });+const shot = async name => { await page.waitForTimeout(400); await page.screenshot({ path: join(SHOTS, `${name}.png`) }); console.log(`shot ${name}.png`); };+const send = async text => { await page.locator('textarea').first().fill(text); await page.getByRole('button', { name: 'Send' }).click(); };+const idle = async (ms = 900000) => page.waitForFunction(() => document.documentElement.dataset.streaming !== 'true', null, { timeout: ms });+const started = async () => page.waitForFunction(() => document.documentElement.dataset.streaming === 'true', null, { timeout: 30000 });+const text = () => page.evaluate(() => document.querySelector('.chat')?.innerText ?? document.body.innerText);++/** Approve every card that asks until the turn is over. */+async function approveUntilIdle(label) {+ let asked = 0; const seen = new Set();+ for (;;) {+ const streaming = await page.evaluate(() => document.documentElement.dataset.streaming === 'true');+ const cards = page.locator('.toolcard[data-state="proposed"]');+ const n = await cards.count();+ for (let i = 0; i < n; i++) {+ const card = cards.nth(i);+ const key = (await card.innerText()).slice(0, 200);+ if (!seen.has(key)) { seen.add(key); asked++; await shot(`${label}-approve-${asked}`); }+ await card.getByRole('button', { name: 'Approve' }).click().catch(() => {});+ }+ if (!streaming && n === 0) break;+ await page.waitForTimeout(400);+ }+ return asked;+}++try {+ await page.goto(`${BASE}/#/chat`);+ await page.waitForTimeout(2000);+ const skip = page.getByText('Go straight to the app');+ if (await skip.count()) { await skip.click(); await page.waitForTimeout(600); }+ await page.getByRole('button', { name: 'New chat' }).first().click();+ await page.waitForTimeout(600);+ await page.getByRole('button', { name: /Choose model/ }).first().click();+ await page.waitForTimeout(300);+ await page.getByRole('menuitemradio', { name: new RegExp(MODEL) }).first().click();+ await page.waitForTimeout(300);++ // 1. the menu+ if (!SKIP.has('menu')) {+ await page.locator('textarea').first().fill('/');+ await page.waitForSelector('.slashmenu', { timeout: 5000 });+ const menu = await page.locator('.slashmenu').innerText();+ await shot('01-slash-menu');+ check(/\/time-skip/.test(menu) && /Try a change on a copy/.test(menu), '/time-skip is listed with its plain description');+ check(/\/goal/.test(menu) && /keep working until it is done/.test(menu), '/goal is listed with its plain description');+ check(/\/loop/.test(menu) && /every few minutes/.test(menu), '/loop is listed with its plain description');+ await page.locator('textarea').first().fill('/lo');+ await shot('01b-slash-menu-filtered');+ await page.locator('textarea').first().fill('');+ }++ // 2. /loop+ if (!SKIP.has('loop')) {+ await send('/loop 1m Reply with exactly three words about the sea.');+ await page.waitForTimeout(1500);+ await shot('02a-loop-started');+ let body = await text();+ check(/Loop started: .Reply with exactly three words about the sea\.. every minute/.test(body), 'a note says the loop started and how often');+ await started().catch(() => {});+ await idle();+ await shot('02b-loop-first-answer');+ const firstCount = (await text()).split('Reply with exactly three words about the sea.').length - 1;+ const t0 = Date.now();+ const wait = Number(process.env.LOOP_WAIT_MS ?? 150000);+ await page.waitForFunction(n => (document.querySelector('.chat')?.innerText ?? '').split('Reply with exactly three words about the sea.').length - 1 > n, firstCount, { timeout: wait }).catch(() => {});+ await idle();+ await shot('02c-loop-second-run');+ const secondCount = (await text()).split('Reply with exactly three words about the sea.').length - 1;+ check(secondCount > firstCount, `the loop sent the message again on its own (${Math.round((Date.now() - t0) / 1000)} s after the first answer)`);+ await send('/loop');+ await page.waitForTimeout(800);+ check(/A loop is running in this chat/.test(await text()), '/loop says a loop is running');+ await send('/loop stop');+ await page.waitForTimeout(800);+ await shot('02d-loop-stopped');+ check(/The loop is stopped/.test(await text()), '/loop stop says it is stopped');+ const afterStop = (await text()).split('Reply with exactly three words about the sea.').length - 1;+ await page.waitForTimeout(75000);+ await idle();+ check((await text()).split('Reply with exactly three words about the sea.').length - 1 === afterStop, 'nothing more was sent after stopping');+ }++ // 3. /goal+ if (!SKIP.has('goal')) {+ await page.getByRole('button', { name: 'New chat' }).first().click();+ await page.waitForTimeout(600);+ await page.getByRole('button', { name: /Choose model/ }).first().click();+ await page.waitForTimeout(300);+ await page.getByRole('menuitemradio', { name: new RegExp(MODEL) }).first().click();+ await page.waitForTimeout(300);+ await send('/goal Create a file called goal-done.txt in the current folder that contains the single word done, then confirm it exists.');+ await started().catch(() => {});+ const asked = await approveUntilIdle('03');+ await idle();+ await shot('03a-goal-finished');+ const body = await text();+ check(!!WORKDIR && existsSync(join(WORKDIR, 'goal-done.txt')), 'the goal produced goal-done.txt');+ check(/goal is complete|goal-done\.txt/i.test(body), 'the chat says the goal is complete or names the file');+ console.log(`approval cards: ${asked}`);+ await send('/goal');+ await page.waitForTimeout(1500);+ await shot('03b-goal-status');+ }++ // 4. /time-skip+ if (!SKIP.has('timeskip')) {+ await page.getByRole('button', { name: 'New chat' }).first().click();+ await page.waitForTimeout(600);+ await page.getByRole('button', { name: /Choose model/ }).first().click();+ await page.waitForTimeout(300);+ await page.getByRole('menuitemradio', { name: new RegExp(MODEL) }).first().click();+ await page.waitForTimeout(300);+ const notes = WORKDIR ? join(WORKDIR, 'notes.md') : null;+ const before = notes && existsSync(notes) ? readFileSync(notes, 'utf8') : null;+ await send('/time-skip Add a second line to notes.md that says: second line added.');+ await page.waitForTimeout(2500);+ await shot('04a-timeskip-working');+ await page.waitForFunction(() => /Ready to compare|Update kept|Saved version kept|could not|stopped/i.test(document.querySelector('#time-skip-content')?.innerText ?? ''), null, { timeout: 1200000 }).catch(() => {});+ await shot('04b-timeskip-ready');+ const panel = await page.locator('#time-skip-content').innerText().catch(() => '');+ check(/Ready to compare/.test(panel), 'the sidebar says the result is ready to compare');+ check(/notes\.md/.test(panel), 'the sidebar names the changed file');+ check(!notes || readFileSync(notes, 'utf8') === before, 'your file is untouched until you choose');+ await page.getByRole('button', { name: 'Compare' }).first().click().catch(() => {});+ await page.waitForTimeout(800);+ await shot('04c-timeskip-compare');+ await page.getByRole('button', { name: /Back to chat|Done comparing/ }).first().click().catch(() => {});+ await page.getByRole('button', { name: 'Keep the update' }).first().click();+ await page.waitForTimeout(4000);+ await shot('04d-timeskip-kept');+ check(!notes || /second line added/i.test(readFileSync(notes, 'utf8')), 'Keep the update put the revision in your file');+ await page.getByRole('button', { name: 'Put the original back' }).first().click();+ await page.waitForTimeout(4000);+ await shot('04e-timeskip-restored');+ check(!notes || readFileSync(notes, 'utf8') === before, 'Put the original back restored your file');+ }+} catch (e) {+ console.log('ERROR', e.message);+ await shot('zz-error').catch(() => {});+ failed = true;+} finally {+ const text = problems.filter(p => !/Failed to load resource|ERR_|favicon/.test(p));+ console.log(`page problems: ${text.length}`); text.slice(0, 8).forEach(p => console.log(' ' + p));+ await browser.close();+}+process.exit(failed ? 1 : 0);diff --git a/apps/host/server/agent/agent-bridge.mjs b/apps/host/server/agent/agent-bridge.mjsindex e6b2a0a0b..79f62ab2a 100644--- a/apps/host/server/agent/agent-bridge.mjs+++ b/apps/host/server/agent/agent-bridge.mjs@@ -54,7 +54,7 @@ const deferred = () => { let resolve; const promise = new Promise(r => { resolve* @param {string} [options.nodePath] the Node runtime that runs the Harness* @param {(line:string)=>void} [options.log]*/-export function createAgentBridge({ dataDir, componentsDir = null, bundledDir = null, gate = true, localInstances, connections, env = process.env, nodePath = process.execPath, log = () => {}, startTimeoutMs = 120_000 }) {+export function createAgentBridge({ dataDir, componentsDir = null, bundledDir = null, gate = true, localInstances, connections, tools = () => undefined, env = process.env, nodePath = process.execPath, log = () => {}, startTimeoutMs = 120_000 }) {const root = join(dataDir, 'agent');const home = join(root, 'home');const sessionsFile = join(root, 'sessions.json');@@ -97,7 +97,7 @@ export function createAgentBridge({ dataDir, componentsDir = null, bundledDir =function status() {const h = harnessInfo();if (!h.ok) return { state: 'unavailable', reason: 'The coding assistant isn’t installed on this computer yet. Chat works without it.', detail: h.reason, code: 'component-missing' };- const profile = buildProfile({ local: localInstances(), connections: connections(), gate });+ const profile = buildProfile({ local: localInstances(), connections: connections(), gate, tools: tools() });return {state: rt?.hp.running ? 'ready' : starting ? 'starting' : lastError ? 'error' : 'idle',reason: lastError ? plainAgentFailure(lastError) === lastError ? 'The coding assistant could not start. Try again.' : plainAgentFailure(lastError) : null,@@ -145,14 +145,14 @@ export function createAgentBridge({ dataDir, componentsDir = null, bundledDir =async function ensureRuntime(choice, signal) {for (;;) {if (starting) { await starting.catch(() => {}); continue; }- const profile = buildProfile({ local: localInstances(), connections: connections(), gate });+ const profile = buildProfile({ local: localInstances(), connections: connections(), gate, tools: tools() });const route = routeFor(profile, choice);if (!route) {throw new AgentUnavailable(choice?.connectionId? 'This API model is no longer available. Choose another model in the chat picker.': 'No local model is ready for the agent. Load a model in Models, or choose an API model.', 'model-unavailable');}- const known = rt?.hp.running && rt.profile.routes.some(r => r.route === route.route && r.modelId === route.modelId && r.fp === route.fp);+ const known = rt?.hp.running && rt.profile.toolsSignature === profile.toolsSignature && rt.profile.routes.some(r => r.route === route.route && r.modelId === route.modelId && r.fp === route.fp);if (known) return { rt, route };starting = (async () => {if (rt?.hp.running) {@@ -517,6 +517,7 @@ export function createAgentBridge({ dataDir, componentsDir = null, bundledDir =onFrame({ type: 'done', finishReason: 'stop', tokensIn: null, tokensOut: null, usageSource: 'unavailable', durationMs: 0 });return { goal: true };};+ if (tools()?.off?.has('goals')) return say('Goals are turned off. You can turn them back on in Settings, Tools.');const known = sessionMap[chatId]?.sessionId;const quick = ['', 'clear', 'pause', 'edit', 'complete'].includes(verb) || (verb === 'status');let runtime = rt;diff --git a/apps/host/server/agent/browser-probe.mjs b/apps/host/server/agent/browser-probe.mjsnew file mode 100644index 000000000..ebd3b0adf--- /dev/null+++ b/apps/host/server/agent/browser-probe.mjs@@ -0,0 +1,45 @@+/**+ * Finds a browser already installed on this computer (Chrome, then Edge, then Chromium) for the assistant's browser tool, so nothing+ * big has to be downloaded. Returns the program's path and the name the screen shows.+ */+import { existsSync } from 'node:fs';+import { join } from 'node:path';+import { spawnSync } from 'node:child_process';++/** @returns {Array<{name: string, path: string}>} candidate programs for a platform, best first */+export function browserCandidates(platform = process.platform, env = process.env) {+ if (platform === 'win32') {+ const roots = [env.PROGRAMFILES, env['PROGRAMFILES(X86)'], env.LOCALAPPDATA].filter(Boolean);+ const at = rel => roots.map(r => join(r, rel));+ return [+ ...at('Google\\Chrome\\Application\\chrome.exe').map(path => ({ name: 'Google Chrome', path })),+ ...at('Microsoft\\Edge\\Application\\msedge.exe').map(path => ({ name: 'Microsoft Edge', path })),+ ...at('Chromium\\Application\\chrome.exe').map(path => ({ name: 'Chromium', path })),+ ];+ }+ if (platform === 'darwin') {+ return [+ { name: 'Google Chrome', path: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' },+ { name: 'Microsoft Edge', path: '/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge' },+ { name: 'Chromium', path: '/Applications/Chromium.app/Contents/MacOS/Chromium' },+ ];+ }+ return [+ ...['/usr/bin/google-chrome', '/usr/bin/google-chrome-stable', '/opt/google/chrome/chrome'].map(path => ({ name: 'Google Chrome', path })),+ ...['/usr/bin/microsoft-edge', '/usr/bin/microsoft-edge-stable', '/opt/microsoft/msedge/msedge'].map(path => ({ name: 'Microsoft Edge', path })),+ ...['/usr/bin/chromium', '/usr/bin/chromium-browser', '/snap/bin/chromium'].map(path => ({ name: 'Chromium', path })),+ ];+}++/** @returns {{found: boolean, name: string|null, path: string|null}} */+export function findBrowser({ platform = process.platform, env = process.env, exists = existsSync } = {}) {+ const hit = browserCandidates(platform, env).find(c => exists(c.path));+ if (hit) return { found: true, name: hit.name, path: hit.path };+ if (platform === 'linux') {+ for (const [name, bin] of [['Google Chrome', 'google-chrome'], ['Microsoft Edge', 'microsoft-edge'], ['Chromium', 'chromium']]) {+ const r = spawnSync('which', [bin], { encoding: 'utf8' });+ if (r.status === 0 && r.stdout.trim()) return { found: true, name, path: r.stdout.trim() };+ }+ }+ return { found: false, name: null, path: null };+}diff --git a/apps/host/server/agent/custom-tools-mcp.mjs b/apps/host/server/agent/custom-tools-mcp.mjsnew file mode 100644index 000000000..93cb40f6f--- /dev/null+++ b/apps/host/server/agent/custom-tools-mcp.mjs@@ -0,0 +1,77 @@+#!/usr/bin/env node+/**+ * The app's own tool server for the tools a person writes in Settings -> Tools.+ *+ * node custom-tools-mcp.mjs instructions <tools.json> one tool per instruction tool: calling it returns the person's text+ * node custom-tools-mcp.mjs commands <tools.json> one tool per command tool: calling it runs the person's command+ *+ * It speaks the Model Context Protocol over stdio (newline-delimited JSON-RPC), the transport the Harness's MCP client uses for a program+ * it starts. The file is re-read on every request, so a change in Settings is seen without restarting anything. The assistant's text for+ * a command tool reaches the command in the STRATA_INPUT environment variable (never spliced into the command line, so it cannot add+ * commands of its own). A command gets 2 minutes and 20,000 characters of output.+ */+import { spawn } from 'node:child_process';+import { readFileSync } from 'node:fs';+import { createInterface } from 'node:readline';++const [mode, file] = process.argv.slice(2);+if (!['instructions', 'commands'].includes(mode) || !file) { console.error('usage: custom-tools-mcp.mjs instructions|commands <tools.json>'); process.exit(2); }++const load = () => { try { return JSON.parse(readFileSync(file, 'utf8'))[mode] ?? {}; } catch { return {}; } };+const OUT_MAX = 20000;+const TIME_MS = 120000;++const schema = mode === 'commands'+ ? { type: 'object', properties: { input: { type: 'string', description: 'Text to give the command (optional).' } }, additionalProperties: false }+ : { type: 'object', properties: {}, additionalProperties: false };++function listTools() {+ return Object.entries(load()).map(([name, t]) => ({ name, description: String(t.description ?? ''), inputSchema: schema, annotations: mode === 'instructions' ? { readOnlyHint: true } : {} }));+}++function runCommand(command, input) {+ return new Promise(resolve => {+ const child = spawn(command, { shell: true, windowsHide: true, env: { ...process.env, STRATA_INPUT: String(input ?? '') }, stdio: ['ignore', 'pipe', 'pipe'] });+ let text = '';+ const take = chunk => { if (text.length < OUT_MAX) text += chunk.toString(); };+ child.stdout.on('data', take); child.stderr.on('data', take);+ const timer = setTimeout(() => { try { child.kill(); } catch { /* gone */ } }, TIME_MS);+ child.on('error', e => { clearTimeout(timer); resolve({ ok: false, text: `The command could not start: ${e.message}` }); });+ child.on('close', (code, signal) => {+ clearTimeout(timer);+ const out = text.slice(0, OUT_MAX).trim();+ if (signal) resolve({ ok: false, text: `The command was stopped after ${TIME_MS / 1000} seconds.${out ? '\n' + out : ''}` });+ else resolve({ ok: code === 0, text: out || (code === 0 ? 'The command finished without any error. It printed nothing.' : `The command failed (exit code ${code}) and printed nothing.`) });+ });+ });+}++async function call(name, args) {+ const t = load()[name];+ if (!t) return { isError: true, content: [{ type: 'text', text: 'That tool is no longer available.' }] };+ if (mode === 'instructions') return { content: [{ type: 'text', text: String(t.text ?? '') }] };+ const r = await runCommand(String(t.command ?? ''), args?.input);+ return { isError: !r.ok, content: [{ type: 'text', text: r.text }] };+}++const send = message => process.stdout.write(JSON.stringify({ jsonrpc: '2.0', ...message }) + '\n');++async function handle(line) {+ let msg;+ try { msg = JSON.parse(line); } catch { return; }+ const { id, method, params } = msg;+ if (id === undefined) return; // notifications need no answer+ try {+ if (method === 'initialize') {+ return send({ id, result: { protocolVersion: params?.protocolVersion ?? '2024-11-05', capabilities: { tools: { listChanged: false } }, serverInfo: { name: `strata-${mode}`, version: '1.0.0' } } });+ }+ if (method === 'ping') return send({ id, result: {} });+ if (method === 'tools/list') return send({ id, result: { tools: listTools() } });+ if (method === 'tools/call') return send({ id, result: await call(String(params?.name ?? ''), params?.arguments ?? {}) });+ return send({ id, error: { code: -32601, message: `Method not found: ${method}` } });+ } catch (e) {+ return send({ id, error: { code: -32603, message: String(e?.message ?? e) } });+ }+}++createInterface({ input: process.stdin }).on('line', line => { void handle(line); }).on('close', () => process.exit(0));diff --git a/apps/host/server/agent/plugins/strata-gate.mjs b/apps/host/server/agent/plugins/strata-gate.mjsindex 0663d85d3..41ea3f3d6 100644--- a/apps/host/server/agent/plugins/strata-gate.mjs+++ b/apps/host/server/agent/plugins/strata-gate.mjs@@ -26,6 +26,10 @@ export const inject = ['approval', 'permissionPresets', 'sessions', 'tools'];/** Preset key the host writes for Manual (apps/host/server/agent/profile.mjs MODE_PRESETS). */export const MANUAL_PRESET = 'strata-manual';+/** Preset key the host writes for Plan (read-only, nothing may change). */+export const PLAN_PRESET = 'strata-plan';+/** The app's own look-only tool server (the person's instruction tools): it only hands back text. */+const LOOK_ONLY_SERVER = 'mcp__strata-instructions__';/** Tools that cannot change anything on this computer. Everything else asks. */export const READ_ONLY_TOOLS = new Set([@@ -111,10 +115,22 @@ export function manualDecision(exec) {if (tool === 'write') return { kind: 'ask', reason: `Create or replace the file ${clip(args.file_path ?? args.path, 200)}` };if (tool === 'edit') return { kind: 'ask', reason: `Change the file ${clip(args.file_path ?? args.path, 200)}` };if (tool === 'job_kill') return { kind: 'ask', reason: 'Stop a background task' };+ if (tool.startsWith(LOOK_ONLY_SERVER)) return null;if (tool.startsWith('mcp__')) return { kind: 'ask', reason: `Use the connected tool ${tool.split('__').slice(1).join(' / ')}` };return { kind: 'ask', reason: `Use the tool ${tool}` };}+/**+ * Plan mode changes nothing. A connected tool (a person's tool server, a command they wrote, the browser) is not under the sandbox, so+ * Plan refuses it instead of letting it run. The look-only instruction tools are fine.+ * @returns {{kind:'deny', reason:string}|null}+ */+export function planDecision(exec) {+ const tool = String(exec.name ?? '');+ if (!tool.startsWith('mcp__') || tool.startsWith(LOOK_ONLY_SERVER)) return null;+ return { kind: 'deny', reason: 'Plan mode only looks and changes nothing, so it cannot use this tool. Switch to Manual or Bypass to use it.' };+}+export function apply(ctx) {/** The preset a session is really working under: a child follows the chat that started it. */const effectivePreset = (session, depth = 0) => {@@ -131,6 +147,7 @@ export function apply(ctx) {if (!agent || exec.name === 'run_code') return next();let preset;try { preset = effectivePreset(agent.session); } catch { return next(); }+ if (preset === PLAN_PRESET) return planDecision(exec) ?? next();if (preset !== MANUAL_PRESET) return next();const gate = manualDecision(exec);if (!gate) return next();diff --git a/apps/host/server/agent/plugins/strata-search.mjs b/apps/host/server/agent/plugins/strata-search.mjsnew file mode 100644index 000000000..57d633348--- /dev/null+++ b/apps/host/server/agent/plugins/strata-search.mjs@@ -0,0 +1,22 @@+/**+ * Registers the app's web search with the Harness's web seam (`ctx.web`), in place of the Harness's own backends that all need a service key.+ *+ * config.provider 'duckduckgo' (default, no key), 'exa' or 'perplexity' (the person's own key, read from the environment variable+ * named by config.keyEnv, which the host sets; the key is never in the profile file).+ *+ * The profile pins `web.searchProvider` to the id registered here, so a second usable backend cannot make the choice ambiguous.+ * Plain ESM, loaded by absolute path like strata-gate.mjs; its only imports are the app's own search modules next to the host.+ */+import { createDuckDuckGoSearch } from '../../web-search-ddg.mjs';+import { createExaSearch, createPerplexitySearch } from '../../web-search-keyed.mjs';++export const name = 'strata-web-search';+export const inject = ['web'];++export function apply(ctx, config = {}) {+ const keyOf = () => (config.keyEnv ? process.env[config.keyEnv] ?? '' : '');+ const provider = config.provider === 'exa' ? createExaSearch({ key: keyOf })+ : config.provider === 'perplexity' ? createPerplexitySearch({ key: keyOf })+ : createDuckDuckGoSearch();+ ctx.web.registerSearchProvider(provider);+}diff --git a/apps/host/server/agent/preset.mjs b/apps/host/server/agent/preset.mjsindex 79eec15ff..3a213d070 100644--- a/apps/host/server/agent/preset.mjs+++ b/apps/host/server/agent/preset.mjs@@ -20,6 +20,8 @@* - The goal, skill, workflow and fork tools are kept (owner decision): the first message is slower, and the short plain system prompt below keeps it as small as it can be.*/+import { withoutDisabled } from './tools-catalog.mjs';+/** Plugin ids of the pinned Harness `standard` preset, in its order (the drift check). */export const STANDARD_PLUGIN_IDS = ['persona', 'agent-instructions', 'time-context', 'tool-bash', 'tool-pwsh', 'tool-fs', 'tool-fs-search', 'tool-jobs',@@ -48,7 +50,7 @@ export const AGENT_PROMPT = [* @param {{platform?: string, search?: boolean}} [options]* @returns {object} the `config` of the `preset-standard` row*/-export function standardPreset({ platform = process.platform, search = false } = {}) {+export function standardPreset({ platform = process.platform, search = false, off = new Set() } = {}) {const windows = platform === 'win32';const plugins = [{ id: 'persona', name: '@deepseek-ai/dsh-persona', config: { prefix: AGENT_PROMPT, complete: true, includeRuntimeContext: false } },@@ -84,9 +86,11 @@ export function standardPreset({ platform = process.platform, search = false } =],},{ id: 'tool-todo', name: '@deepseek-ai/dsh-tool-todo', config: { allowParallelInProgress: true } },- { id: 'tool-web', name: '@deepseek-ai/dsh-tool-web', config: { fetch: true, search, searchTimeoutMs: 60000 } },+ { id: 'tool-web', name: '@deepseek-ai/dsh-tool-web', config: { fetch: !off.has('fetch'), search: search && !off.has('search'), searchTimeoutMs: 60000 } },{ id: 'present', name: '@deepseek-ai/dsh-tool-present' },{ id: 'tool-plugin-manager', name: '@deepseek-ai/dsh-plugin-manager/tools', disabled: true },];- return { id: 'standard', order: 1, plugins };+ // The groups the person switched off in Settings -> Tools; a web plugin with both halves off is not loaded at all.+ const kept = withoutDisabled(plugins, off).filter(p => p.id !== 'tool-web' || p.config.fetch || p.config.search);+ return { id: 'standard', order: 1, plugins: kept };}diff --git a/apps/host/server/agent/profile.mjs b/apps/host/server/agent/profile.mjsindex c71c45235..2c709ab0c 100644--- a/apps/host/server/agent/profile.mjs+++ b/apps/host/server/agent/profile.mjs@@ -20,6 +20,7 @@import { createHash } from 'node:crypto';import { fileURLToPath } from 'node:url';import { standardPreset } from './preset.mjs';+import { DDG_PROVIDER_ID } from '../web-search-ddg.mjs';const PROTOCOL = { 'openai-chat': 'openai-completions', 'openai-responses': 'openai-responses', anthropic: 'anthropic-messages' };@@ -52,6 +53,13 @@ export const modeOf = value => (Object.hasOwn(MODE_PRESETS, value) ? value : DEF/** The approval gate plugin (plain ESM, loaded by absolute path). */export const GATE_PLUGIN = fileURLToPath(new URL('./plugins/strata-gate.mjs', import.meta.url));+/** The app's web search (DuckDuckGo, or the person's own Exa / Perplexity key), registered with the Harness's web seam. */+export const SEARCH_PLUGIN = fileURLToPath(new URL('./plugins/strata-search.mjs', import.meta.url));+/** The app's own tool server for the tools a person writes (instructions, commands). */+export const CUSTOM_TOOLS_SCRIPT = fileURLToPath(new URL('./custom-tools-mcp.mjs', import.meta.url));++/** What Settings -> Tools says when nothing was chosen. */+const NO_TOOLS_SETTINGS = Object.freeze({ off: new Set(), search: { provider: 'builtin', key: '' }, browserEnabled: false, servers: {}, hasInstructionTools: false, hasCommandTools: false, toolFile: null });/*** The tool servers (MCP) set up for the model in Settings, as Harness client rows. Their tools appear to the agent as@@ -120,7 +128,7 @@ export function outputBudget(context) {* @param {Array<{id:string,name:string,baseUrl:string,protocol:string,apiKey?:string,models:Array<{id:string,name?:string,contextMax?:number|null}>}>} input.connections* @returns {{patch:string, env:Record<string,string>, routes:Array, defaultRoute:object|null, signature:string}}*/-export function buildProfile({ local = [], connections = [], gate = true }) {+export function buildProfile({ local = [], connections = [], gate = true, tools = NO_TOOLS_SETTINGS }) {const providers = {};const env = {};const routes = [];@@ -158,14 +166,18 @@ export function buildProfile({ local = [], connections = [], gate = true }) {if (!protocol) return;const route = `api-${slug(conn.id).slice(0, 12)}`;const keyEnv = `STRATA_API_KEY_${slug(conn.id).replace(/-/g, '_').toUpperCase().slice(0, 12)}`;+ // A model server on this network that wants no key (llama.cpp, LM Studio, another Strata) still needs some value: the OpenAI-style client+ // refuses to send anything without one, and the person saw "the model rejected the credentials".+ const keyless = !conn.apiKey && protocol === 'openai-completions';if (conn.apiKey) env[keyEnv] = conn.apiKey;+ else if (keyless) env[keyEnv] = 'not-needed';let baseURL = conn.baseUrl;if (protocol === 'anthropic-messages') baseURL = baseURL.replace(/\/v1\/?$/, '');providers[route] = {displayName: conn.name,api: protocol,baseURL,- ...(conn.apiKey ? { apiKeyEnv: keyEnv } : {}),+ ...(conn.apiKey || keyless ? { apiKeyEnv: keyEnv } : {}),models: conn.models.map(m => ({id: m.id, name: m.name || m.id,contextWindow: m.contextMax ?? ASSUMED_API_CONTEXT, maxTokens: outputBudget(m.contextMax ?? ASSUMED_API_CONTEXT),@@ -178,8 +190,19 @@ export function buildProfile({ local = [], connections = [], gate = true }) {const defaultRoute = routes.find(r => r.kind === 'local') ?? null;// One Harness, one set of tool servers: the default local model's.- const mcp = mcpRows(local[0]?.mcpServers, mcpEnv);+ const mcp = mcpRows({ ...(local[0]?.mcpServers ?? {}), ...(tools.servers ?? {}) }, mcpEnv);Object.assign(env, mcpEnv);+ // The person's own instruction and command tools come from the app's tool server; a command tool asks in Manual like any connected tool.+ const own = (kind, on) => on && tools.toolFile ? [{ id: `mcp-strata-${kind}`, name: '@deepseek-ai/dsh-mcp-client', config: { serverName: `strata-${kind}`, transport: 'stdio', command: process.execPath, args: [CUSTOM_TOOLS_SCRIPT, kind, tools.toolFile] } }] : [];+ const ownRows = [...own('instructions', tools.hasInstructionTools), ...own('commands', tools.hasCommandTools)];+ // Web search: DuckDuckGo with no key unless the person chose a keyed service AND saved its key (then that one; never a half-set-up one).+ const searchOn = !tools.off.has('search');+ const keyed = ['exa', 'perplexity', 'deepseek'].includes(tools.search.provider) && tools.search.key ? tools.search.provider : null;+ if (searchOn && keyed) env.STRATA_SEARCH_KEY = tools.search.key;+ const webRows = !searchOn ? [] : keyed === 'deepseek'+ ? [{ id: 'web', config: { searchProvider: 'deepseek-official', fetchProvider: 'http' } }, { id: 'web-search-deepseek', config: { apiKeyEnv: 'STRATA_SEARCH_KEY' } }]+ : [{ id: 'web', config: { searchProvider: keyed ?? DDG_PROVIDER_ID, fetchProvider: 'http' } }, { id: 'web-search-deepseek', disabled: true },+ { insert: [{ id: 'strata-web-search', name: SEARCH_PLUGIN, config: { provider: keyed ?? 'duckduckgo', keyEnv: 'STRATA_SEARCH_KEY' } }] }];const entries = [...(defaultRoute ? [{ id: 'agent-default-model', name: '@deepseek-ai/dsh-agent-default-model', config: { provider: defaultRoute.route, model: defaultRoute.modelId } }] : []),{ id: 'llm-pi-ai', name: '@deepseek-ai/dsh-llm-pi-ai', config: { providers } },@@ -188,9 +211,10 @@ export function buildProfile({ local = [], connections = [], gate = true }) {{ id: 'subagent', config: { maxActiveSubagents: slots > 0 ? Math.max(slots, MIN_LOCAL_HELPERS) : DEFAULT_API_HELPERS } },// Manual asks before a change runs, so the person sees one Approve/Deny card and nothing has happened yet....(gate ? [{ insert: [{ id: 'strata-gate', name: GATE_PLUGIN }] }] : []),- ...(mcp.length ? [{ insert: mcp }] : []),- // The reminder tools would be a second scheduler beside the app's Tasks; web search needs a search-service key the app has none of.- { id: 'preset-standard', name: '@deepseek-ai/dsh-agent-preset', config: standardPreset() },+ ...(mcp.length || ownRows.length ? [{ insert: [...mcp, ...ownRows] }] : []),+ ...webRows,+ // The reminder tools would be a second scheduler beside the app's Tasks. The tool groups the person switched off in Settings -> Tools are left out.+ { id: 'preset-standard', name: '@deepseek-ai/dsh-agent-preset', config: standardPreset({ off: tools.off, search: searchOn }) },// Strata names its chats itself; the Harness's own title request would be an extra model call on// every first prompt (and, for an API route, an unrequested one).{ id: 'session-title-llm', disabled: true },@@ -204,7 +228,10 @@ export function buildProfile({ local = [], connections = [], gate = true }) {// The signature decides whether the running agent must be restarted. It covers the secret values// through a hash so a changed key is noticed, and the hash is never reversible into the key.const signature = createHash('sha256').update(patch).update(JSON.stringify(Object.entries(env).sort())).digest('hex').slice(0, 16);- return { patch, env, routes, defaultRoute, signature };+ // What the agent is offered (tool groups, web search, tool servers, the person's own tools) decides whether a running agent must be+ // restarted for the next message; the routes are compared separately (a changed context size alone does not restart it).+ const toolsSignature = createHash('sha256').update(JSON.stringify([mcp, ownRows, webRows, entries.find(e => e.id === 'preset-standard'), env.STRATA_SEARCH_KEY ?? '', mcpEnv])).digest('hex').slice(0, 16);+ return { patch, env, routes, defaultRoute, signature, toolsSignature };}/** Find the route for a chat's model choice. */diff --git a/apps/host/server/agent/tools-catalog.mjs b/apps/host/server/agent/tools-catalog.mjsnew file mode 100644index 000000000..3c97ae149--- /dev/null+++ b/apps/host/server/agent/tools-catalog.mjs@@ -0,0 +1,31 @@+/**+ * The tools a person can switch on and off (Settings -> Tools), in plain words, and which Harness plugins each one is.+ *+ * `plugins` are plugin ids of the app's agent preset (server/agent/preset.mjs). `web` marks the two halves of the Harness's one+ * `tool-web` plugin (open pages and search), which the preset switches through that plugin's own `fetch` / `search` options.+ * Everything not listed here (the persona, compaction, instructions, the clock) is not a tool and is always on.+ *+ * `asks` is only a plain-words hint for the screen: in Manual mode the approval gate asks before anything that is not provably look-only.+ */+export const TOOL_GROUPS = [+ { id: 'shell', area: 'computer', name: 'Run commands', about: 'Lets the assistant run commands, for example to open a folder or start a program.', asks: true, plugins: ['tool-pwsh', 'tool-bash'] },+ { id: 'files', area: 'computer', name: 'Read and change files', about: 'Lets the assistant read, create and edit files in your chat folder.', asks: true, plugins: ['tool-fs'] },+ { id: 'find', area: 'computer', name: 'Find files and text', about: 'Lets the assistant look for files by name and search inside them. It only reads.', asks: false, plugins: ['tool-fs-search'] },+ { id: 'jobs', area: 'computer', name: 'Long-running jobs', about: 'Lets the assistant start a slow command in the background and check on it later.', asks: true, plugins: ['tool-jobs'] },+ { id: 'fetch', area: 'web', name: 'Open web pages', about: 'Lets the assistant read a web page you point it to.', asks: false, web: 'fetch' },+ { id: 'search', area: 'web', name: 'Search the web', about: 'Lets the assistant search the web when it needs fresh information.', asks: false, web: 'search' },+ { id: 'helpers', area: 'together', name: 'Helper assistants', about: 'Lets the assistant start helpers that work on parts of a task at the same time.', asks: false, plugins: ['delegation'] },+ { id: 'goals', area: 'together', name: 'Goals', about: 'Lets you give a goal with /goal. The assistant keeps working until it is done.', asks: false, plugins: ['command-goal', 'tool-goal'] },+ { id: 'todo', area: 'together', name: 'To-do list', about: 'Lets the assistant keep a checklist while it works on a big task.', asks: false, plugins: ['tool-todo'] },+ { id: 'skills', area: 'together', name: 'Skills', about: 'Lets the assistant use your saved skills when they fit the task.', asks: false, plugins: ['skill-filesystem', 'tool-skill'] },+ { id: 'present', area: 'together', name: 'Show results', about: 'Lets the assistant show a page or file in the preview next to the chat.', asks: false, plugins: ['present'] },+];++export const GROUP_IDS = TOOL_GROUPS.map(g => g.id);++/** The preset's plugin rows without the groups the person switched off. */+export function withoutDisabled(plugins, off) {+ const gone = new Set();+ for (const group of TOOL_GROUPS) if (off.has(group.id)) for (const id of group.plugins ?? []) gone.add(id);+ return plugins.filter(p => !gone.has(p.id));+}diff --git a/apps/host/server/agent/tools-settings.mjs b/apps/host/server/agent/tools-settings.mjsnew file mode 100644index 000000000..c620b2b8d--- /dev/null+++ b/apps/host/server/agent/tools-settings.mjs@@ -0,0 +1,198 @@+/**+ * 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';++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 },+ 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)),+ })),+ };+}++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: [] };+ 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.');+ } 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}}} 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 };+ }+ 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 },+ browser: { enabled: s.browser.enabled, found: !!probe.found, name: probe.name ?? null },+ 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,+ })),+ };+ }++ 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,+ browserEnabled: s.browser.enabled && !!browser().found,+ 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 (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()); },+ };+}diff --git a/apps/host/server/index.mjs b/apps/host/server/index.mjsindex d8361ff84..5ed5487aa 100644--- a/apps/host/server/index.mjs+++ b/apps/host/server/index.mjs@@ -13,6 +13,8 @@ import { skillEdit } from './skill-edits.mjs';import { systemFonts } from './fonts.mjs';import { tuningDir } from './platform-paths.mjs';import { createAgentBridge } from './agent/agent-bridge.mjs';+import { createAgentTools } from './agent/tools-settings.mjs';+import { findBrowser } from './agent/browser-probe.mjs';/*** Strata engine host.*@@ -90,7 +92,11 @@ const native=createNativeControl({control,appUpdater,hardwareProbe:()=>(hardware/* The agent: chats become DeepSeek Harness sessions when that component is installed. It is a separatepinned component (STRATA_HARNESS_DIR), not part of this host, so its absence is a visible capabilitystate and the chat falls back to the direct path with a notice, never silently to another model. */+// What the person chose in Settings -> Tools (tool groups, web search service and key, the browser, their own tools).+const agentTools = createAgentTools({ read: () => store.get().agentTools, write: value => store.update({ agentTools: value }), dataDir: store.DATA_DIR, browser: () => findBrowser() });+agentTools.ensureFile();const agent = createAgentBridge({+ tools: () => agentTools.profileView(),dataDir: store.DATA_DIR,componentsDir: componentsDirectory(),// The installer ships the Harness next to the host (<install>/host/server -> <install>/harness).@@ -1448,6 +1454,20 @@ async function api(req, res, path, method, url) {// --- agent --------------------------------------------------------------if (path === '/api/agent/status' && method === 'GET') return json(res, 200, agent.status());+ if (path === '/api/agent/tools' && method === 'GET') return json(res, 200, agentTools.view());+ if (path === '/api/agent/tools' && method === 'PUT') {+ try { return json(res, 200, agentTools.update(await readBody(req))); } catch (e) { return error(res, 400, e.message); }+ }+ if (path === '/api/agent/tools/custom' && method === 'POST') {+ try { return json(res, 200, agentTools.addCustom(await readBody(req))); } catch (e) { return error(res, 400, e.message); }+ }+ const customToolRoute = /^\/api\/agent\/tools\/custom\/([A-Za-z0-9_-]+)$/.exec(path);+ if (customToolRoute && method === 'PATCH') {+ try { return json(res, 200, agentTools.editCustom(customToolRoute[1], await readBody(req))); } catch (e) { return error(res, 400, e.message); }+ }+ if (customToolRoute && method === 'DELETE') {+ try { return json(res, 200, agentTools.removeCustom(customToolRoute[1])); } catch (e) { return error(res, 400, e.message); }+ }if (path === '/api/agent/pending' && method === 'GET') {const chat = new URL(req.url, 'http://x').searchParams.get('chatId') ?? '';return json(res, 200, { pending: agent.pendingFor(chat) });diff --git a/apps/host/server/web-search-ddg.mjs b/apps/host/server/web-search-ddg.mjsnew file mode 100644index 000000000..e2f02df10--- /dev/null+++ b/apps/host/server/web-search-ddg.mjs@@ -0,0 +1,188 @@+/**+ * Web search with no account and no key: DuckDuckGo's own web-page search results.+ *+ * The Harness has three search backends and all of them need a paid service key. This is the fourth: it asks DuckDuckGo's plain+ * HTML search page the way a browser does and reads the result list out of the page. It is the default for the agent's+ * `web_search` tool; the person can switch to a keyed service in Settings -> Tools.+ *+ * Being polite is part of the design:+ * - an ordinary browser User-Agent and the page's own request shape (nothing hidden, nothing faked beyond the User-Agent);+ * - at most one request every 1.5 s and 20 a minute from this program, with the same query answered from a 10 minute cache;+ * - a 15 s time limit, no retries in a loop; when DuckDuckGo asks for a human check or refuses, the answer says so in plain+ * words and the model is told to carry on without search.+ * Results come from DuckDuckGo; they are not ranked or filtered here.+ *+ * Nothing in this file touches the Harness, so it can be tested alone. `strata-search.mjs` is the plugin that registers it.+ */++export const DDG_PROVIDER_ID = 'strata-duckduckgo';++const HTML_URL = 'https://html.duckduckgo.com/html/';+const LITE_URL = 'https://lite.duckduckgo.com/lite/';+const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0 Safari/537.36';+const CACHE_MS = 10 * 60 * 1000;+const MIN_GAP_MS = 1500;+const PER_MINUTE = 20;++const ENTITIES = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: ' ', '#39': "'", '#x27': "'", '#x2F': '/' };+const decode = text => String(text).replace(/&(#x?[0-9a-f]+|[a-z]+);/gi, (whole, key) => {+ if (key[0] === '#') {+ const code = key[1].toLowerCase() === 'x' ? parseInt(key.slice(2), 16) : parseInt(key.slice(1), 10);+ return Number.isFinite(code) && code > 0 && code < 0x110000 ? String.fromCodePoint(code) : whole;+ }+ return ENTITIES[key.toLowerCase()] ?? whole;+});+const plain = html => decode(String(html).replace(/<[^>]*>/g, '')).replace(/\s+/g, ' ').trim();++/** DuckDuckGo wraps every result link in its own redirect; the real address is in `uddg`. */+export function realUrl(href) {+ let value = decode(href).trim();+ if (value.startsWith('//')) value = `https:${value}`;+ try {+ const url = new URL(value, 'https://duckduckgo.com/');+ const target = url.searchParams.get('uddg');+ if (target) return new URL(target).href;+ if (url.hostname.endsWith('duckduckgo.com')) return null; // an internal link, an ad or a "more results" link+ return /^https?:$/.test(url.protocol) ? url.href : null;+ } catch { return null; }+}++/** The result list of the HTML page. Tolerant: a result needs a link and a title; the snippet is optional. */+export function parseHtmlResults(html) {+ const out = [];+ const links = [...String(html).matchAll(/<a\b[^>]*class="[^"]*\bresult__a\b[^"]*"[^>]*>/gi)];+ for (let i = 0; i < links.length; i++) {+ const open = links[i];+ const start = open.index + open[0].length;+ const next = links[i + 1]?.index ?? html.length;+ const chunk = html.slice(start, next);+ const close = chunk.indexOf('</a>');+ if (close < 0) continue;+ const href = /href="([^"]*)"/i.exec(open[0])?.[1];+ const url = href ? realUrl(href) : null;+ const title = plain(chunk.slice(0, close));+ if (!url || !title) continue;+ // An ad is a result block whose own opening tag says so (the block that holds this link, not the one before it).+ const block = html.lastIndexOf('<div class="result', open.index);+ if (block >= 0 && /result--ad|result__ad/.test(html.slice(block, html.indexOf('>', block) + 1))) continue;+ const snippetMatch = /<(?:a|td|div)\b[^>]*class="[^"]*\bresult__snippet\b[^"]*"[^>]*>([\s\S]*?)<\/(?:a|td|div)>/i.exec(chunk);+ const snippet = snippetMatch ? plain(snippetMatch[1]) : '';+ out.push({ url, title, ...(snippet ? { snippet } : {}) });+ }+ return dedupe(out);+}++/** The result list of the lite page (a table), used when the HTML page gives nothing. */+export function parseLiteResults(html) {+ const out = [];+ const links = [...String(html).matchAll(/<a\b[^>]*class=['"][^'"]*\bresult-link\b[^'"]*['"][^>]*>/gi)];+ for (let i = 0; i < links.length; i++) {+ const open = links[i];+ const start = open.index + open[0].length;+ const next = links[i + 1]?.index ?? html.length;+ const chunk = html.slice(start, next);+ const close = chunk.indexOf('</a>');+ if (close < 0) continue;+ const href = /href=['"]([^'"]*)['"]/i.exec(open[0])?.[1];+ const url = href ? realUrl(href) : null;+ const title = plain(chunk.slice(0, close));+ if (!url || !title) continue;+ const snippetMatch = /<td\b[^>]*class=['"][^'"]*\bresult-snippet\b[^'"]*['"][^>]*>([\s\S]*?)<\/td>/i.exec(chunk);+ const snippet = snippetMatch ? plain(snippetMatch[1]) : '';+ out.push({ url, title, ...(snippet ? { snippet } : {}) });+ }+ return dedupe(out);+}++function dedupe(rows) {+ const seen = new Set();+ return rows.filter(r => !seen.has(r.url) && seen.add(r.url));+}++/** DuckDuckGo asks a program to prove it is a person by serving a challenge page instead of results. */+export function looksBlocked(status, html) {+ if (status === 202 || status === 403 || status === 418 || status === 429) return true;+ return /anomaly-modal|anomaly\.js|unusual traffic|are you a (?:robot|human)|captcha|challenge-form/i.test(String(html).slice(0, 60000))+ && !/class="[^"]*\bresult__a\b/.test(html);+}++export class SearchUnavailable extends Error {+ constructor(message, code = 'WEB_PROVIDER_ERROR', options) { super(message, options); this.name = 'WebError'; this.code = code; }+}++export const BLOCKED_MESSAGE = 'DuckDuckGo is limiting searches from this computer right now. Try again in a few minutes, or add a search key in Settings, Tools.';++/**+ * @param {{fetch?: typeof fetch, now?: () => number, sleep?: (ms: number) => Promise<void>}} [deps]+ */+export function createDuckDuckGoSearch({ fetch: fetchImpl = globalThis.fetch, now = Date.now, sleep = ms => new Promise(r => setTimeout(r, ms)) } = {}) {+ const cache = new Map();+ const starts = [];+ let last = 0;+ let chain = Promise.resolve();++ /** One request at a time, spaced out, and at most PER_MINUTE a minute. */+ function turn(signal) {+ const run = chain.then(async () => {+ for (;;) {+ signal?.throwIfAborted();+ const t = now();+ while (starts.length && t - starts[0] > 60_000) starts.shift();+ const wait = Math.max(MIN_GAP_MS - (t - last), starts.length >= PER_MINUTE ? 60_000 - (t - starts[0]) : 0);+ if (wait <= 0) break;+ if (wait > 30_000) throw new SearchUnavailable('Too many searches in a short time. Try again in a minute.', 'WEB_RATE_LIMITED');+ await sleep(wait);+ }+ last = now();+ starts.push(last);+ });+ chain = run.catch(() => {});+ return run;+ }++ async function ask(url, query, signal) {+ const timeout = AbortSignal.timeout(15_000);+ const res = await fetchImpl(url, {+ method: 'POST', redirect: 'follow',+ headers: { 'user-agent': USER_AGENT, 'content-type': 'application/x-www-form-urlencoded', accept: 'text/html,application/xhtml+xml', 'accept-language': 'en-US,en;q=0.8' },+ body: new URLSearchParams({ q: query, b: '' }).toString(),+ signal: signal ? AbortSignal.any([signal, timeout]) : timeout,+ });+ const html = await res.text();+ return { status: res.status, html };+ }++ return {+ id: DDG_PROVIDER_ID,+ available: () => typeof fetchImpl === 'function',+ async search(request, signal) {+ const query = String(request.query ?? '').trim().slice(0, 400);+ if (!query) throw new SearchUnavailable('Type what to search for.', 'WEB_PROVIDER_ERROR');+ const key = query.toLowerCase();+ const hit = cache.get(key);+ if (hit && now() - hit.at < CACHE_MS) return { sources: hit.sources.slice(0, request.maxResults ?? 10), truncated: false };+ let sources = [];+ let blocked = false;+ for (const [url, parse] of [[HTML_URL, parseHtmlResults], [LITE_URL, parseLiteResults]]) {+ await turn(signal);+ let page;+ try { page = await ask(url, query, signal); }+ catch (error) {+ if (signal?.aborted) throw new SearchUnavailable('The search was cancelled.', 'WEB_ABORTED', { cause: error });+ if (error?.name === 'TimeoutError') throw new SearchUnavailable('The search took too long. Try again.', 'WEB_TIMEOUT', { cause: error });+ throw new SearchUnavailable('The search could not reach DuckDuckGo. Check the internet connection and try again.', 'WEB_PROVIDER_ERROR', { cause: error });+ }+ if (looksBlocked(page.status, page.html)) { blocked = true; continue; }+ sources = parse(page.html);+ if (sources.length) { blocked = false; break; }+ }+ if (!sources.length) {+ if (blocked) throw new SearchUnavailable(BLOCKED_MESSAGE, 'WEB_RATE_LIMITED');+ return { sources: [], truncated: false }; // a real search with no results is an answer, not an error+ }+ cache.set(key, { at: now(), sources });+ if (cache.size > 60) cache.delete(cache.keys().next().value);+ return { sources: sources.slice(0, request.maxResults ?? 10), truncated: false };+ },+ };+}diff --git a/apps/host/server/web-search-keyed.mjs b/apps/host/server/web-search-keyed.mjsnew file mode 100644index 000000000..6ce91ca7c--- /dev/null+++ b/apps/host/server/web-search-keyed.mjs@@ -0,0 +1,66 @@+/**+ * Search through a service the person has their own key for: Exa or Perplexity (the Harness ships both as separate packages the app's+ * bundle leaves out, so these are the same two small calls written here; DeepSeek's provider is in the bundle and is used as it is).+ * Shapes follow the Harness's providers (packages/web/web-search-exa and web-search-perplexity at the pinned commit).+ */+import { SearchUnavailable } from './web-search-ddg.mjs';++const abortError = e => e?.name === 'AbortError';++async function call(service, url, options, signal) {+ let res;+ try { res = await fetch(url, { ...options, redirect: 'error', ...(signal ? { signal } : {}) }); }+ catch (error) {+ if (abortError(error)) throw new SearchUnavailable('The search was cancelled.', 'WEB_ABORTED', { cause: error });+ throw new SearchUnavailable(`The search could not reach ${service}. Check the internet connection and try again.`, 'WEB_PROVIDER_ERROR', { cause: error });+ }+ if (!res.ok) {+ let detail = '';+ try { const body = await res.json(); detail = typeof body.error === 'string' ? body.error : body.error?.message ?? body.message ?? ''; } catch { /* not JSON */ }+ if (res.status === 401 || res.status === 403) throw new SearchUnavailable(`${service} did not accept the key. Check it in Settings, Tools.`, 'WEB_PROVIDER_ERROR');+ if (res.status === 429) throw new SearchUnavailable(`${service} says there have been too many searches. Try again in a minute.`, 'WEB_RATE_LIMITED');+ throw new SearchUnavailable(`${service} could not search (error ${res.status}).${detail ? ' ' + String(detail).slice(0, 200) : ''}`, 'WEB_PROVIDER_ERROR');+ }+ try { return await res.json(); }+ catch (error) { throw new SearchUnavailable(`${service} sent an answer that could not be read.`, 'WEB_PROVIDER_ERROR', { cause: error }); }+}++export function createExaSearch({ key }) {+ return {+ id: 'exa',+ available: () => typeof key === 'function' ? !!key() : !!key,+ async search(request, signal) {+ const apiKey = typeof key === 'function' ? key() : key;+ const body = await call('Exa', 'https://api.exa.ai/search', {+ method: 'POST',+ headers: { authorization: `Bearer ${apiKey}`, 'content-type': 'application/json', accept: 'application/json' },+ body: JSON.stringify({ query: request.query, type: 'auto', contents: { highlights: { highlightsPerUrl: 1 } }, ...(request.maxResults ? { numResults: request.maxResults } : {}) }),+ }, signal);+ const sources = (body.results ?? []).flatMap(r => {+ const snippet = (r.highlights ?? []).find(h => String(h).trim());+ return snippet === undefined ? [] : [{ url: r.url, ...(r.title ? { title: r.title } : {}), snippet, ...(r.publishedDate ? { publishedAt: r.publishedDate } : {}) }];+ });+ return { sources, truncated: false };+ },+ };+}++export function createPerplexitySearch({ key }) {+ return {+ id: 'perplexity',+ available: () => typeof key === 'function' ? !!key() : !!key,+ async search(request, signal) {+ const apiKey = typeof key === 'function' ? key() : key;+ const body = await call('Perplexity', 'https://api.perplexity.ai/chat/completions', {+ method: 'POST',+ headers: { authorization: `Bearer ${apiKey}`, 'content-type': 'application/json', accept: 'application/json' },+ body: JSON.stringify({ model: 'sonar', max_tokens: 1024, messages: [{ role: 'user', content: request.query }] }),+ }, signal);+ const content = body.choices?.[0]?.message?.content;+ const sources = body.search_results !== undefined+ ? body.search_results.map(r => ({ url: r.url, ...(r.title ? { title: r.title } : {}), ...(r.snippet ? { snippet: r.snippet } : {}), ...(r.date ? { publishedAt: r.date } : {}) }))+ : (body.citations ?? []).map(url => ({ url }));+ return { ...(content ? { content } : {}), sources, truncated: false };+ },+ };+}diff --git a/apps/host/tests/agent-tools-settings.test.mjs b/apps/host/tests/agent-tools-settings.test.mjsnew file mode 100644index 000000000..5e166ead5--- /dev/null+++ b/apps/host/tests/agent-tools-settings.test.mjs@@ -0,0 +1,335 @@+/**+ * Settings -> Tools, behind the screen: what is saved, what reaches the agent, web search with no key, and the person's own tools.+ *+ * Pure tests always run. The two that start the real Harness need STRATA_HARNESS_DIR (the model is scripted; what is real is the Harness,+ * its web seam and tool executor, the Strata gate plugin, the bridge and the app's own tool server).+ */+import test from 'node:test';+import assert from 'node:assert/strict';+import { spawn } from 'node:child_process';+import { existsSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { createInterface } from 'node:readline';+import { startScriptedWorker } from './helpers/scripted-worker.mjs';+import { createAgentBridge } from '../server/agent/agent-bridge.mjs';+import { resolveHarness } from '../server/agent/harness-runtime.mjs';+import { buildProfile, CUSTOM_TOOLS_SCRIPT } from '../server/agent/profile.mjs';+import { standardPreset } from '../server/agent/preset.mjs';+import { TOOL_GROUPS } from '../server/agent/tools-catalog.mjs';+import { createAgentTools } from '../server/agent/tools-settings.mjs';+import { findBrowser, browserCandidates } from '../server/agent/browser-probe.mjs';+import { manualDecision, planDecision } from '../server/agent/plugins/strata-gate.mjs';+import { createDuckDuckGoSearch, looksBlocked, parseHtmlResults, parseLiteResults, realUrl, BLOCKED_MESSAGE } from '../server/web-search-ddg.mjs';+import { createExaSearch, createPerplexitySearch } from '../server/web-search-keyed.mjs';++const HARNESS = process.env.STRATA_HARNESS_DIR;+const skip = !HARNESS || !resolveHarness({ env: process.env }).ok ? 'STRATA_HARNESS_DIR is not set to a built Harness' : false;+const tmp = () => mkdtempSync(join(tmpdir(), 'strata-tools-set-'));+const cleanup = dir => { try { rmSync(dir, { recursive: true, force: true, maxRetries: 3 }); } catch { /* best effort */ } };+const make = (dir, state = {}) => { let saved = state; return { tools: createAgentTools({ read: () => saved, write: v => { saved = v; }, dataDir: dir }), saved: () => saved }; };++// ------------------------------------------------------------------------------------------------ the saved choices++test('Tools: every tool is on at first, in plain words, and turning one off or on is remembered', () => {+ const dir = tmp();+ try {+ const { tools } = make(dir);+ const v = tools.view();+ assert.equal(v.groups.length, TOOL_GROUPS.length);+ assert.ok(v.groups.every(g => g.enabled && g.name && g.about && ['computer', 'web', 'together'].includes(g.area)));+ assert.equal(v.search.provider, 'builtin'); assert.equal(v.search.keySet, false);+ const off = tools.update({ groups: { jobs: false, search: false } });+ assert.deepEqual(off.groups.filter(g => !g.enabled).map(g => g.id), ['jobs', 'search']);+ assert.deepEqual([...tools.profileView().off].sort(), ['jobs', 'search']);+ assert.ok(tools.update({ groups: { jobs: true } }).groups.find(g => g.id === 'jobs').enabled);+ assert.throws(() => tools.update({ groups: { nonsense: false } }), /not known/);+ } finally { cleanup(dir); }+});++test('Tools: a search key is saved for its service only, is never shown again, and is refused when it looks wrong', () => {+ const dir = tmp();+ try {+ const { tools, saved } = make(dir);+ tools.update({ search: { provider: 'exa' } });+ assert.throws(() => tools.update({ search: { key: 'has a space' } }), /does not look right/);+ const v = tools.update({ search: { key: 'exa-key-123456' } });+ assert.deepEqual(v.search, { provider: 'exa', keySet: true });+ assert.ok(!JSON.stringify(v).includes('exa-key-123456'), 'the screen never gets the key back');+ assert.equal(tools.profileView().search.key, 'exa-key-123456');+ assert.equal(tools.update({ search: { provider: 'perplexity' } }).search.keySet, false, 'a key belongs to its service');+ assert.throws(() => tools.update({ search: { provider: 'bing' } }), /listed search services/);+ assert.equal(saved().search.provider, 'perplexity');+ } finally { cleanup(dir); }+});++test('Tools: the person\'s own tools are checked in plain words and saved with the three ways of working', () => {+ const dir = tmp();+ try {+ const { tools } = make(dir);+ assert.throws(() => tools.addCustom({ name: '', about: 'x', kind: 'instructions', instructions: 'y' }), /short name/);+ assert.throws(() => tools.addCustom({ name: 'Notes', about: '', kind: 'instructions', instructions: 'y' }), /what the tool does/);+ assert.throws(() => tools.addCustom({ name: 'Notes', about: 'a', kind: 'instructions', instructions: ' ' }), /instructions/);+ assert.throws(() => tools.addCustom({ name: 'Run', about: 'a', kind: 'command', command: 'a\nb' }), /one line/);+ assert.throws(() => tools.addCustom({ name: 'Srv', about: 'a', kind: 'server', address: 'https://u:[email protected]/mcp' }), /user name or password/);+ let v = tools.addCustom({ name: 'Release notes', about: 'Write release notes in our house style.', kind: 'instructions', instructions: 'Start with one line.' });+ v = tools.addCustom({ name: 'Check site', about: 'Check that the site answers.', kind: 'command', command: 'curl -I https://example.com' });+ v = tools.addCustom({ name: 'Issue tracker', about: 'Look up issues.', kind: 'server', address: 'https://issues.example/mcp' });+ assert.deepEqual(v.custom.map(t => [t.id, t.kind]), [['release_notes', 'instructions'], ['check_site', 'command'], ['issue_tracker', 'server']]);+ assert.throws(() => tools.addCustom({ name: 'release notes', about: 'a', kind: 'instructions', instructions: 'y' }), /already have a tool/);+ const p = tools.profileView();+ assert.equal(p.hasInstructionTools && p.hasCommandTools, true);+ assert.deepEqual(p.servers, { 'custom-issue-tracker': { url: 'https://issues.example/mcp' } });+ const file = JSON.parse(readFileSync(tools.filePath, 'utf8'));+ assert.deepEqual(Object.keys(file.instructions), ['release_notes']); assert.deepEqual(Object.keys(file.commands), ['check_site']);+ v = tools.update({ custom: { check_site: false } });+ assert.equal(tools.profileView().hasCommandTools, false); assert.deepEqual(JSON.parse(readFileSync(tools.filePath, 'utf8')).commands, {});+ v = tools.editCustom('release_notes', { name: 'Release notes', about: 'Changed.', kind: 'instructions', instructions: 'New text.' });+ assert.equal(v.custom[0].about, 'Changed.');+ v = tools.removeCustom('issue_tracker');+ assert.equal(v.custom.length, 2);+ assert.throws(() => tools.removeCustom('nope'), /not in your list/);+ } finally { cleanup(dir); }+});++// ---------------------------------------------------------------------------------------------- what the agent gets++const entriesOf = profile => JSON.parse(profile.patch.split('\n').slice(1).join('\n'));+const toolWeb = entries => entries.find(e => e.id === 'preset-standard').config.plugins.find(p => p.id === 'tool-web');++test('profile: web search is on with no key (DuckDuckGo), pinned so a second backend cannot make the choice ambiguous', () => {+ const e = entriesOf(buildProfile({ local: [], connections: [] }));+ assert.equal(toolWeb(e).config.search, true); assert.equal(toolWeb(e).config.fetch, true);+ assert.deepEqual(e.find(x => x.id === 'web').config, { searchProvider: 'strata-duckduckgo', fetchProvider: 'http' });+ assert.equal(e.find(x => x.id === 'web-search-deepseek').disabled, true);+ const plugin = e.flatMap(x => x.insert ?? []).find(x => x.id === 'strata-web-search');+ assert.ok(existsSync(plugin.name)); assert.equal(plugin.config.provider, 'duckduckgo');+});++test('profile: a keyed service is used only with its key saved; the key travels in the environment and is in no file', () => {+ const view = (provider, key) => ({ off: new Set(), search: { provider, key }, servers: {}, hasInstructionTools: false, hasCommandTools: false, toolFile: null, browserEnabled: false });+ const noKey = buildProfile({ local: [], connections: [], tools: view('exa', '') });+ assert.equal(entriesOf(noKey).find(x => x.id === 'web').config.searchProvider, 'strata-duckduckgo', 'no key: DuckDuckGo, not a broken search');+ const exa = buildProfile({ local: [], connections: [], tools: view('exa', 'exa-secret-key') });+ assert.equal(entriesOf(exa).find(x => x.id === 'web').config.searchProvider, 'exa');+ assert.equal(exa.env.STRATA_SEARCH_KEY, 'exa-secret-key'); assert.ok(!exa.patch.includes('exa-secret-key'));+ const ds = buildProfile({ local: [], connections: [], tools: view('deepseek', 'ds-secret') });+ assert.deepEqual(entriesOf(ds).find(x => x.id === 'web-search-deepseek').config, { apiKeyEnv: 'STRATA_SEARCH_KEY' });+ assert.equal(entriesOf(ds).find(x => x.id === 'web').config.searchProvider, 'deepseek-official');+ assert.notEqual(noKey.signature, exa.signature, 'a changed choice restarts the agent');+});++test('profile: the groups switched off are not loaded; both web halves off drops the web plugin', () => {+ const base = { servers: {}, hasInstructionTools: false, hasCommandTools: false, toolFile: null, browserEnabled: false, search: { provider: 'builtin', key: '' } };+ const ids = off => standardPreset({ off: new Set(off), search: true }).plugins.map(p => p.id);+ assert.ok(!ids(['shell']).includes('tool-pwsh') && !ids(['shell']).includes('tool-bash'));+ assert.ok(!ids(['helpers']).includes('delegation') && ids(['helpers']).includes('tool-fs'));+ assert.ok(!ids(['goals']).includes('tool-goal') && !ids(['goals']).includes('command-goal'));+ assert.ok(ids(['fetch']).includes('tool-web') && !ids(['fetch', 'search']).includes('tool-web'));+ const e = entriesOf(buildProfile({ local: [], connections: [], tools: { ...base, off: new Set(['search']) } }));+ assert.equal(toolWeb(e).config.search, false);+ assert.equal(e.find(x => x.id === 'web'), undefined, 'no search service is set up when search is off');+});++test('profile: the person\'s instruction and command tools are the app\'s own tool server; a person\'s server is a connected tool', () => {+ const dir = tmp();+ try {+ const { tools } = make(dir);+ tools.addCustom({ name: 'Notes', about: 'a', kind: 'instructions', instructions: 'text' });+ tools.addCustom({ name: 'Run it', about: 'a', kind: 'command', command: 'echo hi' });+ tools.addCustom({ name: 'Remote', about: 'a', kind: 'server', address: 'https://r.example/mcp' });+ const e = entriesOf(buildProfile({ local: [], connections: [], tools: tools.profileView() }));+ const rows = e.flatMap(x => x.insert ?? []).filter(r => r.name === '@deepseek-ai/dsh-mcp-client');+ assert.deepEqual(rows.map(r => r.config.serverName).sort(), ['custom-remote', 'strata-commands', 'strata-instructions']);+ const own = rows.find(r => r.config.serverName === 'strata-commands').config;+ assert.deepEqual([own.command, own.args[0], own.args[1]], [process.execPath, CUSTOM_TOOLS_SCRIPT, 'commands']);+ } finally { cleanup(dir); }+});++test('the gate: instruction tools only hand back text, so they do not ask; commands, servers and the browser ask in Manual and are refused in Plan', () => {+ assert.equal(manualDecision({ name: 'mcp__strata-instructions__notes', arguments: {} }), null);+ assert.equal(manualDecision({ name: 'mcp__strata-commands__run_it', arguments: {} })?.kind, 'ask');+ assert.equal(manualDecision({ name: 'mcp__custom-remote__lookup', arguments: {} })?.kind, 'ask');+ assert.equal(planDecision({ name: 'mcp__strata-instructions__notes' }), null);+ assert.equal(planDecision({ name: 'read' }), null);+ for (const name of ['mcp__strata-commands__run_it', 'mcp__custom-remote__lookup', 'mcp__browser__browser_navigate']) assert.equal(planDecision({ name })?.kind, 'deny', name);+ assert.match(planDecision({ name: 'mcp__x__y' }).reason, /Plan mode only looks/);+});++// ------------------------------------------------------------------------------------------------- web search alone++const HTML = `<div class="result results_links results_links_deep web-result">+<div class="links_main links_deep result__body"><h2 class="result__title"><a rel="nofollow" class="result__a" href="//duckduckgo.com/l/?uddg=https%3A%2F%2Fexample.com%2Fa%3Fx%3D1&rut=abc">Example <b>Domain</b> & more</a></h2>+<a class="result__snippet" href="//duckduckgo.com/l/?uddg=https%3A%2F%2Fexample.com%2Fa">A <b>short</b> snippet 'quoted'.</a></div></div>+<div class="result result--ad"><h2 class="result__title"><a rel="nofollow" class="result__a" href="https://duckduckgo.com/y.js?ad_domain=ad.example">An ad</a></h2></div>+<div class="result"><h2 class="result__title"><a rel="nofollow" class="result__a" href="//duckduckgo.com/l/?uddg=https%3A%2F%2Fsecond.example%2F">Second</a></h2>+<a class="result__snippet" href="x">Second snippet</a></div>`;+const LITE = `<table><tr><td>1. </td><td><a rel="nofollow" href="//duckduckgo.com/l/?uddg=https%3A%2F%2Flite.example%2F" class='result-link'>Lite <b>title</b></a></td></tr>+<tr><td class='result-snippet'>Lite snippet text</td></tr>+<tr><td>2. </td><td><a rel="nofollow" href="//duckduckgo.com/l/?uddg=https%3A%2F%2Flite2.example%2F" class='result-link'>Lite two</a></td></tr></table>`;++test('search: the DuckDuckGo result list is read into titles, addresses and snippets; ads and internal links are dropped', () => {+ const rows = parseHtmlResults(HTML);+ assert.deepEqual(rows, [+ { url: 'https://example.com/a?x=1', title: 'Example Domain & more', snippet: "A short snippet 'quoted'." },+ { url: 'https://second.example/', title: 'Second', snippet: 'Second snippet' },+ ]);+ assert.deepEqual(parseLiteResults(LITE).map(r => [r.url, r.title, r.snippet]), [['https://lite.example/', 'Lite title', 'Lite snippet text'], ['https://lite2.example/', 'Lite two', undefined]]);+ assert.equal(realUrl('//duckduckgo.com/l/?uddg=https%3A%2F%2Fa.example%2F'), 'https://a.example/');+ assert.equal(realUrl('https://duckduckgo.com/y.js?x=1'), null);+ assert.equal(realUrl('javascript:alert(1)'), null);+});++test('search: a human check or a refusal is told in plain words, and a real search with no hits is an answer, not an error', async () => {+ assert.equal(looksBlocked(202, ''), true); assert.equal(looksBlocked(429, ''), true);+ assert.equal(looksBlocked(200, '<div class="anomaly-modal">Unfortunately, bots use DuckDuckGo too</div>'), true);+ assert.equal(looksBlocked(200, HTML), false);+ const sleeps = [];+ const blocked = createDuckDuckGoSearch({ fetch: async () => new Response('<html>captcha anomaly-modal</html>', { status: 200 }), sleep: async ms => { sleeps.push(ms); } });+ await assert.rejects(blocked.search({ query: 'x' }), e => e.message === BLOCKED_MESSAGE && e.code === 'WEB_RATE_LIMITED');+ const empty = createDuckDuckGoSearch({ fetch: async () => new Response('<html><body>No results.</body></html>', { status: 200 }), sleep: async () => {} });+ assert.deepEqual(await empty.search({ query: 'zzzz' }), { sources: [], truncated: false });+ const offline = createDuckDuckGoSearch({ fetch: async () => { throw new TypeError('fetch failed'); }, sleep: async () => {} });+ await assert.rejects(offline.search({ query: 'x' }), /could not reach DuckDuckGo/);+});++test('search: polite by design: one request at a time, spaced out, a minute limit, and the same query is answered from a short cache', async () => {+ let clock = 1_000_000, calls = 0;+ const waits = [];+ const provider = createDuckDuckGoSearch({+ now: () => clock, sleep: async ms => { waits.push(ms); clock += ms; },+ fetch: async (url, init) => { calls++; assert.equal(init.method, 'POST'); assert.match(init.headers['user-agent'], /Mozilla/); assert.match(String(url), /duckduckgo\.com/); return new Response(HTML, { status: 200 }); },+ });+ const a = await provider.search({ query: 'Strata engine', maxResults: 1 });+ assert.equal(a.sources.length, 1);+ await provider.search({ query: 'strata ENGINE' }); // same words: from the cache+ assert.equal(calls, 1);+ await provider.search({ query: 'another one' });+ assert.equal(calls, 2); assert.ok(waits.some(ms => ms >= 1400), 'the second request waited for the gap');+ for (let i = 0; i < 25; i++) { try { await provider.search({ query: `q${i}` }); } catch (e) { assert.equal(e.code, 'WEB_RATE_LIMITED'); break; } }+ assert.ok(calls <= 23, 'no more than about 20 a minute');+});++test('search: Exa and Perplexity use the person\'s key, map their answers, and say plainly when the key is refused', async () => {+ const real = globalThis.fetch;+ try {+ globalThis.fetch = async (url, init) => {+ assert.equal(init.headers.authorization, 'Bearer the-key');+ if (String(url).includes('exa.ai')) return new Response(JSON.stringify({ results: [{ url: 'https://e.example', title: 'E', highlights: ['hl'] }, { url: 'https://no-snippet.example', highlights: [] }] }));+ return new Response(JSON.stringify({ choices: [{ message: { content: 'Answer.' } }], search_results: [{ url: 'https://p.example', title: 'P', snippet: 's' }] }));+ };+ assert.deepEqual((await createExaSearch({ key: 'the-key' }).search({ query: 'q' })).sources, [{ url: 'https://e.example', title: 'E', snippet: 'hl' }]);+ const p = await createPerplexitySearch({ key: () => 'the-key' }).search({ query: 'q' });+ assert.equal(p.content, 'Answer.'); assert.equal(p.sources[0].url, 'https://p.example');+ assert.equal(createExaSearch({ key: () => '' }).available(), false);+ globalThis.fetch = async () => new Response('{}', { status: 401 });+ await assert.rejects(createExaSearch({ key: 'the-key' }).search({ query: 'q' }), /did not accept the key/);+ } finally { globalThis.fetch = real; }+});++// ------------------------------------------------------------------------------------------------------- browser probe++test('browser: Chrome, then Edge, then Chromium; nothing found says so', () => {+ const win = { PROGRAMFILES: 'C:\\Program Files', LOCALAPPDATA: 'C:\\Users\\x\\AppData\\Local' };+ assert.equal(browserCandidates('win32', win)[0].name, 'Google Chrome');+ assert.deepEqual(findBrowser({ platform: 'win32', env: win, exists: p => /msedge\.exe$/.test(p) }).name, 'Microsoft Edge');+ assert.equal(findBrowser({ platform: 'win32', env: win, exists: () => false }).found, false);+ assert.equal(findBrowser({ platform: 'darwin', exists: p => p.includes('Google Chrome') }).found, true);+});++// ------------------------------------------------------------------------------------ the app's own tool server (MCP)++function mcpSession(dir, mode) {+ const child = spawn(process.execPath, [CUSTOM_TOOLS_SCRIPT, mode, join(dir, 'agent', 'custom-tools.json')], { stdio: ['pipe', 'pipe', 'inherit'], windowsHide: true });+ const waiting = new Map();+ createInterface({ input: child.stdout }).on('line', line => { const m = JSON.parse(line); waiting.get(m.id)?.(m); });+ let id = 0;+ const ask = (method, params) => new Promise(resolve => { const n = ++id; waiting.set(n, resolve); child.stdin.write(JSON.stringify({ jsonrpc: '2.0', id: n, method, params }) + '\n'); });+ return { ask, close: () => child.kill() };+}++test('tool server: instruction tools return the person\'s text; command tools run their command with the assistant\'s text only in STRATA_INPUT', async () => {+ const dir = tmp();+ try {+ const script = join(dir, 'show.mjs');+ writeFileSync(script, "console.log('got:' + process.env.STRATA_INPUT); if (process.env.STRATA_INPUT === 'fail') process.exit(3);");+ const { tools } = make(dir);+ tools.addCustom({ name: 'Notes', about: 'House style.', kind: 'instructions', instructions: 'Start with one line.' });+ tools.addCustom({ name: 'Show', about: 'Shows its input.', kind: 'command', command: `"${process.execPath}" "${script}"` });+ const instr = mcpSession(dir, 'instructions');+ try {+ assert.equal((await instr.ask('initialize', { protocolVersion: '2024-11-05' })).result.serverInfo.name, 'strata-instructions');+ const list = (await instr.ask('tools/list')).result.tools;+ assert.deepEqual(list.map(t => [t.name, t.description]), [['notes', 'House style.']]);+ assert.equal((await instr.ask('tools/call', { name: 'notes', arguments: {} })).result.content[0].text, 'Start with one line.');+ } finally { instr.close(); }+ const cmd = mcpSession(dir, 'commands');+ try {+ await cmd.ask('initialize', {});+ assert.deepEqual((await cmd.ask('tools/list')).result.tools.map(t => t.name), ['show']);+ const ok = (await cmd.ask('tools/call', { name: 'show', arguments: { input: 'hello; echo HACKED' } })).result;+ assert.equal(ok.isError, false); assert.equal(ok.content[0].text, 'got:hello; echo HACKED', 'the text is data, never a command');+ const bad = (await cmd.ask('tools/call', { name: 'show', arguments: { input: 'fail' } })).result;+ assert.equal(bad.isError, true);+ assert.match((await cmd.ask('tools/call', { name: 'gone', arguments: {} })).result.content[0].text, /no longer available/);+ tools.update({ custom: { show: false } });+ assert.deepEqual((await cmd.ask('tools/list')).result.tools, [], 'a change in Settings is seen without a restart');+ } finally { cmd.close(); }+ } finally { cleanup(dir); }+});++// ------------------------------------------------------------------------------------------------- real Harness++async function rig(t, setup) {+ const worker = await startScriptedWorker({});+ const dir = tmp();+ const ws = join(dir, 'ws'); mkdirSync(ws, { recursive: true });+ const { tools } = make(dir);+ setup?.(tools, dir);+ const local = [{ id: 'inst-1', label: 'Scripted', baseUrl: worker.url, modelId: 'scripted-model', context: 32768, slots: 2 }];+ const bridge = createAgentBridge({ dataDir: dir, localInstances: () => local, connections: () => [], tools: () => tools.profileView(), env: process.env, startTimeoutMs: 90_000 });+ t.after(async () => { await bridge.close(); await worker.close(); cleanup(dir); });+ const run = async (chatId, tool, args, { mode = 'bypass', answer } = {}) => {+ const frames = [];+ await bridge.runTurn({ chatId, message: `SCRIPT:call ${tool} ${JSON.stringify(args)}`, workspacePath: ws, mode, onFrame: f => {+ frames.push(f);+ if (answer && f.type === 'agent-tool' && f.tool.state === 'proposed') void bridge.approve({ chatId, callId: f.tool.id, decision: answer }).catch(() => {});+ } });+ return frames.filter(f => f.type === 'agent-tool').map(f => f.tool);+ };+ return { worker, bridge, tools, dir, ws, run, offered: () => [...worker.requests].reverse().find(q => q.toolNames.length)?.toolNames.sort() };+}++test('real Harness: the person\'s own tools are offered; an instruction tool never asks, a command asks in Manual and is refused in Plan', { skip }, async (t) => {+ const r = await rig(t, (tools, dir) => {+ const script = join(dir, 'touch.mjs');+ writeFileSync(script, "import { writeFileSync } from 'node:fs'; writeFileSync(process.argv[2], 'ran'); console.log('ok:' + process.env.STRATA_INPUT);");+ tools.addCustom({ name: 'House style', about: 'Write in our house style.', kind: 'instructions', instructions: 'Always start with one line.' });+ tools.addCustom({ name: 'Make marker', about: 'Writes a marker file.', kind: 'command', command: `"${process.execPath}" "${script}" "${join(dir, 'marker.txt')}"` });+ });+ const marker = join(r.dir, 'marker.txt');+ const first = await r.run('c1', 'mcp__strata-instructions__house_style', {}, { mode: 'manual' });+ assert.ok(r.offered().includes('mcp__strata-instructions__house_style') && r.offered().includes('mcp__strata-commands__make_marker'), 'both of the person\'s tools are offered');+ assert.ok(!first.some(c => c.state === 'proposed'), 'an instruction tool never asks');+ assert.match(first.at(-1).result, /Always start with one line/);+ const plan = await r.run('c2', 'mcp__strata-commands__make_marker', { input: 'p' }, { mode: 'plan' });+ assert.ok(!existsSync(marker), 'Plan mode refused the command');+ assert.ok(!plan.some(c => c.state === 'done' && /ok:/.test(c.result ?? '')));+ const denied = await r.run('c3', 'mcp__strata-commands__make_marker', { input: 'd' }, { mode: 'manual', answer: 'deny' });+ assert.ok(denied.some(c => c.state === 'proposed') && !existsSync(marker), 'Manual asked first, and a No ran nothing');+ const approved = await r.run('c4', 'mcp__strata-commands__make_marker', { input: 'yes' }, { mode: 'manual', answer: 'approve' });+ assert.ok(approved.some(c => c.state === 'proposed'));+ assert.equal(approved.at(-1).state, 'done'); assert.match(approved.at(-1).result, /ok:yes/); assert.ok(existsSync(marker));+});++test('real Harness: switching a tool off in Settings takes it away from the next message; web search is there with no key', { skip }, async (t) => {+ const r = await rig(t);+ await r.run('o1', 'read', { file_path: join(r.ws, 'nothing.txt') });+ const before = r.offered();+ assert.ok(before.includes('web_search') && before.includes('web_fetch') && before.includes('subagent') && before.includes('job_list'));+ r.tools.update({ groups: { jobs: false, helpers: false, search: false } });+ await r.run('o2', 'read', { file_path: join(r.ws, 'nothing.txt') });+ const after = r.offered();+ assert.ok(!after.includes('web_search') && !after.includes('subagent') && !after.includes('job_list'), 'the three groups are gone');+ assert.ok(after.includes('web_fetch') && after.includes('read') && after.includes('pwsh') !== after.includes('bash'), 'the rest stays');+});diff --git a/apps/host/tests/agent-tools.test.mjs b/apps/host/tests/agent-tools.test.mjsindex 5a6bf0afa..a35986fbe 100644--- a/apps/host/tests/agent-tools.test.mjs+++ b/apps/host/tests/agent-tools.test.mjs@@ -49,8 +49,8 @@ test('the preset: our list is the Harness standard list minus exactly what the aassert.deepEqual(ids, STANDARD_PLUGIN_IDS.filter(id => !REMOVED_PLUGIN_IDS.includes(id) && id !== 'tool-bash'));assert.ok(standardPreset({ platform: 'linux' }).plugins.some(p => p.id === 'tool-bash') && !standardPreset({ platform: 'linux' }).plugins.some(p => p.id === 'tool-pwsh'));const web = standardPreset().plugins.find(p => p.id === 'tool-web').config;- assert.equal(web.fetch, true); assert.equal(web.search, false);- assert.equal(standardPreset({ search: true }).plugins.find(p => p.id === 'tool-web').config.search, true, 'a search key can switch it back on');+ assert.equal(web.fetch, true); assert.equal(web.search, false, 'the preset alone leaves search to the profile, which switches it on (DuckDuckGo needs no key)');+ assert.equal(standardPreset({ search: true }).plugins.find(p => p.id === 'tool-web').config.search, true);assert.ok(!ids.includes('tool-schedule'), 'Tasks is the only scheduler');});@@ -101,12 +101,12 @@ async function rig(t) {const cards = frames => frames.filter(f => f.type === 'agent-tool').map(f => f.tool);const last = frames => cards(frames).at(-1);-test('the agent is offered exactly these tools: everything useful, no scheduler, no search without a key, no unanswerable question', { skip }, async (t) => {+test('the agent is offered exactly these tools: everything useful including web search (no key needed), no scheduler, no unanswerable question', { skip }, async (t) => {const r = await rig(t);await r.run('inv', 'SCRIPT:echo hi');const offered = [...r.worker.requests].reverse().find(q => q.toolNames.length)?.toolNames.sort();- assert.deepEqual(offered, ['create_goal', 'edit', 'get_goal', 'glob', 'grep', 'interrupt_agent', 'job_kill', 'job_list', 'job_output', 'list_agents', 'present', 'pwsh', 'read', 'read_image', 'send_message', 'skill', 'subagent', 'subagent_fork', 'todo_write', 'update_goal', 'web_fetch', 'workflow', 'working_directory', 'write'].sort());- assert.ok(!offered.some(n => n.startsWith('schedule_')) && !offered.includes('web_search') && !offered.includes('ask_user_question') && !offered.includes('exit_plan_mode'));+ assert.deepEqual(offered, ['create_goal', 'edit', 'get_goal', 'glob', 'grep', 'interrupt_agent', 'job_kill', 'job_list', 'job_output', 'list_agents', 'present', 'pwsh', 'read', 'read_image', 'send_message', 'skill', 'subagent', 'subagent_fork', 'todo_write', 'update_goal', 'web_fetch', 'web_search', 'workflow', 'working_directory', 'write'].sort());+ assert.ok(!offered.some(n => n.startsWith('schedule_')) && offered.includes('web_search') && !offered.includes('ask_user_question') && !offered.includes('exit_plan_mode'));});test('a tool from each family runs and shows a card the page can render', { skip }, async (t) => {