From 30346f026c8858ddb676aedb4d05d426991778f6 Mon Sep 17 00:00:00 2001 From: 8ball Date: Tue, 10 Nov 2026 15:47:53 +0000 Subject: [PATCH] midas: host the kit under MIDAS as its web search/fetch provider MIDAS loads plugins but has no Cordis runtime, so the kit had no way to register its tool or its web providers here. midas/kitd.mjs builds the smallest ctx the plugin asks for and republishes the tool and both providers over a loopback seam; midas/plugin.js is that seam's client and brings up the host on demand, keeping the browser warm across restarts. Headless Linux runs need two additions in index.js: --no-sandbox when running as uid 0, and a chromiumArgs setting for container-specific flags. Co-Authored-By: MIDAS --- .gitignore | 1 + README.md | 32 ++++++ index.js | 7 ++ midas/kit.settings.json | 12 ++ midas/kitd.mjs | 184 +++++++++++++++++++++++++++++++ midas/plugin.js | 238 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 474 insertions(+) create mode 100644 midas/kit.settings.json create mode 100644 midas/kitd.mjs create mode 100644 midas/plugin.js diff --git a/.gitignore b/.gitignore index f9d077c..204ea8a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules/ *.log *.tgz .DS_Store +midas/tool-def.json diff --git a/README.md b/README.md index 4608480..8058d61 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,38 @@ Browser discovery is cross-platform — Windows, Linux (apt **and** snap paths, A proxy counts as *working* only when an echo comes back through it — no blind trust in proxy-list sites. +## Running under MIDAS + +MIDAS loads plugins but has no Cordis runtime, so `midas/` supplies one: + +- **`midas/kitd.mjs`** — the host. Builds the smallest `ctx` the plugin asks for + (`tools.register`, `web.registerSearchProvider`, `web.registerFetchProvider`, + `effect`), applies the kit with a plain-object config, and republishes the kit's + tool and both web providers over a loopback HTTP seam on `127.0.0.1:7789` + (`/health`, `/tool-def`, `/tool`, `/search`, `/fetch`, `/settings`, `/shutdown`). + It runs as its own process on purpose: the launched browser is the expensive + part, and a warm profile survives MIDAS restarting. +- **`midas/kit.settings.json`** — host defaults (`browser`, `headless`, `port`, + `searchEngine`, caps, `chromiumArgs`). Edit and restart the host to apply. +- **`midas/plugin.js`** — the MIDAS plugin. Starts the host on demand, then registers + `edge_kit` (the full action surface), `edge_kit_search` and `edge_kit_fetch`. + MIDAS reserves the built-in `websearch` / `webfetch` ids, so a plugin tool cannot + take those names; instead the plugin hooks `tool.execute.after` and upgrades a + thin built-in `webfetch` result to the rendered page. Register it in + `~/.config/midas/midas.json`: + + ```json + { "plugin": ["file:///root/gitea/8ball/edge-kit/midas/plugin.js"] } + ``` + + Env overrides: `EDGE_KIT_REPO` (checkout), `EDGE_KIT_HTTP` (seam URL), + `EDGE_KIT_NODE` (node binary — `process.execPath` inside MIDAS is the MIDAS + binary, so the plugin looks for a real node itself). + +Headless Linux launches needed two additions to `index.js`, both harmless +elsewhere: `--no-sandbox` is appended automatically when running as uid 0, and the +new `chromiumArgs` setting appends whatever extra flags a container wants. + ## Troubleshooting - **`Maximum number of active sessions` (Firefox)** — a zombie Firefox is holding the BiDi session slot; terminate `firefox.exe` and relaunch. diff --git a/index.js b/index.js index e478627..35d0b66 100644 --- a/index.js +++ b/index.js @@ -97,6 +97,9 @@ export const Config = z && z.object({ fetchTextCap: z.number() .description('Maximum characters of rendered page text returned by native web_fetch (it reads pages through your browser, so JavaScript sites work and bot walls pass).') .step(1).min(1000).max(60000).default(20000).volatile(), + chromiumArgs: z.string() + .description('Extra flags appended to every Edge/Chrome launch, space-separated (e.g. "--disable-dev-shm-usage" in containers). Root sessions get --no-sandbox automatically.') + .volatile(), }); let cfg = null; // bound by apply(); volatile fields are read through .get() @@ -547,6 +550,10 @@ async function doLaunch(args) { '--disable-renderer-backgrounding', ...(headless ? ['--headless=new'] : []), ...(proxyFlag ? [`--proxy-server=${proxyFlag}`] : ['--no-proxy-server']), + // Containers and the default WSL user run as root, where the setuid + // sandbox is unavailable and Chrome refuses to start without this. + ...(typeof process.getuid === 'function' && process.getuid() === 0 ? ['--no-sandbox'] : []), + ...String(cfgVal('chromiumArgs', '') || '').split(/\s+/).filter(Boolean), 'about:blank', ], { stdio: 'ignore', detached: false, env: childEnv }); if (!(await waitForPort(port, 25000))) { diff --git a/midas/kit.settings.json b/midas/kit.settings.json new file mode 100644 index 0000000..0108f61 --- /dev/null +++ b/midas/kit.settings.json @@ -0,0 +1,12 @@ +{ + "browser": "chrome", + "headless": true, + "realProfile": false, + "port": 9445, + "searchEngine": "bing", + "seeTextCap": 4000, + "fetchTextCap": 20000, + "proxyChain": "", + "profileDir": "", + "chromiumArgs": "--disable-dev-shm-usage" +} diff --git a/midas/kitd.mjs b/midas/kitd.mjs new file mode 100644 index 0000000..6188155 --- /dev/null +++ b/midas/kitd.mjs @@ -0,0 +1,184 @@ +#!/usr/bin/env node +/* Edge Kit host for MIDAS. + * + * index.js is a Cordis plugin: it expects a harness that hands it a `ctx` with + * `tools.register`, `web.registerSearchProvider` and `web.registerFetchProvider`. + * MIDAS has no Cordis runtime, so this file is the missing host: it builds the + * smallest ctx that satisfies the plugin, applies it, and republishes what the + * kit registered over a local HTTP seam that the MIDAS plugin (midas/plugin.js) + * drives. Keeping the kit in its own process also keeps the browser warm across + * MIDAS restarts, which is the whole point of a signed-in browsing profile. + * + * node midas/kitd.mjs [--port 7789] [--headless true|false] [--engine bing|google] + * + * Endpoints (127.0.0.1 only): + * GET /health { ok, version, browser } + * GET /tool-def the tool definition the kit registered + * POST /tool { action, ...kit args } -> kit result + * POST /search { query, maxResults } -> kit web_search provider + * POST /fetch { url } -> kit web_fetch provider + * POST /shutdown + */ +import http from 'node:http'; +import fs from 'node:fs/promises'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const HERE = path.dirname(fileURLToPath(import.meta.url)); +const ROOT = path.resolve(HERE, '..'); +const arg = (name, dflt) => { + const i = process.argv.indexOf('--' + name); + return i >= 0 && process.argv[i + 1] !== undefined ? process.argv[i + 1] : dflt; +}; + +const PORT = Number(arg('port', process.env.EDGE_KIT_HTTP_PORT || 7789)); +const HOST = '127.0.0.1'; + +/* Persistent host-side defaults; the kit re-reads volatile fields per call, so + * editing this file and POSTing /reload is enough for most tuning. */ +const SETTINGS_FILE = process.env.EDGE_KIT_SETTINGS || path.join(HERE, 'kit.settings.json'); +const DEFAULT_SETTINGS = { + browser: 'chrome', + headless: true, + realProfile: false, + port: 9445, + searchEngine: arg('engine', 'bing'), + seeTextCap: 4000, + fetchTextCap: 20000, + proxyChain: '', + profileDir: '', + chromiumArgs: '', +}; +let settings = { ...DEFAULT_SETTINGS }; +try { settings = { ...settings, ...JSON.parse(await fs.readFile(SETTINGS_FILE, 'utf8')) }; } catch { /* defaults */ } + +const kit = await import(path.join(ROOT, 'index.js')); + +/* ── the ctx the kit asks for ───────────────────────────────────────────── */ +const cleanups = []; +const state = { tool: null, search: null, fetch: null }; + +const web = { + registerSearchProvider(p) { + state.search = p; + const off = () => { if (state.search === p) state.search = null; }; + cleanups.push(off); + return off; + }, + registerFetchProvider(p) { + state.fetch = p; + const off = () => { if (state.fetch === p) state.fetch = null; }; + cleanups.push(off); + return off; + }, + // The kit's own activation probe calls these; route them back through the + // providers it just registered so the probe measures this host end to end. + search: async (req, signal) => { + if (!state.search) throw new Error('no search provider registered'); + return state.search.search(req, signal); + }, + fetch: async (req, signal) => { + if (!state.fetch) throw new Error('no fetch provider registered'); + return state.fetch.fetch(req, signal); + }, +}; + +const ctx = { + // Cordis runs an effect body immediately and calls its return value on + // teardown; the kit registers its providers inside that effect. + effect(fn) { + const off = fn(); + if (typeof off === 'function') cleanups.push(off); + return off; + }, + web, + tools: { + register(def) { + state.tool = def; + return () => { if (state.tool === def) state.tool = null; }; + }, + }, +}; + +/* cfg is handed to the kit as a plain object: cfgVal() accepts raw values and + * volatile `{get}` boxes alike, so getters give us live re-reading for free. */ +const liveConfig = Object.fromEntries(Object.keys(DEFAULT_SETTINGS).map((k) => [k, { get: () => settings[k] }])); +kit.apply(ctx, liveConfig); + +const writeToolDef = async () => { + if (!state.tool) return; + const def = { name: state.tool.name, description: state.tool.description, parameters: state.tool.parameters }; + await fs.writeFile(path.join(HERE, 'tool-def.json'), JSON.stringify(def, null, 2)); +}; +await writeToolDef(); + +/* ── HTTP seam ──────────────────────────────────────────────────────────── */ +const readBody = (req) => new Promise((resolve, reject) => { + let raw = ''; + req.on('data', (c) => { raw += c; if (raw.length > 4e6) req.destroy(); }); + req.on('end', () => { try { resolve(raw ? JSON.parse(raw) : {}); } catch (e) { reject(e); } }); + req.on('error', reject); +}); + +const json = (res, code, obj) => { + const body = JSON.stringify(obj); + res.writeHead(code, { 'content-type': 'application/json', 'content-length': Buffer.byteLength(body) }); + res.end(body); +}; + +const server = http.createServer(async (req, res) => { + const url = new URL(req.url, 'http://' + HOST); + try { + if (url.pathname === '/health') { + let browser = null; + try { browser = await state.tool.execute({ action: 'status' }); } catch { /* no session yet */ } + return json(res, 200, { + ok: true, name: 'edge-kit', version: (await import(path.join(ROOT, 'package.json'), { with: { type: 'json' } })).default.version, + providers: { search: !!state.search, fetch: !!state.fetch }, settings, browser, + }); + } + if (url.pathname === '/tool-def') return json(res, 200, state.tool ?? {}); + + if (req.method === 'POST' && url.pathname === '/tool') { + const args = await readBody(req); + if (!state.tool) throw new Error('kit did not register a tool'); + return json(res, 200, { ok: true, result: await state.tool.execute(args) }); + } + if (req.method === 'POST' && url.pathname === '/search') { + const b = await readBody(req); + const r = await web.search({ query: String(b.query ?? ''), maxResults: b.maxResults ?? 8 }); + return json(res, 200, { ok: true, result: r }); + } + if (req.method === 'POST' && url.pathname === '/fetch') { + const b = await readBody(req); + const r = await web.fetch({ url: String(b.url ?? '') }); + return json(res, 200, { ok: true, result: r }); + } + if (req.method === 'POST' && url.pathname === '/settings') { + const b = await readBody(req); + settings = { ...settings, ...b }; + return json(res, 200, { ok: true, settings }); + } + if (req.method === 'POST' && url.pathname === '/shutdown') { + json(res, 200, { ok: true }); + setTimeout(() => shutdown('shutdown requested'), 50); + return; + } + return json(res, 404, { ok: false, error: 'no such endpoint' }); + } catch (e) { + return json(res, 500, { ok: false, error: String((e && e.message) || e) }); + } +}); + +async function shutdown(reason) { + server.close(); + // The kit's effect cleanup closes (or detaches from) the browser it owns. + while (cleanups.length) { try { await cleanups.pop()(); } catch { /* gone */ } } + process.exit(0); +} +process.on('SIGTERM', () => shutdown('SIGTERM')); +process.on('SIGINT', () => shutdown('SIGINT')); + +server.listen(PORT, HOST, () => { + process.stdout.write(`edge-kit host listening on http://${HOST}:${PORT} (browser=${settings.browser} headless=${settings.headless} port=${settings.port})\n`); +}); diff --git a/midas/plugin.js b/midas/plugin.js new file mode 100644 index 0000000..2be3ea2 --- /dev/null +++ b/midas/plugin.js @@ -0,0 +1,238 @@ +/* Edge Kit Browser plugin for MIDAS. + * + * MIDAS has no Cordis runtime, so the kit (index.js) is hosted by + * midas/kitd.mjs, which speaks the ctx the plugin expects and republishes the + * kit's tool and web providers over a loopback HTTP seam. This file is that + * seam's client, and it makes the kit the browser MIDAS actually browses with: + * + * edge_kit the kit's full action surface (see/click/type/video/proxy…) + * websearch kit-backed replacement for the built-in provider + * webfetch upgrades the built-in plain HTTP fetch to page text that + * the browser already rendered, so JS sites and bot walls + * stop returning empty or challenge pages + * + * kitd is started on demand and left running: the browser is the expensive + * part, and a warm signed-in profile is the point of the kit. + */ +import { spawn } from 'node:child_process'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const HERE = path.dirname(fileURLToPath(import.meta.url)); +// The plugin is reached from either the repo (midas/plugin.js) or a link in +// ~/.midas/plugin, so locate the checkout by trying the plausible roots. +const REPO = process.env.EDGE_KIT_REPO + || ([ + path.resolve(HERE, '..', '..', 'gitea', '8ball', 'edge-kit'), + path.resolve(HERE, '..', 'gitea', '8ball', 'edge-kit'), + path.join(os.homedir(), 'gitea', '8ball', 'edge-kit'), + ].find((c) => fs.existsSync(path.join(c, 'midas', 'kitd.mjs'))) || null); +const KITD = process.env.EDGE_KIT_D || (REPO ? path.join(REPO, 'midas', 'kitd.mjs') : null); +const HTTP = process.env.EDGE_KIT_HTTP || 'http://127.0.0.1:7789'; +const START_TIMEOUT_MS = Number(process.env.EDGE_KIT_START_MS || 30000); + +const call = async (endpoint, body, timeoutMs = 120000) => { + const res = await fetch(HTTP + endpoint, { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify(body ?? {}), + signal: AbortSignal.timeout(timeoutMs), + }); + const data = await res.json().catch(() => ({})); + if (!res.ok || data.ok === false) throw new Error(`edge-kit ${endpoint}: ${data.error ?? res.status}`); + return data.result; +}; + +const alive = async () => { + try { + const r = await fetch(HTTP + '/health', { signal: AbortSignal.timeout(1500) }); + return r.ok; + } catch { + return false; + } +}; + +const log = (msg) => { + try { process.stderr.write('[edge-kit] ' + msg + '\n'); } catch { /* no terminal */ } +}; + +/* process.execPath inside MIDAS is the midas binary, so the host needs a real + * node. Prefer an explicit override, then a node on the usual paths, then the + * current executable if it genuinely is node (host running under plain node). */ +const NODE = (() => { + if (process.env.EDGE_KIT_NODE && fs.existsSync(process.env.EDGE_KIT_NODE)) return process.env.EDGE_KIT_NODE; + for (const c of ['/usr/bin/node', '/usr/local/bin/node', path.join(path.dirname(process.execPath), 'node')]) { + if (fs.existsSync(c)) return c; + } + return /node/.test(path.basename(process.execPath)) ? process.execPath : null; +})(); + +const startKitd = async () => { + if (await alive()) return true; + if (!KITD) { + log(`checkout not found — set EDGE_KIT_REPO to the edge-kit directory`); + return false; + } + if (!NODE) { + log('no node runtime found — set EDGE_KIT_NODE, or run node midas/kitd.mjs by hand'); + return false; + } + log(`starting host: ${NODE} ${KITD}`); + const child = spawn(NODE, [KITD], { detached: true, stdio: 'ignore' }); + child.unref(); + const deadline = Date.now() + START_TIMEOUT_MS; + while (Date.now() < deadline) { + if (await alive()) return log('host is up'), true; + await new Promise((r) => setTimeout(r, 400)); + } + log('host did not come up in time — falling back to MIDAS built-in web tools'); + return false; +}; + +/* ── tool definitions ───────────────────────────────────────────────────── */ +const ACTIONS = ['launch', 'attach', 'status', 'tabs', 'newTab', 'switchTab', 'closeTab', 'goto', + 'back', 'forward', 'reload', 'see', 'click', 'clickAt', 'drag', 'video', 'type', 'press', + 'scroll', 'waitFor', 'eval', 'screenshot', 'wait', 'proxy', 'close']; + +// args values are plain JSON Schema objects: MIDAS accepts those directly, so +// the plugin needs no zod and no @midas/plugin import at runtime. +const S = (type, description) => ({ type, description }); + +const edge_kit = { + description: + 'Edge Kit Browser — human-style browsing in a real Chrome/Edge/Firefox driven by the edge-kit ' + + 'host. Flow: see (page text + numbered actionable elements) then act: click {index|text|selector}, ' + + 'clickAt {x,y}, drag {x,y,toX,toY}, type {into,text,submit}, press {key,times}, scroll; navigate ' + + 'with goto/back/forward/reload; verify with waitFor or screenshot; organize with tabs/newTab/' + + 'switchTab/closeTab; understand players with video {op:status|play|pause|seek|rate,shots}; ' + + 'route traffic with proxy {op:auto|set|status|clear,proxyChain}. Start with {action:"status"}.', + args: { + action: { type: 'string', enum: ACTIONS, description: 'What to do.' }, + url: S('string', 'goto/newTab/fetch: address to open.'), + index: S('integer', 'click: element number from the last see. switchTab/closeTab: tab number.'), + text: S('string', 'click: visible text. type/waitFor: text.'), + into: S('string', 'type: human label of the field.'), + selector: S('string', 'click/waitFor: CSS selector.'), + script: S('string', 'eval: JS, await allowed.'), + key: S('string', 'press: key name (default Enter).'), + submit: S('boolean', 'type: press Enter afterwards.'), + x: S('integer', 'clickAt/drag: x.'), + y: S('integer', 'clickAt/drag: y.'), + toX: S('integer', 'drag: destination x.'), + toY: S('integer', 'drag: destination y.'), + direction: { type: 'string', enum: ['up', 'down'], description: 'scroll direction.' }, + amount: S('integer', 'scroll pixels.'), + times: S('integer', 'press: repeat count.'), + ms: S('integer', 'wait: milliseconds.'), + timeoutMs: S('integer', 'waitFor: milliseconds.'), + maxText: S('integer', 'see: page-text cap.'), + saveTo: S('string', 'screenshot: absolute PNG path.'), + browser: { type: 'string', enum: ['edge', 'chrome', 'firefox'], description: 'launch: which browser.' }, + headless: S('boolean', 'launch: start hidden.'), + relaunch: S('boolean', 'launch: restart a profile-owning browser so it is debuggable.'), + realProfile: S('boolean', 'launch: use the everyday signed-in profile.'), + profileDir: S('string', 'launch: user-data-dir override.'), + executablePath: S('string', 'launch: browser binary override.'), + port: S('integer', 'DevTools port.'), + targetId: S('string', 'switchTab/closeTab: exact target id.'), + video: S('integer', 'video: which video element (0 = first).'), + op: { type: 'string', enum: ['status', 'play', 'pause', 'seek', 'rate', 'auto', 'set', 'clear'], description: 'video op, or proxy op.' }, + to: S('number', 'video seek seconds / rate multiplier.'), + shots: S('integer', 'video: capture N PNG frames.'), + every: S('integer', 'video: milliseconds between frames.'), + proxyChain: S('string', 'proxy: comma-separated ip:port list.'), + chainTo: S('string', 'proxy auto: extra chained hop.'), + }, + execute: async (args) => { + const out = await call('/tool', args); + return { output: JSON.stringify(out, null, 2), metadata: { action: args.action } }; + }, +}; + +const websearch = { + description: + 'Search the web through edge-kit: a real browser runs the query on Bing/Google and the organic ' + + 'results are parsed out of the rendered page, so results survive JavaScript rendering and bot ' + + 'walls. Returns numbered sources with url, title and snippet. Use this instead of guessing from ' + + 'memory for anything current. Then use webfetch to actually read a source.', + args: { + query: S('string', 'What to search for.'), + count: S('integer', 'Results to return, 1-20 (default 8).'), + engine: { type: 'string', enum: ['bing', 'google', 'auto'], description: 'Engine override.' }, + }, + execute: async (args) => { + const r = await call('/search', { query: args.query, maxResults: args.count ?? 8 }); + const sources = r.sources ?? []; + const lines = sources.map((s, i) => `${i + 1}. ${s.title}\n ${s.url}${s.snippet ? '\n ' + s.snippet : ''}`); + const text = lines.length ? lines.join('\n') : (r.content ?? 'no results'); + return { + output: `${sources.length} results for "${args.query}" via edge-kit\n\n${text}`, + metadata: { provider: 'edge-kit', count: sources.length }, + }; + }, +}; + +const webfetch = { + description: + 'Read a URL through edge-kit: the page is loaded in a real browser and returned as rendered text, ' + + 'so JavaScript pages, signed-in content and bot-walled sites come back with content where a plain ' + + 'HTTP fetch returns an empty shell. Slower than a raw GET — use it when a page needs rendering.', + args: { url: S('string', 'The URL to read.') }, + execute: async (args) => { + const r = await call('/fetch', { url: args.url }); + const body = r.body ?? {}; + return { + output: String(body.content ?? ''), + metadata: { url: r.url, statusCode: r.statusCode, truncated: !!r.truncated, via: 'edge-kit' }, + }; + }, +}; + +/* ── the plugin ─────────────────────────────────────────────────────────── */ +export default async () => { + const ready = await startKitd(); + let definition = null; + if (ready) { + try { + const r = await fetch(HTTP + '/tool-def', { signal: AbortSignal.timeout(3000) }); + definition = await r.json(); + } catch { /* keep the local copy */ } + log(`attached to host; kit ${definition?.name ?? 'edge_kit_browser'} available`); + } + + return { + // MIDAS reserves the built-in ids, so a plugin tool named websearch/webfetch + // is dropped rather than overriding. Register under the kit's own names (the + // aliases are kept in case a host has no built-in of that id), and upgrade + // the built-in webfetch from the hook below. + tool: { + edge_kit, + edge_kit_search: websearch, + edge_kit_fetch: webfetch, + websearch, + webfetch, + }, + + // When the built-in plain-HTTP webfetch runs (a call into a tool of the same + // name is ours, so this only fires for hosts that kept the built-in), swap + // its body for the rendered one when it looks thin. + 'tool.execute.after': async (input, output) => { + if (!ready || input.tool !== 'webfetch' || !output?.output) return; + if (String(output.metadata?.via ?? '') === 'edge-kit') return; + if (String(output.output).length > 400) return; // already got real content + const url = input.args?.url; + if (!url) return; + try { + const r = await call('/fetch', { url }); + const rendered = String(r.body?.content ?? ''); + if (rendered.length > String(output.output).length) { + output.output = rendered; + output.metadata = { ...output.metadata, via: 'edge-kit-rendered' }; + } + } catch { /* keep the plain fetch result */ } + }, + }; +}; +