birdclaw 0.8.3 → 0.8.4

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 (284) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/README.md +10 -8
  3. package/bin/birdclaw.mjs +4 -66
  4. package/dist/cli/birdclaw.js +28165 -0
  5. package/dist/client/assets/AvatarChip-DLoqM5xD.js +1 -0
  6. package/dist/client/assets/ProfilePreview-BejAuYsJ.js +1 -0
  7. package/dist/client/assets/SavedTimelineView-BIS7HKpp.js +1 -0
  8. package/dist/client/assets/SmartTimestamp-BR0-9se2.js +1 -0
  9. package/dist/client/assets/TimelineRouteFrame-CyGPj0eM.js +1 -0
  10. package/dist/client/assets/blocks-7UDKQp8H.js +1 -0
  11. package/dist/client/assets/bookmarks-CbBkMvXk.js +1 -0
  12. package/dist/client/assets/chunk-C0phanGV.js +1 -0
  13. package/dist/client/assets/circle-check-C_Vq3ElD.js +1 -0
  14. package/dist/client/assets/client-stream-contracts-ys3XhWG4.js +5 -0
  15. package/dist/client/assets/createLucideIcon-JV8isFFR.js +1 -0
  16. package/dist/client/assets/data-sources-Cm2bpNvg.js +1 -0
  17. package/dist/client/assets/discuss-DTjaOugw.js +1 -0
  18. package/dist/client/assets/dms-CGd6Eogq.js +1 -0
  19. package/dist/client/assets/inbox-BHlGuOy2.js +1 -0
  20. package/dist/client/assets/index-CEIG7S6s.js +31 -0
  21. package/dist/client/assets/index-bpFrQ5yG.css +1 -0
  22. package/dist/client/assets/likes-cLrcAQem.js +1 -0
  23. package/dist/client/assets/links-CM9QO1LC.js +1 -0
  24. package/dist/client/assets/mapbox-BxTlqXt9.js +2 -0
  25. package/dist/client/assets/mapbox-gl-dgf_hX3a.js +3215 -0
  26. package/dist/client/assets/mentions-CxKCgzIf.js +1 -0
  27. package/dist/client/assets/network-map-B5a3whqj.js +2 -0
  28. package/dist/client/assets/profile-analyze-CRtNEwlb.js +1 -0
  29. package/dist/client/assets/profile-hydration-client-YEtJsNTv.js +1 -0
  30. package/dist/client/assets/profiles._handle-C82_HoKX.js +1 -0
  31. package/dist/client/assets/query-client-fYu2rzRC.js +17 -0
  32. package/dist/client/assets/rate-limits-ScaEwdVK.js +1 -0
  33. package/dist/client/assets/react-dom-DHl6Ug7F.js +1 -0
  34. package/dist/client/assets/react-nBylq1U5.js +1 -0
  35. package/dist/client/assets/repeat-2-xCwHM9hD.js +1 -0
  36. package/dist/client/assets/routes-q7ZaW4En.js +1 -0
  37. package/dist/client/assets/schemas-I9r6F6ya.js +64 -0
  38. package/dist/client/assets/search-Dq5kYsxA.js +1 -0
  39. package/dist/client/assets/sparkles-CQVlvHsZ.js +1 -0
  40. package/dist/client/assets/styles-B8s3tt3r.css +2 -0
  41. package/dist/client/assets/today-CeG1O7Py.js +1 -0
  42. package/dist/client/assets/triangle-alert-CBL7s2pf.js +1 -0
  43. package/dist/client/assets/useTimelineRouteData-1HsqgvUi.js +1 -0
  44. package/dist/client/assets/users-CHbKlqUr.js +1 -0
  45. package/dist/server/assets/AvatarChip-zUDAEmjL.js +105 -0
  46. package/dist/server/assets/FeedState-ntW0Q2GB.js +214 -0
  47. package/dist/server/assets/ProfileAnalysisStream-BFvhVdhn.js +198 -0
  48. package/dist/server/assets/ProfilePreview-C6gAor_j.js +752 -0
  49. package/dist/server/assets/SavedTimelineView-BG1jnBp1.js +64 -0
  50. package/dist/server/assets/SmartTimestamp-DfwE1NGf.js +45 -0
  51. package/dist/server/assets/SyncNowButton-BOn_IBXS.js +76 -0
  52. package/dist/server/assets/TimelineRouteFrame-CU7ilM7U.js +85 -0
  53. package/dist/server/assets/TweetRichText-DhCVv1-6.js +74 -0
  54. package/dist/server/assets/_tanstack-start-manifest_v-C-ZRnG_n.js +148 -0
  55. package/dist/server/assets/account-selection-CTdFGWRk.js +44 -0
  56. package/dist/server/assets/blocks-C1mgGTB3.js +299 -0
  57. package/dist/server/assets/blocks-DSgQghqP.js +17 -0
  58. package/dist/server/assets/bookmarks-BJN8tykY.js +14 -0
  59. package/dist/server/assets/client-stream-contracts-C2xbWVjm.js +769 -0
  60. package/dist/server/assets/data-sources-BnA1Q94p.js +180 -0
  61. package/dist/server/assets/discuss-CJ1dau1j.js +15 -0
  62. package/dist/server/assets/discuss-QTo08NCN.js +338 -0
  63. package/dist/server/assets/dms-BuLj4bp8.js +368 -0
  64. package/dist/server/assets/dms-DAyQrVM2.js +240 -0
  65. package/dist/server/assets/empty-plugin-adapters-DzrEkdBF.js +5 -0
  66. package/dist/server/assets/inbox-CkGOaW1q.js +214 -0
  67. package/dist/server/assets/inbox-DpWd1EzC.js +146 -0
  68. package/dist/server/assets/likes-DBOAtsJU.js +14 -0
  69. package/dist/server/assets/links-DAhSjynu.js +240 -0
  70. package/dist/server/assets/links-DmH5lxHO.js +468 -0
  71. package/dist/server/assets/mentions-BH2MzKtB.js +26 -0
  72. package/dist/server/assets/network-map-D3CzRcC4.js +224 -0
  73. package/dist/server/assets/network-map-D5DoLOwR.js +582 -0
  74. package/dist/server/assets/profile-analyze-BP65PED0.js +90 -0
  75. package/dist/server/assets/profile-analyze-C1NoM0tV.js +9 -0
  76. package/dist/server/assets/profile-hydration-client-GJ4kwJS8.js +133 -0
  77. package/dist/server/assets/profiles._handle-DjwlN_ac.js +184 -0
  78. package/dist/server/assets/profiles._handle-DsLNj2Ca.js +12 -0
  79. package/dist/server/assets/query-client-BdauHBgd.js +923 -0
  80. package/dist/server/assets/rate-limits-VAr2qzYD.js +262 -0
  81. package/dist/server/assets/route-search-BUlQ5lGM.js +117 -0
  82. package/dist/server/assets/router-DqkwSPiM.js +17026 -0
  83. package/dist/server/assets/routes-A9c9ZhwN.js +26 -0
  84. package/dist/server/assets/start-FaDOTmto.js +4 -0
  85. package/dist/server/assets/today-B5hVM57Z.js +17 -0
  86. package/dist/server/assets/today-fvGiZ7ud.js +303 -0
  87. package/dist/server/assets/ui-qbmcMSSX.js +137 -0
  88. package/{src/components/useDebouncedValue.ts → dist/server/assets/useDebouncedValue-DOnA6Xcy.js} +4 -4
  89. package/dist/server/assets/useTimelineRouteData-27cxynxa.js +1101 -0
  90. package/dist/server/server.js +5520 -0
  91. package/package.json +26 -34
  92. package/playwright.config.ts +0 -35
  93. package/scripts/browser-perf.mjs +0 -427
  94. package/scripts/build-docs-site.mjs +0 -967
  95. package/scripts/docs-site-assets.mjs +0 -311
  96. package/scripts/run-vitest.mjs +0 -21
  97. package/scripts/sanitize-node-options.mjs +0 -23
  98. package/scripts/start-test-server.mjs +0 -42
  99. package/src/cli/command-context.ts +0 -17
  100. package/src/cli/register-analysis.ts +0 -500
  101. package/src/cli/register-compose.ts +0 -40
  102. package/src/cli/register-graph.ts +0 -132
  103. package/src/cli/register-inbox.ts +0 -41
  104. package/src/cli/register-storage.ts +0 -106
  105. package/src/cli-moderation.ts +0 -210
  106. package/src/cli.ts +0 -1616
  107. package/src/components/AccountSwitcher.tsx +0 -178
  108. package/src/components/AppNav.tsx +0 -118
  109. package/src/components/AvatarChip.tsx +0 -58
  110. package/src/components/AvatarPreload.ts +0 -149
  111. package/src/components/BrandMark.tsx +0 -67
  112. package/src/components/ConversationThread.tsx +0 -131
  113. package/src/components/DmWorkspace.tsx +0 -286
  114. package/src/components/EmbeddedTweetCard.tsx +0 -55
  115. package/src/components/FeedState.tsx +0 -147
  116. package/src/components/FloatingPreview.tsx +0 -382
  117. package/src/components/InboxCard.tsx +0 -152
  118. package/src/components/LinkPreviewCard.tsx +0 -292
  119. package/src/components/MarkdownCitations.tsx +0 -680
  120. package/src/components/MarkdownViewer.tsx +0 -111
  121. package/src/components/ProfileAnalysisClient.ts +0 -191
  122. package/src/components/ProfileAnalysisStream.tsx +0 -259
  123. package/src/components/ProfilePreview.tsx +0 -131
  124. package/src/components/SavedTimelineView.tsx +0 -155
  125. package/src/components/SmartTimestamp.tsx +0 -72
  126. package/src/components/SyncNowButton.tsx +0 -140
  127. package/src/components/ThemeSlider.tsx +0 -80
  128. package/src/components/TimelineCard.tsx +0 -533
  129. package/src/components/TimelineRouteFrame.tsx +0 -186
  130. package/src/components/TweetArticleCard.tsx +0 -66
  131. package/src/components/TweetMediaGrid.tsx +0 -185
  132. package/src/components/TweetRichText.tsx +0 -159
  133. package/src/components/account-selection.ts +0 -64
  134. package/src/components/links-controller.ts +0 -162
  135. package/src/components/links-model.ts +0 -198
  136. package/src/components/network-map-controller.ts +0 -84
  137. package/src/components/network-map-model.ts +0 -255
  138. package/src/components/useTimelineRouteData.ts +0 -193
  139. package/src/lib/account-sync-job.ts +0 -666
  140. package/src/lib/actions-transport.ts +0 -300
  141. package/src/lib/analysis-runtime.ts +0 -238
  142. package/src/lib/api-client.ts +0 -220
  143. package/src/lib/api-contracts.ts +0 -328
  144. package/src/lib/archive-finder.ts +0 -185
  145. package/src/lib/archive-import-plan.ts +0 -102
  146. package/src/lib/archive-import.ts +0 -2695
  147. package/src/lib/authored-live.ts +0 -1027
  148. package/src/lib/avatar-cache.ts +0 -349
  149. package/src/lib/backup.ts +0 -2652
  150. package/src/lib/bird-actions.ts +0 -294
  151. package/src/lib/bird-command.ts +0 -130
  152. package/src/lib/bird.ts +0 -1140
  153. package/src/lib/blocklist.ts +0 -136
  154. package/src/lib/blocks-write.ts +0 -171
  155. package/src/lib/blocks.ts +0 -387
  156. package/src/lib/bookmark-sync-job.ts +0 -536
  157. package/src/lib/config.ts +0 -224
  158. package/src/lib/conversation-surface.ts +0 -205
  159. package/src/lib/data-sources.ts +0 -219
  160. package/src/lib/database-metrics.ts +0 -88
  161. package/src/lib/database-migrations.ts +0 -34
  162. package/src/lib/database-schema.ts +0 -312
  163. package/src/lib/database-writer.ts +0 -69
  164. package/src/lib/db.ts +0 -889
  165. package/src/lib/dm-read-model.ts +0 -533
  166. package/src/lib/dms-live.ts +0 -870
  167. package/src/lib/effect-runtime.ts +0 -45
  168. package/src/lib/follow-graph.ts +0 -1087
  169. package/src/lib/geocoding.ts +0 -296
  170. package/src/lib/http-effect.ts +0 -222
  171. package/src/lib/identity-search-index.ts +0 -369
  172. package/src/lib/import-repository.ts +0 -138
  173. package/src/lib/inbox.ts +0 -242
  174. package/src/lib/link-index.ts +0 -641
  175. package/src/lib/link-insights.ts +0 -860
  176. package/src/lib/link-preview-metadata.ts +0 -754
  177. package/src/lib/live-sync-engine.ts +0 -209
  178. package/src/lib/live-transport-gateway.ts +0 -128
  179. package/src/lib/location.ts +0 -137
  180. package/src/lib/media-fetch.ts +0 -860
  181. package/src/lib/media-includes.ts +0 -165
  182. package/src/lib/mention-threads-live.ts +0 -830
  183. package/src/lib/mentions-export.ts +0 -147
  184. package/src/lib/mentions-live.ts +0 -1114
  185. package/src/lib/moderation-target.ts +0 -227
  186. package/src/lib/moderation-write.ts +0 -131
  187. package/src/lib/mutes-write.ts +0 -171
  188. package/src/lib/mutes.ts +0 -84
  189. package/src/lib/ndjson-stream.ts +0 -106
  190. package/src/lib/network-map.ts +0 -382
  191. package/src/lib/openai-response-runtime.ts +0 -251
  192. package/src/lib/openai.ts +0 -117
  193. package/src/lib/paginated-sync.ts +0 -93
  194. package/src/lib/period-digest.ts +0 -1364
  195. package/src/lib/present.ts +0 -118
  196. package/src/lib/profile-affiliation-hydration.ts +0 -228
  197. package/src/lib/profile-affiliations.ts +0 -262
  198. package/src/lib/profile-analysis.ts +0 -1198
  199. package/src/lib/profile-bio-entities.ts +0 -318
  200. package/src/lib/profile-history.ts +0 -164
  201. package/src/lib/profile-hydration.ts +0 -180
  202. package/src/lib/profile-replies.ts +0 -77
  203. package/src/lib/profile-resolver.ts +0 -650
  204. package/src/lib/queries.ts +0 -6
  205. package/src/lib/query-actions.ts +0 -437
  206. package/src/lib/query-client.tsx +0 -47
  207. package/src/lib/query-read-model-shared.ts +0 -52
  208. package/src/lib/query-read-models.ts +0 -5
  209. package/src/lib/query-resource.ts +0 -41
  210. package/src/lib/query-status.ts +0 -164
  211. package/src/lib/research.ts +0 -652
  212. package/src/lib/runtime-services.ts +0 -20
  213. package/src/lib/search-discussion.ts +0 -812
  214. package/src/lib/seed.ts +0 -613
  215. package/src/lib/server-runtime-services.ts +0 -30
  216. package/src/lib/sqlite.ts +0 -183
  217. package/src/lib/streaming-ingestion.ts +0 -240
  218. package/src/lib/sync-cache.ts +0 -70
  219. package/src/lib/sync-plan.ts +0 -175
  220. package/src/lib/theme-transition.ts +0 -113
  221. package/src/lib/theme.tsx +0 -165
  222. package/src/lib/timeline-collections-live.ts +0 -407
  223. package/src/lib/timeline-live.ts +0 -302
  224. package/src/lib/timeline-read-model.ts +0 -1191
  225. package/src/lib/tweet-account-edges.ts +0 -48
  226. package/src/lib/tweet-lookup.ts +0 -46
  227. package/src/lib/tweet-render.ts +0 -430
  228. package/src/lib/tweet-repository.ts +0 -156
  229. package/src/lib/tweet-search-live.ts +0 -462
  230. package/src/lib/types.ts +0 -723
  231. package/src/lib/ui.ts +0 -529
  232. package/src/lib/url-expansion-store.ts +0 -110
  233. package/src/lib/url-expansion.ts +0 -343
  234. package/src/lib/url-safety.ts +0 -220
  235. package/src/lib/web-sync.ts +0 -533
  236. package/src/lib/whois.ts +0 -1080
  237. package/src/lib/x-profile.ts +0 -365
  238. package/src/lib/x-web.ts +0 -199
  239. package/src/lib/xurl-rate-limits.ts +0 -267
  240. package/src/lib/xurl.ts +0 -1677
  241. package/src/routeTree.gen.ts +0 -723
  242. package/src/router.tsx +0 -20
  243. package/src/routes/__root.tsx +0 -82
  244. package/src/routes/api/action.tsx +0 -147
  245. package/src/routes/api/avatar.tsx +0 -50
  246. package/src/routes/api/blocks.tsx +0 -35
  247. package/src/routes/api/conversation.tsx +0 -45
  248. package/src/routes/api/data-sources.tsx +0 -24
  249. package/src/routes/api/inbox.tsx +0 -43
  250. package/src/routes/api/link-insights.tsx +0 -85
  251. package/src/routes/api/link-preview.tsx +0 -47
  252. package/src/routes/api/network-map.tsx +0 -55
  253. package/src/routes/api/period-digest.tsx +0 -101
  254. package/src/routes/api/profile-analysis.tsx +0 -97
  255. package/src/routes/api/profile-hydrate.tsx +0 -54
  256. package/src/routes/api/query.tsx +0 -121
  257. package/src/routes/api/search-discussion.tsx +0 -113
  258. package/src/routes/api/status.tsx +0 -30
  259. package/src/routes/api/sync.tsx +0 -108
  260. package/src/routes/api/xurl-rate-limits.tsx +0 -24
  261. package/src/routes/blocks.tsx +0 -343
  262. package/src/routes/bookmarks.tsx +0 -18
  263. package/src/routes/data-sources.tsx +0 -249
  264. package/src/routes/discuss.tsx +0 -419
  265. package/src/routes/dms.tsx +0 -415
  266. package/src/routes/inbox.tsx +0 -232
  267. package/src/routes/index.tsx +0 -32
  268. package/src/routes/likes.tsx +0 -18
  269. package/src/routes/links.tsx +0 -631
  270. package/src/routes/mentions.tsx +0 -32
  271. package/src/routes/network-map.tsx +0 -732
  272. package/src/routes/profile-analyze.tsx +0 -112
  273. package/src/routes/profiles.$handle.tsx +0 -228
  274. package/src/routes/rate-limits.tsx +0 -305
  275. package/src/routes/today.tsx +0 -511
  276. package/src/styles.css +0 -257
  277. package/tsconfig.json +0 -29
  278. package/vite.config.ts +0 -29
  279. /package/{public → dist/client}/birdclaw-mark.png +0 -0
  280. /package/{public → dist/client}/favicon.ico +0 -0
  281. /package/{public → dist/client}/logo192.png +0 -0
  282. /package/{public → dist/client}/logo512.png +0 -0
  283. /package/{public → dist/client}/manifest.json +0 -0
  284. /package/{public → dist/client}/robots.txt +0 -0
@@ -1,870 +0,0 @@
1
- import { Effect } from "effect";
2
- import type { Database } from "./sqlite";
3
- import {
4
- type BirdDmConversation,
5
- type BirdDmEvent,
6
- type BirdDmUser,
7
- } from "./bird";
8
- import { getNativeDb } from "./db";
9
- import { runEffectPromise } from "./effect-runtime";
10
- import { liveTransportGateway } from "./live-transport-gateway";
11
- import {
12
- assertLiveAccountMatches,
13
- resolveLiveSyncAccount,
14
- type LiveSyncAccount,
15
- } from "./live-sync-engine";
16
- import { readSyncCache, writeSyncCache } from "./sync-cache";
17
- import { runSyncPlanEffect } from "./sync-plan";
18
- import type { XurlDmEventsResponse, XurlMentionUser } from "./types";
19
- import {
20
- buildExternalProfileId,
21
- randomAvatarHue,
22
- upsertProfileFromXUser,
23
- } from "./x-profile";
24
-
25
- export const DEFAULT_DMS_CACHE_TTL_MS = 2 * 60_000;
26
- const PREVIEW_MESSAGE_ID_PREFIX = "preview:";
27
- const XURL_DMS_MAX_RESULTS = 100;
28
-
29
- export type DirectMessagesSyncMode = "auto" | "bird" | "xurl";
30
-
31
- export interface SyncDirectMessagesViaCachedBirdOptions {
32
- account?: string;
33
- mode?: DirectMessagesSyncMode;
34
- limit?: number;
35
- inbox?: "all" | "accepted" | "requests";
36
- maxPages?: number;
37
- allPages?: boolean;
38
- pageDelayMs?: number;
39
- refresh?: boolean;
40
- cacheTtlMs?: number;
41
- }
42
-
43
- function parseCacheTtlMs(value?: number) {
44
- if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
45
- return DEFAULT_DMS_CACHE_TTL_MS;
46
- }
47
- return Math.floor(value);
48
- }
49
-
50
- function assertBirdLimit(limit: number) {
51
- if (!Number.isFinite(limit) || limit < 1) {
52
- throw new Error("bird DM mode requires --limit of at least 1");
53
- }
54
- }
55
-
56
- function assertXurlLimit(limit: number) {
57
- if (!Number.isFinite(limit) || limit < 1 || limit > XURL_DMS_MAX_RESULTS) {
58
- throw new Error("xurl DM mode requires --limit between 1 and 100");
59
- }
60
- }
61
-
62
- function parseSyncMode(mode: DirectMessagesSyncMode | undefined) {
63
- if (!mode || mode === "bird" || mode === "xurl" || mode === "auto") {
64
- return mode ?? "bird";
65
- }
66
- throw new Error("--mode must be auto, bird, or xurl");
67
- }
68
-
69
- function makePreviewMessageId(conversationId: string): string {
70
- return `${PREVIEW_MESSAGE_ID_PREFIX}${conversationId}`;
71
- }
72
-
73
- function deleteDmFtsRows(db: Database, messageIds: string[]) {
74
- const chunkSize = 500;
75
- for (let index = 0; index < messageIds.length; index += chunkSize) {
76
- const chunk = messageIds.slice(index, index + chunkSize);
77
- if (chunk.length === 0) continue;
78
- db.prepare(
79
- `delete from dm_fts where message_id in (${chunk.map(() => "?").join(",")})`,
80
- ).run(...chunk);
81
- }
82
- }
83
-
84
- function toIsoTimestamp(value?: string) {
85
- if (!value) {
86
- return new Date().toISOString();
87
- }
88
- const parsed = new Date(value);
89
- return Number.isNaN(parsed.getTime()) ? value : parsed.toISOString();
90
- }
91
-
92
- function toXUser(user: BirdDmUser): XurlMentionUser {
93
- return {
94
- id: user.id,
95
- username: user.username ?? `user_${user.id}`,
96
- name: user.name ?? user.username ?? `user_${user.id}`,
97
- profile_image_url: user.profileImageUrl,
98
- public_metrics: { followers_count: 0 },
99
- };
100
- }
101
-
102
- function collectUsers(
103
- payload: {
104
- conversations: BirdDmConversation[];
105
- events: BirdDmEvent[];
106
- },
107
- accountExternalUserId?: string,
108
- ) {
109
- const users = new Map<string, BirdDmUser>();
110
- const add = (user?: BirdDmUser) => {
111
- if (!user?.id) return;
112
- if (
113
- accountExternalUserId &&
114
- user.id === accountExternalUserId &&
115
- !user.username &&
116
- !user.name
117
- ) {
118
- return;
119
- }
120
- users.set(user.id, { ...users.get(user.id), ...user });
121
- };
122
- const addId = (id?: string) => {
123
- if (!id || users.has(id) || id === accountExternalUserId) return;
124
- users.set(id, { id });
125
- };
126
-
127
- for (const conversation of payload.conversations) {
128
- for (const participant of conversation.participants) {
129
- add(participant);
130
- }
131
- }
132
- for (const event of payload.events) {
133
- add(event.sender);
134
- add(event.recipient);
135
- addId(event.senderId);
136
- addId(event.recipientId);
137
- }
138
- return users;
139
- }
140
-
141
- function getLocalExternalUserId(
142
- users: Map<string, BirdDmUser>,
143
- accountUsername: string,
144
- accountExternalUserId?: string,
145
- ) {
146
- if (accountExternalUserId) {
147
- return accountExternalUserId;
148
- }
149
- const normalizedAccountUsername = accountUsername.toLowerCase();
150
- for (const user of users.values()) {
151
- if (user.username?.toLowerCase() === normalizedAccountUsername) {
152
- return user.id;
153
- }
154
- }
155
- return undefined;
156
- }
157
-
158
- function getLatestEvent(events: BirdDmEvent[]) {
159
- return [...events].sort(
160
- (left, right) =>
161
- new Date(right.createdAt ?? 0).getTime() -
162
- new Date(left.createdAt ?? 0).getTime(),
163
- )[0];
164
- }
165
-
166
- function assertAuthenticatedBirdAccountMatches({
167
- source,
168
- account,
169
- liveUsername,
170
- liveExternalUserId,
171
- }: {
172
- source: "bird" | "xurl";
173
- account: LiveSyncAccount;
174
- liveUsername: string;
175
- liveExternalUserId?: string;
176
- }) {
177
- assertLiveAccountMatches({
178
- source,
179
- account,
180
- liveUsername,
181
- liveExternalUserId,
182
- });
183
- }
184
-
185
- function getAuthenticatedXurlAccount(payload: Record<string, unknown> | null): {
186
- id?: string;
187
- username?: string;
188
- } {
189
- if (!payload) return {};
190
- return {
191
- ...(typeof payload.id === "string" ? { id: payload.id } : {}),
192
- ...(typeof payload.username === "string"
193
- ? { username: payload.username }
194
- : {}),
195
- };
196
- }
197
-
198
- function persistAccountExternalUserId(
199
- db: Database,
200
- accountId: string,
201
- externalUserId: string,
202
- ) {
203
- db.prepare(
204
- `
205
- update accounts
206
- set external_user_id = ?
207
- where id = ?
208
- and (external_user_id is null or trim(external_user_id) = '')
209
- `,
210
- ).run(externalUserId, accountId);
211
- }
212
-
213
- function conversationIdReferencesExternalUserId(
214
- conversationId: string,
215
- externalUserId: string,
216
- ) {
217
- return conversationId.split(/[^0-9]+/).includes(externalUserId);
218
- }
219
-
220
- function payloadReferencesExternalUserId(
221
- payload: {
222
- conversations: BirdDmConversation[];
223
- events: BirdDmEvent[];
224
- },
225
- externalUserId: string,
226
- ) {
227
- for (const conversation of payload.conversations) {
228
- if (
229
- conversationIdReferencesExternalUserId(conversation.id, externalUserId)
230
- ) {
231
- return true;
232
- }
233
- if (conversation.participants.some((user) => user.id === externalUserId)) {
234
- return true;
235
- }
236
- }
237
- for (const event of payload.events) {
238
- if (
239
- event.senderId === externalUserId ||
240
- event.recipientId === externalUserId
241
- ) {
242
- return true;
243
- }
244
- if (
245
- event.sender?.id === externalUserId ||
246
- event.recipient?.id === externalUserId
247
- ) {
248
- return true;
249
- }
250
- if (
251
- event.conversationId &&
252
- conversationIdReferencesExternalUserId(
253
- event.conversationId,
254
- externalUserId,
255
- )
256
- ) {
257
- return true;
258
- }
259
- }
260
- return false;
261
- }
262
-
263
- function ensureSparseLocalProfile(
264
- db: Database,
265
- externalUserId: string,
266
- accountUsername: string,
267
- ) {
268
- const profileId = buildExternalProfileId(externalUserId);
269
- const existing = db
270
- .prepare("select id from profiles where id = ? or handle = ? limit 1")
271
- .get(profileId, accountUsername) as { id: string } | undefined;
272
- if (existing) {
273
- return existing.id;
274
- }
275
-
276
- const createdAt = new Date().toISOString();
277
- db.prepare(
278
- `
279
- insert into profiles (
280
- id, handle, display_name, bio, followers_count, following_count,
281
- public_metrics_json, avatar_hue, entities_json, raw_json, created_at
282
- ) values (?, ?, ?, '', 0, 0, '{}', ?, '{}', '{}', ?)
283
- `,
284
- ).run(
285
- profileId,
286
- accountUsername,
287
- accountUsername,
288
- randomAvatarHue(accountUsername),
289
- createdAt,
290
- );
291
- return profileId;
292
- }
293
-
294
- function mergeDirectMessagesIntoLocalStore(
295
- db: Database,
296
- accountId: string,
297
- accountUsername: string,
298
- accountExternalUserId: string | undefined,
299
- payload: {
300
- conversations: BirdDmConversation[];
301
- events: BirdDmEvent[];
302
- },
303
- ) {
304
- const users = collectUsers(payload, accountExternalUserId);
305
- const localExternalUserId = getLocalExternalUserId(
306
- users,
307
- accountUsername,
308
- accountExternalUserId,
309
- );
310
- if (
311
- accountExternalUserId &&
312
- (payload.conversations.length > 0 || payload.events.length > 0) &&
313
- !payloadReferencesExternalUserId(payload, accountExternalUserId)
314
- ) {
315
- throw new Error(
316
- `bird DM payload does not include @${accountUsername}; refusing to sync into ${accountId}`,
317
- );
318
- }
319
- if (
320
- !localExternalUserId &&
321
- (payload.conversations.length > 0 || payload.events.length > 0)
322
- ) {
323
- throw new Error(
324
- `bird DM payload does not include @${accountUsername}; refusing to sync into ${accountId}`,
325
- );
326
- }
327
- if (!localExternalUserId) {
328
- return;
329
- }
330
- const profilesByExternalId = new Map<string, string>();
331
- for (const user of users.values()) {
332
- const resolved = upsertProfileFromXUser(db, toXUser(user));
333
- profilesByExternalId.set(user.id, resolved.profile.id);
334
- }
335
- if (
336
- accountExternalUserId &&
337
- !profilesByExternalId.has(accountExternalUserId)
338
- ) {
339
- profilesByExternalId.set(
340
- accountExternalUserId,
341
- ensureSparseLocalProfile(db, accountExternalUserId, accountUsername),
342
- );
343
- }
344
-
345
- const eventsByConversation = new Map<string, BirdDmEvent[]>();
346
- for (const event of payload.events) {
347
- if (!event.conversationId) continue;
348
- const events = eventsByConversation.get(event.conversationId) ?? [];
349
- events.push(event);
350
- eventsByConversation.set(event.conversationId, events);
351
- }
352
-
353
- const upsertConversation = db.prepare(`
354
- insert into dm_conversations (
355
- id, account_id, participant_profile_id, title, inbox_kind, last_message_at, unread_count, needs_reply
356
- ) values (?, ?, ?, ?, ?, ?, 0, ?)
357
- on conflict(id) do update set
358
- account_id = excluded.account_id,
359
- participant_profile_id = excluded.participant_profile_id,
360
- title = excluded.title,
361
- inbox_kind = excluded.inbox_kind,
362
- last_message_at = excluded.last_message_at,
363
- needs_reply = excluded.needs_reply
364
- `);
365
- const upsertMessage = db.prepare(`
366
- insert into dm_messages (
367
- id, conversation_id, sender_profile_id, text, created_at, direction, is_replied, media_count
368
- ) values (?, ?, ?, ?, ?, ?, 0, 0)
369
- on conflict(id) do update set
370
- conversation_id = excluded.conversation_id,
371
- sender_profile_id = excluded.sender_profile_id,
372
- text = excluded.text,
373
- created_at = excluded.created_at,
374
- direction = excluded.direction,
375
- media_count = excluded.media_count
376
- `);
377
- const insertFts = db.prepare(
378
- "insert into dm_fts (message_id, text) values (?, ?)",
379
- );
380
- const deleteMessage = db.prepare("delete from dm_messages where id = ?");
381
- const ftsMessageIdsToReplace = new Set<string>();
382
- for (const conversation of payload.conversations) {
383
- const events = eventsByConversation.get(conversation.id) ?? [];
384
- if (events.length === 0 && !conversation.lastMessagePreview) {
385
- continue;
386
- }
387
- const participant =
388
- conversation.participants.find(
389
- (user) =>
390
- user.id !== localExternalUserId &&
391
- user.username?.toLowerCase() !== accountUsername.toLowerCase(),
392
- ) ?? conversation.participants[0];
393
- if (!participant || !profilesByExternalId.has(participant.id)) {
394
- continue;
395
- }
396
- if (events.length === 0) {
397
- ftsMessageIdsToReplace.add(makePreviewMessageId(conversation.id));
398
- continue;
399
- }
400
- ftsMessageIdsToReplace.add(makePreviewMessageId(conversation.id));
401
- for (const event of events) {
402
- const senderId = event.senderId ?? event.sender?.id;
403
- if (senderId && profilesByExternalId.has(senderId)) {
404
- ftsMessageIdsToReplace.add(event.id);
405
- }
406
- }
407
- }
408
-
409
- db.transaction(() => {
410
- deleteDmFtsRows(db, [...ftsMessageIdsToReplace]);
411
- const ftsTextByMessageId = new Map<string, string>();
412
-
413
- for (const conversation of payload.conversations) {
414
- const events = eventsByConversation.get(conversation.id) ?? [];
415
- if (events.length === 0 && !conversation.lastMessagePreview) {
416
- continue;
417
- }
418
-
419
- const participant =
420
- conversation.participants.find(
421
- (user) =>
422
- user.id !== localExternalUserId &&
423
- user.username?.toLowerCase() !== accountUsername.toLowerCase(),
424
- ) ?? conversation.participants[0];
425
- if (!participant) {
426
- continue;
427
- }
428
- const participantProfileId = profilesByExternalId.get(participant.id);
429
- if (!participantProfileId) {
430
- continue;
431
- }
432
-
433
- const latest = getLatestEvent(events);
434
- const lastMessageAt = toIsoTimestamp(
435
- latest?.createdAt ?? conversation.lastMessageAt,
436
- );
437
- const inboxKind =
438
- conversation.inboxKind ??
439
- (conversation.isMessageRequest ? "request" : "accepted");
440
- const latestInbound = latest
441
- ? latest.senderId !== localExternalUserId &&
442
- latest.sender?.username?.toLowerCase() !==
443
- accountUsername.toLowerCase()
444
- : inboxKind === "request";
445
- upsertConversation.run(
446
- conversation.id,
447
- accountId,
448
- participantProfileId,
449
- participant.username ?? participant.name ?? participant.id,
450
- inboxKind,
451
- lastMessageAt,
452
- latestInbound ? 1 : 0,
453
- );
454
-
455
- const previewMessageId = makePreviewMessageId(conversation.id);
456
- if (events.length === 0 && conversation.lastMessagePreview) {
457
- const previewSenderProfileId = latestInbound
458
- ? participantProfileId
459
- : (profilesByExternalId.get(localExternalUserId) ??
460
- participantProfileId);
461
- upsertMessage.run(
462
- previewMessageId,
463
- conversation.id,
464
- previewSenderProfileId,
465
- conversation.lastMessagePreview,
466
- lastMessageAt,
467
- latestInbound ? "inbound" : "outbound",
468
- );
469
- ftsTextByMessageId.set(
470
- previewMessageId,
471
- conversation.lastMessagePreview,
472
- );
473
- continue;
474
- }
475
-
476
- deleteMessage.run(previewMessageId);
477
-
478
- for (const event of events) {
479
- const senderId = event.senderId ?? event.sender?.id;
480
- if (!senderId) {
481
- continue;
482
- }
483
- const senderProfileId = profilesByExternalId.get(senderId);
484
- if (!senderProfileId) {
485
- continue;
486
- }
487
- const direction =
488
- senderId === localExternalUserId ||
489
- event.sender?.username?.toLowerCase() ===
490
- accountUsername.toLowerCase()
491
- ? "outbound"
492
- : "inbound";
493
- upsertMessage.run(
494
- event.id,
495
- conversation.id,
496
- senderProfileId,
497
- event.text,
498
- toIsoTimestamp(event.createdAt),
499
- direction,
500
- );
501
- ftsTextByMessageId.set(event.id, event.text);
502
- }
503
- }
504
-
505
- for (const [messageId, text] of ftsTextByMessageId) {
506
- insertFts.run(messageId, text);
507
- }
508
- })();
509
- }
510
-
511
- function getMetaNextToken(meta?: Record<string, unknown>) {
512
- const token = meta?.next_token;
513
- return typeof token === "string" && token.length > 0 ? token : undefined;
514
- }
515
-
516
- function xurlUserToBirdDmUser(user: XurlMentionUser): BirdDmUser {
517
- return {
518
- id: String(user.id),
519
- username: user.username,
520
- name: user.name,
521
- profileImageUrl: user.profile_image_url,
522
- };
523
- }
524
-
525
- function uniqueDefined(values: Array<string | undefined>) {
526
- return [
527
- ...new Set(values.filter((value): value is string => Boolean(value))),
528
- ];
529
- }
530
-
531
- function conversationIdForXurlEvent(
532
- event: XurlDmEventsResponse["data"][number],
533
- localExternalUserId: string,
534
- ) {
535
- if (event.dm_conversation_id) {
536
- return event.dm_conversation_id;
537
- }
538
- const ids = uniqueDefined([
539
- localExternalUserId,
540
- event.sender_id,
541
- ...(event.participant_ids ?? []),
542
- ]).sort();
543
- return ids.length > 1 ? ids.join("-") : `dm-${event.id}`;
544
- }
545
-
546
- function adaptXurlDmEventsToBirdPayload({
547
- payload,
548
- localExternalUserId,
549
- accountUsername,
550
- }: {
551
- payload: XurlDmEventsResponse;
552
- localExternalUserId: string;
553
- accountUsername: string;
554
- }): { conversations: BirdDmConversation[]; events: BirdDmEvent[] } {
555
- const users = new Map<string, BirdDmUser>();
556
- const addUser = (user?: BirdDmUser) => {
557
- if (!user?.id) return;
558
- users.set(user.id, { ...users.get(user.id), ...user });
559
- };
560
- for (const user of payload.includes?.users ?? []) {
561
- addUser(xurlUserToBirdDmUser(user));
562
- }
563
- addUser({
564
- id: localExternalUserId,
565
- username: accountUsername,
566
- name: accountUsername,
567
- });
568
-
569
- const eventsByConversation = new Map<string, BirdDmEvent[]>();
570
- const events: BirdDmEvent[] = [];
571
- for (const event of payload.data) {
572
- if (event.event_type && event.event_type !== "MessageCreate") continue;
573
- if (!event.id || !event.sender_id || event.text === undefined) continue;
574
- const conversationId = conversationIdForXurlEvent(
575
- event,
576
- localExternalUserId,
577
- );
578
- const participantIds = uniqueDefined([
579
- localExternalUserId,
580
- event.sender_id,
581
- ...(event.participant_ids ?? []),
582
- ]);
583
- for (const id of participantIds) {
584
- addUser({ id });
585
- }
586
- const recipientId =
587
- event.sender_id === localExternalUserId
588
- ? participantIds.find((id) => id !== localExternalUserId)
589
- : localExternalUserId;
590
- const dmEvent: BirdDmEvent = {
591
- id: event.id,
592
- conversationId,
593
- text: event.text,
594
- createdAt: event.created_at,
595
- senderId: event.sender_id,
596
- ...(recipientId ? { recipientId } : {}),
597
- sender: users.get(event.sender_id) ?? { id: event.sender_id },
598
- ...(recipientId
599
- ? { recipient: users.get(recipientId) ?? { id: recipientId } }
600
- : {}),
601
- inboxKind: "accepted",
602
- isMessageRequest: false,
603
- };
604
- events.push(dmEvent);
605
- const conversationEvents = eventsByConversation.get(conversationId) ?? [];
606
- conversationEvents.push(dmEvent);
607
- eventsByConversation.set(conversationId, conversationEvents);
608
- }
609
-
610
- const conversations = [...eventsByConversation].map(
611
- ([conversationId, conversationEvents]) => {
612
- const latest = getLatestEvent(conversationEvents);
613
- const participantIds = uniqueDefined([
614
- localExternalUserId,
615
- ...conversationEvents.flatMap((event) => [
616
- event.senderId,
617
- event.recipientId,
618
- ]),
619
- ]);
620
- const participants = participantIds.map((id) => users.get(id) ?? { id });
621
- return {
622
- id: conversationId,
623
- participants,
624
- messages: conversationEvents,
625
- lastMessageAt: latest?.createdAt,
626
- lastMessagePreview: latest?.text,
627
- inboxKind: "accepted" as const,
628
- isMessageRequest: false,
629
- };
630
- },
631
- );
632
-
633
- return { conversations, events };
634
- }
635
-
636
- function mergeXurlDmPages(pages: XurlDmEventsResponse[]): XurlDmEventsResponse {
637
- const usersById = new Map<string, XurlMentionUser>();
638
- const eventsById = new Map<string, XurlDmEventsResponse["data"][number]>();
639
- let meta: Record<string, unknown> | undefined;
640
- for (const page of pages) {
641
- for (const user of page.includes?.users ?? []) {
642
- usersById.set(user.id, { ...usersById.get(user.id), ...user });
643
- }
644
- for (const event of page.data) {
645
- eventsById.set(event.id, event);
646
- }
647
- meta = page.meta ?? meta;
648
- }
649
- return {
650
- data: [...eventsById.values()],
651
- ...(usersById.size > 0
652
- ? { includes: { users: [...usersById.values()] } }
653
- : {}),
654
- ...(meta ? { meta } : {}),
655
- };
656
- }
657
-
658
- function fetchDirectMessagesViaXurlEffect({
659
- limit,
660
- username,
661
- maxPages,
662
- allPages,
663
- pageDelayMs,
664
- }: {
665
- limit: number;
666
- username: string;
667
- maxPages?: number;
668
- allPages: boolean;
669
- pageDelayMs?: number;
670
- }) {
671
- return Effect.gen(function* () {
672
- const pageLimit = allPages
673
- ? Number.POSITIVE_INFINITY
674
- : Math.max(1, (maxPages ?? 0) + 1);
675
- const result = yield* runSyncPlanEffect({
676
- fetchPage: ({ cursor }) =>
677
- liveTransportGateway.xurl.listDirectMessages({
678
- maxResults: limit,
679
- username,
680
- ...(cursor ? { paginationToken: cursor } : {}),
681
- }),
682
- getNextCursor: (page) => getMetaNextToken(page.meta),
683
- maxPages: pageLimit,
684
- pageDelayMs,
685
- });
686
- return mergeXurlDmPages(result.pages);
687
- });
688
- }
689
-
690
- export function syncDirectMessagesViaCachedBirdEffect({
691
- account,
692
- mode,
693
- limit = 20,
694
- inbox = "all",
695
- maxPages,
696
- allPages = false,
697
- pageDelayMs,
698
- refresh = false,
699
- cacheTtlMs,
700
- }: SyncDirectMessagesViaCachedBirdOptions = {}): Effect.Effect<
701
- {
702
- ok: true;
703
- source: "bird" | "cache" | "xurl";
704
- accountId: string;
705
- conversations: number;
706
- messages: number;
707
- },
708
- unknown
709
- > {
710
- return Effect.gen(function* () {
711
- const parsedMode = parseSyncMode(mode);
712
- if (parsedMode === "xurl") {
713
- assertXurlLimit(limit);
714
- } else {
715
- assertBirdLimit(limit);
716
- }
717
- if (inbox === "requests" && parsedMode === "xurl") {
718
- throw new Error(
719
- "xurl DM mode cannot read the message-request inbox or accept/reject state; use --mode bird",
720
- );
721
- }
722
- const db = getNativeDb();
723
- const resolvedAccount = resolveLiveSyncAccount(db, account);
724
- const pageKey = allPages
725
- ? "all-pages"
726
- : `max-pages:${String(maxPages ?? 0)}`;
727
- const cacheMode = parsedMode === "auto" ? "auto" : parsedMode;
728
- const cacheKey = `dms:${cacheMode}:${resolvedAccount.accountId}:${String(limit)}:${inbox}:${pageKey}`;
729
- const ttlMs = parseCacheTtlMs(cacheTtlMs);
730
- const cached = readSyncCache<{
731
- conversations: BirdDmConversation[];
732
- events: BirdDmEvent[];
733
- }>(cacheKey, db);
734
- const cacheAgeMs = cached
735
- ? Date.now() - new Date(cached.updatedAt).getTime()
736
- : Number.POSITIVE_INFINITY;
737
-
738
- const cacheHit = !refresh && cached && cacheAgeMs <= ttlMs;
739
- let accountExternalUserId = resolvedAccount.externalUserId;
740
- let payload:
741
- | {
742
- conversations: BirdDmConversation[];
743
- events: BirdDmEvent[];
744
- }
745
- | undefined;
746
- let source: "bird" | "xurl" | undefined;
747
- if (cacheHit) {
748
- payload = cached.value;
749
- } else {
750
- const tryXurl =
751
- (parsedMode === "xurl" || parsedMode === "auto") &&
752
- inbox !== "requests";
753
- if (tryXurl) {
754
- const xurlPayload = yield* Effect.gen(function* () {
755
- const authenticated = getAuthenticatedXurlAccount(
756
- yield* liveTransportGateway.xurl.lookupAuthenticatedOAuth2User(
757
- resolvedAccount.username,
758
- ),
759
- );
760
- if (!authenticated.username && !authenticated.id) {
761
- return yield* Effect.fail(
762
- new Error("xurl authenticated user unavailable"),
763
- );
764
- }
765
- assertAuthenticatedBirdAccountMatches({
766
- source: "xurl",
767
- account: resolvedAccount,
768
- liveUsername: authenticated.username ?? resolvedAccount.username,
769
- liveExternalUserId: authenticated.id,
770
- });
771
- accountExternalUserId ??= authenticated.id;
772
- if (!resolvedAccount.externalUserId && accountExternalUserId) {
773
- persistAccountExternalUserId(
774
- db,
775
- resolvedAccount.accountId,
776
- accountExternalUserId,
777
- );
778
- }
779
- if (!accountExternalUserId) {
780
- return yield* Effect.fail(
781
- new Error(
782
- "xurl authenticated user id unavailable; refusing to sync DMs",
783
- ),
784
- );
785
- }
786
- return yield* fetchDirectMessagesViaXurlEffect({
787
- limit,
788
- username: resolvedAccount.username,
789
- ...(typeof maxPages === "number" ? { maxPages } : {}),
790
- allPages,
791
- ...(typeof pageDelayMs === "number" ? { pageDelayMs } : {}),
792
- });
793
- }).pipe(
794
- Effect.catchAll((error) => {
795
- if (parsedMode === "xurl") return Effect.fail(error);
796
- return Effect.succeed(undefined);
797
- }),
798
- );
799
- if (xurlPayload) {
800
- const localExternalUserId = accountExternalUserId;
801
- if (!localExternalUserId) {
802
- throw new Error(
803
- "xurl authenticated user id unavailable; refusing to sync DMs",
804
- );
805
- }
806
- payload = adaptXurlDmEventsToBirdPayload({
807
- payload: xurlPayload,
808
- localExternalUserId,
809
- accountUsername: resolvedAccount.username,
810
- });
811
- source = "xurl";
812
- }
813
- }
814
- if (!payload) {
815
- const authenticated =
816
- yield* liveTransportGateway.bird.getAuthenticatedAccount();
817
- assertAuthenticatedBirdAccountMatches({
818
- source: "bird",
819
- account: resolvedAccount,
820
- liveUsername: authenticated.username,
821
- liveExternalUserId: authenticated.id,
822
- });
823
- accountExternalUserId ??= authenticated.id;
824
- if (!resolvedAccount.externalUserId && accountExternalUserId) {
825
- persistAccountExternalUserId(
826
- db,
827
- resolvedAccount.accountId,
828
- accountExternalUserId,
829
- );
830
- }
831
- payload = yield* liveTransportGateway.bird.listDirectMessages({
832
- maxResults: limit,
833
- ...(inbox !== "all" ? { inbox } : {}),
834
- ...(typeof maxPages === "number" ? { maxPages } : {}),
835
- ...(allPages ? { allPages } : {}),
836
- ...(typeof pageDelayMs === "number" ? { pageDelayMs } : {}),
837
- });
838
- source = "bird";
839
- }
840
- }
841
- if (!payload) {
842
- throw new Error("DM sync produced no payload");
843
- }
844
-
845
- mergeDirectMessagesIntoLocalStore(
846
- db,
847
- resolvedAccount.accountId,
848
- resolvedAccount.username,
849
- accountExternalUserId,
850
- payload,
851
- );
852
- if (!cached || refresh || cacheAgeMs > ttlMs) {
853
- writeSyncCache(cacheKey, payload, db);
854
- }
855
-
856
- return {
857
- ok: true,
858
- source: cacheHit ? "cache" : source!,
859
- accountId: resolvedAccount.accountId,
860
- conversations: payload.conversations.length,
861
- messages: payload.events.length,
862
- } as const;
863
- });
864
- }
865
-
866
- export function syncDirectMessagesViaCachedBird(
867
- options: SyncDirectMessagesViaCachedBirdOptions = {},
868
- ) {
869
- return runEffectPromise(syncDirectMessagesViaCachedBirdEffect(options));
870
- }