@promptbook/legacy-documents 0.95.0 โ†’ 0.98.0-10

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 (25) hide show
  1. package/README.md +12 -0
  2. package/esm/index.es.js +184 -123
  3. package/esm/index.es.js.map +1 -1
  4. package/esm/typings/src/_packages/anthropic-claude.index.d.ts +2 -2
  5. package/esm/typings/src/_packages/cli.index.d.ts +4 -0
  6. package/esm/typings/src/_packages/core.index.d.ts +2 -0
  7. package/esm/typings/src/_packages/openai.index.d.ts +10 -0
  8. package/esm/typings/src/_packages/types.index.d.ts +12 -2
  9. package/esm/typings/src/_packages/wizard.index.d.ts +4 -0
  10. package/esm/typings/src/config.d.ts +1 -1
  11. package/esm/typings/src/execution/createPipelineExecutor/$OngoingTaskResult.d.ts +8 -0
  12. package/esm/typings/src/execution/utils/validatePromptResult.d.ts +53 -0
  13. package/esm/typings/src/llm-providers/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +3 -3
  14. package/esm/typings/src/llm-providers/anthropic-claude/AnthropicClaudeExecutionToolsOptions.d.ts +2 -2
  15. package/esm/typings/src/llm-providers/openai/OpenAiAssistantExecutionToolsOptions.d.ts +2 -2
  16. package/esm/typings/src/llm-providers/openai/OpenAiCompatibleExecutionTools.d.ts +4 -4
  17. package/esm/typings/src/llm-providers/openai/OpenAiCompatibleExecutionToolsOptions.d.ts +52 -0
  18. package/esm/typings/src/llm-providers/openai/OpenAiExecutionToolsOptions.d.ts +3 -5
  19. package/esm/typings/src/llm-providers/openai/createOpenAiCompatibleExecutionTools.d.ts +74 -0
  20. package/esm/typings/src/llm-providers/openai/register-configuration.d.ts +11 -0
  21. package/esm/typings/src/llm-providers/openai/register-constructor.d.ts +14 -0
  22. package/esm/typings/src/version.d.ts +1 -1
  23. package/package.json +2 -2
  24. package/umd/index.umd.js +184 -123
  25. package/umd/index.umd.js.map +1 -1
package/umd/index.umd.js CHANGED
@@ -26,7 +26,7 @@
26
26
  * @generated
27
27
  * @see https://github.com/webgptorg/promptbook
28
28
  */
29
- const PROMPTBOOK_ENGINE_VERSION = '0.95.0';
29
+ const PROMPTBOOK_ENGINE_VERSION = '0.98.0-10';
30
30
  /**
31
31
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
32
32
  * Note: [๐Ÿ’ž] Ignore a discrepancy between file name and entity name
@@ -175,7 +175,7 @@
175
175
  *
176
176
  * @public exported from `@promptbook/core`
177
177
  */
178
- const DEFAULT_MAX_EXECUTION_ATTEMPTS = 10; // <- TODO: [๐Ÿคนโ€โ™‚๏ธ]
178
+ const DEFAULT_MAX_EXECUTION_ATTEMPTS = 7; // <- TODO: [๐Ÿคนโ€โ™‚๏ธ]
179
179
  // <- TODO: [๐Ÿ•] Make also `BOOKS_DIRNAME_ALTERNATIVES`
180
180
  // TODO: Just `.promptbook` in config, hardcode subfolders like `download-cache` or `execution-cache`
181
181
  /**
@@ -2560,7 +2560,7 @@
2560
2560
  throw new Error(spaceTrim__default["default"]((block) => `
2561
2561
  ${block(error.message)}
2562
2562
 
2563
- The JSON text:
2563
+ The expected JSON text:
2564
2564
  ${block(value)}
2565
2565
  `));
2566
2566
  }
@@ -4637,6 +4637,77 @@
4637
4637
  return mappedParameters;
4638
4638
  }
4639
4639
 
4640
+ /**
4641
+ * Replaces parameters in template with values from parameters object
4642
+ *
4643
+ * Note: This function is not places strings into string,
4644
+ * It's more complex and can handle this operation specifically for LLM models
4645
+ *
4646
+ * @param template the template with parameters in {curly} braces
4647
+ * @param parameters the object with parameters
4648
+ * @returns the template with replaced parameters
4649
+ * @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
4650
+ * @public exported from `@promptbook/utils`
4651
+ */
4652
+ function templateParameters(template, parameters) {
4653
+ for (const [parameterName, parameterValue] of Object.entries(parameters)) {
4654
+ if (parameterValue === RESERVED_PARAMETER_MISSING_VALUE) {
4655
+ throw new UnexpectedError(`Parameter \`{${parameterName}}\` has missing value`);
4656
+ }
4657
+ else if (parameterValue === RESERVED_PARAMETER_RESTRICTED) {
4658
+ // TODO: [๐Ÿต]
4659
+ throw new UnexpectedError(`Parameter \`{${parameterName}}\` is restricted to use`);
4660
+ }
4661
+ }
4662
+ let replacedTemplates = template;
4663
+ let match;
4664
+ let loopLimit = LOOP_LIMIT;
4665
+ while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
4666
+ .exec(replacedTemplates))) {
4667
+ if (loopLimit-- < 0) {
4668
+ throw new LimitReachedError('Loop limit reached during parameters replacement in `templateParameters`');
4669
+ }
4670
+ const precol = match.groups.precol;
4671
+ const parameterName = match.groups.parameterName;
4672
+ if (parameterName === '') {
4673
+ // Note: Skip empty placeholders. It's used to avoid confusion with JSON-like strings
4674
+ continue;
4675
+ }
4676
+ if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
4677
+ throw new PipelineExecutionError('Parameter is already opened or not closed');
4678
+ }
4679
+ if (parameters[parameterName] === undefined) {
4680
+ throw new PipelineExecutionError(`Parameter \`{${parameterName}}\` is not defined`);
4681
+ }
4682
+ let parameterValue = parameters[parameterName];
4683
+ if (parameterValue === undefined) {
4684
+ throw new PipelineExecutionError(`Parameter \`{${parameterName}}\` is not defined`);
4685
+ }
4686
+ parameterValue = valueToString(parameterValue);
4687
+ // Escape curly braces in parameter values to prevent prompt-injection
4688
+ parameterValue = parameterValue.replace(/[{}]/g, '\\$&');
4689
+ if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
4690
+ parameterValue = parameterValue
4691
+ .split('\n')
4692
+ .map((line, index) => (index === 0 ? line : `${precol}${line}`))
4693
+ .join('\n');
4694
+ }
4695
+ replacedTemplates =
4696
+ replacedTemplates.substring(0, match.index + precol.length) +
4697
+ parameterValue +
4698
+ replacedTemplates.substring(match.index + precol.length + parameterName.length + 2);
4699
+ }
4700
+ // [๐Ÿ’ซ] Check if there are parameters that are not closed properly
4701
+ if (/{\w+$/.test(replacedTemplates)) {
4702
+ throw new PipelineExecutionError('Parameter is not closed');
4703
+ }
4704
+ // [๐Ÿ’ซ] Check if there are parameters that are not opened properly
4705
+ if (/^\w+}/.test(replacedTemplates)) {
4706
+ throw new PipelineExecutionError('Parameter is not opened');
4707
+ }
4708
+ return replacedTemplates;
4709
+ }
4710
+
4640
4711
  /**
4641
4712
  * Extracts all code blocks from markdown.
4642
4713
  *
@@ -4739,77 +4810,6 @@
4739
4810
  * TODO: [๐Ÿข] Make this logic part of `JsonFormatParser` or `isValidJsonString`
4740
4811
  */
4741
4812
 
4742
- /**
4743
- * Replaces parameters in template with values from parameters object
4744
- *
4745
- * Note: This function is not places strings into string,
4746
- * It's more complex and can handle this operation specifically for LLM models
4747
- *
4748
- * @param template the template with parameters in {curly} braces
4749
- * @param parameters the object with parameters
4750
- * @returns the template with replaced parameters
4751
- * @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
4752
- * @public exported from `@promptbook/utils`
4753
- */
4754
- function templateParameters(template, parameters) {
4755
- for (const [parameterName, parameterValue] of Object.entries(parameters)) {
4756
- if (parameterValue === RESERVED_PARAMETER_MISSING_VALUE) {
4757
- throw new UnexpectedError(`Parameter \`{${parameterName}}\` has missing value`);
4758
- }
4759
- else if (parameterValue === RESERVED_PARAMETER_RESTRICTED) {
4760
- // TODO: [๐Ÿต]
4761
- throw new UnexpectedError(`Parameter \`{${parameterName}}\` is restricted to use`);
4762
- }
4763
- }
4764
- let replacedTemplates = template;
4765
- let match;
4766
- let loopLimit = LOOP_LIMIT;
4767
- while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
4768
- .exec(replacedTemplates))) {
4769
- if (loopLimit-- < 0) {
4770
- throw new LimitReachedError('Loop limit reached during parameters replacement in `templateParameters`');
4771
- }
4772
- const precol = match.groups.precol;
4773
- const parameterName = match.groups.parameterName;
4774
- if (parameterName === '') {
4775
- // Note: Skip empty placeholders. It's used to avoid confusion with JSON-like strings
4776
- continue;
4777
- }
4778
- if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
4779
- throw new PipelineExecutionError('Parameter is already opened or not closed');
4780
- }
4781
- if (parameters[parameterName] === undefined) {
4782
- throw new PipelineExecutionError(`Parameter \`{${parameterName}}\` is not defined`);
4783
- }
4784
- let parameterValue = parameters[parameterName];
4785
- if (parameterValue === undefined) {
4786
- throw new PipelineExecutionError(`Parameter \`{${parameterName}}\` is not defined`);
4787
- }
4788
- parameterValue = valueToString(parameterValue);
4789
- // Escape curly braces in parameter values to prevent prompt-injection
4790
- parameterValue = parameterValue.replace(/[{}]/g, '\\$&');
4791
- if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
4792
- parameterValue = parameterValue
4793
- .split('\n')
4794
- .map((line, index) => (index === 0 ? line : `${precol}${line}`))
4795
- .join('\n');
4796
- }
4797
- replacedTemplates =
4798
- replacedTemplates.substring(0, match.index + precol.length) +
4799
- parameterValue +
4800
- replacedTemplates.substring(match.index + precol.length + parameterName.length + 2);
4801
- }
4802
- // [๐Ÿ’ซ] Check if there are parameters that are not closed properly
4803
- if (/{\w+$/.test(replacedTemplates)) {
4804
- throw new PipelineExecutionError('Parameter is not closed');
4805
- }
4806
- // [๐Ÿ’ซ] Check if there are parameters that are not opened properly
4807
- if (/^\w+}/.test(replacedTemplates)) {
4808
- throw new PipelineExecutionError('Parameter is not opened');
4809
- }
4810
- return replacedTemplates;
4811
- }
4812
-
4813
4813
  /**
4814
4814
  * Counts number of characters in the text
4815
4815
  *
@@ -4970,6 +4970,68 @@
4970
4970
  * Note: [๐Ÿ’] and [๐Ÿค ] are interconnected together
4971
4971
  */
4972
4972
 
4973
+ /**
4974
+ * Validates a prompt result against expectations and format requirements.
4975
+ * This function provides a common abstraction for result validation that can be used
4976
+ * by both execution logic and caching logic to ensure consistency.
4977
+ *
4978
+ * @param options - The validation options including result string, expectations, and format
4979
+ * @returns Validation result with processed string and validity status
4980
+ * @private internal function of `createPipelineExecutor` and `cacheLlmTools`
4981
+ */
4982
+ function validatePromptResult(options) {
4983
+ const { resultString, expectations, format } = options;
4984
+ let processedResultString = resultString;
4985
+ let validationError;
4986
+ try {
4987
+ // TODO: [๐Ÿ’] Unite object for expecting amount and format
4988
+ if (format) {
4989
+ if (format === 'JSON') {
4990
+ if (!isValidJsonString(processedResultString)) {
4991
+ // TODO: [๐Ÿข] Do more universally via `FormatParser`
4992
+ try {
4993
+ processedResultString = extractJsonBlock(processedResultString);
4994
+ }
4995
+ catch (error) {
4996
+ keepUnused(error);
4997
+ throw new ExpectError(spaceTrim.spaceTrim((block) => `
4998
+ Expected valid JSON string
4999
+
5000
+ The expected JSON text:
5001
+ ${block(processedResultString)}
5002
+ `));
5003
+ }
5004
+ }
5005
+ }
5006
+ else {
5007
+ throw new UnexpectedError(`Unknown format "${format}"`);
5008
+ }
5009
+ }
5010
+ // TODO: [๐Ÿ’] Unite object for expecting amount and format
5011
+ if (expectations) {
5012
+ checkExpectations(expectations, processedResultString);
5013
+ }
5014
+ return {
5015
+ isValid: true,
5016
+ processedResultString,
5017
+ };
5018
+ }
5019
+ catch (error) {
5020
+ if (error instanceof ExpectError) {
5021
+ validationError = error;
5022
+ }
5023
+ else {
5024
+ // Re-throw non-ExpectError errors (like UnexpectedError)
5025
+ throw error;
5026
+ }
5027
+ return {
5028
+ isValid: false,
5029
+ processedResultString,
5030
+ error: validationError,
5031
+ };
5032
+ }
5033
+ }
5034
+
4973
5035
  /**
4974
5036
  * Executes a pipeline task with multiple attempts, including joker and retry logic. Handles different task types
4975
5037
  * (prompt, script, dialog, etc.), applies postprocessing, checks expectations, and updates the execution report.
@@ -4987,17 +5049,18 @@
4987
5049
  $resultString: null,
4988
5050
  $expectError: null,
4989
5051
  $scriptPipelineExecutionErrors: [],
5052
+ $failedResults: [], // Track all failed attempts
4990
5053
  };
4991
5054
  // TODO: [๐Ÿš] Make arrayable LLMs -> single LLM DRY
4992
5055
  const _llms = arrayableToArray(tools.llm);
4993
5056
  const llmTools = _llms.length === 1 ? _llms[0] : joinLlmExecutionTools(..._llms);
4994
- attempts: for (let attempt = -jokerParameterNames.length; attempt < maxAttempts; attempt++) {
4995
- const isJokerAttempt = attempt < 0;
4996
- const jokerParameterName = jokerParameterNames[jokerParameterNames.length + attempt];
5057
+ attempts: for (let attemptIndex = -jokerParameterNames.length; attemptIndex < maxAttempts; attemptIndex++) {
5058
+ const isJokerAttempt = attemptIndex < 0;
5059
+ const jokerParameterName = jokerParameterNames[jokerParameterNames.length + attemptIndex];
4997
5060
  // TODO: [๐Ÿง ][๐Ÿญ] JOKERS, EXPECTATIONS, POSTPROCESSING and FOREACH
4998
5061
  if (isJokerAttempt && !jokerParameterName) {
4999
5062
  throw new UnexpectedError(spaceTrim.spaceTrim((block) => `
5000
- Joker not found in attempt ${attempt}
5063
+ Joker not found in attempt ${attemptIndex}
5001
5064
 
5002
5065
  ${block(pipelineIdentification)}
5003
5066
  `));
@@ -5195,35 +5258,18 @@
5195
5258
  }
5196
5259
  }
5197
5260
  // TODO: [๐Ÿ’] Unite object for expecting amount and format
5198
- if (task.format) {
5199
- if (task.format === 'JSON') {
5200
- if (!isValidJsonString($ongoingTaskResult.$resultString || '')) {
5201
- // TODO: [๐Ÿข] Do more universally via `FormatParser`
5202
- try {
5203
- $ongoingTaskResult.$resultString = extractJsonBlock($ongoingTaskResult.$resultString || '');
5204
- }
5205
- catch (error) {
5206
- keepUnused(error);
5207
- throw new ExpectError(spaceTrim.spaceTrim((block) => `
5208
- Expected valid JSON string
5209
-
5210
- ${block(
5211
- /*<- Note: No need for `pipelineIdentification`, it will be catched and added later */ '')}
5212
- `));
5213
- }
5214
- }
5215
- }
5216
- else {
5217
- throw new UnexpectedError(spaceTrim.spaceTrim((block) => `
5218
- Unknown format "${task.format}"
5219
-
5220
- ${block(pipelineIdentification)}
5221
- `));
5261
+ // Use the common validation function for both format and expectations
5262
+ if (task.format || task.expectations) {
5263
+ const validationResult = validatePromptResult({
5264
+ resultString: $ongoingTaskResult.$resultString || '',
5265
+ expectations: task.expectations,
5266
+ format: task.format,
5267
+ });
5268
+ if (!validationResult.isValid) {
5269
+ throw validationResult.error;
5222
5270
  }
5223
- }
5224
- // TODO: [๐Ÿ’] Unite object for expecting amount and format
5225
- if (task.expectations) {
5226
- checkExpectations(task.expectations, $ongoingTaskResult.$resultString || '');
5271
+ // Update the result string in case format processing modified it (e.g., JSON extraction)
5272
+ $ongoingTaskResult.$resultString = validationResult.processedResultString;
5227
5273
  }
5228
5274
  break attempts;
5229
5275
  }
@@ -5232,6 +5278,15 @@
5232
5278
  throw error;
5233
5279
  }
5234
5280
  $ongoingTaskResult.$expectError = error;
5281
+ // Store each failed attempt
5282
+ if (!Array.isArray($ongoingTaskResult.$failedResults)) {
5283
+ $ongoingTaskResult.$failedResults = [];
5284
+ }
5285
+ $ongoingTaskResult.$failedResults.push({
5286
+ attemptIndex,
5287
+ result: $ongoingTaskResult.$resultString,
5288
+ error: error,
5289
+ });
5235
5290
  }
5236
5291
  finally {
5237
5292
  if (!isJokerAttempt &&
@@ -5253,35 +5308,41 @@
5253
5308
  });
5254
5309
  }
5255
5310
  }
5256
- if ($ongoingTaskResult.$expectError !== null && attempt === maxAttempts - 1) {
5311
+ if ($ongoingTaskResult.$expectError !== null && attemptIndex === maxAttempts - 1) {
5312
+ // Note: Create a summary of all failures
5313
+ const failuresSummary = $ongoingTaskResult.$failedResults
5314
+ .map((failure) => spaceTrim.spaceTrim((block) => {
5315
+ var _a, _b;
5316
+ return `
5317
+ Attempt ${failure.attemptIndex + 1}:
5318
+ Error ${((_a = failure.error) === null || _a === void 0 ? void 0 : _a.name) || ''}:
5319
+ ${block((_b = failure.error) === null || _b === void 0 ? void 0 : _b.message.split('\n').map((line) => `> ${line}`).join('\n'))}
5320
+
5321
+ Result:
5322
+ ${block(failure.result === null
5323
+ ? 'null'
5324
+ : spaceTrim.spaceTrim(failure.result)
5325
+ .split('\n')
5326
+ .map((line) => `> ${line}`)
5327
+ .join('\n'))}
5328
+ `;
5329
+ }))
5330
+ .join('\n\n---\n\n');
5257
5331
  throw new PipelineExecutionError(spaceTrim.spaceTrim((block) => {
5258
- var _a, _b, _c;
5332
+ var _a;
5259
5333
  return `
5260
5334
  LLM execution failed ${maxExecutionAttempts}x
5261
5335
 
5262
5336
  ${block(pipelineIdentification)}
5263
5337
 
5264
- ---
5265
5338
  The Prompt:
5266
5339
  ${block((((_a = $ongoingTaskResult.$prompt) === null || _a === void 0 ? void 0 : _a.content) || '')
5267
5340
  .split('\n')
5268
5341
  .map((line) => `> ${line}`)
5269
5342
  .join('\n'))}
5270
5343
 
5271
- Last error ${((_b = $ongoingTaskResult.$expectError) === null || _b === void 0 ? void 0 : _b.name) || ''}:
5272
- ${block((((_c = $ongoingTaskResult.$expectError) === null || _c === void 0 ? void 0 : _c.message) || '')
5273
- .split('\n')
5274
- .map((line) => `> ${line}`)
5275
- .join('\n'))}
5276
-
5277
- Last result:
5278
- ${block($ongoingTaskResult.$resultString === null
5279
- ? 'null'
5280
- : spaceTrim.spaceTrim($ongoingTaskResult.$resultString)
5281
- .split('\n')
5282
- .map((line) => `> ${line}`)
5283
- .join('\n'))}
5284
- ---
5344
+ All Failed Attempts:
5345
+ ${block(failuresSummary)}
5285
5346
  `;
5286
5347
  }));
5287
5348
  }