@promptbook/node 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/node",
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,
@@ -47,7 +47,7 @@
47
47
  "module": "./esm/index.es.js",
48
48
  "typings": "./esm/typings/src/_packages/node.index.d.ts",
49
49
  "peerDependencies": {
50
- "@promptbook/core": "0.67.3"
50
+ "@promptbook/core": "0.67.5"
51
51
  },
52
52
  "dependencies": {
53
53
  "colors": "1.4.0",
package/umd/index.umd.js CHANGED
@@ -35,7 +35,7 @@
35
35
  /**
36
36
  * The version of the Promptbook library
37
37
  */
38
- var PROMPTBOOK_VERSION = '0.67.2';
38
+ var PROMPTBOOK_VERSION = '0.67.4';
39
39
  // TODO: !!!! List here all the versions and annotate + put into script
40
40
 
41
41
  /*! *****************************************************************************
@@ -889,7 +889,7 @@
889
889
  });
890
890
  }
891
891
 
892
- 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"}];
892
+ 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"}];
893
893
 
894
894
  /**
895
895
  * This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
@@ -2472,6 +2472,122 @@
2472
2472
  * TODO: [👷‍♂️] @@@ Manual about construction of llmTools
2473
2473
  */
2474
2474
 
2475
+ /**
2476
+ * Extracts all code blocks from markdown.
2477
+ *
2478
+ * Note: There are multiple simmilar function:
2479
+ * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
2480
+ * - `extractJsonBlock` extracts exactly one valid JSON code block
2481
+ * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
2482
+ * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
2483
+ *
2484
+ * @param markdown any valid markdown
2485
+ * @returns code blocks with language and content
2486
+ * @throws {ParsingError} if block is not closed properly
2487
+ * @public exported from `@promptbook/markdown-utils`
2488
+ */
2489
+ function extractAllBlocksFromMarkdown(markdown) {
2490
+ var e_1, _a;
2491
+ var codeBlocks = [];
2492
+ var lines = markdown.split('\n');
2493
+ // Note: [0] Ensure that the last block notated by gt > will be closed
2494
+ lines.push('');
2495
+ var currentCodeBlock = null;
2496
+ try {
2497
+ for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
2498
+ var line = lines_1_1.value;
2499
+ if (line.startsWith('> ') || line === '>') {
2500
+ if (currentCodeBlock === null) {
2501
+ currentCodeBlock = { blockNotation: '>', language: null, content: '' };
2502
+ } /* not else */
2503
+ if (currentCodeBlock.blockNotation === '>') {
2504
+ if (currentCodeBlock.content !== '') {
2505
+ currentCodeBlock.content += '\n';
2506
+ }
2507
+ currentCodeBlock.content += line.slice(2);
2508
+ }
2509
+ }
2510
+ else if (currentCodeBlock !== null && currentCodeBlock.blockNotation === '>' /* <- Note: [0] */) {
2511
+ codeBlocks.push(currentCodeBlock);
2512
+ currentCodeBlock = null;
2513
+ }
2514
+ /* not else */
2515
+ if (line.startsWith('```')) {
2516
+ var language = line.slice(3).trim() || null;
2517
+ if (currentCodeBlock === null) {
2518
+ currentCodeBlock = { blockNotation: '```', language: language, content: '' };
2519
+ }
2520
+ else {
2521
+ if (language !== null) {
2522
+ throw new ParsingError("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
2523
+ }
2524
+ codeBlocks.push(currentCodeBlock);
2525
+ currentCodeBlock = null;
2526
+ }
2527
+ }
2528
+ else if (currentCodeBlock !== null && currentCodeBlock.blockNotation === '```') {
2529
+ if (currentCodeBlock.content !== '') {
2530
+ currentCodeBlock.content += '\n';
2531
+ }
2532
+ currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
2533
+ }
2534
+ }
2535
+ }
2536
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2537
+ finally {
2538
+ try {
2539
+ if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
2540
+ }
2541
+ finally { if (e_1) throw e_1.error; }
2542
+ }
2543
+ if (currentCodeBlock !== null) {
2544
+ throw new ParsingError("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
2545
+ }
2546
+ return codeBlocks;
2547
+ }
2548
+ /**
2549
+ * TODO: Maybe name for `blockNotation` instead of '```' and '>'
2550
+ */
2551
+
2552
+ /**
2553
+ * Extracts extracts exactly one valid JSON code block
2554
+ *
2555
+ * - When given string is a valid JSON as it is, it just returns it
2556
+ * - When there is no JSON code block the function throws a `ParsingError`
2557
+ * - When there are multiple JSON code blocks the function throws a `ParsingError`
2558
+ *
2559
+ * Note: It is not important if marked as ```json BUT if it is VALID JSON
2560
+ * Note: There are multiple simmilar function:
2561
+ * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
2562
+ * - `extractJsonBlock` extracts exactly one valid JSON code block
2563
+ * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
2564
+ * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
2565
+ *
2566
+ * @public exported from `@promptbook/markdown-utils`
2567
+ * @throws {ParsingError} if there is no valid JSON block in the markdown
2568
+ */
2569
+ function extractJsonBlock(markdown) {
2570
+ if (isValidJsonString(markdown)) {
2571
+ return markdown;
2572
+ }
2573
+ var codeBlocks = extractAllBlocksFromMarkdown(markdown);
2574
+ var jsonBlocks = codeBlocks.filter(function (_a) {
2575
+ var content = _a.content;
2576
+ return isValidJsonString(content);
2577
+ });
2578
+ if (jsonBlocks.length === 0) {
2579
+ throw new Error('There is no valid JSON block in the markdown');
2580
+ }
2581
+ if (jsonBlocks.length > 1) {
2582
+ throw new Error('There are multiple JSON code blocks in the markdown');
2583
+ }
2584
+ return jsonBlocks[0].content;
2585
+ }
2586
+ /**
2587
+ * TODO: Add some auto-healing logic + extract YAML, JSON5, TOML, etc.
2588
+ * TODO: [🏢] Make this logic part of `JsonFormatDefinition` or `isValidJsonString`
2589
+ */
2590
+
2475
2591
  /**
2476
2592
  * Determine if the pipeline is fully prepared
2477
2593
  *
@@ -2523,6 +2639,27 @@
2523
2639
  return [input];
2524
2640
  }
2525
2641
 
2642
+ /**
2643
+ * Just says that the variable is not used but should be kept
2644
+ * No side effects.
2645
+ *
2646
+ * Note: It can be usefull for:
2647
+ *
2648
+ * 1) Suppressing eager optimization of unused imports
2649
+ * 2) Suppressing eslint errors of unused variables in the tests
2650
+ * 3) Keeping the type of the variable for type testing
2651
+ *
2652
+ * @param value any values
2653
+ * @returns void
2654
+ * @private within the repository
2655
+ */
2656
+ function keepUnused() {
2657
+ var valuesToKeep = [];
2658
+ for (var _i = 0; _i < arguments.length; _i++) {
2659
+ valuesToKeep[_i] = arguments[_i];
2660
+ }
2661
+ }
2662
+
2526
2663
  /**
2527
2664
  * Just marks a place of place where should be something implemented
2528
2665
  * No side effects.
@@ -2963,7 +3100,11 @@
2963
3100
  usedParameterNames = extractParameterNamesFromPromptTemplate(currentTemplate);
2964
3101
  dependentParameterNames = new Set(currentTemplate.dependentParameterNames);
2965
3102
  if (union(difference(usedParameterNames, dependentParameterNames), difference(dependentParameterNames, usedParameterNames)).size !== 0) {
2966
- 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 "); }));
3103
+ 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)
3104
+ .map(function (name) { return "{".concat(name, "}"); })
3105
+ .join(', '), "\n\n Used parameters:\n ").concat(Array.from(usedParameterNames)
3106
+ .map(function (name) { return "{".concat(name, "}"); })
3107
+ .join(', '), "\n\n "); }));
2967
3108
  }
2968
3109
  _b = (_a = Object).freeze;
2969
3110
  _c = [{}];
@@ -3258,9 +3399,20 @@
3258
3399
  if (currentTemplate.expectFormat) {
3259
3400
  if (currentTemplate.expectFormat === 'JSON') {
3260
3401
  if (!isValidJsonString(resultString || '')) {
3261
- throw new ExpectError(spaceTrim.spaceTrim(function (block) { return "\n Expected valid JSON string\n\n ".concat(block(pipelineIdentification), "\n "); }));
3402
+ // TODO: [🏢] Do more universally via `FormatDefinition`
3403
+ try {
3404
+ resultString = extractJsonBlock(resultString || '');
3405
+ }
3406
+ catch (error) {
3407
+ keepUnused(error);
3408
+ throw new ExpectError(spaceTrim.spaceTrim(function (block) { return "\n Expected valid JSON string\n\n ".concat(block(
3409
+ /*<- Note: No need for `pipelineIdentification`, it will be catched and added later */ ''), "\n "); }));
3410
+ }
3262
3411
  }
3263
3412
  }
3413
+ else {
3414
+ throw new UnexpectedError(spaceTrim.spaceTrim(function (block) { return "\n Unknown expectFormat \"".concat(currentTemplate.expectFormat, "\"\n\n ").concat(block(pipelineIdentification), "\n "); }));
3415
+ }
3264
3416
  }
3265
3417
  // TODO: [💝] Unite object for expecting amount and format
3266
3418
  if (currentTemplate.expectations) {
@@ -3291,7 +3443,18 @@
3291
3443
  return [7 /*endfinally*/];
3292
3444
  case 46:
3293
3445
  if (expectError !== null && attempt === maxAttempts - 1) {
3294
- 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 "); }));
3446
+ 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
3447
+ .split('\n')
3448
+ .map(function (line) { return "> ".concat(line); })
3449
+ .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) || '')
3450
+ .split('\n')
3451
+ .map(function (line) { return "> ".concat(line); })
3452
+ .join('\n')), "\n\n Last result:\n ").concat(block(resultString === null
3453
+ ? 'null'
3454
+ : resultString
3455
+ .split('\n')
3456
+ .map(function (line) { return "> ".concat(line); })
3457
+ .join('\n')), "\n ---\n "); }));
3295
3458
  }
3296
3459
  return [2 /*return*/];
3297
3460
  }
@@ -5403,94 +5566,21 @@
5403
5566
  return listItems;
5404
5567
  }
5405
5568
 
5406
- /**
5407
- * Extracts all code blocks from markdown.
5408
- *
5409
- * Note: There are 3 simmilar function:
5410
- * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
5411
- * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
5412
- * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
5413
- *
5414
- * @param markdown any valid markdown
5415
- * @returns code blocks with language and content
5416
- * @public exported from `@promptbook/markdown-utils`
5417
- */
5418
- function extractAllBlocksFromMarkdown(markdown) {
5419
- var e_1, _a;
5420
- var codeBlocks = [];
5421
- var lines = markdown.split('\n');
5422
- // Note: [0] Ensure that the last block notated by gt > will be closed
5423
- lines.push('');
5424
- var currentCodeBlock = null;
5425
- try {
5426
- for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
5427
- var line = lines_1_1.value;
5428
- if (line.startsWith('> ') || line === '>') {
5429
- if (currentCodeBlock === null) {
5430
- currentCodeBlock = { blockNotation: '>', language: null, content: '' };
5431
- } /* not else */
5432
- if (currentCodeBlock.blockNotation === '>') {
5433
- if (currentCodeBlock.content !== '') {
5434
- currentCodeBlock.content += '\n';
5435
- }
5436
- currentCodeBlock.content += line.slice(2);
5437
- }
5438
- }
5439
- else if (currentCodeBlock !== null && currentCodeBlock.blockNotation === '>' /* <- Note: [0] */) {
5440
- codeBlocks.push(currentCodeBlock);
5441
- currentCodeBlock = null;
5442
- }
5443
- /* not else */
5444
- if (line.startsWith('```')) {
5445
- var language = line.slice(3).trim() || null;
5446
- if (currentCodeBlock === null) {
5447
- currentCodeBlock = { blockNotation: '```', language: language, content: '' };
5448
- }
5449
- else {
5450
- if (language !== null) {
5451
- throw new ParsingError("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
5452
- }
5453
- codeBlocks.push(currentCodeBlock);
5454
- currentCodeBlock = null;
5455
- }
5456
- }
5457
- else if (currentCodeBlock !== null && currentCodeBlock.blockNotation === '```') {
5458
- if (currentCodeBlock.content !== '') {
5459
- currentCodeBlock.content += '\n';
5460
- }
5461
- currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
5462
- }
5463
- }
5464
- }
5465
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
5466
- finally {
5467
- try {
5468
- if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
5469
- }
5470
- finally { if (e_1) throw e_1.error; }
5471
- }
5472
- if (currentCodeBlock !== null) {
5473
- throw new ParsingError("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
5474
- }
5475
- return codeBlocks;
5476
- }
5477
- /**
5478
- * TODO: Maybe name for `blockNotation` instead of '```' and '>'
5479
- */
5480
-
5481
5569
  /**
5482
5570
  * Extracts exactly ONE code block from markdown.
5483
5571
  *
5484
- * Note: If there are multiple or no code blocks the function throws an error
5572
+ * - When there are multiple or no code blocks the function throws a `ParsingError`
5485
5573
  *
5486
- * Note: There are 3 simmilar function:
5574
+ * Note: There are multiple simmilar function:
5487
5575
  * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
5576
+ * - `extractJsonBlock` extracts exactly one valid JSON code block
5488
5577
  * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
5489
5578
  * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
5490
5579
  *
5491
5580
  * @param markdown any valid markdown
5492
5581
  * @returns code block with language and content
5493
5582
  * @public exported from `@promptbook/markdown-utils`
5583
+ * @throws {ParsingError} if there is not exactly one code block in the markdown
5494
5584
  */
5495
5585
  function extractOneBlockFromMarkdown(markdown) {
5496
5586
  var codeBlocks = extractAllBlocksFromMarkdown(markdown);