/* 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 */ } }, }; };