@promptbook/markdown-utils 0.98.0-6 โ†’ 0.98.0-9

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
@@ -25,7 +25,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
25
25
  * @generated
26
26
  * @see https://github.com/webgptorg/promptbook
27
27
  */
28
- const PROMPTBOOK_ENGINE_VERSION = '0.98.0-6';
28
+ const PROMPTBOOK_ENGINE_VERSION = '0.98.0-9';
29
29
  /**
30
30
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
31
31
  * Note: [๐Ÿ’ž] Ignore a discrepancy between file name and entity name
@@ -304,7 +304,7 @@ const DEFAULT_MAX_PARALLEL_COUNT = 5; // <- TODO: [๐Ÿคนโ€โ™‚๏ธ]
304
304
  *
305
305
  * @public exported from `@promptbook/core`
306
306
  */
307
- const DEFAULT_MAX_EXECUTION_ATTEMPTS = 3; // <- TODO: [๐Ÿคนโ€โ™‚๏ธ]
307
+ const DEFAULT_MAX_EXECUTION_ATTEMPTS = 7; // <- TODO: [๐Ÿคนโ€โ™‚๏ธ]
308
308
  // <- TODO: [๐Ÿ•] Make also `BOOKS_DIRNAME_ALTERNATIVES`
309
309
  // TODO: Just `.promptbook` in config, hardcode subfolders like `download-cache` or `execution-cache`
310
310
  /**
@@ -2087,7 +2087,7 @@ function jsonParse(value) {
2087
2087
  throw new Error(spaceTrim((block) => `
2088
2088
  ${block(error.message)}
2089
2089
 
2090
- The JSON text:
2090
+ The expected JSON text:
2091
2091
  ${block(value)}
2092
2092
  `));
2093
2093
  }
@@ -4792,6 +4792,68 @@ function checkExpectations(expectations, value) {
4792
4792
  * Note: [๐Ÿ’] and [๐Ÿค ] are interconnected together
4793
4793
  */
4794
4794
 
4795
+ /**
4796
+ * Validates a prompt result against expectations and format requirements.
4797
+ * This function provides a common abstraction for result validation that can be used
4798
+ * by both execution logic and caching logic to ensure consistency.
4799
+ *
4800
+ * @param options - The validation options including result string, expectations, and format
4801
+ * @returns Validation result with processed string and validity status
4802
+ * @private internal function of `createPipelineExecutor` and `cacheLlmTools`
4803
+ */
4804
+ function validatePromptResult(options) {
4805
+ const { resultString, expectations, format } = options;
4806
+ let processedResultString = resultString;
4807
+ let validationError;
4808
+ try {
4809
+ // TODO: [๐Ÿ’] Unite object for expecting amount and format
4810
+ if (format) {
4811
+ if (format === 'JSON') {
4812
+ if (!isValidJsonString(processedResultString)) {
4813
+ // TODO: [๐Ÿข] Do more universally via `FormatParser`
4814
+ try {
4815
+ processedResultString = extractJsonBlock(processedResultString);
4816
+ }
4817
+ catch (error) {
4818
+ keepUnused(error);
4819
+ throw new ExpectError(spaceTrim$1((block) => `
4820
+ Expected valid JSON string
4821
+
4822
+ The expected JSON text:
4823
+ ${block(processedResultString)}
4824
+ `));
4825
+ }
4826
+ }
4827
+ }
4828
+ else {
4829
+ throw new UnexpectedError(`Unknown format "${format}"`);
4830
+ }
4831
+ }
4832
+ // TODO: [๐Ÿ’] Unite object for expecting amount and format
4833
+ if (expectations) {
4834
+ checkExpectations(expectations, processedResultString);
4835
+ }
4836
+ return {
4837
+ isValid: true,
4838
+ processedResultString,
4839
+ };
4840
+ }
4841
+ catch (error) {
4842
+ if (error instanceof ExpectError) {
4843
+ validationError = error;
4844
+ }
4845
+ else {
4846
+ // Re-throw non-ExpectError errors (like UnexpectedError)
4847
+ throw error;
4848
+ }
4849
+ return {
4850
+ isValid: false,
4851
+ processedResultString,
4852
+ error: validationError,
4853
+ };
4854
+ }
4855
+ }
4856
+
4795
4857
  /**
4796
4858
  * Executes a pipeline task with multiple attempts, including joker and retry logic. Handles different task types
4797
4859
  * (prompt, script, dialog, etc.), applies postprocessing, checks expectations, and updates the execution report.
@@ -4814,13 +4876,13 @@ async function executeAttempts(options) {
4814
4876
  // TODO: [๐Ÿš] Make arrayable LLMs -> single LLM DRY
4815
4877
  const _llms = arrayableToArray(tools.llm);
4816
4878
  const llmTools = _llms.length === 1 ? _llms[0] : joinLlmExecutionTools(..._llms);
4817
- attempts: for (let attempt = -jokerParameterNames.length; attempt < maxAttempts; attempt++) {
4818
- const isJokerAttempt = attempt < 0;
4819
- const jokerParameterName = jokerParameterNames[jokerParameterNames.length + attempt];
4879
+ attempts: for (let attemptIndex = -jokerParameterNames.length; attemptIndex < maxAttempts; attemptIndex++) {
4880
+ const isJokerAttempt = attemptIndex < 0;
4881
+ const jokerParameterName = jokerParameterNames[jokerParameterNames.length + attemptIndex];
4820
4882
  // TODO: [๐Ÿง ][๐Ÿญ] JOKERS, EXPECTATIONS, POSTPROCESSING and FOREACH
4821
4883
  if (isJokerAttempt && !jokerParameterName) {
4822
4884
  throw new UnexpectedError(spaceTrim$1((block) => `
4823
- Joker not found in attempt ${attempt}
4885
+ Joker not found in attempt ${attemptIndex}
4824
4886
 
4825
4887
  ${block(pipelineIdentification)}
4826
4888
  `));
@@ -5018,35 +5080,18 @@ async function executeAttempts(options) {
5018
5080
  }
5019
5081
  }
5020
5082
  // TODO: [๐Ÿ’] Unite object for expecting amount and format
5021
- if (task.format) {
5022
- if (task.format === 'JSON') {
5023
- if (!isValidJsonString($ongoingTaskResult.$resultString || '')) {
5024
- // TODO: [๐Ÿข] Do more universally via `FormatParser`
5025
- try {
5026
- $ongoingTaskResult.$resultString = extractJsonBlock($ongoingTaskResult.$resultString || '');
5027
- }
5028
- catch (error) {
5029
- keepUnused(error);
5030
- throw new ExpectError(spaceTrim$1((block) => `
5031
- Expected valid JSON string
5032
-
5033
- ${block(
5034
- /*<- Note: No need for `pipelineIdentification`, it will be catched and added later */ '')}
5035
- `));
5036
- }
5037
- }
5038
- }
5039
- else {
5040
- throw new UnexpectedError(spaceTrim$1((block) => `
5041
- Unknown format "${task.format}"
5042
-
5043
- ${block(pipelineIdentification)}
5044
- `));
5083
+ // Use the common validation function for both format and expectations
5084
+ if (task.format || task.expectations) {
5085
+ const validationResult = validatePromptResult({
5086
+ resultString: $ongoingTaskResult.$resultString || '',
5087
+ expectations: task.expectations,
5088
+ format: task.format,
5089
+ });
5090
+ if (!validationResult.isValid) {
5091
+ throw validationResult.error;
5045
5092
  }
5046
- }
5047
- // TODO: [๐Ÿ’] Unite object for expecting amount and format
5048
- if (task.expectations) {
5049
- checkExpectations(task.expectations, $ongoingTaskResult.$resultString || '');
5093
+ // Update the result string in case format processing modified it (e.g., JSON extraction)
5094
+ $ongoingTaskResult.$resultString = validationResult.processedResultString;
5050
5095
  }
5051
5096
  break attempts;
5052
5097
  }
@@ -5060,6 +5105,7 @@ async function executeAttempts(options) {
5060
5105
  $ongoingTaskResult.$failedResults = [];
5061
5106
  }
5062
5107
  $ongoingTaskResult.$failedResults.push({
5108
+ attemptIndex,
5063
5109
  result: $ongoingTaskResult.$resultString,
5064
5110
  error: error,
5065
5111
  });
@@ -5084,19 +5130,13 @@ async function executeAttempts(options) {
5084
5130
  });
5085
5131
  }
5086
5132
  }
5087
- if ($ongoingTaskResult.$expectError !== null && attempt === maxAttempts - 1) {
5088
- // Store the current failure before throwing
5089
- $ongoingTaskResult.$failedResults = $ongoingTaskResult.$failedResults || [];
5090
- $ongoingTaskResult.$failedResults.push({
5091
- result: $ongoingTaskResult.$resultString,
5092
- error: $ongoingTaskResult.$expectError,
5093
- });
5094
- // Create a summary of all failures
5133
+ if ($ongoingTaskResult.$expectError !== null && attemptIndex === maxAttempts - 1) {
5134
+ // Note: Create a summary of all failures
5095
5135
  const failuresSummary = $ongoingTaskResult.$failedResults
5096
- .map((failure, index) => spaceTrim$1((block) => {
5136
+ .map((failure) => spaceTrim$1((block) => {
5097
5137
  var _a, _b;
5098
5138
  return `
5099
- Attempt ${index + 1}:
5139
+ Attempt ${failure.attemptIndex + 1}:
5100
5140
  Error ${((_a = failure.error) === null || _a === void 0 ? void 0 : _a.name) || ''}:
5101
5141
  ${block((_b = failure.error) === null || _b === void 0 ? void 0 : _b.message.split('\n').map((line) => `> ${line}`).join('\n'))}
5102
5142