@meistrari/tela-sdk-js 0.0.2 → 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 -123
- 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/file.d.ts
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { ReadStream } from 'node:fs';
|
|
2
|
-
import { ReadableStream } from 'node:stream/web';
|
|
3
|
-
/**
|
|
4
|
-
* Configuration options for `TelaFile`.
|
|
5
|
-
*/
|
|
6
|
-
export type TelaFileOptions = {
|
|
7
|
-
/**
|
|
8
|
-
* Defines the page range of the document to be processed.
|
|
9
|
-
*
|
|
10
|
-
* Specifies the range of pages to process, starting from `startPage` to `endPage`, inclusive, 0-indexed.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```typescript
|
|
14
|
-
* const file = new TelaFile(fileStream, {
|
|
15
|
-
* range: [0, 1],
|
|
16
|
-
* })
|
|
17
|
-
* ```
|
|
18
|
-
*/
|
|
19
|
-
range?: [number, number];
|
|
20
|
-
};
|
|
21
|
-
/**
|
|
22
|
-
* Represents the possible types for a file input,
|
|
23
|
-
* which can be a URL string, Uint8Array, ReadableStream, ReadStream, Blob, or File.
|
|
24
|
-
*/
|
|
25
|
-
export type TelaFileInput = string | Uint8Array | ReadableStream | ReadStream | Blob | File;
|
|
26
|
-
/**
|
|
27
|
-
* Represents a file with support for various types including URLs, binary data, streams, and Blobs.
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* ```typescript
|
|
31
|
-
* // Using a URL string
|
|
32
|
-
* const urlFile = new TelaFile("https://example.com/file.png", { range: { start: 0, end: 1024 } })
|
|
33
|
-
*
|
|
34
|
-
* // Using a Uint8Array (buffer)
|
|
35
|
-
* const buffer = new Uint8Array([/* binary data * /])
|
|
36
|
-
* const bufferFile = new TelaFile(buffer)
|
|
37
|
-
*
|
|
38
|
-
* // Using a ReadStream
|
|
39
|
-
* import { createReadStream } from 'fs'
|
|
40
|
-
* const readStream = createReadStream('path/to/file')
|
|
41
|
-
* const streamFile = new TelaFile(readStream)
|
|
42
|
-
*
|
|
43
|
-
* // Using a ReadableStream
|
|
44
|
-
* const readableStream = new ReadableStream({
|
|
45
|
-
* start(controller) {
|
|
46
|
-
* controller.enqueue(new TextEncoder().encode('Hello, world!'))
|
|
47
|
-
* controller.close()
|
|
48
|
-
* }
|
|
49
|
-
* })
|
|
50
|
-
* const readableStreamFile = new TelaFile(readableStream)
|
|
51
|
-
*
|
|
52
|
-
* // Using a Blob (in environments that support Blob)
|
|
53
|
-
* const blob = new Blob(['file content'], { type: 'text/plain' })
|
|
54
|
-
* const blobFile = new TelaFile(blob)
|
|
55
|
-
*
|
|
56
|
-
* // Using a File (in browser environments)
|
|
57
|
-
* const file = new File(['file content'], 'filename.txt', { type: 'text/plain' })
|
|
58
|
-
* const fileInstance = new TelaFile(file)
|
|
59
|
-
* ```
|
|
60
|
-
*/
|
|
61
|
-
export declare class TelaFile {
|
|
62
|
-
private _file;
|
|
63
|
-
private _options;
|
|
64
|
-
private _size;
|
|
65
|
-
/**
|
|
66
|
-
* Creates an instance of `TelaFile`.
|
|
67
|
-
*
|
|
68
|
-
* @param file - The source of the file. Can be a URL string, Uint8Array, ReadableStream, ReadStream, Blob, or File.
|
|
69
|
-
* @param options - Optional configuration options such as byte range.
|
|
70
|
-
* @throws {InvalidFileURL} If the provided URL is not valid.
|
|
71
|
-
* @throws {EmptyFileError} If the provided file is empty.
|
|
72
|
-
*/
|
|
73
|
-
constructor(file: string | Uint8Array | ReadableStream | ReadStream | Blob | File, options?: TelaFileOptions);
|
|
74
|
-
/**
|
|
75
|
-
* Retrieves the options provided during instantiation.
|
|
76
|
-
*
|
|
77
|
-
* @returns The `TelaFileOptions` or an empty object if none were provided.
|
|
78
|
-
*/
|
|
79
|
-
get options(): TelaFileOptions;
|
|
80
|
-
/**
|
|
81
|
-
* Determines whether the file source is a valid URL.
|
|
82
|
-
*
|
|
83
|
-
* @returns `true` if the file source is a valid URL string, otherwise `false`.
|
|
84
|
-
*/
|
|
85
|
-
get isURL(): boolean;
|
|
86
|
-
/**
|
|
87
|
-
* Gets the size of the file in bytes.
|
|
88
|
-
*
|
|
89
|
-
* @returns The size of the file if available, otherwise `null`.
|
|
90
|
-
*/
|
|
91
|
-
get size(): number | null;
|
|
92
|
-
/**
|
|
93
|
-
* Retrieves the content of the file in a format suitable for uploading.
|
|
94
|
-
*
|
|
95
|
-
* - If the file is a URL, it returns the URL string.
|
|
96
|
-
* - If the file is a `Uint8Array`, it converts it to a `File` object.
|
|
97
|
-
* - If the file is a `ReadStream` or `ReadableStream`, it calculates the size and returns a stream.
|
|
98
|
-
*
|
|
99
|
-
* @returns A promise that resolves to the uploadable content.
|
|
100
|
-
*/
|
|
101
|
-
getUploadableContent(): Promise<string | File | Blob | ReadableStream>;
|
|
102
|
-
/**
|
|
103
|
-
* Validates the provided file based on its type.
|
|
104
|
-
*
|
|
105
|
-
* @throws {InvalidFileURL} If the file is a string but not a valid URL.
|
|
106
|
-
* @throws {EmptyFileError} If the file is empty.
|
|
107
|
-
*/
|
|
108
|
-
private validateFile;
|
|
109
|
-
/**
|
|
110
|
-
* Checks if the provided string is a valid URL.
|
|
111
|
-
*
|
|
112
|
-
* @param url - The URL string to validate.
|
|
113
|
-
* @returns `true` if the URL is valid, otherwise `false`.
|
|
114
|
-
*/
|
|
115
|
-
private isValidURL;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Creates a new `TelaFile` instance from the provided file input.
|
|
119
|
-
*
|
|
120
|
-
* @param file - The file input to create a `TelaFile` instance from.
|
|
121
|
-
* @returns A new `TelaFile` instance.
|
|
122
|
-
*/
|
|
123
|
-
export declare function createTelaFile(file: TelaFileInput): TelaFile;
|
package/dist/file.js
DELETED
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import { ReadStream } from 'node:fs';
|
|
2
|
-
import { Readable } from 'node:stream';
|
|
3
|
-
import { ReadableStream } from 'node:stream/web';
|
|
4
|
-
import { EmptyFileError, InvalidFileURL } from './errors';
|
|
5
|
-
import { getStreamSize } from './utils/stream';
|
|
6
|
-
/**
|
|
7
|
-
* Represents a file with support for various types including URLs, binary data, streams, and Blobs.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```typescript
|
|
11
|
-
* // Using a URL string
|
|
12
|
-
* const urlFile = new TelaFile("https://example.com/file.png", { range: { start: 0, end: 1024 } })
|
|
13
|
-
*
|
|
14
|
-
* // Using a Uint8Array (buffer)
|
|
15
|
-
* const buffer = new Uint8Array([/* binary data * /])
|
|
16
|
-
* const bufferFile = new TelaFile(buffer)
|
|
17
|
-
*
|
|
18
|
-
* // Using a ReadStream
|
|
19
|
-
* import { createReadStream } from 'fs'
|
|
20
|
-
* const readStream = createReadStream('path/to/file')
|
|
21
|
-
* const streamFile = new TelaFile(readStream)
|
|
22
|
-
*
|
|
23
|
-
* // Using a ReadableStream
|
|
24
|
-
* const readableStream = new ReadableStream({
|
|
25
|
-
* start(controller) {
|
|
26
|
-
* controller.enqueue(new TextEncoder().encode('Hello, world!'))
|
|
27
|
-
* controller.close()
|
|
28
|
-
* }
|
|
29
|
-
* })
|
|
30
|
-
* const readableStreamFile = new TelaFile(readableStream)
|
|
31
|
-
*
|
|
32
|
-
* // Using a Blob (in environments that support Blob)
|
|
33
|
-
* const blob = new Blob(['file content'], { type: 'text/plain' })
|
|
34
|
-
* const blobFile = new TelaFile(blob)
|
|
35
|
-
*
|
|
36
|
-
* // Using a File (in browser environments)
|
|
37
|
-
* const file = new File(['file content'], 'filename.txt', { type: 'text/plain' })
|
|
38
|
-
* const fileInstance = new TelaFile(file)
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
|
-
export class TelaFile {
|
|
42
|
-
_file;
|
|
43
|
-
_options;
|
|
44
|
-
_size = null;
|
|
45
|
-
/**
|
|
46
|
-
* Creates an instance of `TelaFile`.
|
|
47
|
-
*
|
|
48
|
-
* @param file - The source of the file. Can be a URL string, Uint8Array, ReadableStream, ReadStream, Blob, or File.
|
|
49
|
-
* @param options - Optional configuration options such as byte range.
|
|
50
|
-
* @throws {InvalidFileURL} If the provided URL is not valid.
|
|
51
|
-
* @throws {EmptyFileError} If the provided file is empty.
|
|
52
|
-
*/
|
|
53
|
-
constructor(file, options) {
|
|
54
|
-
this._file = file;
|
|
55
|
-
this._options = options;
|
|
56
|
-
this.validateFile();
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Retrieves the options provided during instantiation.
|
|
60
|
-
*
|
|
61
|
-
* @returns The `TelaFileOptions` or an empty object if none were provided.
|
|
62
|
-
*/
|
|
63
|
-
get options() {
|
|
64
|
-
return this._options || {};
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Determines whether the file source is a valid URL.
|
|
68
|
-
*
|
|
69
|
-
* @returns `true` if the file source is a valid URL string, otherwise `false`.
|
|
70
|
-
*/
|
|
71
|
-
get isURL() {
|
|
72
|
-
return typeof this._file === 'string' && this.isValidURL(this._file);
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Gets the size of the file in bytes.
|
|
76
|
-
*
|
|
77
|
-
* @returns The size of the file if available, otherwise `null`.
|
|
78
|
-
*/
|
|
79
|
-
get size() {
|
|
80
|
-
return this._size;
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Retrieves the content of the file in a format suitable for uploading.
|
|
84
|
-
*
|
|
85
|
-
* - If the file is a URL, it returns the URL string.
|
|
86
|
-
* - If the file is a `Uint8Array`, it converts it to a `File` object.
|
|
87
|
-
* - If the file is a `ReadStream` or `ReadableStream`, it calculates the size and returns a stream.
|
|
88
|
-
*
|
|
89
|
-
* @returns A promise that resolves to the uploadable content.
|
|
90
|
-
*/
|
|
91
|
-
async getUploadableContent() {
|
|
92
|
-
if (this.isURL && typeof this._file === 'string') {
|
|
93
|
-
return this._file;
|
|
94
|
-
}
|
|
95
|
-
if (this._file instanceof Uint8Array) {
|
|
96
|
-
return new File([this._file], 'file', {
|
|
97
|
-
type: 'application/octet-stream',
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
if (this._file instanceof ReadStream) {
|
|
101
|
-
const webStream = Readable.toWeb(this._file);
|
|
102
|
-
const [sizeStream, contentStream] = webStream.tee();
|
|
103
|
-
this._size = await getStreamSize(sizeStream);
|
|
104
|
-
return contentStream;
|
|
105
|
-
}
|
|
106
|
-
if (this._file instanceof ReadableStream) {
|
|
107
|
-
const [sizeStream, contentStream] = this._file.tee();
|
|
108
|
-
this._size = await getStreamSize(sizeStream);
|
|
109
|
-
return contentStream;
|
|
110
|
-
}
|
|
111
|
-
return this._file;
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Validates the provided file based on its type.
|
|
115
|
-
*
|
|
116
|
-
* @throws {InvalidFileURL} If the file is a string but not a valid URL.
|
|
117
|
-
* @throws {EmptyFileError} If the file is empty.
|
|
118
|
-
*/
|
|
119
|
-
validateFile() {
|
|
120
|
-
if (typeof this._file === 'string') {
|
|
121
|
-
if (!this.isValidURL(this._file)) {
|
|
122
|
-
throw new InvalidFileURL(this._file);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
if (this._file instanceof Uint8Array) {
|
|
126
|
-
if (this._file.length === 0) {
|
|
127
|
-
throw new EmptyFileError();
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
if (this._file instanceof Blob || this._file instanceof File) {
|
|
131
|
-
if (this._file.size === 0) {
|
|
132
|
-
throw new EmptyFileError();
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Checks if the provided string is a valid URL.
|
|
138
|
-
*
|
|
139
|
-
* @param url - The URL string to validate.
|
|
140
|
-
* @returns `true` if the URL is valid, otherwise `false`.
|
|
141
|
-
*/
|
|
142
|
-
isValidURL(url) {
|
|
143
|
-
try {
|
|
144
|
-
// eslint-disable-next-line no-new
|
|
145
|
-
new URL(url);
|
|
146
|
-
return true;
|
|
147
|
-
}
|
|
148
|
-
catch {
|
|
149
|
-
return false;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Creates a new `TelaFile` instance from the provided file input.
|
|
155
|
-
*
|
|
156
|
-
* @param file - The file input to create a `TelaFile` instance from.
|
|
157
|
-
* @returns A new `TelaFile` instance.
|
|
158
|
-
*/
|
|
159
|
-
export function createTelaFile(file) {
|
|
160
|
-
return new TelaFile(file);
|
|
161
|
-
}
|
package/dist/index.js
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import { BaseClient } from './base-client';
|
|
2
|
-
import * as Errors from './errors';
|
|
3
|
-
import { createTelaFile } from './file';
|
|
4
|
-
import * as Resources from './resources';
|
|
5
|
-
export { TelaFile } from './file';
|
|
6
|
-
const baseUrl = 'https://api.tela.com';
|
|
7
|
-
/**
|
|
8
|
-
* The main class for interacting with the Tela API.
|
|
9
|
-
*
|
|
10
|
-
* Provides methods to access various Tela API resources.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```typescript
|
|
14
|
-
* const tela = new TelaSDK({ apiKey: 'your-api-key' });
|
|
15
|
-
* const completion = await tela.completions.create({
|
|
16
|
-
* canvasId: "your-canvas-id",
|
|
17
|
-
* messages: [{ role: "user", content: "Hello!" }],
|
|
18
|
-
* });
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
export class TelaSDK extends BaseClient {
|
|
22
|
-
opts;
|
|
23
|
-
apiKey;
|
|
24
|
-
jwt;
|
|
25
|
-
/**
|
|
26
|
-
* Creates a new instance of the TelaSDK.
|
|
27
|
-
*/
|
|
28
|
-
constructor({ baseURL = baseUrl, apiKey, jwt, ...rest }) {
|
|
29
|
-
super({ baseURL, ...rest });
|
|
30
|
-
this.opts = { baseURL, ...rest };
|
|
31
|
-
this.apiKey = apiKey;
|
|
32
|
-
this.jwt = jwt;
|
|
33
|
-
this.validateAuth();
|
|
34
|
-
}
|
|
35
|
-
validateAuth() {
|
|
36
|
-
if (!this.apiKey && !this.jwt) {
|
|
37
|
-
throw new Errors.MissingApiKeyOrJWTError();
|
|
38
|
-
}
|
|
39
|
-
if (this.apiKey && this.jwt) {
|
|
40
|
-
throw new Errors.ConflictApiKeyAndJWTError();
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
getAuthHeaders() {
|
|
44
|
-
return {
|
|
45
|
-
Authorization: `Bearer ${this.apiKey || this.jwt}`,
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* The ChatCompletions resource for interacting with Tela's chat completion API.
|
|
50
|
-
*
|
|
51
|
-
* Use this to generate chat completions based on provided messages.
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* ```typescript
|
|
55
|
-
* const completion = await tela.completions.create({
|
|
56
|
-
* canvasId: "your-canvas-id",
|
|
57
|
-
* messages: [{ role: "user", content: "Hello!" }],
|
|
58
|
-
* });
|
|
59
|
-
* ```
|
|
60
|
-
*/
|
|
61
|
-
completions = new Resources.ChatCompletions(this);
|
|
62
|
-
/**
|
|
63
|
-
* Creates a new `TelaFile` instance from the provided file input.
|
|
64
|
-
*
|
|
65
|
-
* @param file - The file input to create a `TelaFile` instance from.
|
|
66
|
-
* @returns A new `TelaFile` instance.
|
|
67
|
-
*/
|
|
68
|
-
createFile = createTelaFile.bind(this);
|
|
69
|
-
static TelaSDK = this;
|
|
70
|
-
static DEFAULT_TIMEOUT = 0;
|
|
71
|
-
// Error classes
|
|
72
|
-
/** Thrown when an API request fails. */
|
|
73
|
-
static APIError = Errors.APIError;
|
|
74
|
-
/** Thrown when a request is aborted by the user. */
|
|
75
|
-
static UserAbortError = Errors.UserAbortError;
|
|
76
|
-
/** Thrown when there's a network connection error. */
|
|
77
|
-
static ConnectionError = Errors.ConnectionError;
|
|
78
|
-
/** Thrown when a request times out. */
|
|
79
|
-
static ConnectionTimeout = Errors.ConnectionTimeout;
|
|
80
|
-
/** Thrown when the API returns a 400 Bad Request error. */
|
|
81
|
-
static BadRequestError = Errors.BadRequestError;
|
|
82
|
-
/** Thrown when authentication fails (e.g., invalid API key). */
|
|
83
|
-
static AuthenticationError = Errors.AuthenticationError;
|
|
84
|
-
/** Thrown when the authenticated user doesn't have permission for the requested operation. */
|
|
85
|
-
static AuthorizationError = Errors.AuthorizationError;
|
|
86
|
-
/** Thrown when the requested resource is not found. */
|
|
87
|
-
static NotFoundError = Errors.NotFoundError;
|
|
88
|
-
/** Thrown when there's a conflict with the current state of the resource. */
|
|
89
|
-
static ConflictError = Errors.ConflictError;
|
|
90
|
-
/** Thrown when the request is well-formed but unable to be processed due to semantic errors. */
|
|
91
|
-
static UnprocessableEntityError = Errors.UnprocessableEntityError;
|
|
92
|
-
/** Thrown when the API rate limit is exceeded. */
|
|
93
|
-
static RateLimitError = Errors.RateLimitError;
|
|
94
|
-
/** Thrown when an unexpected server error occurs. */
|
|
95
|
-
static InternalServerError = Errors.InternalServerError;
|
|
96
|
-
/** Thrown when attempting to upload an empty file. */
|
|
97
|
-
static EmptyFileError = Errors.EmptyFileError;
|
|
98
|
-
/** Thrown when an invalid file URL is provided. */
|
|
99
|
-
static InvalidFileURL = Errors.InvalidFileURL;
|
|
100
|
-
/** Thrown when there's an error during file upload. */
|
|
101
|
-
static FileUploadError = Errors.FileUploadError;
|
|
102
|
-
/** Thrown when neither an API key nor a JWT is provided. */
|
|
103
|
-
static MissingApiKeyOrJWTError = Errors.MissingApiKeyOrJWTError;
|
|
104
|
-
/** Thrown when both an API key and a JWT are provided. */
|
|
105
|
-
static ConflictApiKeyAndJWTError = Errors.ConflictApiKeyAndJWTError;
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Creates a new instance of the TelaSDK.
|
|
109
|
-
*
|
|
110
|
-
* @param opts - The options for configuring the TelaSDK.
|
|
111
|
-
* @returns A new instance of the TelaSDK.
|
|
112
|
-
*/
|
|
113
|
-
export function createTelaClient(opts) {
|
|
114
|
-
return new TelaSDK(opts);
|
|
115
|
-
}
|
package/dist/resource.d.ts
DELETED