@meistrari/tela-sdk-js 0.0.3 → 0.0.4
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/dist/index.cjs +1312 -0
- package/dist/index.d.cts +897 -0
- package/dist/index.d.mts +897 -0
- package/dist/index.d.ts +808 -27
- package/dist/index.mjs +1291 -0
- package/package.json +14 -6
- package/dist/base-client.d.ts +0 -49
- package/dist/base-client.js +0 -299
- package/dist/errors.d.ts +0 -67
- package/dist/errors.js +0 -133
- package/dist/file.d.ts +0 -127
- package/dist/file.js +0 -161
- package/dist/index.js +0 -115
- package/dist/resource.d.ts +0 -8
- package/dist/resource.js +0 -9
- package/dist/resources/chat-completions.d.ts +0 -482
- package/dist/resources/chat-completions.js +0 -86
- package/dist/resources/index.d.ts +0 -1
- package/dist/resources/index.js +0 -1
- package/dist/streaming.d.ts +0 -81
- package/dist/streaming.js +0 -462
- package/dist/utils/constants.d.ts +0 -1
- package/dist/utils/constants.js +0 -21
- package/dist/utils/data-transformation.d.ts +0 -2
- package/dist/utils/data-transformation.js +0 -79
- package/dist/utils/stream.d.ts +0 -2
- package/dist/utils/stream.js +0 -11
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,897 @@
|
|
|
1
|
+
import { ReadStream } from 'node:fs';
|
|
2
|
+
import { ReadableStream } from 'node:stream/web';
|
|
3
|
+
|
|
4
|
+
type HTTPMethods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
5
|
+
type RequestOptions<Req = unknown | Record<string, unknown>> = {
|
|
6
|
+
method?: HTTPMethods;
|
|
7
|
+
path?: string;
|
|
8
|
+
query?: Req | undefined;
|
|
9
|
+
body?: Req | null | undefined;
|
|
10
|
+
headers?: Headers | undefined;
|
|
11
|
+
maxRetries?: number;
|
|
12
|
+
stream?: boolean | undefined;
|
|
13
|
+
timeout?: number;
|
|
14
|
+
signal?: AbortSignal | undefined | null;
|
|
15
|
+
};
|
|
16
|
+
interface BaseClientOptions {
|
|
17
|
+
baseURL: string;
|
|
18
|
+
maxRetries?: number;
|
|
19
|
+
timeout?: number;
|
|
20
|
+
}
|
|
21
|
+
declare abstract class BaseClient {
|
|
22
|
+
baseURL: string;
|
|
23
|
+
maxRetries: number;
|
|
24
|
+
timeout: number;
|
|
25
|
+
fetch: typeof fetch;
|
|
26
|
+
constructor({ baseURL, maxRetries, timeout }: BaseClientOptions);
|
|
27
|
+
protected createHeaders(opts?: Record<string, string>): Headers;
|
|
28
|
+
protected getUserAgent(): string;
|
|
29
|
+
protected getAuthHeaders(): Record<string, string>;
|
|
30
|
+
get<Req, Res>(path: string, opts?: RequestOptions<Req>): Promise<Res>;
|
|
31
|
+
post<Req, Res>(path: string, opts?: RequestOptions<Req>): Promise<Res>;
|
|
32
|
+
put<Req, Res>(path: string, opts?: RequestOptions<Req>): Promise<Res>;
|
|
33
|
+
patch<Req, Res>(path: string, opts?: RequestOptions<Req>): Promise<Res>;
|
|
34
|
+
delete<Req, Res>(path: string, opts?: RequestOptions<Req>): Promise<Res>;
|
|
35
|
+
private methodRequest;
|
|
36
|
+
private request;
|
|
37
|
+
private makeRequest;
|
|
38
|
+
private shouldRetry;
|
|
39
|
+
private retryRequest;
|
|
40
|
+
private calculateDefaultRetryTimeoutMillis;
|
|
41
|
+
fetchWithTimeout(url: string, _options: RequestInit, timeout: number, controller: AbortController): Promise<Response>;
|
|
42
|
+
protected buildRequest<Req>(options: RequestOptions<Req>): {
|
|
43
|
+
req: RequestInit;
|
|
44
|
+
url: string;
|
|
45
|
+
timeout: number;
|
|
46
|
+
};
|
|
47
|
+
protected buildURL<Req>(path: string, query: Req | null | undefined): string;
|
|
48
|
+
protected stringifyQuery(query: Record<string, unknown>): string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
declare class TelaError extends Error {
|
|
52
|
+
}
|
|
53
|
+
declare class ConflictApiKeyAndJWTError extends TelaError {
|
|
54
|
+
constructor();
|
|
55
|
+
}
|
|
56
|
+
declare class MissingApiKeyOrJWTError extends TelaError {
|
|
57
|
+
constructor();
|
|
58
|
+
}
|
|
59
|
+
declare class EmptyFileError extends TelaError {
|
|
60
|
+
constructor();
|
|
61
|
+
}
|
|
62
|
+
declare class InvalidFileURL extends TelaError {
|
|
63
|
+
constructor(url: string);
|
|
64
|
+
}
|
|
65
|
+
declare class FileUploadError extends TelaError {
|
|
66
|
+
constructor(message: string);
|
|
67
|
+
}
|
|
68
|
+
declare class APIError extends TelaError {
|
|
69
|
+
readonly statusCode: number | undefined;
|
|
70
|
+
readonly error: any;
|
|
71
|
+
constructor(statusCode: number | undefined, error: any, _message: string | undefined);
|
|
72
|
+
static from(statusCode: number, responseError: object | undefined, message: string | undefined): APIError;
|
|
73
|
+
}
|
|
74
|
+
declare class UserAbortError extends APIError {
|
|
75
|
+
readonly statusCode: undefined;
|
|
76
|
+
constructor({ message }?: {
|
|
77
|
+
message?: string;
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
declare class ConnectionError extends APIError {
|
|
81
|
+
readonly statusCode: undefined;
|
|
82
|
+
constructor({ message, cause, }: {
|
|
83
|
+
message?: string | undefined;
|
|
84
|
+
cause?: Error | undefined;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
declare class ConnectionTimeout extends APIError {
|
|
88
|
+
readonly statusCode: undefined;
|
|
89
|
+
constructor({ message, }?: {
|
|
90
|
+
message?: string | undefined;
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
declare class BadRequestError extends APIError {
|
|
94
|
+
readonly statusCode = 400;
|
|
95
|
+
}
|
|
96
|
+
declare class AuthenticationError extends APIError {
|
|
97
|
+
readonly statusCode = 401;
|
|
98
|
+
}
|
|
99
|
+
declare class AuthorizationError extends APIError {
|
|
100
|
+
readonly statusCode = 403;
|
|
101
|
+
}
|
|
102
|
+
declare class NotFoundError extends APIError {
|
|
103
|
+
readonly statusCode = 404;
|
|
104
|
+
}
|
|
105
|
+
declare class ConflictError extends APIError {
|
|
106
|
+
readonly statusCode = 409;
|
|
107
|
+
}
|
|
108
|
+
declare class UnprocessableEntityError extends APIError {
|
|
109
|
+
readonly statusCode = 422;
|
|
110
|
+
}
|
|
111
|
+
declare class RateLimitError extends APIError {
|
|
112
|
+
readonly statusCode = 429;
|
|
113
|
+
}
|
|
114
|
+
declare class InternalServerError extends APIError {
|
|
115
|
+
readonly statusCode = 500;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Configuration options for `TelaFile`.
|
|
120
|
+
*/
|
|
121
|
+
type TelaFileOptions = {
|
|
122
|
+
/**
|
|
123
|
+
* Defines the page range of the document to be processed.
|
|
124
|
+
*
|
|
125
|
+
* Specifies the range of pages to process, starting from `startPage` to `endPage`, inclusive, 0-indexed.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* const file = new TelaFile(fileStream, {
|
|
130
|
+
* range: [0, 1],
|
|
131
|
+
* })
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
range?: [number, number];
|
|
135
|
+
/**
|
|
136
|
+
* Defines the parser provider to be used for the file.
|
|
137
|
+
*/
|
|
138
|
+
parserType?: string;
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* Represents the possible types for a file input,
|
|
142
|
+
* which can be a URL string, Uint8Array, ReadableStream, ReadStream, Blob, or File.
|
|
143
|
+
*/
|
|
144
|
+
type TelaFileInput = string | Uint8Array | ReadableStream | ReadStream | Blob | File;
|
|
145
|
+
/**
|
|
146
|
+
* Represents a file with support for various types including URLs, binary data, streams, and Blobs.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```typescript
|
|
150
|
+
* // Using a URL string
|
|
151
|
+
* const urlFile = new TelaFile("https://example.com/file.png", { range: { start: 0, end: 1024 } })
|
|
152
|
+
*
|
|
153
|
+
* // Using a Uint8Array (buffer)
|
|
154
|
+
* const buffer = new Uint8Array([/* binary data * /])
|
|
155
|
+
* const bufferFile = new TelaFile(buffer)
|
|
156
|
+
*
|
|
157
|
+
* // Using a ReadStream
|
|
158
|
+
* import { createReadStream } from 'fs'
|
|
159
|
+
* const readStream = createReadStream('path/to/file')
|
|
160
|
+
* const streamFile = new TelaFile(readStream)
|
|
161
|
+
*
|
|
162
|
+
* // Using a ReadableStream
|
|
163
|
+
* const readableStream = new ReadableStream({
|
|
164
|
+
* start(controller) {
|
|
165
|
+
* controller.enqueue(new TextEncoder().encode('Hello, world!'))
|
|
166
|
+
* controller.close()
|
|
167
|
+
* }
|
|
168
|
+
* })
|
|
169
|
+
* const readableStreamFile = new TelaFile(readableStream)
|
|
170
|
+
*
|
|
171
|
+
* // Using a Blob (in environments that support Blob)
|
|
172
|
+
* const blob = new Blob(['file content'], { type: 'text/plain' })
|
|
173
|
+
* const blobFile = new TelaFile(blob)
|
|
174
|
+
*
|
|
175
|
+
* // Using a File (in browser environments)
|
|
176
|
+
* const file = new File(['file content'], 'filename.txt', { type: 'text/plain' })
|
|
177
|
+
* const fileInstance = new TelaFile(file)
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
declare class TelaFile {
|
|
181
|
+
private _file;
|
|
182
|
+
private _options;
|
|
183
|
+
private _size;
|
|
184
|
+
/**
|
|
185
|
+
* Creates an instance of `TelaFile`.
|
|
186
|
+
*
|
|
187
|
+
* @param file - The source of the file. Can be a URL string, Uint8Array, ReadableStream, ReadStream, Blob, or File.
|
|
188
|
+
* @param options - Optional configuration options such as byte range.
|
|
189
|
+
* @throws {InvalidFileURL} If the provided URL is not valid.
|
|
190
|
+
* @throws {EmptyFileError} If the provided file is empty.
|
|
191
|
+
*/
|
|
192
|
+
constructor(file: string | Uint8Array | ReadableStream | ReadStream | Blob | File, options?: TelaFileOptions);
|
|
193
|
+
/**
|
|
194
|
+
* Retrieves the options provided during instantiation.
|
|
195
|
+
*
|
|
196
|
+
* @returns The `TelaFileOptions` or an empty object if none were provided.
|
|
197
|
+
*/
|
|
198
|
+
get options(): TelaFileOptions;
|
|
199
|
+
/**
|
|
200
|
+
* Determines whether the file source is a valid URL.
|
|
201
|
+
*
|
|
202
|
+
* @returns `true` if the file source is a valid URL string, otherwise `false`.
|
|
203
|
+
*/
|
|
204
|
+
get isURL(): boolean;
|
|
205
|
+
/**
|
|
206
|
+
* Gets the size of the file in bytes.
|
|
207
|
+
*
|
|
208
|
+
* @returns The size of the file if available, otherwise `null`.
|
|
209
|
+
*/
|
|
210
|
+
get size(): number | null;
|
|
211
|
+
/**
|
|
212
|
+
* Retrieves the content of the file in a format suitable for uploading.
|
|
213
|
+
*
|
|
214
|
+
* - If the file is a URL, it returns the URL string.
|
|
215
|
+
* - If the file is a `Uint8Array`, it converts it to a `File` object.
|
|
216
|
+
* - If the file is a `ReadStream` or `ReadableStream`, it calculates the size and returns a stream.
|
|
217
|
+
*
|
|
218
|
+
* @returns A promise that resolves to the uploadable content.
|
|
219
|
+
*/
|
|
220
|
+
getUploadableContent(): Promise<string | File | Blob | ReadableStream>;
|
|
221
|
+
/**
|
|
222
|
+
* Validates the provided file based on its type.
|
|
223
|
+
*
|
|
224
|
+
* @throws {InvalidFileURL} If the file is a string but not a valid URL.
|
|
225
|
+
* @throws {EmptyFileError} If the file is empty.
|
|
226
|
+
*/
|
|
227
|
+
private validateFile;
|
|
228
|
+
/**
|
|
229
|
+
* Checks if the provided string is a valid URL.
|
|
230
|
+
*
|
|
231
|
+
* @param url - The URL string to validate.
|
|
232
|
+
* @returns `true` if the URL is valid, otherwise `false`.
|
|
233
|
+
*/
|
|
234
|
+
private isValidURL;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Creates a new `TelaFile` instance from the provided file input.
|
|
238
|
+
*
|
|
239
|
+
* @param file - The file input to create a `TelaFile` instance from.
|
|
240
|
+
* @returns A new `TelaFile` instance.
|
|
241
|
+
*/
|
|
242
|
+
declare function createTelaFile(file: TelaFileInput, options?: TelaFileOptions): TelaFile;
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Represents a stream of items that can be asynchronously iterated over.
|
|
246
|
+
*/
|
|
247
|
+
declare class Stream<Item> implements AsyncIterable<Item> {
|
|
248
|
+
private iterator;
|
|
249
|
+
/**
|
|
250
|
+
* The AbortController associated with this stream.
|
|
251
|
+
*/
|
|
252
|
+
controller: AbortController;
|
|
253
|
+
/**
|
|
254
|
+
* Creates a new Stream instance.
|
|
255
|
+
*
|
|
256
|
+
* @param iterator - A function that returns an AsyncIterator<Item>.
|
|
257
|
+
* @param controller - The AbortController for this stream.
|
|
258
|
+
*/
|
|
259
|
+
constructor(iterator: () => AsyncIterator<Item>, controller: AbortController);
|
|
260
|
+
/**
|
|
261
|
+
* Creates a Stream from a server-sent events (SSE) Response.
|
|
262
|
+
*
|
|
263
|
+
* @param response - The Response object containing the SSE data.
|
|
264
|
+
* @param controller - The AbortController for this stream.
|
|
265
|
+
* @returns A new Stream instance.
|
|
266
|
+
* @throws {Error} If the stream is consumed more than once.
|
|
267
|
+
* @throws {APIError} If the SSE data contains an error.
|
|
268
|
+
*/
|
|
269
|
+
static fromSSEResponse<Item>(response: Response, controller: AbortController): Stream<Item>;
|
|
270
|
+
/**
|
|
271
|
+
* Creates a Stream from a newline-separated ReadableStream where each item is a JSON value.
|
|
272
|
+
*
|
|
273
|
+
* @param readableStream - The ReadableStream containing newline-separated JSON data.
|
|
274
|
+
* @param controller - The AbortController for this stream.
|
|
275
|
+
* @returns A new Stream instance.
|
|
276
|
+
* @throws {Error} If the stream is consumed more than once.
|
|
277
|
+
*/
|
|
278
|
+
static fromReadableStream<Item>(readableStream: ReadableStream, controller: AbortController): Stream<Item>;
|
|
279
|
+
/**
|
|
280
|
+
* Implements the AsyncIterable interface, allowing the Stream to be used in a for-await-of loop.
|
|
281
|
+
*
|
|
282
|
+
* @returns An AsyncIterator for the stream items.
|
|
283
|
+
*/
|
|
284
|
+
[Symbol.asyncIterator](): AsyncIterator<Item>;
|
|
285
|
+
/**
|
|
286
|
+
* Splits the stream into two streams which can be independently read from at different speeds.
|
|
287
|
+
*
|
|
288
|
+
* @returns A tuple containing two new Stream instances.
|
|
289
|
+
*/
|
|
290
|
+
tee(): [Stream<Item>, Stream<Item>];
|
|
291
|
+
/**
|
|
292
|
+
* Converts this stream to a newline-separated ReadableStream of JSON stringified values.
|
|
293
|
+
* The resulting stream can be converted back to a Stream using `Stream.fromReadableStream()`.
|
|
294
|
+
*
|
|
295
|
+
* @returns A ReadableStream containing newline-separated JSON data.
|
|
296
|
+
*/
|
|
297
|
+
toReadableStream(): ReadableStream;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Base class for all resources.
|
|
302
|
+
*/
|
|
303
|
+
declare class Resource {
|
|
304
|
+
protected _client: BaseClient;
|
|
305
|
+
constructor(client: BaseClient);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Manages chat completion operations.
|
|
310
|
+
*
|
|
311
|
+
* Provides methods to create chat completions, handle streaming responses,
|
|
312
|
+
* and manage file uploads associated with chat completions.
|
|
313
|
+
*
|
|
314
|
+
* @example
|
|
315
|
+
* ```typescript
|
|
316
|
+
* const tela = new TelaSDK({
|
|
317
|
+
* apiKey: 'your-api-key',
|
|
318
|
+
* });
|
|
319
|
+
* const response = await tela.completions.create({
|
|
320
|
+
* canvasId: "your-canvas-id",
|
|
321
|
+
* messages: [{ role: "user", content: "Hello!" }],
|
|
322
|
+
* });
|
|
323
|
+
* console.log(response.choices[0].message.content);
|
|
324
|
+
* ```
|
|
325
|
+
*/
|
|
326
|
+
declare class ChatCompletions extends Resource {
|
|
327
|
+
/**
|
|
328
|
+
* Creates a chat completion with various input options and response formats.
|
|
329
|
+
*
|
|
330
|
+
* This method supports synchronous responses, streaming responses, and webhook-based asynchronous processing.
|
|
331
|
+
*
|
|
332
|
+
* @param body - The parameters for creating a chat completion.
|
|
333
|
+
* @param opts - Additional request options.
|
|
334
|
+
* @returns A Promise that resolves to either a stream of chat completion responses,
|
|
335
|
+
* a single chat completion response, or a webhook response, depending on the input parameters.
|
|
336
|
+
*
|
|
337
|
+
* @example
|
|
338
|
+
* ```typescript
|
|
339
|
+
* // Synchronous response
|
|
340
|
+
* const response = await tela.completions.create({
|
|
341
|
+
* canvasId: "your-canvas-id",
|
|
342
|
+
* messages: [{ role: "user", content: "Tell me a joke." }],
|
|
343
|
+
* });
|
|
344
|
+
*
|
|
345
|
+
* // Streaming response
|
|
346
|
+
* const stream = await tela.completions.create({
|
|
347
|
+
* canvasId: "your-canvas-id",
|
|
348
|
+
* messages: [{ role: "user", content: "Tell me a story." }],
|
|
349
|
+
* stream: true,
|
|
350
|
+
* });
|
|
351
|
+
*
|
|
352
|
+
* // Webhook response
|
|
353
|
+
* const webhookResponse = await tela.completions.create({
|
|
354
|
+
* canvasId: "your-canvas-id",
|
|
355
|
+
* messages: [{ role: "user", content: "Generate a report." }],
|
|
356
|
+
* webhookUrl: "https://example.com/webhook",
|
|
357
|
+
* });
|
|
358
|
+
* ```
|
|
359
|
+
*/
|
|
360
|
+
create<Variables = ChatCompletionVariables, CompletionContent = string>(body: ChatCompletionCreateWebhookParams<Variables>, opts?: RequestOptions<ChatCompletionCreateParams<Variables>>): Promise<ChatCompletionWebhookResponse<CompletionContent>>;
|
|
361
|
+
create<Variables = ChatCompletionVariables, CompletionContent = string>(body: CompletionCreateParamsNonStreaming<Variables>, opts?: RequestOptions<ChatCompletionCreateParams<Variables>>): Promise<ChatCompletionCreateResponse<CompletionContent>>;
|
|
362
|
+
create<Variables = ChatCompletionVariables, CompletionContent = string>(body: CompletionCreateParamsStreaming<Variables>, opts?: RequestOptions<ChatCompletionCreateParams<Variables>>): Promise<Stream<ChatCompletionCreateStreamingResponse<CompletionContent>>>;
|
|
363
|
+
create<Variables = ChatCompletionVariables, CompletionContent = string>(body: ChatCompletionCreateParamsBase<Variables>, opts?: RequestOptions<ChatCompletionCreateParams<Variables>>): Promise<Stream<ChatCompletionCreateStreamingResponse<CompletionContent>> | ChatCompletionCreateResponse<CompletionContent> | ChatCompletionWebhookResponse<CompletionContent>>;
|
|
364
|
+
/**
|
|
365
|
+
* Uploads a file and returns its URL and options.
|
|
366
|
+
*
|
|
367
|
+
* This is used internally to handle file uploads associated with chat completions.
|
|
368
|
+
*
|
|
369
|
+
* @param file - The TelaFile to be uploaded.
|
|
370
|
+
* @returns A Promise that resolves to an object containing the file URL and options.
|
|
371
|
+
* @throws {FileUploadError} If the file upload fails.
|
|
372
|
+
*
|
|
373
|
+
* @example
|
|
374
|
+
* ```typescript
|
|
375
|
+
* const file = new TelaFile({ /* file options *\/ });
|
|
376
|
+
* const uploadedFile = await this.uploadFile(file);
|
|
377
|
+
* console.log(uploadedFile.file_url);
|
|
378
|
+
* ```
|
|
379
|
+
*/
|
|
380
|
+
private uploadFile;
|
|
381
|
+
}
|
|
382
|
+
type ChatCompletionVariablesValue = string | number | boolean | TelaFile;
|
|
383
|
+
/**
|
|
384
|
+
* Represents the variables available in the canvas.
|
|
385
|
+
* Keys are strings, and values can be strings, numbers, booleans, or TelaFile instances.
|
|
386
|
+
*/
|
|
387
|
+
type ChatCompletionVariables = Record<string, ChatCompletionVariablesValue>;
|
|
388
|
+
/**
|
|
389
|
+
* Represents the processed variables with file URLs.
|
|
390
|
+
* Similar to ChatCompletionVariables, but TelaFile instances are replaced with their URLs.
|
|
391
|
+
*/
|
|
392
|
+
type ChatCompletionVariablesWithFileURL = Record<string, string | number | boolean | {
|
|
393
|
+
fileUrl: string;
|
|
394
|
+
}>;
|
|
395
|
+
/**
|
|
396
|
+
* Base parameters for creating a chat completion.
|
|
397
|
+
*/
|
|
398
|
+
interface ChatCompletionCreateParamsBase<Variables = ChatCompletionVariables> {
|
|
399
|
+
/**
|
|
400
|
+
* The version ID of the canvas to use for this chat completion.
|
|
401
|
+
* If not provided, the latest version of the specified canvas will be used.
|
|
402
|
+
*/
|
|
403
|
+
versionId?: string;
|
|
404
|
+
/**
|
|
405
|
+
* The ID of the canvas to use for this chat completion.
|
|
406
|
+
* This is required if an applicationId is not provided.
|
|
407
|
+
*/
|
|
408
|
+
canvasId?: string;
|
|
409
|
+
/**
|
|
410
|
+
* The ID of the deployed application to use for this chat completion.
|
|
411
|
+
* This is required if a canvasId is not provided.
|
|
412
|
+
*/
|
|
413
|
+
applicationId?: string;
|
|
414
|
+
/**
|
|
415
|
+
* The URL to receive a webhook notification when the chat completion is finished.
|
|
416
|
+
* If provided, the completion will be processed asynchronously.
|
|
417
|
+
*/
|
|
418
|
+
webhookUrl?: string | null | undefined;
|
|
419
|
+
/**
|
|
420
|
+
* Variables to be passed to the canvas for this chat completion.
|
|
421
|
+
* These can be used to customize the behavior of the canvas for this specific request.
|
|
422
|
+
*/
|
|
423
|
+
variables?: Variables;
|
|
424
|
+
/**
|
|
425
|
+
* An array of previous messages in the conversation.
|
|
426
|
+
* Each message should have a 'role' (e.g., 'user', 'assistant') and 'content'.
|
|
427
|
+
*/
|
|
428
|
+
messages?: Array<{
|
|
429
|
+
role: string;
|
|
430
|
+
content: string;
|
|
431
|
+
}>;
|
|
432
|
+
/**
|
|
433
|
+
* Override default settings specified in the canvas.
|
|
434
|
+
* This allows for fine-tuning the completion behavior for this specific request.
|
|
435
|
+
*/
|
|
436
|
+
override?: {
|
|
437
|
+
/**
|
|
438
|
+
* The specific model to use for this completion.
|
|
439
|
+
* If not specified, the default model set in the canvas will be used.
|
|
440
|
+
*/
|
|
441
|
+
model?: string;
|
|
442
|
+
/**
|
|
443
|
+
* Controls randomness in the output. Values between 0 and 1.
|
|
444
|
+
* Lower values make the output more focused and deterministic.
|
|
445
|
+
*/
|
|
446
|
+
temperature?: number;
|
|
447
|
+
/**
|
|
448
|
+
* Specifies whether to use a chat or completion model.
|
|
449
|
+
*/
|
|
450
|
+
type: 'chat' | 'completion';
|
|
451
|
+
/**
|
|
452
|
+
* An array of functions that the model can call.
|
|
453
|
+
* This allows for more structured and interactive completions.
|
|
454
|
+
*/
|
|
455
|
+
functions?: Array<{
|
|
456
|
+
id: string;
|
|
457
|
+
name: string;
|
|
458
|
+
description?: string;
|
|
459
|
+
parameters?: {
|
|
460
|
+
type: 'object';
|
|
461
|
+
properties: Record<string, unknown>;
|
|
462
|
+
required?: string[];
|
|
463
|
+
};
|
|
464
|
+
}>;
|
|
465
|
+
/**
|
|
466
|
+
* Configuration for generating structured output.
|
|
467
|
+
*/
|
|
468
|
+
structuredOutput?: {
|
|
469
|
+
/**
|
|
470
|
+
* Whether to enable structured output for this completion.
|
|
471
|
+
*/
|
|
472
|
+
enabled: boolean;
|
|
473
|
+
/**
|
|
474
|
+
* The schema defining the structure of the output.
|
|
475
|
+
* This is used when structured output is enabled.
|
|
476
|
+
*/
|
|
477
|
+
schema?: {
|
|
478
|
+
properties: Record<string, unknown>;
|
|
479
|
+
type: 'object';
|
|
480
|
+
title: string;
|
|
481
|
+
description: string;
|
|
482
|
+
required: string[];
|
|
483
|
+
};
|
|
484
|
+
};
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Parameters for creating a streaming chat completion.
|
|
489
|
+
*/
|
|
490
|
+
interface CompletionCreateParamsStreaming<T> extends ChatCompletionCreateParamsBase<T> {
|
|
491
|
+
/**
|
|
492
|
+
* Whether the response should be streamed.
|
|
493
|
+
*/
|
|
494
|
+
stream: true;
|
|
495
|
+
/**
|
|
496
|
+
* The webhook URL, if you want to receive a webhook when the completion is completed.
|
|
497
|
+
*/
|
|
498
|
+
webhookUrl?: undefined | null;
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* Parameters for creating a non-streaming chat completion.
|
|
502
|
+
*/
|
|
503
|
+
interface CompletionCreateParamsNonStreaming<T> extends ChatCompletionCreateParamsBase<T> {
|
|
504
|
+
/**
|
|
505
|
+
* Whether the response should be streamed.
|
|
506
|
+
*/
|
|
507
|
+
stream?: false | undefined | null;
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* Parameters for creating a chat completion with a webhook.
|
|
511
|
+
*/
|
|
512
|
+
interface ChatCompletionCreateWebhookParams<T> extends CompletionCreateParamsNonStreaming<T> {
|
|
513
|
+
/**
|
|
514
|
+
* The webhook URL, if you want to receive a webhook when the completion is completed.
|
|
515
|
+
*/
|
|
516
|
+
webhookUrl: string;
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* Union type for all possible chat completion creation parameters.
|
|
520
|
+
*/
|
|
521
|
+
type ChatCompletionCreateParams<T> = CompletionCreateParamsStreaming<T> | CompletionCreateParamsNonStreaming<T> | ChatCompletionCreateWebhookParams<T>;
|
|
522
|
+
/**
|
|
523
|
+
* Response structure for a created chat completion.
|
|
524
|
+
*/
|
|
525
|
+
type ChatCompletionCreateResponse<T> = {
|
|
526
|
+
/**
|
|
527
|
+
* The ID of the completion.
|
|
528
|
+
*/
|
|
529
|
+
id: string;
|
|
530
|
+
/**
|
|
531
|
+
* The object of the completion.
|
|
532
|
+
*/
|
|
533
|
+
object: string;
|
|
534
|
+
/**
|
|
535
|
+
* The created time of the completion.
|
|
536
|
+
*/
|
|
537
|
+
created: number;
|
|
538
|
+
/**
|
|
539
|
+
* The choices of the completion.
|
|
540
|
+
*/
|
|
541
|
+
choices: Array<{
|
|
542
|
+
message: {
|
|
543
|
+
role: string;
|
|
544
|
+
content: T;
|
|
545
|
+
};
|
|
546
|
+
}>;
|
|
547
|
+
};
|
|
548
|
+
/**
|
|
549
|
+
* Response structure for a chat completion streaming response.
|
|
550
|
+
*/
|
|
551
|
+
type ChatCompletionCreateStreamingResponse<T> = {
|
|
552
|
+
/**
|
|
553
|
+
* The ID of the completion.
|
|
554
|
+
*/
|
|
555
|
+
id: string;
|
|
556
|
+
/**
|
|
557
|
+
* The object of the completion.
|
|
558
|
+
*/
|
|
559
|
+
object: string;
|
|
560
|
+
/**
|
|
561
|
+
* The created time of the completion.
|
|
562
|
+
*/
|
|
563
|
+
created: number;
|
|
564
|
+
/**
|
|
565
|
+
* The model of the completion.
|
|
566
|
+
*/
|
|
567
|
+
model: string;
|
|
568
|
+
/**
|
|
569
|
+
* The choices of the completion.
|
|
570
|
+
*/
|
|
571
|
+
choices: Array<{
|
|
572
|
+
delta: {
|
|
573
|
+
role: string | null;
|
|
574
|
+
content: string | null;
|
|
575
|
+
functionCall: {
|
|
576
|
+
name: string | null;
|
|
577
|
+
arguments: string | null;
|
|
578
|
+
} | null;
|
|
579
|
+
};
|
|
580
|
+
finishReason: string | null;
|
|
581
|
+
index: number;
|
|
582
|
+
logprobs: number | null;
|
|
583
|
+
}>;
|
|
584
|
+
/**
|
|
585
|
+
* The message of the completion.
|
|
586
|
+
*/
|
|
587
|
+
message: {
|
|
588
|
+
role: string;
|
|
589
|
+
content: string | null;
|
|
590
|
+
functionCall: {
|
|
591
|
+
name: string | null;
|
|
592
|
+
arguments: string | null;
|
|
593
|
+
} | null;
|
|
594
|
+
toolCalls: Array<{
|
|
595
|
+
name: string;
|
|
596
|
+
arguments: string;
|
|
597
|
+
}>;
|
|
598
|
+
structuredContent: T;
|
|
599
|
+
};
|
|
600
|
+
};
|
|
601
|
+
/**
|
|
602
|
+
* Response structure for a chat completion webhook.
|
|
603
|
+
* This interface represents the data sent to the webhook URL when a chat completion is finished.
|
|
604
|
+
*/
|
|
605
|
+
interface ChatCompletionWebhookResponse<T> {
|
|
606
|
+
/**
|
|
607
|
+
* Unique identifier for this chat completion response.
|
|
608
|
+
*/
|
|
609
|
+
id: string;
|
|
610
|
+
/**
|
|
611
|
+
* The current status of the chat completion.
|
|
612
|
+
* - 'created': The request has been received but processing hasn't started.
|
|
613
|
+
* - 'running': The chat completion is currently being processed.
|
|
614
|
+
* - 'succeeded': The chat completion has finished successfully.
|
|
615
|
+
* - 'failed': The chat completion encountered an error and couldn't complete.
|
|
616
|
+
*/
|
|
617
|
+
status: 'created' | 'failed' | 'running' | 'succeeded';
|
|
618
|
+
/**
|
|
619
|
+
* Contains information about the input provided for this chat completion.
|
|
620
|
+
*/
|
|
621
|
+
inputContent: {
|
|
622
|
+
/**
|
|
623
|
+
* Variables passed to the chat completion, including any file URLs.
|
|
624
|
+
*/
|
|
625
|
+
variables: ChatCompletionVariablesWithFileURL;
|
|
626
|
+
/**
|
|
627
|
+
* Array of messages that were part of the input.
|
|
628
|
+
*/
|
|
629
|
+
messages: Array<{
|
|
630
|
+
/**
|
|
631
|
+
* The role of the message sender (e.g., 'user', 'assistant', 'system').
|
|
632
|
+
*/
|
|
633
|
+
role: string;
|
|
634
|
+
/**
|
|
635
|
+
* The content of the message. Can be a string for text messages,
|
|
636
|
+
* or an object for more complex content types like images.
|
|
637
|
+
*/
|
|
638
|
+
content: string | {
|
|
639
|
+
type: 'text';
|
|
640
|
+
text: string;
|
|
641
|
+
} | {
|
|
642
|
+
type: 'image_url';
|
|
643
|
+
imageUrl: {
|
|
644
|
+
url: string;
|
|
645
|
+
detail?: 'auto' | 'high' | 'low';
|
|
646
|
+
};
|
|
647
|
+
};
|
|
648
|
+
/**
|
|
649
|
+
* Optional. Information about a function call, if applicable.
|
|
650
|
+
*/
|
|
651
|
+
functionCall?: {
|
|
652
|
+
name: string;
|
|
653
|
+
arguments: string;
|
|
654
|
+
};
|
|
655
|
+
}>;
|
|
656
|
+
/**
|
|
657
|
+
* Information about files used in the chat completion.
|
|
658
|
+
*/
|
|
659
|
+
files: Array<{
|
|
660
|
+
name: string;
|
|
661
|
+
usedAt: 'variables' | 'messages';
|
|
662
|
+
parsedContent: string;
|
|
663
|
+
url: string;
|
|
664
|
+
type: 'image' | 'file';
|
|
665
|
+
}>;
|
|
666
|
+
};
|
|
667
|
+
/**
|
|
668
|
+
* Contains the output generated by the chat completion.
|
|
669
|
+
*/
|
|
670
|
+
outputContent: {
|
|
671
|
+
/**
|
|
672
|
+
* The role of the entity providing the output.
|
|
673
|
+
*/
|
|
674
|
+
role: 'system' | 'user' | 'assistant' | 'function';
|
|
675
|
+
/**
|
|
676
|
+
* Optional. Information about tool calls made during the completion.
|
|
677
|
+
*/
|
|
678
|
+
toolCalls?: Array<{
|
|
679
|
+
name: string;
|
|
680
|
+
arguments: string;
|
|
681
|
+
}>;
|
|
682
|
+
/**
|
|
683
|
+
* Optional. Additional information about the completion process.
|
|
684
|
+
*/
|
|
685
|
+
information?: {
|
|
686
|
+
duration: number;
|
|
687
|
+
cost?: number;
|
|
688
|
+
inputTokens?: number;
|
|
689
|
+
outputTokens?: number;
|
|
690
|
+
};
|
|
691
|
+
/**
|
|
692
|
+
* Indicates whether the output has a structured format.
|
|
693
|
+
*/
|
|
694
|
+
hasStructuredOutput?: boolean;
|
|
695
|
+
/**
|
|
696
|
+
* The main content of the chat completion output.
|
|
697
|
+
*/
|
|
698
|
+
content: T;
|
|
699
|
+
/**
|
|
700
|
+
* Optional. The input messages in a string format.
|
|
701
|
+
*/
|
|
702
|
+
inputMessages?: string | undefined;
|
|
703
|
+
};
|
|
704
|
+
/**
|
|
705
|
+
* The original input parameters provided for this chat completion.
|
|
706
|
+
*/
|
|
707
|
+
rawInput: {
|
|
708
|
+
versionId?: string;
|
|
709
|
+
canvasId?: string;
|
|
710
|
+
applicationId?: string;
|
|
711
|
+
webhookUrl?: string;
|
|
712
|
+
variables?: ChatCompletionVariablesWithFileURL;
|
|
713
|
+
messages?: Array<{
|
|
714
|
+
role: string;
|
|
715
|
+
content: string | {
|
|
716
|
+
type: 'text';
|
|
717
|
+
text: string;
|
|
718
|
+
} | {
|
|
719
|
+
type: 'image_url';
|
|
720
|
+
imageUrl: {
|
|
721
|
+
url: string;
|
|
722
|
+
detail?: 'auto' | 'high' | 'low';
|
|
723
|
+
};
|
|
724
|
+
};
|
|
725
|
+
}>;
|
|
726
|
+
override?: {
|
|
727
|
+
model?: string;
|
|
728
|
+
temperature?: number;
|
|
729
|
+
type: 'chat' | 'completion';
|
|
730
|
+
functions?: Array<{
|
|
731
|
+
id: string;
|
|
732
|
+
name: string;
|
|
733
|
+
description?: string;
|
|
734
|
+
parameters?: {
|
|
735
|
+
type: 'object';
|
|
736
|
+
properties: Record<string, unknown>;
|
|
737
|
+
required?: string[];
|
|
738
|
+
};
|
|
739
|
+
}>;
|
|
740
|
+
structuredOutput?: {
|
|
741
|
+
enabled: boolean;
|
|
742
|
+
schema?: {
|
|
743
|
+
properties: Record<string, unknown>;
|
|
744
|
+
type: 'object';
|
|
745
|
+
title: string;
|
|
746
|
+
description: string;
|
|
747
|
+
required: string[];
|
|
748
|
+
};
|
|
749
|
+
};
|
|
750
|
+
};
|
|
751
|
+
} | null;
|
|
752
|
+
/**
|
|
753
|
+
* The raw output from the chat completion process.
|
|
754
|
+
*/
|
|
755
|
+
rawOutput: Record<string, unknown>;
|
|
756
|
+
/**
|
|
757
|
+
* The date used for compatibility checks.
|
|
758
|
+
*/
|
|
759
|
+
compatibilityDate: Date;
|
|
760
|
+
/**
|
|
761
|
+
* The ID of the prompt version used for this chat completion.
|
|
762
|
+
*/
|
|
763
|
+
promptVersionId: string;
|
|
764
|
+
/**
|
|
765
|
+
* The ID of the prompt application, if applicable.
|
|
766
|
+
*/
|
|
767
|
+
promptApplicationId: string | null;
|
|
768
|
+
/**
|
|
769
|
+
* The ID of the workspace where this chat completion was executed.
|
|
770
|
+
*/
|
|
771
|
+
workspaceId: string;
|
|
772
|
+
/**
|
|
773
|
+
* The timestamp when this chat completion was created.
|
|
774
|
+
*/
|
|
775
|
+
createdAt: string;
|
|
776
|
+
/**
|
|
777
|
+
* The timestamp when this chat completion was last updated.
|
|
778
|
+
*/
|
|
779
|
+
updatedAt: string;
|
|
780
|
+
/**
|
|
781
|
+
* The timestamp when this chat completion was deleted, if applicable.
|
|
782
|
+
*/
|
|
783
|
+
deletedAt: string | null;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* Configuration options for the TelaSDK.
|
|
788
|
+
*
|
|
789
|
+
* @property {string} [apiKey] - The API key for authentication. Required if `jwt` is not provided.
|
|
790
|
+
* @property {string} [jwt] - The JWT token for authentication. Required if `apiKey` is not provided.
|
|
791
|
+
* @property {string} [baseURL] - The base URL for the Tela API. Defaults to 'https://api.tela.com'.
|
|
792
|
+
*/
|
|
793
|
+
interface TelaSDKOptions extends Omit<BaseClientOptions, 'baseURL'> {
|
|
794
|
+
/**
|
|
795
|
+
* The API key for authentication. Required if `jwt` is not provided.
|
|
796
|
+
*/
|
|
797
|
+
apiKey?: string;
|
|
798
|
+
/**
|
|
799
|
+
* The JWT token for authentication. Required if `apiKey` is not provided.
|
|
800
|
+
*/
|
|
801
|
+
jwt?: string;
|
|
802
|
+
/**
|
|
803
|
+
* The base URL for the Tela API. Defaults to 'https://api.tela.com'.
|
|
804
|
+
*/
|
|
805
|
+
baseURL?: string;
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
* The main class for interacting with the Tela API.
|
|
809
|
+
*
|
|
810
|
+
* Provides methods to access various Tela API resources.
|
|
811
|
+
*
|
|
812
|
+
* @example
|
|
813
|
+
* ```typescript
|
|
814
|
+
* const tela = new TelaSDK({ apiKey: 'your-api-key' });
|
|
815
|
+
* const completion = await tela.completions.create({
|
|
816
|
+
* canvasId: "your-canvas-id",
|
|
817
|
+
* messages: [{ role: "user", content: "Hello!" }],
|
|
818
|
+
* });
|
|
819
|
+
* ```
|
|
820
|
+
*/
|
|
821
|
+
declare class TelaSDK extends BaseClient {
|
|
822
|
+
private opts;
|
|
823
|
+
private apiKey;
|
|
824
|
+
private jwt;
|
|
825
|
+
/**
|
|
826
|
+
* Creates a new instance of the TelaSDK.
|
|
827
|
+
*/
|
|
828
|
+
constructor({ baseURL, apiKey, jwt, ...rest }: TelaSDKOptions);
|
|
829
|
+
private validateAuth;
|
|
830
|
+
protected getAuthHeaders(): Record<string, string>;
|
|
831
|
+
/**
|
|
832
|
+
* The ChatCompletions resource for interacting with Tela's chat completion API.
|
|
833
|
+
*
|
|
834
|
+
* Use this to generate chat completions based on provided messages.
|
|
835
|
+
*
|
|
836
|
+
* @example
|
|
837
|
+
* ```typescript
|
|
838
|
+
* const completion = await tela.completions.create({
|
|
839
|
+
* canvasId: "your-canvas-id",
|
|
840
|
+
* messages: [{ role: "user", content: "Hello!" }],
|
|
841
|
+
* });
|
|
842
|
+
* ```
|
|
843
|
+
*/
|
|
844
|
+
completions: ChatCompletions;
|
|
845
|
+
/**
|
|
846
|
+
* Creates a new `TelaFile` instance from the provided file input.
|
|
847
|
+
*
|
|
848
|
+
* @param file - The file input to create a `TelaFile` instance from.
|
|
849
|
+
* @returns A new `TelaFile` instance.
|
|
850
|
+
*/
|
|
851
|
+
createFile: typeof createTelaFile;
|
|
852
|
+
static TelaSDK: typeof TelaSDK;
|
|
853
|
+
static DEFAULT_TIMEOUT: number;
|
|
854
|
+
/** Thrown when an API request fails. */
|
|
855
|
+
static APIError: typeof APIError;
|
|
856
|
+
/** Thrown when a request is aborted by the user. */
|
|
857
|
+
static UserAbortError: typeof UserAbortError;
|
|
858
|
+
/** Thrown when there's a network connection error. */
|
|
859
|
+
static ConnectionError: typeof ConnectionError;
|
|
860
|
+
/** Thrown when a request times out. */
|
|
861
|
+
static ConnectionTimeout: typeof ConnectionTimeout;
|
|
862
|
+
/** Thrown when the API returns a 400 Bad Request error. */
|
|
863
|
+
static BadRequestError: typeof BadRequestError;
|
|
864
|
+
/** Thrown when authentication fails (e.g., invalid API key). */
|
|
865
|
+
static AuthenticationError: typeof AuthenticationError;
|
|
866
|
+
/** Thrown when the authenticated user doesn't have permission for the requested operation. */
|
|
867
|
+
static AuthorizationError: typeof AuthorizationError;
|
|
868
|
+
/** Thrown when the requested resource is not found. */
|
|
869
|
+
static NotFoundError: typeof NotFoundError;
|
|
870
|
+
/** Thrown when there's a conflict with the current state of the resource. */
|
|
871
|
+
static ConflictError: typeof ConflictError;
|
|
872
|
+
/** Thrown when the request is well-formed but unable to be processed due to semantic errors. */
|
|
873
|
+
static UnprocessableEntityError: typeof UnprocessableEntityError;
|
|
874
|
+
/** Thrown when the API rate limit is exceeded. */
|
|
875
|
+
static RateLimitError: typeof RateLimitError;
|
|
876
|
+
/** Thrown when an unexpected server error occurs. */
|
|
877
|
+
static InternalServerError: typeof InternalServerError;
|
|
878
|
+
/** Thrown when attempting to upload an empty file. */
|
|
879
|
+
static EmptyFileError: typeof EmptyFileError;
|
|
880
|
+
/** Thrown when an invalid file URL is provided. */
|
|
881
|
+
static InvalidFileURL: typeof InvalidFileURL;
|
|
882
|
+
/** Thrown when there's an error during file upload. */
|
|
883
|
+
static FileUploadError: typeof FileUploadError;
|
|
884
|
+
/** Thrown when neither an API key nor a JWT is provided. */
|
|
885
|
+
static MissingApiKeyOrJWTError: typeof MissingApiKeyOrJWTError;
|
|
886
|
+
/** Thrown when both an API key and a JWT are provided. */
|
|
887
|
+
static ConflictApiKeyAndJWTError: typeof ConflictApiKeyAndJWTError;
|
|
888
|
+
}
|
|
889
|
+
/**
|
|
890
|
+
* Creates a new instance of the TelaSDK.
|
|
891
|
+
*
|
|
892
|
+
* @param opts - The options for configuring the TelaSDK.
|
|
893
|
+
* @returns A new instance of the TelaSDK.
|
|
894
|
+
*/
|
|
895
|
+
declare function createTelaClient(opts: TelaSDKOptions): TelaSDK;
|
|
896
|
+
|
|
897
|
+
export { TelaFile, TelaSDK, type TelaSDKOptions, createTelaClient };
|