@promptbook/vercel 0.79.0 → 0.80.0-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/esm/index.es.js +222 -143
- package/esm/index.es.js.map +1 -1
- package/esm/typings/books/index.d.ts +6 -6
- package/esm/typings/src/_packages/core.index.d.ts +4 -2
- package/esm/typings/src/_packages/types.index.d.ts +10 -0
- package/esm/typings/src/_packages/utils.index.d.ts +4 -0
- package/esm/typings/src/cli/cli-commands/runInteractiveChatbot.d.ts +32 -0
- package/esm/typings/src/commands/_common/types/CommandParser.d.ts +3 -0
- package/esm/typings/src/config.d.ts +0 -25
- package/esm/typings/src/constants.d.ts +35 -0
- package/esm/typings/src/conversion/pipelineJsonToString.d.ts +1 -0
- package/esm/typings/src/conversion/pipelineStringToJsonSync.d.ts +1 -0
- package/esm/typings/src/high-level-abstractions/_common/HighLevelAbstraction.d.ts +20 -0
- package/esm/typings/src/high-level-abstractions/implicit-formfactor/ImplicitFormfactorHla.d.ts +10 -0
- package/esm/typings/src/high-level-abstractions/index.d.ts +44 -0
- package/esm/typings/src/high-level-abstractions/quick-chatbot/QuickChatbotHla.d.ts +10 -0
- package/esm/typings/src/llm-providers/remote/RemoteLlmExecutionTools.d.ts +1 -1
- package/esm/typings/src/llm-providers/remote/startRemoteServer.d.ts +1 -1
- package/esm/typings/src/prepare/prepareTasks.d.ts +1 -0
- package/esm/typings/src/types/typeAliases.d.ts +1 -1
- package/esm/typings/src/utils/normalization/orderJson.d.ts +21 -0
- package/esm/typings/src/utils/normalization/orderJson.test.d.ts +4 -0
- package/esm/typings/src/utils/organization/keepTypeImported.d.ts +9 -0
- package/esm/typings/src/utils/serialization/$deepFreeze.d.ts +1 -1
- package/esm/typings/src/utils/serialization/checkSerializableAsJson.d.ts +20 -2
- package/esm/typings/src/utils/serialization/deepClone.test.d.ts +1 -0
- package/esm/typings/src/utils/serialization/exportJson.d.ts +29 -0
- package/esm/typings/src/utils/serialization/isSerializableAsJson.d.ts +2 -1
- package/package.json +2 -2
- package/umd/index.umd.js +222 -143
- package/umd/index.umd.js.map +1 -1
- package/esm/typings/src/utils/serialization/$asDeeplyFrozenSerializableJson.d.ts +0 -17
package/README.md
CHANGED
|
@@ -23,6 +23,10 @@
|
|
|
23
23
|
|
|
24
24
|
|
|
25
25
|
|
|
26
|
+
<blockquote style="color: #ff8811">
|
|
27
|
+
<b>⚠ Warning:</b> This is a pre-release version of the library. It is not yet ready for production use. Please look at <a href="https://www.npmjs.com/package/@promptbook/core?activeTab=versions">latest stable release</a>.
|
|
28
|
+
</blockquote>
|
|
29
|
+
|
|
26
30
|
## 📦 Package `@promptbook/vercel`
|
|
27
31
|
|
|
28
32
|
- Promptbooks are [divided into several](#-packages) packages, all are published from [single monorepo](https://github.com/webgptorg/promptbook).
|
package/esm/index.es.js
CHANGED
|
@@ -13,7 +13,7 @@ var BOOK_LANGUAGE_VERSION = '1.0.0';
|
|
|
13
13
|
*
|
|
14
14
|
* @see https://github.com/webgptorg/promptbook
|
|
15
15
|
*/
|
|
16
|
-
var PROMPTBOOK_ENGINE_VERSION = '0.
|
|
16
|
+
var PROMPTBOOK_ENGINE_VERSION = '0.79.0';
|
|
17
17
|
/**
|
|
18
18
|
* TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
|
|
19
19
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
@@ -155,7 +155,7 @@ var PipelineExecutionError = /** @class */ (function (_super) {
|
|
|
155
155
|
}(Error));
|
|
156
156
|
|
|
157
157
|
/**
|
|
158
|
-
*
|
|
158
|
+
* Freezes the given object and all its nested objects recursively
|
|
159
159
|
*
|
|
160
160
|
* Note: `$` is used to indicate that this function is not a pure function - it mutates given object
|
|
161
161
|
* Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
|
|
@@ -185,7 +185,8 @@ function $deepFreeze(objectValue) {
|
|
|
185
185
|
}
|
|
186
186
|
finally { if (e_1) throw e_1.error; }
|
|
187
187
|
}
|
|
188
|
-
|
|
188
|
+
Object.freeze(objectValue);
|
|
189
|
+
return objectValue;
|
|
189
190
|
}
|
|
190
191
|
/**
|
|
191
192
|
* TODO: [🧠] Is there a way how to meaningfully test this utility
|
|
@@ -286,24 +287,6 @@ var ADMIN_GITHUB_NAME = 'hejny';
|
|
|
286
287
|
* @private within the repository - too low-level in comparison with other `MAX_...`
|
|
287
288
|
*/
|
|
288
289
|
var LOOP_LIMIT = 1000;
|
|
289
|
-
/**
|
|
290
|
-
* Nonce which is used for replacing things in strings
|
|
291
|
-
*
|
|
292
|
-
* @private within the repository
|
|
293
|
-
*/
|
|
294
|
-
var REPLACING_NONCE = 'u$k42k%!V2zo34w7Fu#@QUHYPW';
|
|
295
|
-
/**
|
|
296
|
-
* @@@
|
|
297
|
-
*
|
|
298
|
-
* @private within the repository
|
|
299
|
-
*/
|
|
300
|
-
var RESERVED_PARAMETER_MISSING_VALUE = 'MISSING-' + REPLACING_NONCE;
|
|
301
|
-
/**
|
|
302
|
-
* @@@
|
|
303
|
-
*
|
|
304
|
-
* @private within the repository
|
|
305
|
-
*/
|
|
306
|
-
var RESERVED_PARAMETER_RESTRICTED = 'RESTRICTED-' + REPLACING_NONCE;
|
|
307
290
|
// <- TODO: [🧜♂️]
|
|
308
291
|
/**
|
|
309
292
|
* @@@
|
|
@@ -317,26 +300,21 @@ Object.freeze({
|
|
|
317
300
|
skipEmptyLines: true,
|
|
318
301
|
});
|
|
319
302
|
/**
|
|
320
|
-
* TODO: Extract `constants.ts` from `config.ts`
|
|
321
303
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
322
304
|
* TODO: [🧠][🧜♂️] Maybe join remoteUrl and path into single value
|
|
323
305
|
*/
|
|
324
306
|
|
|
325
307
|
/**
|
|
326
|
-
*
|
|
308
|
+
* Orders JSON object by keys
|
|
327
309
|
*
|
|
328
|
-
* @
|
|
310
|
+
* @returns The same type of object as the input re-ordered
|
|
311
|
+
* @public exported from `@promptbook/utils`
|
|
329
312
|
*/
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
function
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
Object.setPrototypeOf(_this, LimitReachedError.prototype);
|
|
336
|
-
return _this;
|
|
337
|
-
}
|
|
338
|
-
return LimitReachedError;
|
|
339
|
-
}(Error));
|
|
313
|
+
function orderJson(options) {
|
|
314
|
+
var value = options.value, order = options.order;
|
|
315
|
+
var orderedValue = __assign(__assign({}, (order === undefined ? {} : Object.fromEntries(order.map(function (key) { return [key, undefined]; })))), value);
|
|
316
|
+
return orderedValue;
|
|
317
|
+
}
|
|
340
318
|
|
|
341
319
|
/**
|
|
342
320
|
* Make error report URL for the given error
|
|
@@ -372,85 +350,6 @@ var UnexpectedError = /** @class */ (function (_super) {
|
|
|
372
350
|
return UnexpectedError;
|
|
373
351
|
}(Error));
|
|
374
352
|
|
|
375
|
-
/**
|
|
376
|
-
* Replaces parameters in template with values from parameters object
|
|
377
|
-
*
|
|
378
|
-
* @param template the template with parameters in {curly} braces
|
|
379
|
-
* @param parameters the object with parameters
|
|
380
|
-
* @returns the template with replaced parameters
|
|
381
|
-
* @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
|
|
382
|
-
* @public exported from `@promptbook/utils`
|
|
383
|
-
*/
|
|
384
|
-
function replaceParameters(template, parameters) {
|
|
385
|
-
var e_1, _a;
|
|
386
|
-
try {
|
|
387
|
-
for (var _b = __values(Object.entries(parameters)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
388
|
-
var _d = __read(_c.value, 2), parameterName = _d[0], parameterValue = _d[1];
|
|
389
|
-
if (parameterValue === RESERVED_PARAMETER_MISSING_VALUE) {
|
|
390
|
-
throw new UnexpectedError("Parameter `{".concat(parameterName, "}` has missing value"));
|
|
391
|
-
}
|
|
392
|
-
else if (parameterValue === RESERVED_PARAMETER_RESTRICTED) {
|
|
393
|
-
// TODO: [🍵]
|
|
394
|
-
throw new UnexpectedError("Parameter `{".concat(parameterName, "}` is restricted to use"));
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
399
|
-
finally {
|
|
400
|
-
try {
|
|
401
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
402
|
-
}
|
|
403
|
-
finally { if (e_1) throw e_1.error; }
|
|
404
|
-
}
|
|
405
|
-
var replacedTemplates = template;
|
|
406
|
-
var match;
|
|
407
|
-
var loopLimit = LOOP_LIMIT;
|
|
408
|
-
var _loop_1 = function () {
|
|
409
|
-
if (loopLimit-- < 0) {
|
|
410
|
-
throw new LimitReachedError('Loop limit reached during parameters replacement in `replaceParameters`');
|
|
411
|
-
}
|
|
412
|
-
var precol = match.groups.precol;
|
|
413
|
-
var parameterName = match.groups.parameterName;
|
|
414
|
-
if (parameterName === '') {
|
|
415
|
-
return "continue";
|
|
416
|
-
}
|
|
417
|
-
if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
|
|
418
|
-
throw new PipelineExecutionError('Parameter is already opened or not closed');
|
|
419
|
-
}
|
|
420
|
-
if (parameters[parameterName] === undefined) {
|
|
421
|
-
throw new PipelineExecutionError("Parameter `{".concat(parameterName, "}` is not defined"));
|
|
422
|
-
}
|
|
423
|
-
var parameterValue = parameters[parameterName];
|
|
424
|
-
if (parameterValue === undefined) {
|
|
425
|
-
throw new PipelineExecutionError("Parameter `{".concat(parameterName, "}` is not defined"));
|
|
426
|
-
}
|
|
427
|
-
parameterValue = parameterValue.toString();
|
|
428
|
-
if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
|
|
429
|
-
parameterValue = parameterValue
|
|
430
|
-
.split('\n')
|
|
431
|
-
.map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
|
|
432
|
-
.join('\n');
|
|
433
|
-
}
|
|
434
|
-
replacedTemplates =
|
|
435
|
-
replacedTemplates.substring(0, match.index + precol.length) +
|
|
436
|
-
parameterValue +
|
|
437
|
-
replacedTemplates.substring(match.index + precol.length + parameterName.length + 2);
|
|
438
|
-
};
|
|
439
|
-
while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
|
|
440
|
-
.exec(replacedTemplates))) {
|
|
441
|
-
_loop_1();
|
|
442
|
-
}
|
|
443
|
-
// [💫] Check if there are parameters that are not closed properly
|
|
444
|
-
if (/{\w+$/.test(replacedTemplates)) {
|
|
445
|
-
throw new PipelineExecutionError('Parameter is not closed');
|
|
446
|
-
}
|
|
447
|
-
// [💫] Check if there are parameters that are not opened properly
|
|
448
|
-
if (/^\w+}/.test(replacedTemplates)) {
|
|
449
|
-
throw new PipelineExecutionError('Parameter is not opened');
|
|
450
|
-
}
|
|
451
|
-
return replacedTemplates;
|
|
452
|
-
}
|
|
453
|
-
|
|
454
353
|
/**
|
|
455
354
|
* Checks if the value is [🚉] serializable as JSON
|
|
456
355
|
* If not, throws an UnexpectedError with a rich error message and tracking
|
|
@@ -471,8 +370,9 @@ function replaceParameters(template, parameters) {
|
|
|
471
370
|
* @throws UnexpectedError if the value is not serializable as JSON
|
|
472
371
|
* @public exported from `@promptbook/utils`
|
|
473
372
|
*/
|
|
474
|
-
function checkSerializableAsJson(
|
|
373
|
+
function checkSerializableAsJson(options) {
|
|
475
374
|
var e_1, _a;
|
|
375
|
+
var value = options.value, name = options.name, message = options.message;
|
|
476
376
|
if (value === undefined) {
|
|
477
377
|
throw new UnexpectedError("".concat(name, " is undefined"));
|
|
478
378
|
}
|
|
@@ -496,12 +396,12 @@ function checkSerializableAsJson(name, value) {
|
|
|
496
396
|
}
|
|
497
397
|
else if (typeof value === 'object' && Array.isArray(value)) {
|
|
498
398
|
for (var i = 0; i < value.length; i++) {
|
|
499
|
-
checkSerializableAsJson("".concat(name, "[").concat(i, "]"), value[i]);
|
|
399
|
+
checkSerializableAsJson({ name: "".concat(name, "[").concat(i, "]"), value: value[i], message: message });
|
|
500
400
|
}
|
|
501
401
|
}
|
|
502
402
|
else if (typeof value === 'object') {
|
|
503
403
|
if (value instanceof Date) {
|
|
504
|
-
throw new UnexpectedError(spaceTrim("\n
|
|
404
|
+
throw new UnexpectedError(spaceTrim(function (block) { return "\n `".concat(name, "` is Date\n\n Use `string_date_iso8601` instead\n\n Additional message for `").concat(name, "`:\n ").concat(block(message || '(nothing)'), "\n "); }));
|
|
505
405
|
}
|
|
506
406
|
else if (value instanceof Map) {
|
|
507
407
|
throw new UnexpectedError("".concat(name, " is Map"));
|
|
@@ -513,7 +413,7 @@ function checkSerializableAsJson(name, value) {
|
|
|
513
413
|
throw new UnexpectedError("".concat(name, " is RegExp"));
|
|
514
414
|
}
|
|
515
415
|
else if (value instanceof Error) {
|
|
516
|
-
throw new UnexpectedError(spaceTrim("\n
|
|
416
|
+
throw new UnexpectedError(spaceTrim(function (block) { return "\n `".concat(name, "` is unserialized Error\n\n Use function `serializeError`\n\n Additional message for `").concat(name, "`:\n ").concat(block(message || '(nothing)'), "\n\n "); }));
|
|
517
417
|
}
|
|
518
418
|
else {
|
|
519
419
|
try {
|
|
@@ -523,7 +423,7 @@ function checkSerializableAsJson(name, value) {
|
|
|
523
423
|
// Note: undefined in object is serializable - it is just omited
|
|
524
424
|
continue;
|
|
525
425
|
}
|
|
526
|
-
checkSerializableAsJson("".concat(name, ".").concat(subName), subValue);
|
|
426
|
+
checkSerializableAsJson({ name: "".concat(name, ".").concat(subName), value: subValue, message: message });
|
|
527
427
|
}
|
|
528
428
|
}
|
|
529
429
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
@@ -540,7 +440,7 @@ function checkSerializableAsJson(name, value) {
|
|
|
540
440
|
if (!(error instanceof Error)) {
|
|
541
441
|
throw error;
|
|
542
442
|
}
|
|
543
|
-
throw new UnexpectedError(spaceTrim(function (block) { return "\n ".concat(name, " is not serializable\n\n ").concat(block(error.toString()), "\n "); }));
|
|
443
|
+
throw new UnexpectedError(spaceTrim(function (block) { return "\n `".concat(name, "` is not serializable\n\n ").concat(block(error.toString()), "\n\n Additional message for `").concat(name, "`:\n ").concat(block(message || '(nothing)'), "\n "); }));
|
|
544
444
|
}
|
|
545
445
|
/*
|
|
546
446
|
TODO: [0] Is there some more elegant way to check circular references?
|
|
@@ -565,35 +465,210 @@ function checkSerializableAsJson(name, value) {
|
|
|
565
465
|
}
|
|
566
466
|
}
|
|
567
467
|
else {
|
|
568
|
-
throw new UnexpectedError("".concat(name, " is unknown"));
|
|
468
|
+
throw new UnexpectedError(spaceTrim(function (block) { return "\n `".concat(name, "` is unknown type\n\n Additional message for `").concat(name, "`:\n ").concat(block(message || '(nothing)'), "\n "); }));
|
|
569
469
|
}
|
|
570
470
|
}
|
|
571
471
|
/**
|
|
572
|
-
* TODO:
|
|
472
|
+
* TODO: Can be return type more type-safe? like `asserts options.value is JsonValue`
|
|
573
473
|
* TODO: [🧠][main] !!! In-memory cache of same values to prevent multiple checks
|
|
574
474
|
* Note: [🐠] This is how `checkSerializableAsJson` + `isSerializableAsJson` together can just retun true/false or rich error message
|
|
575
475
|
*/
|
|
576
476
|
|
|
577
477
|
/**
|
|
578
|
-
* @@@
|
|
579
478
|
* @@@
|
|
580
479
|
*
|
|
581
|
-
*
|
|
480
|
+
* @public exported from `@promptbook/utils`
|
|
481
|
+
*/
|
|
482
|
+
function deepClone(objectValue) {
|
|
483
|
+
return JSON.parse(JSON.stringify(objectValue));
|
|
484
|
+
/*
|
|
485
|
+
!!!!!!!!
|
|
486
|
+
TODO: [🧠] Is there a better implementation?
|
|
487
|
+
> const propertyNames = Object.getOwnPropertyNames(objectValue);
|
|
488
|
+
> for (const propertyName of propertyNames) {
|
|
489
|
+
> const value = (objectValue as really_any)[propertyName];
|
|
490
|
+
> if (value && typeof value === 'object') {
|
|
491
|
+
> deepClone(value);
|
|
492
|
+
> }
|
|
493
|
+
> }
|
|
494
|
+
> return Object.assign({}, objectValue);
|
|
495
|
+
*/
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* TODO: [🧠] Is there a way how to meaningfully test this utility
|
|
499
|
+
*/
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* Utility to export a JSON object from a function
|
|
582
503
|
*
|
|
583
|
-
*
|
|
584
|
-
*
|
|
585
|
-
*
|
|
586
|
-
*
|
|
504
|
+
* 1) Checks if the value is serializable as JSON
|
|
505
|
+
* 2) Makes a deep clone of the object
|
|
506
|
+
* 2) Orders the object properties
|
|
507
|
+
* 2) Deeply freezes the cloned object
|
|
508
|
+
*
|
|
509
|
+
* Note: This function does not mutates the given object
|
|
510
|
+
*
|
|
511
|
+
* @returns The same type of object as the input but read-only and re-ordered
|
|
512
|
+
* @public exported from `@promptbook/utils`
|
|
587
513
|
*/
|
|
588
|
-
function
|
|
589
|
-
|
|
590
|
-
|
|
514
|
+
function exportJson(options) {
|
|
515
|
+
var name = options.name, value = options.value, order = options.order, message = options.message;
|
|
516
|
+
checkSerializableAsJson({ name: name, value: value, message: message });
|
|
517
|
+
var orderedValue =
|
|
518
|
+
// TODO: Fix error "Type instantiation is excessively deep and possibly infinite."
|
|
519
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
520
|
+
// @ts-ignore
|
|
521
|
+
order === undefined
|
|
522
|
+
? deepClone(value)
|
|
523
|
+
: orderJson({
|
|
524
|
+
value: value,
|
|
525
|
+
// <- Note: checkSerializableAsJson asserts that the value is serializable as JSON
|
|
526
|
+
order: order,
|
|
527
|
+
});
|
|
528
|
+
$deepFreeze(orderedValue);
|
|
529
|
+
return orderedValue;
|
|
591
530
|
}
|
|
592
531
|
/**
|
|
593
|
-
* TODO: [🧠][🛣] More elegant way to tracking than passing `name`
|
|
594
532
|
* TODO: [🧠] Is there a way how to meaningfully test this utility
|
|
595
533
|
*/
|
|
596
534
|
|
|
535
|
+
/**
|
|
536
|
+
* Nonce which is used for replacing things in strings
|
|
537
|
+
*
|
|
538
|
+
* @private within the repository
|
|
539
|
+
*/
|
|
540
|
+
var REPLACING_NONCE = 'u$k42k%!V2zo34w7Fu#@QUHYPW';
|
|
541
|
+
/**
|
|
542
|
+
* @@@
|
|
543
|
+
*
|
|
544
|
+
* @private within the repository
|
|
545
|
+
*/
|
|
546
|
+
var RESERVED_PARAMETER_MISSING_VALUE = 'MISSING-' + REPLACING_NONCE;
|
|
547
|
+
/**
|
|
548
|
+
* @@@
|
|
549
|
+
*
|
|
550
|
+
* @private within the repository
|
|
551
|
+
*/
|
|
552
|
+
var RESERVED_PARAMETER_RESTRICTED = 'RESTRICTED-' + REPLACING_NONCE;
|
|
553
|
+
/**
|
|
554
|
+
* The names of the parameters that are reserved for special purposes
|
|
555
|
+
*
|
|
556
|
+
* @public exported from `@promptbook/core`
|
|
557
|
+
*/
|
|
558
|
+
exportJson({
|
|
559
|
+
name: 'RESERVED_PARAMETER_NAMES',
|
|
560
|
+
message: "The names of the parameters that are reserved for special purposes",
|
|
561
|
+
value: [
|
|
562
|
+
'content',
|
|
563
|
+
'context',
|
|
564
|
+
'knowledge',
|
|
565
|
+
'examples',
|
|
566
|
+
'modelName',
|
|
567
|
+
'currentDate',
|
|
568
|
+
// <- TODO: list here all command names
|
|
569
|
+
// <- TODO: Add more like 'date', 'modelName',...
|
|
570
|
+
// <- TODO: Add [emoji] + instructions ACRY when adding new reserved parameter
|
|
571
|
+
],
|
|
572
|
+
});
|
|
573
|
+
/**
|
|
574
|
+
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
575
|
+
*/
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* This error type indicates that some limit was reached
|
|
579
|
+
*
|
|
580
|
+
* @public exported from `@promptbook/core`
|
|
581
|
+
*/
|
|
582
|
+
var LimitReachedError = /** @class */ (function (_super) {
|
|
583
|
+
__extends(LimitReachedError, _super);
|
|
584
|
+
function LimitReachedError(message) {
|
|
585
|
+
var _this = _super.call(this, message) || this;
|
|
586
|
+
_this.name = 'LimitReachedError';
|
|
587
|
+
Object.setPrototypeOf(_this, LimitReachedError.prototype);
|
|
588
|
+
return _this;
|
|
589
|
+
}
|
|
590
|
+
return LimitReachedError;
|
|
591
|
+
}(Error));
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* Replaces parameters in template with values from parameters object
|
|
595
|
+
*
|
|
596
|
+
* @param template the template with parameters in {curly} braces
|
|
597
|
+
* @param parameters the object with parameters
|
|
598
|
+
* @returns the template with replaced parameters
|
|
599
|
+
* @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
|
|
600
|
+
* @public exported from `@promptbook/utils`
|
|
601
|
+
*/
|
|
602
|
+
function replaceParameters(template, parameters) {
|
|
603
|
+
var e_1, _a;
|
|
604
|
+
try {
|
|
605
|
+
for (var _b = __values(Object.entries(parameters)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
606
|
+
var _d = __read(_c.value, 2), parameterName = _d[0], parameterValue = _d[1];
|
|
607
|
+
if (parameterValue === RESERVED_PARAMETER_MISSING_VALUE) {
|
|
608
|
+
throw new UnexpectedError("Parameter `{".concat(parameterName, "}` has missing value"));
|
|
609
|
+
}
|
|
610
|
+
else if (parameterValue === RESERVED_PARAMETER_RESTRICTED) {
|
|
611
|
+
// TODO: [🍵]
|
|
612
|
+
throw new UnexpectedError("Parameter `{".concat(parameterName, "}` is restricted to use"));
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
617
|
+
finally {
|
|
618
|
+
try {
|
|
619
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
620
|
+
}
|
|
621
|
+
finally { if (e_1) throw e_1.error; }
|
|
622
|
+
}
|
|
623
|
+
var replacedTemplates = template;
|
|
624
|
+
var match;
|
|
625
|
+
var loopLimit = LOOP_LIMIT;
|
|
626
|
+
var _loop_1 = function () {
|
|
627
|
+
if (loopLimit-- < 0) {
|
|
628
|
+
throw new LimitReachedError('Loop limit reached during parameters replacement in `replaceParameters`');
|
|
629
|
+
}
|
|
630
|
+
var precol = match.groups.precol;
|
|
631
|
+
var parameterName = match.groups.parameterName;
|
|
632
|
+
if (parameterName === '') {
|
|
633
|
+
return "continue";
|
|
634
|
+
}
|
|
635
|
+
if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
|
|
636
|
+
throw new PipelineExecutionError('Parameter is already opened or not closed');
|
|
637
|
+
}
|
|
638
|
+
if (parameters[parameterName] === undefined) {
|
|
639
|
+
throw new PipelineExecutionError("Parameter `{".concat(parameterName, "}` is not defined"));
|
|
640
|
+
}
|
|
641
|
+
var parameterValue = parameters[parameterName];
|
|
642
|
+
if (parameterValue === undefined) {
|
|
643
|
+
throw new PipelineExecutionError("Parameter `{".concat(parameterName, "}` is not defined"));
|
|
644
|
+
}
|
|
645
|
+
parameterValue = parameterValue.toString();
|
|
646
|
+
if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
|
|
647
|
+
parameterValue = parameterValue
|
|
648
|
+
.split('\n')
|
|
649
|
+
.map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
|
|
650
|
+
.join('\n');
|
|
651
|
+
}
|
|
652
|
+
replacedTemplates =
|
|
653
|
+
replacedTemplates.substring(0, match.index + precol.length) +
|
|
654
|
+
parameterValue +
|
|
655
|
+
replacedTemplates.substring(match.index + precol.length + parameterName.length + 2);
|
|
656
|
+
};
|
|
657
|
+
while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
|
|
658
|
+
.exec(replacedTemplates))) {
|
|
659
|
+
_loop_1();
|
|
660
|
+
}
|
|
661
|
+
// [💫] Check if there are parameters that are not closed properly
|
|
662
|
+
if (/{\w+$/.test(replacedTemplates)) {
|
|
663
|
+
throw new PipelineExecutionError('Parameter is not closed');
|
|
664
|
+
}
|
|
665
|
+
// [💫] Check if there are parameters that are not opened properly
|
|
666
|
+
if (/^\w+}/.test(replacedTemplates)) {
|
|
667
|
+
throw new PipelineExecutionError('Parameter is not opened');
|
|
668
|
+
}
|
|
669
|
+
return replacedTemplates;
|
|
670
|
+
}
|
|
671
|
+
|
|
597
672
|
/**
|
|
598
673
|
* Function `asSerializable` will convert values which are not serializable to serializable values
|
|
599
674
|
* It walks deeply through the object and converts all values
|
|
@@ -673,7 +748,7 @@ function createExecutionToolsFromVercelProvider(options) {
|
|
|
673
748
|
return modelVariant === 'CHAT';
|
|
674
749
|
})) === null || _a === void 0 ? void 0 : _a.modelName);
|
|
675
750
|
if (!modelName) {
|
|
676
|
-
throw new PipelineExecutionError(spaceTrim("\n Can not determine which model to use.\n\n You need to provide at least one of:\n 1) In `createExecutionToolsFromVercelProvider` options, provide `availableModels` with at least one model\n 2) In `prompt.modelRequirements`, provide `modelName` with the name of the model to use\n
|
|
751
|
+
throw new PipelineExecutionError(spaceTrim("\n Can not determine which model to use.\n\n You need to provide at least one of:\n 1) In `createExecutionToolsFromVercelProvider` options, provide `availableModels` with at least one model\n 2) In `prompt.modelRequirements`, provide `modelName` with the name of the model to use\n\n "));
|
|
677
752
|
}
|
|
678
753
|
return [4 /*yield*/, vercelProvider.chat(modelName, __assign({ user: (userId === null || userId === void 0 ? void 0 : userId.toString()) || undefined }, additionalChatSettings))];
|
|
679
754
|
case 1:
|
|
@@ -736,18 +811,22 @@ function createExecutionToolsFromVercelProvider(options) {
|
|
|
736
811
|
}
|
|
737
812
|
complete = $getCurrentDate();
|
|
738
813
|
usage = UNCERTAIN_USAGE;
|
|
739
|
-
return [2 /*return*/,
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
814
|
+
return [2 /*return*/, exportJson({
|
|
815
|
+
name: 'promptResult',
|
|
816
|
+
message: "Result of `createExecutionToolsFromVercelProvider.callChatModel`",
|
|
817
|
+
value: {
|
|
818
|
+
content: rawResponse.text,
|
|
819
|
+
modelName: modelName,
|
|
820
|
+
timing: {
|
|
821
|
+
start: start,
|
|
822
|
+
complete: complete,
|
|
823
|
+
},
|
|
824
|
+
usage: usage,
|
|
825
|
+
rawPromptContent: rawPromptContent,
|
|
826
|
+
rawRequest: rawRequest,
|
|
827
|
+
rawResponse: asSerializable(rawResponse),
|
|
828
|
+
// <- [🗯]
|
|
745
829
|
},
|
|
746
|
-
usage: usage,
|
|
747
|
-
rawPromptContent: rawPromptContent,
|
|
748
|
-
rawRequest: rawRequest,
|
|
749
|
-
rawResponse: asSerializable(rawResponse),
|
|
750
|
-
// <- [🗯]
|
|
751
830
|
})];
|
|
752
831
|
}
|
|
753
832
|
});
|