diff --git a/index.js b/index.js index b7ee331..39a0c5a 100644 --- a/index.js +++ b/index.js @@ -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']; @@ -266,8 +297,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 +333,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 +356,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 +380,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 +417,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 +469,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 +487,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 +531,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}`, @@ -667,12 +763,18 @@ 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 } = session; + const { child, ws, engine } = 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(); const t0 = Date.now(); - while (await probePort(la.port || effectivePort(la)) && Date.now() - t0 < 6000) await new Promise((r) => setTimeout(r, 500)); + while (await probePort(la.port || effectivePort(la)) && Date.now() - t0 < 10000) await new Promise((r) => setTimeout(r, 500)); await doLaunch(la); return true; } @@ -703,13 +805,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)); @@ -739,8 +848,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(); @@ -766,7 +875,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; } } @@ -783,9 +892,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); } @@ -796,8 +909,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; @@ -823,8 +937,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; @@ -851,12 +966,16 @@ 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); @@ -935,7 +1054,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) { @@ -1127,15 +1257,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 }; @@ -1184,7 +1321,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 () => { @@ -1349,8 +1488,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;