omowright 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +282 -2
  3. package/TOOLS.md +366 -0
  4. package/bin/omowright-cloak.mjs +112 -0
  5. package/package.json +57 -4
  6. package/presets/agent-events/SKILL.md +75 -0
  7. package/presets/captcha/SKILL.md +129 -0
  8. package/presets/chrome/SKILL.md +84 -0
  9. package/presets/cloak-bridge/SKILL.md +43 -0
  10. package/presets/network/SKILL.md +126 -0
  11. package/presets/visual-browse/SKILL.md +103 -0
  12. package/skills/omowright/SKILL.md +193 -0
  13. package/skills/omowright/references/1password.md +118 -0
  14. package/skills/omowright/references/browserskill.md +186 -0
  15. package/skills/omowright/references/eval-kernel.md +100 -0
  16. package/skills/omowright/references/frames-layers-human.md +123 -0
  17. package/skills/omowright/references/quickstart.md +96 -0
  18. package/skills/omowright/references/stealth.md +174 -0
  19. package/src/agent-tabs.js +160 -0
  20. package/src/bridge-schemas.js +44 -0
  21. package/src/bridge-transport.js +57 -0
  22. package/src/browser-process.js +122 -0
  23. package/src/bsk/browser-signals.js +90 -0
  24. package/src/bsk/browsers.js +114 -0
  25. package/src/bsk/connect.js +45 -0
  26. package/src/bsk/daemon-info.js +100 -0
  27. package/src/bsk/external-extension.js +110 -0
  28. package/src/bsk/identify-browser.js +88 -0
  29. package/src/bsk/install-cli.js +81 -0
  30. package/src/bsk/ipc-client.js +236 -0
  31. package/src/bsk/onboard.js +133 -0
  32. package/src/bsk/screenshot.js +49 -0
  33. package/src/bsk/session.js +131 -0
  34. package/src/bsk/snapshot.js +56 -0
  35. package/src/bsk/targets.js +25 -0
  36. package/src/captcha-wait.js +74 -0
  37. package/src/captcha.js +142 -0
  38. package/src/cdp-errors.js +23 -0
  39. package/src/cdp-methods.js +61 -0
  40. package/src/chrome-profile.js +176 -0
  41. package/src/chrome.js +192 -0
  42. package/src/cloak-profile.js +266 -0
  43. package/src/connection.js +135 -0
  44. package/src/cookies.js +29 -0
  45. package/src/core.js +1710 -0
  46. package/src/cua.js +74 -0
  47. package/src/dialog-policy.js +80 -0
  48. package/src/emulate.js +127 -0
  49. package/src/events.js +111 -0
  50. package/src/frames-snapshot.js +138 -0
  51. package/src/har.js +103 -0
  52. package/src/human-handoff.js +122 -0
  53. package/src/index.js +86 -0
  54. package/src/injected.js +3 -0
  55. package/src/instance-lock.js +230 -0
  56. package/src/instances.js +493 -0
  57. package/src/internal-capability.js +1 -0
  58. package/src/launch-endpoint.js +185 -0
  59. package/src/layers.js +138 -0
  60. package/src/llm.js +201 -0
  61. package/src/lock-file.js +130 -0
  62. package/src/native-host-win32.js +201 -0
  63. package/src/network-events.js +201 -0
  64. package/src/network-snoop.js +229 -0
  65. package/src/open-shadow-query.js +22 -0
  66. package/src/owner-only.js +109 -0
  67. package/src/page-bundle.js +1397 -0
  68. package/src/page-routes.js +94 -0
  69. package/src/pipe.js +391 -0
  70. package/src/routes.js +256 -0
  71. package/src/running-browser.js +85 -0
  72. package/src/scroll-collect.js +73 -0
  73. package/src/serial-emitter.js +17 -0
  74. package/src/shared-connections.js +278 -0
  75. package/src/snapshot-diff.js +75 -0
  76. package/src/trace.js +183 -0
  77. package/src/url-match.js +148 -0
  78. package/src/viewport.js +12 -0
@@ -0,0 +1,122 @@
1
+ function injectHumanBanner(prompt) {
2
+ if (document.querySelector('[data-omowright-human="1"]')) return true;
3
+ const host = document.createElement("div");
4
+ host.setAttribute("data-omowright-human", "1");
5
+ host.style.cssText = "position:fixed;top:0;left:0;right:0;z-index:2147483647;display:block;visibility:visible;opacity:1;pointer-events:auto;";
6
+ const root = host.attachShadow({ mode: "open" });
7
+ const bar = document.createElement("div");
8
+ bar.setAttribute("style", "display:flex;align-items:center;gap:12px;padding:10px 16px;background:#111;color:#fff;font:14px/1.4 system-ui,sans-serif;");
9
+ const message = document.createElement("span");
10
+ message.textContent = prompt == null ? "" : String(prompt);
11
+ const button = document.createElement("button");
12
+ button.textContent = "Done";
13
+ button.addEventListener("click", () => {
14
+ window.__omowrightHumanDone = true;
15
+ });
16
+ bar.append(message, button);
17
+ root.append(bar);
18
+ (document.documentElement ?? document.body).appendChild(host);
19
+ return true;
20
+ }
21
+
22
+ function humanBannerPresent() {
23
+ return document.querySelector('[data-omowright-human="1"]') !== null;
24
+ }
25
+
26
+ function humanDoneFlag() {
27
+ return window.__omowrightHumanDone === true;
28
+ }
29
+
30
+ function querySelectorExists(selector) {
31
+ return document.querySelector(selector) !== null;
32
+ }
33
+
34
+ function removeHumanBanner() {
35
+ document.querySelectorAll('[data-omowright-human="1"]').forEach(node => node.remove());
36
+ try {
37
+ delete window.__omowrightHumanDone;
38
+ } catch {
39
+ window.__omowrightHumanDone = undefined;
40
+ }
41
+ }
42
+
43
+ function sleep(ms, signal) {
44
+ return new Promise(resolve => {
45
+ if (signal?.aborted) {
46
+ resolve();
47
+ return;
48
+ }
49
+ const timer = setTimeout(() => {
50
+ signal?.removeEventListener("abort", onAbort);
51
+ resolve();
52
+ }, ms);
53
+ function onAbort() {
54
+ clearTimeout(timer);
55
+ resolve();
56
+ }
57
+ signal?.addEventListener("abort", onAbort, { once: true });
58
+ });
59
+ }
60
+
61
+ async function ensureBanner(page, prompt) {
62
+ try {
63
+ if (await page.evaluate(humanBannerPresent)) return;
64
+ await page.evaluate(injectHumanBanner, prompt ?? "");
65
+ } catch {
66
+ // Page may be mid-navigation; the next poll retries.
67
+ }
68
+ }
69
+
70
+ async function matchesUntil(page, until) {
71
+ if (until == null) return false;
72
+ if (typeof until === "function") return Boolean(await until(page));
73
+ if (typeof until === "object") {
74
+ if (Object.prototype.hasOwnProperty.call(until, "url")) {
75
+ const current = page.url();
76
+ const expected = until.url;
77
+ if (expected instanceof RegExp) return expected.test(current);
78
+ return String(current).includes(String(expected));
79
+ }
80
+ if (typeof until.selector === "string") {
81
+ try {
82
+ return Boolean(await page.evaluate(querySelectorExists, until.selector));
83
+ } catch {
84
+ return false;
85
+ }
86
+ }
87
+ }
88
+ return false;
89
+ }
90
+
91
+ async function isDone(page) {
92
+ try {
93
+ return await page.evaluate(humanDoneFlag) === true;
94
+ } catch {
95
+ return false;
96
+ }
97
+ }
98
+
99
+ export async function requestHuman(page, { prompt, until, timeoutMs = 300000, pollMs = 500, signal } = {}) {
100
+ const started = Date.now();
101
+ const finish = (outcome, reason) => ({ outcome, elapsedMs: Date.now() - started, reason });
102
+
103
+ try {
104
+ await page.bringToFront().catch(() => {});
105
+ await ensureBanner(page, prompt);
106
+
107
+ for (;;) {
108
+ if (signal?.aborted) return finish("cancelled", "signal");
109
+ if (Date.now() - started >= timeoutMs) return finish("timed_out", "timed_out");
110
+ if (await matchesUntil(page, until)) return finish("continued", "until");
111
+ if (await isDone(page)) return finish("continued", "done-button");
112
+ // Human waits are idle time, not a race against a specific event; a timer
113
+ // poll loop is the correct tool here.
114
+ await ensureBanner(page, prompt);
115
+ const remaining = timeoutMs - (Date.now() - started);
116
+ if (remaining <= 0) return finish("timed_out", "timed_out");
117
+ await sleep(Math.min(pollMs, remaining), signal);
118
+ }
119
+ } finally {
120
+ await page.evaluate(removeHumanBanner).catch(() => {});
121
+ }
122
+ }
package/src/index.js ADDED
@@ -0,0 +1,86 @@
1
+ export { connect, BrowserConnection, TabRepository } from "./connection.js";
2
+ export { OMOWRIGHT_PAGE_CDP_METHODS } from "./cdp-methods.js";
3
+ export {
4
+ CdpCommandTimeoutError,
5
+ CdpPipeClosedError,
6
+ CdpSessionCrashedError,
7
+ CdpSessionDetachedError,
8
+ connectPipe,
9
+ PipeCdpClient,
10
+ } from "./pipe.js";
11
+ export { connectNamed, listInstances } from "./instances.js";
12
+ export { RunningBrowserError, connectRunningBrowser, findRunningBrowser, readDevToolsActivePort } from "./running-browser.js";
13
+ export { SharedConnectionError } from "./shared-connections.js";
14
+ export {
15
+ buildCloakBrowserArgs,
16
+ connectCloakProfile,
17
+ findCloakBrowserPath,
18
+ resolveCloakProfile,
19
+ } from "./cloak-profile.js";
20
+ export { compactSnapshot, snapshotTokens, toolSchemas } from "./llm.js";
21
+ export { createCua } from "./cua.js";
22
+ export { createCaptcha, macOSVisionOcr } from "./captcha.js";
23
+ export { BROWSER_EVENT_NAMES, createEvents } from "./events.js";
24
+ export { AgentTabManager, createAgentTabs } from "./agent-tabs.js";
25
+ export { DEFAULT_AGENT_VIEWPORT, repinViewport } from "./viewport.js";
26
+ export { createChromeApi } from "./bridge-transport.js";
27
+ export {
28
+ BridgeUnavailableError,
29
+ BridgeProtocolError,
30
+ createNativeMessagingHost,
31
+ createBridgeClient,
32
+ installNativeMessagingHost,
33
+ encodeFrame,
34
+ decodeFrames,
35
+ ExtensionHelloSchema,
36
+ HostHelloSchema,
37
+ EventSchema,
38
+ CommandSchema,
39
+ ResponseSchema,
40
+ } from "./bridge-transport.js";
41
+ export { injectCookies, sanitizeCookies } from "./cookies.js";
42
+ export { pageBundle } from "./injected.js";
43
+ export {
44
+ BrowserCdpCommandError,
45
+ CdpClient,
46
+ ElementHandle,
47
+ FrameLocator,
48
+ FrameManager,
49
+ Locator,
50
+ ModifierState,
51
+ OmOKeyboard,
52
+ OmOMouse,
53
+ OmOPage,
54
+ SessionManager,
55
+ UnsupportedOperationError,
56
+ takeSnapshot,
57
+ selectors,
58
+ } from "./core.js";
59
+ export { normalizeDialogPolicy, resolveDialogAction } from "./dialog-policy.js";
60
+ export { DEVICE_PRESETS, emulate } from "./emulate.js";
61
+ export { reconcileFrames, snapshotWithFrames } from "./frames-snapshot.js";
62
+ export { toHar } from "./har.js";
63
+ export { requestHuman } from "./human-handoff.js";
64
+ export { describeLayers, layersHeader, snapshotWithLayers } from "./layers.js";
65
+ export { createNetworkSnoop } from "./network-snoop.js";
66
+ export { NetworkBodyUnavailableError } from "./network-events.js";
67
+ export { createRoutes } from "./routes.js";
68
+ export { collectWhileScrolling } from "./scroll-collect.js";
69
+ export { createTrace } from "./trace.js";
70
+ export { BskIpcClient, BskRpcError, readDaemonInfo, resolveBskHome } from "./bsk/ipc-client.js";
71
+ export { BskSession } from "./bsk/session.js";
72
+ export { connectBrowserSkill, listBrowsers } from "./bsk/connect.js";
73
+ export { bskSnapshot, buildSnapshotExpression } from "./bsk/snapshot.js";
74
+ export {
75
+ BROWSERSKILL_EXTENSION_IDS,
76
+ STORE_PAGE_URLS,
77
+ detectBrowsers,
78
+ externalExtensionEntry,
79
+ registerExternalExtension,
80
+ unregisterExternalExtension,
81
+ } from "./bsk/external-extension.js";
82
+ export { catalogBrowsers } from "./bsk/browsers.js";
83
+ export { probeBrowserSignals } from "./bsk/browser-signals.js";
84
+ export { identifyBrowser } from "./bsk/identify-browser.js";
85
+ export { installBskCli } from "./bsk/install-cli.js";
86
+ export { bskDoctor, bskOnboard } from "./bsk/onboard.js";
@@ -0,0 +1,3 @@
1
+ import { readFileSync } from "node:fs";
2
+
3
+ export const pageBundle = readFileSync(new URL("./page-bundle.js", import.meta.url), "utf8");
@@ -0,0 +1,230 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { watch } from "node:fs";
3
+ import { link, readdir, rename, stat, unlink } from "node:fs/promises";
4
+ import path from "node:path";
5
+ import { setTimeout as sleep } from "node:timers/promises";
6
+ import {
7
+ createLock,
8
+ isSameLock,
9
+ processStartTime,
10
+ readLockState,
11
+ } from "./lock-file.js";
12
+
13
+ const LOCK_POLL_INTERVAL_MS = 250;
14
+ const LOCK_RETRY_JITTER_MS = 25;
15
+ const LOCK_PUT_BACK_TIMEOUT_MS = 30_000;
16
+ const localLocks = new Map();
17
+
18
+ export function isPidAlive(pid) {
19
+ try {
20
+ process.kill(pid, 0);
21
+ return true;
22
+ } catch (error) {
23
+ if (error?.code === "EPERM") return true;
24
+ if (error?.code === "ESRCH") return false;
25
+ throw error;
26
+ }
27
+ }
28
+
29
+ export async function unlinkIfPresent(target) {
30
+ await unlink(target).catch((error) => {
31
+ if (error?.code !== "ENOENT") throw error;
32
+ });
33
+ }
34
+
35
+ async function isAbandoned(state, staleGraceMs, startTimeOf) {
36
+ if (state.pid === null) return Date.now() - state.mtimeMs >= staleGraceMs;
37
+ if (!isPidAlive(state.pid)) return true;
38
+ if (state.start === null) return false;
39
+ const start = await startTimeOf(state.pid);
40
+ return start !== null && start !== state.start;
41
+ }
42
+
43
+ // Deletes only the lock that was proved abandoned, identified by inode AND
44
+ // token, never another one. The rename is the exclusive step: of two
45
+ // reclaimers only one moves a given entry, and whatever we moved is ours alone
46
+ // to inspect. An inode number alone is not enough because ext4 reuses them, so
47
+ // a live lock created after the dead one vanished can carry the same inode;
48
+ // its token differs, and anything that does not match is put back.
49
+ // Known residuals of a takeover built from path operations (#62 replaces it):
50
+ // while a live lock is moved aside another contender can take the name, so two
51
+ // processes hold it until the put-back; and a holder that releases while its
52
+ // lock is moved aside can have that finished lock put back, which keeps the
53
+ // name locked until the holder's process exits.
54
+ // `hooks` lets a test run code inside the gaps between these steps.
55
+ export async function reclaimLock(lockPath, ino, token, { deadline = Date.now() + LOCK_PUT_BACK_TIMEOUT_MS, hooks = {} } = {}) {
56
+ if (!isSameLock(await readLockState(lockPath), ino, token)) {
57
+ return (await stat(lockPath).catch(() => null)) === null;
58
+ }
59
+ const movedPath = `${lockPath}.${process.pid}.${randomUUID()}.stale`;
60
+ await hooks.beforeMove?.();
61
+ try {
62
+ await rename(lockPath, movedPath);
63
+ } catch (error) {
64
+ if (error?.code === "ENOENT") return true;
65
+ throw error;
66
+ }
67
+ await hooks.afterMove?.();
68
+ if (isSameLock(await readLockState(movedPath).catch(() => null), ino, token)) {
69
+ await unlinkIfPresent(movedPath);
70
+ return true;
71
+ }
72
+ await putBack(movedPath, lockPath, deadline, hooks);
73
+ return false;
74
+ }
75
+
76
+ // The moved file is a live holder's lock. While the name was empty another
77
+ // contender may have locked it; the moved lock then waits for that one to go.
78
+ // It is never deleted here: it leaves only when it is back under its name, when
79
+ // its holder released it (releaseLock removes moved copies of its own lock), or
80
+ // it stays where it is past the deadline for that holder to release.
81
+ async function putBack(movedPath, lockPath, deadline, hooks) {
82
+ for (;;) {
83
+ try {
84
+ await link(movedPath, lockPath);
85
+ await unlinkIfPresent(movedPath);
86
+ return;
87
+ } catch (error) {
88
+ if (error?.code === "ENOENT") return;
89
+ if (error?.code !== "EEXIST") throw error;
90
+ }
91
+ await hooks.onPutBackBlocked?.();
92
+ if ((await stat(movedPath).catch(() => null)) === null) return;
93
+ const remainingMs = deadline - Date.now();
94
+ if (remainingMs <= 0) return;
95
+ await waitForLockRelease(lockPath, Math.min(remainingMs, LOCK_POLL_INTERVAL_MS));
96
+ }
97
+ }
98
+
99
+ // Unlink only entries that still name the lock we created: if ours was
100
+ // reclaimed under us, the file at this path belongs to the next owner. A
101
+ // reclaimer may be holding our lock moved aside, so those copies go first;
102
+ // checked in this order, a copy put back meanwhile is found under the name.
103
+ async function releaseLock(lockPath, ino, token) {
104
+ const prefix = `${path.basename(lockPath)}.`;
105
+ const entries = await readdir(path.dirname(lockPath)).catch(() => []);
106
+ for (const entry of entries) {
107
+ if (!entry.startsWith(prefix) || !entry.endsWith(".stale")) continue;
108
+ const moved = path.join(path.dirname(lockPath), entry);
109
+ if (isSameLock(await readLockState(moved).catch(() => null), ino, token)) {
110
+ await unlinkIfPresent(moved);
111
+ }
112
+ }
113
+ if (!isSameLock(await readLockState(lockPath), ino, token)) return;
114
+ await unlinkIfPresent(lockPath);
115
+ }
116
+
117
+ function waitForLockRelease(lockPath, timeoutMs) {
118
+ return new Promise((resolve, reject) => {
119
+ let settled = false;
120
+ let watcher;
121
+ let timer;
122
+ const finish = (error, released) => {
123
+ if (settled) return;
124
+ settled = true;
125
+ clearTimeout(timer);
126
+ watcher?.close();
127
+ if (error) reject(error);
128
+ else resolve(released);
129
+ };
130
+ const check = () => {
131
+ stat(lockPath).then(
132
+ () => {},
133
+ (error) => {
134
+ if (error?.code === "ENOENT") finish(null, true);
135
+ else finish(error);
136
+ },
137
+ );
138
+ };
139
+ try {
140
+ watcher = watch(path.dirname(lockPath), (event, filename) => {
141
+ if (
142
+ (event === "rename" || event === "change") &&
143
+ (!filename || filename.toString() === path.basename(lockPath))
144
+ ) {
145
+ check();
146
+ }
147
+ });
148
+ } catch (error) {
149
+ if (error?.code !== "ENOENT") {
150
+ finish(error);
151
+ return;
152
+ }
153
+ }
154
+ timer = setTimeout(() => finish(null, false), timeoutMs);
155
+ check();
156
+ });
157
+ }
158
+
159
+ async function acquireLock(lockPath, timeoutMs, staleGraceMs, lookupStartTime) {
160
+ const deadline = Date.now() + timeoutMs;
161
+ // A live holder's start time cannot change while we wait, and on Windows each
162
+ // lookup is a PowerShell call of about a second, so one acquire looks each
163
+ // holder pid up once. A holder that dies is still caught by isPidAlive; only
164
+ // a pid reused within one wait pass escapes it, and the wait then times out.
165
+ const startTimes = new Map();
166
+ const startTimeOf = async (pid) => {
167
+ if (!startTimes.has(pid)) {
168
+ const start = await lookupStartTime(pid);
169
+ if (start === null) return null;
170
+ startTimes.set(pid, start);
171
+ }
172
+ return startTimes.get(pid);
173
+ };
174
+ let holderPid = null;
175
+ for (;;) {
176
+ const ours = await createLock(lockPath);
177
+ if (ours !== null) return () => releaseLock(lockPath, ours.ino, ours.token);
178
+ if (Date.now() >= deadline) {
179
+ throw new Error(
180
+ `timed out waiting for instance lock held by pid ${holderPid ?? "unknown"}`,
181
+ );
182
+ }
183
+
184
+ const state = await readLockState(lockPath);
185
+ if (state === null) continue;
186
+ holderPid = state.pid ?? holderPid;
187
+ if (await isAbandoned(state, staleGraceMs, startTimeOf)) {
188
+ await reclaimLock(lockPath, state.ino, state.token, { deadline });
189
+ // Jitter keeps two reclaimers from retrying in lockstep forever.
190
+ await sleep(Math.random() * LOCK_RETRY_JITTER_MS);
191
+ continue;
192
+ }
193
+ const remainingMs = deadline - Date.now();
194
+ if (remainingMs > 0) {
195
+ await waitForLockRelease(lockPath, Math.min(remainingMs, LOCK_POLL_INTERVAL_MS));
196
+ }
197
+ }
198
+ }
199
+
200
+ async function acquireLocalLock(key) {
201
+ const previous = localLocks.get(key) ?? Promise.resolve();
202
+ let releaseCurrent;
203
+ const current = new Promise((resolve) => {
204
+ releaseCurrent = resolve;
205
+ });
206
+ const tail = previous.then(() => current);
207
+ localLocks.set(key, tail);
208
+ await previous;
209
+ return () => {
210
+ releaseCurrent();
211
+ if (localLocks.get(key) === tail) localLocks.delete(key);
212
+ };
213
+ }
214
+
215
+ // Runs `run` while this process and every other one agree we hold `lockPath`.
216
+ // A lock with no readable identity counts as held until it is `staleGraceMs` old.
217
+ // `startTimeOf` lets a test count start-time lookups; production callers pass none.
218
+ export async function withLock(lockPath, { timeoutMs, staleGraceMs, startTimeOf = processStartTime }, run) {
219
+ const releaseLocal = await acquireLocalLock(lockPath);
220
+ try {
221
+ const release = await acquireLock(lockPath, timeoutMs, staleGraceMs, startTimeOf);
222
+ try {
223
+ return await run();
224
+ } finally {
225
+ await release();
226
+ }
227
+ } finally {
228
+ releaseLocal();
229
+ }
230
+ }