@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/README.md CHANGED
@@ -5,12 +5,11 @@ Library to supercharge your use of large language models
5
5
 
6
6
 
7
7
 
8
-
9
8
  [![NPM Version of ![Promptbook logo - cube with letters P and B](./other/design/logo-h1.png) Promptbook](https://badge.fury.io/js/promptbook.svg)](https://www.npmjs.com/package/promptbook)
10
9
  [![Quality of package ![Promptbook logo - cube with letters P and B](./other/design/logo-h1.png) Promptbook](https://packagequality.com/shield/promptbook.svg)](https://packagequality.com/#?package=promptbook)
11
10
  [![Known Vulnerabilities](https://snyk.io/test/github/webgptorg/promptbook/badge.svg)](https://snyk.io/test/github/webgptorg/promptbook)
12
11
  [![Issues](https://img.shields.io/github/issues/webgptorg/promptbook.svg?style=flat)](https://github.com/webgptorg/promptbook/issues)
13
- [![Socket](https://socket.dev/api/badge/npm/package/promptbook)](https://socket.dev/npm/package/promptbook)
12
+
14
13
 
15
14
  [![Socket Badge](https://socket.dev/api/badge/npm/package/@promptbook/core)](https://socket.dev/npm/package/@promptbook/core)
16
15
 
@@ -664,9 +663,6 @@ GPTs are chat assistants that can be assigned to specific tasks and materials. B
664
663
 
665
664
 
666
665
 
667
-
668
-
669
-
670
666
  ### Where should I store my promptbooks?
671
667
 
672
668
  If you use raw SDKs, you just put prompts in the sourcecode, mixed in with typescript, javascript, python or whatever programming language you use.
package/esm/index.es.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import spaceTrim$1, { spaceTrim } from 'spacetrim';
2
- import 'prettier';
3
- import 'prettier/parser-html';
2
+ import { format } from 'prettier';
3
+ import parserHtml from 'prettier/parser-html';
4
4
  import 'moment';
5
5
 
6
6
  /*! *****************************************************************************
@@ -440,7 +440,7 @@ function removeContentComments(content) {
440
440
  /**
441
441
  * The version of the Promptbook library
442
442
  */
443
- var PROMPTBOOK_VERSION = '0.48.0-1';
443
+ var PROMPTBOOK_VERSION = '0.48.1-0';
444
444
 
445
445
  /**
446
446
  * Parses the given script and returns the list of all used variables that are not defined in the script
@@ -490,6 +490,38 @@ function extractVariables(script) {
490
490
  return variables;
491
491
  }
492
492
 
493
+ /**
494
+ * Prettify the html code
495
+ *
496
+ * @param content raw html code
497
+ * @returns formatted html code
498
+ */
499
+ function prettifyMarkdown(content) {
500
+ try {
501
+ return format(content, {
502
+ parser: 'markdown',
503
+ plugins: [parserHtml],
504
+ // TODO: DRY - make some import or auto-copy of .prettierrc
505
+ endOfLine: 'lf',
506
+ tabWidth: 4,
507
+ singleQuote: true,
508
+ trailingComma: 'all',
509
+ arrowParens: 'always',
510
+ printWidth: 120,
511
+ htmlWhitespaceSensitivity: 'ignore',
512
+ jsxBracketSameLine: false,
513
+ bracketSpacing: true,
514
+ });
515
+ }
516
+ catch (error) {
517
+ console.error('There was an error with prettifying the markdown, using the original as the fallback', {
518
+ error: error,
519
+ html: content,
520
+ });
521
+ return content;
522
+ }
523
+ }
524
+
493
525
  /**
494
526
  * Removes emojis from a string and fix whitespaces
495
527
  *
@@ -582,6 +614,20 @@ function parseNumber(value) {
582
614
  * TODO: Maybe use sth. like safe-eval in fraction/calculation case @see https://www.npmjs.com/package/safe-eval
583
615
  */
584
616
 
617
+ /**
618
+ * This error indicates that the promptbook object has valid syntax but contains logical errors (like circular dependencies)
619
+ */
620
+ var PromptbookLogicError = /** @class */ (function (_super) {
621
+ __extends(PromptbookLogicError, _super);
622
+ function PromptbookLogicError(message) {
623
+ var _this = _super.call(this, message) || this;
624
+ _this.name = 'PromptbookLogicError';
625
+ Object.setPrototypeOf(_this, PromptbookLogicError.prototype);
626
+ return _this;
627
+ }
628
+ return PromptbookLogicError;
629
+ }(Error));
630
+
585
631
  /**
586
632
  * This error indicates errors during the execution of the promptbook
587
633
  */
@@ -1750,28 +1796,191 @@ function promptbookStringToJson(promptbookString) {
1750
1796
  * TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
1751
1797
  */
1752
1798
 
1799
+ /**
1800
+ * Converts promptbook in JSON format to string format
1801
+ *
1802
+ * @param promptbookJson Promptbook in JSON format (.ptbk.json)
1803
+ * @returns Promptbook in string format (.ptbk.md)
1804
+ */
1753
1805
  function promptbookJsonToString(promptbookJson) {
1754
- return spaceTrim$1(function (block) { return "\n\n # ".concat(block(promptbookJson.title), "\n\n "); });
1806
+ var e_1, _a, e_2, _b, e_3, _c, e_4, _d, e_5, _e;
1807
+ var title = promptbookJson.title, promptbookUrl = promptbookJson.promptbookUrl, promptbookVersion = promptbookJson.promptbookVersion, description = promptbookJson.description, parameters = promptbookJson.parameters, promptTemplates = promptbookJson.promptTemplates;
1808
+ var promptbookString = "# ".concat(title);
1809
+ if (description) {
1810
+ promptbookString += '\n\n';
1811
+ promptbookString += description;
1812
+ }
1813
+ // TODO:> const commands: Array<Command>
1814
+ var commands = [];
1815
+ if (promptbookUrl) {
1816
+ commands.push("PROMPTBOOK URL ".concat(promptbookUrl));
1817
+ }
1818
+ commands.push("PROMPTBOOK VERSION ".concat(promptbookVersion));
1819
+ promptbookString = prettifyMarkdown(promptbookString);
1820
+ try {
1821
+ for (var parameters_1 = __values(parameters), parameters_1_1 = parameters_1.next(); !parameters_1_1.done; parameters_1_1 = parameters_1.next()) {
1822
+ var parameter = parameters_1_1.value;
1823
+ var isInput = parameter.isInput, isOutput = parameter.isOutput;
1824
+ if (isInput) {
1825
+ commands.push("INPUT PARAMETER ".concat(promptTemplateParameterJsonToString(parameter)));
1826
+ }
1827
+ else if (isOutput) {
1828
+ commands.push("OUTPUT PARAMETER ".concat(promptTemplateParameterJsonToString(parameter)));
1829
+ }
1830
+ }
1831
+ }
1832
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1833
+ finally {
1834
+ try {
1835
+ if (parameters_1_1 && !parameters_1_1.done && (_a = parameters_1.return)) _a.call(parameters_1);
1836
+ }
1837
+ finally { if (e_1) throw e_1.error; }
1838
+ }
1839
+ promptbookString += '\n\n';
1840
+ promptbookString += commands.map(function (command) { return "- ".concat(command); }).join('\n');
1841
+ try {
1842
+ for (var promptTemplates_1 = __values(promptTemplates), promptTemplates_1_1 = promptTemplates_1.next(); !promptTemplates_1_1.done; promptTemplates_1_1 = promptTemplates_1.next()) {
1843
+ var promptTemplate = promptTemplates_1_1.value;
1844
+ var
1845
+ /* Note: Not using:> name, */
1846
+ title_1 = promptTemplate.title, description_1 = promptTemplate.description,
1847
+ /* Note: dependentParameterNames, */
1848
+ jokers = promptTemplate.jokers, executionType = promptTemplate.executionType, content = promptTemplate.content, postprocessing = promptTemplate.postprocessing, expectations = promptTemplate.expectations, expectFormat = promptTemplate.expectFormat, resultingParameterName = promptTemplate.resultingParameterName;
1849
+ promptbookString += '\n\n';
1850
+ promptbookString += "## ".concat(title_1);
1851
+ if (description_1) {
1852
+ promptbookString += '\n\n';
1853
+ promptbookString += description_1;
1854
+ }
1855
+ // TODO:> const commands: Array<Command>
1856
+ var commands_1 = [];
1857
+ var contentLanguage = 'text';
1858
+ if (executionType === 'PROMPT_TEMPLATE') {
1859
+ var modelRequirements = promptTemplate.modelRequirements;
1860
+ var modelName = modelRequirements.modelName, modelVariant = modelRequirements.modelVariant;
1861
+ commands_1.push("EXECUTE PROMPT TEMPLATE");
1862
+ if (modelVariant) {
1863
+ commands_1.push("MODEL VARIANT ".concat(capitalize(modelVariant)));
1864
+ }
1865
+ if (modelName) {
1866
+ commands_1.push("MODEL NAME `".concat(modelName, "`"));
1867
+ }
1868
+ }
1869
+ else if (executionType === 'SIMPLE_TEMPLATE') {
1870
+ commands_1.push("SIMPLE TEMPLATE");
1871
+ // Note: Nothing special here
1872
+ }
1873
+ else if (executionType === 'SCRIPT') {
1874
+ commands_1.push("EXECUTE SCRIPT");
1875
+ if (promptTemplate.contentLanguage) {
1876
+ contentLanguage = promptTemplate.contentLanguage;
1877
+ }
1878
+ else {
1879
+ contentLanguage = '';
1880
+ }
1881
+ }
1882
+ else if (executionType === 'PROMPT_DIALOG') {
1883
+ commands_1.push("PROMPT DIALOG");
1884
+ // Note: Nothing special here
1885
+ }
1886
+ if (jokers) {
1887
+ try {
1888
+ 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()) {
1889
+ var joker = jokers_1_1.value;
1890
+ commands_1.push("JOKER {".concat(joker, "}"));
1891
+ }
1892
+ }
1893
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
1894
+ finally {
1895
+ try {
1896
+ if (jokers_1_1 && !jokers_1_1.done && (_c = jokers_1.return)) _c.call(jokers_1);
1897
+ }
1898
+ finally { if (e_3) throw e_3.error; }
1899
+ }
1900
+ } /* not else */
1901
+ if (postprocessing) {
1902
+ try {
1903
+ 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()) {
1904
+ var postprocessingFunctionName = postprocessing_1_1.value;
1905
+ commands_1.push("POSTPROCESSING `".concat(postprocessingFunctionName, "`"));
1906
+ }
1907
+ }
1908
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
1909
+ finally {
1910
+ try {
1911
+ if (postprocessing_1_1 && !postprocessing_1_1.done && (_d = postprocessing_1.return)) _d.call(postprocessing_1);
1912
+ }
1913
+ finally { if (e_4) throw e_4.error; }
1914
+ }
1915
+ } /* not else */
1916
+ if (expectations) {
1917
+ try {
1918
+ for (var _f = (e_5 = void 0, __values(Object.entries(expectations))), _g = _f.next(); !_g.done; _g = _f.next()) {
1919
+ var _h = __read(_g.value, 2), unit = _h[0], _j = _h[1], min = _j.min, max = _j.max;
1920
+ if (min === max) {
1921
+ commands_1.push("EXPECT EXACTLY ".concat(min, " ").concat(capitalize(unit + (min > 1 ? 's' : ''))));
1922
+ }
1923
+ else {
1924
+ if (min !== undefined) {
1925
+ commands_1.push("EXPECT MIN ".concat(min, " ").concat(capitalize(unit + (min > 1 ? 's' : ''))));
1926
+ } /* not else */
1927
+ if (max !== undefined) {
1928
+ commands_1.push("EXPECT MAX ".concat(max, " ").concat(capitalize(unit + (max > 1 ? 's' : ''))));
1929
+ }
1930
+ }
1931
+ }
1932
+ }
1933
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
1934
+ finally {
1935
+ try {
1936
+ if (_g && !_g.done && (_e = _f.return)) _e.call(_f);
1937
+ }
1938
+ finally { if (e_5) throw e_5.error; }
1939
+ }
1940
+ } /* not else */
1941
+ if (expectFormat) {
1942
+ if (expectFormat === 'JSON') {
1943
+ // TODO: @deprecated remove
1944
+ commands_1.push("EXPECT JSON");
1945
+ }
1946
+ } /* not else */
1947
+ promptbookString += '\n\n';
1948
+ promptbookString += commands_1.map(function (command) { return "- ".concat(command); }).join('\n');
1949
+ promptbookString += '\n\n';
1950
+ promptbookString += '```' + contentLanguage;
1951
+ promptbookString += '\n';
1952
+ promptbookString += spaceTrim$1(content);
1953
+ // <- TODO: !!! Escape
1954
+ // <- TODO: [🧠] Some clear strategy how to spaceTrim the blocks
1955
+ promptbookString += '\n';
1956
+ promptbookString += '```';
1957
+ promptbookString += '\n\n';
1958
+ promptbookString += "`-> {".concat(resultingParameterName, "}`"); // <- TODO: !!! If the parameter here has description, add it and use promptTemplateParameterJsonToString
1959
+ }
1960
+ }
1961
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
1962
+ finally {
1963
+ try {
1964
+ if (promptTemplates_1_1 && !promptTemplates_1_1.done && (_b = promptTemplates_1.return)) _b.call(promptTemplates_1);
1965
+ }
1966
+ finally { if (e_2) throw e_2.error; }
1967
+ }
1968
+ return promptbookString;
1755
1969
  }
1756
1970
  /**
1757
- * TODO: !!!!! Implement
1758
- * TODO: !!!!! Annotate and warn
1759
- * TODO: !!!!! Test + test together with promptbookStringToJson
1971
+ * @private internal util of promptbookJsonToString
1760
1972
  */
1761
-
1973
+ function promptTemplateParameterJsonToString(promptTemplateParameterJson) {
1974
+ var name = promptTemplateParameterJson.name, description = promptTemplateParameterJson.description;
1975
+ var parameterString = "{".concat(name, "}");
1976
+ if (description) {
1977
+ parameterString = "".concat(parameterString, " ").concat(description);
1978
+ }
1979
+ return parameterString;
1980
+ }
1762
1981
  /**
1763
- * This error indicates that the promptbook object has valid syntax but contains logical errors (like circular dependencies)
1982
+ * TODO: Escape all
1764
1983
  */
1765
- var PromptbookLogicError = /** @class */ (function (_super) {
1766
- __extends(PromptbookLogicError, _super);
1767
- function PromptbookLogicError(message) {
1768
- var _this = _super.call(this, message) || this;
1769
- _this.name = 'PromptbookLogicError';
1770
- Object.setPrototypeOf(_this, PromptbookLogicError.prototype);
1771
- return _this;
1772
- }
1773
- return PromptbookLogicError;
1774
- }(Error));
1775
1984
 
1776
1985
  /**
1777
1986
  * Tests if given string is valid URL.
@@ -1799,7 +2008,7 @@ function isValidUrl(url) {
1799
2008
  }
1800
2009
 
1801
2010
  /**
1802
- * Validates PromptbookJson if it is logically valid.
2011
+ * Validates PromptbookJson if it is logically valid
1803
2012
  *
1804
2013
  * It checks:
1805
2014
  * - if it has correct parameters dependency
@@ -1809,6 +2018,7 @@ function isValidUrl(url) {
1809
2018
  * - if it is meaningful
1810
2019
  *
1811
2020
  * @param promptbook valid or invalid PromptbookJson
2021
+ * @returns the same promptbook if it is logically valid
1812
2022
  * @throws {PromptbookLogicError} on logical error in the promptbook
1813
2023
  */
1814
2024
  function validatePromptbookJson(promptbook) {
@@ -1960,6 +2170,7 @@ function validatePromptbookJson(promptbook) {
1960
2170
  while (unresovedTemplates.length > 0) {
1961
2171
  _loop_2();
1962
2172
  }
2173
+ return promptbook;
1963
2174
  }
1964
2175
  /**
1965
2176
  * TODO: [🧠] Work with promptbookVersion