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
package/src/lib/db.ts DELETED
@@ -1,1135 +0,0 @@
1
- import NativeSqliteDatabase, {
2
- type Database,
3
- SQLITE_BUSY_TIMEOUT_MS,
4
- } from "./sqlite";
5
- import { Kysely, SqliteDialect } from "kysely";
6
- import { ensureBirdclawDirs, getBirdclawPaths } from "./config";
7
- import { seedDemoData } from "./seed";
8
-
9
- export interface AccountsTable {
10
- id: string;
11
- name: string;
12
- handle: string;
13
- external_user_id: string | null;
14
- transport: string;
15
- is_default: number;
16
- created_at: string;
17
- }
18
-
19
- export interface ProfilesTable {
20
- id: string;
21
- handle: string;
22
- display_name: string;
23
- bio: string;
24
- followers_count: number;
25
- following_count: number;
26
- public_metrics_json: string;
27
- avatar_hue: number;
28
- avatar_url: string | null;
29
- location: string | null;
30
- url: string | null;
31
- verified_type: string | null;
32
- entities_json: string;
33
- raw_json: string;
34
- created_at: string;
35
- }
36
-
37
- export interface ProfileAffiliationsTable {
38
- subject_profile_id: string;
39
- organization_profile_id: string;
40
- organization_name: string | null;
41
- organization_handle: string | null;
42
- badge_url: string | null;
43
- url: string | null;
44
- label: string | null;
45
- source: string;
46
- is_active: number;
47
- first_seen_at: string;
48
- last_seen_at: string;
49
- raw_json: string;
50
- updated_at: string;
51
- }
52
-
53
- export interface ProfileSnapshotsTable {
54
- profile_id: string;
55
- snapshot_hash: string;
56
- observed_at: string;
57
- last_seen_at: string;
58
- source: string;
59
- handle: string;
60
- display_name: string;
61
- bio: string;
62
- location: string | null;
63
- url: string | null;
64
- verified_type: string | null;
65
- followers_count: number;
66
- following_count: number;
67
- affiliations_json: string;
68
- raw_json: string;
69
- }
70
-
71
- export interface ProfileBioEntitiesTable {
72
- profile_id: string;
73
- kind: string;
74
- value: string;
75
- source: string;
76
- is_active: number;
77
- first_seen_at: string;
78
- last_seen_at: string;
79
- raw_json: string;
80
- }
81
-
82
- export interface IdentitySearchIndexTable {
83
- profile_id: string;
84
- kind: string;
85
- value: string;
86
- normalized_value: string;
87
- source: string;
88
- weight: number;
89
- updated_at: string;
90
- }
91
-
92
- export interface TweetsTable {
93
- id: string;
94
- account_id: string;
95
- author_profile_id: string;
96
- kind: string;
97
- text: string;
98
- created_at: string;
99
- is_replied: number;
100
- reply_to_id: string | null;
101
- like_count: number;
102
- media_count: number;
103
- bookmarked: number;
104
- liked: number;
105
- entities_json: string;
106
- media_json: string;
107
- quoted_tweet_id: string | null;
108
- }
109
-
110
- export interface TweetCollectionsTable {
111
- account_id: string;
112
- tweet_id: string;
113
- kind: "likes" | "bookmarks";
114
- collected_at: string | null;
115
- source: string;
116
- raw_json: string;
117
- updated_at: string;
118
- }
119
-
120
- export interface TweetAccountEdgesTable {
121
- account_id: string;
122
- tweet_id: string;
123
- kind:
124
- | "home"
125
- | "mention"
126
- | "authored"
127
- | "search"
128
- | "profile"
129
- | "thread_context";
130
- first_seen_at: string;
131
- last_seen_at: string;
132
- seen_count: number;
133
- source: string;
134
- raw_json: string;
135
- updated_at: string;
136
- }
137
-
138
- export interface DmConversationsTable {
139
- id: string;
140
- account_id: string;
141
- participant_profile_id: string;
142
- title: string;
143
- inbox_kind: "accepted" | "request";
144
- last_message_at: string;
145
- unread_count: number;
146
- needs_reply: number;
147
- }
148
-
149
- export interface DmMessagesTable {
150
- id: string;
151
- conversation_id: string;
152
- sender_profile_id: string;
153
- text: string;
154
- created_at: string;
155
- direction: "inbound" | "outbound";
156
- is_replied: number;
157
- media_count: number;
158
- }
159
-
160
- export interface TweetActionsTable {
161
- id: string;
162
- account_id: string;
163
- tweet_id: string | null;
164
- kind: string;
165
- body: string;
166
- created_at: string;
167
- }
168
-
169
- export interface BlocksTable {
170
- account_id: string;
171
- profile_id: string;
172
- source: string;
173
- created_at: string;
174
- }
175
-
176
- export interface MutesTable {
177
- account_id: string;
178
- profile_id: string;
179
- source: string;
180
- created_at: string;
181
- }
182
-
183
- export interface AiScoresTable {
184
- entity_kind: string;
185
- entity_id: string;
186
- model: string;
187
- score: number;
188
- summary: string;
189
- reasoning: string;
190
- updated_at: string;
191
- }
192
-
193
- export interface SyncCacheTable {
194
- cache_key: string;
195
- value_json: string;
196
- updated_at: string;
197
- }
198
-
199
- export interface UrlExpansionsTable {
200
- short_url: string;
201
- expanded_url: string;
202
- final_url: string;
203
- status: string;
204
- expanded_tweet_id: string | null;
205
- expanded_handle: string | null;
206
- title: string | null;
207
- description: string | null;
208
- image_url: string | null;
209
- site_name: string | null;
210
- error: string | null;
211
- source: string;
212
- updated_at: string;
213
- }
214
-
215
- export interface LinkOccurrencesTable {
216
- source_kind: "dm" | "tweet";
217
- source_id: string;
218
- source_position: number;
219
- short_url: string;
220
- account_id: string | null;
221
- conversation_id: string | null;
222
- direction: string | null;
223
- created_at: string;
224
- }
225
-
226
- export interface FollowEdgesTable {
227
- account_id: string;
228
- direction: "followers" | "following";
229
- profile_id: string;
230
- external_user_id: string;
231
- source: string;
232
- current: number;
233
- first_seen_at: string;
234
- last_seen_at: string;
235
- ended_at: string | null;
236
- updated_at: string;
237
- }
238
-
239
- export interface FollowSnapshotsTable {
240
- id: string;
241
- account_id: string;
242
- direction: "followers" | "following";
243
- source: string;
244
- status: "complete" | "incomplete";
245
- page_count: number;
246
- result_count: number;
247
- started_at: string;
248
- completed_at: string;
249
- raw_meta_json: string;
250
- }
251
-
252
- export interface FollowSnapshotMembersTable {
253
- snapshot_id: string;
254
- profile_id: string;
255
- external_user_id: string;
256
- position: number;
257
- }
258
-
259
- export interface FollowEventsTable {
260
- id: string;
261
- account_id: string;
262
- direction: "followers" | "following";
263
- profile_id: string;
264
- external_user_id: string;
265
- kind: "started" | "ended";
266
- event_at: string;
267
- snapshot_id: string;
268
- }
269
-
270
- export interface GeocodedLocationsTable {
271
- normalized_key: string;
272
- original: string;
273
- lat: number;
274
- lng: number;
275
- formatted: string | null;
276
- country_code: string | null;
277
- confidence: number | null;
278
- provider: string;
279
- approx_radius_m: number | null;
280
- bounds_json: string;
281
- components_json: string;
282
- hits: number;
283
- created_at: string;
284
- last_used_at: string;
285
- }
286
-
287
- export interface GeocodedLocationsUnresolvedTable {
288
- normalized_key: string;
289
- original: string;
290
- reason: string;
291
- last_attempted_at: string;
292
- ttl_until: string | null;
293
- }
294
-
295
- export interface BirdclawDatabase {
296
- accounts: AccountsTable;
297
- profiles: ProfilesTable;
298
- profile_affiliations: ProfileAffiliationsTable;
299
- profile_snapshots: ProfileSnapshotsTable;
300
- profile_bio_entities: ProfileBioEntitiesTable;
301
- identity_search_index: IdentitySearchIndexTable;
302
- tweets: TweetsTable;
303
- tweet_collections: TweetCollectionsTable;
304
- tweet_account_edges: TweetAccountEdgesTable;
305
- dm_conversations: DmConversationsTable;
306
- dm_messages: DmMessagesTable;
307
- tweet_actions: TweetActionsTable;
308
- blocks: BlocksTable;
309
- mutes: MutesTable;
310
- ai_scores: AiScoresTable;
311
- sync_cache: SyncCacheTable;
312
- url_expansions: UrlExpansionsTable;
313
- link_occurrences: LinkOccurrencesTable;
314
- follow_edges: FollowEdgesTable;
315
- follow_snapshots: FollowSnapshotsTable;
316
- follow_snapshot_members: FollowSnapshotMembersTable;
317
- follow_events: FollowEventsTable;
318
- geocoded_locations: GeocodedLocationsTable;
319
- geocoded_locations_unresolved: GeocodedLocationsUnresolvedTable;
320
- }
321
-
322
- let nativeDb: Database | undefined;
323
- let kyselyDb: Kysely<BirdclawDatabase> | undefined;
324
- let demoSeedAttempted = false;
325
-
326
- export interface InitDatabaseOptions {
327
- seedDemoData?: boolean;
328
- }
329
-
330
- const BASE_SCHEMA_SQL = `
331
- pragma journal_mode = wal;
332
- pragma busy_timeout = ${SQLITE_BUSY_TIMEOUT_MS};
333
- pragma foreign_keys = on;
334
-
335
- create table if not exists accounts (
336
- id text primary key,
337
- name text not null,
338
- handle text not null unique,
339
- external_user_id text,
340
- transport text not null,
341
- is_default integer not null default 0,
342
- created_at text not null
343
- );
344
-
345
- create table if not exists profiles (
346
- id text primary key,
347
- handle text not null unique,
348
- display_name text not null,
349
- bio text not null,
350
- followers_count integer not null default 0,
351
- following_count integer not null default 0,
352
- public_metrics_json text not null default '{}',
353
- avatar_hue integer not null default 0,
354
- avatar_url text,
355
- location text,
356
- url text,
357
- verified_type text,
358
- entities_json text not null default '{}',
359
- raw_json text not null default '{}',
360
- created_at text not null
361
- );
362
-
363
- create table if not exists profile_affiliations (
364
- subject_profile_id text not null,
365
- organization_profile_id text not null,
366
- organization_name text,
367
- organization_handle text,
368
- badge_url text,
369
- url text,
370
- label text,
371
- source text not null,
372
- is_active integer not null default 1,
373
- first_seen_at text not null,
374
- last_seen_at text not null,
375
- raw_json text not null default '{}',
376
- updated_at text not null,
377
- primary key (subject_profile_id, organization_profile_id)
378
- );
379
-
380
- create table if not exists profile_snapshots (
381
- profile_id text not null,
382
- snapshot_hash text not null,
383
- observed_at text not null,
384
- last_seen_at text not null,
385
- source text not null,
386
- handle text not null,
387
- display_name text not null,
388
- bio text not null,
389
- location text,
390
- url text,
391
- verified_type text,
392
- followers_count integer not null default 0,
393
- following_count integer not null default 0,
394
- affiliations_json text not null default '[]',
395
- raw_json text not null default '{}',
396
- primary key (profile_id, snapshot_hash)
397
- );
398
-
399
- create table if not exists profile_bio_entities (
400
- profile_id text not null,
401
- kind text not null,
402
- value text not null,
403
- source text not null,
404
- is_active integer not null default 1,
405
- first_seen_at text not null,
406
- last_seen_at text not null,
407
- raw_json text not null default '{}',
408
- primary key (profile_id, kind, value)
409
- );
410
-
411
- create table if not exists identity_search_index (
412
- profile_id text not null,
413
- kind text not null,
414
- value text not null,
415
- normalized_value text not null,
416
- source text not null,
417
- weight integer not null,
418
- updated_at text not null,
419
- primary key (profile_id, kind, value, source)
420
- );
421
-
422
- create table if not exists tweets (
423
- id text primary key,
424
- account_id text not null,
425
- author_profile_id text not null,
426
- kind text not null,
427
- text text not null,
428
- created_at text not null,
429
- is_replied integer not null default 0,
430
- reply_to_id text,
431
- like_count integer not null default 0,
432
- media_count integer not null default 0,
433
- bookmarked integer not null default 0,
434
- liked integer not null default 0,
435
- entities_json text not null default '{}',
436
- media_json text not null default '[]',
437
- quoted_tweet_id text
438
- );
439
-
440
- create table if not exists tweet_collections (
441
- account_id text not null,
442
- tweet_id text not null,
443
- kind text not null,
444
- collected_at text,
445
- source text not null,
446
- raw_json text not null default '{}',
447
- updated_at text not null,
448
- primary key (account_id, tweet_id, kind)
449
- );
450
-
451
- create table if not exists tweet_account_edges (
452
- account_id text not null,
453
- tweet_id text not null,
454
- kind text not null,
455
- first_seen_at text not null,
456
- last_seen_at text not null,
457
- seen_count integer not null default 1,
458
- source text not null,
459
- raw_json text not null default '{}',
460
- updated_at text not null,
461
- primary key (account_id, tweet_id, kind)
462
- );
463
-
464
- create table if not exists dm_conversations (
465
- id text primary key,
466
- account_id text not null,
467
- participant_profile_id text not null,
468
- title text not null,
469
- inbox_kind text not null default 'accepted',
470
- last_message_at text not null,
471
- unread_count integer not null default 0,
472
- needs_reply integer not null default 0
473
- );
474
-
475
- create table if not exists dm_messages (
476
- id text primary key,
477
- conversation_id text not null,
478
- sender_profile_id text not null,
479
- text text not null,
480
- created_at text not null,
481
- direction text not null,
482
- is_replied integer not null default 0,
483
- media_count integer not null default 0
484
- );
485
-
486
- create table if not exists tweet_actions (
487
- id text primary key,
488
- account_id text not null,
489
- tweet_id text,
490
- kind text not null,
491
- body text not null,
492
- created_at text not null
493
- );
494
-
495
- create table if not exists blocks (
496
- account_id text not null,
497
- profile_id text not null,
498
- source text not null,
499
- created_at text not null,
500
- primary key (account_id, profile_id)
501
- );
502
-
503
- create table if not exists mutes (
504
- account_id text not null,
505
- profile_id text not null,
506
- source text not null,
507
- created_at text not null,
508
- primary key (account_id, profile_id)
509
- );
510
-
511
- create table if not exists ai_scores (
512
- entity_kind text not null,
513
- entity_id text not null,
514
- model text not null,
515
- score integer not null,
516
- summary text not null,
517
- reasoning text not null,
518
- updated_at text not null,
519
- primary key (entity_kind, entity_id)
520
- );
521
-
522
- create table if not exists sync_cache (
523
- cache_key text primary key,
524
- value_json text not null,
525
- updated_at text not null
526
- );
527
-
528
- create table if not exists url_expansions (
529
- short_url text primary key,
530
- expanded_url text not null,
531
- final_url text not null,
532
- status text not null,
533
- expanded_tweet_id text,
534
- expanded_handle text,
535
- title text,
536
- description text,
537
- image_url text,
538
- site_name text,
539
- error text,
540
- source text not null,
541
- updated_at text not null
542
- );
543
-
544
- create table if not exists link_occurrences (
545
- source_kind text not null,
546
- source_id text not null,
547
- source_position integer not null,
548
- short_url text not null,
549
- account_id text,
550
- conversation_id text,
551
- direction text,
552
- created_at text not null,
553
- primary key (source_kind, source_id, source_position, short_url)
554
- );
555
-
556
- create table if not exists follow_snapshots (
557
- id text primary key,
558
- account_id text not null,
559
- direction text not null,
560
- source text not null,
561
- status text not null,
562
- page_count integer not null default 0,
563
- result_count integer not null default 0,
564
- started_at text not null,
565
- completed_at text not null,
566
- raw_meta_json text not null default '{}'
567
- );
568
-
569
- create table if not exists follow_snapshot_members (
570
- snapshot_id text not null,
571
- profile_id text not null,
572
- external_user_id text not null,
573
- position integer not null,
574
- primary key (snapshot_id, profile_id)
575
- );
576
-
577
- create table if not exists follow_edges (
578
- account_id text not null,
579
- direction text not null,
580
- profile_id text not null,
581
- external_user_id text not null,
582
- source text not null,
583
- current integer not null default 1,
584
- first_seen_at text not null,
585
- last_seen_at text not null,
586
- ended_at text,
587
- updated_at text not null,
588
- primary key (account_id, direction, profile_id)
589
- );
590
-
591
- create table if not exists follow_events (
592
- id text primary key,
593
- account_id text not null,
594
- direction text not null,
595
- profile_id text not null,
596
- external_user_id text not null,
597
- kind text not null,
598
- event_at text not null,
599
- snapshot_id text not null
600
- );
601
-
602
- create table if not exists geocoded_locations (
603
- normalized_key text primary key,
604
- original text not null,
605
- lat real not null,
606
- lng real not null,
607
- formatted text,
608
- country_code text,
609
- confidence integer,
610
- provider text not null,
611
- approx_radius_m real,
612
- bounds_json text not null default '{}',
613
- components_json text not null default '{}',
614
- hits integer not null default 1,
615
- created_at text not null,
616
- last_used_at text not null
617
- );
618
-
619
- create table if not exists geocoded_locations_unresolved (
620
- normalized_key text primary key,
621
- original text not null,
622
- reason text not null,
623
- last_attempted_at text not null,
624
- ttl_until text
625
- );
626
-
627
- create virtual table if not exists tweets_fts using fts5(
628
- tweet_id unindexed,
629
- text
630
- );
631
-
632
- create virtual table if not exists dm_fts using fts5(
633
- message_id unindexed,
634
- text
635
- );
636
- `;
637
-
638
- const INDEX_SQL = `
639
- create index if not exists idx_tweets_kind_created on tweets(kind, created_at desc);
640
- create index if not exists idx_tweets_created on tweets(created_at desc);
641
- create index if not exists idx_tweets_account_created on tweets(account_id, created_at desc);
642
- create index if not exists idx_tweets_quoted on tweets(quoted_tweet_id);
643
- create index if not exists idx_tweet_collections_kind_account on tweet_collections(kind, account_id, collected_at desc, tweet_id);
644
- create index if not exists idx_tweet_collections_tweet on tweet_collections(tweet_id);
645
- create index if not exists idx_tweet_account_edges_kind_account on tweet_account_edges(kind, account_id, last_seen_at desc, tweet_id);
646
- create index if not exists idx_tweet_account_edges_kind_tweet on tweet_account_edges(kind, tweet_id, account_id);
647
- create index if not exists idx_tweet_account_edges_tweet on tweet_account_edges(tweet_id);
648
- create index if not exists idx_dm_conversations_account on dm_conversations(account_id, last_message_at desc);
649
- create index if not exists idx_dm_messages_conversation on dm_messages(conversation_id, created_at asc);
650
- create index if not exists idx_profiles_followers on profiles(followers_count desc);
651
- create index if not exists idx_profiles_following on profiles(following_count desc);
652
- create index if not exists idx_profiles_handle on profiles(handle);
653
- create index if not exists idx_profile_affiliations_subject on profile_affiliations(subject_profile_id, is_active, last_seen_at desc);
654
- create index if not exists idx_profile_affiliations_org on profile_affiliations(organization_profile_id, is_active, last_seen_at desc);
655
- create index if not exists idx_profile_snapshots_profile on profile_snapshots(profile_id, last_seen_at desc);
656
- create index if not exists idx_profile_bio_entities_profile on profile_bio_entities(profile_id, is_active, last_seen_at desc);
657
- create index if not exists idx_profile_bio_entities_value on profile_bio_entities(kind, value, is_active);
658
- create index if not exists idx_identity_search_index_profile on identity_search_index(profile_id);
659
- create index if not exists idx_identity_search_index_value on identity_search_index(normalized_value, kind, weight desc);
660
- create index if not exists idx_blocks_account_created on blocks(account_id, created_at desc);
661
- create index if not exists idx_mutes_account_created on mutes(account_id, created_at desc);
662
- create index if not exists idx_ai_scores_updated on ai_scores(updated_at desc);
663
- create index if not exists idx_sync_cache_updated on sync_cache(updated_at desc);
664
- create index if not exists idx_url_expansions_expanded on url_expansions(expanded_url);
665
- create index if not exists idx_url_expansions_tweet on url_expansions(expanded_tweet_id);
666
- create index if not exists idx_url_expansions_handle on url_expansions(expanded_handle);
667
- create index if not exists idx_link_occurrences_url on link_occurrences(short_url);
668
- create index if not exists idx_link_occurrences_created on link_occurrences(created_at desc);
669
- create index if not exists idx_link_occurrences_account on link_occurrences(account_id, created_at desc);
670
- create index if not exists idx_link_occurrences_direction on link_occurrences(direction, created_at desc);
671
- create index if not exists idx_follow_edges_current on follow_edges(account_id, direction, current, last_seen_at desc);
672
- create index if not exists idx_follow_edges_profile on follow_edges(profile_id, current);
673
- create index if not exists idx_follow_snapshots_account on follow_snapshots(account_id, direction, completed_at desc);
674
- create index if not exists idx_follow_events_account on follow_events(account_id, direction, kind, event_at desc);
675
- create index if not exists idx_geocoded_locations_country on geocoded_locations(country_code);
676
- create index if not exists idx_geocoded_locations_last_used on geocoded_locations(last_used_at desc);
677
- create index if not exists idx_geocoded_unresolved_ttl on geocoded_locations_unresolved(ttl_until desc);
678
- `;
679
-
680
- function getColumnNames(db: Database, tableName: string): Set<string> {
681
- const rows = db.prepare(`pragma table_info(${tableName})`).all() as Array<{
682
- name: string;
683
- }>;
684
- return new Set(rows.map((row) => row.name));
685
- }
686
-
687
- function ensureTweetMetadataColumns(db: Database) {
688
- const columnNames = getColumnNames(db, "tweets");
689
- if (!columnNames.has("entities_json")) {
690
- db.exec(
691
- "alter table tweets add column entities_json text not null default '{}'",
692
- );
693
- }
694
- if (!columnNames.has("media_json")) {
695
- db.exec(
696
- "alter table tweets add column media_json text not null default '[]'",
697
- );
698
- }
699
- if (!columnNames.has("quoted_tweet_id")) {
700
- db.exec("alter table tweets add column quoted_tweet_id text");
701
- }
702
- }
703
-
704
- function ensureProfileAvatarColumns(db: Database) {
705
- const columnNames = getColumnNames(db, "profiles");
706
- if (!columnNames.has("following_count")) {
707
- db.exec(
708
- "alter table profiles add column following_count integer not null default 0",
709
- );
710
- }
711
- if (!columnNames.has("avatar_url")) {
712
- db.exec("alter table profiles add column avatar_url text");
713
- }
714
- if (!columnNames.has("location")) {
715
- db.exec("alter table profiles add column location text");
716
- }
717
- if (!columnNames.has("url")) {
718
- db.exec("alter table profiles add column url text");
719
- }
720
- if (!columnNames.has("verified_type")) {
721
- db.exec("alter table profiles add column verified_type text");
722
- }
723
- if (!columnNames.has("entities_json")) {
724
- db.exec(
725
- "alter table profiles add column entities_json text not null default '{}'",
726
- );
727
- }
728
- if (!columnNames.has("raw_json")) {
729
- db.exec(
730
- "alter table profiles add column raw_json text not null default '{}'",
731
- );
732
- }
733
- if (!columnNames.has("public_metrics_json")) {
734
- db.exec(
735
- "alter table profiles add column public_metrics_json text not null default '{}'",
736
- );
737
- }
738
- }
739
-
740
- function ensureAccountExternalUserIdColumn(db: Database) {
741
- const columnNames = getColumnNames(db, "accounts");
742
- if (!columnNames.has("external_user_id")) {
743
- db.exec("alter table accounts add column external_user_id text");
744
- }
745
- }
746
-
747
- function ensureDmConversationInboxColumns(db: Database) {
748
- const columnNames = getColumnNames(db, "dm_conversations");
749
- if (!columnNames.has("inbox_kind")) {
750
- db.exec(
751
- "alter table dm_conversations add column inbox_kind text not null default 'accepted'",
752
- );
753
- }
754
- }
755
-
756
- function ensureTweetCollectionsTable(db: Database) {
757
- db.exec(`
758
- create table if not exists tweet_collections (
759
- account_id text not null,
760
- tweet_id text not null,
761
- kind text not null,
762
- collected_at text,
763
- source text not null,
764
- raw_json text not null default '{}',
765
- updated_at text not null,
766
- primary key (account_id, tweet_id, kind)
767
- );
768
- `);
769
- }
770
-
771
- function ensureTweetAccountEdgesTable(db: Database) {
772
- db.exec(`
773
- create table if not exists tweet_account_edges (
774
- account_id text not null,
775
- tweet_id text not null,
776
- kind text not null,
777
- first_seen_at text not null,
778
- last_seen_at text not null,
779
- seen_count integer not null default 1,
780
- source text not null,
781
- raw_json text not null default '{}',
782
- updated_at text not null,
783
- primary key (account_id, tweet_id, kind)
784
- );
785
- `);
786
- }
787
-
788
- function ensureProfileAffiliationsTable(db: Database) {
789
- db.exec(`
790
- create table if not exists profile_affiliations (
791
- subject_profile_id text not null,
792
- organization_profile_id text not null,
793
- organization_name text,
794
- organization_handle text,
795
- badge_url text,
796
- url text,
797
- label text,
798
- source text not null,
799
- is_active integer not null default 1,
800
- first_seen_at text not null,
801
- last_seen_at text not null,
802
- raw_json text not null default '{}',
803
- updated_at text not null,
804
- primary key (subject_profile_id, organization_profile_id)
805
- );
806
- `);
807
- }
808
-
809
- function ensureProfileSnapshotsTable(db: Database) {
810
- db.exec(`
811
- create table if not exists profile_snapshots (
812
- profile_id text not null,
813
- snapshot_hash text not null,
814
- observed_at text not null,
815
- last_seen_at text not null,
816
- source text not null,
817
- handle text not null,
818
- display_name text not null,
819
- bio text not null,
820
- location text,
821
- url text,
822
- verified_type text,
823
- followers_count integer not null default 0,
824
- following_count integer not null default 0,
825
- affiliations_json text not null default '[]',
826
- raw_json text not null default '{}',
827
- primary key (profile_id, snapshot_hash)
828
- );
829
- `);
830
- }
831
-
832
- function ensureProfileBioEntitiesTable(db: Database) {
833
- db.exec(`
834
- create table if not exists profile_bio_entities (
835
- profile_id text not null,
836
- kind text not null,
837
- value text not null,
838
- source text not null,
839
- is_active integer not null default 1,
840
- first_seen_at text not null,
841
- last_seen_at text not null,
842
- raw_json text not null default '{}',
843
- primary key (profile_id, kind, value)
844
- );
845
- `);
846
- }
847
-
848
- function ensureIdentitySearchIndexTable(db: Database) {
849
- db.exec(`
850
- create table if not exists identity_search_index (
851
- profile_id text not null,
852
- kind text not null,
853
- value text not null,
854
- normalized_value text not null,
855
- source text not null,
856
- weight integer not null,
857
- updated_at text not null,
858
- primary key (profile_id, kind, value, source)
859
- );
860
- `);
861
- }
862
-
863
- function ensureLinkIndexTables(db: Database) {
864
- db.exec(`
865
- create table if not exists url_expansions (
866
- short_url text primary key,
867
- expanded_url text not null,
868
- final_url text not null,
869
- status text not null,
870
- expanded_tweet_id text,
871
- expanded_handle text,
872
- title text,
873
- description text,
874
- image_url text,
875
- site_name text,
876
- error text,
877
- source text not null,
878
- updated_at text not null
879
- );
880
-
881
- create table if not exists link_occurrences (
882
- source_kind text not null,
883
- source_id text not null,
884
- source_position integer not null,
885
- short_url text not null,
886
- account_id text,
887
- conversation_id text,
888
- direction text,
889
- created_at text not null,
890
- primary key (source_kind, source_id, source_position, short_url)
891
- );
892
- `);
893
-
894
- const urlExpansionColumns = getColumnNames(db, "url_expansions");
895
- if (!urlExpansionColumns.has("image_url")) {
896
- db.exec("alter table url_expansions add column image_url text");
897
- }
898
- if (!urlExpansionColumns.has("site_name")) {
899
- db.exec("alter table url_expansions add column site_name text");
900
- }
901
- }
902
-
903
- function ensureFollowGraphTables(db: Database) {
904
- db.exec(`
905
- create table if not exists follow_snapshots (
906
- id text primary key,
907
- account_id text not null,
908
- direction text not null,
909
- source text not null,
910
- status text not null,
911
- page_count integer not null default 0,
912
- result_count integer not null default 0,
913
- started_at text not null,
914
- completed_at text not null,
915
- raw_meta_json text not null default '{}'
916
- );
917
-
918
- create table if not exists follow_snapshot_members (
919
- snapshot_id text not null,
920
- profile_id text not null,
921
- external_user_id text not null,
922
- position integer not null,
923
- primary key (snapshot_id, profile_id)
924
- );
925
-
926
- create table if not exists follow_edges (
927
- account_id text not null,
928
- direction text not null,
929
- profile_id text not null,
930
- external_user_id text not null,
931
- source text not null,
932
- current integer not null default 1,
933
- first_seen_at text not null,
934
- last_seen_at text not null,
935
- ended_at text,
936
- updated_at text not null,
937
- primary key (account_id, direction, profile_id)
938
- );
939
-
940
- create table if not exists follow_events (
941
- id text primary key,
942
- account_id text not null,
943
- direction text not null,
944
- profile_id text not null,
945
- external_user_id text not null,
946
- kind text not null,
947
- event_at text not null,
948
- snapshot_id text not null
949
- );
950
- `);
951
- }
952
-
953
- function backfillTweetCollections(db: Database) {
954
- const missingKinds = (
955
- [
956
- ["likes", "liked"],
957
- ["bookmarks", "bookmarked"],
958
- ] as const
959
- ).filter(([, column]) =>
960
- db
961
- .prepare(
962
- `
963
- select 1
964
- from tweets as tweet
965
- where tweet.${column} = 1
966
- and not exists (
967
- select 1
968
- from tweet_collections as collection
969
- where collection.account_id = tweet.account_id
970
- and collection.tweet_id = tweet.id
971
- and collection.kind = ?
972
- )
973
- limit 1
974
- `,
975
- )
976
- .get(column === "liked" ? "likes" : "bookmarks"),
977
- );
978
- if (missingKinds.length === 0) {
979
- return;
980
- }
981
-
982
- const now = new Date().toISOString();
983
- const insert = db.prepare(`
984
- insert or ignore into tweet_collections (
985
- account_id, tweet_id, kind, collected_at, source, raw_json, updated_at
986
- )
987
- select account_id, id, ?, null, 'legacy', '{}', ?
988
- from tweets
989
- where
990
- case
991
- when ? = 'likes' then liked
992
- else bookmarked
993
- end = 1
994
- `);
995
-
996
- db.transaction(() => {
997
- for (const [kind] of missingKinds) {
998
- insert.run(kind, now, kind);
999
- }
1000
- })();
1001
- }
1002
-
1003
- function backfillTweetAccountEdges(db: Database) {
1004
- const missing = db
1005
- .prepare(
1006
- `
1007
- select 1
1008
- from tweets as tweet
1009
- where tweet.kind in ('home', 'mention')
1010
- and not exists (
1011
- select 1
1012
- from tweet_account_edges as edge
1013
- where edge.account_id = tweet.account_id
1014
- and edge.tweet_id = tweet.id
1015
- and edge.kind = tweet.kind
1016
- )
1017
- limit 1
1018
- `,
1019
- )
1020
- .get();
1021
- if (!missing) {
1022
- return;
1023
- }
1024
-
1025
- const now = new Date().toISOString();
1026
- db.prepare(`
1027
- insert or ignore into tweet_account_edges (
1028
- account_id, tweet_id, kind, first_seen_at, last_seen_at, seen_count,
1029
- source, raw_json, updated_at
1030
- )
1031
- select
1032
- account_id,
1033
- id,
1034
- kind,
1035
- created_at,
1036
- created_at,
1037
- 1,
1038
- 'legacy',
1039
- '{}',
1040
- ?
1041
- from tweets
1042
- where kind in ('home', 'mention')
1043
- `).run(now);
1044
- }
1045
-
1046
- function ensureSchemaIndexes(db: Database) {
1047
- db.exec(INDEX_SQL);
1048
- }
1049
-
1050
- function ensureDemoData(db: Database) {
1051
- if (demoSeedAttempted) {
1052
- return;
1053
- }
1054
-
1055
- seedDemoData(db);
1056
- backfillTweetCollections(db);
1057
- backfillTweetAccountEdges(db);
1058
- demoSeedAttempted = true;
1059
- }
1060
-
1061
- function initDatabase(options: InitDatabaseOptions = {}) {
1062
- ensureBirdclawDirs();
1063
-
1064
- if (!nativeDb) {
1065
- const { dbPath } = getBirdclawPaths();
1066
- nativeDb = new NativeSqliteDatabase(dbPath);
1067
- nativeDb.exec(BASE_SCHEMA_SQL);
1068
- ensureAccountExternalUserIdColumn(nativeDb);
1069
- ensureDmConversationInboxColumns(nativeDb);
1070
- ensureTweetMetadataColumns(nativeDb);
1071
- ensureProfileAvatarColumns(nativeDb);
1072
- ensureTweetCollectionsTable(nativeDb);
1073
- ensureTweetAccountEdgesTable(nativeDb);
1074
- ensureProfileAffiliationsTable(nativeDb);
1075
- ensureProfileSnapshotsTable(nativeDb);
1076
- ensureProfileBioEntitiesTable(nativeDb);
1077
- ensureIdentitySearchIndexTable(nativeDb);
1078
- ensureLinkIndexTables(nativeDb);
1079
- ensureFollowGraphTables(nativeDb);
1080
- ensureSchemaIndexes(nativeDb);
1081
- if (options.seedDemoData !== false) {
1082
- ensureDemoData(nativeDb);
1083
- } else {
1084
- backfillTweetCollections(nativeDb);
1085
- backfillTweetAccountEdges(nativeDb);
1086
- }
1087
- } else if (options.seedDemoData !== false) {
1088
- ensureDemoData(nativeDb);
1089
- }
1090
-
1091
- if (!kyselyDb) {
1092
- kyselyDb = new Kysely<BirdclawDatabase>({
1093
- dialect: new SqliteDialect({
1094
- database: nativeDb,
1095
- }),
1096
- });
1097
- }
1098
- }
1099
-
1100
- export function getNativeDb(options: InitDatabaseOptions = {}) {
1101
- initDatabase(options);
1102
- return nativeDb as Database;
1103
- }
1104
-
1105
- export function getDb() {
1106
- initDatabase();
1107
- return kyselyDb as Kysely<BirdclawDatabase>;
1108
- }
1109
-
1110
- export async function closeDatabase() {
1111
- const db = kyselyDb;
1112
- const native = nativeDb;
1113
- kyselyDb = undefined;
1114
- nativeDb = undefined;
1115
- demoSeedAttempted = false;
1116
-
1117
- if (db) {
1118
- await db.destroy();
1119
- } else {
1120
- native?.close();
1121
- }
1122
- }
1123
-
1124
- export function resetDatabaseForTests() {
1125
- const db = kyselyDb;
1126
- const native = nativeDb;
1127
- kyselyDb = undefined;
1128
- nativeDb = undefined;
1129
- demoSeedAttempted = false;
1130
- if (db) {
1131
- void db.destroy();
1132
- } else {
1133
- native?.close();
1134
- }
1135
- }