@lobu/runtime-device 24.1.0 → 24.2.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 +26 -28
- package/npm-shrinkwrap.json +155 -172
- package/package.json +1 -1
- package/vendor/cli/dist/bundled-skills/lobu/SKILL.md +14 -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/apply/render.js +4 -14
- 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/context.d.ts +3 -1
- package/vendor/cli/dist/commands/context.js +13 -2
- package/vendor/cli/dist/commands/daemon.js +2 -152
- 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.js +6 -17
- package/vendor/cli/dist/commands/memory/run.js +4 -25
- 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 +5 -3
- 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/device-state.d.ts +2 -0
- package/vendor/cli/dist/internal/device-state.js +4 -0
- 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 +5 -1
- package/vendor/connector-sdk/package.json +1 -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/agent-turn.js +52 -17
- package/vendor/connector-worker/dist/daemon/automation.js +2 -12
- package/vendor/connector-worker/dist/daemon/client.d.ts +8 -2
- package/vendor/connector-worker/dist/daemon/client.js +8 -1
- package/vendor/connector-worker/dist/daemon/interactive-session.js +29 -6
- package/vendor/connector-worker/dist/daemon/poll-loop.js +23 -1
- package/vendor/connector-worker/dist/daemon/terminal-delivery.d.ts +5 -10
- package/vendor/connector-worker/dist/daemon/terminal-delivery.js +55 -23
- package/vendor/connector-worker/dist/daemon/terminal-failure.js +2 -2
- package/vendor/connector-worker/dist/executor/isolate.js +3 -0
- package/vendor/connector-worker/package.json +1 -1
- 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/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 +3 -0
- package/vendor/core/dist/contracts/tools/manage-connections.js +1 -1
- package/vendor/core/dist/contracts/tools/manage-entity-schema.js +1 -1
- package/vendor/core/dist/contracts/tools/manage-entity.d.ts +113 -34
- package/vendor/core/dist/contracts/tools/manage-entity.js +41 -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/reserved.d.ts +6 -3
- package/vendor/core/dist/reserved.js +6 -3
- package/vendor/core/dist/worker/auth.d.ts +3 -2
- package/vendor/core/package.json +6 -1
- 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/postgres/{.bun-tag-2f292c6554a8475e → .bun-tag-22d644c2873790db} +0 -0
|
@@ -0,0 +1,1155 @@
|
|
|
1
|
+
import { recoveryDeadline, runBrowserRecoveryTool, reapBrowserRecovery, RECOVERY_ALARM } from "./browser-recovery.js";
|
|
2
|
+
import { readSetupStatus } from "./setup-status.js";
|
|
3
|
+
// MV3 service worker.
|
|
4
|
+
//
|
|
5
|
+
// Lifecycle: pairing happens inside the sidepanel (sidepanel.js) — no
|
|
6
|
+
// separate pairing tab. On success the sidepanel writes
|
|
7
|
+
// {workerId, accessToken, refreshToken, clientId, clientSecret?} into
|
|
8
|
+
// chrome.storage.local; the onChanged listener here picks it up and
|
|
9
|
+
// starts polling /api/workers/poll.
|
|
10
|
+
//
|
|
11
|
+
// Run model: the extension is a generic dispatcher for the `chrome`
|
|
12
|
+
// connector. Runs arrive with one of:
|
|
13
|
+
// - feed_key='open_tabs' → batch the current tab list
|
|
14
|
+
// - action_key in TOOLS (navigate,
|
|
15
|
+
// get_accessibility_tree,
|
|
16
|
+
// click_ref, type_ref,
|
|
17
|
+
// wait_for_selector,
|
|
18
|
+
// screenshot, evaluate,
|
|
19
|
+
// close_tab, close_user_tabs, …) → run a single tool, return one batch
|
|
20
|
+
// with the observation envelope
|
|
21
|
+
// All other connector_keys are rejected — server-side connectors that
|
|
22
|
+
// compose on top of these tools run on the gateway, not here, and use
|
|
23
|
+
// the ctx.chrome.* SDK (PR 2) to schedule sub-runs of chrome.actions.*.
|
|
24
|
+
//
|
|
25
|
+
// Capabilities advertised on each poll are DEFAULT_CAPABILITIES plus any
|
|
26
|
+
// optional Chrome permissions the user has granted. The gateway
|
|
27
|
+
// re-authorizes the set against @lobu/core/capabilities.
|
|
28
|
+
|
|
29
|
+
import {
|
|
30
|
+
ambientContextForTab,
|
|
31
|
+
sendNativeAmbientContext,
|
|
32
|
+
} from "./ambient-context.js";
|
|
33
|
+
import {
|
|
34
|
+
RECONNECT_ESCALATE,
|
|
35
|
+
REFRESH_REFRESHED,
|
|
36
|
+
REFRESH_TRANSIENT,
|
|
37
|
+
reconnectEscalation,
|
|
38
|
+
refreshStoredPairing,
|
|
39
|
+
} from "./auth-refresh.js";
|
|
40
|
+
import { normalizePageUrl } from "./page-url.js";
|
|
41
|
+
import { browserRunOrigins } from "./browser-origin-policy.js";
|
|
42
|
+
import { controlSource, createControlActivity } from "./control-activity.js";
|
|
43
|
+
import { isDeviceAutomationRun } from "./automation-run.js";
|
|
44
|
+
import { installBridge } from "./bridge.js";
|
|
45
|
+
import { assertCompletionAccepted } from "./completion-response.js";
|
|
46
|
+
import {
|
|
47
|
+
DEFAULT_CAPABILITIES,
|
|
48
|
+
getGatewayUrl,
|
|
49
|
+
normalizeGatewayBase,
|
|
50
|
+
OPTIONAL_CAPABILITIES,
|
|
51
|
+
STORAGE_KEY_GATEWAY_URL,
|
|
52
|
+
serverBlocksExtension,
|
|
53
|
+
} from "./config.js";
|
|
54
|
+
import {
|
|
55
|
+
NATIVE_HANDSHAKE_TIMEOUT_MS,
|
|
56
|
+
NATIVE_HOST,
|
|
57
|
+
nativeBridgeHandshake,
|
|
58
|
+
nativePairStorage,
|
|
59
|
+
readNativePairResponse,
|
|
60
|
+
} from "./panel-session.js";
|
|
61
|
+
import { CONNECTOR_MANIFESTS } from "./connector-manifests.js";
|
|
62
|
+
import { registerExternalDetachHandler } from "./debugger-lease.js";
|
|
63
|
+
import { registerDialogHandler } from "./page-dialogs.js";
|
|
64
|
+
import { deliverFeedTransaction } from "./feed-transaction.js";
|
|
65
|
+
import { installFeedListenerTransport, feedNotifications, acceptFeedReceipts } from "./feed-listener.js";
|
|
66
|
+
import {
|
|
67
|
+
feed_bookmarks,
|
|
68
|
+
feed_downloads,
|
|
69
|
+
feed_history_visits,
|
|
70
|
+
feed_tab_events,
|
|
71
|
+
installFeedListeners,
|
|
72
|
+
} from "./feeds.js";
|
|
73
|
+
import {
|
|
74
|
+
HEARTBEAT_ALARM_PREFIX,
|
|
75
|
+
runIdFromAlarmName,
|
|
76
|
+
sendHeartbeat,
|
|
77
|
+
startHeartbeatAlarm,
|
|
78
|
+
stopHeartbeatAlarm,
|
|
79
|
+
} from "./heartbeat.js";
|
|
80
|
+
import { findExistingLensTab, normalizeLensTarget } from "./lens-target.js";
|
|
81
|
+
import {
|
|
82
|
+
installPageActivationListeners,
|
|
83
|
+
replacePageActivations,
|
|
84
|
+
} from "./page-activation.js";
|
|
85
|
+
import {
|
|
86
|
+
clearLostPairing,
|
|
87
|
+
handlePollAuthorizationFailure,
|
|
88
|
+
handlePollUpgradeRequired,
|
|
89
|
+
} from "./poll-auth.js";
|
|
90
|
+
import { pollFailureBackoffMs } from "./poll-backoff.js";
|
|
91
|
+
import { RunFence } from "./run-fence.js";
|
|
92
|
+
import {
|
|
93
|
+
installActionClickFallback,
|
|
94
|
+
installActionClickOpensPanel,
|
|
95
|
+
} from "./side-panel-action-click.js";
|
|
96
|
+
import { installTabLabelListeners } from "./tab-labels.js";
|
|
97
|
+
import {
|
|
98
|
+
feed_open_tabs,
|
|
99
|
+
handleNotificationClick,
|
|
100
|
+
SiteAccessRequiredError,
|
|
101
|
+
TOOLS,
|
|
102
|
+
} from "./tools.js";
|
|
103
|
+
import {
|
|
104
|
+
feed_watch_observations,
|
|
105
|
+
installWatchActivationListener,
|
|
106
|
+
isWatching,
|
|
107
|
+
recordWatchObservation,
|
|
108
|
+
startWatching,
|
|
109
|
+
stopWatching,
|
|
110
|
+
syncWatchState,
|
|
111
|
+
} from "./watch.js";
|
|
112
|
+
|
|
113
|
+
// connector_key → feed_key → implementation. Lookup table replaces the
|
|
114
|
+
// ad-hoc switch the dispatcher used before. Adding a new feed = one
|
|
115
|
+
// file in connectors/ + one row here.
|
|
116
|
+
const FEEDS = {
|
|
117
|
+
chrome: {
|
|
118
|
+
open_tabs: feed_open_tabs,
|
|
119
|
+
tab_events: feed_tab_events,
|
|
120
|
+
watch_observations: feed_watch_observations,
|
|
121
|
+
},
|
|
122
|
+
"chrome.history": { visits: feed_history_visits },
|
|
123
|
+
"chrome.bookmarks": { bookmarks: feed_bookmarks },
|
|
124
|
+
"chrome.downloads": { downloads: feed_downloads },
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const STORAGE_KEYS = {
|
|
128
|
+
workerId: "owletto.workerId",
|
|
129
|
+
accessToken: "owletto.accessToken",
|
|
130
|
+
refreshToken: "owletto.refreshToken",
|
|
131
|
+
clientId: "owletto.clientId",
|
|
132
|
+
clientSecret: "owletto.clientSecret",
|
|
133
|
+
pairedAt: "owletto.pairedAt",
|
|
134
|
+
// Last refresh ATTEMPT (success or transient failure). Throttles refreshes to
|
|
135
|
+
// at most one per REFRESH_BACKOFF_MS so a prolonged outage doesn't hammer the
|
|
136
|
+
// OAuth endpoints. Persisted so the throttle survives SW eviction.
|
|
137
|
+
lastRefreshAt: "owletto.lastRefreshAt",
|
|
138
|
+
// When the current "reconnecting" streak started (unresolved auth). Persisted
|
|
139
|
+
// so the escalation budget (reconnectEscalation) survives SW eviction; cleared
|
|
140
|
+
// on the next successful poll and on teardown.
|
|
141
|
+
reconnectingSince: "owletto.reconnectingSince",
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const RUN_TIMEOUT_MS = 90_000;
|
|
145
|
+
// Throttle the refresh grant: attempt it at most once per window. Inside the
|
|
146
|
+
// window a 401 skips re-refreshing and goes straight to the reconnecting path,
|
|
147
|
+
// so we don't churn refresh→401→refresh (which would also keep re-bootstrapping
|
|
148
|
+
// the side panel's iframe) or hammer the OAuth endpoints during an outage. The
|
|
149
|
+
// attempt timestamp (lastRefreshAt) is persisted so the throttle survives MV3
|
|
150
|
+
// service-worker eviction (a module var would reset on every wake). 60s is
|
|
151
|
+
// generous vs. the 24h token TTL.
|
|
152
|
+
const REFRESH_BACKOFF_MS = 60_000;
|
|
153
|
+
let pollTimer = null;
|
|
154
|
+
// Generation token for the self-scheduling poll loop. stopPolling() bumps it
|
|
155
|
+
// so an in-flight loop iteration that was awaiting a fetch when we stopped
|
|
156
|
+
// won't re-arm a stale timer; startPolling() claims a fresh generation.
|
|
157
|
+
let pollGen = 0;
|
|
158
|
+
// How long the gateway may hold an idle poll. Must not exceed the server's
|
|
159
|
+
// WORKER_POLL_MAX_WAIT_SECONDS (packages/core/src/contracts/worker/protocol.ts),
|
|
160
|
+
// which schema-rejects anything larger — a client literal above it would 400
|
|
161
|
+
// every poll and take this extension's dispatch offline entirely.
|
|
162
|
+
const WORKER_POLL_HOLD_SECONDS = 25;
|
|
163
|
+
// Consecutive polls that never reached the gateway. Drives the retry backoff
|
|
164
|
+
// (poll-backoff.js); reset by any poll the gateway actually answered.
|
|
165
|
+
let pollFailureStreak = 0;
|
|
166
|
+
// A held poll parks for up to WORKER_POLL_HOLD_SECONDS. A record observed
|
|
167
|
+
// mid-hold must not wait that out, so its notification rides a concurrent
|
|
168
|
+
// zero-wait request rather than aborting the held one — aborting races the
|
|
169
|
+
// gateway's claim commit and would strand a claimed run until the reaper.
|
|
170
|
+
let doorbellInFlight = false;
|
|
171
|
+
// In-flight guard: a poll that 401s does async refresh round-trips that can
|
|
172
|
+
// outlast the interval. Two overlapping polls both seeing 401 could each
|
|
173
|
+
// spend the (rotating) refresh token — the loser then fails and tears down a
|
|
174
|
+
// pairing the winner just healed. Skip a tick rather than overlap.
|
|
175
|
+
let polling = false;
|
|
176
|
+
let ambientPublishTimer = null;
|
|
177
|
+
|
|
178
|
+
async function publishAmbientContext() {
|
|
179
|
+
ambientPublishTimer = null;
|
|
180
|
+
const [tab, browserWindow] = await Promise.all([
|
|
181
|
+
chrome.tabs
|
|
182
|
+
.query({ active: true, lastFocusedWindow: true })
|
|
183
|
+
.then((tabs) => tabs[0] ?? null),
|
|
184
|
+
chrome.windows.getLastFocused().catch(() => null),
|
|
185
|
+
]);
|
|
186
|
+
const message = ambientContextForTab(tab, browserWindow?.focused === true);
|
|
187
|
+
// Context changes are event-driven and infrequent. A one-shot native
|
|
188
|
+
// message lets both the MV3 service worker and the Owletto bridge process
|
|
189
|
+
// return to idle after the write; a persistent connectNative port would
|
|
190
|
+
// keep both processes alive for the lifetime of Chrome.
|
|
191
|
+
await sendNativeAmbientContext(chrome.runtime, NATIVE_HOST, message);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function scheduleAmbientContextPublish() {
|
|
195
|
+
if (ambientPublishTimer != null) clearTimeout(ambientPublishTimer);
|
|
196
|
+
ambientPublishTimer = setTimeout(() => {
|
|
197
|
+
void publishAmbientContext().catch((error) =>
|
|
198
|
+
console.warn("[owletto] ambient context publish failed", error),
|
|
199
|
+
);
|
|
200
|
+
}, 100);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Worker health, surfaced to the side panel so failures aren't silent. The SW
|
|
204
|
+
// is the only place that sees poll/run outcomes; without this the panel just
|
|
205
|
+
// keeps showing the embedded app while the worker is dead. Written to
|
|
206
|
+
// chrome.storage.local; sidepanel.js reads it on load + via storage.onChanged.
|
|
207
|
+
const HEALTH_KEY = "owletto.health";
|
|
208
|
+
async function setHealth(patch) {
|
|
209
|
+
const prev = (await chrome.storage.local.get(HEALTH_KEY))[HEALTH_KEY] ?? {};
|
|
210
|
+
await chrome.storage.local.set({
|
|
211
|
+
[HEALTH_KEY]: { ...prev, ...patch, updatedAt: Date.now() },
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Installed next to a signed-in Lobu Mac app: pair through its bridge right
|
|
216
|
+
// away, so the browser is connected before the user first opens the panel (the
|
|
217
|
+
// panel can't be opened for them). The stored welcome flag makes that first
|
|
218
|
+
// panel open land on Settings. Fresh installs only; an existing pairing or no
|
|
219
|
+
// desktop app leaves the panel's own flow to run as before.
|
|
220
|
+
chrome.runtime.onInstalled.addListener(({ reason }) => {
|
|
221
|
+
if (reason !== "install") return;
|
|
222
|
+
void pairThroughDesktopApp().catch((error) =>
|
|
223
|
+
console.warn("[owletto] desktop pairing on install failed", error),
|
|
224
|
+
);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
async function pairThroughDesktopApp() {
|
|
228
|
+
const stored = await chrome.storage.local.get([
|
|
229
|
+
STORAGE_KEYS.accessToken,
|
|
230
|
+
STORAGE_KEYS.workerId,
|
|
231
|
+
]);
|
|
232
|
+
if (stored[STORAGE_KEYS.accessToken]) return;
|
|
233
|
+
const pair = readNativePairResponse(
|
|
234
|
+
await nativeBridgeHandshake({
|
|
235
|
+
connectNative: () => chrome.runtime.connectNative(NATIVE_HOST),
|
|
236
|
+
readWorkerId: async () => stored[STORAGE_KEYS.workerId],
|
|
237
|
+
timeoutMs: NATIVE_HANDSHAKE_TIMEOUT_MS,
|
|
238
|
+
}),
|
|
239
|
+
);
|
|
240
|
+
const gatewayBase = pair && normalizeGatewayBase(pair.gatewayUrl);
|
|
241
|
+
if (!gatewayBase) return;
|
|
242
|
+
// The access-token write fires storage.onChanged, which starts polling.
|
|
243
|
+
await chrome.storage.local.set(
|
|
244
|
+
nativePairStorage({
|
|
245
|
+
pair,
|
|
246
|
+
gatewayBase,
|
|
247
|
+
keys: STORAGE_KEYS,
|
|
248
|
+
gatewayKey: STORAGE_KEY_GATEWAY_URL,
|
|
249
|
+
}),
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
chrome.runtime.onStartup.addListener(() => {
|
|
254
|
+
void reapBrowserRecovery(chrome, TOOLS).catch(console.error);
|
|
255
|
+
startPolling();
|
|
256
|
+
scheduleAmbientContextPublish();
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
chrome.storage.onChanged.addListener((changes, area) => {
|
|
260
|
+
if (area !== "local") return;
|
|
261
|
+
if (changes[STORAGE_KEYS.accessToken]?.newValue) startPolling();
|
|
262
|
+
if (
|
|
263
|
+
changes[STORAGE_KEYS.accessToken] &&
|
|
264
|
+
!changes[STORAGE_KEYS.accessToken].newValue
|
|
265
|
+
) {
|
|
266
|
+
stopPolling();
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
// Toolbar icon → side panel. Must run on every SW start: Chrome persists the
|
|
271
|
+
// setting, but a fresh profile has never been told.
|
|
272
|
+
installActionClickOpensPanel(chrome);
|
|
273
|
+
installActionClickFallback(chrome);
|
|
274
|
+
|
|
275
|
+
installBridge();
|
|
276
|
+
installFeedListeners(chrome);
|
|
277
|
+
installWatchActivationListener(chrome);
|
|
278
|
+
installPageActivationListeners(chrome);
|
|
279
|
+
// MV3 may evict the SW between events; re-paint the toolbar icon and
|
|
280
|
+
// re-inject the tab observer to match the persisted watch flag on wake.
|
|
281
|
+
void syncWatchState(chrome).catch((err) =>
|
|
282
|
+
console.warn("[owletto] watch state sync failed", err),
|
|
283
|
+
);
|
|
284
|
+
// External debugger detach (user cancels the infobar, DevTools takes the tab)
|
|
285
|
+
// → reset the shared lease refcount + invalidate live capture sessions so a
|
|
286
|
+
// retrying start() reattaches instead of resuming a dead listener.
|
|
287
|
+
registerExternalDetachHandler(chrome);
|
|
288
|
+
registerDialogHandler(chrome);
|
|
289
|
+
// Re-install listeners when an optional permission is granted at runtime
|
|
290
|
+
// — the new chrome.* namespaces become available only after the grant.
|
|
291
|
+
chrome.permissions.onAdded.addListener(() => {
|
|
292
|
+
installFeedListeners(chrome);
|
|
293
|
+
triggerImmediatePoll();
|
|
294
|
+
});
|
|
295
|
+
chrome.permissions.onRemoved.addListener(() => {
|
|
296
|
+
triggerImmediatePoll();
|
|
297
|
+
});
|
|
298
|
+
if (chrome.notifications?.onClicked) {
|
|
299
|
+
chrome.notifications.onClicked.addListener((notificationId) => {
|
|
300
|
+
void handleNotificationClick(chrome, notificationId).catch((err) =>
|
|
301
|
+
console.warn("[owletto] notification click failed", err),
|
|
302
|
+
);
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Sidepanel ↔ SW control channel for the watch flag, plus the watch
|
|
307
|
+
// content-script's input/submit stream.
|
|
308
|
+
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
|
|
309
|
+
if (msg?.op === "setup-status-get" && msg.protocolVersion === 1) {
|
|
310
|
+
void readSetupStatus(chrome, sender)
|
|
311
|
+
.then((value) => sendResponse({ ok: true, value }))
|
|
312
|
+
.catch(() => sendResponse({ ok: false }));
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (typeof msg?.op === "string" && msg.op.startsWith("lens-")) {
|
|
317
|
+
void handleLensMessage(msg, sender)
|
|
318
|
+
.then((value) => sendResponse({ ok: true, value }))
|
|
319
|
+
.catch((error) =>
|
|
320
|
+
sendResponse({
|
|
321
|
+
ok: false,
|
|
322
|
+
error: error instanceof Error ? error.message : String(error),
|
|
323
|
+
}),
|
|
324
|
+
);
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
if (msg?.op === "watch-start") {
|
|
328
|
+
void startWatching(chrome).then(() => sendResponse({ ok: true }));
|
|
329
|
+
return true; // async
|
|
330
|
+
}
|
|
331
|
+
if (msg?.op === "watch-stop") {
|
|
332
|
+
void stopWatching(chrome).then(() => sendResponse({ ok: true }));
|
|
333
|
+
return true;
|
|
334
|
+
}
|
|
335
|
+
if (msg?.op === "watch-input" || msg?.op === "watch-submit") {
|
|
336
|
+
void recordWatchObservation(msg);
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
if (msg?.op === "watch-status") {
|
|
340
|
+
void isWatching().then((on) => sendResponse({ watching: on }));
|
|
341
|
+
return true;
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
function lensContextForTab(tab) {
|
|
346
|
+
return {
|
|
347
|
+
kind: "browser",
|
|
348
|
+
url: typeof tab?.url === "string" ? tab.url : null,
|
|
349
|
+
title: typeof tab?.title === "string" ? tab.title : null,
|
|
350
|
+
applicationName: "Chrome",
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
async function activeLensContext() {
|
|
355
|
+
const [tab] = await chrome.tabs.query({
|
|
356
|
+
active: true,
|
|
357
|
+
lastFocusedWindow: true,
|
|
358
|
+
});
|
|
359
|
+
return lensContextForTab(tab);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
async function openLensTarget(value) {
|
|
363
|
+
const target = normalizeLensTarget(value);
|
|
364
|
+
if (!target) throw new Error("Lens only opens HTTP or HTTPS pages.");
|
|
365
|
+
const tabs = await chrome.tabs.query({});
|
|
366
|
+
const existing = findExistingLensTab(tabs, target);
|
|
367
|
+
if (existing?.id != null) {
|
|
368
|
+
if (existing.windowId != null) {
|
|
369
|
+
await chrome.windows.update(existing.windowId, { focused: true });
|
|
370
|
+
}
|
|
371
|
+
return chrome.tabs.update(existing.id, { active: true });
|
|
372
|
+
}
|
|
373
|
+
return chrome.tabs.create({ url: value, active: true });
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
async function handleLensMessage(msg, sender) {
|
|
377
|
+
if (msg.op === "lens-debugger-attached") {
|
|
378
|
+
const targets = await chrome.debugger.getTargets();
|
|
379
|
+
return targets.some((target) => target.attached);
|
|
380
|
+
}
|
|
381
|
+
if (msg.op === "lens-context-get") return activeLensContext();
|
|
382
|
+
if (msg.op === "lens-open-target") return openLensTarget(msg.url);
|
|
383
|
+
if (msg.op === "lens-focus-host") {
|
|
384
|
+
const tabId = sender?.tab?.id;
|
|
385
|
+
const windowId = sender?.tab?.windowId;
|
|
386
|
+
if (
|
|
387
|
+
tabId == null ||
|
|
388
|
+
!/^https:\/\/([^.]+\.)?lobu\.ai\//.test(sender.tab.url ?? "")
|
|
389
|
+
) {
|
|
390
|
+
throw new Error("Only a Lobu tab can own Lens.");
|
|
391
|
+
}
|
|
392
|
+
if (windowId != null) {
|
|
393
|
+
await chrome.windows.update(windowId, { focused: true });
|
|
394
|
+
}
|
|
395
|
+
return chrome.tabs.update(tabId, { active: true });
|
|
396
|
+
}
|
|
397
|
+
throw new Error("Unsupported Lens operation.");
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
async function notifyLensHosts() {
|
|
401
|
+
const [hosts, context] = await Promise.all([
|
|
402
|
+
chrome.tabs.query({
|
|
403
|
+
url: ["https://lobu.ai/*", "https://*.lobu.ai/*"],
|
|
404
|
+
}),
|
|
405
|
+
activeLensContext(),
|
|
406
|
+
]);
|
|
407
|
+
await Promise.allSettled(
|
|
408
|
+
hosts.map((tab) =>
|
|
409
|
+
tab.id == null
|
|
410
|
+
? null
|
|
411
|
+
: chrome.tabs.sendMessage(tab.id, {
|
|
412
|
+
op: "lens-context-changed",
|
|
413
|
+
context,
|
|
414
|
+
}),
|
|
415
|
+
),
|
|
416
|
+
);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
chrome.tabs.onActivated.addListener(() => {
|
|
420
|
+
void notifyLensHosts();
|
|
421
|
+
scheduleAmbientContextPublish();
|
|
422
|
+
});
|
|
423
|
+
chrome.tabs.onUpdated.addListener((_tabId, changeInfo) => {
|
|
424
|
+
if (changeInfo.status === "complete" || changeInfo.url) {
|
|
425
|
+
void notifyLensHosts();
|
|
426
|
+
scheduleAmbientContextPublish();
|
|
427
|
+
}
|
|
428
|
+
});
|
|
429
|
+
chrome.windows.onFocusChanged.addListener(() => {
|
|
430
|
+
void notifyLensHosts();
|
|
431
|
+
scheduleAmbientContextPublish();
|
|
432
|
+
});
|
|
433
|
+
installTabLabelListeners(chrome);
|
|
434
|
+
|
|
435
|
+
startPolling();
|
|
436
|
+
scheduleAmbientContextPublish();
|
|
437
|
+
installFeedListenerTransport(chrome, triggerImmediatePoll);
|
|
438
|
+
|
|
439
|
+
function startPolling() {
|
|
440
|
+
if (pollTimer) return;
|
|
441
|
+
pollFailureStreak = 0;
|
|
442
|
+
const gen = ++pollGen;
|
|
443
|
+
runPollLoop(0, gen);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function stopPolling() {
|
|
447
|
+
pollGen++; // invalidate any in-flight loop iteration
|
|
448
|
+
if (pollTimer) {
|
|
449
|
+
clearTimeout(pollTimer);
|
|
450
|
+
pollTimer = null;
|
|
451
|
+
}
|
|
452
|
+
pollFailureStreak = 0;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// A page observation (or a tab change) must reach the gateway now, not when the
|
|
456
|
+
// current hold expires. If the loop is parked in a held poll, ring the doorbell
|
|
457
|
+
// on its own zero-wait request; the held poll keeps waiting for work.
|
|
458
|
+
function triggerImmediatePoll() {
|
|
459
|
+
if (polling) {
|
|
460
|
+
void pollDoorbell();
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
stopPolling();
|
|
464
|
+
startPolling();
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// Self-scheduling poll loop. Each iteration polls once — the gateway holds that
|
|
468
|
+
// request open while it has nothing to hand over — then re-arms at whatever
|
|
469
|
+
// budget the gateway reports unspent, so a healthy worker is paced by the
|
|
470
|
+
// server and never by a local guess. Generation-gated so a stopPolling()
|
|
471
|
+
// during an awaited fetch doesn't re-arm a stale timer.
|
|
472
|
+
function runPollLoop(delay, gen) {
|
|
473
|
+
pollTimer = setTimeout(async () => {
|
|
474
|
+
if (gen !== pollGen) return;
|
|
475
|
+
pollTimer = null;
|
|
476
|
+
const result = await pollOnce(WORKER_POLL_HOLD_SECONDS);
|
|
477
|
+
if (gen !== pollGen) return; // stopped while the poll was in flight
|
|
478
|
+
if (!result.keepGoing) return; // creds gone / auth lost — onChanged re-arms
|
|
479
|
+
let delayMs;
|
|
480
|
+
if (result.nextDelayMs === undefined) {
|
|
481
|
+
// The gateway never answered (network, 5xx, auth still settling).
|
|
482
|
+
delayMs = pollFailureBackoffMs(++pollFailureStreak);
|
|
483
|
+
} else {
|
|
484
|
+
pollFailureStreak = 0;
|
|
485
|
+
delayMs = result.nextDelayMs;
|
|
486
|
+
}
|
|
487
|
+
if (gen !== pollGen) return;
|
|
488
|
+
runPollLoop(delayMs, gen);
|
|
489
|
+
}, delay);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// Out-of-band notification poll. Never re-arms the loop and never holds: it
|
|
493
|
+
// exists purely to deliver feed notifications the moment the page produces one.
|
|
494
|
+
async function pollDoorbell() {
|
|
495
|
+
if (doorbellInFlight) return;
|
|
496
|
+
doorbellInFlight = true;
|
|
497
|
+
try {
|
|
498
|
+
await pollOnceInner(0);
|
|
499
|
+
} catch (err) {
|
|
500
|
+
console.warn("[owletto] doorbell poll failed", err);
|
|
501
|
+
} finally {
|
|
502
|
+
doorbellInFlight = false;
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// Returns { claimed, keepGoing, skipped }:
|
|
507
|
+
// keepGoing=false → don't re-arm (missing creds / auth lost; a token-set
|
|
508
|
+
// storage change re-arms via startPolling).
|
|
509
|
+
// skipped=true → this tick overlapped an in-flight poll.
|
|
510
|
+
// nextDelayMs → the gateway's own pacing; absent when it never answered,
|
|
511
|
+
// which is what puts the loop on the retry backoff.
|
|
512
|
+
async function pollOnce(waitSeconds) {
|
|
513
|
+
if (polling) return { claimed: false, keepGoing: true, skipped: true };
|
|
514
|
+
polling = true;
|
|
515
|
+
try {
|
|
516
|
+
return await pollOnceInner(waitSeconds);
|
|
517
|
+
} catch (err) {
|
|
518
|
+
console.warn("[owletto] poll iteration failed", err);
|
|
519
|
+
await setHealth({
|
|
520
|
+
connected: false,
|
|
521
|
+
reason: "poll-failed",
|
|
522
|
+
message: `Browser connection failed: ${err instanceof Error ? err.message : String(err)}. Retrying…`,
|
|
523
|
+
}).catch((error) => console.warn("[owletto] health update failed", error));
|
|
524
|
+
return { claimed: false, keepGoing: true };
|
|
525
|
+
} finally {
|
|
526
|
+
polling = false;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
async function pollOnceInner(waitSeconds) {
|
|
531
|
+
const gatewayUrl = await getGatewayUrl();
|
|
532
|
+
const stored = await chrome.storage.local.get([
|
|
533
|
+
STORAGE_KEYS.workerId,
|
|
534
|
+
STORAGE_KEYS.accessToken,
|
|
535
|
+
STORAGE_KEYS.reconnectingSince,
|
|
536
|
+
]);
|
|
537
|
+
const workerId = stored[STORAGE_KEYS.workerId];
|
|
538
|
+
const token = stored[STORAGE_KEYS.accessToken];
|
|
539
|
+
if (!gatewayUrl || !workerId || !token) {
|
|
540
|
+
return { claimed: false, keepGoing: false };
|
|
541
|
+
}
|
|
542
|
+
const host = new URL(gatewayUrl).host;
|
|
543
|
+
await controlActivity.refresh();
|
|
544
|
+
const capabilities = await computeAdvertisedCapabilities();
|
|
545
|
+
const connectorManifests = CONNECTOR_MANIFESTS;
|
|
546
|
+
try {
|
|
547
|
+
const pollStartedAt = performance.now();
|
|
548
|
+
const res = await fetch(`${gatewayUrl}/api/workers/poll`, {
|
|
549
|
+
// Must outlive the hold, or every held poll aborts client-side at 20s
|
|
550
|
+
// and the worker looks dead to itself while the gateway still waits.
|
|
551
|
+
signal: AbortSignal.timeout(WORKER_POLL_HOLD_SECONDS * 1000 + 10_000),
|
|
552
|
+
method: "POST",
|
|
553
|
+
headers: {
|
|
554
|
+
"content-type": "application/json",
|
|
555
|
+
authorization: `Bearer ${token}`,
|
|
556
|
+
},
|
|
557
|
+
body: JSON.stringify({
|
|
558
|
+
worker_id: workerId,
|
|
559
|
+
platform: "chrome-extension",
|
|
560
|
+
app_version: chrome.runtime.getManifest().version,
|
|
561
|
+
capabilities,
|
|
562
|
+
connector_manifests: connectorManifests,
|
|
563
|
+
feed_notifications: await feedNotifications(),
|
|
564
|
+
wait_seconds: waitSeconds,
|
|
565
|
+
}),
|
|
566
|
+
});
|
|
567
|
+
if (res.status === 401) {
|
|
568
|
+
// Access tokens are capped server-side (24h). Before giving up — which
|
|
569
|
+
// forces a manual re-pair — try the OAuth refresh grant. Only OAuth-paired
|
|
570
|
+
// installs can: native auto-pair / local-init store no refresh token and
|
|
571
|
+
// fall straight through to re-pair. lastRefreshAt throttles attempts to at
|
|
572
|
+
// most one per REFRESH_BACKOFF_MS so a prolonged outage doesn't hammer the
|
|
573
|
+
// OAuth endpoints (and avoids a synchronized retry storm on recovery).
|
|
574
|
+
const lastRefreshAt =
|
|
575
|
+
(await chrome.storage.local.get(STORAGE_KEYS.lastRefreshAt))[
|
|
576
|
+
STORAGE_KEYS.lastRefreshAt
|
|
577
|
+
] ?? 0;
|
|
578
|
+
const refreshedRecently = Date.now() - lastRefreshAt < REFRESH_BACKOFF_MS;
|
|
579
|
+
if (!refreshedRecently) {
|
|
580
|
+
// Stamp the attempt up front so the throttle holds even if the refresh
|
|
581
|
+
// fails or the SW is evicted mid-flight.
|
|
582
|
+
await chrome.storage.local.set({
|
|
583
|
+
[STORAGE_KEYS.lastRefreshAt]: Date.now(),
|
|
584
|
+
});
|
|
585
|
+
const outcome = await tryRefreshToken(gatewayUrl);
|
|
586
|
+
if (outcome === REFRESH_REFRESHED) {
|
|
587
|
+
await setHealth({
|
|
588
|
+
connected: true,
|
|
589
|
+
reason: "ok",
|
|
590
|
+
host,
|
|
591
|
+
message: "",
|
|
592
|
+
lastSyncOkAt: Date.now(),
|
|
593
|
+
});
|
|
594
|
+
// next tick re-reads storage and polls with the new token
|
|
595
|
+
return { claimed: false, keepGoing: true };
|
|
596
|
+
}
|
|
597
|
+
if (outcome === REFRESH_TRANSIENT) {
|
|
598
|
+
// The refresh didn't happen — the gateway was unreachable or erroring
|
|
599
|
+
// (5xx/429/network), NOT a refused grant. Keep the credentials and
|
|
600
|
+
// retry; this is what permanently disconnected the extension after a
|
|
601
|
+
// transient outage. Escalate only after a sustained failure.
|
|
602
|
+
return await handleReconnecting(
|
|
603
|
+
host,
|
|
604
|
+
`Can't reach ${host} to refresh sign-in. Retrying…`,
|
|
605
|
+
);
|
|
606
|
+
}
|
|
607
|
+
// outcome === REFRESH_REJECTED → grant definitively refused. Re-pair.
|
|
608
|
+
return await tearDownPairing(host);
|
|
609
|
+
}
|
|
610
|
+
// Attempted a refresh within the last REFRESH_BACKOFF_MS but still 401 —
|
|
611
|
+
// a freshly-issued token being rejected, or repeated transient failures.
|
|
612
|
+
// Same bounded path: retry now, escalate if it persists.
|
|
613
|
+
return await handleReconnecting(
|
|
614
|
+
host,
|
|
615
|
+
`${host} is rejecting sign-in. Retrying…`,
|
|
616
|
+
);
|
|
617
|
+
}
|
|
618
|
+
const upgradeRequired = await handlePollUpgradeRequired(res, {
|
|
619
|
+
host,
|
|
620
|
+
setHealth,
|
|
621
|
+
});
|
|
622
|
+
if (upgradeRequired) return upgradeRequired;
|
|
623
|
+
const authorizationFailure = await handlePollAuthorizationFailure(res, {
|
|
624
|
+
host,
|
|
625
|
+
setHealth,
|
|
626
|
+
removeStorage: (keys) => chrome.storage.local.remove(keys),
|
|
627
|
+
stopPolling,
|
|
628
|
+
});
|
|
629
|
+
if (authorizationFailure) return authorizationFailure;
|
|
630
|
+
if (!res.ok) {
|
|
631
|
+
await setHealth({
|
|
632
|
+
connected: false,
|
|
633
|
+
reason: "poll-failed",
|
|
634
|
+
host,
|
|
635
|
+
message: `${host} returned ${res.status} on poll.`,
|
|
636
|
+
});
|
|
637
|
+
return { claimed: false, keepGoing: true };
|
|
638
|
+
}
|
|
639
|
+
const body = await res.json();
|
|
640
|
+
const deadline = recoveryDeadline(body?.action_input?.browser_recovery_control, pollStartedAt, performance.now());
|
|
641
|
+
if (Array.isArray(body?.feed_notification_receipts)) {
|
|
642
|
+
try {
|
|
643
|
+
await acceptFeedReceipts(chrome, body.feed_notification_receipts);
|
|
644
|
+
} catch (error) {
|
|
645
|
+
// The server may have claimed a run in this same response. Durable
|
|
646
|
+
// receipt cleanup can retry on the next poll; never strand that claimed
|
|
647
|
+
// run locally because IndexedDB was transiently unavailable.
|
|
648
|
+
console.warn("[owletto] feed receipt processing failed", error);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
await setHealth({
|
|
652
|
+
connected: true,
|
|
653
|
+
reason: "ok",
|
|
654
|
+
host,
|
|
655
|
+
message: "",
|
|
656
|
+
lastSyncOkAt: Date.now(),
|
|
657
|
+
});
|
|
658
|
+
// A clean poll means auth is good again — end any reconnecting streak so a
|
|
659
|
+
// later, unrelated blip starts its escalation budget from zero. Uses the
|
|
660
|
+
// value already batched into the get above, so the happy path adds no extra
|
|
661
|
+
// round-trip and only writes when there's actually a streak to clear.
|
|
662
|
+
if (stored[STORAGE_KEYS.reconnectingSince]) {
|
|
663
|
+
await chrome.storage.local.remove(STORAGE_KEYS.reconnectingSince);
|
|
664
|
+
}
|
|
665
|
+
if (Array.isArray(body?.page_activations)) {
|
|
666
|
+
await replacePageActivations(chrome, body.page_activations);
|
|
667
|
+
}
|
|
668
|
+
const nextDelayMs = Math.max(0, (body?.next_poll_seconds ?? 0) * 1000);
|
|
669
|
+
if (body?.run_id) {
|
|
670
|
+
void executeRun(body, workerId, token, gatewayUrl, deadline).catch((error) =>
|
|
671
|
+
console.error("[owletto] run cleanup failed", error),
|
|
672
|
+
);
|
|
673
|
+
return { claimed: true, keepGoing: true, nextDelayMs };
|
|
674
|
+
}
|
|
675
|
+
return { claimed: false, keepGoing: true, nextDelayMs };
|
|
676
|
+
} catch (err) {
|
|
677
|
+
console.warn("[owletto] poll failed", err);
|
|
678
|
+
// A server that refuses this extension's origin may need a site grant.
|
|
679
|
+
// The SW cannot request it without a user gesture, so offer it in the panel.
|
|
680
|
+
if (await serverBlocksExtension(gatewayUrl)) {
|
|
681
|
+
chrome.action.setBadgeText({ text: "!" });
|
|
682
|
+
chrome.action.setBadgeBackgroundColor({ color: "#d93025" });
|
|
683
|
+
chrome.action.setTitle({
|
|
684
|
+
title: `Lobu needs permission to reach ${host}. Click to grant.`,
|
|
685
|
+
});
|
|
686
|
+
await setHealth({
|
|
687
|
+
connected: false,
|
|
688
|
+
reason: "needs-permission",
|
|
689
|
+
host,
|
|
690
|
+
message: `${host} is reachable but isn't allowing this extension to connect. Try granting access.`,
|
|
691
|
+
});
|
|
692
|
+
return { claimed: false, keepGoing: true };
|
|
693
|
+
}
|
|
694
|
+
await setHealth({
|
|
695
|
+
connected: false,
|
|
696
|
+
reason: "poll-failed",
|
|
697
|
+
host,
|
|
698
|
+
message: `Can't reach ${host}: ${err instanceof Error ? err.message : String(err)}`,
|
|
699
|
+
});
|
|
700
|
+
return { claimed: false, keepGoing: true };
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
// A 401 we couldn't resolve right now: the gateway's OAuth route is transiently
|
|
705
|
+
// down, or a freshly-issued token is still being rejected. Keep the credentials
|
|
706
|
+
// and retry — but don't strand the user forever. If we've been unable to
|
|
707
|
+
// re-establish auth for RECONNECT_ESCALATE_AFTER_MS, escalate to a teardown so
|
|
708
|
+
// the panel surfaces the re-pair CTA. The streak start is persisted (survives SW
|
|
709
|
+
// eviction) and cleared by the next successful poll.
|
|
710
|
+
//
|
|
711
|
+
// Caveat on recovery latency: while reconnecting, the idle poll backs off toward
|
|
712
|
+
// MAX_POLL_INTERVAL_MS and MV3 may evict the SW between polls. Nothing re-arms
|
|
713
|
+
// the loop except the 1-min tab-reaper alarm (which re-runs top-level
|
|
714
|
+
// startPolling), so after the gateway recovers reconnection can lag up to
|
|
715
|
+
// ~1 min — it does recover without a re-pair, just not instantly.
|
|
716
|
+
async function handleReconnecting(host, message) {
|
|
717
|
+
const since =
|
|
718
|
+
(await chrome.storage.local.get(STORAGE_KEYS.reconnectingSince))[
|
|
719
|
+
STORAGE_KEYS.reconnectingSince
|
|
720
|
+
] ?? null;
|
|
721
|
+
const { action, since: start } = reconnectEscalation(since, Date.now());
|
|
722
|
+
if (action === RECONNECT_ESCALATE) {
|
|
723
|
+
return await tearDownPairing(host);
|
|
724
|
+
}
|
|
725
|
+
if (since == null) {
|
|
726
|
+
await chrome.storage.local.set({ [STORAGE_KEYS.reconnectingSince]: start });
|
|
727
|
+
}
|
|
728
|
+
await setHealth({ connected: false, reason: "reconnecting", host, message });
|
|
729
|
+
return { claimed: false, keepGoing: true };
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
// Definitive auth loss: the refresh grant was refused, there's nothing to
|
|
733
|
+
// refresh with, or reconnecting exceeded its budget. Drop the credentials (so
|
|
734
|
+
// pollOnce stops the poll loop) and surface the re-pair CTA.
|
|
735
|
+
// Removing the access token also fires storage.onChanged → stopPolling. The
|
|
736
|
+
// worker_id is intentionally KEPT — it's the stable device identity the Mac
|
|
737
|
+
// bridge forwards on the next native re-pair so mint-child-token reuses it
|
|
738
|
+
// instead of minting a fresh uuid (which would orphan this device_workers row
|
|
739
|
+
// and pile up duplicate "chrome" entries). A bare worker_id with no token
|
|
740
|
+
// can't poll (pollOnce gates on accessToken && workerId), so keeping it
|
|
741
|
+
// is inert until the re-pair supplies fresh credentials. Returns the poll
|
|
742
|
+
// result so callers can `return await` it.
|
|
743
|
+
async function tearDownPairing(host) {
|
|
744
|
+
return clearLostPairing({
|
|
745
|
+
host,
|
|
746
|
+
setHealth,
|
|
747
|
+
removeStorage: (keys) => chrome.storage.local.remove(keys),
|
|
748
|
+
stopPolling,
|
|
749
|
+
});
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
// Device-run dispatcher. The extension implements only its own reserved
|
|
753
|
+
// `chrome.*` connectors: the generic browser tools plus the feed table above.
|
|
754
|
+
// A connector that needs its own code ships that code as an ordinary connector
|
|
755
|
+
// and drives the page through those generic tools — see
|
|
756
|
+
// connector-ownership.test.js.
|
|
757
|
+
//
|
|
758
|
+
// Device Automation runs (`run_type: "automation"`) are also claimable by any
|
|
759
|
+
// user-scoped device worker when pinned — including this extension. Chrome
|
|
760
|
+
// has no local CLI executor (that's Mac `AutomationDispatcher`). Fail closed via
|
|
761
|
+
// `/complete-automation` so we never finalize an Automation through feed
|
|
762
|
+
// `/complete` (wrong lifecycle / exit metadata).
|
|
763
|
+
const controlActivity = createControlActivity(chrome);
|
|
764
|
+
void controlActivity.refresh().catch((error) => console.warn("[lobu] activity reset failed", error));
|
|
765
|
+
|
|
766
|
+
async function executeRun(run, workerId, token, gatewayUrl, deadline) {
|
|
767
|
+
const { run_id, connector_key, feed_key, action_key, run_type } = run;
|
|
768
|
+
console.log("[owletto] claimed run", {
|
|
769
|
+
run_id,
|
|
770
|
+
run_type,
|
|
771
|
+
connector_key,
|
|
772
|
+
feed_key,
|
|
773
|
+
action_key,
|
|
774
|
+
});
|
|
775
|
+
|
|
776
|
+
if (isDeviceAutomationRun(run)) {
|
|
777
|
+
const message =
|
|
778
|
+
"Owletto for Chrome cannot execute device Automations. Pin the Automation to a Mac with the Owletto menubar app (local CLI agent).";
|
|
779
|
+
console.warn("[owletto] rejecting device Automation run on chrome worker", {
|
|
780
|
+
run_id,
|
|
781
|
+
run_type,
|
|
782
|
+
});
|
|
783
|
+
try {
|
|
784
|
+
await postJson(
|
|
785
|
+
`${gatewayUrl}/api/workers/me/runs/${run_id}/complete-automation`,
|
|
786
|
+
token,
|
|
787
|
+
{
|
|
788
|
+
worker_id: workerId,
|
|
789
|
+
output: "",
|
|
790
|
+
error: message,
|
|
791
|
+
duration_ms: 0,
|
|
792
|
+
exit_code: null,
|
|
793
|
+
exit_signal: null,
|
|
794
|
+
exit_reason: "error_message",
|
|
795
|
+
},
|
|
796
|
+
);
|
|
797
|
+
} catch (err) {
|
|
798
|
+
console.warn("[owletto] complete-automation reject failed", err);
|
|
799
|
+
}
|
|
800
|
+
await setHealth({
|
|
801
|
+
lastRunError: {
|
|
802
|
+
message,
|
|
803
|
+
label: "automation",
|
|
804
|
+
at: Date.now(),
|
|
805
|
+
},
|
|
806
|
+
lastRun: {
|
|
807
|
+
runId: run_id,
|
|
808
|
+
label: "automation",
|
|
809
|
+
status: "failed",
|
|
810
|
+
at: Date.now(),
|
|
811
|
+
},
|
|
812
|
+
});
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
// Heartbeat the run every 30s while it's in flight. The connector-worker
|
|
817
|
+
// daemon already does this from executeSyncRun / executeAuthRun and now
|
|
818
|
+
// also from executeActionRun + executeEmbedBackfillRun. The browser
|
|
819
|
+
// worker uses chrome.alarms (not setInterval) because MV3 service
|
|
820
|
+
// workers can be torn down between events; an alarm survives the
|
|
821
|
+
// service-worker sleep and re-fires when Chrome wakes it. The handler
|
|
822
|
+
// (registered once below) drives the actual POST.
|
|
823
|
+
startHeartbeatAlarm(chrome, run_id);
|
|
824
|
+
|
|
825
|
+
let safeComplete;
|
|
826
|
+
let completionPromise = null;
|
|
827
|
+
const fence = new RunFence({
|
|
828
|
+
timeoutMs: RUN_TIMEOUT_MS,
|
|
829
|
+
onTimeout: () => {
|
|
830
|
+
void (async () => {
|
|
831
|
+
// An in-flight success completion is aborted by the fence. Wait for
|
|
832
|
+
// its ambiguity path to settle, then make the timeout completion the
|
|
833
|
+
// only remaining terminal attempt.
|
|
834
|
+
await completionPromise?.catch(() => false);
|
|
835
|
+
await safeComplete?.({
|
|
836
|
+
status: "failed",
|
|
837
|
+
error_message: `run timed out after ${RUN_TIMEOUT_MS}ms`,
|
|
838
|
+
});
|
|
839
|
+
})();
|
|
840
|
+
},
|
|
841
|
+
});
|
|
842
|
+
const postBatch = async (items) => {
|
|
843
|
+
fence.assertActive("batch stream");
|
|
844
|
+
const result = await postJson(
|
|
845
|
+
`${gatewayUrl}/api/workers/stream`,
|
|
846
|
+
token,
|
|
847
|
+
{
|
|
848
|
+
type: "batch",
|
|
849
|
+
run_id,
|
|
850
|
+
worker_id: workerId,
|
|
851
|
+
items,
|
|
852
|
+
},
|
|
853
|
+
{ signal: fence.signal },
|
|
854
|
+
);
|
|
855
|
+
fence.assertActive("batch stream response");
|
|
856
|
+
return result;
|
|
857
|
+
};
|
|
858
|
+
// safeComplete: at-most-once POST per run. Routes to the right
|
|
859
|
+
// endpoint based on run type — feed runs use /complete (which
|
|
860
|
+
// expects items_collected etc.), action runs use /complete-action
|
|
861
|
+
// (which persists action_output). The two endpoints have different
|
|
862
|
+
// payload shapes; we pick on the connector_key + presence of
|
|
863
|
+
// action_key.
|
|
864
|
+
const isActionRun = action_key != null;
|
|
865
|
+
const completeEndpoint = isActionRun
|
|
866
|
+
? `${gatewayUrl}/api/workers/complete-action`
|
|
867
|
+
: `${gatewayUrl}/api/workers/complete`;
|
|
868
|
+
safeComplete = async (payload) => {
|
|
869
|
+
const intent =
|
|
870
|
+
payload.status === "success"
|
|
871
|
+
? "success"
|
|
872
|
+
: fence.state === "timed_out"
|
|
873
|
+
? "timeout"
|
|
874
|
+
: "failure";
|
|
875
|
+
if (completionPromise) {
|
|
876
|
+
const result = await completionPromise;
|
|
877
|
+
return intent === "success" && fence.isSuccessConfirmed()
|
|
878
|
+
? result
|
|
879
|
+
: false;
|
|
880
|
+
}
|
|
881
|
+
const completionToken = fence.beginCompletion(intent);
|
|
882
|
+
if (!completionToken) return false;
|
|
883
|
+
completionPromise = (async () => {
|
|
884
|
+
const runLabel = action_key ?? feed_key ?? connector_key;
|
|
885
|
+
try {
|
|
886
|
+
// Stop heartbeating before the completion POST so the gateway sees
|
|
887
|
+
// the terminal status as the last write on the row.
|
|
888
|
+
await stopHeartbeatAlarm(chrome, run_id);
|
|
889
|
+
const completionAbort = new AbortController();
|
|
890
|
+
const abortFromFence = () => completionAbort.abort(fence.signal.reason);
|
|
891
|
+
if (intent === "success") {
|
|
892
|
+
if (fence.signal.aborted) abortFromFence();
|
|
893
|
+
else
|
|
894
|
+
fence.signal.addEventListener("abort", abortFromFence, {
|
|
895
|
+
once: true,
|
|
896
|
+
});
|
|
897
|
+
}
|
|
898
|
+
const completionTimer = setTimeout(
|
|
899
|
+
() =>
|
|
900
|
+
completionAbort.abort(
|
|
901
|
+
new DOMException("Completion timed out", "TimeoutError"),
|
|
902
|
+
),
|
|
903
|
+
10_000,
|
|
904
|
+
);
|
|
905
|
+
try {
|
|
906
|
+
const completionResponse = await postJson(
|
|
907
|
+
completeEndpoint,
|
|
908
|
+
token,
|
|
909
|
+
{
|
|
910
|
+
run_id,
|
|
911
|
+
worker_id: workerId,
|
|
912
|
+
...payload,
|
|
913
|
+
},
|
|
914
|
+
{ signal: completionAbort.signal },
|
|
915
|
+
);
|
|
916
|
+
assertCompletionAccepted(completionResponse);
|
|
917
|
+
} finally {
|
|
918
|
+
clearTimeout(completionTimer);
|
|
919
|
+
fence.signal.removeEventListener("abort", abortFromFence);
|
|
920
|
+
}
|
|
921
|
+
if (!fence.confirmCompletion(completionToken, intent)) return false;
|
|
922
|
+
} catch (err) {
|
|
923
|
+
fence.failCompletion(completionToken, intent);
|
|
924
|
+
console.warn(`[owletto] ${completeEndpoint} POST failed`, err);
|
|
925
|
+
try {
|
|
926
|
+
await setHealth({
|
|
927
|
+
lastRunError: {
|
|
928
|
+
message: `Run completion could not be confirmed: ${err instanceof Error ? err.message : String(err)}`,
|
|
929
|
+
label: runLabel,
|
|
930
|
+
at: Date.now(),
|
|
931
|
+
},
|
|
932
|
+
});
|
|
933
|
+
} catch {}
|
|
934
|
+
return false;
|
|
935
|
+
} finally {
|
|
936
|
+
if (!fence.completionConfirmed) completionPromise = null;
|
|
937
|
+
}
|
|
938
|
+
try {
|
|
939
|
+
if (payload.status === "failed") {
|
|
940
|
+
await setHealth({
|
|
941
|
+
lastRunError: {
|
|
942
|
+
message: payload.error_message ?? "Task failed.",
|
|
943
|
+
label: runLabel,
|
|
944
|
+
at: Date.now(),
|
|
945
|
+
},
|
|
946
|
+
lastRun: {
|
|
947
|
+
runId: run_id,
|
|
948
|
+
label: runLabel,
|
|
949
|
+
status: "failed",
|
|
950
|
+
at: Date.now(),
|
|
951
|
+
},
|
|
952
|
+
});
|
|
953
|
+
} else if (payload.status === "success") {
|
|
954
|
+
await setHealth({
|
|
955
|
+
lastRunError: null,
|
|
956
|
+
lastRun: {
|
|
957
|
+
runId: run_id,
|
|
958
|
+
label: runLabel,
|
|
959
|
+
status: "success",
|
|
960
|
+
at: Date.now(),
|
|
961
|
+
},
|
|
962
|
+
});
|
|
963
|
+
}
|
|
964
|
+
} catch (err) {
|
|
965
|
+
console.warn("[owletto] run health update failed", err);
|
|
966
|
+
}
|
|
967
|
+
return intent === "success" ? fence.isSuccessConfirmed() : true;
|
|
968
|
+
})();
|
|
969
|
+
return completionPromise;
|
|
970
|
+
};
|
|
971
|
+
|
|
972
|
+
// Each run carries its own scratch tab, origin policy, and the gateway's
|
|
973
|
+
// browser context title + flow id, which label the tabs it opens (who and
|
|
974
|
+
// which session). Numeric flows name the parent connector run; opaque
|
|
975
|
+
// conversation flows label with this action's run instead.
|
|
976
|
+
const flowRunId = Number(run.action_input?.browser_flow_id);
|
|
977
|
+
const holderRunId = Number.isSafeInteger(flowRunId) && flowRunId > 0
|
|
978
|
+
? flowRunId : Number(run_id);
|
|
979
|
+
const nonEmpty = (v) => (typeof v === "string" && v.trim() ? v.trim() : null);
|
|
980
|
+
const runCtx = {
|
|
981
|
+
recoveryControl: run.action_input?.browser_recovery_control,
|
|
982
|
+
recoveryDeadline: deadline,
|
|
983
|
+
runId: run_id,
|
|
984
|
+
feedContext: run.feed_context ?? null,
|
|
985
|
+
scratchTabId: run.action_input?.tab_id ?? run.config?.tab_id ?? null,
|
|
986
|
+
holderRunId: Number.isFinite(holderRunId) ? holderRunId : null,
|
|
987
|
+
sourceAttribution: run.source_attribution ?? null,
|
|
988
|
+
browserFlowId: nonEmpty(run.action_input?.browser_flow_id),
|
|
989
|
+
// The tab the human opened for a page-activated run. Like browser_flow_id
|
|
990
|
+
// this arrives already stripped-and-replaced by the gateway
|
|
991
|
+
// (trustedChromeActionInput), so it is the server's resolution and not a
|
|
992
|
+
// connector's claim — the guard trusts it only as an exact match.
|
|
993
|
+
activationTabId: Number.isInteger(run.action_input?.activation_tab_id)
|
|
994
|
+
? run.action_input.activation_tab_id
|
|
995
|
+
: null,
|
|
996
|
+
activationTargetUrls: Array.isArray(
|
|
997
|
+
run.action_input?.activation_target_urls,
|
|
998
|
+
)
|
|
999
|
+
? run.action_input.activation_target_urls.map(normalizePageUrl).filter(Boolean)
|
|
1000
|
+
: [],
|
|
1001
|
+
allowedOrigins: [],
|
|
1002
|
+
};
|
|
1003
|
+
|
|
1004
|
+
try {
|
|
1005
|
+
runCtx.allowedOrigins = browserRunOrigins(run.config?.allowed_origins, run.action_input?.allowed_origins);
|
|
1006
|
+
// Feed dispatch — table-driven across all chrome.* connectors.
|
|
1007
|
+
// Feeds follow the peek-then-commit contract (see feeds.js):
|
|
1008
|
+
// tx.items has events to stream; tx.commit clears the consumed
|
|
1009
|
+
// buffer entries ONLY after the gateway POST succeeds. A transient
|
|
1010
|
+
// /stream failure preserves the buffer so the next run retries.
|
|
1011
|
+
// Anything that must survive a lost commit() -- the history sync
|
|
1012
|
+
// window, the bookmarks/downloads snapshot fingerprint -- rides on
|
|
1013
|
+
// tx.checkpoint instead, which the server persists as part of the
|
|
1014
|
+
// completion itself.
|
|
1015
|
+
const feedTable = FEEDS[connector_key];
|
|
1016
|
+
if (feedTable && feed_key && feedTable[feed_key]) {
|
|
1017
|
+
fence.assertActive("feed start");
|
|
1018
|
+
const tx = await feedTable[feed_key](chrome, {
|
|
1019
|
+
...run,
|
|
1020
|
+
runtime: { signal: fence.signal, deadline: fence.deadline, fence },
|
|
1021
|
+
});
|
|
1022
|
+
fence.assertActive("feed collection");
|
|
1023
|
+
await deliverFeedTransaction(tx, {
|
|
1024
|
+
postBatch,
|
|
1025
|
+
complete: safeComplete,
|
|
1026
|
+
fence,
|
|
1027
|
+
});
|
|
1028
|
+
return;
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
// Generic browser-action dispatch remains scoped to the chrome connector.
|
|
1032
|
+
if (
|
|
1033
|
+
connector_key === "chrome" &&
|
|
1034
|
+
action_key &&
|
|
1035
|
+
Object.hasOwn(TOOLS, action_key)
|
|
1036
|
+
) {
|
|
1037
|
+
const input = run.action_input ?? {};
|
|
1038
|
+
let result;
|
|
1039
|
+
try {
|
|
1040
|
+
await controlActivity.start(run, gatewayUrl);
|
|
1041
|
+
result = await runBrowserRecoveryTool(chrome, TOOLS, action_key, input, runCtx);
|
|
1042
|
+
} finally {
|
|
1043
|
+
await controlActivity.finish(run_id);
|
|
1044
|
+
}
|
|
1045
|
+
await safeComplete({
|
|
1046
|
+
status: result.ok ? "success" : "failed",
|
|
1047
|
+
action_output: result.observation ?? {},
|
|
1048
|
+
error_message: result.ok ? undefined : result.error,
|
|
1049
|
+
});
|
|
1050
|
+
return;
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
const knownFeeds = Object.entries(FEEDS)
|
|
1054
|
+
.map(([key, table]) => `${key}: ${Object.keys(table).join(", ")}`)
|
|
1055
|
+
.join("; ");
|
|
1056
|
+
await safeComplete({
|
|
1057
|
+
status: "failed",
|
|
1058
|
+
error_message: `Owletto for Chrome: unknown dispatch (connector='${connector_key}', feed_key='${feed_key}', action_key='${action_key}'). Feeds: ${knownFeeds}; actions on 'chrome': ${Object.keys(TOOLS).join(", ")}.`,
|
|
1059
|
+
});
|
|
1060
|
+
} catch (err) {
|
|
1061
|
+
console.error("[owletto] run failed", err);
|
|
1062
|
+
if (err instanceof SiteAccessRequiredError) {
|
|
1063
|
+
await setHealth({
|
|
1064
|
+
siteAccessNeeded: {
|
|
1065
|
+
origin: err.origin,
|
|
1066
|
+
requester: controlSource(run.source_attribution).actor,
|
|
1067
|
+
at: Date.now(),
|
|
1068
|
+
},
|
|
1069
|
+
});
|
|
1070
|
+
}
|
|
1071
|
+
await safeComplete({
|
|
1072
|
+
status: "failed",
|
|
1073
|
+
error_message: err instanceof Error ? err.message : String(err),
|
|
1074
|
+
});
|
|
1075
|
+
} finally {
|
|
1076
|
+
fence.dispose();
|
|
1077
|
+
// NOTE: tabs are NOT disposed here — that would kill the natural
|
|
1078
|
+
// navigate → get_accessibility_tree → click_ref flow (each is a separate
|
|
1079
|
+
// run, all targeting the same tab). Agents close tabs with close_tab.
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
// A once-a-minute alarm whose only job is to wake an evicted MV3 service
|
|
1084
|
+
// worker, whose top level then restarts polling. It closes nothing.
|
|
1085
|
+
const WAKE_ALARM = "owletto.wake";
|
|
1086
|
+
chrome.alarms.create(WAKE_ALARM, { periodInMinutes: 1 });
|
|
1087
|
+
chrome.alarms.onAlarm.addListener((alarm) => {
|
|
1088
|
+
if (alarm.name === RECOVERY_ALARM) { void reapBrowserRecovery(chrome, TOOLS).catch(console.error); return; }
|
|
1089
|
+
if (alarm.name === WAKE_ALARM) return;
|
|
1090
|
+
if (alarm.name.startsWith(HEARTBEAT_ALARM_PREFIX)) {
|
|
1091
|
+
const runId = runIdFromAlarmName(alarm.name);
|
|
1092
|
+
if (runId !== null) {
|
|
1093
|
+
void tickHeartbeat(runId).catch((err) =>
|
|
1094
|
+
console.warn("[owletto] heartbeat failed", err),
|
|
1095
|
+
);
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
});
|
|
1099
|
+
|
|
1100
|
+
// Resolve credentials + gateway from chrome.storage on every tick. MV3
|
|
1101
|
+
// service workers are torn down between events, so we cannot close over
|
|
1102
|
+
// values captured at run start — the sidepanel may have refreshed the
|
|
1103
|
+
// access token in the interim.
|
|
1104
|
+
async function tickHeartbeat(runId) {
|
|
1105
|
+
const gatewayUrl = await getGatewayUrl();
|
|
1106
|
+
const stored = await chrome.storage.local.get([
|
|
1107
|
+
STORAGE_KEYS.workerId,
|
|
1108
|
+
STORAGE_KEYS.accessToken,
|
|
1109
|
+
]);
|
|
1110
|
+
await sendHeartbeat(chrome, runId, {
|
|
1111
|
+
gatewayUrl,
|
|
1112
|
+
workerId: stored[STORAGE_KEYS.workerId],
|
|
1113
|
+
token: stored[STORAGE_KEYS.accessToken],
|
|
1114
|
+
});
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
// Exchange the stored refresh token for a fresh access token via the gateway's
|
|
1118
|
+
// OAuth token endpoint (device-code clients are auto-registered for the
|
|
1119
|
+
// refresh_token grant). Reads the stored OAuth creds, delegates the network
|
|
1120
|
+
// exchange + outcome classification to auth-refresh.js, and persists the new
|
|
1121
|
+
// token on success. Returns the refresh outcome (REFRESH_REFRESHED /
|
|
1122
|
+
// REFRESH_REJECTED / REFRESH_TRANSIENT) so the caller only tears the pairing
|
|
1123
|
+
// down on a definitive rejection — never on a transient gateway failure.
|
|
1124
|
+
async function tryRefreshToken(gatewayUrl) {
|
|
1125
|
+
return refreshStoredPairing(chrome.storage.local, STORAGE_KEYS, gatewayUrl);
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
async function postJson(url, token, body, options = {}) {
|
|
1129
|
+
const res = await fetch(url, {
|
|
1130
|
+
method: "POST",
|
|
1131
|
+
headers: {
|
|
1132
|
+
"content-type": "application/json",
|
|
1133
|
+
authorization: `Bearer ${token}`,
|
|
1134
|
+
},
|
|
1135
|
+
body: JSON.stringify(body),
|
|
1136
|
+
signal: options.signal,
|
|
1137
|
+
});
|
|
1138
|
+
if (!res.ok) {
|
|
1139
|
+
const text = await res.text().catch(() => "");
|
|
1140
|
+
throw new Error(`${url} → ${res.status} ${text.slice(0, 200)}`);
|
|
1141
|
+
}
|
|
1142
|
+
return res.json().catch(() => null);
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
async function computeAdvertisedCapabilities() {
|
|
1146
|
+
const caps = Object.fromEntries(DEFAULT_CAPABILITIES.map((c) => [c, true]));
|
|
1147
|
+
for (const [perm, cap] of Object.entries(OPTIONAL_CAPABILITIES)) {
|
|
1148
|
+
const granted = await chrome.permissions.contains({ permissions: [perm] });
|
|
1149
|
+
if (granted) caps[cap] = true;
|
|
1150
|
+
}
|
|
1151
|
+
// Action-level grants (e.g. close_user_tabs / tabs.manage) stay in
|
|
1152
|
+
// extension storage and are enforced by the tool — they are not platform
|
|
1153
|
+
// capabilities and are not advertised on poll.
|
|
1154
|
+
return caps;
|
|
1155
|
+
}
|