@meistrari/tela-sdk-js 2.5.0 → 2.6.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/dist/index.cjs +951 -253
- package/dist/index.d.cts +1280 -364
- package/dist/index.d.mts +1280 -364
- package/dist/index.d.ts +1280 -364
- package/dist/index.mjs +1022 -327
- package/package.json +1 -2
package/dist/index.mjs
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import * as changeCase from 'change-case';
|
|
2
2
|
import { minimatch } from 'minimatch';
|
|
3
|
-
import
|
|
4
|
-
import z__default, { z as z$1, ZodError } from 'zod';
|
|
3
|
+
import z, { z as z$1, ZodError } from 'zod';
|
|
5
4
|
import Emittery from 'emittery';
|
|
6
5
|
|
|
7
|
-
const version = "2.
|
|
6
|
+
const version = "2.6.0";
|
|
8
7
|
|
|
9
|
-
var __defProp$
|
|
10
|
-
var __defNormalProp$
|
|
11
|
-
var __publicField$
|
|
12
|
-
__defNormalProp$
|
|
8
|
+
var __defProp$a = Object.defineProperty;
|
|
9
|
+
var __defNormalProp$a = (obj, key, value) => key in obj ? __defProp$a(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
10
|
+
var __publicField$a = (obj, key, value) => {
|
|
11
|
+
__defNormalProp$a(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
13
12
|
return value;
|
|
14
13
|
};
|
|
15
14
|
class TelaError extends Error {
|
|
@@ -37,7 +36,7 @@ class InvalidFileURL extends TelaError {
|
|
|
37
36
|
class FileUploadError extends TelaError {
|
|
38
37
|
constructor(message, statusCode) {
|
|
39
38
|
super(`Failed to upload file: ${message} (Status code: ${statusCode})`);
|
|
40
|
-
__publicField$
|
|
39
|
+
__publicField$a(this, "statusCode");
|
|
41
40
|
this.statusCode = statusCode;
|
|
42
41
|
}
|
|
43
42
|
}
|
|
@@ -54,14 +53,21 @@ class InvalidExecutionModeError extends TelaError {
|
|
|
54
53
|
class ExecutionFailedError extends TelaError {
|
|
55
54
|
constructor(rawOutput) {
|
|
56
55
|
super(`Execution failed: ${JSON.stringify(rawOutput)}`);
|
|
57
|
-
__publicField$
|
|
56
|
+
__publicField$a(this, "rawOutput");
|
|
58
57
|
this.rawOutput = rawOutput;
|
|
59
58
|
}
|
|
60
59
|
}
|
|
60
|
+
class TaskFailedError extends TelaError {
|
|
61
|
+
constructor(rawTask, message, cause) {
|
|
62
|
+
super(`Task failed: ${JSON.stringify(rawTask)}`, { cause });
|
|
63
|
+
__publicField$a(this, "rawTask");
|
|
64
|
+
this.rawTask = rawTask;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
61
67
|
class BatchExecutionFailedError extends TelaError {
|
|
62
68
|
constructor(rawResponse) {
|
|
63
69
|
super(`Batch execution failed: ${JSON.stringify(rawResponse)}`);
|
|
64
|
-
__publicField$
|
|
70
|
+
__publicField$a(this, "rawResponse");
|
|
65
71
|
this.rawResponse = rawResponse;
|
|
66
72
|
}
|
|
67
73
|
}
|
|
@@ -69,8 +75,8 @@ class APIError extends TelaError {
|
|
|
69
75
|
constructor(statusCode, error, _message) {
|
|
70
76
|
const message = error?.message ? typeof error.message === "string" ? error.message : JSON.stringify(error.message) : error ? JSON.stringify(error) : _message;
|
|
71
77
|
super(message);
|
|
72
|
-
__publicField$
|
|
73
|
-
__publicField$
|
|
78
|
+
__publicField$a(this, "statusCode");
|
|
79
|
+
__publicField$a(this, "error");
|
|
74
80
|
this.statusCode = statusCode;
|
|
75
81
|
this.error = error;
|
|
76
82
|
}
|
|
@@ -112,7 +118,7 @@ class APIError extends TelaError {
|
|
|
112
118
|
class UserAbortError extends APIError {
|
|
113
119
|
constructor({ message } = {}) {
|
|
114
120
|
super(void 0, void 0, message || "User aborted.");
|
|
115
|
-
__publicField$
|
|
121
|
+
__publicField$a(this, "statusCode");
|
|
116
122
|
}
|
|
117
123
|
}
|
|
118
124
|
class ConnectionError extends APIError {
|
|
@@ -121,7 +127,7 @@ class ConnectionError extends APIError {
|
|
|
121
127
|
cause
|
|
122
128
|
}) {
|
|
123
129
|
super(void 0, void 0, message || "Connection error.");
|
|
124
|
-
__publicField$
|
|
130
|
+
__publicField$a(this, "statusCode");
|
|
125
131
|
if (cause)
|
|
126
132
|
this.cause = cause;
|
|
127
133
|
}
|
|
@@ -131,57 +137,57 @@ class ConnectionTimeout extends APIError {
|
|
|
131
137
|
message
|
|
132
138
|
} = {}) {
|
|
133
139
|
super(void 0, void 0, message || "Request timed out.");
|
|
134
|
-
__publicField$
|
|
140
|
+
__publicField$a(this, "statusCode");
|
|
135
141
|
}
|
|
136
142
|
}
|
|
137
143
|
class BadRequestError extends APIError {
|
|
138
144
|
constructor() {
|
|
139
145
|
super(...arguments);
|
|
140
|
-
__publicField$
|
|
146
|
+
__publicField$a(this, "statusCode", 400);
|
|
141
147
|
}
|
|
142
148
|
// todo: handle validation errors from zod/typebox
|
|
143
149
|
}
|
|
144
150
|
class AuthenticationError extends APIError {
|
|
145
151
|
constructor() {
|
|
146
152
|
super(...arguments);
|
|
147
|
-
__publicField$
|
|
153
|
+
__publicField$a(this, "statusCode", 401);
|
|
148
154
|
}
|
|
149
155
|
}
|
|
150
156
|
class AuthorizationError extends APIError {
|
|
151
157
|
constructor() {
|
|
152
158
|
super(...arguments);
|
|
153
|
-
__publicField$
|
|
159
|
+
__publicField$a(this, "statusCode", 403);
|
|
154
160
|
}
|
|
155
161
|
}
|
|
156
162
|
class NotFoundError extends APIError {
|
|
157
163
|
constructor() {
|
|
158
164
|
super(...arguments);
|
|
159
|
-
__publicField$
|
|
165
|
+
__publicField$a(this, "statusCode", 404);
|
|
160
166
|
}
|
|
161
167
|
}
|
|
162
168
|
class ConflictError extends APIError {
|
|
163
169
|
constructor() {
|
|
164
170
|
super(...arguments);
|
|
165
|
-
__publicField$
|
|
171
|
+
__publicField$a(this, "statusCode", 409);
|
|
166
172
|
}
|
|
167
173
|
}
|
|
168
174
|
class UnprocessableEntityError extends APIError {
|
|
169
175
|
constructor() {
|
|
170
176
|
super(...arguments);
|
|
171
177
|
// todo: check if tela returns 400 or 422 for zod errors
|
|
172
|
-
__publicField$
|
|
178
|
+
__publicField$a(this, "statusCode", 422);
|
|
173
179
|
}
|
|
174
180
|
}
|
|
175
181
|
class RateLimitError extends APIError {
|
|
176
182
|
constructor() {
|
|
177
183
|
super(...arguments);
|
|
178
|
-
__publicField$
|
|
184
|
+
__publicField$a(this, "statusCode", 429);
|
|
179
185
|
}
|
|
180
186
|
}
|
|
181
187
|
class InternalServerError extends APIError {
|
|
182
188
|
constructor() {
|
|
183
189
|
super(...arguments);
|
|
184
|
-
__publicField$
|
|
190
|
+
__publicField$a(this, "statusCode", 500);
|
|
185
191
|
}
|
|
186
192
|
}
|
|
187
193
|
function toError(err) {
|
|
@@ -320,10 +326,10 @@ function transformDurationToMs(durationStr) {
|
|
|
320
326
|
return Number.parseInt(amount) * multiplier;
|
|
321
327
|
}
|
|
322
328
|
|
|
323
|
-
var __defProp$
|
|
324
|
-
var __defNormalProp$
|
|
325
|
-
var __publicField$
|
|
326
|
-
__defNormalProp$
|
|
329
|
+
var __defProp$9 = Object.defineProperty;
|
|
330
|
+
var __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
331
|
+
var __publicField$9 = (obj, key, value) => {
|
|
332
|
+
__defNormalProp$9(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
327
333
|
return value;
|
|
328
334
|
};
|
|
329
335
|
class Stream {
|
|
@@ -587,9 +593,9 @@ function findDoubleNewlineIndex(buffer) {
|
|
|
587
593
|
}
|
|
588
594
|
class SSEDecoder {
|
|
589
595
|
constructor() {
|
|
590
|
-
__publicField$
|
|
591
|
-
__publicField$
|
|
592
|
-
__publicField$
|
|
596
|
+
__publicField$9(this, "data");
|
|
597
|
+
__publicField$9(this, "event");
|
|
598
|
+
__publicField$9(this, "chunks");
|
|
593
599
|
this.event = null;
|
|
594
600
|
this.data = [];
|
|
595
601
|
this.chunks = [];
|
|
@@ -630,9 +636,9 @@ class SSEDecoder {
|
|
|
630
636
|
const _LineDecoder = class _LineDecoder {
|
|
631
637
|
// TextDecoder found in browsers; not typed to avoid pulling in either "dom" or "node" types.
|
|
632
638
|
constructor() {
|
|
633
|
-
__publicField$
|
|
634
|
-
__publicField$
|
|
635
|
-
__publicField$
|
|
639
|
+
__publicField$9(this, "buffer");
|
|
640
|
+
__publicField$9(this, "trailingCR");
|
|
641
|
+
__publicField$9(this, "textDecoder");
|
|
636
642
|
this.buffer = [];
|
|
637
643
|
this.trailingCR = false;
|
|
638
644
|
}
|
|
@@ -707,8 +713,8 @@ const _LineDecoder = class _LineDecoder {
|
|
|
707
713
|
}
|
|
708
714
|
};
|
|
709
715
|
// prettier-ignore
|
|
710
|
-
__publicField$
|
|
711
|
-
__publicField$
|
|
716
|
+
__publicField$9(_LineDecoder, "NEWLINE_CHARS", /* @__PURE__ */ new Set(["\n", "\r"]));
|
|
717
|
+
__publicField$9(_LineDecoder, "NEWLINE_REGEXP", /\r\n|[\n\r]/g);
|
|
712
718
|
let LineDecoder = _LineDecoder;
|
|
713
719
|
function partition(str, delimiter) {
|
|
714
720
|
const index = str.indexOf(delimiter);
|
|
@@ -745,10 +751,10 @@ function readableStreamAsyncIterable(stream) {
|
|
|
745
751
|
};
|
|
746
752
|
}
|
|
747
753
|
|
|
748
|
-
var __defProp$
|
|
749
|
-
var __defNormalProp$
|
|
750
|
-
var __publicField$
|
|
751
|
-
__defNormalProp$
|
|
754
|
+
var __defProp$8 = Object.defineProperty;
|
|
755
|
+
var __defNormalProp$8 = (obj, key, value) => key in obj ? __defProp$8(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
756
|
+
var __publicField$8 = (obj, key, value) => {
|
|
757
|
+
__defNormalProp$8(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
752
758
|
return value;
|
|
753
759
|
};
|
|
754
760
|
function getRequestIdFromResponse(response) {
|
|
@@ -804,31 +810,30 @@ async function defaultParseResponse(props) {
|
|
|
804
810
|
const json = await response.json();
|
|
805
811
|
debug("response", response.status, response.url, response.headers, json);
|
|
806
812
|
const requestId = getRequestIdFromResponse(response);
|
|
807
|
-
const finalJson = { ...json };
|
|
808
813
|
if (requestId) {
|
|
809
|
-
|
|
814
|
+
json.requestId = requestId;
|
|
810
815
|
}
|
|
811
|
-
return transformCase ? transformObjectFromSnakeCaseToCamelCase(
|
|
816
|
+
return transformCase ? transformObjectFromSnakeCaseToCamelCase(json, DEFAULT_FIELDS_TRANSFORMATION_EXCLUSIONS) : json;
|
|
812
817
|
}
|
|
813
818
|
const text = await response.text();
|
|
814
819
|
debug("response", response.status, response.url, response.headers, text);
|
|
815
820
|
return text;
|
|
816
821
|
}
|
|
817
|
-
function
|
|
818
|
-
if (!
|
|
819
|
-
return
|
|
822
|
+
function transformObjectCase(obj, transformCase) {
|
|
823
|
+
if (!obj) {
|
|
824
|
+
return obj;
|
|
820
825
|
}
|
|
821
826
|
if (transformCase) {
|
|
822
|
-
return transformObjectFromCamelCaseToSnakeCase(
|
|
827
|
+
return transformObjectFromCamelCaseToSnakeCase(obj, DEFAULT_FIELDS_TRANSFORMATION_EXCLUSIONS);
|
|
823
828
|
}
|
|
824
|
-
return
|
|
829
|
+
return obj;
|
|
825
830
|
}
|
|
826
831
|
class BaseClient {
|
|
827
832
|
constructor({ baseURL, maxRetries = 5, timeout = 0 }) {
|
|
828
|
-
__publicField$
|
|
829
|
-
__publicField$
|
|
830
|
-
__publicField$
|
|
831
|
-
__publicField$
|
|
833
|
+
__publicField$8(this, "baseURL");
|
|
834
|
+
__publicField$8(this, "maxRetries");
|
|
835
|
+
__publicField$8(this, "timeout");
|
|
836
|
+
__publicField$8(this, "fetch");
|
|
832
837
|
this.baseURL = baseURL;
|
|
833
838
|
this.maxRetries = validateMaxRetries(maxRetries);
|
|
834
839
|
this.timeout = validateTimeout(timeout);
|
|
@@ -870,8 +875,8 @@ class BaseClient {
|
|
|
870
875
|
const { transformCase = true } = opts ?? {};
|
|
871
876
|
const headers = this.createHeaders();
|
|
872
877
|
debug("methodRequest", method, path, opts);
|
|
873
|
-
const transformedQuery = opts?.query
|
|
874
|
-
const transformedBody =
|
|
878
|
+
const transformedQuery = transformObjectCase(opts?.query, transformCase);
|
|
879
|
+
const transformedBody = transformObjectCase(opts?.body, transformCase);
|
|
875
880
|
const { response, options, controller } = await this.request({
|
|
876
881
|
method,
|
|
877
882
|
path,
|
|
@@ -1052,6 +1057,9 @@ class BaseClient {
|
|
|
1052
1057
|
if (value === null) {
|
|
1053
1058
|
return `${encodeURIComponent(key)}=`;
|
|
1054
1059
|
}
|
|
1060
|
+
if (Array.isArray(value)) {
|
|
1061
|
+
return value.map((item) => `${encodeURIComponent(key)}[]=${encodeURIComponent(item)}`).join("&");
|
|
1062
|
+
}
|
|
1055
1063
|
throw new TelaError(
|
|
1056
1064
|
`Cannot stringify type ${typeof value}; Expected string, number, boolean, or null. If you need to pass nested query parameters, you can manually encode them, e.g. { query: { 'foo[key1]': value1, 'foo[key2]': value2 } }, and please open a GitHub issue requesting better support for your use case.`
|
|
1057
1065
|
);
|
|
@@ -1105,22 +1113,22 @@ async function getStreamSize(stream) {
|
|
|
1105
1113
|
return size;
|
|
1106
1114
|
}
|
|
1107
1115
|
|
|
1108
|
-
var __defProp$
|
|
1109
|
-
var __defNormalProp$
|
|
1110
|
-
var __publicField$
|
|
1111
|
-
__defNormalProp$
|
|
1116
|
+
var __defProp$7 = Object.defineProperty;
|
|
1117
|
+
var __defNormalProp$7 = (obj, key, value) => key in obj ? __defProp$7(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1118
|
+
var __publicField$7 = (obj, key, value) => {
|
|
1119
|
+
__defNormalProp$7(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
1112
1120
|
return value;
|
|
1113
1121
|
};
|
|
1114
1122
|
function TelaFileSchema() {
|
|
1115
|
-
return
|
|
1123
|
+
return z.custom((value) => value instanceof TelaFile, { message: "Value must be an instance of TelaFile" }).meta({ isTelaFile: true });
|
|
1116
1124
|
}
|
|
1117
1125
|
class TelaFile {
|
|
1118
1126
|
constructor(file, options = {}) {
|
|
1119
|
-
__publicField$
|
|
1120
|
-
__publicField$
|
|
1121
|
-
__publicField$
|
|
1122
|
-
__publicField$
|
|
1123
|
-
__publicField$
|
|
1127
|
+
__publicField$7(this, "_file");
|
|
1128
|
+
__publicField$7(this, "_options");
|
|
1129
|
+
__publicField$7(this, "_size", null);
|
|
1130
|
+
__publicField$7(this, "_mimeType");
|
|
1131
|
+
__publicField$7(this, "_name");
|
|
1124
1132
|
this._file = file;
|
|
1125
1133
|
if (file instanceof File || file instanceof Blob) {
|
|
1126
1134
|
this._size = file.size;
|
|
@@ -1266,7 +1274,17 @@ class TelaFile {
|
|
|
1266
1274
|
return url.startsWith("vault://");
|
|
1267
1275
|
}
|
|
1268
1276
|
}
|
|
1277
|
+
function isTelaFile(obj) {
|
|
1278
|
+
return obj instanceof TelaFile;
|
|
1279
|
+
}
|
|
1280
|
+
function isTelaFileArray(obj) {
|
|
1281
|
+
return Array.isArray(obj) && obj.length > 0 && obj.every(isTelaFile);
|
|
1282
|
+
}
|
|
1269
1283
|
|
|
1284
|
+
const zod = {
|
|
1285
|
+
...z$1,
|
|
1286
|
+
file: TelaFileSchema
|
|
1287
|
+
};
|
|
1270
1288
|
function compareSchemas(clientSchema, serverSchema, path = "$") {
|
|
1271
1289
|
const mismatches = [];
|
|
1272
1290
|
if (clientSchema.type !== serverSchema.type) {
|
|
@@ -1423,87 +1441,54 @@ function validateInputSchema(clientSchema, serverVariables) {
|
|
|
1423
1441
|
function validateOutputSchema(clientSchema, serverSchema) {
|
|
1424
1442
|
return compareSchemas(clientSchema, serverSchema);
|
|
1425
1443
|
}
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
}
|
|
1441
|
-
}
|
|
1442
|
-
|
|
1443
|
-
}
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
__publicField$4(this, "_interval");
|
|
1447
|
-
__publicField$4(this, "_timeout");
|
|
1448
|
-
__publicField$4(this, "_abortSignal");
|
|
1449
|
-
__publicField$4(this, "_internalAbortController");
|
|
1450
|
-
if (interval <= 0) {
|
|
1451
|
-
throw new TelaError("Interval must be greater than 0");
|
|
1452
|
-
}
|
|
1453
|
-
if (timeout2 <= 0) {
|
|
1454
|
-
throw new TelaError("Timeout must be greater than 0");
|
|
1444
|
+
function validateSchemas(resource, input, output, promptVersion) {
|
|
1445
|
+
const canvasIdentifier = `${promptVersion.title} (${promptVersion.promptId})`;
|
|
1446
|
+
if (input instanceof z$1.ZodType) {
|
|
1447
|
+
try {
|
|
1448
|
+
const inputSchema = z$1.toJSONSchema(input, { unrepresentable: "any" });
|
|
1449
|
+
const mismatches = validateInputSchema(inputSchema, promptVersion.variables);
|
|
1450
|
+
if (mismatches.length === 0) {
|
|
1451
|
+
return;
|
|
1452
|
+
}
|
|
1453
|
+
console.warn(
|
|
1454
|
+
`[Tela SDK - ${resource} Input Validation] Input schema mismatches for ${resource} "${canvasIdentifier}":`
|
|
1455
|
+
);
|
|
1456
|
+
for (const mismatch of mismatches) {
|
|
1457
|
+
console.warn(` - ${mismatch.path}: ${mismatch.issue}`);
|
|
1458
|
+
}
|
|
1459
|
+
} catch (error) {
|
|
1460
|
+
console.warn(
|
|
1461
|
+
`[Tela SDK - ${resource} Input Validation] Failed to validate input schema for ${resource} "${canvasIdentifier}":`,
|
|
1462
|
+
error
|
|
1463
|
+
);
|
|
1455
1464
|
}
|
|
1456
|
-
this._interval = interval;
|
|
1457
|
-
this._timeout = timeout2;
|
|
1458
|
-
this._abortSignal = abortSignal ?? new AbortController().signal;
|
|
1459
|
-
this._internalAbortController = new AbortController();
|
|
1460
|
-
}
|
|
1461
|
-
async startTimeout() {
|
|
1462
|
-
await timeout(this._timeout, this._internalAbortController.signal);
|
|
1463
|
-
throw new ConnectionTimeout({
|
|
1464
|
-
message: `Reached timeout of ${this._timeout}ms when polling`
|
|
1465
|
-
});
|
|
1466
1465
|
}
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
* @throws Any error thrown by the callback function.
|
|
1477
|
-
*
|
|
1478
|
-
* @example
|
|
1479
|
-
* ```typescript
|
|
1480
|
-
* const result = await poller.start(async (signal) => {
|
|
1481
|
-
* const response = await fetch('/api/status', { signal });
|
|
1482
|
-
* const data = await response.json();
|
|
1483
|
-
*
|
|
1484
|
-
* if (data.status === 'completed') {
|
|
1485
|
-
* return { done: true, value: data.result };
|
|
1486
|
-
* }
|
|
1487
|
-
* return { done: false };
|
|
1488
|
-
* });
|
|
1489
|
-
* ```
|
|
1490
|
-
*/
|
|
1491
|
-
start(callback) {
|
|
1492
|
-
const resultPromise = async () => {
|
|
1493
|
-
try {
|
|
1494
|
-
while (!this._abortSignal.aborted) {
|
|
1495
|
-
const result = await callback();
|
|
1496
|
-
if (result.done) {
|
|
1497
|
-
return result.value;
|
|
1498
|
-
}
|
|
1499
|
-
await timeout(this._interval, this._abortSignal);
|
|
1500
|
-
}
|
|
1501
|
-
throw new UserAbortError({ message: "Polling aborted" });
|
|
1502
|
-
} finally {
|
|
1503
|
-
this._internalAbortController.abort();
|
|
1466
|
+
if (output instanceof z$1.ZodType) {
|
|
1467
|
+
try {
|
|
1468
|
+
const outputSchema = z$1.toJSONSchema(output);
|
|
1469
|
+
const serverOutput = promptVersion.configuration.structuredOutput;
|
|
1470
|
+
if (!serverOutput.enabled) {
|
|
1471
|
+
console.warn(
|
|
1472
|
+
`[Tela SDK - ${resource} Output Validation] Output schema provided for ${resource} "${canvasIdentifier}", but structured output is not enabled on the server. The schema may not be enforced.`
|
|
1473
|
+
);
|
|
1474
|
+
return;
|
|
1504
1475
|
}
|
|
1505
|
-
|
|
1506
|
-
|
|
1476
|
+
const mismatches = validateOutputSchema(outputSchema, serverOutput.schema);
|
|
1477
|
+
if (mismatches.length === 0) {
|
|
1478
|
+
return;
|
|
1479
|
+
}
|
|
1480
|
+
console.warn(
|
|
1481
|
+
`[Tela SDK - ${resource} Output Validation] Output schema mismatches for ${resource} "${canvasIdentifier}":`
|
|
1482
|
+
);
|
|
1483
|
+
for (const mismatch of mismatches) {
|
|
1484
|
+
console.warn(` - ${mismatch.path}: ${mismatch.issue}`);
|
|
1485
|
+
}
|
|
1486
|
+
} catch (error) {
|
|
1487
|
+
console.warn(
|
|
1488
|
+
`[Tela SDK - ${resource} Output Validation] Failed to validate output schema for ${resource} "${canvasIdentifier}":`,
|
|
1489
|
+
error
|
|
1490
|
+
);
|
|
1491
|
+
}
|
|
1507
1492
|
}
|
|
1508
1493
|
}
|
|
1509
1494
|
|
|
@@ -1595,6 +1580,10 @@ async function uploadFile(file, client) {
|
|
|
1595
1580
|
}
|
|
1596
1581
|
return { fileUrl: `vault://${id}`, options: file.options };
|
|
1597
1582
|
}
|
|
1583
|
+
async function uploadFiles(files, client) {
|
|
1584
|
+
const uploadPromises = files.map((file) => uploadFile(file, client));
|
|
1585
|
+
return Promise.all(uploadPromises);
|
|
1586
|
+
}
|
|
1598
1587
|
async function downloadFile(vaultReference, client) {
|
|
1599
1588
|
const vaultId = vaultReference.replace("vault://", "");
|
|
1600
1589
|
const response = await client.get(`/_services/vault/files/${vaultId}`);
|
|
@@ -1614,18 +1603,95 @@ async function streamFile(vaultReference, client) {
|
|
|
1614
1603
|
});
|
|
1615
1604
|
}
|
|
1616
1605
|
|
|
1617
|
-
var __defProp$
|
|
1618
|
-
var __defNormalProp$
|
|
1619
|
-
var __publicField$
|
|
1620
|
-
__defNormalProp$
|
|
1606
|
+
var __defProp$6 = Object.defineProperty;
|
|
1607
|
+
var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1608
|
+
var __publicField$6 = (obj, key, value) => {
|
|
1609
|
+
__defNormalProp$6(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
1621
1610
|
return value;
|
|
1622
1611
|
};
|
|
1623
|
-
function
|
|
1624
|
-
return
|
|
1612
|
+
function timeout(ms, signal) {
|
|
1613
|
+
return new Promise((resolve, reject) => {
|
|
1614
|
+
const timer = setTimeout(resolve, ms);
|
|
1615
|
+
if (signal) {
|
|
1616
|
+
signal.addEventListener("abort", () => {
|
|
1617
|
+
clearTimeout(timer);
|
|
1618
|
+
reject(signal.reason);
|
|
1619
|
+
}, { once: true });
|
|
1620
|
+
}
|
|
1621
|
+
});
|
|
1625
1622
|
}
|
|
1626
|
-
|
|
1627
|
-
|
|
1623
|
+
class Poller {
|
|
1624
|
+
constructor({ interval, timeout: timeout2, abortSignal }) {
|
|
1625
|
+
__publicField$6(this, "_interval");
|
|
1626
|
+
__publicField$6(this, "_timeout");
|
|
1627
|
+
__publicField$6(this, "_abortSignal");
|
|
1628
|
+
__publicField$6(this, "_internalAbortController");
|
|
1629
|
+
if (interval <= 0) {
|
|
1630
|
+
throw new TelaError("Interval must be greater than 0");
|
|
1631
|
+
}
|
|
1632
|
+
if (timeout2 <= 0) {
|
|
1633
|
+
throw new TelaError("Timeout must be greater than 0");
|
|
1634
|
+
}
|
|
1635
|
+
this._interval = interval;
|
|
1636
|
+
this._timeout = timeout2;
|
|
1637
|
+
this._abortSignal = abortSignal ?? new AbortController().signal;
|
|
1638
|
+
this._internalAbortController = new AbortController();
|
|
1639
|
+
}
|
|
1640
|
+
async startTimeout() {
|
|
1641
|
+
await timeout(this._timeout, this._internalAbortController.signal);
|
|
1642
|
+
throw new ConnectionTimeout({
|
|
1643
|
+
message: `Reached timeout of ${this._timeout}ms when polling`
|
|
1644
|
+
});
|
|
1645
|
+
}
|
|
1646
|
+
/**
|
|
1647
|
+
* Starts the polling operation, repeatedly calling the callback until completion.
|
|
1648
|
+
* The callback is invoked at the configured interval and must return a PollerResult
|
|
1649
|
+
* indicating whether polling should continue or if the final value is ready.
|
|
1650
|
+
*
|
|
1651
|
+
* @param callback - Function called on each polling iteration.
|
|
1652
|
+
* @returns A promise resolving to the final result value.
|
|
1653
|
+
* @throws {Error} If the polling operation times out.
|
|
1654
|
+
* @throws {Error} If the operation is aborted via the AbortSignal.
|
|
1655
|
+
* @throws Any error thrown by the callback function.
|
|
1656
|
+
*
|
|
1657
|
+
* @example
|
|
1658
|
+
* ```typescript
|
|
1659
|
+
* const result = await poller.start(async (signal) => {
|
|
1660
|
+
* const response = await fetch('/api/status', { signal });
|
|
1661
|
+
* const data = await response.json();
|
|
1662
|
+
*
|
|
1663
|
+
* if (data.status === 'completed') {
|
|
1664
|
+
* return { done: true, value: data.result };
|
|
1665
|
+
* }
|
|
1666
|
+
* return { done: false };
|
|
1667
|
+
* });
|
|
1668
|
+
* ```
|
|
1669
|
+
*/
|
|
1670
|
+
start(callback) {
|
|
1671
|
+
const resultPromise = async () => {
|
|
1672
|
+
try {
|
|
1673
|
+
while (!this._abortSignal.aborted) {
|
|
1674
|
+
const result = await callback();
|
|
1675
|
+
if (result.done) {
|
|
1676
|
+
return result.value;
|
|
1677
|
+
}
|
|
1678
|
+
await timeout(this._interval, this._abortSignal);
|
|
1679
|
+
}
|
|
1680
|
+
throw new UserAbortError({ message: "Polling aborted" });
|
|
1681
|
+
} finally {
|
|
1682
|
+
this._internalAbortController.abort();
|
|
1683
|
+
}
|
|
1684
|
+
};
|
|
1685
|
+
return Promise.race([this.startTimeout(), resultPromise()]);
|
|
1686
|
+
}
|
|
1628
1687
|
}
|
|
1688
|
+
|
|
1689
|
+
var __defProp$5 = Object.defineProperty;
|
|
1690
|
+
var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1691
|
+
var __publicField$5 = (obj, key, value) => {
|
|
1692
|
+
__defNormalProp$5(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
1693
|
+
return value;
|
|
1694
|
+
};
|
|
1629
1695
|
function isUUID(str) {
|
|
1630
1696
|
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
1631
1697
|
return uuidRegex.test(str);
|
|
@@ -1657,6 +1723,17 @@ function getResultFromPollingResponse(response) {
|
|
|
1657
1723
|
}
|
|
1658
1724
|
throw new Error("Invalid response type");
|
|
1659
1725
|
}
|
|
1726
|
+
function normalizeExecutionStatus(status) {
|
|
1727
|
+
switch (status) {
|
|
1728
|
+
case "completed":
|
|
1729
|
+
case "validating":
|
|
1730
|
+
return "succeeded";
|
|
1731
|
+
case "cancelled":
|
|
1732
|
+
return "failed";
|
|
1733
|
+
default:
|
|
1734
|
+
return status;
|
|
1735
|
+
}
|
|
1736
|
+
}
|
|
1660
1737
|
class CanvasExecution extends Emittery {
|
|
1661
1738
|
/**
|
|
1662
1739
|
* Creates a new canvas execution instance.
|
|
@@ -1668,20 +1745,20 @@ class CanvasExecution extends Emittery {
|
|
|
1668
1745
|
*/
|
|
1669
1746
|
constructor(variables, params = { async: false }, outputSchema, client, isTask = false) {
|
|
1670
1747
|
super();
|
|
1671
|
-
__publicField$
|
|
1672
|
-
__publicField$
|
|
1673
|
-
__publicField$
|
|
1674
|
-
__publicField$
|
|
1675
|
-
__publicField$
|
|
1676
|
-
__publicField$
|
|
1677
|
-
__publicField$
|
|
1678
|
-
__publicField$
|
|
1679
|
-
__publicField$
|
|
1680
|
-
__publicField$
|
|
1681
|
-
__publicField$
|
|
1682
|
-
__publicField$
|
|
1683
|
-
__publicField$
|
|
1684
|
-
__publicField$
|
|
1748
|
+
__publicField$5(this, "_id");
|
|
1749
|
+
__publicField$5(this, "_status");
|
|
1750
|
+
__publicField$5(this, "_variables");
|
|
1751
|
+
__publicField$5(this, "_params");
|
|
1752
|
+
__publicField$5(this, "_client");
|
|
1753
|
+
__publicField$5(this, "_outputSchema");
|
|
1754
|
+
__publicField$5(this, "_skipResultValidation");
|
|
1755
|
+
__publicField$5(this, "_abortController");
|
|
1756
|
+
__publicField$5(this, "_isTask");
|
|
1757
|
+
__publicField$5(this, "_resultPromise");
|
|
1758
|
+
__publicField$5(this, "_stream");
|
|
1759
|
+
__publicField$5(this, "_rawResultValue");
|
|
1760
|
+
__publicField$5(this, "_requestId");
|
|
1761
|
+
__publicField$5(this, "_task");
|
|
1685
1762
|
this._variables = variables;
|
|
1686
1763
|
this._params = params;
|
|
1687
1764
|
this._outputSchema = outputSchema;
|
|
@@ -1746,7 +1823,7 @@ class CanvasExecution extends Emittery {
|
|
|
1746
1823
|
execution._rawResultValue = response;
|
|
1747
1824
|
const content = getResultFromPollingResponse(response);
|
|
1748
1825
|
try {
|
|
1749
|
-
const validatedContent = execution._skipResultValidation || !(outputSchema instanceof
|
|
1826
|
+
const validatedContent = execution._skipResultValidation || !(outputSchema instanceof z.ZodType) ? content : outputSchema.parse(content);
|
|
1750
1827
|
execution._resultPromise = Promise.resolve(validatedContent);
|
|
1751
1828
|
} catch (error) {
|
|
1752
1829
|
execution._resultPromise = Promise.reject(error);
|
|
@@ -2091,12 +2168,12 @@ class CanvasExecution extends Emittery {
|
|
|
2091
2168
|
label: this._params.label,
|
|
2092
2169
|
webhook_url: this._params.webhookUrl
|
|
2093
2170
|
};
|
|
2094
|
-
if (this._params.override && this._outputSchema instanceof
|
|
2171
|
+
if (this._params.override && this._outputSchema instanceof z.ZodType) {
|
|
2095
2172
|
return {
|
|
2096
2173
|
...body,
|
|
2097
2174
|
override: {
|
|
2098
2175
|
...this._params.override,
|
|
2099
|
-
structured_output:
|
|
2176
|
+
structured_output: z.toJSONSchema(this._outputSchema)
|
|
2100
2177
|
}
|
|
2101
2178
|
};
|
|
2102
2179
|
}
|
|
@@ -2163,7 +2240,7 @@ class CanvasExecution extends Emittery {
|
|
|
2163
2240
|
this._rawResultValue = response;
|
|
2164
2241
|
return { content: getResultFromPollingResponse(response), response };
|
|
2165
2242
|
}).then(({ content, response }) => {
|
|
2166
|
-
const validatedContent = this._skipResultValidation || !(this._outputSchema instanceof
|
|
2243
|
+
const validatedContent = this._skipResultValidation || !(this._outputSchema instanceof z.ZodType) ? content : this._outputSchema.parse(content);
|
|
2167
2244
|
this.status = "succeeded";
|
|
2168
2245
|
this.emit("success", { ...validatedContent, requestId: response?.requestId });
|
|
2169
2246
|
return validatedContent;
|
|
@@ -2187,13 +2264,13 @@ class CanvasExecution extends Emittery {
|
|
|
2187
2264
|
const resolvedVariables = await this.resolveVariables();
|
|
2188
2265
|
return await this.create(resolvedVariables, { async: true }).then((response) => {
|
|
2189
2266
|
this._id = "completionRunId" in response ? response.completionRunId : response.id;
|
|
2190
|
-
this.status = response.status;
|
|
2267
|
+
this.status = normalizeExecutionStatus(response.status);
|
|
2191
2268
|
if (this._isTask) {
|
|
2192
2269
|
this._task = response;
|
|
2193
2270
|
}
|
|
2194
2271
|
this.emit("poll", {
|
|
2195
2272
|
id: response.id,
|
|
2196
|
-
status: response.status,
|
|
2273
|
+
status: normalizeExecutionStatus(response.status),
|
|
2197
2274
|
outputContent: response.outputContent,
|
|
2198
2275
|
rawOutput: "rawOutput" in response ? response.rawOutput : void 0,
|
|
2199
2276
|
requestId: response.requestId
|
|
@@ -2238,15 +2315,15 @@ class CanvasExecution extends Emittery {
|
|
|
2238
2315
|
signal: this._abortController.signal
|
|
2239
2316
|
}
|
|
2240
2317
|
);
|
|
2241
|
-
this.status = response.status;
|
|
2318
|
+
this.status = normalizeExecutionStatus(response.status);
|
|
2242
2319
|
this.emit("poll", response);
|
|
2243
|
-
if (
|
|
2320
|
+
if (this.status === "failed") {
|
|
2244
2321
|
const error = new ExecutionFailedError(response.rawOutput);
|
|
2245
2322
|
this.cancel();
|
|
2246
2323
|
this.emit("error", error);
|
|
2247
2324
|
throw error;
|
|
2248
2325
|
}
|
|
2249
|
-
if (
|
|
2326
|
+
if (this.status !== "succeeded") {
|
|
2250
2327
|
return {
|
|
2251
2328
|
done: false,
|
|
2252
2329
|
value: void 0
|
|
@@ -2255,16 +2332,16 @@ class CanvasExecution extends Emittery {
|
|
|
2255
2332
|
this._rawResultValue = response;
|
|
2256
2333
|
this.emit("success", { ...getResultFromPollingResponse(response), requestId: response?.requestId });
|
|
2257
2334
|
return {
|
|
2258
|
-
done:
|
|
2335
|
+
done: this.status === "succeeded",
|
|
2259
2336
|
value: getResultFromPollingResponse(response)
|
|
2260
2337
|
};
|
|
2261
2338
|
}).then((value) => {
|
|
2262
|
-
if (this._skipResultValidation || !(this._outputSchema instanceof
|
|
2339
|
+
if (this._skipResultValidation || !(this._outputSchema instanceof z.ZodType)) {
|
|
2263
2340
|
return value;
|
|
2264
2341
|
}
|
|
2265
2342
|
return this._outputSchema.parse(value);
|
|
2266
2343
|
}).catch((error) => {
|
|
2267
|
-
if (this.
|
|
2344
|
+
if (this.status !== "failed") {
|
|
2268
2345
|
this.status = "failed";
|
|
2269
2346
|
}
|
|
2270
2347
|
if (this.listenerCount("error") > 0) {
|
|
@@ -2310,94 +2387,94 @@ class CanvasExecution extends Emittery {
|
|
|
2310
2387
|
}
|
|
2311
2388
|
}
|
|
2312
2389
|
|
|
2313
|
-
var __defProp$
|
|
2314
|
-
var __defNormalProp$
|
|
2315
|
-
var __publicField$
|
|
2316
|
-
__defNormalProp$
|
|
2390
|
+
var __defProp$4 = Object.defineProperty;
|
|
2391
|
+
var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
2392
|
+
var __publicField$4 = (obj, key, value) => {
|
|
2393
|
+
__defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
2317
2394
|
return value;
|
|
2318
2395
|
};
|
|
2319
|
-
const BatchResultItem =
|
|
2320
|
-
reference_id:
|
|
2321
|
-
status:
|
|
2322
|
-
execution_id:
|
|
2323
|
-
result:
|
|
2324
|
-
id:
|
|
2325
|
-
tags:
|
|
2326
|
-
status:
|
|
2327
|
-
metadata:
|
|
2328
|
-
promptVersion:
|
|
2329
|
-
modelConfigurations:
|
|
2330
|
-
type:
|
|
2331
|
-
model:
|
|
2332
|
-
temperature:
|
|
2333
|
-
structuredOutput:
|
|
2334
|
-
schema:
|
|
2335
|
-
enabled:
|
|
2396
|
+
const BatchResultItem = z.looseObject({
|
|
2397
|
+
reference_id: z.string(),
|
|
2398
|
+
status: z.string(),
|
|
2399
|
+
execution_id: z.string(),
|
|
2400
|
+
result: z.object({
|
|
2401
|
+
id: z.string(),
|
|
2402
|
+
tags: z.array(z.string()),
|
|
2403
|
+
status: z.string(),
|
|
2404
|
+
metadata: z.object({
|
|
2405
|
+
promptVersion: z.object({
|
|
2406
|
+
modelConfigurations: z.object({
|
|
2407
|
+
type: z.string(),
|
|
2408
|
+
model: z.string(),
|
|
2409
|
+
temperature: z.number(),
|
|
2410
|
+
structuredOutput: z.object({
|
|
2411
|
+
schema: z.looseObject({}).optional(),
|
|
2412
|
+
enabled: z.boolean()
|
|
2336
2413
|
})
|
|
2337
2414
|
}),
|
|
2338
|
-
variablesDefinitions:
|
|
2339
|
-
|
|
2340
|
-
name:
|
|
2341
|
-
type:
|
|
2342
|
-
required:
|
|
2343
|
-
processingOptions:
|
|
2415
|
+
variablesDefinitions: z.array(
|
|
2416
|
+
z.object({
|
|
2417
|
+
name: z.string(),
|
|
2418
|
+
type: z.string(),
|
|
2419
|
+
required: z.boolean(),
|
|
2420
|
+
processingOptions: z.object({ allowMultimodal: z.boolean() })
|
|
2344
2421
|
})
|
|
2345
2422
|
)
|
|
2346
2423
|
})
|
|
2347
2424
|
}),
|
|
2348
|
-
promptId:
|
|
2349
|
-
rawInput:
|
|
2350
|
-
tags:
|
|
2351
|
-
async:
|
|
2352
|
-
canvas_id:
|
|
2353
|
-
variables:
|
|
2425
|
+
promptId: z.string(),
|
|
2426
|
+
rawInput: z.object({
|
|
2427
|
+
tags: z.array(z.string()),
|
|
2428
|
+
async: z.boolean(),
|
|
2429
|
+
canvas_id: z.string(),
|
|
2430
|
+
variables: z.looseObject({})
|
|
2354
2431
|
}),
|
|
2355
|
-
createdAt:
|
|
2356
|
-
deletedAt:
|
|
2357
|
-
rawOutput:
|
|
2358
|
-
id:
|
|
2359
|
-
usage:
|
|
2360
|
-
cost:
|
|
2361
|
-
total_cost:
|
|
2362
|
-
prompt_cost:
|
|
2363
|
-
completion_cost:
|
|
2432
|
+
createdAt: z.string(),
|
|
2433
|
+
deletedAt: z.null(),
|
|
2434
|
+
rawOutput: z.object({
|
|
2435
|
+
id: z.string(),
|
|
2436
|
+
usage: z.object({
|
|
2437
|
+
cost: z.object({
|
|
2438
|
+
total_cost: z.number(),
|
|
2439
|
+
prompt_cost: z.number(),
|
|
2440
|
+
completion_cost: z.number()
|
|
2364
2441
|
}),
|
|
2365
|
-
total_tokens:
|
|
2366
|
-
prompt_tokens:
|
|
2367
|
-
completion_tokens:
|
|
2442
|
+
total_tokens: z.number(),
|
|
2443
|
+
prompt_tokens: z.number(),
|
|
2444
|
+
completion_tokens: z.number()
|
|
2368
2445
|
}),
|
|
2369
|
-
object:
|
|
2370
|
-
choices:
|
|
2371
|
-
|
|
2372
|
-
message:
|
|
2373
|
-
role:
|
|
2374
|
-
content:
|
|
2375
|
-
tool_calls:
|
|
2376
|
-
function_call:
|
|
2446
|
+
object: z.string(),
|
|
2447
|
+
choices: z.array(
|
|
2448
|
+
z.object({
|
|
2449
|
+
message: z.object({
|
|
2450
|
+
role: z.string(),
|
|
2451
|
+
content: z.looseObject({}),
|
|
2452
|
+
tool_calls: z.array(z.object({ type: z.string(), function: z.object({ name: z.string(), arguments: z.looseObject({}) }) })),
|
|
2453
|
+
function_call: z.null()
|
|
2377
2454
|
})
|
|
2378
2455
|
})
|
|
2379
2456
|
),
|
|
2380
|
-
created:
|
|
2457
|
+
created: z.number()
|
|
2381
2458
|
}),
|
|
2382
|
-
updatedAt:
|
|
2383
|
-
creditsUsed:
|
|
2384
|
-
workspaceId:
|
|
2385
|
-
inputContent:
|
|
2386
|
-
files:
|
|
2387
|
-
messages:
|
|
2388
|
-
variables:
|
|
2459
|
+
updatedAt: z.string(),
|
|
2460
|
+
creditsUsed: z.number(),
|
|
2461
|
+
workspaceId: z.string(),
|
|
2462
|
+
inputContent: z.object({
|
|
2463
|
+
files: z.array(z.object({ file_url: z.string() })),
|
|
2464
|
+
messages: z.array(z.object({ role: z.string(), content: z.looseObject({}) })),
|
|
2465
|
+
variables: z.looseObject({})
|
|
2389
2466
|
}),
|
|
2390
|
-
outputContent:
|
|
2391
|
-
role:
|
|
2392
|
-
content:
|
|
2393
|
-
tool_calls:
|
|
2394
|
-
function_call:
|
|
2467
|
+
outputContent: z.object({
|
|
2468
|
+
role: z.string(),
|
|
2469
|
+
content: z.looseObject({}),
|
|
2470
|
+
tool_calls: z.array(z.looseObject({})),
|
|
2471
|
+
function_call: z.string().nullable()
|
|
2395
2472
|
}),
|
|
2396
|
-
promptVersionId:
|
|
2397
|
-
compatibilityDate:
|
|
2398
|
-
promptApplicationId:
|
|
2473
|
+
promptVersionId: z.string(),
|
|
2474
|
+
compatibilityDate: z.string(),
|
|
2475
|
+
promptApplicationId: z.string().nullable()
|
|
2399
2476
|
}),
|
|
2400
|
-
error:
|
|
2477
|
+
error: z.null()
|
|
2401
2478
|
});
|
|
2402
2479
|
class BatchExecutionResult {
|
|
2403
2480
|
/**
|
|
@@ -2408,8 +2485,8 @@ class BatchExecutionResult {
|
|
|
2408
2485
|
* @internal
|
|
2409
2486
|
*/
|
|
2410
2487
|
constructor(client, response) {
|
|
2411
|
-
__publicField$
|
|
2412
|
-
__publicField$
|
|
2488
|
+
__publicField$4(this, "_client");
|
|
2489
|
+
__publicField$4(this, "_response");
|
|
2413
2490
|
this._client = client;
|
|
2414
2491
|
this._response = response;
|
|
2415
2492
|
}
|
|
@@ -2695,13 +2772,13 @@ class BatchExecution extends Emittery {
|
|
|
2695
2772
|
*/
|
|
2696
2773
|
constructor(id, client, params = {}, creationResponse) {
|
|
2697
2774
|
super();
|
|
2698
|
-
__publicField$
|
|
2699
|
-
__publicField$
|
|
2700
|
-
__publicField$
|
|
2701
|
-
__publicField$
|
|
2702
|
-
__publicField$
|
|
2703
|
-
__publicField$
|
|
2704
|
-
__publicField$
|
|
2775
|
+
__publicField$4(this, "_client");
|
|
2776
|
+
__publicField$4(this, "_id");
|
|
2777
|
+
__publicField$4(this, "_params");
|
|
2778
|
+
__publicField$4(this, "_abortController");
|
|
2779
|
+
__publicField$4(this, "_creationResponse");
|
|
2780
|
+
__publicField$4(this, "_status", "created");
|
|
2781
|
+
__publicField$4(this, "_resultPromise");
|
|
2705
2782
|
this._id = id;
|
|
2706
2783
|
this._client = client;
|
|
2707
2784
|
this._params = params;
|
|
@@ -2914,10 +2991,10 @@ class Batch {
|
|
|
2914
2991
|
* @internal
|
|
2915
2992
|
*/
|
|
2916
2993
|
constructor(client, canvasIdentifier, params = {}) {
|
|
2917
|
-
__publicField$
|
|
2918
|
-
__publicField$
|
|
2919
|
-
__publicField$
|
|
2920
|
-
__publicField$
|
|
2994
|
+
__publicField$4(this, "_client");
|
|
2995
|
+
__publicField$4(this, "_canvasIdentifier");
|
|
2996
|
+
__publicField$4(this, "_items", []);
|
|
2997
|
+
__publicField$4(this, "_params");
|
|
2921
2998
|
this._client = client;
|
|
2922
2999
|
this._canvasIdentifier = canvasIdentifier;
|
|
2923
3000
|
this._params = params;
|
|
@@ -3053,16 +3130,12 @@ class Batch {
|
|
|
3053
3130
|
}
|
|
3054
3131
|
}
|
|
3055
3132
|
|
|
3056
|
-
var __defProp$
|
|
3057
|
-
var __defNormalProp$
|
|
3058
|
-
var __publicField$
|
|
3059
|
-
__defNormalProp$
|
|
3133
|
+
var __defProp$3 = Object.defineProperty;
|
|
3134
|
+
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3135
|
+
var __publicField$3 = (obj, key, value) => {
|
|
3136
|
+
__defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
3060
3137
|
return value;
|
|
3061
3138
|
};
|
|
3062
|
-
const zod = {
|
|
3063
|
-
...z,
|
|
3064
|
-
file: TelaFileSchema
|
|
3065
|
-
};
|
|
3066
3139
|
function fetchById(id, client) {
|
|
3067
3140
|
return client.get(`/prompt/${id}/promoted-version`);
|
|
3068
3141
|
}
|
|
@@ -3084,56 +3157,6 @@ function fetchByAny({ id, versionId, applicationId, client }) {
|
|
|
3084
3157
|
}
|
|
3085
3158
|
throw new Error("Either id, versionId, or applicationId must be provided");
|
|
3086
3159
|
}
|
|
3087
|
-
function validateSchemas(input, output, promptVersion) {
|
|
3088
|
-
const canvasIdentifier = `${promptVersion.title} (${promptVersion.promptId})`;
|
|
3089
|
-
if (input instanceof z$1.ZodType) {
|
|
3090
|
-
try {
|
|
3091
|
-
const inputSchema = z$1.toJSONSchema(input, { unrepresentable: "any" });
|
|
3092
|
-
const mismatches = validateInputSchema(inputSchema, promptVersion.variables);
|
|
3093
|
-
if (mismatches.length === 0) {
|
|
3094
|
-
return;
|
|
3095
|
-
}
|
|
3096
|
-
console.warn(
|
|
3097
|
-
`[Tela SDK - Canvas Input Validation] Input schema mismatches for canvas "${canvasIdentifier}":`
|
|
3098
|
-
);
|
|
3099
|
-
for (const mismatch of mismatches) {
|
|
3100
|
-
console.warn(` - ${mismatch.path}: ${mismatch.issue}`);
|
|
3101
|
-
}
|
|
3102
|
-
} catch (error) {
|
|
3103
|
-
console.warn(
|
|
3104
|
-
`[Tela SDK - Canvas Input Validation] Failed to validate input schema for canvas "${canvasIdentifier}":`,
|
|
3105
|
-
error
|
|
3106
|
-
);
|
|
3107
|
-
}
|
|
3108
|
-
}
|
|
3109
|
-
if (output instanceof z$1.ZodType) {
|
|
3110
|
-
try {
|
|
3111
|
-
const outputSchema = z$1.toJSONSchema(output);
|
|
3112
|
-
const serverOutput = promptVersion.configuration.structuredOutput;
|
|
3113
|
-
if (!serverOutput.enabled) {
|
|
3114
|
-
console.warn(
|
|
3115
|
-
`[Tela SDK - Canvas Output Validation] Output schema provided for canvas "${canvasIdentifier}", but structured output is not enabled on the server. The schema may not be enforced.`
|
|
3116
|
-
);
|
|
3117
|
-
return;
|
|
3118
|
-
}
|
|
3119
|
-
const mismatches = validateOutputSchema(outputSchema, serverOutput.schema);
|
|
3120
|
-
if (mismatches.length === 0) {
|
|
3121
|
-
return;
|
|
3122
|
-
}
|
|
3123
|
-
console.warn(
|
|
3124
|
-
`[Tela SDK - Canvas Output Validation] Output schema mismatches for canvas "${canvasIdentifier}":`
|
|
3125
|
-
);
|
|
3126
|
-
for (const mismatch of mismatches) {
|
|
3127
|
-
console.warn(` - ${mismatch.path}: ${mismatch.issue}`);
|
|
3128
|
-
}
|
|
3129
|
-
} catch (error) {
|
|
3130
|
-
console.warn(
|
|
3131
|
-
`[Tela SDK - Canvas Output Validation] Failed to validate output schema for canvas "${canvasIdentifier}":`,
|
|
3132
|
-
error
|
|
3133
|
-
);
|
|
3134
|
-
}
|
|
3135
|
-
}
|
|
3136
|
-
}
|
|
3137
3160
|
class Canvas {
|
|
3138
3161
|
/**
|
|
3139
3162
|
* Creates a new instance of the Canvas class. Usage of this constructor is not recommended.
|
|
@@ -3142,15 +3165,15 @@ class Canvas {
|
|
|
3142
3165
|
* @private
|
|
3143
3166
|
*/
|
|
3144
3167
|
constructor({ id, applicationId, name, versionId, input, output, client, variables, isWorkflow }) {
|
|
3145
|
-
__publicField$
|
|
3146
|
-
__publicField$
|
|
3147
|
-
__publicField$
|
|
3148
|
-
__publicField$
|
|
3149
|
-
__publicField$
|
|
3150
|
-
__publicField$
|
|
3151
|
-
__publicField$
|
|
3152
|
-
__publicField$
|
|
3153
|
-
__publicField$
|
|
3168
|
+
__publicField$3(this, "_id");
|
|
3169
|
+
__publicField$3(this, "_versionId");
|
|
3170
|
+
__publicField$3(this, "_applicationId");
|
|
3171
|
+
__publicField$3(this, "_name");
|
|
3172
|
+
__publicField$3(this, "_input");
|
|
3173
|
+
__publicField$3(this, "_output");
|
|
3174
|
+
__publicField$3(this, "_client");
|
|
3175
|
+
__publicField$3(this, "_variables");
|
|
3176
|
+
__publicField$3(this, "_isWorkflow");
|
|
3154
3177
|
this._id = id;
|
|
3155
3178
|
this._applicationId = applicationId;
|
|
3156
3179
|
this._name = name;
|
|
@@ -3179,7 +3202,7 @@ class Canvas {
|
|
|
3179
3202
|
const inputSchema = input && input(zod);
|
|
3180
3203
|
const outputSchema = output && output(zod);
|
|
3181
3204
|
if (!skipSchemaValidation) {
|
|
3182
|
-
validateSchemas(inputSchema, outputSchema, promptVersion);
|
|
3205
|
+
validateSchemas("Canvas", inputSchema, outputSchema, promptVersion);
|
|
3183
3206
|
}
|
|
3184
3207
|
return new Canvas({
|
|
3185
3208
|
id: promptVersion.promptId,
|
|
@@ -3369,6 +3392,663 @@ class Canvas {
|
|
|
3369
3392
|
}
|
|
3370
3393
|
}
|
|
3371
3394
|
|
|
3395
|
+
const TaskStatus = z$1.enum(["created", "failed", "running", "validating", "completed", "cancelled"]);
|
|
3396
|
+
const OutputContent = z$1.object({
|
|
3397
|
+
role: z$1.literal("assistant"),
|
|
3398
|
+
content: z$1.unknown(),
|
|
3399
|
+
toolCalls: z$1.array(z$1.any()),
|
|
3400
|
+
functionCall: z$1.any()
|
|
3401
|
+
});
|
|
3402
|
+
const RawInput = z$1.object({
|
|
3403
|
+
async: z$1.boolean(),
|
|
3404
|
+
stream: z$1.boolean(),
|
|
3405
|
+
variables: z$1.record(z$1.string(), z$1.unknown()),
|
|
3406
|
+
applicationId: z$1.string()
|
|
3407
|
+
});
|
|
3408
|
+
z$1.object({
|
|
3409
|
+
id: z$1.string(),
|
|
3410
|
+
reference: z$1.number(),
|
|
3411
|
+
name: z$1.string(),
|
|
3412
|
+
status: TaskStatus,
|
|
3413
|
+
rawInput: RawInput,
|
|
3414
|
+
inputContent: z$1.any(),
|
|
3415
|
+
outputContent: OutputContent,
|
|
3416
|
+
originalOutputContent: OutputContent,
|
|
3417
|
+
createdBy: z$1.string(),
|
|
3418
|
+
approvedBy: z$1.any(),
|
|
3419
|
+
approvedAt: z$1.any(),
|
|
3420
|
+
completionRunId: z$1.string(),
|
|
3421
|
+
workflowRunId: z$1.any(),
|
|
3422
|
+
promptVersionId: z$1.string(),
|
|
3423
|
+
promptApplicationId: z$1.string(),
|
|
3424
|
+
workspaceId: z$1.string(),
|
|
3425
|
+
metadata: z$1.any(),
|
|
3426
|
+
tags: z$1.array(z$1.string()),
|
|
3427
|
+
createdAt: z$1.string(),
|
|
3428
|
+
updatedAt: z$1.string(),
|
|
3429
|
+
deletedAt: z$1.any(),
|
|
3430
|
+
requestId: z$1.string()
|
|
3431
|
+
});
|
|
3432
|
+
|
|
3433
|
+
var __defProp$2 = Object.defineProperty;
|
|
3434
|
+
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3435
|
+
var __publicField$2 = (obj, key, value) => {
|
|
3436
|
+
__defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
3437
|
+
return value;
|
|
3438
|
+
};
|
|
3439
|
+
class Task extends Emittery {
|
|
3440
|
+
constructor(client, variables, task, requestId, outputSchema, skipResultValidation = false) {
|
|
3441
|
+
super();
|
|
3442
|
+
__publicField$2(this, "_variables");
|
|
3443
|
+
__publicField$2(this, "_task");
|
|
3444
|
+
__publicField$2(this, "_abortController", new AbortController());
|
|
3445
|
+
__publicField$2(this, "_client");
|
|
3446
|
+
__publicField$2(this, "_outputSchema");
|
|
3447
|
+
__publicField$2(this, "_skipResultValidation");
|
|
3448
|
+
__publicField$2(this, "_resultPromise");
|
|
3449
|
+
__publicField$2(this, "_status");
|
|
3450
|
+
__publicField$2(this, "_requestId");
|
|
3451
|
+
this._variables = variables;
|
|
3452
|
+
this._task = task;
|
|
3453
|
+
this._client = client;
|
|
3454
|
+
this._outputSchema = outputSchema;
|
|
3455
|
+
this._skipResultValidation = skipResultValidation;
|
|
3456
|
+
this._status = task.status;
|
|
3457
|
+
this._requestId = requestId;
|
|
3458
|
+
}
|
|
3459
|
+
/**
|
|
3460
|
+
* Gets the unique task ID assigned by the server.
|
|
3461
|
+
*
|
|
3462
|
+
* @returns The task ID.
|
|
3463
|
+
*/
|
|
3464
|
+
get id() {
|
|
3465
|
+
if (!this._task.id) {
|
|
3466
|
+
throw new Error("Task ID is not available");
|
|
3467
|
+
}
|
|
3468
|
+
return this._task.id;
|
|
3469
|
+
}
|
|
3470
|
+
/**
|
|
3471
|
+
* Gets the request ID from the `x-request-id` header of the task creation request.
|
|
3472
|
+
*
|
|
3473
|
+
* This is the request ID from the initial POST /task request that created the task.
|
|
3474
|
+
* Each API request has its own unique request ID.
|
|
3475
|
+
*
|
|
3476
|
+
* For polling operations, different request IDs are available:
|
|
3477
|
+
* - `task.requestId` - ID from the task creation request
|
|
3478
|
+
* - `pollResult.requestId` - ID from each polling request (in poll events)
|
|
3479
|
+
* - `result.requestId` - ID from the final successful polling request (in success events)
|
|
3480
|
+
*
|
|
3481
|
+
* @returns A promise that resolves to the request ID from the task creation request.
|
|
3482
|
+
*
|
|
3483
|
+
* @example
|
|
3484
|
+
* ```typescript
|
|
3485
|
+
* const task = await workstation.createTask({ query: 'test' })
|
|
3486
|
+
* const requestId = await task.requestId
|
|
3487
|
+
* console.log('Request ID:', requestId)
|
|
3488
|
+
* ```
|
|
3489
|
+
*/
|
|
3490
|
+
get requestId() {
|
|
3491
|
+
return this._requestId;
|
|
3492
|
+
}
|
|
3493
|
+
/**
|
|
3494
|
+
* Gets the latest known status of the task.
|
|
3495
|
+
*
|
|
3496
|
+
* Status values and transitions:
|
|
3497
|
+
* - `created` → `running` → `completed` or `failed`
|
|
3498
|
+
*
|
|
3499
|
+
* **Important:** Status is set to `completed` only after successful validation.
|
|
3500
|
+
* If validation fails, status will be `failed` even if the API request succeeded.
|
|
3501
|
+
*
|
|
3502
|
+
* Use the `statusChange` event to track status transitions in real-time.
|
|
3503
|
+
*
|
|
3504
|
+
* @returns The current status of the task.
|
|
3505
|
+
*
|
|
3506
|
+
* @example
|
|
3507
|
+
* ```typescript
|
|
3508
|
+
* const task = await workstation.createTask({ query: 'test' })
|
|
3509
|
+
* console.log(task.status) // 'created'
|
|
3510
|
+
*
|
|
3511
|
+
* await task.result
|
|
3512
|
+
* console.log(task.status) // 'completed' or 'failed'
|
|
3513
|
+
* ```
|
|
3514
|
+
*/
|
|
3515
|
+
get status() {
|
|
3516
|
+
return this._status;
|
|
3517
|
+
}
|
|
3518
|
+
/**
|
|
3519
|
+
* Sets the status of the task and emits statusChange event.
|
|
3520
|
+
*
|
|
3521
|
+
* @param status - The new status of the task.
|
|
3522
|
+
* @private
|
|
3523
|
+
*/
|
|
3524
|
+
set status(status) {
|
|
3525
|
+
const changed = this._status !== status;
|
|
3526
|
+
this._status = status;
|
|
3527
|
+
if (changed) {
|
|
3528
|
+
this.emit("statusChange", status);
|
|
3529
|
+
}
|
|
3530
|
+
}
|
|
3531
|
+
/**
|
|
3532
|
+
* Gets the input variables provided to this task.
|
|
3533
|
+
*
|
|
3534
|
+
* @returns The variables object.
|
|
3535
|
+
*/
|
|
3536
|
+
get variables() {
|
|
3537
|
+
return this._variables;
|
|
3538
|
+
}
|
|
3539
|
+
/**
|
|
3540
|
+
* Gets the task label (alias for name).
|
|
3541
|
+
*
|
|
3542
|
+
* @returns The task name.
|
|
3543
|
+
*/
|
|
3544
|
+
get label() {
|
|
3545
|
+
return this.name;
|
|
3546
|
+
}
|
|
3547
|
+
/**
|
|
3548
|
+
* Gets the task name.
|
|
3549
|
+
*
|
|
3550
|
+
* @returns The task name.
|
|
3551
|
+
*/
|
|
3552
|
+
get name() {
|
|
3553
|
+
return this._task.name;
|
|
3554
|
+
}
|
|
3555
|
+
/**
|
|
3556
|
+
* Gets the task tags.
|
|
3557
|
+
*
|
|
3558
|
+
* @returns The task tags.
|
|
3559
|
+
*/
|
|
3560
|
+
get tags() {
|
|
3561
|
+
return this._task.tags;
|
|
3562
|
+
}
|
|
3563
|
+
/**
|
|
3564
|
+
* Gets the raw task definition from the server.
|
|
3565
|
+
*
|
|
3566
|
+
* @returns The raw task definition.
|
|
3567
|
+
*/
|
|
3568
|
+
get rawTask() {
|
|
3569
|
+
return this._task;
|
|
3570
|
+
}
|
|
3571
|
+
/**
|
|
3572
|
+
* Starts polling for the task result without waiting for the promise to resolve.
|
|
3573
|
+
* This allows users to track task progress via events rather than awaiting a promise.
|
|
3574
|
+
*
|
|
3575
|
+
* The following events will be emitted during polling:
|
|
3576
|
+
* - `statusChange`: Emitted when the task status changes (e.g., 'created' → 'running' → 'completed')
|
|
3577
|
+
* - `poll`: Emitted on each polling attempt with the server response
|
|
3578
|
+
* - `success`: Emitted when the task completes successfully with the final result
|
|
3579
|
+
* - `error`: Emitted if the task fails
|
|
3580
|
+
*
|
|
3581
|
+
* **Important:** Events are only emitted while polling is active. You must call `poll()` or
|
|
3582
|
+
* await `task.result` to start polling. Simply setting up event listeners without starting
|
|
3583
|
+
* polling will not trigger any events.
|
|
3584
|
+
*
|
|
3585
|
+
* @example
|
|
3586
|
+
* ```typescript
|
|
3587
|
+
* const task = await workstation.createTask({ query: 'test' })
|
|
3588
|
+
*
|
|
3589
|
+
* // Set up event listeners
|
|
3590
|
+
* task.on('statusChange', (status) => {
|
|
3591
|
+
* console.log('Status:', status)
|
|
3592
|
+
* })
|
|
3593
|
+
*
|
|
3594
|
+
* task.on('success', (result) => {
|
|
3595
|
+
* console.log('Completed:', result)
|
|
3596
|
+
* })
|
|
3597
|
+
*
|
|
3598
|
+
* task.on('error', (error) => {
|
|
3599
|
+
* console.error('Failed:', error)
|
|
3600
|
+
* })
|
|
3601
|
+
*
|
|
3602
|
+
* // Start polling without waiting
|
|
3603
|
+
* task.poll()
|
|
3604
|
+
* ```
|
|
3605
|
+
*/
|
|
3606
|
+
poll() {
|
|
3607
|
+
if (this._resultPromise) {
|
|
3608
|
+
return;
|
|
3609
|
+
}
|
|
3610
|
+
this._resultPromise = this.startPolling();
|
|
3611
|
+
this._resultPromise.catch(() => {
|
|
3612
|
+
});
|
|
3613
|
+
}
|
|
3614
|
+
/**
|
|
3615
|
+
* Gets the task result. Automatically starts polling if not already started.
|
|
3616
|
+
*
|
|
3617
|
+
* @returns A promise that resolves to the validated task output.
|
|
3618
|
+
*
|
|
3619
|
+
* @example
|
|
3620
|
+
* ```typescript
|
|
3621
|
+
* const task = await workstation.createTask({ query: 'test' })
|
|
3622
|
+
* const result = await task.result
|
|
3623
|
+
* console.log(result)
|
|
3624
|
+
* ```
|
|
3625
|
+
*/
|
|
3626
|
+
get result() {
|
|
3627
|
+
if (this._resultPromise) {
|
|
3628
|
+
return this._resultPromise;
|
|
3629
|
+
}
|
|
3630
|
+
this._resultPromise = this.startPolling();
|
|
3631
|
+
return this._resultPromise;
|
|
3632
|
+
}
|
|
3633
|
+
/**
|
|
3634
|
+
* Starts the polling process to retrieve task results.
|
|
3635
|
+
* Emits events during the polling lifecycle and validates output before resolving.
|
|
3636
|
+
*
|
|
3637
|
+
* @returns A promise that resolves to the validated task output.
|
|
3638
|
+
* @private
|
|
3639
|
+
*/
|
|
3640
|
+
async startPolling() {
|
|
3641
|
+
const result = await new Poller({
|
|
3642
|
+
interval: 1e3,
|
|
3643
|
+
timeout: 6e4,
|
|
3644
|
+
abortSignal: this._abortController.signal
|
|
3645
|
+
}).start(async () => {
|
|
3646
|
+
const response = await this._client.get(`/task/${this.id}`, {
|
|
3647
|
+
signal: this._abortController.signal
|
|
3648
|
+
});
|
|
3649
|
+
const newStatus = response.status;
|
|
3650
|
+
this.status = newStatus;
|
|
3651
|
+
this.emit("poll", response);
|
|
3652
|
+
if (response.status === "failed") {
|
|
3653
|
+
const error = new TaskFailedError(response, "Task failure reported by the server");
|
|
3654
|
+
this.status = "failed";
|
|
3655
|
+
this.emit("error", error);
|
|
3656
|
+
if (this.listenerCount("error") === 0) {
|
|
3657
|
+
throw error;
|
|
3658
|
+
}
|
|
3659
|
+
return {
|
|
3660
|
+
done: true,
|
|
3661
|
+
value: void 0
|
|
3662
|
+
};
|
|
3663
|
+
}
|
|
3664
|
+
if (!["completed", "validating"].includes(response.status)) {
|
|
3665
|
+
return {
|
|
3666
|
+
done: false,
|
|
3667
|
+
value: void 0
|
|
3668
|
+
};
|
|
3669
|
+
}
|
|
3670
|
+
return {
|
|
3671
|
+
done: true,
|
|
3672
|
+
value: response
|
|
3673
|
+
};
|
|
3674
|
+
});
|
|
3675
|
+
try {
|
|
3676
|
+
const value = result;
|
|
3677
|
+
if (value === void 0) {
|
|
3678
|
+
return void 0;
|
|
3679
|
+
}
|
|
3680
|
+
const content = value.outputContent.content;
|
|
3681
|
+
const validatedContent = this._skipResultValidation || !(this._outputSchema instanceof z.ZodType) ? content : this._outputSchema.parse(content);
|
|
3682
|
+
this.status = value.status;
|
|
3683
|
+
this.emit("success", validatedContent);
|
|
3684
|
+
return validatedContent;
|
|
3685
|
+
} catch (error) {
|
|
3686
|
+
this.status = "failed";
|
|
3687
|
+
const castError = error instanceof Error ? error : new Error(String(error));
|
|
3688
|
+
const message = castError instanceof ZodError ? z.prettifyError(castError) : castError.message;
|
|
3689
|
+
this.emit("error", new TaskFailedError(result, message, castError));
|
|
3690
|
+
if (this.listenerCount("error") === 0) {
|
|
3691
|
+
throw error;
|
|
3692
|
+
}
|
|
3693
|
+
return void 0;
|
|
3694
|
+
}
|
|
3695
|
+
}
|
|
3696
|
+
}
|
|
3697
|
+
|
|
3698
|
+
var __defProp$1 = Object.defineProperty;
|
|
3699
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3700
|
+
var __publicField$1 = (obj, key, value) => {
|
|
3701
|
+
__defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
3702
|
+
return value;
|
|
3703
|
+
};
|
|
3704
|
+
const DateRange = z.object({
|
|
3705
|
+
since: z.union([z.date().transform((date) => date.toISOString()), z.iso.datetime()]),
|
|
3706
|
+
until: z.union([z.date().transform((date) => date.toISOString()), z.iso.datetime()])
|
|
3707
|
+
});
|
|
3708
|
+
const TaskListFilters = z.looseObject({
|
|
3709
|
+
id: z.uuid().array().optional(),
|
|
3710
|
+
name: z.string().optional(),
|
|
3711
|
+
status: z.array(TaskStatus).optional(),
|
|
3712
|
+
approvedAt: DateRange.partial().optional(),
|
|
3713
|
+
createdAt: DateRange.partial().optional(),
|
|
3714
|
+
updatedAt: DateRange.partial().optional(),
|
|
3715
|
+
approvedBy: z.string().optional(),
|
|
3716
|
+
createdBy: z.string().optional(),
|
|
3717
|
+
completionRunId: z.uuid().optional(),
|
|
3718
|
+
promptVersionId: z.uuid().optional()
|
|
3719
|
+
}).transform(({ name, approvedAt, createdAt, updatedAt, ...rest }) => ({
|
|
3720
|
+
taskName: name,
|
|
3721
|
+
...approvedAt ? { approvedAtSince: approvedAt.since, approvedAtUntil: approvedAt.until } : {},
|
|
3722
|
+
...createdAt ? { createdAtSince: createdAt.since, createdAtUntil: createdAt.until } : {},
|
|
3723
|
+
...updatedAt ? { updatedAtSince: updatedAt.since, updatedAtUntil: updatedAt.until } : {},
|
|
3724
|
+
...rest
|
|
3725
|
+
}));
|
|
3726
|
+
const TaskListOptions = z.looseObject({
|
|
3727
|
+
limit: z.number().optional(),
|
|
3728
|
+
offset: z.number().optional(),
|
|
3729
|
+
order: z.object({
|
|
3730
|
+
by: z.enum(["createdAt", "updatedAt", "approvedAt", "status", "reference", "name"]),
|
|
3731
|
+
direction: z.enum(["asc", "desc"])
|
|
3732
|
+
}).optional()
|
|
3733
|
+
}).transform(({ order, ...rest }) => ({
|
|
3734
|
+
orderBy: order?.by,
|
|
3735
|
+
orderDirection: order?.direction,
|
|
3736
|
+
...rest
|
|
3737
|
+
}));
|
|
3738
|
+
class Workstation {
|
|
3739
|
+
/**
|
|
3740
|
+
* Creates a new Workstation instance.
|
|
3741
|
+
*
|
|
3742
|
+
* Use {@link Workstation.get} instead.
|
|
3743
|
+
* @internal
|
|
3744
|
+
*/
|
|
3745
|
+
constructor({ client, applicationId, promptVersion, input, output }) {
|
|
3746
|
+
__publicField$1(this, "_id");
|
|
3747
|
+
__publicField$1(this, "_promptVersion");
|
|
3748
|
+
__publicField$1(this, "_client");
|
|
3749
|
+
__publicField$1(this, "_input");
|
|
3750
|
+
__publicField$1(this, "_output");
|
|
3751
|
+
this._id = applicationId;
|
|
3752
|
+
this._promptVersion = promptVersion;
|
|
3753
|
+
this._client = client;
|
|
3754
|
+
this._input = input && input(zod);
|
|
3755
|
+
this._output = output && output(zod);
|
|
3756
|
+
}
|
|
3757
|
+
/**
|
|
3758
|
+
* The unique identifier for this workstation (application ID).
|
|
3759
|
+
*/
|
|
3760
|
+
get id() {
|
|
3761
|
+
return this._id;
|
|
3762
|
+
}
|
|
3763
|
+
/**
|
|
3764
|
+
* The prompt version configuration for this workstation.
|
|
3765
|
+
*/
|
|
3766
|
+
get promptVersion() {
|
|
3767
|
+
return this._promptVersion;
|
|
3768
|
+
}
|
|
3769
|
+
/**
|
|
3770
|
+
* Retrieves a workstation by application ID and optionally validates schemas.
|
|
3771
|
+
*
|
|
3772
|
+
* This is the recommended way to create a Workstation instance.
|
|
3773
|
+
*
|
|
3774
|
+
* @param options - Configuration options
|
|
3775
|
+
* @param options.client - The BaseClient instance for API communication
|
|
3776
|
+
* @param options.applicationId - The unique ID of the workstation
|
|
3777
|
+
* @param options.input - Optional input schema function
|
|
3778
|
+
* @param options.output - Optional output schema function
|
|
3779
|
+
* @param options.skipSchemaValidation - Whether to skip schema validation (defaults to false)
|
|
3780
|
+
* @returns A promise that resolves to a Workstation instance
|
|
3781
|
+
*
|
|
3782
|
+
* @throws {Error} If schema validation fails (when schemas are provided and skipSchemaValidation is false)
|
|
3783
|
+
*
|
|
3784
|
+
* @example
|
|
3785
|
+
* ```typescript
|
|
3786
|
+
* const workstation = await Workstation.get({
|
|
3787
|
+
* client,
|
|
3788
|
+
* applicationId: 'app-123',
|
|
3789
|
+
* input: schema => schema.object({
|
|
3790
|
+
* query: schema.string(),
|
|
3791
|
+
* }),
|
|
3792
|
+
* output: schema => schema.object({
|
|
3793
|
+
* answer: schema.string(),
|
|
3794
|
+
* }),
|
|
3795
|
+
* })
|
|
3796
|
+
* ```
|
|
3797
|
+
*/
|
|
3798
|
+
static async get(options) {
|
|
3799
|
+
const { client, applicationId, input, output, skipSchemaValidation = false } = options;
|
|
3800
|
+
const id = z.uuid({ error: (error) => `Invalid application ID: '${error.input}'. Must be a valid UUID.` }).parse(applicationId);
|
|
3801
|
+
const promptVersion = await client.get(`/prompt-application/${id}/targetPromptVersion`);
|
|
3802
|
+
const inputSchema = input && input(zod);
|
|
3803
|
+
const outputSchema = output && output(zod);
|
|
3804
|
+
if (!skipSchemaValidation) {
|
|
3805
|
+
validateSchemas("Workstation", inputSchema, outputSchema, promptVersion);
|
|
3806
|
+
}
|
|
3807
|
+
return new Workstation({
|
|
3808
|
+
client,
|
|
3809
|
+
applicationId: id,
|
|
3810
|
+
promptVersion,
|
|
3811
|
+
input,
|
|
3812
|
+
output
|
|
3813
|
+
});
|
|
3814
|
+
}
|
|
3815
|
+
/**
|
|
3816
|
+
* Creates a new task for this workstation and returns a promise-like object.
|
|
3817
|
+
*
|
|
3818
|
+
* The returned object can be awaited directly to get the Task instance, or you can
|
|
3819
|
+
* access `.result` to get the final task output directly.
|
|
3820
|
+
*
|
|
3821
|
+
* @param variables - Input variables for the task (must match the input schema)
|
|
3822
|
+
* @param params - Optional task parameters (label, tags, skipResultValidation)
|
|
3823
|
+
* @returns A promise-like object that resolves to the Task instance
|
|
3824
|
+
*
|
|
3825
|
+
* @example
|
|
3826
|
+
* ```typescript
|
|
3827
|
+
* // Get the task instance
|
|
3828
|
+
* const task = await workstation.createTask({ query: 'test' })
|
|
3829
|
+
*
|
|
3830
|
+
* // Or get the result directly
|
|
3831
|
+
* const result = await workstation.createTask({ query: 'test' }).result
|
|
3832
|
+
* ```
|
|
3833
|
+
*/
|
|
3834
|
+
createTask(variables, params = {}) {
|
|
3835
|
+
const validatedVariables = this.parseVariables(variables);
|
|
3836
|
+
const taskDefinitionPromise = this.resolveVariables(validatedVariables).then((resolvedVariables) => this._client.post(`/task`, {
|
|
3837
|
+
body: {
|
|
3838
|
+
name: params.label ?? "Task from SDK",
|
|
3839
|
+
tags: params.tags ?? void 0,
|
|
3840
|
+
promptApplicationId: this._id,
|
|
3841
|
+
rawInput: {
|
|
3842
|
+
variables: resolvedVariables
|
|
3843
|
+
}
|
|
3844
|
+
},
|
|
3845
|
+
transformCase: false
|
|
3846
|
+
})).then((response) => {
|
|
3847
|
+
return new Task(
|
|
3848
|
+
this._client,
|
|
3849
|
+
variables,
|
|
3850
|
+
response[0],
|
|
3851
|
+
response.requestId,
|
|
3852
|
+
this._output,
|
|
3853
|
+
params.skipResultValidation ?? false
|
|
3854
|
+
);
|
|
3855
|
+
});
|
|
3856
|
+
return {
|
|
3857
|
+
then(onfulfilled, onrejected) {
|
|
3858
|
+
return Promise.resolve(taskDefinitionPromise).then((task) => onfulfilled?.(task) ?? task).catch(onrejected);
|
|
3859
|
+
},
|
|
3860
|
+
get result() {
|
|
3861
|
+
return Promise.resolve(taskDefinitionPromise).then((task) => task.result);
|
|
3862
|
+
}
|
|
3863
|
+
};
|
|
3864
|
+
}
|
|
3865
|
+
/**
|
|
3866
|
+
* Retrieves an existing task by its ID.
|
|
3867
|
+
*
|
|
3868
|
+
* This is useful for resuming monitoring of a task that was created earlier.
|
|
3869
|
+
*
|
|
3870
|
+
* @param id - The task ID to retrieve
|
|
3871
|
+
* @param options - Optional configuration
|
|
3872
|
+
* @param options.skipResultValidation - Whether to skip output validation
|
|
3873
|
+
* @returns A promise that resolves to the Task instance
|
|
3874
|
+
*
|
|
3875
|
+
* @example
|
|
3876
|
+
* ```typescript
|
|
3877
|
+
* const task = await workstation.getTask('task-id')
|
|
3878
|
+
*
|
|
3879
|
+
* // Set up event listeners
|
|
3880
|
+
* task.on('statusChange', (status) => console.log('Status:', status))
|
|
3881
|
+
* task.on('success', (result) => console.log('Result:', result))
|
|
3882
|
+
*
|
|
3883
|
+
* // Start polling
|
|
3884
|
+
* task.poll()
|
|
3885
|
+
* ```
|
|
3886
|
+
*/
|
|
3887
|
+
async getTask(id, options = {}) {
|
|
3888
|
+
const taskResponse = await this._client.get(`/task/${id}`);
|
|
3889
|
+
return new Task(
|
|
3890
|
+
this._client,
|
|
3891
|
+
taskResponse.rawInput.variables,
|
|
3892
|
+
taskResponse,
|
|
3893
|
+
taskResponse.requestId,
|
|
3894
|
+
this._output,
|
|
3895
|
+
options.skipResultValidation ?? false
|
|
3896
|
+
);
|
|
3897
|
+
}
|
|
3898
|
+
/**
|
|
3899
|
+
* Validates and parses input variables using the workstation input schema.
|
|
3900
|
+
*
|
|
3901
|
+
* @param variables - Raw input variables to validate.
|
|
3902
|
+
* @returns Parsed and validated variables.
|
|
3903
|
+
* @throws {ZodError} If validation fails when a Zod schema is configured.
|
|
3904
|
+
*/
|
|
3905
|
+
parseVariables(variables) {
|
|
3906
|
+
try {
|
|
3907
|
+
if (this._input instanceof z.ZodType) {
|
|
3908
|
+
return this._input.parse(variables);
|
|
3909
|
+
}
|
|
3910
|
+
return variables;
|
|
3911
|
+
} catch (error) {
|
|
3912
|
+
if (!(error instanceof ZodError)) {
|
|
3913
|
+
throw error;
|
|
3914
|
+
}
|
|
3915
|
+
throw new Error(z.prettifyError(error));
|
|
3916
|
+
}
|
|
3917
|
+
}
|
|
3918
|
+
/**
|
|
3919
|
+
* Processes variables and uploads any TelaFile instances to the server.
|
|
3920
|
+
* Replaces TelaFile objects with their uploaded URLs while preserving other values.
|
|
3921
|
+
*
|
|
3922
|
+
* @returns A promise resolving to the processed variables object.
|
|
3923
|
+
*/
|
|
3924
|
+
async resolveVariables(inputVariables) {
|
|
3925
|
+
const variables = {};
|
|
3926
|
+
for (const [key, value] of Object.entries(inputVariables)) {
|
|
3927
|
+
if (isTelaFileArray(value)) {
|
|
3928
|
+
variables[key] = { files: await uploadFiles(value, this._client) };
|
|
3929
|
+
continue;
|
|
3930
|
+
}
|
|
3931
|
+
if (isTelaFile(value)) {
|
|
3932
|
+
variables[key] = await uploadFile(value, this._client);
|
|
3933
|
+
continue;
|
|
3934
|
+
}
|
|
3935
|
+
variables[key] = value;
|
|
3936
|
+
}
|
|
3937
|
+
return variables;
|
|
3938
|
+
}
|
|
3939
|
+
/**
|
|
3940
|
+
* Lists tasks for this workstation with optional filtering and pagination.
|
|
3941
|
+
*
|
|
3942
|
+
* Returns a page of tasks matching the specified filters, along with metadata
|
|
3943
|
+
* for pagination.
|
|
3944
|
+
*
|
|
3945
|
+
* @param params - Query parameters
|
|
3946
|
+
* @param params.filters - Optional filters to apply (ID, name, status, dates, etc.)
|
|
3947
|
+
* @param params.options - Optional pagination and sorting options
|
|
3948
|
+
* @param params.rawQuery - Raw query object (internal use, overrides filters/options)
|
|
3949
|
+
* @returns A promise that resolves to an object containing tasks and pagination metadata
|
|
3950
|
+
*
|
|
3951
|
+
* @example
|
|
3952
|
+
* ```typescript
|
|
3953
|
+
* // Get first 10 completed tasks
|
|
3954
|
+
* const { tasks, meta } = await workstation.listTasks({
|
|
3955
|
+
* filters: { status: ['completed'] },
|
|
3956
|
+
* options: { limit: 10, offset: 0 },
|
|
3957
|
+
* })
|
|
3958
|
+
*
|
|
3959
|
+
* // Sort by creation date
|
|
3960
|
+
* const { tasks, meta } = await workstation.listTasks({
|
|
3961
|
+
* options: {
|
|
3962
|
+
* order: { by: 'createdAt', direction: 'desc' },
|
|
3963
|
+
* limit: 20,
|
|
3964
|
+
* },
|
|
3965
|
+
* })
|
|
3966
|
+
* ```
|
|
3967
|
+
*/
|
|
3968
|
+
async listTasks({ filters, options, rawQuery }) {
|
|
3969
|
+
function getQuery(id) {
|
|
3970
|
+
if (rawQuery) {
|
|
3971
|
+
return rawQuery;
|
|
3972
|
+
}
|
|
3973
|
+
const validatedFilters = TaskListFilters.optional().parse(filters);
|
|
3974
|
+
const validatedOptions = TaskListOptions.optional().parse(options);
|
|
3975
|
+
return {
|
|
3976
|
+
promptApplicationId: id,
|
|
3977
|
+
objectLinks: true,
|
|
3978
|
+
...validatedFilters,
|
|
3979
|
+
...validatedOptions
|
|
3980
|
+
};
|
|
3981
|
+
}
|
|
3982
|
+
const query = getQuery(this._id);
|
|
3983
|
+
const response = await this._client.get(`/task`, {
|
|
3984
|
+
query,
|
|
3985
|
+
transformCase: false
|
|
3986
|
+
});
|
|
3987
|
+
const tasks = response.data.map((task) => new Task(
|
|
3988
|
+
this._client,
|
|
3989
|
+
task.rawInput?.variables,
|
|
3990
|
+
task,
|
|
3991
|
+
task.requestId,
|
|
3992
|
+
this._output
|
|
3993
|
+
));
|
|
3994
|
+
return {
|
|
3995
|
+
tasks,
|
|
3996
|
+
meta: response.meta
|
|
3997
|
+
};
|
|
3998
|
+
}
|
|
3999
|
+
/**
|
|
4000
|
+
* Asynchronously iterates through all tasks matching the specified filters.
|
|
4001
|
+
*
|
|
4002
|
+
* This generator automatically handles pagination, fetching additional pages
|
|
4003
|
+
* as needed. Each iteration yields a task and the current page metadata.
|
|
4004
|
+
*
|
|
4005
|
+
* @param params - Query parameters
|
|
4006
|
+
* @param params.filters - Optional filters to apply (ID, name, status, dates, etc.)
|
|
4007
|
+
* @param params.options - Optional initial pagination and sorting options
|
|
4008
|
+
* @yields A tuple of [task, metadata] for each task
|
|
4009
|
+
*
|
|
4010
|
+
* @example
|
|
4011
|
+
* ```typescript
|
|
4012
|
+
* // Iterate through all pending tasks
|
|
4013
|
+
* for await (const [task, meta] of workstation.iterateTasks({
|
|
4014
|
+
* filters: { status: ['pending'] },
|
|
4015
|
+
* })) {
|
|
4016
|
+
* console.log(`Task ${task.id}: ${task.status}`)
|
|
4017
|
+
* console.log(`Page ${meta.currentPage} of ${meta.totalPages}`)
|
|
4018
|
+
* }
|
|
4019
|
+
*
|
|
4020
|
+
* // Process tasks in batches
|
|
4021
|
+
* for await (const [task, meta] of workstation.iterateTasks({
|
|
4022
|
+
* options: { limit: 50 },
|
|
4023
|
+
* })) {
|
|
4024
|
+
* await processTask(task)
|
|
4025
|
+
* }
|
|
4026
|
+
* ```
|
|
4027
|
+
*/
|
|
4028
|
+
async *iterateTasks({ filters, options }) {
|
|
4029
|
+
let rawQuery;
|
|
4030
|
+
let hasMore = true;
|
|
4031
|
+
while (hasMore) {
|
|
4032
|
+
const { tasks, meta } = await this.listTasks({
|
|
4033
|
+
filters,
|
|
4034
|
+
options,
|
|
4035
|
+
rawQuery
|
|
4036
|
+
});
|
|
4037
|
+
for (const task of tasks) {
|
|
4038
|
+
yield [task, meta];
|
|
4039
|
+
}
|
|
4040
|
+
if (meta.links.next !== null) {
|
|
4041
|
+
rawQuery = {
|
|
4042
|
+
...meta.links.next,
|
|
4043
|
+
objectLinks: true
|
|
4044
|
+
};
|
|
4045
|
+
} else {
|
|
4046
|
+
hasMore = false;
|
|
4047
|
+
}
|
|
4048
|
+
}
|
|
4049
|
+
}
|
|
4050
|
+
}
|
|
4051
|
+
|
|
3372
4052
|
var __defProp = Object.defineProperty;
|
|
3373
4053
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3374
4054
|
var __publicField = (obj, key, value) => {
|
|
@@ -3430,6 +4110,17 @@ const _TelaSDK = class _TelaSDK extends BaseClient {
|
|
|
3430
4110
|
});
|
|
3431
4111
|
}
|
|
3432
4112
|
});
|
|
4113
|
+
__publicField(this, "workstation", {
|
|
4114
|
+
get: async (options) => {
|
|
4115
|
+
return Workstation.get({
|
|
4116
|
+
applicationId: options.applicationId,
|
|
4117
|
+
input: options.input,
|
|
4118
|
+
output: options.output,
|
|
4119
|
+
skipSchemaValidation: options.skipSchemaValidation,
|
|
4120
|
+
client: this
|
|
4121
|
+
});
|
|
4122
|
+
}
|
|
4123
|
+
});
|
|
3433
4124
|
this.opts = { baseURL, ...rest };
|
|
3434
4125
|
this.apiKey = apiKey;
|
|
3435
4126
|
this.jwt = jwt;
|
|
@@ -3489,9 +4180,13 @@ __publicField(_TelaSDK, "FileUploadError", FileUploadError);
|
|
|
3489
4180
|
__publicField(_TelaSDK, "MissingApiKeyOrJWTError", MissingApiKeyOrJWTError);
|
|
3490
4181
|
/** Thrown when both an API key and a JWT are provided. */
|
|
3491
4182
|
__publicField(_TelaSDK, "ConflictApiKeyAndJWTError", ConflictApiKeyAndJWTError);
|
|
4183
|
+
/** Thrown when a canvas execution fails on the server. */
|
|
4184
|
+
__publicField(_TelaSDK, "ExecutionFailedError", ExecutionFailedError);
|
|
4185
|
+
/** Thrown when a workstation task fails on the server. */
|
|
4186
|
+
__publicField(_TelaSDK, "TaskFailedError", TaskFailedError);
|
|
3492
4187
|
let TelaSDK = _TelaSDK;
|
|
3493
4188
|
function createTelaClient(opts) {
|
|
3494
4189
|
return new TelaSDK(opts);
|
|
3495
4190
|
}
|
|
3496
4191
|
|
|
3497
|
-
export { APIError, AuthenticationError, AuthorizationError, BadRequestError, BaseClient, BatchExecutionFailedError, ConflictApiKeyAndJWTError, ConflictError, ConnectionError, ConnectionTimeout, EmptyFileError, ExecutionFailedError, ExecutionNotStartedError, FileUploadError, InternalServerError, InvalidExecutionModeError, InvalidFileURL, MissingApiKeyOrJWTError, NotFoundError, RateLimitError, TelaError, TelaFile, TelaFileSchema, TelaSDK, UnprocessableEntityError, UserAbortError, createTelaClient, toError };
|
|
4192
|
+
export { APIError, AuthenticationError, AuthorizationError, BadRequestError, BaseClient, BatchExecutionFailedError, ConflictApiKeyAndJWTError, ConflictError, ConnectionError, ConnectionTimeout, EmptyFileError, ExecutionFailedError, ExecutionNotStartedError, FileUploadError, InternalServerError, InvalidExecutionModeError, InvalidFileURL, MissingApiKeyOrJWTError, NotFoundError, RateLimitError, TaskFailedError, TelaError, TelaFile, TelaFileSchema, TelaSDK, UnprocessableEntityError, UserAbortError, createTelaClient, isTelaFile, isTelaFileArray, toError };
|