twelvelabs-js 0.1.0

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 (194) hide show
  1. package/LICENSE +183 -0
  2. package/README.md +278 -0
  3. package/dist/README.md +278 -0
  4. package/dist/constants.d.ts +4 -0
  5. package/dist/constants.d.ts.map +1 -0
  6. package/dist/constants.js +7 -0
  7. package/dist/constants.js.map +1 -0
  8. package/dist/constants.mjs +4 -0
  9. package/dist/constants.mjs.map +1 -0
  10. package/dist/core.d.ts +23 -0
  11. package/dist/core.d.ts.map +1 -0
  12. package/dist/core.js +145 -0
  13. package/dist/core.js.map +1 -0
  14. package/dist/core.mjs +114 -0
  15. package/dist/core.mjs.map +1 -0
  16. package/dist/error.d.ts +34 -0
  17. package/dist/error.d.ts.map +1 -0
  18. package/dist/error.js +55 -0
  19. package/dist/error.js.map +1 -0
  20. package/dist/error.mjs +40 -0
  21. package/dist/error.mjs.map +1 -0
  22. package/dist/index.d.ts +20 -0
  23. package/dist/index.d.ts.map +1 -0
  24. package/dist/index.js +69 -0
  25. package/dist/index.js.map +1 -0
  26. package/dist/index.mjs +23 -0
  27. package/dist/index.mjs.map +1 -0
  28. package/dist/interfaces.d.ts +7 -0
  29. package/dist/interfaces.d.ts.map +1 -0
  30. package/dist/interfaces.js +3 -0
  31. package/dist/interfaces.js.map +1 -0
  32. package/dist/interfaces.mjs +2 -0
  33. package/dist/interfaces.mjs.map +1 -0
  34. package/dist/models/engine/index.d.ts +16 -0
  35. package/dist/models/engine/index.d.ts.map +1 -0
  36. package/dist/models/engine/index.js +14 -0
  37. package/dist/models/engine/index.js.map +1 -0
  38. package/dist/models/engine/index.mjs +10 -0
  39. package/dist/models/engine/index.mjs.map +1 -0
  40. package/dist/models/generate/index.d.ts +29 -0
  41. package/dist/models/generate/index.d.ts.map +1 -0
  42. package/dist/models/generate/index.js +3 -0
  43. package/dist/models/generate/index.js.map +1 -0
  44. package/dist/models/generate/index.mjs +2 -0
  45. package/dist/models/generate/index.mjs.map +1 -0
  46. package/dist/models/index/index.d.ts +57 -0
  47. package/dist/models/index/index.d.ts.map +1 -0
  48. package/dist/models/index/index.js +72 -0
  49. package/dist/models/index/index.js.map +1 -0
  50. package/dist/models/index/index.mjs +67 -0
  51. package/dist/models/index/index.mjs.map +1 -0
  52. package/dist/models/index.d.ts +8 -0
  53. package/dist/models/index.d.ts.map +1 -0
  54. package/dist/models/index.js +24 -0
  55. package/dist/models/index.js.map +1 -0
  56. package/dist/models/index.mjs +8 -0
  57. package/dist/models/index.mjs.map +1 -0
  58. package/dist/models/interfaces.d.ts +7 -0
  59. package/dist/models/interfaces.d.ts.map +1 -0
  60. package/dist/models/interfaces.js +3 -0
  61. package/dist/models/interfaces.js.map +1 -0
  62. package/dist/models/interfaces.mjs +2 -0
  63. package/dist/models/interfaces.mjs.map +1 -0
  64. package/dist/models/search/index.d.ts +47 -0
  65. package/dist/models/search/index.d.ts.map +1 -0
  66. package/dist/models/search/index.js +22 -0
  67. package/dist/models/search/index.js.map +1 -0
  68. package/dist/models/search/index.mjs +18 -0
  69. package/dist/models/search/index.mjs.map +1 -0
  70. package/dist/models/task/index.d.ts +59 -0
  71. package/dist/models/task/index.d.ts.map +1 -0
  72. package/dist/models/task/index.js +67 -0
  73. package/dist/models/task/index.js.map +1 -0
  74. package/dist/models/task/index.mjs +62 -0
  75. package/dist/models/task/index.mjs.map +1 -0
  76. package/dist/models/video/index.d.ts +52 -0
  77. package/dist/models/video/index.d.ts.map +1 -0
  78. package/dist/models/video/index.js +64 -0
  79. package/dist/models/video/index.js.map +1 -0
  80. package/dist/models/video/index.mjs +59 -0
  81. package/dist/models/video/index.mjs.map +1 -0
  82. package/dist/package.json +37 -0
  83. package/dist/resource.d.ts +12 -0
  84. package/dist/resource.d.ts.map +1 -0
  85. package/dist/resource.js +17 -0
  86. package/dist/resource.js.map +1 -0
  87. package/dist/resource.mjs +13 -0
  88. package/dist/resource.mjs.map +1 -0
  89. package/dist/resources/engine/index.d.ts +8 -0
  90. package/dist/resources/engine/index.d.ts.map +1 -0
  91. package/dist/resources/engine/index.js +42 -0
  92. package/dist/resources/engine/index.js.map +1 -0
  93. package/dist/resources/engine/index.mjs +15 -0
  94. package/dist/resources/engine/index.mjs.map +1 -0
  95. package/dist/resources/generate/index.d.ts +10 -0
  96. package/dist/resources/generate/index.d.ts.map +1 -0
  97. package/dist/resources/generate/index.js +36 -0
  98. package/dist/resources/generate/index.js.map +1 -0
  99. package/dist/resources/generate/index.mjs +32 -0
  100. package/dist/resources/generate/index.mjs.map +1 -0
  101. package/dist/resources/generate/interfaces.d.ts +3 -0
  102. package/dist/resources/generate/interfaces.d.ts.map +1 -0
  103. package/dist/resources/generate/interfaces.js +3 -0
  104. package/dist/resources/generate/interfaces.js.map +1 -0
  105. package/dist/resources/generate/interfaces.mjs +2 -0
  106. package/dist/resources/generate/interfaces.mjs.map +1 -0
  107. package/dist/resources/index/index.d.ts +17 -0
  108. package/dist/resources/index/index.d.ts.map +1 -0
  109. package/dist/resources/index/index.js +82 -0
  110. package/dist/resources/index/index.js.map +1 -0
  111. package/dist/resources/index/index.mjs +55 -0
  112. package/dist/resources/index/index.mjs.map +1 -0
  113. package/dist/resources/index/interfaces.d.ts +18 -0
  114. package/dist/resources/index/interfaces.d.ts.map +1 -0
  115. package/dist/resources/index/interfaces.js +3 -0
  116. package/dist/resources/index/interfaces.js.map +1 -0
  117. package/dist/resources/index/interfaces.mjs +2 -0
  118. package/dist/resources/index/interfaces.mjs.map +1 -0
  119. package/dist/resources/index.d.ts +12 -0
  120. package/dist/resources/index.d.ts.map +1 -0
  121. package/dist/resources/index.js +28 -0
  122. package/dist/resources/index.js.map +1 -0
  123. package/dist/resources/index.mjs +12 -0
  124. package/dist/resources/index.mjs.map +1 -0
  125. package/dist/resources/search/index.d.ts +9 -0
  126. package/dist/resources/search/index.d.ts.map +1 -0
  127. package/dist/resources/search/index.js +45 -0
  128. package/dist/resources/search/index.js.map +1 -0
  129. package/dist/resources/search/index.mjs +18 -0
  130. package/dist/resources/search/index.mjs.map +1 -0
  131. package/dist/resources/search/interfaces.d.ts +13 -0
  132. package/dist/resources/search/interfaces.d.ts.map +1 -0
  133. package/dist/resources/search/interfaces.js +3 -0
  134. package/dist/resources/search/interfaces.js.map +1 -0
  135. package/dist/resources/search/interfaces.mjs +2 -0
  136. package/dist/resources/search/interfaces.mjs.map +1 -0
  137. package/dist/resources/task/index.d.ts +23 -0
  138. package/dist/resources/task/index.d.ts.map +1 -0
  139. package/dist/resources/task/index.js +147 -0
  140. package/dist/resources/task/index.js.map +1 -0
  141. package/dist/resources/task/index.mjs +117 -0
  142. package/dist/resources/task/index.mjs.map +1 -0
  143. package/dist/resources/task/interfaces.d.ts +24 -0
  144. package/dist/resources/task/interfaces.d.ts.map +1 -0
  145. package/dist/resources/task/interfaces.js +3 -0
  146. package/dist/resources/task/interfaces.js.map +1 -0
  147. package/dist/resources/task/interfaces.mjs +2 -0
  148. package/dist/resources/task/interfaces.mjs.map +1 -0
  149. package/dist/resources/video/index.d.ts +16 -0
  150. package/dist/resources/video/index.d.ts.map +1 -0
  151. package/dist/resources/video/index.js +92 -0
  152. package/dist/resources/video/index.js.map +1 -0
  153. package/dist/resources/video/index.mjs +65 -0
  154. package/dist/resources/video/index.mjs.map +1 -0
  155. package/dist/resources/video/interfaces.d.ts +23 -0
  156. package/dist/resources/video/interfaces.d.ts.map +1 -0
  157. package/dist/resources/video/interfaces.js +3 -0
  158. package/dist/resources/video/interfaces.js.map +1 -0
  159. package/dist/resources/video/interfaces.mjs +2 -0
  160. package/dist/resources/video/interfaces.mjs.map +1 -0
  161. package/dist/src/constants.ts +3 -0
  162. package/dist/src/core.ts +138 -0
  163. package/dist/src/error.ts +40 -0
  164. package/dist/src/index.ts +86 -0
  165. package/dist/src/interfaces.ts +6 -0
  166. package/dist/src/models/engine/index.ts +23 -0
  167. package/dist/src/models/generate/index.ts +32 -0
  168. package/dist/src/models/index/index.ts +135 -0
  169. package/dist/src/models/index.ts +7 -0
  170. package/dist/src/models/interfaces.ts +6 -0
  171. package/dist/src/models/search/index.ts +66 -0
  172. package/dist/src/models/task/index.ts +132 -0
  173. package/dist/src/models/video/index.ts +135 -0
  174. package/dist/src/resource.ts +21 -0
  175. package/dist/src/resources/engine/index.ts +17 -0
  176. package/dist/src/resources/generate/index.ts +56 -0
  177. package/dist/src/resources/generate/interfaces.ts +2 -0
  178. package/dist/src/resources/index/index.ts +91 -0
  179. package/dist/src/resources/index/interfaces.ts +16 -0
  180. package/dist/src/resources/index.ts +12 -0
  181. package/dist/src/resources/search/index.ts +28 -0
  182. package/dist/src/resources/search/interfaces.ts +12 -0
  183. package/dist/src/resources/task/index.ts +161 -0
  184. package/dist/src/resources/task/interfaces.ts +23 -0
  185. package/dist/src/resources/video/index.ts +157 -0
  186. package/dist/src/resources/video/interfaces.ts +25 -0
  187. package/dist/src/util.ts +94 -0
  188. package/dist/util.d.ts +5 -0
  189. package/dist/util.d.ts.map +1 -0
  190. package/dist/util.js +94 -0
  191. package/dist/util.js.map +1 -0
  192. package/dist/util.mjs +87 -0
  193. package/dist/util.mjs.map +1 -0
  194. package/package.json +37 -0
@@ -0,0 +1,138 @@
1
+ import fetch, { RequestInit, Response } from 'node-fetch';
2
+ import FormData from 'form-data';
3
+ import { API_KEY_HEADER } from './constants';
4
+ import * as Errors from './error';
5
+ import { convertKeysToCamelCase } from './util';
6
+
7
+ export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
8
+ export type RequestOptions = RequestInit & { params?: Record<string, any>; skipCamelKeys?: string[] };
9
+
10
+ export interface APIClientOptions {
11
+ baseUrl: string;
12
+ apiKey: string;
13
+ }
14
+
15
+ export class APIClient {
16
+ baseUrl: string;
17
+ apiKey: string;
18
+
19
+ constructor({ baseUrl, apiKey }: APIClientOptions) {
20
+ this.baseUrl = baseUrl;
21
+ this.apiKey = apiKey;
22
+ }
23
+
24
+ private async _request(
25
+ method: HttpMethod,
26
+ endpoint: string,
27
+ { params, body, skipCamelKeys, ...options }: RequestOptions = {},
28
+ ): Promise<any> {
29
+ const url = new URL(endpoint, this.baseUrl);
30
+ if (params) {
31
+ Object.keys(params).forEach((key) => url.searchParams.append(key, params![key]));
32
+ }
33
+
34
+ let headers = {
35
+ [API_KEY_HEADER]: this.apiKey,
36
+ ['content-type']: 'application/json',
37
+ ...options.headers,
38
+ };
39
+ if (body instanceof FormData) {
40
+ headers = {
41
+ ...headers,
42
+ ...body.getHeaders(),
43
+ };
44
+ }
45
+
46
+ const config: RequestInit = {
47
+ method,
48
+ headers,
49
+ body:
50
+ body ?
51
+ body instanceof FormData ?
52
+ body
53
+ : JSON.stringify(body)
54
+ : undefined,
55
+ ...options,
56
+ };
57
+
58
+ try {
59
+ const response = await fetch(url, config);
60
+ const contentType = response.headers.get('Content-Type');
61
+ let body = null;
62
+
63
+ if (contentType && contentType.includes('application/json')) {
64
+ const rawBody = await response.json();
65
+ body = convertKeysToCamelCase(rawBody, skipCamelKeys);
66
+ } else {
67
+ body = await response.text();
68
+ }
69
+
70
+ handleResponse(response, body, config);
71
+
72
+ return body;
73
+ } catch (error) {
74
+ if (error instanceof fetch.FetchError) {
75
+ if (error.code === 'ENOTFOUND') {
76
+ throw new Errors.APIConnectionError('API Connection Error', error);
77
+ } else if (error.type === 'request-timeout') {
78
+ throw new Errors.APITimeoutError('API Timeout Error', error);
79
+ }
80
+ }
81
+ if (error instanceof Error) {
82
+ throw error;
83
+ } else {
84
+ throw new Errors.APIError('An unknown error occurred');
85
+ }
86
+ }
87
+ }
88
+
89
+ async _get<T>(endpoint: string, params?: Record<string, any>, options: RequestOptions = {}) {
90
+ return (await this._request('GET', endpoint, { ...options, params })) as T;
91
+ }
92
+
93
+ async _post<T>(endpoint: string, data?: any, options: RequestOptions = {}) {
94
+ return (await this._request('POST', endpoint, { ...options, body: data })) as T;
95
+ }
96
+
97
+ async _patch<T>(endpoint: string, data?: any, options: RequestOptions = {}) {
98
+ return (await this._request('PATCH', endpoint, { ...options, body: data })) as T;
99
+ }
100
+
101
+ async _put<T>(endpoint: string, data?: any, options: RequestOptions = {}) {
102
+ return (await this._request('PUT', endpoint, { ...options, body: data })) as T;
103
+ }
104
+
105
+ async _delete<T>(endpoint: string, options: RequestOptions = {}) {
106
+ return (await this._request('DELETE', endpoint, options)) as T;
107
+ }
108
+ }
109
+
110
+ export function handleResponse(response: Response, body: any, request?: RequestInit): void {
111
+ if (!response.ok) {
112
+ const message = body && body.message ? body.message : response.statusText;
113
+ const headers = response.headers as unknown as Headers;
114
+ if (response.status >= 400 && response.status < 500) {
115
+ switch (response.status) {
116
+ case 400:
117
+ throw new Errors.BadRequestError(message, response.status, body, headers, request);
118
+ case 401:
119
+ throw new Errors.UnauthorizedError(message, response.status, body, headers, request);
120
+ case 403:
121
+ throw new Errors.ForbiddenError(message, response.status, body, headers, request);
122
+ case 404:
123
+ throw new Errors.NotFoundError(message, response.status, body, headers, request);
124
+ case 405:
125
+ throw new Errors.MethodNotAllowedError(message, response.status, body, headers, request);
126
+ case 409:
127
+ throw new Errors.ConflictError(message, response.status, body, headers, request);
128
+ case 429:
129
+ throw new Errors.TooManyRequestsError(message, response.status, body, headers, request);
130
+ default:
131
+ throw new Errors.APIError(message, response.status, body, headers, request);
132
+ }
133
+ } else if (response.status >= 500) {
134
+ throw new Errors.InternalServerError(message, response.status, body, headers, request);
135
+ }
136
+ throw new Errors.APIError(message, response.status, body, headers, request);
137
+ }
138
+ }
@@ -0,0 +1,40 @@
1
+ import { RequestInit } from 'node-fetch';
2
+ export class TwelveLabsError extends Error {}
3
+
4
+ export class APIError extends Error {
5
+ status: number | undefined;
6
+ body: any;
7
+ headers: Headers | undefined;
8
+ request?: RequestInit;
9
+
10
+ constructor(message: string, status?: number, body?: any, headers?: Headers, request?: RequestInit) {
11
+ super(message);
12
+ this.name = this.constructor.name;
13
+ this.status = status;
14
+ this.body = body;
15
+ this.headers = headers;
16
+ this.request = request;
17
+ Object.setPrototypeOf(this, new.target.prototype);
18
+ }
19
+ }
20
+
21
+ export class APIConnectionError extends APIError {
22
+ constructor(message: string = 'Connection error.', body?: any) {
23
+ super(message, undefined, body);
24
+ }
25
+ }
26
+
27
+ export class APITimeoutError extends APIError {
28
+ constructor(message: string = 'Request timed out.', body?: any) {
29
+ super(message, undefined, body);
30
+ }
31
+ }
32
+
33
+ export class BadRequestError extends APIError {}
34
+ export class UnauthorizedError extends APIError {}
35
+ export class ForbiddenError extends APIError {}
36
+ export class NotFoundError extends APIError {}
37
+ export class MethodNotAllowedError extends APIError {}
38
+ export class ConflictError extends APIError {}
39
+ export class TooManyRequestsError extends APIError {}
40
+ export class InternalServerError extends APIError {}
@@ -0,0 +1,86 @@
1
+ import { APIClient } from './core';
2
+ import * as Resources from './resources';
3
+ import { BASE_URL, DEFAULT_API_VERSION } from './constants';
4
+
5
+ export interface ClientOptions {
6
+ apiKey: string;
7
+ version?: 'v1.1' | 'v1.2';
8
+ }
9
+
10
+ export class TwelveLabs extends APIClient {
11
+ engine: Resources.Engine;
12
+ index: Resources.Index;
13
+ task: Resources.Task;
14
+ search: Resources.Search;
15
+ generate: Resources.Generate;
16
+
17
+ baseUrl: string;
18
+ apiKey: string;
19
+
20
+ constructor({ apiKey, version = DEFAULT_API_VERSION }: ClientOptions) {
21
+ if (!apiKey) {
22
+ throw new Error(
23
+ 'Provide `apiKey` to initialize a client. You can see the API Key in the Dashboard page: https://dashboard.playground.io',
24
+ );
25
+ }
26
+
27
+ const customBaseUrl = process.env.TWELVELABS_BASE_URL;
28
+ const baseUrl = `${customBaseUrl || BASE_URL}/${version}/`;
29
+
30
+ super({ baseUrl, apiKey });
31
+
32
+ this.baseUrl = baseUrl;
33
+ this.apiKey = apiKey;
34
+
35
+ this.engine = new Resources.Engine(this);
36
+ this.index = new Resources.Index(this);
37
+ this.task = new Resources.Task(this);
38
+ this.search = new Resources.Search(this);
39
+ this.generate = new Resources.Generate(this);
40
+ }
41
+ }
42
+
43
+ export {
44
+ Engine,
45
+ Index,
46
+ IndexListWithPagination,
47
+ Video,
48
+ VideoListWithPagination,
49
+ Task,
50
+ TaskListWithPagination,
51
+ TaskStatus,
52
+ SearchResult,
53
+ SearchData,
54
+ GroupByVideoSearchData,
55
+ GenerateGistResult,
56
+ GenerateSummarizeResult,
57
+ GenerateOpenEndedTextResult,
58
+ PageInfo,
59
+ } from './models';
60
+
61
+ export {
62
+ CreateIndexParams,
63
+ CreateTaskParams,
64
+ ListIndexParams,
65
+ ListTaskParams,
66
+ ListVideoParams,
67
+ SearchOptions,
68
+ UpdateVideoParams,
69
+ VideoFilterOptions,
70
+ GenerateGistType,
71
+ GenerateSummarizeType,
72
+ } from './resources';
73
+
74
+ export {
75
+ TwelveLabsError,
76
+ APIConnectionError,
77
+ APITimeoutError,
78
+ BadRequestError,
79
+ ConflictError,
80
+ ForbiddenError,
81
+ MethodNotAllowedError,
82
+ NotFoundError,
83
+ TooManyRequestsError,
84
+ UnauthorizedError,
85
+ InternalServerError,
86
+ } from './error';
@@ -0,0 +1,6 @@
1
+ export interface PageOptions {
2
+ page?: number;
3
+ pageLimit?: number;
4
+ sortBy?: 'created_at' | 'updated_at';
5
+ sortOption?: 'asc' | 'desc';
6
+ }
@@ -0,0 +1,23 @@
1
+ export interface EngineResponse {
2
+ id: string;
3
+ author: string;
4
+ allowedEngineOptions: string[];
5
+ ready: boolean;
6
+ finetune: boolean;
7
+ }
8
+
9
+ export class Engine {
10
+ id: string;
11
+ author: string;
12
+ allowedEngineOptions: string[];
13
+ ready: boolean;
14
+ finetune: boolean;
15
+
16
+ constructor(res: EngineResponse) {
17
+ this.id = res.id;
18
+ this.author = res.author;
19
+ this.allowedEngineOptions = res.allowedEngineOptions;
20
+ this.ready = res.ready;
21
+ this.finetune = res.finetune;
22
+ }
23
+ }
@@ -0,0 +1,32 @@
1
+ export interface GenerateOpenEndedTextResult {
2
+ id: string;
3
+ data: string;
4
+ }
5
+
6
+ export interface GenerateSummarizeChapterResult {
7
+ chapterNumber: number;
8
+ start: number;
9
+ end: number;
10
+ chapterTitle: string;
11
+ chapterSummary: string;
12
+ }
13
+
14
+ export interface GenerateSummarizeHighlightResult {
15
+ start: number;
16
+ end: number;
17
+ highlight: string;
18
+ }
19
+
20
+ export interface GenerateSummarizeResult {
21
+ id: string;
22
+ summary?: string;
23
+ chapters?: GenerateSummarizeChapterResult[];
24
+ highlights?: GenerateSummarizeHighlightResult[];
25
+ }
26
+
27
+ export interface GenerateGistResult {
28
+ id: string;
29
+ title?: string;
30
+ topics?: string[];
31
+ hashtags?: string[];
32
+ }
@@ -0,0 +1,135 @@
1
+ import { RequestOptions } from '../../core';
2
+ import * as Resources from '../../resources';
3
+ import { PageInfo } from '../interfaces';
4
+ import { SearchResult } from '../search';
5
+ import { Task, TaskStatus } from '../task';
6
+ import { Video } from '../video';
7
+
8
+ export interface IndexResponse {
9
+ id: string;
10
+ indexName: string;
11
+ engines: IndexEngineResponse[];
12
+ videoCount: number;
13
+ totalDuration: number;
14
+ createdAt: string;
15
+ updatedAt?: string;
16
+ }
17
+
18
+ export class Index {
19
+ private readonly _resource: Resources.Index;
20
+
21
+ id: string;
22
+ name: string;
23
+ engines: IndexEngine[];
24
+ videoCount: number;
25
+ totalDuration: number;
26
+ createdAt: string;
27
+ updatedAt?: string;
28
+
29
+ constructor(resource: Resources.Index, data: IndexResponse) {
30
+ this._resource = resource;
31
+ this.id = data.id;
32
+ this.name = data.indexName;
33
+ this.engines = data.engines.map((v) => new IndexEngine(v));
34
+ this.videoCount = data.videoCount;
35
+ this.totalDuration = data.totalDuration;
36
+ this.createdAt = data.createdAt;
37
+ this.updatedAt = data.updatedAt;
38
+ }
39
+
40
+ // Index related methods
41
+
42
+ async retrieve(options: RequestOptions = {}): Promise<Index> {
43
+ return await this._resource.retrieve(this.id, options);
44
+ }
45
+
46
+ async update(name: string, options: RequestOptions = {}): Promise<void> {
47
+ return await this._resource.update(this.id, name, options);
48
+ }
49
+
50
+ async delete(options: RequestOptions = {}): Promise<void> {
51
+ return await this._resource.delete(this.id, options);
52
+ }
53
+
54
+ // Task related methods
55
+
56
+ async createTask(
57
+ body: Omit<Resources.CreateTaskParams, 'indexId'>,
58
+ options: RequestOptions = {},
59
+ ): Promise<Task> {
60
+ return await this._resource._client.task.create({ ...body, indexId: this.id }, options);
61
+ }
62
+
63
+ async taskStatus(options: RequestOptions = {}): Promise<TaskStatus> {
64
+ return await this._resource._client.task.status(this.id, options);
65
+ }
66
+
67
+ async taskExternalProvider(url: string, options: RequestOptions = {}): Promise<Task> {
68
+ return await this._resource._client.task.externalProvider(this.id, url, options);
69
+ }
70
+
71
+ // Video related methods
72
+
73
+ async listVideos(params: Resources.ListVideoParams = {}, options: RequestOptions = {}): Promise<Video[]> {
74
+ return await this._resource.video.list(this.id, params, options);
75
+ }
76
+
77
+ // Search related methods
78
+
79
+ async query(
80
+ body: Omit<Resources.SearchOptions, 'indexId'>,
81
+ options: RequestOptions = {},
82
+ ): Promise<SearchResult> {
83
+ return await this._resource._client.search.query({ ...body, indexId: this.id }, options);
84
+ }
85
+ }
86
+
87
+ export class IndexListWithPagination {
88
+ private readonly _resource: Resources.Index;
89
+ private readonly _originParams: Resources.ListIndexParams;
90
+ data: Index[];
91
+ pageInfo: PageInfo;
92
+
93
+ constructor(
94
+ resource: Resources.Index,
95
+ originParams: Resources.ListIndexParams,
96
+ data: IndexResponse[],
97
+ pageInfo: PageInfo,
98
+ ) {
99
+ this._resource = resource;
100
+ this._originParams = originParams;
101
+ this.data = data.map((v) => new Index(resource, v));
102
+ this.pageInfo = pageInfo;
103
+ }
104
+
105
+ async next(): Promise<Index[] | null> {
106
+ if (this.pageInfo.page >= this.pageInfo.totalPage) {
107
+ return null;
108
+ }
109
+ const params = { ...this._originParams };
110
+ params.page = this.pageInfo.page + 1;
111
+ const res = await this._resource.listPagination(params);
112
+ this.pageInfo = res.pageInfo;
113
+ return res.data;
114
+ }
115
+ }
116
+
117
+ interface IndexEngineResponse {
118
+ id: string;
119
+ engineName: string;
120
+ engineOptions: string[];
121
+ addons?: string[];
122
+ }
123
+
124
+ class IndexEngine {
125
+ id: string;
126
+ name: string;
127
+ options: string[];
128
+ addons?: string[];
129
+ constructor(res: IndexEngineResponse) {
130
+ this.id = res.id;
131
+ this.name = res.engineName;
132
+ this.options = res.engineOptions;
133
+ this.addons = res.addons;
134
+ }
135
+ }
@@ -0,0 +1,7 @@
1
+ export * from './interfaces';
2
+ export * from './engine';
3
+ export * from './index/';
4
+ export * from './task';
5
+ export * from './video';
6
+ export * from './search';
7
+ export * from './generate';
@@ -0,0 +1,6 @@
1
+ export interface PageInfo {
2
+ limitPerPage: number;
3
+ page: number;
4
+ totalPage: number;
5
+ totalResults: number;
6
+ }
@@ -0,0 +1,66 @@
1
+ import * as Resources from '../../resources';
2
+
3
+ export interface SearchResultResponse {
4
+ searchPool: SearchPool;
5
+ data: SearchData[];
6
+ pageInfo: SearchPageInfo;
7
+ }
8
+
9
+ export class SearchResult {
10
+ private readonly _resource: Resources.Search;
11
+ pool: SearchPool;
12
+ data: SearchData[] | GroupByVideoSearchData[];
13
+ pageInfo: SearchPageInfo;
14
+ constructor(resource: Resources.Search, data: SearchResultResponse) {
15
+ this._resource = resource;
16
+ this.pool = data.searchPool;
17
+ this.data = data.data;
18
+ this.pageInfo = data.pageInfo;
19
+ }
20
+
21
+ async next(): Promise<SearchData[] | GroupByVideoSearchData[] | null> {
22
+ const { nextPageToken } = this.pageInfo;
23
+ if (!nextPageToken) {
24
+ return null;
25
+ }
26
+ const res = await this._resource.byPageToken(nextPageToken);
27
+ this.pageInfo = res.pageInfo;
28
+ return res.data;
29
+ }
30
+ }
31
+
32
+ export interface SearchData {
33
+ score: number;
34
+ start: number;
35
+ end: number;
36
+ videoId: string;
37
+ metadata?: Record<string, any>;
38
+ confidence: string;
39
+ thumbnailUrl?: string;
40
+ moduleConfidence?: Record<string, any>;
41
+ modules?: SearchModule[];
42
+ }
43
+
44
+ interface SearchModule {
45
+ type: 'visual' | 'conversation' | 'text_in_video' | 'logo';
46
+ confidence: 'high' | 'medium' | 'low';
47
+ }
48
+
49
+ export interface GroupByVideoSearchData {
50
+ clips?: SearchData[];
51
+ id: string;
52
+ }
53
+
54
+ interface SearchPool {
55
+ totalCount: number;
56
+ totalDuration: number;
57
+ indexId: string;
58
+ }
59
+
60
+ interface SearchPageInfo {
61
+ limitPerPage: number;
62
+ totalResults: number;
63
+ pageExpiredAt: string;
64
+ nextPageToken?: string;
65
+ prevPageToken?: string;
66
+ }
@@ -0,0 +1,132 @@
1
+ import { RequestOptions } from '../../core';
2
+ import * as Resources from '../../resources';
3
+ import { PageInfo } from '../interfaces';
4
+
5
+ export interface TaskResponse {
6
+ id: string;
7
+ indexId: string;
8
+ videoId?: string[];
9
+ estimatedTime?: string;
10
+ status: string;
11
+ metadata: Record<string, any>;
12
+ process?: Record<string, any>;
13
+ createdAt: string;
14
+ updatedAt?: string;
15
+ }
16
+
17
+ export class Task {
18
+ private readonly _resource: Resources.Task;
19
+ id: string;
20
+ indexId: string;
21
+ videoId?: string[];
22
+ estimatedTime?: string;
23
+ status: string;
24
+ metadata: Record<string, any>;
25
+ process?: TaskProcess;
26
+ hls?: TaskHLS;
27
+ createdAt: string;
28
+ updatedAt?: string;
29
+
30
+ constructor(resource: Resources.Task, data: TaskResponse) {
31
+ this._resource = resource;
32
+ this.id = data.id;
33
+ this.indexId = data.indexId;
34
+ this.videoId = data.videoId;
35
+ this.estimatedTime = data.estimatedTime;
36
+ this.status = data.status;
37
+ this.metadata = data.metadata;
38
+ this.process = data.process;
39
+ this.createdAt = data.createdAt;
40
+ this.updatedAt = data.updatedAt;
41
+ }
42
+
43
+ async retrieve(options: RequestOptions = {}): Promise<Task> {
44
+ return await this._resource.retrieve(this.id, options);
45
+ }
46
+
47
+ async delete(options: RequestOptions = {}): Promise<void> {
48
+ await this._resource.delete(this.id, options);
49
+ }
50
+
51
+ async waitForDone(sleepInterval: number = 5000, callback?: (task: Task) => void): Promise<Task> {
52
+ const isDone = () => this.status === 'ready' || this.status === 'failed';
53
+ if (sleepInterval <= 0) {
54
+ throw new Error('sleepInterval must be greater than 0');
55
+ }
56
+
57
+ let done = isDone();
58
+
59
+ while (!done) {
60
+ await this.sleep(sleepInterval);
61
+ const task = await this.retrieve();
62
+
63
+ this.estimatedTime = task.estimatedTime;
64
+ this.status = task.status;
65
+ this.metadata = task.metadata;
66
+ this.process = task.process;
67
+
68
+ done = isDone();
69
+
70
+ if (callback) {
71
+ callback(this);
72
+ }
73
+ }
74
+
75
+ return this;
76
+ }
77
+
78
+ private sleep(ms: number): Promise<void> {
79
+ return new Promise((resolve) => setTimeout(resolve, ms));
80
+ }
81
+ }
82
+
83
+ export class TaskListWithPagination {
84
+ private readonly _resource: Resources.Task;
85
+ private readonly _originParams: Resources.ListTaskParams;
86
+ data: Task[];
87
+ pageInfo: PageInfo;
88
+
89
+ constructor(
90
+ resource: Resources.Task,
91
+ originParams: Resources.ListTaskParams,
92
+ data: TaskResponse[],
93
+ pageInfo: PageInfo,
94
+ ) {
95
+ this._resource = resource;
96
+ this._originParams = originParams;
97
+ this.data = data.map((v) => new Task(resource, v));
98
+ this.pageInfo = pageInfo;
99
+ }
100
+
101
+ async next(): Promise<Task[] | null> {
102
+ if (this.pageInfo.page >= this.pageInfo.totalPage) {
103
+ return null;
104
+ }
105
+ const params = { ...this._originParams };
106
+ params.page = this.pageInfo.page + 1;
107
+ const res = await this._resource.listPagination(params);
108
+ this.pageInfo = res.pageInfo;
109
+ return res.data;
110
+ }
111
+ }
112
+
113
+ export interface TaskStatus {
114
+ indexId: string;
115
+ ready: number;
116
+ validating: number;
117
+ pending: number;
118
+ failed: number;
119
+ totalResult: number;
120
+ }
121
+
122
+ export interface TaskHLS {
123
+ videoUrl?: string;
124
+ thumbnailUrls?: string[];
125
+ status?: string;
126
+ updatedAt: string;
127
+ }
128
+
129
+ export interface TaskProcess {
130
+ percentage?: number;
131
+ remainSeconds?: number;
132
+ }