@promptbook/core 0.48.0 → 0.48.1-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/index.es.js +226 -20
- package/esm/index.es.js.map +1 -1
- package/esm/typings/_packages/utils.index.d.ts +2 -1
- package/esm/typings/conversion/prettify/renderPromptbookMermaid.d.ts +15 -2
- package/esm/typings/conversion/promptbookJsonToString.d.ts +1 -0
- package/esm/typings/conversion/utils/renameParameter.d.ts +25 -0
- package/esm/typings/conversion/validation/_importPromptbook.d.ts +1 -0
- package/esm/typings/conversion/validation/validatePromptbookJson.d.ts +3 -2
- package/esm/typings/utils/markdown/prettifyMarkdown.d.ts +2 -2
- package/package.json +2 -1
- package/umd/index.umd.js +265 -59
- package/umd/index.umd.js.map +1 -1
- package/umd/typings/_packages/utils.index.d.ts +2 -1
- package/umd/typings/conversion/prettify/renderPromptbookMermaid.d.ts +15 -2
- package/umd/typings/conversion/promptbookJsonToString.d.ts +1 -0
- package/umd/typings/conversion/promptbookStringToJson.test.d.ts +1 -0
- package/umd/typings/conversion/utils/renameParameter.d.ts +25 -0
- package/umd/typings/conversion/utils/renameParameter.test.d.ts +1 -0
- package/umd/typings/conversion/validation/_importPromptbook.d.ts +1 -0
- package/umd/typings/conversion/validation/validatePromptbookJson.d.ts +3 -2
- package/umd/typings/utils/markdown/prettifyMarkdown.d.ts +2 -2
- /package/esm/typings/conversion/{validation/promptbookStringToJson.test.d.ts → promptbookStringToJson.test.d.ts} +0 -0
- /package/{umd/typings/conversion/validation/promptbookStringToJson.test.d.ts → esm/typings/conversion/utils/renameParameter.test.d.ts} +0 -0
package/esm/index.es.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import 'prettier';
|
|
3
|
-
import 'prettier/parser-html';
|
|
1
|
+
import { spaceTrim } from 'spacetrim';
|
|
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
|
|
443
|
+
var PROMPTBOOK_VERSION = '0.48.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
|
*/
|
|
@@ -1751,28 +1797,186 @@ function promptbookStringToJson(promptbookString) {
|
|
|
1751
1797
|
*/
|
|
1752
1798
|
|
|
1753
1799
|
function promptbookJsonToString(promptbookJson) {
|
|
1754
|
-
|
|
1800
|
+
var e_1, _a, e_2, _b, e_3, _c, e_4, _d, e_5, _e;
|
|
1801
|
+
var title = promptbookJson.title, promptbookUrl = promptbookJson.promptbookUrl, promptbookVersion = promptbookJson.promptbookVersion, description = promptbookJson.description, parameters = promptbookJson.parameters, promptTemplates = promptbookJson.promptTemplates;
|
|
1802
|
+
var promptbookString = "# ".concat(title);
|
|
1803
|
+
if (description) {
|
|
1804
|
+
promptbookString += '\n\n';
|
|
1805
|
+
promptbookString += description;
|
|
1806
|
+
}
|
|
1807
|
+
// TODO:> const commands: Array<Command>
|
|
1808
|
+
var commands = [];
|
|
1809
|
+
if (promptbookUrl) {
|
|
1810
|
+
commands.push("PROMPTBOOK URL ".concat(promptbookUrl));
|
|
1811
|
+
}
|
|
1812
|
+
commands.push("PROMPTBOOK VERSION ".concat(promptbookVersion));
|
|
1813
|
+
promptbookString = prettifyMarkdown(promptbookString);
|
|
1814
|
+
try {
|
|
1815
|
+
for (var parameters_1 = __values(parameters), parameters_1_1 = parameters_1.next(); !parameters_1_1.done; parameters_1_1 = parameters_1.next()) {
|
|
1816
|
+
var parameter = parameters_1_1.value;
|
|
1817
|
+
var isInput = parameter.isInput, isOutput = parameter.isOutput;
|
|
1818
|
+
if (isInput) {
|
|
1819
|
+
commands.push("INPUT PARAMETER ".concat(promptTemplateParameterJsonToString(parameter)));
|
|
1820
|
+
}
|
|
1821
|
+
else if (isOutput) {
|
|
1822
|
+
commands.push("OUTPUT PARAMETER ".concat(promptTemplateParameterJsonToString(parameter)));
|
|
1823
|
+
}
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1827
|
+
finally {
|
|
1828
|
+
try {
|
|
1829
|
+
if (parameters_1_1 && !parameters_1_1.done && (_a = parameters_1.return)) _a.call(parameters_1);
|
|
1830
|
+
}
|
|
1831
|
+
finally { if (e_1) throw e_1.error; }
|
|
1832
|
+
}
|
|
1833
|
+
promptbookString += '\n\n';
|
|
1834
|
+
promptbookString += commands.map(function (command) { return "- ".concat(command); }).join('\n');
|
|
1835
|
+
try {
|
|
1836
|
+
for (var promptTemplates_1 = __values(promptTemplates), promptTemplates_1_1 = promptTemplates_1.next(); !promptTemplates_1_1.done; promptTemplates_1_1 = promptTemplates_1.next()) {
|
|
1837
|
+
var promptTemplate = promptTemplates_1_1.value;
|
|
1838
|
+
var
|
|
1839
|
+
/* Note: Not using:> name, */
|
|
1840
|
+
title_1 = promptTemplate.title, description_1 = promptTemplate.description,
|
|
1841
|
+
/* Note: dependentParameterNames, */
|
|
1842
|
+
jokers = promptTemplate.jokers, executionType = promptTemplate.executionType, content = promptTemplate.content, postprocessing = promptTemplate.postprocessing, expectations = promptTemplate.expectations, expectFormat = promptTemplate.expectFormat, resultingParameterName = promptTemplate.resultingParameterName;
|
|
1843
|
+
promptbookString += '\n\n';
|
|
1844
|
+
promptbookString += "## ".concat(title_1);
|
|
1845
|
+
if (description_1) {
|
|
1846
|
+
promptbookString += '\n\n';
|
|
1847
|
+
promptbookString += description_1;
|
|
1848
|
+
}
|
|
1849
|
+
// TODO:> const commands: Array<Command>
|
|
1850
|
+
var commands_1 = [];
|
|
1851
|
+
var contentLanguage = 'markdown';
|
|
1852
|
+
if (executionType === 'PROMPT_TEMPLATE') {
|
|
1853
|
+
var modelRequirements = promptTemplate.modelRequirements;
|
|
1854
|
+
var modelName = modelRequirements.modelName, modelVariant = modelRequirements.modelVariant;
|
|
1855
|
+
commands_1.push("EXECUTE PROMPT TEMPLATE");
|
|
1856
|
+
if (modelVariant) {
|
|
1857
|
+
commands_1.push("MODEL VARIANT ".concat(capitalize(modelVariant)));
|
|
1858
|
+
}
|
|
1859
|
+
if (modelName) {
|
|
1860
|
+
commands_1.push("MODEL NAME `".concat(modelName, "`"));
|
|
1861
|
+
}
|
|
1862
|
+
}
|
|
1863
|
+
else if (executionType === 'SIMPLE_TEMPLATE') {
|
|
1864
|
+
commands_1.push("SIMPLE TEMPLATE");
|
|
1865
|
+
// Note: Nothing special here
|
|
1866
|
+
}
|
|
1867
|
+
else if (executionType === 'SCRIPT') {
|
|
1868
|
+
commands_1.push("EXECUTE SCRIPT");
|
|
1869
|
+
if (promptTemplate.contentLanguage) {
|
|
1870
|
+
contentLanguage = promptTemplate.contentLanguage;
|
|
1871
|
+
}
|
|
1872
|
+
else {
|
|
1873
|
+
contentLanguage = '';
|
|
1874
|
+
}
|
|
1875
|
+
}
|
|
1876
|
+
else if (executionType === 'PROMPT_DIALOG') {
|
|
1877
|
+
commands_1.push("PROMPT DIALOG");
|
|
1878
|
+
// Note: Nothing special here
|
|
1879
|
+
}
|
|
1880
|
+
if (jokers) {
|
|
1881
|
+
try {
|
|
1882
|
+
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()) {
|
|
1883
|
+
var joker = jokers_1_1.value;
|
|
1884
|
+
commands_1.push("JOKER {".concat(joker, "}"));
|
|
1885
|
+
}
|
|
1886
|
+
}
|
|
1887
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
1888
|
+
finally {
|
|
1889
|
+
try {
|
|
1890
|
+
if (jokers_1_1 && !jokers_1_1.done && (_c = jokers_1.return)) _c.call(jokers_1);
|
|
1891
|
+
}
|
|
1892
|
+
finally { if (e_3) throw e_3.error; }
|
|
1893
|
+
}
|
|
1894
|
+
} /* not else */
|
|
1895
|
+
if (postprocessing) {
|
|
1896
|
+
try {
|
|
1897
|
+
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()) {
|
|
1898
|
+
var postprocessingFunctionName = postprocessing_1_1.value;
|
|
1899
|
+
commands_1.push("POSTPROCESSING `".concat(postprocessingFunctionName, "`"));
|
|
1900
|
+
}
|
|
1901
|
+
}
|
|
1902
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
1903
|
+
finally {
|
|
1904
|
+
try {
|
|
1905
|
+
if (postprocessing_1_1 && !postprocessing_1_1.done && (_d = postprocessing_1.return)) _d.call(postprocessing_1);
|
|
1906
|
+
}
|
|
1907
|
+
finally { if (e_4) throw e_4.error; }
|
|
1908
|
+
}
|
|
1909
|
+
} /* not else */
|
|
1910
|
+
if (expectations) {
|
|
1911
|
+
try {
|
|
1912
|
+
for (var _f = (e_5 = void 0, __values(Object.entries(expectations))), _g = _f.next(); !_g.done; _g = _f.next()) {
|
|
1913
|
+
var _h = __read(_g.value, 2), unit = _h[0], _j = _h[1], min = _j.min, max = _j.max;
|
|
1914
|
+
if (min === max) {
|
|
1915
|
+
commands_1.push("EXPECT EXACTLY ".concat(min, " ").concat(capitalize(unit + (min > 1 ? 's' : ''))));
|
|
1916
|
+
}
|
|
1917
|
+
else {
|
|
1918
|
+
if (min !== undefined) {
|
|
1919
|
+
commands_1.push("EXPECT MIN ".concat(min, " ").concat(capitalize(unit + (min > 1 ? 's' : ''))));
|
|
1920
|
+
} /* not else */
|
|
1921
|
+
if (max !== undefined) {
|
|
1922
|
+
commands_1.push("EXPECT MAX ".concat(max, " ").concat(capitalize(unit + (max > 1 ? 's' : ''))));
|
|
1923
|
+
}
|
|
1924
|
+
}
|
|
1925
|
+
}
|
|
1926
|
+
}
|
|
1927
|
+
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
1928
|
+
finally {
|
|
1929
|
+
try {
|
|
1930
|
+
if (_g && !_g.done && (_e = _f.return)) _e.call(_f);
|
|
1931
|
+
}
|
|
1932
|
+
finally { if (e_5) throw e_5.error; }
|
|
1933
|
+
}
|
|
1934
|
+
} /* not else */
|
|
1935
|
+
if (expectFormat) {
|
|
1936
|
+
if (expectFormat === 'JSON') {
|
|
1937
|
+
// TODO: @deprecated remove
|
|
1938
|
+
commands_1.push("EXPECT JSON");
|
|
1939
|
+
}
|
|
1940
|
+
} /* not else */
|
|
1941
|
+
promptbookString += '\n\n';
|
|
1942
|
+
promptbookString += commands_1.map(function (command) { return "- ".concat(command); }).join('\n');
|
|
1943
|
+
promptbookString += '\n\n';
|
|
1944
|
+
promptbookString += '```' + contentLanguage;
|
|
1945
|
+
promptbookString += '\n';
|
|
1946
|
+
promptbookString += content; // <- TODO: !!! Escape
|
|
1947
|
+
promptbookString += '\n';
|
|
1948
|
+
promptbookString += '```';
|
|
1949
|
+
promptbookString += '\n\n';
|
|
1950
|
+
promptbookString += "`-> {".concat(resultingParameterName, "}`"); // <- TODO: !!! If the parameter here has description, add it and use promptTemplateParameterJsonToString
|
|
1951
|
+
}
|
|
1952
|
+
}
|
|
1953
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
1954
|
+
finally {
|
|
1955
|
+
try {
|
|
1956
|
+
if (promptTemplates_1_1 && !promptTemplates_1_1.done && (_b = promptTemplates_1.return)) _b.call(promptTemplates_1);
|
|
1957
|
+
}
|
|
1958
|
+
finally { if (e_2) throw e_2.error; }
|
|
1959
|
+
}
|
|
1960
|
+
return promptbookString;
|
|
1961
|
+
}
|
|
1962
|
+
/**
|
|
1963
|
+
* @private internal util of promptbookJsonToString
|
|
1964
|
+
*/
|
|
1965
|
+
function promptTemplateParameterJsonToString(promptTemplateParameterJson) {
|
|
1966
|
+
var name = promptTemplateParameterJson.name, description = promptTemplateParameterJson.description;
|
|
1967
|
+
var parameterString = "{".concat(name, "}");
|
|
1968
|
+
if (description) {
|
|
1969
|
+
parameterString = "".concat(parameterString, " ").concat(description);
|
|
1970
|
+
}
|
|
1971
|
+
return parameterString;
|
|
1755
1972
|
}
|
|
1756
1973
|
/**
|
|
1757
1974
|
* TODO: !!!!! Implement
|
|
1758
1975
|
* TODO: !!!!! Annotate and warn
|
|
1759
1976
|
* TODO: !!!!! Test + test together with promptbookStringToJson
|
|
1977
|
+
* TODO: Escape all
|
|
1760
1978
|
*/
|
|
1761
1979
|
|
|
1762
|
-
/**
|
|
1763
|
-
* This error indicates that the promptbook object has valid syntax but contains logical errors (like circular dependencies)
|
|
1764
|
-
*/
|
|
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
|
-
|
|
1776
1980
|
/**
|
|
1777
1981
|
* Tests if given string is valid URL.
|
|
1778
1982
|
*
|
|
@@ -1799,7 +2003,7 @@ function isValidUrl(url) {
|
|
|
1799
2003
|
}
|
|
1800
2004
|
|
|
1801
2005
|
/**
|
|
1802
|
-
* Validates PromptbookJson if it is logically valid
|
|
2006
|
+
* Validates PromptbookJson if it is logically valid
|
|
1803
2007
|
*
|
|
1804
2008
|
* It checks:
|
|
1805
2009
|
* - if it has correct parameters dependency
|
|
@@ -1809,6 +2013,7 @@ function isValidUrl(url) {
|
|
|
1809
2013
|
* - if it is meaningful
|
|
1810
2014
|
*
|
|
1811
2015
|
* @param promptbook valid or invalid PromptbookJson
|
|
2016
|
+
* @returns the same promptbook if it is logically valid
|
|
1812
2017
|
* @throws {PromptbookLogicError} on logical error in the promptbook
|
|
1813
2018
|
*/
|
|
1814
2019
|
function validatePromptbookJson(promptbook) {
|
|
@@ -1960,6 +2165,7 @@ function validatePromptbookJson(promptbook) {
|
|
|
1960
2165
|
while (unresovedTemplates.length > 0) {
|
|
1961
2166
|
_loop_2();
|
|
1962
2167
|
}
|
|
2168
|
+
return promptbook;
|
|
1963
2169
|
}
|
|
1964
2170
|
/**
|
|
1965
2171
|
* TODO: [🧠] Work with promptbookVersion
|