vidnavigator 1.0.3 → 1.0.5

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.
package/src/index.ts CHANGED
@@ -22,8 +22,8 @@ import {
22
22
  NamespaceRef,
23
23
  NamespaceRefJSON,
24
24
  ExtractionSchema,
25
- ExtractionTokenUsage,
26
- ExtractionTokenUsageJSON,
25
+ UsageBlock,
26
+ UsageBlockJSON,
27
27
  CarouselInfo,
28
28
  CarouselInfoJSON,
29
29
  CarouselVideoResult,
@@ -33,6 +33,11 @@ import {
33
33
  TikTokProfileScrapeSubmissionJSON,
34
34
  TikTokProfileTask,
35
35
  TikTokProfileTaskJSON,
36
+ TikTokSearchRequest,
37
+ TikTokSearchSubmission,
38
+ TikTokSearchSubmissionJSON,
39
+ TikTokSearchTask,
40
+ TikTokSearchTaskJSON,
36
41
  TweetStatement,
37
42
  TweetStatementJSON,
38
43
  } from './models';
@@ -54,7 +59,7 @@ export * from './models';
54
59
  export * from './errors';
55
60
 
56
61
  /** SDK version (keep in sync with package.json) */
57
- export const SDK_VERSION = '1.0.2';
62
+ export const SDK_VERSION = '1.0.5';
58
63
 
59
64
  //region --- Interfaces ---
60
65
  export interface SDKConfig {
@@ -66,6 +71,7 @@ export interface SDKConfig {
66
71
  interface ApiSuccessResponse<T> {
67
72
  status: 'success';
68
73
  data: T;
74
+ usage?: UsageBlockJSON;
69
75
  }
70
76
 
71
77
  export type TranscriptRequestPayload = {
@@ -74,22 +80,32 @@ export type TranscriptRequestPayload = {
74
80
  metadata_only?: boolean;
75
81
  fallback_to_metadata?: boolean;
76
82
  transcript_text?: boolean;
83
+ include_usage?: boolean;
77
84
  };
78
85
 
79
86
  export type TranscribeVideoPayload = {
80
87
  video_url: string;
81
88
  transcript_text?: boolean;
82
89
  all_videos?: boolean;
90
+ include_usage?: boolean;
91
+ };
92
+
93
+ export type TranscriptResult = {
94
+ video_info: VideoInfo;
95
+ transcript: TranscriptOutput;
96
+ usage?: UsageBlock;
83
97
  };
84
98
 
85
99
  export type TranscribeVideoSingleResult = {
86
100
  video_info: VideoInfo;
87
101
  transcript: TranscriptOutput;
102
+ usage?: UsageBlock;
88
103
  };
89
104
 
90
105
  export type TranscribeVideoCarouselResult = {
91
106
  carousel_info: CarouselInfo;
92
107
  videos: CarouselVideoResult[];
108
+ usage?: UsageBlock;
93
109
  };
94
110
 
95
111
  export type TranscribeVideoResult = TranscribeVideoSingleResult | TranscribeVideoCarouselResult;
@@ -119,8 +135,107 @@ export interface ExtractDataResult {
119
135
  data: Record<string, unknown>;
120
136
  video_info?: VideoInfo;
121
137
  file_info?: FileInfo;
122
- usage?: ExtractionTokenUsage;
138
+ usage?: UsageBlock;
123
139
  }
140
+
141
+ export type YouTubeSearchFocus = 'relevance' | 'popularity' | 'brevity';
142
+
143
+ export type SearchYouTubePayload = {
144
+ query: string;
145
+ use_enhanced_search?: boolean;
146
+ start_year?: number;
147
+ end_year?: number;
148
+ focus?: YouTubeSearchFocus;
149
+ duration?: number;
150
+ max_results?: number;
151
+ include_usage?: boolean;
152
+ };
153
+
154
+ export type SearchYouTubeResult = {
155
+ results: VideoSearchResult[];
156
+ query: string;
157
+ total_found: number;
158
+ explanation?: string;
159
+ usage?: UsageBlock;
160
+ /** Present when a 402 partial-results response is returned. */
161
+ status?: 'success' | 'partial';
162
+ /** Present on partial results (e.g. `insufficient_credits_video_search`). */
163
+ error_code?: string;
164
+ };
165
+
166
+ export type SearchFilesPayload = {
167
+ query: string;
168
+ namespace_ids?: string[];
169
+ include_usage?: boolean;
170
+ };
171
+
172
+ export type SearchFilesResult = {
173
+ results: FileSearchResult[];
174
+ query: string;
175
+ total_found: number;
176
+ explanation?: string;
177
+ usage?: UsageBlock;
178
+ };
179
+
180
+ export type AnalyzeVideoPayload = {
181
+ video_url: string;
182
+ query?: string;
183
+ transcript_text?: boolean;
184
+ include_usage?: boolean;
185
+ };
186
+
187
+ export type AnalyzeFilePayload = {
188
+ file_id: string;
189
+ query?: string;
190
+ transcript_text?: boolean;
191
+ include_usage?: boolean;
192
+ };
193
+
194
+ export type AnalyzeVideoResult = {
195
+ video_info: VideoInfo;
196
+ transcript: TranscriptOutput;
197
+ transcript_analysis: AnalysisResult;
198
+ usage?: UsageBlock;
199
+ };
200
+
201
+ export type AnalyzeFileResult = {
202
+ file_info: FileInfo;
203
+ transcript: TranscriptOutput;
204
+ transcript_analysis: AnalysisResult;
205
+ usage?: UsageBlock;
206
+ };
207
+
208
+ export type ExtractVideoDataPayload = {
209
+ video_url: string;
210
+ schema: ExtractionSchema;
211
+ what_to_extract?: string;
212
+ transcribe?: boolean;
213
+ include_usage?: boolean;
214
+ };
215
+
216
+ export type ExtractVideoDataMultipartPayload = {
217
+ video_url: string;
218
+ /** Path to a JSON or YAML schema file. The API accepts either a raw schema or a full extraction request object. */
219
+ schemaFilePath: string;
220
+ what_to_extract?: string;
221
+ transcribe?: boolean;
222
+ include_usage?: boolean;
223
+ };
224
+
225
+ export type ExtractFileDataPayload = {
226
+ file_id: string;
227
+ schema: ExtractionSchema;
228
+ what_to_extract?: string;
229
+ include_usage?: boolean;
230
+ };
231
+
232
+ export type ExtractFileDataMultipartPayload = {
233
+ file_id: string;
234
+ /** Path to a JSON or YAML schema file. The API accepts either a raw schema or a full extraction request object. */
235
+ schemaFilePath: string;
236
+ what_to_extract?: string;
237
+ include_usage?: boolean;
238
+ };
124
239
  //endregion
125
240
 
126
241
  function parseApiErrorPayload(data: any): {
@@ -137,6 +252,38 @@ function parseApiErrorPayload(data: any): {
137
252
  return { errorCode, errorMessage, details: data };
138
253
  }
139
254
 
255
+ function appendOptionalFormField(form: FormData, name: string, value: string | number | boolean | undefined): void {
256
+ if (value !== undefined) {
257
+ form.append(name, String(value));
258
+ }
259
+ }
260
+
261
+ function buildTikTokPollParams(query?: {
262
+ cursor?: string;
263
+ limit?: number;
264
+ include_usage?: boolean;
265
+ }): Record<string, string | number> | undefined {
266
+ if (!query) return undefined;
267
+ const params: Record<string, string | number> = {};
268
+ if (query.cursor !== undefined) params.cursor = query.cursor;
269
+ if (query.limit !== undefined) params.limit = query.limit;
270
+ // The TikTok pollers expect include_usage as a query string value.
271
+ if (query.include_usage) params.include_usage = 'true';
272
+ return params;
273
+ }
274
+
275
+ const _deprecationWarned = new Set<string>();
276
+
277
+ function warnDeprecated(oldName: string, newName: string): void {
278
+ if (_deprecationWarned.has(oldName)) return;
279
+ _deprecationWarned.add(oldName);
280
+ if (typeof console !== 'undefined' && typeof console.warn === 'function') {
281
+ console.warn(
282
+ `[vidnavigator] ${oldName}() is deprecated and will be removed in a future release; use ${newName}() instead.`
283
+ );
284
+ }
285
+ }
286
+
140
287
  export class VidNavigatorClient {
141
288
  private client: AxiosInstance;
142
289
 
@@ -215,10 +362,12 @@ export class VidNavigatorClient {
215
362
  }
216
363
 
217
364
  //region --- Transcripts ---
218
- async getTranscript(payload: TranscriptRequestPayload): Promise<{
219
- video_info: VideoInfo;
220
- transcript: TranscriptOutput;
221
- }> {
365
+ /**
366
+ * Get a transcript for any supported video. The endpoint auto-detects the
367
+ * platform (YouTube, Vimeo, X/Twitter, TikTok, Facebook, Dailymotion, Loom, etc.)
368
+ * from `video_url`. For Instagram, use {@link VidNavigatorClient.transcribeVideo}.
369
+ */
370
+ async getTranscript(payload: TranscriptRequestPayload): Promise<TranscriptResult> {
222
371
  const response = await this.request<
223
372
  ApiSuccessResponse<{
224
373
  video_info: VideoInfoJSON;
@@ -229,24 +378,18 @@ export class VidNavigatorClient {
229
378
  return {
230
379
  video_info: VideoInfo.fromJSON(response.data.video_info),
231
380
  transcript: transcriptFromJSON(response.data.transcript)!,
381
+ usage: response.usage ? UsageBlock.fromJSON(response.usage) : undefined,
232
382
  };
233
383
  }
234
384
 
235
- async getYouTubeTranscript(payload: TranscriptRequestPayload): Promise<{
236
- video_info: VideoInfo;
237
- transcript: TranscriptOutput;
238
- }> {
239
- const response = await this.request<
240
- ApiSuccessResponse<{
241
- video_info: VideoInfoJSON;
242
- transcript: TranscriptSegmentJSON[] | string;
243
- }>
244
- >('POST', '/youtube/transcript', payload);
245
-
246
- return {
247
- video_info: VideoInfo.fromJSON(response.data.video_info),
248
- transcript: transcriptFromJSON(response.data.transcript)!,
249
- };
385
+ /**
386
+ * @deprecated There is now a single `/transcript` endpoint that auto-detects the
387
+ * platform. Use {@link VidNavigatorClient.getTranscript} instead. This alias
388
+ * forwards to `getTranscript` and will be removed in a future release.
389
+ */
390
+ async getYouTubeTranscript(payload: TranscriptRequestPayload): Promise<TranscriptResult> {
391
+ warnDeprecated('getYouTubeTranscript', 'getTranscript');
392
+ return this.getTranscript(payload);
250
393
  }
251
394
 
252
395
  async transcribeVideo(payload: TranscribeVideoPayload): Promise<TranscribeVideoResult> {
@@ -262,10 +405,12 @@ export class VidNavigatorClient {
262
405
  >('POST', '/transcribe', payload);
263
406
 
264
407
  const inner = response.data;
408
+ const usage = response.usage ? UsageBlock.fromJSON(response.usage) : undefined;
265
409
  if ('videos' in inner && 'carousel_info' in inner) {
266
410
  return {
267
411
  carousel_info: CarouselInfo.fromJSON(inner.carousel_info),
268
412
  videos: inner.videos.map((v) => CarouselVideoResult.fromJSON(v)),
413
+ usage,
269
414
  };
270
415
  }
271
416
  const single = inner as {
@@ -275,6 +420,7 @@ export class VidNavigatorClient {
275
420
  return {
276
421
  video_info: VideoInfo.fromJSON(single.video_info),
277
422
  transcript: transcriptFromJSON(single.transcript)!,
423
+ usage,
278
424
  };
279
425
  }
280
426
  //endregion
@@ -293,15 +439,41 @@ export class VidNavigatorClient {
293
439
 
294
440
  async getTikTokProfileScrape(
295
441
  task_id: string,
296
- query?: { cursor?: string; limit?: number }
442
+ query?: { cursor?: string; limit?: number; include_usage?: boolean }
297
443
  ): Promise<TikTokProfileTask> {
298
444
  const response = await this.request<ApiSuccessResponse<TikTokProfileTaskJSON>>(
299
445
  'GET',
300
446
  `/tiktok/profile/${task_id}`,
301
447
  undefined,
302
- query
448
+ buildTikTokPollParams(query)
449
+ );
450
+ const task = TikTokProfileTask.fromJSON(response.data);
451
+ if (response.usage) task.usage = UsageBlock.fromJSON(response.usage);
452
+ return task;
453
+ }
454
+
455
+ async submitTikTokSearch(payload: TikTokSearchRequest): Promise<TikTokSearchSubmission> {
456
+ const response = await this.request<ApiSuccessResponse<TikTokSearchSubmissionJSON>>(
457
+ 'POST',
458
+ '/tiktok/search',
459
+ payload
460
+ );
461
+ return TikTokSearchSubmission.fromJSON(response.data);
462
+ }
463
+
464
+ async getTikTokSearch(
465
+ task_id: string,
466
+ query?: { cursor?: string; limit?: number; include_usage?: boolean }
467
+ ): Promise<TikTokSearchTask> {
468
+ const response = await this.request<ApiSuccessResponse<TikTokSearchTaskJSON>>(
469
+ 'GET',
470
+ `/tiktok/search/${task_id}`,
471
+ undefined,
472
+ buildTikTokPollParams(query)
303
473
  );
304
- return TikTokProfileTask.fromJSON(response.data);
474
+ const task = TikTokSearchTask.fromJSON(response.data);
475
+ if (response.usage) task.usage = UsageBlock.fromJSON(response.usage);
476
+ return task;
305
477
  }
306
478
  //endregion
307
479
 
@@ -518,15 +690,7 @@ export class VidNavigatorClient {
518
690
  //endregion
519
691
 
520
692
  //region --- Analysis ---
521
- async analyzeVideo(payload: {
522
- video_url: string;
523
- query?: string;
524
- transcript_text?: boolean;
525
- }): Promise<{
526
- video_info: VideoInfo;
527
- transcript: TranscriptOutput;
528
- transcript_analysis: AnalysisResult;
529
- }> {
693
+ async analyzeVideo(payload: AnalyzeVideoPayload): Promise<AnalyzeVideoResult> {
530
694
  const response = await this.request<
531
695
  ApiSuccessResponse<{
532
696
  video_info: VideoInfoJSON;
@@ -539,18 +703,11 @@ export class VidNavigatorClient {
539
703
  video_info: VideoInfo.fromJSON(response.data.video_info),
540
704
  transcript: transcriptFromJSON(response.data.transcript)!,
541
705
  transcript_analysis: AnalysisResult.fromJSON(response.data.transcript_analysis),
706
+ usage: response.usage ? UsageBlock.fromJSON(response.usage) : undefined,
542
707
  };
543
708
  }
544
709
 
545
- async analyzeFile(payload: {
546
- file_id: string;
547
- query?: string;
548
- transcript_text?: boolean;
549
- }): Promise<{
550
- file_info: FileInfo;
551
- transcript: TranscriptOutput;
552
- transcript_analysis: AnalysisResult;
553
- }> {
710
+ async analyzeFile(payload: AnalyzeFilePayload): Promise<AnalyzeFileResult> {
554
711
  const response = await this.request<
555
712
  ApiSuccessResponse<{
556
713
  file_info: FileInfoJSON;
@@ -563,6 +720,7 @@ export class VidNavigatorClient {
563
720
  file_info: FileInfo.fromJSON(response.data.file_info),
564
721
  transcript: transcriptFromJSON(response.data.transcript)!,
565
722
  transcript_analysis: AnalysisResult.fromJSON(response.data.transcript_analysis),
723
+ usage: response.usage ? UsageBlock.fromJSON(response.usage) : undefined,
566
724
  };
567
725
  }
568
726
 
@@ -577,86 +735,135 @@ export class VidNavigatorClient {
577
735
  //endregion
578
736
 
579
737
  //region --- Extraction ---
580
- async extractVideoData(payload: {
581
- video_url: string;
582
- schema: ExtractionSchema;
583
- what_to_extract?: string;
584
- transcribe?: boolean;
585
- include_usage?: boolean;
586
- }): Promise<ExtractDataResult> {
738
+ async extractVideoData(
739
+ payload: ExtractVideoDataPayload | ExtractVideoDataMultipartPayload
740
+ ): Promise<ExtractDataResult> {
741
+ if ('schemaFilePath' in payload) {
742
+ const form = new FormData();
743
+ form.append('video_url', payload.video_url);
744
+ form.append('schema', fs.createReadStream(payload.schemaFilePath));
745
+ appendOptionalFormField(form, 'what_to_extract', payload.what_to_extract);
746
+ appendOptionalFormField(form, 'transcribe', payload.transcribe);
747
+ appendOptionalFormField(form, 'include_usage', payload.include_usage);
748
+
749
+ const body = await this.request<{
750
+ status: 'success';
751
+ data: Record<string, unknown>;
752
+ video_info?: VideoInfoJSON;
753
+ usage?: UsageBlockJSON;
754
+ }>('POST', '/extract/video', form, undefined, form.getHeaders());
755
+ return {
756
+ data: body.data,
757
+ video_info: body.video_info ? VideoInfo.fromJSON(body.video_info) : undefined,
758
+ usage: body.usage ? UsageBlock.fromJSON(body.usage) : undefined,
759
+ };
760
+ }
761
+
587
762
  const body = await this.request<{
588
763
  status: 'success';
589
764
  data: Record<string, unknown>;
590
765
  video_info?: VideoInfoJSON;
591
- usage?: ExtractionTokenUsageJSON;
766
+ usage?: UsageBlockJSON;
592
767
  }>('POST', '/extract/video', payload);
593
768
  return {
594
769
  data: body.data,
595
770
  video_info: body.video_info ? VideoInfo.fromJSON(body.video_info) : undefined,
596
- usage: body.usage ? ExtractionTokenUsage.fromJSON(body.usage) : undefined,
771
+ usage: body.usage ? UsageBlock.fromJSON(body.usage) : undefined,
597
772
  };
598
773
  }
599
774
 
600
- async extractFileData(payload: {
601
- file_id: string;
602
- schema: ExtractionSchema;
603
- what_to_extract?: string;
604
- include_usage?: boolean;
605
- }): Promise<ExtractDataResult> {
775
+ async extractFileData(
776
+ payload: ExtractFileDataPayload | ExtractFileDataMultipartPayload
777
+ ): Promise<ExtractDataResult> {
778
+ if ('schemaFilePath' in payload) {
779
+ const form = new FormData();
780
+ form.append('file_id', payload.file_id);
781
+ form.append('schema', fs.createReadStream(payload.schemaFilePath));
782
+ appendOptionalFormField(form, 'what_to_extract', payload.what_to_extract);
783
+ appendOptionalFormField(form, 'include_usage', payload.include_usage);
784
+
785
+ const body = await this.request<{
786
+ status: 'success';
787
+ data: Record<string, unknown>;
788
+ file_info?: FileInfoJSON;
789
+ usage?: UsageBlockJSON;
790
+ }>('POST', '/extract/file', form, undefined, form.getHeaders());
791
+ return {
792
+ data: body.data,
793
+ file_info: body.file_info ? FileInfo.fromJSON(body.file_info) : undefined,
794
+ usage: body.usage ? UsageBlock.fromJSON(body.usage) : undefined,
795
+ };
796
+ }
797
+
606
798
  const body = await this.request<{
607
799
  status: 'success';
608
800
  data: Record<string, unknown>;
609
801
  file_info?: FileInfoJSON;
610
- usage?: ExtractionTokenUsageJSON;
802
+ usage?: UsageBlockJSON;
611
803
  }>('POST', '/extract/file', payload);
612
804
  return {
613
805
  data: body.data,
614
806
  file_info: body.file_info ? FileInfo.fromJSON(body.file_info) : undefined,
615
- usage: body.usage ? ExtractionTokenUsage.fromJSON(body.usage) : undefined,
807
+ usage: body.usage ? UsageBlock.fromJSON(body.usage) : undefined,
616
808
  };
617
809
  }
618
810
  //endregion
619
811
 
620
812
  //region --- Search ---
621
- async searchVideos(payload: {
622
- query: string;
623
- use_enhanced_search?: boolean;
624
- start_year?: number;
625
- end_year?: number;
626
- focus?: 'relevance' | 'popularity' | 'brevity';
627
- duration?: number;
628
- }): Promise<{
629
- results: VideoSearchResult[];
630
- query: string;
631
- total_found: number;
632
- explanation?: string;
633
- }> {
634
- const response = await this.request<
635
- ApiSuccessResponse<{
813
+ /**
814
+ * Search YouTube for videos with AI analysis and ranking.
815
+ *
816
+ * If the account runs out of `residential_request` credits mid-search, the API
817
+ * returns HTTP 402 with `status: "partial"` and the videos analysed so far. The
818
+ * SDK surfaces that partial payload (results, `status: "partial"`, `error_code`,
819
+ * and `usage` when requested) instead of throwing.
820
+ */
821
+ async searchYouTube(payload: SearchYouTubePayload): Promise<SearchYouTubeResult> {
822
+ let response: {
823
+ status?: 'success' | 'partial';
824
+ error_code?: string;
825
+ data: {
636
826
  results: VideoSearchResultJSON[];
637
827
  query: string;
638
828
  total_found: number;
639
829
  explanation?: string;
640
- }>
641
- >('POST', '/search/video', payload);
830
+ };
831
+ usage?: UsageBlockJSON;
832
+ };
833
+
834
+ try {
835
+ response = await this.request('POST', '/youtube/search', payload);
836
+ } catch (error: any) {
837
+ const body = error?.details;
838
+ if (error instanceof PaymentRequiredError && body?.status === 'partial' && body?.data) {
839
+ response = body;
840
+ } else {
841
+ throw error;
842
+ }
843
+ }
642
844
 
643
845
  return {
644
- results: response.data.results.map(VideoSearchResult.fromJSON),
846
+ results: (response.data.results ?? []).map(VideoSearchResult.fromJSON),
645
847
  query: response.data.query,
646
848
  total_found: response.data.total_found,
647
849
  explanation: response.data.explanation,
850
+ usage: response.usage ? UsageBlock.fromJSON(response.usage) : undefined,
851
+ status: response.status,
852
+ error_code: response.error_code,
648
853
  };
649
854
  }
650
855
 
651
- async searchFiles(payload: {
652
- query: string;
653
- namespace_ids?: string[];
654
- }): Promise<{
655
- results: FileSearchResult[];
656
- query: string;
657
- total_found: number;
658
- explanation?: string;
659
- }> {
856
+ /**
857
+ * @deprecated `/search/video` moved to `/youtube/search`. Use
858
+ * {@link VidNavigatorClient.searchYouTube} instead. This alias forwards to
859
+ * `searchYouTube` and will be removed in a future release.
860
+ */
861
+ async searchVideos(payload: SearchYouTubePayload): Promise<SearchYouTubeResult> {
862
+ warnDeprecated('searchVideos', 'searchYouTube');
863
+ return this.searchYouTube(payload);
864
+ }
865
+
866
+ async searchFiles(payload: SearchFilesPayload): Promise<SearchFilesResult> {
660
867
  const response = await this.request<
661
868
  ApiSuccessResponse<{
662
869
  results: FileSearchResultJSON[];
@@ -671,6 +878,7 @@ export class VidNavigatorClient {
671
878
  query: response.data.query,
672
879
  total_found: response.data.total_found,
673
880
  explanation: response.data.explanation,
881
+ usage: response.usage ? UsageBlock.fromJSON(response.usage) : undefined,
674
882
  };
675
883
  }
676
884
  //endregion