birdclaw 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/CHANGELOG.md +63 -0
  2. package/README.md +55 -5
  3. package/bin/birdclaw.mjs +50 -11
  4. package/package.json +9 -7
  5. package/public/birdclaw-mark.png +0 -0
  6. package/scripts/browser-perf.mjs +400 -0
  7. package/scripts/build-docs-site.mjs +940 -0
  8. package/scripts/docs-site-assets.mjs +311 -0
  9. package/scripts/run-vitest.mjs +21 -0
  10. package/scripts/sanitize-node-options.mjs +23 -0
  11. package/scripts/start-test-server.mjs +42 -0
  12. package/src/cli.ts +422 -14
  13. package/src/components/AccountSwitcher.tsx +186 -0
  14. package/src/components/AppNav.tsx +29 -9
  15. package/src/components/AvatarChip.tsx +9 -3
  16. package/src/components/BrandMark.tsx +67 -0
  17. package/src/components/ConversationThread.tsx +11 -10
  18. package/src/components/DmWorkspace.tsx +39 -14
  19. package/src/components/FeedState.tsx +147 -0
  20. package/src/components/InboxCard.tsx +14 -2
  21. package/src/components/LinkPreviewCard.tsx +40 -18
  22. package/src/components/MarkdownViewer.tsx +452 -0
  23. package/src/components/SavedTimelineView.tsx +64 -56
  24. package/src/components/SyncNowButton.tsx +137 -0
  25. package/src/components/ThemeSlider.tsx +49 -93
  26. package/src/components/TimelineCard.tsx +364 -136
  27. package/src/components/TimelineRouteFrame.tsx +170 -0
  28. package/src/components/TweetMediaGrid.tsx +170 -24
  29. package/src/components/TweetRichText.tsx +28 -13
  30. package/src/components/account-selection.ts +64 -0
  31. package/src/components/useTimelineRouteData.ts +153 -0
  32. package/src/lib/account-sync-job.ts +654 -0
  33. package/src/lib/actions-transport.ts +216 -146
  34. package/src/lib/api-client.ts +304 -0
  35. package/src/lib/archive-finder.ts +72 -53
  36. package/src/lib/archive-import.ts +1377 -1298
  37. package/src/lib/authored-live.ts +261 -204
  38. package/src/lib/avatar-cache.ts +159 -44
  39. package/src/lib/backup.ts +1532 -951
  40. package/src/lib/bird-actions.ts +139 -57
  41. package/src/lib/bird-command.ts +101 -28
  42. package/src/lib/bird.ts +549 -194
  43. package/src/lib/blocklist.ts +40 -23
  44. package/src/lib/blocks-write.ts +129 -80
  45. package/src/lib/blocks.ts +165 -97
  46. package/src/lib/bookmark-sync-job.ts +250 -160
  47. package/src/lib/conversation-surface.ts +205 -0
  48. package/src/lib/db.ts +35 -3
  49. package/src/lib/dms-live.ts +720 -66
  50. package/src/lib/effect-runtime.ts +45 -0
  51. package/src/lib/follow-graph.ts +224 -180
  52. package/src/lib/http-effect.ts +222 -0
  53. package/src/lib/inbox.ts +74 -43
  54. package/src/lib/link-index.ts +88 -76
  55. package/src/lib/link-insights.ts +24 -0
  56. package/src/lib/link-preview-metadata.ts +472 -52
  57. package/src/lib/media-fetch.ts +286 -213
  58. package/src/lib/mention-threads-live.ts +352 -288
  59. package/src/lib/mentions-live.ts +390 -342
  60. package/src/lib/moderation-target.ts +102 -65
  61. package/src/lib/moderation-write.ts +77 -18
  62. package/src/lib/mutes-write.ts +129 -80
  63. package/src/lib/mutes.ts +8 -1
  64. package/src/lib/openai.ts +84 -53
  65. package/src/lib/period-digest.ts +953 -0
  66. package/src/lib/profile-affiliation-hydration.ts +93 -54
  67. package/src/lib/profile-hydration.ts +124 -72
  68. package/src/lib/profile-replies.ts +60 -43
  69. package/src/lib/profile-resolver.ts +402 -294
  70. package/src/lib/queries.ts +1024 -189
  71. package/src/lib/research.ts +165 -120
  72. package/src/lib/sqlite.ts +1 -0
  73. package/src/lib/timeline-collections-live.ts +204 -167
  74. package/src/lib/timeline-live.ts +60 -39
  75. package/src/lib/tweet-lookup.ts +30 -19
  76. package/src/lib/types.ts +38 -1
  77. package/src/lib/ui.ts +41 -10
  78. package/src/lib/url-expansion.ts +226 -55
  79. package/src/lib/url-safety.ts +220 -0
  80. package/src/lib/web-sync.ts +511 -0
  81. package/src/lib/whois.ts +166 -147
  82. package/src/lib/x-web.ts +102 -71
  83. package/src/lib/xurl.ts +681 -411
  84. package/src/routeTree.gen.ts +63 -0
  85. package/src/routes/__root.tsx +25 -5
  86. package/src/routes/api/action.tsx +127 -78
  87. package/src/routes/api/avatar.tsx +39 -30
  88. package/src/routes/api/blocks.tsx +26 -23
  89. package/src/routes/api/conversation.tsx +25 -14
  90. package/src/routes/api/inbox.tsx +27 -21
  91. package/src/routes/api/link-insights.tsx +31 -25
  92. package/src/routes/api/link-preview.tsx +25 -21
  93. package/src/routes/api/period-digest.tsx +123 -0
  94. package/src/routes/api/profile-hydrate.tsx +31 -28
  95. package/src/routes/api/query.tsx +79 -55
  96. package/src/routes/api/status.tsx +18 -10
  97. package/src/routes/api/sync.tsx +105 -0
  98. package/src/routes/dms.tsx +195 -55
  99. package/src/routes/inbox.tsx +32 -19
  100. package/src/routes/index.tsx +21 -127
  101. package/src/routes/links.tsx +50 -5
  102. package/src/routes/mentions.tsx +21 -127
  103. package/src/routes/today.tsx +441 -0
  104. package/src/styles.css +74 -11
  105. package/vite.config.ts +8 -0
@@ -0,0 +1,400 @@
1
+ #!/usr/bin/env node
2
+ import { spawn } from "node:child_process";
3
+ import path from "node:path";
4
+ import { setTimeout as sleep } from "node:timers/promises";
5
+ import { chromium } from "@playwright/test";
6
+ import { withSanitizedNodeOptions } from "./sanitize-node-options.mjs";
7
+
8
+ const DEFAULT_BASE_URL = "http://localhost:3000";
9
+ const DEFAULT_ITERATIONS = 5;
10
+ const READY_TIMEOUT_MS = 20_000;
11
+
12
+ const SCENARIOS = {
13
+ home: {
14
+ path: "/",
15
+ ready: async (page) => waitForAny(page, ['[data-perf="timeline-card"]']),
16
+ },
17
+ mentions: {
18
+ path: "/mentions",
19
+ ready: async (page) => waitForAny(page, ['[data-perf="timeline-card"]']),
20
+ },
21
+ "mentions-search": {
22
+ path: "/mentions",
23
+ ready: async (page) => {
24
+ await page.getByPlaceholder("Search mentions").fill("peekaboo");
25
+ await waitForAny(page, ['[data-perf="timeline-card"]']);
26
+ },
27
+ },
28
+ links: {
29
+ path: "/links",
30
+ ready: async (page) =>
31
+ waitForAny(page, [
32
+ '[data-perf="link-insight-row"]',
33
+ "text=No links in this window.",
34
+ ]),
35
+ },
36
+ "links-toggle": {
37
+ path: "/links",
38
+ ready: async (page) =>
39
+ waitForAny(page, [
40
+ '[data-perf="link-insight-row"]',
41
+ "text=No links in this window.",
42
+ ]),
43
+ action: async (page) => {
44
+ await page.getByRole("button", { name: "videos" }).click();
45
+ await waitForAny(page, [
46
+ '[data-perf="link-insight-row"]',
47
+ "text=No links in this window.",
48
+ ]);
49
+ },
50
+ },
51
+ };
52
+
53
+ function parseArgs(argv) {
54
+ const options = {
55
+ baseUrl: process.env.BIRDCLAW_PERF_URL || DEFAULT_BASE_URL,
56
+ iterations: DEFAULT_ITERATIONS,
57
+ scenarios: Object.keys(SCENARIOS),
58
+ json: false,
59
+ budgets: {},
60
+ };
61
+
62
+ for (const arg of argv) {
63
+ if (arg === "--json") {
64
+ options.json = true;
65
+ } else if (arg.startsWith("--url=")) {
66
+ options.baseUrl = arg.slice("--url=".length).replace(/\/$/, "");
67
+ } else if (arg.startsWith("--iterations=")) {
68
+ options.iterations = Math.max(
69
+ 1,
70
+ Number.parseInt(arg.split("=")[1] ?? "", 10),
71
+ );
72
+ } else if (arg.startsWith("--scenario=")) {
73
+ options.scenarios = arg
74
+ .slice("--scenario=".length)
75
+ .split(",")
76
+ .map((value) => value.trim())
77
+ .filter(Boolean);
78
+ } else if (arg.startsWith("--budget-ready-ms=")) {
79
+ options.budgets.readyMs = Number(arg.split("=")[1]);
80
+ } else if (arg.startsWith("--budget-action-ms=")) {
81
+ options.budgets.actionMs = Number(arg.split("=")[1]);
82
+ } else if (arg.startsWith("--budget-api-p95-ms=")) {
83
+ options.budgets.apiP95Ms = Number(arg.split("=")[1]);
84
+ } else if (arg.startsWith("--budget-preview-calls=")) {
85
+ options.budgets.previewCalls = Number(arg.split("=")[1]);
86
+ }
87
+ }
88
+
89
+ for (const scenario of options.scenarios) {
90
+ if (!SCENARIOS[scenario]) {
91
+ throw new Error(`Unknown scenario: ${scenario}`);
92
+ }
93
+ }
94
+
95
+ return options;
96
+ }
97
+
98
+ async function isReachable(baseUrl) {
99
+ try {
100
+ const response = await fetch(baseUrl, {
101
+ signal: AbortSignal.timeout(1000),
102
+ });
103
+ return response.ok || response.status < 500;
104
+ } catch {
105
+ return false;
106
+ }
107
+ }
108
+
109
+ async function startServerIfNeeded(baseUrl) {
110
+ if (await isReachable(baseUrl)) {
111
+ return null;
112
+ }
113
+
114
+ const url = new URL(baseUrl);
115
+ const port = url.port || "3000";
116
+ const viteBin = path.join(
117
+ process.cwd(),
118
+ "node_modules",
119
+ "vite",
120
+ "bin",
121
+ "vite.js",
122
+ );
123
+ const child = spawn(
124
+ process.execPath,
125
+ [viteBin, "dev", "--port", port, "--host", "127.0.0.1"],
126
+ {
127
+ cwd: process.cwd(),
128
+ stdio: ["ignore", "pipe", "pipe"],
129
+ env: {
130
+ ...withSanitizedNodeOptions(process.env),
131
+ BIRDCLAW_LOCAL_WEB: "1",
132
+ },
133
+ },
134
+ );
135
+
136
+ const deadline = Date.now() + 30_000;
137
+ while (Date.now() < deadline) {
138
+ if (await isReachable(baseUrl)) {
139
+ return child;
140
+ }
141
+ await sleep(250);
142
+ }
143
+
144
+ child.kill("SIGTERM");
145
+ throw new Error(`Timed out waiting for ${baseUrl}`);
146
+ }
147
+
148
+ async function waitForAny(page, selectors) {
149
+ await page.waitForFunction(
150
+ (values) =>
151
+ values.some((selector) => {
152
+ if (selector.startsWith("text=")) {
153
+ return document.body.innerText.includes(selector.slice(5));
154
+ }
155
+ return Boolean(document.querySelector(selector));
156
+ }),
157
+ selectors,
158
+ { timeout: READY_TIMEOUT_MS },
159
+ );
160
+ }
161
+
162
+ function percentile(values, point) {
163
+ if (values.length === 0) return 0;
164
+ const sorted = [...values].sort((left, right) => left - right);
165
+ const index = Math.min(
166
+ sorted.length - 1,
167
+ Math.ceil(sorted.length * point) - 1,
168
+ );
169
+ return sorted[index] ?? 0;
170
+ }
171
+
172
+ function median(values) {
173
+ return percentile(values, 0.5);
174
+ }
175
+
176
+ function round(value) {
177
+ return Math.round(value);
178
+ }
179
+
180
+ function apiBucket(rawUrl) {
181
+ const url = new URL(rawUrl);
182
+ if (url.pathname === "/api/link-insights") {
183
+ return `${url.pathname}:${url.searchParams.get("kind") ?? "unknown"}`;
184
+ }
185
+ return url.pathname;
186
+ }
187
+
188
+ function summarizeRuns(runs) {
189
+ const ready = runs.map((run) => run.readyMs);
190
+ const action = runs
191
+ .map((run) => run.actionMs)
192
+ .filter((value) => typeof value === "number");
193
+ const apiDurations = runs.flatMap((run) => run.apiDurationsMs);
194
+ return {
195
+ readyMedianMs: round(median(ready)),
196
+ readyP95Ms: round(percentile(ready, 0.95)),
197
+ actionMedianMs: action.length > 0 ? round(median(action)) : null,
198
+ actionP95Ms: action.length > 0 ? round(percentile(action, 0.95)) : null,
199
+ apiP95Ms: round(percentile(apiDurations, 0.95)),
200
+ apiCallsMedian: round(median(runs.map((run) => run.apiCalls))),
201
+ previewCallsMedian: round(median(runs.map((run) => run.previewCalls))),
202
+ rowsMedian: round(median(runs.map((run) => run.rows))),
203
+ previewsMedian: round(median(runs.map((run) => run.previews))),
204
+ endpoints: summarizeEndpoints(runs),
205
+ };
206
+ }
207
+
208
+ function summarizeEndpoints(runs) {
209
+ const totals = new Map();
210
+ for (const run of runs) {
211
+ for (const [endpoint, count] of Object.entries(run.apiEndpoints)) {
212
+ totals.set(endpoint, (totals.get(endpoint) ?? 0) + count);
213
+ }
214
+ }
215
+ return [...totals.entries()]
216
+ .map(([endpoint, count]) => ({
217
+ endpoint,
218
+ medianCalls: Math.round(count / runs.length),
219
+ }))
220
+ .sort((left, right) => right.medianCalls - left.medianCalls)
221
+ .slice(0, 8);
222
+ }
223
+
224
+ async function runScenario(browser, baseUrl, name) {
225
+ const scenario = SCENARIOS[name];
226
+ const page = await browser.newPage({
227
+ viewport: { width: 1474, height: 910 },
228
+ });
229
+ const requestStart = new Map();
230
+ const apiDurationsMs = [];
231
+ let apiCalls = 0;
232
+ let previewCalls = 0;
233
+ const apiEndpoints = {};
234
+
235
+ page.on("request", (request) => {
236
+ if (request.url().includes("/api/")) {
237
+ requestStart.set(request, performance.now());
238
+ }
239
+ });
240
+ page.on("response", (response) => {
241
+ const request = response.request();
242
+ const startedAt = requestStart.get(request);
243
+ if (startedAt === undefined) return;
244
+ requestStart.delete(request);
245
+ apiCalls += 1;
246
+ const endpoint = apiBucket(response.url());
247
+ apiEndpoints[endpoint] = (apiEndpoints[endpoint] ?? 0) + 1;
248
+ apiDurationsMs.push(performance.now() - startedAt);
249
+ if (response.url().includes("/api/link-preview")) {
250
+ previewCalls += 1;
251
+ }
252
+ });
253
+
254
+ const startedAt = performance.now();
255
+ await page.goto(`${baseUrl}${scenario.path}`, {
256
+ waitUntil: "domcontentloaded",
257
+ });
258
+ await scenario.ready(page);
259
+ const readyMs = performance.now() - startedAt;
260
+
261
+ let actionMs = null;
262
+ if (scenario.action) {
263
+ const actionStartedAt = performance.now();
264
+ await scenario.action(page);
265
+ actionMs = performance.now() - actionStartedAt;
266
+ }
267
+
268
+ await page.waitForTimeout(250);
269
+ const counts = await page.evaluate(() => ({
270
+ rows: document.querySelectorAll(
271
+ '[data-perf="timeline-card"], [data-perf="link-insight-row"]',
272
+ ).length,
273
+ previews: document.querySelectorAll('[data-perf="link-preview-card"]')
274
+ .length,
275
+ }));
276
+ await page.close();
277
+
278
+ return {
279
+ readyMs,
280
+ actionMs,
281
+ apiCalls,
282
+ previewCalls,
283
+ apiEndpoints,
284
+ apiDurationsMs,
285
+ rows: counts.rows,
286
+ previews: counts.previews,
287
+ };
288
+ }
289
+
290
+ function budgetFailures(name, summary, budgets) {
291
+ const failures = [];
292
+ if (
293
+ Number.isFinite(budgets.readyMs) &&
294
+ summary.readyP95Ms > budgets.readyMs
295
+ ) {
296
+ failures.push(
297
+ `${name} ready p95 ${summary.readyP95Ms}ms > ${budgets.readyMs}ms`,
298
+ );
299
+ }
300
+ if (
301
+ Number.isFinite(budgets.actionMs) &&
302
+ summary.actionP95Ms !== null &&
303
+ summary.actionP95Ms > budgets.actionMs
304
+ ) {
305
+ failures.push(
306
+ `${name} action p95 ${summary.actionP95Ms}ms > ${budgets.actionMs}ms`,
307
+ );
308
+ }
309
+ if (
310
+ Number.isFinite(budgets.apiP95Ms) &&
311
+ summary.apiP95Ms > budgets.apiP95Ms
312
+ ) {
313
+ failures.push(
314
+ `${name} api p95 ${summary.apiP95Ms}ms > ${budgets.apiP95Ms}ms`,
315
+ );
316
+ }
317
+ if (
318
+ Number.isFinite(budgets.previewCalls) &&
319
+ summary.previewCallsMedian > budgets.previewCalls
320
+ ) {
321
+ failures.push(
322
+ `${name} preview calls median ${summary.previewCallsMedian} > ${budgets.previewCalls}`,
323
+ );
324
+ }
325
+ return failures;
326
+ }
327
+
328
+ function printHuman(results, failures) {
329
+ console.log("browser perf");
330
+ for (const result of results) {
331
+ const summary = result.summary;
332
+ console.log("");
333
+ console.log(`scenario: ${result.name}`);
334
+ console.log(`ready median: ${summary.readyMedianMs}ms`);
335
+ console.log(`ready p95: ${summary.readyP95Ms}ms`);
336
+ if (summary.actionMedianMs !== null) {
337
+ console.log(`action median: ${summary.actionMedianMs}ms`);
338
+ console.log(`action p95: ${summary.actionP95Ms}ms`);
339
+ }
340
+ console.log(`api p95: ${summary.apiP95Ms}ms`);
341
+ console.log(`api calls median: ${summary.apiCallsMedian}`);
342
+ console.log(`preview calls median: ${summary.previewCallsMedian}`);
343
+ console.log(`rows median: ${summary.rowsMedian}`);
344
+ console.log(`preview cards median: ${summary.previewsMedian}`);
345
+ if (summary.endpoints.length > 0) {
346
+ console.log("top endpoints:");
347
+ for (const endpoint of summary.endpoints) {
348
+ console.log(`- ${endpoint.endpoint}: ${endpoint.medianCalls}`);
349
+ }
350
+ }
351
+ }
352
+ if (failures.length > 0) {
353
+ console.log("");
354
+ console.log("budget failures:");
355
+ for (const failure of failures) {
356
+ console.log(`- ${failure}`);
357
+ }
358
+ }
359
+ }
360
+
361
+ async function main() {
362
+ const options = parseArgs(process.argv.slice(2));
363
+ const server = await startServerIfNeeded(options.baseUrl);
364
+ const browser = await chromium.launch({ headless: true });
365
+ const results = [];
366
+ const failures = [];
367
+
368
+ try {
369
+ for (const name of options.scenarios) {
370
+ await runScenario(browser, options.baseUrl, name);
371
+ const runs = [];
372
+ for (let index = 0; index < options.iterations; index += 1) {
373
+ runs.push(await runScenario(browser, options.baseUrl, name));
374
+ }
375
+ const summary = summarizeRuns(runs);
376
+ results.push({ name, summary, runs });
377
+ failures.push(...budgetFailures(name, summary, options.budgets));
378
+ }
379
+ } finally {
380
+ await browser.close();
381
+ if (server) {
382
+ server.kill("SIGTERM");
383
+ }
384
+ }
385
+
386
+ if (options.json) {
387
+ console.log(JSON.stringify({ results, failures }, null, 2));
388
+ } else {
389
+ printHuman(results, failures);
390
+ }
391
+
392
+ if (failures.length > 0) {
393
+ process.exitCode = 1;
394
+ }
395
+ }
396
+
397
+ main().catch((error) => {
398
+ console.error(error instanceof Error ? error.stack || error.message : error);
399
+ process.exit(1);
400
+ });