@promptbook/cli 0.66.0 → 0.67.0-1

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.
Files changed (60) hide show
  1. package/esm/index.es.js +486 -306
  2. package/esm/index.es.js.map +1 -1
  3. package/esm/typings/src/_packages/core.index.d.ts +4 -4
  4. package/esm/typings/src/_packages/types.index.d.ts +7 -1
  5. package/esm/typings/src/_packages/utils.index.d.ts +14 -8
  6. package/esm/typings/src/commands/EXPECT/ExpectFormatCommand.d.ts +2 -0
  7. package/esm/typings/src/errors/{ReferenceError.d.ts → PipelineUrlError.d.ts} +2 -2
  8. package/esm/typings/src/errors/index.d.ts +27 -0
  9. package/esm/typings/src/errors/utils/ErrorJson.d.ts +20 -0
  10. package/esm/typings/src/errors/utils/deserializeError.d.ts +7 -0
  11. package/esm/typings/src/errors/utils/deserializeError.test.d.ts +1 -0
  12. package/esm/typings/src/errors/utils/serializeError.d.ts +7 -0
  13. package/esm/typings/src/errors/utils/serializeError.test.d.ts +1 -0
  14. package/esm/typings/src/execution/ExecutionTools.d.ts +4 -1
  15. package/esm/typings/src/execution/PipelineExecutor.d.ts +1 -47
  16. package/esm/typings/src/execution/PipelineExecutorResult.d.ts +49 -0
  17. package/esm/typings/src/execution/PromptResult.d.ts +4 -4
  18. package/esm/typings/src/execution/PromptResultUsage.d.ts +4 -0
  19. package/esm/typings/src/execution/UncertainNumber.d.ts +1 -0
  20. package/esm/typings/src/execution/assertsExecutionSuccessful.d.ts +2 -2
  21. package/esm/typings/src/llm-providers/_common/utils/cache/CacheItem.d.ts +0 -1
  22. package/esm/typings/src/llm-providers/mocked/$fakeTextToExpectations.d.ts +2 -2
  23. package/esm/typings/src/llm-providers/mocked/MockedFackedLlmExecutionTools.d.ts +3 -3
  24. package/esm/typings/src/llm-providers/remote/RemoteLlmExecutionTools.d.ts +1 -0
  25. package/esm/typings/src/llm-providers/remote/interfaces/PromptbookServer_Error.d.ts +2 -6
  26. package/esm/typings/src/llm-providers/remote/startRemoteServer.d.ts +1 -0
  27. package/esm/typings/src/personas/preparePersona.d.ts +1 -0
  28. package/esm/typings/src/prepare/isPipelinePrepared.d.ts +1 -0
  29. package/esm/typings/src/scripting/javascript/JavascriptExecutionToolsOptions.d.ts +2 -2
  30. package/esm/typings/src/storage/_common/PromptbookStorage.d.ts +1 -1
  31. package/esm/typings/src/types/ModelRequirements.d.ts +5 -5
  32. package/esm/typings/src/types/PipelineJson/Expectations.d.ts +3 -1
  33. package/esm/typings/src/types/PipelineJson/KnowledgePieceJson.d.ts +2 -0
  34. package/esm/typings/src/types/PipelineJson/KnowledgeSourceJson.d.ts +4 -0
  35. package/esm/typings/src/types/PipelineJson/LlmTemplateJson.d.ts +2 -0
  36. package/esm/typings/src/types/PipelineJson/PersonaJson.d.ts +4 -0
  37. package/esm/typings/src/types/PipelineJson/PipelineJson.d.ts +2 -0
  38. package/esm/typings/src/types/PipelineJson/PromptDialogJson.d.ts +1 -0
  39. package/esm/typings/src/types/PipelineJson/PromptTemplateJson.d.ts +2 -0
  40. package/esm/typings/src/types/PipelineJson/PromptTemplateJsonCommon.d.ts +2 -2
  41. package/esm/typings/src/types/PipelineJson/PromptTemplateParameterJson.d.ts +2 -0
  42. package/esm/typings/src/types/PipelineJson/ScriptJson.d.ts +1 -0
  43. package/esm/typings/src/types/PipelineJson/SimpleTemplateJson.d.ts +1 -0
  44. package/esm/typings/src/types/Prompt.d.ts +7 -7
  45. package/esm/typings/src/types/ScriptLanguage.d.ts +2 -0
  46. package/esm/typings/src/types/execution-report/ExecutionPromptReportJson.d.ts +24 -0
  47. package/esm/typings/src/types/execution-report/ExecutionReportJson.d.ts +3 -20
  48. package/esm/typings/src/types/typeAliases.d.ts +7 -0
  49. package/esm/typings/src/utils/environment/$getGlobalScope.d.ts +1 -4
  50. package/esm/typings/src/utils/serialization/$asDeeplyFrozenSerializableJson.d.ts +17 -0
  51. package/esm/typings/src/utils/{deepFreeze.d.ts → serialization/$deepFreeze.d.ts} +0 -10
  52. package/esm/typings/src/utils/serialization/checkSerializableAsJson.d.ts +27 -0
  53. package/esm/typings/src/utils/{clonePipeline.d.ts → serialization/clonePipeline.d.ts} +1 -1
  54. package/esm/typings/src/utils/serialization/isSerializableAsJson.d.ts +24 -0
  55. package/esm/typings/src/utils/serialization/isSerializableAsJson.test.d.ts +1 -0
  56. package/package.json +1 -1
  57. package/umd/index.umd.js +406 -226
  58. package/umd/index.umd.js.map +1 -1
  59. package/esm/typings/src/errors/VersionMismatchError.d.ts +0 -10
  60. /package/esm/typings/src/utils/{deepClone.d.ts → serialization/deepClone.d.ts} +0 -0
package/esm/index.es.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import commander from 'commander';
2
- import spaceTrim, { spaceTrim as spaceTrim$1 } from 'spacetrim';
2
+ import spaceTrim$1, { spaceTrim } from 'spacetrim';
3
3
  import colors from 'colors';
4
4
  import { forTime } from 'waitasecond';
5
5
  import { stat, access, constants, readdir, readFile, writeFile, mkdir, unlink } from 'fs/promises';
@@ -20,7 +20,7 @@ import OpenAI from 'openai';
20
20
  /**
21
21
  * The version of the Promptbook library
22
22
  */
23
- var PROMPTBOOK_VERSION = '0.66.0-9';
23
+ var PROMPTBOOK_VERSION = '0.67.0-0';
24
24
  // TODO: !!!! List here all the versions and annotate + put into script
25
25
 
26
26
  /*! *****************************************************************************
@@ -167,6 +167,26 @@ var EnvironmentMismatchError = /** @class */ (function (_super) {
167
167
  */
168
168
  var $isRunningInNode = new Function("\n try {\n return this === global;\n } catch (e) {\n return false;\n }\n");
169
169
 
170
+ /**
171
+ * Returns the same value that is passed as argument.
172
+ * No side effects.
173
+ *
174
+ * Note: It can be usefull for:
175
+ *
176
+ * 1) Leveling indentation
177
+ * 2) Putting always-true or always-false conditions without getting eslint errors
178
+ *
179
+ * @param value any values
180
+ * @returns the same values
181
+ * @private within the repository
182
+ */
183
+ function just(value) {
184
+ if (value === undefined) {
185
+ return undefined;
186
+ }
187
+ return value;
188
+ }
189
+
170
190
  /**
171
191
  * @@@
172
192
  *
@@ -197,42 +217,169 @@ function $deepFreeze(objectValue) {
197
217
  }
198
218
  return Object.freeze(objectValue);
199
219
  }
220
+ /**
221
+ * TODO: [🧠] Is there a way how to meaningfully test this utility
222
+ */
223
+
224
+ /**
225
+ * This error type indicates that the error should not happen and its last check before crashing with some other error
226
+ *
227
+ * @public exported from `@promptbook/core`
228
+ */
229
+ var UnexpectedError = /** @class */ (function (_super) {
230
+ __extends(UnexpectedError, _super);
231
+ function UnexpectedError(message) {
232
+ 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;
233
+ _this.name = 'UnexpectedError';
234
+ Object.setPrototypeOf(_this, UnexpectedError.prototype);
235
+ return _this;
236
+ }
237
+ return UnexpectedError;
238
+ }(Error));
239
+
240
+ /**
241
+ * Checks if the value is [🚉] serializable as JSON
242
+ * If not, throws an UnexpectedError with a rich error message and tracking
243
+ *
244
+ * - Almost all primitives are serializable BUT:
245
+ * - `undefined` is not serializable
246
+ * - `NaN` is not serializable
247
+ * - Objects and arrays are serializable if all their properties are serializable
248
+ * - Functions are not serializable
249
+ * - Circular references are not serializable
250
+ * - `Date` objects are not serializable
251
+ * - `Map` and `Set` objects are not serializable
252
+ * - `RegExp` objects are not serializable
253
+ * - `Error` objects are not serializable
254
+ * - `Symbol` objects are not serializable
255
+ * - And much more...
256
+ *
257
+ * @throws UnexpectedError if the value is not serializable as JSON
258
+ * @public exported from `@promptbook/utils`
259
+ */
260
+ function checkSerializableAsJson(name, value) {
261
+ var e_1, _a;
262
+ if (value === undefined) {
263
+ throw new UnexpectedError("".concat(name, " is undefined"));
264
+ }
265
+ else if (value === null) {
266
+ return;
267
+ }
268
+ else if (typeof value === 'boolean') {
269
+ return;
270
+ }
271
+ else if (typeof value === 'number' && !isNaN(value)) {
272
+ return;
273
+ }
274
+ else if (typeof value === 'string') {
275
+ return;
276
+ }
277
+ else if (typeof value === 'symbol') {
278
+ throw new UnexpectedError("".concat(name, " is symbol"));
279
+ }
280
+ else if (typeof value === 'function') {
281
+ throw new UnexpectedError("".concat(name, " is function"));
282
+ }
283
+ else if (typeof value === 'object' && Array.isArray(value)) {
284
+ for (var i = 0; i < value.length; i++) {
285
+ checkSerializableAsJson("".concat(name, "[").concat(i, "]"), value[i]);
286
+ }
287
+ }
288
+ else if (typeof value === 'object') {
289
+ if (value instanceof Date) {
290
+ throw new UnexpectedError(spaceTrim$1("\n ".concat(name, " is Date\n\n Use `string_date_iso8601` instead\n ")));
291
+ }
292
+ else if (value instanceof Map) {
293
+ throw new UnexpectedError("".concat(name, " is Map"));
294
+ }
295
+ else if (value instanceof Set) {
296
+ throw new UnexpectedError("".concat(name, " is Set"));
297
+ }
298
+ else if (value instanceof RegExp) {
299
+ throw new UnexpectedError("".concat(name, " is RegExp"));
300
+ }
301
+ else if (value instanceof Error) {
302
+ throw new UnexpectedError(spaceTrim$1("\n ".concat(name, " is unserialized Error\n\n Use function `serializeError`\n ")));
303
+ }
304
+ else {
305
+ try {
306
+ for (var _b = __values(Object.entries(value)), _c = _b.next(); !_c.done; _c = _b.next()) {
307
+ var _d = __read(_c.value, 2), subName = _d[0], subValue = _d[1];
308
+ if (subValue === undefined) {
309
+ // Note: undefined in object is serializable - it is just omited
310
+ continue;
311
+ }
312
+ checkSerializableAsJson("".concat(name, ".").concat(subName), subValue);
313
+ }
314
+ }
315
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
316
+ finally {
317
+ try {
318
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
319
+ }
320
+ finally { if (e_1) throw e_1.error; }
321
+ }
322
+ try {
323
+ JSON.stringify(value); // <- TODO: [0]
324
+ }
325
+ catch (error) {
326
+ if (!(error instanceof Error)) {
327
+ throw error;
328
+ }
329
+ throw new UnexpectedError(spaceTrim$1(function (block) { return "\n ".concat(name, " is not serializable\n\n ").concat(block(error.toString()), "\n "); }));
330
+ }
331
+ /*
332
+ TODO: [0] Is there some more elegant way to check circular references?
333
+ const seen = new Set();
334
+ const stack = [{ value }];
335
+ while (stack.length > 0) {
336
+ const { value } = stack.pop()!;
337
+ if (typeof value === 'object' && value !== null) {
338
+ if (seen.has(value)) {
339
+ throw new UnexpectedError(`${name} has circular reference`);
340
+ }
341
+ seen.add(value);
342
+ if (Array.isArray(value)) {
343
+ stack.push(...value.map((value) => ({ value })));
344
+ } else {
345
+ stack.push(...Object.values(value).map((value) => ({ value })));
346
+ }
347
+ }
348
+ }
349
+ */
350
+ return;
351
+ }
352
+ }
353
+ else {
354
+ throw new UnexpectedError("".concat(name, " is unknown"));
355
+ }
356
+ }
357
+ /**
358
+ * TODO: [🧠][🛣] More elegant way to tracking than passing `name`
359
+ * TODO: [🧠] !!! In-memory cache of same values to prevent multiple checks
360
+ * Note: [🐠] This is how `checkSerializableAsJson` + `isSerializableAsJson` together can just retun true/false or rich error message
361
+ */
362
+
200
363
  /**
201
364
  * @@@
202
365
  * @@@
203
366
  *
204
367
  * Note: This function mutates the object and returns the original (but mutated-deep-freezed) object
205
368
  *
369
+ * @param name - Name of the object for debugging purposes
370
+ * @param objectValue - Object to be deeply frozen
206
371
  * @returns The same object as the input, but deeply frozen
207
372
  * @private this is in comparison to `deepFreeze` a more specific utility and maybe not very good practice to use without specific reason and considerations
208
373
  */
209
- function deepFreezeWithSameType(objectValue) {
374
+ function $asDeeplyFrozenSerializableJson(name, objectValue) {
375
+ checkSerializableAsJson(name, objectValue);
210
376
  return $deepFreeze(objectValue);
211
377
  }
212
378
  /**
379
+ * TODO: [🧠][🛣] More elegant way to tracking than passing `name`
213
380
  * TODO: [🧠] Is there a way how to meaningfully test this utility
214
381
  */
215
382
 
216
- /**
217
- * Returns the same value that is passed as argument.
218
- * No side effects.
219
- *
220
- * Note: It can be usefull for:
221
- *
222
- * 1) Leveling indentation
223
- * 2) Putting always-true or always-false conditions without getting eslint errors
224
- *
225
- * @param value any values
226
- * @returns the same values
227
- * @private within the repository
228
- */
229
- function just(value) {
230
- if (value === undefined) {
231
- return undefined;
232
- }
233
- return value;
234
- }
235
-
236
383
  /**
237
384
  * Warning message for the generated sections and files files
238
385
  *
@@ -301,7 +448,7 @@ var REPLACING_NONCE = 'u$k42k%!V2zo34w7Fu#@QUHYPW';
301
448
  *
302
449
  * @public exported from `@promptbook/core`
303
450
  */
304
- var RESERVED_PARAMETER_NAMES = $deepFreeze([
451
+ var RESERVED_PARAMETER_NAMES = $asDeeplyFrozenSerializableJson('RESERVED_PARAMETER_NAMES', [
305
452
  'content',
306
453
  'context',
307
454
  'knowledge',
@@ -354,7 +501,7 @@ var IS_VERBOSE = false;
354
501
  function initializeAboutCommand(program) {
355
502
  var _this = this;
356
503
  var makeCommand = program.command('about');
357
- makeCommand.description(spaceTrim("\n Tells about Promptbook CLI and its abilities\n "));
504
+ makeCommand.description(spaceTrim$1("\n Tells about Promptbook CLI and its abilities\n "));
358
505
  makeCommand.action(function () { return __awaiter(_this, void 0, void 0, function () {
359
506
  return __generator(this, function (_a) {
360
507
  console.info(colors.cyan("Promptbook"));
@@ -379,7 +526,7 @@ function initializeAboutCommand(program) {
379
526
  function initializeHelloCommand(program) {
380
527
  var _this = this;
381
528
  var helloCommand = program.command('hello');
382
- helloCommand.description(spaceTrim("\n Just command for testing\n "));
529
+ helloCommand.description(spaceTrim$1("\n Just command for testing\n "));
383
530
  helloCommand.argument('[name]', 'Your name', 'Paul');
384
531
  helloCommand.option('-g, --greeting <greeting>', "Greeting", 'Hello');
385
532
  helloCommand.action(function (name, _a) {
@@ -641,7 +788,7 @@ function pipelineJsonToString(pipelineJson) {
641
788
  pipelineString += '\n\n';
642
789
  pipelineString += '```' + contentLanguage;
643
790
  pipelineString += '\n';
644
- pipelineString += spaceTrim(content);
791
+ pipelineString += spaceTrim$1(content);
645
792
  // <- TODO: !!! Escape
646
793
  // <- TODO: [🧠] Some clear strategy how to spaceTrim the blocks
647
794
  pipelineString += '\n';
@@ -874,7 +1021,7 @@ function forEachAsync(array, options, callbackfunction) {
874
1021
  });
875
1022
  }
876
1023
 
877
- var PipelineCollection = [{title:"Prepare Knowledge from Markdown",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.66.0-9",parameters:[{name:"knowledgeContent",description:"Markdown document content",isInput:true,isOutput:false},{name:"knowledgePieces",description:"The knowledge JSON object",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, extract the important knowledge from the document.\n\n# Rules\n\n- Make pieces of information concise, clear, and easy to understand\n- One piece of information should be approximately 1 paragraph\n- Divide the paragraphs by markdown horizontal lines ---\n- Omit irrelevant information\n- Group redundant information\n- Write just extracted information, nothing else\n\n# The document\n\nTake information from this document:\n\n> {knowledgeContent}",dependentParameterNames:["knowledgeContent"],resultingParameterName:"knowledgePieces"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-from-markdown.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-keywords.ptbk.md",promptbookVersion:"0.66.0-9",parameters:[{name:"knowledgePieceContent",description:"The content",isInput:true,isOutput:false},{name:"keywords",description:"Keywords separated by comma",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, detect the important keywords in the document.\n\n# Rules\n\n- Write just keywords separated by comma\n\n# The document\n\nTake information from this document:\n\n> {knowledgePieceContent}",dependentParameterNames:["knowledgePieceContent"],resultingParameterName:"keywords"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-keywords.ptbk.md"},{title:"Prepare Title",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-title.ptbk.md",promptbookVersion:"0.66.0-9",parameters:[{name:"knowledgePieceContent",description:"The content",isInput:true,isOutput:false},{name:"title",description:"The title of the document",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced content creator, write best title for the document.\n\n# Rules\n\n- Write just title, nothing else\n- Title should be concise and clear\n- Write maximum 5 words for the title\n\n# The document\n\n> {knowledgePieceContent}",expectations:{words:{min:1,max:8}},dependentParameterNames:["knowledgePieceContent"],resultingParameterName:"title"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-title.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-persona.ptbk.md",promptbookVersion:"0.66.0-9",parameters:[{name:"availableModelNames",description:"List of available model names separated by comma (,)",isInput:true,isOutput:false},{name:"personaDescription",description:"Description of the persona",isInput:true,isOutput:false},{name:"modelRequirements",description:"Specific requirements for the model",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"make-model-requirements",title:"Make modelRequirements",modelRequirements:{modelVariant:"CHAT",modelName:"gpt-4-turbo"},content:"You are experienced AI engineer, you need to create virtual assistant.\nWrite\n\n## Sample\n\n```json\n{\n\"modelName\": \"gpt-4o\",\n\"systemMessage\": \"You are experienced AI engineer and helpfull assistant.\",\n\"temperature\": 0.7\n}\n```\n\n## Instructions\n\n### Option `modelName`\n\nPick from the following models:\n\n- {availableModelNames}\n\n### Option `systemMessage`\n\nThe system message is used to communicate instructions or provide context to the model at the beginning of a conversation. It is displayed in a different format compared to user messages, helping the model understand its role in the conversation. The system message typically guides the model's behavior, sets the tone, or specifies desired output from the model. By utilizing the system message effectively, users can steer the model towards generating more accurate and relevant responses.\n\nFor example:\n\n> You are an experienced AI engineer and helpful assistant.\n\n> You are a friendly and knowledgeable chatbot.\n\n### Option `temperature`\n\nThe sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.\n\nYou can pick a value between 0 and 2. For example:\n\n- `0.1`: Low temperature, extremely conservative and deterministic\n- `0.5`: Medium temperature, balanced between conservative and creative\n- `1.0`: High temperature, creative and bit random\n- `1.5`: Very high temperature, extremely creative and often chaotic and unpredictable\n- `2.0`: Maximum temperature, completely random and unpredictable, for some extreme creative use cases\n\n# The assistant\n\nTake this description of the persona:\n\n> {personaDescription}",expectFormat:"JSON",dependentParameterNames:["availableModelNames","personaDescription"],resultingParameterName:"modelRequirements"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-persona.ptbk.md"}];
1024
+ var PipelineCollection = [{title:"Prepare Knowledge from Markdown",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.67.0-0",parameters:[{name:"knowledgeContent",description:"Markdown document content",isInput:true,isOutput:false},{name:"knowledgePieces",description:"The knowledge JSON object",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, extract the important knowledge from the document.\n\n# Rules\n\n- Make pieces of information concise, clear, and easy to understand\n- One piece of information should be approximately 1 paragraph\n- Divide the paragraphs by markdown horizontal lines ---\n- Omit irrelevant information\n- Group redundant information\n- Write just extracted information, nothing else\n\n# The document\n\nTake information from this document:\n\n> {knowledgeContent}",dependentParameterNames:["knowledgeContent"],resultingParameterName:"knowledgePieces"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-from-markdown.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-keywords.ptbk.md",promptbookVersion:"0.67.0-0",parameters:[{name:"knowledgePieceContent",description:"The content",isInput:true,isOutput:false},{name:"keywords",description:"Keywords separated by comma",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, detect the important keywords in the document.\n\n# Rules\n\n- Write just keywords separated by comma\n\n# The document\n\nTake information from this document:\n\n> {knowledgePieceContent}",dependentParameterNames:["knowledgePieceContent"],resultingParameterName:"keywords"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-keywords.ptbk.md"},{title:"Prepare Title",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-title.ptbk.md",promptbookVersion:"0.67.0-0",parameters:[{name:"knowledgePieceContent",description:"The content",isInput:true,isOutput:false},{name:"title",description:"The title of the document",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced content creator, write best title for the document.\n\n# Rules\n\n- Write just title, nothing else\n- Title should be concise and clear\n- Write maximum 5 words for the title\n\n# The document\n\n> {knowledgePieceContent}",expectations:{words:{min:1,max:8}},dependentParameterNames:["knowledgePieceContent"],resultingParameterName:"title"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-title.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-persona.ptbk.md",promptbookVersion:"0.67.0-0",parameters:[{name:"availableModelNames",description:"List of available model names separated by comma (,)",isInput:true,isOutput:false},{name:"personaDescription",description:"Description of the persona",isInput:true,isOutput:false},{name:"modelRequirements",description:"Specific requirements for the model",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"make-model-requirements",title:"Make modelRequirements",modelRequirements:{modelVariant:"CHAT",modelName:"gpt-4-turbo"},content:"You are experienced AI engineer, you need to create virtual assistant.\nWrite\n\n## Sample\n\n```json\n{\n\"modelName\": \"gpt-4o\",\n\"systemMessage\": \"You are experienced AI engineer and helpfull assistant.\",\n\"temperature\": 0.7\n}\n```\n\n## Instructions\n\n### Option `modelName`\n\nPick from the following models:\n\n- {availableModelNames}\n\n### Option `systemMessage`\n\nThe system message is used to communicate instructions or provide context to the model at the beginning of a conversation. It is displayed in a different format compared to user messages, helping the model understand its role in the conversation. The system message typically guides the model's behavior, sets the tone, or specifies desired output from the model. By utilizing the system message effectively, users can steer the model towards generating more accurate and relevant responses.\n\nFor example:\n\n> You are an experienced AI engineer and helpful assistant.\n\n> You are a friendly and knowledgeable chatbot.\n\n### Option `temperature`\n\nThe sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.\n\nYou can pick a value between 0 and 2. For example:\n\n- `0.1`: Low temperature, extremely conservative and deterministic\n- `0.5`: Medium temperature, balanced between conservative and creative\n- `1.0`: High temperature, creative and bit random\n- `1.5`: Very high temperature, extremely creative and often chaotic and unpredictable\n- `2.0`: Maximum temperature, completely random and unpredictable, for some extreme creative use cases\n\n# The assistant\n\nTake this description of the persona:\n\n> {personaDescription}",expectFormat:"JSON",dependentParameterNames:["availableModelNames","personaDescription"],resultingParameterName:"modelRequirements"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-persona.ptbk.md"}];
878
1025
 
879
1026
  /**
880
1027
  * This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
@@ -908,22 +1055,6 @@ var PipelineLogicError = /** @class */ (function (_super) {
908
1055
  return PipelineLogicError;
909
1056
  }(Error));
910
1057
 
911
- /**
912
- * This error type indicates that the error should not happen and its last check before crashing with some other error
913
- *
914
- * @public exported from `@promptbook/core`
915
- */
916
- var UnexpectedError = /** @class */ (function (_super) {
917
- __extends(UnexpectedError, _super);
918
- function UnexpectedError(message) {
919
- var _this = _super.call(this, spaceTrim$1(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;
920
- _this.name = 'UnexpectedError';
921
- Object.setPrototypeOf(_this, UnexpectedError.prototype);
922
- return _this;
923
- }
924
- return UnexpectedError;
925
- }(Error));
926
-
927
1058
  /**
928
1059
  * Tests if given string is valid semantic version
929
1060
  *
@@ -1109,36 +1240,36 @@ function validatePipeline(pipeline) {
1109
1240
  })();
1110
1241
  if (pipeline.pipelineUrl !== undefined && !isValidPipelineUrl(pipeline.pipelineUrl)) {
1111
1242
  // <- Note: [🚲]
1112
- throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n Invalid promptbook URL \"".concat(pipeline.pipelineUrl, "\"\n\n ").concat(block(pipelineIdentification), "\n "); }));
1243
+ throw new PipelineLogicError(spaceTrim(function (block) { return "\n Invalid promptbook URL \"".concat(pipeline.pipelineUrl, "\"\n\n ").concat(block(pipelineIdentification), "\n "); }));
1113
1244
  }
1114
1245
  if (!isValidPromptbookVersion(pipeline.promptbookVersion)) {
1115
1246
  // <- Note: [🚲]
1116
- throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n Invalid Promptbook Version \"".concat(pipeline.promptbookVersion, "\"\n\n ").concat(block(pipelineIdentification), "\n "); }));
1247
+ throw new PipelineLogicError(spaceTrim(function (block) { return "\n Invalid Promptbook Version \"".concat(pipeline.promptbookVersion, "\"\n\n ").concat(block(pipelineIdentification), "\n "); }));
1117
1248
  }
1118
1249
  // TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
1119
1250
  if (!Array.isArray(pipeline.parameters)) {
1120
1251
  // TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
1121
- throw new ParsingError(spaceTrim$1(function (block) { return "\n Promptbook is valid JSON but with wrong structure\n\n `promptbook.parameters` expected to be an array, but got ".concat(typeof pipeline.parameters, "\n\n ").concat(block(pipelineIdentification), "\n "); }));
1252
+ throw new ParsingError(spaceTrim(function (block) { return "\n Promptbook is valid JSON but with wrong structure\n\n `promptbook.parameters` expected to be an array, but got ".concat(typeof pipeline.parameters, "\n\n ").concat(block(pipelineIdentification), "\n "); }));
1122
1253
  }
1123
1254
  // TODO: [🧠] Maybe do here some propper JSON-schema / ZOD checking
1124
1255
  if (!Array.isArray(pipeline.promptTemplates)) {
1125
1256
  // TODO: [🧠] what is the correct error tp throw - maybe PromptbookSchemaError
1126
- throw new ParsingError(spaceTrim$1(function (block) { return "\n Promptbook is valid JSON but with wrong structure\n\n `promptbook.promptTemplates` expected to be an array, but got ".concat(typeof pipeline.promptTemplates, "\n\n ").concat(block(pipelineIdentification), "\n "); }));
1257
+ throw new ParsingError(spaceTrim(function (block) { return "\n Promptbook is valid JSON but with wrong structure\n\n `promptbook.promptTemplates` expected to be an array, but got ".concat(typeof pipeline.promptTemplates, "\n\n ").concat(block(pipelineIdentification), "\n "); }));
1127
1258
  }
1128
1259
  var _loop_1 = function (parameter) {
1129
1260
  if (parameter.isInput && parameter.isOutput) {
1130
- throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n\n Parameter {".concat(parameter.name, "} can not be both input and output\n\n ").concat(block(pipelineIdentification), "\n "); }));
1261
+ throw new PipelineLogicError(spaceTrim(function (block) { return "\n\n Parameter {".concat(parameter.name, "} can not be both input and output\n\n ").concat(block(pipelineIdentification), "\n "); }));
1131
1262
  }
1132
1263
  // Note: Testing that parameter is either intermediate or output BUT not created and unused
1133
1264
  if (!parameter.isInput &&
1134
1265
  !parameter.isOutput &&
1135
1266
  !pipeline.promptTemplates.some(function (template) { return template.dependentParameterNames.includes(parameter.name); })) {
1136
- throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n Parameter {".concat(parameter.name, "} is created but not used\n\n You can declare {").concat(parameter.name, "} as output parameter by adding in the header:\n - OUTPUT PARAMETER `{").concat(parameter.name, "}` ").concat(parameter.description || '', "\n\n ").concat(block(pipelineIdentification), "\n\n "); }));
1267
+ throw new PipelineLogicError(spaceTrim(function (block) { return "\n Parameter {".concat(parameter.name, "} is created but not used\n\n You can declare {").concat(parameter.name, "} as output parameter by adding in the header:\n - OUTPUT PARAMETER `{").concat(parameter.name, "}` ").concat(parameter.description || '', "\n\n ").concat(block(pipelineIdentification), "\n\n "); }));
1137
1268
  }
1138
1269
  // Note: Testing that parameter is either input or result of some template
1139
1270
  if (!parameter.isInput &&
1140
1271
  !pipeline.promptTemplates.some(function (template) { return template.resultingParameterName === parameter.name; })) {
1141
- throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n Parameter {".concat(parameter.name, "} is declared but not defined\n\n You can do one of these:\n - Remove declaration of {").concat(parameter.name, "}\n - Add prompt template that results in -> {").concat(parameter.name, "}\n\n ").concat(block(pipelineIdentification), "\n "); }));
1272
+ throw new PipelineLogicError(spaceTrim(function (block) { return "\n Parameter {".concat(parameter.name, "} is declared but not defined\n\n You can do one of these:\n - Remove declaration of {").concat(parameter.name, "}\n - Add prompt template that results in -> {").concat(parameter.name, "}\n\n ").concat(block(pipelineIdentification), "\n "); }));
1142
1273
  }
1143
1274
  };
1144
1275
  try {
@@ -1166,20 +1297,20 @@ function validatePipeline(pipeline) {
1166
1297
  var _loop_2 = function (template) {
1167
1298
  var e_4, _h, e_5, _j;
1168
1299
  if (definedParameters.has(template.resultingParameterName)) {
1169
- throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n Parameter {".concat(template.resultingParameterName, "} is defined multiple times\n\n ").concat(block(pipelineIdentification), "\n "); }));
1300
+ throw new PipelineLogicError(spaceTrim(function (block) { return "\n Parameter {".concat(template.resultingParameterName, "} is defined multiple times\n\n ").concat(block(pipelineIdentification), "\n "); }));
1170
1301
  }
1171
1302
  if (RESERVED_PARAMETER_NAMES.includes(template.resultingParameterName)) {
1172
- throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n Parameter name {".concat(template.resultingParameterName, "} is reserved, please use different name\n\n ").concat(block(pipelineIdentification), "\n "); }));
1303
+ throw new PipelineLogicError(spaceTrim(function (block) { return "\n Parameter name {".concat(template.resultingParameterName, "} is reserved, please use different name\n\n ").concat(block(pipelineIdentification), "\n "); }));
1173
1304
  }
1174
1305
  definedParameters.add(template.resultingParameterName);
1175
1306
  if (template.jokerParameterNames && template.jokerParameterNames.length > 0) {
1176
1307
  if (!template.expectFormat &&
1177
1308
  !template.expectations /* <- TODO: Require at least 1 -> min <- expectation to use jokers */) {
1178
- throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n Joker parameters are used for {".concat(template.resultingParameterName, "} but no expectations are defined\n\n ").concat(block(pipelineIdentification), "\n "); }));
1309
+ throw new PipelineLogicError(spaceTrim(function (block) { return "\n Joker parameters are used for {".concat(template.resultingParameterName, "} but no expectations are defined\n\n ").concat(block(pipelineIdentification), "\n "); }));
1179
1310
  }
1180
1311
  var _loop_4 = function (joker) {
1181
1312
  if (!template.dependentParameterNames.includes(joker)) {
1182
- throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n Parameter {".concat(joker, "} is used for {").concat(template.resultingParameterName, "} as joker but not in `dependentParameterNames`\n\n ").concat(block(pipelineIdentification), "\n "); }));
1313
+ throw new PipelineLogicError(spaceTrim(function (block) { return "\n Parameter {".concat(joker, "} is used for {").concat(template.resultingParameterName, "} as joker but not in `dependentParameterNames`\n\n ").concat(block(pipelineIdentification), "\n "); }));
1183
1314
  }
1184
1315
  };
1185
1316
  try {
@@ -1199,13 +1330,13 @@ function validatePipeline(pipeline) {
1199
1330
  if (template.expectations) {
1200
1331
  var _loop_5 = function (unit, min, max) {
1201
1332
  if (min !== undefined && max !== undefined && min > max) {
1202
- throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n Min expectation (=".concat(min, ") of ").concat(unit, " is higher than max expectation (=").concat(max, ")\n\n ").concat(block(pipelineIdentification), "\n "); }));
1333
+ throw new PipelineLogicError(spaceTrim(function (block) { return "\n Min expectation (=".concat(min, ") of ").concat(unit, " is higher than max expectation (=").concat(max, ")\n\n ").concat(block(pipelineIdentification), "\n "); }));
1203
1334
  }
1204
1335
  if (min !== undefined && min < 0) {
1205
- throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n Min expectation of ".concat(unit, " must be zero or positive\n\n ").concat(block(pipelineIdentification), "\n "); }));
1336
+ throw new PipelineLogicError(spaceTrim(function (block) { return "\n Min expectation of ".concat(unit, " must be zero or positive\n\n ").concat(block(pipelineIdentification), "\n "); }));
1206
1337
  }
1207
1338
  if (max !== undefined && max <= 0) {
1208
- throw new PipelineLogicError(spaceTrim$1(function (block) { return "\n Max expectation of ".concat(unit, " must be positive\n\n ").concat(block(pipelineIdentification), "\n "); }));
1339
+ throw new PipelineLogicError(spaceTrim(function (block) { return "\n Max expectation of ".concat(unit, " must be positive\n\n ").concat(block(pipelineIdentification), "\n "); }));
1209
1340
  }
1210
1341
  };
1211
1342
  try {
@@ -1267,7 +1398,7 @@ function validatePipeline(pipeline) {
1267
1398
  var _loop_3 = function () {
1268
1399
  if (loopLimit-- < 0) {
1269
1400
  // Note: Really UnexpectedError not LimitReachedError - this should not happen and be caught below
1270
- throw new UnexpectedError(spaceTrim$1(function (block) { return "\n Loop limit reached during detection of circular dependencies in `validatePipeline`\n\n ".concat(block(pipelineIdentification), "\n "); }));
1401
+ throw new UnexpectedError(spaceTrim(function (block) { return "\n Loop limit reached during detection of circular dependencies in `validatePipeline`\n\n ".concat(block(pipelineIdentification), "\n "); }));
1271
1402
  }
1272
1403
  var currentlyResovedTemplates = unresovedTemplates.filter(function (template) {
1273
1404
  return template.dependentParameterNames.every(function (name) { return resovedParameters.includes(name); });
@@ -1275,7 +1406,7 @@ function validatePipeline(pipeline) {
1275
1406
  if (currentlyResovedTemplates.length === 0) {
1276
1407
  throw new PipelineLogicError(
1277
1408
  // TODO: [🐎] DRY
1278
- spaceTrim$1(function (block) { return "\n\n Can not resolve some parameters:\n Either you are using a parameter that is not defined, or there are some circular dependencies.\n\n Can not resolve:\n ".concat(block(unresovedTemplates
1409
+ spaceTrim(function (block) { return "\n\n Can not resolve some parameters:\n Either you are using a parameter that is not defined, or there are some circular dependencies.\n\n Can not resolve:\n ".concat(block(unresovedTemplates
1279
1410
  .map(function (_a) {
1280
1411
  var resultingParameterName = _a.resultingParameterName, dependentParameterNames = _a.dependentParameterNames;
1281
1412
  return "- Parameter {".concat(resultingParameterName, "} which depends on ").concat(dependentParameterNames
@@ -1337,15 +1468,15 @@ var NotFoundError = /** @class */ (function (_super) {
1337
1468
  *
1338
1469
  * @public exported from `@promptbook/core`
1339
1470
  */
1340
- var ReferenceError$1 = /** @class */ (function (_super) {
1341
- __extends(ReferenceError, _super);
1342
- function ReferenceError(message) {
1471
+ var PipelineUrlError = /** @class */ (function (_super) {
1472
+ __extends(PipelineUrlError, _super);
1473
+ function PipelineUrlError(message) {
1343
1474
  var _this = _super.call(this, message) || this;
1344
- _this.name = 'ReferenceError';
1345
- Object.setPrototypeOf(_this, ReferenceError.prototype);
1475
+ _this.name = 'PipelineUrlError';
1476
+ Object.setPrototypeOf(_this, PipelineUrlError.prototype);
1346
1477
  return _this;
1347
1478
  }
1348
- return ReferenceError;
1479
+ return PipelineUrlError;
1349
1480
  }(Error));
1350
1481
 
1351
1482
  /**
@@ -1393,7 +1524,7 @@ function unpreparePipeline(pipeline) {
1393
1524
  delete promptTemplateUnprepared.preparedContent;
1394
1525
  return promptTemplateUnprepared;
1395
1526
  });
1396
- return __assign(__assign({}, pipeline), { promptTemplates: promptTemplates, knowledgeSources: knowledgeSources, knowledgePieces: [], personas: personas, preparations: [] });
1527
+ return $asDeeplyFrozenSerializableJson('Unprepared PipelineJson', __assign(__assign({}, pipeline), { promptTemplates: promptTemplates, knowledgeSources: knowledgeSources, knowledgePieces: [], personas: personas, preparations: [] }));
1397
1528
  }
1398
1529
  /**
1399
1530
  * TODO: [🧿] Maybe do same process with same granularity and subfinctions as `preparePipeline`
@@ -1429,7 +1560,7 @@ var SimplePipelineCollection = /** @class */ (function () {
1429
1560
  var pipeline = pipelines_1_1.value;
1430
1561
  // TODO: [👠] DRY
1431
1562
  if (pipeline.pipelineUrl === undefined) {
1432
- throw new ReferenceError$1(spaceTrim$1("\n Pipeline with name \"".concat(pipeline.title, "\" does not have defined URL\n\n File:\n ").concat(pipeline.sourceFile || 'Unknown', "\n\n Note: Pipelines without URLs are called anonymous pipelines\n They can be used as standalone pipelines, but they cannot be referenced by other pipelines\n And also they cannot be used in the pipeline collection\n\n ")));
1563
+ throw new PipelineUrlError(spaceTrim("\n Pipeline with name \"".concat(pipeline.title, "\" does not have defined URL\n\n File:\n ").concat(pipeline.sourceFile || 'Unknown', "\n\n Note: Pipelines without URLs are called anonymous pipelines\n They can be used as standalone pipelines, but they cannot be referenced by other pipelines\n And also they cannot be used in the pipeline collection\n\n ")));
1433
1564
  }
1434
1565
  // Note: [🐨]
1435
1566
  validatePipeline(pipeline);
@@ -1441,7 +1572,7 @@ var SimplePipelineCollection = /** @class */ (function () {
1441
1572
  pipelineJsonToString(unpreparePipeline(pipeline)) !==
1442
1573
  pipelineJsonToString(unpreparePipeline(this.collection.get(pipeline.pipelineUrl)))) {
1443
1574
  var existing = this.collection.get(pipeline.pipelineUrl);
1444
- throw new ReferenceError$1(spaceTrim$1("\n Pipeline with URL \"".concat(pipeline.pipelineUrl, "\" is already in the collection \uD83C\uDF4E\n\n Conflicting files:\n ").concat(existing.sourceFile || 'Unknown', "\n ").concat(pipeline.sourceFile || 'Unknown', "\n\n Note: You have probably forgotten to run \"ptbk make\" to update the collection\n Note: Pipelines with the same URL are not allowed\n Only exepction is when the pipelines are identical\n\n ")));
1575
+ throw new PipelineUrlError(spaceTrim("\n Pipeline with URL \"".concat(pipeline.pipelineUrl, "\" is already in the collection \uD83C\uDF4E\n\n Conflicting files:\n ").concat(existing.sourceFile || 'Unknown', "\n ").concat(pipeline.sourceFile || 'Unknown', "\n\n Note: You have probably forgotten to run \"ptbk make\" to update the collection\n Note: Pipelines with the same URL are not allowed\n Only exepction is when the pipelines are identical\n\n ")));
1445
1576
  }
1446
1577
  // Note: [🧠] Overwrite existing pipeline with the same URL
1447
1578
  this.collection.set(pipeline.pipelineUrl, pipeline);
@@ -1471,9 +1602,9 @@ var SimplePipelineCollection = /** @class */ (function () {
1471
1602
  var pipeline = this.collection.get(url);
1472
1603
  if (!pipeline) {
1473
1604
  if (this.listPipelines().length === 0) {
1474
- throw new NotFoundError(spaceTrim$1("\n Pipeline with url \"".concat(url, "\" not found\n\n No pipelines available\n ")));
1605
+ throw new NotFoundError(spaceTrim("\n Pipeline with url \"".concat(url, "\" not found\n\n No pipelines available\n ")));
1475
1606
  }
1476
- throw new NotFoundError(spaceTrim$1(function (block) { return "\n Pipeline with url \"".concat(url, "\" not found\n\n Available pipelines:\n ").concat(block(_this.listPipelines()
1607
+ throw new NotFoundError(spaceTrim(function (block) { return "\n Pipeline with url \"".concat(url, "\" not found\n\n Available pipelines:\n ").concat(block(_this.listPipelines()
1477
1608
  .map(function (pipelineUrl) { return "- ".concat(pipelineUrl); })
1478
1609
  .join('\n')), "\n\n "); }));
1479
1610
  }
@@ -1886,9 +2017,89 @@ var PipelineExecutionError = /** @class */ (function (_super) {
1886
2017
  }(Error));
1887
2018
 
1888
2019
  /**
1889
- * Asserts that the execution of a promptnook is successful
2020
+ * This error indicates that the pipeline collection cannot be propperly loaded
2021
+ *
2022
+ * @public exported from `@promptbook/core`
2023
+ */
2024
+ var CollectionError = /** @class */ (function (_super) {
2025
+ __extends(CollectionError, _super);
2026
+ function CollectionError(message) {
2027
+ var _this = _super.call(this, message) || this;
2028
+ _this.name = 'CollectionError';
2029
+ Object.setPrototypeOf(_this, CollectionError.prototype);
2030
+ return _this;
2031
+ }
2032
+ return CollectionError;
2033
+ }(Error));
2034
+
2035
+ /**
2036
+ * This error type indicates that some limit was reached
2037
+ *
2038
+ * @public exported from `@promptbook/core`
2039
+ */
2040
+ var LimitReachedError = /** @class */ (function (_super) {
2041
+ __extends(LimitReachedError, _super);
2042
+ function LimitReachedError(message) {
2043
+ var _this = _super.call(this, message) || this;
2044
+ _this.name = 'LimitReachedError';
2045
+ Object.setPrototypeOf(_this, LimitReachedError.prototype);
2046
+ return _this;
2047
+ }
2048
+ return LimitReachedError;
2049
+ }(Error));
2050
+
2051
+ /**
2052
+ * This error type indicates that some part of the code is not implemented yet
1890
2053
  *
1891
- * @param executionResult - The partial result of the promptnook execution
2054
+ * @public exported from `@promptbook/core`
2055
+ */
2056
+ var NotYetImplementedError = /** @class */ (function (_super) {
2057
+ __extends(NotYetImplementedError, _super);
2058
+ function NotYetImplementedError(message) {
2059
+ var _this = _super.call(this, spaceTrim(function (block) { return "\n ".concat(block(message), "\n\n Note: This feature is not implemented yet but it will be soon.\n\n If you want speed up the implementation or just read more, look here:\n https://github.com/webgptorg/promptbook\n\n Or contact us on me@pavolhejny.com\n\n "); })) || this;
2060
+ _this.name = 'NotYetImplementedError';
2061
+ Object.setPrototypeOf(_this, NotYetImplementedError.prototype);
2062
+ return _this;
2063
+ }
2064
+ return NotYetImplementedError;
2065
+ }(Error));
2066
+
2067
+ /**
2068
+ * Index of all custom errors
2069
+ *
2070
+ * @public exported from `@promptbook/core`
2071
+ */
2072
+ var ERRORS = {
2073
+ CollectionError: CollectionError,
2074
+ EnvironmentMismatchError: EnvironmentMismatchError,
2075
+ LimitReachedError: LimitReachedError,
2076
+ NotFoundError: NotFoundError,
2077
+ NotYetImplementedError: NotYetImplementedError,
2078
+ ParsingError: ParsingError,
2079
+ PipelineExecutionError: PipelineExecutionError,
2080
+ PipelineLogicError: PipelineLogicError,
2081
+ PipelineUrlError: PipelineUrlError,
2082
+ UnexpectedError: UnexpectedError,
2083
+ // TODO: [🪑]> VersionMismatchError,
2084
+ };
2085
+
2086
+ /**
2087
+ * Deserializes the error object
2088
+ *
2089
+ * @public exported from `@promptbook/utils`
2090
+ */
2091
+ function deserializeError(error) {
2092
+ if (error.name === 'Error') {
2093
+ return new Error(error.message);
2094
+ }
2095
+ var CustomError = ERRORS[error.name];
2096
+ return new CustomError(error.message);
2097
+ }
2098
+
2099
+ /**
2100
+ * Asserts that the execution of a Promptbook is successful
2101
+ *
2102
+ * @param executionResult - The partial result of the Promptbook execution
1892
2103
  * @throws {PipelineExecutionError} If the execution is not successful or if multiple errors occurred
1893
2104
  * @public exported from `@promptbook/core`
1894
2105
  */
@@ -1898,15 +2109,16 @@ function assertsExecutionSuccessful(executionResult) {
1898
2109
  return;
1899
2110
  }
1900
2111
  if (errors.length === 0) {
1901
- throw new PipelineExecutionError("Promptnook Execution failed because of unknown reason");
2112
+ throw new PipelineExecutionError("Promptbook Execution failed because of unknown reason");
1902
2113
  }
1903
2114
  else if (errors.length === 1) {
1904
- throw errors[0];
2115
+ throw deserializeError(errors[0]);
1905
2116
  }
1906
2117
  else {
1907
- throw new PipelineExecutionError(spaceTrim$1(function (block) { return "\n Multiple errors occurred during promptnook execution\n\n ".concat(block(errors
1908
- .map(function (error, index) {
1909
- return spaceTrim$1(function (block) { return "\n Error ".concat(index + 1, ":\n ").concat(block(error.stack || error.message), "\n "); });
2118
+ throw new PipelineExecutionError(spaceTrim(function (block) { return "\n Multiple errors occurred during Promptbook execution\n\n ".concat(block(errors
2119
+ .map(function (_a, index) {
2120
+ var name = _a.name, stack = _a.stack, message = _a.message;
2121
+ return spaceTrim(function (block) { return "\n ".concat(name, " ").concat(index + 1, ":\n ").concat(block(stack || message), "\n "); });
1910
2122
  })
1911
2123
  .join('\n')), "\n "); }));
1912
2124
  }
@@ -1938,7 +2150,7 @@ function extractVariables(script) {
1938
2150
  var undefinedName = error.message.split(' ')[0];
1939
2151
  /*
1940
2152
  Note: Parsing the error
1941
- [ReferenceError: thing is not defined]
2153
+ [PipelineUrlError: thing is not defined]
1942
2154
  */
1943
2155
  if (!undefinedName) {
1944
2156
  throw error;
@@ -1956,7 +2168,7 @@ function extractVariables(script) {
1956
2168
  if (!(error instanceof Error)) {
1957
2169
  throw error;
1958
2170
  }
1959
- throw new ParsingError(spaceTrim$1(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.name), ": ").concat(block(error.message), "\n "); }));
2171
+ throw new ParsingError(spaceTrim(function (block) { return "\n Can not extract variables from the script\n\n ".concat(block(error.toString()), "}\n "); }));
1960
2172
  }
1961
2173
  return variables;
1962
2174
  }
@@ -2043,6 +2255,23 @@ var ExpectError = /** @class */ (function (_super) {
2043
2255
  return ExpectError;
2044
2256
  }(Error));
2045
2257
 
2258
+ /**
2259
+ * Serializes an error into a [🚉] JSON-serializable object
2260
+ *
2261
+ * @public exported from `@promptbook/utils`
2262
+ */
2263
+ function serializeError(error) {
2264
+ var name = error.name, message = error.message, stack = error.stack;
2265
+ if (!__spreadArray(['Error'], __read(Object.keys(ERRORS)), false).includes(name)) {
2266
+ throw new UnexpectedError(spaceTrim$1(function (block) { return "\n \n Cannot serialize error with name \"".concat(name, "\"\n\n ").concat(block(stack || message), "\n \n "); }));
2267
+ }
2268
+ return {
2269
+ name: name,
2270
+ message: message,
2271
+ stack: stack,
2272
+ };
2273
+ }
2274
+
2046
2275
  /**
2047
2276
  * Function isValidJsonString will tell you if the string is valid JSON or not
2048
2277
  *
@@ -2282,7 +2511,7 @@ var MultipleLlmExecutionTools = /** @class */ (function () {
2282
2511
  // 1) OpenAI throw PipelineExecutionError: Parameter {knowledge} is not defined
2283
2512
  // 2) AnthropicClaude throw PipelineExecutionError: Parameter {knowledge} is not defined
2284
2513
  // 3) ...
2285
- spaceTrim(function (block) { return "\n All execution tools failed:\n\n ".concat(block(errors
2514
+ spaceTrim$1(function (block) { return "\n All execution tools failed:\n\n ".concat(block(errors
2286
2515
  .map(function (error, i) { return "".concat(i + 1, ") **").concat(error.name || 'Error', ":** ").concat(error.message); })
2287
2516
  .join('\n')), "\n\n "); }));
2288
2517
  }
@@ -2290,7 +2519,7 @@ var MultipleLlmExecutionTools = /** @class */ (function () {
2290
2519
  throw new PipelineExecutionError("You have not provided any `LlmExecutionTools`");
2291
2520
  }
2292
2521
  else {
2293
- throw new PipelineExecutionError(spaceTrim(function (block) { return "\n You have not provided any `LlmExecutionTools` that support model variant \"".concat(prompt.modelRequirements.modelVariant, "\n\n Available `LlmExecutionTools`:\n ").concat(block(_this.llmExecutionTools
2522
+ throw new PipelineExecutionError(spaceTrim$1(function (block) { return "\n You have not provided any `LlmExecutionTools` that support model variant \"".concat(prompt.modelRequirements.modelVariant, "\n\n Available `LlmExecutionTools`:\n ").concat(block(_this.llmExecutionTools
2294
2523
  .map(function (tools) { return "- ".concat(tools.title, " ").concat(tools.description || ''); })
2295
2524
  .join('\n')), "\n\n "); }));
2296
2525
  }
@@ -2327,7 +2556,7 @@ function joinLlmExecutionTools() {
2327
2556
  llmExecutionTools[_i] = arguments[_i];
2328
2557
  }
2329
2558
  if (llmExecutionTools.length === 0) {
2330
- var warningMessage = spaceTrim("\n You have not provided any `LlmExecutionTools`\n This means that you won't be able to execute any prompts that require large language models like GPT-4 or Anthropic's Claude.\n\n Technically, it's not an error, but it's probably not what you want because it does not make sense to use Promptbook without language models.\n ");
2559
+ var warningMessage = spaceTrim$1("\n You have not provided any `LlmExecutionTools`\n This means that you won't be able to execute any prompts that require large language models like GPT-4 or Anthropic's Claude.\n\n Technically, it's not an error, but it's probably not what you want because it does not make sense to use Promptbook without language models.\n ");
2331
2560
  // TODO: [🟥] Detect browser / node and make it colorfull
2332
2561
  console.warn(warningMessage);
2333
2562
  /*
@@ -2380,6 +2609,7 @@ function isPipelinePrepared(pipeline) {
2380
2609
  return true;
2381
2610
  }
2382
2611
  /**
2612
+ * TODO: [🔃] !!!!! If the pipeline was prepared with different version or different set of models, prepare it once again
2383
2613
  * TODO: [🐠] Maybe base this on `makeValidator`
2384
2614
  * TODO: [🧊] Pipeline can be partially prepared, this should return true ONLY if fully prepared
2385
2615
  * TODO: [🧿] Maybe do same process with same granularity and subfinctions as `preparePipeline`
@@ -2424,22 +2654,6 @@ function TODO_USE() {
2424
2654
  }
2425
2655
  }
2426
2656
 
2427
- /**
2428
- * This error type indicates that some limit was reached
2429
- *
2430
- * @public exported from `@promptbook/core`
2431
- */
2432
- var LimitReachedError = /** @class */ (function (_super) {
2433
- __extends(LimitReachedError, _super);
2434
- function LimitReachedError(message) {
2435
- var _this = _super.call(this, message) || this;
2436
- _this.name = 'LimitReachedError';
2437
- Object.setPrototypeOf(_this, LimitReachedError.prototype);
2438
- return _this;
2439
- }
2440
- return LimitReachedError;
2441
- }(Error));
2442
-
2443
2657
  /**
2444
2658
  * Replaces parameters in template with values from parameters object
2445
2659
  *
@@ -2739,7 +2953,7 @@ function createPipelineExecutor(options) {
2739
2953
  preparedPipeline = pipeline;
2740
2954
  }
2741
2955
  else if (isNotPreparedWarningSupressed !== true) {
2742
- console.warn(spaceTrim$1("\n Pipeline ".concat(pipeline.pipelineUrl || pipeline.sourceFile || pipeline.title, " is not prepared\n\n ").concat(pipeline.sourceFile, "\n\n It will be prepared ad-hoc before the first execution and **returned as `preparedPipeline` in `PipelineExecutorResult`**\n But it is recommended to prepare the pipeline during collection preparation\n\n @see more at https://ptbk.io/prepare-pipeline\n ")));
2956
+ console.warn(spaceTrim("\n Pipeline ".concat(pipeline.pipelineUrl || pipeline.sourceFile || pipeline.title, " is not prepared\n\n ").concat(pipeline.sourceFile, "\n\n It will be prepared ad-hoc before the first execution and **returned as `preparedPipeline` in `PipelineExecutorResult`**\n But it is recommended to prepare the pipeline during collection preparation\n\n @see more at https://ptbk.io/prepare-pipeline\n ")));
2743
2957
  }
2744
2958
  var pipelineExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
2745
2959
  // TODO: !!! Extract to separate functions and files - ALL FUNCTIONS BELOW
@@ -2825,7 +3039,6 @@ function createPipelineExecutor(options) {
2825
3039
  return __awaiter(this, void 0, void 0, function () {
2826
3040
  var name, title, priority, usedParameterNames, dependentParameterNames, definedParameters, _a, _b, _c, definedParameterNames, parameters, _d, _e, parameterName, prompt, chatResult, completionResult, embeddingResult, result, resultString, expectError, scriptPipelineExecutionErrors, maxAttempts, jokerParameterNames, preparedContent, attempt, isJokerAttempt, jokerParameterName, _f, _g, _h, _j, scriptTools, error_2, e_4_1, _k, _l, functionName, postprocessingError, _m, _o, scriptTools, error_3, e_5_1, e_6_1, error_4;
2827
3041
  var e_7, _p, e_4, _q, e_6, _r, e_5, _s, _t;
2828
- var _this = this;
2829
3042
  return __generator(this, function (_u) {
2830
3043
  switch (_u.label) {
2831
3044
  case 0:
@@ -2850,7 +3063,7 @@ function createPipelineExecutor(options) {
2850
3063
  usedParameterNames = extractParameterNamesFromPromptTemplate(currentTemplate);
2851
3064
  dependentParameterNames = new Set(currentTemplate.dependentParameterNames);
2852
3065
  if (union(difference(usedParameterNames, dependentParameterNames), difference(dependentParameterNames, usedParameterNames)).size !== 0) {
2853
- throw new UnexpectedError(spaceTrim$1("\n Dependent parameters are not consistent used parameters:\n\n Dependent parameters:\n ".concat(Array.from(dependentParameterNames).join(', '), "\n\n Used parameters:\n ").concat(Array.from(usedParameterNames).join(', '), "\n\n ")));
3066
+ throw new UnexpectedError(spaceTrim("\n Dependent parameters are not consistent used parameters:\n\n Dependent parameters:\n ".concat(Array.from(dependentParameterNames).join(', '), "\n\n Used parameters:\n ").concat(Array.from(usedParameterNames).join(', '), "\n\n ")));
2854
3067
  }
2855
3068
  _b = (_a = Object).freeze;
2856
3069
  _c = [{}];
@@ -2875,7 +3088,7 @@ function createPipelineExecutor(options) {
2875
3088
  else if (!definedParameterNames.has(parameterName) && usedParameterNames.has(parameterName)) {
2876
3089
  // Houston, we have a problem
2877
3090
  // Note: Checking part is also done in `validatePipeline`, but it’s good to doublecheck
2878
- throw new UnexpectedError(spaceTrim$1("\n Parameter {".concat(parameterName, "} is NOT defined\n BUT used in template \"").concat(currentTemplate.title || currentTemplate.name, "\"\n\n This should be catched in `validatePipeline`\n\n ")));
3091
+ throw new UnexpectedError(spaceTrim("\n Parameter {".concat(parameterName, "} is NOT defined\n BUT used in template \"").concat(currentTemplate.title || currentTemplate.name, "\"\n\n This should be catched in `validatePipeline`\n\n ")));
2879
3092
  }
2880
3093
  }
2881
3094
  }
@@ -2946,71 +3159,8 @@ function createPipelineExecutor(options) {
2946
3159
  return name === currentTemplate.personaName;
2947
3160
  }) || {})), currentTemplate.expectations),
2948
3161
  expectFormat: currentTemplate.expectFormat,
2949
- postprocessing: (currentTemplate.postprocessingFunctionNames || []).map(function (functionName) { return function (result) { return __awaiter(_this, void 0, void 0, function () {
2950
- var errors, _a, _b, scriptTools, error_5, e_8_1;
2951
- var e_8, _c;
2952
- return __generator(this, function (_d) {
2953
- switch (_d.label) {
2954
- case 0:
2955
- errors = [];
2956
- _d.label = 1;
2957
- case 1:
2958
- _d.trys.push([1, 8, 9, 10]);
2959
- _a = __values(arrayableToArray(tools.script)), _b = _a.next();
2960
- _d.label = 2;
2961
- case 2:
2962
- if (!!_b.done) return [3 /*break*/, 7];
2963
- scriptTools = _b.value;
2964
- _d.label = 3;
2965
- case 3:
2966
- _d.trys.push([3, 5, , 6]);
2967
- return [4 /*yield*/, scriptTools.execute({
2968
- scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
2969
- script: "".concat(functionName, "(result)"),
2970
- parameters: {
2971
- result: result || '',
2972
- // Note: No ...parametersForTemplate, because working with result only
2973
- },
2974
- })];
2975
- case 4: return [2 /*return*/, _d.sent()];
2976
- case 5:
2977
- error_5 = _d.sent();
2978
- if (!(error_5 instanceof Error)) {
2979
- throw error_5;
2980
- }
2981
- if (error_5 instanceof UnexpectedError) {
2982
- throw error_5;
2983
- }
2984
- errors.push(error_5);
2985
- return [3 /*break*/, 6];
2986
- case 6:
2987
- _b = _a.next();
2988
- return [3 /*break*/, 2];
2989
- case 7: return [3 /*break*/, 10];
2990
- case 8:
2991
- e_8_1 = _d.sent();
2992
- e_8 = { error: e_8_1 };
2993
- return [3 /*break*/, 10];
2994
- case 9:
2995
- try {
2996
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
2997
- }
2998
- finally { if (e_8) throw e_8.error; }
2999
- return [7 /*endfinally*/];
3000
- case 10:
3001
- if (errors.length === 0) {
3002
- throw new PipelineExecutionError('Postprocessing in LlmExecutionTools failed because no ScriptExecutionTools were provided');
3003
- }
3004
- else if (errors.length === 1) {
3005
- throw errors[0];
3006
- }
3007
- else {
3008
- throw new PipelineExecutionError(spaceTrim$1(function (block) { return "\n Postprocessing in LlmExecutionTools failed ".concat(errors.length, "x\n\n ").concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n\n')), "\n "); }));
3009
- }
3010
- }
3011
- });
3012
- }); }; }),
3013
- };
3162
+ postprocessingFunctionNames: currentTemplate.postprocessingFunctionNames,
3163
+ }; // <- TODO: Not very good type guard
3014
3164
  _g = currentTemplate.modelRequirements.modelVariant;
3015
3165
  switch (_g) {
3016
3166
  case 'CHAT': return [3 /*break*/, 8];
@@ -3099,7 +3249,7 @@ function createPipelineExecutor(options) {
3099
3249
  throw scriptPipelineExecutionErrors[0];
3100
3250
  }
3101
3251
  else {
3102
- throw new PipelineExecutionError(spaceTrim$1(function (block) { return "\n Script execution failed ".concat(scriptPipelineExecutionErrors.length, " times\n\n ").concat(block(scriptPipelineExecutionErrors
3252
+ throw new PipelineExecutionError(spaceTrim(function (block) { return "\n Script execution failed ".concat(scriptPipelineExecutionErrors.length, " times\n\n ").concat(block(scriptPipelineExecutionErrors
3103
3253
  .map(function (error) { return '- ' + error.message; })
3104
3254
  .join('\n\n')), "\n "); }));
3105
3255
  }
@@ -3235,13 +3385,13 @@ function createPipelineExecutor(options) {
3235
3385
  executionReport.promptExecutions.push({
3236
3386
  prompt: __assign({}, prompt),
3237
3387
  result: result || undefined,
3238
- error: expectError || undefined,
3388
+ error: expectError === null ? undefined : serializeError(expectError),
3239
3389
  });
3240
3390
  }
3241
3391
  return [7 /*endfinally*/];
3242
3392
  case 50:
3243
3393
  if (expectError !== null && attempt === maxAttempts - 1) {
3244
- throw new PipelineExecutionError(spaceTrim$1(function (block) { return "\n LLM execution failed ".concat(maxExecutionAttempts, "x\n\n ---\n Last error ").concat((expectError === null || expectError === void 0 ? void 0 : expectError.name) || '', ":\n ").concat(block((expectError === null || expectError === void 0 ? void 0 : expectError.message) || ''), "\n\n Last result:\n ").concat(resultString, "\n ---\n "); }));
3394
+ throw new PipelineExecutionError(spaceTrim(function (block) { return "\n LLM execution failed ".concat(maxExecutionAttempts, "x\n\n ---\n Last error ").concat((expectError === null || expectError === void 0 ? void 0 : expectError.name) || '', ":\n ").concat(block((expectError === null || expectError === void 0 ? void 0 : expectError.message) || ''), "\n\n Last result:\n ").concat(resultString, "\n ---\n "); }));
3245
3395
  }
3246
3396
  _u.label = 51;
3247
3397
  case 51:
@@ -3270,7 +3420,7 @@ function createPipelineExecutor(options) {
3270
3420
  });
3271
3421
  }
3272
3422
  function filterJustOutputParameters() {
3273
- var e_9, _a;
3423
+ var e_8, _a;
3274
3424
  var outputParameters = {};
3275
3425
  try {
3276
3426
  // Note: Filter ONLY output parameters
@@ -3287,12 +3437,12 @@ function createPipelineExecutor(options) {
3287
3437
  outputParameters[parameter.name] = parametersToPass[parameter.name] || '';
3288
3438
  }
3289
3439
  }
3290
- catch (e_9_1) { e_9 = { error: e_9_1 }; }
3440
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
3291
3441
  finally {
3292
3442
  try {
3293
3443
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
3294
3444
  }
3295
- finally { if (e_9) throw e_9.error; }
3445
+ finally { if (e_8) throw e_8.error; }
3296
3446
  }
3297
3447
  return outputParameters;
3298
3448
  }
@@ -3329,11 +3479,11 @@ function createPipelineExecutor(options) {
3329
3479
  })), _b = _a.next(); !_b.done; _b = _a.next()) {
3330
3480
  parameter = _b.value;
3331
3481
  if (inputParameters[parameter.name] === undefined) {
3332
- return [2 /*return*/, deepFreezeWithSameType({
3482
+ return [2 /*return*/, $asDeeplyFrozenSerializableJson("Unuccessful PipelineExecutorResult (with missing parameter {".concat(parameter.name, "}) PipelineExecutorResult"), {
3333
3483
  isSuccessful: false,
3334
3484
  errors: __spreadArray([
3335
3485
  new PipelineExecutionError("Parameter {".concat(parameter.name, "} is required as an input parameter"))
3336
- ], __read(errors), false),
3486
+ ], __read(errors), false).map(serializeError),
3337
3487
  warnings: [],
3338
3488
  executionReport: executionReport,
3339
3489
  outputParameters: {},
@@ -3359,12 +3509,12 @@ function createPipelineExecutor(options) {
3359
3509
  warnings.push(new PipelineExecutionError("Extra parameter {".concat(parameterName, "} is being passed which is not part of the pipeline.")));
3360
3510
  }
3361
3511
  else if (parameter.isInput === false) {
3362
- return { value: deepFreezeWithSameType({
3512
+ return { value: $asDeeplyFrozenSerializableJson("Unuccessful PipelineExecutorResult (with extra parameter {".concat(parameter.name, "}) PipelineExecutorResult"), {
3363
3513
  isSuccessful: false,
3364
3514
  errors: __spreadArray([
3365
3515
  new PipelineExecutionError("Parameter {".concat(parameter.name, "} is passed as input parameter but it is not input"))
3366
- ], __read(errors), false),
3367
- warnings: warnings,
3516
+ ], __read(errors), false).map(serializeError),
3517
+ warnings: warnings.map(serializeError),
3368
3518
  executionReport: executionReport,
3369
3519
  outputParameters: {},
3370
3520
  usage: ZERO_USAGE,
@@ -3421,7 +3571,7 @@ function createPipelineExecutor(options) {
3421
3571
  if (!(!currentTemplate && resolving_1.length === 0)) return [3 /*break*/, 1];
3422
3572
  throw new UnexpectedError(
3423
3573
  // TODO: [🐎] DRY
3424
- spaceTrim$1(function (block) { return "\n Can not resolve some parameters:\n\n Can not resolve:\n ".concat(block(unresovedTemplates_1
3574
+ spaceTrim(function (block) { return "\n Can not resolve some parameters:\n\n Can not resolve:\n ".concat(block(unresovedTemplates_1
3425
3575
  .map(function (_a) {
3426
3576
  var resultingParameterName = _a.resultingParameterName, dependentParameterNames = _a.dependentParameterNames;
3427
3577
  return "- Parameter {".concat(resultingParameterName, "} which depends on ").concat(dependentParameterNames
@@ -3471,10 +3621,10 @@ function createPipelineExecutor(options) {
3471
3621
  return (result === null || result === void 0 ? void 0 : result.usage) || ZERO_USAGE;
3472
3622
  })), false));
3473
3623
  outputParameters_1 = filterJustOutputParameters();
3474
- return [2 /*return*/, deepFreezeWithSameType({
3624
+ return [2 /*return*/, $asDeeplyFrozenSerializableJson('Unuccessful PipelineExecutorResult (with misc errors) PipelineExecutorResult', {
3475
3625
  isSuccessful: false,
3476
- errors: __spreadArray([error_1], __read(errors), false),
3477
- warnings: warnings,
3626
+ errors: __spreadArray([error_1], __read(errors), false).map(serializeError),
3627
+ warnings: warnings.map(serializeError),
3478
3628
  usage: usage_1,
3479
3629
  executionReport: executionReport,
3480
3630
  outputParameters: outputParameters_1,
@@ -3486,10 +3636,10 @@ function createPipelineExecutor(options) {
3486
3636
  return (result === null || result === void 0 ? void 0 : result.usage) || ZERO_USAGE;
3487
3637
  })), false));
3488
3638
  outputParameters = filterJustOutputParameters();
3489
- return [2 /*return*/, deepFreezeWithSameType({
3639
+ return [2 /*return*/, $asDeeplyFrozenSerializableJson('Successful PipelineExecutorResult', {
3490
3640
  isSuccessful: true,
3491
- errors: errors,
3492
- warnings: warnings,
3641
+ errors: errors.map(serializeError),
3642
+ warnings: warnings.map(serializeError),
3493
3643
  usage: usage,
3494
3644
  executionReport: executionReport,
3495
3645
  outputParameters: outputParameters,
@@ -3575,8 +3725,8 @@ function prepareKnowledgeFromMarkdown(knowledgeContent /* <- TODO: [🖖] (?mayb
3575
3725
  switch (_c.label) {
3576
3726
  case 0:
3577
3727
  name = "piece-".concat(i);
3578
- title = spaceTrim(knowledgeTextPiece.substring(0, 100));
3579
- knowledgePieceContent = spaceTrim(knowledgeTextPiece);
3728
+ title = spaceTrim$1(knowledgeTextPiece.substring(0, 100));
3729
+ knowledgePieceContent = spaceTrim$1(knowledgeTextPiece);
3580
3730
  keywords = [];
3581
3731
  index = [];
3582
3732
  _c.label = 1;
@@ -3586,7 +3736,7 @@ function prepareKnowledgeFromMarkdown(knowledgeContent /* <- TODO: [🖖] (?mayb
3586
3736
  case 2:
3587
3737
  titleResult = _c.sent();
3588
3738
  _a = titleResult.outputParameters.title, titleRaw = _a === void 0 ? 'Untitled' : _a;
3589
- title = spaceTrim(titleRaw) /* <- TODO: Maybe do in pipeline */;
3739
+ title = spaceTrim$1(titleRaw) /* <- TODO: Maybe do in pipeline */;
3590
3740
  name = titleToName(title);
3591
3741
  return [4 /*yield*/, prepareKeywordsExecutor({ knowledgePieceContent: knowledgePieceContent })];
3592
3742
  case 3:
@@ -3859,6 +4009,7 @@ function preparePersona(personaDescription, options) {
3859
4009
  });
3860
4010
  }
3861
4011
  /**
4012
+ * TODO: [🔃] !!!!! If the persona was prepared with different version or different set of models, prepare it once again
3862
4013
  * TODO: [🏢] !! Check validity of `modelName` in pipeline
3863
4014
  * TODO: [🏢] !! Check validity of `systemMessage` in pipeline
3864
4015
  * TODO: [🏢] !! Check validity of `temperature` in pipeline
@@ -3918,7 +4069,7 @@ function prepareTemplates(pipeline, options) {
3918
4069
  dependentParameterNames = template.dependentParameterNames;
3919
4070
  preparedContent = undefined;
3920
4071
  if (knowledgePiecesCount > 0 && !dependentParameterNames.includes('knowledge')) {
3921
- preparedContent = spaceTrim$1("\n {content}\n\n ## Knowledge\n\n {knowledge}\n ");
4072
+ preparedContent = spaceTrim("\n {content}\n\n ## Knowledge\n\n {knowledge}\n ");
3922
4073
  // <- TODO: [🧠][🧻] Cutomize shape/language/formatting of the addition to the prompt
3923
4074
  dependentParameterNames = __spreadArray(__spreadArray([], __read(dependentParameterNames), false), [
3924
4075
  'knowledge',
@@ -3959,7 +4110,7 @@ function preparePipeline(pipeline, options) {
3959
4110
  return __awaiter(this, void 0, void 0, function () {
3960
4111
  var llmTools, _a, maxParallelCount, _b, isVerbose, parameters, promptTemplates,
3961
4112
  /*
3962
- <- TODO: [🧠][0] `promptbookVersion` */
4113
+ <- TODO: [🧠][🪑] `promptbookVersion` */
3963
4114
  knowledgeSources /*
3964
4115
  <- TODO: [🧊] `knowledgePieces` */, personas /*
3965
4116
  <- TODO: [🧊] `preparations` */, llmToolsWithUsage, currentPreparation, preparations, preparedPersonas, knowledgeSourcesPrepared, partialknowledgePiecesPrepared, knowledgePiecesPrepared, promptTemplatesPrepared /* TODO: parameters: parametersPrepared*/;
@@ -4029,7 +4180,7 @@ function preparePipeline(pipeline, options) {
4029
4180
  // ----- /Templates preparation -----
4030
4181
  // Note: Count total usage
4031
4182
  currentPreparation.usage = llmToolsWithUsage.getTotalUsage();
4032
- return [2 /*return*/, __assign(__assign({}, clonePipeline(pipeline)), { promptTemplates: promptTemplatesPrepared, knowledgeSources: knowledgeSourcesPrepared, knowledgePieces: knowledgePiecesPrepared, personas: preparedPersonas, preparations: preparations })];
4183
+ return [2 /*return*/, $asDeeplyFrozenSerializableJson('Prepared PipelineJson', __assign(__assign({}, clonePipeline(pipeline)), { promptTemplates: promptTemplatesPrepared, knowledgeSources: knowledgeSourcesPrepared, knowledgePieces: knowledgePiecesPrepared, personas: preparedPersonas, preparations: preparations }))];
4033
4184
  }
4034
4185
  });
4035
4186
  });
@@ -4105,7 +4256,7 @@ var knowledgeCommandParser = {
4105
4256
  */
4106
4257
  parse: function (input) {
4107
4258
  var args = input.args;
4108
- var sourceContent = spaceTrim(args[0] || '');
4259
+ var sourceContent = spaceTrim$1(args[0] || '');
4109
4260
  if (sourceContent === '') {
4110
4261
  throw new ParsingError("Source is not defined");
4111
4262
  }
@@ -4221,8 +4372,8 @@ var personaCommandParser = {
4221
4372
  persona.description = personaDescription;
4222
4373
  return;
4223
4374
  }
4224
- console.warn(spaceTrim("\n\n Persona \"".concat(personaName, "\" is defined multiple times with different description:\n\n First definition:\n ").concat(persona.description, "\n\n Second definition:\n ").concat(personaDescription, "\n\n ")));
4225
- persona.description += spaceTrim('\n\n' + personaDescription);
4375
+ console.warn(spaceTrim$1("\n\n Persona \"".concat(personaName, "\" is defined multiple times with different description:\n\n First definition:\n ").concat(persona.description, "\n\n Second definition:\n ").concat(personaDescription, "\n\n ")));
4376
+ persona.description += spaceTrim$1('\n\n' + personaDescription);
4226
4377
  },
4227
4378
  };
4228
4379
 
@@ -4409,7 +4560,7 @@ var blockCommandParser = {
4409
4560
  normalized = normalized.split('EXAMPLE').join('SAMPLE');
4410
4561
  var blockTypes = BlockTypes.filter(function (blockType) { return normalized.includes(blockType); });
4411
4562
  if (blockTypes.length !== 1) {
4412
- throw new ParsingError(spaceTrim(function (block) { return "\n Unknown block type in BLOCK command\n\n Supported block types are:\n ".concat(block(BlockTypes.join(', ')), "\n "); }));
4563
+ throw new ParsingError(spaceTrim$1(function (block) { return "\n Unknown block type in BLOCK command\n\n Supported block types are:\n ".concat(block(BlockTypes.join(', ')), "\n "); }));
4413
4564
  }
4414
4565
  var blockType = blockTypes[0];
4415
4566
  return {
@@ -4516,7 +4667,7 @@ var expectCommandParser = {
4516
4667
  /**
4517
4668
  * Description of the EXPECT command
4518
4669
  */
4519
- description: spaceTrim("\n Expect command describes the desired output of the prompt template (after post-processing)\n It can set limits for the maximum/minimum length of the output, measured in characters, words, sentences, paragraphs or some other shape of the output.\n "),
4670
+ description: spaceTrim$1("\n Expect command describes the desired output of the prompt template (after post-processing)\n It can set limits for the maximum/minimum length of the output, measured in characters, words, sentences, paragraphs or some other shape of the output.\n "),
4520
4671
  /**
4521
4672
  * Link to discussion
4522
4673
  */
@@ -4609,7 +4760,7 @@ var expectCommandParser = {
4609
4760
  if (!(error instanceof Error)) {
4610
4761
  throw error;
4611
4762
  }
4612
- throw new ParsingError(spaceTrim(function (block) {
4763
+ throw new ParsingError(spaceTrim$1(function (block) {
4613
4764
  return "\n Invalid EXPECT command\n ".concat(block(error.message), ":\n ");
4614
4765
  }));
4615
4766
  }
@@ -4728,7 +4879,7 @@ var modelCommandParser = {
4728
4879
  // <- Note: [🤖]
4729
4880
  }
4730
4881
  else {
4731
- throw new ParsingError(spaceTrim(function (block) { return "\n Unknown model variant in command:\n\n Supported variants are:\n ".concat(block(MODEL_VARIANTS.map(function (variantName) { return "- ".concat(variantName); }).join('\n')), "\n "); }));
4882
+ throw new ParsingError(spaceTrim$1(function (block) { return "\n Unknown model variant in command:\n\n Supported variants are:\n ".concat(block(MODEL_VARIANTS.map(function (variantName) { return "- ".concat(variantName); }).join('\n')), "\n "); }));
4732
4883
  }
4733
4884
  }
4734
4885
  if (normalized.startsWith('MODEL_NAME')) {
@@ -4739,7 +4890,7 @@ var modelCommandParser = {
4739
4890
  };
4740
4891
  }
4741
4892
  else {
4742
- throw new ParsingError(spaceTrim(function (block) { return "\n Unknown model key in command.\n\n Supported model keys are:\n ".concat(block(['variant', 'name'].join(', ')), "\n\n Example:\n\n - MODEL VARIANT Chat\n - MODEL NAME gpt-4\n "); }));
4893
+ throw new ParsingError(spaceTrim$1(function (block) { return "\n Unknown model key in command.\n\n Supported model keys are:\n ".concat(block(['variant', 'name'].join(', ')), "\n\n Example:\n\n - MODEL VARIANT Chat\n - MODEL NAME gpt-4\n "); }));
4743
4894
  }
4744
4895
  },
4745
4896
  };
@@ -5198,7 +5349,7 @@ function parseCommand(raw, usagePlace) {
5198
5349
  .map(removeMarkdownFormatting)
5199
5350
  .map(function (item) { return item.trim(); });
5200
5351
  if (items.length === 0 || items[0] === '') {
5201
- throw new ParsingError(spaceTrim$1(function (block) {
5352
+ throw new ParsingError(spaceTrim(function (block) {
5202
5353
  return "\n Malformed command:\n\n - ".concat(raw, "\n\n Supported commands are:\n ").concat(block(getSupportedCommandsMessage()), "\n\n ");
5203
5354
  }));
5204
5355
  }
@@ -5225,7 +5376,7 @@ function parseCommand(raw, usagePlace) {
5225
5376
  return command;
5226
5377
  }
5227
5378
  }
5228
- throw new ParsingError(spaceTrim$1(function (block) {
5379
+ throw new ParsingError(spaceTrim(function (block) {
5229
5380
  return "\n Malformed or unknown command:\n\n - ".concat(raw, "\n\n Supported commands are:\n ").concat(block(getSupportedCommandsMessage()), "\n\n ");
5230
5381
  }));
5231
5382
  }
@@ -5258,7 +5409,7 @@ function parseCommandVariant(input) {
5258
5409
  if (!(error instanceof ParsingError)) {
5259
5410
  throw error;
5260
5411
  }
5261
- throw new ParsingError(spaceTrim$1(function (block) {
5412
+ throw new ParsingError(spaceTrim(function (block) {
5262
5413
  return "\n Invalid ".concat(commandName, " command:\n ").concat(block(error.message), "\n\n - ").concat(raw, "\n\n Usage of ").concat(commandName, ":\n ").concat(block(commandParser.examples.map(function (example) { return "- ".concat(example); }).join('\n')), "\n\n All supported commands are:\n ").concat(block(getSupportedCommandsMessage()), "\n\n ");
5263
5414
  }));
5264
5415
  }
@@ -5285,22 +5436,6 @@ function parseCommandVariant(input) {
5285
5436
  return null;
5286
5437
  }
5287
5438
 
5288
- /**
5289
- * This error type indicates that some part of the code is not implemented yet
5290
- *
5291
- * @public exported from `@promptbook/core`
5292
- */
5293
- var NotYetImplementedError = /** @class */ (function (_super) {
5294
- __extends(NotYetImplementedError, _super);
5295
- function NotYetImplementedError(message) {
5296
- var _this = _super.call(this, spaceTrim$1(function (block) { return "\n ".concat(block(message), "\n\n Note: This feature is not implemented yet but it will be soon.\n\n If you want speed up the implementation or just read more, look here:\n https://github.com/webgptorg/promptbook\n\n Or contact us on me@pavolhejny.com\n\n "); })) || this;
5297
- _this.name = 'NotYetImplementedError';
5298
- Object.setPrototypeOf(_this, NotYetImplementedError.prototype);
5299
- return _this;
5300
- }
5301
- return NotYetImplementedError;
5302
- }(Error));
5303
-
5304
5439
  /**
5305
5440
  * Supported script languages
5306
5441
  *
@@ -5440,7 +5575,7 @@ function extractAllBlocksFromMarkdown(markdown) {
5440
5575
  function extractOneBlockFromMarkdown(markdown) {
5441
5576
  var codeBlocks = extractAllBlocksFromMarkdown(markdown);
5442
5577
  if (codeBlocks.length !== 1) {
5443
- throw new ParsingError(spaceTrim(function (block) { return "\n There should be exactly 1 code block, found ".concat(codeBlocks.length, " code blocks\n\n ").concat(block(codeBlocks.map(function (block, i) { return "Block ".concat(i + 1, ":\n").concat(block.content); }).join('\n\n\n')), "\n "); }));
5578
+ throw new ParsingError(spaceTrim$1(function (block) { return "\n There should be exactly 1 code block, found ".concat(codeBlocks.length, " code blocks\n\n ").concat(block(codeBlocks.map(function (block, i) { return "Block ".concat(i + 1, ":\n").concat(block.content); }).join('\n\n\n')), "\n "); }));
5444
5579
  }
5445
5580
  return codeBlocks[0];
5446
5581
  }
@@ -5461,7 +5596,7 @@ function parseMarkdownSection(value) {
5461
5596
  }
5462
5597
  var title = lines[0].replace(/^#+\s*/, '');
5463
5598
  var level = (_b = (_a = lines[0].match(/^#+/)) === null || _a === void 0 ? void 0 : _a[0].length) !== null && _b !== void 0 ? _b : 0;
5464
- var content = spaceTrim(lines.slice(1).join('\n'));
5599
+ var content = spaceTrim$1(lines.slice(1).join('\n'));
5465
5600
  if (level < 1 || level > 6) {
5466
5601
  throw new ParsingError('Markdown section must have heading level between 1 and 6');
5467
5602
  }
@@ -5489,7 +5624,7 @@ function splitMarkdownIntoSections(markdown) {
5489
5624
  if (buffer.length === 0) {
5490
5625
  return;
5491
5626
  }
5492
- var section = spaceTrim(buffer.join('\n'));
5627
+ var section = spaceTrim$1(buffer.join('\n'));
5493
5628
  if (section === '') {
5494
5629
  return;
5495
5630
  }
@@ -5585,7 +5720,7 @@ function flattenMarkdown(markdown) {
5585
5720
  }
5586
5721
  finally { if (e_1) throw e_1.error; }
5587
5722
  }
5588
- return spaceTrim(flattenedMarkdown);
5723
+ return spaceTrim$1(flattenedMarkdown);
5589
5724
  }
5590
5725
  /**
5591
5726
  * TODO: [🏛] This can be part of markdown builder
@@ -5603,7 +5738,7 @@ function flattenMarkdown(markdown) {
5603
5738
  * @public exported from `@promptbook/markdown-utils`
5604
5739
  */
5605
5740
  function removeContentComments(content) {
5606
- return spaceTrim$1(content.replace(/<!--(.*?)-->/gs, ''));
5741
+ return spaceTrim(content.replace(/<!--(.*?)-->/gs, ''));
5607
5742
  }
5608
5743
 
5609
5744
  /**
@@ -5644,13 +5779,13 @@ function pipelineStringToJsonSync(pipelineString) {
5644
5779
  pipelineString = pipelineString.replaceAll(/`->\s+\{(?<parameterName>[a-z0-9_]+)\}`/gi, '-> {$<parameterName>}');
5645
5780
  var _c = __read(splitMarkdownIntoSections(pipelineString).map(parseMarkdownSection)), pipelineHead = _c[0], pipelineSections = _c.slice(1); /* <- Note: [🥞] */
5646
5781
  if (pipelineHead === undefined) {
5647
- throw new UnexpectedError(spaceTrim$1("\n Pipeline head is not defined\n\n This should never happen, because the pipeline already flattened\n "));
5782
+ throw new UnexpectedError(spaceTrim("\n Pipeline head is not defined\n\n This should never happen, because the pipeline already flattened\n "));
5648
5783
  }
5649
5784
  if (pipelineHead.level !== 1) {
5650
- throw new UnexpectedError(spaceTrim$1("\n Pipeline head is not h1\n\n This should never happen, because the pipeline already flattened\n "));
5785
+ throw new UnexpectedError(spaceTrim("\n Pipeline head is not h1\n\n This should never happen, because the pipeline already flattened\n "));
5651
5786
  }
5652
5787
  if (!pipelineSections.every(function (section) { return section.level === 2; })) {
5653
- throw new UnexpectedError(spaceTrim$1("\n Not every pipeline section is h2\n\n This should never happen, because the pipeline already flattened\n "));
5788
+ throw new UnexpectedError(spaceTrim("\n Not every pipeline section is h2\n\n This should never happen, because the pipeline already flattened\n "));
5654
5789
  }
5655
5790
  // =============================================================
5656
5791
  ///Note: 2️⃣ Function for defining parameters
@@ -5664,7 +5799,7 @@ function pipelineStringToJsonSync(pipelineString) {
5664
5799
  existingParameter.description &&
5665
5800
  existingParameter.description !== parameterDescription &&
5666
5801
  parameterDescription) {
5667
- throw new ParsingError(spaceTrim$1(function (block) { return "\n Parameter {".concat(parameterName, "} is defined multiple times with different description:\n\n First definition:\n ").concat(block(existingParameter.description || '[undefined]'), "\n\n Second definition:\n ").concat(block(parameterDescription || '[undefined]'), "\n "); }));
5802
+ throw new ParsingError(spaceTrim(function (block) { return "\n Parameter {".concat(parameterName, "} is defined multiple times with different description:\n\n First definition:\n ").concat(block(existingParameter.description || '[undefined]'), "\n\n Second definition:\n ").concat(block(parameterDescription || '[undefined]'), "\n "); }));
5668
5803
  }
5669
5804
  if (existingParameter) {
5670
5805
  if (parameterDescription) {
@@ -5690,7 +5825,7 @@ function pipelineStringToJsonSync(pipelineString) {
5690
5825
  description = description.split(/^>.*$/gm).join('');
5691
5826
  //Note: Remove lists and return statement - TODO: [🎾] Make util (exported from `@promptbool/utils`)
5692
5827
  description = description.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
5693
- description = spaceTrim$1(description);
5828
+ description = spaceTrim(description);
5694
5829
  if (description === '') {
5695
5830
  description = undefined;
5696
5831
  }
@@ -5761,7 +5896,7 @@ function pipelineStringToJsonSync(pipelineString) {
5761
5896
  if (resultingParameterName !== null) {
5762
5897
  return resultingParameterName;
5763
5898
  }
5764
- throw new ParsingError(spaceTrim$1(function (block) { return "\n Template section must end with -> {parameterName}\n\n Invalid section:\n ".concat(block(
5899
+ throw new ParsingError(spaceTrim(function (block) { return "\n Template section must end with -> {parameterName}\n\n Invalid section:\n ".concat(block(
5765
5900
  // TODO: Show code of invalid sections each time + DRY
5766
5901
  section.content
5767
5902
  .split('\n')
@@ -5776,7 +5911,7 @@ function pipelineStringToJsonSync(pipelineString) {
5776
5911
  description_1 = description_1.split(/^>.*$/gm).join('');
5777
5912
  //Note: Remove lists and return statement - TODO: [🎾]
5778
5913
  description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
5779
- description_1 = spaceTrim$1(description_1);
5914
+ description_1 = spaceTrim(description_1);
5780
5915
  if (description_1 === '') {
5781
5916
  description_1 = undefined;
5782
5917
  }
@@ -5860,7 +5995,7 @@ function pipelineStringToJsonSync(pipelineString) {
5860
5995
  break;
5861
5996
  case 'EXPECT_FORMAT':
5862
5997
  if (templateJson.expectFormat !== undefined && command.format !== templateJson.expectFormat) {
5863
- throw new ParsingError(spaceTrim$1("\n Expect format is already defined to \"".concat(templateJson.expectFormat, "\".\n Now you try to redefine it by \"").concat(command.format, "\".\n ")));
5998
+ throw new ParsingError(spaceTrim("\n Expect format is already defined to \"".concat(templateJson.expectFormat, "\".\n Now you try to redefine it by \"").concat(command.format, "\".\n ")));
5864
5999
  }
5865
6000
  templateJson.expectFormat = command.format;
5866
6001
  break;
@@ -5917,7 +6052,7 @@ function pipelineStringToJsonSync(pipelineString) {
5917
6052
  throw new ParsingError('You must specify the language of the script in the prompt template');
5918
6053
  }
5919
6054
  if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
5920
- throw new ParsingError(spaceTrim$1(function (block) { return "\n Script language ".concat(language, " is not supported.\n\n Supported languages are:\n ").concat(block(SUPPORTED_SCRIPT_LANGUAGES.join(', ')), "\n\n "); }));
6055
+ throw new ParsingError(spaceTrim(function (block) { return "\n Script language ".concat(language, " is not supported.\n\n Supported languages are:\n ").concat(block(SUPPORTED_SCRIPT_LANGUAGES.join(', ')), "\n\n "); }));
5921
6056
  }
5922
6057
  templateJson.contentLanguage = language;
5923
6058
  }
@@ -5992,7 +6127,7 @@ function pipelineStringToJsonSync(pipelineString) {
5992
6127
  }
5993
6128
  });
5994
6129
  // =============================================================
5995
- return pipelineJson;
6130
+ return $asDeeplyFrozenSerializableJson('pipelineJson', pipelineJson);
5996
6131
  }
5997
6132
  /**
5998
6133
  * TODO: !!!! Warn if used only sync version
@@ -6036,7 +6171,9 @@ function pipelineStringToJson(pipelineString, options) {
6036
6171
  case 1:
6037
6172
  pipelineJson = _a.sent();
6038
6173
  _a.label = 2;
6039
- case 2: return [2 /*return*/, pipelineJson];
6174
+ case 2:
6175
+ // Note: No need to use `$asDeeplyFrozenSerializableJson` because `pipelineStringToJsonSync` and `preparePipeline` already do that
6176
+ return [2 /*return*/, pipelineJson];
6040
6177
  }
6041
6178
  });
6042
6179
  });
@@ -6047,22 +6184,6 @@ function pipelineStringToJson(pipelineString, options) {
6047
6184
  * TODO: [🧠] Should be in generated JSON file GENERATOR_WARNING
6048
6185
  */
6049
6186
 
6050
- /**
6051
- * This error indicates that the pipeline collection cannot be propperly loaded
6052
- *
6053
- * @public exported from `@promptbook/core`
6054
- */
6055
- var CollectionError = /** @class */ (function (_super) {
6056
- __extends(CollectionError, _super);
6057
- function CollectionError(message) {
6058
- var _this = _super.call(this, message) || this;
6059
- _this.name = 'CollectionError';
6060
- Object.setPrototypeOf(_this, CollectionError.prototype);
6061
- return _this;
6062
- }
6063
- return CollectionError;
6064
- }(Error));
6065
-
6066
6187
  /**
6067
6188
  * Checks if the file exists
6068
6189
  *
@@ -6419,7 +6540,7 @@ function createCollectionFromDirectory(path, options) {
6419
6540
  }
6420
6541
  else {
6421
6542
  existing = collection.get(pipeline.pipelineUrl);
6422
- throw new ReferenceError(spaceTrim("\n Pipeline with URL \"".concat(pipeline.pipelineUrl, "\" is already in the collection \uD83C\uDF4F\n\n Conflicting files:\n ").concat(existing.sourceFile || 'Unknown', "\n ").concat(pipeline.sourceFile || 'Unknown', "\n\n Note: You have probably forgotten to run \"ptbk make\" to update the collection\n Note: Pipelines with the same URL are not allowed\n Only exepction is when the pipelines are identical\n\n ")));
6543
+ throw new PipelineUrlError(spaceTrim$1("\n Pipeline with URL \"".concat(pipeline.pipelineUrl, "\" is already in the collection \uD83C\uDF4F\n\n Conflicting files:\n ").concat(existing.sourceFile || 'Unknown', "\n ").concat(pipeline.sourceFile || 'Unknown', "\n\n Note: You have probably forgotten to run \"ptbk make\" to update the collection\n Note: Pipelines with the same URL are not allowed\n Only exepction is when the pipelines are identical\n\n ")));
6423
6544
  }
6424
6545
  }
6425
6546
  }
@@ -6429,7 +6550,7 @@ function createCollectionFromDirectory(path, options) {
6429
6550
  if (!(error_1 instanceof Error)) {
6430
6551
  throw error_1;
6431
6552
  }
6432
- wrappedErrorMessage = spaceTrim(function (block) { return "\n ".concat(error_1.name, " in pipeline ").concat(fileName.split('\\').join('/'), "\u2060:\n\n ").concat(block(error_1.message), "\n\n "); });
6553
+ wrappedErrorMessage = spaceTrim$1(function (block) { return "\n ".concat(error_1.name, " in pipeline ").concat(fileName.split('\\').join('/'), "\u2060:\n\n ").concat(block(error_1.message), "\n\n "); });
6433
6554
  if (isCrashedOnError) {
6434
6555
  throw new CollectionError(wrappedErrorMessage);
6435
6556
  }
@@ -6485,6 +6606,39 @@ function createCollectionFromDirectory(path, options) {
6485
6606
  * TODO: [🖇] What about symlinks? Maybe option isSymlinksFollowed
6486
6607
  */
6487
6608
 
6609
+ /**
6610
+ * Tests if the value is [🚉] serializable as JSON
6611
+ *
6612
+ * - Almost all primitives are serializable BUT:
6613
+ * - `undefined` is not serializable
6614
+ * - `NaN` is not serializable
6615
+ * - Objects and arrays are serializable if all their properties are serializable
6616
+ * - Functions are not serializable
6617
+ * - Circular references are not serializable
6618
+ * - `Date` objects are not serializable
6619
+ * - `Map` and `Set` objects are not serializable
6620
+ * - `RegExp` objects are not serializable
6621
+ * - `Error` objects are not serializable
6622
+ * - `Symbol` objects are not serializable
6623
+ * - And much more...
6624
+ *
6625
+ *
6626
+ * @public exported from `@promptbook/utils`
6627
+ */
6628
+ function isSerializableAsJson(value) {
6629
+ try {
6630
+ checkSerializableAsJson('', value);
6631
+ return true;
6632
+ }
6633
+ catch (error) {
6634
+ return false;
6635
+ }
6636
+ }
6637
+ /**
6638
+ * TODO: [🧠] !!! In-memory cache of same values to prevent multiple checks
6639
+ * TODO: [🧠][💺] Can be done this on type-level?
6640
+ */
6641
+
6488
6642
  /**
6489
6643
  * Stringify the PipelineJson with proper formatting
6490
6644
  *
@@ -6494,6 +6648,9 @@ function createCollectionFromDirectory(path, options) {
6494
6648
  * @public exported from `@promptbook/core`
6495
6649
  */
6496
6650
  function stringifyPipelineJson(pipeline) {
6651
+ if (!isSerializableAsJson(pipeline)) {
6652
+ throw new UnexpectedError(spaceTrim$1("\n Cannot stringify the pipeline, because it is not serializable as JSON\n\n There can be multiple reasons:\n 1) The pipeline contains circular references\n 2) It is not a valid PipelineJson\n "));
6653
+ }
6497
6654
  var pipelineJsonStringified = JSON.stringify(pipeline, null, 4);
6498
6655
  for (var i = 0; i < LOOP_LIMIT; i++) {
6499
6656
  pipelineJsonStringified = pipelineJsonStringified.replace(/(-?0\.\d+),[\n\s]+(-?0\.\d+)/gms, "$1".concat(REPLACING_NONCE, "$2"));
@@ -6542,7 +6699,7 @@ function usageToHuman(usage) {
6542
6699
  };
6543
6700
  report += '\n' + "- Cost ".concat(uncertainNumberToHuman(usage.price), " USD");
6544
6701
  report += '\n' + "- Saved ".concat(uncertainNumberToHuman(usageToWorktime(usage)), " hours of human time");
6545
- return spaceTrim(report);
6702
+ return spaceTrim$1(report);
6546
6703
  }
6547
6704
  /**
6548
6705
  * TODO: Use "$1" not "1 USD"
@@ -6619,6 +6776,9 @@ var FilesStorage = /** @class */ (function () {
6619
6776
  switch (_a.label) {
6620
6777
  case 0:
6621
6778
  filename = this.getFilenameForKey(key);
6779
+ if (!isSerializableAsJson(value)) {
6780
+ throw new UnexpectedError("The \"".concat(key, "\" you want to store in JSON file is not serializable as JSON"));
6781
+ }
6622
6782
  fileContent = stringifyPipelineJson(value);
6623
6783
  return [4 /*yield*/, mkdir(dirname(filename), { recursive: true })];
6624
6784
  case 1:
@@ -6663,14 +6823,11 @@ var FilesStorage = /** @class */ (function () {
6663
6823
  *
6664
6824
  * Note: `$` is used to indicate that this function is not a pure function - it access global scope
6665
6825
  *
6666
- * @public exported from `@promptbook/utils`
6826
+ * @private internal function of `$Register`
6667
6827
  */
6668
6828
  function $getGlobalScope() {
6669
6829
  return Function('return this')();
6670
6830
  }
6671
- /***
6672
- * TODO: !!!!! Make private and promptbook registry from this
6673
- */
6674
6831
 
6675
6832
  /**
6676
6833
  * Register is @@@
@@ -6795,7 +6952,7 @@ function $registeredLlmToolsMessage() {
6795
6952
  });
6796
6953
  return __assign(__assign({}, metadata), { isMetadataAviailable: isMetadataAviailable, isInstalled: isInstalled });
6797
6954
  });
6798
- return spaceTrim(function (block) { return "\n Available LLM providers are:\n ".concat(block(metadata
6955
+ return spaceTrim$1(function (block) { return "\n Available LLM providers are:\n ".concat(block(metadata
6799
6956
  .map(function (_a, i) {
6800
6957
  var packageName = _a.packageName, className = _a.className, isMetadataAviailable = _a.isMetadataAviailable, isInstalled = _a.isInstalled;
6801
6958
  var more;
@@ -6843,7 +7000,7 @@ function createLlmToolsFromConfiguration(configuration, options) {
6843
7000
  return llmConfiguration.packageName === packageName && llmConfiguration.className === className;
6844
7001
  });
6845
7002
  if (registeredItem === undefined) {
6846
- throw new Error(spaceTrim(function (block) { return "\n There is no constructor for LLM provider `".concat(llmConfiguration.className, "` from `").concat(llmConfiguration.packageName, "`\n\n You have probably forgotten install and import the provider package.\n To fix this issue, you can:\n\n Install:\n\n > npm install ").concat(llmConfiguration.packageName, "\n\n And import:\n\n > import '").concat(llmConfiguration.packageName, "';\n\n\n ").concat(block($registeredLlmToolsMessage()), "\n "); }));
7003
+ throw new Error(spaceTrim$1(function (block) { return "\n There is no constructor for LLM provider `".concat(llmConfiguration.className, "` from `").concat(llmConfiguration.packageName, "`\n\n You have probably forgotten install and import the provider package.\n To fix this issue, you can:\n\n Install:\n\n > npm install ").concat(llmConfiguration.packageName, "\n\n And import:\n\n > import '").concat(llmConfiguration.packageName, "';\n\n\n ").concat(block($registeredLlmToolsMessage()), "\n "); }));
6847
7004
  }
6848
7005
  return registeredItem(__assign({ isVerbose: isVerbose }, llmConfiguration.options));
6849
7006
  });
@@ -6914,7 +7071,7 @@ function createLlmToolsFromEnv(options) {
6914
7071
  var configuration = createLlmToolsFromConfigurationFromEnv();
6915
7072
  if (configuration.length === 0) {
6916
7073
  // TODO: [🥃]
6917
- throw new Error(spaceTrim(function (block) { return "\n No LLM tools found in the environment\n\n Please set one of environment variables:\n - OPENAI_API_KEY\n - ANTHROPIC_CLAUDE_API_KEY\n\n ".concat(block($registeredLlmToolsMessage()), "}\n "); }));
7074
+ throw new Error(spaceTrim$1(function (block) { return "\n No LLM tools found in the environment\n\n Please set one of environment variables:\n - OPENAI_API_KEY\n - ANTHROPIC_CLAUDE_API_KEY\n\n ".concat(block($registeredLlmToolsMessage()), "}\n "); }));
6918
7075
  }
6919
7076
  return createLlmToolsFromConfiguration(configuration, options);
6920
7077
  }
@@ -7140,17 +7297,17 @@ function getLlmToolsForCli(options) {
7140
7297
  function initializeMakeCommand(program) {
7141
7298
  var _this = this;
7142
7299
  var makeCommand = program.command('make');
7143
- makeCommand.description(spaceTrim("\n Makes a new pipeline collection in given folder\n "));
7300
+ makeCommand.description(spaceTrim$1("\n Makes a new pipeline collection in given folder\n "));
7144
7301
  makeCommand.argument('[path]',
7145
7302
  // <- TODO: [🧟‍♂️] Unite path to promptbook collection argument
7146
7303
  'Path to promptbook directory', './promptbook-collection');
7147
7304
  makeCommand.option('--project-name', "Name of the project for whom collection is", 'Untitled Promptbook project');
7148
- makeCommand.option('-f, --format <format>', spaceTrim("\n Output format of builded collection \"javascript\", \"typescript\" or \"json\"\n\n Note: You can use multiple formats separated by comma\n "), 'javascript' /* <- Note: [🏳‍🌈] */);
7305
+ makeCommand.option('-f, --format <format>', spaceTrim$1("\n Output format of builded collection \"javascript\", \"typescript\" or \"json\"\n\n Note: You can use multiple formats separated by comma\n "), 'javascript' /* <- Note: [🏳‍🌈] */);
7149
7306
  makeCommand.option('--no-validation', "Do not validate logic of pipelines in collection", true);
7150
7307
  makeCommand.option('--validation', "Types of validations separated by comma (options \"logic\",\"imports\")", 'logic,imports');
7151
7308
  makeCommand.option('--reload-cache', "Use LLM models even if cached ", false);
7152
7309
  makeCommand.option('--verbose', "Is verbose", false);
7153
- makeCommand.option('-o, --out-file <path>', spaceTrim("\n Where to save the builded collection\n\n Note: If you keep it \"".concat(PIPELINE_COLLECTION_BASE_FILENAME, "\" it will be saved in the root of the promptbook directory\n If you set it to a path, it will be saved in that path\n BUT you can use only one format and set correct extension\n ")), PIPELINE_COLLECTION_BASE_FILENAME);
7310
+ makeCommand.option('-o, --out-file <path>', spaceTrim$1("\n Where to save the builded collection\n\n Note: If you keep it \"".concat(PIPELINE_COLLECTION_BASE_FILENAME, "\" it will be saved in the root of the promptbook directory\n If you set it to a path, it will be saved in that path\n BUT you can use only one format and set correct extension\n ")), PIPELINE_COLLECTION_BASE_FILENAME);
7154
7311
  makeCommand.action(function (path, _a) {
7155
7312
  var projectName = _a.projectName, format = _a.format, validation = _a.validation, reloadCache = _a.reloadCache, verbose = _a.verbose, outFile = _a.outFile;
7156
7313
  return __awaiter(_this, void 0, void 0, function () {
@@ -7255,7 +7412,7 @@ function initializeMakeCommand(program) {
7255
7412
  if (lastChar !== ']') {
7256
7413
  throw new UnexpectedError("Last character of serialized collection should be \"]\" not \"".concat(lastChar, "\""));
7257
7414
  }
7258
- return spaceTrim(collectionJsonString.substring(1, collectionJsonString.length - 1));
7415
+ return spaceTrim$1(collectionJsonString.substring(1, collectionJsonString.length - 1));
7259
7416
  })();
7260
7417
  saveFile = function (extension, content) { return __awaiter(_this, void 0, void 0, function () {
7261
7418
  var filePath;
@@ -7289,14 +7446,14 @@ function initializeMakeCommand(program) {
7289
7446
  case 19:
7290
7447
  if (!(formats.includes('javascript') || formats.includes('js'))) return [3 /*break*/, 21];
7291
7448
  formats = formats.filter(function (format) { return format !== 'javascript' && format !== 'js'; });
7292
- return [4 /*yield*/, saveFile('js', spaceTrim(function (block) { return "\n // ".concat(block(GENERATOR_WARNING_BY_PROMPTBOOK_CLI), "\n\n import { createCollectionFromJson } from '@promptbook/core';\n\n /**\n * Pipeline collection for ").concat(projectName, "\n *\n * ").concat(block(GENERATOR_WARNING_BY_PROMPTBOOK_CLI), "\n *\n * @private internal cache for `getPipelineCollection`\n */\n let pipelineCollection = null;\n\n\n /**\n * Get pipeline collection for ").concat(projectName, "\n *\n * ").concat(block(GENERATOR_WARNING_BY_PROMPTBOOK_CLI), "\n *\n * @returns {PipelineCollection} Library of promptbooks for ").concat(projectName, "\n */\n export function getPipelineCollection(){\n if(pipelineCollection===null){\n pipelineCollection = createCollectionFromJson(\n ").concat(block(collectionJsonItems), "\n );\n }\n\n return pipelineCollection;\n }\n "); }))];
7449
+ return [4 /*yield*/, saveFile('js', spaceTrim$1(function (block) { return "\n // ".concat(block(GENERATOR_WARNING_BY_PROMPTBOOK_CLI), "\n\n import { createCollectionFromJson } from '@promptbook/core';\n\n /**\n * Pipeline collection for ").concat(projectName, "\n *\n * ").concat(block(GENERATOR_WARNING_BY_PROMPTBOOK_CLI), "\n *\n * @private internal cache for `getPipelineCollection`\n */\n let pipelineCollection = null;\n\n\n /**\n * Get pipeline collection for ").concat(projectName, "\n *\n * ").concat(block(GENERATOR_WARNING_BY_PROMPTBOOK_CLI), "\n *\n * @returns {PipelineCollection} Library of promptbooks for ").concat(projectName, "\n */\n export function getPipelineCollection(){\n if(pipelineCollection===null){\n pipelineCollection = createCollectionFromJson(\n ").concat(block(collectionJsonItems), "\n );\n }\n\n return pipelineCollection;\n }\n "); }))];
7293
7450
  case 20:
7294
7451
  (_f.sent()) + '\n';
7295
7452
  _f.label = 21;
7296
7453
  case 21:
7297
7454
  if (!(formats.includes('typescript') || formats.includes('ts'))) return [3 /*break*/, 23];
7298
7455
  formats = formats.filter(function (format) { return format !== 'typescript' && format !== 'ts'; });
7299
- return [4 /*yield*/, saveFile('ts', spaceTrim(function (block) { return "\n // ".concat(block(GENERATOR_WARNING_BY_PROMPTBOOK_CLI), "\n\n import { createCollectionFromJson } from '@promptbook/core';\n import type { PipelineCollection } from '@promptbook/types';\n\n /**\n * Pipeline collection for ").concat(projectName, "\n *\n * ").concat(block(GENERATOR_WARNING_BY_PROMPTBOOK_CLI), "\n *\n * @private internal cache for `getPipelineCollection`\n */\n let pipelineCollection: null | PipelineCollection = null;\n\n\n /**\n * Get pipeline collection for ").concat(projectName, "\n *\n * ").concat(block(GENERATOR_WARNING_BY_PROMPTBOOK_CLI), "\n *\n * @returns {PipelineCollection} Library of promptbooks for ").concat(projectName, "\n */\n export function getPipelineCollection(): PipelineCollection{\n if(pipelineCollection===null){\n pipelineCollection = createCollectionFromJson(\n ").concat(block(collectionJsonItems), "\n );\n }\n\n return pipelineCollection;\n }\n "); }) + '\n')];
7456
+ return [4 /*yield*/, saveFile('ts', spaceTrim$1(function (block) { return "\n // ".concat(block(GENERATOR_WARNING_BY_PROMPTBOOK_CLI), "\n\n import { createCollectionFromJson } from '@promptbook/core';\n import type { PipelineCollection } from '@promptbook/types';\n\n /**\n * Pipeline collection for ").concat(projectName, "\n *\n * ").concat(block(GENERATOR_WARNING_BY_PROMPTBOOK_CLI), "\n *\n * @private internal cache for `getPipelineCollection`\n */\n let pipelineCollection: null | PipelineCollection = null;\n\n\n /**\n * Get pipeline collection for ").concat(projectName, "\n *\n * ").concat(block(GENERATOR_WARNING_BY_PROMPTBOOK_CLI), "\n *\n * @returns {PipelineCollection} Library of promptbooks for ").concat(projectName, "\n */\n export function getPipelineCollection(): PipelineCollection{\n if(pipelineCollection===null){\n pipelineCollection = createCollectionFromJson(\n ").concat(block(collectionJsonItems), "\n );\n }\n\n return pipelineCollection;\n }\n "); }) + '\n')];
7300
7457
  case 22:
7301
7458
  _f.sent();
7302
7459
  _f.label = 23;
@@ -7334,7 +7491,7 @@ function addAutoGeneratedSection(content, options) {
7334
7491
  var sectionRegex = new RegExp("<!--".concat(sectionName, "-->([\\s\\S]*?)<!--/").concat(sectionName, "-->"), 'g');
7335
7492
  var sectionMatch = content.match(sectionRegex);
7336
7493
  if (sectionMatch) {
7337
- return content.replace(sectionRegex, spaceTrim$1(function (block) { return "\n <!--".concat(sectionName, "-->\n ").concat(block(warningLine), "\n ").concat(block(sectionContent), "\n <!--/").concat(sectionName, "-->\n "); }));
7494
+ return content.replace(sectionRegex, spaceTrim(function (block) { return "\n <!--".concat(sectionName, "-->\n ").concat(block(warningLine), "\n ").concat(block(sectionContent), "\n <!--/").concat(sectionName, "-->\n "); }));
7338
7495
  }
7339
7496
  var placeForSection = removeContentComments(content).match(/^##.*$/im);
7340
7497
  if (!placeForSection) {
@@ -7437,7 +7594,7 @@ function renderPromptbookMermaid(pipelineJson, options) {
7437
7594
  }
7438
7595
  return normalizeTo_camelCase('template-' + titleToName(template.title));
7439
7596
  };
7440
- var promptbookMermaid = spaceTrim$1(function (block) { return "\n\n %% \uD83D\uDD2E Tip: Open this on GitHub or in the VSCode website to see the Mermaid graph visually\n\n flowchart LR\n subgraph \"".concat(pipelineJson.title, "\"\n\n direction TB\n\n input((Input)):::input\n ").concat(block(pipelineJson.promptTemplates
7597
+ var promptbookMermaid = spaceTrim(function (block) { return "\n\n %% \uD83D\uDD2E Tip: Open this on GitHub or in the VSCode website to see the Mermaid graph visually\n\n flowchart LR\n subgraph \"".concat(pipelineJson.title, "\"\n\n direction TB\n\n input((Input)):::input\n ").concat(block(pipelineJson.promptTemplates
7441
7598
  .flatMap(function (_a) {
7442
7599
  var title = _a.title, dependentParameterNames = _a.dependentParameterNames, resultingParameterName = _a.resultingParameterName;
7443
7600
  return __spreadArray([
@@ -7495,7 +7652,7 @@ function prettifyPipelineString(pipelineString, options) {
7495
7652
  return { href: "#".concat(promptTemplate.name), title: promptTemplate.title };
7496
7653
  },
7497
7654
  });
7498
- promptbookMermaidBlock = spaceTrim$1(function (block) { return "\n ```mermaid\n ".concat(block(promptbookMermaid_1), "\n ```\n "); });
7655
+ promptbookMermaidBlock = spaceTrim(function (block) { return "\n ```mermaid\n ".concat(block(promptbookMermaid_1), "\n ```\n "); });
7499
7656
  pipelineString = addAutoGeneratedSection(pipelineString, {
7500
7657
  sectionName: 'Graph',
7501
7658
  sectionContent: promptbookMermaidBlock,
@@ -7523,7 +7680,7 @@ function prettifyPipelineString(pipelineString, options) {
7523
7680
  function initializePrettifyCommand(program) {
7524
7681
  var _this = this;
7525
7682
  var prettifyCommand = program.command('prettify');
7526
- prettifyCommand.description(spaceTrim("\n Iterates over promptbooks and does multiple enhancing operations on them:\n\n 1) Adds Mermaid graph\n 2) Prettifies the markdown\n "));
7683
+ prettifyCommand.description(spaceTrim$1("\n Iterates over promptbooks and does multiple enhancing operations on them:\n\n 1) Adds Mermaid graph\n 2) Prettifies the markdown\n "));
7527
7684
  prettifyCommand.argument('<filesGlob>',
7528
7685
  // <- TODO: [🧟‍♂️] Unite path to promptbook collection argument
7529
7686
  'Promptbooks to prettify as glob pattern');
@@ -7615,13 +7772,13 @@ function promptbookCli() {
7615
7772
  var program;
7616
7773
  return __generator(this, function (_a) {
7617
7774
  if (!$isRunningInNode()) {
7618
- throw new EnvironmentMismatchError(spaceTrim$1("\n Function promptbookCli is initiator of CLI script and should be run in Node.js environment.\n\n - In browser use function exported from `@promptbook/utils` or `@promptbook/core` directly, for example `prettifyPipelineString`.\n\n "));
7775
+ throw new EnvironmentMismatchError(spaceTrim("\n Function promptbookCli is initiator of CLI script and should be run in Node.js environment.\n\n - In browser use function exported from `@promptbook/utils` or `@promptbook/core` directly, for example `prettifyPipelineString`.\n\n "));
7619
7776
  }
7620
7777
  program = new commander.Command();
7621
7778
  program.name('promptbook');
7622
7779
  program.alias('ptbk');
7623
7780
  program.version(PROMPTBOOK_VERSION);
7624
- program.description(spaceTrim$1("\n Promptbook utilities for enhancing workflow with promptbooks\n "));
7781
+ program.description(spaceTrim("\n Promptbook utilities for enhancing workflow with promptbooks\n "));
7625
7782
  initializeAboutCommand(program);
7626
7783
  initializeHelloCommand(program);
7627
7784
  initializeMakeCommand(program);
@@ -7751,13 +7908,13 @@ var RemoteLlmExecutionTools = /** @class */ (function () {
7751
7908
  socket.emit('listModels-request', {
7752
7909
  isAnonymous: true,
7753
7910
  llmToolsConfiguration: this.options.llmToolsConfiguration,
7754
- });
7911
+ } /* <- TODO: [🤛] */);
7755
7912
  }
7756
7913
  else {
7757
7914
  socket.emit('listModels-request', {
7758
7915
  isAnonymous: false,
7759
7916
  clientId: this.options.clientId,
7760
- });
7917
+ } /* <- TODO: [🤛] */);
7761
7918
  }
7762
7919
  return [4 /*yield*/, new Promise(function (resolve, reject) {
7763
7920
  socket.on('listModels-response', function (response) {
@@ -7765,7 +7922,7 @@ var RemoteLlmExecutionTools = /** @class */ (function () {
7765
7922
  socket.disconnect();
7766
7923
  });
7767
7924
  socket.on('error', function (error) {
7768
- reject(new Error(error.errorMessage));
7925
+ reject(deserializeError(error));
7769
7926
  socket.disconnect();
7770
7927
  });
7771
7928
  })];
@@ -7844,16 +8001,14 @@ var RemoteLlmExecutionTools = /** @class */ (function () {
7844
8001
  isAnonymous: true,
7845
8002
  llmToolsConfiguration: this.options.llmToolsConfiguration,
7846
8003
  prompt: prompt,
7847
- // <- TODO: [🛫] `prompt` is NOT fully serializable as JSON, it contains functions which are not serializable
7848
- });
8004
+ } /* <- TODO: [🤛] */);
7849
8005
  }
7850
8006
  else {
7851
8007
  socket.emit('prompt-request', {
7852
8008
  isAnonymous: false,
7853
8009
  clientId: this.options.clientId,
7854
8010
  prompt: prompt,
7855
- // <- TODO: [🛫] `prompt` is NOT fully serializable as JSON, it contains functions which are not serializable
7856
- });
8011
+ } /* <- TODO: [🤛] */);
7857
8012
  }
7858
8013
  return [4 /*yield*/, new Promise(function (resolve, reject) {
7859
8014
  socket.on('prompt-response', function (response) {
@@ -7861,7 +8016,7 @@ var RemoteLlmExecutionTools = /** @class */ (function () {
7861
8016
  socket.disconnect();
7862
8017
  });
7863
8018
  socket.on('error', function (error) {
7864
- reject(new PipelineExecutionError(error.errorMessage));
8019
+ reject(deserializeError(error));
7865
8020
  socket.disconnect();
7866
8021
  });
7867
8022
  })];
@@ -7876,6 +8031,7 @@ var RemoteLlmExecutionTools = /** @class */ (function () {
7876
8031
  return RemoteLlmExecutionTools;
7877
8032
  }());
7878
8033
  /**
8034
+ * TODO: Maybe use `$asDeeplyFrozenSerializableJson`
7879
8035
  * TODO: [🧠][🛍] Maybe not `isAnonymous: boolean` BUT `mode: 'ANONYMOUS'|'COLLECTION'`
7880
8036
  * TODO: [🍓] Allow to list compatible models with each variant
7881
8037
  * TODO: [🗯] RemoteLlmExecutionTools should extend Destroyable and implement IDestroyable
@@ -7910,7 +8066,7 @@ function computeUsage(value) {
7910
8066
  * @see https://docs.anthropic.com/en/docs/models-overview
7911
8067
  * @public exported from `@promptbook/anthropic-claude`
7912
8068
  */
7913
- var ANTHROPIC_CLAUDE_MODELS = [
8069
+ var ANTHROPIC_CLAUDE_MODELS = $asDeeplyFrozenSerializableJson('ANTHROPIC_CLAUDE_MODELS', [
7914
8070
  {
7915
8071
  modelVariant: 'CHAT',
7916
8072
  modelTitle: 'Claude 3.5 Sonnet',
@@ -7975,7 +8131,7 @@ var ANTHROPIC_CLAUDE_MODELS = [
7975
8131
  },
7976
8132
  },
7977
8133
  // TODO: !!! Claude 1 and 2 has also completion versions - ask Hoagy
7978
- ];
8134
+ ]);
7979
8135
  /**
7980
8136
  * Note: [🤖] Add models of new variant
7981
8137
  * TODO: [🧠] !!! Add embedding models OR Anthropic has only chat+completion models?
@@ -8186,7 +8342,7 @@ var AnthropicClaudeExecutionTools = /** @class */ (function () {
8186
8342
  // eslint-disable-next-line prefer-const
8187
8343
  complete = getCurrentIsoDate();
8188
8344
  usage = computeAnthropicClaudeUsage(content, '', rawResponse);
8189
- return [2 /*return*/, {
8345
+ return [2 /*return*/, $asDeeplyFrozenSerializableJson('AnthropicClaudeExecutionTools ChatPromptResult', {
8190
8346
  content: resultContent,
8191
8347
  modelName: rawResponse.model,
8192
8348
  timing: {
@@ -8198,7 +8354,7 @@ var AnthropicClaudeExecutionTools = /** @class */ (function () {
8198
8354
  rawRequest: rawRequest,
8199
8355
  rawResponse: rawResponse,
8200
8356
  // <- [🗯]
8201
- }];
8357
+ })];
8202
8358
  }
8203
8359
  });
8204
8360
  });
@@ -8207,7 +8363,7 @@ var AnthropicClaudeExecutionTools = /** @class */ (function () {
8207
8363
  TODO: [👏]
8208
8364
  public async callCompletionModel(
8209
8365
  prompt: Pick<Prompt, 'content' | 'parameters' | 'modelRequirements'>,
8210
- ): Promise<PromptCompletionResult> {
8366
+ ): Promise<CompletionPromptResult> {
8211
8367
 
8212
8368
  if (this.options.isVerbose) {
8213
8369
  console.info('🖋 Anthropic Claude callCompletionModel call');
@@ -8260,7 +8416,7 @@ var AnthropicClaudeExecutionTools = /** @class */ (function () {
8260
8416
 
8261
8417
 
8262
8418
 
8263
- return {
8419
+ return $asDeeplyFrozenSerializableJson('AnthropicClaudeExecutionTools CompletionPromptResult',{
8264
8420
  content: resultContent,
8265
8421
  modelName: rawResponse.model || model,
8266
8422
  timing: {
@@ -8270,7 +8426,7 @@ var AnthropicClaudeExecutionTools = /** @class */ (function () {
8270
8426
  usage,
8271
8427
  rawResponse,
8272
8428
  // <- [🗯]
8273
- };
8429
+ });
8274
8430
  }
8275
8431
  */
8276
8432
  // <- Note: [🤖] callXxxModel
@@ -8283,7 +8439,7 @@ var AnthropicClaudeExecutionTools = /** @class */ (function () {
8283
8439
  return modelName.startsWith(defaultModelName);
8284
8440
  });
8285
8441
  if (model === undefined) {
8286
- throw new UnexpectedError(spaceTrim(function (block) {
8442
+ throw new UnexpectedError(spaceTrim$1(function (block) {
8287
8443
  return "\n Cannot find model in OpenAI models with name \"".concat(defaultModelName, "\" which should be used as default.\n\n Available models:\n ").concat(block(ANTHROPIC_CLAUDE_MODELS.map(function (_a) {
8288
8444
  var modelName = _a.modelName;
8289
8445
  return "- \"".concat(modelName, "\"");
@@ -8392,7 +8548,7 @@ var _AzureOpenAiMetadataRegistration = $llmToolsMetadataRegister.register({
8392
8548
  else if (typeof env.AZUREOPENAI_RESOURCE_NAME === 'string' ||
8393
8549
  typeof env.AZUREOPENAI_DEPLOYMENT_NAME === 'string' ||
8394
8550
  typeof env.AZUREOPENAI_API_KEY === 'string') {
8395
- throw new Error(spaceTrim("\n You must provide all of the following environment variables:\n \n - AZUREOPENAI_RESOURCE_NAME (".concat(typeof env.AZUREOPENAI_RESOURCE_NAME === 'string' ? 'defined' : 'not defined', ")\n - AZUREOPENAI_DEPLOYMENT_NAME (").concat(typeof env.AZUREOPENAI_DEPLOYMENT_NAME === 'string' ? 'defined' : 'not defined', ")\n - AZUREOPENAI_API_KEY (").concat(typeof env.AZUREOPENAI_API_KEY === 'string' ? 'defined' : 'not defined', ") \n ")));
8551
+ throw new Error(spaceTrim$1("\n You must provide all of the following environment variables:\n \n - AZUREOPENAI_RESOURCE_NAME (".concat(typeof env.AZUREOPENAI_RESOURCE_NAME === 'string' ? 'defined' : 'not defined', ")\n - AZUREOPENAI_DEPLOYMENT_NAME (").concat(typeof env.AZUREOPENAI_DEPLOYMENT_NAME === 'string' ? 'defined' : 'not defined', ")\n - AZUREOPENAI_API_KEY (").concat(typeof env.AZUREOPENAI_API_KEY === 'string' ? 'defined' : 'not defined', ") \n ")));
8396
8552
  }
8397
8553
  return null;
8398
8554
  },
@@ -8407,7 +8563,7 @@ var _AzureOpenAiMetadataRegistration = $llmToolsMetadataRegister.register({
8407
8563
  * @see https://openai.com/api/pricing/
8408
8564
  * @public exported from `@promptbook/openai`
8409
8565
  */
8410
- var OPENAI_MODELS = [
8566
+ var OPENAI_MODELS = $asDeeplyFrozenSerializableJson('OPENAI_MODELS', [
8411
8567
  /*/
8412
8568
  {
8413
8569
  modelTitle: 'dall-e-3',
@@ -8735,7 +8891,7 @@ var OPENAI_MODELS = [
8735
8891
  },
8736
8892
  },
8737
8893
  /**/
8738
- ];
8894
+ ]);
8739
8895
  /**
8740
8896
  * Note: [🤖] Add models of new variant
8741
8897
  * TODO: [🧠] Some mechanism to propagate unsureness
@@ -8903,7 +9059,7 @@ var AzureOpenAiExecutionTools = /** @class */ (function () {
8903
9059
  input: __assign({ tokensCount: uncertainNumber((_a = rawResponse.usage) === null || _a === void 0 ? void 0 : _a.promptTokens) }, computeUsageCounts(prompt.content)),
8904
9060
  output: __assign({ tokensCount: uncertainNumber((_b = rawResponse.usage) === null || _b === void 0 ? void 0 : _b.completionTokens) }, computeUsageCounts(prompt.content)),
8905
9061
  };
8906
- return [2 /*return*/, {
9062
+ return [2 /*return*/, $asDeeplyFrozenSerializableJson('AzureOpenAiExecutionTools ChatPromptResult', {
8907
9063
  content: resultContent,
8908
9064
  modelName: modelName,
8909
9065
  timing: {
@@ -8913,9 +9069,9 @@ var AzureOpenAiExecutionTools = /** @class */ (function () {
8913
9069
  usage: usage,
8914
9070
  rawPromptContent: rawPromptContent,
8915
9071
  rawRequest: rawRequest,
8916
- rawResponse: rawResponse,
9072
+ rawResponse: __assign(__assign({}, rawResponse), { created: rawResponse.created.toISOString() }),
8917
9073
  // <- [🗯]
8918
- }];
9074
+ })];
8919
9075
  case 4:
8920
9076
  error_1 = _c.sent();
8921
9077
  throw this.transformAzureError(error_1);
@@ -8990,7 +9146,7 @@ var AzureOpenAiExecutionTools = /** @class */ (function () {
8990
9146
  input: __assign({ tokensCount: uncertainNumber((_a = rawResponse.usage) === null || _a === void 0 ? void 0 : _a.promptTokens) }, computeUsageCounts(prompt.content)),
8991
9147
  output: __assign({ tokensCount: uncertainNumber((_b = rawResponse.usage) === null || _b === void 0 ? void 0 : _b.completionTokens) }, computeUsageCounts(prompt.content)),
8992
9148
  };
8993
- return [2 /*return*/, {
9149
+ return [2 /*return*/, $asDeeplyFrozenSerializableJson('AzureOpenAiExecutionTools CompletionPromptResult', {
8994
9150
  content: resultContent,
8995
9151
  modelName: modelName,
8996
9152
  timing: {
@@ -9000,9 +9156,9 @@ var AzureOpenAiExecutionTools = /** @class */ (function () {
9000
9156
  usage: usage,
9001
9157
  rawPromptContent: rawPromptContent,
9002
9158
  rawRequest: rawRequest,
9003
- rawResponse: rawResponse,
9159
+ rawResponse: __assign(__assign({}, rawResponse), { created: rawResponse.created.toISOString() }),
9004
9160
  // <- [🗯]
9005
- }];
9161
+ })];
9006
9162
  case 4:
9007
9163
  error_2 = _c.sent();
9008
9164
  throw this.transformAzureError(error_2);
@@ -9016,11 +9172,14 @@ var AzureOpenAiExecutionTools = /** @class */ (function () {
9016
9172
  * Changes Azure error (which is not propper Error but object) to propper Error
9017
9173
  */
9018
9174
  AzureOpenAiExecutionTools.prototype.transformAzureError = function (azureError) {
9175
+ if (azureError instanceof UnexpectedError) {
9176
+ return azureError;
9177
+ }
9019
9178
  if (typeof azureError !== 'object' || azureError === null) {
9020
9179
  return new PipelineExecutionError("Unknown Azure OpenAI error");
9021
9180
  }
9022
9181
  var code = azureError.code, message = azureError.message;
9023
- return new PipelineExecutionError("".concat(code, ": ").concat(message));
9182
+ return new PipelineExecutionError("".concat(code || '(No Azure error code)', ": ").concat(message));
9024
9183
  };
9025
9184
  return AzureOpenAiExecutionTools;
9026
9185
  }());
@@ -9097,6 +9256,24 @@ var _OpenAiMetadataRegistration = $llmToolsMetadataRegister.register({
9097
9256
  },
9098
9257
  });
9099
9258
 
9259
+ /**
9260
+ * Detects if the code is running in a browser environment in main thread (Not in a web worker)
9261
+ *
9262
+ * Note: `$` is used to indicate that this function is not a pure function - it looks at the global object to determine the environment
9263
+ *
9264
+ * @public exported from `@promptbook/utils`
9265
+ */
9266
+ var $isRunningInBrowser = new Function("\n try {\n return this === window;\n } catch (e) {\n return false;\n }\n");
9267
+
9268
+ /**
9269
+ * Detects if the code is running in a web worker
9270
+ *
9271
+ * Note: `$` is used to indicate that this function is not a pure function - it looks at the global object to determine the environment
9272
+ *
9273
+ * @public exported from `@promptbook/utils`
9274
+ */
9275
+ var $isRunningInWebWorker = new Function("\n try {\n if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {\n return true;\n } else {\n return false;\n }\n } catch (e) {\n return false;\n }\n");
9276
+
9100
9277
  /**
9101
9278
  * Computes the usage of the OpenAI API based on the response from OpenAI
9102
9279
  *
@@ -9282,7 +9459,7 @@ var OpenAiExecutionTools = /** @class */ (function () {
9282
9459
  if (resultContent === null) {
9283
9460
  throw new PipelineExecutionError('No response message from OpenAI');
9284
9461
  }
9285
- return [2 /*return*/, {
9462
+ return [2 /*return*/, $asDeeplyFrozenSerializableJson('OpenAiExecutionTools ChatPromptResult', {
9286
9463
  content: resultContent,
9287
9464
  modelName: rawResponse.model || modelName,
9288
9465
  timing: {
@@ -9294,7 +9471,7 @@ var OpenAiExecutionTools = /** @class */ (function () {
9294
9471
  rawRequest: rawRequest,
9295
9472
  rawResponse: rawResponse,
9296
9473
  // <- [🗯]
9297
- }];
9474
+ })];
9298
9475
  }
9299
9476
  });
9300
9477
  });
@@ -9351,7 +9528,7 @@ var OpenAiExecutionTools = /** @class */ (function () {
9351
9528
  // eslint-disable-next-line prefer-const
9352
9529
  complete = getCurrentIsoDate();
9353
9530
  usage = computeOpenAiUsage(content, resultContent || '', rawResponse);
9354
- return [2 /*return*/, {
9531
+ return [2 /*return*/, $asDeeplyFrozenSerializableJson('OpenAiExecutionTools CompletionPromptResult', {
9355
9532
  content: resultContent,
9356
9533
  modelName: rawResponse.model || modelName,
9357
9534
  timing: {
@@ -9363,7 +9540,7 @@ var OpenAiExecutionTools = /** @class */ (function () {
9363
9540
  rawRequest: rawRequest,
9364
9541
  rawResponse: rawResponse,
9365
9542
  // <- [🗯]
9366
- }];
9543
+ })];
9367
9544
  }
9368
9545
  });
9369
9546
  });
@@ -9411,7 +9588,7 @@ var OpenAiExecutionTools = /** @class */ (function () {
9411
9588
  // eslint-disable-next-line prefer-const
9412
9589
  complete = getCurrentIsoDate();
9413
9590
  usage = computeOpenAiUsage(content, '', rawResponse);
9414
- return [2 /*return*/, {
9591
+ return [2 /*return*/, $asDeeplyFrozenSerializableJson('OpenAiExecutionTools EmbeddingPromptResult', {
9415
9592
  content: resultContent,
9416
9593
  modelName: rawResponse.model || modelName,
9417
9594
  timing: {
@@ -9423,7 +9600,7 @@ var OpenAiExecutionTools = /** @class */ (function () {
9423
9600
  rawRequest: rawRequest,
9424
9601
  rawResponse: rawResponse,
9425
9602
  // <- [🗯]
9426
- }];
9603
+ })];
9427
9604
  }
9428
9605
  });
9429
9606
  });
@@ -9438,7 +9615,7 @@ var OpenAiExecutionTools = /** @class */ (function () {
9438
9615
  return modelName === defaultModelName;
9439
9616
  });
9440
9617
  if (model === undefined) {
9441
- throw new UnexpectedError(spaceTrim(function (block) {
9618
+ throw new UnexpectedError(spaceTrim$1(function (block) {
9442
9619
  return "\n Cannot find model in OpenAI models with name \"".concat(defaultModelName, "\" which should be used as default.\n\n Available models:\n ").concat(block(OPENAI_MODELS.map(function (_a) {
9443
9620
  var modelName = _a.modelName;
9444
9621
  return "- \"".concat(modelName, "\"");
@@ -9482,6 +9659,9 @@ var OpenAiExecutionTools = /** @class */ (function () {
9482
9659
  */
9483
9660
  var createOpenAiExecutionTools = Object.assign(function (options) {
9484
9661
  // TODO: [🧠] !!!! If browser, auto add `dangerouslyAllowBrowser`
9662
+ if (($isRunningInBrowser() || $isRunningInWebWorker()) && !options.dangerouslyAllowBrowser) {
9663
+ options = __assign(__assign({}, options), { dangerouslyAllowBrowser: true });
9664
+ }
9485
9665
  return new OpenAiExecutionTools(options);
9486
9666
  }, {
9487
9667
  packageName: '@promptbook/openai',