@promptbook/markdown-utils 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 +113 -52
  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 +1 -1
  24. package/umd/index.umd.js +113 -52
  25. package/umd/index.umd.js.map +1 -1
package/README.md CHANGED
@@ -25,6 +25,10 @@ Write AI applications using plain human language across multiple models and plat
25
25
 
26
26
 
27
27
 
28
+ <blockquote style="color: #ff8811">
29
+ <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>.
30
+ </blockquote>
31
+
28
32
  ## πŸ“¦ Package `@promptbook/markdown-utils`
29
33
 
30
34
  - Promptbooks are [divided into several](#-packages) packages, all are published from [single monorepo](https://github.com/webgptorg/promptbook).
@@ -56,6 +60,8 @@ Rest of the documentation is common for **entire promptbook ecosystem**:
56
60
 
57
61
  During the computer revolution, we have seen [multiple generations of computer languages](https://github.com/webgptorg/promptbook/discussions/180), from the physical rewiring of the vacuum tubes through low-level machine code to the high-level languages like Python or JavaScript. And now, we're on the edge of the **next revolution**!
58
62
 
63
+
64
+
59
65
  It's a revolution of writing software in **plain human language** that is understandable and executable by both humans and machines – and it's going to change everything!
60
66
 
61
67
  The incredible growth in power of microprocessors and the Moore's Law have been the driving force behind the ever-more powerful languages, and it's been an amazing journey! Similarly, the large language models (like GPT or Claude) are the next big thing in language technology, and they're set to transform the way we interact with computers.
@@ -181,6 +187,8 @@ Join our growing community of developers and users:
181
187
 
182
188
  _A concise, Markdown-based DSL for crafting AI workflows and automations._
183
189
 
190
+
191
+
184
192
  ### Introduction
185
193
 
186
194
  Book is a Markdown-based language that simplifies the creation of AI applications, workflows, and automations. With human-readable commands, you can define inputs, outputs, personas, knowledge sources, and actionsβ€”without needing model-specific details.
@@ -230,6 +238,8 @@ Personas can have access to different knowledge, tools and actions. They can als
230
238
 
231
239
  - [PERSONA](https://github.com/webgptorg/promptbook/blob/main/documents/commands/PERSONA.md)
232
240
 
241
+
242
+
233
243
  ### **3. How:** Knowledge, Instruments and Actions
234
244
 
235
245
  The resources used by the personas are used to do the work.
@@ -329,6 +339,8 @@ The following glossary is used to clarify certain concepts:
329
339
 
330
340
  _Note: This section is not a complete dictionary, more list of general AI / LLM terms that has connection with Promptbook_
331
341
 
342
+
343
+
332
344
  ### πŸ’― Core concepts
333
345
 
334
346
  - [πŸ“š Collection of pipelines](https://github.com/webgptorg/promptbook/discussions/65)
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.95.0';
28
+ const PROMPTBOOK_ENGINE_VERSION = '0.98.0-10';
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 = 10; // <- 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.
@@ -4809,17 +4871,18 @@ async function executeAttempts(options) {
4809
4871
  $resultString: null,
4810
4872
  $expectError: null,
4811
4873
  $scriptPipelineExecutionErrors: [],
4874
+ $failedResults: [], // Track all failed attempts
4812
4875
  };
4813
4876
  // TODO: [🚐] Make arrayable LLMs -> single LLM DRY
4814
4877
  const _llms = arrayableToArray(tools.llm);
4815
4878
  const llmTools = _llms.length === 1 ? _llms[0] : joinLlmExecutionTools(..._llms);
4816
- attempts: for (let attempt = -jokerParameterNames.length; attempt < maxAttempts; attempt++) {
4817
- const isJokerAttempt = attempt < 0;
4818
- 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];
4819
4882
  // TODO: [🧠][🍭] JOKERS, EXPECTATIONS, POSTPROCESSING and FOREACH
4820
4883
  if (isJokerAttempt && !jokerParameterName) {
4821
4884
  throw new UnexpectedError(spaceTrim$1((block) => `
4822
- Joker not found in attempt ${attempt}
4885
+ Joker not found in attempt ${attemptIndex}
4823
4886
 
4824
4887
  ${block(pipelineIdentification)}
4825
4888
  `));
@@ -5017,35 +5080,18 @@ async function executeAttempts(options) {
5017
5080
  }
5018
5081
  }
5019
5082
  // TODO: [πŸ’] Unite object for expecting amount and format
5020
- if (task.format) {
5021
- if (task.format === 'JSON') {
5022
- if (!isValidJsonString($ongoingTaskResult.$resultString || '')) {
5023
- // TODO: [🏒] Do more universally via `FormatParser`
5024
- try {
5025
- $ongoingTaskResult.$resultString = extractJsonBlock($ongoingTaskResult.$resultString || '');
5026
- }
5027
- catch (error) {
5028
- keepUnused(error);
5029
- throw new ExpectError(spaceTrim$1((block) => `
5030
- Expected valid JSON string
5031
-
5032
- ${block(
5033
- /*<- Note: No need for `pipelineIdentification`, it will be catched and added later */ '')}
5034
- `));
5035
- }
5036
- }
5037
- }
5038
- else {
5039
- throw new UnexpectedError(spaceTrim$1((block) => `
5040
- Unknown format "${task.format}"
5041
-
5042
- ${block(pipelineIdentification)}
5043
- `));
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;
5044
5092
  }
5045
- }
5046
- // TODO: [πŸ’] Unite object for expecting amount and format
5047
- if (task.expectations) {
5048
- 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;
5049
5095
  }
5050
5096
  break attempts;
5051
5097
  }
@@ -5054,6 +5100,15 @@ async function executeAttempts(options) {
5054
5100
  throw error;
5055
5101
  }
5056
5102
  $ongoingTaskResult.$expectError = error;
5103
+ // Store each failed attempt
5104
+ if (!Array.isArray($ongoingTaskResult.$failedResults)) {
5105
+ $ongoingTaskResult.$failedResults = [];
5106
+ }
5107
+ $ongoingTaskResult.$failedResults.push({
5108
+ attemptIndex,
5109
+ result: $ongoingTaskResult.$resultString,
5110
+ error: error,
5111
+ });
5057
5112
  }
5058
5113
  finally {
5059
5114
  if (!isJokerAttempt &&
@@ -5075,35 +5130,41 @@ async function executeAttempts(options) {
5075
5130
  });
5076
5131
  }
5077
5132
  }
5078
- if ($ongoingTaskResult.$expectError !== null && attempt === maxAttempts - 1) {
5133
+ if ($ongoingTaskResult.$expectError !== null && attemptIndex === maxAttempts - 1) {
5134
+ // Note: Create a summary of all failures
5135
+ const failuresSummary = $ongoingTaskResult.$failedResults
5136
+ .map((failure) => spaceTrim$1((block) => {
5137
+ var _a, _b;
5138
+ return `
5139
+ Attempt ${failure.attemptIndex + 1}:
5140
+ Error ${((_a = failure.error) === null || _a === void 0 ? void 0 : _a.name) || ''}:
5141
+ ${block((_b = failure.error) === null || _b === void 0 ? void 0 : _b.message.split('\n').map((line) => `> ${line}`).join('\n'))}
5142
+
5143
+ Result:
5144
+ ${block(failure.result === null
5145
+ ? 'null'
5146
+ : spaceTrim$1(failure.result)
5147
+ .split('\n')
5148
+ .map((line) => `> ${line}`)
5149
+ .join('\n'))}
5150
+ `;
5151
+ }))
5152
+ .join('\n\n---\n\n');
5079
5153
  throw new PipelineExecutionError(spaceTrim$1((block) => {
5080
- var _a, _b, _c;
5154
+ var _a;
5081
5155
  return `
5082
5156
  LLM execution failed ${maxExecutionAttempts}x
5083
5157
 
5084
5158
  ${block(pipelineIdentification)}
5085
5159
 
5086
- ---
5087
5160
  The Prompt:
5088
5161
  ${block((((_a = $ongoingTaskResult.$prompt) === null || _a === void 0 ? void 0 : _a.content) || '')
5089
5162
  .split('\n')
5090
5163
  .map((line) => `> ${line}`)
5091
5164
  .join('\n'))}
5092
5165
 
5093
- Last error ${((_b = $ongoingTaskResult.$expectError) === null || _b === void 0 ? void 0 : _b.name) || ''}:
5094
- ${block((((_c = $ongoingTaskResult.$expectError) === null || _c === void 0 ? void 0 : _c.message) || '')
5095
- .split('\n')
5096
- .map((line) => `> ${line}`)
5097
- .join('\n'))}
5098
-
5099
- Last result:
5100
- ${block($ongoingTaskResult.$resultString === null
5101
- ? 'null'
5102
- : spaceTrim$1($ongoingTaskResult.$resultString)
5103
- .split('\n')
5104
- .map((line) => `> ${line}`)
5105
- .join('\n'))}
5106
- ---
5166
+ All Failed Attempts:
5167
+ ${block(failuresSummary)}
5107
5168
  `;
5108
5169
  }));
5109
5170
  }