@lobu/runtime-device 24.1.1 → 24.3.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.
- package/dist/component.json +1 -1
- package/dist/dependencies.bun.lock +46 -134
- package/npm-shrinkwrap.json +229 -730
- package/package.json +1 -1
- package/vendor/cli/dist/bundled-skills/lobu/SKILL.md +20 -5
- package/vendor/cli/dist/chrome-extension/accessibility-tree.js +834 -0
- package/vendor/cli/dist/chrome-extension/ambient-context.js +53 -0
- package/vendor/cli/dist/chrome-extension/auth-refresh.js +167 -0
- package/vendor/cli/dist/chrome-extension/automation-run.js +13 -0
- package/vendor/cli/dist/chrome-extension/background.js +1155 -0
- package/vendor/cli/dist/chrome-extension/bridge.js +140 -0
- package/vendor/cli/dist/chrome-extension/browser-origin-policy.js +62 -0
- package/vendor/cli/dist/chrome-extension/browser-recovery.js +260 -0
- package/vendor/cli/dist/chrome-extension/browser-url-sanitizer.js +284 -0
- package/vendor/cli/dist/chrome-extension/completion-response.js +12 -0
- package/vendor/cli/dist/chrome-extension/config.js +144 -0
- package/vendor/cli/dist/chrome-extension/connector-manifests/chrome.json +1516 -0
- package/vendor/cli/dist/chrome-extension/connector-manifests/chrome_bookmarks.json +84 -0
- package/vendor/cli/dist/chrome-extension/connector-manifests/chrome_downloads.json +86 -0
- package/vendor/cli/dist/chrome-extension/connector-manifests/chrome_history.json +78 -0
- package/vendor/cli/dist/chrome-extension/connector-manifests.js +1767 -0
- package/vendor/cli/dist/chrome-extension/console-capture.js +627 -0
- package/vendor/cli/dist/chrome-extension/control-activity.js +70 -0
- package/vendor/cli/dist/chrome-extension/debugger-lease.js +439 -0
- package/vendor/cli/dist/chrome-extension/feed-listener.js +488 -0
- package/vendor/cli/dist/chrome-extension/feed-transaction.js +48 -0
- package/vendor/cli/dist/chrome-extension/feeds.js +632 -0
- package/vendor/cli/dist/chrome-extension/focus-tab.js +25 -0
- package/vendor/cli/dist/chrome-extension/fonts/dm-mono-latin-500-normal.woff2 +0 -0
- package/vendor/cli/dist/chrome-extension/fonts/dm-sans-latin-400-normal.woff2 +0 -0
- package/vendor/cli/dist/chrome-extension/fonts/dm-sans-latin-600-normal.woff2 +0 -0
- package/vendor/cli/dist/chrome-extension/heartbeat.js +140 -0
- package/vendor/cli/dist/chrome-extension/icons/icon-128-rec.png +0 -0
- package/vendor/cli/dist/chrome-extension/icons/icon-128.png +0 -0
- package/vendor/cli/dist/chrome-extension/icons/icon-16-rec.png +0 -0
- package/vendor/cli/dist/chrome-extension/icons/icon-16.png +0 -0
- package/vendor/cli/dist/chrome-extension/icons/icon-32-rec.png +0 -0
- package/vendor/cli/dist/chrome-extension/icons/icon-32.png +0 -0
- package/vendor/cli/dist/chrome-extension/icons/icon-48-rec.png +0 -0
- package/vendor/cli/dist/chrome-extension/icons/icon-48.png +0 -0
- package/vendor/cli/dist/chrome-extension/lens-page.js +135 -0
- package/vendor/cli/dist/chrome-extension/lens-target.js +20 -0
- package/vendor/cli/dist/chrome-extension/manifest.json +76 -0
- package/vendor/cli/dist/chrome-extension/network-intercept.js +657 -0
- package/vendor/cli/dist/chrome-extension/page-activation.js +120 -0
- package/vendor/cli/dist/chrome-extension/page-dialogs.js +125 -0
- package/vendor/cli/dist/chrome-extension/page-url.js +11 -0
- package/vendor/cli/dist/chrome-extension/panel-session.js +184 -0
- package/vendor/cli/dist/chrome-extension/poll-auth.js +85 -0
- package/vendor/cli/dist/chrome-extension/poll-backoff.js +22 -0
- package/vendor/cli/dist/chrome-extension/run-fence.js +117 -0
- package/vendor/cli/dist/chrome-extension/setup-status.js +71 -0
- package/vendor/cli/dist/chrome-extension/side-panel-action-click.js +65 -0
- package/vendor/cli/dist/chrome-extension/sidepanel-context.js +50 -0
- package/vendor/cli/dist/chrome-extension/sidepanel.html +680 -0
- package/vendor/cli/dist/chrome-extension/sidepanel.js +1643 -0
- package/vendor/cli/dist/chrome-extension/tab-labels.js +233 -0
- package/vendor/cli/dist/chrome-extension/tools.js +3354 -0
- package/vendor/cli/dist/chrome-extension/watch-observer.js +354 -0
- package/vendor/cli/dist/chrome-extension/watch-redact.js +96 -0
- package/vendor/cli/dist/chrome-extension/watch.js +348 -0
- package/vendor/cli/dist/commands/_lib/apply/desired-state.d.ts +6 -4
- package/vendor/cli/dist/commands/_lib/apply/desired-state.js +5 -3
- package/vendor/cli/dist/commands/_lib/apply/diff.js +4 -3
- package/vendor/cli/dist/commands/_lib/chrome-for-testing.d.ts +13 -0
- package/vendor/cli/dist/commands/_lib/chrome-for-testing.js +226 -0
- package/vendor/cli/dist/commands/_lib/device-credential.d.ts +34 -0
- package/vendor/cli/dist/commands/_lib/device-credential.js +157 -0
- package/vendor/cli/dist/commands/_lib/device-wizard.d.ts +17 -0
- package/vendor/cli/dist/commands/_lib/device-wizard.js +11 -2
- package/vendor/cli/dist/commands/_lib/native-messaging.d.ts +7 -0
- package/vendor/cli/dist/commands/_lib/native-messaging.js +40 -0
- package/vendor/cli/dist/commands/browser.d.ts +23 -0
- package/vendor/cli/dist/commands/browser.js +492 -0
- package/vendor/cli/dist/commands/call.d.ts +12 -0
- package/vendor/cli/dist/commands/call.js +48 -26
- package/vendor/cli/dist/commands/chat.d.ts +0 -12
- package/vendor/cli/dist/commands/chat.js +1 -1
- package/vendor/cli/dist/commands/context.d.ts +3 -1
- package/vendor/cli/dist/commands/context.js +13 -2
- package/vendor/cli/dist/commands/daemon.js +38 -171
- package/vendor/cli/dist/commands/doctor.d.ts +0 -6
- package/vendor/cli/dist/commands/doctor.js +1 -1
- package/vendor/cli/dist/commands/memory/_lib/errors.d.ts +4 -1
- package/vendor/cli/dist/commands/memory/_lib/errors.js +9 -1
- package/vendor/cli/dist/commands/memory/_lib/mcp.d.ts +0 -9
- package/vendor/cli/dist/commands/memory/_lib/mcp.js +1 -23
- package/vendor/cli/dist/commands/memory/_lib/memory-auth.d.ts +20 -4
- package/vendor/cli/dist/commands/memory/_lib/memory-auth.js +21 -17
- package/vendor/cli/dist/commands/memory/_lib/seed-cmd.d.ts +0 -8
- package/vendor/cli/dist/commands/memory/_lib/seed-cmd.js +7 -18
- package/vendor/cli/dist/commands/memory/run.js +4 -25
- package/vendor/cli/dist/config/define.d.ts +1 -1
- package/vendor/cli/dist/connectors/gchat.ts +31 -1
- package/vendor/cli/dist/connectors/google_gmail.ts +70 -41
- package/vendor/cli/dist/connectors/google_gmail_references.ts +406 -0
- package/vendor/cli/dist/connectors/linkedin-inbox-adapter.js +107 -36
- package/vendor/cli/dist/connectors/linkedin-inbox.ts +13 -0
- package/vendor/cli/dist/connectors/linkedin.ts +1 -1
- package/vendor/cli/dist/connectors/postgres.ts +14 -8
- package/vendor/cli/dist/connectors/reddit.ts +171 -5
- package/vendor/cli/dist/connectors/whatsapp-web-adapter.js +147 -61
- package/vendor/cli/dist/connectors/whatsapp-web-helpers.ts +6 -2
- package/vendor/cli/dist/connectors/whatsapp_web.ts +48 -17
- package/vendor/cli/dist/connectors/x-dm-adapter.js +252 -35
- package/vendor/cli/dist/connectors/x.ts +189 -13
- package/vendor/cli/dist/index.js +46 -6
- package/vendor/cli/dist/internal/cli-update-check.d.ts +33 -0
- package/vendor/cli/dist/internal/cli-update-check.js +98 -0
- package/vendor/cli/dist/internal/credentials.js +10 -36
- package/vendor/cli/dist/internal/device-state.d.ts +2 -0
- package/vendor/cli/dist/internal/device-state.js +4 -0
- package/vendor/cli/dist/internal/gateway-url.d.ts +0 -8
- package/vendor/cli/dist/internal/gateway-url.js +1 -1
- package/vendor/cli/dist/runtime-components.json +4 -4
- package/vendor/cli/dist/templates/AGENTS.md.tmpl +3 -1
- package/vendor/cli/dist/templates/TESTING.md.tmpl +8 -8
- package/vendor/cli/package.json +1 -1
- package/vendor/connector-sdk/dist/browser-requirement.js +1 -1
- package/vendor/connector-sdk/dist/connector-types.d.ts +34 -1
- package/vendor/connector-sdk/dist/index.d.ts +3 -1
- package/vendor/connector-sdk/dist/index.js +4 -0
- package/vendor/connector-sdk/dist/redact.d.ts +10 -0
- package/vendor/{connector-worker/dist/executor → connector-sdk/dist}/redact.js +0 -49
- package/vendor/connector-sdk/dist/types.d.ts +2 -1
- package/vendor/connector-sdk/package.json +6 -1
- package/vendor/connector-worker/dist/agent-turn/gateway-tools.d.ts +9 -0
- package/vendor/connector-worker/dist/agent-turn/gateway-tools.js +23 -2
- package/vendor/connector-worker/dist/agent-turn/guest-entry.js +14 -6
- package/vendor/connector-worker/dist/agent-turn/guest.bundle.js +62 -25
- package/vendor/connector-worker/dist/agent-turn/media-tools.d.ts +1 -0
- package/vendor/connector-worker/dist/agent-turn/media-tools.js +5 -2
- package/vendor/connector-worker/dist/bin.js +2 -0
- package/vendor/connector-worker/dist/daemon/acp-adapters/THIRD_PARTY_LICENSES.txt +1 -1
- package/vendor/connector-worker/dist/daemon/acp-adapters/claude.js +7339 -2136
- package/vendor/connector-worker/dist/daemon/agent-turn.js +52 -17
- package/vendor/connector-worker/dist/daemon/automation.js +3 -13
- package/vendor/connector-worker/dist/daemon/client.d.ts +23 -3
- package/vendor/connector-worker/dist/daemon/client.js +59 -5
- package/vendor/connector-worker/dist/daemon/executor.d.ts +0 -28
- package/vendor/connector-worker/dist/daemon/executor.js +27 -12
- package/vendor/connector-worker/dist/daemon/index.d.ts +1 -1
- package/vendor/connector-worker/dist/daemon/index.js +1 -1
- package/vendor/connector-worker/dist/daemon/interactive-session.js +29 -6
- package/vendor/connector-worker/dist/daemon/poll-loop.js +29 -2
- package/vendor/connector-worker/dist/daemon/start.d.ts +0 -1
- package/vendor/connector-worker/dist/daemon/start.js +1 -1
- package/vendor/connector-worker/dist/daemon/terminal-delivery.d.ts +6 -10
- package/vendor/connector-worker/dist/daemon/terminal-delivery.js +59 -23
- package/vendor/connector-worker/dist/daemon/terminal-failure.js +2 -2
- package/vendor/connector-worker/dist/executor/isolate.js +4 -1
- package/vendor/connector-worker/dist/isolate/bridge.d.ts +6 -2
- package/vendor/connector-worker/dist/isolate/bridge.js +14 -8
- package/vendor/connector-worker/package.json +2 -2
- package/vendor/core/dist/agent-tooling.d.ts +8 -0
- package/vendor/core/dist/agent-tooling.js +9 -1
- package/vendor/core/dist/capabilities.d.ts +1 -1
- package/vendor/core/dist/capabilities.js +5 -0
- package/vendor/core/dist/connector-query-errors.d.ts +6 -0
- package/vendor/core/dist/connector-query-errors.js +4 -0
- package/vendor/core/dist/contracts/entity-reference.d.ts +3 -0
- package/vendor/core/dist/contracts/entity-reference.js +31 -0
- package/vendor/core/dist/contracts/tools/manage-automations.d.ts +3 -0
- package/vendor/core/dist/contracts/tools/manage-automations.js +4 -1
- package/vendor/core/dist/contracts/tools/manage-connections.js +1 -1
- package/vendor/core/dist/contracts/tools/manage-entity-schema.js +2 -2
- package/vendor/core/dist/contracts/tools/manage-entity.d.ts +117 -34
- package/vendor/core/dist/contracts/tools/manage-entity.js +47 -14
- package/vendor/core/dist/contracts/tools/manage-operations.d.ts +13 -4
- package/vendor/core/dist/contracts/tools/manage-operations.js +19 -7
- package/vendor/core/dist/contracts/tools/read-knowledge.d.ts +6 -1
- package/vendor/core/dist/contracts/tools/read-knowledge.js +20 -1
- package/vendor/core/dist/contracts/tools/view-attach.d.ts +1 -1
- package/vendor/core/dist/contracts/tools/view-runtime.d.ts +2 -0
- package/vendor/core/dist/contracts/tools/view-runtime.js +3 -1
- package/vendor/core/dist/contracts/worker/device-automation.d.ts +4 -9
- package/vendor/core/dist/contracts/worker/device-automation.js +10 -15
- package/vendor/core/dist/contracts/worker/protocol.d.ts +20 -0
- package/vendor/core/dist/contracts/worker/protocol.js +20 -0
- package/vendor/core/dist/errors.d.ts +0 -20
- package/vendor/core/dist/errors.js +1 -25
- package/vendor/core/dist/otel.js +38 -10
- package/vendor/core/dist/reserved.d.ts +6 -3
- package/vendor/core/dist/reserved.js +6 -3
- package/vendor/core/dist/utils/ajv.js +3 -1
- package/vendor/core/dist/worker/auth.d.ts +3 -2
- package/vendor/core/package.json +31 -5
- package/vendor/postgres/cf/src/query.js +3 -1
- package/vendor/postgres/cjs/src/query.js +3 -1
- package/vendor/postgres/src/query.js +3 -1
- package/vendor/connector-worker/dist/executor/http-status.d.ts +0 -16
- package/vendor/connector-worker/dist/executor/http-status.js +0 -21
- package/vendor/connector-worker/dist/executor/redact.d.ts +0 -29
- /package/vendor/postgres/{.bun-tag-2f292c6554a8475e → .bun-tag-22d644c2873790db} +0 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// Typed postMessage broker between the sidepanel iframe (owletto at
|
|
2
|
+
// gateway origin) and the extension service worker. Pi's review called this out as
|
|
3
|
+
// a security boundary: the iframe is hosted code we trust today, but we
|
|
4
|
+
// design the protocol as if it might not be. Deny-by-default, named ops only,
|
|
5
|
+
// origin checks, correlation IDs. No "run script X on tab Y" RPC ever.
|
|
6
|
+
//
|
|
7
|
+
// Wire-level shape:
|
|
8
|
+
// iframe → extension: { id, op: "<name>", params?: object }
|
|
9
|
+
// extension → iframe: { id, ok: true, result?: object }
|
|
10
|
+
// | { id, ok: false, error: string }
|
|
11
|
+
|
|
12
|
+
import { getGatewayUrl } from "./config.js";
|
|
13
|
+
import { focusChromeTab } from "./focus-tab.js";
|
|
14
|
+
|
|
15
|
+
const ALLOWED_OPS = new Set([
|
|
16
|
+
"getActiveTabContext",
|
|
17
|
+
"listTabs",
|
|
18
|
+
"openTab",
|
|
19
|
+
"closeTab",
|
|
20
|
+
"focusTab",
|
|
21
|
+
"captureVisibleTab",
|
|
22
|
+
"requestOptionalPermission",
|
|
23
|
+
]);
|
|
24
|
+
|
|
25
|
+
// The sidepanel iframes the WHOLE app at the gateway's root, so the
|
|
26
|
+
// trusted iframe origin is just the gateway origin. (Earlier this used
|
|
27
|
+
// a separate `getEmbeddedAppUrl()` helper; #233 removed it but left
|
|
28
|
+
// this caller importing it, which crashed the service worker.)
|
|
29
|
+
async function expectedEmbeddedOrigin() {
|
|
30
|
+
const url = await getGatewayUrl();
|
|
31
|
+
return url ? new URL(url).origin : null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function installBridge() {
|
|
35
|
+
chrome.runtime.onConnect.addListener((port) => {
|
|
36
|
+
if (port.name !== "owletto.sidepanel") return;
|
|
37
|
+
// The sidepanel forwards iframe messages over a runtime port so the
|
|
38
|
+
// service worker can stay asleep until needed.
|
|
39
|
+
port.onMessage.addListener(async (msg) => {
|
|
40
|
+
const { id, op, params, origin } = msg ?? {};
|
|
41
|
+
if (typeof id !== "string" || typeof op !== "string") {
|
|
42
|
+
return; // unsigned/malformed, drop silently
|
|
43
|
+
}
|
|
44
|
+
const expected = await expectedEmbeddedOrigin();
|
|
45
|
+
if (!expected || origin !== expected) {
|
|
46
|
+
port.postMessage({ id, ok: false, error: "untrusted_origin" });
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (!ALLOWED_OPS.has(op)) {
|
|
50
|
+
port.postMessage({ id, ok: false, error: "unknown_op" });
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
const result = await dispatchBridgeOperation(op, params ?? {});
|
|
55
|
+
port.postMessage({ id, ok: true, result });
|
|
56
|
+
} catch (err) {
|
|
57
|
+
port.postMessage({
|
|
58
|
+
id,
|
|
59
|
+
ok: false,
|
|
60
|
+
error: err instanceof Error ? err.message : String(err),
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export async function dispatchBridgeOperation(op, params) {
|
|
68
|
+
switch (op) {
|
|
69
|
+
case "getActiveTabContext": {
|
|
70
|
+
const [tab] = await chrome.tabs.query({
|
|
71
|
+
active: true,
|
|
72
|
+
lastFocusedWindow: true,
|
|
73
|
+
});
|
|
74
|
+
if (!tab) return null;
|
|
75
|
+
return { tabId: tab.id, url: tab.url, title: tab.title };
|
|
76
|
+
}
|
|
77
|
+
case "listTabs": {
|
|
78
|
+
const tabs = await chrome.tabs.query({});
|
|
79
|
+
return tabs.map((t) => ({
|
|
80
|
+
tabId: t.id,
|
|
81
|
+
url: t.url,
|
|
82
|
+
title: t.title,
|
|
83
|
+
windowId: t.windowId,
|
|
84
|
+
active: t.active,
|
|
85
|
+
}));
|
|
86
|
+
}
|
|
87
|
+
case "openTab": {
|
|
88
|
+
const rawUrl = stringOrThrow(params.url, "url");
|
|
89
|
+
let url;
|
|
90
|
+
try {
|
|
91
|
+
url = new URL(rawUrl);
|
|
92
|
+
} catch {
|
|
93
|
+
throw new Error("invalid_param:url");
|
|
94
|
+
}
|
|
95
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
96
|
+
throw new Error("invalid_param:url");
|
|
97
|
+
}
|
|
98
|
+
const tab = await chrome.tabs.create({
|
|
99
|
+
url: url.toString(),
|
|
100
|
+
active: params.active !== false,
|
|
101
|
+
});
|
|
102
|
+
return { tabId: tab.id };
|
|
103
|
+
}
|
|
104
|
+
case "closeTab": {
|
|
105
|
+
await chrome.tabs.remove(numberOrThrow(params.tabId, "tabId"));
|
|
106
|
+
return { closed: true };
|
|
107
|
+
}
|
|
108
|
+
case "focusTab": {
|
|
109
|
+
const tabId = numberOrThrow(params.tabId, "tabId");
|
|
110
|
+
return await focusChromeTab(chrome, tabId);
|
|
111
|
+
}
|
|
112
|
+
case "captureVisibleTab": {
|
|
113
|
+
const dataUrl = await chrome.tabs.captureVisibleTab(undefined, {
|
|
114
|
+
format: "png",
|
|
115
|
+
});
|
|
116
|
+
return { dataUrl };
|
|
117
|
+
}
|
|
118
|
+
case "requestOptionalPermission": {
|
|
119
|
+
const perm = stringOrThrow(params.permission, "permission");
|
|
120
|
+
const granted = await chrome.permissions.request({ permissions: [perm] });
|
|
121
|
+
return { granted };
|
|
122
|
+
}
|
|
123
|
+
default:
|
|
124
|
+
throw new Error("unhandled_op");
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function stringOrThrow(v, name) {
|
|
129
|
+
if (typeof v !== "string" || v.length === 0) {
|
|
130
|
+
throw new Error(`missing_param:${name}`);
|
|
131
|
+
}
|
|
132
|
+
return v;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function numberOrThrow(v, name) {
|
|
136
|
+
if (typeof v !== "number" || !Number.isFinite(v)) {
|
|
137
|
+
throw new Error(`missing_param:${name}`);
|
|
138
|
+
}
|
|
139
|
+
return v;
|
|
140
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/** Exact origins for delegated connectors; legacy host patterns for native browser tools. */
|
|
2
|
+
export function browserUrlAllowed(url, allowedOrigins) {
|
|
3
|
+
if (!Array.isArray(allowedOrigins) || allowedOrigins.length === 0)
|
|
4
|
+
return true;
|
|
5
|
+
let parsed;
|
|
6
|
+
try {
|
|
7
|
+
parsed = new URL(url);
|
|
8
|
+
} catch {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
return allowedOrigins.some((pattern) => {
|
|
12
|
+
if (typeof pattern !== "string") return false;
|
|
13
|
+
if (pattern.includes("://")) return parsed.origin === pattern;
|
|
14
|
+
return (
|
|
15
|
+
pattern === parsed.hostname ||
|
|
16
|
+
(pattern.startsWith("*.") && parsed.hostname.endsWith(pattern.slice(1)))
|
|
17
|
+
);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// True when everything `pattern` grants is also granted by `policy`.
|
|
22
|
+
function grantContained(pattern, policy) {
|
|
23
|
+
if (policy.includes(pattern)) return true;
|
|
24
|
+
if (pattern.includes("://")) return browserUrlAllowed(pattern, policy);
|
|
25
|
+
return policy.some(
|
|
26
|
+
(parent) => parent.startsWith("*.") && pattern.endsWith(parent.slice(1)),
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** A captured session cannot lend a broader grant to its next caller. */
|
|
31
|
+
export function assertBrowserSessionGrant(row, runCtx) {
|
|
32
|
+
const grant = runCtx?.allowedOrigins;
|
|
33
|
+
if (!grant?.length || !row) return;
|
|
34
|
+
const stored = row.allowed_origins;
|
|
35
|
+
if (
|
|
36
|
+
!stored?.length ||
|
|
37
|
+
!stored.every((origin) => grantContained(origin, grant))
|
|
38
|
+
) {
|
|
39
|
+
throw new Error(
|
|
40
|
+
"Browser capture session is outside the current allowedOrigins",
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Honor both the Chrome connection policy and the per-operation grant. */
|
|
46
|
+
export function browserRunOrigins(config, input) {
|
|
47
|
+
const configured = Array.isArray(config) ? config : [];
|
|
48
|
+
const requested = Array.isArray(input) ? input : [];
|
|
49
|
+
if (!configured.length) return requested;
|
|
50
|
+
if (!requested.length) return configured;
|
|
51
|
+
const common = [
|
|
52
|
+
...new Set([
|
|
53
|
+
...configured.filter((pattern) => grantContained(pattern, requested)),
|
|
54
|
+
...requested.filter((pattern) => grantContained(pattern, configured)),
|
|
55
|
+
]),
|
|
56
|
+
];
|
|
57
|
+
if (!common.length)
|
|
58
|
+
throw new Error(
|
|
59
|
+
"Browser operation is outside the connection allowedOrigins",
|
|
60
|
+
);
|
|
61
|
+
return common;
|
|
62
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
// The gateway owns authorization and the durable lease. This device-side fence
|
|
2
|
+
// joins that lease to an exact Chrome target and serializes work on that target.
|
|
3
|
+
import { withKeyLock } from './tab-labels.js';
|
|
4
|
+
import { acquireDebuggerLease, releaseDebuggerLease } from './debugger-lease.js';
|
|
5
|
+
import { browserUrlAllowed } from './browser-origin-policy.js';
|
|
6
|
+
|
|
7
|
+
const KEY = 'owletto.browser_recovery';
|
|
8
|
+
export const RECOVERY_ALARM = 'owletto.browser_recovery_expiry';
|
|
9
|
+
const observations = new Set(['screenshot', 'get_accessibility_tree']);
|
|
10
|
+
const actions = new Set(['click_ref', 'focus_tab']);
|
|
11
|
+
const runtimeId = crypto.randomUUID();
|
|
12
|
+
const localDeadline = ms => ({ runtime_id: runtimeId, wall: Date.now() + ms, monotonic: performance.now() + ms });
|
|
13
|
+
function remaining(deadline) {
|
|
14
|
+
// A restarted service worker cannot restore a monotonic clock. Reap its
|
|
15
|
+
// exact owned targets instead of resurrecting an uncertain lease.
|
|
16
|
+
if (!deadline || deadline.runtime_id !== runtimeId) return 0;
|
|
17
|
+
return Math.max(0, Math.min(deadline.wall - Date.now(), deadline.monotonic - performance.now()));
|
|
18
|
+
}
|
|
19
|
+
export function recoveryDeadline(grant, startedAt, receivedAt) {
|
|
20
|
+
const rtt = receivedAt - startedAt;
|
|
21
|
+
if (!grant || !Number.isFinite(grant.remaining_ms) || grant.remaining_ms <= 0 || grant.remaining_ms > 300_000
|
|
22
|
+
|| !Number.isFinite(grant.poll_elapsed_ms) || grant.poll_elapsed_ms < 0 || grant.poll_elapsed_ms > rtt + 1) return null;
|
|
23
|
+
// Subtract the whole network round trip, excluding the gateway's long poll.
|
|
24
|
+
// This conservatively accounts for delivery delay without clock sync.
|
|
25
|
+
const ms = grant.remaining_ms - Math.max(0, rtt - grant.poll_elapsed_ms) - (performance.now() - receivedAt) - 1;
|
|
26
|
+
return ms > 0 ? localDeadline(ms) : null;
|
|
27
|
+
}
|
|
28
|
+
const fail = message => { throw new Error(`Browser recovery: ${message}`); };
|
|
29
|
+
const read = async api => (await api.storage.local.get(KEY))?.[KEY] ?? {};
|
|
30
|
+
async function save(api, id, record) {
|
|
31
|
+
// Only the storage merge is device-wide; page work never holds this lock.
|
|
32
|
+
await withKeyLock(KEY, async () => {
|
|
33
|
+
const records = await read(api);
|
|
34
|
+
if (record) records[id] = record;
|
|
35
|
+
else delete records[id];
|
|
36
|
+
await api.storage.local.set({ [KEY]: records });
|
|
37
|
+
if (!Object.keys(records).length) await api.alarms.clear(RECOVERY_ALARM);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
const recoveryLock = id => `${KEY}:${id}`;
|
|
41
|
+
const control = ctx => ctx?.recoveryControl;
|
|
42
|
+
async function target(api, tabId) {
|
|
43
|
+
const [tab, targets] = await Promise.all([api.tabs.get(tabId), api.debugger.getTargets()]);
|
|
44
|
+
const found = targets.find(item => item.type === 'page' && item.tabId === tabId);
|
|
45
|
+
if (!found) fail('tab is closed or no longer has its original target');
|
|
46
|
+
return { tab_id: tabId, target_id: found.id, window_id: tab.windowId, url: tab.url };
|
|
47
|
+
}
|
|
48
|
+
function exact(record, current) {
|
|
49
|
+
if (record.target_id !== current.target_id || record.window_id !== current.window_id || record.tab_id !== current.tab_id) fail('tab, target, or window changed');
|
|
50
|
+
}
|
|
51
|
+
export async function assertRecoveryTab(api, tabId, ctx) {
|
|
52
|
+
const records = await read(api);
|
|
53
|
+
const record = Object.values(records).find(item => item.tab_id === tabId);
|
|
54
|
+
if (!record) return;
|
|
55
|
+
// Even expired records remain fenced until cleanup acknowledges their target.
|
|
56
|
+
exact(record, await target(api, tabId));
|
|
57
|
+
const grant = control(ctx);
|
|
58
|
+
if (!grant || grant.recovery_id !== record.recovery_id || grant.owner_run_id !== record.owner_run_id
|
|
59
|
+
|| !remaining(ctx.recoveryDeadline) || !remaining(record.deadline)) fail('tab belongs to another read or recovery');
|
|
60
|
+
}
|
|
61
|
+
// An isolated world lives only as long as its CDP session, so one session stays
|
|
62
|
+
// attached from each observation until the recovery resumes or closes.
|
|
63
|
+
const sessions = new Map();
|
|
64
|
+
async function unpin(api, recoveryId) {
|
|
65
|
+
const lease = sessions.get(recoveryId);
|
|
66
|
+
sessions.delete(recoveryId);
|
|
67
|
+
if (lease) await releaseDebuggerLease(api, lease);
|
|
68
|
+
}
|
|
69
|
+
async function pin(api, record) {
|
|
70
|
+
await unpin(api, record.recovery_id);
|
|
71
|
+
sessions.set(record.recovery_id, await acquireDebuggerLease(api, record.tab_id));
|
|
72
|
+
}
|
|
73
|
+
async function stamp(api, tabId) {
|
|
74
|
+
const lease = await acquireDebuggerLease(api, tabId);
|
|
75
|
+
try {
|
|
76
|
+
const send = (method, params = {}) => api.debugger.sendCommand({ tabId }, method, params);
|
|
77
|
+
const { frameTree } = await send('Page.getFrameTree');
|
|
78
|
+
const { executionContextId } = await send('Page.createIsolatedWorld', { frameId: frameTree.frame.id, worldName: 'lobu-recovery-observation' });
|
|
79
|
+
const result = await send('Runtime.evaluate', { contextId: executionContextId, returnByValue: true,
|
|
80
|
+
expression: `(() => { if (!globalThis.__lobuRecoveryEpoch) { const s = globalThis.__lobuRecoveryEpoch = { id: crypto.randomUUID(), n: 1, page: 1 }; new MutationObserver(records => { s.n++; if (records.some(r => r.type !== 'attributes' || !r.attributeName.startsWith('data-lobu-'))) s.page++; }).observe(document, { subtree:true, childList:true, attributes:true, characterData:true }); } const s = __lobuRecoveryEpoch; return JSON.stringify({ world:s.id, epoch:s.n, page:s.page, href:location.href, width:innerWidth, height:innerHeight, dpr:devicePixelRatio, x:scrollX, y:scrollY }); })()` });
|
|
81
|
+
if (result.exceptionDetails || typeof result.result?.value !== 'string') fail('cannot establish an observation epoch');
|
|
82
|
+
const { epoch, ...page } = JSON.parse(result.result.value);
|
|
83
|
+
// An accessibility observation writes its own data-lobu-* refs; only the
|
|
84
|
+
// observation itself may do that, so `observed` ignores them and `exact` does not.
|
|
85
|
+
return { exact: JSON.stringify({ loader: frameTree.frame.loaderId, epoch, ...page }), observed: JSON.stringify({ loader: frameTree.frame.loaderId, ...page }) };
|
|
86
|
+
} finally { await releaseDebuggerLease(api, lease); }
|
|
87
|
+
}
|
|
88
|
+
async function stopCaptures(api, record, tools, ctx) {
|
|
89
|
+
for (const session_id of record.captures ?? []) {
|
|
90
|
+
const result = await tools.network_intercept_stop(api, { session_id }, ctx);
|
|
91
|
+
if (!result.ok || result.observation?.stopped !== true) fail('capture cleanup was not acknowledged');
|
|
92
|
+
}
|
|
93
|
+
record.captures = [];
|
|
94
|
+
}
|
|
95
|
+
async function closeOwned(api, record, tools, ctx) {
|
|
96
|
+
await unpin(api, record.recovery_id);
|
|
97
|
+
await stopCaptures(api, record, tools, ctx);
|
|
98
|
+
const current = await target(api, record.tab_id).catch(() => null);
|
|
99
|
+
if (!current || current.target_id !== record.target_id) return; // Never close a reused tab ID.
|
|
100
|
+
// A window move invalidates observations, not ownership of this same target.
|
|
101
|
+
await api.tabs.remove(record.tab_id); // Only the scrape-owned exact target.
|
|
102
|
+
}
|
|
103
|
+
export async function recoveryTabIds(api) {
|
|
104
|
+
return Object.values(await read(api)).map(record => record.tab_id);
|
|
105
|
+
}
|
|
106
|
+
export async function reapBrowserRecovery(api, tools) {
|
|
107
|
+
for (const [id, snapshot] of Object.entries(await read(api))) {
|
|
108
|
+
if (remaining(snapshot.deadline)) continue;
|
|
109
|
+
try {
|
|
110
|
+
await withKeyLock(recoveryLock(id), async () => {
|
|
111
|
+
const record = (await read(api))[id];
|
|
112
|
+
if (!record || remaining(record.deadline)) return;
|
|
113
|
+
await closeOwned(api, record, tools, { allowedOrigins: record.origins });
|
|
114
|
+
await save(api, id, null);
|
|
115
|
+
});
|
|
116
|
+
} catch (error) {
|
|
117
|
+
// Keep this target fenced for a later cleanup retry without blocking others.
|
|
118
|
+
console.error('Browser recovery cleanup failed', id, error);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
export async function runBrowserRecoveryTool(api, tools, key, args, ctx) {
|
|
123
|
+
let grant = control(ctx);
|
|
124
|
+
if (!grant) {
|
|
125
|
+
if (args?.recovery_id || args?.observation_id) fail('gateway recovery authorization is missing');
|
|
126
|
+
for (const tabId of [args?.tab_id, ...(Array.isArray(args?.tab_ids) ? args.tab_ids : [])]) {
|
|
127
|
+
if (Number.isInteger(tabId)) await assertRecoveryTab(api, tabId, ctx);
|
|
128
|
+
}
|
|
129
|
+
const records = await read(api);
|
|
130
|
+
if (args?.session_id && Object.values(records).some(record => record.captures?.includes(args.session_id))) fail('capture belongs to another read');
|
|
131
|
+
return tools[key](api, args, ctx);
|
|
132
|
+
}
|
|
133
|
+
if (args.persistent || args.group || args.window_id) fail('recovery cannot retarget browser work');
|
|
134
|
+
// Reap expired work before a new read, outside its per-recovery lock.
|
|
135
|
+
if (!(await read(api))[grant.recovery_id]) await reapBrowserRecovery(api, tools);
|
|
136
|
+
// Observation consumption and resume serialize only on their own target.
|
|
137
|
+
return withKeyLock(recoveryLock(grant.recovery_id), async () => {
|
|
138
|
+
if (!Number.isFinite(Date.parse(grant.expires_at)) || !remaining(ctx.recoveryDeadline)) fail('lease expired or dispatch timing is unavailable');
|
|
139
|
+
let record = (await read(api))[grant.recovery_id];
|
|
140
|
+
if (record && !remaining(record.deadline)) fail('retained lease expired or browser worker restarted');
|
|
141
|
+
let created = false;
|
|
142
|
+
if (!record) {
|
|
143
|
+
if (grant.mode !== 'read' || grant.resumed || key !== 'navigate' || args.new_tab !== true) fail('retained tab is unavailable');
|
|
144
|
+
// Create only a new scrape-owned tab. It is registered before later work.
|
|
145
|
+
const opened = await tools.navigate(api, { url: "about:blank", new_tab: true, wait_for_load: false }, { ...ctx, scratchTabId: null });
|
|
146
|
+
if (!opened.ok) {
|
|
147
|
+
// navigate created only our new scratch target; dispose it even if the
|
|
148
|
+
// initial about:blank navigation failed before registration.
|
|
149
|
+
if (Number.isInteger(opened.observation?.tab_id)) {
|
|
150
|
+
await tools.close_tab(api, { tab_id: opened.observation.tab_id }, ctx);
|
|
151
|
+
}
|
|
152
|
+
return opened;
|
|
153
|
+
}
|
|
154
|
+
const current = await target(api, opened.observation?.tab_id);
|
|
155
|
+
record = { ...current, recovery_id: grant.recovery_id, owner_run_id: grant.owner_run_id,
|
|
156
|
+
deadline: ctx.recoveryDeadline, origins: ctx.allowedOrigins, state: 'reading', captures: [], actions: 0, observations: 0 };
|
|
157
|
+
try {
|
|
158
|
+
await save(api, grant.recovery_id, record);
|
|
159
|
+
await api.alarms.create(RECOVERY_ALARM, { periodInMinutes: 0.5 });
|
|
160
|
+
} catch (error) {
|
|
161
|
+
// Registration failed before source work. Close only the target we just created.
|
|
162
|
+
await closeOwned(api, record, tools, ctx);
|
|
163
|
+
await save(api, grant.recovery_id, null);
|
|
164
|
+
throw error;
|
|
165
|
+
}
|
|
166
|
+
created = true;
|
|
167
|
+
grant = { ...grant, tab_id: record.tab_id, target_id: record.target_id, window_id: record.window_id };
|
|
168
|
+
args = { ...args, tab_id: record.tab_id, new_tab: false };
|
|
169
|
+
ctx = { ...ctx, recoveryControl: grant };
|
|
170
|
+
}
|
|
171
|
+
exact(record, await target(api, record.tab_id));
|
|
172
|
+
if (grant.tab_id !== record.tab_id || grant.target_id !== record.target_id || grant.window_id !== record.window_id) fail('gateway target does not match the retained target');
|
|
173
|
+
if (grant.resumed && grant.mode === 'read' && record.state === 'ready') {
|
|
174
|
+
await unpin(api, record.recovery_id);
|
|
175
|
+
record.owner_run_id = grant.owner_run_id;
|
|
176
|
+
record.state = 'reading';
|
|
177
|
+
record.observation = null;
|
|
178
|
+
}
|
|
179
|
+
if (record.owner_run_id !== grant.owner_run_id) fail('read owner changed');
|
|
180
|
+
if (grant.mode === 'read' && record.state !== 'reading') fail('old source read has ended');
|
|
181
|
+
if (args.tab_id !== undefined && args.tab_id !== record.tab_id) fail('a different tab was requested');
|
|
182
|
+
if (args.new_tab) fail('recovery cannot retarget browser work');
|
|
183
|
+
record.url = (await api.tabs.get(record.tab_id)).url;
|
|
184
|
+
if (!browserUrlAllowed(record.url, record.origins) && !(record.state === 'reading' && record.url === 'about:blank')) fail('retained tab left its allowed origin');
|
|
185
|
+
record.deadline = ctx.recoveryDeadline;
|
|
186
|
+
await save(api, grant.recovery_id, record);
|
|
187
|
+
// Session ids cannot borrow another reader's traffic, even on the same origin.
|
|
188
|
+
// This first recovery contract tracks network capture only.
|
|
189
|
+
if (key.startsWith('console_capture_')) fail('console capture is unsupported for a recovery read');
|
|
190
|
+
if (key.startsWith('network_intercept_') && (key !== 'network_intercept_start' || args.session_id !== undefined)
|
|
191
|
+
&& !record.captures.includes(args.session_id)) fail('capture does not belong to this source read');
|
|
192
|
+
const scoped = { ...args, tab_id: record.tab_id };
|
|
193
|
+
const scopedCtx = { ...ctx, allowedOrigins: record.origins, scratchTabId: record.tab_id };
|
|
194
|
+
if (grant.mode === 'retain') {
|
|
195
|
+
if (key !== 'close_tab' || record.state !== 'reading' || grant.resumed) fail('invalid retention request');
|
|
196
|
+
await stopCaptures(api, record, tools, scopedCtx);
|
|
197
|
+
record.state = 'ready';
|
|
198
|
+
record.observation = null;
|
|
199
|
+
await save(api, grant.recovery_id, record);
|
|
200
|
+
return { ok: true, observation: { cleanup_ack: true } };
|
|
201
|
+
}
|
|
202
|
+
const recovering = grant.mode === 'observe' || grant.mode === 'act';
|
|
203
|
+
if (recovering && record.state !== 'ready') fail('source read is still running or recovery was consumed');
|
|
204
|
+
if (grant.mode === 'observe' && (!observations.has(key) || record.observations >= 16)) fail('observation budget exhausted or operation is not an observation');
|
|
205
|
+
if (grant.mode === 'act') {
|
|
206
|
+
if (!actions.has(key) || record.actions >= 8) fail('action budget exhausted or operation needs human recovery');
|
|
207
|
+
if (!record.observation || record.observation.id !== args.observation_id || !remaining(record.observation.deadline)
|
|
208
|
+
|| record.observation.stamp !== (await stamp(api, record.tab_id)).exact) fail('observation is stale; observe this tab again');
|
|
209
|
+
record.observation = null;
|
|
210
|
+
record.actions++;
|
|
211
|
+
await save(api, grant.recovery_id, record); // Consume before dispatch, even if delivery fails.
|
|
212
|
+
}
|
|
213
|
+
if (grant.mode === 'observe') {
|
|
214
|
+
record.observations++;
|
|
215
|
+
record.observation = null;
|
|
216
|
+
await save(api, grant.recovery_id, record);
|
|
217
|
+
await pin(api, record);
|
|
218
|
+
}
|
|
219
|
+
const before = grant.mode === 'observe' ? (await stamp(api, record.tab_id)).observed : undefined;
|
|
220
|
+
if (!remaining(record.deadline)) fail('lease expired before browser work');
|
|
221
|
+
let timer;
|
|
222
|
+
let timedOut = false;
|
|
223
|
+
try {
|
|
224
|
+
const expired = new Promise((_, reject) => {
|
|
225
|
+
timer = setTimeout(() => {
|
|
226
|
+
timedOut = true;
|
|
227
|
+
// Closing the exact scrape-owned target terminates outstanding page
|
|
228
|
+
// promises as well as CDP work. No retained handoff follows a timeout.
|
|
229
|
+
void closeOwned(api, record, tools, scopedCtx).then(() => reject(new Error('Browser source deadline expired')), reject);
|
|
230
|
+
}, Math.max(1, remaining(record.deadline)));
|
|
231
|
+
});
|
|
232
|
+
const work = (async () => {
|
|
233
|
+
if (grant.mode === 'read' && key === 'close_tab') await stopCaptures(api, record, tools, scopedCtx);
|
|
234
|
+
const result = await tools[key](api, scoped, scopedCtx);
|
|
235
|
+
if (timedOut) {
|
|
236
|
+
if (key === 'network_intercept_start' && result.observation?.session_id) {
|
|
237
|
+
await tools.network_intercept_stop(api, { session_id: result.observation.session_id }, scopedCtx);
|
|
238
|
+
}
|
|
239
|
+
fail('source work completed after its deadline');
|
|
240
|
+
}
|
|
241
|
+
return result;
|
|
242
|
+
})();
|
|
243
|
+
const result = await Promise.race([work, expired]);
|
|
244
|
+
if (created) result.observation = { ...result.observation, browser_tab: { tab_id: record.tab_id, target_id: record.target_id, window_id: record.window_id } };
|
|
245
|
+
if (!result.ok) return result;
|
|
246
|
+
if (key === 'network_intercept_start') record.captures.push(result.observation.session_id);
|
|
247
|
+
if (key === 'network_intercept_stop') record.captures = record.captures.filter(id => id !== args.session_id);
|
|
248
|
+
if (grant.mode === 'observe') {
|
|
249
|
+
const after = await stamp(api, record.tab_id);
|
|
250
|
+
if (before !== after.observed) fail('page changed during observation');
|
|
251
|
+
if (key === 'screenshot' && !result.observation?.attachments?.some(item => item.kind === 'image' && typeof item.data === 'string' && item.data.length)) fail('screenshot contains no image bytes');
|
|
252
|
+
record.observation = { id: crypto.randomUUID(), stamp: after.exact, deadline: localDeadline(30_000) };
|
|
253
|
+
result.observation = { ...result.observation, recovery_id: grant.recovery_id, observation_id: record.observation.id,
|
|
254
|
+
observation_expires_at: new Date(Date.parse(grant.expires_at) - Math.max(0, remaining(record.deadline) - 30_000)).toISOString(), target_id: record.target_id, window_id: record.window_id };
|
|
255
|
+
}
|
|
256
|
+
await save(api, grant.recovery_id, key === 'close_tab' ? null : record);
|
|
257
|
+
return result;
|
|
258
|
+
} finally { clearTimeout(timer); }
|
|
259
|
+
});
|
|
260
|
+
}
|