@promptbook/vercel 0.76.0 → 0.77.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 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
@@ -1,3 +1,6 @@
1
+ import colors from 'colors';
2
+ import spaceTrim$1, { spaceTrim } from 'spacetrim';
3
+
1
4
  // ⚠️ WARNING: This code has been generated so that any manual changes will be overwritten
2
5
  /**
3
6
  * The version of the Book language
@@ -10,11 +13,677 @@ var BOOK_LANGUAGE_VERSION = '1.0.0';
10
13
  *
11
14
  * @see https://github.com/webgptorg/promptbook
12
15
  */
13
- var PROMPTBOOK_ENGINE_VERSION = '0.75.10';
16
+ var PROMPTBOOK_ENGINE_VERSION = '0.76.0';
14
17
  /**
15
18
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
16
19
  * Note: [💞] Ignore a discrepancy between file name and entity name
17
20
  */
18
21
 
19
- export { BOOK_LANGUAGE_VERSION, PROMPTBOOK_ENGINE_VERSION };
22
+ /*! *****************************************************************************
23
+ Copyright (c) Microsoft Corporation.
24
+
25
+ Permission to use, copy, modify, and/or distribute this software for any
26
+ purpose with or without fee is hereby granted.
27
+
28
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
29
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
30
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
31
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
32
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
33
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
34
+ PERFORMANCE OF THIS SOFTWARE.
35
+ ***************************************************************************** */
36
+ /* global Reflect, Promise */
37
+
38
+ var extendStatics = function(d, b) {
39
+ extendStatics = Object.setPrototypeOf ||
40
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
41
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
42
+ return extendStatics(d, b);
43
+ };
44
+
45
+ function __extends(d, b) {
46
+ if (typeof b !== "function" && b !== null)
47
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
48
+ extendStatics(d, b);
49
+ function __() { this.constructor = d; }
50
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
51
+ }
52
+
53
+ var __assign = function() {
54
+ __assign = Object.assign || function __assign(t) {
55
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
56
+ s = arguments[i];
57
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
58
+ }
59
+ return t;
60
+ };
61
+ return __assign.apply(this, arguments);
62
+ };
63
+
64
+ function __awaiter(thisArg, _arguments, P, generator) {
65
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
66
+ return new (P || (P = Promise))(function (resolve, reject) {
67
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
68
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
69
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
70
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
71
+ });
72
+ }
73
+
74
+ function __generator(thisArg, body) {
75
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
76
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
77
+ function verb(n) { return function (v) { return step([n, v]); }; }
78
+ function step(op) {
79
+ if (f) throw new TypeError("Generator is already executing.");
80
+ while (_) try {
81
+ 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;
82
+ if (y = 0, t) op = [op[0] & 2, t.value];
83
+ switch (op[0]) {
84
+ case 0: case 1: t = op; break;
85
+ case 4: _.label++; return { value: op[1], done: false };
86
+ case 5: _.label++; y = op[1]; op = [0]; continue;
87
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
88
+ default:
89
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
90
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
91
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
92
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
93
+ if (t[2]) _.ops.pop();
94
+ _.trys.pop(); continue;
95
+ }
96
+ op = body.call(thisArg, _);
97
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
98
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
99
+ }
100
+ }
101
+
102
+ function __values(o) {
103
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
104
+ if (m) return m.call(o);
105
+ if (o && typeof o.length === "number") return {
106
+ next: function () {
107
+ if (o && i >= o.length) o = void 0;
108
+ return { value: o && o[i++], done: !o };
109
+ }
110
+ };
111
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
112
+ }
113
+
114
+ function __read(o, n) {
115
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
116
+ if (!m) return o;
117
+ var i = m.call(o), r, ar = [], e;
118
+ try {
119
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
120
+ }
121
+ catch (error) { e = { error: error }; }
122
+ finally {
123
+ try {
124
+ if (r && !r.done && (m = i["return"])) m.call(i);
125
+ }
126
+ finally { if (e) throw e.error; }
127
+ }
128
+ return ar;
129
+ }
130
+
131
+ function __spreadArray(to, from, pack) {
132
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
133
+ if (ar || !(i in from)) {
134
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
135
+ ar[i] = from[i];
136
+ }
137
+ }
138
+ return to.concat(ar || Array.prototype.slice.call(from));
139
+ }
140
+
141
+ /**
142
+ * @@@
143
+ *
144
+ * Note: `$` is used to indicate that this function is not a pure function - it mutates given object
145
+ * Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
146
+ *
147
+ * @returns The same object as the input, but deeply frozen
148
+ * @public exported from `@promptbook/utils`
149
+ */
150
+ function $deepFreeze(objectValue) {
151
+ var e_1, _a;
152
+ var propertyNames = Object.getOwnPropertyNames(objectValue);
153
+ try {
154
+ for (var propertyNames_1 = __values(propertyNames), propertyNames_1_1 = propertyNames_1.next(); !propertyNames_1_1.done; propertyNames_1_1 = propertyNames_1.next()) {
155
+ var propertyName = propertyNames_1_1.value;
156
+ var value = objectValue[propertyName];
157
+ if (value && typeof value === 'object') {
158
+ $deepFreeze(value);
159
+ }
160
+ }
161
+ }
162
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
163
+ finally {
164
+ try {
165
+ if (propertyNames_1_1 && !propertyNames_1_1.done && (_a = propertyNames_1.return)) _a.call(propertyNames_1);
166
+ }
167
+ finally { if (e_1) throw e_1.error; }
168
+ }
169
+ return Object.freeze(objectValue);
170
+ }
171
+ /**
172
+ * TODO: [🧠] Is there a way how to meaningfully test this utility
173
+ */
174
+
175
+ /**
176
+ * Represents the usage with no resources consumed
177
+ *
178
+ * @public exported from `@promptbook/core`
179
+ */
180
+ $deepFreeze({
181
+ price: { value: 0 },
182
+ input: {
183
+ tokensCount: { value: 0 },
184
+ charactersCount: { value: 0 },
185
+ wordsCount: { value: 0 },
186
+ sentencesCount: { value: 0 },
187
+ linesCount: { value: 0 },
188
+ paragraphsCount: { value: 0 },
189
+ pagesCount: { value: 0 },
190
+ },
191
+ output: {
192
+ tokensCount: { value: 0 },
193
+ charactersCount: { value: 0 },
194
+ wordsCount: { value: 0 },
195
+ sentencesCount: { value: 0 },
196
+ linesCount: { value: 0 },
197
+ paragraphsCount: { value: 0 },
198
+ pagesCount: { value: 0 },
199
+ },
200
+ });
201
+ /**
202
+ * Represents the usage with unknown resources consumed
203
+ *
204
+ * @public exported from `@promptbook/core`
205
+ */
206
+ var UNCERTAIN_USAGE = $deepFreeze({
207
+ price: { value: 0, isUncertain: true },
208
+ input: {
209
+ tokensCount: { value: 0, isUncertain: true },
210
+ charactersCount: { value: 0, isUncertain: true },
211
+ wordsCount: { value: 0, isUncertain: true },
212
+ sentencesCount: { value: 0, isUncertain: true },
213
+ linesCount: { value: 0, isUncertain: true },
214
+ paragraphsCount: { value: 0, isUncertain: true },
215
+ pagesCount: { value: 0, isUncertain: true },
216
+ },
217
+ output: {
218
+ tokensCount: { value: 0, isUncertain: true },
219
+ charactersCount: { value: 0, isUncertain: true },
220
+ wordsCount: { value: 0, isUncertain: true },
221
+ sentencesCount: { value: 0, isUncertain: true },
222
+ linesCount: { value: 0, isUncertain: true },
223
+ paragraphsCount: { value: 0, isUncertain: true },
224
+ pagesCount: { value: 0, isUncertain: true },
225
+ },
226
+ });
227
+ /**
228
+ * Note: [💞] Ignore a discrepancy between file name and entity name
229
+ */
230
+
231
+ /**
232
+ * This error type indicates that the error should not happen and its last check before crashing with some other error
233
+ *
234
+ * @public exported from `@promptbook/core`
235
+ */
236
+ var UnexpectedError = /** @class */ (function (_super) {
237
+ __extends(UnexpectedError, _super);
238
+ function UnexpectedError(message) {
239
+ var _this = _super.call(this, spaceTrim(function (block) { return "\n ".concat(block(message), "\n\n Note: This error should not happen.\n It's probbably a bug in the pipeline collection\n\n Please report issue:\n https://github.com/webgptorg/promptbook/issues\n\n Or contact us on me@pavolhejny.com\n\n "); })) || this;
240
+ _this.name = 'UnexpectedError';
241
+ Object.setPrototypeOf(_this, UnexpectedError.prototype);
242
+ return _this;
243
+ }
244
+ return UnexpectedError;
245
+ }(Error));
246
+
247
+ /**
248
+ * Checks if the value is [🚉] serializable as JSON
249
+ * If not, throws an UnexpectedError with a rich error message and tracking
250
+ *
251
+ * - Almost all primitives are serializable BUT:
252
+ * - `undefined` is not serializable
253
+ * - `NaN` is not serializable
254
+ * - Objects and arrays are serializable if all their properties are serializable
255
+ * - Functions are not serializable
256
+ * - Circular references are not serializable
257
+ * - `Date` objects are not serializable
258
+ * - `Map` and `Set` objects are not serializable
259
+ * - `RegExp` objects are not serializable
260
+ * - `Error` objects are not serializable
261
+ * - `Symbol` objects are not serializable
262
+ * - And much more...
263
+ *
264
+ * @throws UnexpectedError if the value is not serializable as JSON
265
+ * @public exported from `@promptbook/utils`
266
+ */
267
+ function checkSerializableAsJson(name, value) {
268
+ var e_1, _a;
269
+ if (value === undefined) {
270
+ throw new UnexpectedError("".concat(name, " is undefined"));
271
+ }
272
+ else if (value === null) {
273
+ return;
274
+ }
275
+ else if (typeof value === 'boolean') {
276
+ return;
277
+ }
278
+ else if (typeof value === 'number' && !isNaN(value)) {
279
+ return;
280
+ }
281
+ else if (typeof value === 'string') {
282
+ return;
283
+ }
284
+ else if (typeof value === 'symbol') {
285
+ throw new UnexpectedError("".concat(name, " is symbol"));
286
+ }
287
+ else if (typeof value === 'function') {
288
+ throw new UnexpectedError("".concat(name, " is function"));
289
+ }
290
+ else if (typeof value === 'object' && Array.isArray(value)) {
291
+ for (var i = 0; i < value.length; i++) {
292
+ checkSerializableAsJson("".concat(name, "[").concat(i, "]"), value[i]);
293
+ }
294
+ }
295
+ else if (typeof value === 'object') {
296
+ if (value instanceof Date) {
297
+ throw new UnexpectedError(spaceTrim$1("\n ".concat(name, " is Date\n\n Use `string_date_iso8601` instead\n ")));
298
+ }
299
+ else if (value instanceof Map) {
300
+ throw new UnexpectedError("".concat(name, " is Map"));
301
+ }
302
+ else if (value instanceof Set) {
303
+ throw new UnexpectedError("".concat(name, " is Set"));
304
+ }
305
+ else if (value instanceof RegExp) {
306
+ throw new UnexpectedError("".concat(name, " is RegExp"));
307
+ }
308
+ else if (value instanceof Error) {
309
+ throw new UnexpectedError(spaceTrim$1("\n ".concat(name, " is unserialized Error\n\n Use function `serializeError`\n ")));
310
+ }
311
+ else {
312
+ try {
313
+ for (var _b = __values(Object.entries(value)), _c = _b.next(); !_c.done; _c = _b.next()) {
314
+ var _d = __read(_c.value, 2), subName = _d[0], subValue = _d[1];
315
+ if (subValue === undefined) {
316
+ // Note: undefined in object is serializable - it is just omited
317
+ continue;
318
+ }
319
+ checkSerializableAsJson("".concat(name, ".").concat(subName), subValue);
320
+ }
321
+ }
322
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
323
+ finally {
324
+ try {
325
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
326
+ }
327
+ finally { if (e_1) throw e_1.error; }
328
+ }
329
+ try {
330
+ JSON.stringify(value); // <- TODO: [0]
331
+ }
332
+ catch (error) {
333
+ if (!(error instanceof Error)) {
334
+ throw error;
335
+ }
336
+ throw new UnexpectedError(spaceTrim$1(function (block) { return "\n ".concat(name, " is not serializable\n\n ").concat(block(error.toString()), "\n "); }));
337
+ }
338
+ /*
339
+ TODO: [0] Is there some more elegant way to check circular references?
340
+ const seen = new Set();
341
+ const stack = [{ value }];
342
+ while (stack.length > 0) {
343
+ const { value } = stack.pop()!;
344
+ if (typeof value === 'object' && value !== null) {
345
+ if (seen.has(value)) {
346
+ throw new UnexpectedError(`${name} has circular reference`);
347
+ }
348
+ seen.add(value);
349
+ if (Array.isArray(value)) {
350
+ stack.push(...value.map((value) => ({ value })));
351
+ } else {
352
+ stack.push(...Object.values(value).map((value) => ({ value })));
353
+ }
354
+ }
355
+ }
356
+ */
357
+ return;
358
+ }
359
+ }
360
+ else {
361
+ throw new UnexpectedError("".concat(name, " is unknown"));
362
+ }
363
+ }
364
+ /**
365
+ * TODO: [🧠][🛣] More elegant way to tracking than passing `name`
366
+ * TODO: [🧠][main] !!! In-memory cache of same values to prevent multiple checks
367
+ * Note: [🐠] This is how `checkSerializableAsJson` + `isSerializableAsJson` together can just retun true/false or rich error message
368
+ */
369
+
370
+ /**
371
+ * @@@
372
+ * @@@
373
+ *
374
+ * Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
375
+ *
376
+ * @param name - Name of the object for debugging purposes
377
+ * @param objectValue - Object to be deeply frozen
378
+ * @returns The same object as the input, but deeply frozen
379
+ * @private this is in comparison to `deepFreeze` a more specific utility and maybe not very good practice to use without specific reason and considerations
380
+ */
381
+ function $asDeeplyFrozenSerializableJson(name, objectValue) {
382
+ checkSerializableAsJson(name, objectValue);
383
+ return $deepFreeze(objectValue);
384
+ }
385
+ /**
386
+ * TODO: [🧠][🛣] More elegant way to tracking than passing `name`
387
+ * TODO: [🧠] Is there a way how to meaningfully test this utility
388
+ */
389
+
390
+ // <- TODO: [🧠] Better system for generator warnings - not always "code" and "by `@promptbook/cli`"
391
+ /**
392
+ * The maximum number of iterations for a loops
393
+ *
394
+ * @private within the repository - too low-level in comparison with other `MAX_...`
395
+ */
396
+ var LOOP_LIMIT = 1000;
397
+ /**
398
+ * Nonce which is used for replacing things in strings
399
+ *
400
+ * @private within the repository
401
+ */
402
+ var REPLACING_NONCE = 'u$k42k%!V2zo34w7Fu#@QUHYPW';
403
+ /**
404
+ * The names of the parameters that are reserved for special purposes
405
+ *
406
+ * @public exported from `@promptbook/core`
407
+ */
408
+ $asDeeplyFrozenSerializableJson('RESERVED_PARAMETER_NAMES', [
409
+ 'content',
410
+ 'context',
411
+ 'knowledge',
412
+ 'examples',
413
+ 'modelName',
414
+ 'currentDate',
415
+ // <- TODO: list here all command names
416
+ // <- TODO: Add more like 'date', 'modelName',...
417
+ // <- TODO: Add [emoji] + instructions ACRY when adding new reserved parameter
418
+ ]);
419
+ /**
420
+ * @@@
421
+ *
422
+ * @private within the repository
423
+ */
424
+ var RESERVED_PARAMETER_MISSING_VALUE = 'MISSING-' + REPLACING_NONCE;
425
+ /**
426
+ * @@@
427
+ *
428
+ * @private within the repository
429
+ */
430
+ var RESERVED_PARAMETER_RESTRICTED = 'RESTRICTED-' + REPLACING_NONCE;
431
+ // <- TODO: [🧜‍♂️]
432
+ /**
433
+ * @@@
434
+ *
435
+ * @public exported from `@promptbook/core`
436
+ */
437
+ Object.freeze({
438
+ delimiter: ',',
439
+ quoteChar: '"',
440
+ newline: '\n',
441
+ skipEmptyLines: true,
442
+ });
443
+ /**
444
+ * TODO: Extract `constants.ts` from `config.ts`
445
+ * Note: [💞] Ignore a discrepancy between file name and entity name
446
+ * TODO: [🧠][🧜‍♂️] Maybe join remoteUrl and path into single value
447
+ */
448
+
449
+ /**
450
+ * This error type indicates that some limit was reached
451
+ *
452
+ * @public exported from `@promptbook/core`
453
+ */
454
+ var LimitReachedError = /** @class */ (function (_super) {
455
+ __extends(LimitReachedError, _super);
456
+ function LimitReachedError(message) {
457
+ var _this = _super.call(this, message) || this;
458
+ _this.name = 'LimitReachedError';
459
+ Object.setPrototypeOf(_this, LimitReachedError.prototype);
460
+ return _this;
461
+ }
462
+ return LimitReachedError;
463
+ }(Error));
464
+
465
+ /**
466
+ * This error indicates errors during the execution of the pipeline
467
+ *
468
+ * @public exported from `@promptbook/core`
469
+ */
470
+ var PipelineExecutionError = /** @class */ (function (_super) {
471
+ __extends(PipelineExecutionError, _super);
472
+ function PipelineExecutionError(message) {
473
+ var _this = _super.call(this, message) || this;
474
+ _this.name = 'PipelineExecutionError';
475
+ Object.setPrototypeOf(_this, PipelineExecutionError.prototype);
476
+ return _this;
477
+ }
478
+ return PipelineExecutionError;
479
+ }(Error));
480
+
481
+ /**
482
+ * Replaces parameters in template with values from parameters object
483
+ *
484
+ * @param template the template with parameters in {curly} braces
485
+ * @param parameters the object with parameters
486
+ * @returns the template with replaced parameters
487
+ * @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
488
+ * @public exported from `@promptbook/utils`
489
+ */
490
+ function replaceParameters(template, parameters) {
491
+ var e_1, _a;
492
+ try {
493
+ for (var _b = __values(Object.entries(parameters)), _c = _b.next(); !_c.done; _c = _b.next()) {
494
+ var _d = __read(_c.value, 2), parameterName = _d[0], parameterValue = _d[1];
495
+ if (parameterValue === RESERVED_PARAMETER_MISSING_VALUE) {
496
+ throw new UnexpectedError("Parameter `{".concat(parameterName, "}` has missing value"));
497
+ }
498
+ else if (parameterValue === RESERVED_PARAMETER_RESTRICTED) {
499
+ // TODO: [🍵]
500
+ throw new UnexpectedError("Parameter `{".concat(parameterName, "}` is restricted to use"));
501
+ }
502
+ }
503
+ }
504
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
505
+ finally {
506
+ try {
507
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
508
+ }
509
+ finally { if (e_1) throw e_1.error; }
510
+ }
511
+ var replacedTemplates = template;
512
+ var match;
513
+ var loopLimit = LOOP_LIMIT;
514
+ var _loop_1 = function () {
515
+ if (loopLimit-- < 0) {
516
+ throw new LimitReachedError('Loop limit reached during parameters replacement in `replaceParameters`');
517
+ }
518
+ var precol = match.groups.precol;
519
+ var parameterName = match.groups.parameterName;
520
+ if (parameterName === '') {
521
+ return "continue";
522
+ }
523
+ if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
524
+ throw new PipelineExecutionError('Parameter is already opened or not closed');
525
+ }
526
+ if (parameters[parameterName] === undefined) {
527
+ throw new PipelineExecutionError("Parameter `{".concat(parameterName, "}` is not defined"));
528
+ }
529
+ var parameterValue = parameters[parameterName];
530
+ if (parameterValue === undefined) {
531
+ throw new PipelineExecutionError("Parameter `{".concat(parameterName, "}` is not defined"));
532
+ }
533
+ parameterValue = parameterValue.toString();
534
+ if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
535
+ parameterValue = parameterValue
536
+ .split('\n')
537
+ .map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
538
+ .join('\n');
539
+ }
540
+ replacedTemplates =
541
+ replacedTemplates.substring(0, match.index + precol.length) +
542
+ parameterValue +
543
+ replacedTemplates.substring(match.index + precol.length + parameterName.length + 2);
544
+ };
545
+ while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
546
+ .exec(replacedTemplates))) {
547
+ _loop_1();
548
+ }
549
+ // [💫] Check if there are parameters that are not closed properly
550
+ if (/{\w+$/.test(replacedTemplates)) {
551
+ throw new PipelineExecutionError('Parameter is not closed');
552
+ }
553
+ // [💫] Check if there are parameters that are not opened properly
554
+ if (/^\w+}/.test(replacedTemplates)) {
555
+ throw new PipelineExecutionError('Parameter is not opened');
556
+ }
557
+ return replacedTemplates;
558
+ }
559
+
560
+ /**
561
+ * Simple wrapper `new Date().toISOString()`
562
+ *
563
+ * Note: `$` is used to indicate that this function is not a pure function - it is not deterministic because it depends on the current time
564
+ *
565
+ * @returns string_date branded type
566
+ * @public exported from `@promptbook/utils`
567
+ */
568
+ function $getCurrentDate() {
569
+ return new Date().toISOString();
570
+ }
571
+
572
+ // <- TODO: Is there some way to get the type of the provider directly, NOT this stupid way via inferring the return type from a specific vercel provider⁉
573
+ /**
574
+ * !!!!!!
575
+ *
576
+ * @public exported from `@promptbook/vercel`
577
+ */
578
+ function createExecutionToolsFromVercelProvider(vercelProvider, options) {
579
+ if (options === void 0) { options = {}; }
580
+ return {
581
+ title: '!!!',
582
+ description: "!!! (through Vercel)",
583
+ checkConfiguration: function () {
584
+ // TODO: !!!!!!
585
+ return Promise.resolve();
586
+ },
587
+ listModels: function () {
588
+ return __awaiter(this, void 0, void 0, function () {
589
+ return __generator(this, function (_a) {
590
+ return [2 /*return*/, [
591
+ /* TODO: !!!!! */
592
+ ]];
593
+ });
594
+ });
595
+ },
596
+ callChatModel: function (prompt) {
597
+ var _a;
598
+ return __awaiter(this, void 0, void 0, function () {
599
+ var content, parameters, modelRequirements, modelName, model, rawPromptContent, rawRequest, start, rawResponse, complete, usage;
600
+ return __generator(this, function (_b) {
601
+ switch (_b.label) {
602
+ case 0:
603
+ content = prompt.content, parameters = prompt.parameters, modelRequirements = prompt.modelRequirements;
604
+ if (modelRequirements.modelVariant !== 'CHAT') {
605
+ throw new PipelineExecutionError('Use callChatModel only for CHAT variant');
606
+ }
607
+ modelName = 'gpt-4';
608
+ return [4 /*yield*/, vercelProvider.chat(modelName, {
609
+ user: ((_a = options.userId) === null || _a === void 0 ? void 0 : _a.toString()) || undefined,
610
+ })];
611
+ case 1:
612
+ model = _b.sent();
613
+ rawPromptContent = replaceParameters(content, __assign(__assign({}, parameters), { modelName: modelName }));
614
+ rawRequest = {
615
+ // <- TODO: [☂]
616
+ inputFormat: 'messages',
617
+ mode: {
618
+ type: 'regular',
619
+ tools: [
620
+ /* !!!!!! */
621
+ ],
622
+ },
623
+ prompt: __spreadArray(__spreadArray([], __read((modelRequirements.systemMessage === undefined
624
+ ? []
625
+ : [
626
+ {
627
+ role: 'system',
628
+ content: modelRequirements.systemMessage,
629
+ },
630
+ ])), false), [
631
+ {
632
+ role: 'user',
633
+ content: [
634
+ {
635
+ type: 'text',
636
+ text: rawPromptContent,
637
+ },
638
+ ],
639
+ },
640
+ ], false),
641
+ };
642
+ start = $getCurrentDate();
643
+ if (options.isVerbose) {
644
+ console.info(colors.bgWhite('rawRequest'), JSON.stringify(rawRequest, null, 4));
645
+ }
646
+ return [4 /*yield*/, model.doGenerate(rawRequest)];
647
+ case 2:
648
+ rawResponse = _b.sent();
649
+ /*
650
+ TODO: !!!!!! Handle errors
651
+ .catch((error) => {
652
+ if (options.isVerbose) {
653
+ console.info(colors.bgRed('error'), error);
654
+ }
655
+ throw error;
656
+ });
657
+ */
658
+ if (options.isVerbose) {
659
+ console.info(colors.bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
660
+ }
661
+ if (rawResponse.text === undefined) {
662
+ throw new PipelineExecutionError('No response message');
663
+ }
664
+ complete = $getCurrentDate();
665
+ usage = UNCERTAIN_USAGE;
666
+ return [2 /*return*/, $asDeeplyFrozenSerializableJson('createExecutionToolsFromVercelProvider ChatPromptResult', {
667
+ content: rawResponse.text,
668
+ modelName: modelName,
669
+ timing: {
670
+ start: start,
671
+ complete: complete,
672
+ },
673
+ usage: usage,
674
+ rawPromptContent: rawPromptContent,
675
+ rawRequest: rawRequest,
676
+ rawResponse: {
677
+ /* TODO: !!!!!! UnexpectedError: createExecutionToolsFromVercelProvider ChatPromptResult.rawResponse.response.timestamp is Date */
678
+ },
679
+ // <- [🗯]
680
+ })];
681
+ }
682
+ });
683
+ });
684
+ },
685
+ };
686
+ }
687
+
688
+ export { BOOK_LANGUAGE_VERSION, PROMPTBOOK_ENGINE_VERSION, createExecutionToolsFromVercelProvider };
20
689
  //# sourceMappingURL=index.es.js.map