notebooklm-kit 0.0.1 → 2.1.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 (100) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +4102 -0
  3. package/dist/src/auth/auth.d.ts +46 -0
  4. package/dist/src/auth/auth.d.ts.map +1 -0
  5. package/dist/src/auth/auth.js +323 -0
  6. package/dist/src/auth/auth.js.map +1 -0
  7. package/dist/src/auth/refresh.d.ts +150 -0
  8. package/dist/src/auth/refresh.d.ts.map +1 -0
  9. package/dist/src/auth/refresh.js +433 -0
  10. package/dist/src/auth/refresh.js.map +1 -0
  11. package/dist/src/client/notebooklm-client.d.ts +372 -0
  12. package/dist/src/client/notebooklm-client.d.ts.map +1 -0
  13. package/dist/src/client/notebooklm-client.js +550 -0
  14. package/dist/src/client/notebooklm-client.js.map +1 -0
  15. package/dist/src/index.d.ts +50 -0
  16. package/dist/src/index.d.ts.map +1 -0
  17. package/dist/src/index.js +45 -0
  18. package/dist/src/index.js.map +1 -0
  19. package/dist/src/rpc/rpc-client.d.ts +48 -0
  20. package/dist/src/rpc/rpc-client.d.ts.map +1 -0
  21. package/dist/src/rpc/rpc-client.js +94 -0
  22. package/dist/src/rpc/rpc-client.js.map +1 -0
  23. package/dist/src/rpc/rpc-methods.d.ts +127 -0
  24. package/dist/src/rpc/rpc-methods.d.ts.map +1 -0
  25. package/dist/src/rpc/rpc-methods.js +169 -0
  26. package/dist/src/rpc/rpc-methods.js.map +1 -0
  27. package/dist/src/services/artifacts.d.ts +1017 -0
  28. package/dist/src/services/artifacts.d.ts.map +1 -0
  29. package/dist/src/services/artifacts.js +5413 -0
  30. package/dist/src/services/artifacts.js.map +1 -0
  31. package/dist/src/services/generation.d.ts +147 -0
  32. package/dist/src/services/generation.d.ts.map +1 -0
  33. package/dist/src/services/generation.js +479 -0
  34. package/dist/src/services/generation.js.map +1 -0
  35. package/dist/src/services/notebook-language.d.ts +109 -0
  36. package/dist/src/services/notebook-language.d.ts.map +1 -0
  37. package/dist/src/services/notebook-language.js +204 -0
  38. package/dist/src/services/notebook-language.js.map +1 -0
  39. package/dist/src/services/notebooks.d.ts +26 -0
  40. package/dist/src/services/notebooks.d.ts.map +1 -0
  41. package/dist/src/services/notebooks.js +539 -0
  42. package/dist/src/services/notebooks.js.map +1 -0
  43. package/dist/src/services/notes.d.ts +72 -0
  44. package/dist/src/services/notes.d.ts.map +1 -0
  45. package/dist/src/services/notes.js +340 -0
  46. package/dist/src/services/notes.js.map +1 -0
  47. package/dist/src/services/sources.d.ts +1085 -0
  48. package/dist/src/services/sources.d.ts.map +1 -0
  49. package/dist/src/services/sources.js +2675 -0
  50. package/dist/src/services/sources.js.map +1 -0
  51. package/dist/src/types/artifact.d.ts +258 -0
  52. package/dist/src/types/artifact.d.ts.map +1 -0
  53. package/dist/src/types/artifact.js +42 -0
  54. package/dist/src/types/artifact.js.map +1 -0
  55. package/dist/src/types/common.d.ts +226 -0
  56. package/dist/src/types/common.d.ts.map +1 -0
  57. package/dist/src/types/common.js +80 -0
  58. package/dist/src/types/common.js.map +1 -0
  59. package/dist/src/types/languages.d.ts +179 -0
  60. package/dist/src/types/languages.d.ts.map +1 -0
  61. package/dist/src/types/languages.js +254 -0
  62. package/dist/src/types/languages.js.map +1 -0
  63. package/dist/src/types/note.d.ts +41 -0
  64. package/dist/src/types/note.d.ts.map +1 -0
  65. package/dist/src/types/note.js +12 -0
  66. package/dist/src/types/note.js.map +1 -0
  67. package/dist/src/types/notebook.d.ts +81 -0
  68. package/dist/src/types/notebook.d.ts.map +1 -0
  69. package/dist/src/types/notebook.js +5 -0
  70. package/dist/src/types/notebook.js.map +1 -0
  71. package/dist/src/types/source.d.ts +241 -0
  72. package/dist/src/types/source.d.ts.map +1 -0
  73. package/dist/src/types/source.js +60 -0
  74. package/dist/src/types/source.js.map +1 -0
  75. package/dist/src/utils/batch-execute.d.ts +58 -0
  76. package/dist/src/utils/batch-execute.d.ts.map +1 -0
  77. package/dist/src/utils/batch-execute.js +398 -0
  78. package/dist/src/utils/batch-execute.js.map +1 -0
  79. package/dist/src/utils/chunked-decoder.d.ts +11 -0
  80. package/dist/src/utils/chunked-decoder.d.ts.map +1 -0
  81. package/dist/src/utils/chunked-decoder.js +326 -0
  82. package/dist/src/utils/chunked-decoder.js.map +1 -0
  83. package/dist/src/utils/chunked-parser.d.ts +61 -0
  84. package/dist/src/utils/chunked-parser.d.ts.map +1 -0
  85. package/dist/src/utils/chunked-parser.js +609 -0
  86. package/dist/src/utils/chunked-parser.js.map +1 -0
  87. package/dist/src/utils/errors.d.ts +58 -0
  88. package/dist/src/utils/errors.d.ts.map +1 -0
  89. package/dist/src/utils/errors.js +357 -0
  90. package/dist/src/utils/errors.js.map +1 -0
  91. package/dist/src/utils/quota.d.ts +213 -0
  92. package/dist/src/utils/quota.d.ts.map +1 -0
  93. package/dist/src/utils/quota.js +518 -0
  94. package/dist/src/utils/quota.js.map +1 -0
  95. package/dist/src/utils/streaming-client.d.ts +129 -0
  96. package/dist/src/utils/streaming-client.d.ts.map +1 -0
  97. package/dist/src/utils/streaming-client.js +559 -0
  98. package/dist/src/utils/streaming-client.js.map +1 -0
  99. package/package.json +85 -7
  100. package/index.js +0 -2
@@ -0,0 +1,518 @@
1
+ /**
2
+ * Client-side quota management and rate limiting
3
+ * Enforces NotebookLM's usage limits to prevent API errors
4
+ *
5
+ * ALL LIMITS IN ONE PLACE - Update here when NotebookLM changes quotas
6
+ * Reference: https://support.google.com/notebooklm/answer/16213268
7
+ */
8
+ import { APIError, ErrorType } from './errors.js';
9
+ /**
10
+ * Quota limits for each NotebookLM plan
11
+ * Reference: https://support.google.com/notebooklm/answer/16213268
12
+ */
13
+ export const PLAN_LIMITS = {
14
+ standard: {
15
+ notebooks: 100,
16
+ sourcesPerNotebook: 50,
17
+ wordsPerSource: 500000,
18
+ fileSizeMB: 200,
19
+ chatsPerDay: 50,
20
+ audioOverviewsPerDay: 3,
21
+ videoOverviewsPerDay: 3,
22
+ reportsPerDay: 10,
23
+ flashcardsPerDay: 10,
24
+ quizzesPerDay: 10,
25
+ deepResearchPerMonth: 10,
26
+ dataTables: 'limited',
27
+ infographics: 'limited',
28
+ slides: 'limited',
29
+ mindMaps: 'unlimited',
30
+ },
31
+ plus: {
32
+ notebooks: 200,
33
+ sourcesPerNotebook: 100,
34
+ wordsPerSource: 500000,
35
+ fileSizeMB: 200,
36
+ chatsPerDay: 200,
37
+ audioOverviewsPerDay: 6,
38
+ videoOverviewsPerDay: 6,
39
+ reportsPerDay: 20,
40
+ flashcardsPerDay: 20,
41
+ quizzesPerDay: 20,
42
+ deepResearchPerMonth: 90, // 3/day * 30 days
43
+ dataTables: 'more',
44
+ infographics: 'more',
45
+ slides: 'more',
46
+ mindMaps: 'unlimited',
47
+ },
48
+ pro: {
49
+ notebooks: 500,
50
+ sourcesPerNotebook: 300,
51
+ wordsPerSource: 500000,
52
+ fileSizeMB: 200,
53
+ chatsPerDay: 500,
54
+ audioOverviewsPerDay: 20,
55
+ videoOverviewsPerDay: 20,
56
+ reportsPerDay: 100,
57
+ flashcardsPerDay: 100,
58
+ quizzesPerDay: 100,
59
+ deepResearchPerMonth: 600, // 20/day * 30 days
60
+ dataTables: 'higher',
61
+ infographics: 'higher',
62
+ slides: 'higher',
63
+ mindMaps: 'unlimited',
64
+ },
65
+ ultra: {
66
+ notebooks: 500,
67
+ sourcesPerNotebook: 600,
68
+ wordsPerSource: 500000,
69
+ fileSizeMB: 200,
70
+ chatsPerDay: 5000,
71
+ audioOverviewsPerDay: 200,
72
+ videoOverviewsPerDay: 200,
73
+ reportsPerDay: 1000,
74
+ flashcardsPerDay: 1000,
75
+ quizzesPerDay: 1000,
76
+ deepResearchPerMonth: 6000, // 200/day * 30 days
77
+ dataTables: 'highest',
78
+ infographics: 'highest',
79
+ slides: 'highest',
80
+ mindMaps: 'unlimited',
81
+ },
82
+ };
83
+ /**
84
+ * Legacy constant for backward compatibility
85
+ * Uses Standard plan limits
86
+ */
87
+ export const NOTEBOOKLM_LIMITS = {
88
+ /** Per account limits */
89
+ MAX_NOTEBOOKS: PLAN_LIMITS.standard.notebooks,
90
+ MAX_SOURCES_PER_NOTEBOOK: PLAN_LIMITS.standard.sourcesPerNotebook,
91
+ MAX_WORDS_PER_SOURCE: PLAN_LIMITS.standard.wordsPerSource,
92
+ MAX_FILE_SIZE_MB: PLAN_LIMITS.standard.fileSizeMB,
93
+ /** Daily limits (reset after 24 hours) */
94
+ CHATS_PER_DAY: PLAN_LIMITS.standard.chatsPerDay,
95
+ AUDIO_OVERVIEWS_PER_DAY: PLAN_LIMITS.standard.audioOverviewsPerDay,
96
+ VIDEO_OVERVIEWS_PER_DAY: PLAN_LIMITS.standard.videoOverviewsPerDay,
97
+ REPORTS_PER_DAY: PLAN_LIMITS.standard.reportsPerDay,
98
+ FLASHCARDS_PER_DAY: PLAN_LIMITS.standard.flashcardsPerDay,
99
+ QUIZZES_PER_DAY: PLAN_LIMITS.standard.quizzesPerDay,
100
+ /** Monthly limits (reset after 30 days) */
101
+ DEEP_RESEARCH_PER_MONTH: PLAN_LIMITS.standard.deepResearchPerMonth,
102
+ /** No limits (but may be restricted server-side) */
103
+ MIND_MAPS: Infinity, // "No Limits"
104
+ /** Limited (server enforced, no specific client limit) */
105
+ DATA_TABLES: -1, // "Limited" - server enforced
106
+ INFOGRAPHICS: -1, // "Limited" - server enforced
107
+ SLIDES: -1, // "Limited" - server enforced
108
+ };
109
+ /**
110
+ * Rate limit error (uses existing error code 324934)
111
+ */
112
+ export class RateLimitError extends APIError {
113
+ resource;
114
+ used;
115
+ limit;
116
+ resetTime;
117
+ constructor(message, resource, used, limit, resetTime) {
118
+ super(message, {
119
+ code: 324934,
120
+ type: ErrorType.RATE_LIMIT,
121
+ message: 'Rate limit exceeded',
122
+ description: message,
123
+ retryable: true,
124
+ }, 429);
125
+ this.resource = resource;
126
+ this.used = used;
127
+ this.limit = limit;
128
+ this.resetTime = resetTime;
129
+ this.name = 'RateLimitError';
130
+ Object.setPrototypeOf(this, RateLimitError.prototype);
131
+ }
132
+ }
133
+ /**
134
+ * Client-side quota manager
135
+ * Tracks usage and enforces NotebookLM limits before making API calls
136
+ */
137
+ export class QuotaManager {
138
+ usage;
139
+ enabled;
140
+ plan;
141
+ limits;
142
+ storageKey = 'notebooklm-quota';
143
+ constructor(enabled = false, plan = 'standard') {
144
+ this.enabled = enabled;
145
+ this.plan = plan;
146
+ this.limits = PLAN_LIMITS[plan];
147
+ this.usage = this.loadUsage();
148
+ // Check for resets on initialization
149
+ this.checkAndResetDaily();
150
+ this.checkAndResetMonthly();
151
+ }
152
+ /**
153
+ * Set the plan and update limits
154
+ */
155
+ setPlan(plan) {
156
+ this.plan = plan;
157
+ this.limits = PLAN_LIMITS[plan];
158
+ }
159
+ /**
160
+ * Get current plan
161
+ */
162
+ getPlan() {
163
+ return this.plan;
164
+ }
165
+ /**
166
+ * Get current plan limits
167
+ */
168
+ getLimits() {
169
+ return { ...this.limits };
170
+ }
171
+ /**
172
+ * Check if operation is allowed
173
+ * @throws {RateLimitError} if limit exceeded
174
+ */
175
+ checkQuota(operation, notebookId) {
176
+ if (!this.enabled)
177
+ return;
178
+ this.checkAndResetDaily();
179
+ this.checkAndResetMonthly();
180
+ switch (operation) {
181
+ case 'createNotebook':
182
+ if (this.usage.notebooks.total >= this.limits.notebooks) {
183
+ throw new RateLimitError(`Notebook limit exceeded: ${this.usage.notebooks.total}/${this.limits.notebooks} notebooks created (${this.plan} plan)`, 'notebooks', this.usage.notebooks.total, this.limits.notebooks);
184
+ }
185
+ break;
186
+ case 'addSource':
187
+ if (notebookId) {
188
+ const sourceCount = this.usage.notebooks.sources[notebookId] || 0;
189
+ if (sourceCount >= this.limits.sourcesPerNotebook) {
190
+ throw new RateLimitError(`Source limit exceeded for notebook: ${sourceCount}/${this.limits.sourcesPerNotebook} sources (${this.plan} plan)`, 'sources', sourceCount, this.limits.sourcesPerNotebook);
191
+ }
192
+ }
193
+ break;
194
+ case 'chat':
195
+ if (this.usage.daily.chats >= this.limits.chatsPerDay) {
196
+ const resetTime = this.getDailyResetTime();
197
+ throw new RateLimitError(`Daily chat limit exceeded: ${this.usage.daily.chats}/${this.limits.chatsPerDay} chats used today (${this.plan} plan)`, 'chats', this.usage.daily.chats, this.limits.chatsPerDay, resetTime);
198
+ }
199
+ break;
200
+ case 'createAudioOverview':
201
+ if (this.usage.daily.audioOverviews >= this.limits.audioOverviewsPerDay) {
202
+ const resetTime = this.getDailyResetTime();
203
+ throw new RateLimitError(`Daily audio limit exceeded: ${this.usage.daily.audioOverviews}/${this.limits.audioOverviewsPerDay} audio overviews created today (${this.plan} plan)`, 'audioOverviews', this.usage.daily.audioOverviews, this.limits.audioOverviewsPerDay, resetTime);
204
+ }
205
+ break;
206
+ case 'createVideoOverview':
207
+ if (this.usage.daily.videoOverviews >= this.limits.videoOverviewsPerDay) {
208
+ const resetTime = this.getDailyResetTime();
209
+ throw new RateLimitError(`Daily video limit exceeded: ${this.usage.daily.videoOverviews}/${this.limits.videoOverviewsPerDay} video overviews created today (${this.plan} plan)`, 'videoOverviews', this.usage.daily.videoOverviews, this.limits.videoOverviewsPerDay, resetTime);
210
+ }
211
+ break;
212
+ case 'createReport':
213
+ if (this.usage.daily.reports >= this.limits.reportsPerDay) {
214
+ const resetTime = this.getDailyResetTime();
215
+ throw new RateLimitError(`Daily report limit exceeded: ${this.usage.daily.reports}/${this.limits.reportsPerDay} reports created today (${this.plan} plan)`, 'reports', this.usage.daily.reports, this.limits.reportsPerDay, resetTime);
216
+ }
217
+ break;
218
+ case 'createFlashcards':
219
+ if (this.usage.daily.flashcards >= this.limits.flashcardsPerDay) {
220
+ const resetTime = this.getDailyResetTime();
221
+ throw new RateLimitError(`Daily flashcard limit exceeded: ${this.usage.daily.flashcards}/${this.limits.flashcardsPerDay} flashcards created today (${this.plan} plan)`, 'flashcards', this.usage.daily.flashcards, this.limits.flashcardsPerDay, resetTime);
222
+ }
223
+ break;
224
+ case 'createQuiz':
225
+ if (this.usage.daily.quizzes >= this.limits.quizzesPerDay) {
226
+ const resetTime = this.getDailyResetTime();
227
+ throw new RateLimitError(`Daily quiz limit exceeded: ${this.usage.daily.quizzes}/${this.limits.quizzesPerDay} quizzes created today (${this.plan} plan)`, 'quizzes', this.usage.daily.quizzes, this.limits.quizzesPerDay, resetTime);
228
+ }
229
+ break;
230
+ case 'deepResearch':
231
+ if (this.usage.monthly.deepResearch >= this.limits.deepResearchPerMonth) {
232
+ const resetTime = this.getMonthlyResetTime();
233
+ throw new RateLimitError(`Monthly deep research limit exceeded: ${this.usage.monthly.deepResearch}/${this.limits.deepResearchPerMonth} used this month (${this.plan} plan)`, 'deepResearch', this.usage.monthly.deepResearch, this.limits.deepResearchPerMonth, resetTime);
234
+ }
235
+ break;
236
+ }
237
+ }
238
+ /**
239
+ * Record usage after successful operation
240
+ */
241
+ recordUsage(operation, notebookId) {
242
+ if (!this.enabled)
243
+ return;
244
+ switch (operation) {
245
+ case 'createNotebook':
246
+ this.usage.notebooks.total++;
247
+ break;
248
+ case 'addSource':
249
+ if (notebookId) {
250
+ this.usage.notebooks.sources[notebookId] =
251
+ (this.usage.notebooks.sources[notebookId] || 0) + 1;
252
+ }
253
+ break;
254
+ case 'chat':
255
+ this.usage.daily.chats++;
256
+ break;
257
+ case 'createAudioOverview':
258
+ this.usage.daily.audioOverviews++;
259
+ break;
260
+ case 'createVideoOverview':
261
+ this.usage.daily.videoOverviews++;
262
+ break;
263
+ case 'createReport':
264
+ this.usage.daily.reports++;
265
+ break;
266
+ case 'createFlashcards':
267
+ this.usage.daily.flashcards++;
268
+ break;
269
+ case 'createQuiz':
270
+ this.usage.daily.quizzes++;
271
+ break;
272
+ case 'deepResearch':
273
+ this.usage.monthly.deepResearch++;
274
+ break;
275
+ }
276
+ this.saveUsage();
277
+ }
278
+ /**
279
+ * Get current usage statistics
280
+ */
281
+ getUsage() {
282
+ this.checkAndResetDaily();
283
+ this.checkAndResetMonthly();
284
+ return { ...this.usage };
285
+ }
286
+ /**
287
+ * Get remaining quota for a resource
288
+ */
289
+ getRemaining(resource) {
290
+ this.checkAndResetDaily();
291
+ this.checkAndResetMonthly();
292
+ switch (resource) {
293
+ case 'notebooks':
294
+ return Math.max(0, this.limits.notebooks - this.usage.notebooks.total);
295
+ case 'chats':
296
+ return Math.max(0, this.limits.chatsPerDay - this.usage.daily.chats);
297
+ case 'audioOverviews':
298
+ return Math.max(0, this.limits.audioOverviewsPerDay - this.usage.daily.audioOverviews);
299
+ case 'videoOverviews':
300
+ return Math.max(0, this.limits.videoOverviewsPerDay - this.usage.daily.videoOverviews);
301
+ case 'reports':
302
+ return Math.max(0, this.limits.reportsPerDay - this.usage.daily.reports);
303
+ case 'flashcards':
304
+ return Math.max(0, this.limits.flashcardsPerDay - this.usage.daily.flashcards);
305
+ case 'quizzes':
306
+ return Math.max(0, this.limits.quizzesPerDay - this.usage.daily.quizzes);
307
+ case 'deepResearch':
308
+ return Math.max(0, this.limits.deepResearchPerMonth - this.usage.monthly.deepResearch);
309
+ default:
310
+ return Infinity;
311
+ }
312
+ }
313
+ /**
314
+ * Reset all usage (for testing or manual reset)
315
+ */
316
+ resetUsage() {
317
+ this.usage = this.createEmptyUsage();
318
+ this.saveUsage();
319
+ }
320
+ /**
321
+ * Validate text source word count
322
+ * Note: Validation is simple - if too large, NotebookLM will reject it
323
+ */
324
+ validateTextSource(text) {
325
+ if (!this.enabled)
326
+ return;
327
+ const wordCount = this.countWords(text);
328
+ if (wordCount > this.limits.wordsPerSource) {
329
+ throw new APIError(`Text source exceeds ${this.limits.wordsPerSource} word limit: ${wordCount} words (${this.plan} plan)`, {
330
+ code: 6,
331
+ type: ErrorType.INVALID_INPUT,
332
+ message: 'Text too long',
333
+ description: `Source exceeds word limit (${wordCount}/${this.limits.wordsPerSource} words). Note: NotebookLM will reject sources exceeding 500,000 words or 200MB file size.`,
334
+ retryable: false,
335
+ });
336
+ }
337
+ }
338
+ /**
339
+ * Validate file size
340
+ * Note: Validation is simple - if too large, NotebookLM will reject it
341
+ */
342
+ validateFileSize(sizeBytes) {
343
+ if (!this.enabled)
344
+ return;
345
+ const sizeMB = sizeBytes / (1024 * 1024);
346
+ const limitMB = this.limits.fileSizeMB;
347
+ if (sizeMB > limitMB) {
348
+ throw new APIError(`File size exceeds ${limitMB}MB limit: ${sizeMB.toFixed(2)}MB (${this.plan} plan)`, {
349
+ code: 6,
350
+ type: ErrorType.INVALID_INPUT,
351
+ message: 'File too large',
352
+ description: `File exceeds size limit (${sizeMB.toFixed(2)}MB/${limitMB}MB). Note: NotebookLM will reject files exceeding 200MB or sources with more than 500,000 words.`,
353
+ retryable: false,
354
+ });
355
+ }
356
+ }
357
+ /**
358
+ * Count words in text (approximation)
359
+ */
360
+ countWords(text) {
361
+ return text.trim().split(/\s+/).filter(word => word.length > 0).length;
362
+ }
363
+ /**
364
+ * Check and reset daily quotas if 24 hours passed
365
+ */
366
+ checkAndResetDaily() {
367
+ const now = Date.now();
368
+ const lastReset = this.usage.daily.lastReset;
369
+ const hoursSinceReset = (now - lastReset) / (1000 * 60 * 60);
370
+ if (hoursSinceReset >= 24) {
371
+ this.usage.daily = {
372
+ chats: 0,
373
+ audioOverviews: 0,
374
+ videoOverviews: 0,
375
+ reports: 0,
376
+ flashcards: 0,
377
+ quizzes: 0,
378
+ lastReset: now,
379
+ };
380
+ this.saveUsage();
381
+ }
382
+ }
383
+ /**
384
+ * Check and reset monthly quotas if 30 days passed
385
+ */
386
+ checkAndResetMonthly() {
387
+ const now = Date.now();
388
+ const lastReset = this.usage.monthly.lastReset;
389
+ const daysSinceReset = (now - lastReset) / (1000 * 60 * 60 * 24);
390
+ if (daysSinceReset >= 30) {
391
+ this.usage.monthly = {
392
+ deepResearch: 0,
393
+ lastReset: now,
394
+ };
395
+ this.saveUsage();
396
+ }
397
+ }
398
+ /**
399
+ * Get time when daily quota resets
400
+ */
401
+ getDailyResetTime() {
402
+ const lastReset = new Date(this.usage.daily.lastReset);
403
+ return new Date(lastReset.getTime() + 24 * 60 * 60 * 1000);
404
+ }
405
+ /**
406
+ * Get time when monthly quota resets
407
+ */
408
+ getMonthlyResetTime() {
409
+ const lastReset = new Date(this.usage.monthly.lastReset);
410
+ return new Date(lastReset.getTime() + 30 * 24 * 60 * 60 * 1000);
411
+ }
412
+ /**
413
+ * Create empty usage data
414
+ */
415
+ createEmptyUsage() {
416
+ const now = Date.now();
417
+ return {
418
+ daily: {
419
+ chats: 0,
420
+ audioOverviews: 0,
421
+ videoOverviews: 0,
422
+ reports: 0,
423
+ flashcards: 0,
424
+ quizzes: 0,
425
+ lastReset: now,
426
+ },
427
+ monthly: {
428
+ deepResearch: 0,
429
+ lastReset: now,
430
+ },
431
+ notebooks: {
432
+ total: 0,
433
+ sources: {},
434
+ },
435
+ };
436
+ }
437
+ /**
438
+ * Load usage from storage
439
+ */
440
+ loadUsage() {
441
+ try {
442
+ // Try localStorage first (browser)
443
+ if (typeof localStorage !== 'undefined') {
444
+ const stored = localStorage.getItem(this.storageKey);
445
+ if (stored) {
446
+ return JSON.parse(stored);
447
+ }
448
+ }
449
+ }
450
+ catch {
451
+ // Fallback to in-memory
452
+ }
453
+ return this.createEmptyUsage();
454
+ }
455
+ /**
456
+ * Save usage to storage
457
+ */
458
+ saveUsage() {
459
+ try {
460
+ // Try localStorage (browser)
461
+ if (typeof localStorage !== 'undefined') {
462
+ localStorage.setItem(this.storageKey, JSON.stringify(this.usage));
463
+ }
464
+ }
465
+ catch {
466
+ // In-memory only if localStorage not available
467
+ }
468
+ }
469
+ /**
470
+ * Enable or disable quota enforcement
471
+ */
472
+ setEnabled(enabled) {
473
+ this.enabled = enabled;
474
+ }
475
+ /**
476
+ * Check if quota enforcement is enabled
477
+ */
478
+ isEnabled() {
479
+ return this.enabled;
480
+ }
481
+ }
482
+ /**
483
+ * Helper function to validate text source before upload
484
+ * Uses Standard plan limits by default
485
+ * Note: Validation is simple - if too large, NotebookLM will reject it
486
+ */
487
+ export function validateTextSource(text, plan = 'standard') {
488
+ const limits = PLAN_LIMITS[plan];
489
+ const wordCount = text.trim().split(/\s+/).filter(w => w.length > 0).length;
490
+ if (wordCount > limits.wordsPerSource) {
491
+ throw new APIError(`Text source exceeds ${limits.wordsPerSource} word limit: ${wordCount} words (${plan} plan)`, {
492
+ code: 6,
493
+ type: ErrorType.INVALID_INPUT,
494
+ message: 'Text too long',
495
+ description: `Source exceeds word limit (${wordCount}/${limits.wordsPerSource} words). Note: NotebookLM will reject sources exceeding 500,000 words or 200MB file size.`,
496
+ retryable: false,
497
+ });
498
+ }
499
+ }
500
+ /**
501
+ * Helper function to validate file size
502
+ * Uses Standard plan limits by default
503
+ * Note: Validation is simple - if too large, NotebookLM will reject it
504
+ */
505
+ export function validateFileSize(sizeBytes, plan = 'standard') {
506
+ const limits = PLAN_LIMITS[plan];
507
+ const sizeMB = sizeBytes / (1024 * 1024);
508
+ if (sizeMB > limits.fileSizeMB) {
509
+ throw new APIError(`File size exceeds ${limits.fileSizeMB}MB limit: ${sizeMB.toFixed(2)}MB (${plan} plan)`, {
510
+ code: 6,
511
+ type: ErrorType.INVALID_INPUT,
512
+ message: 'File too large',
513
+ description: `File exceeds size limit (${sizeMB.toFixed(2)}MB/${limits.fileSizeMB}MB). Note: NotebookLM will reject files exceeding 200MB or sources with more than 500,000 words.`,
514
+ retryable: false,
515
+ });
516
+ }
517
+ }
518
+ //# sourceMappingURL=quota.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quota.js","sourceRoot":"","sources":["../../../src/utils/quota.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAuClD;;;GAGG;AACH,MAAM,CAAC,MAAM,WAAW,GAAuC;IAC7D,QAAQ,EAAE;QACR,SAAS,EAAE,GAAG;QACd,kBAAkB,EAAE,EAAE;QACtB,cAAc,EAAE,MAAM;QACtB,UAAU,EAAE,GAAG;QACf,WAAW,EAAE,EAAE;QACf,oBAAoB,EAA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AAE,eAAe;YACxB,WAAW,EAAE,8BAA8B,SAAS,IAAI,MAAM,CAAC,cAAc,2FAA2F;YACxK,SAAS,EAAE,KAAK;SACjB,CACF,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,SAAiB,EAAE,OAAuB,UAAU;IACnF,MAAM,MAAM,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IACjC,MAAM,MAAM,GAAG,SAAS,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC;IAEzC,IAAI,MAAM,GAAG,MAAM,CAAC,UAAU,EAAE,CAAC;QAC/B,MAAM,IAAI,QAAQ,CAChB,qBAAqB,MAAM,CAAC,UAAU,aAAa,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,IAAI,QAAQ,EACvF;YACE,IAAI,EAAE,CAAC;YACP,IAAI,EAAE,SAAS,CAAC,aAAa;YAC7B,OAAO,EAAE,gBAAgB;YACzB,WAAW,EAAE,4BAA4B,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,MAAM,CAAC,UAAU,kGAAkG;YACnL,SAAS,EAAE,KAAK;SACjB,CACF,CAAC;IACJ,CAAC;AACH,CAAC"}
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Streaming client for NotebookLM chat
3
+ * Handles chunked streaming responses from GenerateFreeFormStreamed endpoint
4
+ *
5
+ * REWRITTEN VERSION with improved buffer draining logic
6
+ * ======================================================
7
+ *
8
+ * Key improvements:
9
+ * 1. Simplified buffer processing logic
10
+ * 2. More aggressive buffer draining after stream ends
11
+ * 3. Better handling of incomplete frames
12
+ * 4. Cleaner code structure with same interfaces
13
+ *
14
+ * CRITICAL FIXES MAINTAINED:
15
+ * ==========================
16
+ * 1. ✅ source-path parameter in URL
17
+ * 2. ✅ Single URL encoding (no double encoding)
18
+ * 3. ✅ notebookId as last parameter in request body
19
+ *
20
+ * Response Format:
21
+ * ----------------
22
+ * Chunks arrive in this format:
23
+ * <byte_count>
24
+ * [["wrb.fr", null, "<escaped_json>"]]
25
+ */
26
+ import type { RPCClientConfig } from '../rpc/rpc-client.js';
27
+ export interface StreamChunk {
28
+ /** Chunk number (1-based) */
29
+ chunkNumber: number;
30
+ /** Byte count for this chunk */
31
+ byteCount: number;
32
+ /** Full text content from this chunk */
33
+ text: string;
34
+ /** Thinking headers (bold text) */
35
+ thinking: string[];
36
+ /** Response text (non-bold) */
37
+ response: string;
38
+ /** Metadata (conversation ID, message ID, timestamp) */
39
+ metadata?: [string, string, number];
40
+ /** Message IDs for conversation history */
41
+ messageIds?: [string, string];
42
+ /** Timestamp */
43
+ timestamp?: number;
44
+ /** Is this thinking content? */
45
+ isThinking?: boolean;
46
+ /** Is this response content? */
47
+ isResponse?: boolean;
48
+ /** Formatting information */
49
+ formatting?: any;
50
+ /** Citation numbers */
51
+ citations?: number[];
52
+ /** Raw parsed data */
53
+ rawData?: any;
54
+ /** Is this an error chunk? */
55
+ isError?: boolean;
56
+ /** Error code if this is an error */
57
+ errorCode?: number;
58
+ }
59
+ export interface StreamingOptions {
60
+ /** Callback for each chunk */
61
+ onChunk?: (chunk: StreamChunk) => void;
62
+ /** Whether to include thinking process */
63
+ showThinking?: boolean;
64
+ }
65
+ /**
66
+ * Streaming client for NotebookLM chat
67
+ * Handles chunked streaming responses with improved buffer draining
68
+ */
69
+ export declare class StreamingClient {
70
+ private config;
71
+ private requestCounter;
72
+ private buffer;
73
+ private chunkCount;
74
+ constructor(config: RPCClientConfig);
75
+ /**
76
+ * Log message (disabled by default)
77
+ */
78
+ private log;
79
+ /**
80
+ * Log error (disabled by default)
81
+ */
82
+ private logError;
83
+ /**
84
+ * Reset parser state for new stream
85
+ */
86
+ private reset;
87
+ /**
88
+ * Stream a chat request
89
+ *
90
+ * @param notebookId - The notebook UUID
91
+ * @param prompt - User's question
92
+ * @param sourceIds - List of source IDs to query
93
+ * @param conversationId - Conversation UUID
94
+ * @param conversationHistory - Message history (always null)
95
+ * @param options - Streaming options
96
+ */
97
+ streamChat(notebookId: string, prompt: string, sourceIds: string[], conversationId: string, conversationHistory: any[] | null, options?: StreamingOptions): AsyncGenerator<StreamChunk, void, unknown>;
98
+ /**
99
+ * Extract all complete frames from buffer
100
+ * This is the core buffer processing logic - simplified and foolproof
101
+ */
102
+ private extractCompleteFrames;
103
+ /**
104
+ * Parse a single frame
105
+ * Returns null for incomplete frames (incremental updates)
106
+ */
107
+ private parseFrame;
108
+ /**
109
+ * Create StreamChunk from ParsedChunk
110
+ */
111
+ private createStreamChunk;
112
+ /**
113
+ * Build streaming URL
114
+ */
115
+ private buildStreamingURL;
116
+ /**
117
+ * Build request headers
118
+ */
119
+ private buildHeaders;
120
+ /**
121
+ * Build request body
122
+ */
123
+ private buildRequestBody;
124
+ /**
125
+ * Decode request body for logging
126
+ */
127
+ private decodeRequestBody;
128
+ }
129
+ //# sourceMappingURL=streaming-client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"streaming-client.d.ts","sourceRoot":"","sources":["../../../src/utils/streaming-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAE5D,MAAM,WAAW,WAAW;IAC1B,6BAA6B;IAC7B,WAAW,EAAE,MAAM,CAAC;IACpB,gCAAgC;IAChC,SAAS,EAAE,MAAM,CAAC;IAClB,wCAAwC;IACxC,IAAI,EAAE,MAAM,CAAC;IACb,mCAAmC;IACnC,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,+BAA+B;IAC/B,QAAQ,EAAE,MAAM,CAAC;IACjB,wDAAwD;IACxD,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IACpC,2CAA2C;IAC3C,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9B,gBAAgB;IAChB,SAAS,CAAC,EAAE,MAAM,CA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