@promptbook/core 0.48.0 → 0.49.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 (26) hide show
  1. package/README.md +1 -5
  2. package/esm/index.es.js +231 -20
  3. package/esm/index.es.js.map +1 -1
  4. package/esm/typings/_packages/utils.index.d.ts +2 -1
  5. package/esm/typings/conversion/prettify/renderPromptbookMermaid.d.ts +15 -2
  6. package/esm/typings/conversion/promptbookJsonToString.d.ts +7 -3
  7. package/esm/typings/conversion/utils/renameParameter.d.ts +25 -0
  8. package/esm/typings/conversion/validation/_importPromptbook.d.ts +1 -0
  9. package/esm/typings/conversion/validation/validatePromptbookJson.d.ts +3 -2
  10. package/esm/typings/types/PromptbookJson/PromptTemplateJson.d.ts +2 -2
  11. package/esm/typings/utils/markdown/prettifyMarkdown.d.ts +2 -2
  12. package/package.json +2 -1
  13. package/umd/index.umd.js +232 -20
  14. package/umd/index.umd.js.map +1 -1
  15. package/umd/typings/_packages/utils.index.d.ts +2 -1
  16. package/umd/typings/conversion/prettify/renderPromptbookMermaid.d.ts +15 -2
  17. package/umd/typings/conversion/promptbookJsonToString.d.ts +7 -3
  18. package/umd/typings/conversion/promptbookStringToJson.test.d.ts +1 -0
  19. package/umd/typings/conversion/utils/renameParameter.d.ts +25 -0
  20. package/umd/typings/conversion/utils/renameParameter.test.d.ts +1 -0
  21. package/umd/typings/conversion/validation/_importPromptbook.d.ts +1 -0
  22. package/umd/typings/conversion/validation/validatePromptbookJson.d.ts +3 -2
  23. package/umd/typings/types/PromptbookJson/PromptTemplateJson.d.ts +2 -2
  24. package/umd/typings/utils/markdown/prettifyMarkdown.d.ts +2 -2
  25. /package/esm/typings/conversion/{validation/promptbookStringToJson.test.d.ts → promptbookStringToJson.test.d.ts} +0 -0
  26. /package/{umd/typings/conversion/validation/promptbookStringToJson.test.d.ts → esm/typings/conversion/utils/renameParameter.test.d.ts} +0 -0
package/umd/index.umd.js CHANGED
@@ -1,12 +1,13 @@
1
1
  (function (global, factory) {
2
2
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('spacetrim'), require('prettier'), require('prettier/parser-html'), require('moment')) :
3
3
  typeof define === 'function' && define.amd ? define(['exports', 'spacetrim', 'prettier', 'prettier/parser-html', 'moment'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["promptbook-core"] = {}, global.spaceTrim));
5
- })(this, (function (exports, spaceTrim) { 'use strict';
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["promptbook-core"] = {}, global.spaceTrim, global.prettier, global.parserHtml));
5
+ })(this, (function (exports, spaceTrim, prettier, parserHtml) { 'use strict';
6
6
 
7
7
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
8
 
9
9
  var spaceTrim__default = /*#__PURE__*/_interopDefaultLegacy(spaceTrim);
10
+ var parserHtml__default = /*#__PURE__*/_interopDefaultLegacy(parserHtml);
10
11
 
11
12
  /*! *****************************************************************************
12
13
  Copyright (c) Microsoft Corporation.
@@ -445,7 +446,7 @@
445
446
  /**
446
447
  * The version of the Promptbook library
447
448
  */
448
- var PROMPTBOOK_VERSION = '0.48.0-1';
449
+ var PROMPTBOOK_VERSION = '0.48.1-0';
449
450
 
450
451
  /**
451
452
  * Parses the given script and returns the list of all used variables that are not defined in the script
@@ -495,6 +496,38 @@
495
496
  return variables;
496
497
  }
497
498
 
499
+ /**
500
+ * Prettify the html code
501
+ *
502
+ * @param content raw html code
503
+ * @returns formatted html code
504
+ */
505
+ function prettifyMarkdown(content) {
506
+ try {
507
+ return prettier.format(content, {
508
+ parser: 'markdown',
509
+ plugins: [parserHtml__default["default"]],
510
+ // TODO: DRY - make some import or auto-copy of .prettierrc
511
+ endOfLine: 'lf',
512
+ tabWidth: 4,
513
+ singleQuote: true,
514
+ trailingComma: 'all',
515
+ arrowParens: 'always',
516
+ printWidth: 120,
517
+ htmlWhitespaceSensitivity: 'ignore',
518
+ jsxBracketSameLine: false,
519
+ bracketSpacing: true,
520
+ });
521
+ }
522
+ catch (error) {
523
+ console.error('There was an error with prettifying the markdown, using the original as the fallback', {
524
+ error: error,
525
+ html: content,
526
+ });
527
+ return content;
528
+ }
529
+ }
530
+
498
531
  /**
499
532
  * Removes emojis from a string and fix whitespaces
500
533
  *
@@ -587,6 +620,20 @@
587
620
  * TODO: Maybe use sth. like safe-eval in fraction/calculation case @see https://www.npmjs.com/package/safe-eval
588
621
  */
589
622
 
623
+ /**
624
+ * This error indicates that the promptbook object has valid syntax but contains logical errors (like circular dependencies)
625
+ */
626
+ var PromptbookLogicError = /** @class */ (function (_super) {
627
+ __extends(PromptbookLogicError, _super);
628
+ function PromptbookLogicError(message) {
629
+ var _this = _super.call(this, message) || this;
630
+ _this.name = 'PromptbookLogicError';
631
+ Object.setPrototypeOf(_this, PromptbookLogicError.prototype);
632
+ return _this;
633
+ }
634
+ return PromptbookLogicError;
635
+ }(Error));
636
+
590
637
  /**
591
638
  * This error indicates errors during the execution of the promptbook
592
639
  */
@@ -1755,28 +1802,191 @@
1755
1802
  * TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
1756
1803
  */
1757
1804
 
1805
+ /**
1806
+ * Converts promptbook in JSON format to string format
1807
+ *
1808
+ * @param promptbookJson Promptbook in JSON format (.ptbk.json)
1809
+ * @returns Promptbook in string format (.ptbk.md)
1810
+ */
1758
1811
  function promptbookJsonToString(promptbookJson) {
1759
- return spaceTrim__default["default"](function (block) { return "\n\n # ".concat(block(promptbookJson.title), "\n\n "); });
1812
+ var e_1, _a, e_2, _b, e_3, _c, e_4, _d, e_5, _e;
1813
+ var title = promptbookJson.title, promptbookUrl = promptbookJson.promptbookUrl, promptbookVersion = promptbookJson.promptbookVersion, description = promptbookJson.description, parameters = promptbookJson.parameters, promptTemplates = promptbookJson.promptTemplates;
1814
+ var promptbookString = "# ".concat(title);
1815
+ if (description) {
1816
+ promptbookString += '\n\n';
1817
+ promptbookString += description;
1818
+ }
1819
+ // TODO:> const commands: Array<Command>
1820
+ var commands = [];
1821
+ if (promptbookUrl) {
1822
+ commands.push("PROMPTBOOK URL ".concat(promptbookUrl));
1823
+ }
1824
+ commands.push("PROMPTBOOK VERSION ".concat(promptbookVersion));
1825
+ promptbookString = prettifyMarkdown(promptbookString);
1826
+ try {
1827
+ for (var parameters_1 = __values(parameters), parameters_1_1 = parameters_1.next(); !parameters_1_1.done; parameters_1_1 = parameters_1.next()) {
1828
+ var parameter = parameters_1_1.value;
1829
+ var isInput = parameter.isInput, isOutput = parameter.isOutput;
1830
+ if (isInput) {
1831
+ commands.push("INPUT PARAMETER ".concat(promptTemplateParameterJsonToString(parameter)));
1832
+ }
1833
+ else if (isOutput) {
1834
+ commands.push("OUTPUT PARAMETER ".concat(promptTemplateParameterJsonToString(parameter)));
1835
+ }
1836
+ }
1837
+ }
1838
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1839
+ finally {
1840
+ try {
1841
+ if (parameters_1_1 && !parameters_1_1.done && (_a = parameters_1.return)) _a.call(parameters_1);
1842
+ }
1843
+ finally { if (e_1) throw e_1.error; }
1844
+ }
1845
+ promptbookString += '\n\n';
1846
+ promptbookString += commands.map(function (command) { return "- ".concat(command); }).join('\n');
1847
+ try {
1848
+ for (var promptTemplates_1 = __values(promptTemplates), promptTemplates_1_1 = promptTemplates_1.next(); !promptTemplates_1_1.done; promptTemplates_1_1 = promptTemplates_1.next()) {
1849
+ var promptTemplate = promptTemplates_1_1.value;
1850
+ var
1851
+ /* Note: Not using:> name, */
1852
+ title_1 = promptTemplate.title, description_1 = promptTemplate.description,
1853
+ /* Note: dependentParameterNames, */
1854
+ jokers = promptTemplate.jokers, executionType = promptTemplate.executionType, content = promptTemplate.content, postprocessing = promptTemplate.postprocessing, expectations = promptTemplate.expectations, expectFormat = promptTemplate.expectFormat, resultingParameterName = promptTemplate.resultingParameterName;
1855
+ promptbookString += '\n\n';
1856
+ promptbookString += "## ".concat(title_1);
1857
+ if (description_1) {
1858
+ promptbookString += '\n\n';
1859
+ promptbookString += description_1;
1860
+ }
1861
+ // TODO:> const commands: Array<Command>
1862
+ var commands_1 = [];
1863
+ var contentLanguage = 'text';
1864
+ if (executionType === 'PROMPT_TEMPLATE') {
1865
+ var modelRequirements = promptTemplate.modelRequirements;
1866
+ var modelName = modelRequirements.modelName, modelVariant = modelRequirements.modelVariant;
1867
+ commands_1.push("EXECUTE PROMPT TEMPLATE");
1868
+ if (modelVariant) {
1869
+ commands_1.push("MODEL VARIANT ".concat(capitalize(modelVariant)));
1870
+ }
1871
+ if (modelName) {
1872
+ commands_1.push("MODEL NAME `".concat(modelName, "`"));
1873
+ }
1874
+ }
1875
+ else if (executionType === 'SIMPLE_TEMPLATE') {
1876
+ commands_1.push("SIMPLE TEMPLATE");
1877
+ // Note: Nothing special here
1878
+ }
1879
+ else if (executionType === 'SCRIPT') {
1880
+ commands_1.push("EXECUTE SCRIPT");
1881
+ if (promptTemplate.contentLanguage) {
1882
+ contentLanguage = promptTemplate.contentLanguage;
1883
+ }
1884
+ else {
1885
+ contentLanguage = '';
1886
+ }
1887
+ }
1888
+ else if (executionType === 'PROMPT_DIALOG') {
1889
+ commands_1.push("PROMPT DIALOG");
1890
+ // Note: Nothing special here
1891
+ }
1892
+ if (jokers) {
1893
+ try {
1894
+ for (var jokers_1 = (e_3 = void 0, __values(jokers)), jokers_1_1 = jokers_1.next(); !jokers_1_1.done; jokers_1_1 = jokers_1.next()) {
1895
+ var joker = jokers_1_1.value;
1896
+ commands_1.push("JOKER {".concat(joker, "}"));
1897
+ }
1898
+ }
1899
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
1900
+ finally {
1901
+ try {
1902
+ if (jokers_1_1 && !jokers_1_1.done && (_c = jokers_1.return)) _c.call(jokers_1);
1903
+ }
1904
+ finally { if (e_3) throw e_3.error; }
1905
+ }
1906
+ } /* not else */
1907
+ if (postprocessing) {
1908
+ try {
1909
+ for (var postprocessing_1 = (e_4 = void 0, __values(postprocessing)), postprocessing_1_1 = postprocessing_1.next(); !postprocessing_1_1.done; postprocessing_1_1 = postprocessing_1.next()) {
1910
+ var postprocessingFunctionName = postprocessing_1_1.value;
1911
+ commands_1.push("POSTPROCESSING `".concat(postprocessingFunctionName, "`"));
1912
+ }
1913
+ }
1914
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
1915
+ finally {
1916
+ try {
1917
+ if (postprocessing_1_1 && !postprocessing_1_1.done && (_d = postprocessing_1.return)) _d.call(postprocessing_1);
1918
+ }
1919
+ finally { if (e_4) throw e_4.error; }
1920
+ }
1921
+ } /* not else */
1922
+ if (expectations) {
1923
+ try {
1924
+ for (var _f = (e_5 = void 0, __values(Object.entries(expectations))), _g = _f.next(); !_g.done; _g = _f.next()) {
1925
+ var _h = __read(_g.value, 2), unit = _h[0], _j = _h[1], min = _j.min, max = _j.max;
1926
+ if (min === max) {
1927
+ commands_1.push("EXPECT EXACTLY ".concat(min, " ").concat(capitalize(unit + (min > 1 ? 's' : ''))));
1928
+ }
1929
+ else {
1930
+ if (min !== undefined) {
1931
+ commands_1.push("EXPECT MIN ".concat(min, " ").concat(capitalize(unit + (min > 1 ? 's' : ''))));
1932
+ } /* not else */
1933
+ if (max !== undefined) {
1934
+ commands_1.push("EXPECT MAX ".concat(max, " ").concat(capitalize(unit + (max > 1 ? 's' : ''))));
1935
+ }
1936
+ }
1937
+ }
1938
+ }
1939
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
1940
+ finally {
1941
+ try {
1942
+ if (_g && !_g.done && (_e = _f.return)) _e.call(_f);
1943
+ }
1944
+ finally { if (e_5) throw e_5.error; }
1945
+ }
1946
+ } /* not else */
1947
+ if (expectFormat) {
1948
+ if (expectFormat === 'JSON') {
1949
+ // TODO: @deprecated remove
1950
+ commands_1.push("EXPECT JSON");
1951
+ }
1952
+ } /* not else */
1953
+ promptbookString += '\n\n';
1954
+ promptbookString += commands_1.map(function (command) { return "- ".concat(command); }).join('\n');
1955
+ promptbookString += '\n\n';
1956
+ promptbookString += '```' + contentLanguage;
1957
+ promptbookString += '\n';
1958
+ promptbookString += spaceTrim__default["default"](content);
1959
+ // <- TODO: !!! Escape
1960
+ // <- TODO: [🧠] Some clear strategy how to spaceTrim the blocks
1961
+ promptbookString += '\n';
1962
+ promptbookString += '```';
1963
+ promptbookString += '\n\n';
1964
+ promptbookString += "`-> {".concat(resultingParameterName, "}`"); // <- TODO: !!! If the parameter here has description, add it and use promptTemplateParameterJsonToString
1965
+ }
1966
+ }
1967
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
1968
+ finally {
1969
+ try {
1970
+ if (promptTemplates_1_1 && !promptTemplates_1_1.done && (_b = promptTemplates_1.return)) _b.call(promptTemplates_1);
1971
+ }
1972
+ finally { if (e_2) throw e_2.error; }
1973
+ }
1974
+ return promptbookString;
1760
1975
  }
1761
1976
  /**
1762
- * TODO: !!!!! Implement
1763
- * TODO: !!!!! Annotate and warn
1764
- * TODO: !!!!! Test + test together with promptbookStringToJson
1977
+ * @private internal util of promptbookJsonToString
1765
1978
  */
1766
-
1979
+ function promptTemplateParameterJsonToString(promptTemplateParameterJson) {
1980
+ var name = promptTemplateParameterJson.name, description = promptTemplateParameterJson.description;
1981
+ var parameterString = "{".concat(name, "}");
1982
+ if (description) {
1983
+ parameterString = "".concat(parameterString, " ").concat(description);
1984
+ }
1985
+ return parameterString;
1986
+ }
1767
1987
  /**
1768
- * This error indicates that the promptbook object has valid syntax but contains logical errors (like circular dependencies)
1988
+ * TODO: Escape all
1769
1989
  */
1770
- var PromptbookLogicError = /** @class */ (function (_super) {
1771
- __extends(PromptbookLogicError, _super);
1772
- function PromptbookLogicError(message) {
1773
- var _this = _super.call(this, message) || this;
1774
- _this.name = 'PromptbookLogicError';
1775
- Object.setPrototypeOf(_this, PromptbookLogicError.prototype);
1776
- return _this;
1777
- }
1778
- return PromptbookLogicError;
1779
- }(Error));
1780
1990
 
1781
1991
  /**
1782
1992
  * Tests if given string is valid URL.
@@ -1804,7 +2014,7 @@
1804
2014
  }
1805
2015
 
1806
2016
  /**
1807
- * Validates PromptbookJson if it is logically valid.
2017
+ * Validates PromptbookJson if it is logically valid
1808
2018
  *
1809
2019
  * It checks:
1810
2020
  * - if it has correct parameters dependency
@@ -1814,6 +2024,7 @@
1814
2024
  * - if it is meaningful
1815
2025
  *
1816
2026
  * @param promptbook valid or invalid PromptbookJson
2027
+ * @returns the same promptbook if it is logically valid
1817
2028
  * @throws {PromptbookLogicError} on logical error in the promptbook
1818
2029
  */
1819
2030
  function validatePromptbookJson(promptbook) {
@@ -1965,6 +2176,7 @@
1965
2176
  while (unresovedTemplates.length > 0) {
1966
2177
  _loop_2();
1967
2178
  }
2179
+ return promptbook;
1968
2180
  }
1969
2181
  /**
1970
2182
  * TODO: [🧠] Work with promptbookVersion