Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 30346f026c | |||
| bf30e64d59 | |||
| 7fdd0fc543 | |||
| 0cb2daa402 | |||
| cb6c077541 | |||
| a2afeff8e0 | |||
| 0ed41d1f2f |
@@ -2,3 +2,4 @@ node_modules/
|
||||
*.log
|
||||
*.tgz
|
||||
.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.
|
||||
|
||||
## 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.
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
/**
|
||||
* Edge Kit Browser v3 — browsing the way a human does it.
|
||||
*
|
||||
* Local branch overlay: EDGE_KIT_NATIVE_FIXES (pending review in 'native-fixes').
|
||||
*
|
||||
* Design: agents perceive with `see` (visible text + numbered actionable
|
||||
* elements, across shadow DOM and dialogs), act like a person (`click` a
|
||||
* numbered element or the text they can read, `type` into a field by its
|
||||
@@ -21,9 +23,38 @@ import { promises as fs } from 'node:fs';
|
||||
// resolve it by bare name, so import it from the running installation (the
|
||||
// very file the Web search plugin loads). If that ever fails to resolve, the
|
||||
// kit still works — only the settings page stays hidden.
|
||||
/* EDGE_KIT_NATIVE_PORTABILITY: resolve schemastery from the running DeepSeek Harness
|
||||
* installation instead of a hardcoded per-user path. Candidate roots cover Electron's
|
||||
* resources directory (with app.asar), a resources folder beside the executable, and the
|
||||
* per-user install location; the CommonJS build is tried before the ES module build. If
|
||||
* none resolves the kit still runs and only the settings page stays hidden. */
|
||||
let z = null;
|
||||
try {
|
||||
({ default: z } = await import('file:///C:/Users/Administrator/AppData/Local/Programs/DeepSeek%20Harness/resources/app.asar/dsh/node_modules/@deepseek-ai/schemastery/lib/index.mjs'));
|
||||
await (async () => {
|
||||
const [{ createRequire }, { pathToFileURL }, pathMod] = await Promise.all([
|
||||
import('node:module'),
|
||||
import('node:url'),
|
||||
import('node:path'),
|
||||
]);
|
||||
const requireFromHere = createRequire(import.meta.url);
|
||||
const roots = [];
|
||||
try { if (process.resourcesPath) roots.push(process.resourcesPath); } catch { /* not Electron */ }
|
||||
try { roots.push(pathMod.join(pathMod.dirname(process.execPath), 'resources')); } catch { /* no execPath */ }
|
||||
if (process.env.LOCALAPPDATA) roots.push(pathMod.join(process.env.LOCALAPPDATA, 'Programs', 'DeepSeek Harness', 'resources'));
|
||||
for (const root of roots) {
|
||||
const base = pathMod.join(root, 'app.asar', 'dsh', 'node_modules', '@deepseek-ai', 'schemastery');
|
||||
try {
|
||||
const cjs = requireFromHere(pathMod.join(base, 'lib', 'index.cjs'));
|
||||
z = (cjs && (cjs.default || cjs)) || null;
|
||||
} catch { /* try the ES module build next */ }
|
||||
if (z) break;
|
||||
try {
|
||||
const esm = await import(pathToFileURL(pathMod.join(base, 'lib', 'index.mjs')).href);
|
||||
z = (esm && (esm.default || esm)) || null;
|
||||
} catch { /* try the next candidate root */ }
|
||||
if (z) break;
|
||||
}
|
||||
})();
|
||||
} catch { z = null; }
|
||||
|
||||
export const inject = ['tools', 'web'];
|
||||
@@ -66,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()
|
||||
@@ -266,8 +300,18 @@ function bidiCall(m, p) {
|
||||
});
|
||||
}
|
||||
|
||||
const KEYBI = { Enter: '\uE006', Tab: '\uE004', Space: ' ', Backspace: '\uE003', Escape: '\uE01B', ArrowUp: '\uE013', ArrowDown: '\uE015', ArrowLeft: '\uE012', ArrowRight: '\uE014', Home: '\uE010', End: '\uE011', Shift: '\uE008', Control: '\uE009', Alt: '\uE00A' };
|
||||
const keyVal = (params) => (typeof params.text === 'string' && params.text.length === 1 ? params.text : KEYBI[params.code || params.key] || (typeof params.key === 'string' ? params.key : ' '));
|
||||
const KEYBI = { Enter: '\uE006', Tab: '\uE004', Space: ' ', Backspace: '\uE003', Escape: '\uE00C', Delete: '\uE017', Insert: '\uE016', PageUp: '\uE00E', PageDown: '\uE00F', ArrowUp: '\uE013', ArrowDown: '\uE015', ArrowLeft: '\uE012', ArrowRight: '\uE014', Home: '\uE010', End: '\uE011', Shift: '\uE008', Control: '\uE009', Alt: '\uE00A' };
|
||||
// Named keys (Enter/Delete/…) must win over their one-character text form: a
|
||||
// "keyDown" value of '\r' is not the Enter key in WebDriver BiDi, and named keys
|
||||
// like Delete have no text at all. Character keys fall back to the typed text.
|
||||
const keyVal = (params) => {
|
||||
const named = KEYBI[params.code || params.key];
|
||||
if (named) return named;
|
||||
if (typeof params.text === 'string' && params.text.length === 1 && params.text !== '\r') return params.text;
|
||||
if (typeof params.code === 'string' && /^Key[A-Z]$/.test(params.code)) return params.code.slice(3).toLowerCase();
|
||||
if (typeof params.code === 'string' && /^Digit[0-9]$/.test(params.code)) return params.code.slice(5);
|
||||
return typeof params.key === 'string' ? params.key : ' ';
|
||||
};
|
||||
|
||||
async function bidiSend(method, params, sid) {
|
||||
const ctx = sid ?? session.context;
|
||||
@@ -292,7 +336,16 @@ async function bidiSend(method, params, sid) {
|
||||
return { targetInfos: (r.contexts || []).map((c) => ({ targetId: c.context, url: c.url || '', title: '', type: 'page' })) };
|
||||
}
|
||||
case 'Target.attachToTarget': session.context = params.targetId; return { sessionId: params.targetId };
|
||||
case 'Target.createTarget': { const r = await bidiCall('browsingContext.create', { url: params.url || 'about:blank', type: 'tab' }); return { targetId: r.context }; }
|
||||
case 'Target.createTarget': {
|
||||
// Firefox's browsingContext.create ignores an inline URL: create the tab
|
||||
// first and navigate it explicitly, then wait for the load to settle.
|
||||
const wanted = typeof params.url === 'string' && params.url ? params.url : 'about:blank';
|
||||
const r = await bidiCall('browsingContext.create', { type: 'tab' });
|
||||
if (wanted !== 'about:blank') {
|
||||
try { await bidiCall('browsingContext.navigate', { url: wanted, context: r.context, wait: 'complete' }); } catch { /* about:blank tab is usable */ }
|
||||
}
|
||||
return { targetId: r.context };
|
||||
}
|
||||
case 'Target.closeTarget': await bidiCall('browsingContext.close', { context: params.targetId }); return {};
|
||||
case 'Runtime.evaluate': {
|
||||
// Firefox's remote agent reads the draft-style `expression` param; the
|
||||
@@ -306,7 +359,18 @@ async function bidiSend(method, params, sid) {
|
||||
if (r.exceptionDetails) return { exceptionDetails: { text: r.exceptionDetails.text || JSON.stringify(r.exceptionDetails.stacktrace || {}) } };
|
||||
return { result: { value: decr(r.result) } };
|
||||
}
|
||||
case 'Input.insertText': await pointer('key', [{ type: 'text', text: params.text }]); return {};
|
||||
// Firefox rejects the draft `{type:'text'}` key action; type by dispatching
|
||||
// one keyDown/keyUp pair per code point (real input events, keeps focus and
|
||||
// selection behaviour), which is what BiDi actually supports.
|
||||
case 'Input.insertText': {
|
||||
const acts = [];
|
||||
for (const ch of [...String(params.text)]) {
|
||||
const value = ch === '\n' ? '\r' : ch;
|
||||
acts.push({ type: 'keyDown', value }, { type: 'keyUp', value });
|
||||
}
|
||||
if (acts.length) await pointer('key', acts);
|
||||
return {};
|
||||
}
|
||||
case 'Input.dispatchKeyEvent': {
|
||||
const v = keyVal(params);
|
||||
const acts = params.type === 'keyUp' ? [{ type: 'keyUp', value: v }] : params.type === 'keyDown' ? [{ type: 'keyDown', value: v }, { type: 'keyUp', value: v }] : [{ type: 'keyDown', value: v }];
|
||||
@@ -319,6 +383,7 @@ async function bidiSend(method, params, sid) {
|
||||
if (params.type === 'mouseReleased') acts.push({ type: 'pointerUp', x: params.x, y: params.y, button: 0 });
|
||||
await pointer('pointer', acts); return {};
|
||||
}
|
||||
case 'session.end': try { await bidiCall('session.end', {}); } catch { /* no session */ } return {};
|
||||
case 'Browser.close': try { await bidiCall('browser.close', {}); } catch { /* browser gone */ } return {};
|
||||
default: return {};
|
||||
}
|
||||
@@ -355,14 +420,28 @@ async function evaluate(expression) {
|
||||
|
||||
async function waitForLoad(timeoutMs = 15000) {
|
||||
const loaded = new Promise((resolve) => {
|
||||
let done = false, poll = null;
|
||||
const finish = (how) => {
|
||||
if (done) return;
|
||||
done = true;
|
||||
clearTimeout(timer);
|
||||
clearInterval(poll);
|
||||
eventHooks.delete(hook);
|
||||
resolve(how);
|
||||
};
|
||||
const hook = (msg) => {
|
||||
if (msg.method === 'Page.loadEventFired' && msg.sessionId === session?.sessionId) {
|
||||
eventHooks.delete(hook);
|
||||
resolve('load');
|
||||
}
|
||||
if (msg.method === 'Page.loadEventFired' && msg.sessionId === session?.sessionId) finish('load');
|
||||
};
|
||||
eventHooks.add(hook);
|
||||
setTimeout(() => { eventHooks.delete(hook); resolve('timeout'); }, timeoutMs);
|
||||
const timer = setTimeout(() => finish('timeout'), timeoutMs);
|
||||
// BiDi (Firefox) can fire the load event before a freshly created tab is
|
||||
// attached/subscribed, so an event-only wait misses it and burns the whole
|
||||
// timeout (newTab took ~15 s before this). Poll the document state as a
|
||||
// fast second signal.
|
||||
poll = setInterval(async () => {
|
||||
if (session?.engine !== 'bidi' || done) return;
|
||||
try { if ((await evaluate('document.readyState')) === 'complete') finish('readyState'); } catch { /* navigating */ }
|
||||
}, 250);
|
||||
});
|
||||
const how = await loaded;
|
||||
await new Promise((r) => setTimeout(r, 400)); // settle for hydration
|
||||
@@ -393,7 +472,17 @@ async function bidiHandshake() {
|
||||
// session.status answers, but commands only work after session.new
|
||||
// with capabilities — empirically probed against Firefox 158.
|
||||
await bidiCall('session.status', {});
|
||||
const r = await bidiCall('session.new', { capabilities: { alwaysMatch: {}, firstMatch: [{}] } });
|
||||
let r;
|
||||
try {
|
||||
r = await bidiCall('session.new', { capabilities: { alwaysMatch: {}, firstMatch: [{}] } });
|
||||
} catch (e) {
|
||||
// Firefox allows one BiDi session; a previous detach or another client can
|
||||
// hold it, and a bare WebSocket close does not release it.
|
||||
if (/Maximum number of active sessions/i.test(String((e && e.message) || ''))) {
|
||||
throw new Error('Firefox already has an active WebDriver BiDi session (a previous detach or another client left it behind). Restart that Firefox instance, then retry.');
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
session.bidiSessionId = r && r.sessionId;
|
||||
session.bidiReady = true;
|
||||
}
|
||||
@@ -401,7 +490,15 @@ async function bidiHandshake() {
|
||||
async function attachOnly(port) {
|
||||
const { ws, info } = await connect(port);
|
||||
session = { ws, port, child: null, headless: null, profileDir: null, attached: true, kind: 'unknown', engine: info.bidi ? 'bidi' : 'cdp' };
|
||||
await attachToPage();
|
||||
try {
|
||||
await attachToPage();
|
||||
} catch (error) {
|
||||
// Never leave a half-attached session behind: status would report
|
||||
// running:true with no active tab for every later call.
|
||||
try { ws.close(); } catch { /* already gone */ }
|
||||
session = null;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function doLaunch(args) {
|
||||
@@ -437,7 +534,9 @@ async function doLaunch(args) {
|
||||
'-profile', profileDir,
|
||||
'-no-remote',
|
||||
...(headless ? ['-headless'] : []),
|
||||
...(proxyFlag ? ['-proxy-server', proxyFlag] : ['-preferences', '{"network.proxy.type":0}']),
|
||||
// Firefox's -proxy-server takes host:port, not an http:// URL; a
|
||||
// proxy-less launch states direct traffic explicitly (network.proxy.type=0).
|
||||
...(proxyFlag ? ['-proxy-server', proxyFlag.replace(/^https?:\/\//, '')] : ['-preferences', '{"network.proxy.type":0}']),
|
||||
'about:blank',
|
||||
] : [
|
||||
`--remote-debugging-port=${port}`,
|
||||
@@ -451,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))) {
|
||||
@@ -678,7 +781,13 @@ async function egressVia(hops) {
|
||||
async function restartSession() {
|
||||
const la = session && session.launchArgs ? session.launchArgs : null;
|
||||
if (!session || session.attached || !la) return false;
|
||||
const { child, ws, kind } = session;
|
||||
const { child, ws, engine, kind } = session;
|
||||
// Close the browser itself before dropping the socket: for Firefox the spawn
|
||||
// handle is only the Windows launcher, so killing it can orphan the browser
|
||||
// and leave Firefox's single BiDi session registered (the next session.new
|
||||
// then fails with "Maximum number of active sessions").
|
||||
try { ws.send(JSON.stringify({ id: nextId++, method: engine === 'bidi' ? 'browser.close' : 'Browser.close', params: {} })); } catch { /* gone */ }
|
||||
await new Promise((r) => setTimeout(r, 300));
|
||||
session = null;
|
||||
try { ws.close(); } catch { /* gone */ }
|
||||
if (child && child.exitCode === null) child.kill();
|
||||
@@ -691,9 +800,11 @@ async function restartSession() {
|
||||
if (!(await waitFree(6000))) {
|
||||
// A survivor browser (Edge startup-boost, profile handoff, leftover window)
|
||||
// retook the port without our new flags — a fresh spawn would merge into it
|
||||
// and silently lose proxy routing. Kill instances of this browser kind
|
||||
// (same semantics as relaunch:true; logins survive, windows reopen).
|
||||
const exe = kind === 'firefox' ? 'firefox' : kind === 'chrome' ? 'chrome' : 'msedge';
|
||||
// and silently lose proxy routing. For Chromium, kill instances of this
|
||||
// browser kind (same semantics as relaunch:true; logins survive, windows
|
||||
// reopen). Firefox refuses: taskkill would close every Firefox window.
|
||||
if (kind === 'firefox') return false;
|
||||
const exe = kind === 'chrome' ? 'chrome' : 'msedge';
|
||||
const killCmd = process.platform === 'win32' ? `taskkill /IM ${exe}.exe /F` : `pkill -9 -x ${exe}`;
|
||||
await new Promise((r) => exec(killCmd, () => r()));
|
||||
if (!(await waitFree(8000))) return false;
|
||||
@@ -728,13 +839,20 @@ for (let c = 0; c < 26; c++) KEYCODES['Key' + String.fromCharCode(65 + c)] = [65
|
||||
for (let d = 0; d < 10; d++) KEYCODES['Digit' + d] = [48 + d, 48 + d];
|
||||
|
||||
async function pressKey(key, times = 1) {
|
||||
const vk = KEYCODES[key] || [key.length === 1 ? key.toUpperCase().charCodeAt(0) : 0, 0];
|
||||
const code = KEYCODES[key] ? key : (key.length === 1 ? `Key${key.toUpperCase()}` : '');
|
||||
const text = key === 'Space' ? ' ' : key === 'Enter' ? '\r' : (key.length === 1 ? key : undefined);
|
||||
const base = { key, code, windowsVirtualKeyCode: vk[0], nativeVirtualKeyCode: vk[1] };
|
||||
// Normalize the requested key: named keys are case-insensitive ("Space" and
|
||||
// "space" are the same key), and the character-carrying keys (Space, Enter)
|
||||
// must carry their real character in `key`/`text` — CDP otherwise reports an
|
||||
// empty event.key for the named form and games/forms never see the key.
|
||||
const requested = String(key);
|
||||
const named = Object.keys(KEYCODES).find((candidate) => candidate.toLowerCase() === requested.toLowerCase());
|
||||
const keyName = named || requested;
|
||||
const vk = KEYCODES[keyName] || [requested.length === 1 ? requested.toUpperCase().charCodeAt(0) : 0, 0];
|
||||
const code = KEYCODES[keyName] ? keyName : (requested.length === 1 ? `Key${requested.toUpperCase()}` : '');
|
||||
const char = keyName === 'Space' ? ' ' : keyName === 'Enter' ? '\r' : (requested.length === 1 ? requested : undefined);
|
||||
const base = { key: keyName === 'Space' ? ' ' : keyName, code, windowsVirtualKeyCode: vk[0], nativeVirtualKeyCode: vk[1] };
|
||||
for (let i = 0; i < times; i++) {
|
||||
await send('Input.dispatchKeyEvent', { type: text !== undefined ? 'keyDown' : 'rawKeyDown', ...base, ...(text !== undefined ? { text } : {}) }, session.sessionId);
|
||||
await send('Input.dispatchKeyEvent', { type: text !== undefined ? 'keyUp' : 'rawKeyDown', ...base }, session.sessionId).catch(async () => {
|
||||
await send('Input.dispatchKeyEvent', { type: char !== undefined ? 'keyDown' : 'rawKeyDown', ...base, ...(char !== undefined ? { text: char } : {}) }, session.sessionId);
|
||||
await send('Input.dispatchKeyEvent', { type: 'keyUp', ...base }, session.sessionId).catch(async () => {
|
||||
await send('Input.dispatchKeyEvent', { type: 'keyUp', ...base }, session.sessionId);
|
||||
});
|
||||
if (times > 1) await new Promise((r) => setTimeout(r, 90));
|
||||
@@ -764,8 +882,8 @@ const WALKER = `
|
||||
try { const cs = getComputedStyle(el); if (cs.visibility === 'hidden' || cs.display === 'none' || cs.opacity === '0') return null; } catch { }
|
||||
return r; };
|
||||
const clean = (s) => String(s || '').replace(/\\s+/g, ' ').trim();
|
||||
const nameOf = (el) => clean(el.innerText).slice(0, 70)
|
||||
|| clean(el.getAttribute('aria-label')) || clean(el.getAttribute('placeholder'))
|
||||
const nameOf = (el) => clean(el.getAttribute('aria-label')).slice(0, 70)
|
||||
|| clean(el.innerText).slice(0, 70) || clean(el.getAttribute('placeholder'))
|
||||
|| clean(el.getAttribute('title')) || clean(el.getAttribute('alt')) || clean(el.value) || '';
|
||||
const isInter = (el) => {
|
||||
const tag = el.tagName.toLowerCase(); const role = (el.getAttribute('role') || '').toLowerCase();
|
||||
@@ -791,7 +909,7 @@ async function seeOnce(opts = {}) {
|
||||
const key = tag + '|' + (el.getAttribute('role') || '') + '|' + name;
|
||||
if (seen.has(key)) continue; seen.add(key);
|
||||
els.push({ role: (el.getAttribute('role') || (tag === 'a' ? 'link' : tag)), name,
|
||||
x: Math.round(r.x + r.width / 2), y: Math.round(r.y + r.height / 2) });
|
||||
x: Math.round(r.x + r.width / 2), y: Math.round(r.y + r.height / 2), el });
|
||||
if (els.length >= 60) return;
|
||||
}
|
||||
}
|
||||
@@ -808,9 +926,13 @@ async function seeOnce(opts = {}) {
|
||||
}
|
||||
}
|
||||
dwalk(document);
|
||||
// Keep the live element references from this see so click{index} can
|
||||
// re-resolve its row after scrolling or DOM movement instead of firing at a
|
||||
// stale, cached coordinate (which silently no-ops on a moved page).
|
||||
try { window.__edgeKitElements = els.map((entry) => entry.el); } catch { }
|
||||
return { url: location.href, title: document.title,
|
||||
text: text.slice(0, ${Math.min(opts.maxText ?? cfgVal('seeTextCap', 2200), 6000)}),
|
||||
elements: els, dialogs: [...new Set(dialogs)].slice(0, 4) };
|
||||
elements: els.map(({ role, name, x, y }) => ({ role, name, x, y })), dialogs: [...new Set(dialogs)].slice(0, 4) };
|
||||
})()`;
|
||||
return evaluate(js);
|
||||
}
|
||||
@@ -821,8 +943,9 @@ async function findByName(needle) {
|
||||
const want = ${JSON.stringify(String(needle).toLowerCase())};
|
||||
let best = null;
|
||||
function walk(root) {
|
||||
for (const el of root.querySelectorAll('a,button,input,textarea,select,summary,[role],[jsaction],[contenteditable]')) {
|
||||
for (const el of root.querySelectorAll('*')) {
|
||||
if (el.shadowRoot) walk(el.shadowRoot);
|
||||
if (!isInter(el) && !el.matches('a,button,input,textarea,select,summary,[role],[jsaction],[contenteditable]')) continue;
|
||||
const label = nameOf(el).toLowerCase();
|
||||
if (!label.includes(want)) continue;
|
||||
const r = vis(el); if (!r) continue;
|
||||
@@ -848,8 +971,9 @@ async function focusField(by) {
|
||||
};
|
||||
let best = null;
|
||||
function walk(root) {
|
||||
for (const el of root.querySelectorAll('input,textarea,select,[contenteditable="true"]')) {
|
||||
for (const el of root.querySelectorAll('*')) {
|
||||
if (el.shadowRoot) walk(el.shadowRoot);
|
||||
if (!el.matches('input,textarea,select,[contenteditable="true"]')) continue;
|
||||
if (el.type === 'hidden' || el.disabled) continue;
|
||||
const hay = labelOf(el);
|
||||
if (!hay.includes(want)) continue;
|
||||
@@ -876,18 +1000,30 @@ async function run(args) {
|
||||
switch (action) {
|
||||
case 'launch': {
|
||||
if (args.relaunch) {
|
||||
// Take over a profile-owning Edge we do not control (or do): close all
|
||||
// msedge instances, then spawn fresh debuggable. Session restore brings
|
||||
// the user's tabs back on the reopened browser.
|
||||
// Take over a profile-owning browser we do not control (or do): close
|
||||
// its instances, then spawn a fresh debuggable one. Session restore
|
||||
// brings the user's tabs back on the reopened browser. Only the engine
|
||||
// being relaunched is touched, and Firefox refuses outright because
|
||||
// taskkill would close every Firefox window the user has open.
|
||||
const relKind = args.executablePath ? kindOf(args.executablePath) : String(args.browser || cfgVal('browser', 'edge')).toLowerCase();
|
||||
if (relKind === 'firefox') throw new Error('relaunch is not supported for Firefox: it would kill every Firefox window. Use an isolated profileDir, or close that Firefox yourself.');
|
||||
try { session?.ws?.close(); } catch { /* gone */ }
|
||||
session = null;
|
||||
await new Promise((res) => exec('taskkill /IM msedge.exe /F', () => res()));
|
||||
await new Promise((res) => exec(`taskkill /IM ${relKind === 'chrome' ? 'chrome.exe' : 'msedge.exe'} /F`, () => res()));
|
||||
await new Promise((r) => setTimeout(r, 1800));
|
||||
}
|
||||
return doLaunch(args);
|
||||
}
|
||||
case 'attach': {
|
||||
const port = effectivePort(args);
|
||||
// Firefox allows exactly one WebDriver BiDi session. Re-attaching to a
|
||||
// browser this process already drives would open a second socket against
|
||||
// that single session, fail the handshake, and strand the browser with no
|
||||
// owner (neither relaunch nor close could reach it). Report the existing
|
||||
// connection instead.
|
||||
if (session && session.port === port && session.ws.readyState === 1) {
|
||||
return { ok: true, attached: true, port, liveMode: !!session.attached, alreadyConnected: true, note: 'already connected to this browser in this process' };
|
||||
}
|
||||
await attachOnly(port);
|
||||
return { ok: true, attached: true, port, liveMode: true };
|
||||
}
|
||||
@@ -960,7 +1096,18 @@ async function run(args) {
|
||||
if (Number.isInteger(args.index)) {
|
||||
const e = lastElements[args.index];
|
||||
if (!e) return { ok: false, error: `no element #${args.index} in the last see; call see first` };
|
||||
point = { x: e.x, y: e.y, name: e.name };
|
||||
// Re-resolve the element captured by the most recent see: scroll it into
|
||||
// view and measure it again, so a scrolled or re-laid-out page cannot
|
||||
// turn the click into a silent miss at a stale coordinate.
|
||||
point = await evaluate(`(() => {
|
||||
const list = window.__edgeKitElements || [];
|
||||
const el = list[${Number(args.index)}];
|
||||
if (!el || !el.isConnected) return null;
|
||||
el.scrollIntoView({ block: 'center' });
|
||||
const r = el.getBoundingClientRect();
|
||||
return { x: r.x + r.width / 2, y: r.y + r.height / 2, name: (el.innerText || el.getAttribute('aria-label') || '').trim().slice(0, 60) };
|
||||
})()`);
|
||||
if (!point) { lastElements = []; return { ok: false, error: `element #${args.index} from the last see is gone; call see again` }; }
|
||||
} else if (typeof args.text === 'string' && args.text) {
|
||||
point = await findByName(args.text);
|
||||
} else if (typeof args.selector === 'string' && args.selector) {
|
||||
@@ -1152,15 +1299,22 @@ async function run(args) {
|
||||
case 'close': {
|
||||
if (!session) return { ok: true, note: 'no active session' };
|
||||
const { child, attached, ws } = session;
|
||||
session = null;
|
||||
if (attached) {
|
||||
// Firefox keeps its single BiDi session registered after a bare
|
||||
// WebSocket close, so detach by ending that session first (the browser
|
||||
// itself is borrowed and stays up).
|
||||
try { if (session.engine === 'bidi' && session.bidiReady) await send('session.end', {}, null); } catch { /* best effort */ }
|
||||
session = null;
|
||||
try { ws.close(); } catch { /* already gone */ }
|
||||
return { ok: true, detached: true, note: 'left the borrowed Edge running (live mode)' };
|
||||
return { ok: true, detached: true, note: 'left the borrowed browser running (live mode)' };
|
||||
}
|
||||
await Promise.race([
|
||||
send('Browser.close', {}, null).catch(() => {}),
|
||||
new Promise((r) => setTimeout(r, 1500)),
|
||||
]);
|
||||
/* EDGE_KIT_NATIVE_CLOSE_FIX: clear the session only after Browser.close was sent; the
|
||||
* upstream order cleared it first, so send() threw and close leaked the browser. */
|
||||
session = null;
|
||||
if (child && child.exitCode === null) child.kill();
|
||||
try { ws.close(); } catch { /* already gone */ }
|
||||
return { ok: true, closed: true };
|
||||
@@ -1209,7 +1363,9 @@ async function withPage(navigateTo, extract, signal) {
|
||||
}
|
||||
}
|
||||
let initLock = null;
|
||||
const PROBE_FILE = 'C:\\Users\\Administrator\\Documents\\deepseek-harness\\default-workspace\\edge-kit-seam-probe.json';
|
||||
/* EDGE_KIT_NATIVE_PORTABILITY: keep the activation probe under the current user's
|
||||
* home directory instead of a hardcoded developer path. */
|
||||
const PROBE_FILE = path.join(os.homedir(), 'Documents', 'deepseek-harness', 'default-workspace', 'edge-kit-seam-probe.json');
|
||||
let probed = false;
|
||||
|
||||
const FETCH_EXTRACT = `(async () => {
|
||||
@@ -1218,27 +1374,43 @@ const FETCH_EXTRACT = `(async () => {
|
||||
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 out = []; const seen = new Set();
|
||||
// Engine-agnostic organic parse: a result headline is a heading inside a
|
||||
// link on Google and Bing alike — layout class names drift, this doesn't.
|
||||
for (const a of document.querySelectorAll('a[href]')) {
|
||||
const heading = a.querySelector('h2, h3');
|
||||
if (!heading) continue;
|
||||
let url = a.href;
|
||||
if (url.startsWith('/url?')) { const m = url.match(/[?&]url=([^&]+)/); if (m) { try { url = decodeURIComponent(m[1]); } catch {} } }
|
||||
if (!/^https?:/i.test(url) || seen.has(url)) continue;
|
||||
const title = clean(heading.innerText);
|
||||
const selfUrl = (u) => /^https?:\\/\\/(?:www\\.)?(?:bing|google|duckduckgo)\\./i.test(u);
|
||||
// Organic result headlines come in both directions now: heading inside the
|
||||
// link (Google: a > h3) AND link inside the heading (Bing: h2 > a). Match
|
||||
// both — engine class names drift, the heading/link nesting does not.
|
||||
for (const node of document.querySelectorAll('h2 a, h3 a, a h2, a h3')) {
|
||||
if (out.length >= 20) break;
|
||||
const link = node.matches('a') ? node : node.closest('a');
|
||||
if (!link) continue;
|
||||
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;
|
||||
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) : '';
|
||||
if (snippet.startsWith(title)) snippet = snippet.slice(title.length).trim();
|
||||
seen.add(url);
|
||||
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 = {
|
||||
@@ -1260,7 +1432,7 @@ const edgeSearchProvider = {
|
||||
async search(request, signal) {
|
||||
const n = Math.min(Math.max(request.maxResults ?? 10, 1), 20);
|
||||
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'
|
||||
? `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)}`;
|
||||
@@ -1374,8 +1546,20 @@ export function apply(ctx, config) {
|
||||
execute: async (args) => run(args),
|
||||
});
|
||||
return () => {
|
||||
try { session?.child?.kill(); } catch { /* already gone */ }
|
||||
try { session?.ws?.close(); } catch { /* already gone */ }
|
||||
// Release the browser this plugin owns — or only the BiDi session when the
|
||||
// browser is borrowed. Killing the spawn handle alone is not enough for
|
||||
// Firefox (it is only the Windows launcher), so the browser could outlive
|
||||
// the host with its single BiDi session still registered.
|
||||
const ending = session;
|
||||
try {
|
||||
if (ending && ending.attached && ending.engine === 'bidi' && ending.bidiReady) {
|
||||
ending.ws.send(JSON.stringify({ id: nextId++, method: 'session.end', params: {} }));
|
||||
} else if (ending && !ending.attached && ending.ws.readyState === 1) {
|
||||
ending.ws.send(JSON.stringify({ id: nextId++, method: ending.engine === 'bidi' ? 'browser.close' : 'Browser.close', params: {} }));
|
||||
}
|
||||
} catch { /* already gone */ }
|
||||
try { ending?.child?.kill(); } catch { /* already gone */ }
|
||||
try { ending?.ws?.close(); } catch { /* already gone */ }
|
||||
session = null;
|
||||
lastElements = [];
|
||||
cfg = null;
|
||||
|
||||
@@ -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",
|
||||
"version": "3.6.4",
|
||||
"version": "3.6.8",
|
||||
"private": true,
|
||||
"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",
|
||||
|
||||
Reference in New Issue
Block a user