dp-worker 0.1.23 → 0.1.25

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 (120) hide show
  1. package/dist/bird/ops.js +44 -0
  2. package/dist/bird/ops.js.map +1 -1
  3. package/dist/bird/sidecar-entry.js +16 -2
  4. package/dist/bird/sidecar-entry.js.map +1 -1
  5. package/dist/chrome-extension/omni-flow/manifest.json +1 -1
  6. package/node_modules/dp-shared/dist/bird-contracts.d.ts +308 -0
  7. package/node_modules/dp-shared/dist/bird-contracts.d.ts.map +1 -1
  8. package/node_modules/dp-shared/dist/bird-contracts.js +228 -0
  9. package/node_modules/dp-shared/dist/bird-contracts.js.map +1 -1
  10. package/node_modules/dp-shared/dist/post-drafts-contracts.d.ts +3 -3
  11. package/package.json +2 -2
  12. package/vendor/bird/LICENSE +21 -0
  13. package/vendor/bird/README.md +58 -0
  14. package/vendor/bird/dist/cli/pagination.d.ts +35 -0
  15. package/vendor/bird/dist/cli/pagination.js +43 -0
  16. package/vendor/bird/dist/cli/program.d.ts +5 -0
  17. package/vendor/bird/dist/cli/program.js +113 -0
  18. package/vendor/bird/dist/cli/shared.d.ts +77 -0
  19. package/vendor/bird/dist/cli/shared.js +327 -0
  20. package/vendor/bird/dist/cli.d.ts +12 -0
  21. package/vendor/bird/dist/cli.js +29 -0
  22. package/vendor/bird/dist/commands/bookmarks.d.ts +4 -0
  23. package/vendor/bird/dist/commands/bookmarks.js +189 -0
  24. package/vendor/bird/dist/commands/check.d.ts +4 -0
  25. package/vendor/bird/dist/commands/check.js +43 -0
  26. package/vendor/bird/dist/commands/follow.d.ts +4 -0
  27. package/vendor/bird/dist/commands/follow.js +91 -0
  28. package/vendor/bird/dist/commands/help.d.ts +4 -0
  29. package/vendor/bird/dist/commands/help.js +19 -0
  30. package/vendor/bird/dist/commands/home.d.ts +4 -0
  31. package/vendor/bird/dist/commands/home.js +43 -0
  32. package/vendor/bird/dist/commands/lists.d.ts +4 -0
  33. package/vendor/bird/dist/commands/lists.js +125 -0
  34. package/vendor/bird/dist/commands/news.d.ts +4 -0
  35. package/vendor/bird/dist/commands/news.js +131 -0
  36. package/vendor/bird/dist/commands/post.d.ts +4 -0
  37. package/vendor/bird/dist/commands/post.js +101 -0
  38. package/vendor/bird/dist/commands/query-ids.d.ts +4 -0
  39. package/vendor/bird/dist/commands/query-ids.js +80 -0
  40. package/vendor/bird/dist/commands/read.d.ts +4 -0
  41. package/vendor/bird/dist/commands/read.js +152 -0
  42. package/vendor/bird/dist/commands/search.d.ts +4 -0
  43. package/vendor/bird/dist/commands/search.js +115 -0
  44. package/vendor/bird/dist/commands/unbookmark.d.ts +4 -0
  45. package/vendor/bird/dist/commands/unbookmark.js +36 -0
  46. package/vendor/bird/dist/commands/user-tweets.d.ts +4 -0
  47. package/vendor/bird/dist/commands/user-tweets.js +109 -0
  48. package/vendor/bird/dist/commands/users.d.ts +4 -0
  49. package/vendor/bird/dist/commands/users.js +295 -0
  50. package/vendor/bird/dist/index.d.ts +2 -0
  51. package/vendor/bird/dist/index.js +2 -0
  52. package/vendor/bird/dist/lib/cli-args.d.ts +7 -0
  53. package/vendor/bird/dist/lib/cli-args.js +25 -0
  54. package/vendor/bird/dist/lib/cookies.d.ts +31 -0
  55. package/vendor/bird/dist/lib/cookies.js +81 -0
  56. package/vendor/bird/dist/lib/extract-bookmark-folder-id.d.ts +2 -0
  57. package/vendor/bird/dist/lib/extract-bookmark-folder-id.js +20 -0
  58. package/vendor/bird/dist/lib/extract-list-id.d.ts +2 -0
  59. package/vendor/bird/dist/lib/extract-list-id.js +19 -0
  60. package/vendor/bird/dist/lib/extract-tweet-id.d.ts +2 -0
  61. package/vendor/bird/dist/lib/extract-tweet-id.js +14 -0
  62. package/vendor/bird/dist/lib/features.json +17 -0
  63. package/vendor/bird/dist/lib/index.d.ts +10 -0
  64. package/vendor/bird/dist/lib/index.js +4 -0
  65. package/vendor/bird/dist/lib/normalize-handle.d.ts +6 -0
  66. package/vendor/bird/dist/lib/normalize-handle.js +31 -0
  67. package/vendor/bird/dist/lib/output.d.ts +29 -0
  68. package/vendor/bird/dist/lib/output.js +88 -0
  69. package/vendor/bird/dist/lib/paginate-cursor.d.ts +27 -0
  70. package/vendor/bird/dist/lib/paginate-cursor.js +37 -0
  71. package/vendor/bird/dist/lib/query-ids.json +20 -0
  72. package/vendor/bird/dist/lib/runtime-features.d.ts +19 -0
  73. package/vendor/bird/dist/lib/runtime-features.js +151 -0
  74. package/vendor/bird/dist/lib/runtime-query-ids.d.ts +33 -0
  75. package/vendor/bird/dist/lib/runtime-query-ids.js +264 -0
  76. package/vendor/bird/dist/lib/thread-filters.d.ts +8 -0
  77. package/vendor/bird/dist/lib/thread-filters.js +124 -0
  78. package/vendor/bird/dist/lib/twitter-client-base.d.ts +38 -0
  79. package/vendor/bird/dist/lib/twitter-client-base.js +129 -0
  80. package/vendor/bird/dist/lib/twitter-client-bookmarks.d.ts +7 -0
  81. package/vendor/bird/dist/lib/twitter-client-bookmarks.js +61 -0
  82. package/vendor/bird/dist/lib/twitter-client-constants.d.ts +43 -0
  83. package/vendor/bird/dist/lib/twitter-client-constants.js +50 -0
  84. package/vendor/bird/dist/lib/twitter-client-engagement.d.ts +16 -0
  85. package/vendor/bird/dist/lib/twitter-client-engagement.js +81 -0
  86. package/vendor/bird/dist/lib/twitter-client-features.d.ts +14 -0
  87. package/vendor/bird/dist/lib/twitter-client-features.js +347 -0
  88. package/vendor/bird/dist/lib/twitter-client-follow.d.ts +8 -0
  89. package/vendor/bird/dist/lib/twitter-client-follow.js +178 -0
  90. package/vendor/bird/dist/lib/twitter-client-home.d.ts +13 -0
  91. package/vendor/bird/dist/lib/twitter-client-home.js +138 -0
  92. package/vendor/bird/dist/lib/twitter-client-lists.d.ts +11 -0
  93. package/vendor/bird/dist/lib/twitter-client-lists.js +321 -0
  94. package/vendor/bird/dist/lib/twitter-client-media.d.ts +11 -0
  95. package/vendor/bird/dist/lib/twitter-client-media.js +136 -0
  96. package/vendor/bird/dist/lib/twitter-client-news.d.ts +47 -0
  97. package/vendor/bird/dist/lib/twitter-client-news.js +283 -0
  98. package/vendor/bird/dist/lib/twitter-client-posting.d.ts +8 -0
  99. package/vendor/bird/dist/lib/twitter-client-posting.js +218 -0
  100. package/vendor/bird/dist/lib/twitter-client-search.d.ts +19 -0
  101. package/vendor/bird/dist/lib/twitter-client-search.js +157 -0
  102. package/vendor/bird/dist/lib/twitter-client-timelines.d.ts +23 -0
  103. package/vendor/bird/dist/lib/twitter-client-timelines.js +471 -0
  104. package/vendor/bird/dist/lib/twitter-client-tweet-detail.d.ts +25 -0
  105. package/vendor/bird/dist/lib/twitter-client-tweet-detail.js +295 -0
  106. package/vendor/bird/dist/lib/twitter-client-types.d.ts +407 -0
  107. package/vendor/bird/dist/lib/twitter-client-types.js +2 -0
  108. package/vendor/bird/dist/lib/twitter-client-user-lookup.d.ts +16 -0
  109. package/vendor/bird/dist/lib/twitter-client-user-lookup.js +224 -0
  110. package/vendor/bird/dist/lib/twitter-client-user-tweets.d.ts +22 -0
  111. package/vendor/bird/dist/lib/twitter-client-user-tweets.js +154 -0
  112. package/vendor/bird/dist/lib/twitter-client-users.d.ts +9 -0
  113. package/vendor/bird/dist/lib/twitter-client-users.js +358 -0
  114. package/vendor/bird/dist/lib/twitter-client-utils.d.ts +173 -0
  115. package/vendor/bird/dist/lib/twitter-client-utils.js +511 -0
  116. package/vendor/bird/dist/lib/twitter-client.d.ts +23 -0
  117. package/vendor/bird/dist/lib/twitter-client.js +21 -0
  118. package/vendor/bird/dist/lib/version.d.ts +6 -0
  119. package/vendor/bird/dist/lib/version.js +174 -0
  120. package/vendor/bird/upstream-package.json +61 -0
@@ -0,0 +1,471 @@
1
+ import { TWITTER_API_BASE } from './twitter-client-constants.js';
2
+ import { buildBookmarksFeatures, buildLikesFeatures } from './twitter-client-features.js';
3
+ import { extractCursorFromInstructions, parseTweetsFromInstructions } from './twitter-client-utils.js';
4
+ export function withTimelines(Base) {
5
+ class TwitterClientTimelines extends Base {
6
+ // biome-ignore lint/complexity/noUselessConstructor lint/suspicious/noExplicitAny: TS mixin constructor requirement.
7
+ constructor(...args) {
8
+ super(...args);
9
+ }
10
+ logBookmarksDebug(message, data) {
11
+ if (process.env.BIRD_DEBUG_BOOKMARKS !== '1') {
12
+ return;
13
+ }
14
+ if (data) {
15
+ console.error(`[bird][debug][bookmarks] ${message}`, JSON.stringify(data));
16
+ }
17
+ else {
18
+ console.error(`[bird][debug][bookmarks] ${message}`);
19
+ }
20
+ }
21
+ async getBookmarksQueryIds() {
22
+ const primary = await this.getQueryId('Bookmarks');
23
+ return Array.from(new Set([primary, 'RV1g3b8n_SGOHwkqKYSCFw', 'tmd4ifV8RHltzn8ymGg1aw']));
24
+ }
25
+ async getBookmarkFolderQueryIds() {
26
+ const primary = await this.getQueryId('BookmarkFolderTimeline');
27
+ return Array.from(new Set([primary, 'KJIQpsvxrTfRIlbaRIySHQ']));
28
+ }
29
+ async getLikesQueryIds() {
30
+ const primary = await this.getQueryId('Likes');
31
+ return Array.from(new Set([primary, 'JR2gceKucIKcVNB_9JkhsA']));
32
+ }
33
+ /**
34
+ * Get the authenticated user's bookmarks
35
+ */
36
+ async getBookmarks(count = 20, options = {}) {
37
+ return this.getBookmarksPaged(count, options);
38
+ }
39
+ async getAllBookmarks(options) {
40
+ return this.getBookmarksPaged(Number.POSITIVE_INFINITY, options);
41
+ }
42
+ /**
43
+ * Get the authenticated user's liked tweets
44
+ */
45
+ async getLikes(count = 20, options = {}) {
46
+ return this.getLikesPaged(count, options);
47
+ }
48
+ async getAllLikes(options) {
49
+ return this.getLikesPaged(Number.POSITIVE_INFINITY, options);
50
+ }
51
+ async getLikesPaged(limit, options = {}) {
52
+ const userResult = await this.getCurrentUser();
53
+ if (!userResult.success || !userResult.user) {
54
+ return { success: false, error: userResult.error ?? 'Could not determine current user' };
55
+ }
56
+ const userId = userResult.user.id;
57
+ const features = buildLikesFeatures();
58
+ const pageSize = 20;
59
+ const seen = new Set();
60
+ const tweets = [];
61
+ let cursor = options.cursor;
62
+ let nextCursor;
63
+ let pagesFetched = 0;
64
+ const { includeRaw = false, maxPages } = options;
65
+ const fetchPage = async (pageCount, pageCursor) => {
66
+ let lastError;
67
+ let had404 = false;
68
+ const queryIds = await this.getLikesQueryIds();
69
+ for (const queryId of queryIds) {
70
+ const variables = {
71
+ userId,
72
+ count: pageCount,
73
+ includePromotedContent: false,
74
+ withClientEventToken: false,
75
+ withBirdwatchNotes: false,
76
+ withVoice: true,
77
+ ...(pageCursor ? { cursor: pageCursor } : {}),
78
+ };
79
+ const params = new URLSearchParams({
80
+ variables: JSON.stringify(variables),
81
+ features: JSON.stringify(features),
82
+ });
83
+ const url = `${TWITTER_API_BASE}/${queryId}/Likes?${params.toString()}`;
84
+ try {
85
+ const response = await this.fetchWithTimeout(url, {
86
+ method: 'GET',
87
+ headers: this.getHeaders(),
88
+ });
89
+ if (response.status === 404) {
90
+ had404 = true;
91
+ lastError = `HTTP ${response.status}`;
92
+ continue;
93
+ }
94
+ if (!response.ok) {
95
+ const text = await response.text();
96
+ return { success: false, error: `HTTP ${response.status}: ${text.slice(0, 200)}`, had404 };
97
+ }
98
+ const data = (await response.json());
99
+ const instructions = data.data?.user?.result?.timeline?.timeline?.instructions;
100
+ if (data.errors && data.errors.length > 0) {
101
+ const message = data.errors.map((e) => e.message).join(', ');
102
+ if (!instructions) {
103
+ if (message.includes('Query: Unspecified')) {
104
+ lastError = message;
105
+ continue;
106
+ }
107
+ return { success: false, error: message, had404 };
108
+ }
109
+ }
110
+ const pageTweets = parseTweetsFromInstructions(instructions, { quoteDepth: this.quoteDepth, includeRaw });
111
+ const extractedCursor = extractCursorFromInstructions(instructions);
112
+ return { success: true, tweets: pageTweets, cursor: extractedCursor, had404 };
113
+ }
114
+ catch (error) {
115
+ lastError = error instanceof Error ? error.message : String(error);
116
+ }
117
+ }
118
+ return { success: false, error: lastError ?? 'Unknown error fetching likes', had404 };
119
+ };
120
+ const fetchWithRefresh = async (pageCount, pageCursor) => {
121
+ const firstAttempt = await fetchPage(pageCount, pageCursor);
122
+ if (firstAttempt.success) {
123
+ return firstAttempt;
124
+ }
125
+ const shouldRefresh = firstAttempt.had404 ||
126
+ (typeof firstAttempt.error === 'string' && firstAttempt.error.includes('Query: Unspecified'));
127
+ if (shouldRefresh) {
128
+ await this.refreshQueryIds();
129
+ const secondAttempt = await fetchPage(pageCount, pageCursor);
130
+ if (secondAttempt.success) {
131
+ return secondAttempt;
132
+ }
133
+ return { success: false, error: secondAttempt.error };
134
+ }
135
+ return { success: false, error: firstAttempt.error };
136
+ };
137
+ const unlimited = limit === Number.POSITIVE_INFINITY;
138
+ while (unlimited || tweets.length < limit) {
139
+ const pageCount = unlimited ? pageSize : Math.min(pageSize, limit - tweets.length);
140
+ const page = await fetchWithRefresh(pageCount, cursor);
141
+ if (!page.success) {
142
+ return { success: false, error: page.error };
143
+ }
144
+ pagesFetched += 1;
145
+ let added = 0;
146
+ for (const tweet of page.tweets) {
147
+ if (seen.has(tweet.id)) {
148
+ continue;
149
+ }
150
+ seen.add(tweet.id);
151
+ tweets.push(tweet);
152
+ added += 1;
153
+ if (!unlimited && tweets.length >= limit) {
154
+ break;
155
+ }
156
+ }
157
+ const pageCursor = page.cursor;
158
+ if (!pageCursor || pageCursor === cursor || page.tweets.length === 0 || added === 0) {
159
+ nextCursor = undefined;
160
+ break;
161
+ }
162
+ if (maxPages && pagesFetched >= maxPages) {
163
+ nextCursor = pageCursor;
164
+ break;
165
+ }
166
+ cursor = pageCursor;
167
+ nextCursor = pageCursor;
168
+ }
169
+ return { success: true, tweets, nextCursor };
170
+ }
171
+ /**
172
+ * Get the authenticated user's bookmark folder timeline
173
+ */
174
+ async getBookmarkFolderTimeline(folderId, count = 20, options = {}) {
175
+ return this.getBookmarkFolderTimelinePaged(folderId, count, options);
176
+ }
177
+ async getAllBookmarkFolderTimeline(folderId, options) {
178
+ return this.getBookmarkFolderTimelinePaged(folderId, Number.POSITIVE_INFINITY, options);
179
+ }
180
+ async getBookmarksPaged(limit, options = {}) {
181
+ const features = buildBookmarksFeatures();
182
+ const pageSize = 20;
183
+ const seen = new Set();
184
+ const tweets = [];
185
+ let cursor = options.cursor;
186
+ let nextCursor;
187
+ let pagesFetched = 0;
188
+ const { includeRaw = false, maxPages } = options;
189
+ const fetchPage = async (pageCount, pageCursor) => {
190
+ let lastError;
191
+ let had404 = false;
192
+ const queryIds = await this.getBookmarksQueryIds();
193
+ const variables = {
194
+ count: pageCount,
195
+ includePromotedContent: false,
196
+ withDownvotePerspective: false,
197
+ withReactionsMetadata: false,
198
+ withReactionsPerspective: false,
199
+ ...(pageCursor ? { cursor: pageCursor } : {}),
200
+ };
201
+ const params = new URLSearchParams({
202
+ variables: JSON.stringify(variables),
203
+ features: JSON.stringify(features),
204
+ });
205
+ for (const queryId of queryIds) {
206
+ const url = `${TWITTER_API_BASE}/${queryId}/Bookmarks?${params.toString()}`;
207
+ try {
208
+ this.logBookmarksDebug('request bookmarks page', {
209
+ queryId,
210
+ pageCount,
211
+ hasCursor: Boolean(pageCursor),
212
+ });
213
+ const response = await this.fetchWithRetry(url, {
214
+ method: 'GET',
215
+ headers: this.getHeaders(),
216
+ });
217
+ if (response.status === 404) {
218
+ had404 = true;
219
+ lastError = `HTTP ${response.status}`;
220
+ this.logBookmarksDebug('bookmarks 404', { queryId });
221
+ continue;
222
+ }
223
+ if (!response.ok) {
224
+ const text = await response.text();
225
+ this.logBookmarksDebug('bookmarks non-200', {
226
+ queryId,
227
+ status: response.status,
228
+ body: text.slice(0, 200),
229
+ });
230
+ return { success: false, error: `HTTP ${response.status}: ${text.slice(0, 200)}`, had404 };
231
+ }
232
+ const data = (await response.json());
233
+ const instructions = data.data?.bookmark_timeline_v2?.timeline?.instructions;
234
+ const pageTweets = parseTweetsFromInstructions(instructions, { quoteDepth: this.quoteDepth, includeRaw });
235
+ const nextCursor = extractCursorFromInstructions(instructions);
236
+ if (data.errors && data.errors.length > 0) {
237
+ this.logBookmarksDebug('bookmarks graphql errors (non-fatal)', { queryId, errors: data.errors });
238
+ if (!instructions) {
239
+ lastError = data.errors.map((e) => e.message).join(', ');
240
+ continue;
241
+ }
242
+ }
243
+ this.logBookmarksDebug('bookmarks page parsed', {
244
+ queryId,
245
+ tweets: pageTweets.length,
246
+ hasNextCursor: Boolean(nextCursor),
247
+ });
248
+ return { success: true, tweets: pageTweets, cursor: nextCursor, had404 };
249
+ }
250
+ catch (error) {
251
+ lastError = error instanceof Error ? error.message : String(error);
252
+ this.logBookmarksDebug('bookmarks request error', { queryId, error: lastError });
253
+ }
254
+ }
255
+ return { success: false, error: lastError ?? 'Unknown error fetching bookmarks', had404 };
256
+ };
257
+ const fetchWithRefresh = async (pageCount, pageCursor) => {
258
+ const firstAttempt = await fetchPage(pageCount, pageCursor);
259
+ if (firstAttempt.success) {
260
+ return firstAttempt;
261
+ }
262
+ if (firstAttempt.had404) {
263
+ await this.refreshQueryIds();
264
+ const secondAttempt = await fetchPage(pageCount, pageCursor);
265
+ if (secondAttempt.success) {
266
+ return secondAttempt;
267
+ }
268
+ return { success: false, error: secondAttempt.error };
269
+ }
270
+ return { success: false, error: firstAttempt.error };
271
+ };
272
+ const unlimited = limit === Number.POSITIVE_INFINITY;
273
+ while (unlimited || tweets.length < limit) {
274
+ const pageCount = unlimited ? pageSize : Math.min(pageSize, limit - tweets.length);
275
+ const page = await fetchWithRefresh(pageCount, cursor);
276
+ if (!page.success) {
277
+ return { success: false, error: page.error };
278
+ }
279
+ pagesFetched += 1;
280
+ let added = 0;
281
+ for (const tweet of page.tweets) {
282
+ if (seen.has(tweet.id)) {
283
+ continue;
284
+ }
285
+ seen.add(tweet.id);
286
+ tweets.push(tweet);
287
+ added += 1;
288
+ if (!unlimited && tweets.length >= limit) {
289
+ break;
290
+ }
291
+ }
292
+ const pageCursor = page.cursor;
293
+ if (!pageCursor || pageCursor === cursor || page.tweets.length === 0 || added === 0) {
294
+ nextCursor = undefined;
295
+ break;
296
+ }
297
+ if (maxPages && pagesFetched >= maxPages) {
298
+ nextCursor = pageCursor;
299
+ break;
300
+ }
301
+ cursor = pageCursor;
302
+ nextCursor = pageCursor;
303
+ }
304
+ return { success: true, tweets, nextCursor };
305
+ }
306
+ async getBookmarkFolderTimelinePaged(folderId, limit, options = {}) {
307
+ const features = buildBookmarksFeatures();
308
+ const pageSize = 20;
309
+ const seen = new Set();
310
+ const tweets = [];
311
+ let cursor = options.cursor;
312
+ let nextCursor;
313
+ let pagesFetched = 0;
314
+ const { includeRaw = false, maxPages } = options;
315
+ const buildVariables = (pageCount, pageCursor, includeCount) => ({
316
+ bookmark_collection_id: folderId,
317
+ includePromotedContent: true,
318
+ ...(includeCount ? { count: pageCount } : {}),
319
+ ...(pageCursor ? { cursor: pageCursor } : {}),
320
+ });
321
+ const fetchPage = async (pageCount, pageCursor) => {
322
+ let lastError;
323
+ let had404 = false;
324
+ const queryIds = await this.getBookmarkFolderQueryIds();
325
+ const tryOnce = async (variables) => {
326
+ const params = new URLSearchParams({
327
+ variables: JSON.stringify(variables),
328
+ features: JSON.stringify(features),
329
+ });
330
+ for (const queryId of queryIds) {
331
+ const url = `${TWITTER_API_BASE}/${queryId}/BookmarkFolderTimeline?${params.toString()}`;
332
+ try {
333
+ this.logBookmarksDebug('request bookmark folder page', {
334
+ queryId,
335
+ pageCount,
336
+ hasCursor: Boolean(pageCursor),
337
+ includeCount: Object.hasOwn(variables, 'count'),
338
+ });
339
+ const response = await this.fetchWithRetry(url, {
340
+ method: 'GET',
341
+ headers: this.getHeaders(),
342
+ });
343
+ if (response.status === 404) {
344
+ had404 = true;
345
+ lastError = `HTTP ${response.status}`;
346
+ this.logBookmarksDebug('bookmark folder 404', { queryId });
347
+ continue;
348
+ }
349
+ if (!response.ok) {
350
+ const text = await response.text();
351
+ this.logBookmarksDebug('bookmark folder non-200', {
352
+ queryId,
353
+ status: response.status,
354
+ body: text.slice(0, 200),
355
+ });
356
+ return { success: false, error: `HTTP ${response.status}: ${text.slice(0, 200)}`, had404 };
357
+ }
358
+ const data = (await response.json());
359
+ const instructions = data.data?.bookmark_collection_timeline?.timeline?.instructions;
360
+ const pageTweets = parseTweetsFromInstructions(instructions, { quoteDepth: this.quoteDepth, includeRaw });
361
+ const nextCursor = extractCursorFromInstructions(instructions);
362
+ if (data.errors && data.errors.length > 0) {
363
+ this.logBookmarksDebug('bookmark folder graphql errors (non-fatal)', { queryId, errors: data.errors });
364
+ if (!instructions) {
365
+ lastError = data.errors.map((e) => e.message).join(', ');
366
+ continue;
367
+ }
368
+ }
369
+ this.logBookmarksDebug('bookmark folder page parsed', {
370
+ queryId,
371
+ tweets: pageTweets.length,
372
+ hasNextCursor: Boolean(nextCursor),
373
+ });
374
+ return { success: true, tweets: pageTweets, cursor: nextCursor, had404 };
375
+ }
376
+ catch (error) {
377
+ lastError = error instanceof Error ? error.message : String(error);
378
+ this.logBookmarksDebug('bookmark folder request error', { queryId, error: lastError });
379
+ }
380
+ }
381
+ return { success: false, error: lastError ?? 'Unknown error fetching bookmark folder', had404 };
382
+ };
383
+ let attempt = await tryOnce(buildVariables(pageCount, pageCursor, true));
384
+ if (!attempt.success && attempt.error?.includes('Variable "$count"')) {
385
+ attempt = await tryOnce(buildVariables(pageCount, pageCursor, false));
386
+ }
387
+ if (!attempt.success && attempt.error?.includes('Variable "$cursor"') && pageCursor) {
388
+ return {
389
+ success: false,
390
+ error: 'Bookmark folder pagination rejected the cursor parameter',
391
+ had404: attempt.had404,
392
+ };
393
+ }
394
+ return attempt;
395
+ };
396
+ const fetchWithRefresh = async (pageCount, pageCursor) => {
397
+ const firstAttempt = await fetchPage(pageCount, pageCursor);
398
+ if (firstAttempt.success) {
399
+ return firstAttempt;
400
+ }
401
+ if (firstAttempt.had404) {
402
+ await this.refreshQueryIds();
403
+ const secondAttempt = await fetchPage(pageCount, pageCursor);
404
+ if (secondAttempt.success) {
405
+ return secondAttempt;
406
+ }
407
+ return { success: false, error: secondAttempt.error };
408
+ }
409
+ return { success: false, error: firstAttempt.error };
410
+ };
411
+ const unlimited = limit === Number.POSITIVE_INFINITY;
412
+ while (unlimited || tweets.length < limit) {
413
+ const pageCount = unlimited ? pageSize : Math.min(pageSize, limit - tweets.length);
414
+ const page = await fetchWithRefresh(pageCount, cursor);
415
+ if (!page.success) {
416
+ return { success: false, error: page.error };
417
+ }
418
+ pagesFetched += 1;
419
+ let added = 0;
420
+ for (const tweet of page.tweets) {
421
+ if (seen.has(tweet.id)) {
422
+ continue;
423
+ }
424
+ seen.add(tweet.id);
425
+ tweets.push(tweet);
426
+ added += 1;
427
+ if (!unlimited && tweets.length >= limit) {
428
+ break;
429
+ }
430
+ }
431
+ const pageCursor = page.cursor;
432
+ if (!pageCursor || pageCursor === cursor || page.tweets.length === 0 || added === 0) {
433
+ nextCursor = undefined;
434
+ break;
435
+ }
436
+ if (maxPages && pagesFetched >= maxPages) {
437
+ nextCursor = pageCursor;
438
+ break;
439
+ }
440
+ cursor = pageCursor;
441
+ nextCursor = pageCursor;
442
+ }
443
+ return { success: true, tweets, nextCursor };
444
+ }
445
+ async fetchWithRetry(url, init) {
446
+ const maxRetries = 2;
447
+ const baseDelayMs = 500;
448
+ const retryable = new Set([429, 500, 502, 503, 504]);
449
+ for (let attempt = 0; attempt <= maxRetries; attempt += 1) {
450
+ const response = await this.fetchWithTimeout(url, init);
451
+ if (!retryable.has(response.status) || attempt === maxRetries) {
452
+ return response;
453
+ }
454
+ this.logBookmarksDebug('retrying bookmarks request', {
455
+ status: response.status,
456
+ attempt,
457
+ });
458
+ // Retry-After supports delta-seconds only; HTTP-date falls back to backoff.
459
+ const retryAfter = response.headers?.get?.('retry-after');
460
+ const retryAfterMs = retryAfter ? Number.parseInt(retryAfter, 10) * 1000 : Number.NaN;
461
+ const backoffMs = Number.isFinite(retryAfterMs)
462
+ ? retryAfterMs
463
+ : baseDelayMs * 2 ** attempt + Math.floor(Math.random() * baseDelayMs);
464
+ await new Promise((resolve) => setTimeout(resolve, backoffMs));
465
+ }
466
+ return this.fetchWithTimeout(url, init);
467
+ }
468
+ }
469
+ return TwitterClientTimelines;
470
+ }
471
+ //# sourceMappingURL=twitter-client-timelines.js.map
@@ -0,0 +1,25 @@
1
+ import type { AbstractConstructor, Mixin, TwitterClientBase } from './twitter-client-base.js';
2
+ import type { GetTweetResult, SearchResult } from './twitter-client-types.js';
3
+ /** Options for tweet fetching methods */
4
+ export interface TweetFetchOptions {
5
+ /** Include raw GraphQL response in `_raw` field */
6
+ includeRaw?: boolean;
7
+ }
8
+ /** Options for paginated tweet detail fetch */
9
+ export interface TweetDetailPaginationOptions extends TweetFetchOptions {
10
+ /** Maximum number of pages to fetch (default: unlimited when using pagination) */
11
+ maxPages?: number;
12
+ /** Starting cursor for pagination (resume from previous fetch) */
13
+ cursor?: string;
14
+ /** Delay in milliseconds between page fetches (default: 1000) */
15
+ pageDelayMs?: number;
16
+ }
17
+ export interface TwitterClientTweetDetailMethods {
18
+ getTweet(tweetId: string, options?: TweetFetchOptions): Promise<GetTweetResult>;
19
+ getReplies(tweetId: string, options?: TweetFetchOptions): Promise<SearchResult>;
20
+ getThread(tweetId: string, options?: TweetFetchOptions): Promise<SearchResult>;
21
+ getRepliesPaged(tweetId: string, options?: TweetDetailPaginationOptions): Promise<SearchResult>;
22
+ getThreadPaged(tweetId: string, options?: TweetDetailPaginationOptions): Promise<SearchResult>;
23
+ }
24
+ export declare function withTweetDetails<TBase extends AbstractConstructor<TwitterClientBase>>(Base: TBase): Mixin<TBase, TwitterClientTweetDetailMethods>;
25
+ //# sourceMappingURL=twitter-client-tweet-detail.d.ts.map