commit 4da855f5e182d01abce5e8e2e464979d03f441a5 Author: 8ball Date: Sun Nov 8 19:48:48 2026 +0330 Edge Kit Browser v3.5.6 - Firefox (WebDriver BiDi) + Chrome, video understanding, gameplay, native web seam diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f9d077c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +node_modules/ +*.log +*.tgz +.DS_Store diff --git a/README.md b/README.md new file mode 100644 index 0000000..d5c85ed --- /dev/null +++ b/README.md @@ -0,0 +1,56 @@ +# Edge Kit Browser + +Human-style browsing for AI agents — a zero-dependency [Cordis](https://github.com/moeru-ai) plugin for DeepSeek Harness that drives **Microsoft Edge, Google Chrome, or Mozilla Firefox** directly, with no extra services and no API keys. + +The kit talks to the browser the way a person does: it *sees* the page (visible text + numbered actionable elements, shadow-DOM aware), *acts* (click, type, press, scroll — real input events), *verifies* (screenshots, wait-for-content), and *organizes* work across tabs. It also powers the harness's native `web_search` / `web_fetch` seam, so agents get clean, JavaScript-rendered content from signed-in or bot-walled pages. + +## What it does + +- **Human-style browsing** — `see`, `click`, `clickAt`, `drag`, `type`, `press` (real key codes + repeats), `scroll`, `waitFor`, `goto`, tabs, screenshots, raw `eval`. +- **Video understanding** — the `video` action enumerates players, reports full playback state (`playing`, `currentTime`, `duration`, `readyState`, geometry, source), controls them (`play`/`pause`/`seek`/`rate`), and captures PNG frames straight off the player region. +- **Gameplay** — correct key codes (Space is actually Space), coordinate clicks, and drags, plus before/after screenshots: agents can literally play a browser game and see what happened. +- **Native web seam** — registers `edge-kit` as a `web_search` / `web_fetch` provider: rendered-page extraction with dual-engine fallback (Google/Bing), no bot walls, no API keys. +- **Live-attach first** — if a debuggable browser is already listening, the kit attaches to it (borrowed browsers are never closed). Otherwise it launches: your signed-in profile (default) or an isolated kit profile. + +## Install + +Copy this package into your harness profile (or `pnpm install` it as a workspace link), then add to your composition: + +```yaml +- insert: + - id: edge-kit-easy + name: "@local/edge-kit-easy" + config: {} +``` + +Optionally route the built-in web tools through the kit (pin the seam): + +```yaml +- id: web + config: + fetchProvider: edge-kit + searchProvider: edge-kit +``` + +Settings live on the plugin's native page (**Plugins → Edge Kit Browser**); every field is volatile, so changes apply live without restarting the browser session. + +## How it drives each browser + +| Engine | Protocol | Launch flags | Port (default) | +|---|---|---|---| +| Edge / Chrome | CDP over WebSocket (`/json/version`) | `--remote-debugging-port`, `--user-data-dir`, anti-occlusion | 9444 (Edge) | +| Firefox | **WebDriver BiDi** (`/session`) — the kit contains a CDP→BiDi adapter | `-remote-debugging-port`, `-profile`, `-no-remote` | — | + +Firefox (158+) exposes WebDriver BiDi on its debug port rather than CDP; the adapter translates every call transparently, including `session.status`/`session.new` handshaking, `script.evaluate` with `expression`, `browsingContext.captureScreenshot` (`{type:'box'}` clips), and load-event mapping. + +Per-browser profiles: the user's real profile by default (`realProfile`), or isolated kit profiles under `~/.dsh/browser-profiles/edge-kit[-chrome|-firefox]`. If your everyday browser owns the profile, pass `relaunch: true` to restart it debuggable (logins survive) or set `profileDir`. + +## Actions + +`launch` · `attach` · `status` · `tabs` · `newTab` · `switchTab` · `closeTab` · `goto` · `back` · `forward` · `reload` · `see` · `click` · `clickAt` · `drag` · `video` · `type` · `press` · `scroll` · `waitFor` · `eval` · `screenshot` · `wait` · `close` + +## Troubleshooting + +- **`Maximum number of active sessions` (Firefox)** — a zombie Firefox is holding the BiDi session slot; terminate `firefox.exe` and relaunch. +- **DevTools port never opens** — your normal browser owns that profile: pass `relaunch: true` or a separate `profileDir`. +- **Screenshots black on headless/console sessions** — the kit launches with anti-occlusion flags and `fromSurface:false` to avoid this; on Firefox use the default screenshot path. diff --git a/cordis.patch.yml b/cordis.patch.yml new file mode 100644 index 0000000..571efcc --- /dev/null +++ b/cordis.patch.yml @@ -0,0 +1,12 @@ +- insert: + - id: edge-kit-easy + name: '@local/edge-kit-easy' + config: {} + +# Pin the native web seam to this plugin so web_search / web_fetch run +# through your real Edge (rendered, signed-in, wall-passing). Remove this +# entry to return to auto-selection of the built-in providers. +- id: web + config: + fetchProvider: edge-kit + searchProvider: edge-kit diff --git a/icon.svg b/icon.svg new file mode 100644 index 0000000..6b62588 --- /dev/null +++ b/icon.svg @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/index.js b/index.js new file mode 100644 index 0000000..e452ddb --- /dev/null +++ b/index.js @@ -0,0 +1,1070 @@ +/** + * Edge Kit Browser v3 — browsing the way a human does it. + * + * 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 + * human label, `press` keys, `scroll`, wait for content to appear), verify + * with `screenshot`, and organize work in tabs. + * + * Live-attach-first: if a DevTools-enabled Edge is already listening we + * attach to it (and "close" only detaches — we never close a borrowed + * browser). Otherwise we launch: the user's real Edge profile with + * realProfile:true, or the persistent kit profile. Raw CDP, zero deps. + */ +import os from 'node:os'; +import path from 'node:path'; +import { spawn, exec } from 'node:child_process'; +import { promises as fs } from 'node:fs'; +// schemastery ships inside the app bundle; a profile-linked package cannot +// 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. +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')); +} catch { z = null; } + +export const inject = ['tools', 'web']; + +/** + * Persistent defaults, editable on this plugin's native settings page in + * DeepSeek (Plugins > Edge Kit Browser) — the same surface the Web search + * plugin uses. Every field is volatile, so saving on the page updates the kit + * live without a restart and the running browser session survives. Per-call + * tool arguments still override these values. + */ +export const Config = z && z.object({ + realProfile: z.boolean() + .description('Use your everyday signed-in Edge profile when launching, so saved logins and cookies are available. Turn off to browse in a fresh, isolated Edge Kit profile.') + .default(true).volatile(), + port: z.number() + .description('DevTools port Edge Kit launches Edge with, and attaches to when a debuggable Edge is already listening.') + .step(1).min(1).max(65535).default(9444).volatile(), + headless: z.boolean() + .description('Start Edge without a visible window. Leave off so browsing behaves like a person looking at a screen.') + .default(false).volatile(), + profileDir: z.string() + .description('Where an isolated Edge Kit profile lives when using your signed-in profile is off. Leave blank for the default location.') + .volatile(), + executablePath: z.string() + .description('Full path to msedge.exe. Leave blank to use Edge in its default install locations.') + .volatile(), + seeTextCap: z.number() + .description('How much visible page text the see action returns before truncating (200-6000 characters).') + .step(1).min(200).max(6000).default(2200).volatile(), + searchEngine: z.string() + .description('Which search engine the kit drives for native web_search: "google" or "bing".') + .default('google').volatile(), + browser: z.string() + .description('Which browser the kit launches by default: "edge", "chrome", or "firefox". The launch action can still pick another per call.') + .default('edge').volatile(), + 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(), +}); + +let cfg = null; // bound by apply(); volatile fields are read through .get() +const cfgVal = (field, fallback) => { + const f = cfg?.[field]; + if (f === undefined || f === null) return fallback; + const v = typeof f.get === 'function' ? f.get() : f; + return v === undefined || v === '' ? fallback : v; +}; +const effectivePort = (args) => (Number.isInteger(args?.port) ? args.port : cfgVal('port', 9444)); +const settingsSnapshot = () => ({ + realProfile: cfgVal('realProfile', true), + port: cfgVal('port', 9444), + headless: cfgVal('headless', false), + profileDir: cfgVal('profileDir', ''), + executablePath: cfgVal('executablePath', ''), + seeTextCap: cfgVal('seeTextCap', 2200), + browser: cfgVal('browser', 'edge'), +}); + +const DEFAULT_PORT = 9444; +const KIT_PROFILE = path.join(os.homedir(), '.dsh', 'browser-profiles', 'edge-kit'); +const REAL_PROFILE = process.env.LOCALAPPDATA + ? path.join(process.env.LOCALAPPDATA, 'Microsoft', 'Edge', 'User Data') + : null; +const CHROME_REAL_PROFILE = process.env.LOCALAPPDATA + ? path.join(process.env.LOCALAPPDATA, 'Google', 'Chrome', 'User Data') + : null; +const FF_PROFILES_ROOT = process.env.APPDATA + ? path.join(process.env.APPDATA, 'Mozilla', 'Firefox', 'Profiles') + : null; +const BROWSER_CANDIDATES = { + edge: [ + 'C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe', + 'C:\\Program Files\\Microsoft\\Edge\\Application\\msedge.exe', + ], + chrome: [ + 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe', + 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe', + process.env.LOCALAPPDATA + '\\Google\\Chrome\\Application\\chrome.exe', + ], + firefox: [ + 'C:\\Program Files\\Mozilla Firefox\\firefox.exe', + 'C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe', + ], +}; +const kindOf = (exe) => /firefox/i.test(exe) ? 'firefox' : /chrome/i.test(exe) ? 'chrome' : 'edge'; + +// Firefox's real profile is whichever profiles.ini marks Default=1. +async function ffDefaultProfile() { + if (!FF_PROFILES_ROOT) return null; + try { + const ini = await fs.readFile(path.join(FF_PROFILES_ROOT, 'profiles.ini'), 'utf8'); + let cur = null, pick = null, pickPath = null, pickRel = 1; + for (const line of ini.split(/\r?\n/)) { + const s = line.trim(); + if (s.startsWith('[')) { cur = s.slice(1, -1); continue; } + const m = /^(Path|Default|IsRelative)=(.*)$/i.exec(s); + if (!m || !cur?.startsWith('Profile')) continue; + const key = m[1].toLowerCase(), val = m[2].trim(); + if (key === 'default' && val === '1') pick = cur; + if (key === 'path' && pick === cur) pickPath = val; + if (key === 'path' && pick === cur && pickPath) pickRel = pickRel; // keep default relative=1 + if (key === 'isrelative' && pick === cur) pickRel = Number(val) || 0; + } + if (!pickPath) return null; + return pickRel ? path.join(FF_PROFILES_ROOT, pickPath) : pickPath; + } catch { return null; } +} + +let session = null; // { ws, port, child, headless, profileDir, attached, sessionId, targetId } +let lastElements = []; // numbered elements from the most recent see, with rects +let nextId = 1; +const pending = new Map(); +const eventHooks = new Set(); + +function requireSession() { + if (!session || session.ws.readyState !== 1) { + throw new Error('no active Edge session; call "launch" or "attach" first'); + } + return session; +} + +function send(method, params = {}, sessionId) { + const s = requireSession(); + if (s.engine === 'bidi') return (async () => { if (!s.bidiReady) await bidiHandshake(); return bidiSend(method, params, sessionId === null ? undefined : (sessionId ?? s.sessionId)); })(); + const { ws } = s; + const id = nextId++; + const sid = sessionId === null ? undefined : (sessionId ?? session.sessionId); + return new Promise((resolve, reject) => { + const timer = setTimeout(() => { + pending.delete(id); + reject(new Error(`CDP timeout: ${method}`)); + }, 30000); + pending.set(id, { resolve, reject, timer }); + const frame = { id, method, params }; + if (sid !== undefined) frame.sessionId = sid; + ws.send(JSON.stringify(frame)); + }); +} + +function onMessage(raw) { + let msg; + try { msg = JSON.parse(raw); } catch { return; } + if (msg.id && pending.has(msg.id)) { + const { resolve, reject, timer } = pending.get(msg.id); + pending.delete(msg.id); + clearTimeout(timer); + if (msg.error) reject(new Error(typeof msg.error === 'string' ? `BiDi ${msg.error}: ${msg.message || ''}` : `CDP ${msg.error.code}: ${msg.error.message}`)); + else resolve(msg.result); + return; + } + if (msg.method) { + if (session && session.engine === 'bidi') { + if (msg.method === 'browsingContext.load' || msg.method === 'browsingContext.domContentLoaded' || msg.method === 'browsingContext.navigationComplete') { + for (const hook of [...eventHooks]) hook({ method: 'Page.loadEventFired', sessionId: msg.params && msg.params.context }); + } + return; + } + for (const hook of [...eventHooks]) hook(msg); + } +} + +async function probePort(port) { + try { + const res = await fetch(`http://127.0.0.1:${port}/json/version`); + if (res.ok) return await res.json(); + // Firefox answers here with its httpd.js server and 404s the CDP HTTP + // endpoints — its debug dialect is WebDriver BiDi over ws://…/session. + if (res.status === 404) { + const root = await fetch(`http://127.0.0.1:${port}/`).catch(() => null); + if (root && (await root.text().catch(() => '')).includes('httpd.js')) return { bidi: true, Browser: 'Firefox (BiDi)' }; + } + return null; + } catch { + return null; + } +} + +async function connect(port) { + const info = await probePort(port); + if (!info) throw new Error(`no DevTools endpoint on port ${port}`); + const ws = new WebSocket(info.bidi ? `ws://127.0.0.1:${port}/session` : info.webSocketDebuggerUrl); + await new Promise((resolve, reject) => { + ws.addEventListener('open', resolve, { once: true }); + ws.addEventListener('error', () => reject(new Error('DevTools WebSocket connect failed')), { once: true }); + setTimeout(() => reject(new Error('DevTools WebSocket open timeout')), 10000); + }); + ws.addEventListener('message', (e) => onMessage(typeof e.data === 'string' ? e.data : String(e.data))); + return { ws, info }; +} + +/* ── WebDriver BiDi adapter (Firefox speaks BiDi, not CDP, on its debug port). + * Only the dialect changes: every call the kit makes maps 1:1 onto a BiDi + * command, and responses are rewritten back into CDP shapes so the rest of + * the kit never learns which engine is underneath. */ +function decr(v) { + if (!v || typeof v !== 'object') return v; + switch (v.type) { + case 'undefined': case 'null': return undefined; + case 'string': case 'number': case 'boolean': return v.value; + case 'bigint': return Number(v.value); + case 'array': return (v.value || []).map(decr); + case 'object': { const o = {}; for (const [k, val] of v.value || []) o[k] = decr(val); return o; } + default: return v.value ?? null; + } +} + +function bidiCall(m, p) { + const { ws } = requireSession(); + const id = nextId++; + return new Promise((resolve, reject) => { + const timer = setTimeout(() => { pending.delete(id); reject(new Error(`BiDi timeout: ${m}`)); }, 30000); + pending.set(id, { resolve, reject, timer }); + ws.send(JSON.stringify({ id, method: m, params: 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 : ' ')); + +async function bidiSend(method, params, sid) { + const ctx = sid ?? session.context; + const pointer = (label, actions) => bidiCall('input.performActions', { context: ctx, actions: [{ type: label, id: label === 'key' ? 'k' : 'm', ...(label === 'pointer' ? { parameters: { pointerType: 'mouse' } } : {}), actions }] }); + switch (method) { + case 'Page.enable': await bidiCall('session.subscribe', { events: ['browsingContext.load', 'browsingContext.domContentLoaded'] }); return {}; + case 'Page.disable': return {}; + case 'Page.bringToFront': try { await bidiCall('browsingContext.activate', { context: ctx }); } catch { /* detached? */ } return {}; + case 'Page.navigate': await bidiCall('browsingContext.navigate', { url: params.url, context: ctx }); return {}; + case 'Page.captureScreenshot': { + // Firefox builds vary: newer clip wants {type:'box',…}, format may be + // omitted (png default). Try plain, then legacy, then explicit shapes. + const base = { context: ctx, ...(params.clip ? { clip: { type: 'box', ...params.clip, scale: params.clip.scale ?? 1 } } : {}) }; + let lastErr; + for (const shape of [base, { ...base, format: 'png' }, { context: ctx }, { context: ctx, format: 'png' }]) { + try { return await bidiCall('browsingContext.captureScreenshot', shape); } catch (e) { lastErr = e; } + } + throw lastErr; + } + case 'Target.getTargets': { + const r = await bidiCall('browsingContext.getTree', {}); + 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.closeTarget': await bidiCall('browsingContext.close', { context: params.targetId }); return {}; + case 'Runtime.evaluate': { + // Firefox's remote agent reads the draft-style `expression` param; the + // BiDi standard name rides along for other implementations. + const r = await bidiCall('script.evaluate', { + expression: params.expression, + functionDeclaration: `async function() { return eval(${JSON.stringify(params.expression)}); }`, + awaitPromise: true, + target: { context: ctx }, + }); + 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 {}; + 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 }]; + await pointer('key', acts); return {}; + } + case 'Input.dispatchMouseEvent': { + const acts = []; + if (params.type === 'mouseMoved' || params.type === 'mousePressed') acts.push({ type: 'pointerMove', x: params.x, y: params.y, duration: 10 }); + if (params.type === 'mousePressed') acts.push({ type: 'pointerDown', button: 0 }); + if (params.type === 'mouseReleased') acts.push({ type: 'pointerUp', x: params.x, y: params.y, button: 0 }); + await pointer('pointer', acts); return {}; + } + case 'Browser.close': try { await bidiCall('browser.close', {}); } catch { /* browser gone */ } return {}; + default: return {}; + } +} + +async function pageTargets() { + const { targetInfos } = await send('Target.getTargets', {}, null); + return targetInfos.filter((t) => t.type === 'page' && !t.url.startsWith('devtools://')); +} + +async function attachTo(targetId) { + const prev = session.sessionId; + const { sessionId } = await send('Target.attachToTarget', { targetId, flatten: true }, null); + session.sessionId = sessionId; + session.targetId = targetId; + if (prev !== sessionId) await send('Page.enable', {}); +} + +async function attachToPage() { + const pages = await pageTargets(); + if (!pages.length) { + const created = await send('Target.createTarget', { url: 'about:blank' }, null); + await attachTo(created.targetId); + return; + } + await attachTo(pages[0].targetId); +} + +async function evaluate(expression) { + const r = await send('Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true }); + if (r.exceptionDetails) throw new Error(`page script error: ${r.exceptionDetails.text}`); + return r.result?.value; +} + +async function waitForLoad(timeoutMs = 15000) { + const loaded = new Promise((resolve) => { + const hook = (msg) => { + if (msg.method === 'Page.loadEventFired' && msg.sessionId === session?.sessionId) { + eventHooks.delete(hook); + resolve('load'); + } + }; + eventHooks.add(hook); + setTimeout(() => { eventHooks.delete(hook); resolve('timeout'); }, timeoutMs); + }); + const how = await loaded; + await new Promise((r) => setTimeout(r, 400)); // settle for hydration + const ready = await evaluate('document.readyState').catch(() => 'unknown'); + return { loadEvent: how, readyState: ready }; +} + +async function waitForPort(port, timeoutMs) { + const deadline = Date.now() + timeoutMs; + while (Date.now() < deadline) { + if (await probePort(port)) return true; + await new Promise((r) => setTimeout(r, 400)); + } + return false; +} + +async function findEdge(explicit, kind = 'edge') { + const cand = BROWSER_CANDIDATES[kind] || BROWSER_CANDIDATES.edge; + for (const p of [explicit, ...cand]) { + if (!p) continue; + try { await fs.access(p); return p; } catch { /* next */ } + } + return null; +} + +async function bidiHandshake() { + // Firefox's BiDi (remote agent) requires a real WebDriver session: + // 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: [{}] } }); + session.bidiSessionId = r && r.sessionId; + session.bidiReady = true; +} + +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(); +} + +async function doLaunch(args) { + const port = effectivePort(args); + if (session && session.port === port && session.ws.readyState === 1) { + return { ok: true, note: 'already running', port: session.port }; + } + if (await probePort(port)) { + await attachOnly(port); + return { ok: true, attached: true, note: `attached to the browser already listening on ${port} (live mode)`, port }; + } + const wantedKind = String(args.browser || cfgVal('browser', 'edge')).toLowerCase(); + const explicit = args.executablePath || cfgVal('executablePath', '') || undefined; + const executable = await findEdge(explicit, explicit ? kindOf(explicit) : wantedKind); + if (!executable) throw new Error(`${wantedKind} not found; pass executablePath.`); + const kind = kindOf(executable); + const useReal = args.realProfile ?? cfgVal('realProfile', true); + const realFor = kind === 'edge' ? REAL_PROFILE : kind === 'chrome' ? CHROME_REAL_PROFILE : await ffDefaultProfile(); + const kitDir = kind === 'edge' ? KIT_PROFILE : `${KIT_PROFILE}-${kind}`; + const profileDir = typeof args.profileDir === 'string' && args.profileDir + ? args.profileDir + : (useReal && realFor ? realFor : cfgVal('profileDir', '') || kitDir); + await fs.mkdir(profileDir, { recursive: true }); + const headless = args.headless ?? cfgVal('headless', false); + const child = spawn(executable, kind === 'firefox' ? [ + `-remote-debugging-port=${port}`, + '-profile', profileDir, + '-no-remote', + ...(headless ? ['-headless'] : []), + 'about:blank', + ] : [ + `--remote-debugging-port=${port}`, + `--user-data-dir=${profileDir}`, + '--no-first-run', + '--no-default-browser-check', + // Server/console sessions treat windows as occluded and stop painting, + // which starves surface-based screenshots and throttles timers. + '--disable-features=CalculateNativeWinOcclusion', + '--disable-backgrounding-occluded-windows', + '--disable-renderer-backgrounding', + ...(headless ? ['--headless=new'] : []), + 'about:blank', + ], { stdio: 'ignore', detached: false }); + if (!(await waitForPort(port, 25000))) { + child.kill(); + throw new Error('browser started but the DevTools port never opened. If your everyday browser is running without a debug port it owns this profile: pass relaunch:true (closes it and reopens it debuggable), or use a different profileDir.'); + } + const { ws, info } = await connect(port); + session = { ws, port, child, headless, profileDir, attached: false, kind, engine: info.bidi ? 'bidi' : 'cdp' }; + await attachToPage(); + return { ok: true, launched: true, headless, port, profileDir, realProfile: profileDir === realFor, browser: kind, executable }; +} + +async function ensureActive(args) { + if (session && session.ws.readyState === 1) return; + const port = effectivePort(args); + if (await probePort(port)) { + await attachOnly(port); + return; + } + await doLaunch(args ?? {}); +} + +async function mouseClick(x, y, clickCount = 1) { + for (const type of ['mousePressed', 'mouseReleased']) { + await send('Input.dispatchMouseEvent', { type, x, y, button: 'left', clickCount, buttons: type === 'mousePressed' ? 1 : 0 }, session.sessionId); + } +} + +async function mouseDrag(fromX, fromY, toX, toY, steps = 12) { + await send('Input.dispatchMouseEvent', { type: 'mouseMoved', x: fromX, y: fromY }, session.sessionId); + await send('Input.dispatchMouseEvent', { type: 'mousePressed', x: fromX, y: fromY, button: 'left', clickCount: 1, buttons: 1 }, session.sessionId); + for (let i = 1; i <= steps; i++) { + await send('Input.dispatchMouseEvent', { type: 'mouseMoved', x: fromX + (toX - fromX) * i / steps, y: fromY + (toY - fromY) * i / steps, button: 'left', buttons: 1 }, session.sessionId); + await new Promise((r) => setTimeout(r, 16)); + } + await send('Input.dispatchMouseEvent', { type: 'mouseReleased', x: toX, y: toY, button: 'left', clickCount: 1 }, session.sessionId); +} + +const KEYCODES = { + Enter: [13, 13], Tab: [9, 9], Escape: [27, 27], Space: [32, 32], Backspace: [8, 8], Delete: [46, 46], + ArrowLeft: [37, 37], ArrowUp: [38, 38], ArrowRight: [39, 39], ArrowDown: [40, 40], + Home: [36, 36], End: [35, 35], PageUp: [33, 33], PageDown: [34, 34], + Shift: [16, 16], Control: [17, 17], Alt: [18, 18], +}; +for (let c = 0; c < 26; c++) KEYCODES['Key' + String.fromCharCode(65 + c)] = [65 + c, 65 + c]; +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] }; + 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: 'keyUp', ...base }, session.sessionId); + }); + if (times > 1) await new Promise((r) => setTimeout(r, 90)); + } +} + +async function captureShot(saveTo, clip) { + await send('Page.bringToFront', {}).catch(() => {}); + const params = { format: 'png', fromSurface: false }; + if (clip) params.clip = { ...clip, scale: 1 }; + let data; + try { + ({ data } = await send('Page.captureScreenshot', params)); + } catch { + ({ data } = await send('Page.captureScreenshot', { format: 'png', ...(clip ? { clip: { ...clip, scale: 1 } } : {}) })); + } + const buffer = Buffer.from(data, 'base64'); + const file = saveTo || path.join(os.tmpdir(), `edge-kit-shot-${Date.now()}.png`); + await fs.mkdir(path.dirname(file), { recursive: true }); + await fs.writeFile(file, buffer); + return { path: file, bytes: buffer.length }; +} + +/** Shared in-page walker: shadow-piercing visibility, naming, and element catalog. */ +const WALKER = ` + const vis = (el) => { const r = el.getBoundingClientRect(); if (r.width < 4 || r.height < 4) return null; + 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')) + || 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(); + if (['button', 'summary'].includes(tag)) return true; + if (tag === 'a' && el.hasAttribute('href')) return true; + if (['input', 'textarea', 'select'].includes(tag) && el.type !== 'hidden') return true; + if (el.isContentEditable) return true; + return ['button', 'link', 'textbox', 'searchbox', 'checkbox', 'radio', 'combobox', 'menuitem', 'tab', 'option'].includes(role); + }; +`; + +async function seeOnce(opts = {}) { + const js = `(async () => { + ${WALKER} + const els = []; const seen = new Set(); + function walk(root) { + for (const el of root.querySelectorAll('*')) { + if (el.shadowRoot) walk(el.shadowRoot); + if (!isInter(el)) continue; + const r = vis(el); if (!r) continue; + const name = nameOf(el); + const tag = el.tagName.toLowerCase(); + 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) }); + if (els.length >= 60) return; + } + } + walk(document); + let text = document.body ? clean(document.body.innerText) : ''; + function xt(root) { for (const el of root.querySelectorAll('*')) { if (el.shadowRoot) { text += ' ' + clean(el.shadowRoot.textContent); xt(el.shadowRoot); } } } + xt(document); + const dialogs = []; + function dwalk(root) { + for (const el of root.querySelectorAll('[role="dialog"],[role="alertdialog"],dialog')) { + if (el.shadowRoot) dwalk(el.shadowRoot); + const r = vis(el); if (!r) continue; + dialogs.push(clean(el.innerText).slice(0, 160)); + } + } + dwalk(document); + 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) }; + })()`; + return evaluate(js); +} + +async function findByName(needle) { + const js = `(() => { + ${WALKER} + 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]')) { + if (el.shadowRoot) walk(el.shadowRoot); + const label = nameOf(el).toLowerCase(); + if (!label.includes(want)) continue; + const r = vis(el); if (!r) continue; + const score = (label === want ? 0 : label.startsWith(want) ? 1 : 2) + (isInter(el) ? 0 : 5); + if (!best || score < best.score) best = { score, x: r.x + r.width / 2, y: r.y + r.height / 2, name: nameOf(el) }; + } + } + walk(document); + return best; + })()`; + return evaluate(js); +} + +async function focusField(by) { + const js = `(() => { + ${WALKER} + const want = ${JSON.stringify(String(by).toLowerCase())}; + const labelOf = (el) => { + const names = [el.getAttribute('aria-label'), el.getAttribute('placeholder'), el.getAttribute('title'), el.name || null]; + if (el.labels) for (const l of el.labels) names.push(l.innerText); + if (el.id) { const l = document.querySelector('label[for="' + el.id + '"]'); if (l) names.push(l.innerText); } + return names.map(clean).filter(Boolean).join(' ').toLowerCase(); + }; + let best = null; + function walk(root) { + for (const el of root.querySelectorAll('input,textarea,select,[contenteditable="true"]')) { + if (el.shadowRoot) walk(el.shadowRoot); + if (el.type === 'hidden' || el.disabled) continue; + const hay = labelOf(el); + if (!hay.includes(want)) continue; + const r = vis(el); if (!r) continue; + const score = hay === want ? 0 : 1; + if (!best || score < best.score) best = { score, el }; + } + } + walk(document); + if (!best) return null; + best.el.scrollIntoView({ block: 'center' }); + best.el.focus(); + return document.activeElement === best.el ? nameOf(best.el) || ${JSON.stringify(by)} : 'focused-by-invoke'; + })()`; + return evaluate(js); +} + +async function pageInfo() { + return { url: await evaluate('location.href').catch(() => null), title: await evaluate('document.title').catch(() => null) }; +} + +async function run(args) { + const action = typeof args?.action === 'string' ? args.action : 'status'; + 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. + try { session?.ws?.close(); } catch { /* gone */ } + session = null; + await new Promise((res) => exec('taskkill /IM msedge.exe /F', () => res())); + await new Promise((r) => setTimeout(r, 1800)); + } + return doLaunch(args); + } + case 'attach': { + const port = effectivePort(args); + await attachOnly(port); + return { ok: true, attached: true, port, liveMode: true }; + } + case 'status': { + const settings = settingsSnapshot(); + if (session && session.ws.readyState === 1) { + return { ok: true, running: true, liveMode: session.attached, port: session.port, headless: session.headless, childPid: session.child?.pid ?? null, profileDir: session.profileDir, settings, ...await pageInfo() }; + } + const occupied = !!(await probePort(effectivePort(args))); + return { ok: true, running: false, portOccupied: occupied, settings }; + } + case 'tabs': { + await ensureActive(args); + const pages = await pageTargets(); + return { ok: true, current: session.targetId, tabs: pages.map((t, i) => ({ i, targetId: t.targetId, url: t.url, title: t.title, current: t.targetId === session.targetId })) }; + } + case 'newTab': { + await ensureActive(args); + const { targetId } = await send('Target.createTarget', { url: typeof args.url === 'string' ? args.url : 'about:blank' }, null); + await attachTo(targetId); + if (typeof args.url === 'string' && args.url !== 'about:blank') await waitForLoad(); + return { ok: true, ...await pageInfo() }; + } + case 'switchTab': { + await ensureActive(args); + const pages = await pageTargets(); + const t = Number.isInteger(args.index) ? pages[args.index] : pages.find((p) => p.targetId === args.targetId); + if (!t) return { ok: false, error: 'tab not found', tabs: pages.map((p, i) => ({ i, url: p.url })) }; + await attachTo(t.targetId); + return { ok: true, ...await pageInfo() }; + } + case 'closeTab': { + await ensureActive(args); + const pages = await pageTargets(); + const t = Number.isInteger(args.index) ? pages[args.index] : pages.find((p) => p.targetId === args.targetId); + if (!t) return { ok: false, error: 'tab not found' }; + await send('Target.closeTarget', { targetId: t.targetId }, null); + const rest = await pageTargets(); + if (t.targetId === session.targetId) { + if (rest.length) await attachTo(rest[0].targetId); + else session = null; + } + return { ok: true, remaining: rest.length }; + } + case 'goto': { + if (typeof args.url !== 'string' || !args.url) throw new Error('goto needs a url'); + await ensureActive(args); + await send('Page.navigate', { url: args.url }); + const load = await waitForLoad(); + return { ok: true, ...await pageInfo(), ...load }; + } + case 'back': + case 'forward': + case 'reload': { + await ensureActive(args); + const hist = action === 'back' ? 'history.back()' : action === 'forward' ? 'history.forward()' : 'location.reload()'; + await evaluate(hist); + await waitForLoad().catch(() => ({})); + return { ok: true, ...await pageInfo() }; + } + case 'see': { + await ensureActive(args); + const snap = await seeOnce(args); + lastElements = snap.elements.map((e) => ({ x: e.x, y: e.y, role: e.role, name: e.name })); + return { ok: true, url: snap.url, title: snap.title, text: snap.text, dialogs: snap.dialogs, elements: snap.elements.map((e, i) => ({ i, role: e.role, name: e.name })) }; + } + case 'click': { + await ensureActive(args); + let point = null; + 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 }; + } else if (typeof args.text === 'string' && args.text) { + point = await findByName(args.text); + } else if (typeof args.selector === 'string' && args.selector) { + point = await evaluate(`(() => { const el = document.querySelector(${JSON.stringify(args.selector)}); if (!el) 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 || '').trim().slice(0, 60) }; })()`); + } + if (!point) return { ok: false, error: 'nothing matched; call see to list what is clickable' }; + await mouseClick(point.x, point.y); + await new Promise((r) => setTimeout(r, 500)); + return { ok: true, clicked: point.name || args.text || args.selector, ...await pageInfo() }; + } + case 'type': { + await ensureActive(args); + if (typeof args.text !== 'string') throw new Error('type needs text'); + if (args.selector) { + const ok = await evaluate(`(() => { const el = document.querySelector(${JSON.stringify(String(args.selector))}); if (!el) return false; el.scrollIntoView({ block: 'center' }); el.focus(); return document.activeElement === el; })()`); + if (!ok) return { ok: false, error: `selector not focusable: ${args.selector}` }; + } else if (typeof args.into === 'string' && args.into) { + const label = await focusField(args.into); + if (!label) return { ok: false, error: `no field labeled like ${JSON.stringify(args.into)}; call see` }; + } + await send('Input.insertText', { text: args.text }, session.sessionId); + if (args.submit === true) { + const base = { key: 'Enter', code: 'Enter', windowsVirtualKeyCode: 13, nativeVirtualKeyCode: 13 }; + await send('Input.dispatchKeyEvent', { type: 'keyDown', ...base, text: '\r' }, session.sessionId); + await send('Input.dispatchKeyEvent', { type: 'keyUp', ...base }, session.sessionId); + await waitForLoad().catch(() => ({})); + } + return { ok: true, typed: args.text.length, submitted: args.submit === true, ...await pageInfo() }; + } + case 'press': { + await ensureActive(args); + const key = typeof args.key === 'string' ? args.key : 'Enter'; + await pressKey(key, Math.min(Number.isInteger(args.times) ? args.times : 1, 50)); + await new Promise((r) => setTimeout(r, 400)); + return { ok: true, key, ...await pageInfo() }; + } + case 'clickAt': { + await ensureActive(args); + const x = Number(args.x), y = Number(args.y); + if (!Number.isFinite(x) || !Number.isFinite(y)) throw new Error('clickAt needs numeric x and y'); + await mouseClick(x, y); + await new Promise((r) => setTimeout(r, 300)); + return { ok: true, clickedAt: { x, y }, ...await pageInfo() }; + } + case 'drag': { + await ensureActive(args); + const x = Number(args.x), y = Number(args.y), toX = Number(args.toX), toY = Number(args.toY); + if (![x, y, toX, toY].every(Number.isFinite)) throw new Error('drag needs x, y, toX, toY'); + await mouseDrag(x, y, toX, toY); + return { ok: true, dragged: { from: { x, y }, to: { x: toX, y: toY } }, ...await pageInfo() }; + } + case 'video': { + await ensureActive(args); + const vi = Number.isInteger(args.video) ? args.video : 0; + const op = typeof args.op === 'string' ? args.op : 'status'; + const to = Number(args.to); + const js = `(async () => { + const vs = [...document.querySelectorAll('video')]; + const v = vs[${vi}]; + if (v) { + ${op === 'play' ? 'v.muted = true; await v.play().catch(() => {});' : ''} + ${op === 'pause' ? 'v.pause();' : ''} + ${op === 'seek' ? `v.currentTime = ${Number.isFinite(to) ? to : 0};` : ''} + ${op === 'rate' ? `v.playbackRate = ${Number.isFinite(to) ? Math.min(Math.max(to, 0.1), 8) : 1};` : ''} + } + return { page: { url: location.href, title: document.title }, videos: vs.map((v) => { + const r = v.getBoundingClientRect(); + return { playing: !v.paused && !v.ended, paused: v.paused, ended: v.ended, + currentTime: Math.round(v.currentTime * 100) / 100, duration: v.duration || 0, + muted: v.muted, volume: v.volume, rate: v.playbackRate, ready: v.readyState, + rect: { x: Math.round(r.x), y: Math.round(r.y), w: Math.round(r.width), h: Math.round(r.height) }, + src: (v.currentSrc || v.src || '').slice(0, 160) }; + }) }; + })()`; + const state = await evaluate(js); + const shotsN = Math.min(Number.isInteger(args.shots) ? args.shots : 0, 12); + const every = Math.min(Number.isInteger(args.every) ? args.every : 1000, 20000); + const shots = []; + for (let i = 0; i < shotsN; i++) { + const rect = state.videos[vi] && state.videos[vi].rect; + const file = path.join(os.homedir(), 'Documents', 'deepseek-harness', 'default-workspace', `edge-kit-video-${Date.now()}-${i}.png`); + const clip = rect && rect.w > 4 && rect.h > 4 ? { x: rect.x, y: rect.y, width: rect.w, height: rect.h } : undefined; + shots.push(await captureShot(file, clip)); + if (i < shotsN - 1) await new Promise((r) => setTimeout(r, every)); + } + return { ok: true, ...state, shots }; + } + case 'scroll': { + await ensureActive(args); + const dir = args.direction === 'up' ? -1 : 1; + const px = Math.min(Number.isInteger(args.amount) ? args.amount : 600, 4000); + await evaluate(`window.scrollBy(0, ${dir * px}); 'ok'`); + return { ok: true, direction: dir > 0 ? 'down' : 'up', px }; + } + case 'waitFor': { + await ensureActive(args); + const timeoutMs = Math.min(Number.isInteger(args.timeoutMs) ? args.timeoutMs : 10000, 30000); + const js = `(async () => { + ${WALKER} + const needle = ${JSON.stringify(typeof args.text === 'string' ? args.text.toLowerCase() : '')}; + const sel = ${JSON.stringify(typeof args.selector === 'string' ? args.selector : '')}; + const start = Date.now(); + const hasText = () => { + let found = (document.body ? document.body.innerText : '').toLowerCase().includes(needle); + if (found || !needle) return found; + const stack = [document]; + while (stack.length) { + const root = stack.pop(); + for (const el of root.querySelectorAll('*')) { + if (el.shadowRoot) { stack.push(el.shadowRoot); if (needle && el.shadowRoot.textContent.toLowerCase().includes(needle)) return true; } + } + } + return false; + }; + while (Date.now() - start < ${timeoutMs}) { + if (sel && document.querySelector(sel)) return { found: true, how: 'selector', ms: Date.now() - start }; + if (needle && hasText()) return { found: true, how: 'text', ms: Date.now() - start }; + await new Promise((r) => setTimeout(r, 250)); + } + return { found: false, ms: Date.now() - start }; + })()`; + const r = await evaluate(js); + return { ok: true, ...r, ...await pageInfo() }; + } + case 'eval': { + await ensureActive(args); + if (typeof args.script !== 'string') throw new Error('eval needs a script'); + return { ok: true, result: await evaluate(`(async () => { ${args.script} })()`) }; + } + case 'screenshot': { + await ensureActive(args); + const saveTo = typeof args.saveTo === 'string' && path.isAbsolute(args.saveTo) ? args.saveTo : undefined; + return { ok: true, ...await captureShot(saveTo) }; + } + case 'wait': { + const ms = Math.min(Number.isInteger(args.ms) ? args.ms : 1000, 20000); + await new Promise((r) => setTimeout(r, ms)); + return { ok: true, waited: ms }; + } + case 'close': { + if (!session) return { ok: true, note: 'no active session' }; + const { child, attached, ws } = session; + session = null; + if (attached) { + try { ws.close(); } catch { /* already gone */ } + return { ok: true, detached: true, note: 'left the borrowed Edge running (live mode)' }; + } + await Promise.race([ + send('Browser.close', {}, null).catch(() => {}), + new Promise((r) => setTimeout(r, 1500)), + ]); + if (child && child.exitCode === null) child.kill(); + try { ws.close(); } catch { /* already gone */ } + return { ok: true, closed: true }; + } + default: + return { ok: false, error: `unknown action: ${action}` }; + } +} + +/* ── Native web providers (Exa-style): the harness web seam, pinned to this + * plugin by cordis.patch.yml, serves web_search / web_fetch through your + * real signed-in Edge — full JS rendering and a home fingerprint, so content + * that plain HTTP fetchers get wall-Blocked on still comes back clean. */ + +async function withPage(navigateTo, extract, signal) { + if (!session || session.ws.readyState !== 1) { + if (!initLock) initLock = (async () => { + try { await doLaunch({}); } + catch (e) { + // Seam calls have nobody to ask for a relaunch: if the signed-in + // profile is locked by the everyday Edge, degrade to the kit profile. + if (cfgVal('realProfile', true)) { await doLaunch({ profileDir: cfgVal('profileDir', '') || KIT_PROFILE }); return; } + throw e; + } + })().finally(() => { initLock = null; }); + await initLock; + } + const { targetId } = await send('Target.createTarget', { url: 'about:blank' }, null); + const { sessionId: sid } = await send('Target.attachToTarget', { targetId, flatten: true }, null); + try { + await send('Page.enable', {}, sid); + await send('Page.navigate', { url: navigateTo }, sid); + const deadline = Date.now() + 20000; + while (Date.now() < deadline) { + if (signal && signal.aborted) throw new Error('web request aborted'); + const r = await send('Runtime.evaluate', { expression: 'document.readyState', returnByValue: true }, sid); + if (r.result && r.result.value === 'complete') break; + await new Promise((res) => setTimeout(res, 400)); + } + await new Promise((res) => setTimeout(res, 600)); // let hydration settle + const r = await send('Runtime.evaluate', { expression: extract, returnByValue: true, awaitPromise: true }, sid); + if (r.exceptionDetails) throw new Error(`page script error: ${r.exceptionDetails.text}`); + return r.result ? r.result.value : undefined; + } finally { + await send('Target.closeTarget', { targetId }, null).catch(() => {}); + } +} +let initLock = null; +const PROBE_FILE = 'C:\\Users\\Administrator\\Documents\\deepseek-harness\\default-workspace\\edge-kit-seam-probe.json'; +let probed = false; + +const FETCH_EXTRACT = `(async () => { + let status = 200; + try { const n = performance.getEntriesByType('navigation')[0]; if (n && typeof n.responseStatus === 'number') status = n.responseStatus; } catch {} + return { url: location.href, status, text: document.body ? document.body.innerText : '' }; +})()`; + +const SEARCH_EXTRACT = `(() => { + 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); + if (!title || title.length < 4) continue; + const box = a.closest('li,div.g,div.tF2Cxc,li.b_algo') || a.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) } }; +})()`; + +const edgeFetchProvider = { + id: 'edge-kit', + available: () => true, + async fetch(request, signal) { + const r = await withPage(request.url, FETCH_EXTRACT, signal) || {}; + const cap = cfgVal('fetchTextCap', 20000); + let text = String(r.text || ''); + const truncated = text.length > cap; + if (truncated) text = text.slice(0, cap); + return { url: r.url || request.url, statusCode: r.status || 200, body: { kind: 'text', content: text }, truncated }; + }, +}; + +const edgeSearchProvider = { + id: 'edge-kit', + available: () => true, + 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 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)}`; + const r = (await withPage(build(engine), SEARCH_EXTRACT, signal)) || {}; + let sources = (r.sources || []).slice(0, n); + let page = r.page || {}; + if (!sources.length) { + // Engine layouts and consent walls differ; an agent's search should + // survive either. Try the other engine once before reporting empty. + const alt = (await withPage(build(engine === 'bing' ? 'google' : 'bing'), SEARCH_EXTRACT, signal)) || {}; + sources = (alt.sources || []).slice(0, n); + if (alt.page) page = alt.page; + } + return { + sources, + truncated: false, + ...(sources.length || !page.url ? {} : { content: 'edge-kit search parsed no results; landed on ' + page.url + ' (title: ' + (page.title || 'none') + ')' }), + }; + }, +}; + +export function apply(ctx, config) { + cfg = config; + // One-time activation probe: run the seam's own fetch/search against this + // provider and record the outcome, proving selection + rendering end to end. + if (!probed) { + probed = true; + setTimeout(async () => { + const out = { at: new Date().toISOString() }; + try { + const f = await ctx.web.fetch({ url: 'https://quotes.toscrape.com/js/' }); + out.fetch = { url: f.url, statusCode: f.statusCode, kind: f.body.kind, length: f.body.content.length, truncated: f.truncated, sample: f.body.content.slice(0, 220) }; + } catch (e) { out.fetchError = String((e && e.message) || e); } + try { + const s = await ctx.web.search({ query: 'deepseek harness', maxResults: 3 }); + out.search = { count: s.sources.length, urls: s.sources.slice(0, 3).map((x) => x.url), content: s.content }; + } catch (e) { out.searchError = String((e && e.message) || e); } + try { await fs.writeFile(PROBE_FILE, JSON.stringify(out, null, 2)); } catch { /* probe file optional */ } + }, 400); + } + ctx.effect(() => { + // The seam disposes provider registrations with the registering fiber, + // so they must live inside this effect, not at apply scope. + const offSearch = ctx.web.registerSearchProvider(edgeSearchProvider); + const offFetch = ctx.web.registerFetchProvider(edgeFetchProvider); + const dispose = ctx.tools.register({ + name: 'edge_kit_browser', + description: + 'Edge Kit Browser — browse like a human in Edge, Chrome, or Firefox. Flow: see (read the ' + + 'page: text + numbered elements, shadow-DOM aware) then act: click {index|text|selector}, ' + + 'clickAt {x,y}, drag {x,y,toX,toY}, type {into,text,submit?}, press {key,times}, scroll; ' + + 'understand video: video {video?, op:status|play|pause|seek|rate, to?, shots?, every?} ' + + 'returns playback state and PNG frames of the player; verify with waitFor {text|selector} ' + + 'or screenshot; navigate with goto/back/forward/reload; organize with tabs/newTab/' + + 'switchTab/closeTab. Session: launch (browser: edge|chrome|firefox; live-attaches to a ' + + 'running debuggable browser first; realProfile:true keeps your logins; relaunch:true ' + + 'restarts your browser debuggable), attach, status, wait {ms}, eval {script}, close ' + + '(detaches from borrowed browsers, never closes them).', + parameters: { + type: 'object', + properties: { + action: { + type: 'string', + enum: ['launch', 'attach', 'status', 'tabs', 'newTab', 'switchTab', 'closeTab', 'goto', 'back', 'forward', 'reload', 'see', 'click', 'clickAt', 'drag', 'video', 'type', 'press', 'scroll', 'waitFor', 'eval', 'screenshot', 'wait', 'close'], + description: 'What to do.', + }, + url: { type: 'string', description: 'goto/newTab: address to open.' }, + headless: { type: 'boolean', description: 'launch only: start hidden (default false = visible, like a person).' }, + port: { type: 'integer', description: 'DevTools port (default comes from the Edge Kit settings page).' }, + profileDir: { type: 'string', description: 'launch only: user-data-dir override.' }, + realProfile: { type: 'boolean', description: 'launch only: use your everyday Edge profile (keeps all your logins). Default comes from the Edge Kit settings page.' }, + relaunch: { type: 'boolean', description: 'launch only: restart a running profile-owning Edge so it becomes debuggable.' }, + executablePath: { type: 'string', description: 'launch only: msedge.exe path override.' }, + index: { type: 'integer', description: 'click: element number from the most recent see. switchTab/closeTab: tab number from tabs.' }, + targetId: { type: 'string', description: 'switchTab/closeTab: exact target id.' }, + text: { type: 'string', description: 'click: visible text to click. type: text to enter. waitFor: text to wait for.' }, + into: { type: 'string', description: 'type: human label of the field (label/placeholder/aria-label).' }, + submit: { type: 'boolean', description: 'type: press Enter after typing.' }, + selector: { type: 'string', description: 'click: CSS selector (light DOM). waitFor: selector to wait for.' }, + script: { type: 'string', description: 'eval: JS statements (await allowed).' }, + key: { type: 'string', description: 'press: key name (default Enter).' }, + direction: { type: 'string', enum: ['up', 'down'], description: 'scroll direction.' }, + amount: { type: 'integer', description: 'scroll pixels (default 600).' }, + ms: { type: 'integer', description: 'wait: milliseconds.' }, + timeoutMs: { type: 'integer', description: 'waitFor: max milliseconds (default 10000).' }, + maxText: { type: 'integer', description: 'see: page-text cap (default 2200).' }, + saveTo: { type: 'string', description: 'screenshot: absolute PNG path to write.' }, + browser: { type: 'string', enum: ['edge', 'chrome', 'firefox'], description: 'launch: which browser to start (default comes from the settings page).' }, + x: { type: 'integer', description: 'clickAt/drag: x coordinate.' }, + y: { type: 'integer', description: 'clickAt/drag: y coordinate.' }, + toX: { type: 'integer', description: 'drag: destination x.' }, + toY: { type: 'integer', description: 'drag: destination y.' }, + times: { type: 'integer', description: 'press: repeat the key this many times (games).' }, + video: { type: 'integer', description: 'video: which video element on the page (0 = first).' }, + op: { type: 'string', enum: ['status', 'play', 'pause', 'seek', 'rate'], description: 'video: what to do.' }, + to: { type: 'number', description: 'video seek: seconds; video rate: playback speed.' }, + shots: { type: 'integer', description: 'video: also capture this many PNG frames (max 12).' }, + every: { type: 'integer', description: 'video: milliseconds between frames (default 1000).' }, + }, + additionalProperties: false, + }, + output: { + schema: { type: 'object' }, + render: (args, value) => [{ type: 'text', text: JSON.stringify(value, null, 2) }], + }, + execute: async (args) => run(args), + }); + return () => { + try { session?.child?.kill(); } catch { /* already gone */ } + try { session?.ws?.close(); } catch { /* already gone */ } + session = null; + lastElements = []; + cfg = null; + offSearch(); + offFetch(); + dispose(); + }; + }); +} diff --git a/locale/en.json b/locale/en.json new file mode 100644 index 0000000..66086d5 --- /dev/null +++ b/locale/en.json @@ -0,0 +1,6 @@ +{ + "meta": { + "title": "Edge Kit Browser", + "description": "Human-style browsing for agents: open pages, see what is on screen (across shadow DOM), click by visible text, type into labeled fields, wait for content, screenshot, manage tabs. Attaches to a running debug-enabled Edge first; launches your real or a persistent profile otherwise. Also powers the native web_search and web_fetch tools: agents get clean, JavaScript-rendered content from signed-in, bot-walled pages through your Edge, no API key needed." + } +} diff --git a/locale/zh.json b/locale/zh.json new file mode 100644 index 0000000..f430ff3 --- /dev/null +++ b/locale/zh.json @@ -0,0 +1,6 @@ +{ + "meta": { + "title": "Edge Kit 浏览器", + "description": "像人一样浏览网页的智能体工具:打开页面、查看屏幕内容(穿透 Shadow DOM)、按可见文字点击、向输入框输入、等待内容出现、截图、管理标签页。优先附加到已开启调试端口的 Edge,否则用真实或持久化配置启动。同时为原生 web_search 与 web_fetch 提供内容:无需 API key,通过你的 Edge 获取已登录、可过反爬、JavaScript 渲染后的干净正文。" + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..41e8ef4 --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "@local/edge-kit-easy", + "version": "3.5.6", + "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", + "repository": { + "type": "git", + "url": "https://gitty.iw5.ir/8ball/edge-kit.git" + }, + "keywords": ["browser", "cdp", "webdriver-bidi", "firefox", "chrome", "edge", "agent-tooling", "cordis", "deepseek-harness"], + "license": "MIT", + "exports": { + ".": "./index.js", + "./locale/*.json": "./locale/*.json", + "./icon": "./icon.svg" + }, + "files": ["index.js", "cordis.patch.yml", "locale/*.json", "icon.svg"], + "dsh": { "bundle": { "patch": "./cordis.patch.yml" } } +}