@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.
- package/esm/index.es.js +172 -82
- package/esm/index.es.js.map +1 -1
- package/esm/typings/src/_packages/markdown-utils.index.d.ts +4 -0
- package/esm/typings/src/_packages/utils.index.d.ts +0 -2
- package/esm/typings/src/formats/_common/FormatDefinition.d.ts +1 -0
- package/esm/typings/src/postprocessing/utils/extractBlock.d.ts +5 -6
- package/esm/typings/src/postprocessing/utils/extractJsonBlock.d.ts +25 -0
- package/esm/typings/src/postprocessing/utils/extractJsonBlock.test.d.ts +1 -0
- package/esm/typings/src/utils/markdown/extractAllBlocksFromMarkdown.d.ts +3 -1
- package/esm/typings/src/utils/markdown/extractOneBlockFromMarkdown.d.ts +4 -2
- package/package.json +2 -2
- package/umd/index.umd.js +172 -82
- package/umd/index.umd.js.map +1 -1
package/esm/index.es.js
CHANGED
|
@@ -13,7 +13,7 @@ import * as dotenv from 'dotenv';
|
|
|
13
13
|
/**
|
|
14
14
|
* The version of the Promptbook library
|
|
15
15
|
*/
|
|
16
|
-
var PROMPTBOOK_VERSION = '0.67.
|
|
16
|
+
var PROMPTBOOK_VERSION = '0.67.4';
|
|
17
17
|
// TODO: !!!! List here all the versions and annotate + put into script
|
|
18
18
|
|
|
19
19
|
/*! *****************************************************************************
|
|
@@ -867,7 +867,7 @@ function forEachAsync(array, options, callbackfunction) {
|
|
|
867
867
|
});
|
|
868
868
|
}
|
|
869
869
|
|
|
870
|
-
var PipelineCollection = [{title:"Prepare Knowledge from Markdown",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.67.
|
|
870
|
+
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"}];
|
|
871
871
|
|
|
872
872
|
/**
|
|
873
873
|
* This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
|
|
@@ -2450,6 +2450,122 @@ function joinLlmExecutionTools() {
|
|
|
2450
2450
|
* TODO: [👷♂️] @@@ Manual about construction of llmTools
|
|
2451
2451
|
*/
|
|
2452
2452
|
|
|
2453
|
+
/**
|
|
2454
|
+
* Extracts all code blocks from markdown.
|
|
2455
|
+
*
|
|
2456
|
+
* Note: There are multiple simmilar function:
|
|
2457
|
+
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
2458
|
+
* - `extractJsonBlock` extracts exactly one valid JSON code block
|
|
2459
|
+
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
2460
|
+
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
2461
|
+
*
|
|
2462
|
+
* @param markdown any valid markdown
|
|
2463
|
+
* @returns code blocks with language and content
|
|
2464
|
+
* @throws {ParsingError} if block is not closed properly
|
|
2465
|
+
* @public exported from `@promptbook/markdown-utils`
|
|
2466
|
+
*/
|
|
2467
|
+
function extractAllBlocksFromMarkdown(markdown) {
|
|
2468
|
+
var e_1, _a;
|
|
2469
|
+
var codeBlocks = [];
|
|
2470
|
+
var lines = markdown.split('\n');
|
|
2471
|
+
// Note: [0] Ensure that the last block notated by gt > will be closed
|
|
2472
|
+
lines.push('');
|
|
2473
|
+
var currentCodeBlock = null;
|
|
2474
|
+
try {
|
|
2475
|
+
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
2476
|
+
var line = lines_1_1.value;
|
|
2477
|
+
if (line.startsWith('> ') || line === '>') {
|
|
2478
|
+
if (currentCodeBlock === null) {
|
|
2479
|
+
currentCodeBlock = { blockNotation: '>', language: null, content: '' };
|
|
2480
|
+
} /* not else */
|
|
2481
|
+
if (currentCodeBlock.blockNotation === '>') {
|
|
2482
|
+
if (currentCodeBlock.content !== '') {
|
|
2483
|
+
currentCodeBlock.content += '\n';
|
|
2484
|
+
}
|
|
2485
|
+
currentCodeBlock.content += line.slice(2);
|
|
2486
|
+
}
|
|
2487
|
+
}
|
|
2488
|
+
else if (currentCodeBlock !== null && currentCodeBlock.blockNotation === '>' /* <- Note: [0] */) {
|
|
2489
|
+
codeBlocks.push(currentCodeBlock);
|
|
2490
|
+
currentCodeBlock = null;
|
|
2491
|
+
}
|
|
2492
|
+
/* not else */
|
|
2493
|
+
if (line.startsWith('```')) {
|
|
2494
|
+
var language = line.slice(3).trim() || null;
|
|
2495
|
+
if (currentCodeBlock === null) {
|
|
2496
|
+
currentCodeBlock = { blockNotation: '```', language: language, content: '' };
|
|
2497
|
+
}
|
|
2498
|
+
else {
|
|
2499
|
+
if (language !== null) {
|
|
2500
|
+
throw new ParsingError("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
|
|
2501
|
+
}
|
|
2502
|
+
codeBlocks.push(currentCodeBlock);
|
|
2503
|
+
currentCodeBlock = null;
|
|
2504
|
+
}
|
|
2505
|
+
}
|
|
2506
|
+
else if (currentCodeBlock !== null && currentCodeBlock.blockNotation === '```') {
|
|
2507
|
+
if (currentCodeBlock.content !== '') {
|
|
2508
|
+
currentCodeBlock.content += '\n';
|
|
2509
|
+
}
|
|
2510
|
+
currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
|
|
2511
|
+
}
|
|
2512
|
+
}
|
|
2513
|
+
}
|
|
2514
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2515
|
+
finally {
|
|
2516
|
+
try {
|
|
2517
|
+
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
2518
|
+
}
|
|
2519
|
+
finally { if (e_1) throw e_1.error; }
|
|
2520
|
+
}
|
|
2521
|
+
if (currentCodeBlock !== null) {
|
|
2522
|
+
throw new ParsingError("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
|
|
2523
|
+
}
|
|
2524
|
+
return codeBlocks;
|
|
2525
|
+
}
|
|
2526
|
+
/**
|
|
2527
|
+
* TODO: Maybe name for `blockNotation` instead of '```' and '>'
|
|
2528
|
+
*/
|
|
2529
|
+
|
|
2530
|
+
/**
|
|
2531
|
+
* Extracts extracts exactly one valid JSON code block
|
|
2532
|
+
*
|
|
2533
|
+
* - When given string is a valid JSON as it is, it just returns it
|
|
2534
|
+
* - When there is no JSON code block the function throws a `ParsingError`
|
|
2535
|
+
* - When there are multiple JSON code blocks the function throws a `ParsingError`
|
|
2536
|
+
*
|
|
2537
|
+
* Note: It is not important if marked as ```json BUT if it is VALID JSON
|
|
2538
|
+
* Note: There are multiple simmilar function:
|
|
2539
|
+
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
2540
|
+
* - `extractJsonBlock` extracts exactly one valid JSON code block
|
|
2541
|
+
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
2542
|
+
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
2543
|
+
*
|
|
2544
|
+
* @public exported from `@promptbook/markdown-utils`
|
|
2545
|
+
* @throws {ParsingError} if there is no valid JSON block in the markdown
|
|
2546
|
+
*/
|
|
2547
|
+
function extractJsonBlock(markdown) {
|
|
2548
|
+
if (isValidJsonString(markdown)) {
|
|
2549
|
+
return markdown;
|
|
2550
|
+
}
|
|
2551
|
+
var codeBlocks = extractAllBlocksFromMarkdown(markdown);
|
|
2552
|
+
var jsonBlocks = codeBlocks.filter(function (_a) {
|
|
2553
|
+
var content = _a.content;
|
|
2554
|
+
return isValidJsonString(content);
|
|
2555
|
+
});
|
|
2556
|
+
if (jsonBlocks.length === 0) {
|
|
2557
|
+
throw new Error('There is no valid JSON block in the markdown');
|
|
2558
|
+
}
|
|
2559
|
+
if (jsonBlocks.length > 1) {
|
|
2560
|
+
throw new Error('There are multiple JSON code blocks in the markdown');
|
|
2561
|
+
}
|
|
2562
|
+
return jsonBlocks[0].content;
|
|
2563
|
+
}
|
|
2564
|
+
/**
|
|
2565
|
+
* TODO: Add some auto-healing logic + extract YAML, JSON5, TOML, etc.
|
|
2566
|
+
* TODO: [🏢] Make this logic part of `JsonFormatDefinition` or `isValidJsonString`
|
|
2567
|
+
*/
|
|
2568
|
+
|
|
2453
2569
|
/**
|
|
2454
2570
|
* Determine if the pipeline is fully prepared
|
|
2455
2571
|
*
|
|
@@ -2501,6 +2617,27 @@ function arrayableToArray(input) {
|
|
|
2501
2617
|
return [input];
|
|
2502
2618
|
}
|
|
2503
2619
|
|
|
2620
|
+
/**
|
|
2621
|
+
* Just says that the variable is not used but should be kept
|
|
2622
|
+
* No side effects.
|
|
2623
|
+
*
|
|
2624
|
+
* Note: It can be usefull for:
|
|
2625
|
+
*
|
|
2626
|
+
* 1) Suppressing eager optimization of unused imports
|
|
2627
|
+
* 2) Suppressing eslint errors of unused variables in the tests
|
|
2628
|
+
* 3) Keeping the type of the variable for type testing
|
|
2629
|
+
*
|
|
2630
|
+
* @param value any values
|
|
2631
|
+
* @returns void
|
|
2632
|
+
* @private within the repository
|
|
2633
|
+
*/
|
|
2634
|
+
function keepUnused() {
|
|
2635
|
+
var valuesToKeep = [];
|
|
2636
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2637
|
+
valuesToKeep[_i] = arguments[_i];
|
|
2638
|
+
}
|
|
2639
|
+
}
|
|
2640
|
+
|
|
2504
2641
|
/**
|
|
2505
2642
|
* Just marks a place of place where should be something implemented
|
|
2506
2643
|
* No side effects.
|
|
@@ -2941,7 +3078,11 @@ function createPipelineExecutor(options) {
|
|
|
2941
3078
|
usedParameterNames = extractParameterNamesFromPromptTemplate(currentTemplate);
|
|
2942
3079
|
dependentParameterNames = new Set(currentTemplate.dependentParameterNames);
|
|
2943
3080
|
if (union(difference(usedParameterNames, dependentParameterNames), difference(dependentParameterNames, usedParameterNames)).size !== 0) {
|
|
2944
|
-
throw new UnexpectedError(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)
|
|
3081
|
+
throw new UnexpectedError(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)
|
|
3082
|
+
.map(function (name) { return "{".concat(name, "}"); })
|
|
3083
|
+
.join(', '), "\n\n Used parameters:\n ").concat(Array.from(usedParameterNames)
|
|
3084
|
+
.map(function (name) { return "{".concat(name, "}"); })
|
|
3085
|
+
.join(', '), "\n\n "); }));
|
|
2945
3086
|
}
|
|
2946
3087
|
_b = (_a = Object).freeze;
|
|
2947
3088
|
_c = [{}];
|
|
@@ -3236,9 +3377,20 @@ function createPipelineExecutor(options) {
|
|
|
3236
3377
|
if (currentTemplate.expectFormat) {
|
|
3237
3378
|
if (currentTemplate.expectFormat === 'JSON') {
|
|
3238
3379
|
if (!isValidJsonString(resultString || '')) {
|
|
3239
|
-
|
|
3380
|
+
// TODO: [🏢] Do more universally via `FormatDefinition`
|
|
3381
|
+
try {
|
|
3382
|
+
resultString = extractJsonBlock(resultString || '');
|
|
3383
|
+
}
|
|
3384
|
+
catch (error) {
|
|
3385
|
+
keepUnused(error);
|
|
3386
|
+
throw new ExpectError(spaceTrim(function (block) { return "\n Expected valid JSON string\n\n ".concat(block(
|
|
3387
|
+
/*<- Note: No need for `pipelineIdentification`, it will be catched and added later */ ''), "\n "); }));
|
|
3388
|
+
}
|
|
3240
3389
|
}
|
|
3241
3390
|
}
|
|
3391
|
+
else {
|
|
3392
|
+
throw new UnexpectedError(spaceTrim(function (block) { return "\n Unknown expectFormat \"".concat(currentTemplate.expectFormat, "\"\n\n ").concat(block(pipelineIdentification), "\n "); }));
|
|
3393
|
+
}
|
|
3242
3394
|
}
|
|
3243
3395
|
// TODO: [💝] Unite object for expecting amount and format
|
|
3244
3396
|
if (currentTemplate.expectations) {
|
|
@@ -3269,7 +3421,18 @@ function createPipelineExecutor(options) {
|
|
|
3269
3421
|
return [7 /*endfinally*/];
|
|
3270
3422
|
case 46:
|
|
3271
3423
|
if (expectError !== null && attempt === maxAttempts - 1) {
|
|
3272
|
-
throw new PipelineExecutionError(spaceTrim(function (block) { return "\n LLM execution failed ".concat(maxExecutionAttempts, "x\n\n
|
|
3424
|
+
throw new PipelineExecutionError(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
|
|
3425
|
+
.split('\n')
|
|
3426
|
+
.map(function (line) { return "> ".concat(line); })
|
|
3427
|
+
.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) || '')
|
|
3428
|
+
.split('\n')
|
|
3429
|
+
.map(function (line) { return "> ".concat(line); })
|
|
3430
|
+
.join('\n')), "\n\n Last result:\n ").concat(block(resultString === null
|
|
3431
|
+
? 'null'
|
|
3432
|
+
: resultString
|
|
3433
|
+
.split('\n')
|
|
3434
|
+
.map(function (line) { return "> ".concat(line); })
|
|
3435
|
+
.join('\n')), "\n ---\n "); }));
|
|
3273
3436
|
}
|
|
3274
3437
|
return [2 /*return*/];
|
|
3275
3438
|
}
|
|
@@ -5381,94 +5544,21 @@ function extractAllListItemsFromMarkdown(markdown) {
|
|
|
5381
5544
|
return listItems;
|
|
5382
5545
|
}
|
|
5383
5546
|
|
|
5384
|
-
/**
|
|
5385
|
-
* Extracts all code blocks from markdown.
|
|
5386
|
-
*
|
|
5387
|
-
* Note: There are 3 simmilar function:
|
|
5388
|
-
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
5389
|
-
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
5390
|
-
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
5391
|
-
*
|
|
5392
|
-
* @param markdown any valid markdown
|
|
5393
|
-
* @returns code blocks with language and content
|
|
5394
|
-
* @public exported from `@promptbook/markdown-utils`
|
|
5395
|
-
*/
|
|
5396
|
-
function extractAllBlocksFromMarkdown(markdown) {
|
|
5397
|
-
var e_1, _a;
|
|
5398
|
-
var codeBlocks = [];
|
|
5399
|
-
var lines = markdown.split('\n');
|
|
5400
|
-
// Note: [0] Ensure that the last block notated by gt > will be closed
|
|
5401
|
-
lines.push('');
|
|
5402
|
-
var currentCodeBlock = null;
|
|
5403
|
-
try {
|
|
5404
|
-
for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
|
5405
|
-
var line = lines_1_1.value;
|
|
5406
|
-
if (line.startsWith('> ') || line === '>') {
|
|
5407
|
-
if (currentCodeBlock === null) {
|
|
5408
|
-
currentCodeBlock = { blockNotation: '>', language: null, content: '' };
|
|
5409
|
-
} /* not else */
|
|
5410
|
-
if (currentCodeBlock.blockNotation === '>') {
|
|
5411
|
-
if (currentCodeBlock.content !== '') {
|
|
5412
|
-
currentCodeBlock.content += '\n';
|
|
5413
|
-
}
|
|
5414
|
-
currentCodeBlock.content += line.slice(2);
|
|
5415
|
-
}
|
|
5416
|
-
}
|
|
5417
|
-
else if (currentCodeBlock !== null && currentCodeBlock.blockNotation === '>' /* <- Note: [0] */) {
|
|
5418
|
-
codeBlocks.push(currentCodeBlock);
|
|
5419
|
-
currentCodeBlock = null;
|
|
5420
|
-
}
|
|
5421
|
-
/* not else */
|
|
5422
|
-
if (line.startsWith('```')) {
|
|
5423
|
-
var language = line.slice(3).trim() || null;
|
|
5424
|
-
if (currentCodeBlock === null) {
|
|
5425
|
-
currentCodeBlock = { blockNotation: '```', language: language, content: '' };
|
|
5426
|
-
}
|
|
5427
|
-
else {
|
|
5428
|
-
if (language !== null) {
|
|
5429
|
-
throw new ParsingError("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed and already opening new ").concat(language, " code block"));
|
|
5430
|
-
}
|
|
5431
|
-
codeBlocks.push(currentCodeBlock);
|
|
5432
|
-
currentCodeBlock = null;
|
|
5433
|
-
}
|
|
5434
|
-
}
|
|
5435
|
-
else if (currentCodeBlock !== null && currentCodeBlock.blockNotation === '```') {
|
|
5436
|
-
if (currentCodeBlock.content !== '') {
|
|
5437
|
-
currentCodeBlock.content += '\n';
|
|
5438
|
-
}
|
|
5439
|
-
currentCodeBlock.content += line.split('\\`\\`\\`').join('```') /* <- TODO: Maybe make propper unescape */;
|
|
5440
|
-
}
|
|
5441
|
-
}
|
|
5442
|
-
}
|
|
5443
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
5444
|
-
finally {
|
|
5445
|
-
try {
|
|
5446
|
-
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
|
5447
|
-
}
|
|
5448
|
-
finally { if (e_1) throw e_1.error; }
|
|
5449
|
-
}
|
|
5450
|
-
if (currentCodeBlock !== null) {
|
|
5451
|
-
throw new ParsingError("".concat(capitalize(currentCodeBlock.language || 'the'), " code block was not closed at the end of the markdown"));
|
|
5452
|
-
}
|
|
5453
|
-
return codeBlocks;
|
|
5454
|
-
}
|
|
5455
|
-
/**
|
|
5456
|
-
* TODO: Maybe name for `blockNotation` instead of '```' and '>'
|
|
5457
|
-
*/
|
|
5458
|
-
|
|
5459
5547
|
/**
|
|
5460
5548
|
* Extracts exactly ONE code block from markdown.
|
|
5461
5549
|
*
|
|
5462
|
-
*
|
|
5550
|
+
* - When there are multiple or no code blocks the function throws a `ParsingError`
|
|
5463
5551
|
*
|
|
5464
|
-
* Note: There are
|
|
5552
|
+
* Note: There are multiple simmilar function:
|
|
5465
5553
|
* - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
|
|
5554
|
+
* - `extractJsonBlock` extracts exactly one valid JSON code block
|
|
5466
5555
|
* - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
|
|
5467
5556
|
* - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
|
|
5468
5557
|
*
|
|
5469
5558
|
* @param markdown any valid markdown
|
|
5470
5559
|
* @returns code block with language and content
|
|
5471
5560
|
* @public exported from `@promptbook/markdown-utils`
|
|
5561
|
+
* @throws {ParsingError} if there is not exactly one code block in the markdown
|
|
5472
5562
|
*/
|
|
5473
5563
|
function extractOneBlockFromMarkdown(markdown) {
|
|
5474
5564
|
var codeBlocks = extractAllBlocksFromMarkdown(markdown);
|