@promptbook/utils 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 +96 -11
  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 +1 -1
  13. package/umd/index.umd.js +96 -10
  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
@@ -1,8 +1,12 @@
1
1
  import type { PromptbookJson } from '../types/PromptbookJson/PromptbookJson';
2
2
  import type { PromptbookString } from '../types/PromptbookString';
3
+ /**
4
+ * Converts promptbook in JSON format to string format
5
+ *
6
+ * @param promptbookJson Promptbook in JSON format (.ptbk.json)
7
+ * @returns Promptbook in string format (.ptbk.md)
8
+ */
3
9
  export declare function promptbookJsonToString(promptbookJson: PromptbookJson): PromptbookString;
4
10
  /**
5
- * TODO: !!!!! Implement
6
- * TODO: !!!!! Annotate and warn
7
- * TODO: !!!!! Test + test together with promptbookStringToJson
11
+ * TODO: Escape all
8
12
  */
@@ -0,0 +1,25 @@
1
+ import type { PromptbookJson } from '../../types/PromptbookJson/PromptbookJson';
2
+ import type { string_name } from '../../types/typeAliases';
3
+ type RenameParameterOptions = {
4
+ /**
5
+ * Promptbook to search and replace for parameters
6
+ * This promptbook is returned as copy with replaced parameters
7
+ */
8
+ promptbook: PromptbookJson;
9
+ /**
10
+ * Original parameter name that should be replaced
11
+ */
12
+ oldParameterName: string_name;
13
+ /**
14
+ * New parameter name that should replace the original parameter name
15
+ */
16
+ newParameterName: string_name;
17
+ };
18
+ /**
19
+ * Function renameParameter will find all usable parameters for given prompt template
20
+ * In other words, it will find all parameters that are not used in the prompt template itseld and all its dependencies
21
+ *
22
+ * @throws {PromptbookLogicError} If the new parameter name is already used in the promptbook
23
+ */
24
+ export declare function renameParameter(options: RenameParameterOptions): PromptbookJson;
25
+ export {};
@@ -6,6 +6,7 @@ import { PromptbookString } from '../../types/PromptbookString';
6
6
  * Note: Using here custom import to work in jest tests
7
7
  * Note: Using sync version is 💩 in the production code, but it's ok here in tests
8
8
  *
9
+ * @param path - The path to the file relative to samples/templates directory
9
10
  * @private
10
11
  */
11
12
  export declare function importPromptbook(path: `${string}.ptbk.md`): PromptbookString;
@@ -1,6 +1,6 @@
1
1
  import type { PromptbookJson } from '../../types/PromptbookJson/PromptbookJson';
2
2
  /**
3
- * Validates PromptbookJson if it is logically valid.
3
+ * Validates PromptbookJson if it is logically valid
4
4
  *
5
5
  * It checks:
6
6
  * - if it has correct parameters dependency
@@ -10,9 +10,10 @@ import type { PromptbookJson } from '../../types/PromptbookJson/PromptbookJson';
10
10
  * - if it is meaningful
11
11
  *
12
12
  * @param promptbook valid or invalid PromptbookJson
13
+ * @returns the same promptbook if it is logically valid
13
14
  * @throws {PromptbookLogicError} on logical error in the promptbook
14
15
  */
15
- export declare function validatePromptbookJson(promptbook: PromptbookJson): void;
16
+ export declare function validatePromptbookJson(promptbook: PromptbookJson): PromptbookJson;
16
17
  /**
17
18
  * TODO: [🧠] Work with promptbookVersion
18
19
  * TODO: Use here some json-schema, Zod or something similar and change it to:
@@ -6,7 +6,7 @@ import type { number_integer, number_positive_or_zero, string_javascript, string
6
6
  /**
7
7
  * Describes one prompt template in the promptbook
8
8
  */
9
- export type PromptTemplateJson = LlmTemplateJson | SimpleTemplateJson | ScriptTemplateJson | PromptDialogJson;
9
+ export type PromptTemplateJson = LlmTemplateJson | SimpleTemplateJson | ScriptJson | PromptDialogJson;
10
10
  /**
11
11
  * Template for prompt to LLM
12
12
  */
@@ -50,7 +50,7 @@ interface SimpleTemplateJson extends PromptTemplateJsonCommon {
50
50
  /**
51
51
  * Template for script execution
52
52
  */
53
- interface ScriptTemplateJson extends PromptTemplateJsonCommon {
53
+ interface ScriptJson extends PromptTemplateJsonCommon {
54
54
  readonly executionType: 'SCRIPT';
55
55
  /**
56
56
  * Language of the script
@@ -2,7 +2,7 @@ import type { string_html } from '../../types/typeAliases';
2
2
  /**
3
3
  * Prettify the html code
4
4
  *
5
- * @param html raw html code
5
+ * @param content raw html code
6
6
  * @returns formatted html code
7
7
  */
8
- export declare function prettifyMarkdown(html: string_html): string_html;
8
+ export declare function prettifyMarkdown<TContent extends string_html>(content: TContent): TContent;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promptbook/utils",
3
- "version": "0.48.0",
3
+ "version": "0.49.0",
4
4
  "description": "Library to supercharge your use of large language models",
5
5
  "private": false,
6
6
  "sideEffects": false,
package/umd/index.umd.js CHANGED
@@ -408,7 +408,7 @@
408
408
  /**
409
409
  * The version of the Promptbook library
410
410
  */
411
- var PROMPTBOOK_VERSION = '0.48.0-1';
411
+ var PROMPTBOOK_VERSION = '0.48.1-0';
412
412
 
413
413
  /**
414
414
  * Parses the given script and returns the list of all used variables that are not defined in the script
@@ -1135,12 +1135,12 @@
1135
1135
  /**
1136
1136
  * Prettify the html code
1137
1137
  *
1138
- * @param html raw html code
1138
+ * @param content raw html code
1139
1139
  * @returns formatted html code
1140
1140
  */
1141
- function prettifyMarkdown(html) {
1141
+ function prettifyMarkdown(content) {
1142
1142
  try {
1143
- return prettier.format(html, {
1143
+ return prettier.format(content, {
1144
1144
  parser: 'markdown',
1145
1145
  plugins: [parserHtml__default["default"]],
1146
1146
  // TODO: DRY - make some import or auto-copy of .prettierrc
@@ -1158,9 +1158,9 @@
1158
1158
  catch (error) {
1159
1159
  console.error('There was an error with prettifying the markdown, using the original as the fallback', {
1160
1160
  error: error,
1161
- html: html,
1161
+ html: content,
1162
1162
  });
1163
- return html;
1163
+ return content;
1164
1164
  }
1165
1165
  }
1166
1166
 
@@ -1169,7 +1169,8 @@
1169
1169
  *
1170
1170
  * Note: The result is not wrapped in a Markdown code block
1171
1171
  */
1172
- function renderPromptbookMermaid(promptbookJson) {
1172
+ function renderPromptbookMermaid(promptbookJson, options) {
1173
+ var _a = (options || {}).linkPromptTemplate, linkPromptTemplate = _a === void 0 ? function () { return null; } : _a;
1173
1174
  var parameterNameToTemplateName = function (parameterName) {
1174
1175
  var parameter = promptbookJson.parameters.find(function (parameter) { return parameter.name === parameterName; });
1175
1176
  if (!parameter) {
@@ -1202,8 +1203,18 @@
1202
1203
  var name = _a.name;
1203
1204
  return "".concat(parameterNameToTemplateName(name), "--\"{").concat(name, "}\"-->output");
1204
1205
  })
1205
- .join('\n')), "\n output((Output)):::output\n\n classDef input color: grey;\n classDef output color: grey;\n\n end;\n\n "); });
1206
- // TODO: !!!!! Allow to put link callback into `renderPromptbookMermaid`
1206
+ .join('\n')), "\n output((Output)):::output\n\n ").concat(block(promptbookJson.promptTemplates
1207
+ .map(function (promptTemplate) {
1208
+ var link = linkPromptTemplate(promptTemplate);
1209
+ if (link === null) {
1210
+ return '';
1211
+ }
1212
+ var href = link.href, title = link.title;
1213
+ var templateName = parameterNameToTemplateName(promptTemplate.resultingParameterName);
1214
+ return "click ".concat(templateName, " href \"").concat(href, "\" \"").concat(title, "\";");
1215
+ })
1216
+ .filter(function (line) { return line !== ''; })
1217
+ .join('\n')), "\n\n classDef input color: grey;\n classDef output color: grey;\n\n end;\n\n "); });
1207
1218
  return promptbookMermaid;
1208
1219
  }
1209
1220
  /**
@@ -1218,7 +1229,11 @@
1218
1229
  var isGraphAdded = options.isGraphAdded, isPrettifyed = options.isPrettifyed;
1219
1230
  if (isGraphAdded) {
1220
1231
  var promptbookJson = promptbookStringToJson(promptbookString);
1221
- var promptbookMermaid_1 = renderPromptbookMermaid(promptbookJson);
1232
+ var promptbookMermaid_1 = renderPromptbookMermaid(promptbookJson, {
1233
+ linkPromptTemplate: function (promptTemplate) {
1234
+ return { href: "#".concat(promptTemplate.name), title: promptTemplate.title };
1235
+ },
1236
+ });
1222
1237
  var promptbookMermaidBlock = spacetrim.spaceTrim(function (block) { return "\n ```mermaid\n ".concat(block(promptbookMermaid_1), "\n ```\n "); });
1223
1238
  promptbookString = addAutoGeneratedSection(promptbookString, {
1224
1239
  sectionName: 'Graph',
@@ -1235,6 +1250,76 @@
1235
1250
  * TODO: [🕌] When more than 2 functionalities, split into separate functions
1236
1251
  */
1237
1252
 
1253
+ /**
1254
+ * This error indicates that the promptbook object has valid syntax but contains logical errors (like circular dependencies)
1255
+ */
1256
+ var PromptbookLogicError = /** @class */ (function (_super) {
1257
+ __extends(PromptbookLogicError, _super);
1258
+ function PromptbookLogicError(message) {
1259
+ var _this = _super.call(this, message) || this;
1260
+ _this.name = 'PromptbookLogicError';
1261
+ Object.setPrototypeOf(_this, PromptbookLogicError.prototype);
1262
+ return _this;
1263
+ }
1264
+ return PromptbookLogicError;
1265
+ }(Error));
1266
+
1267
+ /**
1268
+ * Function renameParameter will find all usable parameters for given prompt template
1269
+ * In other words, it will find all parameters that are not used in the prompt template itseld and all its dependencies
1270
+ *
1271
+ * @throws {PromptbookLogicError} If the new parameter name is already used in the promptbook
1272
+ */
1273
+ function renameParameter(options) {
1274
+ var e_1, _a, e_2, _b;
1275
+ var promptbook = options.promptbook, oldParameterName = options.oldParameterName, newParameterName = options.newParameterName;
1276
+ if (promptbook.parameters.some(function (parameter) { return parameter.name === newParameterName; })) {
1277
+ throw new PromptbookLogicError("Can not replace {".concat(oldParameterName, "} to {").concat(newParameterName, "} because {").concat(newParameterName, "} is already used in the promptbook"));
1278
+ }
1279
+ var renamedPromptbook = __assign(__assign({}, promptbook), { parameters: __spreadArray([], __read(promptbook.parameters), false), promptTemplates: __spreadArray([], __read(promptbook.promptTemplates), false) });
1280
+ try {
1281
+ for (var _c = __values(renamedPromptbook.parameters), _d = _c.next(); !_d.done; _d = _c.next()) {
1282
+ var parameter = _d.value;
1283
+ if (parameter.name !== oldParameterName) {
1284
+ continue;
1285
+ }
1286
+ parameter.name = newParameterName;
1287
+ }
1288
+ }
1289
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1290
+ finally {
1291
+ try {
1292
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
1293
+ }
1294
+ finally { if (e_1) throw e_1.error; }
1295
+ }
1296
+ try {
1297
+ for (var _e = __values(renamedPromptbook.promptTemplates), _f = _e.next(); !_f.done; _f = _e.next()) {
1298
+ var promptTemplate = _f.value;
1299
+ if (promptTemplate.resultingParameterName === oldParameterName) {
1300
+ promptTemplate.resultingParameterName = newParameterName;
1301
+ }
1302
+ promptTemplate.dependentParameterNames = promptTemplate.dependentParameterNames.map(function (dependentParameterName) {
1303
+ return dependentParameterName === oldParameterName ? newParameterName : dependentParameterName;
1304
+ });
1305
+ promptTemplate.content = promptTemplate.content.replace(new RegExp("{".concat(oldParameterName, "}"), 'g'), "{".concat(newParameterName, "}"));
1306
+ promptTemplate.title = promptTemplate.title.replace(new RegExp("{".concat(oldParameterName, "}"), 'g'), "{".concat(newParameterName, "}"));
1307
+ promptTemplate.description =
1308
+ promptTemplate.description === undefined
1309
+ ? undefined
1310
+ : promptTemplate.description.replace(new RegExp("{".concat(oldParameterName, "}"), 'g'), "{".concat(newParameterName, "}"));
1311
+ }
1312
+ }
1313
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
1314
+ finally {
1315
+ try {
1316
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
1317
+ }
1318
+ finally { if (e_2) throw e_2.error; }
1319
+ }
1320
+ return renamedPromptbook;
1321
+ }
1322
+
1238
1323
  /**
1239
1324
  * This error indicates errors during the execution of the promptbook
1240
1325
  */
@@ -2590,6 +2675,7 @@
2590
2675
  exports.removeEmojis = removeEmojis;
2591
2676
  exports.removeMarkdownFormatting = removeMarkdownFormatting;
2592
2677
  exports.removeQuotes = removeQuotes;
2678
+ exports.renameParameter = renameParameter;
2593
2679
  exports.renderPromptbookMermaid = renderPromptbookMermaid;
2594
2680
  exports.replaceParameters = replaceParameters;
2595
2681
  exports.searchKeywords = searchKeywords;