@promptbook/utils 0.80.0 → 0.81.0-7

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 (40) hide show
  1. package/README.md +11 -5
  2. package/esm/index.es.js +151 -50
  3. package/esm/index.es.js.map +1 -1
  4. package/esm/typings/books/index.d.ts +15 -0
  5. package/esm/typings/src/_packages/core.index.d.ts +2 -6
  6. package/esm/typings/src/_packages/editable.index.d.ts +10 -0
  7. package/esm/typings/src/_packages/templates.index.d.ts +4 -0
  8. package/esm/typings/src/_packages/types.index.d.ts +4 -0
  9. package/esm/typings/src/_packages/utils.index.d.ts +10 -2
  10. package/esm/typings/src/config.d.ts +26 -0
  11. package/esm/typings/src/execution/ExecutionTools.d.ts +7 -0
  12. package/esm/typings/src/execution/PromptbookFetch.d.ts +5 -0
  13. package/esm/typings/src/execution/PromptbookFetch.test-type.d.ts +5 -0
  14. package/esm/typings/src/expectations/drafts/isDomainNameFree.d.ts +2 -1
  15. package/esm/typings/src/expectations/drafts/isGithubNameFree.d.ts +2 -1
  16. package/esm/typings/src/high-level-abstractions/index.d.ts +10 -0
  17. package/esm/typings/src/other/templates/getBookTemplate.d.ts +12 -0
  18. package/esm/typings/src/other/templates/getTemplatesPipelineCollection.d.ts +10 -0
  19. package/esm/typings/src/pipeline/PipelineJson/PipelineJson.d.ts +10 -0
  20. package/esm/typings/src/scrapers/_common/utils/makeKnowledgeSourceHandler.d.ts +1 -1
  21. package/esm/typings/src/scrapers/_common/utils/scraperFetch.d.ts +7 -0
  22. package/esm/typings/src/utils/editable/types/PipelineEditableSerialized.d.ts +27 -0
  23. package/esm/typings/src/{conversion → utils/editable}/utils/removePipelineCommand.d.ts +3 -3
  24. package/esm/typings/src/{conversion → utils/editable}/utils/renamePipelineParameter.d.ts +3 -3
  25. package/esm/typings/src/{conversion → utils/editable}/utils/stringifyPipelineJson.d.ts +2 -2
  26. package/esm/typings/src/utils/parameters/numberToString.d.ts +7 -0
  27. package/esm/typings/src/utils/parameters/{replaceParameters.d.ts → templateParameters.d.ts} +6 -2
  28. package/esm/typings/src/utils/parameters/valueToString.d.ts +17 -0
  29. package/esm/typings/src/utils/parameters/valueToString.test.d.ts +1 -0
  30. package/esm/typings/src/utils/serialization/asSerializable.d.ts +4 -0
  31. package/esm/typings/src/version.d.ts +7 -0
  32. package/package.json +1 -1
  33. package/umd/index.umd.js +155 -50
  34. package/umd/index.umd.js.map +1 -1
  35. package/esm/typings/src/utils/formatNumber.d.ts +0 -6
  36. /package/esm/typings/src/{conversion → utils/editable}/utils/removePipelineCommand.test.d.ts +0 -0
  37. /package/esm/typings/src/{conversion → utils/editable}/utils/renamePipelineParameter.test.d.ts +0 -0
  38. /package/esm/typings/src/{conversion → utils/editable}/utils/stringifyPipelineJson.test.d.ts +0 -0
  39. /package/esm/typings/src/utils/{formatNumber.test.d.ts → parameters/numberToString.test.d.ts} +0 -0
  40. /package/esm/typings/src/utils/parameters/{replaceParameters.test.d.ts → templateParameters.test.d.ts} +0 -0
package/README.md CHANGED
@@ -23,6 +23,10 @@
23
23
 
24
24
 
25
25
 
26
+ <blockquote style="color: #ff8811">
27
+ <b>⚠ Warning:</b> This is a pre-release version of the library. It is not yet ready for production use. Please look at <a href="https://www.npmjs.com/package/@promptbook/core?activeTab=versions">latest stable release</a>.
28
+ </blockquote>
29
+
26
30
  ## 📦 Package `@promptbook/utils`
27
31
 
28
32
  - Promptbooks are [divided into several](#-packages) packages, all are published from [single monorepo](https://github.com/webgptorg/promptbook).
@@ -70,20 +74,20 @@ unwrapResult('Best greeting for the user is "Hi Pavol!"'); // 'Hi Pavol!'
70
74
 
71
75
  ## Templating
72
76
 
73
- There is a function `replaceParameters` which is used to replace the parameters in given template optimized to LLM prompt templates.
77
+ There is a function `templateParameters` which is used to replace the parameters in given template optimized to LLM prompt templates.
74
78
 
75
79
  ```typescript
76
- import { replaceParameters } from '@promptbook/utils';
80
+ import { templateParameters } from '@promptbook/utils';
77
81
 
78
- replaceParameters('Hello, {name}!', { name: 'world' }); // 'Hello, world!'
82
+ templateParameters('Hello, {name}!', { name: 'world' }); // 'Hello, world!'
79
83
  ```
80
84
 
81
85
  And also multiline templates with blockquotes
82
86
 
83
87
  ```typescript
84
- import { replaceParameters, spaceTrim } from '@promptbook/utils';
88
+ import { templateParameters, spaceTrim } from '@promptbook/utils';
85
89
 
86
- replaceParameters(
90
+ templateParameters(
87
91
  spaceTrim(`
88
92
  Hello, {name}!
89
93
 
@@ -368,6 +372,8 @@ Or you can install them separately:
368
372
  - **[@promptbook/documents](https://www.npmjs.com/package/@promptbook/documents)** - Read knowledge from documents like `.docx`, `.odt`,…
369
373
  - **[@promptbook/legacy-documents](https://www.npmjs.com/package/@promptbook/legacy-documents)** - Read knowledge from legacy documents like `.doc`, `.rtf`,…
370
374
  - **[@promptbook/website-crawler](https://www.npmjs.com/package/@promptbook/website-crawler)** - Crawl knowledge from the web
375
+ - **[@promptbook/editable](https://www.npmjs.com/package/@promptbook/editable)** - Editable book as native javascript object with imperative object API
376
+ - **[@promptbook/templates](https://www.npmjs.com/package/@promptbook/templates)** - Usefull templates and examples of books which can be used as a starting point
371
377
  - **[@promptbook/types](https://www.npmjs.com/package/@promptbook/types)** - Just typescript types used in the library
372
378
  - **[@promptbook/cli](https://www.npmjs.com/package/@promptbook/cli)** - Command line interface utilities for promptbooks
373
379
 
package/esm/index.es.js CHANGED
@@ -5,20 +5,86 @@ import { basename } from 'path';
5
5
  /**
6
6
  * The version of the Book language
7
7
  *
8
+ * @generated
8
9
  * @see https://github.com/webgptorg/book
9
10
  */
10
11
  var BOOK_LANGUAGE_VERSION = '1.0.0';
11
12
  /**
12
13
  * The version of the Promptbook engine
13
14
  *
15
+ * @generated
14
16
  * @see https://github.com/webgptorg/promptbook
15
17
  */
16
- var PROMPTBOOK_ENGINE_VERSION = '0.80.0-1';
18
+ var PROMPTBOOK_ENGINE_VERSION = '0.81.0-6';
17
19
  /**
18
20
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
19
21
  * Note: [💞] Ignore a discrepancy between file name and entity name
20
22
  */
21
23
 
24
+ /**
25
+ * Name for the Promptbook
26
+ *
27
+ * TODO: [🗽] Unite branding and make single place for it
28
+ *
29
+ * @public exported from `@promptbook/core`
30
+ */
31
+ var NAME = "Promptbook";
32
+ /**
33
+ * Email of the responsible person
34
+ *
35
+ * @public exported from `@promptbook/core`
36
+ */
37
+ var ADMIN_EMAIL = 'me@pavolhejny.com';
38
+ /**
39
+ * Name of the responsible person for the Promptbook on GitHub
40
+ *
41
+ * @public exported from `@promptbook/core`
42
+ */
43
+ var ADMIN_GITHUB_NAME = 'hejny';
44
+ // <- TODO: [🧠] Better system for generator warnings - not always "code" and "by `@promptbook/cli`"
45
+ /**
46
+ * The maximum number of iterations for a loops
47
+ *
48
+ * @private within the repository - too low-level in comparison with other `MAX_...`
49
+ */
50
+ var LOOP_LIMIT = 1000;
51
+ /**
52
+ * Strings to represent various values in the context of parameter values
53
+ *
54
+ * @public exported from `@promptbook/utils`
55
+ */
56
+ var VALUE_STRINGS = {
57
+ empty: '(nothing; empty string)',
58
+ null: '(no value; null)',
59
+ undefined: '(unknown value; undefined)',
60
+ nan: '(not a number; NaN)',
61
+ infinity: '(infinity; ∞)',
62
+ negativeInfinity: '(negative infinity; -∞)',
63
+ unserializable: '(unserializable value)',
64
+ };
65
+ /**
66
+ * Small number limit
67
+ *
68
+ * @public exported from `@promptbook/utils`
69
+ */
70
+ var SMALL_NUMBER = 0.001;
71
+ // <- TODO: [🧜‍♂️]
72
+ /**
73
+ * @@@
74
+ *
75
+ * @public exported from `@promptbook/core`
76
+ */
77
+ Object.freeze({
78
+ delimiter: ',',
79
+ quoteChar: '"',
80
+ newline: '\n',
81
+ skipEmptyLines: true,
82
+ });
83
+ /**
84
+ * Note: [💞] Ignore a discrepancy between file name and entity name
85
+ * TODO: [🧠][🧜‍♂️] Maybe join remoteUrl and path into single value
86
+ */
87
+
22
88
  /*! *****************************************************************************
23
89
  Copyright (c) Microsoft Corporation.
24
90
 
@@ -138,50 +204,6 @@ function __spreadArray(to, from, pack) {
138
204
  return to.concat(ar || Array.prototype.slice.call(from));
139
205
  }
140
206
 
141
- /**
142
- * Name for the Promptbook
143
- *
144
- * TODO: [🗽] Unite branding and make single place for it
145
- *
146
- * @public exported from `@promptbook/core`
147
- */
148
- var NAME = "Promptbook";
149
- /**
150
- * Email of the responsible person
151
- *
152
- * @public exported from `@promptbook/core`
153
- */
154
- var ADMIN_EMAIL = 'me@pavolhejny.com';
155
- /**
156
- * Name of the responsible person for the Promptbook on GitHub
157
- *
158
- * @public exported from `@promptbook/core`
159
- */
160
- var ADMIN_GITHUB_NAME = 'hejny';
161
- // <- TODO: [🧠] Better system for generator warnings - not always "code" and "by `@promptbook/cli`"
162
- /**
163
- * The maximum number of iterations for a loops
164
- *
165
- * @private within the repository - too low-level in comparison with other `MAX_...`
166
- */
167
- var LOOP_LIMIT = 1000;
168
- // <- TODO: [🧜‍♂️]
169
- /**
170
- * @@@
171
- *
172
- * @public exported from `@promptbook/core`
173
- */
174
- Object.freeze({
175
- delimiter: ',',
176
- quoteChar: '"',
177
- newline: '\n',
178
- skipEmptyLines: true,
179
- });
180
- /**
181
- * Note: [💞] Ignore a discrepancy between file name and entity name
182
- * TODO: [🧠][🧜‍♂️] Maybe join remoteUrl and path into single value
183
- */
184
-
185
207
  /**
186
208
  * Make error report URL for the given error
187
209
  *
@@ -1703,6 +1725,34 @@ function extractParameterNames(template) {
1703
1725
  return parameterNames;
1704
1726
  }
1705
1727
 
1728
+ /**
1729
+ * Format either small or big number
1730
+ *
1731
+ * @public exported from `@promptbook/utils`
1732
+ */
1733
+ function numberToString(value) {
1734
+ if (value === 0) {
1735
+ return '0';
1736
+ }
1737
+ else if (Number.isNaN(value)) {
1738
+ return VALUE_STRINGS.nan;
1739
+ }
1740
+ else if (value === Infinity) {
1741
+ return VALUE_STRINGS.infinity;
1742
+ }
1743
+ else if (value === -Infinity) {
1744
+ return VALUE_STRINGS.negativeInfinity;
1745
+ }
1746
+ for (var exponent = 0; exponent < 15; exponent++) {
1747
+ var factor = Math.pow(10, exponent);
1748
+ var valueRounded = Math.round(value * factor) / factor;
1749
+ if (Math.abs(value - valueRounded) / value < SMALL_NUMBER) {
1750
+ return valueRounded.toFixed(exponent);
1751
+ }
1752
+ }
1753
+ return value.toString();
1754
+ }
1755
+
1706
1756
  /**
1707
1757
  * Freezes the given object and all its nested objects recursively
1708
1758
  *
@@ -1965,16 +2015,66 @@ exportJson({
1965
2015
  * Note: [💞] Ignore a discrepancy between file name and entity name
1966
2016
  */
1967
2017
 
2018
+ /**
2019
+ * Function `valueToString` will convert the given value to string
2020
+ * This is useful and used in the `templateParameters` function
2021
+ *
2022
+ * Note: This function is not just calling `toString` method
2023
+ * It's more complex and can handle this conversion specifically for LLM models
2024
+ * See `VALUE_STRINGS`
2025
+ *
2026
+ * Note: There are 2 similar functions
2027
+ * - `valueToString` converts value to string for LLM models as human-readable string
2028
+ * - `asSerializable` converts value to string to preserve full information to be able to convert it back
2029
+ *
2030
+ * @public exported from `@promptbook/utils`
2031
+ */
2032
+ function valueToString(value) {
2033
+ try {
2034
+ if (value === '') {
2035
+ return VALUE_STRINGS.empty;
2036
+ }
2037
+ else if (value === null) {
2038
+ return VALUE_STRINGS.null;
2039
+ }
2040
+ else if (value === undefined) {
2041
+ return VALUE_STRINGS.undefined;
2042
+ }
2043
+ else if (typeof value === 'string') {
2044
+ return value;
2045
+ }
2046
+ else if (typeof value === 'number') {
2047
+ return numberToString(value);
2048
+ }
2049
+ else if (value instanceof Date) {
2050
+ return value.toISOString();
2051
+ }
2052
+ else {
2053
+ return JSON.stringify(value);
2054
+ }
2055
+ }
2056
+ catch (error) {
2057
+ if (!(error instanceof Error)) {
2058
+ throw error;
2059
+ }
2060
+ console.error(error);
2061
+ return VALUE_STRINGS.unserializable;
2062
+ }
2063
+ }
2064
+
1968
2065
  /**
1969
2066
  * Replaces parameters in template with values from parameters object
1970
2067
  *
2068
+ * Note: This function is not places strings into string,
2069
+ * It's more complex and can handle this operation specifically for LLM models
2070
+ *
1971
2071
  * @param template the template with parameters in {curly} braces
1972
2072
  * @param parameters the object with parameters
1973
2073
  * @returns the template with replaced parameters
1974
2074
  * @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
1975
2075
  * @public exported from `@promptbook/utils`
1976
2076
  */
1977
- function replaceParameters(template, parameters) {
2077
+ function templateParameters(template, parameters) {
1978
2078
  var e_1, _a;
1979
2079
  try {
1980
2080
  for (var _b = __values(Object.entries(parameters)), _c = _b.next(); !_c.done; _c = _b.next()) {
@@ -2000,7 +2100,7 @@ function replaceParameters(template, parameters) {
2000
2100
  var loopLimit = LOOP_LIMIT;
2001
2101
  var _loop_1 = function () {
2002
2102
  if (loopLimit-- < 0) {
2003
- throw new LimitReachedError('Loop limit reached during parameters replacement in `replaceParameters`');
2103
+ throw new LimitReachedError('Loop limit reached during parameters replacement in `templateParameters`');
2004
2104
  }
2005
2105
  var precol = match.groups.precol;
2006
2106
  var parameterName = match.groups.parameterName;
@@ -2017,7 +2117,7 @@ function replaceParameters(template, parameters) {
2017
2117
  if (parameterValue === undefined) {
2018
2118
  throw new PipelineExecutionError("Parameter `{".concat(parameterName, "}` is not defined"));
2019
2119
  }
2020
- parameterValue = parameterValue.toString();
2120
+ parameterValue = valueToString(parameterValue);
2021
2121
  if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
2022
2122
  parameterValue = parameterValue
2023
2123
  .split('\n')
@@ -2156,7 +2256,7 @@ function removeQuotes(text) {
2156
2256
  */
2157
2257
  function clonePipeline(pipeline) {
2158
2258
  // Note: Not using spread operator (...) because @@@
2159
- var pipelineUrl = pipeline.pipelineUrl, sourceFile = pipeline.sourceFile, title = pipeline.title, bookVersion = pipeline.bookVersion, description = pipeline.description, formfactorName = pipeline.formfactorName, parameters = pipeline.parameters, tasks = pipeline.tasks, knowledgeSources = pipeline.knowledgeSources, knowledgePieces = pipeline.knowledgePieces, personas = pipeline.personas, preparations = pipeline.preparations;
2259
+ var pipelineUrl = pipeline.pipelineUrl, sourceFile = pipeline.sourceFile, title = pipeline.title, bookVersion = pipeline.bookVersion, description = pipeline.description, formfactorName = pipeline.formfactorName, parameters = pipeline.parameters, tasks = pipeline.tasks, knowledgeSources = pipeline.knowledgeSources, knowledgePieces = pipeline.knowledgePieces, personas = pipeline.personas, preparations = pipeline.preparations, sources = pipeline.sources;
2160
2260
  return {
2161
2261
  pipelineUrl: pipelineUrl,
2162
2262
  sourceFile: sourceFile,
@@ -2170,6 +2270,7 @@ function clonePipeline(pipeline) {
2170
2270
  knowledgePieces: knowledgePieces,
2171
2271
  personas: personas,
2172
2272
  preparations: preparations,
2273
+ sources: sources,
2173
2274
  };
2174
2275
  }
2175
2276
  /**
@@ -2594,5 +2695,5 @@ function isValidUuid(value) {
2594
2695
  return /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i.test(value);
2595
2696
  }
2596
2697
 
2597
- export { $deepFreeze, $getCurrentDate, $isRunningInBrowser, $isRunningInJest, $isRunningInNode, $isRunningInWebWorker, $randomSeed, BOOK_LANGUAGE_VERSION, CHARACTERS_PER_STANDARD_LINE, CountUtils, DIACRITIC_VARIANTS_LETTERS, LINES_PER_STANDARD_PAGE, PROMPTBOOK_ENGINE_VERSION, capitalize, checkSerializableAsJson, clonePipeline, countCharacters, countLines, countPages, countParagraphs, countSentences, countWords, decapitalize, deepClone, deserializeError, difference, exportJson, extractParameterNames, extractVariablesFromScript, forEachAsync, intersection, isHostnameOnPrivateNetwork, isSerializableAsJson, isUrlOnPrivateNetwork, isValidEmail, isValidFilePath, isValidJavascriptName, isValidJsonString, isValidKeyword, isValidPipelineUrl, isValidPromptbookVersion, isValidSemanticVersion, isValidUrl, isValidUuid, nameToUriPart, nameToUriParts, normalizeToKebabCase, normalizeTo_PascalCase, normalizeTo_SCREAMING_CASE, normalizeTo_camelCase, normalizeTo_snake_case, normalizeWhitespaces, orderJson, parseKeywords, parseKeywordsFromString, parseNumber, removeDiacritics, removeEmojis, removeQuotes, renderPromptbookMermaid, replaceParameters, searchKeywords, serializeError, spaceTrim, splitIntoSentences, titleToName, trimCodeBlock, trimEndOfCodeBlock, union, unwrapResult };
2698
+ export { $deepFreeze, $getCurrentDate, $isRunningInBrowser, $isRunningInJest, $isRunningInNode, $isRunningInWebWorker, $randomSeed, BOOK_LANGUAGE_VERSION, CHARACTERS_PER_STANDARD_LINE, CountUtils, DIACRITIC_VARIANTS_LETTERS, LINES_PER_STANDARD_PAGE, PROMPTBOOK_ENGINE_VERSION, SMALL_NUMBER, VALUE_STRINGS, capitalize, checkSerializableAsJson, clonePipeline, countCharacters, countLines, countPages, countParagraphs, countSentences, countWords, decapitalize, deepClone, deserializeError, difference, exportJson, extractParameterNames, extractVariablesFromScript, forEachAsync, intersection, isHostnameOnPrivateNetwork, isSerializableAsJson, isUrlOnPrivateNetwork, isValidEmail, isValidFilePath, isValidJavascriptName, isValidJsonString, isValidKeyword, isValidPipelineUrl, isValidPromptbookVersion, isValidSemanticVersion, isValidUrl, isValidUuid, nameToUriPart, nameToUriParts, normalizeToKebabCase, normalizeTo_PascalCase, normalizeTo_SCREAMING_CASE, normalizeTo_camelCase, normalizeTo_snake_case, normalizeWhitespaces, numberToString, orderJson, parseKeywords, parseKeywordsFromString, parseNumber, removeDiacritics, removeEmojis, removeQuotes, renderPromptbookMermaid, searchKeywords, serializeError, spaceTrim, splitIntoSentences, templateParameters, titleToName, trimCodeBlock, trimEndOfCodeBlock, union, unwrapResult, valueToString };
2598
2699
  //# sourceMappingURL=index.es.js.map