@meistrari/tela-sdk-js 1.0.2 → 2.0.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.
- package/README.md +213 -89
- package/dist/index.cjs +1062 -110
- package/dist/index.d.cts +601 -428
- package/dist/index.d.mts +601 -428
- package/dist/index.d.ts +601 -428
- package/dist/index.mjs +1036 -111
- package/package.json +17 -7
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
import z, { string, number, boolean, object, array, any, unknown, z as z$1 } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Base HTTP client with retry logic, request/response transformation, and streaming support.
|
|
5
|
+
*
|
|
6
|
+
* This module provides the abstract BaseClient class that handles all HTTP communication
|
|
7
|
+
* with the Tela API, including automatic field transformation, retry logic, timeout handling,
|
|
8
|
+
* and Server-Sent Events streaming.
|
|
9
|
+
*
|
|
10
|
+
* @module Core
|
|
11
|
+
*/
|
|
1
12
|
type HTTPMethods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
2
13
|
type RequestOptions<Req = unknown | Record<string, unknown>> = {
|
|
3
14
|
method?: HTTPMethods;
|
|
@@ -15,6 +26,11 @@ interface BaseClientOptions {
|
|
|
15
26
|
maxRetries?: number;
|
|
16
27
|
timeout?: number;
|
|
17
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* Abstract base HTTP client with retry logic and request/response transformation.
|
|
31
|
+
*
|
|
32
|
+
* @category Client
|
|
33
|
+
*/
|
|
18
34
|
declare abstract class BaseClient {
|
|
19
35
|
baseURL: string;
|
|
20
36
|
maxRetries: number;
|
|
@@ -23,7 +39,8 @@ declare abstract class BaseClient {
|
|
|
23
39
|
constructor({ baseURL, maxRetries, timeout }: BaseClientOptions);
|
|
24
40
|
protected createHeaders(opts?: Record<string, string>): Headers;
|
|
25
41
|
protected getUserAgent(): string;
|
|
26
|
-
protected getAuthHeaders(): Record<string, string>;
|
|
42
|
+
protected abstract getAuthHeaders(): Record<string, string>;
|
|
43
|
+
abstract get authToken(): string;
|
|
27
44
|
get<Req, Res>(path: string, opts?: RequestOptions<Req>): Promise<Res>;
|
|
28
45
|
post<Req, Res>(path: string, opts?: RequestOptions<Req>): Promise<Res>;
|
|
29
46
|
put<Req, Res>(path: string, opts?: RequestOptions<Req>): Promise<Res>;
|
|
@@ -45,23 +62,92 @@ declare abstract class BaseClient {
|
|
|
45
62
|
protected stringifyQuery(query: Record<string, unknown>): string;
|
|
46
63
|
}
|
|
47
64
|
|
|
65
|
+
/**
|
|
66
|
+
* Error classes and exception handling for the Tela SDK.
|
|
67
|
+
*
|
|
68
|
+
* This module provides all custom error classes used throughout the SDK,
|
|
69
|
+
* including API errors, authentication errors, file errors, and execution errors.
|
|
70
|
+
*
|
|
71
|
+
* @module Core
|
|
72
|
+
*/
|
|
73
|
+
/**
|
|
74
|
+
* Base error class for all Tela SDK errors.
|
|
75
|
+
*
|
|
76
|
+
* @category Errors
|
|
77
|
+
*/
|
|
48
78
|
declare class TelaError extends Error {
|
|
49
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Thrown when both API key and JWT are provided.
|
|
82
|
+
*
|
|
83
|
+
* @category Errors
|
|
84
|
+
*/
|
|
50
85
|
declare class ConflictApiKeyAndJWTError extends TelaError {
|
|
51
86
|
constructor();
|
|
52
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* Thrown when neither API key nor JWT is provided.
|
|
90
|
+
*
|
|
91
|
+
* @category Errors
|
|
92
|
+
*/
|
|
53
93
|
declare class MissingApiKeyOrJWTError extends TelaError {
|
|
54
94
|
constructor();
|
|
55
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* Thrown when attempting to upload an empty file.
|
|
98
|
+
*
|
|
99
|
+
* @category Errors
|
|
100
|
+
*/
|
|
56
101
|
declare class EmptyFileError extends TelaError {
|
|
57
102
|
constructor();
|
|
58
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Thrown when an invalid file URL is provided.
|
|
106
|
+
*
|
|
107
|
+
* @category Errors
|
|
108
|
+
*/
|
|
59
109
|
declare class InvalidFileURL extends TelaError {
|
|
60
110
|
constructor(url: string);
|
|
61
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Thrown when file upload to vault storage fails.
|
|
114
|
+
*
|
|
115
|
+
* @category Errors
|
|
116
|
+
*/
|
|
62
117
|
declare class FileUploadError extends TelaError {
|
|
118
|
+
readonly statusCode: number;
|
|
119
|
+
constructor(message: string, statusCode: number);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Thrown when accessing execution result before execution has started.
|
|
123
|
+
*
|
|
124
|
+
* @category Errors
|
|
125
|
+
*/
|
|
126
|
+
declare class ExecutionNotStartedError extends TelaError {
|
|
127
|
+
constructor();
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Thrown when invalid execution mode parameters are provided.
|
|
131
|
+
*
|
|
132
|
+
* @category Errors
|
|
133
|
+
*/
|
|
134
|
+
declare class InvalidExecutionModeError extends TelaError {
|
|
63
135
|
constructor(message: string);
|
|
64
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* Thrown when canvas execution fails on the server.
|
|
139
|
+
*
|
|
140
|
+
* @category Errors
|
|
141
|
+
*/
|
|
142
|
+
declare class ExecutionFailedError extends TelaError {
|
|
143
|
+
readonly rawOutput: Record<string, any>;
|
|
144
|
+
constructor(rawOutput: Record<string, any>);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Base class for all API-related errors.
|
|
148
|
+
*
|
|
149
|
+
* @category Errors
|
|
150
|
+
*/
|
|
65
151
|
declare class APIError extends TelaError {
|
|
66
152
|
readonly statusCode: number | undefined;
|
|
67
153
|
readonly error: any;
|
|
@@ -111,11 +197,22 @@ declare class RateLimitError extends APIError {
|
|
|
111
197
|
declare class InternalServerError extends APIError {
|
|
112
198
|
readonly statusCode = 500;
|
|
113
199
|
}
|
|
200
|
+
declare function toError(err: any): Error;
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* File handling module for TelaFile wrapper and file upload operations.
|
|
204
|
+
*
|
|
205
|
+
* This module provides the TelaFile class for handling various file input types
|
|
206
|
+
* (URLs, Blobs, Streams, Buffers, vault references) with support for MIME types,
|
|
207
|
+
* page ranges, and file uploads to vault storage.
|
|
208
|
+
*
|
|
209
|
+
* @module Resources
|
|
210
|
+
*/
|
|
114
211
|
|
|
115
212
|
/**
|
|
116
213
|
* Configuration options for `TelaFile`.
|
|
117
214
|
*/
|
|
118
|
-
type
|
|
215
|
+
type BaseTelaFileOptions = {
|
|
119
216
|
/**
|
|
120
217
|
* Defines the page range of the document to be processed.
|
|
121
218
|
*
|
|
@@ -133,12 +230,22 @@ type TelaFileOptions = {
|
|
|
133
230
|
* Defines the parser provider to be used for the file.
|
|
134
231
|
*/
|
|
135
232
|
parserType?: string;
|
|
233
|
+
/**
|
|
234
|
+
* Defines the name of the file.
|
|
235
|
+
* If content is an instance of File or Blob, this will override the `name` property of the content.
|
|
236
|
+
*/
|
|
237
|
+
name?: string;
|
|
136
238
|
};
|
|
239
|
+
type TelaFileOptionsWithMimeType = BaseTelaFileOptions & {
|
|
240
|
+
mimeType: string;
|
|
241
|
+
};
|
|
242
|
+
type TelaFileOptions = BaseTelaFileOptions | TelaFileOptionsWithMimeType;
|
|
137
243
|
/**
|
|
138
244
|
* Represents the possible types for a file input,
|
|
139
245
|
* which can be a URL string, Uint8Array, ReadableStream, ReadStream, Blob, or File.
|
|
140
246
|
*/
|
|
141
247
|
type TelaFileInput = string | Uint8Array | ReadableStream | Blob | File;
|
|
248
|
+
declare function TelaFileSchema(): z.ZodCustom<TelaFile, TelaFile>;
|
|
142
249
|
/**
|
|
143
250
|
* Represents a file with support for various types including URLs, binary data, streams, and Blobs.
|
|
144
251
|
*
|
|
@@ -148,7 +255,7 @@ type TelaFileInput = string | Uint8Array | ReadableStream | Blob | File;
|
|
|
148
255
|
* const urlFile = new TelaFile("https://example.com/file.png", { range: { start: 0, end: 1024 } })
|
|
149
256
|
*
|
|
150
257
|
* // Using a Uint8Array (buffer)
|
|
151
|
-
* const buffer = new Uint8Array([
|
|
258
|
+
* const buffer = new Uint8Array(['binary data...'])
|
|
152
259
|
* const bufferFile = new TelaFile(buffer)
|
|
153
260
|
*
|
|
154
261
|
* // Using a ReadStream
|
|
@@ -172,12 +279,29 @@ type TelaFileInput = string | Uint8Array | ReadableStream | Blob | File;
|
|
|
172
279
|
* // Using a File (in browser environments)
|
|
173
280
|
* const file = new File(['file content'], 'filename.txt', { type: 'text/plain' })
|
|
174
281
|
* const fileInstance = new TelaFile(file)
|
|
282
|
+
*
|
|
283
|
+
* // Using multiple files in a collection
|
|
284
|
+
* const files = [
|
|
285
|
+
* new TelaFile("https://example.com/file1.pdf", { range: [0, 1] }),
|
|
286
|
+
* new TelaFile("https://example.com/file2.pdf", { range: [1, 2] })
|
|
287
|
+
* ]
|
|
288
|
+
* // Pass the array of files to a variable
|
|
289
|
+
* const result = await tela.completions.create({
|
|
290
|
+
* canvasId: "your-canvas-id",
|
|
291
|
+
* variables: {
|
|
292
|
+
* documents: files
|
|
293
|
+
* }
|
|
294
|
+
* })
|
|
175
295
|
* ```
|
|
296
|
+
*
|
|
297
|
+
* @category File
|
|
176
298
|
*/
|
|
177
299
|
declare class TelaFile {
|
|
178
300
|
private _file;
|
|
179
301
|
private _options;
|
|
180
302
|
private _size;
|
|
303
|
+
private _mimeType;
|
|
304
|
+
private _name;
|
|
181
305
|
/**
|
|
182
306
|
* Creates an instance of `TelaFile`.
|
|
183
307
|
*
|
|
@@ -186,7 +310,9 @@ declare class TelaFile {
|
|
|
186
310
|
* @throws {InvalidFileURL} If the provided URL is not valid.
|
|
187
311
|
* @throws {EmptyFileError} If the provided file is empty.
|
|
188
312
|
*/
|
|
189
|
-
constructor(
|
|
313
|
+
private constructor();
|
|
314
|
+
static create(file: Blob | File | string, options?: BaseTelaFileOptions): TelaFile;
|
|
315
|
+
static create(file: Uint8Array | ReadableStream, options: TelaFileOptionsWithMimeType): TelaFile;
|
|
190
316
|
/**
|
|
191
317
|
* Retrieves the options provided during instantiation.
|
|
192
318
|
*
|
|
@@ -199,12 +325,30 @@ declare class TelaFile {
|
|
|
199
325
|
* @returns `true` if the file source is a valid URL string, otherwise `false`.
|
|
200
326
|
*/
|
|
201
327
|
get isURL(): boolean;
|
|
328
|
+
/**
|
|
329
|
+
* Determines whether the file source is a valid Vault reference.
|
|
330
|
+
*
|
|
331
|
+
* @returns `true` if the file source is a valid Vault reference, otherwise `false`.
|
|
332
|
+
*/
|
|
333
|
+
get isVaultReference(): boolean;
|
|
202
334
|
/**
|
|
203
335
|
* Gets the size of the file in bytes.
|
|
204
336
|
*
|
|
205
337
|
* @returns The size of the file if available, otherwise `null`.
|
|
206
338
|
*/
|
|
207
339
|
get size(): number | null;
|
|
340
|
+
/**
|
|
341
|
+
* Gets the name of the file.
|
|
342
|
+
*
|
|
343
|
+
* @returns The name of the file if available, otherwise `null`.
|
|
344
|
+
*/
|
|
345
|
+
get name(): string | null;
|
|
346
|
+
/**
|
|
347
|
+
* Gets the type of the file.
|
|
348
|
+
*
|
|
349
|
+
* @returns The type of the file if available, otherwise `null`.
|
|
350
|
+
*/
|
|
351
|
+
get type(): string | null;
|
|
208
352
|
/**
|
|
209
353
|
* Retrieves the content of the file in a format suitable for uploading.
|
|
210
354
|
*
|
|
@@ -229,170 +373,26 @@ declare class TelaFile {
|
|
|
229
373
|
* @returns `true` if the URL is valid, otherwise `false`.
|
|
230
374
|
*/
|
|
231
375
|
private isValidURL;
|
|
232
|
-
}
|
|
233
|
-
/**
|
|
234
|
-
* Creates a new `TelaFile` instance from the provided file input.
|
|
235
|
-
*
|
|
236
|
-
* @param file - The file input to create a `TelaFile` instance from.
|
|
237
|
-
* @returns A new `TelaFile` instance.
|
|
238
|
-
*/
|
|
239
|
-
declare function createTelaFile(file: TelaFileInput, options?: TelaFileOptions): TelaFile;
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Base class for all resources.
|
|
243
|
-
*/
|
|
244
|
-
declare class Resource {
|
|
245
|
-
protected _client: BaseClient;
|
|
246
|
-
constructor(client: BaseClient);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Represents a stream of items that can be asynchronously iterated over.
|
|
251
|
-
*/
|
|
252
|
-
declare class Stream<Item> implements AsyncIterable<Item> {
|
|
253
|
-
private iterator;
|
|
254
|
-
/**
|
|
255
|
-
* The AbortController associated with this stream.
|
|
256
|
-
*/
|
|
257
|
-
controller: AbortController;
|
|
258
376
|
/**
|
|
259
|
-
*
|
|
377
|
+
* Checks if the provided string is a valid Vault reference.
|
|
260
378
|
*
|
|
261
|
-
* @param
|
|
262
|
-
* @
|
|
379
|
+
* @param url - The Vault reference string to validate.
|
|
380
|
+
* @returns `true` if the Vault reference is valid, otherwise `false`.
|
|
263
381
|
*/
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* Creates a Stream from a server-sent events (SSE) Response.
|
|
267
|
-
*
|
|
268
|
-
* @param response - The Response object containing the SSE data.
|
|
269
|
-
* @param controller - The AbortController for this stream.
|
|
270
|
-
* @returns A new Stream instance.
|
|
271
|
-
* @throws {Error} If the stream is consumed more than once.
|
|
272
|
-
* @throws {APIError} If the SSE data contains an error.
|
|
273
|
-
*/
|
|
274
|
-
static fromSSEResponse<Item>(response: Response, controller: AbortController): Stream<Item>;
|
|
275
|
-
/**
|
|
276
|
-
* Creates a Stream from a newline-separated ReadableStream where each item is a JSON value.
|
|
277
|
-
*
|
|
278
|
-
* @param readableStream - The ReadableStream containing newline-separated JSON data.
|
|
279
|
-
* @param controller - The AbortController for this stream.
|
|
280
|
-
* @returns A new Stream instance.
|
|
281
|
-
* @throws {Error} If the stream is consumed more than once.
|
|
282
|
-
*/
|
|
283
|
-
static fromReadableStream<Item>(readableStream: ReadableStream, controller: AbortController): Stream<Item>;
|
|
284
|
-
/**
|
|
285
|
-
* Implements the AsyncIterable interface, allowing the Stream to be used in a for-await-of loop.
|
|
286
|
-
*
|
|
287
|
-
* @returns An AsyncIterator for the stream items.
|
|
288
|
-
*/
|
|
289
|
-
[Symbol.asyncIterator](): AsyncIterator<Item>;
|
|
290
|
-
/**
|
|
291
|
-
* Splits the stream into two streams which can be independently read from at different speeds.
|
|
292
|
-
*
|
|
293
|
-
* @returns A tuple containing two new Stream instances.
|
|
294
|
-
*/
|
|
295
|
-
tee(): [Stream<Item>, Stream<Item>];
|
|
296
|
-
/**
|
|
297
|
-
* Converts this stream to a newline-separated ReadableStream of JSON stringified values.
|
|
298
|
-
* The resulting stream can be converted back to a Stream using `Stream.fromReadableStream()`.
|
|
299
|
-
*
|
|
300
|
-
* @returns A ReadableStream containing newline-separated JSON data.
|
|
301
|
-
*/
|
|
302
|
-
toReadableStream(): ReadableStream;
|
|
382
|
+
private isValidVaultReference;
|
|
303
383
|
}
|
|
304
384
|
|
|
305
385
|
/**
|
|
306
|
-
*
|
|
386
|
+
* Canvas execution module for managing canvas execution lifecycle and modes.
|
|
307
387
|
*
|
|
308
|
-
*
|
|
309
|
-
*
|
|
388
|
+
* This module handles the execution of canvas templates with support for
|
|
389
|
+
* synchronous, asynchronous (with polling), and streaming modes. It also
|
|
390
|
+
* manages file uploads and result validation.
|
|
310
391
|
*
|
|
311
|
-
* @
|
|
312
|
-
* ```typescript
|
|
313
|
-
* const tela = new TelaSDK({
|
|
314
|
-
* apiKey: 'your-api-key',
|
|
315
|
-
* });
|
|
316
|
-
* const response = await tela.completions.create({
|
|
317
|
-
* canvasId: "your-canvas-id",
|
|
318
|
-
* messages: [{ role: "user", content: "Hello!" }],
|
|
319
|
-
* });
|
|
320
|
-
* console.log(response.choices[0].message.content);
|
|
321
|
-
* ```
|
|
322
|
-
*/
|
|
323
|
-
declare class ChatCompletions extends Resource {
|
|
324
|
-
/**
|
|
325
|
-
* Creates a chat completion with various input options and response formats.
|
|
326
|
-
*
|
|
327
|
-
* This method supports synchronous responses, streaming responses, and webhook-based asynchronous processing.
|
|
328
|
-
*
|
|
329
|
-
* @param body - The parameters for creating a chat completion.
|
|
330
|
-
* @param opts - Additional request options.
|
|
331
|
-
* @returns A Promise that resolves to either a stream of chat completion responses,
|
|
332
|
-
* a single chat completion response, or a webhook response, depending on the input parameters.
|
|
333
|
-
*
|
|
334
|
-
* @example
|
|
335
|
-
* ```typescript
|
|
336
|
-
* // Synchronous response
|
|
337
|
-
* const response = await tela.completions.create({
|
|
338
|
-
* canvasId: "your-canvas-id",
|
|
339
|
-
* messages: [{ role: "user", content: "Tell me a joke." }],
|
|
340
|
-
* });
|
|
341
|
-
*
|
|
342
|
-
* // Streaming response
|
|
343
|
-
* const stream = await tela.completions.create({
|
|
344
|
-
* canvasId: "your-canvas-id",
|
|
345
|
-
* messages: [{ role: "user", content: "Tell me a story." }],
|
|
346
|
-
* stream: true,
|
|
347
|
-
* });
|
|
348
|
-
*
|
|
349
|
-
* // Webhook response
|
|
350
|
-
* const webhookResponse = await tela.completions.create({
|
|
351
|
-
* canvasId: "your-canvas-id",
|
|
352
|
-
* messages: [{ role: "user", content: "Generate a report." }],
|
|
353
|
-
* webhookUrl: "https://example.com/webhook",
|
|
354
|
-
* });
|
|
355
|
-
* ```
|
|
356
|
-
*/
|
|
357
|
-
create<Variables = ChatCompletionVariables, CompletionContent = string>(body: ChatCompletionCreateWebhookParams<Variables>, opts?: RequestOptions<ChatCompletionCreateParams<Variables>>): Promise<ChatCompletionWebhookResponse<CompletionContent>>;
|
|
358
|
-
create<Variables = ChatCompletionVariables, CompletionContent = string>(body: CompletionCreateParamsNonStreaming<Variables>, opts?: RequestOptions<ChatCompletionCreateParams<Variables>>): Promise<ChatCompletionCreateResponse<CompletionContent>>;
|
|
359
|
-
create<Variables = ChatCompletionVariables, CompletionContent = string>(body: CompletionCreateParamsStreaming<Variables>, opts?: RequestOptions<ChatCompletionCreateParams<Variables>>): Promise<Stream<ChatCompletionCreateStreamingResponse<CompletionContent>>>;
|
|
360
|
-
create<Variables = ChatCompletionVariables, CompletionContent = string>(body: ChatCompletionCreateParamsBase<Variables>, opts?: RequestOptions<ChatCompletionCreateParams<Variables>>): Promise<Stream<ChatCompletionCreateStreamingResponse<CompletionContent>> | ChatCompletionCreateResponse<CompletionContent> | ChatCompletionWebhookResponse<CompletionContent>>;
|
|
361
|
-
/**
|
|
362
|
-
* Uploads a file and returns its URL and options.
|
|
363
|
-
*
|
|
364
|
-
* This is used internally to handle file uploads associated with chat completions.
|
|
365
|
-
*
|
|
366
|
-
* @param file - The TelaFile to be uploaded.
|
|
367
|
-
* @returns A Promise that resolves to an object containing the file URL and options.
|
|
368
|
-
* @throws {FileUploadError} If the file upload fails.
|
|
369
|
-
*
|
|
370
|
-
* @example
|
|
371
|
-
* ```typescript
|
|
372
|
-
* const file = new TelaFile({ /* file options *\/ });
|
|
373
|
-
* const uploadedFile = await this.uploadFile(file);
|
|
374
|
-
* console.log(uploadedFile.file_url);
|
|
375
|
-
* ```
|
|
376
|
-
*/
|
|
377
|
-
private uploadFile;
|
|
378
|
-
}
|
|
379
|
-
type ChatCompletionVariablesValue = string | number | boolean | TelaFile;
|
|
380
|
-
/**
|
|
381
|
-
* Represents the variables available in the canvas.
|
|
382
|
-
* Keys are strings, and values can be strings, numbers, booleans, or TelaFile instances.
|
|
392
|
+
* @module Resources
|
|
383
393
|
*/
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
* Represents the processed variables with file URLs.
|
|
387
|
-
* Similar to ChatCompletionVariables, but TelaFile instances are replaced with their URLs.
|
|
388
|
-
*/
|
|
389
|
-
type ChatCompletionVariablesWithFileURL = Record<string, string | number | boolean | {
|
|
390
|
-
fileUrl: string;
|
|
391
|
-
}>;
|
|
392
|
-
/**
|
|
393
|
-
* Base parameters for creating a chat completion.
|
|
394
|
-
*/
|
|
395
|
-
interface ChatCompletionCreateParamsBase<Variables = ChatCompletionVariables> {
|
|
394
|
+
|
|
395
|
+
interface BaseExecutionParams {
|
|
396
396
|
/**
|
|
397
397
|
* The version ID of the canvas to use for this chat completion.
|
|
398
398
|
* If not provided, the latest version of the specified canvas will be used.
|
|
@@ -408,16 +408,6 @@ interface ChatCompletionCreateParamsBase<Variables = ChatCompletionVariables> {
|
|
|
408
408
|
* This is required if a canvasId is not provided.
|
|
409
409
|
*/
|
|
410
410
|
applicationId?: string;
|
|
411
|
-
/**
|
|
412
|
-
* The URL to receive a webhook notification when the chat completion is finished.
|
|
413
|
-
* If provided, the completion will be processed asynchronously.
|
|
414
|
-
*/
|
|
415
|
-
webhookUrl?: string | null | undefined;
|
|
416
|
-
/**
|
|
417
|
-
* Variables to be passed to the canvas for this chat completion.
|
|
418
|
-
* These can be used to customize the behavior of the canvas for this specific request.
|
|
419
|
-
*/
|
|
420
|
-
variables?: Variables;
|
|
421
411
|
/**
|
|
422
412
|
* An array of previous messages in the conversation.
|
|
423
413
|
* Each message should have a 'role' (e.g., 'user', 'assistant') and 'content'.
|
|
@@ -459,327 +449,490 @@ interface ChatCompletionCreateParamsBase<Variables = ChatCompletionVariables> {
|
|
|
459
449
|
required?: string[];
|
|
460
450
|
};
|
|
461
451
|
}>;
|
|
462
|
-
/**
|
|
463
|
-
* Configuration for generating structured output.
|
|
464
|
-
*/
|
|
465
|
-
structuredOutput?: {
|
|
466
|
-
/**
|
|
467
|
-
* Whether to enable structured output for this completion.
|
|
468
|
-
*/
|
|
469
|
-
enabled: boolean;
|
|
470
|
-
/**
|
|
471
|
-
* The schema defining the structure of the output.
|
|
472
|
-
* This is used when structured output is enabled.
|
|
473
|
-
*/
|
|
474
|
-
schema?: {
|
|
475
|
-
properties: Record<string, unknown>;
|
|
476
|
-
type: 'object';
|
|
477
|
-
title: string;
|
|
478
|
-
description: string;
|
|
479
|
-
required: string[];
|
|
480
|
-
};
|
|
481
|
-
};
|
|
482
452
|
};
|
|
453
|
+
/**
|
|
454
|
+
* Whether to skip Zod schema validation on execution results.
|
|
455
|
+
* When true, results are typecast to the expected output type without validation.
|
|
456
|
+
* @default false
|
|
457
|
+
*/
|
|
458
|
+
skipResultValidation?: boolean;
|
|
483
459
|
}
|
|
484
460
|
/**
|
|
485
|
-
*
|
|
461
|
+
* Configuration for asynchronous canvas execution with polling.
|
|
462
|
+
* The execution starts immediately but results must be retrieved via polling.
|
|
486
463
|
*/
|
|
487
|
-
interface
|
|
464
|
+
interface AsyncExecutionParams extends BaseExecutionParams {
|
|
488
465
|
/**
|
|
489
|
-
*
|
|
466
|
+
* Enable asynchronous execution mode.
|
|
467
|
+
* @default false
|
|
490
468
|
*/
|
|
491
|
-
|
|
469
|
+
async?: true;
|
|
470
|
+
/**
|
|
471
|
+
* Optional webhook URL to receive completion notifications.
|
|
472
|
+
*/
|
|
473
|
+
webhookUrl?: string;
|
|
492
474
|
/**
|
|
493
|
-
*
|
|
475
|
+
* Time in milliseconds between polling attempts.
|
|
476
|
+
* @default 1000
|
|
494
477
|
*/
|
|
495
|
-
|
|
478
|
+
pollingInterval?: number;
|
|
479
|
+
/**
|
|
480
|
+
* Maximum time in milliseconds to wait for completion before timing out.
|
|
481
|
+
* @default 60000
|
|
482
|
+
*/
|
|
483
|
+
pollingTimeout?: number;
|
|
484
|
+
/**
|
|
485
|
+
* Stream mode is not available for async executions.
|
|
486
|
+
*/
|
|
487
|
+
stream?: never;
|
|
496
488
|
}
|
|
497
489
|
/**
|
|
498
|
-
*
|
|
490
|
+
* Configuration for streaming canvas execution.
|
|
491
|
+
* Results are returned incrementally as they're generated.
|
|
499
492
|
*/
|
|
500
|
-
interface
|
|
493
|
+
interface StreamExecutionParams extends BaseExecutionParams {
|
|
501
494
|
/**
|
|
502
|
-
*
|
|
495
|
+
* Enable streaming mode.
|
|
503
496
|
*/
|
|
504
|
-
stream
|
|
497
|
+
stream: true;
|
|
498
|
+
/**
|
|
499
|
+
* Async mode is not available for streaming executions.
|
|
500
|
+
*/
|
|
501
|
+
async?: false;
|
|
502
|
+
/**
|
|
503
|
+
* Webhook URL is not available for streaming executions.
|
|
504
|
+
*/
|
|
505
|
+
webhookUrl?: never;
|
|
505
506
|
}
|
|
506
507
|
/**
|
|
507
|
-
*
|
|
508
|
+
* Configuration for synchronous canvas execution.
|
|
509
|
+
* The execution waits for the complete result before returning.
|
|
508
510
|
*/
|
|
509
|
-
interface
|
|
511
|
+
interface SyncExecutionParams extends BaseExecutionParams {
|
|
512
|
+
/**
|
|
513
|
+
* Disable asynchronous execution mode.
|
|
514
|
+
* @default false
|
|
515
|
+
*/
|
|
516
|
+
async?: false;
|
|
517
|
+
/**
|
|
518
|
+
* Webhook URL is not available for synchronous executions.
|
|
519
|
+
*/
|
|
520
|
+
webhookUrl?: never;
|
|
510
521
|
/**
|
|
511
|
-
*
|
|
522
|
+
* Stream mode is not available for synchronous executions.
|
|
512
523
|
*/
|
|
513
|
-
|
|
524
|
+
stream?: never;
|
|
514
525
|
}
|
|
515
526
|
/**
|
|
516
|
-
* Union type
|
|
527
|
+
* Union type of all possible execution parameter configurations.
|
|
517
528
|
*/
|
|
518
|
-
type
|
|
529
|
+
type ExecutionParams = AsyncExecutionParams | StreamExecutionParams | SyncExecutionParams;
|
|
519
530
|
/**
|
|
520
|
-
*
|
|
531
|
+
* Conditional type that determines the return type based on execution parameters.
|
|
532
|
+
*
|
|
533
|
+
* @category Canvas
|
|
534
|
+
*
|
|
535
|
+
* @template TParams - The execution parameters type.
|
|
536
|
+
* @template TOutput - The output type.
|
|
537
|
+
* @returns AsyncGenerator for streaming executions, Promise otherwise.
|
|
521
538
|
*/
|
|
522
|
-
type
|
|
539
|
+
type CanvasExecutionResult<TParams, TOutput = unknown> = TParams extends StreamExecutionParams ? AsyncGenerator<Partial<TOutput>> : Promise<TOutput>;
|
|
540
|
+
/**
|
|
541
|
+
* Manages the execution lifecycle of a canvas.
|
|
542
|
+
*
|
|
543
|
+
* Handles synchronous, asynchronous (with polling), and streaming execution modes.
|
|
544
|
+
* Automatically uploads TelaFile instances and validates results based on output schema.
|
|
545
|
+
*
|
|
546
|
+
* @category Canvas
|
|
547
|
+
*
|
|
548
|
+
* @typeParam TParams - The execution parameters type
|
|
549
|
+
* @typeParam TInput - The input variables type
|
|
550
|
+
* @typeParam TOutput - The output result type
|
|
551
|
+
*/
|
|
552
|
+
declare class CanvasExecution<TParams extends ExecutionParams = SyncExecutionParams, TInput = unknown, TOutput = unknown> {
|
|
553
|
+
private _id;
|
|
554
|
+
private readonly _variables;
|
|
555
|
+
private readonly _params;
|
|
556
|
+
private readonly _client;
|
|
557
|
+
private readonly _outputSchema;
|
|
558
|
+
private readonly _skipResultValidation;
|
|
559
|
+
private readonly _abortController;
|
|
560
|
+
private _startPropmise;
|
|
561
|
+
private _resultPromise;
|
|
562
|
+
private _stream;
|
|
523
563
|
/**
|
|
524
|
-
*
|
|
564
|
+
* Creates a new canvas execution instance.
|
|
565
|
+
*
|
|
566
|
+
* @param variables - Input variables to be passed to the canvas template.
|
|
567
|
+
* @param params - Execution parameters controlling sync/async/stream behavior.
|
|
568
|
+
* @param outputSchema - Zod schema or object schema for validating/parsing output.
|
|
569
|
+
* @param client - HTTP client instance for making API requests.
|
|
525
570
|
*/
|
|
526
|
-
|
|
571
|
+
constructor(variables: TInput, params: TParams | undefined, outputSchema: z.ZodType | Record<string, unknown>, client: BaseClient);
|
|
527
572
|
/**
|
|
528
|
-
*
|
|
573
|
+
* Gets the unique execution ID assigned by the server.
|
|
574
|
+
*
|
|
575
|
+
* @throws {Error} If the execution has not been started yet.
|
|
576
|
+
* @returns The execution ID.
|
|
529
577
|
*/
|
|
530
|
-
|
|
578
|
+
get id(): string;
|
|
531
579
|
/**
|
|
532
|
-
*
|
|
580
|
+
* Gets the input variables provided to this execution.
|
|
581
|
+
*
|
|
582
|
+
* @returns The variables object.
|
|
533
583
|
*/
|
|
534
|
-
|
|
584
|
+
get variables(): TInput;
|
|
535
585
|
/**
|
|
536
|
-
*
|
|
586
|
+
* Gets the execution parameters configured for this instance.
|
|
587
|
+
*
|
|
588
|
+
* @returns The execution parameters or undefined.
|
|
537
589
|
*/
|
|
538
|
-
|
|
539
|
-
message: {
|
|
540
|
-
role: string;
|
|
541
|
-
content: T;
|
|
542
|
-
};
|
|
543
|
-
}>;
|
|
544
|
-
};
|
|
545
|
-
/**
|
|
546
|
-
* Response structure for a chat completion streaming response.
|
|
547
|
-
*/
|
|
548
|
-
type ChatCompletionCreateStreamingResponse<T> = {
|
|
590
|
+
get params(): TParams;
|
|
549
591
|
/**
|
|
550
|
-
*
|
|
592
|
+
* Checks if this execution is configured for asynchronous processing.
|
|
593
|
+
*
|
|
594
|
+
* @returns True if async mode is enabled.
|
|
551
595
|
*/
|
|
552
|
-
|
|
596
|
+
get isAsync(): boolean;
|
|
553
597
|
/**
|
|
554
|
-
*
|
|
598
|
+
* Checks if this execution is configured for synchronous processing.
|
|
599
|
+
*
|
|
600
|
+
* @returns True if sync mode is enabled (not async).
|
|
555
601
|
*/
|
|
556
|
-
|
|
602
|
+
get isSync(): boolean;
|
|
557
603
|
/**
|
|
558
|
-
*
|
|
604
|
+
* Checks if this execution is configured for streaming responses.
|
|
605
|
+
*
|
|
606
|
+
* @returns True if stream mode is enabled.
|
|
559
607
|
*/
|
|
560
|
-
|
|
608
|
+
get isStream(): boolean;
|
|
561
609
|
/**
|
|
562
|
-
*
|
|
610
|
+
* Type guard to check if params indicate async execution.
|
|
611
|
+
*
|
|
612
|
+
* @param params - Execution parameters to check.
|
|
613
|
+
* @returns True if params indicate async mode.
|
|
563
614
|
*/
|
|
564
|
-
|
|
615
|
+
private _isAsync;
|
|
565
616
|
/**
|
|
566
|
-
*
|
|
617
|
+
* Starts the execution based on configured parameters.
|
|
618
|
+
* Routes to the appropriate execution method (sync, async, or stream).
|
|
619
|
+
*
|
|
620
|
+
* @returns A promise or async generator depending on execution mode.
|
|
567
621
|
*/
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
role: string | null;
|
|
571
|
-
content: string | null;
|
|
572
|
-
functionCall: {
|
|
573
|
-
name: string | null;
|
|
574
|
-
arguments: string | null;
|
|
575
|
-
} | null;
|
|
576
|
-
};
|
|
577
|
-
finishReason: string | null;
|
|
578
|
-
index: number;
|
|
579
|
-
logprobs: number | null;
|
|
622
|
+
start(): Promise<CanvasExecutionResult<TParams, TOutput>> | AsyncGenerator<Partial<TOutput>, any, any> | Promise<AsyncGenerator<Partial<TOutput>, any, any>> | Promise<{
|
|
623
|
+
id: string;
|
|
580
624
|
}>;
|
|
581
625
|
/**
|
|
582
|
-
*
|
|
626
|
+
* Gets the execution result. For async executions, automatically starts polling.
|
|
627
|
+
* For sync executions, returns the result promise. For streams, returns the generator.
|
|
628
|
+
*
|
|
629
|
+
* @returns The execution result as a promise or async generator.
|
|
583
630
|
*/
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
631
|
+
get result(): CanvasExecutionResult<TParams, TOutput>;
|
|
632
|
+
/**
|
|
633
|
+
* Cancels the ongoing execution by aborting all active requests and polling operations.
|
|
634
|
+
*/
|
|
635
|
+
cancel(): void;
|
|
636
|
+
/**
|
|
637
|
+
* Builds the base request body shared across all execution types.
|
|
638
|
+
* Includes messages, overrides, and structured output configuration.
|
|
639
|
+
*
|
|
640
|
+
* @returns The base request body object.
|
|
641
|
+
*/
|
|
642
|
+
private get baseBody();
|
|
643
|
+
/**
|
|
644
|
+
* Processes variables and uploads any TelaFile instances to the server.
|
|
645
|
+
* Replaces TelaFile objects with their uploaded URLs while preserving other values.
|
|
646
|
+
*
|
|
647
|
+
* @returns A promise resolving to the processed variables object.
|
|
648
|
+
*/
|
|
649
|
+
private resolveVariables;
|
|
650
|
+
/**
|
|
651
|
+
* Initiates a synchronous execution that waits for the complete result.
|
|
652
|
+
* The result is validated against the output schema if provided.
|
|
653
|
+
*
|
|
654
|
+
* @returns A promise resolving to the parsed execution result.
|
|
655
|
+
*/
|
|
656
|
+
private startSync;
|
|
657
|
+
/**
|
|
658
|
+
* Initiates an asynchronous execution that returns immediately with an execution ID.
|
|
659
|
+
* Results must be retrieved separately via polling using the `result` getter.
|
|
660
|
+
*
|
|
661
|
+
* @returns A promise that resolves when the execution is queued.
|
|
662
|
+
*/
|
|
663
|
+
private startAsync;
|
|
664
|
+
/**
|
|
665
|
+
* Initiates a streaming execution that returns results incrementally as they're generated.
|
|
666
|
+
*
|
|
667
|
+
* @returns A promise resolving to an async generator yielding result chunks.
|
|
668
|
+
*/
|
|
669
|
+
private startStream;
|
|
670
|
+
/**
|
|
671
|
+
* Polls the server for async execution results at regular intervals.
|
|
672
|
+
* Continues polling until the execution completes or the timeout is reached.
|
|
673
|
+
*
|
|
674
|
+
* @throws {Error} If called on a non-async execution.
|
|
675
|
+
* @throws {Error} If the execution fails on the server.
|
|
676
|
+
* @throws {Error} If the polling operation times out.
|
|
677
|
+
* @returns A promise resolving to the final execution result.
|
|
678
|
+
*/
|
|
679
|
+
private startPolling;
|
|
680
|
+
/**
|
|
681
|
+
* Uploads a file and returns its URL and options.
|
|
682
|
+
*
|
|
683
|
+
* This is used internally to handle file uploads associated with chat completions.
|
|
684
|
+
*
|
|
685
|
+
* @param file - The TelaFile to be uploaded.
|
|
686
|
+
* @returns A Promise that resolves to an object containing the file URL and options.
|
|
687
|
+
* @throws {FileUploadError} If the file upload fails.
|
|
688
|
+
*
|
|
689
|
+
* @example
|
|
690
|
+
* ```typescript
|
|
691
|
+
* const file = new TelaFile({ /* file options *\/ });
|
|
692
|
+
* const uploadedFile = await this.uploadFile(file);
|
|
693
|
+
* console.log(uploadedFile.fileUrl);
|
|
694
|
+
* ```
|
|
695
|
+
*/
|
|
696
|
+
private uploadFile;
|
|
697
|
+
/**
|
|
698
|
+
* Uploads multiple files and returns their URLs and options.
|
|
699
|
+
*
|
|
700
|
+
* This is used internally to handle multiple file uploads associated with chat completions.
|
|
701
|
+
*
|
|
702
|
+
* @param files - An array of TelaFile instances to be uploaded.
|
|
703
|
+
* @returns A Promise that resolves to an array of objects containing file URLs and options.
|
|
704
|
+
* @throws {FileUploadError} If any file upload fails.
|
|
705
|
+
*/
|
|
706
|
+
private uploadFiles;
|
|
707
|
+
}
|
|
708
|
+
|
|
598
709
|
/**
|
|
599
|
-
*
|
|
600
|
-
*
|
|
710
|
+
* Canvas resource for managing prompt templates and their execution.
|
|
711
|
+
*
|
|
712
|
+
* This module provides the core Canvas class for retrieving and executing
|
|
713
|
+
* prompt templates from the Tela API, with support for schema validation,
|
|
714
|
+
* type safety, and multiple execution modes.
|
|
715
|
+
*
|
|
716
|
+
* @module Resources
|
|
601
717
|
*/
|
|
602
|
-
|
|
718
|
+
|
|
719
|
+
/**
|
|
720
|
+
* Represents a variable that can be used in a canvas template.
|
|
721
|
+
*/
|
|
722
|
+
type CanvasVariable = {
|
|
603
723
|
/**
|
|
604
|
-
*
|
|
724
|
+
* The name of the variable.
|
|
605
725
|
*/
|
|
606
|
-
|
|
726
|
+
name: string;
|
|
607
727
|
/**
|
|
608
|
-
* The
|
|
609
|
-
* - 'created': The request has been received but processing hasn't started.
|
|
610
|
-
* - 'running': The chat completion is currently being processed.
|
|
611
|
-
* - 'succeeded': The chat completion has finished successfully.
|
|
612
|
-
* - 'failed': The chat completion encountered an error and couldn't complete.
|
|
728
|
+
* The type of the variable (e.g., 'string', 'number', 'file').
|
|
613
729
|
*/
|
|
614
|
-
|
|
730
|
+
type: string;
|
|
615
731
|
/**
|
|
616
|
-
*
|
|
732
|
+
* Whether this variable is required for canvas execution.
|
|
617
733
|
*/
|
|
618
|
-
|
|
619
|
-
/**
|
|
620
|
-
* Variables passed to the chat completion, including any file URLs.
|
|
621
|
-
*/
|
|
622
|
-
variables: ChatCompletionVariablesWithFileURL;
|
|
623
|
-
/**
|
|
624
|
-
* Array of messages that were part of the input.
|
|
625
|
-
*/
|
|
626
|
-
messages: Array<{
|
|
627
|
-
/**
|
|
628
|
-
* The role of the message sender (e.g., 'user', 'assistant', 'system').
|
|
629
|
-
*/
|
|
630
|
-
role: string;
|
|
631
|
-
/**
|
|
632
|
-
* The content of the message. Can be a string for text messages,
|
|
633
|
-
* or an object for more complex content types like images.
|
|
634
|
-
*/
|
|
635
|
-
content: string | {
|
|
636
|
-
type: 'text';
|
|
637
|
-
text: string;
|
|
638
|
-
} | {
|
|
639
|
-
type: 'image_url';
|
|
640
|
-
imageUrl: {
|
|
641
|
-
url: string;
|
|
642
|
-
detail?: 'auto' | 'high' | 'low';
|
|
643
|
-
};
|
|
644
|
-
};
|
|
645
|
-
/**
|
|
646
|
-
* Optional. Information about a function call, if applicable.
|
|
647
|
-
*/
|
|
648
|
-
functionCall?: {
|
|
649
|
-
name: string;
|
|
650
|
-
arguments: string;
|
|
651
|
-
};
|
|
652
|
-
}>;
|
|
653
|
-
/**
|
|
654
|
-
* Information about files used in the chat completion.
|
|
655
|
-
*/
|
|
656
|
-
files: Array<{
|
|
657
|
-
name: string;
|
|
658
|
-
usedAt: 'variables' | 'messages';
|
|
659
|
-
parsedContent: string;
|
|
660
|
-
url: string;
|
|
661
|
-
type: 'image' | 'file';
|
|
662
|
-
}>;
|
|
663
|
-
};
|
|
734
|
+
required: boolean;
|
|
664
735
|
/**
|
|
665
|
-
*
|
|
736
|
+
* Description of the variable's purpose.
|
|
666
737
|
*/
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
/**
|
|
673
|
-
* Optional. Information about tool calls made during the completion.
|
|
674
|
-
*/
|
|
675
|
-
toolCalls?: Array<{
|
|
676
|
-
name: string;
|
|
677
|
-
arguments: string;
|
|
678
|
-
}>;
|
|
679
|
-
/**
|
|
680
|
-
* Optional. Additional information about the completion process.
|
|
681
|
-
*/
|
|
682
|
-
information?: {
|
|
683
|
-
duration: number;
|
|
684
|
-
cost?: number;
|
|
685
|
-
inputTokens?: number;
|
|
686
|
-
outputTokens?: number;
|
|
687
|
-
};
|
|
688
|
-
/**
|
|
689
|
-
* Indicates whether the output has a structured format.
|
|
690
|
-
*/
|
|
691
|
-
hasStructuredOutput?: boolean;
|
|
692
|
-
/**
|
|
693
|
-
* The main content of the chat completion output.
|
|
694
|
-
*/
|
|
695
|
-
content: T;
|
|
738
|
+
description: string;
|
|
739
|
+
/**
|
|
740
|
+
* Processing options for the variable.
|
|
741
|
+
*/
|
|
742
|
+
processingOptions: {
|
|
696
743
|
/**
|
|
697
|
-
*
|
|
744
|
+
* Whether multimodal content (images, files) is allowed.
|
|
698
745
|
*/
|
|
699
|
-
|
|
746
|
+
allowMultimodal: boolean;
|
|
700
747
|
};
|
|
748
|
+
};
|
|
749
|
+
interface CanvasOptions<TInput, TOutput> {
|
|
701
750
|
/**
|
|
702
|
-
*
|
|
703
|
-
*/
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
751
|
+
* Id of the canvas that will be used to create the completion.
|
|
752
|
+
*/
|
|
753
|
+
id?: string;
|
|
754
|
+
/**
|
|
755
|
+
* Id of the version of the canvas. If not provided, the latest promoted version will be used.
|
|
756
|
+
*/
|
|
757
|
+
versionId?: string;
|
|
758
|
+
/**
|
|
759
|
+
* Id of the application that the canvas belongs to.
|
|
760
|
+
*/
|
|
761
|
+
applicationId?: string;
|
|
762
|
+
/**
|
|
763
|
+
* Name of the canvas. Useful for debugging.
|
|
764
|
+
*/
|
|
765
|
+
name?: string;
|
|
766
|
+
/**
|
|
767
|
+
* Input schema of the canvas.
|
|
768
|
+
*/
|
|
769
|
+
input?: SchemaFunction<TInput>;
|
|
770
|
+
/**
|
|
771
|
+
* Output schema of the canvas.
|
|
772
|
+
*/
|
|
773
|
+
output?: SchemaFunction<TOutput>;
|
|
774
|
+
/**
|
|
775
|
+
* Base client for making requests to the Tela API.
|
|
776
|
+
*/
|
|
777
|
+
client: BaseClient;
|
|
778
|
+
/**
|
|
779
|
+
* Variables of the canvas.
|
|
780
|
+
*/
|
|
781
|
+
variables: Array<CanvasVariable>;
|
|
782
|
+
}
|
|
783
|
+
type CanvasGetOptions<TInput extends ZodTypeOrRecord, TOutput extends ZodTypeOrRecord> = Omit<CanvasOptions<TInput, TOutput>, 'client' | 'variables'> & {
|
|
784
|
+
/**
|
|
785
|
+
* Whether to skip schema validation warnings during canvas retrieval.
|
|
786
|
+
* When true, no warnings will be logged for schema mismatches.
|
|
787
|
+
* @default false
|
|
788
|
+
*/
|
|
789
|
+
skipSchemaValidation?: boolean;
|
|
790
|
+
};
|
|
791
|
+
declare const zod: {
|
|
792
|
+
string: typeof string;
|
|
793
|
+
number: typeof number;
|
|
794
|
+
boolean: typeof boolean;
|
|
795
|
+
object: typeof object;
|
|
796
|
+
array: typeof array;
|
|
797
|
+
file: typeof TelaFileSchema;
|
|
798
|
+
any: typeof any;
|
|
799
|
+
unknown: typeof unknown;
|
|
800
|
+
};
|
|
801
|
+
type SchemaBuilder = typeof zod;
|
|
802
|
+
type SchemaFunction<T> = (schema: SchemaBuilder) => T;
|
|
803
|
+
type Output<T> = T extends z$1.ZodType ? z$1.infer<T> : T;
|
|
804
|
+
type ZodTypeOrRecord = z$1.ZodType | Record<string, unknown>;
|
|
805
|
+
type CanvasExecutionPromiseLike<TParams extends ExecutionParams = SyncExecutionParams, TInput = unknown, TOutput = unknown> = PromiseLike<CanvasExecution<TParams, TInput, TOutput>> & {
|
|
806
|
+
result: Promise<CanvasExecutionResult<TParams, TOutput>>;
|
|
807
|
+
};
|
|
808
|
+
/**
|
|
809
|
+
* Represents a canvas (prompt template) from the Tela API.
|
|
810
|
+
*
|
|
811
|
+
* Canvas instances are retrieved using `tela.canvas.get()` and provide methods
|
|
812
|
+
* for executing prompts with various modes (sync, async, streaming).
|
|
813
|
+
*
|
|
814
|
+
* @category Canvas
|
|
815
|
+
*
|
|
816
|
+
* @typeParam TInput - The input schema type (Zod schema or plain object type)
|
|
817
|
+
* @typeParam TOutput - The output schema type (Zod schema or plain object type)
|
|
818
|
+
*/
|
|
819
|
+
declare class Canvas<TInput extends ZodTypeOrRecord, TOutput extends ZodTypeOrRecord> {
|
|
820
|
+
private readonly _id;
|
|
821
|
+
private readonly _versionId;
|
|
822
|
+
private readonly _applicationId;
|
|
823
|
+
private readonly _name;
|
|
824
|
+
private readonly _input;
|
|
825
|
+
private readonly _output;
|
|
826
|
+
private readonly _client;
|
|
827
|
+
private readonly _variables;
|
|
749
828
|
/**
|
|
750
|
-
*
|
|
829
|
+
* Creates a new instance of the Canvas class. Usage of this constructor is not recommended.
|
|
830
|
+
* Use the `tela.getCanvas` method instead.
|
|
831
|
+
*
|
|
832
|
+
* @private
|
|
751
833
|
*/
|
|
752
|
-
|
|
834
|
+
private constructor();
|
|
753
835
|
/**
|
|
754
|
-
*
|
|
836
|
+
* Gets a canvas by its ID.
|
|
837
|
+
*
|
|
838
|
+
* @param options - The options to use to get the canvas.
|
|
839
|
+
* @param options.id - The ID of the canvas to get.
|
|
840
|
+
* @param options.versionId - The version ID of the canvas to get.
|
|
841
|
+
* @param options.applicationId - The application ID of the canvas to get.
|
|
842
|
+
* @param options.client - The client to use to make the request.
|
|
843
|
+
* @param options.input - The input schema of the canvas to get.
|
|
844
|
+
* @param options.output - The output schema of the canvas to get.
|
|
845
|
+
* @returns The canvas.
|
|
755
846
|
*/
|
|
756
|
-
|
|
847
|
+
static get<TInput extends ZodTypeOrRecord, TOutput extends ZodTypeOrRecord>(options: CanvasGetOptions<TInput, TOutput> & {
|
|
848
|
+
client: BaseClient;
|
|
849
|
+
}): Promise<Canvas<TInput, TOutput>>;
|
|
757
850
|
/**
|
|
758
|
-
*
|
|
851
|
+
* Gets the unique identifier of this canvas.
|
|
852
|
+
*
|
|
853
|
+
* @returns The canvas ID.
|
|
759
854
|
*/
|
|
760
|
-
|
|
855
|
+
get id(): string;
|
|
761
856
|
/**
|
|
762
|
-
*
|
|
857
|
+
* Gets the name of this canvas, if provided.
|
|
858
|
+
*
|
|
859
|
+
* @returns The canvas name or undefined.
|
|
763
860
|
*/
|
|
764
|
-
|
|
861
|
+
get name(): string;
|
|
765
862
|
/**
|
|
766
|
-
*
|
|
863
|
+
* Gets the version identifier of this canvas, if specified.
|
|
864
|
+
* When undefined, the latest promoted version is used.
|
|
865
|
+
*
|
|
866
|
+
* @returns The version ID or undefined.
|
|
767
867
|
*/
|
|
768
|
-
|
|
868
|
+
get versionId(): string;
|
|
869
|
+
get applicationId(): string;
|
|
769
870
|
/**
|
|
770
|
-
*
|
|
871
|
+
* Gets the variables of this canvas.
|
|
872
|
+
*
|
|
873
|
+
* @returns The variables of the canvas.
|
|
771
874
|
*/
|
|
772
|
-
|
|
875
|
+
get variables(): Array<CanvasVariable>;
|
|
773
876
|
/**
|
|
774
|
-
*
|
|
877
|
+
* Validates and parses input variables using the canvas input schema.
|
|
878
|
+
*
|
|
879
|
+
* @param variables - Raw input variables to validate.
|
|
880
|
+
* @returns Parsed and validated variables.
|
|
881
|
+
* @throws {ZodError} If validation fails when a Zod schema is configured.
|
|
775
882
|
*/
|
|
776
|
-
|
|
883
|
+
private parseVariables;
|
|
777
884
|
/**
|
|
778
|
-
*
|
|
885
|
+
* Executes this canvas with the provided input variables.
|
|
886
|
+
* Creates and starts a canvas execution, handling variable validation and schema parsing.
|
|
887
|
+
*
|
|
888
|
+
* Returns a promise-like object that allows flexible usage patterns:
|
|
889
|
+
* - Await for the execution object: `const exec = await canvas.execute(vars)`
|
|
890
|
+
* - Direct result access: `const result = await canvas.execute(vars).result`
|
|
891
|
+
* - Stream iteration: `for await (const chunk of canvas.execute(vars, { stream: true }).result)`
|
|
892
|
+
*
|
|
893
|
+
* @param variables - Input variables to pass to the canvas template.
|
|
894
|
+
* @param params - Optional execution parameters (sync/async/stream configuration).
|
|
895
|
+
* @returns A promise-like object with a `result` property for direct result access.
|
|
896
|
+
*
|
|
897
|
+
* @example
|
|
898
|
+
* ```typescript
|
|
899
|
+
* // Direct result access (recommended for simple cases)
|
|
900
|
+
* const result = await canvas.execute({ query: "Hello" }).result;
|
|
901
|
+
*
|
|
902
|
+
* // Get execution object for control (e.g., to abort)
|
|
903
|
+
* const execution = await canvas.execute({ query: "Hello" });
|
|
904
|
+
* const result = await execution.result;
|
|
905
|
+
*
|
|
906
|
+
* // Asynchronous execution with polling
|
|
907
|
+
* const result = await canvas.execute(
|
|
908
|
+
* { query: "Hello" },
|
|
909
|
+
* { async: true, pollingInterval: 500 }
|
|
910
|
+
* ).result;
|
|
911
|
+
*
|
|
912
|
+
* // Streaming execution
|
|
913
|
+
* for await (const chunk of canvas.execute(
|
|
914
|
+
* { query: "Hello" },
|
|
915
|
+
* { stream: true }
|
|
916
|
+
* ).result) {
|
|
917
|
+
* console.log(chunk);
|
|
918
|
+
* }
|
|
919
|
+
* ```
|
|
779
920
|
*/
|
|
780
|
-
|
|
921
|
+
execute(variables: z$1.input<TInput>, params?: SyncExecutionParams): CanvasExecutionPromiseLike<SyncExecutionParams, Output<TInput>, Output<TOutput>>;
|
|
922
|
+
execute(variables: z$1.input<TInput>, params?: AsyncExecutionParams): CanvasExecutionPromiseLike<AsyncExecutionParams, Output<TInput>, Output<TOutput>>;
|
|
923
|
+
execute(variables: z$1.input<TInput>, params?: StreamExecutionParams): CanvasExecutionPromiseLike<StreamExecutionParams, Output<TInput>, Output<TOutput>>;
|
|
781
924
|
}
|
|
782
925
|
|
|
926
|
+
/**
|
|
927
|
+
* Tela SDK main entry point and client initialization.
|
|
928
|
+
*
|
|
929
|
+
* This module provides the main TelaSDK class for initializing the client
|
|
930
|
+
* and accessing all Tela API resources including canvas management, file
|
|
931
|
+
* handling, and error classes.
|
|
932
|
+
*
|
|
933
|
+
* @module Core
|
|
934
|
+
*/
|
|
935
|
+
|
|
783
936
|
/**
|
|
784
937
|
* Configuration options for the TelaSDK.
|
|
785
938
|
*
|
|
@@ -806,6 +959,8 @@ interface TelaSDKOptions extends Omit<BaseClientOptions, 'baseURL'> {
|
|
|
806
959
|
*
|
|
807
960
|
* Provides methods to access various Tela API resources.
|
|
808
961
|
*
|
|
962
|
+
* @category SDK
|
|
963
|
+
*
|
|
809
964
|
* @example
|
|
810
965
|
* ```typescript
|
|
811
966
|
* const tela = new TelaSDK({ apiKey: 'your-api-key' });
|
|
@@ -823,29 +978,47 @@ declare class TelaSDK extends BaseClient {
|
|
|
823
978
|
* Creates a new instance of the TelaSDK.
|
|
824
979
|
*/
|
|
825
980
|
constructor({ baseURL, apiKey, jwt, ...rest }: TelaSDKOptions);
|
|
981
|
+
get authToken(): string;
|
|
826
982
|
private validateAuth;
|
|
827
983
|
protected getAuthHeaders(): Record<string, string>;
|
|
828
984
|
/**
|
|
829
|
-
*
|
|
985
|
+
* Creates a new `TelaFile` instance from the provided file input.
|
|
830
986
|
*
|
|
831
|
-
*
|
|
987
|
+
* @param file - The file input to create a `TelaFile` instance from.
|
|
988
|
+
* @returns A new `TelaFile` instance.
|
|
989
|
+
*/
|
|
990
|
+
createFile: typeof TelaFile.create;
|
|
991
|
+
/**
|
|
992
|
+
* Retrieves a canvas by its ID, version ID, or application ID.
|
|
993
|
+
* Validates input and output schemas if Zod schemas are provided.
|
|
994
|
+
*
|
|
995
|
+
* @param options - Options for retrieving the canvas.
|
|
996
|
+
* @returns A promise resolving to a Canvas instance.
|
|
832
997
|
*
|
|
833
998
|
* @example
|
|
834
999
|
* ```typescript
|
|
835
|
-
*
|
|
836
|
-
*
|
|
837
|
-
*
|
|
1000
|
+
* import { z } from 'zod';
|
|
1001
|
+
*
|
|
1002
|
+
* // Get canvas by ID with schemas
|
|
1003
|
+
* const canvas = await tela.canvas.get({
|
|
1004
|
+
* id: 'canvas-id',
|
|
1005
|
+
* input: z.object({ query: z.string() }),
|
|
1006
|
+
* output: z.object({ response: z.string() })
|
|
838
1007
|
* });
|
|
839
|
-
* ```
|
|
840
|
-
*/
|
|
841
|
-
completions: ChatCompletions;
|
|
842
|
-
/**
|
|
843
|
-
* Creates a new `TelaFile` instance from the provided file input.
|
|
844
1008
|
*
|
|
845
|
-
*
|
|
846
|
-
*
|
|
1009
|
+
* // Get canvas by application ID
|
|
1010
|
+
* const canvas = await tela.canvas.get({
|
|
1011
|
+
* applicationId: 'app-id'
|
|
1012
|
+
* });
|
|
1013
|
+
*
|
|
1014
|
+
* // Execute the canvas
|
|
1015
|
+
* const execution = await canvas.execute({ query: 'Hello' });
|
|
1016
|
+
* const result = await execution.result;
|
|
1017
|
+
* ```
|
|
847
1018
|
*/
|
|
848
|
-
|
|
1019
|
+
canvas: {
|
|
1020
|
+
get: <TInput extends z$1.ZodType | Record<string, unknown> = Record<string, unknown>, TOutput extends z$1.ZodType | Record<string, unknown> = Record<string, unknown>>(options: CanvasGetOptions<TInput, TOutput>) => Promise<Canvas<TInput, TOutput>>;
|
|
1021
|
+
};
|
|
849
1022
|
static TelaSDK: typeof TelaSDK;
|
|
850
1023
|
static DEFAULT_TIMEOUT: number;
|
|
851
1024
|
/** Thrown when an API request fails. */
|
|
@@ -891,4 +1064,4 @@ declare class TelaSDK extends BaseClient {
|
|
|
891
1064
|
*/
|
|
892
1065
|
declare function createTelaClient(opts: TelaSDKOptions): TelaSDK;
|
|
893
1066
|
|
|
894
|
-
export { TelaFile, TelaSDK, type TelaSDKOptions, createTelaClient };
|
|
1067
|
+
export { APIError, AuthenticationError, AuthorizationError, BadRequestError, BaseClient, type BaseClientOptions, type BaseTelaFileOptions, ConflictApiKeyAndJWTError, ConflictError, ConnectionError, ConnectionTimeout, EmptyFileError, ExecutionFailedError, ExecutionNotStartedError, FileUploadError, type HTTPMethods, InternalServerError, InvalidExecutionModeError, InvalidFileURL, MissingApiKeyOrJWTError, NotFoundError, RateLimitError, type RequestOptions, type SchemaBuilder, TelaError, TelaFile, type TelaFileInput, type TelaFileOptions, type TelaFileOptionsWithMimeType, TelaFileSchema, TelaSDK, type TelaSDKOptions, UnprocessableEntityError, UserAbortError, createTelaClient, toError };
|