@promptbook/node 0.69.0-17 → 0.69.0-19

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 CHANGED
@@ -15,7 +15,7 @@ import * as dotenv from 'dotenv';
15
15
  /**
16
16
  * The version of the Promptbook library
17
17
  */
18
- var PROMPTBOOK_VERSION = '0.69.0-16';
18
+ var PROMPTBOOK_VERSION = '0.69.0-18';
19
19
  // TODO:[main] !!!! List here all the versions and annotate + put into script
20
20
 
21
21
  /*! *****************************************************************************
@@ -3748,15 +3748,15 @@ function executeFormatCells(options) {
3748
3748
  .join('\n')), "\n\n [\u26F7] This should never happen because format name should be validated during parsing\n\n ").concat(block(pipelineIdentification), "\n "); }));
3749
3749
  }
3750
3750
  subvalueDefinition = formatDefinition.subvalueDefinitions.find(function (subvalueDefinition) {
3751
- return __spreadArray([subvalueDefinition.subvalueName], __read((subvalueDefinition.aliases || [])), false).includes(template.foreach.cellName);
3751
+ return __spreadArray([subvalueDefinition.subvalueName], __read((subvalueDefinition.aliases || [])), false).includes(template.foreach.subformatName);
3752
3752
  });
3753
3753
  if (subvalueDefinition === undefined) {
3754
3754
  throw new UnexpectedError(
3755
3755
  // <- TODO: [🧠][🧐] Should be formats fixed per promptbook version or behave as plugins (=> change UnexpectedError)
3756
- spaceTrim$1(function (block) { return "\n Unsupported cell name \"".concat(template.foreach.cellName, "\" for format \"").concat(template.foreach.formatName, "\"\n\n Available cell names for format \"").concat(formatDefinition.formatName, "\":\n ").concat(block(formatDefinition.subvalueDefinitions
3756
+ spaceTrim$1(function (block) { return "\n Unsupported subformat name \"".concat(template.foreach.subformatName, "\" for format \"").concat(template.foreach.formatName, "\"\n\n Available subformat names for format \"").concat(formatDefinition.formatName, "\":\n ").concat(block(formatDefinition.subvalueDefinitions
3757
3757
  .map(function (subvalueDefinition) { return subvalueDefinition.subvalueName; })
3758
3758
  .map(function (subvalueName) { return "- ".concat(subvalueName); })
3759
- .join('\n')), "\n\n [\u26F7] This should never happen because cell name should be validated during parsing\n\n ").concat(block(pipelineIdentification), "\n "); }));
3759
+ .join('\n')), "\n\n [\u26F7] This should never happen because subformat name should be validated during parsing\n\n ").concat(block(pipelineIdentification), "\n "); }));
3760
3760
  }
3761
3761
  if (formatDefinition.formatName === 'CSV') {
3762
3762
  formatSettings = settings.csvSettings;
@@ -3801,8 +3801,6 @@ function executeFormatCells(options) {
3801
3801
  /**
3802
3802
  * TODO: !!!!!! Make pipelineIdentification more precise
3803
3803
  * TODO: !!!!!! How FOREACH execution looks in the report
3804
- * TODO: [🧠][🦥] Better (less confusing) name for "cell" / "subvalue" / "subparameter"
3805
- * TODO: []
3806
3804
  */
3807
3805
 
3808
3806
  /**
@@ -5665,6 +5663,29 @@ function normalizeTo_camelCase(text, _isFirstLetterCapital) {
5665
5663
  * TODO: [🌺] Use some intermediate util splitWords
5666
5664
  */
5667
5665
 
5666
+ /**
5667
+ * Removes quotes from a string
5668
+ *
5669
+ * Tip: This is very usefull for post-processing of the result of the LLM model
5670
+ * Note: This function removes only the same quotes from the beginning and the end of the string
5671
+ * Note: There are two simmilar functions:
5672
+ * - `removeQuotes` which removes only bounding quotes
5673
+ * - `unwrapResult` which removes whole introduce sentence
5674
+ *
5675
+ * @param text optionally quoted text
5676
+ * @returns text without quotes
5677
+ * @public exported from `@promptbook/utils`
5678
+ */
5679
+ function removeQuotes(text) {
5680
+ if (text.startsWith('"') && text.endsWith('"')) {
5681
+ return text.slice(1, -1);
5682
+ }
5683
+ if (text.startsWith('\'') && text.endsWith('\'')) {
5684
+ return text.slice(1, -1);
5685
+ }
5686
+ return text;
5687
+ }
5688
+
5668
5689
  /**
5669
5690
  * Function `validateParameterName` will @@@
5670
5691
  *
@@ -5723,6 +5744,9 @@ function validateParameterName(parameterName) {
5723
5744
  parameterName.includes(']')) {
5724
5745
  throw new ParseError("Parameter name cannot contain braces");
5725
5746
  }
5747
+ parameterName = removeDiacritics(parameterName);
5748
+ parameterName = removeEmojis(parameterName);
5749
+ parameterName = removeQuotes(parameterName);
5726
5750
  parameterName = normalizeTo_camelCase(parameterName);
5727
5751
  if (parameterName === '') {
5728
5752
  throw new ParseError("Parameter name cannot be empty");
@@ -5776,7 +5800,7 @@ var foreachCommandParser = {
5776
5800
  examples: [
5777
5801
  'FOREACH Text Line `{customers}` -> `{customer}`',
5778
5802
  'FOR Csv Row `{customers}` -> `{firstName}`, `{lastName}`',
5779
- 'EACH Csv Cell `{customers}` -> `{cell}`',
5803
+ 'EACH Csv Cell `{customers}` -> `{subformat}`',
5780
5804
  ],
5781
5805
  /**
5782
5806
  * Parses the FOREACH command
@@ -5784,8 +5808,8 @@ var foreachCommandParser = {
5784
5808
  parse: function (input) {
5785
5809
  var args = input.args;
5786
5810
  var formatName = normalizeTo_SCREAMING_CASE(args[0] || '');
5787
- var cellName = normalizeTo_SCREAMING_CASE(args[1] || '');
5788
- var parameterNameWrapped = args[2];
5811
+ var subformatName = normalizeTo_SCREAMING_CASE(args[1] || '');
5812
+ var parameterNameArg = args[2] || '';
5789
5813
  var assignSign = args[3];
5790
5814
  var formatDefinition = FORMAT_DEFINITIONS.find(function (formatDefinition) {
5791
5815
  return __spreadArray([formatDefinition.formatName], __read((formatDefinition.aliases || [])), false).includes(formatName);
@@ -5797,24 +5821,19 @@ var foreachCommandParser = {
5797
5821
  // <- TODO: [🏢] List all supported format names
5798
5822
  }
5799
5823
  var subvalueDefinition = formatDefinition.subvalueDefinitions.find(function (subvalueDefinition) {
5800
- return __spreadArray([subvalueDefinition.subvalueName], __read((subvalueDefinition.aliases || [])), false).includes(cellName);
5824
+ return __spreadArray([subvalueDefinition.subvalueName], __read((subvalueDefinition.aliases || [])), false).includes(subformatName);
5801
5825
  });
5802
5826
  if (subvalueDefinition === undefined) {
5803
- throw new ParseError(spaceTrim$1(function (block) { return "\n Unsupported cell name \"".concat(cellName, "\" for format \"").concat(formatName, "\"\n\n Available cell names for format \"").concat(formatDefinition.formatName, "\":\n ").concat(block(formatDefinition.subvalueDefinitions
5827
+ throw new ParseError(spaceTrim$1(function (block) { return "\n Unsupported subformat name \"".concat(subformatName, "\" for format \"").concat(formatName, "\"\n\n Available subformat names for format \"").concat(formatDefinition.formatName, "\":\n ").concat(block(formatDefinition.subvalueDefinitions
5804
5828
  .map(function (subvalueDefinition) { return subvalueDefinition.subvalueName; })
5805
5829
  .map(function (subvalueName) { return "- ".concat(subvalueName); })
5806
5830
  .join('\n')), "\n "); }));
5807
- // <- TODO: [🏢] List all supported cell names for the format
5831
+ // <- TODO: [🏢] List all supported subformat names for the format
5808
5832
  }
5809
5833
  if (assignSign !== '->') {
5810
5834
  throw new ParseError("FOREACH command must have '->' to assign the value to the parameter");
5811
5835
  }
5812
- // TODO: !!! Replace with propper parameter name validation `validateParameterName`
5813
- if ((parameterNameWrapped === null || parameterNameWrapped === void 0 ? void 0 : parameterNameWrapped.substring(0, 1)) !== '{' ||
5814
- (parameterNameWrapped === null || parameterNameWrapped === void 0 ? void 0 : parameterNameWrapped.substring(parameterNameWrapped.length - 1, parameterNameWrapped.length)) !== '}') {
5815
- throw new ParseError("Invalid parameter name \"".concat(parameterNameWrapped, "\" - must be wrapped in curly brackets: {parameterName}"));
5816
- }
5817
- var parameterName = parameterNameWrapped.substring(1, parameterNameWrapped.length - 1);
5836
+ var parameterName = validateParameterName(parameterNameArg);
5818
5837
  var subparameterNames = args
5819
5838
  .slice(4)
5820
5839
  .map(function (parameterName) { return parameterName.split(',').join(' ').trim(); })
@@ -5826,7 +5845,7 @@ var foreachCommandParser = {
5826
5845
  return {
5827
5846
  type: 'FOREACH',
5828
5847
  formatName: formatName,
5829
- cellName: cellName,
5848
+ subformatName: subformatName,
5830
5849
  parameterName: parameterName,
5831
5850
  subparameterNames: subparameterNames,
5832
5851
  };
@@ -5837,10 +5856,10 @@ var foreachCommandParser = {
5837
5856
  * Note: `$` is used to indicate that this function mutates given `templateJson`
5838
5857
  */
5839
5858
  $applyToTemplateJson: function (command, $templateJson, $pipelineJson) {
5840
- var formatName = command.formatName, cellName = command.cellName, parameterName = command.parameterName, subparameterNames = command.subparameterNames;
5841
- // TODO: !!!!!! Detect double use
5842
- // TODO: !!!!!! Detect usage with JOKER and don't allow it
5843
- $templateJson.foreach = { formatName: formatName, cellName: cellName, parameterName: parameterName, subparameterNames: subparameterNames };
5859
+ var formatName = command.formatName, subformatName = command.subformatName, parameterName = command.parameterName, subparameterNames = command.subparameterNames;
5860
+ // TODO: [🍭] Detect double use
5861
+ // TODO: [🍭] Detect usage with JOKER and don't allow it
5862
+ $templateJson.foreach = { formatName: formatName, subformatName: subformatName, parameterName: parameterName, subparameterNames: subparameterNames };
5844
5863
  keepUnused($pipelineJson); // <- TODO: [🧠] Maybe register subparameter from foreach into parameters of the pipeline
5845
5864
  // Note: [🍭] FOREACH apply has some sideeffects on different places in codebase
5846
5865
  },
@@ -5864,8 +5883,7 @@ var foreachCommandParser = {
5864
5883
  },
5865
5884
  };
5866
5885
  /**
5867
- * TODO: [🧠][🦥] Better (less confusing) name for "cell" / "subvalue" / "subparameter"
5868
- * TODO: [🍭] !!!!!! Make .ptbk.md file with examples of the FOREACH command and also with wrong parsing and logic
5886
+ * TODO: [🍭] Make .ptbk.md file with examples of the FOREACH with wrong parsing and logic
5869
5887
  */
5870
5888
 
5871
5889
  /**
@@ -5975,12 +5993,11 @@ var jokerCommandParser = {
5975
5993
  */
5976
5994
  parse: function (input) {
5977
5995
  var args = input.args;
5978
- // TODO: !!!!!! Replace with propper parameter name validation `validateParameterName`
5979
- var parametersMatch = (args.pop() || '').match(/^\{(?<parameterName>[a-z0-9_]+)\}$/im);
5980
- if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
5981
- throw new ParseError("Invalid joker");
5996
+ if (args.length !== 1) {
5997
+ throw new ParseError("JOKE command expects exactly one parameter name");
5982
5998
  }
5983
- var parameterName = parametersMatch.groups.parameterName;
5999
+ var parameterNameArg = args[0] || '';
6000
+ var parameterName = validateParameterName(parameterNameArg);
5984
6001
  return {
5985
6002
  type: 'JOKER',
5986
6003
  parameterName: parameterName,
@@ -6204,14 +6221,13 @@ var parameterCommandParser = {
6204
6221
  * Parses the PARAMETER command
6205
6222
  */
6206
6223
  parse: function (input) {
6207
- var normalized = input.normalized, raw = input.raw;
6208
- var parametersMatch = raw.match(/\{(?<parameterName>[a-z0-9_]+)\}[^\S\r\n]*(?<parameterDescription>.*)$/im);
6209
- if (!parametersMatch || !parametersMatch.groups || !parametersMatch.groups.parameterName) {
6210
- throw new ParseError("Invalid parameter");
6211
- }
6212
- var _a = parametersMatch.groups, parameterName = _a.parameterName, parameterDescription = _a.parameterDescription;
6213
- if (parameterDescription && parameterDescription.match(/\{(?<parameterName>[a-z0-9_]+)\}/im)) {
6214
- throw new ParseError("Parameter {".concat(parameterName, "} can not contain another parameter in description"));
6224
+ var normalized = input.normalized, args = input.args, raw = input.raw;
6225
+ var parameterNameRaw = args.shift() || '';
6226
+ var parameterDescriptionRaw = args.join(' ');
6227
+ // <- TODO: When [🥶] fixed, change to:
6228
+ // > const parameterDescriptionRaw = rawArgs.split(parameterNameRaw).join('').trim();
6229
+ if (parameterDescriptionRaw && parameterDescriptionRaw.match(/\{(?<embeddedParameterName>[a-z0-9_]+)\}/im)) {
6230
+ throw new ParseError(spaceTrim$1(function (block) { return "\n Parameter {".concat(parameterNameRaw, "} can not contain another parameter in description\n\n The description:\n ").concat(block(parameterDescriptionRaw), "\n "); }));
6215
6231
  }
6216
6232
  var isInput = normalized.startsWith('INPUT');
6217
6233
  var isOutput = normalized.startsWith('OUTPUT');
@@ -6219,11 +6235,12 @@ var parameterCommandParser = {
6219
6235
  isInput = false;
6220
6236
  isOutput = false;
6221
6237
  }
6222
- // TODO: !!!!!! Add parameter name validation
6238
+ var parameterName = validateParameterName(parameterNameRaw);
6239
+ var parameterDescription = parameterDescriptionRaw.trim() || null;
6223
6240
  return {
6224
6241
  type: 'PARAMETER',
6225
6242
  parameterName: parameterName,
6226
- parameterDescription: parameterDescription.trim() || null,
6243
+ parameterDescription: parameterDescription,
6227
6244
  isInput: isInput,
6228
6245
  isOutput: isOutput,
6229
6246
  };
@@ -7031,7 +7048,9 @@ function parseCommand(raw, usagePlace) {
7031
7048
  for (var commandNameSegmentsCount = 0; commandNameSegmentsCount < Math.min(items.length, 3); commandNameSegmentsCount++) {
7032
7049
  var commandNameRaw = items.slice(0, commandNameSegmentsCount + 1).join('_');
7033
7050
  var args = items.slice(commandNameSegmentsCount + 1);
7034
- var rawArgs = raw.substring(commandNameRaw.length).trim();
7051
+ var rawArgs = raw
7052
+ .substring(commandNameRaw.length)
7053
+ .trim();
7035
7054
  var command = parseCommandVariant({ usagePlace: usagePlace, raw: raw, rawArgs: rawArgs, normalized: normalized, args: args, commandNameRaw: commandNameRaw });
7036
7055
  if (command !== null) {
7037
7056
  return command;
@@ -7042,7 +7061,9 @@ function parseCommand(raw, usagePlace) {
7042
7061
  {
7043
7062
  var commandNameRaw = items.slice(-1).join('_');
7044
7063
  var args = items.slice(0, -1); // <- Note: This is probbably not correct
7045
- var rawArgs = raw.substring(0, raw.length - commandNameRaw.length).trim();
7064
+ var rawArgs = raw
7065
+ .substring(0, raw.length - commandNameRaw.length)
7066
+ .trim();
7046
7067
  var command = parseCommandVariant({ usagePlace: usagePlace, raw: raw, rawArgs: rawArgs, normalized: normalized, args: args, commandNameRaw: commandNameRaw });
7047
7068
  if (command !== null) {
7048
7069
  return command;