@lobu/runtime-device 22.0.0 → 23.0.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 (77) hide show
  1. package/dist/component.json +1 -1
  2. package/dist/dependencies.bun.lock +3 -3
  3. package/npm-shrinkwrap.json +15 -15
  4. package/package.json +1 -1
  5. package/vendor/cli/dist/bundled-skills/lobu/SKILL.md +1 -1
  6. package/vendor/cli/dist/connectors/README.md +14 -1
  7. package/vendor/cli/dist/connectors/google_gmail.ts +1 -0
  8. package/vendor/cli/dist/connectors/jira-helpers.ts +313 -0
  9. package/vendor/cli/dist/connectors/jira.ts +29 -221
  10. package/vendor/cli/dist/connectors/linkedin-browser-script.ts +13 -0
  11. package/vendor/cli/dist/connectors/linkedin-draft-page.js +104 -0
  12. package/vendor/cli/dist/connectors/linkedin-drafts.ts +73 -0
  13. package/vendor/cli/dist/connectors/linkedin-identity.ts +32 -0
  14. package/vendor/cli/dist/connectors/linkedin-inbox-adapter.js +1185 -0
  15. package/vendor/cli/dist/connectors/linkedin-inbox.ts +156 -0
  16. package/vendor/cli/dist/connectors/linkedin-like.ts +114 -0
  17. package/vendor/cli/dist/connectors/linkedin-takeout.ts +577 -0
  18. package/vendor/cli/dist/connectors/linkedin.ts +3998 -0
  19. package/vendor/cli/dist/connectors/takeout-utils.ts +240 -0
  20. package/vendor/cli/dist/connectors/tiktok-inbox-page.js +9 -9
  21. package/vendor/cli/dist/connectors/tiktok-web-page.js +21 -15
  22. package/vendor/cli/dist/connectors/tiktok_web.ts +63 -30
  23. package/vendor/cli/dist/connectors/whatsapp-web-adapter.js +64 -564
  24. package/vendor/cli/dist/connectors/whatsapp-web-helpers.ts +30 -444
  25. package/vendor/cli/dist/connectors/whatsapp-web-identity.ts +20 -6
  26. package/vendor/cli/dist/connectors/whatsapp_web.ts +45 -645
  27. package/vendor/cli/dist/connectors/x-dm-adapter.js +556 -0
  28. package/vendor/cli/dist/connectors/x-identity.ts +31 -2
  29. package/vendor/cli/dist/connectors/x.ts +266 -120
  30. package/vendor/cli/dist/runtime-components.json +4 -4
  31. package/vendor/cli/dist/templates/AGENTS.md.tmpl +2 -2
  32. package/vendor/cli/package.json +1 -1
  33. package/vendor/connector-sdk/dist/browser-handoff-banner.d.ts +39 -0
  34. package/vendor/connector-sdk/dist/browser-handoff-banner.js +175 -0
  35. package/vendor/connector-sdk/dist/browser-source-read.d.ts +52 -0
  36. package/vendor/connector-sdk/dist/browser-source-read.js +107 -0
  37. package/vendor/connector-sdk/dist/connector-types.d.ts +10 -0
  38. package/vendor/connector-sdk/dist/index.d.ts +3 -0
  39. package/vendor/connector-sdk/dist/index.js +2 -0
  40. package/vendor/connector-sdk/package.json +9 -1
  41. package/vendor/connector-worker/dist/agent-turn/guest-entry.js +15 -6
  42. package/vendor/connector-worker/dist/agent-turn/guest.bundle.js +288 -148
  43. package/vendor/connector-worker/dist/daemon/automation.d.ts +4 -11
  44. package/vendor/connector-worker/dist/daemon/automation.js +6 -6
  45. package/vendor/connector-worker/dist/daemon/client.d.ts +2 -4
  46. package/vendor/connector-worker/dist/daemon/client.js +0 -3
  47. package/vendor/connector-worker/dist/daemon/executor.js +0 -3
  48. package/vendor/connector-worker/dist/daemon/mac-device-daemon.d.ts +4 -3
  49. package/vendor/connector-worker/dist/daemon/mac-device-daemon.js +4 -10
  50. package/vendor/connector-worker/dist/daemon/native-bridge/executor.js +27 -25
  51. package/vendor/connector-worker/dist/daemon/start.js +3 -15
  52. package/vendor/connector-worker/dist/daemon/terminal-failure.js +0 -11
  53. package/vendor/connector-worker/dist/daemon/worker-id.d.ts +17 -0
  54. package/vendor/connector-worker/dist/daemon/worker-id.js +22 -0
  55. package/vendor/connector-worker/dist/mac-device-daemon.js +14 -26
  56. package/vendor/connector-worker/package.json +1 -1
  57. package/vendor/core/dist/capabilities.d.ts +1 -1
  58. package/vendor/core/dist/capabilities.js +0 -1
  59. package/vendor/core/dist/connector-query-errors.d.ts +32 -0
  60. package/vendor/core/dist/connector-query-errors.js +41 -1
  61. package/vendor/core/dist/contracts/tools/manage-conversations.js +2 -2
  62. package/vendor/core/dist/contracts/tools/manage-feeds.d.ts +14 -0
  63. package/vendor/core/dist/contracts/tools/manage-feeds.js +17 -0
  64. package/vendor/core/dist/contracts/worker/protocol.d.ts +8 -131
  65. package/vendor/core/dist/contracts/worker/protocol.js +9 -61
  66. package/vendor/core/dist/device-pin-tombstones.d.ts +44 -0
  67. package/vendor/core/dist/device-pin-tombstones.js +57 -0
  68. package/vendor/core/dist/index.d.ts +2 -1
  69. package/vendor/core/dist/index.js +1 -0
  70. package/vendor/core/dist/logger.js +10 -25
  71. package/vendor/core/dist/utils/diagnostic-scrubber.js +258 -21
  72. package/vendor/core/dist/worker/wire.d.ts +0 -10
  73. package/vendor/core/package.json +6 -6
  74. package/vendor/connector-worker/dist/daemon/device-chat.d.ts +0 -9
  75. package/vendor/connector-worker/dist/daemon/device-chat.js +0 -61
  76. package/vendor/core/dist/contracts/worker/device-chat.d.ts +0 -4
  77. package/vendor/core/dist/contracts/worker/device-chat.js +0 -26
@@ -0,0 +1,3998 @@
1
+ /**
2
+ * LinkedIn Connector
3
+ *
4
+ * Built-in connector spanning live browser feeds and local export readers:
5
+ *
6
+ * • LIVE feeds (home_feed, company_updates, jobs) — scraped via the paired
7
+ * Owletto Chrome extension's network-intercept / content-script primitives.
8
+ * The extension runs inside the user's real Chrome session — no Playwright,
9
+ * no cookie cache, no `--remote-debugging-port` plumbing. We attach the CDP
10
+ * Network domain in the user's signed-in tab, drive scroll pagination, and
11
+ * parse the Voyager API responses the page emits. Auth is implicit: the user
12
+ * is already signed into linkedin.com in the paired Chrome. If no online
13
+ * Owletto extension is reachable in the connection's org, these syncs fail
14
+ * fast with a clear "no paired Owletto extension" error.
15
+ *
16
+ * • TAKEOUT feeds (connections, messages, invitations, applied_jobs, profile,
17
+ * companies, learning, events, endorsements, media) — read from the user's
18
+ * local LinkedIn Data Export CSV files through an injected reader. The
19
+ * platform does not currently supply that reader; those feeds fail with
20
+ * a missing-capability error until a filesystem-capable caller supplies it.
21
+ *
22
+ * Folding both into ONE connector (key "linkedin") means a single connection
23
+ * dedups all people on the shared `linkedin_slug`/`email` identity: a person met
24
+ * live and a person in the CSV export collapse to the same entity.
25
+ *
26
+ * Write handoff: `prepare_comment` waits for the user to open the exact post,
27
+ * fills that page's comment composer, and stops. The human clicks Post
28
+ * themselves — Lobu never submits the comment.
29
+ *
30
+ * Closed-loop (optional): `verify_staged_comment` re-opens the post and scrapes
31
+ * comments to check whether the human actually posted the draft.
32
+ * Staging ≠ verified posted.
33
+ */
34
+
35
+ import {
36
+ buildInjectHandoffBannerExpression as buildSharedHandoffBannerExpression,
37
+ requireBrowser,
38
+ truncateHandoffReason as truncateSharedHandoffReason,
39
+ type ActionContext,
40
+ type ActionResult,
41
+ type ChromeActionDispatcher,
42
+ type RuntimeConnectorDefinition,
43
+ ConnectorRuntime,
44
+ calculateEngagementScore,
45
+ type EventAttributionRule,
46
+ type EventEnvelope,
47
+ extensionDomScrape,
48
+ extensionNetworkSync,
49
+ IDENTITY,
50
+ type SyncContext,
51
+ type SyncResult,
52
+ } from "@lobu/connector-sdk";
53
+ import {
54
+ LINKEDIN_IDENTITY,
55
+ normalizeLinkedInMemberId,
56
+ normalizeLinkedInSlug,
57
+ } from "./linkedin-identity.js";
58
+ import { setLinkedInLike } from "./linkedin-like.js";
59
+ import {
60
+ readCompanyFollows,
61
+ readConnections,
62
+ readEndorsements,
63
+ readEvents,
64
+ readInvitations,
65
+ readJobs,
66
+ readLearning,
67
+ readMessages,
68
+ readProfile,
69
+ readRichMedia,
70
+ type TakeoutCsvReader,
71
+ } from "./linkedin-takeout.js";
72
+ import {
73
+ batchSize,
74
+ type LocalTakeoutConfig,
75
+ maxEventCursor,
76
+ takeBatch,
77
+ } from "./takeout-utils.js";
78
+ import {
79
+ observeLinkedInInbox,
80
+ readLinkedInInbox,
81
+ sendLinkedInMessage,
82
+ type LinkedInInboxCheckpoint,
83
+ } from "./linkedin-inbox.js";
84
+ import { prepareLinkedInDraft } from "./linkedin-drafts.js";
85
+ import { LINKEDIN_HOME_URL } from "./linkedin-browser-script.js";
86
+
87
+ // ── Types ──────────────────────────────────────────────────────
88
+
89
+ /**
90
+ * Merged checkpoint. Carries the live cursor fields (last_post_id / last_job_id
91
+ * / last_timestamp) AND every takeout feed's `last_*_timestamp` cursor. The
92
+ * takeout `jobs` feed was renamed to `applied_jobs`, so its cursor is
93
+ * `last_applied_jobs_timestamp` (no ambiguity with the live `jobs` feed, which
94
+ * checkpoints on last_job_id/last_timestamp). No production checkpoint exists,
95
+ * so the rename is safe.
96
+ */
97
+ interface LinkedInCheckpoint extends LinkedInInboxCheckpoint {
98
+ // Live feed cursors.
99
+ last_post_id?: string;
100
+ last_job_id?: string;
101
+ last_timestamp?: string;
102
+ // Takeout feed cursors.
103
+ last_messages_timestamp?: string;
104
+ last_connections_timestamp?: string;
105
+ last_invitations_timestamp?: string;
106
+ last_applied_jobs_timestamp?: string;
107
+ last_profile_timestamp?: string;
108
+ last_companies_timestamp?: string;
109
+ last_learning_timestamp?: string;
110
+ last_events_timestamp?: string;
111
+ last_endorsements_timestamp?: string;
112
+ last_media_timestamp?: string;
113
+ }
114
+
115
+ /**
116
+ * Merged config. Permissive superset of the live config (company_url,
117
+ * max_scrolls) and the takeout config (takeout_dir, batch_size). A given feed
118
+ * only reads the keys relevant to its source.
119
+ */
120
+ interface LinkedInConfig extends LocalTakeoutConfig {
121
+ company_url?: string;
122
+ max_scrolls?: number;
123
+ min_scrolls?: number;
124
+ }
125
+
126
+ interface LinkedInPost {
127
+ id: string;
128
+ text: string;
129
+ author: string;
130
+ authorHeadline?: string;
131
+ /** Immutable `urn:li:fsd_profile:<id>` tail — primary identity when present. */
132
+ authorMemberId?: string;
133
+ /** Canonical `/in/<slug>` vanity id — soft identity, bridges to takeout. */
134
+ authorSlug?: string;
135
+ likes: number;
136
+ comments: number;
137
+ shares: number;
138
+ publishedAt: Date;
139
+ }
140
+
141
+ interface LinkedInJob {
142
+ id: string;
143
+ title: string;
144
+ location: string;
145
+ postedAt: Date;
146
+ url: string;
147
+ description?: string;
148
+ }
149
+
150
+ function normalizeCheckpointPostId(postId?: string): string | undefined {
151
+ if (!postId) return undefined;
152
+ return postId.startsWith("li_post_")
153
+ ? postId.slice("li_post_".length)
154
+ : postId;
155
+ }
156
+
157
+ // ── Identity attributions ──────────────────────────────────────
158
+
159
+ /**
160
+ * Link a `connection` event to the connected person via their canonical
161
+ * `linkedin_slug` (extracted from the profile URL) plus their `email`. Neither
162
+ * is `primary` — they match equal-weight cross-channel until a stable primary
163
+ * id arrives from the live connector. The full URL is kept as a display trait,
164
+ * not an identity (case/URL noise would fork the entity). Connections ARE the
165
+ * user's network, so we mint (`autoCreate`) a person per row.
166
+ */
167
+ const LINKEDIN_CONNECTION_ATTRIBUTIONS: EventAttributionRule[] = [
168
+ {
169
+ role: "about",
170
+ autoCreate: true,
171
+ target: {
172
+ entityType: "person",
173
+ titlePath: "author_name",
174
+ identities: [
175
+ {
176
+ namespace: LINKEDIN_IDENTITY.SLUG,
177
+ eventPath: "metadata.linkedin_slug",
178
+ },
179
+ { namespace: IDENTITY.EMAIL, eventPath: "metadata.email" },
180
+ ],
181
+ },
182
+ traits: {
183
+ linkedin_url: {
184
+ eventPath: "metadata.linkedin_url",
185
+ mergeStrategy: "prefer_non_empty",
186
+ },
187
+ company: {
188
+ eventPath: "metadata.company",
189
+ mergeStrategy: "prefer_non_empty",
190
+ },
191
+ position: {
192
+ eventPath: "metadata.position",
193
+ mergeStrategy: "prefer_non_empty",
194
+ },
195
+ },
196
+ },
197
+ ];
198
+
199
+ /**
200
+ * Link a `message` event to its sender (the counterparty) via the
201
+ * `linkedin_slug` extracted from their profile URL, plus display name. Real
202
+ * counterparties, so mint on no match.
203
+ */
204
+ const LINKEDIN_MESSAGE_ATTRIBUTIONS: EventAttributionRule[] = [
205
+ {
206
+ role: "authored_by",
207
+ autoCreate: true,
208
+ target: {
209
+ entityType: "person",
210
+ titlePath: "metadata.from",
211
+ identities: [
212
+ {
213
+ namespace: LINKEDIN_IDENTITY.SLUG,
214
+ eventPath: "metadata.sender_linkedin_slug",
215
+ },
216
+ ],
217
+ },
218
+ traits: {
219
+ linkedin_url: {
220
+ eventPath: "metadata.sender_profile_url",
221
+ mergeStrategy: "prefer_non_empty",
222
+ },
223
+ last_linkedin_message_at: {
224
+ eventPath: "occurred_at",
225
+ mergeStrategy: "overwrite",
226
+ },
227
+ },
228
+ },
229
+ ];
230
+
231
+ /**
232
+ * Link a live `post` to its author. The Voyager feed (company_updates) exposes
233
+ * the actor's immutable `urn:li:fsd_profile:<id>` as `author_member_id` plus the
234
+ * `/in/<slug>`. Both are identities; NEITHER is `primary` — they match
235
+ * EQUAL-WEIGHT (same as the connections/messages attributions).
236
+ *
237
+ * Why not primary member_id: takeout-ingested people already exist keyed on
238
+ * `linkedin_slug` (no member id — the export has none). A `primary` member_id
239
+ * would be authoritative: on a live post it would find no member-id match, mint
240
+ * a NEW person, and fork the existing slug-person (the resolver does not fall
241
+ * back to a secondary slug hit when a primary is present). Equal-weight instead
242
+ * UNIONS the hits: the live post matches the existing person by slug AND
243
+ * accretes the member_id onto them. Durability still holds — once the member_id
244
+ * is on the person, a later vanity-URL change still resolves via that member_id
245
+ * in the equal-weight union. Real authors, so mint on no match.
246
+ */
247
+ const LINKEDIN_POST_AUTHOR_ATTRIBUTIONS: EventAttributionRule[] = [
248
+ {
249
+ role: "authored_by",
250
+ autoCreate: true,
251
+ target: {
252
+ entityType: "person",
253
+ titlePath: "author_name",
254
+ identities: [
255
+ {
256
+ namespace: LINKEDIN_IDENTITY.MEMBER_ID,
257
+ eventPath: "metadata.author_member_id",
258
+ },
259
+ {
260
+ namespace: LINKEDIN_IDENTITY.SLUG,
261
+ eventPath: "metadata.author_linkedin_slug",
262
+ },
263
+ ],
264
+ },
265
+ traits: {
266
+ linkedin_headline: {
267
+ eventPath: "metadata.author_headline",
268
+ mergeStrategy: "prefer_non_empty",
269
+ },
270
+ last_linkedin_post_at: {
271
+ eventPath: "occurred_at",
272
+ mergeStrategy: "overwrite",
273
+ },
274
+ },
275
+ },
276
+ ];
277
+
278
+ /**
279
+ * Attribute only people whose `linkedin_slug` was resolved from the row's
280
+ * profile links. The engine skips rules with no resolved identity, so a card
281
+ * that exposes no link for a member cannot create a name-only entity for them.
282
+ * When an engagement card exposes links for both members, it carries both
283
+ * slugs so the author can be minted instead of being dropped.
284
+ */
285
+ const LINKEDIN_HOME_FEED_ATTRIBUTIONS: EventAttributionRule[] = [
286
+ {
287
+ name: "author",
288
+ role: "authored_by",
289
+ autoCreate: true,
290
+ target: {
291
+ entityType: "person",
292
+ titlePath: "author_name",
293
+ identities: [
294
+ {
295
+ namespace: LINKEDIN_IDENTITY.SLUG,
296
+ eventPath: "metadata.author_linkedin_slug",
297
+ },
298
+ ],
299
+ },
300
+ traits: {
301
+ linkedin_url: {
302
+ eventPath: "metadata.author_profile_url",
303
+ mergeStrategy: "prefer_non_empty",
304
+ },
305
+ },
306
+ },
307
+ {
308
+ name: "engager",
309
+ role: "performed_by",
310
+ autoCreate: true,
311
+ target: {
312
+ entityType: "person",
313
+ titlePath: "metadata.social_actor",
314
+ identities: [
315
+ {
316
+ namespace: LINKEDIN_IDENTITY.SLUG,
317
+ eventPath: "metadata.social_actor_slug",
318
+ },
319
+ ],
320
+ },
321
+ traits: {
322
+ linkedin_url: {
323
+ eventPath: "metadata.social_actor_profile_url",
324
+ mergeStrategy: "prefer_non_empty",
325
+ },
326
+ },
327
+ },
328
+ ];
329
+
330
+ /** A visible comment links its author to the author of the parent post. */
331
+ const LINKEDIN_HOME_COMMENT_ATTRIBUTIONS: EventAttributionRule[] = [
332
+ {
333
+ name: "commenter",
334
+ role: "authored_by",
335
+ autoCreate: true,
336
+ target: {
337
+ entityType: "person",
338
+ titlePath: "author_name",
339
+ identities: [
340
+ {
341
+ namespace: LINKEDIN_IDENTITY.SLUG,
342
+ eventPath: "metadata.author_linkedin_slug",
343
+ },
344
+ ],
345
+ },
346
+ traits: {
347
+ linkedin_url: {
348
+ eventPath: "metadata.author_profile_url",
349
+ mergeStrategy: "prefer_non_empty",
350
+ },
351
+ },
352
+ },
353
+ {
354
+ name: "post_author",
355
+ role: "about",
356
+ autoCreate: true,
357
+ target: {
358
+ entityType: "person",
359
+ titlePath: "metadata.parent_author",
360
+ identities: [
361
+ {
362
+ namespace: LINKEDIN_IDENTITY.SLUG,
363
+ eventPath: "metadata.parent_author_linkedin_slug",
364
+ },
365
+ ],
366
+ },
367
+ traits: {
368
+ linkedin_url: {
369
+ eventPath: "metadata.parent_author_profile_url",
370
+ mergeStrategy: "prefer_non_empty",
371
+ },
372
+ },
373
+ },
374
+ {
375
+ name: "parent_comment_author",
376
+ role: "mentions",
377
+ autoCreate: true,
378
+ target: {
379
+ entityType: "person",
380
+ titlePath: "metadata.parent_comment_author",
381
+ identities: [
382
+ {
383
+ namespace: LINKEDIN_IDENTITY.SLUG,
384
+ eventPath: "metadata.parent_comment_author_linkedin_slug",
385
+ },
386
+ ],
387
+ },
388
+ traits: {
389
+ linkedin_url: {
390
+ eventPath: "metadata.parent_comment_author_profile_url",
391
+ mergeStrategy: "prefer_non_empty",
392
+ },
393
+ },
394
+ },
395
+ ];
396
+
397
+ // ── Home-feed content-script scrape contract ────────────────────
398
+ //
399
+ // The personalized home feed (linkedin.com/feed/) is the ONE feed that can't
400
+ // be read via network capture: attaching the CDP debugger stops the feed from
401
+ // rendering, so the Voyager responses never arrive. Instead we drive the
402
+ // extension's `cs_scrape` op (a content script, no debugger) with a declarative
403
+ // selector config defined here. The extension runs a site-agnostic scrape
404
+ // engine — the LinkedIn selectors live in this connector, not the extension.
405
+
406
+ /** One profile/company anchor scraped from a home-feed card. */
407
+ interface HomeFeedLink {
408
+ /** The `/in/<slug>` or `/company/<slug>` href. */
409
+ href?: string;
410
+ /**
411
+ * The anchor's accessible name — LinkedIn renders these as `View <Name>'s
412
+ * profile` (person) or `View company: <Name>` (company). Used to match an
413
+ * anchor to the actor/author named elsewhere in the card, so identity never
414
+ * depends on link order.
415
+ */
416
+ name?: string;
417
+ }
418
+
419
+ /** One media URL scraped from a feed card. */
420
+ interface HomeFeedMedia {
421
+ url?: string;
422
+ alt_text?: string;
423
+ }
424
+
425
+ /** A row produced by the extension's cs_scrape from HOME_FEED_SCRAPE_CONFIG. */
426
+ interface HomeFeedRow {
427
+ /** The componentkey token (base64url-ish, NOT a numeric activity id). */
428
+ id?: string;
429
+ body?: string;
430
+ author?: string;
431
+ /**
432
+ * The post-author's name, read from the card's control-menu button
433
+ * (`aria-label="Open control menu for post by <Author>"`). LinkedIn names the
434
+ * author here on the observed plain, engagement, promoted, and company card
435
+ * shapes, regardless of link order. This is preferred over the body-text
436
+ * heuristic when present.
437
+ */
438
+ author_control_label?: string;
439
+ /**
440
+ * Post permalink, when a row carries one. The scrape spec that fed this is
441
+ * inert on current cards (see `post_url` in HOME_FEED_SCRAPE_CONFIG), so
442
+ * treat it as best-effort and prefer `post_identity` for durable identity.
443
+ */
444
+ post_url?: string;
445
+ /**
446
+ * Rendered identifier carrying the stable post id: a readable shareId /
447
+ * ugcPostId / activity URN, or the encoded comment-tools id that
448
+ * `decodeHomeFeedPostIdentity` turns into a URN before ingestion.
449
+ */
450
+ post_identity?: string;
451
+ /** Every profile/company anchor in the card, each with its accessible name. */
452
+ links?: HomeFeedLink[];
453
+ /** Media that belongs to a top-level post (nested comment media excluded). */
454
+ post_media?: HomeFeedMedia[];
455
+ /** Media that belongs to a visible comment row. */
456
+ comment_media?: HomeFeedMedia[];
457
+ /** Dedicated engagement-summary values, never inferred from post prose. */
458
+ reaction_count_text?: string;
459
+ reaction_count_label?: string;
460
+ comment_count_text?: string;
461
+ comment_count_label?: string;
462
+ repost_count_text?: string;
463
+ repost_count_label?: string;
464
+ /** Explicit browser-runtime proof that the post's thread was fully expanded. */
465
+ comment_coverage?: HomeFeedCommentCoverage;
466
+ /** Durable identity of an enclosing comment when this row is a nested reply. */
467
+ parent_comment_identity?: string;
468
+ }
469
+
470
+ interface HomeFeedCommentCoverage {
471
+ expected: number;
472
+ collected: number;
473
+ complete: boolean;
474
+ timedOut?: boolean;
475
+ }
476
+
477
+ /** LinkedIn origins the cs_scrape window is allowed to touch. */
478
+ const LINKEDIN_ALLOWED_ORIGINS = ["linkedin.com", "*.linkedin.com"];
479
+
480
+ const HOME_FEED_COMMENT_COUNT_TEXT_SELECTOR =
481
+ ':scope:not([id^="replaceableComment_"]) div:has(+ hr + div button[aria-label="Open reactions menu"]):not([id^="replaceableComment_"] *) > div:last-child > div:first-child, .social-details-social-counts__comments, .social-details-social-counts__comments-count';
482
+ const HOME_FEED_COMMENT_COUNT_LABEL_SELECTOR =
483
+ '.social-details-social-counts [aria-label*="comment" i], [aria-label$=" comment" i]:not([id^="replaceableComment_"] *), [aria-label$=" comments" i]:not([id^="replaceableComment_"] *)';
484
+ const HOME_FEED_POST_CONTROL_SELECTOR =
485
+ '[role="button"]:not([id^="replaceableComment_"] *):not([componentkey^="commentsSectionContainer"] *), button:not([id^="replaceableComment_"] *):not([componentkey^="commentsSectionContainer"] *), a:not([id^="replaceableComment_"] *):not([componentkey^="commentsSectionContainer"] *)';
486
+ const HOME_FEED_POST_IDENTITY_SELECTOR =
487
+ ':is([id*="shareId="], [id*="ugcPostId="], [id*="urn:li:activity:"], [id*="-replaceableCommentTools"]):not([id^="replaceableComment_"]):not([id^="replaceableComment_"] *)';
488
+
489
+ /** Current comment-tool ids encode a typed post URN, followed by the recycled
490
+ * card key. Decode only the typed prefix: field 1 is activity, field 2 ugcPost;
491
+ * each contains field 1 as a protobuf sint64 (ZigZag). Native comment URNs and
492
+ * the corresponding live post URLs independently confirm those namespaces. */
493
+ function decodeHomeFeedPostIdentity(
494
+ raw: string | undefined
495
+ ): string | undefined {
496
+ const marker = "-replaceableCommentTools";
497
+ if (!raw?.includes(marker)) return raw;
498
+ const invalid = () =>
499
+ new Error(
500
+ "LinkedIn returned an invalid encoded post identity. No home-feed events were persisted."
501
+ );
502
+ let bytes: string;
503
+ try {
504
+ bytes = atob(
505
+ raw.slice(0, raw.indexOf(marker)).replace(/-/g, "+").replace(/_/g, "/")
506
+ );
507
+ } catch {
508
+ throw invalid();
509
+ }
510
+ const tag = bytes.charCodeAt(0);
511
+ if (
512
+ (tag !== 10 && tag !== 18) ||
513
+ bytes.length < 4 ||
514
+ bytes.length > 13 ||
515
+ bytes.charCodeAt(1) !== bytes.length - 2 ||
516
+ bytes.charCodeAt(2) !== 8
517
+ ) {
518
+ throw invalid();
519
+ }
520
+ let encoded = 0n;
521
+ for (let i = 3; i < bytes.length; i++) {
522
+ const byte = bytes.charCodeAt(i);
523
+ const isLast = i === bytes.length - 1;
524
+ if (Boolean(byte & 128) === isLast) throw invalid();
525
+ encoded |= BigInt(byte & 127) << BigInt(7 * (i - 3));
526
+ }
527
+ if ((encoded & 1n) !== 0n || encoded >= 1n << 64n || encoded < 200_000n) {
528
+ throw invalid();
529
+ }
530
+ return `urn:li:${tag === 10 ? "activity" : "ugcPost"}:${encoded >> 1n}`;
531
+ }
532
+
533
+ // A fenced dispatch discards every row it collected, so an oversized scrape
534
+ // repeats forever (prod hit `run timed out after 90000ms` on this feed).
535
+ // Target a 60s dispatch within Owletto's 90s run fence: reserve 23s for
536
+ // navigation (20s load wait + up to 3s settle in tools.js) and 12s for
537
+ // deadline overrun and harvesting. This is a cooperative expansion deadline,
538
+ // not a hard scrape timeout: the engine stops scrolling after observing it,
539
+ // then finishes harvesting. DOM work and anchor-lock waits can still overrun.
540
+ const HOME_FEED_SCRAPE_DEADLINE_MS = 25_000;
541
+
542
+ /**
543
+ * Selectors for the virtualized linkedin.com/feed/ DOM. Home-feed posts are
544
+ * componentkey divs with no activity urn, so the row id is the componentkey
545
+ * token (NOT numeric). These selectors live here, not in the extension.
546
+ */
547
+ const HOME_FEED_SCRAPE_CONFIG = {
548
+ // Current LinkedIn pages scroll inside <main>, not document/window.
549
+ scroll: { max: 8, stall: 3, waitMs: 1500, deep: true },
550
+ loggedOutWhen: {
551
+ pathRegex: "/(login|authwall|uas/login|checkpoint|signup)\\b",
552
+ },
553
+ rowSelector:
554
+ 'div[componentkey^="expanded"][componentkey*="FeedType_MAIN_FEED_RELEVANCE"], [id^="replaceableComment_urn:li:comment:"]',
555
+ expandRows: {
556
+ // Only actual post containers start with `expanded`; helper/component rows
557
+ // share the FeedType suffix but must never drive another post's controls.
558
+ rowSelector:
559
+ 'div[componentkey^="expanded"][componentkey*="FeedType_MAIN_FEED_RELEVANCE"]',
560
+ // LinkedIn swaps the whole card for a fresh element mid-expansion (sorting
561
+ // by "Most recent" is enough to trigger it). Without a row-local durable
562
+ // id the extension cannot tell a replacement from a recycled slot, so it
563
+ // abandons the thread. Same selector as post_identity below: the two must
564
+ // resolve to the same post or expansion proof binds to the wrong row.
565
+ identity: {
566
+ selector: HOME_FEED_POST_IDENTITY_SELECTOR,
567
+ take: "attr",
568
+ attr: "id",
569
+ // Exclude the recycled card-key suffix from expansion ownership.
570
+ regex: "^(.*?)(?:-replaceableCommentTools|$)",
571
+ group: 1,
572
+ },
573
+ expected: {
574
+ // Keep this aligned with the post-level comment count fields below. The
575
+ // count is not always interactive: LinkedIn also renders it as a plain
576
+ // counter wrapper or an aria-labelled span.
577
+ selector: `${HOME_FEED_COMMENT_COUNT_TEXT_SELECTOR}, ${HOME_FEED_COMMENT_COUNT_LABEL_SELECTOR}, ${HOME_FEED_POST_CONTROL_SELECTOR}`,
578
+ textRegex: "^\\d[\\d,.]*(?:\\s*[kmb])?\\s+comments?",
579
+ textRegexFlags: "i",
580
+ regex: "(\\d[\\d,.]*(?:\\s*[kmb])?)\\s+comments?",
581
+ regexFlags: "i",
582
+ },
583
+ items: {
584
+ selector: '[id^="replaceableComment_urn:li:comment:"]',
585
+ idAttr: "id",
586
+ },
587
+ open: {
588
+ selector: '[role="button"], button, a',
589
+ textRegex: "^\\d[\\d,.]*(?:\\s*[kmb])?\\s+comments?",
590
+ textRegexFlags: "i",
591
+ },
592
+ sort: {
593
+ triggerSelector: '[role="button"], button',
594
+ triggerTextRegex: "^Most relevant$|^Most recent$",
595
+ triggerTextRegexFlags: "i",
596
+ selectedTextRegex: "^Most recent$",
597
+ selectedTextRegexFlags: "i",
598
+ optionSelector: '[role="menuitem"]',
599
+ optionTextRegex: "^Most recent\\b",
600
+ optionTextRegexFlags: "i",
601
+ maxWaitMs: 1500,
602
+ },
603
+ more: {
604
+ selector: '[role="button"], button, a',
605
+ textRegex:
606
+ "^(?:(?:See|Load|View)\\s+\\d[\\d,.]*\\s+(?:more\\s+)?comments?|(?:See|Load|View)\\s+(?:more|previous)\\s+(?:comments?|replies)|\\d[\\d,.]*\\s+replies)\\b",
607
+ textRegexFlags: "i",
608
+ },
609
+ maxPasses: 40,
610
+ maxDurationMs: HOME_FEED_SCRAPE_DEADLINE_MS,
611
+ // LinkedIn removes its controls before the requested comments finish
612
+ // rendering. Allow a short control-free settling window; the shared
613
+ // maxDurationMs budget still caps the whole expansion pass.
614
+ stall: 12,
615
+ waitMs: 350,
616
+ outputField: "comment_coverage",
617
+ },
618
+ id: {
619
+ source: "attr",
620
+ // Posts identify themselves with componentkey; native comments use their
621
+ // replaceableComment id. Selecting comments individually keeps two visible
622
+ // comments (and their media) from collapsing into one container row.
623
+ name: ["componentkey", "id"],
624
+ // Post rows keep only the token before FeedType_. Comment ids have no
625
+ // FeedType_ suffix, so the end-of-string alternative preserves the full
626
+ // native comment identity.
627
+ regex: "^(?:expanded)?(.+?)(?=FeedType_|$)",
628
+ group: 1,
629
+ },
630
+ requireFields: ["body"],
631
+ fields: {
632
+ body: { take: "text" },
633
+ author: {
634
+ // LinkedIn obfuscates the actor classes, so the old
635
+ // .update-components-actor__* selectors no longer match. Best-effort:
636
+ // grab the visible name span inside the actor's profile/company link
637
+ // when present. When this misses, buildHomeFeedEvents falls back to
638
+ // parsing the author out of the row body text.
639
+ selector:
640
+ 'a[href*="/in/"] span[aria-hidden="true"], a[href*="/company/"] span[aria-hidden="true"]',
641
+ take: "text",
642
+ firstLine: true,
643
+ },
644
+ author_control_label: {
645
+ // The post's own control-menu button supplies the preferred author signal
646
+ // independently of profile-link order — see HomeFeedRow.
647
+ selector: 'button[aria-label*="control menu for post by"]',
648
+ take: "attr",
649
+ attr: "aria-label",
650
+ },
651
+ post_url: {
652
+ // Current cards expose neither a permalink anchor nor a post URN. Their
653
+ // control menu still has LinkedIn's own "Copy link to post" action, but
654
+ // the extension does not click clipboard-writing actions. `post_url` is
655
+ // therefore best-effort/empty; `post_identity` below is the durable key.
656
+ take: "clipboardAction",
657
+ triggerSelector: 'button[aria-label*="control menu for post by"]',
658
+ actionSelector: '[role="menuitem"]',
659
+ actionTextRegex: "^Copy link(?: to post)?$",
660
+ actionTextRegexFlags: "i",
661
+ maxWaitMs: 1500,
662
+ },
663
+ post_identity: {
664
+ // Readable commentary URNs and encoded comment-tool ids both carry the
665
+ // durable post identity. parseRows decodes the latter before ingestion.
666
+ selector: HOME_FEED_POST_IDENTITY_SELECTOR,
667
+ take: "attr",
668
+ attr: "id",
669
+ },
670
+ links: {
671
+ // Every profile/company anchor with its accessible name, so
672
+ // buildHomeFeedEvents can match the author/engager by NAME rather than by
673
+ // fragile DOM position. `aria` reads the anchor's own or descendant
674
+ // aria-label ("View <Name>'s profile"); `alt` covers the avatar img on
675
+ // company links ("View company: <Name>").
676
+ selector: 'a[href*="/in/"], a[href*="/company/"]',
677
+ take: "objectAll",
678
+ parts: {
679
+ href: { take: "attr", attr: "href" },
680
+ name: { take: "aria" },
681
+ nameAlt: { take: "alt" },
682
+ },
683
+ },
684
+ post_media: {
685
+ // Profile photos and company logos are identities, not post attachments.
686
+ // The final :not keeps media inside a nested visible comment off the post.
687
+ selector:
688
+ ':scope:not([id^="replaceableComment_"]) img[src*="media.licdn.com/dms/image/"]:not([src*="profile-displayphoto"]):not([src*="company-logo"]):not([id^="replaceableComment_"] img)',
689
+ take: "objectAll",
690
+ parts: {
691
+ url: { take: "attr", attr: "src" },
692
+ alt_text: { take: "attr", attr: "alt" },
693
+ },
694
+ },
695
+ comment_media: {
696
+ selector:
697
+ ':scope[id^="replaceableComment_"] img[src*="media.licdn.com/dms/image/"]:not([src*="profile-displayphoto"]):not([src*="company-logo"])',
698
+ take: "objectAll",
699
+ parts: {
700
+ url: { take: "attr", attr: "src" },
701
+ alt_text: { take: "attr", attr: "alt" },
702
+ },
703
+ },
704
+ parent_comment_identity: {
705
+ // Replies are nested beneath their parent comment on the current feed.
706
+ // Starting the closest lookup at parentElement keeps top-level comments
707
+ // empty while recording the enclosing durable comment id for replies.
708
+ closestSelector: '[id^="replaceableComment_urn:li:comment:"]',
709
+ take: "attr",
710
+ attr: "id",
711
+ },
712
+ // Engagement comes only from LinkedIn's dedicated social-count controls.
713
+ // Keep both visible text and accessible-label variants because the current
714
+ // feed uses both shapes across post types and experiments.
715
+ //
716
+ // Row scoping matters here: a post row CONTAINS its visible comment rows,
717
+ // and `querySelector` returns the first match in DOCUMENT order across the
718
+ // whole comma list, not the first branch that matches. So every branch that
719
+ // is not already anchored to `.social-details-social-counts` (the post's own
720
+ // block) carries `:not([id^="replaceableComment_"] *)`, which excludes
721
+ // elements inside a nested comment. The reaction fields then add a leading
722
+ // `:scope[id^="replaceableComment_"]` branch so a comment row reads its OWN
723
+ // reactions: the current feed puts the count beside an "Open reactions
724
+ // menu" button using obfuscated classes, while permalink pages still use a
725
+ // comment social-bar class with an experiment suffix (`--cr`). The semantic
726
+ // sibling branch covers the feed and the prefix class match covers the
727
+ // permalink shape. Current post cards put their count block immediately
728
+ // before the divider and action bar; those semantic siblings avoid the
729
+ // obfuscated classes. Comment and repost counts stay post-only — a
730
+ // comment's reply count is a different quantity.
731
+ reaction_count_text: {
732
+ selector:
733
+ ':scope:not([id^="replaceableComment_"]) div:has(+ hr + div button[aria-label="Open reactions menu"]):not([id^="replaceableComment_"] *) > a:first-child, :scope[id^="replaceableComment_"] div:has(> button[aria-label="Open reactions menu"]) > :first-child, :scope[id^="replaceableComment_"] [class*="comments-comment-social-bar__reactions-count"], .social-details-social-counts__reactions-count, .social-details-social-counts__reactions',
734
+ take: "text",
735
+ },
736
+ reaction_count_label: {
737
+ selector:
738
+ ':scope[id^="replaceableComment_"] [aria-label*=" reaction on " i], :scope[id^="replaceableComment_"] [aria-label*=" reactions on " i], .social-details-social-counts [aria-label*="reaction" i], [aria-label$=" reaction" i]:not([aria-label^="Reaction button state:" i]):not([id^="replaceableComment_"] *), [aria-label$=" reactions" i]:not([aria-label^="Reaction button state:" i]):not([id^="replaceableComment_"] *)',
739
+ take: "attr",
740
+ attr: "aria-label",
741
+ },
742
+ comment_count_text: {
743
+ selector: HOME_FEED_COMMENT_COUNT_TEXT_SELECTOR,
744
+ take: "text",
745
+ },
746
+ comment_count_label: {
747
+ selector: HOME_FEED_COMMENT_COUNT_LABEL_SELECTOR,
748
+ take: "attr",
749
+ attr: "aria-label",
750
+ },
751
+ repost_count_text: {
752
+ selector:
753
+ ':scope:not([id^="replaceableComment_"]) div:has(+ hr + div button[aria-label="Open reactions menu"]):not([id^="replaceableComment_"] *) > div:last-child > a:last-child, .social-details-social-counts__reposts, .social-details-social-counts__reposts-count',
754
+ take: "text",
755
+ },
756
+ repost_count_label: {
757
+ selector:
758
+ '.social-details-social-counts [aria-label*="repost" i], [aria-label$=" repost" i]:not([id^="replaceableComment_"] *), [aria-label$=" reposts" i]:not([id^="replaceableComment_"] *)',
759
+ take: "attr",
760
+ attr: "aria-label",
761
+ },
762
+ },
763
+ } as const;
764
+
765
+ /**
766
+ * Profile activity lists render the same cards as the home feed under their own
767
+ * FeedType, so they share its fields and identity parsing. Read only comments
768
+ * already rendered on those pages, without expanding threads.
769
+ */
770
+ const MY_ACTIVITY_LISTS = {
771
+ posts: { path: "all", feedType: "FeedType_CREATOR_PROFILE_ALL_CONTENT_VIEW" },
772
+ comments: { path: "comments", feedType: "FeedType_PROFILE_COMMENTS" },
773
+ } as const;
774
+
775
+ function myActivityScrapeConfig(feedType: string, maxScrolls: number) {
776
+ const { expandRows: _expandRows, ...cardConfig } = HOME_FEED_SCRAPE_CONFIG;
777
+ // post_url opens every card's control menu and waits for a copy-link item it
778
+ // never uses; activity cards carry post_identity, so skip that per-row wait.
779
+ const { post_url: _postUrl, ...fields } = cardConfig.fields;
780
+ return {
781
+ ...cardConfig,
782
+ fields,
783
+ rowSelector: `div[componentkey^="expanded"][componentkey*="${feedType}"], [id^="replaceableComment_urn:li:comment:"]`,
784
+ scroll: { ...cardConfig.scroll, max: maxScrolls },
785
+ };
786
+ }
787
+
788
+ /** UI prefixes on post cards, stripped before resolving the author's name. */
789
+ const MY_ACTIVITY_CHROME_PREFIXES = [
790
+ /^Feed post(?:\s+|$)/i,
791
+ /^Promote this post to reach people who matter to you\.\s*Boost\s*/i,
792
+ /^This post type can['’]t be boosted\.\s*/i,
793
+ ];
794
+
795
+ function stripMyActivityChrome(text: string): string {
796
+ let out = text.trim();
797
+ for (const prefix of MY_ACTIVITY_CHROME_PREFIXES) {
798
+ out = out.replace(prefix, "");
799
+ }
800
+ return out;
801
+ }
802
+
803
+ function linkedInProfileSlugFromUrl(
804
+ url: string | undefined
805
+ ): string | undefined {
806
+ const match = url?.match(/linkedin\.com\/in\/([^/?#]+)/i);
807
+ const slug = match?.[1] ? decodeURIComponent(match[1]) : undefined;
808
+ return slug && slug.toLowerCase() !== "me" ? slug : undefined;
809
+ }
810
+
811
+ async function readMyLinkedInActivity(
812
+ ctx: ActionContext
813
+ ): Promise<ActionResult> {
814
+ const requested = Array.isArray(ctx.input.kinds)
815
+ ? ctx.input.kinds.filter(
816
+ (kind): kind is keyof typeof MY_ACTIVITY_LISTS =>
817
+ kind === "posts" || kind === "comments"
818
+ )
819
+ : [];
820
+ const kinds =
821
+ requested.length > 0 ? requested : (["posts", "comments"] as const);
822
+ const rawScrolls = Number(ctx.input.max_scrolls ?? 3);
823
+ const maxScrolls = Number.isFinite(rawScrolls)
824
+ ? Math.min(10, Math.max(1, Math.trunc(rawScrolls)))
825
+ : 3;
826
+ const dispatcher = requireBrowser(ctx);
827
+
828
+ // Resolve the /in/me/ redirect before constructing canonical activity URLs.
829
+ const profile = await extensionDomScrape<Record<string, unknown>>({
830
+ dispatcher,
831
+ url: "https://www.linkedin.com/in/me/",
832
+ config: {
833
+ rowSelector: "main",
834
+ requireFields: [],
835
+ fields: {},
836
+ loggedOutWhen: HOME_FEED_SCRAPE_CONFIG.loggedOutWhen,
837
+ },
838
+ parseRows: (rows) => rows,
839
+ allowedOrigins: LINKEDIN_ALLOWED_ORIGINS,
840
+ // One action run reads several pages; a run-scoped scratch tab is released
841
+ // after each scrape, so reuse LinkedIn's anchor tab across these reads.
842
+ persistent: true,
843
+ });
844
+ if (!profile.loggedIn) {
845
+ return {
846
+ success: false,
847
+ error:
848
+ "Not logged into LinkedIn. Sign in to linkedin.com in the paired Chrome profile, then retry.",
849
+ };
850
+ }
851
+ const slug = linkedInProfileSlugFromUrl(profile.landedUrl);
852
+ // Author slugs are stored in normalizeLinkedInSlug's canonical form (still
853
+ // percent-encoded), so ownership compares against that form, not `slug`.
854
+ const memberSlug = normalizeLinkedInSlug(profile.landedUrl);
855
+ if (!slug || !memberSlug) {
856
+ return {
857
+ success: false,
858
+ error: `Could not resolve the signed-in LinkedIn profile from ${JSON.stringify(profile.landedUrl ?? null)}.`,
859
+ };
860
+ }
861
+
862
+ const items: Array<Record<string, unknown>> = [];
863
+ for (const kind of kinds) {
864
+ const list = MY_ACTIVITY_LISTS[kind];
865
+ const { items: rows, loggedIn } = await extensionDomScrape<HomeFeedRow>({
866
+ dispatcher,
867
+ url: `https://www.linkedin.com/in/${encodeURIComponent(slug)}/recent-activity/${list.path}/`,
868
+ config: myActivityScrapeConfig(list.feedType, maxScrolls),
869
+ parseRows: (raw) =>
870
+ (raw as HomeFeedRow[]).map((row) => ({
871
+ ...row,
872
+ body: parseHomeFeedCommentIdentity(row.id)
873
+ ? row.body
874
+ : stripMyActivityChrome(row.body ?? ""),
875
+ post_identity: decodeHomeFeedPostIdentity(row.post_identity),
876
+ })),
877
+ allowedOrigins: LINKEDIN_ALLOWED_ORIGINS,
878
+ persistent: true,
879
+ });
880
+ if (!loggedIn) {
881
+ return {
882
+ success: false,
883
+ error:
884
+ "Not logged into LinkedIn. Sign in to linkedin.com in the paired Chrome profile, then retry.",
885
+ };
886
+ }
887
+ for (const event of buildHomeFeedEvents(rows, new Date())) {
888
+ const metadata = (event.metadata ?? {}) as Record<string, unknown>;
889
+ const authorSlug = metadata.author_linkedin_slug;
890
+ items.push({
891
+ list: kind,
892
+ type: event.origin_type,
893
+ id: event.origin_id,
894
+ ...(event.origin_parent_id
895
+ ? { parent_id: event.origin_parent_id }
896
+ : {}),
897
+ is_mine: authorSlug === memberSlug,
898
+ author: event.author_name ?? null,
899
+ text: event.payload_text ?? "",
900
+ url: event.source_url ?? null,
901
+ ...(typeof metadata.parent_author === "string"
902
+ ? { parent_author: metadata.parent_author }
903
+ : {}),
904
+ });
905
+ }
906
+ }
907
+
908
+ return {
909
+ success: true,
910
+ output: { profile_slug: slug, count: items.length, items },
911
+ };
912
+ }
913
+
914
+ /**
915
+ * Live home-feed read for virtual timelines: scrapes the same cards the
916
+ * home_feed sync persists, but returns them without storing any events.
917
+ * Skips short-URL resolution and reports unaddressable posts as skipped:
918
+ * the flagger can only stage drafts for posts with a durable URL.
919
+ */
920
+ async function readHomeFeed(ctx: ActionContext): Promise<ActionResult> {
921
+ const rawScrolls = Number(ctx.input.max_scrolls ?? 6);
922
+ const maxScrolls = Number.isFinite(rawScrolls)
923
+ ? Math.min(10, Math.max(1, Math.trunc(rawScrolls)))
924
+ : 6;
925
+ const dispatcher = requireBrowser(ctx);
926
+ // Timeline reads prioritize more posts. Expanding a busy comment thread
927
+ // spends the shared expansion deadline before the scraper can paginate.
928
+ // Already-rendered comments remain readable through the same row selector.
929
+ const { expandRows: _expandRows, ...timelineConfig } =
930
+ HOME_FEED_SCRAPE_CONFIG;
931
+ const { items: rows, loggedIn } = await extensionDomScrape<HomeFeedRow>({
932
+ dispatcher,
933
+ url: LINKEDIN_HOME_URL,
934
+ config: {
935
+ ...timelineConfig,
936
+ scroll: { ...timelineConfig.scroll, max: maxScrolls },
937
+ },
938
+ parseRows: (raw) =>
939
+ (raw as HomeFeedRow[]).map((row) => ({
940
+ ...row,
941
+ post_identity: decodeHomeFeedPostIdentity(row.post_identity),
942
+ })),
943
+ allowedOrigins: LINKEDIN_ALLOWED_ORIGINS,
944
+ // A hidden warm tab can retain only the initial batch. The default scrape
945
+ // window gives LinkedIn a fresh rendering surface and closes after reading.
946
+ persistent: false,
947
+ });
948
+ if (!loggedIn) {
949
+ return {
950
+ success: false,
951
+ error:
952
+ "Not logged into LinkedIn. Sign in to linkedin.com in the paired Chrome profile, then retry.",
953
+ };
954
+ }
955
+ const items: Array<Record<string, unknown>> = [];
956
+ // Count only emitted events without a URL: unresolved scrape rows may
957
+ // never become events (dropped as noise), so counting rows would inflate
958
+ // the number. The flagger can only stage drafts for addressable posts.
959
+ let skipped = 0;
960
+ for (const event of buildHomeFeedEvents(rows, new Date())) {
961
+ if (!event.source_url) {
962
+ skipped += 1;
963
+ continue;
964
+ }
965
+ items.push({
966
+ type: event.origin_type,
967
+ id: event.origin_id,
968
+ ...(event.origin_parent_id ? { parent_id: event.origin_parent_id } : {}),
969
+ author: event.author_name ?? null,
970
+ text: event.payload_text ?? "",
971
+ url: event.source_url,
972
+ });
973
+ }
974
+ return {
975
+ success: true,
976
+ output: { count: items.length, skipped, items },
977
+ };
978
+ }
979
+
980
+ /**
981
+ * Best-effort author extraction from a home-feed row's body text. Social
982
+ * context appears before the post author in two shapes: actor banners name an
983
+ * engaging member ("X likes this", "X commented", "X reposted this") and
984
+ * actor-less banners are platform labels with no member ("Recommended for
985
+ * you", "Voices worth following"). Stacked context can contain more than one
986
+ * actor banner; the author follows the last one.
987
+ */
988
+ const HOME_FEED_ACTOR_BANNER_RE =
989
+ /\b(?:(?:likes?|loves?|celebrates?|supports?) this|finds? this (?:insightful|funny)|commented(?: on this)?|reposted this)\s*/gi;
990
+ const HOME_FEED_ACTORLESS_BANNER_RE =
991
+ /^(?:recommended for you|voices worth following)\s+/i;
992
+
993
+ /** Normalize a matched actor-banner to a stable engagement verb. */
994
+ function normalizeSocialAction(banner: string): string {
995
+ const b = banner.toLowerCase();
996
+ if (b.includes("comment")) return "comment";
997
+ if (b.includes("repost")) return "repost";
998
+ if (b.includes("insightful")) return "insightful";
999
+ if (b.includes("funny")) return "funny";
1000
+ if (b.includes("love")) return "love";
1001
+ if (b.includes("celebrate")) return "celebrate";
1002
+ if (b.includes("support")) return "support";
1003
+ return "like";
1004
+ }
1005
+
1006
+ type HomeFeedBanner =
1007
+ | { kind: "actor"; actor: string; action: string }
1008
+ | { kind: "actorless" };
1009
+
1010
+ /** Strip relative-time tokens, fused CTAs, and leading emoji from a name. */
1011
+ function cleanHomeFeedDisplayName(raw: string): string {
1012
+ let name = raw.trim();
1013
+ // A repost segment puts a relative-time token (e.g. "17h") right after the
1014
+ // original poster's name and before the marker — strip it so we keep just
1015
+ // the name.
1016
+ name = name.replace(/\s+\d+\s*[smhdwy]o?$/i, "").trim();
1017
+ // Feed cards sometimes fuse UI chrome into the name after a repost /
1018
+ // company author: "Sim Visit website", "Acme View job".
1019
+ name = name
1020
+ .replace(
1021
+ /\s+(?:Visit website|View (?:job|page|profile)|Apply now|Register now|Sign up)$/i,
1022
+ ""
1023
+ )
1024
+ .trim();
1025
+ // Leading emoji / symbol decorations on display names (e.g. "🦔 james").
1026
+ name = name.replace(/^[^\p{L}\p{N}]+/u, "").trim();
1027
+ return name.slice(0, 60);
1028
+ }
1029
+
1030
+ function parseHomeFeedAuthorDetails(body: string): {
1031
+ author: string;
1032
+ banner: HomeFeedBanner | null;
1033
+ } {
1034
+ if (!body) return { author: "", banner: null };
1035
+ const text = body.replace(/^feed post\s+/i, "").trim();
1036
+
1037
+ // Limit banner matching to the row header so post content cannot be mistaken
1038
+ // for social context.
1039
+ const sepIdx = text.indexOf(" • ");
1040
+ if (sepIdx === -1) {
1041
+ // Expanded-post cards can use "<Name> Author <headline>" without a degree
1042
+ // marker. Resolve that bounded prefix before scanning for banner phrases
1043
+ // so words in the post content cannot affect attribution.
1044
+ const badge = text.match(/^(.{1,60}?)\s+Author\b/);
1045
+ return {
1046
+ author: badge ? cleanHomeFeedDisplayName(badge[1]) : "",
1047
+ banner: null,
1048
+ };
1049
+ }
1050
+ let header = text.slice(0, sepIdx).trim();
1051
+
1052
+ let banner: HomeFeedBanner | null = null;
1053
+ const actorless = header.match(HOME_FEED_ACTORLESS_BANNER_RE);
1054
+ if (actorless) {
1055
+ header = header.slice(actorless[0].length).trim();
1056
+ banner = { kind: "actorless" };
1057
+ }
1058
+
1059
+ const matches = [...header.matchAll(HOME_FEED_ACTOR_BANNER_RE)];
1060
+ if (matches.length > 0) {
1061
+ const first = matches[0];
1062
+ const last = matches[matches.length - 1];
1063
+ const actorGroup = header.slice(0, first.index).trim();
1064
+ const pluralActors = actorGroup.match(/^(.*?)\s+and\s+\d+\s+others?$/i);
1065
+ // "A and 3 others" / "A, B and 22 others" use A's profile link. Preserve
1066
+ // commas in a single member's display name (for example, "A, MBA").
1067
+ const actor = cleanHomeFeedDisplayName(
1068
+ pluralActors ? pluralActors[1].split(",")[0] : actorGroup
1069
+ );
1070
+ banner = {
1071
+ kind: "actor",
1072
+ actor: actor.slice(0, 60),
1073
+ action: normalizeSocialAction(first[0]),
1074
+ };
1075
+ header = header.slice(last.index + last[0].length).trim();
1076
+ }
1077
+
1078
+ let name = header;
1079
+ // A "<Name> Premium Profile <degree> <Name>" badge row repeats the name —
1080
+ // keep only the leading one.
1081
+ const premiumIdx = name.indexOf(" Premium Profile");
1082
+ if (premiumIdx !== -1) name = name.slice(0, premiumIdx);
1083
+ return { author: cleanHomeFeedDisplayName(name), banner };
1084
+ }
1085
+
1086
+ export function parseHomeFeedAuthor(body: string): string {
1087
+ return parseHomeFeedAuthorDetails(body).author;
1088
+ }
1089
+
1090
+ /**
1091
+ * Pull the post author's name out of the control-menu aria-label
1092
+ * ("Open control menu for post by <Author>"). Returns "" when the field is
1093
+ * missing (older cards / extensions) so callers fall back to the body parse.
1094
+ */
1095
+ function parseAuthorControlLabel(label?: string): string {
1096
+ if (!label) return "";
1097
+ const m = label.match(/control menu for post by\s+(.+)$/i);
1098
+ return m ? cleanHomeFeedDisplayName(m[1]) : "";
1099
+ }
1100
+
1101
+ /** A comparison key that ignores case, accents, and punctuation. */
1102
+ function nameKey(name: string): string {
1103
+ return cleanHomeFeedDisplayName(name)
1104
+ .toLowerCase()
1105
+ .normalize("NFD")
1106
+ .replace(/[\u0300-\u036f]/g, "")
1107
+ .replace(/[^\p{L}\p{N} ]/gu, "")
1108
+ .replace(/\s+/g, " ")
1109
+ .trim();
1110
+ }
1111
+
1112
+ /** The accessible name LinkedIn renders on a profile/company anchor. */
1113
+ function linkDisplayName(link: HomeFeedLink): string {
1114
+ const raw = link.name || "";
1115
+ // "View <Name>'s profile" / "View <Name>'s graphic" (person avatars) and
1116
+ // "View company: <Name>" (company avatars).
1117
+ const person = raw.match(/^View\s+(.+?)['’]s\s+(?:profile|graphic)/i);
1118
+ if (person) return cleanHomeFeedDisplayName(person[1]);
1119
+ const company = raw.match(/^View company:\s+(.+)$/i);
1120
+ if (company) return cleanHomeFeedDisplayName(company[1]);
1121
+ return cleanHomeFeedDisplayName(raw);
1122
+ }
1123
+
1124
+ /**
1125
+ * Coerce the extension's `links` field into HomeFeedLink[]. The objectAll scrape
1126
+ * emits `{href, name, nameAlt}` per anchor (name = aria-label, nameAlt = img
1127
+ * alt); coalesce them into one accessible name. Guards a non-array value so a
1128
+ * malformed/absent field yields no links rather than a crash.
1129
+ */
1130
+ function normalizeHomeFeedLinks(raw: unknown): HomeFeedLink[] {
1131
+ if (!Array.isArray(raw)) return [];
1132
+ return raw.map((l) => {
1133
+ const link = (l ?? {}) as {
1134
+ href?: string;
1135
+ name?: string;
1136
+ nameAlt?: string;
1137
+ };
1138
+ return { href: link.href, name: link.name || link.nameAlt || "" };
1139
+ });
1140
+ }
1141
+
1142
+ /**
1143
+ * Whether the paired extension understood the `objectAll` scrape take. A build
1144
+ * that predates it ignores the take and returns the field as plain text, so
1145
+ * `row.links` arrives as a string rather than an array. Attribution still
1146
+ * degrades gracefully (events emit with author names, just no slugs), but the
1147
+ * caller surfaces this so a stale extension is observable rather than silent.
1148
+ * A card with genuinely no profile links yields `[]`, which is indistinguishable
1149
+ * from support — so support is inferred from ANY row producing an array.
1150
+ */
1151
+ export function homeFeedObjectAllSupported(rows: HomeFeedRow[]): boolean {
1152
+ const withLinks = rows.filter((r) => r?.links !== undefined);
1153
+ if (withLinks.length === 0) return true; // no evidence either way — assume ok
1154
+ return withLinks.some((r) => Array.isArray(r.links));
1155
+ }
1156
+
1157
+ /** Whether an href points at a `/company/` page rather than a member. */
1158
+ function isCompanyHref(href?: string): boolean {
1159
+ return /\/company\//i.test(href ?? "");
1160
+ }
1161
+
1162
+ /**
1163
+ * Resolve a member's `/in/<slug>` from the card's anchors by matching a target
1164
+ * display name to the anchor's accessible name. Matching by NAME (not DOM
1165
+ * position) makes attribution robust to link count and order.
1166
+ *
1167
+ * Attribution requires the name to resolve UNAMBIGUOUSLY to one member. It
1168
+ * returns undefined when:
1169
+ * - a company anchor shares the name (the target is that company, e.g. a
1170
+ * company author whose name also appears on a person mention), or
1171
+ * - more than one distinct member slug matches the name.
1172
+ * This prevents a same-named person mention from being promoted to author when
1173
+ * the real author is a company or a different same-named person.
1174
+ */
1175
+ function resolveMemberSlug(
1176
+ links: HomeFeedLink[],
1177
+ targetName: string
1178
+ ): string | undefined {
1179
+ const target = nameKey(targetName);
1180
+ if (!target) return undefined;
1181
+ const memberSlugs = new Set<string>();
1182
+ let companyMatch = false;
1183
+ for (const link of links) {
1184
+ if (nameKey(linkDisplayName(link)) !== target) continue;
1185
+ const slug = normalizeLinkedInSlug(link.href);
1186
+ if (slug) memberSlugs.add(slug);
1187
+ else if (isCompanyHref(link.href)) companyMatch = true;
1188
+ }
1189
+ if (companyMatch || memberSlugs.size !== 1) return undefined;
1190
+ return [...memberSlugs][0];
1191
+ }
1192
+
1193
+ /**
1194
+ * The home feed mixes in ads, suggestions, and non-post noise. These never
1195
+ * become useful events, so drop them before emitting. Heuristic by necessity —
1196
+ * the home feed has no structured "is this an ad" field over the content-script
1197
+ * scrape.
1198
+ */
1199
+ export function isHomeFeedNoise(
1200
+ body: string,
1201
+ signals: {
1202
+ authorControlLabel?: string;
1203
+ hasDurableIdentity?: boolean;
1204
+ } = {}
1205
+ ): boolean {
1206
+ if (!body || body.trim().length < 30) return true;
1207
+ if (/\bPromoted\b/i.test(body.slice(0, 130))) return true;
1208
+ if (/\bSuggested\b/i.test(body.slice(0, 30))) return true;
1209
+ // Current feed modules use the same outer componentkey shape and "Feed post"
1210
+ // prefix as posts, but have neither a post control nor an author/time header.
1211
+ // Real member/company cards expose at least one of those signals. Keeping the
1212
+ // header fallback makes a broken control-menu selector fail the durable-id
1213
+ // health check instead of silently dropping genuine posts.
1214
+ const text = body.replace(/^feed post\s+/i, "").trim();
1215
+ const hasMemberHeader = text.includes(" • ") || /^\S.+\s+Author\b/.test(text);
1216
+ const hasPostControl = Boolean(
1217
+ parseAuthorControlLabel(signals.authorControlLabel)
1218
+ );
1219
+ return !hasMemberHeader && !hasPostControl && !signals.hasDurableIdentity;
1220
+ }
1221
+
1222
+ interface HomeFeedEngagement {
1223
+ reactions?: number;
1224
+ comments?: number;
1225
+ reposts?: number;
1226
+ }
1227
+
1228
+ interface HomeFeedCommentIdentity {
1229
+ urn: string;
1230
+ parentNamespace: "activity" | "share" | "ugcPost";
1231
+ parentId: string;
1232
+ commentId: string;
1233
+ }
1234
+
1235
+ interface HomeFeedRowContext {
1236
+ author: string;
1237
+ authorSlug?: string;
1238
+ postUrl?: string;
1239
+ metadata: Record<string, unknown>;
1240
+ }
1241
+
1242
+ function homeFeedPostIdentityKey(
1243
+ row: HomeFeedRow,
1244
+ context: HomeFeedRowContext
1245
+ ): string | undefined {
1246
+ for (const raw of [context.postUrl, row.post_identity]) {
1247
+ if (!raw) continue;
1248
+ const match = raw.match(
1249
+ /(?:(shareId|ugcPostId)=|urn:li:(activity|share|ugcPost):)(\d{6,})/i
1250
+ );
1251
+ if (!match) continue;
1252
+ return `${linkedInUrnNamespace(match[1] ?? match[2] ?? "")}:${match[3]}`;
1253
+ }
1254
+ return undefined;
1255
+ }
1256
+
1257
+ function homeFeedPostOriginId(identityKey: string): string {
1258
+ return `li_home_${identityKey.replace(":", "_")}`;
1259
+ }
1260
+
1261
+ function parseHomeFeedCommentIdentity(
1262
+ raw: string | undefined
1263
+ ): HomeFeedCommentIdentity | undefined {
1264
+ if (!raw) return undefined;
1265
+ // Normalize both parent forms so the same comment has one canonical URN.
1266
+ const match = raw.match(
1267
+ /urn:li:comment:\((?:urn:li:)?(activity|share|ugcPost):(\d+),(\d+)\)/i
1268
+ );
1269
+ if (!match) return undefined;
1270
+ const parentNamespace = linkedInUrnNamespace(match[1]);
1271
+ return {
1272
+ urn: `urn:li:comment:(urn:li:${parentNamespace}:${match[2]},${match[3]})`,
1273
+ parentNamespace,
1274
+ parentId: match[2],
1275
+ commentId: match[3],
1276
+ };
1277
+ }
1278
+
1279
+ function parseLinkedInCompactCount(raw: string): number {
1280
+ const match = raw.trim().match(/^(\d[\d,.]*)(?:\s*([kmb]))?$/i);
1281
+ if (!match) return 0;
1282
+ const base = Number(match[1].replace(/,/g, ""));
1283
+ if (!Number.isFinite(base)) return 0;
1284
+ const multiplier =
1285
+ match[2]?.toLowerCase() === "b"
1286
+ ? 1_000_000_000
1287
+ : match[2]?.toLowerCase() === "m"
1288
+ ? 1_000_000
1289
+ : match[2]?.toLowerCase() === "k"
1290
+ ? 1_000
1291
+ : 1;
1292
+ return Math.round(base * multiplier);
1293
+ }
1294
+
1295
+ function maxHomeFeedCounter(
1296
+ ...values: Array<string | undefined>
1297
+ ): number | undefined {
1298
+ let max = 0;
1299
+ for (const value of values) {
1300
+ if (!value) continue;
1301
+ for (const match of value.matchAll(/\b(\d[\d,.]*(?:\s*[kmb])?)\b/gi)) {
1302
+ max = Math.max(max, parseLinkedInCompactCount(match[1]));
1303
+ }
1304
+ }
1305
+ return max > 0 ? max : undefined;
1306
+ }
1307
+
1308
+ function homeFeedReactionCount(row: HomeFeedRow): number | undefined {
1309
+ for (const value of [row.reaction_count_text, row.reaction_count_label]) {
1310
+ const others = value?.match(
1311
+ /\band\s+(\d[\d,.]*(?:\s*[kmb])?)\s+others?\s+reacted\b/i
1312
+ );
1313
+ if (others) return parseLinkedInCompactCount(others[1]) + 1;
1314
+ }
1315
+
1316
+ const explicitTotal = maxHomeFeedCounter(row.reaction_count_text);
1317
+ if (explicitTotal !== undefined) return explicitTotal;
1318
+
1319
+ const label = row.reaction_count_label;
1320
+ if (!label) return undefined;
1321
+ return maxHomeFeedCounter(label);
1322
+ }
1323
+
1324
+ export function parseHomeFeedEngagement(row: HomeFeedRow): HomeFeedEngagement {
1325
+ return {
1326
+ reactions: homeFeedReactionCount(row),
1327
+ comments: maxHomeFeedCounter(
1328
+ row.comment_count_text,
1329
+ row.comment_count_label
1330
+ ),
1331
+ reposts: maxHomeFeedCounter(row.repost_count_text, row.repost_count_label),
1332
+ };
1333
+ }
1334
+
1335
+ interface HomeFeedCommentCoverageSummary {
1336
+ threads: number;
1337
+ expected: number;
1338
+ collected: number;
1339
+ incomplete: number;
1340
+ unsupported: number;
1341
+ timedOut: number;
1342
+ details: string[];
1343
+ }
1344
+
1345
+ /**
1346
+ * Prove completeness from the browser runtime's per-post expansion result.
1347
+ * The independently parsed LinkedIn counter is authoritative: a stale runtime
1348
+ * that omits coverage, or an expansion selector that reads a different count,
1349
+ * fails closed instead of silently persisting a visible-only comment subset.
1350
+ */
1351
+ function summarizeHomeFeedCommentCoverage(
1352
+ rows: HomeFeedRow[]
1353
+ ): HomeFeedCommentCoverageSummary {
1354
+ const byPost = new Map<
1355
+ string,
1356
+ { advertised: number; coverage?: HomeFeedCommentCoverage }
1357
+ >();
1358
+ for (const row of rows) {
1359
+ if (!row?.id || parseHomeFeedCommentIdentity(row.id)) continue;
1360
+ const advertised = parseHomeFeedEngagement(row).comments ?? 0;
1361
+ const expected = Math.max(advertised, row.comment_coverage?.expected ?? 0);
1362
+ if (expected <= 0) continue;
1363
+ const context = buildHomeFeedRowContext(row);
1364
+ if (isHomeFeedRowNoise(row, context)) continue;
1365
+ const identityKey = homeFeedPostIdentityKey(row, context);
1366
+ if (!identityKey) continue;
1367
+ const existing = byPost.get(identityKey);
1368
+ if (!existing || (!existing.coverage && row.comment_coverage)) {
1369
+ byPost.set(identityKey, {
1370
+ advertised: expected,
1371
+ ...(row.comment_coverage ? { coverage: row.comment_coverage } : {}),
1372
+ });
1373
+ }
1374
+ }
1375
+
1376
+ let expected = 0;
1377
+ let collected = 0;
1378
+ let incomplete = 0;
1379
+ let unsupported = 0;
1380
+ let timedOut = 0;
1381
+ const details: string[] = [];
1382
+ for (const thread of byPost.values()) {
1383
+ expected += thread.advertised;
1384
+ if (!thread.coverage) {
1385
+ unsupported++;
1386
+ incomplete++;
1387
+ details.push(`${thread.advertised}/unknown/unknown`);
1388
+ continue;
1389
+ }
1390
+ collected += Math.min(thread.coverage.collected, thread.advertised);
1391
+ if (
1392
+ !thread.coverage.complete ||
1393
+ thread.coverage.expected !== thread.advertised ||
1394
+ thread.coverage.collected < thread.advertised
1395
+ ) {
1396
+ incomplete++;
1397
+ if (thread.coverage.timedOut) timedOut++;
1398
+ details.push(
1399
+ `${thread.advertised}/${thread.coverage.expected}/${thread.coverage.collected}`
1400
+ );
1401
+ }
1402
+ }
1403
+ return {
1404
+ threads: byPost.size,
1405
+ expected,
1406
+ collected,
1407
+ incomplete,
1408
+ unsupported,
1409
+ timedOut,
1410
+ details,
1411
+ };
1412
+ }
1413
+
1414
+ function homeFeedEngagementMetadata(
1415
+ counts: HomeFeedEngagement
1416
+ ): Record<string, number> {
1417
+ return {
1418
+ ...(counts.reactions ? { reactions: counts.reactions } : {}),
1419
+ ...(counts.comments ? { comments: counts.comments } : {}),
1420
+ ...(counts.reposts ? { reposts: counts.reposts } : {}),
1421
+ };
1422
+ }
1423
+
1424
+ function homeFeedEngagementScore(counts: HomeFeedEngagement): number {
1425
+ return Math.min(
1426
+ (counts.reactions ?? 0) +
1427
+ (counts.comments ?? 0) * 2 +
1428
+ (counts.reposts ?? 0) * 3,
1429
+ 100
1430
+ );
1431
+ }
1432
+
1433
+ function normalizeHomeFeedMedia(raw: unknown): Array<Record<string, unknown>> {
1434
+ if (!Array.isArray(raw)) return [];
1435
+ const seen = new Set<string>();
1436
+ const attachments: Array<Record<string, unknown>> = [];
1437
+ for (const item of raw) {
1438
+ if (!item || typeof item !== "object") continue;
1439
+ const candidate = item as HomeFeedMedia;
1440
+ if (!candidate.url) continue;
1441
+ let url: URL;
1442
+ try {
1443
+ url = new URL(candidate.url, "https://www.linkedin.com");
1444
+ } catch {
1445
+ continue;
1446
+ }
1447
+ if (
1448
+ url.protocol !== "https:" ||
1449
+ !(
1450
+ url.hostname === "media.licdn.com" ||
1451
+ url.hostname.endsWith(".licdn.com")
1452
+ ) ||
1453
+ /(?:profile-displayphoto|company-logo)/i.test(url.pathname) ||
1454
+ seen.has(url.href)
1455
+ ) {
1456
+ continue;
1457
+ }
1458
+ seen.add(url.href);
1459
+ const altText = candidate.alt_text?.trim();
1460
+ attachments.push({
1461
+ kind: "image",
1462
+ url: url.href,
1463
+ ...(altText ? { alt_text: altText } : {}),
1464
+ });
1465
+ }
1466
+ return attachments;
1467
+ }
1468
+
1469
+ function buildHomeFeedRowContext(row: HomeFeedRow): HomeFeedRowContext {
1470
+ const parsedAuthor = parseHomeFeedAuthorDetails(row.body ?? "");
1471
+ const domAuthor = cleanHomeFeedDisplayName(
1472
+ (row.author ?? "").trim().split(" • ")[0] ?? ""
1473
+ );
1474
+ const actorBanner =
1475
+ parsedAuthor.banner?.kind === "actor" ? parsedAuthor.banner : null;
1476
+ const controlAuthor = parseAuthorControlLabel(row.author_control_label);
1477
+ let banner = parsedAuthor.banner;
1478
+ if (actorBanner && controlAuthor) {
1479
+ if (nameKey(parsedAuthor.author) !== nameKey(controlAuthor)) banner = null;
1480
+ } else if (
1481
+ actorBanner &&
1482
+ domAuthor &&
1483
+ actorBanner.actor.localeCompare(domAuthor, undefined, {
1484
+ sensitivity: "accent",
1485
+ }) !== 0
1486
+ ) {
1487
+ banner = null;
1488
+ }
1489
+ const author =
1490
+ controlAuthor ||
1491
+ (banner ? parsedAuthor.author : "") ||
1492
+ domAuthor ||
1493
+ parsedAuthor.author;
1494
+ const links = normalizeHomeFeedLinks(row.links);
1495
+ const engagement = banner?.kind === "actor" ? banner : null;
1496
+ const authorSlug = resolveMemberSlug(links, author);
1497
+ const engagerSlug = engagement
1498
+ ? resolveMemberSlug(links, engagement.actor)
1499
+ : undefined;
1500
+ const profileUrlFor = (slug: string) => `https://www.linkedin.com/in/${slug}`;
1501
+ const counts = parseHomeFeedEngagement(row);
1502
+ const metadata: Record<string, unknown> = {
1503
+ author,
1504
+ ...homeFeedEngagementMetadata(counts),
1505
+ };
1506
+ if (engagement) {
1507
+ metadata.social_actor = engagement.actor;
1508
+ metadata.social_action = engagement.action;
1509
+ if (engagerSlug) {
1510
+ metadata.social_actor_slug = engagerSlug;
1511
+ metadata.social_actor_profile_url = profileUrlFor(engagerSlug);
1512
+ }
1513
+ }
1514
+ if (authorSlug) {
1515
+ metadata.author_linkedin_slug = authorSlug;
1516
+ metadata.author_profile_url = profileUrlFor(authorSlug);
1517
+ }
1518
+
1519
+ const embeddedUrn = row.post_identity?.match(
1520
+ /(?:(shareId|ugcPostId)=|urn:li:(activity|share|ugcPost):)(\d{6,})/i
1521
+ );
1522
+ const embeddedId = embeddedUrn?.[3];
1523
+ const embeddedNamespace = linkedInUrnNamespace(
1524
+ embeddedUrn?.[1] ?? embeddedUrn?.[2] ?? ""
1525
+ );
1526
+ const postUrl = normalizeLinkedInPostUrl(
1527
+ row.post_url ??
1528
+ (embeddedId ? `urn:li:${embeddedNamespace}:${embeddedId}` : "")
1529
+ );
1530
+ return {
1531
+ author,
1532
+ authorSlug,
1533
+ postUrl: postUrl || undefined,
1534
+ metadata,
1535
+ };
1536
+ }
1537
+
1538
+ function isHomeFeedRowNoise(
1539
+ row: HomeFeedRow,
1540
+ context: HomeFeedRowContext
1541
+ ): boolean {
1542
+ return isHomeFeedNoise(row.body ?? "", {
1543
+ authorControlLabel: row.author_control_label,
1544
+ hasDurableIdentity: Boolean(homeFeedPostIdentityKey(row, context)),
1545
+ });
1546
+ }
1547
+
1548
+ /**
1549
+ * Map cs_scrape home-feed rows to event envelopes. A scraped permalink still
1550
+ * wins when a row carries one, but current cards expose none, so the canonical
1551
+ * post identity is normally the activity/share/ugcPost id embedded in
1552
+ * `post_identity`. The scrape-only componentkey never becomes an event origin,
1553
+ * and a post without either durable identity is not emitted. Native comments
1554
+ * carry their own durable id and point at the stable parent-post origin.
1555
+ * Home-feed posts expose no reliable timestamp, so the caller stamps
1556
+ * occurred_at with the sync time.
1557
+ */
1558
+ export function buildHomeFeedEvents(
1559
+ rows: HomeFeedRow[],
1560
+ occurredAt: Date
1561
+ ): EventEnvelope[] {
1562
+ const seenOrigins = new Set<string>();
1563
+ const events: EventEnvelope[] = [];
1564
+ // Each row carries its own context: LinkedIn recycles the componentkey, so
1565
+ // keying per-row state on row.id would let one card's author and permalink
1566
+ // stand in for a different post that reused the key.
1567
+ const prepared: Array<{
1568
+ row: HomeFeedRow;
1569
+ body: string;
1570
+ context: HomeFeedRowContext;
1571
+ nativeIdentity?: HomeFeedCommentIdentity;
1572
+ parentCommentIdentity?: HomeFeedCommentIdentity;
1573
+ }> = [];
1574
+ const postContextsByIdentity = new Map<string, HomeFeedRowContext>();
1575
+ const commentContextsById = new Map<string, HomeFeedRowContext>();
1576
+ for (const row of rows) {
1577
+ if (!row?.id || !row.body) continue;
1578
+ const nativeIdentity = parseHomeFeedCommentIdentity(row.id);
1579
+ const enclosingCommentIdentity = parseHomeFeedCommentIdentity(
1580
+ row.parent_comment_identity
1581
+ );
1582
+ const parentCommentIdentity =
1583
+ enclosingCommentIdentity?.commentId !== nativeIdentity?.commentId
1584
+ ? enclosingCommentIdentity
1585
+ : undefined;
1586
+ const context = buildHomeFeedRowContext(row);
1587
+ if (!nativeIdentity && isHomeFeedRowNoise(row, context)) {
1588
+ continue;
1589
+ }
1590
+ prepared.push({
1591
+ row,
1592
+ body: row.body,
1593
+ context,
1594
+ ...(nativeIdentity ? { nativeIdentity } : {}),
1595
+ ...(parentCommentIdentity ? { parentCommentIdentity } : {}),
1596
+ });
1597
+ if (nativeIdentity) {
1598
+ commentContextsById.set(nativeIdentity.commentId, context);
1599
+ } else {
1600
+ const identityKey = homeFeedPostIdentityKey(row, context);
1601
+ if (identityKey && !postContextsByIdentity.has(identityKey)) {
1602
+ postContextsByIdentity.set(identityKey, context);
1603
+ }
1604
+ }
1605
+ }
1606
+ for (const {
1607
+ row,
1608
+ body,
1609
+ context,
1610
+ nativeIdentity,
1611
+ parentCommentIdentity,
1612
+ } of prepared) {
1613
+ if (nativeIdentity) {
1614
+ const originId = `li_comment_${nativeIdentity.commentId}`;
1615
+ if (seenOrigins.has(originId)) continue;
1616
+ seenOrigins.add(originId);
1617
+ const parentIdentityKey = `${nativeIdentity.parentNamespace}:${nativeIdentity.parentId}`;
1618
+ const parentOriginId = homeFeedPostOriginId(parentIdentityKey);
1619
+ const immediateParentOriginId = parentCommentIdentity
1620
+ ? `li_comment_${parentCommentIdentity.commentId}`
1621
+ : parentOriginId;
1622
+ const parent = postContextsByIdentity.get(parentIdentityKey);
1623
+ const parentComment = parentCommentIdentity
1624
+ ? commentContextsById.get(parentCommentIdentity.commentId)
1625
+ : undefined;
1626
+ const sourceUrl =
1627
+ parent?.postUrl ??
1628
+ normalizeLinkedInPostUrl(
1629
+ `urn:li:${nativeIdentity.parentNamespace}:${nativeIdentity.parentId}`
1630
+ );
1631
+ const counts = parseHomeFeedEngagement(row);
1632
+ const commentMetadata: Record<string, unknown> = {
1633
+ ...context.metadata,
1634
+ ...homeFeedEngagementMetadata(counts),
1635
+ parent_post_origin_id: parentOriginId,
1636
+ comment_urn: nativeIdentity.urn,
1637
+ comment_id: nativeIdentity.commentId,
1638
+ parent_activity_id: nativeIdentity.parentId,
1639
+ parent_activity_namespace: nativeIdentity.parentNamespace,
1640
+ };
1641
+ if (parentCommentIdentity) {
1642
+ commentMetadata.parent_comment_origin_id = immediateParentOriginId;
1643
+ commentMetadata.parent_comment_id = parentCommentIdentity.commentId;
1644
+ commentMetadata.is_reply = true;
1645
+ if (parentComment) {
1646
+ commentMetadata.parent_comment_author = parentComment.author;
1647
+ if (parentComment.authorSlug) {
1648
+ commentMetadata.parent_comment_author_linkedin_slug =
1649
+ parentComment.authorSlug;
1650
+ commentMetadata.parent_comment_author_profile_url = `https://www.linkedin.com/in/${parentComment.authorSlug}`;
1651
+ }
1652
+ }
1653
+ }
1654
+ if (parent) {
1655
+ commentMetadata.parent_author = parent.author;
1656
+ if (parent.authorSlug) {
1657
+ commentMetadata.parent_author_linkedin_slug = parent.authorSlug;
1658
+ commentMetadata.parent_author_profile_url = `https://www.linkedin.com/in/${parent.authorSlug}`;
1659
+ }
1660
+ }
1661
+ events.push({
1662
+ origin_id: originId,
1663
+ origin_parent_id: immediateParentOriginId,
1664
+ payload_text: body,
1665
+ attachments: normalizeHomeFeedMedia(row.comment_media),
1666
+ author_name: context.author,
1667
+ occurred_at: occurredAt,
1668
+ origin_type: "comment",
1669
+ ...(sourceUrl ? { source_url: sourceUrl } : {}),
1670
+ score: homeFeedEngagementScore(counts),
1671
+ metadata: commentMetadata,
1672
+ });
1673
+ continue;
1674
+ }
1675
+
1676
+ const identityKey = homeFeedPostIdentityKey(row, context);
1677
+ if (!identityKey) continue;
1678
+ const originId = homeFeedPostOriginId(identityKey);
1679
+ if (seenOrigins.has(originId)) continue;
1680
+ seenOrigins.add(originId);
1681
+ const counts = parseHomeFeedEngagement(row);
1682
+ // The identity key already proved a durable post id, so a canonical URL is
1683
+ // always derivable. Deriving it from the key is the normal path now that
1684
+ // post_url comes back empty; it also covers a scraped permalink that did
1685
+ // not normalize (post_url holding the bare /feed/ surface).
1686
+ const sourceUrl =
1687
+ context.postUrl ?? normalizeLinkedInPostUrl(`urn:li:${identityKey}`);
1688
+
1689
+ events.push({
1690
+ origin_id: originId,
1691
+ payload_text: body,
1692
+ attachments: normalizeHomeFeedMedia(row.post_media),
1693
+ author_name: context.author,
1694
+ // Feed posts expose no reliable timestamp; use the sync time.
1695
+ occurred_at: occurredAt,
1696
+ origin_type: "post",
1697
+ score: homeFeedEngagementScore(counts),
1698
+ // source_url is the durable post handle action callers (prepare_comment)
1699
+ // resolve directly.
1700
+ ...(sourceUrl ? { source_url: sourceUrl } : {}),
1701
+ metadata: context.metadata,
1702
+ });
1703
+ }
1704
+ return events;
1705
+ }
1706
+
1707
+ // ── prepare_comment handoff (browser stage, human submits) ───────────
1708
+
1709
+ type PrepareCommentResult = {
1710
+ prepared: boolean;
1711
+ tab_id: number;
1712
+ post_url: string;
1713
+ body: string;
1714
+ method: "evaluate";
1715
+ /** Whether the in-page handoff banner was injected. */
1716
+ banner_shown?: boolean;
1717
+ /** Truncated reason shown on the banner (if any). */
1718
+ reason_preview?: string;
1719
+ message: string;
1720
+ };
1721
+
1722
+ /**
1723
+ * Canonical casing for the LinkedIn URN namespace an id came from. share and
1724
+ * ugcPost ids live in different id spaces than activity ids, so the namespace
1725
+ * has to survive normalization; /feed/update/ resolves all three.
1726
+ */
1727
+ function linkedInUrnNamespace(token: string): "activity" | "share" | "ugcPost" {
1728
+ const normalized = token.toLowerCase();
1729
+ if (normalized === "shareid" || normalized === "share") return "share";
1730
+ if (normalized === "ugcpostid" || normalized === "ugcpost") return "ugcPost";
1731
+ return "activity";
1732
+ }
1733
+
1734
+ /**
1735
+ * Normalize a post URL or activity id into a linkedin.com update URL.
1736
+ * Accepts full URLs, `urn:li:activity:…` / `urn:li:share:…` /
1737
+ * `urn:li:ugcPost:…`, bare activity digits, or `/feed/update/…` paths.
1738
+ */
1739
+ export function normalizeLinkedInPostUrl(input: string): string | null {
1740
+ const raw = input.trim();
1741
+ if (!raw) return null;
1742
+
1743
+ // Bare activity id (digits only, possibly with li_post_ prefix from events).
1744
+ const bareId = raw.replace(/^li_post_/, "");
1745
+ if (/^\d{6,}$/.test(bareId)) {
1746
+ return `https://www.linkedin.com/feed/update/urn:li:activity:${bareId}`;
1747
+ }
1748
+
1749
+ // urn:li:activity:123, urn:li:share:123, urn:li:ugcPost:123, or the bare
1750
+ // `activity:123` shorthand.
1751
+ const urnMatch = raw.match(/(?:urn:li:)?(activity|share|ugcPost):(\d{6,})/i);
1752
+ if (urnMatch && !raw.includes("://") && !raw.startsWith("/")) {
1753
+ const ns = linkedInUrnNamespace(urnMatch[1] ?? "");
1754
+ return `https://www.linkedin.com/feed/update/urn:li:${ns}:${urnMatch[2] ?? ""}`;
1755
+ }
1756
+
1757
+ try {
1758
+ const withScheme = raw.startsWith("//")
1759
+ ? `https:${raw}`
1760
+ : raw.startsWith("/")
1761
+ ? `https://www.linkedin.com${raw}`
1762
+ : raw.includes("://")
1763
+ ? raw
1764
+ : `https://${raw}`;
1765
+ const u = new URL(withScheme);
1766
+ if (u.protocol !== "https:" && u.protocol !== "http:") return null;
1767
+ u.protocol = "https:";
1768
+ const host = u.hostname.toLowerCase().replace(/^www\./, "");
1769
+ if (host !== "linkedin.com" && !host.endsWith(".linkedin.com")) {
1770
+ return null;
1771
+ }
1772
+ // Prefer canonical feed/update URLs when we can extract a post id, keeping
1773
+ // whichever URN namespace the source URL used.
1774
+ const urnInUrl = `${u.pathname}${u.search}`.match(
1775
+ /(?:urn(?::|%3A)li(?::|%3A))?(activity|share|ugcPost)(?::|%3A|-)(\d{6,})/i
1776
+ );
1777
+ if (urnInUrl) {
1778
+ const ns = linkedInUrnNamespace(urnInUrl[1] ?? "");
1779
+ return `https://www.linkedin.com/feed/update/urn:li:${ns}:${urnInUrl[2] ?? ""}`;
1780
+ }
1781
+ return null;
1782
+ } catch {
1783
+ return null;
1784
+ }
1785
+ }
1786
+
1787
+ function isLinkedInShortPostUrl(input: string | undefined): boolean {
1788
+ if (!input) return false;
1789
+ try {
1790
+ const url = new URL(input);
1791
+ return (
1792
+ url.protocol === "https:" &&
1793
+ url.hostname === "lnkd.in" &&
1794
+ url.pathname.startsWith("/p/")
1795
+ );
1796
+ } catch {
1797
+ return false;
1798
+ }
1799
+ }
1800
+
1801
+ /** Resolve LinkedIn's Copy-link short URL to its durable post URN URL. */
1802
+ export async function resolveLinkedInShortPostUrl(
1803
+ input: string,
1804
+ fetchImpl: typeof fetch = fetch
1805
+ ): Promise<string | null> {
1806
+ let shortUrl: URL;
1807
+ try {
1808
+ shortUrl = new URL(input);
1809
+ } catch {
1810
+ return null;
1811
+ }
1812
+ if (!isLinkedInShortPostUrl(shortUrl.href)) return null;
1813
+
1814
+ try {
1815
+ const response = await fetchImpl(shortUrl, {
1816
+ method: "HEAD",
1817
+ redirect: "manual",
1818
+ signal: AbortSignal.timeout(5_000),
1819
+ });
1820
+ const location = response.headers.get("location");
1821
+ if (!location) return normalizeLinkedInPostUrl(response.url);
1822
+ return normalizeLinkedInPostUrl(new URL(location, shortUrl).href);
1823
+ } catch {
1824
+ return null;
1825
+ }
1826
+ }
1827
+
1828
+ export async function resolveHomeFeedPostUrls(
1829
+ rows: HomeFeedRow[],
1830
+ fetchImpl: typeof fetch = fetch
1831
+ ): Promise<HomeFeedRow[]> {
1832
+ return Promise.all(
1833
+ rows.map(async (row) => {
1834
+ const postUrl = row.post_url?.trim();
1835
+ if (!postUrl || normalizeLinkedInPostUrl(postUrl)) return row;
1836
+ const resolved = await resolveLinkedInShortPostUrl(postUrl, fetchImpl);
1837
+ return resolved ? { ...row, post_url: resolved } : row;
1838
+ })
1839
+ );
1840
+ }
1841
+
1842
+ function unresolvedHomeFeedPostRows(rows: HomeFeedRow[]): HomeFeedRow[] {
1843
+ return rows.filter((row) => {
1844
+ if (!row?.id || !row.body) return false;
1845
+ const context = buildHomeFeedRowContext(row);
1846
+ if (
1847
+ parseHomeFeedCommentIdentity(row.id) ||
1848
+ isHomeFeedRowNoise(row, context)
1849
+ ) {
1850
+ return false;
1851
+ }
1852
+ return !homeFeedPostIdentityKey(row, context);
1853
+ });
1854
+ }
1855
+
1856
+ function unresolvedHomeFeedShortUrlCount(rows: HomeFeedRow[]): number {
1857
+ return rows.filter((row) => {
1858
+ if (!row?.id || !row.body || parseHomeFeedCommentIdentity(row.id)) {
1859
+ return false;
1860
+ }
1861
+ const context = buildHomeFeedRowContext(row);
1862
+ return (
1863
+ !isHomeFeedRowNoise(row, context) && isLinkedInShortPostUrl(row.post_url)
1864
+ );
1865
+ }).length;
1866
+ }
1867
+
1868
+ /** True when navigate landed on login / authwall rather than the post. */
1869
+ export function isLinkedInAuthWall(url: string | null | undefined): boolean {
1870
+ if (!url) return false;
1871
+ const lower = url.toLowerCase();
1872
+ return (
1873
+ lower.includes("/login") ||
1874
+ lower.includes("/authwall") ||
1875
+ lower.includes("/checkpoint") ||
1876
+ lower.includes("uas/login")
1877
+ );
1878
+ }
1879
+
1880
+ function chromeOriginsInput(): { allowed_origins: string[] } {
1881
+ return { allowed_origins: LINKEDIN_ALLOWED_ORIGINS };
1882
+ }
1883
+
1884
+ /** Cap the explanation shown in the in-page banner. */
1885
+ export const truncateHandoffReason = truncateSharedHandoffReason;
1886
+
1887
+ const LINKEDIN_HANDOFF_INSTRUCTION =
1888
+ "Review the draft and click Post yourself \u2014 Lobu did not submit.";
1889
+ const LINKEDIN_HANDOFF_FOOTER =
1890
+ "After posting or editing, record the outcome in Lobu Activity. You can also reject it with a reason.";
1891
+ const LINKEDIN_HANDOFF_COMPOSER_SELECTORS = [
1892
+ ".comments-comment-box-comment__text-editor .ql-editor",
1893
+ ".comments-comment-box__form .ql-editor",
1894
+ 'div.ql-editor[contenteditable="true"]',
1895
+ 'div[role="textbox"][contenteditable="true"]',
1896
+ ];
1897
+ const LINKEDIN_HANDOFF_HOST_SELECTORS = [".comments-comment-box", "form"];
1898
+
1899
+ /**
1900
+ * JS expression: inject a small Lobu handoff banner on the page.
1901
+ * The banner points the user to the side panel for the run details.
1902
+ * Best-effort; LinkedIn re-renders may remove it.
1903
+ */
1904
+ export function buildInjectHandoffBannerExpression(opts: {
1905
+ reason?: string;
1906
+ title?: string;
1907
+ }): string {
1908
+ return buildSharedHandoffBannerExpression({
1909
+ reason: opts.reason,
1910
+ title: opts.title ?? "Lobu staged this comment",
1911
+ instruction: LINKEDIN_HANDOFF_INSTRUCTION,
1912
+ footer: LINKEDIN_HANDOFF_FOOTER,
1913
+ composerSelectors: LINKEDIN_HANDOFF_COMPOSER_SELECTORS,
1914
+ hostSelectors: LINKEDIN_HANDOFF_HOST_SELECTORS,
1915
+ });
1916
+ }
1917
+
1918
+ /** JS expression: open the comment composer if collapsed, then fill it. */
1919
+ export function buildFillCommentExpression(body: string): string {
1920
+ // JSON.stringify for safe embedding of the draft text.
1921
+ const textLit = JSON.stringify(body);
1922
+ return `(async()=>{
1923
+ const text = ${textLit};
1924
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
1925
+
1926
+ function visible(el) {
1927
+ if (!el) return false;
1928
+ const s = window.getComputedStyle(el);
1929
+ if (s.display === 'none' || s.visibility === 'hidden' || s.opacity === '0') return false;
1930
+ const r = el.getBoundingClientRect();
1931
+ return r.width > 0 && r.height > 0;
1932
+ }
1933
+
1934
+ function findComposer() {
1935
+ const selectors = [
1936
+ '.comments-comment-box-comment__text-editor .ql-editor',
1937
+ '.comments-comment-box__form .ql-editor',
1938
+ 'form.comments-comment-box__form div.ql-editor',
1939
+ 'div.comments-comment-texteditor .ql-editor',
1940
+ 'div.ql-editor[contenteditable="true"]',
1941
+ 'div[role="textbox"][contenteditable="true"]',
1942
+ 'div[contenteditable="true"][aria-label*="comment" i]',
1943
+ 'div[contenteditable="true"][data-placeholder*="comment" i]',
1944
+ ];
1945
+ for (const sel of selectors) {
1946
+ try {
1947
+ const el = document.querySelector(sel);
1948
+ if (el && visible(el)) return el;
1949
+ } catch (_) {}
1950
+ }
1951
+ // Broader: any visible contenteditable textbox in a comments region.
1952
+ const regions = document.querySelectorAll(
1953
+ '.comments-comment-box, .feed-shared-update-v2__comments-container, [class*="comments-comment"]'
1954
+ );
1955
+ for (const region of regions) {
1956
+ const el = region.querySelector('[contenteditable="true"]');
1957
+ if (el && visible(el)) return el;
1958
+ }
1959
+ return null;
1960
+ }
1961
+
1962
+ // SAFETY: never click submit controls. Only the human posts.
1963
+ function isSubmitLabel(label) {
1964
+ const n = String(label || '').replace(/\\s+/g, ' ').trim();
1965
+ if (/^(post|submit|send|publish|share)$/i.test(n)) return true;
1966
+ if (/^(post|submit|send)\\s+(comment|reply)$/i.test(n)) return true;
1967
+ return false;
1968
+ }
1969
+ function isOpenLabel(label) {
1970
+ if (isSubmitLabel(label)) return false;
1971
+ const n = String(label || '').replace(/\\s+/g, ' ').trim();
1972
+ return (
1973
+ /^comment$/i.test(n) ||
1974
+ /^comments$/i.test(n) ||
1975
+ /^comment on /i.test(n) ||
1976
+ /^leave a comment/i.test(n) ||
1977
+ /^post a comment/i.test(n)
1978
+ );
1979
+ }
1980
+
1981
+ function openComposer() {
1982
+ if (findComposer()) return true;
1983
+ const nodes = Array.from(
1984
+ document.querySelectorAll('button, [role="button"]')
1985
+ );
1986
+ for (const el of nodes) {
1987
+ if (!visible(el)) continue;
1988
+ const label = (
1989
+ el.getAttribute('aria-label') ||
1990
+ el.innerText ||
1991
+ el.textContent ||
1992
+ ''
1993
+ )
1994
+ .replace(/\\s+/g, ' ')
1995
+ .trim();
1996
+ if (isSubmitLabel(label)) continue;
1997
+ if (isOpenLabel(label)) {
1998
+ el.click();
1999
+ return true;
2000
+ }
2001
+ }
2002
+ return false;
2003
+ }
2004
+
2005
+ openComposer();
2006
+ await sleep(600);
2007
+ let composer = findComposer();
2008
+ if (!composer) {
2009
+ openComposer();
2010
+ await sleep(800);
2011
+ composer = findComposer();
2012
+ }
2013
+ if (!composer) {
2014
+ return { ok: false, reason: 'composer_not_found' };
2015
+ }
2016
+
2017
+ const existing = (composer.innerText || composer.textContent || '').trim();
2018
+ if (existing && existing !== text.trim()) {
2019
+ return { ok: false, reason: 'existing_draft', submitted: false };
2020
+ }
2021
+ if (existing === text.trim()) {
2022
+ return { ok: true, reason: 'already_prepared', preview: existing.slice(0, 120), submitted: false };
2023
+ }
2024
+ composer.focus();
2025
+ try {
2026
+ document.execCommand('selectAll', false, undefined);
2027
+ document.execCommand('delete', false, undefined);
2028
+ } catch (_) {
2029
+ composer.textContent = '';
2030
+ }
2031
+ // insertText only — never Enter / Cmd+Enter (those can submit on LinkedIn).
2032
+ const inserted = document.execCommand('insertText', false, text);
2033
+ if (!inserted) {
2034
+ composer.textContent = text;
2035
+ composer.dispatchEvent(new InputEvent('input', { bubbles: true, data: text, inputType: 'insertText' }));
2036
+ }
2037
+ await sleep(100);
2038
+ const got = (composer.innerText || composer.textContent || '').trim();
2039
+ // Scroll composer into view so the human sees the draft. Do NOT click Post.
2040
+ try { composer.scrollIntoView({ block: 'center', behavior: 'smooth' }); } catch (_) {}
2041
+ return {
2042
+ ok: got.length > 0,
2043
+ reason: got.length > 0 ? 'typed' : 'empty_after_type',
2044
+ preview: got.slice(0, 120),
2045
+ used_insert_text: !!inserted,
2046
+ submitted: false,
2047
+ };
2048
+ })()`;
2049
+ }
2050
+
2051
+ /**
2052
+ * Stage a LinkedIn comment in the exact user-opened page: activate → fill
2053
+ * composer. The ordinary Lobu notification is created by the calling Automation.
2054
+ *
2055
+ * HARD RULE — never auto-post:
2056
+ * - never click Post / Submit / Send
2057
+ * - never press Enter / Cmd+Enter after typing
2058
+ * - only open the composer (Comment) + type draft text
2059
+ * The human must click Post themselves.
2060
+ */
2061
+ export async function prepareLinkedInComment(
2062
+ dispatcher: ChromeActionDispatcher,
2063
+ opts: {
2064
+ postUrl: string;
2065
+ body: string;
2066
+ /** Short explanation for the in-page banner. */
2067
+ reason?: string;
2068
+ /** Inject in-page handoff banner (default true). */
2069
+ banner?: boolean;
2070
+ }
2071
+ ): Promise<PrepareCommentResult> {
2072
+ const body = opts.body.trim();
2073
+ if (!body) {
2074
+ throw new Error("prepare_comment: body must be non-empty");
2075
+ }
2076
+ const postUrl = normalizeLinkedInPostUrl(opts.postUrl);
2077
+ if (!postUrl) {
2078
+ throw new Error(
2079
+ `prepare_comment: invalid post_url / activity_id: ${JSON.stringify(opts.postUrl)}`
2080
+ );
2081
+ }
2082
+ const banner = opts.banner !== false;
2083
+ const reasonPreview = truncateHandoffReason(opts.reason);
2084
+
2085
+ const nav = await dispatcher.dispatch<{
2086
+ tab_id?: number;
2087
+ current_url?: string;
2088
+ title?: string;
2089
+ }>("navigate", {
2090
+ // LinkedIn rewrites /feed/update/<urn> to its trailing-slash form after
2091
+ // load, and an activated tab is only drivable on its exact URL, so the
2092
+ // caller's activation target is this same trailing-slash page URL.
2093
+ url: `${postUrl}/`,
2094
+ require_page_activation: true,
2095
+ ...chromeOriginsInput(),
2096
+ });
2097
+ const tabId = nav.tab_id;
2098
+ if (typeof tabId !== "number") {
2099
+ throw new Error("prepare_comment: navigate did not return tab_id");
2100
+ }
2101
+ if (isLinkedInAuthWall(nav.current_url)) {
2102
+ throw new Error(
2103
+ `Not logged into LinkedIn (landed on ${nav.current_url}). Sign in in the paired Chrome profile, then retry.`
2104
+ );
2105
+ }
2106
+
2107
+ // Give the post chrome a beat to hydrate the social-action bar.
2108
+ try {
2109
+ await dispatcher.dispatch("wait_for_selector", {
2110
+ tab_id: tabId,
2111
+ selector:
2112
+ 'button, [role="button"], .feed-shared-social-action-bar, .comments-comment-box',
2113
+ timeout_ms: 8000,
2114
+ ...chromeOriginsInput(),
2115
+ });
2116
+ } catch {
2117
+ // Non-fatal — the DOM fill below still checks the composer.
2118
+ }
2119
+
2120
+ // Fill only through the DOM, which can read the composer before writing. An
2121
+ // accessibility textbox does not expose an existing draft, so there is no
2122
+ // type_ref fallback.
2123
+ let evalOut:
2124
+ | {
2125
+ value?: {
2126
+ ok?: boolean;
2127
+ reason?: string;
2128
+ preview?: string;
2129
+ submitted?: boolean;
2130
+ };
2131
+ exception?: string;
2132
+ }
2133
+ | undefined;
2134
+ try {
2135
+ evalOut = await dispatcher.dispatch<NonNullable<typeof evalOut>>(
2136
+ "evaluate",
2137
+ {
2138
+ tab_id: tabId,
2139
+ expression: buildFillCommentExpression(body),
2140
+ await_promise: true,
2141
+ ...chromeOriginsInput(),
2142
+ }
2143
+ );
2144
+ } catch {
2145
+ // Failure is reported below; never fill an editor whose contents cannot be checked.
2146
+ }
2147
+ const value = evalOut?.value;
2148
+ if (value?.reason === "existing_draft") {
2149
+ throw new Error(
2150
+ "prepare_comment: preserving your existing draft; clear it before preparing another comment"
2151
+ );
2152
+ }
2153
+ if (value?.submitted === true) {
2154
+ throw new Error(
2155
+ "prepare_comment: fill expression reported submitted=true — aborting (must never auto-post)"
2156
+ );
2157
+ }
2158
+ if (evalOut?.exception || !value?.ok) {
2159
+ throw new Error(
2160
+ "prepare_comment: could not fill comment composer (the DOM draft could not be verified). Is the post still available and commentable?"
2161
+ );
2162
+ }
2163
+
2164
+ let bannerShown = false;
2165
+ if (banner) {
2166
+ try {
2167
+ const bannerOut = await dispatcher.dispatch<{
2168
+ value?: { ok?: boolean; anchored?: boolean };
2169
+ exception?: string;
2170
+ }>("evaluate", {
2171
+ tab_id: tabId,
2172
+ expression: buildInjectHandoffBannerExpression({
2173
+ reason: reasonPreview,
2174
+ }),
2175
+ await_promise: true,
2176
+ ...chromeOriginsInput(),
2177
+ });
2178
+ bannerShown = bannerOut.value?.ok === true && !bannerOut.exception;
2179
+ } catch {
2180
+ // Banner is best-effort; draft is already staged.
2181
+ }
2182
+ }
2183
+
2184
+ return {
2185
+ prepared: true,
2186
+ tab_id: tabId,
2187
+ post_url: postUrl,
2188
+ body,
2189
+ method: "evaluate",
2190
+ banner_shown: bannerShown,
2191
+ reason_preview: reasonPreview,
2192
+ message:
2193
+ "Comment staged in your browser. Review the draft and click Post yourself — Lobu did not submit.",
2194
+ };
2195
+ }
2196
+
2197
+ // ── verify_staged_comment (read: did the human Post?) ────────────────
2198
+
2199
+ export type ScrapedComment = {
2200
+ author?: string;
2201
+ text: string;
2202
+ };
2203
+
2204
+ export type VerifyStagedCommentResult = {
2205
+ /** True when a visible comment matches the expected body. */
2206
+ verified: boolean;
2207
+ post_url: string;
2208
+ body: string;
2209
+ tab_id?: number;
2210
+ comments_scanned: number;
2211
+ match?: {
2212
+ author?: string;
2213
+ text_preview: string;
2214
+ match_kind: "exact" | "prefix" | "contains";
2215
+ };
2216
+ /** Why verification failed or was inconclusive. */
2217
+ reason?: string;
2218
+ message: string;
2219
+ };
2220
+
2221
+ /** Collapse whitespace / case for comment body comparison. */
2222
+ export function normalizeCommentMatchText(text: string): string {
2223
+ return text
2224
+ .normalize("NFKC")
2225
+ .replace(/\u00a0/g, " ")
2226
+ .replace(/\s+/g, " ")
2227
+ .trim()
2228
+ .toLowerCase();
2229
+ }
2230
+
2231
+ /**
2232
+ * Match staged draft text against a scraped comment body.
2233
+ * Allows LinkedIn "…more" truncation and longer surrounding blocks.
2234
+ */
2235
+ export function commentBodiesMatch(
2236
+ expected: string,
2237
+ actual: string
2238
+ ): { ok: true; kind: "exact" | "prefix" | "contains" } | { ok: false } {
2239
+ const e = normalizeCommentMatchText(expected);
2240
+ const a = normalizeCommentMatchText(actual);
2241
+ if (!e || !a) return { ok: false };
2242
+ if (a === e) return { ok: true, kind: "exact" };
2243
+ // Partial matches must be distinctive enough not to verify common phrases.
2244
+ if (Math.min(e.length, a.length) < 24) return { ok: false };
2245
+ // LinkedIn may truncate the visible comment before its "See more" control.
2246
+ if (e.startsWith(a)) return { ok: true, kind: "prefix" };
2247
+ // Some DOM variants wrap the comment body in a longer text block.
2248
+ if (a.includes(e)) return { ok: true, kind: "contains" };
2249
+ return { ok: false };
2250
+ }
2251
+
2252
+ /**
2253
+ * JS expression: scrape visible comments on a LinkedIn post/permalink page.
2254
+ * Best-effort DOM selectors — LinkedIn class names churn.
2255
+ */
2256
+ export function buildScrapeCommentsExpression(): string {
2257
+ return `(() => {
2258
+ const roots = [
2259
+ ...document.querySelectorAll('article.comments-comment-item'),
2260
+ ...document.querySelectorAll('.comments-comment-item'),
2261
+ ...document.querySelectorAll('.comments-comment-entity'),
2262
+ ...document.querySelectorAll('[class*="comments-comment-item"]'),
2263
+ ];
2264
+ const seen = new Set();
2265
+ const out = [];
2266
+ for (const root of roots) {
2267
+ if (!(root instanceof HTMLElement)) continue;
2268
+ if (seen.has(root)) continue;
2269
+ seen.add(root);
2270
+ const textEl =
2271
+ root.querySelector('.update-components-text') ||
2272
+ root.querySelector('.comments-comment-item__main-content') ||
2273
+ root.querySelector('[class*="comment-item__main"]') ||
2274
+ root.querySelector('.feed-shared-inline-show-more-text') ||
2275
+ root.querySelector('[data-test-id="main-feed-activity-card"] span[dir="ltr"]') ||
2276
+ root.querySelector('span[dir="ltr"]');
2277
+ let text = (textEl?.innerText || textEl?.textContent || '').replace(/\\s+/g, ' ').trim();
2278
+ // Drop "See more" / "…more" chrome.
2279
+ text = text.replace(/\\s*…?\\s*see more\\s*$/i, '').replace(/\\s*…more\\s*$/i, '').trim();
2280
+ if (!text || text.length < 2) continue;
2281
+ const authorEl =
2282
+ root.querySelector('.comments-post-meta__name-text') ||
2283
+ root.querySelector('.comments-comment-meta__description-title') ||
2284
+ root.querySelector('a[href*="/in/"] span[aria-hidden="true"]') ||
2285
+ root.querySelector('a[href*="/in/"]');
2286
+ const author = (authorEl?.textContent || '').replace(/\\s+/g, ' ').trim() || undefined;
2287
+ out.push({ author, text: text.slice(0, 2000) });
2288
+ if (out.length >= 80) break;
2289
+ }
2290
+ return { ok: true, comments: out, count: out.length, url: location.href };
2291
+ })()`;
2292
+ }
2293
+
2294
+ /**
2295
+ * Re-open a LinkedIn post and check whether a comment matching `body` is visible.
2296
+ * Read-only — never types or submits. Matching is best-effort DOM scrape.
2297
+ */
2298
+ export async function verifyLinkedInStagedComment(
2299
+ dispatcher: ChromeActionDispatcher,
2300
+ opts: {
2301
+ postUrl: string;
2302
+ body: string;
2303
+ /** Require the author's display name to include this (case-insensitive). */
2304
+ author_hint?: string;
2305
+ focus?: boolean;
2306
+ }
2307
+ ): Promise<VerifyStagedCommentResult> {
2308
+ const body = opts.body.trim();
2309
+ if (!body) {
2310
+ throw new Error("verify_staged_comment: body must be non-empty");
2311
+ }
2312
+ const postUrl = normalizeLinkedInPostUrl(opts.postUrl);
2313
+ if (!postUrl) {
2314
+ throw new Error(
2315
+ `verify_staged_comment: invalid post_url / activity_id: ${JSON.stringify(opts.postUrl)}`
2316
+ );
2317
+ }
2318
+ const authorHint = opts.author_hint?.trim().toLowerCase() || undefined;
2319
+ const focus = opts.focus !== false;
2320
+
2321
+ const nav = await dispatcher.dispatch<{
2322
+ tab_id?: number;
2323
+ current_url?: string;
2324
+ }>("navigate", {
2325
+ url: postUrl,
2326
+ new_tab: true,
2327
+ wait_for_load: true,
2328
+ ...chromeOriginsInput(),
2329
+ });
2330
+ const tabId = nav.tab_id;
2331
+ if (typeof tabId !== "number") {
2332
+ throw new Error("verify_staged_comment: navigate did not return tab_id");
2333
+ }
2334
+ if (isLinkedInAuthWall(nav.current_url)) {
2335
+ throw new Error(
2336
+ `Not logged into LinkedIn (landed on ${nav.current_url}). Sign in in the paired Chrome profile, then retry.`
2337
+ );
2338
+ }
2339
+
2340
+ try {
2341
+ await dispatcher.dispatch("wait_for_selector", {
2342
+ tab_id: tabId,
2343
+ selector:
2344
+ ".comments-comment-item, .comments-comment-entity, .feed-shared-update-v2, article",
2345
+ timeout_ms: 8000,
2346
+ ...chromeOriginsInput(),
2347
+ });
2348
+ } catch {
2349
+ // Non-fatal — page may still expose comments.
2350
+ }
2351
+
2352
+ // Best-effort: expand the comments section / load more (never Post).
2353
+ try {
2354
+ await dispatcher.dispatch("evaluate", {
2355
+ tab_id: tabId,
2356
+ expression: `(() => {
2357
+ const labels = [/^(load|show|view) more comments/i, /^\\d+\\s+comments?$/i, /^comments$/i];
2358
+ const clickables = [
2359
+ ...document.querySelectorAll('button, [role="button"], a'),
2360
+ ];
2361
+ const control = clickables.find((el) => {
2362
+ const n = (el.textContent || '').replace(/\\s+/g, ' ').trim();
2363
+ return n && labels.some((re) => re.test(n));
2364
+ });
2365
+ if (control) {
2366
+ try { control.click(); } catch (_) {}
2367
+ }
2368
+ return !!control;
2369
+ })()`,
2370
+ await_promise: false,
2371
+ ...chromeOriginsInput(),
2372
+ });
2373
+ } catch {
2374
+ // Expand is optional.
2375
+ }
2376
+
2377
+ try {
2378
+ await dispatcher.dispatch("wait_for_selector", {
2379
+ tab_id: tabId,
2380
+ selector: ".comments-comment-item, .comments-comment-entity",
2381
+ timeout_ms: 3000,
2382
+ ...chromeOriginsInput(),
2383
+ });
2384
+ } catch {
2385
+ // A post can legitimately have no comments.
2386
+ }
2387
+
2388
+ if (focus) {
2389
+ try {
2390
+ await dispatcher.dispatch("focus_tab", {
2391
+ tab_id: tabId,
2392
+ draw_attention: true,
2393
+ ...chromeOriginsInput(),
2394
+ });
2395
+ } catch {
2396
+ // Focus is best-effort.
2397
+ }
2398
+ }
2399
+
2400
+ let comments: ScrapedComment[] = [];
2401
+ try {
2402
+ const scrape = await dispatcher.dispatch<{
2403
+ value?: { comments?: unknown; count?: number };
2404
+ exception?: string;
2405
+ }>("evaluate", {
2406
+ tab_id: tabId,
2407
+ expression: buildScrapeCommentsExpression(),
2408
+ await_promise: true,
2409
+ ...chromeOriginsInput(),
2410
+ });
2411
+ if (scrape.exception) {
2412
+ return {
2413
+ verified: false,
2414
+ post_url: postUrl,
2415
+ body,
2416
+ tab_id: tabId,
2417
+ comments_scanned: 0,
2418
+ reason: `scrape_exception: ${scrape.exception}`,
2419
+ message:
2420
+ "Could not scrape comments on the page. Open the post in Chrome and confirm the comment is visible.",
2421
+ };
2422
+ }
2423
+ const raw = scrape.value?.comments;
2424
+ if (Array.isArray(raw)) {
2425
+ comments = raw.filter(
2426
+ (c): c is ScrapedComment =>
2427
+ !!c &&
2428
+ typeof c === "object" &&
2429
+ typeof c.text === "string" &&
2430
+ c.text.trim().length > 0 &&
2431
+ (c.author === undefined || typeof c.author === "string")
2432
+ );
2433
+ }
2434
+ } catch (err) {
2435
+ return {
2436
+ verified: false,
2437
+ post_url: postUrl,
2438
+ body,
2439
+ tab_id: tabId,
2440
+ comments_scanned: 0,
2441
+ reason: err instanceof Error ? err.message : String(err),
2442
+ message: "Comment scrape failed (extension evaluate error).",
2443
+ };
2444
+ }
2445
+
2446
+ const candidates = authorHint
2447
+ ? comments.filter((c) =>
2448
+ (c.author ?? "").toLowerCase().includes(authorHint)
2449
+ )
2450
+ : comments;
2451
+
2452
+ for (const c of candidates) {
2453
+ const m = commentBodiesMatch(body, c.text);
2454
+ if (!m.ok) continue;
2455
+ return {
2456
+ verified: true,
2457
+ post_url: postUrl,
2458
+ body,
2459
+ tab_id: tabId,
2460
+ comments_scanned: comments.length,
2461
+ match: {
2462
+ author: c.author,
2463
+ text_preview: c.text.slice(0, 160),
2464
+ match_kind: m.kind,
2465
+ },
2466
+ message: `Verified: a visible comment matches the staged draft${
2467
+ c.author ? ` (author: ${c.author})` : ""
2468
+ }.`,
2469
+ };
2470
+ }
2471
+
2472
+ return {
2473
+ verified: false,
2474
+ post_url: postUrl,
2475
+ body,
2476
+ tab_id: tabId,
2477
+ comments_scanned: comments.length,
2478
+ reason:
2479
+ comments.length === 0
2480
+ ? "no_comments_visible"
2481
+ : authorHint
2482
+ ? "no_matching_comment_for_author"
2483
+ : "no_matching_comment",
2484
+ message:
2485
+ comments.length === 0
2486
+ ? "No comments visible on the post yet. The human may not have posted, comments may be collapsed, or the DOM selectors missed them."
2487
+ : "Comments are visible but none matched the staged draft body. The human may have edited the text before posting.",
2488
+ };
2489
+ }
2490
+
2491
+ export function filterPostsSinceCheckpoint(
2492
+ posts: LinkedInPost[],
2493
+ checkpoint: LinkedInCheckpoint
2494
+ ): LinkedInPost[] {
2495
+ const seenIds = new Set<string>();
2496
+ const checkpointPostId = normalizeCheckpointPostId(checkpoint.last_post_id);
2497
+ const checkpointTimestamp = checkpoint.last_timestamp
2498
+ ? new Date(checkpoint.last_timestamp).getTime()
2499
+ : null;
2500
+
2501
+ const filtered: LinkedInPost[] = [];
2502
+ for (const post of posts) {
2503
+ if (!post.id || !post.text || seenIds.has(post.id)) continue;
2504
+ seenIds.add(post.id);
2505
+
2506
+ if (checkpointPostId && post.id === checkpointPostId) break;
2507
+ if (
2508
+ checkpointTimestamp !== null &&
2509
+ post.publishedAt.getTime() <= checkpointTimestamp
2510
+ ) {
2511
+ continue;
2512
+ }
2513
+
2514
+ filtered.push(post);
2515
+ }
2516
+
2517
+ return filtered;
2518
+ }
2519
+
2520
+ // ── Voyager API Response Parsers ──────────────────────────────
2521
+
2522
+ export function parseCompanyUpdates(
2523
+ _url: string,
2524
+ json: unknown
2525
+ ): LinkedInPost[] {
2526
+ const posts: LinkedInPost[] = [];
2527
+ const data = json as any;
2528
+
2529
+ // Build URN lookup from `included` array (LinkedIn GraphQL uses references)
2530
+ const included: any[] = data?.included ?? [];
2531
+ const byUrn: Record<string, any> = {};
2532
+ for (const item of included) {
2533
+ const urn = item.entityUrn || item.$id;
2534
+ if (urn) byUrn[urn] = item;
2535
+ }
2536
+
2537
+ // Find feed elements - LinkedIn nests under data.data with a long key
2538
+ const feedRoot = data?.data?.data ?? data?.data ?? data;
2539
+ let elements: any[] = [];
2540
+ for (const key of Object.keys(feedRoot)) {
2541
+ const val = feedRoot[key];
2542
+ if (val?.["*elements"] && Array.isArray(val["*elements"])) {
2543
+ elements = val["*elements"];
2544
+ break;
2545
+ }
2546
+ if (val?.elements && Array.isArray(val.elements)) {
2547
+ elements = val.elements;
2548
+ break;
2549
+ }
2550
+ }
2551
+
2552
+ const resolve = (ref: any) =>
2553
+ (typeof ref === "string" ? byUrn[ref] : ref) ?? {};
2554
+
2555
+ for (const ref of elements) {
2556
+ const el = resolve(ref);
2557
+
2558
+ // Get commentary text (may be a reference)
2559
+ const commentaryObj = resolve(el["*commentary"] ?? el.commentary);
2560
+ const textObj = commentaryObj?.text ?? commentaryObj;
2561
+ const text = textObj?.text ?? textObj?.attributedText?.text ?? "";
2562
+ if (!text) continue;
2563
+
2564
+ // Get actor
2565
+ const actorObj = resolve(el["*actor"] ?? el.actor);
2566
+ const authorName = actorObj?.name?.text ?? actorObj?.name ?? "Unknown";
2567
+ const authorDesc =
2568
+ actorObj?.description?.text ?? actorObj?.description ?? undefined;
2569
+
2570
+ // Actor identity: the member's immutable `urn:li:fsd_profile:<id>` and the
2571
+ // vanity `/in/<slug>`. Voyager threads the profile urn through several
2572
+ // shapes — the actor may be a ref into `included`, and the urn hides under
2573
+ // `*miniProfile` (a bare urn string OR a ref to resolve), `miniProfile` (a
2574
+ // bare urn string OR an object with `entityUrn`), `urn`, or `entityUrn`.
2575
+ // normalizeLinkedInMemberId returns null for a non-person urn (e.g. a
2576
+ // company actor's `urn:li:fsd_company:…`), so a company-authored post yields
2577
+ // no member id and its attribution match-only-gates off (createWhen).
2578
+ const authorUrn: string =
2579
+ (typeof actorObj?.["*miniProfile"] === "string"
2580
+ ? actorObj["*miniProfile"]
2581
+ : resolve(actorObj?.["*miniProfile"])?.entityUrn) ??
2582
+ (typeof actorObj?.miniProfile === "string"
2583
+ ? actorObj.miniProfile
2584
+ : actorObj?.miniProfile?.entityUrn) ??
2585
+ actorObj?.urn ??
2586
+ actorObj?.entityUrn ??
2587
+ "";
2588
+ const authorMemberId = normalizeLinkedInMemberId(authorUrn) ?? undefined;
2589
+ const authorProfileUrl: string =
2590
+ actorObj?.navigationContext?.actionTarget ??
2591
+ actorObj?.navigationUrl ??
2592
+ resolve(actorObj?.["*miniProfile"])?.publicIdentifier ??
2593
+ "";
2594
+ // normalizeLinkedInSlug pulls the `/in/<slug>` segment from a full URL (and
2595
+ // returns null for a company `/company/…` URL or anything slug-invalid).
2596
+ const authorSlug = normalizeLinkedInSlug(authorProfileUrl);
2597
+
2598
+ // Get social counts
2599
+ const socialRef = el["*socialDetail"] ?? el.socialDetail;
2600
+ const social = resolve(socialRef);
2601
+ const counts =
2602
+ social?.totalSocialActivityCounts ??
2603
+ social?.socialActivityCountsInsight?.totalSocialActivityCounts ??
2604
+ {};
2605
+
2606
+ // Get URN for ID
2607
+ const urn = el.entityUrn ?? el["*backendUrn"] ?? "";
2608
+ const urnParts = urn.split(":");
2609
+ const id =
2610
+ urnParts[urnParts.length - 1] ||
2611
+ `li_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
2612
+
2613
+ // Get timestamp
2614
+ const metadata = resolve(el["*metadata"] ?? el.metadata);
2615
+ const publishedAt = metadata?.publishedAt ?? el.createdAt ?? Date.now();
2616
+
2617
+ posts.push({
2618
+ id,
2619
+ text,
2620
+ author: authorName,
2621
+ authorHeadline: typeof authorDesc === "string" ? authorDesc : undefined,
2622
+ authorMemberId: authorMemberId || undefined,
2623
+ authorSlug: authorSlug ?? undefined,
2624
+ likes: counts.numLikes ?? 0,
2625
+ comments: counts.numComments ?? 0,
2626
+ shares: counts.numShares ?? 0,
2627
+ publishedAt: new Date(publishedAt),
2628
+ });
2629
+ }
2630
+
2631
+ return posts;
2632
+ }
2633
+
2634
+ function parseJobListings(_url: string, json: unknown): LinkedInJob[] {
2635
+ const jobs: LinkedInJob[] = [];
2636
+ const data = json as any;
2637
+
2638
+ const elements = data?.elements ?? data?.data?.elements ?? [];
2639
+
2640
+ for (const element of elements) {
2641
+ const jobPosting = element?.jobPosting ?? element;
2642
+ const title = jobPosting?.title ?? element?.title ?? "";
2643
+ if (!title) continue;
2644
+
2645
+ const urnParts = (
2646
+ jobPosting?.entityUrn ??
2647
+ element?.dashEntityUrn ??
2648
+ ""
2649
+ ).split(":");
2650
+ const id =
2651
+ urnParts[urnParts.length - 1] ||
2652
+ `job_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
2653
+
2654
+ jobs.push({
2655
+ id,
2656
+ title,
2657
+ location: jobPosting?.formattedLocation ?? jobPosting?.location ?? "",
2658
+ postedAt: new Date(
2659
+ jobPosting?.listedAt ?? element?.createdAt ?? Date.now()
2660
+ ),
2661
+ url: `https://www.linkedin.com/jobs/view/${id}`,
2662
+ description: jobPosting?.description?.text ?? undefined,
2663
+ });
2664
+ }
2665
+
2666
+ return jobs;
2667
+ }
2668
+
2669
+ // ── Config Schemas ────────────────────────────────────────────
2670
+
2671
+ const companyUpdatesConfigSchema = {
2672
+ type: "object",
2673
+ required: ["company_url"],
2674
+ properties: {
2675
+ company_url: {
2676
+ type: "string",
2677
+ description:
2678
+ 'LinkedIn company page URL (e.g., "https://www.linkedin.com/company/openai")',
2679
+ },
2680
+ max_scrolls: {
2681
+ type: "integer",
2682
+ minimum: 1,
2683
+ maximum: 20,
2684
+ default: 5,
2685
+ description: "Maximum scroll iterations for pagination (default: 5)",
2686
+ },
2687
+ },
2688
+ };
2689
+
2690
+ const liveMessagesConfigSchema = {
2691
+ type: "object",
2692
+ properties: {
2693
+ inbox: {
2694
+ type: "string",
2695
+ pattern: "^(?:me|urn:li:fsd_pageMailbox:[0-9]+)$",
2696
+ default: "me",
2697
+ description:
2698
+ 'Whose inbox to read: "me" (default) for the signed-in member, or a company page\'s mailbox URN (urn:li:fsd_pageMailbox:<number>, the mailboxUrn the page\'s admin inbox at /company/<id>/admin/inbox/ requests; it differs from the company id). The signed-in member must administer the page. Changing it restarts the feed\'s subscription from a fresh baseline.',
2699
+ },
2700
+ },
2701
+ };
2702
+
2703
+ const homeFeedConfigSchema = {
2704
+ type: "object",
2705
+ properties: {
2706
+ max_scrolls: {
2707
+ type: "integer",
2708
+ minimum: 1,
2709
+ maximum: 30,
2710
+ default: 8,
2711
+ description:
2712
+ "Maximum scroll iterations for the home feed (default: 8). Upper end of the range when min_scrolls is set.",
2713
+ },
2714
+ min_scrolls: {
2715
+ type: "integer",
2716
+ minimum: 1,
2717
+ maximum: 30,
2718
+ description:
2719
+ "Optional lower bound. When set below max_scrolls, each sync picks a uniform random scroll count in [min_scrolls, max_scrolls].",
2720
+ },
2721
+ },
2722
+ };
2723
+
2724
+ /** Per-run scroll budget: fixed max_scrolls, or uniform [min_scrolls, max_scrolls]. */
2725
+ function readHomeScrollBudget(config: {
2726
+ max_scrolls?: number;
2727
+ min_scrolls?: number;
2728
+ }): number {
2729
+ const max = Math.max(1, Math.min(30, Number(config.max_scrolls ?? 8) || 8));
2730
+ if (config.min_scrolls === undefined || config.min_scrolls === null) {
2731
+ return max;
2732
+ }
2733
+ const min = Math.max(1, Math.min(max, Number(config.min_scrolls) || 1));
2734
+ if (min >= max) return max;
2735
+ return min + Math.floor(Math.random() * (max - min + 1));
2736
+ }
2737
+
2738
+ const jobsConfigSchema = {
2739
+ type: "object",
2740
+ required: ["company_url"],
2741
+ properties: {
2742
+ company_url: {
2743
+ type: "string",
2744
+ description:
2745
+ 'LinkedIn company page URL (e.g., "https://www.linkedin.com/company/openai")',
2746
+ },
2747
+ max_scrolls: {
2748
+ type: "integer",
2749
+ minimum: 1,
2750
+ maximum: 10,
2751
+ default: 3,
2752
+ description: "Maximum scroll iterations for job listings (default: 3)",
2753
+ },
2754
+ },
2755
+ };
2756
+
2757
+ function localTakeoutSchema(description: string): Record<string, unknown> {
2758
+ return {
2759
+ type: "object",
2760
+ properties: {
2761
+ takeout_dir: { type: "string", description },
2762
+ batch_size: {
2763
+ type: "integer",
2764
+ minimum: 1,
2765
+ maximum: 5000,
2766
+ default: 1000,
2767
+ description: "Maximum events to emit per sync run.",
2768
+ },
2769
+ },
2770
+ };
2771
+ }
2772
+
2773
+ // ── Connector ─────────────────────────────────────────────────
2774
+
2775
+ /**
2776
+ * True when a raw post reference is a LinkedIn HOME-FEED surface with no
2777
+ * specific post identity (e.g. `https://www.linkedin.com/feed/`, the feed
2778
+ * root, `/feed/hashtag/…`, or a bare `/feed/update` with no id). Such a URL
2779
+ * addresses the whole feed, not a post — prepare_comment must never treat it
2780
+ * as a durable post id (it would enqueue a doomed navigate and fail). Only
2781
+ * consulted after normalizeLinkedInPostUrl returned null, so any input that
2782
+ * resolves to a durable post URL is already handled.
2783
+ */
2784
+ export function isGenericLinkedInFeedUrl(raw: string): boolean {
2785
+ try {
2786
+ const withScheme = raw.startsWith("//")
2787
+ ? `https:${raw}`
2788
+ : raw.startsWith("/")
2789
+ ? `https://www.linkedin.com${raw}`
2790
+ : raw.includes("://")
2791
+ ? raw
2792
+ : `https://${raw}`;
2793
+ const u = new URL(withScheme);
2794
+ const host = u.hostname.toLowerCase().replace(/^www\./, "");
2795
+ if (host !== "linkedin.com" && !host.endsWith(".linkedin.com")) {
2796
+ return false;
2797
+ }
2798
+ const path = u.pathname.replace(/\/+$/, "");
2799
+ // A URL that carries a durable post identity (urn:li:…) is not the bare
2800
+ // feed surface, even though it lives under /feed/ — it resolves to a real
2801
+ // post. Only paths with NO embedded identity are "generic feed".
2802
+ if (/urn(?::|%3A)li(?::|%3A)(?:activity|share|ugcPost)/i.test(path)) {
2803
+ return false;
2804
+ }
2805
+ // Any other /feed/ path is a feed surface once no durable post id
2806
+ // resolved — the feed root, /feed/hashtag/<tag>, /feed/trending, and a
2807
+ // bare /feed/update (no urn). Nothing that normalizes to a real post URL
2808
+ // reaches here, so a prefix match is safe and complete.
2809
+ return path === "/feed" || path.startsWith("/feed/");
2810
+ } catch {
2811
+ return false;
2812
+ }
2813
+ }
2814
+
2815
+ /**
2816
+ * Non-failing result for a post reference that is not a supported durable
2817
+ * identifier. `success: true` so the action completes WITHOUT a failed
2818
+ * operational run — a not_actionable input is not a Lobu infrastructure
2819
+ * incident. `output.status` lets callers distinguish this from a real stage.
2820
+ */
2821
+ function notActionableMissingPostId(postUrlRaw: string): ActionResult {
2822
+ return {
2823
+ success: true,
2824
+ output: {
2825
+ status: "not_actionable",
2826
+ reason: "missing_durable_post_id",
2827
+ post_url: postUrlRaw,
2828
+ message:
2829
+ "A durable LinkedIn post identifier is required (e.g. urn:li:activity:…, a /feed/update/urn:li:… URL, or the canonical post URL from a synced post event). The provided reference addresses the LinkedIn home feed, not a specific post.",
2830
+ },
2831
+ };
2832
+ }
2833
+
2834
+ /**
2835
+ * The ten LinkedIn Data Export feeds: checkpoint cursor and pure reader per
2836
+ * feed key. This table replaces a chain of ten near-identical `if (feedKey ===
2837
+ * …)` blocks; the mapping it encodes is unchanged.
2838
+ *
2839
+ * Feed keys and cursor names are the persisted contract — a feed row keys on
2840
+ * the feed key and a checkpoint on the cursor name — so both are copied over
2841
+ * verbatim rather than tidied (see {@link LinkedInCheckpoint} on why the
2842
+ * takeout `jobs` cursor is `last_applied_jobs_timestamp`).
2843
+ */
2844
+ const LINKEDIN_TAKEOUT_FEEDS: Record<
2845
+ string,
2846
+ {
2847
+ cursor: Exclude<keyof LinkedInCheckpoint, "linkedin_inbox">;
2848
+ read: (readCsv: TakeoutCsvReader) => EventEnvelope[];
2849
+ }
2850
+ > = {
2851
+ messages: { cursor: "last_messages_timestamp", read: readMessages },
2852
+ connections: { cursor: "last_connections_timestamp", read: readConnections },
2853
+ invitations: { cursor: "last_invitations_timestamp", read: readInvitations },
2854
+ applied_jobs: { cursor: "last_applied_jobs_timestamp", read: readJobs },
2855
+ profile: { cursor: "last_profile_timestamp", read: readProfile },
2856
+ companies: { cursor: "last_companies_timestamp", read: readCompanyFollows },
2857
+ learning: { cursor: "last_learning_timestamp", read: readLearning },
2858
+ events: { cursor: "last_events_timestamp", read: readEvents },
2859
+ endorsements: {
2860
+ cursor: "last_endorsements_timestamp",
2861
+ read: readEndorsements,
2862
+ },
2863
+ media: { cursor: "last_media_timestamp", read: readRichMedia },
2864
+ };
2865
+
2866
+ /**
2867
+ * The configured export directory, or a thrown error naming what is missing.
2868
+ *
2869
+ * Only checks that the value is SET; it cannot stat the path, because this runs
2870
+ * inside the isolate, which has no filesystem. An injected host reader must
2871
+ * validate its directory before reading; the platform does not provide one.
2872
+ */
2873
+ function assertTakeoutDir(config: LinkedInConfig): string {
2874
+ const dir = config.takeout_dir;
2875
+ if (!dir) throw new Error("Missing takeout_dir for LinkedIn.");
2876
+ return dir;
2877
+ }
2878
+
2879
+ /** One sync pass's output; `syncFeed` commits it as a single page. */
2880
+ interface LinkedInSyncPage {
2881
+ events: EventEnvelope[];
2882
+ checkpoint: LinkedInCheckpoint;
2883
+ metadata?: Record<string, unknown>;
2884
+ }
2885
+
2886
+ export default class LinkedInConnector extends ConnectorRuntime<
2887
+ LinkedInCheckpoint,
2888
+ LinkedInConfig
2889
+ > {
2890
+ private readonly fetchImpl: typeof fetch;
2891
+ /**
2892
+ * Builds a CSV reader rooted at the export directory, or `undefined` when the
2893
+ * host has no filesystem — which is the case in the isolate every platform
2894
+ * run uses. Supplying it is how a Node-hosted caller (or a future worker that
2895
+ * advertises a local-file capability) turns the takeout feeds on, WITHOUT
2896
+ * putting `node:fs` into the bundle the live browser feeds must load.
2897
+ */
2898
+ private readonly csvReader?: (takeoutDir: string) => TakeoutCsvReader;
2899
+
2900
+ constructor(
2901
+ fetchImpl: typeof fetch = fetch,
2902
+ csvReader?: (takeoutDir: string) => TakeoutCsvReader
2903
+ ) {
2904
+ super();
2905
+ this.fetchImpl = fetchImpl;
2906
+ this.csvReader = csvReader;
2907
+ }
2908
+
2909
+ readonly definition: RuntimeConnectorDefinition<
2910
+ LinkedInCheckpoint,
2911
+ LinkedInConfig
2912
+ > = {
2913
+ key: "linkedin",
2914
+ name: "LinkedIn",
2915
+ description:
2916
+ "Read LinkedIn feeds, notifications, and recent inbox messages through paired Chrome. Subscribe to source changes, answer direct messages, and prepare message, post, or comment drafts for human submission. Also supports local LinkedIn Data Export CSV files.",
2917
+ version: "3.20.0",
2918
+ faviconDomain: "linkedin.com",
2919
+ // Live feeds bind a signed-in Chrome account. Local takeout imports need no browser.
2920
+ browser: {
2921
+ authMethods: ["browser"],
2922
+ origins: ["https://www.linkedin.com"],
2923
+ accountProbe: {
2924
+ url: LINKEDIN_HOME_URL,
2925
+ // Only the self-profile endpoint; vanity profile URLs can be renamed.
2926
+ expression: `(async () => {
2927
+ const raw = document.cookie.split('; ').find(cookie => cookie.startsWith('JSESSIONID='));
2928
+ if (!raw) return null;
2929
+ const csrf = decodeURIComponent(raw.slice(11)).replace(/^"|"$/g, '');
2930
+ const response = await fetch('/voyager/api/me', {
2931
+ credentials: 'include',
2932
+ headers: { 'csrf-token': csrf, 'x-restli-protocol-version': '2.0.0' },
2933
+ });
2934
+ if (response.status === 401 || response.status === 403) return null;
2935
+ if (!response.ok) throw new Error('LinkedIn account check failed (HTTP ' + response.status + ')');
2936
+ const self = await response.json();
2937
+ const accountId = self.miniProfile?.entityUrn;
2938
+ return typeof accountId === 'string' && accountId ? { accountId } : null;
2939
+ })()`,
2940
+ },
2941
+ },
2942
+ authSchema: {
2943
+ methods: [
2944
+ {
2945
+ type: "browser",
2946
+ mode: "live",
2947
+ },
2948
+ { type: "none", label: "Local data export" },
2949
+ // Keep browser first: connection setup uses the first authenticated
2950
+ // method, while sign-in independently discovers OAuth loginScopes.
2951
+ {
2952
+ type: "oauth",
2953
+ provider: "linkedin",
2954
+ requiredScopes: [],
2955
+ loginScopes: ["openid", "profile", "email"],
2956
+ loginProvisioning: { autoCreateConnection: false },
2957
+ authorizationUrl: "https://www.linkedin.com/oauth/v2/authorization",
2958
+ tokenUrl: "https://www.linkedin.com/oauth/v2/accessToken",
2959
+ userinfoUrl: "https://api.linkedin.com/v2/userinfo",
2960
+ tokenEndpointAuthMethod: "client_secret_post",
2961
+ clientIdKey: "LINKEDIN_CLIENT_ID",
2962
+ clientSecretKey: "LINKEDIN_CLIENT_SECRET",
2963
+ description:
2964
+ "Identity-only LinkedIn sign-in. Feeds use the browser or local exports.",
2965
+ setupInstructions:
2966
+ "Enable Sign In with LinkedIn using OpenID Connect in your LinkedIn app. Register /api/auth/oauth2/callback/linkedin on your Lobu origin for sign-in, then enter the client ID and client secret.",
2967
+ },
2968
+ ],
2969
+ },
2970
+ // Source subscriptions deliver only declared keys. Declaring any replaces the
2971
+ // eventKinds-derived catalog, so every feed kind is listed here.
2972
+ automationEvents: [
2973
+ {
2974
+ key: "notification.changed",
2975
+ label: "Notification Changed",
2976
+ resourceType: "notification",
2977
+ description:
2978
+ "A new or changed notification reference. Read its content from the notifications feed.",
2979
+ },
2980
+ {
2981
+ key: "message.created",
2982
+ label: "Message Created",
2983
+ resourceType: "message",
2984
+ description:
2985
+ "A new inbox message reference. Read its content from the live messages feed by id:<message URN>. A reply Automation can answer a direct message in its conversation.",
2986
+ capabilities: { replyToSource: true },
2987
+ },
2988
+ {
2989
+ key: "source.gap",
2990
+ label: "Source Gap",
2991
+ description:
2992
+ "Catch-up reached the end of what LinkedIn exposes, so earlier changes at this conversation or notification position may be missing.",
2993
+ },
2994
+ { key: "post", label: "Post" },
2995
+ { key: "comment", label: "Comment" },
2996
+ { key: "job_posting", label: "Job Posting" },
2997
+ { key: "message", label: "Message" },
2998
+ { key: "connection", label: "Connection" },
2999
+ ],
3000
+ feeds: {
3001
+ // ── Live (Chrome-extension) feeds ──────────────────────────
3002
+ notifications: {
3003
+ key: "notifications",
3004
+ name: "Notifications",
3005
+ description:
3006
+ "Read notification content and links without marking them seen. Subscriptions check once per minute while the paired browser is online; grouped notification content changes activate Automations, and catch-up beyond the scan budget reports a source gap. Exact source timestamps are unavailable.",
3007
+ read: (ctx) => readLinkedInInbox(ctx),
3008
+ observe: (ctx) => observeLinkedInInbox(ctx),
3009
+ webhook: {
3010
+ mode: "trigger",
3011
+ events: ["notification.changed", "source.gap"],
3012
+ },
3013
+ eventKinds: {
3014
+ "notification.changed": {
3015
+ description: "A new or meaningfully changed LinkedIn notification",
3016
+ },
3017
+ "source.gap": {
3018
+ description:
3019
+ "Catch-up scanned its page budget without reaching a known notification",
3020
+ },
3021
+ },
3022
+ },
3023
+ live_messages: {
3024
+ key: "live_messages",
3025
+ name: "Live Messages",
3026
+ description:
3027
+ "Read recent messages from inbox conversations without marking them seen. The cursor pages conversations and their recent message pages, not a full history export; `id:<message URN>` reads one recent message back. Rows carry the conversation's thread_id and full conversation_id, whether it is a group, the sender's member id, slug and name, and the message's content_kind; a company page inbox's rows also carry the visitor's topic when LinkedIn shows one. The `inbox` setting reads a company page's inbox the signed-in member administers instead of the member's own. Subscriptions check once per minute while the browser is online and report catch-up gaps.",
3028
+ configSchema: liveMessagesConfigSchema,
3029
+ read: (ctx) => readLinkedInInbox(ctx),
3030
+ observe: (ctx) => observeLinkedInInbox(ctx),
3031
+ webhook: { mode: "trigger", events: ["message.created", "source.gap"] },
3032
+ eventKinds: {
3033
+ "message.created": { description: "A new LinkedIn inbox message" },
3034
+ "source.gap": {
3035
+ description:
3036
+ "A conversation's recent message page may hide older catch-up messages",
3037
+ },
3038
+ },
3039
+ },
3040
+ home_feed: {
3041
+ sync: (ctx) => this.syncFeed(ctx),
3042
+ key: "home_feed",
3043
+ name: "Home Feed",
3044
+ description: "Your personalized LinkedIn home feed.",
3045
+ configSchema: homeFeedConfigSchema,
3046
+ eventKinds: {
3047
+ post: {
3048
+ description: "A post from your personalized LinkedIn home feed",
3049
+ attributions: LINKEDIN_HOME_FEED_ATTRIBUTIONS,
3050
+ metadataSchema: {
3051
+ type: "object",
3052
+ properties: {
3053
+ author: { type: "string" },
3054
+ author_linkedin_slug: { type: "string" },
3055
+ author_profile_url: { type: "string" },
3056
+ social_actor: {
3057
+ type: "string",
3058
+ description:
3059
+ "Member whose engagement surfaced this post (likes/commented/reposted banners).",
3060
+ },
3061
+ social_action: {
3062
+ type: "string",
3063
+ description:
3064
+ "Engagement verb: like, love, celebrate, support, insightful, funny, comment, repost.",
3065
+ },
3066
+ social_actor_slug: { type: "string" },
3067
+ social_actor_profile_url: { type: "string" },
3068
+ reactions: { type: "number" },
3069
+ comments: { type: "number" },
3070
+ reposts: { type: "number" },
3071
+ },
3072
+ },
3073
+ },
3074
+ comment: {
3075
+ description:
3076
+ "A complete comment or reply on a personalized feed post",
3077
+ attributions: LINKEDIN_HOME_COMMENT_ATTRIBUTIONS,
3078
+ metadataSchema: {
3079
+ type: "object",
3080
+ properties: {
3081
+ author: { type: "string" },
3082
+ author_linkedin_slug: { type: "string" },
3083
+ author_profile_url: { type: "string" },
3084
+ parent_post_origin_id: { type: "string" },
3085
+ parent_author: { type: "string" },
3086
+ parent_author_linkedin_slug: { type: "string" },
3087
+ parent_author_profile_url: { type: "string" },
3088
+ comment_urn: { type: "string" },
3089
+ comment_id: { type: "string" },
3090
+ parent_activity_id: { type: "string" },
3091
+ parent_activity_namespace: { type: "string" },
3092
+ parent_comment_origin_id: { type: "string" },
3093
+ parent_comment_id: { type: "string" },
3094
+ parent_comment_author: { type: "string" },
3095
+ parent_comment_author_linkedin_slug: { type: "string" },
3096
+ parent_comment_author_profile_url: { type: "string" },
3097
+ is_reply: { type: "boolean" },
3098
+ reactions: { type: "number" },
3099
+ comments: { type: "number" },
3100
+ reposts: { type: "number" },
3101
+ },
3102
+ },
3103
+ },
3104
+ },
3105
+ },
3106
+ company_updates: {
3107
+ sync: (ctx) => this.syncFeed(ctx),
3108
+ key: "company_updates",
3109
+ name: "Company Updates",
3110
+ description: "Posts and updates from the company LinkedIn page.",
3111
+ configSchema: companyUpdatesConfigSchema,
3112
+ eventKinds: {
3113
+ post: {
3114
+ description: "A company LinkedIn post",
3115
+ attributions: LINKEDIN_POST_AUTHOR_ATTRIBUTIONS,
3116
+ metadataSchema: {
3117
+ type: "object",
3118
+ properties: {
3119
+ author_headline: { type: "string" },
3120
+ author_member_id: { type: "string" },
3121
+ author_linkedin_slug: { type: "string" },
3122
+ likes: { type: "number" },
3123
+ comments: { type: "number" },
3124
+ shares: { type: "number" },
3125
+ },
3126
+ },
3127
+ },
3128
+ },
3129
+ },
3130
+ jobs: {
3131
+ sync: (ctx) => this.syncFeed(ctx),
3132
+ key: "jobs",
3133
+ name: "Job Listings",
3134
+ description: "Open job positions (hiring velocity signal).",
3135
+ configSchema: jobsConfigSchema,
3136
+ eventKinds: {
3137
+ job_posting: {
3138
+ description: "An open job listing",
3139
+ metadataSchema: {
3140
+ type: "object",
3141
+ properties: {
3142
+ location: { type: "string" },
3143
+ },
3144
+ },
3145
+ },
3146
+ },
3147
+ },
3148
+ // ── Takeout (local CSV) feeds ──────────────────────────────
3149
+ messages: {
3150
+ sync: (ctx) => this.syncFeed(ctx),
3151
+ key: "messages",
3152
+ name: "Messages",
3153
+ configSchema: localTakeoutSchema(
3154
+ "Path to the LinkedIn Data Export folder."
3155
+ ),
3156
+ eventKinds: {
3157
+ message: {
3158
+ description: "A LinkedIn direct message",
3159
+ attributions: LINKEDIN_MESSAGE_ATTRIBUTIONS,
3160
+ },
3161
+ },
3162
+ },
3163
+ connections: {
3164
+ sync: (ctx) => this.syncFeed(ctx),
3165
+ key: "connections",
3166
+ name: "Connections",
3167
+ configSchema: localTakeoutSchema(
3168
+ "Path to the LinkedIn Data Export folder."
3169
+ ),
3170
+ eventKinds: {
3171
+ connection: {
3172
+ description: "A first-degree LinkedIn connection",
3173
+ attributions: LINKEDIN_CONNECTION_ATTRIBUTIONS,
3174
+ },
3175
+ },
3176
+ },
3177
+ invitations: {
3178
+ sync: (ctx) => this.syncFeed(ctx),
3179
+ key: "invitations",
3180
+ name: "Invitations",
3181
+ configSchema: localTakeoutSchema(
3182
+ "Path to the LinkedIn Data Export folder."
3183
+ ),
3184
+ },
3185
+ applied_jobs: {
3186
+ sync: (ctx) => this.syncFeed(ctx),
3187
+ key: "applied_jobs",
3188
+ name: "Applied Jobs",
3189
+ description:
3190
+ "Your own applied/saved job postings from the LinkedIn Data Export.",
3191
+ configSchema: localTakeoutSchema(
3192
+ "Path to the LinkedIn Data Export folder."
3193
+ ),
3194
+ },
3195
+ profile: {
3196
+ sync: (ctx) => this.syncFeed(ctx),
3197
+ key: "profile",
3198
+ name: "Profile",
3199
+ configSchema: localTakeoutSchema(
3200
+ "Path to the LinkedIn Data Export folder."
3201
+ ),
3202
+ },
3203
+ companies: {
3204
+ sync: (ctx) => this.syncFeed(ctx),
3205
+ key: "companies",
3206
+ name: "Company Follows",
3207
+ configSchema: localTakeoutSchema(
3208
+ "Path to the LinkedIn Data Export folder."
3209
+ ),
3210
+ },
3211
+ learning: {
3212
+ sync: (ctx) => this.syncFeed(ctx),
3213
+ key: "learning",
3214
+ name: "Learning",
3215
+ configSchema: localTakeoutSchema(
3216
+ "Path to the LinkedIn Data Export folder."
3217
+ ),
3218
+ },
3219
+ events: {
3220
+ sync: (ctx) => this.syncFeed(ctx),
3221
+ key: "events",
3222
+ name: "Events",
3223
+ configSchema: localTakeoutSchema(
3224
+ "Path to the LinkedIn Data Export folder."
3225
+ ),
3226
+ },
3227
+ endorsements: {
3228
+ sync: (ctx) => this.syncFeed(ctx),
3229
+ key: "endorsements",
3230
+ name: "Endorsements and Recommendations",
3231
+ configSchema: localTakeoutSchema(
3232
+ "Path to the LinkedIn Data Export folder."
3233
+ ),
3234
+ },
3235
+ media: {
3236
+ sync: (ctx) => this.syncFeed(ctx),
3237
+ key: "media",
3238
+ name: "Rich Media",
3239
+ configSchema: localTakeoutSchema(
3240
+ "Path to the LinkedIn Data Export folder."
3241
+ ),
3242
+ },
3243
+ },
3244
+ actions: {
3245
+ set_like: {
3246
+ key: "set_like",
3247
+ name: "Set like",
3248
+ kind: "write",
3249
+ description:
3250
+ "Set the Like reaction on one exact LinkedIn post and verify it after reload. Already-matching posts are unchanged. Preserves other reaction types. Never comments or posts; selection and budgets belong to the Automation.",
3251
+ annotations: {
3252
+ openWorldHint: true,
3253
+ destructiveHint: false,
3254
+ idempotentHint: true,
3255
+ },
3256
+ inputSchema: {
3257
+ type: "object",
3258
+ required: ["post_url", "liked"],
3259
+ properties: {
3260
+ post_url: { type: "string", format: "uri" },
3261
+ liked: { type: "boolean" },
3262
+ },
3263
+ additionalProperties: false,
3264
+ },
3265
+ },
3266
+ send_message: {
3267
+ key: "send_message",
3268
+ name: "Send direct message reply",
3269
+ kind: "write",
3270
+ description:
3271
+ "Send a text reply into an exact existing LinkedIn direct-message conversation and confirm it appears there. Pass a member-inbox row's thread_id, or a row's full conversation_id to reply from the inbox the message came from (a company page the signed-in member administers). This direct send is approved only for answering direct messages on accounts the owner operates; group conversations are refused, and posts, comments and other messages stay draft-only through prepare_message, prepare_post and prepare_comment. A retry with the same idempotency_key returns the earlier message instead of sending again.",
3272
+ annotations: {
3273
+ openWorldHint: true,
3274
+ destructiveHint: false,
3275
+ idempotentHint: true,
3276
+ },
3277
+ inputSchema: {
3278
+ type: "object",
3279
+ required: ["conversation_id", "text", "idempotency_key"],
3280
+ additionalProperties: false,
3281
+ properties: {
3282
+ conversation_id: {
3283
+ type: "string",
3284
+ pattern:
3285
+ "^(?:[A-Za-z0-9_-]+=*|urn:li:msg_conversation:\\((?:urn:li:fsd_profile:[A-Za-z0-9_-]+|urn:li:fsd_pageMailbox:[0-9]+),[A-Za-z0-9_-]+=*\\))$",
3286
+ maxLength: 500,
3287
+ description:
3288
+ "The row's thread_id (the member's own inbox) or its full conversation_id (the inbox it names).",
3289
+ },
3290
+ text: { type: "string", minLength: 1, maxLength: 3000 },
3291
+ idempotency_key: {
3292
+ type: "string",
3293
+ minLength: 1,
3294
+ maxLength: 300,
3295
+ description: "One reply per key: a retry returns the first send.",
3296
+ },
3297
+ },
3298
+ },
3299
+ },
3300
+ prepare_message: {
3301
+ key: "prepare_message",
3302
+ name: "Prepare message",
3303
+ kind: "write",
3304
+ description:
3305
+ "Prepare a text reply in an exact existing conversation. Execute with activation: { kind: 'page_visit', urls: [conversation_url] }. When the user visits that URL, fill its empty composer. The user clicks Send. Never sends or overwrites a draft.",
3306
+ annotations: {
3307
+ openWorldHint: true,
3308
+ destructiveHint: false,
3309
+ idempotentHint: false,
3310
+ },
3311
+ inputSchema: {
3312
+ type: "object",
3313
+ required: ["conversation_url", "body"],
3314
+ additionalProperties: false,
3315
+ properties: {
3316
+ conversation_url: { type: "string", format: "uri" },
3317
+ body: { type: "string", minLength: 1, maxLength: 3000 },
3318
+ },
3319
+ },
3320
+ },
3321
+ prepare_post: {
3322
+ key: "prepare_post",
3323
+ name: "Prepare personal post",
3324
+ kind: "write",
3325
+ description: `Prepare a text post for the signed-in member. Execute with activation: { kind: 'page_visit', urls: ['${LINKEDIN_HOME_URL}'] }. When the user visits that page, open its personal post composer and fill an empty draft. The user clicks Post. Never publishes or overwrites a draft.`,
3326
+ annotations: {
3327
+ openWorldHint: true,
3328
+ destructiveHint: false,
3329
+ idempotentHint: false,
3330
+ },
3331
+ inputSchema: {
3332
+ type: "object",
3333
+ required: ["body"],
3334
+ additionalProperties: false,
3335
+ properties: {
3336
+ body: { type: "string", minLength: 1, maxLength: 3000 },
3337
+ },
3338
+ },
3339
+ },
3340
+ prepare_comment: {
3341
+ key: "prepare_comment",
3342
+ name: "Prepare comment",
3343
+ description:
3344
+ "Stage a comment draft after the user opens the exact LinkedIn post (fill composer, banner). The page-activation target is the post page URL with a trailing slash (https://www.linkedin.com/feed/update/urn:li:activity:<id>/), the form LinkedIn settles on. NEVER opens a tab or submits — the human must click Post.",
3345
+ // Page activation is separate from org policy approval. This action only
3346
+ // fills the composer; the human performs the irreversible Post click.
3347
+ // The fill expression skips submit-labelled controls and never presses Enter.
3348
+ kind: "write",
3349
+ annotations: {
3350
+ openWorldHint: true,
3351
+ destructiveHint: false,
3352
+ idempotentHint: false,
3353
+ },
3354
+ inputSchema: {
3355
+ type: "object",
3356
+ required: ["body"],
3357
+ anyOf: [{ required: ["post_url"] }, { required: ["activity_id"] }],
3358
+ properties: {
3359
+ post_url: {
3360
+ type: "string",
3361
+ description:
3362
+ 'LinkedIn post URL, or activity id / URN (e.g. "https://www.linkedin.com/feed/update/urn:li:activity:7312345678901234567", "urn:li:activity:7312345678901234567", or "7312345678901234567").',
3363
+ },
3364
+ activity_id: {
3365
+ type: "string",
3366
+ description:
3367
+ "Activity id when post_url is omitted (digits or urn:li:activity:…).",
3368
+ },
3369
+ body: {
3370
+ type: "string",
3371
+ description:
3372
+ "Draft comment text. Left in the composer for the user to edit/send.",
3373
+ minLength: 1,
3374
+ maxLength: 3000,
3375
+ },
3376
+ reason: {
3377
+ type: "string",
3378
+ description:
3379
+ "Optional short explanation for the in-page banner (truncated).",
3380
+ maxLength: 500,
3381
+ },
3382
+ banner: {
3383
+ type: "boolean",
3384
+ description:
3385
+ "Inject a small in-page Lobu handoff banner (default true).",
3386
+ },
3387
+ },
3388
+ },
3389
+ outputSchema: {
3390
+ type: "object",
3391
+ properties: {
3392
+ prepared: { type: "boolean" },
3393
+ status: {
3394
+ type: "string",
3395
+ enum: ["not_actionable", "prepared"],
3396
+ description:
3397
+ "prepared after the draft is staged; not_actionable when the input did not carry a durable post id (no browser action was taken).",
3398
+ },
3399
+ reason: {
3400
+ type: "string",
3401
+ description:
3402
+ "Present with status=not_actionable: why the input was not actionable (e.g. missing_durable_post_id).",
3403
+ },
3404
+ tab_id: { type: "integer" },
3405
+ post_url: { type: "string" },
3406
+ body: { type: "string" },
3407
+ method: { type: "string" },
3408
+ banner_shown: { type: "boolean" },
3409
+ reason_preview: { type: "string" },
3410
+ message: { type: "string" },
3411
+ },
3412
+ },
3413
+ },
3414
+ verify_staged_comment: {
3415
+ key: "verify_staged_comment",
3416
+ name: "Verify staged comment",
3417
+ description:
3418
+ "Re-open a LinkedIn post in the paired Chrome browser and check whether a comment matching the draft body is visible. Read-only — never posts. Best-effort DOM scrape (not a guarantee of publication).",
3419
+ kind: "read",
3420
+ annotations: {
3421
+ openWorldHint: true,
3422
+ destructiveHint: false,
3423
+ idempotentHint: true,
3424
+ },
3425
+ inputSchema: {
3426
+ type: "object",
3427
+ required: ["body"],
3428
+ anyOf: [{ required: ["post_url"] }, { required: ["activity_id"] }],
3429
+ properties: {
3430
+ post_url: {
3431
+ type: "string",
3432
+ description:
3433
+ "LinkedIn post URL, activity id, or URN (same shapes as prepare_comment).",
3434
+ },
3435
+ activity_id: {
3436
+ type: "string",
3437
+ description:
3438
+ "Activity id when post_url is omitted (digits or urn:li:activity:…).",
3439
+ },
3440
+ body: {
3441
+ type: "string",
3442
+ description: "Expected comment text (the staged draft).",
3443
+ minLength: 1,
3444
+ maxLength: 3000,
3445
+ },
3446
+ author_hint: {
3447
+ type: "string",
3448
+ description:
3449
+ "Optional author display-name substring to require when matching.",
3450
+ maxLength: 200,
3451
+ },
3452
+ focus: {
3453
+ type: "boolean",
3454
+ description:
3455
+ "Bring the post tab to the foreground (default true).",
3456
+ },
3457
+ },
3458
+ },
3459
+ outputSchema: {
3460
+ type: "object",
3461
+ properties: {
3462
+ verified: { type: "boolean" },
3463
+ status: {
3464
+ type: "string",
3465
+ enum: ["not_actionable", "verified"],
3466
+ description:
3467
+ "verified after a matching comment is found; not_actionable when the input did not carry a durable post id (no browser action was taken).",
3468
+ },
3469
+ reason: {
3470
+ type: "string",
3471
+ description:
3472
+ "Present whenever verification did not succeed: why (e.g. missing_durable_post_id, no_matching_comment, no_comments_visible, scrape_exception).",
3473
+ },
3474
+ post_url: { type: "string" },
3475
+ body: { type: "string" },
3476
+ tab_id: { type: "integer" },
3477
+ comments_scanned: { type: "integer" },
3478
+ match: {
3479
+ type: "object",
3480
+ properties: {
3481
+ author: { type: "string" },
3482
+ text_preview: { type: "string" },
3483
+ match_kind: {
3484
+ type: "string",
3485
+ enum: ["exact", "prefix", "contains"],
3486
+ },
3487
+ },
3488
+ },
3489
+ message: { type: "string" },
3490
+ },
3491
+ },
3492
+ },
3493
+ read_my_activity: {
3494
+ key: "read_my_activity",
3495
+ name: "Read my activity",
3496
+ description:
3497
+ "Read the signed-in member's own recent LinkedIn posts and comments from their profile activity pages in the paired Chrome browser, and return them without storing any events. Read-only.",
3498
+ kind: "read",
3499
+ annotations: {
3500
+ openWorldHint: true,
3501
+ destructiveHint: false,
3502
+ idempotentHint: true,
3503
+ },
3504
+ inputSchema: {
3505
+ type: "object",
3506
+ properties: {
3507
+ kinds: {
3508
+ type: "array",
3509
+ items: { type: "string", enum: ["posts", "comments"] },
3510
+ description:
3511
+ 'Which activity lists to read. Default ["posts", "comments"].',
3512
+ },
3513
+ max_scrolls: {
3514
+ type: "integer",
3515
+ minimum: 1,
3516
+ maximum: 10,
3517
+ description: "Scroll passes per list (default 3).",
3518
+ },
3519
+ },
3520
+ },
3521
+ },
3522
+ read_home_feed: {
3523
+ key: "read_home_feed",
3524
+ name: "Read home feed",
3525
+ description:
3526
+ "Read the signed-in member's current LinkedIn home feed in the paired Chrome browser, and return the visible posts and comments without storing any events. Read-only.",
3527
+ kind: "read",
3528
+ annotations: {
3529
+ openWorldHint: true,
3530
+ destructiveHint: false,
3531
+ idempotentHint: true,
3532
+ },
3533
+ inputSchema: {
3534
+ type: "object",
3535
+ properties: {
3536
+ max_scrolls: {
3537
+ type: "integer",
3538
+ minimum: 1,
3539
+ maximum: 10,
3540
+ description: "Scroll passes over the feed (default 6).",
3541
+ },
3542
+ },
3543
+ },
3544
+ },
3545
+ },
3546
+ };
3547
+
3548
+ async execute(ctx: ActionContext): Promise<ActionResult> {
3549
+ try {
3550
+ if (ctx.actionKey === "set_like") {
3551
+ if (typeof ctx.input.liked !== "boolean") {
3552
+ return { success: false, error: "liked must be a boolean" };
3553
+ }
3554
+ const url =
3555
+ typeof ctx.input.post_url === "string"
3556
+ ? normalizeLinkedInPostUrl(ctx.input.post_url)
3557
+ : null;
3558
+ if (!url)
3559
+ return {
3560
+ success: false,
3561
+ error: "post_url must identify an exact LinkedIn post",
3562
+ };
3563
+ return {
3564
+ success: true,
3565
+ output: await setLinkedInLike(
3566
+ requireBrowser(ctx),
3567
+ `${url}/`,
3568
+ ctx.input.liked
3569
+ ),
3570
+ };
3571
+ }
3572
+ if (ctx.actionKey === "send_message") {
3573
+ return await sendLinkedInMessage(ctx);
3574
+ }
3575
+ if (
3576
+ ctx.actionKey === "prepare_message" ||
3577
+ ctx.actionKey === "prepare_post"
3578
+ ) {
3579
+ return await prepareLinkedInDraft(ctx);
3580
+ }
3581
+ if (ctx.actionKey === "read_my_activity") {
3582
+ return await readMyLinkedInActivity(ctx);
3583
+ }
3584
+ if (ctx.actionKey === "read_home_feed") {
3585
+ return await readHomeFeed(ctx);
3586
+ }
3587
+ if (
3588
+ ctx.actionKey !== "prepare_comment" &&
3589
+ ctx.actionKey !== "verify_staged_comment"
3590
+ ) {
3591
+ return { success: false, error: `Unknown action: ${ctx.actionKey}` };
3592
+ }
3593
+ const body =
3594
+ typeof ctx.input.body === "string" ? ctx.input.body.trim() : "";
3595
+ if (!body) {
3596
+ return { success: false, error: "body is required" };
3597
+ }
3598
+ const postUrlRaw =
3599
+ (typeof ctx.input.post_url === "string" && ctx.input.post_url.trim()) ||
3600
+ (typeof ctx.input.activity_id === "string" &&
3601
+ ctx.input.activity_id.trim()) ||
3602
+ "";
3603
+ if (!postUrlRaw) {
3604
+ return {
3605
+ success: false,
3606
+ error: "post_url or activity_id is required",
3607
+ };
3608
+ }
3609
+
3610
+ // Require a supported DURABLE post identifier BEFORE any browser
3611
+ // dispatch. A generic home-feed URL must not enqueue a doomed navigate:
3612
+ // return a non-failing not_actionable result instead (no failed run, no
3613
+ // computer-use action, not an infrastructure incident).
3614
+ const normalizedPostUrl = normalizeLinkedInPostUrl(postUrlRaw);
3615
+ if (!normalizedPostUrl) {
3616
+ if (isGenericLinkedInFeedUrl(postUrlRaw)) {
3617
+ return notActionableMissingPostId(postUrlRaw);
3618
+ }
3619
+ return {
3620
+ success: false,
3621
+ error: `post_url or activity_id must reference a specific LinkedIn post (got ${JSON.stringify(postUrlRaw)})`,
3622
+ };
3623
+ }
3624
+
3625
+ const dispatcher = requireBrowser(ctx);
3626
+
3627
+ if (ctx.actionKey === "verify_staged_comment") {
3628
+ const authorHint =
3629
+ typeof ctx.input.author_hint === "string"
3630
+ ? ctx.input.author_hint.trim()
3631
+ : "";
3632
+ const output = await verifyLinkedInStagedComment(dispatcher, {
3633
+ postUrl: normalizedPostUrl,
3634
+ body,
3635
+ author_hint: authorHint || undefined,
3636
+ focus: ctx.input.focus !== false,
3637
+ });
3638
+ return {
3639
+ success: true,
3640
+ output: {
3641
+ ...output,
3642
+ ...(output.verified ? { status: "verified" } : {}),
3643
+ },
3644
+ };
3645
+ }
3646
+
3647
+ const reason =
3648
+ typeof ctx.input.reason === "string" ? ctx.input.reason.trim() : "";
3649
+ const output = await prepareLinkedInComment(dispatcher, {
3650
+ postUrl: normalizedPostUrl,
3651
+ body,
3652
+ reason: reason || undefined,
3653
+ banner: ctx.input.banner !== false,
3654
+ });
3655
+ return { success: true, output: { ...output, status: "prepared" } };
3656
+ } catch (error) {
3657
+ return {
3658
+ success: false,
3659
+ error: error instanceof Error ? error.message : String(error),
3660
+ };
3661
+ }
3662
+ }
3663
+
3664
+ private async syncFeed(
3665
+ ctx: SyncContext<LinkedInCheckpoint, LinkedInConfig>
3666
+ ): Promise<SyncResult> {
3667
+ const page = await this.collectFeed(ctx);
3668
+ await ctx.commit(page.events, page.checkpoint);
3669
+ return { status: "complete", metadata: page.metadata };
3670
+ }
3671
+
3672
+ private async collectFeed(
3673
+ ctx: SyncContext<LinkedInCheckpoint, LinkedInConfig>
3674
+ ): Promise<LinkedInSyncPage> {
3675
+ const feedKey = ctx.feedKey;
3676
+
3677
+ // ── Live (Chrome-extension) feeds ────────────────────────────
3678
+ if (
3679
+ feedKey === "home_feed" ||
3680
+ feedKey === "company_updates" ||
3681
+ feedKey === "jobs"
3682
+ ) {
3683
+ return this.syncLive(ctx);
3684
+ }
3685
+
3686
+ // ── Takeout (local CSV) feeds ────────────────────────────────
3687
+ // Parsing lives in `linkedin-takeout.ts` and is filesystem-free; the
3688
+ // capability to actually READ the export is supplied here. Injecting it
3689
+ // (rather than importing `node:fs` alongside the live feeds) is what keeps
3690
+ // the whole bundle isolate-eligible — see this class's header.
3691
+ const takeout = Object.hasOwn(LINKEDIN_TAKEOUT_FEEDS, feedKey)
3692
+ ? LINKEDIN_TAKEOUT_FEEDS[feedKey]
3693
+ : undefined;
3694
+ if (takeout) {
3695
+ const readCsv = this.csvReader;
3696
+ if (!readCsv) {
3697
+ throw new Error(
3698
+ "LinkedIn feed '" +
3699
+ feedKey +
3700
+ "' reads the local LinkedIn Data Export, " +
3701
+ "and this runtime provides no filesystem access. This invocation has no CSV reader; " +
3702
+ "browser-backed live feeds do not require one. For a local import, construct " +
3703
+ "the connector with an explicit reader in a filesystem-capable host."
3704
+ );
3705
+ }
3706
+ return this.result(
3707
+ ctx,
3708
+ takeout.cursor,
3709
+ takeout.read(readCsv(assertTakeoutDir(ctx.config))),
3710
+ batchSize(ctx.config)
3711
+ );
3712
+ }
3713
+
3714
+ throw new Error(`Unknown LinkedIn feed: ${feedKey}`);
3715
+ }
3716
+
3717
+ // ── Live scrape dispatch ─────────────────────────────────────
3718
+
3719
+ private async syncLive(
3720
+ ctx: SyncContext<LinkedInCheckpoint, LinkedInConfig>
3721
+ ): Promise<LinkedInSyncPage> {
3722
+ const config = ctx.config;
3723
+ const checkpoint = (ctx.checkpoint ?? {}) as LinkedInCheckpoint;
3724
+ const feedKey = ctx.feedKey;
3725
+
3726
+ // home_feed is the one feed that needs a content-script scrape (the CDP
3727
+ // debugger stops the personalized feed from rendering) and takes no
3728
+ // company_url — it always reads LINKEDIN_HOME_URL.
3729
+ if (feedKey === "home_feed") {
3730
+ const homeScrolls = readHomeScrollBudget(config);
3731
+ return this.syncHomeFeed(homeScrolls, checkpoint, requireBrowser(ctx));
3732
+ }
3733
+
3734
+ if (!config.company_url) {
3735
+ throw new Error("company_url is required");
3736
+ }
3737
+
3738
+ // Canonicalize the public alias before applying the exact browser grant.
3739
+ const companyUrl = new URL(config.company_url);
3740
+ if (companyUrl.hostname === "linkedin.com") {
3741
+ companyUrl.hostname = "www.linkedin.com";
3742
+ }
3743
+ const baseUrl = companyUrl.href.replace(/\/$/, "");
3744
+ const maxScrolls = config.max_scrolls ?? (feedKey === "jobs" ? 3 : 5);
3745
+
3746
+ const dispatcher = requireBrowser(ctx);
3747
+ if (feedKey === "jobs") {
3748
+ return this.syncJobs(baseUrl, maxScrolls, checkpoint, dispatcher);
3749
+ }
3750
+ return this.syncUpdates(baseUrl, maxScrolls, checkpoint, dispatcher);
3751
+ }
3752
+
3753
+ /**
3754
+ * Personalized home feed via the extension's content-script scrape. Network
3755
+ * capture can't read it (the CDP debugger stops the feed rendering), so we
3756
+ * dispatch a `cs_scrape` against LINKEDIN_HOME_URL with the home-feed
3757
+ * selectors. Use the same fresh scrape window as timeline reads: a hidden
3758
+ * persistent tab can leave LinkedIn's lazy-loaded feed on its initial batch.
3759
+ */
3760
+ private async syncHomeFeed(
3761
+ maxScrolls: number,
3762
+ checkpoint: LinkedInCheckpoint,
3763
+ dispatcher: ChromeActionDispatcher
3764
+ ): Promise<LinkedInSyncPage> {
3765
+ const { items: rows, loggedIn } = await extensionDomScrape<HomeFeedRow>({
3766
+ dispatcher,
3767
+ url: LINKEDIN_HOME_URL,
3768
+ config: {
3769
+ ...HOME_FEED_SCRAPE_CONFIG,
3770
+ scroll: { ...HOME_FEED_SCRAPE_CONFIG.scroll, max: maxScrolls },
3771
+ },
3772
+ parseRows: (raw) =>
3773
+ (raw as HomeFeedRow[]).map((row) => ({
3774
+ ...row,
3775
+ post_identity: decodeHomeFeedPostIdentity(row.post_identity),
3776
+ })),
3777
+ allowedOrigins: LINKEDIN_ALLOWED_ORIGINS,
3778
+ persistent: false,
3779
+ });
3780
+
3781
+ if (!loggedIn) {
3782
+ throw new Error(
3783
+ "Not logged into LinkedIn. Sign in to linkedin.com in the paired Chrome profile, then re-run the sync."
3784
+ );
3785
+ }
3786
+ if (rows.length === 0) {
3787
+ throw new Error(
3788
+ "LinkedIn is logged in, but the home feed produced no post rows. Confirm that the paired browser is signed into LinkedIn and retry."
3789
+ );
3790
+ }
3791
+
3792
+ const resolvedRows = await resolveHomeFeedPostUrls(rows, this.fetchImpl);
3793
+ const unresolvedShortUrlCount =
3794
+ unresolvedHomeFeedShortUrlCount(resolvedRows);
3795
+ if (unresolvedShortUrlCount > 0) {
3796
+ throw new Error(
3797
+ `LinkedIn could not resolve ${unresolvedShortUrlCount} copied post short URL${unresolvedShortUrlCount === 1 ? "" : "s"} to a durable identity. No home-feed events were persisted.`
3798
+ );
3799
+ }
3800
+ const unresolvedPosts = unresolvedHomeFeedPostRows(resolvedRows);
3801
+ if (unresolvedPosts.length > 0) {
3802
+ const count = unresolvedPosts.length;
3803
+ // The row id alone (no post text) is enough to spot a LinkedIn markup change.
3804
+ throw new Error(
3805
+ `LinkedIn scraped ${count} post row${count === 1 ? "" : "s"} without a durable activity/share/ugcPost identity (first row id: ${String(unresolvedPosts[0].id).slice(0, 120)}). No partial home-feed batch was persisted.`
3806
+ );
3807
+ }
3808
+
3809
+ const commentCoverage = summarizeHomeFeedCommentCoverage(resolvedRows);
3810
+
3811
+ const events = buildHomeFeedEvents(resolvedRows, new Date());
3812
+ if (events.length === 0) {
3813
+ throw new Error(
3814
+ "LinkedIn is logged in and returned feed rows, but no row combined usable content with a durable identity. Confirm that the paired browser is signed into LinkedIn and retry."
3815
+ );
3816
+ }
3817
+
3818
+ // Capability gate: author/engager slugs need the `objectAll` scrape take. An
3819
+ // older extension can't produce it, so identity degrades to name-only.
3820
+ // Surface it in metadata (rather than failing) so a stale extension is
3821
+ // observable — events still flow, just without slugs.
3822
+ const objectAllSupported = homeFeedObjectAllSupported(resolvedRows);
3823
+
3824
+ return {
3825
+ events,
3826
+ // The home feed exposes no stable per-post cursor (opaque token ids, no
3827
+ // timestamps), so there is nothing new to checkpoint — pass it through.
3828
+ checkpoint,
3829
+ metadata: {
3830
+ items_found: events.length,
3831
+ items_scraped: rows.length,
3832
+ scrolls_this_run: maxScrolls,
3833
+ backend: "extension-cs-scrape",
3834
+ object_all_supported: objectAllSupported,
3835
+ comment_threads_complete: commentCoverage.incomplete === 0,
3836
+ comment_threads_checked: commentCoverage.threads,
3837
+ comment_threads_incomplete: commentCoverage.incomplete,
3838
+ comment_threads_unsupported: commentCoverage.unsupported,
3839
+ comment_threads_timed_out: commentCoverage.timedOut,
3840
+ comment_coverage_details: commentCoverage.details,
3841
+ comments_expected: commentCoverage.expected,
3842
+ comments_collected: commentCoverage.collected,
3843
+ },
3844
+ };
3845
+ }
3846
+
3847
+ private async syncUpdates(
3848
+ baseUrl: string,
3849
+ maxScrolls: number,
3850
+ checkpoint: LinkedInCheckpoint,
3851
+ dispatcher: ChromeActionDispatcher
3852
+ ): Promise<LinkedInSyncPage> {
3853
+ const postsUrl = `${baseUrl}/posts/`;
3854
+ const result = await extensionNetworkSync<LinkedInPost>({
3855
+ dispatcher,
3856
+ url: postsUrl,
3857
+ config: {
3858
+ interceptPatterns: [
3859
+ {
3860
+ regex: "voyager/api/graphql\\?variables=.*ORGANIZATION_MEMBER_FEED",
3861
+ },
3862
+ { regex: "voyager/api/graphql\\?variables=.*organizationalPageUrn" },
3863
+ ],
3864
+ allowedOrigins: ["linkedin.com", "*.linkedin.com"],
3865
+ maxScrolls,
3866
+ scrollDelayMs: 3000,
3867
+ responseTimeoutMs: 8000,
3868
+ },
3869
+ parseResponse: parseCompanyUpdates,
3870
+ checkAuth: (currentUrl) =>
3871
+ !currentUrl.includes("/login") && !currentUrl.includes("/authwall"),
3872
+ });
3873
+
3874
+ const posts = filterPostsSinceCheckpoint(result.items, checkpoint);
3875
+ const events: EventEnvelope[] = posts.map((post) => ({
3876
+ origin_id: `li_post_${post.id}`,
3877
+ payload_text: post.text,
3878
+ author_name: post.author,
3879
+ occurred_at: post.publishedAt,
3880
+ origin_type: "post",
3881
+ source_url: `https://www.linkedin.com/feed/update/urn:li:activity:${post.id}`,
3882
+ score: calculateEngagementScore("linkedin", {
3883
+ upvotes: post.likes,
3884
+ reply_count: post.comments,
3885
+ }),
3886
+ metadata: {
3887
+ author_headline: post.authorHeadline,
3888
+ // Author identity for the `post` attribution. member_id (primary) when
3889
+ // Voyager exposed the actor's fsd_profile urn; slug bridges to takeout.
3890
+ author_member_id: post.authorMemberId,
3891
+ author_linkedin_slug: post.authorSlug,
3892
+ likes: post.likes,
3893
+ comments: post.comments,
3894
+ shares: post.shares,
3895
+ },
3896
+ }));
3897
+ events.sort(
3898
+ (a, b) =>
3899
+ new Date(b.occurred_at).getTime() - new Date(a.occurred_at).getTime()
3900
+ );
3901
+
3902
+ return {
3903
+ events,
3904
+ checkpoint: {
3905
+ ...checkpoint,
3906
+ last_post_id: posts[0]?.id ?? checkpoint.last_post_id,
3907
+ last_timestamp:
3908
+ events[0]?.occurred_at?.toISOString?.() ?? checkpoint.last_timestamp,
3909
+ },
3910
+ // No cookie persistence — auth lives in the user's signed-in Chrome,
3911
+ // not in our cookie cache.
3912
+ metadata: {
3913
+ items_found: events.length,
3914
+ items_skipped: result.items.length - posts.length,
3915
+ api_calls: result.apiCallCount,
3916
+ backend: "extension",
3917
+ },
3918
+ };
3919
+ }
3920
+
3921
+ private async syncJobs(
3922
+ baseUrl: string,
3923
+ maxScrolls: number,
3924
+ checkpoint: LinkedInCheckpoint,
3925
+ dispatcher: ChromeActionDispatcher
3926
+ ): Promise<LinkedInSyncPage> {
3927
+ const jobsUrl = `${baseUrl}/jobs/`;
3928
+ const result = await extensionNetworkSync<LinkedInJob>({
3929
+ dispatcher,
3930
+ url: jobsUrl,
3931
+ config: {
3932
+ interceptPatterns: [
3933
+ { regex: "voyager/api/graphql.*jobPosting", flags: "i" },
3934
+ { regex: "voyager/api/search/dash/.*jobs", flags: "i" },
3935
+ { regex: "voyager/api/organization/.*jobs", flags: "i" },
3936
+ ],
3937
+ allowedOrigins: ["linkedin.com", "*.linkedin.com"],
3938
+ maxScrolls,
3939
+ scrollDelayMs: 3000,
3940
+ responseTimeoutMs: 8000,
3941
+ },
3942
+ parseResponse: parseJobListings,
3943
+ checkAuth: (currentUrl) =>
3944
+ !currentUrl.includes("/login") && !currentUrl.includes("/authwall"),
3945
+ });
3946
+
3947
+ const seenIds = new Set<string>();
3948
+ const jobs = result.items.filter((j) => {
3949
+ if (!j.id || seenIds.has(j.id)) return false;
3950
+ seenIds.add(j.id);
3951
+ return true;
3952
+ });
3953
+ jobs.sort((a, b) => b.postedAt.getTime() - a.postedAt.getTime());
3954
+
3955
+ const events: EventEnvelope[] = jobs.map((job) => ({
3956
+ origin_id: `li_job_${job.id}`,
3957
+ payload_text: job.description ?? job.title,
3958
+ title: job.title,
3959
+ occurred_at: job.postedAt,
3960
+ origin_type: "job_posting",
3961
+ source_url: job.url,
3962
+ metadata: { location: job.location },
3963
+ }));
3964
+
3965
+ return {
3966
+ events,
3967
+ checkpoint: {
3968
+ ...checkpoint,
3969
+ last_job_id: jobs[0]?.id ?? checkpoint.last_job_id,
3970
+ last_timestamp:
3971
+ jobs[0]?.postedAt?.toISOString?.() ?? checkpoint.last_timestamp,
3972
+ },
3973
+ metadata: {
3974
+ items_found: events.length,
3975
+ api_calls: result.apiCallCount,
3976
+ backend: "extension",
3977
+ },
3978
+ };
3979
+ }
3980
+
3981
+ // ── Takeout (local CSV) readers ──────────────────────────────
3982
+
3983
+ private result(
3984
+ ctx: SyncContext<LinkedInCheckpoint, LinkedInConfig>,
3985
+ key: Exclude<keyof LinkedInCheckpoint, "linkedin_inbox">,
3986
+ allEvents: EventEnvelope[],
3987
+ max: number
3988
+ ): LinkedInSyncPage {
3989
+ const events = takeBatch(allEvents, ctx.checkpoint?.[key], max);
3990
+ return {
3991
+ events,
3992
+ checkpoint: {
3993
+ ...ctx.checkpoint,
3994
+ [key]: maxEventCursor(events, ctx.checkpoint?.[key]),
3995
+ },
3996
+ };
3997
+ }
3998
+ }