birdclaw 0.4.1 → 0.5.1

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 (85) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/README.md +113 -7
  3. package/bin/birdclaw.mjs +50 -11
  4. package/package.json +30 -28
  5. package/playwright.config.ts +1 -0
  6. package/public/birdclaw-mark.png +0 -0
  7. package/public/favicon.ico +0 -0
  8. package/public/logo192.png +0 -0
  9. package/public/logo512.png +0 -0
  10. package/public/manifest.json +2 -2
  11. package/scripts/browser-perf.mjs +399 -0
  12. package/scripts/build-docs-site.mjs +940 -0
  13. package/scripts/docs-site-assets.mjs +311 -0
  14. package/scripts/run-vitest.mjs +21 -0
  15. package/scripts/sanitize-node-options.mjs +23 -0
  16. package/scripts/start-test-server.mjs +29 -0
  17. package/src/cli.ts +496 -19
  18. package/src/components/AppNav.tsx +66 -29
  19. package/src/components/AvatarChip.tsx +10 -5
  20. package/src/components/BrandMark.tsx +67 -0
  21. package/src/components/ConversationThread.tsx +126 -0
  22. package/src/components/DmWorkspace.tsx +118 -105
  23. package/src/components/EmbeddedTweetCard.tsx +20 -14
  24. package/src/components/FeedState.tsx +147 -0
  25. package/src/components/InboxCard.tsx +104 -90
  26. package/src/components/LinkPreviewCard.tsx +270 -0
  27. package/src/components/ProfilePreview.tsx +8 -3
  28. package/src/components/SavedTimelineView.tsx +89 -71
  29. package/src/components/SyncNowButton.tsx +105 -0
  30. package/src/components/ThemeSlider.tsx +10 -59
  31. package/src/components/TimelineCard.tsx +326 -86
  32. package/src/components/TimelineRouteFrame.tsx +156 -0
  33. package/src/components/TweetMediaGrid.tsx +120 -23
  34. package/src/components/TweetRichText.tsx +19 -4
  35. package/src/components/useTimelineRouteData.ts +137 -0
  36. package/src/lib/api-client.ts +229 -0
  37. package/src/lib/archive-finder.ts +24 -20
  38. package/src/lib/archive-import.ts +1582 -67
  39. package/src/lib/authored-live.ts +1074 -0
  40. package/src/lib/backup.ts +316 -14
  41. package/src/lib/bird-actions.ts +1 -10
  42. package/src/lib/bird-command.ts +57 -0
  43. package/src/lib/bird.ts +89 -5
  44. package/src/lib/config.ts +1 -1
  45. package/src/lib/conversation-surface.ts +174 -0
  46. package/src/lib/db.ts +193 -4
  47. package/src/lib/follow-graph.ts +1053 -0
  48. package/src/lib/link-index.ts +11 -98
  49. package/src/lib/link-insights.ts +834 -0
  50. package/src/lib/link-preview-metadata.ts +334 -0
  51. package/src/lib/media-fetch.ts +787 -0
  52. package/src/lib/media-includes.ts +165 -0
  53. package/src/lib/mention-threads-live.ts +535 -43
  54. package/src/lib/mentions-live.ts +623 -19
  55. package/src/lib/moderation-target.ts +6 -0
  56. package/src/lib/profile-hydration.ts +1 -1
  57. package/src/lib/profile-resolver.ts +115 -1
  58. package/src/lib/queries.ts +326 -35
  59. package/src/lib/seed.ts +127 -8
  60. package/src/lib/timeline-collections-live.ts +145 -22
  61. package/src/lib/timeline-live.ts +10 -15
  62. package/src/lib/tweet-account-edges.ts +9 -2
  63. package/src/lib/tweet-render.ts +97 -0
  64. package/src/lib/types.ts +185 -2
  65. package/src/lib/ui.ts +383 -147
  66. package/src/lib/url-expansion-store.ts +110 -0
  67. package/src/lib/url-expansion.ts +20 -3
  68. package/src/lib/web-sync.ts +443 -0
  69. package/src/lib/x-profile.ts +7 -2
  70. package/src/lib/xurl.ts +296 -12
  71. package/src/routeTree.gen.ts +126 -0
  72. package/src/routes/__root.tsx +7 -3
  73. package/src/routes/api/conversation.tsx +34 -0
  74. package/src/routes/api/link-insights.tsx +79 -0
  75. package/src/routes/api/link-preview.tsx +43 -0
  76. package/src/routes/api/profile-hydrate.tsx +51 -0
  77. package/src/routes/api/sync.tsx +59 -0
  78. package/src/routes/blocks.tsx +111 -86
  79. package/src/routes/dms.tsx +172 -87
  80. package/src/routes/inbox.tsx +98 -86
  81. package/src/routes/index.tsx +22 -115
  82. package/src/routes/links.tsx +928 -0
  83. package/src/routes/mentions.tsx +22 -115
  84. package/src/styles.css +169 -43
  85. package/vite.config.ts +8 -0
@@ -0,0 +1,399 @@
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
+ },
132
+ },
133
+ );
134
+
135
+ const deadline = Date.now() + 30_000;
136
+ while (Date.now() < deadline) {
137
+ if (await isReachable(baseUrl)) {
138
+ return child;
139
+ }
140
+ await sleep(250);
141
+ }
142
+
143
+ child.kill("SIGTERM");
144
+ throw new Error(`Timed out waiting for ${baseUrl}`);
145
+ }
146
+
147
+ async function waitForAny(page, selectors) {
148
+ await page.waitForFunction(
149
+ (values) =>
150
+ values.some((selector) => {
151
+ if (selector.startsWith("text=")) {
152
+ return document.body.innerText.includes(selector.slice(5));
153
+ }
154
+ return Boolean(document.querySelector(selector));
155
+ }),
156
+ selectors,
157
+ { timeout: READY_TIMEOUT_MS },
158
+ );
159
+ }
160
+
161
+ function percentile(values, point) {
162
+ if (values.length === 0) return 0;
163
+ const sorted = [...values].sort((left, right) => left - right);
164
+ const index = Math.min(
165
+ sorted.length - 1,
166
+ Math.ceil(sorted.length * point) - 1,
167
+ );
168
+ return sorted[index] ?? 0;
169
+ }
170
+
171
+ function median(values) {
172
+ return percentile(values, 0.5);
173
+ }
174
+
175
+ function round(value) {
176
+ return Math.round(value);
177
+ }
178
+
179
+ function apiBucket(rawUrl) {
180
+ const url = new URL(rawUrl);
181
+ if (url.pathname === "/api/link-insights") {
182
+ return `${url.pathname}:${url.searchParams.get("kind") ?? "unknown"}`;
183
+ }
184
+ return url.pathname;
185
+ }
186
+
187
+ function summarizeRuns(runs) {
188
+ const ready = runs.map((run) => run.readyMs);
189
+ const action = runs
190
+ .map((run) => run.actionMs)
191
+ .filter((value) => typeof value === "number");
192
+ const apiDurations = runs.flatMap((run) => run.apiDurationsMs);
193
+ return {
194
+ readyMedianMs: round(median(ready)),
195
+ readyP95Ms: round(percentile(ready, 0.95)),
196
+ actionMedianMs: action.length > 0 ? round(median(action)) : null,
197
+ actionP95Ms: action.length > 0 ? round(percentile(action, 0.95)) : null,
198
+ apiP95Ms: round(percentile(apiDurations, 0.95)),
199
+ apiCallsMedian: round(median(runs.map((run) => run.apiCalls))),
200
+ previewCallsMedian: round(median(runs.map((run) => run.previewCalls))),
201
+ rowsMedian: round(median(runs.map((run) => run.rows))),
202
+ previewsMedian: round(median(runs.map((run) => run.previews))),
203
+ endpoints: summarizeEndpoints(runs),
204
+ };
205
+ }
206
+
207
+ function summarizeEndpoints(runs) {
208
+ const totals = new Map();
209
+ for (const run of runs) {
210
+ for (const [endpoint, count] of Object.entries(run.apiEndpoints)) {
211
+ totals.set(endpoint, (totals.get(endpoint) ?? 0) + count);
212
+ }
213
+ }
214
+ return [...totals.entries()]
215
+ .map(([endpoint, count]) => ({
216
+ endpoint,
217
+ medianCalls: Math.round(count / runs.length),
218
+ }))
219
+ .sort((left, right) => right.medianCalls - left.medianCalls)
220
+ .slice(0, 8);
221
+ }
222
+
223
+ async function runScenario(browser, baseUrl, name) {
224
+ const scenario = SCENARIOS[name];
225
+ const page = await browser.newPage({
226
+ viewport: { width: 1474, height: 910 },
227
+ });
228
+ const requestStart = new Map();
229
+ const apiDurationsMs = [];
230
+ let apiCalls = 0;
231
+ let previewCalls = 0;
232
+ const apiEndpoints = {};
233
+
234
+ page.on("request", (request) => {
235
+ if (request.url().includes("/api/")) {
236
+ requestStart.set(request, performance.now());
237
+ }
238
+ });
239
+ page.on("response", (response) => {
240
+ const request = response.request();
241
+ const startedAt = requestStart.get(request);
242
+ if (startedAt === undefined) return;
243
+ requestStart.delete(request);
244
+ apiCalls += 1;
245
+ const endpoint = apiBucket(response.url());
246
+ apiEndpoints[endpoint] = (apiEndpoints[endpoint] ?? 0) + 1;
247
+ apiDurationsMs.push(performance.now() - startedAt);
248
+ if (response.url().includes("/api/link-preview")) {
249
+ previewCalls += 1;
250
+ }
251
+ });
252
+
253
+ const startedAt = performance.now();
254
+ await page.goto(`${baseUrl}${scenario.path}`, {
255
+ waitUntil: "domcontentloaded",
256
+ });
257
+ await scenario.ready(page);
258
+ const readyMs = performance.now() - startedAt;
259
+
260
+ let actionMs = null;
261
+ if (scenario.action) {
262
+ const actionStartedAt = performance.now();
263
+ await scenario.action(page);
264
+ actionMs = performance.now() - actionStartedAt;
265
+ }
266
+
267
+ await page.waitForTimeout(250);
268
+ const counts = await page.evaluate(() => ({
269
+ rows: document.querySelectorAll(
270
+ '[data-perf="timeline-card"], [data-perf="link-insight-row"]',
271
+ ).length,
272
+ previews: document.querySelectorAll('[data-perf="link-preview-card"]')
273
+ .length,
274
+ }));
275
+ await page.close();
276
+
277
+ return {
278
+ readyMs,
279
+ actionMs,
280
+ apiCalls,
281
+ previewCalls,
282
+ apiEndpoints,
283
+ apiDurationsMs,
284
+ rows: counts.rows,
285
+ previews: counts.previews,
286
+ };
287
+ }
288
+
289
+ function budgetFailures(name, summary, budgets) {
290
+ const failures = [];
291
+ if (
292
+ Number.isFinite(budgets.readyMs) &&
293
+ summary.readyP95Ms > budgets.readyMs
294
+ ) {
295
+ failures.push(
296
+ `${name} ready p95 ${summary.readyP95Ms}ms > ${budgets.readyMs}ms`,
297
+ );
298
+ }
299
+ if (
300
+ Number.isFinite(budgets.actionMs) &&
301
+ summary.actionP95Ms !== null &&
302
+ summary.actionP95Ms > budgets.actionMs
303
+ ) {
304
+ failures.push(
305
+ `${name} action p95 ${summary.actionP95Ms}ms > ${budgets.actionMs}ms`,
306
+ );
307
+ }
308
+ if (
309
+ Number.isFinite(budgets.apiP95Ms) &&
310
+ summary.apiP95Ms > budgets.apiP95Ms
311
+ ) {
312
+ failures.push(
313
+ `${name} api p95 ${summary.apiP95Ms}ms > ${budgets.apiP95Ms}ms`,
314
+ );
315
+ }
316
+ if (
317
+ Number.isFinite(budgets.previewCalls) &&
318
+ summary.previewCallsMedian > budgets.previewCalls
319
+ ) {
320
+ failures.push(
321
+ `${name} preview calls median ${summary.previewCallsMedian} > ${budgets.previewCalls}`,
322
+ );
323
+ }
324
+ return failures;
325
+ }
326
+
327
+ function printHuman(results, failures) {
328
+ console.log("browser perf");
329
+ for (const result of results) {
330
+ const summary = result.summary;
331
+ console.log("");
332
+ console.log(`scenario: ${result.name}`);
333
+ console.log(`ready median: ${summary.readyMedianMs}ms`);
334
+ console.log(`ready p95: ${summary.readyP95Ms}ms`);
335
+ if (summary.actionMedianMs !== null) {
336
+ console.log(`action median: ${summary.actionMedianMs}ms`);
337
+ console.log(`action p95: ${summary.actionP95Ms}ms`);
338
+ }
339
+ console.log(`api p95: ${summary.apiP95Ms}ms`);
340
+ console.log(`api calls median: ${summary.apiCallsMedian}`);
341
+ console.log(`preview calls median: ${summary.previewCallsMedian}`);
342
+ console.log(`rows median: ${summary.rowsMedian}`);
343
+ console.log(`preview cards median: ${summary.previewsMedian}`);
344
+ if (summary.endpoints.length > 0) {
345
+ console.log("top endpoints:");
346
+ for (const endpoint of summary.endpoints) {
347
+ console.log(`- ${endpoint.endpoint}: ${endpoint.medianCalls}`);
348
+ }
349
+ }
350
+ }
351
+ if (failures.length > 0) {
352
+ console.log("");
353
+ console.log("budget failures:");
354
+ for (const failure of failures) {
355
+ console.log(`- ${failure}`);
356
+ }
357
+ }
358
+ }
359
+
360
+ async function main() {
361
+ const options = parseArgs(process.argv.slice(2));
362
+ const server = await startServerIfNeeded(options.baseUrl);
363
+ const browser = await chromium.launch({ headless: true });
364
+ const results = [];
365
+ const failures = [];
366
+
367
+ try {
368
+ for (const name of options.scenarios) {
369
+ await runScenario(browser, options.baseUrl, name);
370
+ const runs = [];
371
+ for (let index = 0; index < options.iterations; index += 1) {
372
+ runs.push(await runScenario(browser, options.baseUrl, name));
373
+ }
374
+ const summary = summarizeRuns(runs);
375
+ results.push({ name, summary, runs });
376
+ failures.push(...budgetFailures(name, summary, options.budgets));
377
+ }
378
+ } finally {
379
+ await browser.close();
380
+ if (server) {
381
+ server.kill("SIGTERM");
382
+ }
383
+ }
384
+
385
+ if (options.json) {
386
+ console.log(JSON.stringify({ results, failures }, null, 2));
387
+ } else {
388
+ printHuman(results, failures);
389
+ }
390
+
391
+ if (failures.length > 0) {
392
+ process.exitCode = 1;
393
+ }
394
+ }
395
+
396
+ main().catch((error) => {
397
+ console.error(error instanceof Error ? error.stack || error.message : error);
398
+ process.exit(1);
399
+ });