@promptbook/pdf 0.98.0-6 โ 0.98.0-8
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-8';
|
|
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
|
/**
|
|
@@ -2401,7 +2401,7 @@ function jsonParse(value) {
|
|
|
2401
2401
|
throw new Error(spaceTrim((block) => `
|
|
2402
2402
|
${block(error.message)}
|
|
2403
2403
|
|
|
2404
|
-
The JSON text:
|
|
2404
|
+
The expected JSON text:
|
|
2405
2405
|
${block(value)}
|
|
2406
2406
|
`));
|
|
2407
2407
|
}
|
|
@@ -4488,6 +4488,77 @@ function mapAvailableToExpectedParameters(options) {
|
|
|
4488
4488
|
return mappedParameters;
|
|
4489
4489
|
}
|
|
4490
4490
|
|
|
4491
|
+
/**
|
|
4492
|
+
* Replaces parameters in template with values from parameters object
|
|
4493
|
+
*
|
|
4494
|
+
* Note: This function is not places strings into string,
|
|
4495
|
+
* It's more complex and can handle this operation specifically for LLM models
|
|
4496
|
+
*
|
|
4497
|
+
* @param template the template with parameters in {curly} braces
|
|
4498
|
+
* @param parameters the object with parameters
|
|
4499
|
+
* @returns the template with replaced parameters
|
|
4500
|
+
* @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
|
|
4501
|
+
* @public exported from `@promptbook/utils`
|
|
4502
|
+
*/
|
|
4503
|
+
function templateParameters(template, parameters) {
|
|
4504
|
+
for (const [parameterName, parameterValue] of Object.entries(parameters)) {
|
|
4505
|
+
if (parameterValue === RESERVED_PARAMETER_MISSING_VALUE) {
|
|
4506
|
+
throw new UnexpectedError(`Parameter \`{${parameterName}}\` has missing value`);
|
|
4507
|
+
}
|
|
4508
|
+
else if (parameterValue === RESERVED_PARAMETER_RESTRICTED) {
|
|
4509
|
+
// TODO: [๐ต]
|
|
4510
|
+
throw new UnexpectedError(`Parameter \`{${parameterName}}\` is restricted to use`);
|
|
4511
|
+
}
|
|
4512
|
+
}
|
|
4513
|
+
let replacedTemplates = template;
|
|
4514
|
+
let match;
|
|
4515
|
+
let loopLimit = LOOP_LIMIT;
|
|
4516
|
+
while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
|
|
4517
|
+
.exec(replacedTemplates))) {
|
|
4518
|
+
if (loopLimit-- < 0) {
|
|
4519
|
+
throw new LimitReachedError('Loop limit reached during parameters replacement in `templateParameters`');
|
|
4520
|
+
}
|
|
4521
|
+
const precol = match.groups.precol;
|
|
4522
|
+
const parameterName = match.groups.parameterName;
|
|
4523
|
+
if (parameterName === '') {
|
|
4524
|
+
// Note: Skip empty placeholders. It's used to avoid confusion with JSON-like strings
|
|
4525
|
+
continue;
|
|
4526
|
+
}
|
|
4527
|
+
if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
|
|
4528
|
+
throw new PipelineExecutionError('Parameter is already opened or not closed');
|
|
4529
|
+
}
|
|
4530
|
+
if (parameters[parameterName] === undefined) {
|
|
4531
|
+
throw new PipelineExecutionError(`Parameter \`{${parameterName}}\` is not defined`);
|
|
4532
|
+
}
|
|
4533
|
+
let parameterValue = parameters[parameterName];
|
|
4534
|
+
if (parameterValue === undefined) {
|
|
4535
|
+
throw new PipelineExecutionError(`Parameter \`{${parameterName}}\` is not defined`);
|
|
4536
|
+
}
|
|
4537
|
+
parameterValue = valueToString(parameterValue);
|
|
4538
|
+
// Escape curly braces in parameter values to prevent prompt-injection
|
|
4539
|
+
parameterValue = parameterValue.replace(/[{}]/g, '\\$&');
|
|
4540
|
+
if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
|
|
4541
|
+
parameterValue = parameterValue
|
|
4542
|
+
.split('\n')
|
|
4543
|
+
.map((line, index) => (index === 0 ? line : `${precol}${line}`))
|
|
4544
|
+
.join('\n');
|
|
4545
|
+
}
|
|
4546
|
+
replacedTemplates =
|
|
4547
|
+
replacedTemplates.substring(0, match.index + precol.length) +
|
|
4548
|
+
parameterValue +
|
|
4549
|
+
replacedTemplates.substring(match.index + precol.length + parameterName.length + 2);
|
|
4550
|
+
}
|
|
4551
|
+
// [๐ซ] Check if there are parameters that are not closed properly
|
|
4552
|
+
if (/{\w+$/.test(replacedTemplates)) {
|
|
4553
|
+
throw new PipelineExecutionError('Parameter is not closed');
|
|
4554
|
+
}
|
|
4555
|
+
// [๐ซ] Check if there are parameters that are not opened properly
|
|
4556
|
+
if (/^\w+}/.test(replacedTemplates)) {
|
|
4557
|
+
throw new PipelineExecutionError('Parameter is not opened');
|
|
4558
|
+
}
|
|
4559
|
+
return replacedTemplates;
|
|
4560
|
+
}
|
|
4561
|
+
|
|
4491
4562
|
/**
|
|
4492
4563
|
* Extracts all code blocks from markdown.
|
|
4493
4564
|
*
|
|
@@ -4590,77 +4661,6 @@ function extractJsonBlock(markdown) {
|
|
|
4590
4661
|
* TODO: [๐ข] Make this logic part of `JsonFormatParser` or `isValidJsonString`
|
|
4591
4662
|
*/
|
|
4592
4663
|
|
|
4593
|
-
/**
|
|
4594
|
-
* Replaces parameters in template with values from parameters object
|
|
4595
|
-
*
|
|
4596
|
-
* Note: This function is not places strings into string,
|
|
4597
|
-
* It's more complex and can handle this operation specifically for LLM models
|
|
4598
|
-
*
|
|
4599
|
-
* @param template the template with parameters in {curly} braces
|
|
4600
|
-
* @param parameters the object with parameters
|
|
4601
|
-
* @returns the template with replaced parameters
|
|
4602
|
-
* @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
|
|
4603
|
-
* @public exported from `@promptbook/utils`
|
|
4604
|
-
*/
|
|
4605
|
-
function templateParameters(template, parameters) {
|
|
4606
|
-
for (const [parameterName, parameterValue] of Object.entries(parameters)) {
|
|
4607
|
-
if (parameterValue === RESERVED_PARAMETER_MISSING_VALUE) {
|
|
4608
|
-
throw new UnexpectedError(`Parameter \`{${parameterName}}\` has missing value`);
|
|
4609
|
-
}
|
|
4610
|
-
else if (parameterValue === RESERVED_PARAMETER_RESTRICTED) {
|
|
4611
|
-
// TODO: [๐ต]
|
|
4612
|
-
throw new UnexpectedError(`Parameter \`{${parameterName}}\` is restricted to use`);
|
|
4613
|
-
}
|
|
4614
|
-
}
|
|
4615
|
-
let replacedTemplates = template;
|
|
4616
|
-
let match;
|
|
4617
|
-
let loopLimit = LOOP_LIMIT;
|
|
4618
|
-
while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
|
|
4619
|
-
.exec(replacedTemplates))) {
|
|
4620
|
-
if (loopLimit-- < 0) {
|
|
4621
|
-
throw new LimitReachedError('Loop limit reached during parameters replacement in `templateParameters`');
|
|
4622
|
-
}
|
|
4623
|
-
const precol = match.groups.precol;
|
|
4624
|
-
const parameterName = match.groups.parameterName;
|
|
4625
|
-
if (parameterName === '') {
|
|
4626
|
-
// Note: Skip empty placeholders. It's used to avoid confusion with JSON-like strings
|
|
4627
|
-
continue;
|
|
4628
|
-
}
|
|
4629
|
-
if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
|
|
4630
|
-
throw new PipelineExecutionError('Parameter is already opened or not closed');
|
|
4631
|
-
}
|
|
4632
|
-
if (parameters[parameterName] === undefined) {
|
|
4633
|
-
throw new PipelineExecutionError(`Parameter \`{${parameterName}}\` is not defined`);
|
|
4634
|
-
}
|
|
4635
|
-
let parameterValue = parameters[parameterName];
|
|
4636
|
-
if (parameterValue === undefined) {
|
|
4637
|
-
throw new PipelineExecutionError(`Parameter \`{${parameterName}}\` is not defined`);
|
|
4638
|
-
}
|
|
4639
|
-
parameterValue = valueToString(parameterValue);
|
|
4640
|
-
// Escape curly braces in parameter values to prevent prompt-injection
|
|
4641
|
-
parameterValue = parameterValue.replace(/[{}]/g, '\\$&');
|
|
4642
|
-
if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
|
|
4643
|
-
parameterValue = parameterValue
|
|
4644
|
-
.split('\n')
|
|
4645
|
-
.map((line, index) => (index === 0 ? line : `${precol}${line}`))
|
|
4646
|
-
.join('\n');
|
|
4647
|
-
}
|
|
4648
|
-
replacedTemplates =
|
|
4649
|
-
replacedTemplates.substring(0, match.index + precol.length) +
|
|
4650
|
-
parameterValue +
|
|
4651
|
-
replacedTemplates.substring(match.index + precol.length + parameterName.length + 2);
|
|
4652
|
-
}
|
|
4653
|
-
// [๐ซ] Check if there are parameters that are not closed properly
|
|
4654
|
-
if (/{\w+$/.test(replacedTemplates)) {
|
|
4655
|
-
throw new PipelineExecutionError('Parameter is not closed');
|
|
4656
|
-
}
|
|
4657
|
-
// [๐ซ] Check if there are parameters that are not opened properly
|
|
4658
|
-
if (/^\w+}/.test(replacedTemplates)) {
|
|
4659
|
-
throw new PipelineExecutionError('Parameter is not opened');
|
|
4660
|
-
}
|
|
4661
|
-
return replacedTemplates;
|
|
4662
|
-
}
|
|
4663
|
-
|
|
4664
4664
|
/**
|
|
4665
4665
|
* Counts number of characters in the text
|
|
4666
4666
|
*
|
|
@@ -4821,6 +4821,68 @@ function checkExpectations(expectations, value) {
|
|
|
4821
4821
|
* Note: [๐] and [๐ค ] are interconnected together
|
|
4822
4822
|
*/
|
|
4823
4823
|
|
|
4824
|
+
/**
|
|
4825
|
+
* Validates a prompt result against expectations and format requirements.
|
|
4826
|
+
* This function provides a common abstraction for result validation that can be used
|
|
4827
|
+
* by both execution logic and caching logic to ensure consistency.
|
|
4828
|
+
*
|
|
4829
|
+
* @param options - The validation options including result string, expectations, and format
|
|
4830
|
+
* @returns Validation result with processed string and validity status
|
|
4831
|
+
* @private internal function of `createPipelineExecutor` and `cacheLlmTools`
|
|
4832
|
+
*/
|
|
4833
|
+
function validatePromptResult(options) {
|
|
4834
|
+
const { resultString, expectations, format } = options;
|
|
4835
|
+
let processedResultString = resultString;
|
|
4836
|
+
let validationError;
|
|
4837
|
+
try {
|
|
4838
|
+
// TODO: [๐] Unite object for expecting amount and format
|
|
4839
|
+
if (format) {
|
|
4840
|
+
if (format === 'JSON') {
|
|
4841
|
+
if (!isValidJsonString(processedResultString)) {
|
|
4842
|
+
// TODO: [๐ข] Do more universally via `FormatParser`
|
|
4843
|
+
try {
|
|
4844
|
+
processedResultString = extractJsonBlock(processedResultString);
|
|
4845
|
+
}
|
|
4846
|
+
catch (error) {
|
|
4847
|
+
keepUnused(error);
|
|
4848
|
+
throw new ExpectError(spaceTrim$1((block) => `
|
|
4849
|
+
Expected valid JSON string
|
|
4850
|
+
|
|
4851
|
+
The expected JSON text:
|
|
4852
|
+
${block(processedResultString)}
|
|
4853
|
+
`));
|
|
4854
|
+
}
|
|
4855
|
+
}
|
|
4856
|
+
}
|
|
4857
|
+
else {
|
|
4858
|
+
throw new UnexpectedError(`Unknown format "${format}"`);
|
|
4859
|
+
}
|
|
4860
|
+
}
|
|
4861
|
+
// TODO: [๐] Unite object for expecting amount and format
|
|
4862
|
+
if (expectations) {
|
|
4863
|
+
checkExpectations(expectations, processedResultString);
|
|
4864
|
+
}
|
|
4865
|
+
return {
|
|
4866
|
+
isValid: true,
|
|
4867
|
+
processedResultString,
|
|
4868
|
+
};
|
|
4869
|
+
}
|
|
4870
|
+
catch (error) {
|
|
4871
|
+
if (error instanceof ExpectError) {
|
|
4872
|
+
validationError = error;
|
|
4873
|
+
}
|
|
4874
|
+
else {
|
|
4875
|
+
// Re-throw non-ExpectError errors (like UnexpectedError)
|
|
4876
|
+
throw error;
|
|
4877
|
+
}
|
|
4878
|
+
return {
|
|
4879
|
+
isValid: false,
|
|
4880
|
+
processedResultString,
|
|
4881
|
+
error: validationError,
|
|
4882
|
+
};
|
|
4883
|
+
}
|
|
4884
|
+
}
|
|
4885
|
+
|
|
4824
4886
|
/**
|
|
4825
4887
|
* Executes a pipeline task with multiple attempts, including joker and retry logic. Handles different task types
|
|
4826
4888
|
* (prompt, script, dialog, etc.), applies postprocessing, checks expectations, and updates the execution report.
|
|
@@ -4843,13 +4905,13 @@ async function executeAttempts(options) {
|
|
|
4843
4905
|
// TODO: [๐] Make arrayable LLMs -> single LLM DRY
|
|
4844
4906
|
const _llms = arrayableToArray(tools.llm);
|
|
4845
4907
|
const llmTools = _llms.length === 1 ? _llms[0] : joinLlmExecutionTools(..._llms);
|
|
4846
|
-
attempts: for (let
|
|
4847
|
-
const isJokerAttempt =
|
|
4848
|
-
const jokerParameterName = jokerParameterNames[jokerParameterNames.length +
|
|
4908
|
+
attempts: for (let attemptIndex = -jokerParameterNames.length; attemptIndex < maxAttempts; attemptIndex++) {
|
|
4909
|
+
const isJokerAttempt = attemptIndex < 0;
|
|
4910
|
+
const jokerParameterName = jokerParameterNames[jokerParameterNames.length + attemptIndex];
|
|
4849
4911
|
// TODO: [๐ง ][๐ญ] JOKERS, EXPECTATIONS, POSTPROCESSING and FOREACH
|
|
4850
4912
|
if (isJokerAttempt && !jokerParameterName) {
|
|
4851
4913
|
throw new UnexpectedError(spaceTrim$1((block) => `
|
|
4852
|
-
Joker not found in attempt ${
|
|
4914
|
+
Joker not found in attempt ${attemptIndex}
|
|
4853
4915
|
|
|
4854
4916
|
${block(pipelineIdentification)}
|
|
4855
4917
|
`));
|
|
@@ -5047,35 +5109,18 @@ async function executeAttempts(options) {
|
|
|
5047
5109
|
}
|
|
5048
5110
|
}
|
|
5049
5111
|
// TODO: [๐] Unite object for expecting amount and format
|
|
5050
|
-
|
|
5051
|
-
|
|
5052
|
-
|
|
5053
|
-
|
|
5054
|
-
|
|
5055
|
-
|
|
5056
|
-
|
|
5057
|
-
|
|
5058
|
-
|
|
5059
|
-
throw new ExpectError(spaceTrim$1((block) => `
|
|
5060
|
-
Expected valid JSON string
|
|
5061
|
-
|
|
5062
|
-
${block(
|
|
5063
|
-
/*<- Note: No need for `pipelineIdentification`, it will be catched and added later */ '')}
|
|
5064
|
-
`));
|
|
5065
|
-
}
|
|
5066
|
-
}
|
|
5067
|
-
}
|
|
5068
|
-
else {
|
|
5069
|
-
throw new UnexpectedError(spaceTrim$1((block) => `
|
|
5070
|
-
Unknown format "${task.format}"
|
|
5071
|
-
|
|
5072
|
-
${block(pipelineIdentification)}
|
|
5073
|
-
`));
|
|
5112
|
+
// Use the common validation function for both format and expectations
|
|
5113
|
+
if (task.format || task.expectations) {
|
|
5114
|
+
const validationResult = validatePromptResult({
|
|
5115
|
+
resultString: $ongoingTaskResult.$resultString || '',
|
|
5116
|
+
expectations: task.expectations,
|
|
5117
|
+
format: task.format,
|
|
5118
|
+
});
|
|
5119
|
+
if (!validationResult.isValid) {
|
|
5120
|
+
throw validationResult.error;
|
|
5074
5121
|
}
|
|
5075
|
-
|
|
5076
|
-
|
|
5077
|
-
if (task.expectations) {
|
|
5078
|
-
checkExpectations(task.expectations, $ongoingTaskResult.$resultString || '');
|
|
5122
|
+
// Update the result string in case format processing modified it (e.g., JSON extraction)
|
|
5123
|
+
$ongoingTaskResult.$resultString = validationResult.processedResultString;
|
|
5079
5124
|
}
|
|
5080
5125
|
break attempts;
|
|
5081
5126
|
}
|
|
@@ -5089,6 +5134,7 @@ async function executeAttempts(options) {
|
|
|
5089
5134
|
$ongoingTaskResult.$failedResults = [];
|
|
5090
5135
|
}
|
|
5091
5136
|
$ongoingTaskResult.$failedResults.push({
|
|
5137
|
+
attemptIndex,
|
|
5092
5138
|
result: $ongoingTaskResult.$resultString,
|
|
5093
5139
|
error: error,
|
|
5094
5140
|
});
|
|
@@ -5113,19 +5159,13 @@ async function executeAttempts(options) {
|
|
|
5113
5159
|
});
|
|
5114
5160
|
}
|
|
5115
5161
|
}
|
|
5116
|
-
if ($ongoingTaskResult.$expectError !== null &&
|
|
5117
|
-
//
|
|
5118
|
-
$ongoingTaskResult.$failedResults = $ongoingTaskResult.$failedResults || [];
|
|
5119
|
-
$ongoingTaskResult.$failedResults.push({
|
|
5120
|
-
result: $ongoingTaskResult.$resultString,
|
|
5121
|
-
error: $ongoingTaskResult.$expectError,
|
|
5122
|
-
});
|
|
5123
|
-
// Create a summary of all failures
|
|
5162
|
+
if ($ongoingTaskResult.$expectError !== null && attemptIndex === maxAttempts - 1) {
|
|
5163
|
+
// Note: Create a summary of all failures
|
|
5124
5164
|
const failuresSummary = $ongoingTaskResult.$failedResults
|
|
5125
|
-
.map((failure
|
|
5165
|
+
.map((failure) => spaceTrim$1((block) => {
|
|
5126
5166
|
var _a, _b;
|
|
5127
5167
|
return `
|
|
5128
|
-
Attempt ${
|
|
5168
|
+
Attempt ${failure.attemptIndex + 1}:
|
|
5129
5169
|
Error ${((_a = failure.error) === null || _a === void 0 ? void 0 : _a.name) || ''}:
|
|
5130
5170
|
${block((_b = failure.error) === null || _b === void 0 ? void 0 : _b.message.split('\n').map((line) => `> ${line}`).join('\n'))}
|
|
5131
5171
|
|