birdclaw 0.4.0 → 0.5.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 (64) hide show
  1. package/CHANGELOG.md +39 -1
  2. package/README.md +108 -7
  3. package/package.json +24 -23
  4. package/playwright.config.ts +1 -0
  5. package/public/favicon.ico +0 -0
  6. package/public/logo192.png +0 -0
  7. package/public/logo512.png +0 -0
  8. package/public/manifest.json +2 -2
  9. package/src/cli.ts +563 -18
  10. package/src/components/AppNav.tsx +66 -29
  11. package/src/components/AvatarChip.tsx +10 -5
  12. package/src/components/ConversationThread.tsx +125 -0
  13. package/src/components/DmWorkspace.tsx +96 -98
  14. package/src/components/EmbeddedTweetCard.tsx +20 -14
  15. package/src/components/InboxCard.tsx +92 -90
  16. package/src/components/LinkPreviewCard.tsx +270 -0
  17. package/src/components/ProfilePreview.tsx +8 -3
  18. package/src/components/SavedTimelineView.tsx +48 -38
  19. package/src/components/TimelineCard.tsx +228 -84
  20. package/src/components/TweetRichText.tsx +6 -2
  21. package/src/lib/archive-import.ts +1567 -65
  22. package/src/lib/authored-live.ts +1074 -0
  23. package/src/lib/backup.ts +318 -14
  24. package/src/lib/bird-actions.ts +1 -10
  25. package/src/lib/bird-command.ts +57 -0
  26. package/src/lib/bird.ts +89 -5
  27. package/src/lib/config.ts +1 -1
  28. package/src/lib/db.ts +282 -4
  29. package/src/lib/follow-graph.ts +1053 -0
  30. package/src/lib/link-index.ts +629 -0
  31. package/src/lib/link-insights.ts +834 -0
  32. package/src/lib/link-preview-metadata.ts +334 -0
  33. package/src/lib/media-fetch.ts +787 -0
  34. package/src/lib/media-includes.ts +165 -0
  35. package/src/lib/mention-threads-live.ts +535 -43
  36. package/src/lib/mentions-live.ts +623 -19
  37. package/src/lib/moderation-target.ts +6 -0
  38. package/src/lib/profile-hydration.ts +1 -1
  39. package/src/lib/profile-resolver.ts +115 -1
  40. package/src/lib/queries.ts +233 -7
  41. package/src/lib/seed.ts +127 -8
  42. package/src/lib/timeline-collections-live.ts +145 -22
  43. package/src/lib/timeline-live.ts +10 -15
  44. package/src/lib/tweet-account-edges.ts +9 -2
  45. package/src/lib/tweet-render.ts +97 -0
  46. package/src/lib/types.ts +219 -2
  47. package/src/lib/ui.ts +375 -147
  48. package/src/lib/url-expansion-store.ts +110 -0
  49. package/src/lib/url-expansion.ts +33 -8
  50. package/src/lib/x-profile.ts +7 -2
  51. package/src/lib/xurl.ts +296 -12
  52. package/src/routeTree.gen.ts +105 -0
  53. package/src/routes/__root.tsx +7 -3
  54. package/src/routes/api/conversation.tsx +34 -0
  55. package/src/routes/api/link-insights.tsx +79 -0
  56. package/src/routes/api/link-preview.tsx +43 -0
  57. package/src/routes/api/profile-hydrate.tsx +51 -0
  58. package/src/routes/blocks.tsx +111 -86
  59. package/src/routes/dms.tsx +90 -78
  60. package/src/routes/inbox.tsx +98 -86
  61. package/src/routes/index.tsx +63 -50
  62. package/src/routes/links.tsx +883 -0
  63. package/src/routes/mentions.tsx +63 -50
  64. package/src/styles.css +106 -43
@@ -0,0 +1,1053 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { getNativeDb } from "./db";
3
+ import { listFollowUsersViaBird } from "./bird";
4
+ import type { Database } from "./sqlite";
5
+ import { readSyncCache, writeSyncCache } from "./sync-cache";
6
+ import type {
7
+ FollowEventKind,
8
+ FollowDirection,
9
+ FollowGraphEvent,
10
+ FollowGraphProfile,
11
+ FollowGraphSummary,
12
+ XurlFollowUsersResponse,
13
+ XurlMentionUser,
14
+ XurlPublicMetrics,
15
+ } from "./types";
16
+ import { upsertProfileFromXUser } from "./x-profile";
17
+ import { listFollowUsersViaXurl } from "./xurl";
18
+
19
+ const DEFAULT_FOLLOW_CACHE_TTL_MS = 24 * 60 * 60_000;
20
+ const DEFAULT_FOLLOW_PAGE_LIMIT = 1000;
21
+ const MIN_FOLLOW_PAGE_LIMIT = 1;
22
+ const MAX_FOLLOW_PAGE_LIMIT = 1000;
23
+ const BIRD_FOLLOW_PAGE_LIMIT = 100;
24
+
25
+ export interface SyncFollowGraphOptions {
26
+ direction: FollowDirection;
27
+ account?: string;
28
+ mode?: FollowGraphSyncMode;
29
+ limit?: number;
30
+ maxPages?: number;
31
+ maxResources?: number;
32
+ yes?: boolean;
33
+ refresh?: boolean;
34
+ allowPartial?: boolean;
35
+ cacheTtlMs?: number;
36
+ }
37
+
38
+ type FollowGraphSyncMode = "auto" | "bird" | "xurl";
39
+ type FollowGraphLiveSource = "bird" | "xurl";
40
+
41
+ interface ResolvedAccount {
42
+ accountId: string;
43
+ username: string;
44
+ externalUserId?: string;
45
+ }
46
+
47
+ interface MergedFollowPayload {
48
+ data: XurlMentionUser[];
49
+ meta: Record<string, unknown>;
50
+ complete: boolean;
51
+ pageCount: number;
52
+ truncatedByMaxResources: boolean;
53
+ }
54
+
55
+ function parseLimit(value = DEFAULT_FOLLOW_PAGE_LIMIT) {
56
+ if (
57
+ !Number.isFinite(value) ||
58
+ value < MIN_FOLLOW_PAGE_LIMIT ||
59
+ value > MAX_FOLLOW_PAGE_LIMIT
60
+ ) {
61
+ throw new Error("--limit must be between 1 and 1000 for follow sync");
62
+ }
63
+ return Math.floor(value);
64
+ }
65
+
66
+ function parseOptionalPositiveInteger(name: string, value?: number) {
67
+ if (value === undefined) {
68
+ return undefined;
69
+ }
70
+ if (!Number.isFinite(value) || value < 1) {
71
+ throw new Error(`${name} must be at least 1`);
72
+ }
73
+ return Math.floor(value);
74
+ }
75
+
76
+ function parseCacheTtlMs(value?: number) {
77
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
78
+ return DEFAULT_FOLLOW_CACHE_TTL_MS;
79
+ }
80
+ return Math.floor(value);
81
+ }
82
+
83
+ function parseJsonField<T>(value: unknown, fallback: T): T {
84
+ if (typeof value !== "string" || value.length === 0) {
85
+ return fallback;
86
+ }
87
+ try {
88
+ return JSON.parse(value) as T;
89
+ } catch {
90
+ return fallback;
91
+ }
92
+ }
93
+
94
+ function resolveAccount(db: Database, accountId?: string): ResolvedAccount {
95
+ const row = accountId
96
+ ? (db
97
+ .prepare(
98
+ "select id, handle, external_user_id from accounts where id = ?",
99
+ )
100
+ .get(accountId) as
101
+ | { id: string; handle: string; external_user_id: string | null }
102
+ | undefined)
103
+ : (db
104
+ .prepare(
105
+ `
106
+ select id, handle, external_user_id
107
+ from accounts
108
+ order by is_default desc, created_at asc
109
+ limit 1
110
+ `,
111
+ )
112
+ .get() as
113
+ | { id: string; handle: string; external_user_id: string | null }
114
+ | undefined);
115
+
116
+ if (!row) {
117
+ throw new Error(`Unknown account: ${accountId ?? "default"}`);
118
+ }
119
+
120
+ return {
121
+ accountId: row.id,
122
+ username: row.handle.replace(/^@/, ""),
123
+ externalUserId:
124
+ typeof row.external_user_id === "string" &&
125
+ row.external_user_id.length > 0
126
+ ? row.external_user_id
127
+ : undefined,
128
+ };
129
+ }
130
+
131
+ function buildCacheKey({
132
+ direction,
133
+ accountId,
134
+ mode,
135
+ limit,
136
+ maxPages,
137
+ maxResources,
138
+ }: {
139
+ direction: FollowDirection;
140
+ accountId: string;
141
+ mode: FollowGraphSyncMode;
142
+ limit: number;
143
+ maxPages?: number;
144
+ maxResources?: number;
145
+ }) {
146
+ return [
147
+ "follow-graph",
148
+ direction,
149
+ accountId,
150
+ `mode:${mode}`,
151
+ `limit:${String(limit)}`,
152
+ `pages:${maxPages === undefined ? "all" : String(maxPages)}`,
153
+ `resources:${maxResources === undefined ? "all" : String(maxResources)}`,
154
+ ].join(":");
155
+ }
156
+
157
+ function readCurrentCount(
158
+ db: Database,
159
+ accountId: string,
160
+ direction: FollowDirection,
161
+ ) {
162
+ const row = db
163
+ .prepare(
164
+ `
165
+ select count(*) as count
166
+ from follow_edges
167
+ where account_id = ? and direction = ? and current = 1
168
+ `,
169
+ )
170
+ .get(accountId, direction) as { count: number };
171
+ return Number(row.count);
172
+ }
173
+
174
+ function getLastSnapshot(
175
+ db: Database,
176
+ accountId: string,
177
+ direction: FollowDirection,
178
+ status: "complete" | "incomplete",
179
+ ) {
180
+ const row = db
181
+ .prepare(
182
+ `
183
+ select completed_at
184
+ from follow_snapshots
185
+ where account_id = ? and direction = ? and status = ?
186
+ order by completed_at desc
187
+ limit 1
188
+ `,
189
+ )
190
+ .get(accountId, direction, status) as { completed_at: string } | undefined;
191
+ return row?.completed_at;
192
+ }
193
+
194
+ function mergePages(
195
+ pages: XurlFollowUsersResponse[],
196
+ nextToken: string | undefined,
197
+ maxResources?: number,
198
+ ): MergedFollowPayload {
199
+ const users: XurlMentionUser[] = [];
200
+ const seen = new Set<string>();
201
+ let truncatedByMaxResources = false;
202
+
203
+ for (const page of pages) {
204
+ for (const user of page.data) {
205
+ if (seen.has(user.id)) {
206
+ continue;
207
+ }
208
+ if (maxResources !== undefined && users.length >= maxResources) {
209
+ truncatedByMaxResources = true;
210
+ break;
211
+ }
212
+ seen.add(user.id);
213
+ users.push(user);
214
+ }
215
+ if (truncatedByMaxResources) {
216
+ break;
217
+ }
218
+ }
219
+
220
+ const lastPage = pages.at(-1);
221
+ const payloadPageCount = Number(lastPage?.meta?.page_count);
222
+ const pageCount =
223
+ Number.isFinite(payloadPageCount) && payloadPageCount > pages.length
224
+ ? payloadPageCount
225
+ : pages.length;
226
+ return {
227
+ data: users,
228
+ meta: {
229
+ ...lastPage?.meta,
230
+ result_count: users.length,
231
+ page_count: pageCount,
232
+ next_token: nextToken ?? null,
233
+ truncated_by_max_resources: truncatedByMaxResources,
234
+ },
235
+ complete: !nextToken && !truncatedByMaxResources,
236
+ pageCount,
237
+ truncatedByMaxResources,
238
+ };
239
+ }
240
+
241
+ async function fetchFollowGraphViaXurl({
242
+ direction,
243
+ username,
244
+ userId,
245
+ limit,
246
+ maxPages,
247
+ maxResources,
248
+ }: {
249
+ direction: FollowDirection;
250
+ username: string;
251
+ userId?: string;
252
+ limit: number;
253
+ maxPages?: number;
254
+ maxResources?: number;
255
+ }): Promise<MergedFollowPayload> {
256
+ const pages: XurlFollowUsersResponse[] = [];
257
+ let nextToken: string | undefined;
258
+ let pageCount = 0;
259
+ let collectedCount = 0;
260
+
261
+ do {
262
+ const payload = await listFollowUsersViaXurl({
263
+ direction,
264
+ username,
265
+ userId,
266
+ maxResults: limit,
267
+ paginationToken: nextToken,
268
+ });
269
+ pages.push(payload);
270
+ collectedCount += payload.data.length;
271
+ nextToken =
272
+ typeof payload.meta?.next_token === "string"
273
+ ? String(payload.meta.next_token)
274
+ : undefined;
275
+ pageCount += 1;
276
+ } while (
277
+ nextToken &&
278
+ (maxPages === undefined || pageCount < maxPages) &&
279
+ (maxResources === undefined || collectedCount < maxResources)
280
+ );
281
+
282
+ return mergePages(pages, nextToken, maxResources);
283
+ }
284
+
285
+ async function fetchFollowGraphViaBird({
286
+ direction,
287
+ userId,
288
+ limit,
289
+ maxPages,
290
+ maxResources,
291
+ }: {
292
+ direction: FollowDirection;
293
+ userId?: string;
294
+ limit: number;
295
+ maxPages?: number;
296
+ maxResources?: number;
297
+ }): Promise<MergedFollowPayload> {
298
+ const birdLimit = Math.min(limit, BIRD_FOLLOW_PAGE_LIMIT);
299
+ const cappedMaxPages =
300
+ maxResources === undefined
301
+ ? maxPages
302
+ : Math.min(
303
+ maxPages ?? Number.POSITIVE_INFINITY,
304
+ Math.ceil(maxResources / birdLimit),
305
+ );
306
+ const payload = await listFollowUsersViaBird({
307
+ direction,
308
+ userId,
309
+ maxResults: Math.min(birdLimit, maxResources ?? birdLimit),
310
+ all: true,
311
+ maxPages: Number.isFinite(cappedMaxPages) ? cappedMaxPages : undefined,
312
+ });
313
+ return mergePages(
314
+ [payload],
315
+ typeof payload.meta?.next_token === "string"
316
+ ? String(payload.meta.next_token)
317
+ : undefined,
318
+ maxResources,
319
+ );
320
+ }
321
+
322
+ async function fetchFollowGraph({
323
+ mode,
324
+ direction,
325
+ username,
326
+ userId,
327
+ limit,
328
+ maxPages,
329
+ maxResources,
330
+ }: {
331
+ mode: FollowGraphSyncMode;
332
+ direction: FollowDirection;
333
+ username: string;
334
+ userId?: string;
335
+ limit: number;
336
+ maxPages?: number;
337
+ maxResources?: number;
338
+ }): Promise<{ source: FollowGraphLiveSource; payload: MergedFollowPayload }> {
339
+ if (mode === "bird") {
340
+ return {
341
+ source: "bird",
342
+ payload: await fetchFollowGraphViaBird({
343
+ direction,
344
+ userId,
345
+ limit,
346
+ maxPages,
347
+ maxResources,
348
+ }),
349
+ };
350
+ }
351
+ if (mode === "xurl") {
352
+ return {
353
+ source: "xurl",
354
+ payload: await fetchFollowGraphViaXurl({
355
+ direction,
356
+ username,
357
+ userId,
358
+ limit,
359
+ maxPages,
360
+ maxResources,
361
+ }),
362
+ };
363
+ }
364
+
365
+ try {
366
+ return {
367
+ source: "bird",
368
+ payload: await fetchFollowGraphViaBird({
369
+ direction,
370
+ userId,
371
+ limit,
372
+ maxPages,
373
+ maxResources,
374
+ }),
375
+ };
376
+ } catch (birdError) {
377
+ try {
378
+ return {
379
+ source: "xurl",
380
+ payload: await fetchFollowGraphViaXurl({
381
+ direction,
382
+ username,
383
+ userId,
384
+ limit,
385
+ maxPages,
386
+ maxResources,
387
+ }),
388
+ };
389
+ } catch (xurlError) {
390
+ throw new Error(
391
+ `follow graph sync failed via bird and xurl: bird: ${errorMessage(
392
+ birdError,
393
+ )}; xurl: ${errorMessage(xurlError)}`,
394
+ );
395
+ }
396
+ }
397
+ }
398
+
399
+ function getExistingEdges(
400
+ db: Database,
401
+ accountId: string,
402
+ direction: FollowDirection,
403
+ ) {
404
+ const rows = db
405
+ .prepare(
406
+ `
407
+ select profile_id, external_user_id, current
408
+ from follow_edges
409
+ where account_id = ? and direction = ?
410
+ `,
411
+ )
412
+ .all(accountId, direction) as Array<{
413
+ profile_id: string;
414
+ external_user_id: string;
415
+ current: number;
416
+ }>;
417
+ return new Map(rows.map((row) => [row.profile_id, row]));
418
+ }
419
+
420
+ function mergeFollowPayloadIntoLocalStore({
421
+ db,
422
+ accountId,
423
+ direction,
424
+ payload,
425
+ source,
426
+ }: {
427
+ db: Database;
428
+ accountId: string;
429
+ direction: FollowDirection;
430
+ payload: MergedFollowPayload;
431
+ source: FollowGraphLiveSource | "cache";
432
+ }) {
433
+ const now = new Date().toISOString();
434
+ const snapshotId = `follow_snapshot_${randomUUID()}`;
435
+ const status = payload.complete ? "complete" : "incomplete";
436
+
437
+ const insertSnapshot = db.prepare(`
438
+ insert into follow_snapshots (
439
+ id, account_id, direction, source, status, page_count, result_count,
440
+ started_at, completed_at, raw_meta_json
441
+ ) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
442
+ `);
443
+ const insertMember = db.prepare(`
444
+ insert into follow_snapshot_members (
445
+ snapshot_id, profile_id, external_user_id, position
446
+ ) values (?, ?, ?, ?)
447
+ `);
448
+ const insertEdge = db.prepare(`
449
+ insert into follow_edges (
450
+ account_id, direction, profile_id, external_user_id, source, current,
451
+ first_seen_at, last_seen_at, ended_at, updated_at
452
+ ) values (?, ?, ?, ?, ?, 1, ?, ?, null, ?)
453
+ on conflict(account_id, direction, profile_id) do update set
454
+ external_user_id = excluded.external_user_id,
455
+ source = excluded.source,
456
+ current = 1,
457
+ last_seen_at = excluded.last_seen_at,
458
+ ended_at = null,
459
+ updated_at = excluded.updated_at
460
+ `);
461
+ const endEdge = db.prepare(`
462
+ update follow_edges
463
+ set current = 0, ended_at = ?, updated_at = ?
464
+ where account_id = ? and direction = ? and profile_id = ?
465
+ `);
466
+ const insertEvent = db.prepare(`
467
+ insert into follow_events (
468
+ id, account_id, direction, profile_id, external_user_id, kind, event_at, snapshot_id
469
+ ) values (?, ?, ?, ?, ?, ?, ?, ?)
470
+ `);
471
+
472
+ return db.transaction(() => {
473
+ const existingEdges = getExistingEdges(db, accountId, direction);
474
+ const currentProfileIds = new Set<string>();
475
+
476
+ insertSnapshot.run(
477
+ snapshotId,
478
+ accountId,
479
+ direction,
480
+ source,
481
+ status,
482
+ payload.pageCount,
483
+ payload.data.length,
484
+ now,
485
+ now,
486
+ JSON.stringify(payload.meta),
487
+ );
488
+
489
+ payload.data.forEach((user, index) => {
490
+ const resolved = upsertProfileFromXUser(db, user);
491
+ currentProfileIds.add(resolved.profile.id);
492
+ insertMember.run(
493
+ snapshotId,
494
+ resolved.profile.id,
495
+ resolved.externalUserId,
496
+ index,
497
+ );
498
+
499
+ if (!payload.complete) {
500
+ return;
501
+ }
502
+
503
+ const previous = existingEdges.get(resolved.profile.id);
504
+ insertEdge.run(
505
+ accountId,
506
+ direction,
507
+ resolved.profile.id,
508
+ resolved.externalUserId,
509
+ source,
510
+ now,
511
+ now,
512
+ now,
513
+ );
514
+ if (!previous || previous.current === 0) {
515
+ insertEvent.run(
516
+ `follow_event_${randomUUID()}`,
517
+ accountId,
518
+ direction,
519
+ resolved.profile.id,
520
+ resolved.externalUserId,
521
+ "started",
522
+ now,
523
+ snapshotId,
524
+ );
525
+ }
526
+ });
527
+
528
+ if (payload.complete) {
529
+ for (const [profileId, previous] of existingEdges) {
530
+ if (previous.current === 1 && !currentProfileIds.has(profileId)) {
531
+ endEdge.run(now, now, accountId, direction, profileId);
532
+ insertEvent.run(
533
+ `follow_event_${randomUUID()}`,
534
+ accountId,
535
+ direction,
536
+ profileId,
537
+ previous.external_user_id,
538
+ "ended",
539
+ now,
540
+ snapshotId,
541
+ );
542
+ }
543
+ }
544
+ }
545
+
546
+ return {
547
+ snapshotId,
548
+ status,
549
+ count: payload.data.length,
550
+ pageCount: payload.pageCount,
551
+ };
552
+ })();
553
+ }
554
+
555
+ function makeDryRunResponse({
556
+ db,
557
+ account,
558
+ direction,
559
+ mode,
560
+ limit,
561
+ maxPages,
562
+ maxResources,
563
+ cacheKey,
564
+ cacheTtlMs,
565
+ }: {
566
+ db: Database;
567
+ account: ResolvedAccount;
568
+ direction: FollowDirection;
569
+ mode: FollowGraphSyncMode;
570
+ limit: number;
571
+ maxPages?: number;
572
+ maxResources?: number;
573
+ cacheKey: string;
574
+ cacheTtlMs: number;
575
+ }) {
576
+ const cached = readSyncCache<MergedFollowPayload>(cacheKey, db);
577
+ const cacheAgeMs = cached
578
+ ? Date.now() - new Date(cached.updatedAt).getTime()
579
+ : Number.POSITIVE_INFINITY;
580
+ const wouldCallLive = !cached || cacheAgeMs > cacheTtlMs;
581
+ return {
582
+ ok: true,
583
+ dryRun: true,
584
+ direction,
585
+ accountId: account.accountId,
586
+ mode,
587
+ wouldCallLive,
588
+ wouldCallX: mode === "bird" ? false : wouldCallLive,
589
+ requiredFlag: "--yes",
590
+ estimate: {
591
+ maxResultsPerPage: limit,
592
+ maxPages: maxPages ?? null,
593
+ maxResources: maxResources ?? null,
594
+ cacheTtlSeconds: Math.floor(cacheTtlMs / 1000),
595
+ },
596
+ cache: {
597
+ key: cacheKey,
598
+ hit: Boolean(cached),
599
+ fresh: Boolean(cached && cacheAgeMs <= cacheTtlMs),
600
+ updatedAt: cached?.updatedAt ?? null,
601
+ ageSeconds: Number.isFinite(cacheAgeMs)
602
+ ? Math.floor(cacheAgeMs / 1000)
603
+ : null,
604
+ count: cached?.value.data.length ?? 0,
605
+ },
606
+ currentCount: readCurrentCount(db, account.accountId, direction),
607
+ message:
608
+ "Dry run only. Pass --yes to use a fresh cache or perform live follow graph sync.",
609
+ };
610
+ }
611
+
612
+ function parseMode(value?: FollowGraphSyncMode) {
613
+ if (!value || value === "auto" || value === "bird" || value === "xurl") {
614
+ return value ?? "auto";
615
+ }
616
+ throw new Error("--mode must be auto, bird, or xurl");
617
+ }
618
+
619
+ function errorMessage(error: unknown) {
620
+ return error instanceof Error ? error.message : String(error);
621
+ }
622
+
623
+ export async function syncFollowGraph(options: SyncFollowGraphOptions) {
624
+ const db = getNativeDb();
625
+ const mode = parseMode(options.mode);
626
+ const limit = parseLimit(options.limit);
627
+ const maxPages = parseOptionalPositiveInteger(
628
+ "--max-pages",
629
+ options.maxPages,
630
+ );
631
+ const maxResources = parseOptionalPositiveInteger(
632
+ "--max-resources",
633
+ options.maxResources,
634
+ );
635
+ const cacheTtlMs = parseCacheTtlMs(options.cacheTtlMs);
636
+ const account = resolveAccount(db, options.account);
637
+ const cacheKey = buildCacheKey({
638
+ direction: options.direction,
639
+ accountId: account.accountId,
640
+ mode,
641
+ limit,
642
+ maxPages,
643
+ maxResources,
644
+ });
645
+
646
+ if (!options.yes) {
647
+ return makeDryRunResponse({
648
+ db,
649
+ account,
650
+ direction: options.direction,
651
+ mode,
652
+ limit,
653
+ maxPages,
654
+ maxResources,
655
+ cacheKey,
656
+ cacheTtlMs,
657
+ });
658
+ }
659
+
660
+ const cached = readSyncCache<MergedFollowPayload>(cacheKey, db);
661
+ const cacheAgeMs = cached
662
+ ? Date.now() - new Date(cached.updatedAt).getTime()
663
+ : Number.POSITIVE_INFINITY;
664
+ const useCache = Boolean(
665
+ !options.refresh && cached && cacheAgeMs <= cacheTtlMs,
666
+ );
667
+
668
+ const liveResult = useCache
669
+ ? undefined
670
+ : await fetchFollowGraph({
671
+ mode,
672
+ direction: options.direction,
673
+ username: account.username,
674
+ userId: account.externalUserId,
675
+ limit,
676
+ maxPages,
677
+ maxResources,
678
+ });
679
+ const payload = useCache ? cached!.value : liveResult!.payload;
680
+
681
+ if (!useCache) {
682
+ writeSyncCache(cacheKey, payload, db);
683
+ }
684
+
685
+ const mergeResult = mergeFollowPayloadIntoLocalStore({
686
+ db,
687
+ accountId: account.accountId,
688
+ direction: options.direction,
689
+ payload,
690
+ source: useCache ? "cache" : liveResult!.source,
691
+ });
692
+
693
+ return {
694
+ ok: true,
695
+ dryRun: false,
696
+ source: useCache ? "cache" : liveResult!.source,
697
+ mode,
698
+ direction: options.direction,
699
+ accountId: account.accountId,
700
+ status: mergeResult.status,
701
+ count: mergeResult.count,
702
+ pageCount: mergeResult.pageCount,
703
+ snapshotId: mergeResult.snapshotId,
704
+ partial: mergeResult.status === "incomplete",
705
+ cache: {
706
+ key: cacheKey,
707
+ reused: useCache,
708
+ updatedAt: useCache ? cached?.updatedAt : new Date().toISOString(),
709
+ },
710
+ warning:
711
+ mergeResult.status === "incomplete" && !options.allowPartial
712
+ ? "Snapshot is incomplete because a page/resource cap stopped pagination. It was recorded but not used for churn events."
713
+ : undefined,
714
+ };
715
+ }
716
+
717
+ function toGraphProfile(row: Record<string, unknown>): FollowGraphProfile {
718
+ return {
719
+ id: String(row.profile_id ?? row.id),
720
+ externalUserId: String(row.external_user_id),
721
+ handle: String(row.handle),
722
+ displayName: String(row.display_name),
723
+ bio: String(row.bio ?? ""),
724
+ followersCount: Number(row.followers_count ?? 0),
725
+ publicMetrics: parseJsonField<XurlPublicMetrics>(
726
+ row.public_metrics_json,
727
+ {},
728
+ ),
729
+ avatarUrl:
730
+ typeof row.avatar_url === "string" && row.avatar_url.length > 0
731
+ ? String(row.avatar_url)
732
+ : undefined,
733
+ };
734
+ }
735
+
736
+ export function listTopFollowers({
737
+ account,
738
+ limit = 20,
739
+ }: {
740
+ account?: string;
741
+ limit?: number;
742
+ } = {}) {
743
+ const db = getNativeDb();
744
+ const resolved = resolveAccount(db, account);
745
+ const rows = db
746
+ .prepare(
747
+ `
748
+ select
749
+ e.profile_id,
750
+ e.external_user_id,
751
+ p.handle,
752
+ p.display_name,
753
+ p.bio,
754
+ p.followers_count,
755
+ p.public_metrics_json,
756
+ p.avatar_url
757
+ from follow_edges e
758
+ join profiles p on p.id = e.profile_id
759
+ where e.account_id = ? and e.direction = 'followers' and e.current = 1
760
+ order by p.followers_count desc, lower(p.handle) asc
761
+ limit ?
762
+ `,
763
+ )
764
+ .all(resolved.accountId, limit) as Array<Record<string, unknown>>;
765
+ return {
766
+ accountId: resolved.accountId,
767
+ items: rows.map(toGraphProfile),
768
+ };
769
+ }
770
+
771
+ export function listMutuals({
772
+ account,
773
+ limit = 100,
774
+ }: {
775
+ account?: string;
776
+ limit?: number;
777
+ } = {}) {
778
+ const db = getNativeDb();
779
+ const resolved = resolveAccount(db, account);
780
+ const rows = db
781
+ .prepare(
782
+ `
783
+ select
784
+ following.profile_id,
785
+ following.external_user_id,
786
+ p.handle,
787
+ p.display_name,
788
+ p.bio,
789
+ p.followers_count,
790
+ p.public_metrics_json,
791
+ p.avatar_url
792
+ from follow_edges following
793
+ join follow_edges followers
794
+ on followers.account_id = following.account_id
795
+ and followers.profile_id = following.profile_id
796
+ and followers.direction = 'followers'
797
+ and followers.current = 1
798
+ join profiles p on p.id = following.profile_id
799
+ where
800
+ following.account_id = ?
801
+ and following.direction = 'following'
802
+ and following.current = 1
803
+ order by p.followers_count desc, lower(p.handle) asc
804
+ limit ?
805
+ `,
806
+ )
807
+ .all(resolved.accountId, limit) as Array<Record<string, unknown>>;
808
+ return {
809
+ accountId: resolved.accountId,
810
+ items: rows.map(toGraphProfile),
811
+ };
812
+ }
813
+
814
+ export function listNonMutualFollowing({
815
+ account,
816
+ sort = "followers",
817
+ limit = 100,
818
+ }: {
819
+ account?: string;
820
+ sort?: "followers" | "handle";
821
+ limit?: number;
822
+ } = {}) {
823
+ const db = getNativeDb();
824
+ const resolved = resolveAccount(db, account);
825
+ const orderBy =
826
+ sort === "handle"
827
+ ? "lower(p.handle) asc"
828
+ : "p.followers_count desc, lower(p.handle) asc";
829
+ const rows = db
830
+ .prepare(
831
+ `
832
+ select
833
+ following.profile_id,
834
+ following.external_user_id,
835
+ p.handle,
836
+ p.display_name,
837
+ p.bio,
838
+ p.followers_count,
839
+ p.public_metrics_json,
840
+ p.avatar_url
841
+ from follow_edges following
842
+ left join follow_edges followers
843
+ on followers.account_id = following.account_id
844
+ and followers.profile_id = following.profile_id
845
+ and followers.direction = 'followers'
846
+ and followers.current = 1
847
+ join profiles p on p.id = following.profile_id
848
+ where
849
+ following.account_id = ?
850
+ and following.direction = 'following'
851
+ and following.current = 1
852
+ and followers.profile_id is null
853
+ order by ${orderBy}
854
+ limit ?
855
+ `,
856
+ )
857
+ .all(resolved.accountId, limit) as Array<Record<string, unknown>>;
858
+ return {
859
+ accountId: resolved.accountId,
860
+ items: rows.map(toGraphProfile),
861
+ };
862
+ }
863
+
864
+ export function listUnfollowedSince({
865
+ account,
866
+ date,
867
+ direction = "followers",
868
+ limit = 100,
869
+ }: {
870
+ account?: string;
871
+ date: string;
872
+ direction?: FollowDirection;
873
+ limit?: number;
874
+ }) {
875
+ const db = getNativeDb();
876
+ const resolved = resolveAccount(db, account);
877
+ const since = date.includes("T") ? date : `${date}T00:00:00.000Z`;
878
+ const rows = db
879
+ .prepare(
880
+ `
881
+ select
882
+ ev.event_at,
883
+ ev.profile_id,
884
+ ev.external_user_id,
885
+ p.handle,
886
+ p.display_name,
887
+ p.bio,
888
+ p.followers_count,
889
+ p.public_metrics_json,
890
+ p.avatar_url
891
+ from follow_events ev
892
+ join profiles p on p.id = ev.profile_id
893
+ where
894
+ ev.account_id = ?
895
+ and ev.direction = ?
896
+ and ev.kind = 'ended'
897
+ and ev.event_at >= ?
898
+ order by ev.event_at desc, p.followers_count desc
899
+ limit ?
900
+ `,
901
+ )
902
+ .all(resolved.accountId, direction, since, limit) as Array<
903
+ Record<string, unknown>
904
+ >;
905
+ return {
906
+ accountId: resolved.accountId,
907
+ direction,
908
+ since,
909
+ items: rows.map((row) => ({
910
+ eventAt: String(row.event_at),
911
+ profile: toGraphProfile(row),
912
+ })),
913
+ };
914
+ }
915
+
916
+ export function listFollowEvents({
917
+ account,
918
+ direction,
919
+ kind,
920
+ since,
921
+ until,
922
+ limit = 100,
923
+ }: {
924
+ account?: string;
925
+ direction?: FollowDirection;
926
+ kind?: FollowEventKind;
927
+ since?: string;
928
+ until?: string;
929
+ limit?: number;
930
+ } = {}) {
931
+ const db = getNativeDb();
932
+ const resolved = resolveAccount(db, account);
933
+ const where = ["ev.account_id = ?"];
934
+ const params: unknown[] = [resolved.accountId];
935
+
936
+ if (direction) {
937
+ where.push("ev.direction = ?");
938
+ params.push(direction);
939
+ }
940
+ if (kind) {
941
+ where.push("ev.kind = ?");
942
+ params.push(kind);
943
+ }
944
+ if (since) {
945
+ where.push("ev.event_at >= ?");
946
+ params.push(since.includes("T") ? since : `${since}T00:00:00.000Z`);
947
+ }
948
+ if (until) {
949
+ where.push("ev.event_at < ?");
950
+ params.push(until.includes("T") ? until : `${until}T00:00:00.000Z`);
951
+ }
952
+
953
+ params.push(limit);
954
+ const rows = db
955
+ .prepare(
956
+ `
957
+ select
958
+ ev.event_at,
959
+ ev.direction,
960
+ ev.kind,
961
+ ev.snapshot_id,
962
+ ev.profile_id,
963
+ ev.external_user_id,
964
+ p.handle,
965
+ p.display_name,
966
+ p.bio,
967
+ p.followers_count,
968
+ p.public_metrics_json,
969
+ p.avatar_url
970
+ from follow_events ev
971
+ join profiles p on p.id = ev.profile_id
972
+ where ${where.join(" and ")}
973
+ order by ev.event_at desc, p.followers_count desc, lower(p.handle) asc
974
+ limit ?
975
+ `,
976
+ )
977
+ .all(...params) as Array<Record<string, unknown>>;
978
+
979
+ return {
980
+ accountId: resolved.accountId,
981
+ items: rows.map(
982
+ (row): FollowGraphEvent => ({
983
+ eventAt: String(row.event_at),
984
+ direction: row.direction as FollowDirection,
985
+ kind: row.kind as FollowEventKind,
986
+ snapshotId: String(row.snapshot_id),
987
+ profile: toGraphProfile(row),
988
+ }),
989
+ ),
990
+ };
991
+ }
992
+
993
+ export function getFollowGraphSummary({
994
+ account,
995
+ }: { account?: string } = {}): FollowGraphSummary {
996
+ const db = getNativeDb();
997
+ const resolved = resolveAccount(db, account);
998
+ const followers = readCurrentCount(db, resolved.accountId, "followers");
999
+ const following = readCurrentCount(db, resolved.accountId, "following");
1000
+ const mutualsRow = db
1001
+ .prepare(
1002
+ `
1003
+ select count(*) as count
1004
+ from follow_edges following
1005
+ join follow_edges followers
1006
+ on followers.account_id = following.account_id
1007
+ and followers.profile_id = following.profile_id
1008
+ and followers.direction = 'followers'
1009
+ and followers.current = 1
1010
+ where
1011
+ following.account_id = ?
1012
+ and following.direction = 'following'
1013
+ and following.current = 1
1014
+ `,
1015
+ )
1016
+ .get(resolved.accountId) as { count: number };
1017
+
1018
+ return {
1019
+ accountId: resolved.accountId,
1020
+ followers,
1021
+ following,
1022
+ mutuals: Number(mutualsRow.count),
1023
+ nonMutualFollowing: following - Number(mutualsRow.count),
1024
+ lastCompleteSnapshots: {
1025
+ followers: getLastSnapshot(
1026
+ db,
1027
+ resolved.accountId,
1028
+ "followers",
1029
+ "complete",
1030
+ ),
1031
+ following: getLastSnapshot(
1032
+ db,
1033
+ resolved.accountId,
1034
+ "following",
1035
+ "complete",
1036
+ ),
1037
+ },
1038
+ lastIncompleteSnapshots: {
1039
+ followers: getLastSnapshot(
1040
+ db,
1041
+ resolved.accountId,
1042
+ "followers",
1043
+ "incomplete",
1044
+ ),
1045
+ following: getLastSnapshot(
1046
+ db,
1047
+ resolved.accountId,
1048
+ "following",
1049
+ "incomplete",
1050
+ ),
1051
+ },
1052
+ };
1053
+ }