@promptbook/vercel 0.86.10 → 0.86.22
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 +35 -42
- package/esm/index.es.js +256 -364
- package/esm/index.es.js.map +1 -1
- package/esm/typings/src/_packages/types.index.d.ts +2 -0
- package/esm/typings/src/conversion/utils/extractVariablesFromScript.d.ts +5 -5
- package/esm/typings/src/execution/AbstractTaskResult.d.ts +3 -3
- package/esm/typings/src/execution/PipelineExecutorResult.d.ts +1 -1
- package/esm/typings/src/execution/utils/usageToHuman.d.ts +11 -1
- package/esm/typings/src/execution/utils/usageToWorktime.d.ts +9 -1
- package/esm/typings/src/formats/_common/FormatDefinition.d.ts +1 -1
- package/esm/typings/src/formats/text/TextFormatDefinition.d.ts +1 -1
- package/esm/typings/src/llm-providers/multiple/MultipleLlmExecutionTools.d.ts +1 -1
- package/esm/typings/src/types/typeAliases.d.ts +6 -0
- package/package.json +2 -2
- package/umd/index.umd.js +256 -364
- package/umd/index.umd.js.map +1 -1
package/esm/index.es.js
CHANGED
|
@@ -8,153 +8,31 @@ import spaceTrim, { spaceTrim as spaceTrim$1 } from 'spacetrim';
|
|
|
8
8
|
* @generated
|
|
9
9
|
* @see https://github.com/webgptorg/book
|
|
10
10
|
*/
|
|
11
|
-
|
|
11
|
+
const BOOK_LANGUAGE_VERSION = '1.0.0';
|
|
12
12
|
/**
|
|
13
13
|
* The version of the Promptbook engine
|
|
14
14
|
*
|
|
15
15
|
* @generated
|
|
16
16
|
* @see https://github.com/webgptorg/promptbook
|
|
17
17
|
*/
|
|
18
|
-
|
|
18
|
+
const PROMPTBOOK_ENGINE_VERSION = '0.86.22';
|
|
19
19
|
/**
|
|
20
20
|
* TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
|
|
21
21
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
|
-
/*! *****************************************************************************
|
|
25
|
-
Copyright (c) Microsoft Corporation.
|
|
26
|
-
|
|
27
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
28
|
-
purpose with or without fee is hereby granted.
|
|
29
|
-
|
|
30
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
31
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
32
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
33
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
34
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
35
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
36
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
37
|
-
***************************************************************************** */
|
|
38
|
-
/* global Reflect, Promise */
|
|
39
|
-
|
|
40
|
-
var extendStatics = function(d, b) {
|
|
41
|
-
extendStatics = Object.setPrototypeOf ||
|
|
42
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
43
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
44
|
-
return extendStatics(d, b);
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
function __extends(d, b) {
|
|
48
|
-
if (typeof b !== "function" && b !== null)
|
|
49
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
50
|
-
extendStatics(d, b);
|
|
51
|
-
function __() { this.constructor = d; }
|
|
52
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
var __assign = function() {
|
|
56
|
-
__assign = Object.assign || function __assign(t) {
|
|
57
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
58
|
-
s = arguments[i];
|
|
59
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
60
|
-
}
|
|
61
|
-
return t;
|
|
62
|
-
};
|
|
63
|
-
return __assign.apply(this, arguments);
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
67
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
68
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
69
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
70
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
71
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
72
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function __generator(thisArg, body) {
|
|
77
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
78
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
79
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
80
|
-
function step(op) {
|
|
81
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
82
|
-
while (_) try {
|
|
83
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
84
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
85
|
-
switch (op[0]) {
|
|
86
|
-
case 0: case 1: t = op; break;
|
|
87
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
88
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
89
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
90
|
-
default:
|
|
91
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
92
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
93
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
94
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
95
|
-
if (t[2]) _.ops.pop();
|
|
96
|
-
_.trys.pop(); continue;
|
|
97
|
-
}
|
|
98
|
-
op = body.call(thisArg, _);
|
|
99
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
100
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function __values(o) {
|
|
105
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
106
|
-
if (m) return m.call(o);
|
|
107
|
-
if (o && typeof o.length === "number") return {
|
|
108
|
-
next: function () {
|
|
109
|
-
if (o && i >= o.length) o = void 0;
|
|
110
|
-
return { value: o && o[i++], done: !o };
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function __read(o, n) {
|
|
117
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
118
|
-
if (!m) return o;
|
|
119
|
-
var i = m.call(o), r, ar = [], e;
|
|
120
|
-
try {
|
|
121
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
122
|
-
}
|
|
123
|
-
catch (error) { e = { error: error }; }
|
|
124
|
-
finally {
|
|
125
|
-
try {
|
|
126
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
127
|
-
}
|
|
128
|
-
finally { if (e) throw e.error; }
|
|
129
|
-
}
|
|
130
|
-
return ar;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
function __spreadArray(to, from, pack) {
|
|
134
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
135
|
-
if (ar || !(i in from)) {
|
|
136
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
137
|
-
ar[i] = from[i];
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
141
|
-
}
|
|
142
|
-
|
|
143
24
|
/**
|
|
144
25
|
* This error indicates errors during the execution of the pipeline
|
|
145
26
|
*
|
|
146
27
|
* @public exported from `@promptbook/core`
|
|
147
28
|
*/
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
Object.setPrototypeOf(_this, PipelineExecutionError.prototype);
|
|
154
|
-
return _this;
|
|
29
|
+
class PipelineExecutionError extends Error {
|
|
30
|
+
constructor(message) {
|
|
31
|
+
super(message);
|
|
32
|
+
this.name = 'PipelineExecutionError';
|
|
33
|
+
Object.setPrototypeOf(this, PipelineExecutionError.prototype);
|
|
155
34
|
}
|
|
156
|
-
|
|
157
|
-
}(Error));
|
|
35
|
+
}
|
|
158
36
|
|
|
159
37
|
/**
|
|
160
38
|
* Freezes the given object and all its nested objects recursively
|
|
@@ -166,26 +44,15 @@ var PipelineExecutionError = /** @class */ (function (_super) {
|
|
|
166
44
|
* @public exported from `@promptbook/utils`
|
|
167
45
|
*/
|
|
168
46
|
function $deepFreeze(objectValue) {
|
|
169
|
-
var e_1, _a;
|
|
170
47
|
if (Array.isArray(objectValue)) {
|
|
171
|
-
return Object.freeze(objectValue.map(
|
|
172
|
-
}
|
|
173
|
-
var propertyNames = Object.getOwnPropertyNames(objectValue);
|
|
174
|
-
try {
|
|
175
|
-
for (var propertyNames_1 = __values(propertyNames), propertyNames_1_1 = propertyNames_1.next(); !propertyNames_1_1.done; propertyNames_1_1 = propertyNames_1.next()) {
|
|
176
|
-
var propertyName = propertyNames_1_1.value;
|
|
177
|
-
var value = objectValue[propertyName];
|
|
178
|
-
if (value && typeof value === 'object') {
|
|
179
|
-
$deepFreeze(value);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
48
|
+
return Object.freeze(objectValue.map((item) => $deepFreeze(item)));
|
|
182
49
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
50
|
+
const propertyNames = Object.getOwnPropertyNames(objectValue);
|
|
51
|
+
for (const propertyName of propertyNames) {
|
|
52
|
+
const value = objectValue[propertyName];
|
|
53
|
+
if (value && typeof value === 'object') {
|
|
54
|
+
$deepFreeze(value);
|
|
187
55
|
}
|
|
188
|
-
finally { if (e_1) throw e_1.error; }
|
|
189
56
|
}
|
|
190
57
|
Object.freeze(objectValue);
|
|
191
58
|
return objectValue;
|
|
@@ -225,7 +92,7 @@ $deepFreeze({
|
|
|
225
92
|
*
|
|
226
93
|
* @public exported from `@promptbook/core`
|
|
227
94
|
*/
|
|
228
|
-
|
|
95
|
+
const UNCERTAIN_USAGE = $deepFreeze({
|
|
229
96
|
price: { value: 0, isUncertain: true },
|
|
230
97
|
input: {
|
|
231
98
|
tokensCount: { value: 0, isUncertain: true },
|
|
@@ -269,32 +136,32 @@ function $getCurrentDate() {
|
|
|
269
136
|
*
|
|
270
137
|
* @public exported from `@promptbook/core`
|
|
271
138
|
*/
|
|
272
|
-
|
|
139
|
+
const NAME = `Promptbook`;
|
|
273
140
|
/**
|
|
274
141
|
* Email of the responsible person
|
|
275
142
|
*
|
|
276
143
|
* @public exported from `@promptbook/core`
|
|
277
144
|
*/
|
|
278
|
-
|
|
145
|
+
const ADMIN_EMAIL = 'pavol@ptbk.io';
|
|
279
146
|
/**
|
|
280
147
|
* Name of the responsible person for the Promptbook on GitHub
|
|
281
148
|
*
|
|
282
149
|
* @public exported from `@promptbook/core`
|
|
283
150
|
*/
|
|
284
|
-
|
|
151
|
+
const ADMIN_GITHUB_NAME = 'hejny';
|
|
285
152
|
// <- TODO: [🧠] Better system for generator warnings - not always "code" and "by `@promptbook/cli`"
|
|
286
153
|
/**
|
|
287
154
|
* The maximum number of iterations for a loops
|
|
288
155
|
*
|
|
289
156
|
* @private within the repository - too low-level in comparison with other `MAX_...`
|
|
290
157
|
*/
|
|
291
|
-
|
|
158
|
+
const LOOP_LIMIT = 1000;
|
|
292
159
|
/**
|
|
293
160
|
* Strings to represent various values in the context of parameter values
|
|
294
161
|
*
|
|
295
162
|
* @public exported from `@promptbook/utils`
|
|
296
163
|
*/
|
|
297
|
-
|
|
164
|
+
const VALUE_STRINGS = {
|
|
298
165
|
empty: '(nothing; empty string)',
|
|
299
166
|
null: '(no value; null)',
|
|
300
167
|
undefined: '(unknown value; undefined)',
|
|
@@ -308,7 +175,7 @@ var VALUE_STRINGS = {
|
|
|
308
175
|
*
|
|
309
176
|
* @public exported from `@promptbook/utils`
|
|
310
177
|
*/
|
|
311
|
-
|
|
178
|
+
const SMALL_NUMBER = 0.001;
|
|
312
179
|
// <- TODO: [🧜♂️]
|
|
313
180
|
/**
|
|
314
181
|
* @@@
|
|
@@ -333,8 +200,11 @@ Object.freeze({
|
|
|
333
200
|
* @public exported from `@promptbook/utils`
|
|
334
201
|
*/
|
|
335
202
|
function orderJson(options) {
|
|
336
|
-
|
|
337
|
-
|
|
203
|
+
const { value, order } = options;
|
|
204
|
+
const orderedValue = {
|
|
205
|
+
...(order === undefined ? {} : Object.fromEntries(order.map((key) => [key, undefined]))),
|
|
206
|
+
...value,
|
|
207
|
+
};
|
|
338
208
|
return orderedValue;
|
|
339
209
|
}
|
|
340
210
|
|
|
@@ -344,11 +214,37 @@ function orderJson(options) {
|
|
|
344
214
|
* @private private within the repository
|
|
345
215
|
*/
|
|
346
216
|
function getErrorReportUrl(error) {
|
|
347
|
-
|
|
348
|
-
title:
|
|
349
|
-
body: spaceTrim(
|
|
217
|
+
const report = {
|
|
218
|
+
title: `🐜 Error report from ${NAME}`,
|
|
219
|
+
body: spaceTrim((block) => `
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
\`${error.name || 'Error'}\` has occurred in the [${NAME}], please look into it @${ADMIN_GITHUB_NAME}.
|
|
223
|
+
|
|
224
|
+
\`\`\`
|
|
225
|
+
${block(error.message || '(no error message)')}
|
|
226
|
+
\`\`\`
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
## More info:
|
|
230
|
+
|
|
231
|
+
- **Promptbook engine version:** ${PROMPTBOOK_ENGINE_VERSION}
|
|
232
|
+
- **Book language version:** ${BOOK_LANGUAGE_VERSION}
|
|
233
|
+
- **Time:** ${new Date().toISOString()}
|
|
234
|
+
|
|
235
|
+
<details>
|
|
236
|
+
<summary>Stack trace:</summary>
|
|
237
|
+
|
|
238
|
+
## Stack trace:
|
|
239
|
+
|
|
240
|
+
\`\`\`stacktrace
|
|
241
|
+
${block(error.stack || '(empty)')}
|
|
242
|
+
\`\`\`
|
|
243
|
+
</details>
|
|
244
|
+
|
|
245
|
+
`),
|
|
350
246
|
};
|
|
351
|
-
|
|
247
|
+
const reportUrl = new URL(`https://github.com/webgptorg/promptbook/issues/new`);
|
|
352
248
|
reportUrl.searchParams.set('labels', 'bug');
|
|
353
249
|
reportUrl.searchParams.set('assignees', ADMIN_GITHUB_NAME);
|
|
354
250
|
reportUrl.searchParams.set('title', report.title);
|
|
@@ -361,16 +257,24 @@ function getErrorReportUrl(error) {
|
|
|
361
257
|
*
|
|
362
258
|
* @public exported from `@promptbook/core`
|
|
363
259
|
*/
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
260
|
+
class UnexpectedError extends Error {
|
|
261
|
+
constructor(message) {
|
|
262
|
+
super(spaceTrim$1((block) => `
|
|
263
|
+
${block(message)}
|
|
264
|
+
|
|
265
|
+
Note: This error should not happen.
|
|
266
|
+
It's probbably a bug in the pipeline collection
|
|
267
|
+
|
|
268
|
+
Please report issue:
|
|
269
|
+
${block(getErrorReportUrl(new Error(message)).href)}
|
|
270
|
+
|
|
271
|
+
Or contact us on ${ADMIN_EMAIL}
|
|
272
|
+
|
|
273
|
+
`));
|
|
274
|
+
this.name = 'UnexpectedError';
|
|
275
|
+
Object.setPrototypeOf(this, UnexpectedError.prototype);
|
|
371
276
|
}
|
|
372
|
-
|
|
373
|
-
}(Error));
|
|
277
|
+
}
|
|
374
278
|
|
|
375
279
|
/**
|
|
376
280
|
* Checks if the value is [🚉] serializable as JSON
|
|
@@ -393,10 +297,9 @@ var UnexpectedError = /** @class */ (function (_super) {
|
|
|
393
297
|
* @public exported from `@promptbook/utils`
|
|
394
298
|
*/
|
|
395
299
|
function checkSerializableAsJson(options) {
|
|
396
|
-
|
|
397
|
-
var value = options.value, name = options.name, message = options.message;
|
|
300
|
+
const { value, name, message } = options;
|
|
398
301
|
if (value === undefined) {
|
|
399
|
-
throw new UnexpectedError(
|
|
302
|
+
throw new UnexpectedError(`${name} is undefined`);
|
|
400
303
|
}
|
|
401
304
|
else if (value === null) {
|
|
402
305
|
return;
|
|
@@ -411,49 +314,54 @@ function checkSerializableAsJson(options) {
|
|
|
411
314
|
return;
|
|
412
315
|
}
|
|
413
316
|
else if (typeof value === 'symbol') {
|
|
414
|
-
throw new UnexpectedError(
|
|
317
|
+
throw new UnexpectedError(`${name} is symbol`);
|
|
415
318
|
}
|
|
416
319
|
else if (typeof value === 'function') {
|
|
417
|
-
throw new UnexpectedError(
|
|
320
|
+
throw new UnexpectedError(`${name} is function`);
|
|
418
321
|
}
|
|
419
322
|
else if (typeof value === 'object' && Array.isArray(value)) {
|
|
420
|
-
for (
|
|
421
|
-
checkSerializableAsJson({ name:
|
|
323
|
+
for (let i = 0; i < value.length; i++) {
|
|
324
|
+
checkSerializableAsJson({ name: `${name}[${i}]`, value: value[i], message });
|
|
422
325
|
}
|
|
423
326
|
}
|
|
424
327
|
else if (typeof value === 'object') {
|
|
425
328
|
if (value instanceof Date) {
|
|
426
|
-
throw new UnexpectedError(spaceTrim(
|
|
329
|
+
throw new UnexpectedError(spaceTrim((block) => `
|
|
330
|
+
\`${name}\` is Date
|
|
331
|
+
|
|
332
|
+
Use \`string_date_iso8601\` instead
|
|
333
|
+
|
|
334
|
+
Additional message for \`${name}\`:
|
|
335
|
+
${block(message || '(nothing)')}
|
|
336
|
+
`));
|
|
427
337
|
}
|
|
428
338
|
else if (value instanceof Map) {
|
|
429
|
-
throw new UnexpectedError(
|
|
339
|
+
throw new UnexpectedError(`${name} is Map`);
|
|
430
340
|
}
|
|
431
341
|
else if (value instanceof Set) {
|
|
432
|
-
throw new UnexpectedError(
|
|
342
|
+
throw new UnexpectedError(`${name} is Set`);
|
|
433
343
|
}
|
|
434
344
|
else if (value instanceof RegExp) {
|
|
435
|
-
throw new UnexpectedError(
|
|
345
|
+
throw new UnexpectedError(`${name} is RegExp`);
|
|
436
346
|
}
|
|
437
347
|
else if (value instanceof Error) {
|
|
438
|
-
throw new UnexpectedError(spaceTrim(
|
|
348
|
+
throw new UnexpectedError(spaceTrim((block) => `
|
|
349
|
+
\`${name}\` is unserialized Error
|
|
350
|
+
|
|
351
|
+
Use function \`serializeError\`
|
|
352
|
+
|
|
353
|
+
Additional message for \`${name}\`:
|
|
354
|
+
${block(message || '(nothing)')}
|
|
355
|
+
|
|
356
|
+
`));
|
|
439
357
|
}
|
|
440
358
|
else {
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
// Note: undefined in object is serializable - it is just omited
|
|
446
|
-
continue;
|
|
447
|
-
}
|
|
448
|
-
checkSerializableAsJson({ name: "".concat(name, ".").concat(subName), value: subValue, message: message });
|
|
359
|
+
for (const [subName, subValue] of Object.entries(value)) {
|
|
360
|
+
if (subValue === undefined) {
|
|
361
|
+
// Note: undefined in object is serializable - it is just omited
|
|
362
|
+
continue;
|
|
449
363
|
}
|
|
450
|
-
|
|
451
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
452
|
-
finally {
|
|
453
|
-
try {
|
|
454
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
455
|
-
}
|
|
456
|
-
finally { if (e_1) throw e_1.error; }
|
|
364
|
+
checkSerializableAsJson({ name: `${name}.${subName}`, value: subValue, message });
|
|
457
365
|
}
|
|
458
366
|
try {
|
|
459
367
|
JSON.stringify(value); // <- TODO: [0]
|
|
@@ -462,7 +370,14 @@ function checkSerializableAsJson(options) {
|
|
|
462
370
|
if (!(error instanceof Error)) {
|
|
463
371
|
throw error;
|
|
464
372
|
}
|
|
465
|
-
throw new UnexpectedError(spaceTrim(
|
|
373
|
+
throw new UnexpectedError(spaceTrim((block) => `
|
|
374
|
+
\`${name}\` is not serializable
|
|
375
|
+
|
|
376
|
+
${block(error.stack || error.message)}
|
|
377
|
+
|
|
378
|
+
Additional message for \`${name}\`:
|
|
379
|
+
${block(message || '(nothing)')}
|
|
380
|
+
`));
|
|
466
381
|
}
|
|
467
382
|
/*
|
|
468
383
|
TODO: [0] Is there some more elegant way to check circular references?
|
|
@@ -487,7 +402,12 @@ function checkSerializableAsJson(options) {
|
|
|
487
402
|
}
|
|
488
403
|
}
|
|
489
404
|
else {
|
|
490
|
-
throw new UnexpectedError(spaceTrim(
|
|
405
|
+
throw new UnexpectedError(spaceTrim((block) => `
|
|
406
|
+
\`${name}\` is unknown type
|
|
407
|
+
|
|
408
|
+
Additional message for \`${name}\`:
|
|
409
|
+
${block(message || '(nothing)')}
|
|
410
|
+
`));
|
|
491
411
|
}
|
|
492
412
|
}
|
|
493
413
|
/**
|
|
@@ -533,9 +453,9 @@ function deepClone(objectValue) {
|
|
|
533
453
|
* @public exported from `@promptbook/utils`
|
|
534
454
|
*/
|
|
535
455
|
function exportJson(options) {
|
|
536
|
-
|
|
537
|
-
checkSerializableAsJson({ name
|
|
538
|
-
|
|
456
|
+
const { name, value, order, message } = options;
|
|
457
|
+
checkSerializableAsJson({ name, value, message });
|
|
458
|
+
const orderedValue =
|
|
539
459
|
// TODO: Fix error "Type instantiation is excessively deep and possibly infinite."
|
|
540
460
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
541
461
|
// @ts-ignore
|
|
@@ -558,19 +478,19 @@ function exportJson(options) {
|
|
|
558
478
|
*
|
|
559
479
|
* @private within the repository
|
|
560
480
|
*/
|
|
561
|
-
|
|
481
|
+
const REPLACING_NONCE = 'ptbkauk42kV2dzao34faw7FudQUHYPtW';
|
|
562
482
|
/**
|
|
563
483
|
* @@@
|
|
564
484
|
*
|
|
565
485
|
* @private within the repository
|
|
566
486
|
*/
|
|
567
|
-
|
|
487
|
+
const RESERVED_PARAMETER_MISSING_VALUE = 'MISSING-' + REPLACING_NONCE;
|
|
568
488
|
/**
|
|
569
489
|
* @@@
|
|
570
490
|
*
|
|
571
491
|
* @private within the repository
|
|
572
492
|
*/
|
|
573
|
-
|
|
493
|
+
const RESERVED_PARAMETER_RESTRICTED = 'RESTRICTED-' + REPLACING_NONCE;
|
|
574
494
|
/**
|
|
575
495
|
* The names of the parameters that are reserved for special purposes
|
|
576
496
|
*
|
|
@@ -578,7 +498,7 @@ var RESERVED_PARAMETER_RESTRICTED = 'RESTRICTED-' + REPLACING_NONCE;
|
|
|
578
498
|
*/
|
|
579
499
|
exportJson({
|
|
580
500
|
name: 'RESERVED_PARAMETER_NAMES',
|
|
581
|
-
message:
|
|
501
|
+
message: `The names of the parameters that are reserved for special purposes`,
|
|
582
502
|
value: [
|
|
583
503
|
'content',
|
|
584
504
|
'context',
|
|
@@ -600,16 +520,13 @@ exportJson({
|
|
|
600
520
|
*
|
|
601
521
|
* @public exported from `@promptbook/core`
|
|
602
522
|
*/
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
Object.setPrototypeOf(_this, LimitReachedError.prototype);
|
|
609
|
-
return _this;
|
|
523
|
+
class LimitReachedError extends Error {
|
|
524
|
+
constructor(message) {
|
|
525
|
+
super(message);
|
|
526
|
+
this.name = 'LimitReachedError';
|
|
527
|
+
Object.setPrototypeOf(this, LimitReachedError.prototype);
|
|
610
528
|
}
|
|
611
|
-
|
|
612
|
-
}(Error));
|
|
529
|
+
}
|
|
613
530
|
|
|
614
531
|
/**
|
|
615
532
|
* Format either small or big number
|
|
@@ -629,9 +546,9 @@ function numberToString(value) {
|
|
|
629
546
|
else if (value === -Infinity) {
|
|
630
547
|
return VALUE_STRINGS.negativeInfinity;
|
|
631
548
|
}
|
|
632
|
-
for (
|
|
633
|
-
|
|
634
|
-
|
|
549
|
+
for (let exponent = 0; exponent < 15; exponent++) {
|
|
550
|
+
const factor = 10 ** exponent;
|
|
551
|
+
const valueRounded = Math.round(value * factor) / factor;
|
|
635
552
|
if (Math.abs(value - valueRounded) / value < SMALL_NUMBER) {
|
|
636
553
|
return valueRounded.toFixed(exponent);
|
|
637
554
|
}
|
|
@@ -699,47 +616,38 @@ function valueToString(value) {
|
|
|
699
616
|
* @public exported from `@promptbook/utils`
|
|
700
617
|
*/
|
|
701
618
|
function templateParameters(template, parameters) {
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
var _d = __read(_c.value, 2), parameterName = _d[0], parameterValue = _d[1];
|
|
706
|
-
if (parameterValue === RESERVED_PARAMETER_MISSING_VALUE) {
|
|
707
|
-
throw new UnexpectedError("Parameter `{".concat(parameterName, "}` has missing value"));
|
|
708
|
-
}
|
|
709
|
-
else if (parameterValue === RESERVED_PARAMETER_RESTRICTED) {
|
|
710
|
-
// TODO: [🍵]
|
|
711
|
-
throw new UnexpectedError("Parameter `{".concat(parameterName, "}` is restricted to use"));
|
|
712
|
-
}
|
|
619
|
+
for (const [parameterName, parameterValue] of Object.entries(parameters)) {
|
|
620
|
+
if (parameterValue === RESERVED_PARAMETER_MISSING_VALUE) {
|
|
621
|
+
throw new UnexpectedError(`Parameter \`{${parameterName}}\` has missing value`);
|
|
713
622
|
}
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
try {
|
|
718
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
623
|
+
else if (parameterValue === RESERVED_PARAMETER_RESTRICTED) {
|
|
624
|
+
// TODO: [🍵]
|
|
625
|
+
throw new UnexpectedError(`Parameter \`{${parameterName}}\` is restricted to use`);
|
|
719
626
|
}
|
|
720
|
-
finally { if (e_1) throw e_1.error; }
|
|
721
627
|
}
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
628
|
+
let replacedTemplates = template;
|
|
629
|
+
let match;
|
|
630
|
+
let loopLimit = LOOP_LIMIT;
|
|
631
|
+
while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
|
|
632
|
+
.exec(replacedTemplates))) {
|
|
726
633
|
if (loopLimit-- < 0) {
|
|
727
634
|
throw new LimitReachedError('Loop limit reached during parameters replacement in `templateParameters`');
|
|
728
635
|
}
|
|
729
|
-
|
|
730
|
-
|
|
636
|
+
const precol = match.groups.precol;
|
|
637
|
+
const parameterName = match.groups.parameterName;
|
|
731
638
|
if (parameterName === '') {
|
|
732
|
-
|
|
639
|
+
// Note: Skip empty placeholders. It's used to avoid confusion with JSON-like strings
|
|
640
|
+
continue;
|
|
733
641
|
}
|
|
734
642
|
if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
|
|
735
643
|
throw new PipelineExecutionError('Parameter is already opened or not closed');
|
|
736
644
|
}
|
|
737
645
|
if (parameters[parameterName] === undefined) {
|
|
738
|
-
throw new PipelineExecutionError(
|
|
646
|
+
throw new PipelineExecutionError(`Parameter \`{${parameterName}}\` is not defined`);
|
|
739
647
|
}
|
|
740
|
-
|
|
648
|
+
let parameterValue = parameters[parameterName];
|
|
741
649
|
if (parameterValue === undefined) {
|
|
742
|
-
throw new PipelineExecutionError(
|
|
650
|
+
throw new PipelineExecutionError(`Parameter \`{${parameterName}}\` is not defined`);
|
|
743
651
|
}
|
|
744
652
|
parameterValue = valueToString(parameterValue);
|
|
745
653
|
// Escape curly braces in parameter values to prevent prompt-injection
|
|
@@ -747,17 +655,13 @@ function templateParameters(template, parameters) {
|
|
|
747
655
|
if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
|
|
748
656
|
parameterValue = parameterValue
|
|
749
657
|
.split('\n')
|
|
750
|
-
.map(
|
|
658
|
+
.map((line, index) => (index === 0 ? line : `${precol}${line}`))
|
|
751
659
|
.join('\n');
|
|
752
660
|
}
|
|
753
661
|
replacedTemplates =
|
|
754
662
|
replacedTemplates.substring(0, match.index + precol.length) +
|
|
755
663
|
parameterValue +
|
|
756
664
|
replacedTemplates.substring(match.index + precol.length + parameterName.length + 2);
|
|
757
|
-
};
|
|
758
|
-
while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
|
|
759
|
-
.exec(replacedTemplates))) {
|
|
760
|
-
_loop_1();
|
|
761
665
|
}
|
|
762
666
|
// [💫] Check if there are parameters that are not closed properly
|
|
763
667
|
if (/{\w+$/.test(replacedTemplates)) {
|
|
@@ -791,8 +695,8 @@ function asSerializable(value) {
|
|
|
791
695
|
return value.map(asSerializable);
|
|
792
696
|
}
|
|
793
697
|
else if (value !== null && typeof value === 'object') {
|
|
794
|
-
|
|
795
|
-
for (
|
|
698
|
+
const result = {};
|
|
699
|
+
for (const key in value) {
|
|
796
700
|
if (Object.prototype.hasOwnProperty.call(value, key)) {
|
|
797
701
|
result[key] = asSerializable(value[key]);
|
|
798
702
|
}
|
|
@@ -810,131 +714,119 @@ function asSerializable(value) {
|
|
|
810
714
|
* @public exported from `@promptbook/vercel`
|
|
811
715
|
*/
|
|
812
716
|
function createExecutionToolsFromVercelProvider(options) {
|
|
813
|
-
|
|
814
|
-
|
|
717
|
+
let { title, description } = options;
|
|
718
|
+
const { vercelProvider, availableModels, userId, additionalChatSettings = {} } = options;
|
|
815
719
|
if (!/Vercel/i.test(title)) {
|
|
816
|
-
title =
|
|
720
|
+
title = `${title} (through Vercel)`;
|
|
817
721
|
} /* not else */
|
|
818
722
|
if (description === undefined) {
|
|
819
|
-
description =
|
|
723
|
+
description = `Implementation of ${title} through Vercel`;
|
|
820
724
|
} /* not else */
|
|
821
725
|
if (!/Vercel/i.test(description)) {
|
|
822
|
-
description =
|
|
726
|
+
description = `${description} (through Vercel)`;
|
|
823
727
|
} /* not else */
|
|
824
728
|
return {
|
|
825
|
-
title
|
|
826
|
-
description
|
|
827
|
-
checkConfiguration
|
|
729
|
+
title,
|
|
730
|
+
description,
|
|
731
|
+
checkConfiguration() {
|
|
828
732
|
// Note: There is no way how to check configuration of Vercel provider
|
|
829
733
|
return Promise.resolve();
|
|
830
734
|
},
|
|
831
|
-
listModels
|
|
832
|
-
return
|
|
833
|
-
return __generator(this, function (_a) {
|
|
834
|
-
return [2 /*return*/, availableModels];
|
|
835
|
-
});
|
|
836
|
-
});
|
|
735
|
+
async listModels() {
|
|
736
|
+
return availableModels;
|
|
837
737
|
},
|
|
838
|
-
callChatModel
|
|
738
|
+
async callChatModel(prompt) {
|
|
839
739
|
var _a;
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
rawPromptContent: rawPromptContent,
|
|
931
|
-
rawRequest: rawRequest,
|
|
932
|
-
rawResponse: asSerializable(rawResponse),
|
|
933
|
-
// <- [🗯]
|
|
934
|
-
},
|
|
935
|
-
})];
|
|
936
|
-
}
|
|
937
|
-
});
|
|
740
|
+
const { content, parameters, modelRequirements } = prompt;
|
|
741
|
+
if (modelRequirements.modelVariant !== 'CHAT') {
|
|
742
|
+
throw new PipelineExecutionError('Use callChatModel only for CHAT variant');
|
|
743
|
+
}
|
|
744
|
+
const modelName = modelRequirements.modelName ||
|
|
745
|
+
((_a = availableModels.find(({ modelVariant }) => modelVariant === 'CHAT')) === null || _a === void 0 ? void 0 : _a.modelName);
|
|
746
|
+
if (!modelName) {
|
|
747
|
+
throw new PipelineExecutionError(spaceTrim(`
|
|
748
|
+
Can not determine which model to use.
|
|
749
|
+
|
|
750
|
+
You need to provide at least one of:
|
|
751
|
+
1) In \`createExecutionToolsFromVercelProvider\` options, provide \`availableModels\` with at least one model
|
|
752
|
+
2) In \`prompt.modelRequirements\`, provide \`modelName\` with the name of the model to use
|
|
753
|
+
|
|
754
|
+
`));
|
|
755
|
+
}
|
|
756
|
+
const model = await vercelProvider.chat(modelName, {
|
|
757
|
+
user: (userId === null || userId === void 0 ? void 0 : userId.toString()) || undefined,
|
|
758
|
+
...additionalChatSettings,
|
|
759
|
+
});
|
|
760
|
+
const rawPromptContent = templateParameters(content, { ...parameters, modelName });
|
|
761
|
+
const rawRequest = {
|
|
762
|
+
// <- TODO: [☂]
|
|
763
|
+
inputFormat: 'messages',
|
|
764
|
+
mode: {
|
|
765
|
+
type: 'regular',
|
|
766
|
+
tools: [
|
|
767
|
+
/* <- TODO: Pass the tools */
|
|
768
|
+
],
|
|
769
|
+
},
|
|
770
|
+
prompt: [
|
|
771
|
+
...(modelRequirements.systemMessage === undefined
|
|
772
|
+
? []
|
|
773
|
+
: [
|
|
774
|
+
{
|
|
775
|
+
role: 'system',
|
|
776
|
+
content: modelRequirements.systemMessage,
|
|
777
|
+
},
|
|
778
|
+
]),
|
|
779
|
+
{
|
|
780
|
+
role: 'user',
|
|
781
|
+
content: [
|
|
782
|
+
{
|
|
783
|
+
type: 'text',
|
|
784
|
+
text: rawPromptContent,
|
|
785
|
+
},
|
|
786
|
+
],
|
|
787
|
+
},
|
|
788
|
+
],
|
|
789
|
+
};
|
|
790
|
+
const start = $getCurrentDate();
|
|
791
|
+
if (options.isVerbose) {
|
|
792
|
+
console.info(colors.bgWhite('rawRequest'), JSON.stringify(rawRequest, null, 4));
|
|
793
|
+
}
|
|
794
|
+
const rawResponse = await (async () => await model.doGenerate(rawRequest))().catch((error) => {
|
|
795
|
+
// <- Note: This weird structure is here to catch errors in both sync and async `doGenerate`
|
|
796
|
+
if (options.isVerbose) {
|
|
797
|
+
console.info(colors.bgRed('error'), error);
|
|
798
|
+
}
|
|
799
|
+
throw error;
|
|
800
|
+
});
|
|
801
|
+
await model.doGenerate(rawRequest);
|
|
802
|
+
if (options.isVerbose) {
|
|
803
|
+
console.info(colors.bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
|
|
804
|
+
}
|
|
805
|
+
if (rawResponse.text === undefined) {
|
|
806
|
+
throw new PipelineExecutionError('No response message');
|
|
807
|
+
}
|
|
808
|
+
const complete = $getCurrentDate();
|
|
809
|
+
/*
|
|
810
|
+
TODO: [🕘] Usage count
|
|
811
|
+
const usage = computeOpenAiUsage(content || '', resultContent || '', rawResponse);
|
|
812
|
+
*/
|
|
813
|
+
const usage = UNCERTAIN_USAGE;
|
|
814
|
+
return exportJson({
|
|
815
|
+
name: 'promptResult',
|
|
816
|
+
message: `Result of \`createExecutionToolsFromVercelProvider.callChatModel\``,
|
|
817
|
+
value: {
|
|
818
|
+
content: rawResponse.text,
|
|
819
|
+
modelName,
|
|
820
|
+
timing: {
|
|
821
|
+
start,
|
|
822
|
+
complete,
|
|
823
|
+
},
|
|
824
|
+
usage,
|
|
825
|
+
rawPromptContent,
|
|
826
|
+
rawRequest,
|
|
827
|
+
rawResponse: asSerializable(rawResponse),
|
|
828
|
+
// <- [🗯]
|
|
829
|
+
},
|
|
938
830
|
});
|
|
939
831
|
},
|
|
940
832
|
};
|