Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 30346f026c | |||
| bf30e64d59 | |||
| 7fdd0fc543 | |||
| 0cb2daa402 | |||
| cb6c077541 |
@@ -2,3 +2,4 @@ node_modules/
|
|||||||
*.log
|
*.log
|
||||||
*.tgz
|
*.tgz
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
midas/tool-def.json
|
||||||
|
|||||||
@@ -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.
|
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
|
## Troubleshooting
|
||||||
|
|
||||||
- **`Maximum number of active sessions` (Firefox)** — a zombie Firefox is holding the BiDi session slot; terminate `firefox.exe` and relaunch.
|
- **`Maximum number of active sessions` (Firefox)** — a zombie Firefox is holding the BiDi session slot; terminate `firefox.exe` and relaunch.
|
||||||
|
|||||||
@@ -97,6 +97,9 @@ export const Config = z && z.object({
|
|||||||
fetchTextCap: z.number()
|
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).')
|
.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(),
|
.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()
|
let cfg = null; // bound by apply(); volatile fields are read through .get()
|
||||||
@@ -547,6 +550,10 @@ async function doLaunch(args) {
|
|||||||
'--disable-renderer-backgrounding',
|
'--disable-renderer-backgrounding',
|
||||||
...(headless ? ['--headless=new'] : []),
|
...(headless ? ['--headless=new'] : []),
|
||||||
...(proxyFlag ? [`--proxy-server=${proxyFlag}`] : ['--no-proxy-server']),
|
...(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',
|
'about:blank',
|
||||||
], { stdio: 'ignore', detached: false, env: childEnv });
|
], { stdio: 'ignore', detached: false, env: childEnv });
|
||||||
if (!(await waitForPort(port, 25000))) {
|
if (!(await waitForPort(port, 25000))) {
|
||||||
@@ -1367,27 +1374,43 @@ const FETCH_EXTRACT = `(async () => {
|
|||||||
return { url: location.href, status, text: document.body ? document.body.innerText : '' };
|
return { url: location.href, status, text: document.body ? document.body.innerText : '' };
|
||||||
})()`;
|
})()`;
|
||||||
|
|
||||||
const SEARCH_EXTRACT = `(() => {
|
const SEARCH_EXTRACT = `(async () => {
|
||||||
|
const parse = () => {
|
||||||
const clean = (s) => String(s || '').replace(/\\s+/g, ' ').trim();
|
const clean = (s) => String(s || '').replace(/\\s+/g, ' ').trim();
|
||||||
const out = []; const seen = new Set();
|
const out = []; const seen = new Set();
|
||||||
// Engine-agnostic organic parse: a result headline is a heading inside a
|
const selfUrl = (u) => /^https?:\\/\\/(?:www\\.)?(?:bing|google|duckduckgo)\\./i.test(u);
|
||||||
// link on Google and Bing alike — layout class names drift, this doesn't.
|
// Organic result headlines come in both directions now: heading inside the
|
||||||
for (const a of document.querySelectorAll('a[href]')) {
|
// link (Google: a > h3) AND link inside the heading (Bing: h2 > a). Match
|
||||||
const heading = a.querySelector('h2, h3');
|
// both — engine class names drift, the heading/link nesting does not.
|
||||||
if (!heading) continue;
|
for (const node of document.querySelectorAll('h2 a, h3 a, a h2, a h3')) {
|
||||||
let url = a.href;
|
if (out.length >= 20) break;
|
||||||
if (url.startsWith('/url?')) { const m = url.match(/[?&]url=([^&]+)/); if (m) { try { url = decodeURIComponent(m[1]); } catch {} } }
|
const link = node.matches('a') ? node : node.closest('a');
|
||||||
if (!/^https?:/i.test(url) || seen.has(url)) continue;
|
if (!link) continue;
|
||||||
const title = clean(heading.innerText);
|
let url = link.href;
|
||||||
|
// Engines wrap organic URLs: Google /url?url=..., Bing /ck/a?u=a1<base64>.
|
||||||
|
// Unwrap first — then apply the own-domain filter, or the wrappers eat
|
||||||
|
// every real result.
|
||||||
|
{ const m = url.match(/[?&]u=([^&]+)/); if (m) { let v = m[1]; if (v.startsWith('a1')) v = v.slice(2); try { const d = atob(v.replace(/-/g, '+').replace(/_/g, '/')); if (/^https?:/i.test(d)) url = d; } catch {} } }
|
||||||
|
{ const m = url.match(/[?&](?:url|q)=([^&]+)/); if (m) { try { const d = decodeURIComponent(m[1]); if (/^https?:/i.test(d)) url = d; } catch {} } }
|
||||||
|
if (!/^https?:/i.test(url) || seen.has(url) || selfUrl(url)) continue;
|
||||||
|
const title = clean(link.innerText);
|
||||||
if (!title || title.length < 4) continue;
|
if (!title || title.length < 4) continue;
|
||||||
const box = a.closest('li,div.g,div.tF2Cxc,li.b_algo') || a.parentElement;
|
const box = link.closest('li,div.g,div.tF2Cxc,li.b_algo,div.b_title,div[class*=result]') || link.parentElement;
|
||||||
let snippet = box ? clean(box.innerText) : '';
|
let snippet = box ? clean(box.innerText) : '';
|
||||||
if (snippet.startsWith(title)) snippet = snippet.slice(title.length).trim();
|
if (snippet.startsWith(title)) snippet = snippet.slice(title.length).trim();
|
||||||
seen.add(url);
|
seen.add(url);
|
||||||
out.push({ url, title: title.slice(0, 200), ...(snippet ? { snippet: snippet.slice(0, 400) } : {}) });
|
out.push({ url, title: title.slice(0, 200), ...(snippet ? { snippet: snippet.slice(0, 400) } : {}) });
|
||||||
if (out.length >= 20) break;
|
|
||||||
}
|
}
|
||||||
return { sources: out, page: { url: location.href, title: clean(document.title) } };
|
return { sources: out, page: { url: location.href, title: clean(document.title) } };
|
||||||
|
};
|
||||||
|
// Engines serving datacenter exits hand back a redirect shell first
|
||||||
|
// (Bing: &rdr=1&rdrig=…) and hydrate organics after readyState — poll
|
||||||
|
// in-page until real results exist (max ~6s) instead of parsing the shell.
|
||||||
|
for (let i = 0; i < 12; i++) {
|
||||||
|
const r = parse();
|
||||||
|
if (r.sources.length || i >= 11) return r;
|
||||||
|
await new Promise((res) => setTimeout(res, 500));
|
||||||
|
}
|
||||||
})()`;
|
})()`;
|
||||||
|
|
||||||
const edgeFetchProvider = {
|
const edgeFetchProvider = {
|
||||||
@@ -1409,7 +1432,7 @@ const edgeSearchProvider = {
|
|||||||
async search(request, signal) {
|
async search(request, signal) {
|
||||||
const n = Math.min(Math.max(request.maxResults ?? 10, 1), 20);
|
const n = Math.min(Math.max(request.maxResults ?? 10, 1), 20);
|
||||||
const q = encodeURIComponent(request.query);
|
const q = encodeURIComponent(request.query);
|
||||||
const engine = cfgVal('searchEngine', 'google') === 'bing' ? 'bing' : 'google';
|
const engine = cfgVal('searchEngine', 'bing') === 'google' ? 'google' : 'bing';
|
||||||
const build = (e) => e === 'bing'
|
const build = (e) => e === 'bing'
|
||||||
? `https://www.bing.com/search?q=${q}&count=${Math.min(n * 2, 30)}`
|
? `https://www.bing.com/search?q=${q}&count=${Math.min(n * 2, 30)}`
|
||||||
: `https://www.google.com/search?q=${q}&num=${Math.min(n * 2, 20)}`;
|
: `https://www.google.com/search?q=${q}&num=${Math.min(n * 2, 20)}`;
|
||||||
|
|||||||
@@ -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"
|
||||||
|
}
|
||||||
+184
@@ -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`);
|
||||||
|
});
|
||||||
+238
@@ -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 */ }
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@local/edge-kit-easy",
|
"name": "@local/edge-kit-easy",
|
||||||
"version": "3.6.4",
|
"version": "3.6.8",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"description": "Edge Kit Browser — human-style browsing for agents in Edge, Chrome, or Firefox: see, click, type, video understanding, gameplay; live-attach-first over CDP/BiDi with a native settings page and the native web seam",
|
"description": "Edge Kit Browser — human-style browsing for agents in Edge, Chrome, or Firefox: see, click, type, video understanding, gameplay; live-attach-first over CDP/BiDi with a native settings page and the native web seam",
|
||||||
|
|||||||
Reference in New Issue
Block a user