@promptbook/cli 0.67.3 → 0.67.5

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.
@@ -1,4 +1,6 @@
1
1
  import { PROMPTBOOK_VERSION } from '../version';
2
+ import { extractBlock } from '../postprocessing/utils/extractBlock';
3
+ import { extractJsonBlock } from '../postprocessing/utils/extractJsonBlock';
2
4
  import type { string_markdown } from '../types/typeAliases';
3
5
  import type { string_markdown_section } from '../types/typeAliases';
4
6
  import type { string_markdown_section_content } from '../types/typeAliases';
@@ -17,6 +19,8 @@ import { removeContentComments } from '../utils/markdown/removeContentComments';
17
19
  import { removeMarkdownFormatting } from '../utils/markdown/removeMarkdownFormatting';
18
20
  import { splitMarkdownIntoSections } from '../utils/markdown/splitMarkdownIntoSections';
19
21
  export { PROMPTBOOK_VERSION };
22
+ export { extractBlock };
23
+ export { extractJsonBlock };
20
24
  export type { string_markdown };
21
25
  export type { string_markdown_section };
22
26
  export type { string_markdown_section_content };
@@ -8,7 +8,6 @@ import { deserializeError } from '../errors/utils/deserializeError';
8
8
  import { serializeError } from '../errors/utils/serializeError';
9
9
  import { forEachAsync } from '../execution/utils/forEachAsync';
10
10
  import { isValidJsonString } from '../formats/json/utils/isValidJsonString';
11
- import { extractBlock } from '../postprocessing/utils/extractBlock';
12
11
  import { $currentDate } from '../utils/$currentDate';
13
12
  import { $isRunningInBrowser } from '../utils/environment/$isRunningInBrowser';
14
13
  import { $isRunningInNode } from '../utils/environment/$isRunningInNode';
@@ -80,7 +79,6 @@ export { deserializeError };
80
79
  export { serializeError };
81
80
  export { forEachAsync };
82
81
  export { isValidJsonString };
83
- export { extractBlock };
84
82
  export { $currentDate };
85
83
  export { $isRunningInBrowser };
86
84
  export { $isRunningInNode };
@@ -62,6 +62,7 @@ export type FormatDefinition<TValue extends TPartialValue, TPartialValue extends
62
62
  extractValues(value: string, schema?: TSchema): Array<string>;
63
63
  };
64
64
  /**
65
+ * TODO: [♏] Add some prepare hook to modify prompt according to the format
65
66
  * TODO: [🍓]`name` and `aliases` should be UPPERCASE only and interpreted as case-insensitive (via normalization)
66
67
  * TODO: [🍓][👨‍⚖️] Compute TPartialValue dynamically - PartialString<TValue>
67
68
  * TODO: [🍓][🧠] Should execution tools be aviable to heal, canBeValid and isValid?
@@ -2,16 +2,15 @@ import type { string_markdown } from '../../types/typeAliases';
2
2
  /**
3
3
  * Extracts code block from markdown.
4
4
  *
5
- * Note: If there are multiple or no code blocks the function throws an error
5
+ * - When there are multiple or no code blocks the function throws a `ParsingError`
6
6
  *
7
- * Note: There are 3 simmilar function:
7
+ * Note: There are multiple simmilar function:
8
8
  * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
9
+ * - `extractJsonBlock` extracts exactly one valid JSON code block
9
10
  * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
10
11
  * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
11
12
  *
12
- * @public exported from `@promptbook/utils`
13
+ * @public exported from `@promptbook/markdown-utils`
14
+ * @throws {ParsingError} if there is not exactly one code block in the markdown
13
15
  */
14
16
  export declare function extractBlock(markdown: string_markdown): string;
15
- /**
16
- * TODO: [🧠][🌻] Maybe export through `@promptbook/markdown-utils` not `@promptbook/utils`
17
- */
@@ -0,0 +1,25 @@
1
+ import type { string_json } from '../../types/typeAliases';
2
+ import type { string_markdown } from '../../types/typeAliases';
3
+ import type { really_unknown } from '../../utils/organization/really_unknown';
4
+ /**
5
+ * Extracts extracts exactly one valid JSON code block
6
+ *
7
+ * - When given string is a valid JSON as it is, it just returns it
8
+ * - When there is no JSON code block the function throws a `ParsingError`
9
+ * - When there are multiple JSON code blocks the function throws a `ParsingError`
10
+ *
11
+ * Note: It is not important if marked as ```json BUT if it is VALID JSON
12
+ * Note: There are multiple simmilar function:
13
+ * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
14
+ * - `extractJsonBlock` extracts exactly one valid JSON code block
15
+ * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
16
+ * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
17
+ *
18
+ * @public exported from `@promptbook/markdown-utils`
19
+ * @throws {ParsingError} if there is no valid JSON block in the markdown
20
+ */
21
+ export declare function extractJsonBlock(markdown: string_markdown): string_json<really_unknown>;
22
+ /**
23
+ * TODO: Add some auto-healing logic + extract YAML, JSON5, TOML, etc.
24
+ * TODO: [🏢] Make this logic part of `JsonFormatDefinition` or `isValidJsonString`
25
+ */
@@ -19,13 +19,15 @@ export type CodeBlock = {
19
19
  /**
20
20
  * Extracts all code blocks from markdown.
21
21
  *
22
- * Note: There are 3 simmilar function:
22
+ * Note: There are multiple simmilar function:
23
23
  * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
24
+ * - `extractJsonBlock` extracts exactly one valid JSON code block
24
25
  * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
25
26
  * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
26
27
  *
27
28
  * @param markdown any valid markdown
28
29
  * @returns code blocks with language and content
30
+ * @throws {ParsingError} if block is not closed properly
29
31
  * @public exported from `@promptbook/markdown-utils`
30
32
  */
31
33
  export declare function extractAllBlocksFromMarkdown(markdown: string_markdown): Array<CodeBlock>;
@@ -3,16 +3,18 @@ import type { CodeBlock } from './extractAllBlocksFromMarkdown';
3
3
  /**
4
4
  * Extracts exactly ONE code block from markdown.
5
5
  *
6
- * Note: If there are multiple or no code blocks the function throws an error
6
+ * - When there are multiple or no code blocks the function throws a `ParsingError`
7
7
  *
8
- * Note: There are 3 simmilar function:
8
+ * Note: There are multiple simmilar function:
9
9
  * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
10
+ * - `extractJsonBlock` extracts exactly one valid JSON code block
10
11
  * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
11
12
  * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
12
13
  *
13
14
  * @param markdown any valid markdown
14
15
  * @returns code block with language and content
15
16
  * @public exported from `@promptbook/markdown-utils`
17
+ * @throws {ParsingError} if there is not exactly one code block in the markdown
16
18
  */
17
19
  export declare function extractOneBlockFromMarkdown(markdown: string_markdown): CodeBlock;
18
20
  /***
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promptbook/cli",
3
- "version": "0.67.3",
3
+ "version": "0.67.5",
4
4
  "description": "Supercharge your use of large language models",
5
5
  "private": false,
6
6
  "sideEffects": false,
package/umd/index.umd.js CHANGED
@@ -39,7 +39,7 @@
39
39
  /**
40
40
  * The version of the Promptbook library
41
41
  */
42
- var PROMPTBOOK_VERSION = '0.67.2';
42
+ var PROMPTBOOK_VERSION = '0.67.4';
43
43
  // TODO: !!!! List here all the versions and annotate + put into script
44
44
 
45
45
  /*! *****************************************************************************
@@ -1053,7 +1053,7 @@
1053
1053
  });
1054
1054
  }
1055
1055
 
1056
- var PipelineCollection = [{title:"Prepare Knowledge from Markdown",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.67.2",parameters:[{name:"knowledgeContent",description:"Markdown document content",isInput:true,isOutput:false},{name:"knowledgePieces",description:"The knowledge JSON object",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT"},content:"You are experienced data researcher, extract the important knowledge from the document.\n\n# Rules\n\n- Make pieces of information concise, clear, and easy to understand\n- One piece of information should be approximately 1 paragraph\n- Divide the paragraphs by markdown horizontal lines ---\n- Omit irrelevant information\n- Group redundant information\n- Write just extracted information, nothing else\n\n# The document\n\nTake information from this document:\n\n> {knowledgeContent}",dependentParameterNames:["knowledgeContent"],resultingParameterName:"knowledgePieces"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-from-markdown.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-keywords.ptbk.md",promptbookVersion:"0.67.2",parameters:[{name:"knowledgePieceContent",description:"The content",isInput:true,isOutput:false},{name:"keywords",description:"Keywords separated by comma",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT"},content:"You are experienced data researcher, detect the important keywords in the document.\n\n# Rules\n\n- Write just keywords separated by comma\n\n# The document\n\nTake information from this document:\n\n> {knowledgePieceContent}",dependentParameterNames:["knowledgePieceContent"],resultingParameterName:"keywords"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-keywords.ptbk.md"},{title:"Prepare Title",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-title.ptbk.md",promptbookVersion:"0.67.2",parameters:[{name:"knowledgePieceContent",description:"The content",isInput:true,isOutput:false},{name:"title",description:"The title of the document",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT"},content:"You are experienced content creator, write best title for the document.\n\n# Rules\n\n- Write just title, nothing else\n- Title should be concise and clear\n- Write maximum 5 words for the title\n\n# The document\n\n> {knowledgePieceContent}",expectations:{words:{min:1,max:8}},dependentParameterNames:["knowledgePieceContent"],resultingParameterName:"title"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-title.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-persona.ptbk.md",promptbookVersion:"0.67.2",parameters:[{name:"availableModelNames",description:"List of available model names separated by comma (,)",isInput:true,isOutput:false},{name:"personaDescription",description:"Description of the persona",isInput:true,isOutput:false},{name:"modelRequirements",description:"Specific requirements for the model",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"make-model-requirements",title:"Make modelRequirements",modelRequirements:{modelVariant:"CHAT"},content:"You are experienced AI engineer, you need to create virtual assistant.\nWrite\n\n## Sample\n\n```json\n{\n\"modelName\": \"gpt-4o\",\n\"systemMessage\": \"You are experienced AI engineer and helpfull assistant.\",\n\"temperature\": 0.7\n}\n```\n\n## Instructions\n\n- Your output format is JSON object\n- Write just the JSON object, no other text should be present\n- It contains the following keys:\n - `modelName`: The name of the model to use\n - `systemMessage`: The system message to provide context to the model\n - `temperature`: The sampling temperature to use\n\n### Key `modelName`\n\nPick from the following models:\n\n- {availableModelNames}\n\n### Key `systemMessage`\n\nThe system message is used to communicate instructions or provide context to the model at the beginning of a conversation. It is displayed in a different format compared to user messages, helping the model understand its role in the conversation. The system message typically guides the model's behavior, sets the tone, or specifies desired output from the model. By utilizing the system message effectively, users can steer the model towards generating more accurate and relevant responses.\n\nFor example:\n\n> You are an experienced AI engineer and helpful assistant.\n\n> You are a friendly and knowledgeable chatbot.\n\n### Key `temperature`\n\nThe sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.\n\nYou can pick a value between 0 and 2. For example:\n\n- `0.1`: Low temperature, extremely conservative and deterministic\n- `0.5`: Medium temperature, balanced between conservative and creative\n- `1.0`: High temperature, creative and bit random\n- `1.5`: Very high temperature, extremely creative and often chaotic and unpredictable\n- `2.0`: Maximum temperature, completely random and unpredictable, for some extreme creative use cases\n\n# The assistant\n\nTake this description of the persona:\n\n> {personaDescription}",expectFormat:"JSON",dependentParameterNames:["availableModelNames","personaDescription"],resultingParameterName:"modelRequirements"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-persona.ptbk.md"}];
1056
+ var PipelineCollection = [{title:"Prepare Knowledge from Markdown",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.67.4",parameters:[{name:"knowledgeContent",description:"Markdown document content",isInput:true,isOutput:false},{name:"knowledgePieces",description:"The knowledge JSON object",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT"},content:"You are experienced data researcher, extract the important knowledge from the document.\n\n# Rules\n\n- Make pieces of information concise, clear, and easy to understand\n- One piece of information should be approximately 1 paragraph\n- Divide the paragraphs by markdown horizontal lines ---\n- Omit irrelevant information\n- Group redundant information\n- Write just extracted information, nothing else\n\n# The document\n\nTake information from this document:\n\n> {knowledgeContent}",dependentParameterNames:["knowledgeContent"],resultingParameterName:"knowledgePieces"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-from-markdown.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-keywords.ptbk.md",promptbookVersion:"0.67.4",parameters:[{name:"knowledgePieceContent",description:"The content",isInput:true,isOutput:false},{name:"keywords",description:"Keywords separated by comma",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT"},content:"You are experienced data researcher, detect the important keywords in the document.\n\n# Rules\n\n- Write just keywords separated by comma\n\n# The document\n\nTake information from this document:\n\n> {knowledgePieceContent}",dependentParameterNames:["knowledgePieceContent"],resultingParameterName:"keywords"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-keywords.ptbk.md"},{title:"Prepare Title",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-title.ptbk.md",promptbookVersion:"0.67.4",parameters:[{name:"knowledgePieceContent",description:"The content",isInput:true,isOutput:false},{name:"title",description:"The title of the document",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT"},content:"You are experienced content creator, write best title for the document.\n\n# Rules\n\n- Write just title, nothing else\n- Title should be concise and clear\n- Write maximum 5 words for the title\n\n# The document\n\n> {knowledgePieceContent}",expectations:{words:{min:1,max:8}},dependentParameterNames:["knowledgePieceContent"],resultingParameterName:"title"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-title.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-persona.ptbk.md",promptbookVersion:"0.67.4",parameters:[{name:"availableModelNames",description:"List of available model names separated by comma (,)",isInput:true,isOutput:false},{name:"personaDescription",description:"Description of the persona",isInput:true,isOutput:false},{name:"modelRequirements",description:"Specific requirements for the model",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"make-model-requirements",title:"Make modelRequirements",modelRequirements:{modelVariant:"CHAT"},content:"You are experienced AI engineer, you need to create virtual assistant.\nWrite\n\n## Sample\n\n```json\n{\n\"modelName\": \"gpt-4o\",\n\"systemMessage\": \"You are experienced AI engineer and helpfull assistant.\",\n\"temperature\": 0.7\n}\n```\n\n## Instructions\n\n- Your output format is JSON object\n- Write just the JSON object, no other text should be present\n- It contains the following keys:\n - `modelName`: The name of the model to use\n - `systemMessage`: The system message to provide context to the model\n - `temperature`: The sampling temperature to use\n\n### Key `modelName`\n\nPick from the following models:\n\n- {availableModelNames}\n\n### Key `systemMessage`\n\nThe system message is used to communicate instructions or provide context to the model at the beginning of a conversation. It is displayed in a different format compared to user messages, helping the model understand its role in the conversation. The system message typically guides the model's behavior, sets the tone, or specifies desired output from the model. By utilizing the system message effectively, users can steer the model towards generating more accurate and relevant responses.\n\nFor example:\n\n> You are an experienced AI engineer and helpful assistant.\n\n> You are a friendly and knowledgeable chatbot.\n\n### Key `temperature`\n\nThe sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.\n\nYou can pick a value between 0 and 2. For example:\n\n- `0.1`: Low temperature, extremely conservative and deterministic\n- `0.5`: Medium temperature, balanced between conservative and creative\n- `1.0`: High temperature, creative and bit random\n- `1.5`: Very high temperature, extremely creative and often chaotic and unpredictable\n- `2.0`: Maximum temperature, completely random and unpredictable, for some extreme creative use cases\n\n# The assistant\n\nTake this description of the persona:\n\n> {personaDescription}",expectFormat:"JSON",dependentParameterNames:["availableModelNames","personaDescription"],resultingParameterName:"modelRequirements"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-persona.ptbk.md"}];
1057
1057
 
1058
1058
  /**
1059
1059
  * This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
@@ -2620,6 +2620,122 @@
2620
2620
  * TODO: [👷‍♂️] @@@ Manual about construction of llmTools
2621
2621
  */
2622
2622
 
2623
+ /**
2624
+ * Extracts all code blocks from markdown.
2625
+ *
2626
+ * Note: There are multiple simmilar function:
2627
+ * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
2628
+ * - `extractJsonBlock` extracts exactly one valid JSON code block
2629
+ * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
2630
+ * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
2631
+ *
2632
+ * @param markdown any valid markdown
2633
+ * @returns code blocks with language and content
2634
+ * @throws {ParsingError} if block is not closed properly
2635
+ * @public exported from `@promptbook/markdown-utils`
2636
+ */
2637
+ function extractAllBlocksFromMarkdown(markdown) {
2638
+ var e_1, _a;
2639
+ var codeBlocks = [];
2640
+ var lines = markdown.split('\n');
2641
+ // Note: [0] Ensure that the last block notated by gt > will be closed
2642
+ lines.push('');
2643
+ var currentCodeBlock = null;
2644
+ try {
2645
+ for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
2646
+ var line = lines_1_1.value;
2647
+ if (line.startsWith('> ') || line === '>') {
2648
+ if (currentCodeBlock === null) {
2649
+ currentCodeBlock = { blockNotation: '>', language: null, content: '' };
2650
+ } /* not else */
2651
+ if (currentCodeBlock.blockNotation === '>') {
2652
+ if (currentCodeBlock.content !== '') {
2653
+ currentCodeBlock.content += '\n';
2654
+ }
2655
+ currentCodeBlock.content += line.slice(2);
2656
+ }
2657
+ }
2658
+ else if (currentCodeBlock !== null && currentCodeBlock.blockNotation === '>' /* <- Note: [0] */) {
2659
+ codeBlocks.push(currentCodeBlock);
2660
+ currentCodeBlock = null;
2661
+ }
2662
+ /* not else */
2663
+ if (line.startsWith('```')) {
2664
+ var language = line.slice(3).trim() || null;
2665
+ if (currentCodeBlock === null) {
2666
+ currentCodeBlock = { blockNotation: '```', language: language, content: '' };
2667
+ }
2668
+ else {
2669
+ if (language !== null) {
2670
+ throw new ParsingError("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
2671
+ }
2672
+ codeBlocks.push(currentCodeBlock);
2673
+ currentCodeBlock = null;
2674
+ }
2675
+ }
2676
+ else if (currentCodeBlock !== null && currentCodeBlock.blockNotation === '```') {
2677
+ if (currentCodeBlock.content !== '') {
2678
+ currentCodeBlock.content += '\n';
2679
+ }
2680
+ currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
2681
+ }
2682
+ }
2683
+ }
2684
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2685
+ finally {
2686
+ try {
2687
+ if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
2688
+ }
2689
+ finally { if (e_1) throw e_1.error; }
2690
+ }
2691
+ if (currentCodeBlock !== null) {
2692
+ throw new ParsingError("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
2693
+ }
2694
+ return codeBlocks;
2695
+ }
2696
+ /**
2697
+ * TODO: Maybe name for `blockNotation` instead of '```' and '>'
2698
+ */
2699
+
2700
+ /**
2701
+ * Extracts extracts exactly one valid JSON code block
2702
+ *
2703
+ * - When given string is a valid JSON as it is, it just returns it
2704
+ * - When there is no JSON code block the function throws a `ParsingError`
2705
+ * - When there are multiple JSON code blocks the function throws a `ParsingError`
2706
+ *
2707
+ * Note: It is not important if marked as ```json BUT if it is VALID JSON
2708
+ * Note: There are multiple simmilar function:
2709
+ * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
2710
+ * - `extractJsonBlock` extracts exactly one valid JSON code block
2711
+ * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
2712
+ * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
2713
+ *
2714
+ * @public exported from `@promptbook/markdown-utils`
2715
+ * @throws {ParsingError} if there is no valid JSON block in the markdown
2716
+ */
2717
+ function extractJsonBlock(markdown) {
2718
+ if (isValidJsonString(markdown)) {
2719
+ return markdown;
2720
+ }
2721
+ var codeBlocks = extractAllBlocksFromMarkdown(markdown);
2722
+ var jsonBlocks = codeBlocks.filter(function (_a) {
2723
+ var content = _a.content;
2724
+ return isValidJsonString(content);
2725
+ });
2726
+ if (jsonBlocks.length === 0) {
2727
+ throw new Error('There is no valid JSON block in the markdown');
2728
+ }
2729
+ if (jsonBlocks.length > 1) {
2730
+ throw new Error('There are multiple JSON code blocks in the markdown');
2731
+ }
2732
+ return jsonBlocks[0].content;
2733
+ }
2734
+ /**
2735
+ * TODO: Add some auto-healing logic + extract YAML, JSON5, TOML, etc.
2736
+ * TODO: [🏢] Make this logic part of `JsonFormatDefinition` or `isValidJsonString`
2737
+ */
2738
+
2623
2739
  /**
2624
2740
  * Determine if the pipeline is fully prepared
2625
2741
  *
@@ -2671,6 +2787,27 @@
2671
2787
  return [input];
2672
2788
  }
2673
2789
 
2790
+ /**
2791
+ * Just says that the variable is not used but should be kept
2792
+ * No side effects.
2793
+ *
2794
+ * Note: It can be usefull for:
2795
+ *
2796
+ * 1) Suppressing eager optimization of unused imports
2797
+ * 2) Suppressing eslint errors of unused variables in the tests
2798
+ * 3) Keeping the type of the variable for type testing
2799
+ *
2800
+ * @param value any values
2801
+ * @returns void
2802
+ * @private within the repository
2803
+ */
2804
+ function keepUnused() {
2805
+ var valuesToKeep = [];
2806
+ for (var _i = 0; _i < arguments.length; _i++) {
2807
+ valuesToKeep[_i] = arguments[_i];
2808
+ }
2809
+ }
2810
+
2674
2811
  /**
2675
2812
  * Just marks a place of place where should be something implemented
2676
2813
  * No side effects.
@@ -3111,7 +3248,11 @@
3111
3248
  usedParameterNames = extractParameterNamesFromPromptTemplate(currentTemplate);
3112
3249
  dependentParameterNames = new Set(currentTemplate.dependentParameterNames);
3113
3250
  if (union(difference(usedParameterNames, dependentParameterNames), difference(dependentParameterNames, usedParameterNames)).size !== 0) {
3114
- throw new UnexpectedError(spaceTrim.spaceTrim(function (block) { return "\n Dependent parameters are not consistent used parameters:\n\n ".concat(block(pipelineIdentification), "\n\n Dependent parameters:\n ").concat(Array.from(dependentParameterNames).join(', '), "\n\n Used parameters:\n ").concat(Array.from(usedParameterNames).join(', '), "\n\n "); }));
3251
+ throw new UnexpectedError(spaceTrim.spaceTrim(function (block) { return "\n Dependent parameters are not consistent with used parameters:\n\n ".concat(block(pipelineIdentification), "\n\n Dependent parameters:\n ").concat(Array.from(dependentParameterNames)
3252
+ .map(function (name) { return "{".concat(name, "}"); })
3253
+ .join(', '), "\n\n Used parameters:\n ").concat(Array.from(usedParameterNames)
3254
+ .map(function (name) { return "{".concat(name, "}"); })
3255
+ .join(', '), "\n\n "); }));
3115
3256
  }
3116
3257
  _b = (_a = Object).freeze;
3117
3258
  _c = [{}];
@@ -3406,9 +3547,20 @@
3406
3547
  if (currentTemplate.expectFormat) {
3407
3548
  if (currentTemplate.expectFormat === 'JSON') {
3408
3549
  if (!isValidJsonString(resultString || '')) {
3409
- throw new ExpectError(spaceTrim.spaceTrim(function (block) { return "\n Expected valid JSON string\n\n ".concat(block(pipelineIdentification), "\n "); }));
3550
+ // TODO: [🏢] Do more universally via `FormatDefinition`
3551
+ try {
3552
+ resultString = extractJsonBlock(resultString || '');
3553
+ }
3554
+ catch (error) {
3555
+ keepUnused(error);
3556
+ throw new ExpectError(spaceTrim.spaceTrim(function (block) { return "\n Expected valid JSON string\n\n ".concat(block(
3557
+ /*<- Note: No need for `pipelineIdentification`, it will be catched and added later */ ''), "\n "); }));
3558
+ }
3410
3559
  }
3411
3560
  }
3561
+ else {
3562
+ throw new UnexpectedError(spaceTrim.spaceTrim(function (block) { return "\n Unknown expectFormat \"".concat(currentTemplate.expectFormat, "\"\n\n ").concat(block(pipelineIdentification), "\n "); }));
3563
+ }
3412
3564
  }
3413
3565
  // TODO: [💝] Unite object for expecting amount and format
3414
3566
  if (currentTemplate.expectations) {
@@ -3439,7 +3591,18 @@
3439
3591
  return [7 /*endfinally*/];
3440
3592
  case 46:
3441
3593
  if (expectError !== null && attempt === maxAttempts - 1) {
3442
- throw new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n LLM execution failed ".concat(maxExecutionAttempts, "x\n\n ---\n Last error ").concat((expectError === null || expectError === void 0 ? void 0 : expectError.name) || '', ":\n ").concat(block((expectError === null || expectError === void 0 ? void 0 : expectError.message) || ''), "\n\n Last result:\n ").concat(resultString, "\n ---\n "); }));
3594
+ throw new PipelineExecutionError(spaceTrim.spaceTrim(function (block) { return "\n LLM execution failed ".concat(maxExecutionAttempts, "x\n\n ").concat(block(pipelineIdentification), "\n\n ---\n The Prompt:\n ").concat(block(prompt.content
3595
+ .split('\n')
3596
+ .map(function (line) { return "> ".concat(line); })
3597
+ .join('\n')), "\n\n Last error ").concat((expectError === null || expectError === void 0 ? void 0 : expectError.name) || '', ":\n ").concat(block(((expectError === null || expectError === void 0 ? void 0 : expectError.message) || '')
3598
+ .split('\n')
3599
+ .map(function (line) { return "> ".concat(line); })
3600
+ .join('\n')), "\n\n Last result:\n ").concat(block(resultString === null
3601
+ ? 'null'
3602
+ : resultString
3603
+ .split('\n')
3604
+ .map(function (line) { return "> ".concat(line); })
3605
+ .join('\n')), "\n ---\n "); }));
3443
3606
  }
3444
3607
  return [2 /*return*/];
3445
3608
  }
@@ -5551,94 +5714,21 @@
5551
5714
  return listItems;
5552
5715
  }
5553
5716
 
5554
- /**
5555
- * Extracts all code blocks from markdown.
5556
- *
5557
- * Note: There are 3 simmilar function:
5558
- * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
5559
- * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
5560
- * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
5561
- *
5562
- * @param markdown any valid markdown
5563
- * @returns code blocks with language and content
5564
- * @public exported from `@promptbook/markdown-utils`
5565
- */
5566
- function extractAllBlocksFromMarkdown(markdown) {
5567
- var e_1, _a;
5568
- var codeBlocks = [];
5569
- var lines = markdown.split('\n');
5570
- // Note: [0] Ensure that the last block notated by gt > will be closed
5571
- lines.push('');
5572
- var currentCodeBlock = null;
5573
- try {
5574
- for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
5575
- var line = lines_1_1.value;
5576
- if (line.startsWith('> ') || line === '>') {
5577
- if (currentCodeBlock === null) {
5578
- currentCodeBlock = { blockNotation: '>', language: null, content: '' };
5579
- } /* not else */
5580
- if (currentCodeBlock.blockNotation === '>') {
5581
- if (currentCodeBlock.content !== '') {
5582
- currentCodeBlock.content += '\n';
5583
- }
5584
- currentCodeBlock.content += line.slice(2);
5585
- }
5586
- }
5587
- else if (currentCodeBlock !== null && currentCodeBlock.blockNotation === '>' /* <- Note: [0] */) {
5588
- codeBlocks.push(currentCodeBlock);
5589
- currentCodeBlock = null;
5590
- }
5591
- /* not else */
5592
- if (line.startsWith('```')) {
5593
- var language = line.slice(3).trim() || null;
5594
- if (currentCodeBlock === null) {
5595
- currentCodeBlock = { blockNotation: '```', language: language, content: '' };
5596
- }
5597
- else {
5598
- if (language !== null) {
5599
- throw new ParsingError("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
5600
- }
5601
- codeBlocks.push(currentCodeBlock);
5602
- currentCodeBlock = null;
5603
- }
5604
- }
5605
- else if (currentCodeBlock !== null && currentCodeBlock.blockNotation === '```') {
5606
- if (currentCodeBlock.content !== '') {
5607
- currentCodeBlock.content += '\n';
5608
- }
5609
- currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
5610
- }
5611
- }
5612
- }
5613
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
5614
- finally {
5615
- try {
5616
- if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
5617
- }
5618
- finally { if (e_1) throw e_1.error; }
5619
- }
5620
- if (currentCodeBlock !== null) {
5621
- throw new ParsingError("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
5622
- }
5623
- return codeBlocks;
5624
- }
5625
- /**
5626
- * TODO: Maybe name for `blockNotation` instead of '```' and '>'
5627
- */
5628
-
5629
5717
  /**
5630
5718
  * Extracts exactly ONE code block from markdown.
5631
5719
  *
5632
- * Note: If there are multiple or no code blocks the function throws an error
5720
+ * - When there are multiple or no code blocks the function throws a `ParsingError`
5633
5721
  *
5634
- * Note: There are 3 simmilar function:
5722
+ * Note: There are multiple simmilar function:
5635
5723
  * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
5724
+ * - `extractJsonBlock` extracts exactly one valid JSON code block
5636
5725
  * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
5637
5726
  * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
5638
5727
  *
5639
5728
  * @param markdown any valid markdown
5640
5729
  * @returns code block with language and content
5641
5730
  * @public exported from `@promptbook/markdown-utils`
5731
+ * @throws {ParsingError} if there is not exactly one code block in the markdown
5642
5732
  */
5643
5733
  function extractOneBlockFromMarkdown(markdown) {
5644
5734
  var codeBlocks = extractAllBlocksFromMarkdown(markdown);
@@ -7567,7 +7657,7 @@
7567
7657
  var placeForSection = removeContentComments(content).match(/^##.*$/im);
7568
7658
  if (!placeForSection) {
7569
7659
  throw new ParsingError(
7570
- // <- [🧠] Maybe something better than `ParsingError`
7660
+ // <- [🧠] Maybe something better tha `ParsingError`
7571
7661
  "No place where to put the section <!--".concat(sectionName, "-->"));
7572
7662
  // <- [🚞]
7573
7663
  }