@promptbook/core 0.44.0-9 โ†’ 0.45.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +740 -2
  2. package/esm/index.es.js +164 -44
  3. package/esm/index.es.js.map +1 -1
  4. package/esm/typings/_packages/utils.index.d.ts +4 -2
  5. package/esm/typings/conversion/promptbookStringToJson.d.ts +2 -2
  6. package/esm/typings/execution/ExecutionTools.d.ts +3 -3
  7. package/esm/typings/execution/NaturalExecutionTools.d.ts +2 -2
  8. package/esm/typings/execution/ScriptExecutionTools.d.ts +2 -2
  9. package/esm/typings/execution/UserInterfaceTools.d.ts +1 -1
  10. package/esm/typings/execution/plugins/natural-execution-tools/langtail/LangtailExecutionTools.d.ts +10 -0
  11. package/esm/typings/execution/plugins/natural-execution-tools/mocked/fakeTextToExpectations.d.ts +6 -2
  12. package/esm/typings/execution/plugins/natural-execution-tools/openai/OpenAiExecutionTools.d.ts +5 -0
  13. package/esm/typings/execution/plugins/natural-execution-tools/openai/OpenAiExecutionToolsOptions.d.ts +7 -7
  14. package/esm/typings/execution/plugins/natural-execution-tools/remote/RemoteNaturalExecutionToolsOptions.d.ts +1 -1
  15. package/esm/typings/execution/plugins/script-execution-tools/javascript/JavascriptExecutionToolsOptions.d.ts +5 -1
  16. package/esm/typings/execution/plugins/script-execution-tools/javascript/utils/unknownToString.d.ts +7 -0
  17. package/esm/typings/execution/plugins/user-interface-execution-tools/callback/CallbackInterfaceToolsOptions.d.ts +3 -3
  18. package/esm/typings/execution/translation/automatic-translate/automatic-translators/AutomaticTranslator.d.ts +1 -1
  19. package/esm/typings/library/PromptbookLibrary.d.ts +1 -1
  20. package/esm/typings/types/ExecutionTypes.d.ts +1 -1
  21. package/esm/typings/types/ModelRequirements.d.ts +1 -1
  22. package/esm/typings/types/Prompt.d.ts +5 -0
  23. package/esm/typings/types/PromptbookJson/PromptTemplateJson.d.ts +6 -5
  24. package/esm/typings/types/PromptbookJson/PromptTemplateParameterJson.d.ts +1 -1
  25. package/esm/typings/types/PromptbookJson/PromptbookJson.d.ts +3 -3
  26. package/esm/typings/types/ScriptLanguage.d.ts +1 -1
  27. package/esm/typings/types/TaskProgress.d.ts +2 -2
  28. package/esm/typings/types/execution-report/ExecutionReportStringOptions.d.ts +1 -1
  29. package/esm/typings/utils/markdown/extractAllBlocksFromMarkdown.d.ts +1 -1
  30. package/package.json +3 -3
  31. package/umd/index.umd.js +204 -88
  32. package/umd/index.umd.js.map +1 -1
  33. package/umd/typings/_packages/utils.index.d.ts +4 -2
  34. package/umd/typings/conversion/promptbookStringToJson.d.ts +2 -2
  35. package/umd/typings/execution/ExecutionTools.d.ts +3 -3
  36. package/umd/typings/execution/NaturalExecutionTools.d.ts +2 -2
  37. package/umd/typings/execution/ScriptExecutionTools.d.ts +2 -2
  38. package/umd/typings/execution/UserInterfaceTools.d.ts +1 -1
  39. package/umd/typings/execution/plugins/natural-execution-tools/langtail/LangtailExecutionTools.d.ts +10 -0
  40. package/umd/typings/execution/plugins/natural-execution-tools/mocked/fakeTextToExpectations.d.ts +6 -2
  41. package/umd/typings/execution/plugins/natural-execution-tools/openai/OpenAiExecutionTools.d.ts +5 -0
  42. package/umd/typings/execution/plugins/natural-execution-tools/openai/OpenAiExecutionToolsOptions.d.ts +7 -7
  43. package/umd/typings/execution/plugins/natural-execution-tools/remote/RemoteNaturalExecutionToolsOptions.d.ts +1 -1
  44. package/umd/typings/execution/plugins/script-execution-tools/javascript/JavascriptExecutionToolsOptions.d.ts +5 -1
  45. package/umd/typings/execution/plugins/script-execution-tools/javascript/utils/unknownToString.d.ts +7 -0
  46. package/umd/typings/execution/plugins/user-interface-execution-tools/callback/CallbackInterfaceToolsOptions.d.ts +3 -3
  47. package/umd/typings/execution/translation/automatic-translate/automatic-translators/AutomaticTranslator.d.ts +1 -1
  48. package/umd/typings/library/PromptbookLibrary.d.ts +1 -1
  49. package/umd/typings/types/ExecutionTypes.d.ts +1 -1
  50. package/umd/typings/types/ModelRequirements.d.ts +1 -1
  51. package/umd/typings/types/Prompt.d.ts +5 -0
  52. package/umd/typings/types/PromptbookJson/PromptTemplateJson.d.ts +6 -5
  53. package/umd/typings/types/PromptbookJson/PromptTemplateParameterJson.d.ts +1 -1
  54. package/umd/typings/types/PromptbookJson/PromptbookJson.d.ts +3 -3
  55. package/umd/typings/types/ScriptLanguage.d.ts +1 -1
  56. package/umd/typings/types/TaskProgress.d.ts +2 -2
  57. package/umd/typings/types/execution-report/ExecutionReportStringOptions.d.ts +1 -1
  58. package/umd/typings/utils/markdown/extractAllBlocksFromMarkdown.d.ts +1 -1
package/esm/index.es.js CHANGED
@@ -1,4 +1,4 @@
1
- import spaceTrim from 'spacetrim';
1
+ import { spaceTrim } from 'spacetrim';
2
2
  import 'prettier';
3
3
  import 'prettier/parser-html';
4
4
  import 'moment';
@@ -1161,7 +1161,7 @@ function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
1161
1161
  /**
1162
1162
  * The version of the Promptbook library
1163
1163
  */
1164
- var PROMPTBOOK_VERSION = '0.44.0-8';
1164
+ var PROMPTBOOK_VERSION = '0.44.0';
1165
1165
 
1166
1166
  /**
1167
1167
  * Parses the given script and returns the list of all used variables that are not defined in the script
@@ -1986,6 +1986,7 @@ function createPromptbookExecutor(options) {
1986
1986
  return __awaiter(this, void 0, void 0, function () {
1987
1987
  var name, title, priority, prompt, chatThread, completionResult, result, resultString, expectError, scriptExecutionErrors, maxAttempts, jokers, attempt, isJokerAttempt, joker, _a, _b, _c, _d, scriptTools, error_2, e_2_1, _e, _f, functionName, postprocessingError, _g, _h, scriptTools, error_3, e_3_1, e_4_1, error_4;
1988
1988
  var e_2, _j, e_4, _k, e_3, _l, _m;
1989
+ var _this = this;
1989
1990
  return __generator(this, function (_o) {
1990
1991
  switch (_o.label) {
1991
1992
  case 0:
@@ -2055,6 +2056,67 @@ function createPromptbookExecutor(options) {
2055
2056
  content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
2056
2057
  modelRequirements: currentTemplate.modelRequirements,
2057
2058
  expectations: currentTemplate.expectations,
2059
+ postprocessing: (currentTemplate.postprocessing || []).map(function (functionName) { return function (result) { return __awaiter(_this, void 0, void 0, function () {
2060
+ var errors, _a, _b, scriptTools, error_5, e_5_1;
2061
+ var e_5, _c;
2062
+ return __generator(this, function (_d) {
2063
+ switch (_d.label) {
2064
+ case 0:
2065
+ errors = [];
2066
+ _d.label = 1;
2067
+ case 1:
2068
+ _d.trys.push([1, 8, 9, 10]);
2069
+ _a = __values(tools.script), _b = _a.next();
2070
+ _d.label = 2;
2071
+ case 2:
2072
+ if (!!_b.done) return [3 /*break*/, 7];
2073
+ scriptTools = _b.value;
2074
+ _d.label = 3;
2075
+ case 3:
2076
+ _d.trys.push([3, 5, , 6]);
2077
+ return [4 /*yield*/, scriptTools.execute({
2078
+ scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
2079
+ script: "".concat(functionName, "(result)"),
2080
+ parameters: {
2081
+ result: result || '',
2082
+ // Note: No ...parametersToPass, because working with result only
2083
+ },
2084
+ })];
2085
+ case 4: return [2 /*return*/, _d.sent()];
2086
+ case 5:
2087
+ error_5 = _d.sent();
2088
+ if (!(error_5 instanceof Error)) {
2089
+ throw error_5;
2090
+ }
2091
+ errors.push(error_5);
2092
+ return [3 /*break*/, 6];
2093
+ case 6:
2094
+ _b = _a.next();
2095
+ return [3 /*break*/, 2];
2096
+ case 7: return [3 /*break*/, 10];
2097
+ case 8:
2098
+ e_5_1 = _d.sent();
2099
+ e_5 = { error: e_5_1 };
2100
+ return [3 /*break*/, 10];
2101
+ case 9:
2102
+ try {
2103
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
2104
+ }
2105
+ finally { if (e_5) throw e_5.error; }
2106
+ return [7 /*endfinally*/];
2107
+ case 10:
2108
+ if (errors.length === 0) {
2109
+ throw new PromptbookExecutionError('Postprocessing in NaturalExecutionTools failed because no ScriptExecutionTools were provided');
2110
+ }
2111
+ else if (errors.length === 1) {
2112
+ throw errors[0];
2113
+ }
2114
+ else {
2115
+ throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n Postprocessing in NaturalExecutionTools failed ".concat(errors.length, "x\n\n ").concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n\n')), "\n "); }));
2116
+ }
2117
+ }
2118
+ });
2119
+ }); }; }),
2058
2120
  };
2059
2121
  _b = currentTemplate.modelRequirements.modelVariant;
2060
2122
  switch (_b) {
@@ -2127,7 +2189,7 @@ function createPromptbookExecutor(options) {
2127
2189
  finally { if (e_2) throw e_2.error; }
2128
2190
  return [7 /*endfinally*/];
2129
2191
  case 23:
2130
- if (resultString) {
2192
+ if (resultString !== null) {
2131
2193
  return [3 /*break*/, 27];
2132
2194
  }
2133
2195
  if (scriptExecutionErrors.length === 1) {
@@ -2180,7 +2242,10 @@ function createPromptbookExecutor(options) {
2180
2242
  return [4 /*yield*/, scriptTools.execute({
2181
2243
  scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
2182
2244
  script: "".concat(functionName, "(resultString)"),
2183
- parameters: __assign(__assign({}, parametersToPass), { resultString: resultString || '' }),
2245
+ parameters: {
2246
+ resultString: resultString || '',
2247
+ // Note: No ...parametersToPass, because working with result only
2248
+ },
2184
2249
  })];
2185
2250
  case 33:
2186
2251
  resultString = _o.sent();
@@ -2271,7 +2336,7 @@ function createPromptbookExecutor(options) {
2271
2336
  return [7 /*endfinally*/];
2272
2337
  case 47:
2273
2338
  if (expectError !== null && attempt === maxAttempts - 1) {
2274
- throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n Natural execution failed ".concat(maxExecutionAttempts, "x\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 "); }));
2339
+ throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n Natural 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 "); }));
2275
2340
  }
2276
2341
  _o.label = 48;
2277
2342
  case 48:
@@ -2503,29 +2568,73 @@ var MockedEchoNaturalExecutionTools = /** @class */ (function () {
2503
2568
  /**
2504
2569
  * Gets the expectations and creates a fake text that meets the expectations
2505
2570
  *
2571
+ * Note: You can provide postprocessing functions to modify the text before checking the expectations
2572
+ * The result will be the text BEFORE the postprocessing
2573
+ *
2506
2574
  * @private internal util for MockedFackedNaturalExecutionTools
2507
2575
  */
2508
- function $fakeTextToExpectations(expectations) {
2509
- var lorem = new LoremIpsum({
2510
- wordsPerSentence: { min: 5, max: 15 },
2511
- sentencesPerParagraph: { min: 5, max: 15 },
2576
+ function $fakeTextToExpectations(expectations, postprocessing) {
2577
+ return __awaiter(this, void 0, void 0, function () {
2578
+ var lorem, loremText, text, loopLimit, textToCheck, _a, _b, func, e_1_1;
2579
+ var e_1, _c;
2580
+ return __generator(this, function (_d) {
2581
+ switch (_d.label) {
2582
+ case 0:
2583
+ lorem = new LoremIpsum({
2584
+ wordsPerSentence: { min: 5, max: 15 },
2585
+ sentencesPerParagraph: { min: 5, max: 15 },
2586
+ });
2587
+ loremText = '';
2588
+ text = '';
2589
+ loopLimit = CHARACTER_LOOP_LIMIT;
2590
+ _d.label = 1;
2591
+ case 1:
2592
+ if (!(loopLimit-- > 0)) return [3 /*break*/, 11];
2593
+ textToCheck = text;
2594
+ _d.label = 2;
2595
+ case 2:
2596
+ _d.trys.push([2, 7, 8, 9]);
2597
+ _a = (e_1 = void 0, __values(postprocessing || [])), _b = _a.next();
2598
+ _d.label = 3;
2599
+ case 3:
2600
+ if (!!_b.done) return [3 /*break*/, 6];
2601
+ func = _b.value;
2602
+ return [4 /*yield*/, func(textToCheck)];
2603
+ case 4:
2604
+ textToCheck = _d.sent();
2605
+ _d.label = 5;
2606
+ case 5:
2607
+ _b = _a.next();
2608
+ return [3 /*break*/, 3];
2609
+ case 6: return [3 /*break*/, 9];
2610
+ case 7:
2611
+ e_1_1 = _d.sent();
2612
+ e_1 = { error: e_1_1 };
2613
+ return [3 /*break*/, 9];
2614
+ case 8:
2615
+ try {
2616
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
2617
+ }
2618
+ finally { if (e_1) throw e_1.error; }
2619
+ return [7 /*endfinally*/];
2620
+ case 9:
2621
+ if (isPassingExpectations(expectations, textToCheck)) {
2622
+ return [2 /*return*/, text]; // <- Note: Returning the text because the postprocessing
2623
+ }
2624
+ if (loremText === '') {
2625
+ loremText = lorem.generateParagraphs(1) + '\n\n';
2626
+ }
2627
+ text += loremText.substring(0, 1);
2628
+ loremText = loremText.substring(1);
2629
+ _d.label = 10;
2630
+ case 10: return [3 /*break*/, 1];
2631
+ case 11: throw new Error(spaceTrim(function (block) { return "\n Can not generate fake text to met the expectations\n\n Loop limit reached\n The expectations:\n ".concat(block(JSON.stringify(expectations, null, 4)), "\n\n The draft text:\n ").concat(block(text), "\n\n "); }));
2632
+ }
2633
+ });
2512
2634
  });
2513
- var loremText = '';
2514
- var text = '';
2515
- for (var loopLimit = CHARACTER_LOOP_LIMIT; loopLimit-- > 0;) {
2516
- if (isPassingExpectations(expectations, text)) {
2517
- return text;
2518
- }
2519
- if (loremText === '') {
2520
- loremText = lorem.generateParagraphs(1) + '\n\n';
2521
- }
2522
- text += loremText.substring(0, 1);
2523
- loremText = loremText.substring(1);
2524
- }
2525
- throw new Error(spaceTrim(function (block) { return "\n Can not generate fake text to met the expectations\n\n Loop limit reached\n The expectations:\n ".concat(block(JSON.stringify(expectations, null, 4)), "\n\n The draft text:\n ").concat(block(text), "\n\n "); }));
2526
2635
  }
2527
2636
  /**
2528
- * TODO: Implement better
2637
+ * TODO: Implement better - create FakeLLM from this
2529
2638
  * TODO: [๐Ÿ’] Unite object for expecting amount and format - use here also a format
2530
2639
  */
2531
2640
 
@@ -2541,29 +2650,40 @@ var MockedFackedNaturalExecutionTools = /** @class */ (function () {
2541
2650
  */
2542
2651
  MockedFackedNaturalExecutionTools.prototype.gptChat = function (prompt) {
2543
2652
  return __awaiter(this, void 0, void 0, function () {
2653
+ var content, result;
2544
2654
  return __generator(this, function (_a) {
2545
- if (this.options.isVerbose) {
2546
- console.info('๐Ÿ’ฌ Mocked faked call');
2655
+ switch (_a.label) {
2656
+ case 0:
2657
+ if (this.options.isVerbose) {
2658
+ console.info('๐Ÿ’ฌ Mocked faked prompt', prompt);
2659
+ }
2660
+ return [4 /*yield*/, $fakeTextToExpectations(prompt.expectations || {
2661
+ sentences: { min: 1, max: 1 },
2662
+ }, prompt.postprocessing)];
2663
+ case 1:
2664
+ content = _a.sent();
2665
+ result = {
2666
+ content: content,
2667
+ model: 'mocked-facked',
2668
+ timing: {
2669
+ start: getCurrentIsoDate(),
2670
+ complete: getCurrentIsoDate(),
2671
+ },
2672
+ usage: {
2673
+ price: 0,
2674
+ inputTokens: 0,
2675
+ outputTokens: 0,
2676
+ },
2677
+ rawResponse: {
2678
+ note: 'This is mocked echo',
2679
+ },
2680
+ // <- [๐Ÿคนโ€โ™‚๏ธ]
2681
+ };
2682
+ if (this.options.isVerbose) {
2683
+ console.info('๐Ÿ’ฌ Mocked faked result', result);
2684
+ }
2685
+ return [2 /*return*/, result];
2547
2686
  }
2548
- return [2 /*return*/, {
2549
- content: $fakeTextToExpectations(prompt.expectations || {
2550
- sentences: { min: 1, max: 1 },
2551
- }),
2552
- model: 'mocked-facked',
2553
- timing: {
2554
- start: getCurrentIsoDate(),
2555
- complete: getCurrentIsoDate(),
2556
- },
2557
- usage: {
2558
- price: 0,
2559
- inputTokens: 0,
2560
- outputTokens: 0,
2561
- },
2562
- rawResponse: {
2563
- note: 'This is mocked echo',
2564
- },
2565
- // <- [๐Ÿคนโ€โ™‚๏ธ]
2566
- }];
2567
2687
  });
2568
2688
  });
2569
2689
  };