@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
|
@@ -28,6 +28,7 @@ import type { DiscoveryDocument } from './_google/discovery';
|
|
|
28
28
|
import gmailDiscovery from './_google/discovery/gmail_v1.json';
|
|
29
29
|
import { GOOGLE_API_POLICIES } from './_google/policies';
|
|
30
30
|
import TurndownService from 'turndown';
|
|
31
|
+
import { syncGmailReferences, type GmailReferenceState } from './google_gmail_references';
|
|
31
32
|
|
|
32
33
|
/**
|
|
33
34
|
* Supported Gmail API methods as actions, compiled from the pinned Discovery
|
|
@@ -108,6 +109,7 @@ interface GmailThreadGetResponse {
|
|
|
108
109
|
// ---------------------------------------------------------------------------
|
|
109
110
|
|
|
110
111
|
interface GmailCheckpoint {
|
|
112
|
+
references?: GmailReferenceState;
|
|
111
113
|
/** Bumped when the shape below changes; a mismatch re-runs the lookback. */
|
|
112
114
|
schema_version?: number;
|
|
113
115
|
/** Serialized search + person filter the two fields below were produced under. */
|
|
@@ -173,7 +175,7 @@ export default class GmailConnector extends ConnectorRuntime<GmailCheckpoint, Gm
|
|
|
173
175
|
name: 'Gmail',
|
|
174
176
|
description:
|
|
175
177
|
'Syncs Gmail threads, live-reads matching messages, and supports sending, drafts, and replies.',
|
|
176
|
-
version: '1.0
|
|
178
|
+
version: '1.1.0',
|
|
177
179
|
faviconDomain: 'mail.google.com',
|
|
178
180
|
authSchema: {
|
|
179
181
|
methods: [
|
|
@@ -193,6 +195,59 @@ export default class GmailConnector extends ConnectorRuntime<GmailCheckpoint, Gm
|
|
|
193
195
|
],
|
|
194
196
|
},
|
|
195
197
|
feeds: {
|
|
198
|
+
message_references: {
|
|
199
|
+
key: 'message_references',
|
|
200
|
+
name: 'Message references',
|
|
201
|
+
userManaged: true,
|
|
202
|
+
requiredScopes: ['https://www.googleapis.com/auth/gmail.readonly'],
|
|
203
|
+
description:
|
|
204
|
+
'ID-only references to available messages and subsequent additions, including spam, trash, imports and drafts. No subjects, senders, snippets, bodies or provider occurrence dates. Use created_at for arrival previews; source_occurred_at is unknown. History expiry is a visible gap; recovery lists currently available messages. Sync-only, explicitly triggered or scheduled. Not a current-state mirror, edit/deletion archive or observer subscription.',
|
|
205
|
+
sync: (ctx) =>
|
|
206
|
+
syncGmailReferences(ctx, (token) => this.createClient(token)),
|
|
207
|
+
configSchema: {
|
|
208
|
+
type: 'object',
|
|
209
|
+
properties: {
|
|
210
|
+
max_results: {
|
|
211
|
+
type: 'integer',
|
|
212
|
+
minimum: 1,
|
|
213
|
+
maximum: 500,
|
|
214
|
+
default: 500,
|
|
215
|
+
description:
|
|
216
|
+
'Maximum message references per pass, without individual message downloads. Unfinished inventory/history resumes immediately from its committed checkpoint, even without a schedule.',
|
|
217
|
+
},
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
eventKinds: {
|
|
221
|
+
message_reference: {
|
|
222
|
+
description:
|
|
223
|
+
'An available Gmail message reference, without email content',
|
|
224
|
+
metadataSchema: {
|
|
225
|
+
type: 'object',
|
|
226
|
+
properties: {
|
|
227
|
+
source_id: { type: 'string' },
|
|
228
|
+
thread_id: { type: 'string' },
|
|
229
|
+
source_occurred_at: { type: 'null' },
|
|
230
|
+
timestamp_basis: { type: 'string', enum: ['unknown'] },
|
|
231
|
+
},
|
|
232
|
+
},
|
|
233
|
+
},
|
|
234
|
+
source_gap: {
|
|
235
|
+
description:
|
|
236
|
+
'A retained Gmail history discontinuity; available inventory cannot restore deleted or expired history',
|
|
237
|
+
metadataSchema: {
|
|
238
|
+
type: 'object',
|
|
239
|
+
properties: {
|
|
240
|
+
reason: { type: 'string' },
|
|
241
|
+
from_history_id: { type: 'string' },
|
|
242
|
+
to_history_id: { type: 'string' },
|
|
243
|
+
recovery: { type: 'string' },
|
|
244
|
+
source_occurred_at: { type: 'null' },
|
|
245
|
+
timestamp_basis: { type: 'string', enum: ['unknown'] },
|
|
246
|
+
},
|
|
247
|
+
},
|
|
248
|
+
},
|
|
249
|
+
},
|
|
250
|
+
},
|
|
196
251
|
threads: {
|
|
197
252
|
key: 'threads',
|
|
198
253
|
name: 'Threads',
|
|
@@ -270,15 +325,12 @@ export default class GmailConnector extends ConnectorRuntime<GmailCheckpoint, Gm
|
|
|
270
325
|
attributions: [
|
|
271
326
|
{
|
|
272
327
|
role: 'authored_by',
|
|
273
|
-
//
|
|
274
|
-
//
|
|
275
|
-
//
|
|
276
|
-
//
|
|
277
|
-
//
|
|
278
|
-
//
|
|
279
|
-
// unreplied counterparties and rejects role/list/broadcast noise.
|
|
280
|
-
// Only person-relevant senders materialize a `person`; everything
|
|
281
|
-
// else still links to an existing contact on identity match.
|
|
328
|
+
// Default mode requires interaction with a non-role counterparty;
|
|
329
|
+
// human_senders_only also admits unreplied heuristic matches.
|
|
330
|
+
// Both reject list/bulk threads for person creation.
|
|
331
|
+
// Missing verdicts from older runs fail closed for creation;
|
|
332
|
+
// a replied-only fallback would override explicit rejections.
|
|
333
|
+
// Rejected or unclassified senders still link to existing contacts.
|
|
282
334
|
autoCreate: true,
|
|
283
335
|
target: {
|
|
284
336
|
entityType: 'person',
|
|
@@ -297,32 +349,6 @@ export default class GmailConnector extends ConnectorRuntime<GmailCheckpoint, Gm
|
|
|
297
349
|
},
|
|
298
350
|
},
|
|
299
351
|
},
|
|
300
|
-
{
|
|
301
|
-
role: 'authored_by',
|
|
302
|
-
// Legacy gate for v1.0.3 run payloads, which carry `replied` but
|
|
303
|
-
// not `person_relevant`. Keep it until old in-flight runs/caches
|
|
304
|
-
// drain: the server loads the current definition by connector key,
|
|
305
|
-
// so a rolling deploy would otherwise silently skip person creation
|
|
306
|
-
// for a pre-refresh event. Both rules share the email identity, so
|
|
307
|
-
// the pipeline's first-writer-wins dedupes them for new payloads.
|
|
308
|
-
autoCreate: true,
|
|
309
|
-
target: {
|
|
310
|
-
entityType: 'person',
|
|
311
|
-
createWhen: { path: 'metadata.replied', equals: true },
|
|
312
|
-
titlePath: 'metadata.from_name',
|
|
313
|
-
identities: [{ namespace: 'email', eventPath: 'metadata.from_email' }],
|
|
314
|
-
},
|
|
315
|
-
traits: {
|
|
316
|
-
from_name: {
|
|
317
|
-
eventPath: 'metadata.from_name',
|
|
318
|
-
mergeStrategy: 'prefer_non_empty',
|
|
319
|
-
},
|
|
320
|
-
last_email_at: {
|
|
321
|
-
eventPath: 'occurred_at',
|
|
322
|
-
mergeStrategy: 'overwrite',
|
|
323
|
-
},
|
|
324
|
-
},
|
|
325
|
-
},
|
|
326
352
|
],
|
|
327
353
|
},
|
|
328
354
|
},
|
|
@@ -1156,7 +1182,10 @@ export default class GmailConnector extends ConnectorRuntime<GmailCheckpoint, Gm
|
|
|
1156
1182
|
!this.isListThread(messages) &&
|
|
1157
1183
|
(replied ||
|
|
1158
1184
|
this.externalRecipientCount(selectedSentMessage, isSelf) <= BROADCAST_RECIPIENT_CAP)
|
|
1159
|
-
: replied
|
|
1185
|
+
: replied &&
|
|
1186
|
+
!!selected &&
|
|
1187
|
+
isPersonRelevantSender(selected.email, selected.name, true) &&
|
|
1188
|
+
!this.isListThread(messages);
|
|
1160
1189
|
|
|
1161
1190
|
return {
|
|
1162
1191
|
replied,
|
|
@@ -1497,10 +1526,10 @@ const CONSUMER_MAIL_DOMAINS = new Set([
|
|
|
1497
1526
|
const HUMAN_NAME_RE = /^[A-ZÀ-Ý][a-zà-ÿ]+(?:[ '\u2019-][A-ZÀ-Ý][a-zà-ÿ]+)+$/;
|
|
1498
1527
|
|
|
1499
1528
|
/**
|
|
1500
|
-
*
|
|
1501
|
-
*
|
|
1502
|
-
*
|
|
1503
|
-
*
|
|
1529
|
+
* Address eligibility for person minting in both feed modes. Role/system
|
|
1530
|
+
* mailboxes never qualify. A replied non-role address qualifies directly;
|
|
1531
|
+
* human_senders_only also admits unreplied addresses with a consumer-mail
|
|
1532
|
+
* domain or a human-looking display name. Thread-level gates apply separately.
|
|
1504
1533
|
*/
|
|
1505
1534
|
export function isPersonRelevantSender(
|
|
1506
1535
|
email: string | null | undefined,
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
/** Gmail mailbox additions, retained as references rather than email content. */
|
|
2
|
+
import type {
|
|
3
|
+
EventEnvelope,
|
|
4
|
+
HttpClient,
|
|
5
|
+
SyncContext,
|
|
6
|
+
SyncResult,
|
|
7
|
+
} from '@lobu/connector-sdk';
|
|
8
|
+
|
|
9
|
+
const BASE = 'https://gmail.googleapis.com/gmail/v1/users/me';
|
|
10
|
+
const REQUEST_BUDGET = 20;
|
|
11
|
+
|
|
12
|
+
export interface GmailReferenceState {
|
|
13
|
+
version: 1;
|
|
14
|
+
account: string;
|
|
15
|
+
history_id: string;
|
|
16
|
+
inventory?: { page_token?: string; recent_tokens?: string[] };
|
|
17
|
+
partial?: { record_id: string; after_message_id: string; digest: string };
|
|
18
|
+
history_page?: { token: string; recent_tokens?: string[] };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface ReferenceCheckpoint {
|
|
22
|
+
references?: GmailReferenceState;
|
|
23
|
+
}
|
|
24
|
+
interface ReferenceConfig {
|
|
25
|
+
max_results?: number;
|
|
26
|
+
}
|
|
27
|
+
interface MessageIdentity {
|
|
28
|
+
id: string;
|
|
29
|
+
threadId: string;
|
|
30
|
+
}
|
|
31
|
+
interface HistoryRecord {
|
|
32
|
+
id: string;
|
|
33
|
+
messagesAdded?: Array<{ message: MessageIdentity }>;
|
|
34
|
+
}
|
|
35
|
+
interface HistoryPage {
|
|
36
|
+
historyId: string;
|
|
37
|
+
history?: HistoryRecord[];
|
|
38
|
+
nextPageToken?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function historyId(value: unknown): string {
|
|
42
|
+
if (typeof value !== 'string' || !/^\d{1,128}$/.test(value))
|
|
43
|
+
throw new Error('Gmail returned an invalid history identity');
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function nativeId(value: unknown): string {
|
|
48
|
+
if (typeof value !== 'string' || !/^[a-zA-Z0-9_-]{1,256}$/.test(value))
|
|
49
|
+
throw new Error('Gmail returned an invalid message identity');
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function profile(
|
|
54
|
+
http: HttpClient,
|
|
55
|
+
): Promise<{ account: string; history: string }> {
|
|
56
|
+
const result = await http.raw(
|
|
57
|
+
`${BASE}/profile?fields=emailAddress,historyId`,
|
|
58
|
+
);
|
|
59
|
+
if (!result.ok) throw new Error(`Gmail profile error (${result.status})`);
|
|
60
|
+
const data = (await result.json()) as {
|
|
61
|
+
emailAddress?: string;
|
|
62
|
+
historyId?: string;
|
|
63
|
+
};
|
|
64
|
+
const account = data.emailAddress?.trim().toLowerCase();
|
|
65
|
+
if (!account || !/^[^\s@]+@[^\s@]+$/.test(account))
|
|
66
|
+
throw new Error('Gmail returned an invalid account identity');
|
|
67
|
+
return { account, history: historyId(data.historyId) };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function gap(
|
|
71
|
+
account: string,
|
|
72
|
+
key: string,
|
|
73
|
+
metadata: Record<string, unknown>,
|
|
74
|
+
): EventEnvelope {
|
|
75
|
+
return {
|
|
76
|
+
origin_id: `gmail_gap:${encodeURIComponent(account)}:${key}`,
|
|
77
|
+
origin_type: 'source_gap',
|
|
78
|
+
payload_text: '',
|
|
79
|
+
// A gap has no provider occurrence timestamp. The epoch keeps retries of
|
|
80
|
+
// this same source discontinuity identical; arrival reads use created_at.
|
|
81
|
+
occurred_at: new Date(0),
|
|
82
|
+
metadata: {
|
|
83
|
+
...metadata,
|
|
84
|
+
source_occurred_at: null,
|
|
85
|
+
timestamp_basis: 'unknown',
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function messageReference(
|
|
91
|
+
state: GmailReferenceState,
|
|
92
|
+
stub: MessageIdentity,
|
|
93
|
+
): EventEnvelope {
|
|
94
|
+
const id = nativeId(stub.id);
|
|
95
|
+
const thread = nativeId(stub.threadId);
|
|
96
|
+
return {
|
|
97
|
+
// Thread content uses bare thread IDs, which may equal the first message ID.
|
|
98
|
+
// Scope native identity to the authenticated account, independently of feeds.
|
|
99
|
+
origin_id: `gmail_message:${encodeURIComponent(state.account)}:${id}`,
|
|
100
|
+
origin_type: 'message_reference',
|
|
101
|
+
payload_text: '',
|
|
102
|
+
source_url: `https://mail.google.com/mail/u/${encodeURIComponent(state.account)}/#all/${thread}`,
|
|
103
|
+
// List/history stubs do not include provider occurrence time. This stable
|
|
104
|
+
// transport sentinel is NOT the email date: use created_at for arrival
|
|
105
|
+
// previews and source_occurred_at for provider-time rendering. Never infer
|
|
106
|
+
// a first contact from it. Omitting title/text also avoids empty embeddings.
|
|
107
|
+
occurred_at: new Date(0),
|
|
108
|
+
metadata: {
|
|
109
|
+
source_id: id,
|
|
110
|
+
thread_id: thread,
|
|
111
|
+
source_occurred_at: null,
|
|
112
|
+
timestamp_basis: 'unknown',
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function identities(stubs: MessageIdentity[]): MessageIdentity[] {
|
|
118
|
+
const messages = new Map<string, MessageIdentity>();
|
|
119
|
+
for (const stub of stubs) {
|
|
120
|
+
const id = nativeId(stub?.id);
|
|
121
|
+
const threadId = nativeId(stub?.threadId);
|
|
122
|
+
const previous = messages.get(id);
|
|
123
|
+
if (previous && previous.threadId !== threadId)
|
|
124
|
+
throw new Error('Gmail message identity conflicts');
|
|
125
|
+
messages.set(id, { id, threadId });
|
|
126
|
+
}
|
|
127
|
+
return [...messages.values()];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function additions(record: HistoryRecord): MessageIdentity[] {
|
|
131
|
+
const entries =
|
|
132
|
+
record.messagesAdded === undefined ? [] : record.messagesAdded;
|
|
133
|
+
if (!Array.isArray(entries))
|
|
134
|
+
throw new Error('Gmail returned invalid history additions');
|
|
135
|
+
return identities(entries.map((entry) => entry?.message)).sort((a, b) =>
|
|
136
|
+
a.id < b.id ? -1 : a.id > b.id ? 1 : 0,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function pageToken(value: unknown): string | undefined {
|
|
141
|
+
if (value === undefined) return undefined;
|
|
142
|
+
if (typeof value !== 'string' || !value || value.length > 8192)
|
|
143
|
+
throw new Error('Gmail returned an invalid page token');
|
|
144
|
+
return value;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Remember only a bounded set of exact recent cursors. Empty advancing pages
|
|
148
|
+
// are legitimate source progress; the per-pass request budget bounds work.
|
|
149
|
+
// Reject actual short cursor cycles without imposing an item-arrival deadline.
|
|
150
|
+
function nextTokens(
|
|
151
|
+
next: string,
|
|
152
|
+
current: string | undefined,
|
|
153
|
+
recent: string[] = [],
|
|
154
|
+
): string[] {
|
|
155
|
+
if (next === current || recent.includes(next))
|
|
156
|
+
throw new Error('Gmail cursor did not advance');
|
|
157
|
+
return [
|
|
158
|
+
...recent,
|
|
159
|
+
...(current && !recent.includes(current) ? [current] : []),
|
|
160
|
+
next,
|
|
161
|
+
].slice(-8);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Initial available-message inventory, then Gmail messageAdded history,
|
|
166
|
+
* including spam/trash. This is not a current-state mailbox mirror, an
|
|
167
|
+
* edit/deletion archive or an observer capture subscription.
|
|
168
|
+
* Every durable position travels with exactly the references it covers.
|
|
169
|
+
*/
|
|
170
|
+
export async function syncGmailReferences(
|
|
171
|
+
ctx: SyncContext<ReferenceCheckpoint, ReferenceConfig>,
|
|
172
|
+
createClient: (token: string) => HttpClient,
|
|
173
|
+
): Promise<SyncResult> {
|
|
174
|
+
const token = ctx.credentials?.accessToken;
|
|
175
|
+
if (!token) throw new Error('Gmail requires Google OAuth credentials.');
|
|
176
|
+
const maximum = ctx.config.max_results ?? 500;
|
|
177
|
+
if (!Number.isInteger(maximum) || maximum < 1 || maximum > 500)
|
|
178
|
+
throw new Error('Gmail reference max_results must be between 1 and 500');
|
|
179
|
+
const http = createClient(token);
|
|
180
|
+
const identity = await profile(http);
|
|
181
|
+
const saved = ctx.checkpoint?.references;
|
|
182
|
+
if (saved && saved.account !== identity.account)
|
|
183
|
+
throw new Error(
|
|
184
|
+
'Gmail reference account does not match its saved checkpoint',
|
|
185
|
+
);
|
|
186
|
+
if (saved && saved.version !== 1)
|
|
187
|
+
throw new Error('Gmail reference checkpoint version is unsupported');
|
|
188
|
+
if (saved) historyId(saved.history_id);
|
|
189
|
+
let state: GmailReferenceState = saved
|
|
190
|
+
? { ...saved }
|
|
191
|
+
: {
|
|
192
|
+
version: 1,
|
|
193
|
+
account: identity.account,
|
|
194
|
+
history_id: identity.history,
|
|
195
|
+
inventory: {},
|
|
196
|
+
};
|
|
197
|
+
const commit = async (events: EventEnvelope[], next: GmailReferenceState) => {
|
|
198
|
+
if ((await profile(http)).account !== state.account)
|
|
199
|
+
throw new Error('Gmail reference account changed during capture');
|
|
200
|
+
await ctx.commit(events, { references: next });
|
|
201
|
+
state = next;
|
|
202
|
+
};
|
|
203
|
+
// Commit the history boundary before the first inventory page so arrivals
|
|
204
|
+
// during a long scan are reconciled after available history has been listed.
|
|
205
|
+
if (!saved) {
|
|
206
|
+
await commit([], {
|
|
207
|
+
version: 1,
|
|
208
|
+
account: identity.account,
|
|
209
|
+
history_id: identity.history,
|
|
210
|
+
inventory: {},
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
let inspected = 0;
|
|
214
|
+
let found = 0;
|
|
215
|
+
const recover = async (reason: string) => {
|
|
216
|
+
const baseline = await profile(http);
|
|
217
|
+
if (baseline.account !== state.account)
|
|
218
|
+
throw new Error('Gmail reference account changed during recovery');
|
|
219
|
+
const event = gap(
|
|
220
|
+
state.account,
|
|
221
|
+
`${reason}:${state.history_id}:${baseline.history}`,
|
|
222
|
+
{
|
|
223
|
+
reason,
|
|
224
|
+
from_history_id: state.history_id,
|
|
225
|
+
to_history_id: baseline.history,
|
|
226
|
+
recovery: 'available_message_inventory',
|
|
227
|
+
},
|
|
228
|
+
);
|
|
229
|
+
await commit([event], {
|
|
230
|
+
version: 1,
|
|
231
|
+
account: state.account,
|
|
232
|
+
history_id: baseline.history,
|
|
233
|
+
inventory: {},
|
|
234
|
+
});
|
|
235
|
+
return {
|
|
236
|
+
status: 'more',
|
|
237
|
+
metadata: { items_found: found + 1, continuity_gap: true },
|
|
238
|
+
} satisfies SyncResult;
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
for (let requests = 0; requests < REQUEST_BUDGET; requests++) {
|
|
242
|
+
if (state.inventory) {
|
|
243
|
+
const params = new URLSearchParams({
|
|
244
|
+
includeSpamTrash: 'true',
|
|
245
|
+
maxResults: String(maximum - inspected),
|
|
246
|
+
fields: 'messages(id,threadId),nextPageToken',
|
|
247
|
+
});
|
|
248
|
+
if (state.inventory.page_token)
|
|
249
|
+
params.set('pageToken', state.inventory.page_token);
|
|
250
|
+
const result = await http.raw(`${BASE}/messages?${params}`);
|
|
251
|
+
if (!result.ok) {
|
|
252
|
+
if (
|
|
253
|
+
state.inventory.page_token &&
|
|
254
|
+
(result.status === 400 || result.status === 404)
|
|
255
|
+
) {
|
|
256
|
+
// Retain this generation's original pre-inventory history baseline.
|
|
257
|
+
await commit([], { ...state, inventory: {} });
|
|
258
|
+
return {
|
|
259
|
+
status: 'more',
|
|
260
|
+
metadata: { items_found: found, inventory_restarted: true },
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
throw new Error(`Gmail reference inventory error (${result.status})`);
|
|
264
|
+
}
|
|
265
|
+
const data = (await result.json()) as {
|
|
266
|
+
messages?: MessageIdentity[];
|
|
267
|
+
nextPageToken?: string;
|
|
268
|
+
};
|
|
269
|
+
const messages = data.messages === undefined ? [] : data.messages;
|
|
270
|
+
if (!Array.isArray(messages))
|
|
271
|
+
throw new Error('Gmail returned invalid inventory messages');
|
|
272
|
+
const token = pageToken(data.nextPageToken);
|
|
273
|
+
if (messages.length > maximum - inspected)
|
|
274
|
+
throw new Error(
|
|
275
|
+
'Gmail inventory exceeded its requested message budget',
|
|
276
|
+
);
|
|
277
|
+
const recent = token
|
|
278
|
+
? nextTokens(
|
|
279
|
+
token,
|
|
280
|
+
state.inventory.page_token,
|
|
281
|
+
state.inventory.recent_tokens,
|
|
282
|
+
)
|
|
283
|
+
: undefined;
|
|
284
|
+
const events: EventEnvelope[] = [];
|
|
285
|
+
for (const message of identities(messages))
|
|
286
|
+
events.push(messageReference(state, message));
|
|
287
|
+
inspected += messages.length;
|
|
288
|
+
found += events.length;
|
|
289
|
+
const next = { ...state };
|
|
290
|
+
if (token) next.inventory = { page_token: token, recent_tokens: recent };
|
|
291
|
+
else delete next.inventory;
|
|
292
|
+
await commit(events, next);
|
|
293
|
+
} else {
|
|
294
|
+
const params = new URLSearchParams({
|
|
295
|
+
startHistoryId: state.history_id,
|
|
296
|
+
historyTypes: 'messageAdded',
|
|
297
|
+
maxResults: '1',
|
|
298
|
+
fields:
|
|
299
|
+
'history(id,messagesAdded(message(id,threadId))),historyId,nextPageToken',
|
|
300
|
+
});
|
|
301
|
+
if (state.history_page) params.set('pageToken', state.history_page.token);
|
|
302
|
+
const result = await http.raw(`${BASE}/history?${params}`);
|
|
303
|
+
if (!result.ok) {
|
|
304
|
+
if (
|
|
305
|
+
state.history_page &&
|
|
306
|
+
(result.status === 400 || result.status === 404)
|
|
307
|
+
) {
|
|
308
|
+
// A retired page token does not prove the history boundary expired.
|
|
309
|
+
// Replay from that boundary, retaining any partially consumed record.
|
|
310
|
+
const next = { ...state };
|
|
311
|
+
delete next.history_page;
|
|
312
|
+
await commit([], next);
|
|
313
|
+
return { status: 'more', metadata: { items_found: found } };
|
|
314
|
+
}
|
|
315
|
+
if (result.status === 404) return recover('history_expired');
|
|
316
|
+
throw new Error(`Gmail reference history error (${result.status})`);
|
|
317
|
+
}
|
|
318
|
+
const page = (await result.json()) as HistoryPage;
|
|
319
|
+
const head = historyId(page.historyId);
|
|
320
|
+
if (BigInt(head) < BigInt(state.history_id))
|
|
321
|
+
throw new Error('Gmail history moved backwards');
|
|
322
|
+
const records = page.history === undefined ? [] : page.history;
|
|
323
|
+
if (!Array.isArray(records))
|
|
324
|
+
throw new Error('Gmail returned invalid history records');
|
|
325
|
+
const token = pageToken(page.nextPageToken);
|
|
326
|
+
if (records.length > 1)
|
|
327
|
+
throw new Error('Gmail history exceeded its requested record budget');
|
|
328
|
+
const record = records[0];
|
|
329
|
+
if (records.length === 0) {
|
|
330
|
+
if (token) {
|
|
331
|
+
const recent = nextTokens(
|
|
332
|
+
token,
|
|
333
|
+
state.history_page?.token,
|
|
334
|
+
state.history_page?.recent_tokens,
|
|
335
|
+
);
|
|
336
|
+
await commit([], {
|
|
337
|
+
...state,
|
|
338
|
+
history_page: { token, recent_tokens: recent },
|
|
339
|
+
});
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
if (state.partial) return recover('history_anchor_missing');
|
|
343
|
+
const next = { ...state, history_id: head };
|
|
344
|
+
delete next.history_page;
|
|
345
|
+
await commit([], next);
|
|
346
|
+
return { status: 'complete', metadata: { items_found: found } };
|
|
347
|
+
}
|
|
348
|
+
const recordId = historyId(record?.id);
|
|
349
|
+
if (
|
|
350
|
+
BigInt(recordId) <= BigInt(state.history_id) ||
|
|
351
|
+
BigInt(recordId) > BigInt(head)
|
|
352
|
+
)
|
|
353
|
+
throw new Error('Gmail returned an invalid history boundary');
|
|
354
|
+
if (state.partial && recordId !== state.partial.record_id)
|
|
355
|
+
return recover('history_anchor_missing');
|
|
356
|
+
const messages = additions(record);
|
|
357
|
+
const digest = Array.from(
|
|
358
|
+
new Uint8Array(
|
|
359
|
+
await crypto.subtle.digest(
|
|
360
|
+
'SHA-256',
|
|
361
|
+
new TextEncoder().encode(JSON.stringify(messages)),
|
|
362
|
+
),
|
|
363
|
+
),
|
|
364
|
+
(byte) => byte.toString(16).padStart(2, '0'),
|
|
365
|
+
).join('');
|
|
366
|
+
if (
|
|
367
|
+
state.partial &&
|
|
368
|
+
(state.partial.digest !== digest ||
|
|
369
|
+
!messages.some(
|
|
370
|
+
(message) => message.id === state.partial?.after_message_id,
|
|
371
|
+
))
|
|
372
|
+
)
|
|
373
|
+
return recover('history_anchor_changed');
|
|
374
|
+
const unread = state.partial
|
|
375
|
+
? messages.filter(
|
|
376
|
+
(message) => message.id > state.partial!.after_message_id,
|
|
377
|
+
)
|
|
378
|
+
: messages;
|
|
379
|
+
const batch = unread.slice(0, maximum - inspected);
|
|
380
|
+
const events: EventEnvelope[] = [];
|
|
381
|
+
for (const message of batch) {
|
|
382
|
+
events.push(messageReference(state, message));
|
|
383
|
+
inspected++;
|
|
384
|
+
}
|
|
385
|
+
found += events.length;
|
|
386
|
+
const next = { ...state };
|
|
387
|
+
if (batch.length < unread.length) {
|
|
388
|
+
next.partial = {
|
|
389
|
+
record_id: recordId,
|
|
390
|
+
after_message_id: batch[batch.length - 1].id,
|
|
391
|
+
digest,
|
|
392
|
+
};
|
|
393
|
+
} else {
|
|
394
|
+
next.history_id = token ? recordId : head;
|
|
395
|
+
delete next.partial;
|
|
396
|
+
delete next.history_page;
|
|
397
|
+
}
|
|
398
|
+
await commit(events, next);
|
|
399
|
+
if (!next.partial && !token)
|
|
400
|
+
return { status: 'complete', metadata: { items_found: found } };
|
|
401
|
+
}
|
|
402
|
+
if (inspected >= maximum)
|
|
403
|
+
return { status: 'more', metadata: { items_found: found } };
|
|
404
|
+
}
|
|
405
|
+
return { status: 'more', metadata: { items_found: found } };
|
|
406
|
+
}
|