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,652 +0,0 @@
1
- import { mkdirSync, writeFileSync } from "node:fs";
2
- import path from "node:path";
3
- import { Effect } from "effect";
4
- import { getNativeDb } from "./db";
5
- import { runEffectPromise } from "./effect-runtime";
6
- import { listTimelineItems } from "./queries";
7
- import { lookupTweetsByIdsEffect } from "./tweet-lookup";
8
- import { renderTweetMarkdown, renderTweetPlainText } from "./tweet-render";
9
- import type { TweetEntities, XurlMentionUser } from "./types";
10
-
11
- type ResearchNodeSource = "local" | "live";
12
-
13
- export interface ResearchNode {
14
- id: string;
15
- url: string;
16
- authorHandle: string;
17
- authorName: string;
18
- createdAt: string;
19
- text: string;
20
- plainText: string;
21
- markdown: string;
22
- likeCount: number;
23
- bookmarked: boolean;
24
- liked: boolean;
25
- replyToTweetId?: string | null;
26
- quotedTweetId?: string | null;
27
- threadDepth: number;
28
- source: ResearchNodeSource;
29
- }
30
-
31
- export interface ResearchThread {
32
- seedTweetId: string;
33
- seedUrl: string;
34
- seedText: string;
35
- threadRootId: string;
36
- thread: ResearchNode[];
37
- links: string[];
38
- handles: string[];
39
- }
40
-
41
- export interface ResearchReport {
42
- query?: string;
43
- account?: string;
44
- generatedAt: string;
45
- seedCount: number;
46
- threadCount: number;
47
- items: ResearchThread[];
48
- markdown: string;
49
- }
50
-
51
- export interface ResearchOptions {
52
- account?: string;
53
- query?: string;
54
- limit?: number;
55
- maxThreadDepth?: number;
56
- outPath?: string;
57
- }
58
-
59
- interface ResearchRow {
60
- id: string;
61
- account_id: string;
62
- account_handle: string;
63
- kind: string;
64
- text: string;
65
- created_at: string;
66
- is_replied: number;
67
- like_count: number;
68
- bookmarked: number;
69
- liked: number;
70
- reply_to_id: string | null;
71
- quoted_tweet_id: string | null;
72
- entities_json: string;
73
- author_handle: string;
74
- author_name: string;
75
- author_bio: string;
76
- author_followers_count: number;
77
- author_avatar_hue: number;
78
- author_avatar_url: string | null;
79
- author_created_at: string;
80
- thread_depth?: number;
81
- }
82
-
83
- function trySync<T>(try_: () => T) {
84
- return Effect.try({
85
- try: try_,
86
- catch: (cause) => cause,
87
- });
88
- }
89
-
90
- function parseJsonField<T>(value: unknown, fallback: T): T {
91
- if (typeof value !== "string" || value.length === 0) {
92
- return fallback;
93
- }
94
-
95
- try {
96
- return JSON.parse(value) as T;
97
- } catch {
98
- return fallback;
99
- }
100
- }
101
-
102
- function normalizeTweetEntities(raw: unknown): TweetEntities {
103
- if (!raw || typeof raw !== "object") {
104
- return {};
105
- }
106
-
107
- const entities = raw as Record<string, unknown>;
108
- const mentions = Array.isArray(entities.mentions) ? entities.mentions : [];
109
- const urls = Array.isArray(entities.urls) ? entities.urls : [];
110
- const hashtags = Array.isArray(entities.hashtags) ? entities.hashtags : [];
111
-
112
- return {
113
- ...(mentions.length
114
- ? {
115
- mentions: mentions.map((mention) => {
116
- const value =
117
- mention && typeof mention === "object"
118
- ? (mention as Record<string, unknown>)
119
- : {};
120
- return {
121
- username: String(value.username ?? ""),
122
- id: typeof value.id === "string" ? String(value.id) : undefined,
123
- start: Number(value.start ?? 0),
124
- end: Number(value.end ?? 0),
125
- };
126
- }),
127
- }
128
- : {}),
129
- ...(urls.length
130
- ? {
131
- urls: urls.map((url) => {
132
- const value =
133
- url && typeof url === "object"
134
- ? (url as Record<string, unknown>)
135
- : {};
136
- return {
137
- url: String(value.url ?? ""),
138
- expandedUrl: String(
139
- value.expanded_url ?? value.expandedUrl ?? value.url ?? "",
140
- ),
141
- displayUrl: String(
142
- value.display_url ?? value.displayUrl ?? value.url ?? "",
143
- ),
144
- start: Number(value.start ?? 0),
145
- end: Number(value.end ?? 0),
146
- };
147
- }),
148
- }
149
- : {}),
150
- ...(hashtags.length
151
- ? {
152
- hashtags: hashtags.map((hashtag) => {
153
- const value =
154
- hashtag && typeof hashtag === "object"
155
- ? (hashtag as Record<string, unknown>)
156
- : {};
157
- return {
158
- tag: String(value.tag ?? ""),
159
- start: Number(value.start ?? 0),
160
- end: Number(value.end ?? 0),
161
- };
162
- }),
163
- }
164
- : {}),
165
- };
166
- }
167
-
168
- function toProfile(record: ResearchRow) {
169
- return {
170
- id: record.author_handle,
171
- handle: record.author_handle,
172
- displayName: record.author_name,
173
- bio: record.author_bio,
174
- followersCount: Number(record.author_followers_count ?? 0),
175
- avatarHue: Number(record.author_avatar_hue ?? 0),
176
- avatarUrl:
177
- typeof record.author_avatar_url === "string"
178
- ? record.author_avatar_url
179
- : undefined,
180
- createdAt: record.author_created_at,
181
- };
182
- }
183
-
184
- function toResearchNode(
185
- record: ResearchRow,
186
- source: ResearchNodeSource,
187
- ): ResearchNode {
188
- const author = toProfile(record);
189
- const entities = normalizeTweetEntities(
190
- parseJsonField(record.entities_json, {}),
191
- );
192
- return {
193
- id: record.id,
194
- url: `https://x.com/${author.handle}/status/${record.id}`,
195
- authorHandle: author.handle,
196
- authorName: author.displayName,
197
- createdAt: record.created_at,
198
- text: record.text,
199
- plainText: renderTweetPlainText(record.text, entities),
200
- markdown: renderTweetMarkdown(record.text, entities),
201
- likeCount: Number(record.like_count ?? 0),
202
- bookmarked: Boolean(record.bookmarked),
203
- liked: Boolean(record.liked),
204
- replyToTweetId: record.reply_to_id,
205
- quotedTweetId: record.quoted_tweet_id,
206
- threadDepth: Number(record.thread_depth ?? 0),
207
- source,
208
- };
209
- }
210
-
211
- function getTweetRowById(tweetId: string): ResearchRow | null {
212
- const db = getNativeDb();
213
- const row = db
214
- .prepare(
215
- `
216
- select
217
- t.id,
218
- t.account_id,
219
- a.handle as account_handle,
220
- t.kind,
221
- t.text,
222
- t.created_at,
223
- t.is_replied,
224
- t.like_count,
225
- t.bookmarked,
226
- t.liked,
227
- t.reply_to_id,
228
- t.quoted_tweet_id,
229
- t.entities_json,
230
- p.id as author_profile_id,
231
- p.handle as author_handle,
232
- p.display_name as author_name,
233
- p.bio as author_bio,
234
- p.followers_count as author_followers_count,
235
- p.avatar_hue as author_avatar_hue,
236
- p.avatar_url as author_avatar_url,
237
- p.created_at as author_created_at
238
- from tweets t
239
- join accounts a on a.id = t.account_id
240
- join profiles p on p.id = t.author_profile_id
241
- where t.id = ?
242
- `,
243
- )
244
- .get(tweetId) as ResearchRow | undefined;
245
-
246
- return row ?? null;
247
- }
248
-
249
- function getTweetDescendants(
250
- rootTweetId: string,
251
- maxThreadDepth: number,
252
- ): ResearchNode[] {
253
- const db = getNativeDb();
254
- const rows = db
255
- .prepare(
256
- `
257
- with recursive thread(id, depth, path) as (
258
- select id, 0 as depth, ',' || id || ',' as path
259
- from tweets
260
- where id = ?
261
- union all
262
- select child.id, thread.depth + 1, thread.path || child.id || ','
263
- from tweets child
264
- join thread on child.reply_to_id = thread.id
265
- where thread.depth < ?
266
- and instr(thread.path, ',' || child.id || ',') = 0
267
- )
268
- select
269
- t.id,
270
- t.account_id,
271
- a.handle as account_handle,
272
- t.kind,
273
- t.text,
274
- t.created_at,
275
- t.is_replied,
276
- t.like_count,
277
- t.bookmarked,
278
- t.liked,
279
- t.reply_to_id,
280
- t.quoted_tweet_id,
281
- t.entities_json,
282
- p.id as author_profile_id,
283
- p.handle as author_handle,
284
- p.display_name as author_name,
285
- p.bio as author_bio,
286
- p.followers_count as author_followers_count,
287
- p.avatar_hue as author_avatar_hue,
288
- p.avatar_url as author_avatar_url,
289
- p.created_at as author_created_at,
290
- thread.depth as thread_depth
291
- from thread
292
- join tweets t on t.id = thread.id
293
- join accounts a on a.id = t.account_id
294
- join profiles p on p.id = t.author_profile_id
295
- order by t.created_at asc, t.id asc
296
- `,
297
- )
298
- .all(rootTweetId, maxThreadDepth) as ResearchRow[];
299
-
300
- return rows.map((row) => toResearchNode(row, "local"));
301
- }
302
-
303
- function lookupTweetNodeEffect(
304
- tweetId: string,
305
- ): Effect.Effect<ResearchNode | null, unknown> {
306
- return Effect.gen(function* () {
307
- const payload = yield* lookupTweetsByIdsEffect([tweetId]);
308
- const tweet = payload.data[0];
309
- if (!tweet) {
310
- return null;
311
- }
312
- const entities = normalizeTweetEntities(tweet.entities);
313
-
314
- const usersById = new Map(
315
- (payload.includes?.users ?? []).map((user: XurlMentionUser) => [
316
- user.id,
317
- user,
318
- ]),
319
- );
320
- const author = usersById.get(tweet.author_id) ?? {
321
- id: tweet.author_id,
322
- username: `user_${tweet.author_id}`,
323
- name: `user_${tweet.author_id}`,
324
- };
325
-
326
- return {
327
- id: tweet.id,
328
- url: `https://x.com/${author.username}/status/${tweet.id}`,
329
- authorHandle: author.username,
330
- authorName: author.name,
331
- createdAt: tweet.created_at,
332
- text: tweet.text,
333
- plainText: renderTweetPlainText(tweet.text, entities),
334
- markdown: renderTweetMarkdown(tweet.text, entities),
335
- likeCount: Number(tweet.public_metrics?.like_count ?? 0),
336
- bookmarked: false,
337
- liked: false,
338
- replyToTweetId:
339
- tweet.referenced_tweets?.find((item) => item.type === "replied_to")
340
- ?.id ?? null,
341
- quotedTweetId:
342
- tweet.referenced_tweets?.find((item) => item.type === "quoted")?.id ??
343
- null,
344
- threadDepth: 0,
345
- source: "live",
346
- };
347
- });
348
- }
349
-
350
- function collectAncestorChainEffect(
351
- tweetId: string,
352
- maxThreadDepth: number,
353
- ): Effect.Effect<ResearchNode[], unknown> {
354
- return Effect.gen(function* () {
355
- const chain: ResearchNode[] = [];
356
- let currentId: string | undefined = tweetId;
357
- const visited = new Set<string>();
358
- let depth = 0;
359
-
360
- while (currentId && !visited.has(currentId) && depth < maxThreadDepth) {
361
- const lookupId: string = currentId;
362
- visited.add(lookupId);
363
- const localRow: ResearchRow | null = yield* trySync(() =>
364
- getTweetRowById(lookupId),
365
- );
366
- if (localRow) {
367
- chain.push(yield* trySync(() => toResearchNode(localRow, "local")));
368
- currentId = localRow.reply_to_id ?? undefined;
369
- depth += 1;
370
- continue;
371
- }
372
-
373
- const remoteNode = yield* lookupTweetNodeEffect(lookupId);
374
- if (!remoteNode) {
375
- break;
376
- }
377
- chain.push(remoteNode);
378
- currentId = remoteNode.replyToTweetId ?? undefined;
379
- depth += 1;
380
- }
381
-
382
- return chain
383
- .reverse()
384
- .map((node, index) => ({ ...node, threadDepth: index }));
385
- });
386
- }
387
-
388
- function dedupeNodes(nodes: ResearchNode[]) {
389
- const seen = new Set<string>();
390
- const output: ResearchNode[] = [];
391
- for (const node of nodes) {
392
- if (seen.has(node.id)) {
393
- const existingIndex = output.findIndex((item) => item.id === node.id);
394
- if (
395
- existingIndex >= 0 &&
396
- output[existingIndex]?.source === "live" &&
397
- node.source === "local"
398
- ) {
399
- output[existingIndex] = node;
400
- }
401
- continue;
402
- }
403
- seen.add(node.id);
404
- output.push(node);
405
- }
406
- return output;
407
- }
408
-
409
- function compareThreadNodes(a: ResearchNode, b: ResearchNode) {
410
- const byCreatedAt = a.createdAt.localeCompare(b.createdAt);
411
- return byCreatedAt === 0 ? a.id.localeCompare(b.id) : byCreatedAt;
412
- }
413
-
414
- function orderThreadNodes(rootId: string, nodes: ResearchNode[]) {
415
- const byId = new Map(nodes.map((node) => [node.id, node]));
416
- const childrenByParentId = new Map<string, ResearchNode[]>();
417
-
418
- for (const node of nodes) {
419
- const parentId = node.replyToTweetId;
420
- if (!parentId || !byId.has(parentId) || node.id === rootId) {
421
- continue;
422
- }
423
- const siblings = childrenByParentId.get(parentId) ?? [];
424
- siblings.push(node);
425
- childrenByParentId.set(parentId, siblings);
426
- }
427
-
428
- for (const siblings of childrenByParentId.values()) {
429
- siblings.sort(compareThreadNodes);
430
- }
431
-
432
- const ordered: ResearchNode[] = [];
433
- const visited = new Set<string>();
434
- const visit = (node: ResearchNode, depth: number) => {
435
- if (visited.has(node.id)) {
436
- return;
437
- }
438
- visited.add(node.id);
439
- ordered.push({ ...node, threadDepth: depth });
440
- for (const child of childrenByParentId.get(node.id) ?? []) {
441
- visit(child, depth + 1);
442
- }
443
- };
444
-
445
- const root = byId.get(rootId);
446
- if (root) {
447
- visit(root, 0);
448
- }
449
-
450
- for (const node of [...nodes].sort(compareThreadNodes)) {
451
- if (!visited.has(node.id)) {
452
- visit(node, Math.max(0, node.threadDepth));
453
- }
454
- }
455
-
456
- return ordered;
457
- }
458
-
459
- function collectExternalLinks(nodes: ResearchNode[]) {
460
- const links = new Set<string>();
461
- for (const node of nodes) {
462
- for (const match of node.plainText.matchAll(/https?:\/\/[^\s<>\])"]+/g)) {
463
- links.add(match[0]);
464
- }
465
- }
466
- return Array.from(links);
467
- }
468
-
469
- function collectHandles(nodes: ResearchNode[]) {
470
- const handles = new Set<string>();
471
- for (const node of nodes) {
472
- handles.add(`@${node.authorHandle}`);
473
- for (const match of node.plainText.matchAll(/@([A-Za-z0-9_]+)/g)) {
474
- handles.add(`@${match[1]}`);
475
- }
476
- }
477
- return Array.from(handles);
478
- }
479
-
480
- function renderThreadMarkdown(thread: ResearchNode[]) {
481
- return thread
482
- .map((node) => {
483
- const indent = " ".repeat(node.threadDepth);
484
- const source = node.source === "live" ? "live" : "local";
485
- return `${indent}- [@${node.authorHandle}](${node.url}) [${source}] ${node.markdown}`;
486
- })
487
- .join("\n");
488
- }
489
-
490
- function renderReportMarkdown(report: Omit<ResearchReport, "markdown">) {
491
- const lines = [
492
- "# Birdclaw Research",
493
- "",
494
- `- Generated: ${report.generatedAt}`,
495
- `- Query: ${report.query ? `\`${report.query}\`` : "(all bookmarks)"}`,
496
- `- Account: ${report.account ?? "all"}`,
497
- `- Seed bookmarks: ${report.seedCount}`,
498
- `- Threads expanded: ${report.threadCount}`,
499
- "",
500
- ];
501
-
502
- report.items.forEach((item, index) => {
503
- lines.push(
504
- `## ${index + 1}. Seed tweet`,
505
- "",
506
- `- URL: ${item.seedUrl}`,
507
- `- Seed tweet: \`${item.seedTweetId}\``,
508
- `- Thread root: \`${item.threadRootId}\``,
509
- `- Seed text: ${item.seedText}`,
510
- "",
511
- "### Thread",
512
- "",
513
- renderThreadMarkdown(item.thread),
514
- "",
515
- );
516
-
517
- if (item.links.length > 0) {
518
- lines.push("### Links", "");
519
- for (const link of item.links) {
520
- lines.push(`- ${link}`);
521
- }
522
- lines.push("");
523
- }
524
-
525
- if (item.handles.length > 0) {
526
- lines.push("### Handles", "");
527
- lines.push(`- ${item.handles.join(", ")}`, "");
528
- }
529
- });
530
-
531
- return lines.join("\n").trimEnd() + "\n";
532
- }
533
-
534
- function resolveSeedTimelineItems({
535
- account,
536
- query,
537
- limit,
538
- }: {
539
- account?: string;
540
- query?: string;
541
- limit: number;
542
- }) {
543
- return listTimelineItems({
544
- resource: "home",
545
- account,
546
- search: query,
547
- bookmarkedOnly: true,
548
- includeReplies: true,
549
- qualityFilter: "all",
550
- limit,
551
- });
552
- }
553
-
554
- export function runResearchModeEffect(
555
- options: ResearchOptions = {},
556
- ): Effect.Effect<ResearchReport, unknown> {
557
- return Effect.gen(function* () {
558
- const limit = yield* trySync(() =>
559
- Number.isFinite(options.limit ?? 20)
560
- ? Math.max(1, Math.floor(options.limit ?? 20))
561
- : 20,
562
- );
563
- const maxThreadDepth = yield* trySync(() =>
564
- Number.isFinite(options.maxThreadDepth ?? 10)
565
- ? Math.max(1, Math.floor(options.maxThreadDepth ?? 10))
566
- : 10,
567
- );
568
- const seeds = yield* trySync(() =>
569
- resolveSeedTimelineItems({
570
- account: options.account,
571
- query: options.query,
572
- limit,
573
- }),
574
- );
575
-
576
- const items: ResearchThread[] = [];
577
- for (const seed of seeds) {
578
- const ancestorChain = yield* collectAncestorChainEffect(
579
- seed.id,
580
- maxThreadDepth,
581
- );
582
- const rootId = ancestorChain[0]?.id ?? seed.id;
583
- const localThread = yield* trySync(() =>
584
- getTweetDescendants(rootId, maxThreadDepth),
585
- );
586
- const thread = yield* trySync(() =>
587
- orderThreadNodes(
588
- rootId,
589
- dedupeNodes([...ancestorChain, ...localThread]),
590
- ),
591
- );
592
- const links = yield* trySync(() => collectExternalLinks(thread));
593
- const handles = yield* trySync(() => collectHandles(thread));
594
- items.push({
595
- seedTweetId: seed.id,
596
- seedUrl: `https://x.com/${seed.author.handle}/status/${seed.id}`,
597
- seedText: yield* trySync(() =>
598
- renderTweetPlainText(seed.text, seed.entities),
599
- ),
600
- threadRootId: rootId,
601
- thread,
602
- links,
603
- handles,
604
- });
605
- }
606
-
607
- const reportBase = {
608
- query: options.query,
609
- account: options.account,
610
- generatedAt: new Date().toISOString(),
611
- seedCount: seeds.length,
612
- threadCount: items.length,
613
- items,
614
- };
615
- const markdown = yield* trySync(() => renderReportMarkdown(reportBase));
616
- const report: ResearchReport = {
617
- ...reportBase,
618
- markdown,
619
- };
620
-
621
- if (options.outPath) {
622
- yield* trySync(() => {
623
- const resolved = path.resolve(options.outPath ?? "");
624
- mkdirSync(path.dirname(resolved), { recursive: true });
625
- writeFileSync(resolved, markdown, "utf8");
626
- });
627
- }
628
-
629
- return report;
630
- });
631
- }
632
-
633
- export function runResearchMode(
634
- options: ResearchOptions = {},
635
- ): Promise<ResearchReport> {
636
- return runEffectPromise(runResearchModeEffect(options));
637
- }
638
-
639
- export const __test__ = {
640
- parseJsonField,
641
- normalizeTweetEntities,
642
- toResearchNode,
643
- dedupeNodes,
644
- orderThreadNodes,
645
- collectExternalLinks,
646
- collectHandles,
647
- renderReportMarkdown,
648
- lookupTweetNodeEffect,
649
- collectAncestorChainEffect,
650
- };
651
-
652
- export type { ResearchRow };