@promptbook/markitdown 0.98.0-6 โ 0.98.0-9
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 +156 -116
- package/esm/index.es.js.map +1 -1
- package/esm/typings/src/_packages/anthropic-claude.index.d.ts +2 -2
- package/esm/typings/src/_packages/openai.index.d.ts +4 -0
- package/esm/typings/src/_packages/types.index.d.ts +10 -2
- package/esm/typings/src/config.d.ts +1 -1
- package/esm/typings/src/execution/createPipelineExecutor/$OngoingTaskResult.d.ts +1 -0
- package/esm/typings/src/execution/utils/validatePromptResult.d.ts +53 -0
- package/esm/typings/src/llm-providers/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +3 -3
- package/esm/typings/src/llm-providers/anthropic-claude/AnthropicClaudeExecutionToolsOptions.d.ts +2 -2
- package/esm/typings/src/llm-providers/openai/OpenAiCompatibleExecutionTools.d.ts +4 -4
- package/esm/typings/src/llm-providers/openai/OpenAiCompatibleExecutionToolsOptions.d.ts +42 -1
- package/esm/typings/src/llm-providers/openai/createOpenAiCompatibleExecutionTools.d.ts +58 -1
- package/esm/typings/src/version.d.ts +1 -1
- package/package.json +2 -2
- package/umd/index.umd.js +156 -116
- package/umd/index.umd.js.map +1 -1
package/esm/index.es.js
CHANGED
|
@@ -26,7 +26,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
|
|
|
26
26
|
* @generated
|
|
27
27
|
* @see https://github.com/webgptorg/promptbook
|
|
28
28
|
*/
|
|
29
|
-
const PROMPTBOOK_ENGINE_VERSION = '0.98.0-
|
|
29
|
+
const PROMPTBOOK_ENGINE_VERSION = '0.98.0-9';
|
|
30
30
|
/**
|
|
31
31
|
* TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
|
|
32
32
|
* Note: [๐] Ignore a discrepancy between file name and entity name
|
|
@@ -175,7 +175,7 @@ const DEFAULT_MAX_PARALLEL_COUNT = 5; // <- TODO: [๐คนโโ๏ธ]
|
|
|
175
175
|
*
|
|
176
176
|
* @public exported from `@promptbook/core`
|
|
177
177
|
*/
|
|
178
|
-
const DEFAULT_MAX_EXECUTION_ATTEMPTS =
|
|
178
|
+
const DEFAULT_MAX_EXECUTION_ATTEMPTS = 7; // <- TODO: [๐คนโโ๏ธ]
|
|
179
179
|
// <- TODO: [๐] Make also `BOOKS_DIRNAME_ALTERNATIVES`
|
|
180
180
|
// TODO: Just `.promptbook` in config, hardcode subfolders like `download-cache` or `execution-cache`
|
|
181
181
|
/**
|
|
@@ -2388,7 +2388,7 @@ function jsonParse(value) {
|
|
|
2388
2388
|
throw new Error(spaceTrim((block) => `
|
|
2389
2389
|
${block(error.message)}
|
|
2390
2390
|
|
|
2391
|
-
The JSON text:
|
|
2391
|
+
The expected JSON text:
|
|
2392
2392
|
${block(value)}
|
|
2393
2393
|
`));
|
|
2394
2394
|
}
|
|
@@ -4475,6 +4475,77 @@ function mapAvailableToExpectedParameters(options) {
|
|
|
4475
4475
|
return mappedParameters;
|
|
4476
4476
|
}
|
|
4477
4477
|
|
|
4478
|
+
/**
|
|
4479
|
+
* Replaces parameters in template with values from parameters object
|
|
4480
|
+
*
|
|
4481
|
+
* Note: This function is not places strings into string,
|
|
4482
|
+
* It's more complex and can handle this operation specifically for LLM models
|
|
4483
|
+
*
|
|
4484
|
+
* @param template the template with parameters in {curly} braces
|
|
4485
|
+
* @param parameters the object with parameters
|
|
4486
|
+
* @returns the template with replaced parameters
|
|
4487
|
+
* @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
|
|
4488
|
+
* @public exported from `@promptbook/utils`
|
|
4489
|
+
*/
|
|
4490
|
+
function templateParameters(template, parameters) {
|
|
4491
|
+
for (const [parameterName, parameterValue] of Object.entries(parameters)) {
|
|
4492
|
+
if (parameterValue === RESERVED_PARAMETER_MISSING_VALUE) {
|
|
4493
|
+
throw new UnexpectedError(`Parameter \`{${parameterName}}\` has missing value`);
|
|
4494
|
+
}
|
|
4495
|
+
else if (parameterValue === RESERVED_PARAMETER_RESTRICTED) {
|
|
4496
|
+
// TODO: [๐ต]
|
|
4497
|
+
throw new UnexpectedError(`Parameter \`{${parameterName}}\` is restricted to use`);
|
|
4498
|
+
}
|
|
4499
|
+
}
|
|
4500
|
+
let replacedTemplates = template;
|
|
4501
|
+
let match;
|
|
4502
|
+
let loopLimit = LOOP_LIMIT;
|
|
4503
|
+
while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
|
|
4504
|
+
.exec(replacedTemplates))) {
|
|
4505
|
+
if (loopLimit-- < 0) {
|
|
4506
|
+
throw new LimitReachedError('Loop limit reached during parameters replacement in `templateParameters`');
|
|
4507
|
+
}
|
|
4508
|
+
const precol = match.groups.precol;
|
|
4509
|
+
const parameterName = match.groups.parameterName;
|
|
4510
|
+
if (parameterName === '') {
|
|
4511
|
+
// Note: Skip empty placeholders. It's used to avoid confusion with JSON-like strings
|
|
4512
|
+
continue;
|
|
4513
|
+
}
|
|
4514
|
+
if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
|
|
4515
|
+
throw new PipelineExecutionError('Parameter is already opened or not closed');
|
|
4516
|
+
}
|
|
4517
|
+
if (parameters[parameterName] === undefined) {
|
|
4518
|
+
throw new PipelineExecutionError(`Parameter \`{${parameterName}}\` is not defined`);
|
|
4519
|
+
}
|
|
4520
|
+
let parameterValue = parameters[parameterName];
|
|
4521
|
+
if (parameterValue === undefined) {
|
|
4522
|
+
throw new PipelineExecutionError(`Parameter \`{${parameterName}}\` is not defined`);
|
|
4523
|
+
}
|
|
4524
|
+
parameterValue = valueToString(parameterValue);
|
|
4525
|
+
// Escape curly braces in parameter values to prevent prompt-injection
|
|
4526
|
+
parameterValue = parameterValue.replace(/[{}]/g, '\\$&');
|
|
4527
|
+
if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
|
|
4528
|
+
parameterValue = parameterValue
|
|
4529
|
+
.split('\n')
|
|
4530
|
+
.map((line, index) => (index === 0 ? line : `${precol}${line}`))
|
|
4531
|
+
.join('\n');
|
|
4532
|
+
}
|
|
4533
|
+
replacedTemplates =
|
|
4534
|
+
replacedTemplates.substring(0, match.index + precol.length) +
|
|
4535
|
+
parameterValue +
|
|
4536
|
+
replacedTemplates.substring(match.index + precol.length + parameterName.length + 2);
|
|
4537
|
+
}
|
|
4538
|
+
// [๐ซ] Check if there are parameters that are not closed properly
|
|
4539
|
+
if (/{\w+$/.test(replacedTemplates)) {
|
|
4540
|
+
throw new PipelineExecutionError('Parameter is not closed');
|
|
4541
|
+
}
|
|
4542
|
+
// [๐ซ] Check if there are parameters that are not opened properly
|
|
4543
|
+
if (/^\w+}/.test(replacedTemplates)) {
|
|
4544
|
+
throw new PipelineExecutionError('Parameter is not opened');
|
|
4545
|
+
}
|
|
4546
|
+
return replacedTemplates;
|
|
4547
|
+
}
|
|
4548
|
+
|
|
4478
4549
|
/**
|
|
4479
4550
|
* Extracts all code blocks from markdown.
|
|
4480
4551
|
*
|
|
@@ -4577,77 +4648,6 @@ function extractJsonBlock(markdown) {
|
|
|
4577
4648
|
* TODO: [๐ข] Make this logic part of `JsonFormatParser` or `isValidJsonString`
|
|
4578
4649
|
*/
|
|
4579
4650
|
|
|
4580
|
-
/**
|
|
4581
|
-
* Replaces parameters in template with values from parameters object
|
|
4582
|
-
*
|
|
4583
|
-
* Note: This function is not places strings into string,
|
|
4584
|
-
* It's more complex and can handle this operation specifically for LLM models
|
|
4585
|
-
*
|
|
4586
|
-
* @param template the template with parameters in {curly} braces
|
|
4587
|
-
* @param parameters the object with parameters
|
|
4588
|
-
* @returns the template with replaced parameters
|
|
4589
|
-
* @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
|
|
4590
|
-
* @public exported from `@promptbook/utils`
|
|
4591
|
-
*/
|
|
4592
|
-
function templateParameters(template, parameters) {
|
|
4593
|
-
for (const [parameterName, parameterValue] of Object.entries(parameters)) {
|
|
4594
|
-
if (parameterValue === RESERVED_PARAMETER_MISSING_VALUE) {
|
|
4595
|
-
throw new UnexpectedError(`Parameter \`{${parameterName}}\` has missing value`);
|
|
4596
|
-
}
|
|
4597
|
-
else if (parameterValue === RESERVED_PARAMETER_RESTRICTED) {
|
|
4598
|
-
// TODO: [๐ต]
|
|
4599
|
-
throw new UnexpectedError(`Parameter \`{${parameterName}}\` is restricted to use`);
|
|
4600
|
-
}
|
|
4601
|
-
}
|
|
4602
|
-
let replacedTemplates = template;
|
|
4603
|
-
let match;
|
|
4604
|
-
let loopLimit = LOOP_LIMIT;
|
|
4605
|
-
while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
|
|
4606
|
-
.exec(replacedTemplates))) {
|
|
4607
|
-
if (loopLimit-- < 0) {
|
|
4608
|
-
throw new LimitReachedError('Loop limit reached during parameters replacement in `templateParameters`');
|
|
4609
|
-
}
|
|
4610
|
-
const precol = match.groups.precol;
|
|
4611
|
-
const parameterName = match.groups.parameterName;
|
|
4612
|
-
if (parameterName === '') {
|
|
4613
|
-
// Note: Skip empty placeholders. It's used to avoid confusion with JSON-like strings
|
|
4614
|
-
continue;
|
|
4615
|
-
}
|
|
4616
|
-
if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
|
|
4617
|
-
throw new PipelineExecutionError('Parameter is already opened or not closed');
|
|
4618
|
-
}
|
|
4619
|
-
if (parameters[parameterName] === undefined) {
|
|
4620
|
-
throw new PipelineExecutionError(`Parameter \`{${parameterName}}\` is not defined`);
|
|
4621
|
-
}
|
|
4622
|
-
let parameterValue = parameters[parameterName];
|
|
4623
|
-
if (parameterValue === undefined) {
|
|
4624
|
-
throw new PipelineExecutionError(`Parameter \`{${parameterName}}\` is not defined`);
|
|
4625
|
-
}
|
|
4626
|
-
parameterValue = valueToString(parameterValue);
|
|
4627
|
-
// Escape curly braces in parameter values to prevent prompt-injection
|
|
4628
|
-
parameterValue = parameterValue.replace(/[{}]/g, '\\$&');
|
|
4629
|
-
if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
|
|
4630
|
-
parameterValue = parameterValue
|
|
4631
|
-
.split('\n')
|
|
4632
|
-
.map((line, index) => (index === 0 ? line : `${precol}${line}`))
|
|
4633
|
-
.join('\n');
|
|
4634
|
-
}
|
|
4635
|
-
replacedTemplates =
|
|
4636
|
-
replacedTemplates.substring(0, match.index + precol.length) +
|
|
4637
|
-
parameterValue +
|
|
4638
|
-
replacedTemplates.substring(match.index + precol.length + parameterName.length + 2);
|
|
4639
|
-
}
|
|
4640
|
-
// [๐ซ] Check if there are parameters that are not closed properly
|
|
4641
|
-
if (/{\w+$/.test(replacedTemplates)) {
|
|
4642
|
-
throw new PipelineExecutionError('Parameter is not closed');
|
|
4643
|
-
}
|
|
4644
|
-
// [๐ซ] Check if there are parameters that are not opened properly
|
|
4645
|
-
if (/^\w+}/.test(replacedTemplates)) {
|
|
4646
|
-
throw new PipelineExecutionError('Parameter is not opened');
|
|
4647
|
-
}
|
|
4648
|
-
return replacedTemplates;
|
|
4649
|
-
}
|
|
4650
|
-
|
|
4651
4651
|
/**
|
|
4652
4652
|
* Counts number of characters in the text
|
|
4653
4653
|
*
|
|
@@ -4808,6 +4808,68 @@ function checkExpectations(expectations, value) {
|
|
|
4808
4808
|
* Note: [๐] and [๐ค ] are interconnected together
|
|
4809
4809
|
*/
|
|
4810
4810
|
|
|
4811
|
+
/**
|
|
4812
|
+
* Validates a prompt result against expectations and format requirements.
|
|
4813
|
+
* This function provides a common abstraction for result validation that can be used
|
|
4814
|
+
* by both execution logic and caching logic to ensure consistency.
|
|
4815
|
+
*
|
|
4816
|
+
* @param options - The validation options including result string, expectations, and format
|
|
4817
|
+
* @returns Validation result with processed string and validity status
|
|
4818
|
+
* @private internal function of `createPipelineExecutor` and `cacheLlmTools`
|
|
4819
|
+
*/
|
|
4820
|
+
function validatePromptResult(options) {
|
|
4821
|
+
const { resultString, expectations, format } = options;
|
|
4822
|
+
let processedResultString = resultString;
|
|
4823
|
+
let validationError;
|
|
4824
|
+
try {
|
|
4825
|
+
// TODO: [๐] Unite object for expecting amount and format
|
|
4826
|
+
if (format) {
|
|
4827
|
+
if (format === 'JSON') {
|
|
4828
|
+
if (!isValidJsonString(processedResultString)) {
|
|
4829
|
+
// TODO: [๐ข] Do more universally via `FormatParser`
|
|
4830
|
+
try {
|
|
4831
|
+
processedResultString = extractJsonBlock(processedResultString);
|
|
4832
|
+
}
|
|
4833
|
+
catch (error) {
|
|
4834
|
+
keepUnused(error);
|
|
4835
|
+
throw new ExpectError(spaceTrim$1((block) => `
|
|
4836
|
+
Expected valid JSON string
|
|
4837
|
+
|
|
4838
|
+
The expected JSON text:
|
|
4839
|
+
${block(processedResultString)}
|
|
4840
|
+
`));
|
|
4841
|
+
}
|
|
4842
|
+
}
|
|
4843
|
+
}
|
|
4844
|
+
else {
|
|
4845
|
+
throw new UnexpectedError(`Unknown format "${format}"`);
|
|
4846
|
+
}
|
|
4847
|
+
}
|
|
4848
|
+
// TODO: [๐] Unite object for expecting amount and format
|
|
4849
|
+
if (expectations) {
|
|
4850
|
+
checkExpectations(expectations, processedResultString);
|
|
4851
|
+
}
|
|
4852
|
+
return {
|
|
4853
|
+
isValid: true,
|
|
4854
|
+
processedResultString,
|
|
4855
|
+
};
|
|
4856
|
+
}
|
|
4857
|
+
catch (error) {
|
|
4858
|
+
if (error instanceof ExpectError) {
|
|
4859
|
+
validationError = error;
|
|
4860
|
+
}
|
|
4861
|
+
else {
|
|
4862
|
+
// Re-throw non-ExpectError errors (like UnexpectedError)
|
|
4863
|
+
throw error;
|
|
4864
|
+
}
|
|
4865
|
+
return {
|
|
4866
|
+
isValid: false,
|
|
4867
|
+
processedResultString,
|
|
4868
|
+
error: validationError,
|
|
4869
|
+
};
|
|
4870
|
+
}
|
|
4871
|
+
}
|
|
4872
|
+
|
|
4811
4873
|
/**
|
|
4812
4874
|
* Executes a pipeline task with multiple attempts, including joker and retry logic. Handles different task types
|
|
4813
4875
|
* (prompt, script, dialog, etc.), applies postprocessing, checks expectations, and updates the execution report.
|
|
@@ -4830,13 +4892,13 @@ async function executeAttempts(options) {
|
|
|
4830
4892
|
// TODO: [๐] Make arrayable LLMs -> single LLM DRY
|
|
4831
4893
|
const _llms = arrayableToArray(tools.llm);
|
|
4832
4894
|
const llmTools = _llms.length === 1 ? _llms[0] : joinLlmExecutionTools(..._llms);
|
|
4833
|
-
attempts: for (let
|
|
4834
|
-
const isJokerAttempt =
|
|
4835
|
-
const jokerParameterName = jokerParameterNames[jokerParameterNames.length +
|
|
4895
|
+
attempts: for (let attemptIndex = -jokerParameterNames.length; attemptIndex < maxAttempts; attemptIndex++) {
|
|
4896
|
+
const isJokerAttempt = attemptIndex < 0;
|
|
4897
|
+
const jokerParameterName = jokerParameterNames[jokerParameterNames.length + attemptIndex];
|
|
4836
4898
|
// TODO: [๐ง ][๐ญ] JOKERS, EXPECTATIONS, POSTPROCESSING and FOREACH
|
|
4837
4899
|
if (isJokerAttempt && !jokerParameterName) {
|
|
4838
4900
|
throw new UnexpectedError(spaceTrim$1((block) => `
|
|
4839
|
-
Joker not found in attempt ${
|
|
4901
|
+
Joker not found in attempt ${attemptIndex}
|
|
4840
4902
|
|
|
4841
4903
|
${block(pipelineIdentification)}
|
|
4842
4904
|
`));
|
|
@@ -5034,35 +5096,18 @@ async function executeAttempts(options) {
|
|
|
5034
5096
|
}
|
|
5035
5097
|
}
|
|
5036
5098
|
// TODO: [๐] Unite object for expecting amount and format
|
|
5037
|
-
|
|
5038
|
-
|
|
5039
|
-
|
|
5040
|
-
|
|
5041
|
-
|
|
5042
|
-
|
|
5043
|
-
|
|
5044
|
-
|
|
5045
|
-
|
|
5046
|
-
throw new ExpectError(spaceTrim$1((block) => `
|
|
5047
|
-
Expected valid JSON string
|
|
5048
|
-
|
|
5049
|
-
${block(
|
|
5050
|
-
/*<- Note: No need for `pipelineIdentification`, it will be catched and added later */ '')}
|
|
5051
|
-
`));
|
|
5052
|
-
}
|
|
5053
|
-
}
|
|
5054
|
-
}
|
|
5055
|
-
else {
|
|
5056
|
-
throw new UnexpectedError(spaceTrim$1((block) => `
|
|
5057
|
-
Unknown format "${task.format}"
|
|
5058
|
-
|
|
5059
|
-
${block(pipelineIdentification)}
|
|
5060
|
-
`));
|
|
5099
|
+
// Use the common validation function for both format and expectations
|
|
5100
|
+
if (task.format || task.expectations) {
|
|
5101
|
+
const validationResult = validatePromptResult({
|
|
5102
|
+
resultString: $ongoingTaskResult.$resultString || '',
|
|
5103
|
+
expectations: task.expectations,
|
|
5104
|
+
format: task.format,
|
|
5105
|
+
});
|
|
5106
|
+
if (!validationResult.isValid) {
|
|
5107
|
+
throw validationResult.error;
|
|
5061
5108
|
}
|
|
5062
|
-
|
|
5063
|
-
|
|
5064
|
-
if (task.expectations) {
|
|
5065
|
-
checkExpectations(task.expectations, $ongoingTaskResult.$resultString || '');
|
|
5109
|
+
// Update the result string in case format processing modified it (e.g., JSON extraction)
|
|
5110
|
+
$ongoingTaskResult.$resultString = validationResult.processedResultString;
|
|
5066
5111
|
}
|
|
5067
5112
|
break attempts;
|
|
5068
5113
|
}
|
|
@@ -5076,6 +5121,7 @@ async function executeAttempts(options) {
|
|
|
5076
5121
|
$ongoingTaskResult.$failedResults = [];
|
|
5077
5122
|
}
|
|
5078
5123
|
$ongoingTaskResult.$failedResults.push({
|
|
5124
|
+
attemptIndex,
|
|
5079
5125
|
result: $ongoingTaskResult.$resultString,
|
|
5080
5126
|
error: error,
|
|
5081
5127
|
});
|
|
@@ -5100,19 +5146,13 @@ async function executeAttempts(options) {
|
|
|
5100
5146
|
});
|
|
5101
5147
|
}
|
|
5102
5148
|
}
|
|
5103
|
-
if ($ongoingTaskResult.$expectError !== null &&
|
|
5104
|
-
//
|
|
5105
|
-
$ongoingTaskResult.$failedResults = $ongoingTaskResult.$failedResults || [];
|
|
5106
|
-
$ongoingTaskResult.$failedResults.push({
|
|
5107
|
-
result: $ongoingTaskResult.$resultString,
|
|
5108
|
-
error: $ongoingTaskResult.$expectError,
|
|
5109
|
-
});
|
|
5110
|
-
// Create a summary of all failures
|
|
5149
|
+
if ($ongoingTaskResult.$expectError !== null && attemptIndex === maxAttempts - 1) {
|
|
5150
|
+
// Note: Create a summary of all failures
|
|
5111
5151
|
const failuresSummary = $ongoingTaskResult.$failedResults
|
|
5112
|
-
.map((failure
|
|
5152
|
+
.map((failure) => spaceTrim$1((block) => {
|
|
5113
5153
|
var _a, _b;
|
|
5114
5154
|
return `
|
|
5115
|
-
Attempt ${
|
|
5155
|
+
Attempt ${failure.attemptIndex + 1}:
|
|
5116
5156
|
Error ${((_a = failure.error) === null || _a === void 0 ? void 0 : _a.name) || ''}:
|
|
5117
5157
|
${block((_b = failure.error) === null || _b === void 0 ? void 0 : _b.message.split('\n').map((line) => `> ${line}`).join('\n'))}
|
|
5118
5158
|
|