@lobu/runtime-device 24.9.0 → 24.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/dist/component.json +1 -1
  2. package/dist/dependencies.bun.lock +19 -17
  3. package/npm-shrinkwrap.json +81 -64
  4. package/package.json +1 -1
  5. package/vendor/cli/dist/bundled-skills/lobu/SKILL.md +3 -0
  6. package/vendor/cli/dist/commands/_lib/apply/apply-cmd.js +4 -4
  7. package/vendor/cli/dist/commands/_lib/apply/client.d.ts +21 -17
  8. package/vendor/cli/dist/commands/_lib/apply/client.js +32 -34
  9. package/vendor/cli/dist/commands/dev.js +2 -0
  10. package/vendor/cli/dist/connectors/linkedin-inbox-adapter.js +13 -3
  11. package/vendor/cli/dist/connectors/postgres.ts +39 -24
  12. package/vendor/cli/dist/connectors/whatsapp-web-adapter.js +54 -22
  13. package/vendor/cli/dist/connectors/whatsapp-web-helpers.ts +48 -2
  14. package/vendor/cli/dist/connectors/whatsapp_web.ts +23 -7
  15. package/vendor/cli/dist/connectors/x-dm-adapter.js +17 -3
  16. package/vendor/cli/dist/internal/context.d.ts +5 -0
  17. package/vendor/cli/dist/internal/context.js +23 -9
  18. package/vendor/cli/dist/runtime-components.json +4 -4
  19. package/vendor/cli/package.json +1 -1
  20. package/vendor/connector-sdk/dist/connector-types.d.ts +6 -0
  21. package/vendor/connector-sdk/dist/index.d.ts +1 -0
  22. package/vendor/connector-sdk/dist/index.js +1 -0
  23. package/vendor/connector-sdk/dist/ip-reachability.d.ts +1 -1
  24. package/vendor/connector-sdk/dist/ip-reachability.js +1 -1
  25. package/vendor/connector-sdk/dist/logger.d.ts +1 -1
  26. package/vendor/connector-sdk/dist/logger.js +1 -1
  27. package/vendor/connector-sdk/dist/reaction-client-types.d.ts +3 -1
  28. package/vendor/connector-sdk/dist/retry.d.ts +1 -1
  29. package/vendor/connector-sdk/dist/retry.js +1 -1
  30. package/vendor/connector-sdk/dist/source-withheld-error.d.ts +16 -0
  31. package/vendor/connector-sdk/dist/source-withheld-error.js +19 -0
  32. package/vendor/connector-sdk/package.json +1 -1
  33. package/vendor/connector-worker/dist/agent-turn/guest.bundle.js +1 -1
  34. package/vendor/connector-worker/dist/daemon/index.d.ts +1 -0
  35. package/vendor/connector-worker/dist/daemon/index.js +1 -0
  36. package/vendor/connector-worker/dist/daemon/terminal-delivery.d.ts +12 -2
  37. package/vendor/connector-worker/dist/daemon/terminal-delivery.js +19 -12
  38. package/vendor/connector-worker/dist/executor/interface.d.ts +10 -0
  39. package/vendor/connector-worker/dist/executor/interface.js +6 -0
  40. package/vendor/connector-worker/dist/executor/isolate.d.ts +1 -0
  41. package/vendor/connector-worker/dist/executor/isolate.js +85 -24
  42. package/vendor/connector-worker/dist/isolate/prelude.js +5 -0
  43. package/vendor/connector-worker/package.json +1 -1
  44. package/vendor/core/dist/classify-error.d.ts +1 -1
  45. package/vendor/core/dist/classify-error.js +2 -2
  46. package/vendor/core/dist/contracts/tools/manage-automations.d.ts +28 -3
  47. package/vendor/core/dist/contracts/tools/manage-automations.js +27 -8
  48. package/vendor/core/dist/contracts/tools/manage-entity.d.ts +8 -0
  49. package/vendor/core/dist/contracts/tools/manage-entity.js +11 -3
  50. package/vendor/core/dist/contracts/tools/save-memory.d.ts +11 -1
  51. package/vendor/core/dist/contracts/tools/save-memory.js +25 -3
  52. package/vendor/core/dist/contracts/tools/search-memory.js +1 -1
  53. package/vendor/core/dist/credentials.d.ts +12 -0
  54. package/vendor/core/dist/credentials.js +1 -0
  55. package/vendor/core/dist/errors.js +2 -2
  56. package/vendor/core/dist/worker/auth.d.ts +5 -0
  57. package/vendor/core/dist/worker/auth.js +15 -4
  58. package/vendor/core/package.json +1 -1
  59. package/vendor/postgres/cf/src/connection.js +7 -2
  60. package/vendor/postgres/cjs/src/connection.js +7 -2
  61. package/vendor/postgres/src/connection.js +7 -2
  62. package/vendor/views/dist/native.d.ts +33 -1
  63. package/vendor/views/dist/native.js +3 -0
  64. package/vendor/views/package.json +1 -1
  65. /package/vendor/postgres/{.bun-tag-c81dacd34e959a7a → .bun-tag-652f2580b8090d71} +0 -0
@@ -155,6 +155,8 @@ export interface RemoteAutomation {
155
155
  outputs?: Record<string, unknown> | null;
156
156
  reactions_guidance?: string | null;
157
157
  }
158
+ /** Whether apply creates a schema type or updates the existing one. */
159
+ type SchemaWriteVerb = "create" | "update";
158
160
  interface UpsertEntityTypeResult {
159
161
  created?: boolean;
160
162
  updated?: boolean;
@@ -378,15 +380,19 @@ export declare class ApplyClient {
378
380
  listEntityTypes(): Promise<RemoteEntityType[]>;
379
381
  /**
380
382
  * The `manage_entity_schema` admin tool exposes separate `create` / `update`
381
- * actions and surfaces duplicates as a coded 409 (`[entity_type_exists]` /
382
- * `[relationship_type_exists]`). Probe with `create`; on that explicit
383
- * duplicate signal retry with `update`. Any other error (e.g. a 422
384
- * `[invalid_schema]` validation failure) propagates verbatim — retrying it
385
- * as an update used to mask the real message behind "Entity type not
386
- * found" (issue #1177).
383
+ * actions. The plan already knows which one a row needs, so an `update` row
384
+ * goes straight to `update`: probing with `create` first made every apply of
385
+ * an existing type a failed call on the server. A `create` row that meets a
386
+ * duplicate (`[entity_type_exists]` / `[relationship_type_exists]`, 409) was
387
+ * created concurrently after the plan was read, so it is reconciled as an
388
+ * update. Any other error (e.g. a 422 `[invalid_schema]` validation failure)
389
+ * propagates verbatim — retrying it as an update used to mask the real
390
+ * message behind "Entity type not found" (issue #1177).
387
391
  */
388
392
  private upsertSchemaResource;
389
- upsertEntityType(entity: Omit<DesiredEntityType, "metadata">,
393
+ upsertEntityType(
394
+ /** The plan's verb for this row: whether the type exists remotely. */
395
+ verb: SchemaWriteVerb, entity: Omit<DesiredEntityType, "metadata">,
390
396
  /**
391
397
  * The live type's `metadata_schema` extension keys
392
398
  * ({@link RemoteEntityType.schemaExtras}), from the remote snapshot this
@@ -431,7 +437,9 @@ export declare class ApplyClient {
431
437
  listRelationshipTypeRules(slug: string): Promise<Array<RelationshipRule & {
432
438
  id: number;
433
439
  }>>;
434
- upsertRelationshipType(rel: Omit<DesiredRelationshipType, "metadata">): Promise<UpsertEntityTypeResult>;
440
+ upsertRelationshipType(
441
+ /** The plan's verb for this row: whether the type exists remotely. */
442
+ verb: SchemaWriteVerb, rel: Omit<DesiredRelationshipType, "metadata">): Promise<UpsertEntityTypeResult>;
435
443
  /**
436
444
  * Delete an entity type (code-managed prune). The server soft-deletes and
437
445
  * REFUSES if instances of the type still exist — the data is exempt from
@@ -566,18 +574,14 @@ export declare class ApplyClient {
566
574
  updateFeed(feedId: number, payload: UpdateFeedPayload): Promise<RemoteFeed>;
567
575
  }
568
576
  /**
569
- * Recognise duplicate-name errors from the admin tools without substring
570
- * matching the user-facing message. The server stamps a bracketed code into
571
- * the error message of every duplicate path `apply` upserts through
572
- * (`manage_entity_schema` create / add_rule → `[entity_type_exists]`,
573
- * `[relationship_type_exists]`, `[already_exists]`, all httpStatus 409).
577
+ * Recognise duplicate-name errors from the admin tools by status alone. Every
578
+ * duplicate path `apply` upserts through (`manage_entity_schema` create /
579
+ * add_rule) answers 409, and nothing else those calls return does.
574
580
  *
575
581
  * Anything else is NOT a duplicate. In particular a 422 schema-validation
576
582
  * error (`[invalid_schema]`, e.g. ">4 x-table-column fields") must surface
577
- * verbatim — the old status-only fallback treated any 4xx as "already
578
- * exists", retried with `action: update`, and buried the real message under
579
- * a misleading "Entity type not found" (issue #1177). The 409 fallback stays
580
- * as a belt-and-braces signal for duplicate paths that predate the codes.
583
+ * verbatim: the old any-4xx fallback retried with `action: update` and buried
584
+ * the real message under a misleading "Entity type not found" (issue #1177).
581
585
  */
582
586
  export declare function isDuplicateError(err: ApiError): boolean;
583
587
  interface ResolvedClient {
@@ -380,15 +380,27 @@ export class ApplyClient {
380
380
  }
381
381
  /**
382
382
  * The `manage_entity_schema` admin tool exposes separate `create` / `update`
383
- * actions and surfaces duplicates as a coded 409 (`[entity_type_exists]` /
384
- * `[relationship_type_exists]`). Probe with `create`; on that explicit
385
- * duplicate signal retry with `update`. Any other error (e.g. a 422
386
- * `[invalid_schema]` validation failure) propagates verbatim — retrying it
387
- * as an update used to mask the real message behind "Entity type not
388
- * found" (issue #1177).
383
+ * actions. The plan already knows which one a row needs, so an `update` row
384
+ * goes straight to `update`: probing with `create` first made every apply of
385
+ * an existing type a failed call on the server. A `create` row that meets a
386
+ * duplicate (`[entity_type_exists]` / `[relationship_type_exists]`, 409) was
387
+ * created concurrently after the plan was read, so it is reconciled as an
388
+ * update. Any other error (e.g. a 422 `[invalid_schema]` validation failure)
389
+ * propagates verbatim — retrying it as an update used to mask the real
390
+ * message behind "Entity type not found" (issue #1177).
389
391
  */
390
- async upsertSchemaResource(schemaType, payload) {
392
+ async upsertSchemaResource(verb, schemaType, payload) {
391
393
  const url = `/api/${this.orgSlug}/manage_entity_schema`;
394
+ const update = async () => {
395
+ await this.request("POST", url, {
396
+ schema_type: schemaType,
397
+ action: "update",
398
+ ...payload,
399
+ });
400
+ return { updated: true };
401
+ };
402
+ if (verb === "update")
403
+ return update();
392
404
  try {
393
405
  await this.request("POST", url, {
394
406
  schema_type: schemaType,
@@ -398,18 +410,14 @@ export class ApplyClient {
398
410
  return { created: true };
399
411
  }
400
412
  catch (err) {
401
- if (err instanceof ApiError && isDuplicateError(err)) {
402
- await this.request("POST", url, {
403
- schema_type: schemaType,
404
- action: "update",
405
- ...payload,
406
- });
407
- return { updated: true };
408
- }
413
+ if (err instanceof ApiError && isDuplicateError(err))
414
+ return update();
409
415
  throw err;
410
416
  }
411
417
  }
412
418
  async upsertEntityType(
419
+ /** The plan's verb for this row: whether the type exists remotely. */
420
+ verb,
413
421
  // `metadata` is authoring-only — never sent to the server (see
414
422
  // DesiredEntityType); extra properties on the passed value are ignored.
415
423
  entity,
@@ -548,7 +556,7 @@ export class ApplyClient {
548
556
  if (rulesSource !== undefined || clearFacets?.has("rules")) {
549
557
  payload.rules_source = rulesSource?.sourceCode ?? null;
550
558
  }
551
- return this.upsertSchemaResource("entity_type", payload);
559
+ return this.upsertSchemaResource(verb, "entity_type", payload);
552
560
  }
553
561
  async listRelationshipTypes() {
554
562
  const { body } = await this.request("POST", `/api/${this.orgSlug}/manage_entity_schema`, {
@@ -578,10 +586,12 @@ export class ApplyClient {
578
586
  }));
579
587
  }
580
588
  async upsertRelationshipType(
589
+ /** The plan's verb for this row: whether the type exists remotely. */
590
+ verb,
581
591
  // `metadata` is authoring-only — never sent (see DesiredRelationshipType).
582
592
  rel) {
583
593
  const { rules, ...payload } = rel;
584
- const result = await this.upsertSchemaResource("relationship_type", payload);
594
+ const result = await this.upsertSchemaResource(verb, "relationship_type", payload);
585
595
  // Reconcile rules to exactly the desired set so config is the source of
586
596
  // truth (declarative). Without removing extras, dropping a rule from config
587
597
  // would never take effect AND would churn a perpetual "rules changed"
@@ -1137,28 +1147,16 @@ function mapConnectorDefinitionItem(item) {
1137
1147
  };
1138
1148
  }
1139
1149
  /**
1140
- * Recognise duplicate-name errors from the admin tools without substring
1141
- * matching the user-facing message. The server stamps a bracketed code into
1142
- * the error message of every duplicate path `apply` upserts through
1143
- * (`manage_entity_schema` create / add_rule → `[entity_type_exists]`,
1144
- * `[relationship_type_exists]`, `[already_exists]`, all httpStatus 409).
1150
+ * Recognise duplicate-name errors from the admin tools by status alone. Every
1151
+ * duplicate path `apply` upserts through (`manage_entity_schema` create /
1152
+ * add_rule) answers 409, and nothing else those calls return does.
1145
1153
  *
1146
1154
  * Anything else is NOT a duplicate. In particular a 422 schema-validation
1147
1155
  * error (`[invalid_schema]`, e.g. ">4 x-table-column fields") must surface
1148
- * verbatim — the old status-only fallback treated any 4xx as "already
1149
- * exists", retried with `action: update`, and buried the real message under
1150
- * a misleading "Entity type not found" (issue #1177). The 409 fallback stays
1151
- * as a belt-and-braces signal for duplicate paths that predate the codes.
1156
+ * verbatim: the old any-4xx fallback retried with `action: update` and buried
1157
+ * the real message under a misleading "Entity type not found" (issue #1177).
1152
1158
  */
1153
1159
  export function isDuplicateError(err) {
1154
- if (typeof err.status !== "number")
1155
- return false;
1156
- const message = err.message.toLowerCase();
1157
- if (message.includes("[entity_type_exists]") ||
1158
- message.includes("[relationship_type_exists]") ||
1159
- message.includes("[already_exists]")) {
1160
- return true;
1161
- }
1162
1160
  return err.status === 409;
1163
1161
  }
1164
1162
  export async function resolveApplyClient(opts) {
@@ -250,6 +250,8 @@ export async function devCommand(cwd, options = {}) {
250
250
  envVars.WEB_DIST_DIR ||
251
251
  (existsSync(bundledWebDistPath) ? bundledWebDistPath : undefined);
252
252
  const childEnv = {
253
+ // Reported by the anonymous install ping; the project .env may override.
254
+ LOBU_DEPLOY_KIND: "cli",
253
255
  ...mergedEnv,
254
256
  ...runtime.env,
255
257
  LOBU_DEV_PROJECT_PATH: projectPath,
@@ -20,6 +20,12 @@ export function linkedInInboxProgram() {
20
20
  function fail(message) {
21
21
  throw new Error(`LinkedIn inbox: ${message}`);
22
22
  }
23
+ // A stop reason is durable evidence: keep the underlying failure instead of
24
+ // reporting only that the listener could not attach.
25
+ function errorDetail(error) {
26
+ const message = error instanceof Error ? error.message : String(error);
27
+ return clean(message.replace(/^LinkedIn inbox: /, "")).slice(0, 300);
28
+ }
23
29
  function headers() {
24
30
  const cookie = document.cookie
25
31
  .split("; ")
@@ -1018,6 +1024,7 @@ export function linkedInInboxProgram() {
1018
1024
  clearTimeout(listener.timer);
1019
1025
  window.removeEventListener("message", listener.onReply);
1020
1026
  listeners.delete(listener.id);
1027
+ listener.stopReason = reason;
1021
1028
  if (reason)
1022
1029
  post(listener, {
1023
1030
  id: `linkedin:${listener.feed}:source-observation-error`,
@@ -1053,10 +1060,10 @@ export function linkedInInboxProgram() {
1053
1060
  listener.batches = await batchesOf(feed, checkpoint, result);
1054
1061
  }
1055
1062
  if (active(listener)) post(listener, listener.batches[0]);
1056
- } catch {
1063
+ } catch (error) {
1057
1064
  stop(
1058
1065
  listener,
1059
- "LinkedIn source observation failed; reconnect to recover without skipping changes"
1066
+ `LinkedIn source observation failed (${errorDetail(error)}); reconnect to recover without skipping changes`
1060
1067
  );
1061
1068
  }
1062
1069
  if (active(listener))
@@ -1246,7 +1253,10 @@ export function linkedInInboxProgram() {
1246
1253
  window.addEventListener("message", listener.onReply);
1247
1254
  await poll(listener);
1248
1255
  if (!listeners.has(listener.id))
1249
- fail("Unable to attach source subscription");
1256
+ fail(
1257
+ "Unable to attach source subscription: " +
1258
+ (listener.stopReason ?? "listener stopped")
1259
+ );
1250
1260
  return { listening: true };
1251
1261
  }
1252
1262
  globalThis[key] = { version, read, listen, send, detach };
@@ -72,9 +72,9 @@ interface PgQueryConfig {
72
72
  }
73
73
 
74
74
  interface PgCheckpoint {
75
- /** Last cursor value seen, serialized (ISO for dates, String() otherwise). */
75
+ /** Native PostgreSQL text for the last cursor, preserving timestamp microseconds. */
76
76
  last_cursor?: string;
77
- /** Last primary-key value seen, serialized. */
77
+ /** Native PostgreSQL text for the last primary-key value. */
78
78
  last_pk?: string;
79
79
  }
80
80
 
@@ -362,11 +362,6 @@ async function setReadOnly(
362
362
  await tx.unsafe(`SET LOCAL statement_timeout = ${Math.floor(timeoutMs)}`);
363
363
  }
364
364
 
365
- function toCheckpointValue(v: unknown): string {
366
- if (v instanceof Date) return v.toISOString();
367
- return String(v);
368
- }
369
-
370
365
  function toDate(v: unknown): Date | null {
371
366
  if (v instanceof Date) return v;
372
367
  if (typeof v === 'number') return new Date(v);
@@ -386,7 +381,7 @@ export default class PostgresConnector extends ConnectorRuntime {
386
381
  name: 'PostgreSQL',
387
382
  description:
388
383
  'Bring your own PostgreSQL database as memory, read a configured feed directly, or run governed connection-level SQL.',
389
- version: '1.1.0',
384
+ version: '1.1.1',
390
385
  queryCapabilities: { exactMatch: true, verbatimExactMatch: true, exactMatchAny: true, selection: true },
391
386
  faviconDomain: 'postgresql.org',
392
387
  authSchema: {
@@ -449,7 +444,7 @@ export default class PostgresConnector extends ConnectorRuntime {
449
444
  // Everything runs inside ONE read-only transaction — the probe included —
450
445
  // so even a crafted query (e.g. a data-modifying CTE that slipped past
451
446
  // validation) cannot write while we introspect or read.
452
- const rows = (await sql.begin(async (tx) => {
447
+ const rows = await sql.begin(async (tx) => {
453
448
  await setReadOnly(tx, timeoutMs);
454
449
 
455
450
  // 1. Probe column types so the string-serialized checkpoint can be re-cast.
@@ -470,10 +465,28 @@ export default class PostgresConnector extends ConnectorRuntime {
470
465
  };
471
466
  const curCast = castFor(cursorCol);
472
467
  const pkCast = castFor(pkCol);
473
-
474
- // 2. Build the keyset-paginated query.
468
+ // Bind as text before the native cast: binding directly as a timestamp
469
+ // makes postgres.js serialize the string through Date, losing precision again.
470
+ const curBound = curCast ? `$1::text${curCast}` : '$1';
471
+ const pkBound = pkCast ? `$2::text${pkCast}` : '$2';
472
+
473
+ // Keep checkpoints separate from driver-decoded payloads: Date loses
474
+ // microseconds, which can make a capped page repeat forever. Native
475
+ // text round-trips through the same type casts used by the predicate.
476
+ // Choose unused aliases so source columns with these names survive.
477
+ const checkpointAlias = (base: string): string => {
478
+ let name = base;
479
+ let suffix = 0;
480
+ while (colByName.has(name)) name = `${base}_${++suffix}`;
481
+ return name;
482
+ };
483
+ const cursorAlias = checkpointAlias('__lobu_checkpoint_cursor');
484
+ const pkAlias = checkpointAlias('__lobu_checkpoint_pk');
475
485
  const colCur = `q."${cursorCol}"`;
476
486
  const colPk = `q."${pkCol}"`;
487
+ const projection = `q.*, ${colCur}::text AS "${cursorAlias}", ${colPk}::text AS "${pkAlias}"`;
488
+
489
+ // 2. Build the keyset-paginated query.
477
490
  const haveCursor =
478
491
  checkpoint.last_cursor !== undefined && checkpoint.last_pk !== undefined;
479
492
 
@@ -483,18 +496,25 @@ export default class PostgresConnector extends ConnectorRuntime {
483
496
  // security-allowed: colCur/colPk are assertIdentifier-validated + quoted;
484
497
  // the cursor/pk values are bound parameters ($1/$2/$3), not interpolated.
485
498
  wrapped =
486
- `SELECT * FROM (\n${baseSql}\n) q\n` +
487
- `WHERE (${colCur} > $1${curCast} ` +
488
- `OR (${colCur} = $1${curCast} AND ${colPk} > $2${pkCast}))\n` +
499
+ `SELECT ${projection} FROM (\n${baseSql}\n) q\n` +
500
+ `WHERE (${colCur} > ${curBound} ` +
501
+ `OR (${colCur} = ${curBound} AND ${colPk} > ${pkBound}))\n` +
489
502
  `ORDER BY ${colCur}, ${colPk} LIMIT $3`;
490
503
  params = [checkpoint.last_cursor, checkpoint.last_pk, limit];
491
504
  } else {
492
- wrapped = `SELECT * FROM (\n${baseSql}\n) q\nORDER BY ${colCur}, ${colPk} LIMIT $1`;
505
+ wrapped = `SELECT ${projection} FROM (\n${baseSql}\n) q\nORDER BY ${colCur}, ${colPk} LIMIT $1`;
493
506
  params = [limit];
494
507
  }
495
508
 
496
- return tx.unsafe(wrapped, params as never[]);
497
- })) as unknown as Array<Record<string, unknown>>;
509
+ const page = await tx.unsafe(wrapped, params as never[]);
510
+ return page.map((source) => {
511
+ const { [cursorAlias]: cursor, [pkAlias]: pk, ...row } = source;
512
+ return {
513
+ row,
514
+ checkpoint: { last_cursor: String(cursor), last_pk: String(pk) } satisfies PgCheckpoint,
515
+ };
516
+ });
517
+ });
498
518
 
499
519
  // Namespace origin_ids by the feed INSTANCE, not the feed key: every
500
520
  // postgres feed shares feedKey 'query', so two feeds on one connection with
@@ -508,14 +528,9 @@ export default class PostgresConnector extends ConnectorRuntime {
508
528
  // so a run cut short keeps every slice it committed.
509
529
  for (let start = 0; start < rows.length; start += COMMIT_ROWS) {
510
530
  const slice = rows.slice(start, start + COMMIT_ROWS);
511
- const last = slice[slice.length - 1];
512
- const sliceCheckpoint: PgCheckpoint = {
513
- last_cursor: toCheckpointValue(last[cursorCol]),
514
- last_pk: toCheckpointValue(last[pkCol]),
515
- };
516
531
  await ctx.commit(
517
- slice.map((row) => this.rowToEvent(originPrefix, row, config, cursorCol, pkCol)),
518
- sliceCheckpoint as unknown as Record<string, unknown>
532
+ slice.map(({ row }) => this.rowToEvent(originPrefix, row, config, cursorCol, pkCol)),
533
+ slice[slice.length - 1].checkpoint
519
534
  );
520
535
  }
521
536
 
@@ -9,11 +9,12 @@
9
9
  * module shapes are load-bearing.
10
10
  *
11
11
  * How it gets into the page: the connector serialises this function with
12
- * Function.prototype.toString() and runs `(<source>)()` through the generic
13
- * chrome `evaluate` op, which executes in the page's MAIN world. That is the
14
- * same mechanism the extension used with chrome.scripting.executeScript({func}),
15
- * minus the extension. The adapter is idempotent and self-versioning
16
- * (__owlettoWhatsAppAdapterV1 + ADAPTER_VERSION), so re-injecting is safe.
12
+ * Function.prototype.toString() and runs `(<source>)("<version>")` through the
13
+ * generic chrome `evaluate` op, which executes in the page's MAIN world. That is
14
+ * the same mechanism the extension used with chrome.scripting.executeScript({func}),
15
+ * minus the extension. The version is a fingerprint of that exact source,
16
+ * computed by the connector (WHATSAPP_ADAPTER_INSTALL in whatsapp-web-helpers.ts),
17
+ * so the adapter is idempotent per program text and re-injecting is safe.
17
18
  *
18
19
  * INVARIANT — every binding this function needs must live INSIDE it. A constant
19
20
  * hoisted to module scope would vanish from toString() output and the page would
@@ -26,14 +27,14 @@
26
27
  * and cost a @ts-nocheck plus a set of `declare const` shims.
27
28
  */
28
29
 
29
- export function whatsAppWebAdapterProgram() {
30
+ export function whatsAppWebAdapterProgram(adapterVersion) {
30
31
  const GLOBAL_KEY = "__owlettoWhatsAppAdapterV1";
31
- // Bumped whenever the program changes at all, not only its RPC surface.
32
- // A WhatsApp tab outlives a deploy, so the resident adapter is replaced only
33
- // when this number moves: shipping a fix under the old number leaves every
34
- // already-open tab running the previous code with nothing to show for it.
35
- // Keep in lockstep with WHATSAPP_ADAPTER_VERSION in whatsapp-web-helpers.ts.
36
- const ADAPTER_VERSION = 27;
32
+ // A WhatsApp tab outlives a deploy, so a resident adapter is replaced only
33
+ // when this version differs. The connector derives it from this program's
34
+ // own source, so any change to the program replaces it with no manual bump.
35
+ const ADAPTER_VERSION = adapterVersion;
36
+ if (typeof ADAPTER_VERSION !== "string" || !ADAPTER_VERSION)
37
+ throw new Error("WhatsApp adapter must be installed with its content version");
37
38
  const SOURCE_ERROR_ID = "whatsapp-web:source-observation-error";
38
39
  // Storage rows one `chat:` read examines; a whole personal history is
39
40
  // usually under this, so one read answers.
@@ -340,6 +341,23 @@ export function whatsAppWebAdapterProgram() {
340
341
  return false;
341
342
  }
342
343
 
344
+ /** Actual self-mentions, excluding quotes of our messages and other people. */
345
+ async function ownMentionJids(row) {
346
+ const mentions = Array.isArray(row.mentionedJidList) ? row.mentionedJidList : [];
347
+ if (mentions.length === 0) return [];
348
+ const own = await ownJids();
349
+ const mentioned = new Set();
350
+ for (const value of mentions) {
351
+ const candidate = await canonicalJid(value);
352
+ for (const jid of [candidate.jid, candidate.lid]) {
353
+ if (typeof jid === "string" && /^[0-9]{5,32}@(s\.whatsapp\.net|lid)$/.test(jid) && own.has(jid)) {
354
+ mentioned.add(jid);
355
+ }
356
+ }
357
+ }
358
+ return [...mentioned].slice(0, 2);
359
+ }
360
+
343
361
  /**
344
362
  * Module names for each write op, most-current first.
345
363
  *
@@ -386,7 +404,7 @@ export function whatsAppWebAdapterProgram() {
386
404
  const key = row.id ?? model?.id;
387
405
  const id = rawId(key);
388
406
  const remote = widString(key?.remote ?? row.chatId ?? (key?.fromMe ? row.to : row.from));
389
- if (!id || !remote || remote === "status@broadcast" || remote.endsWith("@newsletter") || SYSTEM_TYPES.has(row.type)) return null;
407
+ if (!id || !remote || isBroadcastOnlyJid(remote) || SYSTEM_TYPES.has(row.type)) return null;
390
408
  return { id, timestamp: unixSeconds(row), is_group: remote.endsWith("@g.us") };
391
409
  }
392
410
 
@@ -496,6 +514,21 @@ export function whatsAppWebAdapterProgram() {
496
514
  return [];
497
515
  }
498
516
 
517
+ /**
518
+ * Chats that are feeds, not conversations: status updates, channels, and the
519
+ * read-only service-announcement chat (`0@c.us`, canonically
520
+ * `0@s.whatsapp.net`). Nobody can reply in them, so they never surface as
521
+ * messages and a send to one is refused instead of hanging.
522
+ */
523
+ function isBroadcastOnlyJid(jid) {
524
+ return (
525
+ jid === "status@broadcast" ||
526
+ jid.endsWith("@newsletter") ||
527
+ jid === "0@c.us" ||
528
+ jid === "0@s.whatsapp.net"
529
+ );
530
+ }
531
+
499
532
  function widString(value) {
500
533
  if (typeof value === "string") return value;
501
534
  if (!value) return null;
@@ -670,11 +703,7 @@ export function whatsAppWebAdapterProgram() {
670
703
  const authorRaw = row.author ?? key?.participant;
671
704
  const chatRaw = remoteRaw ?? (fromMe ? toRaw : fromRaw);
672
705
  const chat = await canonicalJid(chatRaw);
673
- if (
674
- !chat.jid ||
675
- chat.jid === "status@broadcast" ||
676
- chat.jid.endsWith("@newsletter")
677
- ) {
706
+ if (!chat.jid || isBroadcastOnlyJid(chat.jid)) {
678
707
  return null;
679
708
  }
680
709
  const senderRaw = fromMe ? fromRaw : (authorRaw ?? fromRaw);
@@ -723,6 +752,7 @@ export function whatsAppWebAdapterProgram() {
723
752
  media_filename: row.filename ?? row.fileName ?? row.title ?? null,
724
753
  quoted_id: quotedId,
725
754
  mentions_me: await mentionsOwnAccount(row),
755
+ own_mention_jids: await ownMentionJids(row),
726
756
  is_forwarded: Boolean(row.isForwarded || Number(row.forwardingScore) > 0),
727
757
  is_starred: Boolean(row.star ?? row.isStarred),
728
758
  is_system_event: SYSTEM_TYPES.has(type),
@@ -1091,7 +1121,7 @@ export function whatsAppWebAdapterProgram() {
1091
1121
  const key = keyModule.fromString(row.id);
1092
1122
  if (exactId !== null && key.id !== exactId) continue;
1093
1123
  const remote = widString(key.remote);
1094
- if (remote === "status@broadcast" || remote.endsWith("@newsletter")) continue;
1124
+ if (isBroadcastOnlyJid(remote)) continue;
1095
1125
  const isGroup = remote.endsWith("@g.us");
1096
1126
  if ((chatFilter === "group" && !isGroup) || (chatFilter === "individual" && isGroup)) continue;
1097
1127
  selected.push(row);
@@ -1161,8 +1191,7 @@ export function whatsAppWebAdapterProgram() {
1161
1191
  const jid = (
1162
1192
  await canonicalJid(modelData(candidate).id ?? candidate?.id)
1163
1193
  ).jid;
1164
- if (!jid || jid === "status@broadcast" || jid.endsWith("@newsletter"))
1165
- continue;
1194
+ if (!jid || isBroadcastOnlyJid(jid)) continue;
1166
1195
  const isGroup = jid.endsWith("@g.us");
1167
1196
  if (chatFilter === "group" ? isGroup : !isGroup) {
1168
1197
  scopedChats.push(candidate);
@@ -1294,6 +1323,9 @@ export function whatsAppWebAdapterProgram() {
1294
1323
  async function sendMessage(input, account) {
1295
1324
  const collections = requireFirst(["WAWebCollections"]);
1296
1325
  const chat = await resolveChat(collections, input);
1326
+ const chatJid = (await canonicalJid(modelData(chat).id ?? chat?.id)).jid;
1327
+ if (chatJid && isBroadcastOnlyJid(chatJid))
1328
+ throw new Error(`WhatsApp chat ${chatJid} does not accept messages`);
1297
1329
  const text = String(input?.text ?? "");
1298
1330
  if (!text.trim()) throw new Error("text is required");
1299
1331
  const module = requireFirst(SEND_MODULES);
@@ -1531,7 +1563,7 @@ export function whatsAppWebAdapterProgram() {
1531
1563
 
1532
1564
  async function invoke(request) {
1533
1565
  try {
1534
- if (Number(request?.adapter_version) !== ADAPTER_VERSION) {
1566
+ if (request?.adapter_version !== ADAPTER_VERSION) {
1535
1567
  return {
1536
1568
  ok: false,
1537
1569
  error: {
@@ -7,6 +7,7 @@
7
7
  * that live reads and searches return. These helpers do not ingest events.
8
8
  */
9
9
 
10
+ import { whatsAppWebAdapterProgram } from "./whatsapp-web-adapter.js";
10
11
  import {
11
12
  normalizeWhatsAppUserJid,
12
13
  WHATSAPP_GROUP_SUFFIX,
@@ -14,7 +15,42 @@ import {
14
15
  WHATSAPP_PHONE_SERVER,
15
16
  } from "./whatsapp-web-identity.js";
16
17
 
17
- export const WHATSAPP_ADAPTER_VERSION = 27;
18
+ /**
19
+ * A short fingerprint of a program's source (cyrb53, base 36). It only has to
20
+ * tell two program texts apart, so a non-cryptographic hash is enough, and it
21
+ * runs synchronously wherever the connector does.
22
+ */
23
+ function sourceFingerprint(source: string): string {
24
+ let h1 = 0xdeadbeef;
25
+ let h2 = 0x41c6ce57;
26
+ for (let index = 0; index < source.length; index++) {
27
+ const code = source.charCodeAt(index);
28
+ h1 = Math.imul(h1 ^ code, 2654435761);
29
+ h2 = Math.imul(h2 ^ code, 1597334677);
30
+ }
31
+ h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
32
+ h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
33
+ h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
34
+ h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
35
+ return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36);
36
+ }
37
+
38
+ /**
39
+ * The page adapter's expression and version, both derived from the program
40
+ * text this build injects. An open tab keeps a resident adapter only while the
41
+ * versions match, so any change to the program reaches every open tab without
42
+ * a hand-maintained number two concurrent changes could both claim.
43
+ */
44
+ export function whatsAppAdapterInstall(
45
+ source: string = whatsAppWebAdapterProgram.toString()
46
+ ): { version: string; expression: string } {
47
+ const version = `src-${sourceFingerprint(source)}`;
48
+ return { version, expression: `(${source})(${JSON.stringify(version)})` };
49
+ }
50
+
51
+ const ADAPTER_INSTALL = whatsAppAdapterInstall();
52
+ export const WHATSAPP_ADAPTER_VERSION = ADAPTER_INSTALL.version;
53
+ export const WHATSAPP_ADAPTER_INSTALL = ADAPTER_INSTALL.expression;
18
54
  export const WHATSAPP_ORIGIN = "https://web.whatsapp.com";
19
55
 
20
56
  export type ChatFilter = "all" | "individual" | "group";
@@ -39,6 +75,8 @@ export interface WhatsAppMessage {
39
75
  quoted_id?: string | null;
40
76
  /** The message @mentions the account or quotes one of its messages. */
41
77
  mentions_me?: boolean;
78
+ /** Verified phone/LID identities of the paired account actually @mentioned. */
79
+ own_mention_jids?: string[];
42
80
  edited?: boolean;
43
81
  edit_timestamp?: number | null;
44
82
  revoked?: boolean;
@@ -114,10 +152,13 @@ export function normalizeRelayedMessage(
114
152
  const timestamp = timestampOf(raw);
115
153
  if (timestamp <= 0) return null;
116
154
  const chatJid = canonicalizeJid(raw.chat_jid);
155
+ // Status updates, channels and the read-only service-announcement chat
156
+ // (`0@c.us`) are feeds nobody can reply in; the page adapter drops them too.
117
157
  if (
118
158
  !chatJid ||
119
159
  chatJid === "status@broadcast" ||
120
- chatJid.endsWith("@newsletter")
160
+ chatJid.endsWith("@newsletter") ||
161
+ chatJid === `0@${WHATSAPP_PHONE_SERVER}`
121
162
  ) {
122
163
  return null;
123
164
  }
@@ -136,6 +177,11 @@ export function normalizeRelayedMessage(
136
177
  from_me: fromMe,
137
178
  timestamp,
138
179
  occurred_at: new Date(timestamp * 1000).toISOString(),
180
+ own_mention_jids: Array.isArray(raw.own_mention_jids)
181
+ ? [...new Set(raw.own_mention_jids.map((jid: unknown) =>
182
+ typeof jid === "string" ? normalizeWhatsAppUserJid(jid) : null,
183
+ ).filter((jid: string | null): jid is string => jid !== null))].slice(0, 2)
184
+ : [],
139
185
  };
140
186
  }
141
187