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