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,165 @@
1
+ import type { TweetMediaItem, XurlMediaItem } from "./types";
2
+
3
+ interface TweetWithMediaAttachments {
4
+ attachments?: { media_keys?: string[] };
5
+ entities?: Record<string, unknown>;
6
+ }
7
+
8
+ function mediaKeys(tweet: TweetWithMediaAttachments) {
9
+ return Array.isArray(tweet.attachments?.media_keys)
10
+ ? tweet.attachments.media_keys.filter((key) => typeof key === "string")
11
+ : [];
12
+ }
13
+
14
+ export function countTweetMedia(tweet: TweetWithMediaAttachments) {
15
+ const keys = mediaKeys(tweet);
16
+ if (keys.length > 0) {
17
+ return keys.length;
18
+ }
19
+
20
+ const urls = Array.isArray(tweet.entities?.urls) ? tweet.entities.urls : [];
21
+ return urls.filter(
22
+ (url) =>
23
+ url &&
24
+ typeof url === "object" &&
25
+ typeof (url as Record<string, unknown>).media_key === "string",
26
+ ).length;
27
+ }
28
+
29
+ function localType(media: XurlMediaItem): TweetMediaItem["type"] {
30
+ if (media.type === "photo") {
31
+ return "image";
32
+ }
33
+ if (media.type === "animated_gif") {
34
+ return "gif";
35
+ }
36
+ if (media.type === "video") {
37
+ return "video";
38
+ }
39
+ return "unknown";
40
+ }
41
+
42
+ function mp4Variants(
43
+ media: XurlMediaItem,
44
+ ): NonNullable<TweetMediaItem["variants"]> {
45
+ return (media.variants ?? [])
46
+ .filter(
47
+ (variant) =>
48
+ variant.content_type === "video/mp4" && typeof variant.url === "string",
49
+ )
50
+ .map((variant) => ({
51
+ url: variant.url,
52
+ contentType: variant.content_type,
53
+ ...(Number.isFinite(Number(variant.bit_rate))
54
+ ? { bitRate: Number(variant.bit_rate) }
55
+ : {}),
56
+ }))
57
+ .sort(
58
+ (left, right) => Number(right.bitRate ?? 0) - Number(left.bitRate ?? 0),
59
+ );
60
+ }
61
+
62
+ function record(value: unknown) {
63
+ return value && typeof value === "object" && !Array.isArray(value)
64
+ ? (value as Record<string, unknown>)
65
+ : null;
66
+ }
67
+
68
+ function parsedMediaUrl(value: unknown) {
69
+ if (typeof value !== "string" || value.length === 0) return null;
70
+ try {
71
+ const url = new URL(value);
72
+ return url.protocol === "https:" ? url : null;
73
+ } catch {
74
+ return null;
75
+ }
76
+ }
77
+
78
+ function isKnownMediaCdn(url: URL) {
79
+ return url.hostname === "pbs.twimg.com" || url.hostname === "video.twimg.com";
80
+ }
81
+
82
+ function directMediaUrl(value: unknown, trustedField = false) {
83
+ const url = parsedMediaUrl(value);
84
+ if (!url || url.hostname === "t.co") return null;
85
+ return trustedField || isKnownMediaCdn(url) ? url.toString() : null;
86
+ }
87
+
88
+ function entityMediaUrl(value: Record<string, unknown>) {
89
+ for (const key of ["media_url_https", "media_url"]) {
90
+ const url = directMediaUrl(value[key], true);
91
+ if (url) return url;
92
+ }
93
+
94
+ for (const key of ["expanded_url", "url"]) {
95
+ const url = directMediaUrl(value[key]);
96
+ if (url) return url;
97
+ }
98
+
99
+ const images = Array.isArray(value.images) ? value.images : [];
100
+ for (const image of images) {
101
+ const item = record(image);
102
+ const url = directMediaUrl(item?.url);
103
+ if (url) return url;
104
+ }
105
+ return null;
106
+ }
107
+
108
+ function birdEntityMedia(tweet: TweetWithMediaAttachments) {
109
+ const urls = Array.isArray(tweet.entities?.urls) ? tweet.entities.urls : [];
110
+ const seen = new Set<string>();
111
+ const items: TweetMediaItem[] = [];
112
+ for (const url of urls) {
113
+ const item = record(url);
114
+ if (!item || typeof item.media_key !== "string") continue;
115
+ const mediaUrl = entityMediaUrl(item);
116
+ if (!mediaUrl || seen.has(mediaUrl)) continue;
117
+ seen.add(mediaUrl);
118
+ items.push({ url: mediaUrl, type: "image" });
119
+ }
120
+ return items;
121
+ }
122
+
123
+ export function buildMediaJsonFromIncludes(
124
+ tweet: TweetWithMediaAttachments,
125
+ media: XurlMediaItem[] = [],
126
+ ) {
127
+ const byKey = new Map(media.map((item) => [item.media_key, item]));
128
+ const keys = mediaKeys(tweet);
129
+ const items = keys
130
+ .map((key) => byKey.get(key))
131
+ .filter((item): item is XurlMediaItem => item !== undefined)
132
+ .map((item) => {
133
+ const variants = mp4Variants(item);
134
+ const type = localType(item);
135
+ const url =
136
+ type === "image"
137
+ ? (item.url ?? item.preview_image_url ?? "")
138
+ : (item.preview_image_url ?? variants[0]?.url ?? item.url ?? "");
139
+ if (!url) {
140
+ return null;
141
+ }
142
+
143
+ return {
144
+ url,
145
+ type,
146
+ ...(item.alt_text ? { altText: item.alt_text } : {}),
147
+ ...(Number.isFinite(Number(item.width))
148
+ ? { width: Number(item.width) }
149
+ : {}),
150
+ ...(Number.isFinite(Number(item.height))
151
+ ? { height: Number(item.height) }
152
+ : {}),
153
+ ...(item.preview_image_url
154
+ ? { thumbnailUrl: item.preview_image_url }
155
+ : {}),
156
+ ...(Number.isFinite(Number(item.duration_ms))
157
+ ? { durationMs: Number(item.duration_ms) }
158
+ : {}),
159
+ ...(variants.length > 0 ? { variants } : {}),
160
+ } satisfies TweetMediaItem;
161
+ })
162
+ .filter((item): item is TweetMediaItem => item !== null);
163
+
164
+ return JSON.stringify(keys.length > 0 ? items : birdEntityMedia(tweet));
165
+ }