@promptbook/markdown-utils 0.112.0-39 β†’ 0.112.0-41

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.
Files changed (56) hide show
  1. package/README.md +23 -21
  2. package/esm/index.es.js +481 -288
  3. package/esm/index.es.js.map +1 -1
  4. package/esm/src/book-2.0/agent-source/createAgentModelRequirementsWithCommitments.d.ts +8 -2
  5. package/esm/src/book-components/Chat/Chat/ChatInputArea.d.ts +0 -10
  6. package/esm/src/book-components/Chat/Chat/ChatInputUploadedFile.d.ts +10 -0
  7. package/esm/src/book-components/Chat/Chat/ChatToolCallModalContent.d.ts +46 -0
  8. package/esm/src/book-components/Chat/Chat/resolveRunBrowserToolCallDetailsState.d.ts +146 -0
  9. package/esm/src/book-components/Chat/Chat/useChatInputAreaAttachments.d.ts +1 -1
  10. package/esm/src/book-components/Chat/Chat/useChatInputAreaComposer.d.ts +39 -0
  11. package/esm/src/book-components/Chat/Chat/useChatPostprocessedMessages.d.ts +17 -0
  12. package/esm/src/book-components/Chat/Chat/useChatScrollState.d.ts +34 -0
  13. package/esm/src/book-components/Chat/Chat/useChatToolCallModalState.d.ts +61 -0
  14. package/esm/src/book-components/Chat/Chat/useChatToolCallState.d.ts +35 -0
  15. package/esm/src/book-components/Chat/LlmChat/useLlmChatMessageHandler.d.ts +58 -0
  16. package/esm/src/book-components/Chat/LlmChat/useLlmChatMessages.d.ts +29 -0
  17. package/esm/src/book-components/Chat/LlmChat/useLlmChatState.d.ts +53 -0
  18. package/esm/src/collection/pipeline-collection/constructors/createPipelineCollectionFromDirectory.d.ts +7 -1
  19. package/esm/src/llm-providers/agent/AgentLlmExecutionTools.d.ts +52 -0
  20. package/esm/src/llm-providers/openai/OpenAiAssistantExecutionTools.d.ts +128 -0
  21. package/esm/src/llm-providers/openai/OpenAiCompatibleExecutionTools.d.ts +3 -3
  22. package/esm/src/llm-providers/openai/OpenAiVectorStoreHandler.d.ts +54 -0
  23. package/esm/src/llm-providers/openai/utils/OpenAiCompatibleUnsupportedParameterRetrier.d.ts +29 -0
  24. package/esm/src/llm-providers/openai/utils/callOpenAiCompatibleChatModel.d.ts +28 -0
  25. package/esm/src/types/number_usd.d.ts +1 -1
  26. package/esm/src/types/string_parameter_name.d.ts +2 -2
  27. package/esm/src/types/typeAliases.d.ts +2 -2
  28. package/esm/src/version.d.ts +1 -1
  29. package/package.json +1 -1
  30. package/umd/index.umd.js +481 -288
  31. package/umd/index.umd.js.map +1 -1
  32. package/umd/src/book-2.0/agent-source/createAgentModelRequirementsWithCommitments.d.ts +8 -2
  33. package/umd/src/book-components/Chat/Chat/ChatInputArea.d.ts +0 -10
  34. package/umd/src/book-components/Chat/Chat/ChatInputUploadedFile.d.ts +10 -0
  35. package/umd/src/book-components/Chat/Chat/ChatToolCallModalContent.d.ts +46 -0
  36. package/umd/src/book-components/Chat/Chat/resolveRunBrowserToolCallDetailsState.d.ts +146 -0
  37. package/umd/src/book-components/Chat/Chat/useChatInputAreaAttachments.d.ts +1 -1
  38. package/umd/src/book-components/Chat/Chat/useChatInputAreaComposer.d.ts +39 -0
  39. package/umd/src/book-components/Chat/Chat/useChatPostprocessedMessages.d.ts +17 -0
  40. package/umd/src/book-components/Chat/Chat/useChatScrollState.d.ts +34 -0
  41. package/umd/src/book-components/Chat/Chat/useChatToolCallModalState.d.ts +61 -0
  42. package/umd/src/book-components/Chat/Chat/useChatToolCallState.d.ts +35 -0
  43. package/umd/src/book-components/Chat/LlmChat/useLlmChatMessageHandler.d.ts +58 -0
  44. package/umd/src/book-components/Chat/LlmChat/useLlmChatMessages.d.ts +29 -0
  45. package/umd/src/book-components/Chat/LlmChat/useLlmChatState.d.ts +53 -0
  46. package/umd/src/collection/pipeline-collection/constructors/createPipelineCollectionFromDirectory.d.ts +7 -1
  47. package/umd/src/llm-providers/agent/AgentLlmExecutionTools.d.ts +52 -0
  48. package/umd/src/llm-providers/openai/OpenAiAssistantExecutionTools.d.ts +128 -0
  49. package/umd/src/llm-providers/openai/OpenAiCompatibleExecutionTools.d.ts +3 -3
  50. package/umd/src/llm-providers/openai/OpenAiVectorStoreHandler.d.ts +54 -0
  51. package/umd/src/llm-providers/openai/utils/OpenAiCompatibleUnsupportedParameterRetrier.d.ts +29 -0
  52. package/umd/src/llm-providers/openai/utils/callOpenAiCompatibleChatModel.d.ts +28 -0
  53. package/umd/src/types/number_usd.d.ts +1 -1
  54. package/umd/src/types/string_parameter_name.d.ts +2 -2
  55. package/umd/src/types/typeAliases.d.ts +2 -2
  56. package/umd/src/version.d.ts +1 -1
package/esm/index.es.js CHANGED
@@ -23,7 +23,7 @@ const BOOK_LANGUAGE_VERSION = '2.0.0';
23
23
  * @generated
24
24
  * @see https://github.com/webgptorg/promptbook
25
25
  */
26
- const PROMPTBOOK_ENGINE_VERSION = '0.112.0-39';
26
+ const PROMPTBOOK_ENGINE_VERSION = '0.112.0-41';
27
27
  /**
28
28
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
29
29
  * Note: [πŸ’ž] Ignore a discrepancy between file name and entity name
@@ -6085,317 +6085,511 @@ function validatePromptResult(options) {
6085
6085
  * @private internal utility of `createPipelineExecutor`
6086
6086
  */
6087
6087
  async function executeAttempts(options) {
6088
- const { jokerParameterNames, priority, maxAttempts, // <- Note: [πŸ’‚]
6089
- preparedContent, parameters, task, preparedPipeline, tools, $executionReport, pipelineIdentification, maxExecutionAttempts, onProgress, logLlmCall, } = options;
6090
- const $ongoingTaskResult = {
6088
+ const $ongoingTaskResult = createOngoingTaskResult();
6089
+ const llmTools = getSingleLlmExecutionTools(options.tools.llm);
6090
+ attempts: for (let attemptIndex = -options.jokerParameterNames.length; attemptIndex < options.maxAttempts; attemptIndex++) {
6091
+ const attempt = createAttemptDescriptor({
6092
+ attemptIndex,
6093
+ jokerParameterNames: options.jokerParameterNames,
6094
+ pipelineIdentification: options.pipelineIdentification,
6095
+ });
6096
+ resetAttemptExecutionState($ongoingTaskResult);
6097
+ try {
6098
+ await executeSingleAttempt({
6099
+ attempt,
6100
+ options,
6101
+ llmTools,
6102
+ $ongoingTaskResult,
6103
+ });
6104
+ break attempts;
6105
+ }
6106
+ catch (error) {
6107
+ if (!(error instanceof ExpectError)) {
6108
+ throw error;
6109
+ }
6110
+ recordFailedAttempt({
6111
+ error,
6112
+ attemptIndex,
6113
+ onProgress: options.onProgress,
6114
+ $ongoingTaskResult,
6115
+ });
6116
+ }
6117
+ finally {
6118
+ reportPromptExecution({
6119
+ attempt,
6120
+ task: options.task,
6121
+ $executionReport: options.$executionReport,
6122
+ logLlmCall: options.logLlmCall,
6123
+ $ongoingTaskResult,
6124
+ });
6125
+ }
6126
+ throwIfFinalAttemptFailed({
6127
+ attemptIndex,
6128
+ maxAttempts: options.maxAttempts,
6129
+ maxExecutionAttempts: options.maxExecutionAttempts,
6130
+ pipelineIdentification: options.pipelineIdentification,
6131
+ $ongoingTaskResult,
6132
+ });
6133
+ }
6134
+ return getSuccessfulResultString({
6135
+ pipelineIdentification: options.pipelineIdentification,
6136
+ $ongoingTaskResult,
6137
+ });
6138
+ }
6139
+ /**
6140
+ * Creates mutable attempt state for one task execution lifecycle.
6141
+ */
6142
+ function createOngoingTaskResult() {
6143
+ return {
6091
6144
  $result: null,
6092
6145
  $resultString: null,
6093
6146
  $expectError: null,
6094
6147
  $scriptPipelineExecutionErrors: [],
6095
- $failedResults: [], // Track all failed attempts
6148
+ $failedResults: [],
6096
6149
  };
6097
- const llmTools = getSingleLlmExecutionTools(tools.llm);
6098
- attempts: for (let attemptIndex = -jokerParameterNames.length; attemptIndex < maxAttempts; attemptIndex++) {
6099
- const isJokerAttempt = attemptIndex < 0;
6100
- const jokerParameterName = jokerParameterNames[jokerParameterNames.length + attemptIndex];
6101
- // TODO: [🧠][🍭] JOKERS, EXPECTATIONS, POSTPROCESSING and FOREACH
6102
- if (isJokerAttempt && !jokerParameterName) {
6103
- throw new UnexpectedError(spaceTrim$1((block) => `
6104
- Joker not found in attempt ${attemptIndex}
6150
+ }
6151
+ /**
6152
+ * Resolves the bookkeeping for one loop iteration, including joker lookup.
6153
+ */
6154
+ function createAttemptDescriptor(options) {
6155
+ const { attemptIndex, jokerParameterNames, pipelineIdentification } = options;
6156
+ const isJokerAttempt = attemptIndex < 0;
6157
+ const jokerParameterName = isJokerAttempt
6158
+ ? jokerParameterNames[jokerParameterNames.length + attemptIndex]
6159
+ : undefined;
6160
+ if (isJokerAttempt && !jokerParameterName) {
6161
+ throw new UnexpectedError(spaceTrim$1((block) => `
6162
+ Joker not found in attempt ${attemptIndex}
6105
6163
 
6106
- ${block(pipelineIdentification)}
6164
+ ${block(pipelineIdentification)}
6165
+ `));
6166
+ }
6167
+ return {
6168
+ attemptIndex,
6169
+ isJokerAttempt,
6170
+ jokerParameterName,
6171
+ };
6172
+ }
6173
+ /**
6174
+ * Clears the per-attempt result slots while preserving cumulative failure history.
6175
+ */
6176
+ function resetAttemptExecutionState($ongoingTaskResult) {
6177
+ $ongoingTaskResult.$result = null;
6178
+ $ongoingTaskResult.$resultString = null;
6179
+ $ongoingTaskResult.$expectError = null;
6180
+ }
6181
+ /**
6182
+ * Executes one loop iteration, from joker resolution or task execution through validation.
6183
+ */
6184
+ async function executeSingleAttempt(options) {
6185
+ const { attempt, options: executeAttemptsOptions, llmTools, $ongoingTaskResult } = options;
6186
+ if (attempt.isJokerAttempt) {
6187
+ resolveJokerAttemptResult({
6188
+ jokerParameterName: attempt.jokerParameterName,
6189
+ parameters: executeAttemptsOptions.parameters,
6190
+ pipelineIdentification: executeAttemptsOptions.pipelineIdentification,
6191
+ $ongoingTaskResult,
6192
+ });
6193
+ }
6194
+ else {
6195
+ await executeTaskAttempt({
6196
+ options: executeAttemptsOptions,
6197
+ llmTools,
6198
+ $ongoingTaskResult,
6199
+ });
6200
+ await applyPostprocessingFunctions({
6201
+ task: executeAttemptsOptions.task,
6202
+ tools: executeAttemptsOptions.tools,
6203
+ $ongoingTaskResult,
6204
+ });
6205
+ }
6206
+ validateAttemptResult({
6207
+ task: executeAttemptsOptions.task,
6208
+ $ongoingTaskResult,
6209
+ });
6210
+ }
6211
+ /**
6212
+ * Resolves the shortcut value used by a joker attempt.
6213
+ */
6214
+ function resolveJokerAttemptResult(options) {
6215
+ const { jokerParameterName, parameters, pipelineIdentification, $ongoingTaskResult } = options;
6216
+ if (parameters[jokerParameterName] === undefined) {
6217
+ throw new PipelineExecutionError(spaceTrim$1((block) => `
6218
+ Joker parameter {${jokerParameterName}} not defined
6219
+
6220
+ ${block(pipelineIdentification)}
6221
+ `));
6222
+ // <- TODO: This is maybe `PipelineLogicError` which should be detected in `validatePipeline` and here just thrown as `UnexpectedError`
6223
+ }
6224
+ $ongoingTaskResult.$resultString = parameters[jokerParameterName];
6225
+ }
6226
+ /**
6227
+ * Dispatches non-joker execution to the handler for the current task type.
6228
+ */
6229
+ async function executeTaskAttempt(options) {
6230
+ const { options: executeAttemptsOptions, llmTools, $ongoingTaskResult } = options;
6231
+ const { task } = executeAttemptsOptions;
6232
+ switch (task.taskType) {
6233
+ case 'SIMPLE_TASK':
6234
+ executeSimpleTaskAttempt({
6235
+ preparedContent: executeAttemptsOptions.preparedContent,
6236
+ parameters: executeAttemptsOptions.parameters,
6237
+ $ongoingTaskResult,
6238
+ });
6239
+ return;
6240
+ case 'PROMPT_TASK':
6241
+ await executePromptTaskAttempt({
6242
+ preparedPipeline: executeAttemptsOptions.preparedPipeline,
6243
+ task,
6244
+ parameters: executeAttemptsOptions.parameters,
6245
+ preparedContent: executeAttemptsOptions.preparedContent,
6246
+ pipelineIdentification: executeAttemptsOptions.pipelineIdentification,
6247
+ llmTools,
6248
+ $ongoingTaskResult,
6249
+ });
6250
+ return;
6251
+ case 'SCRIPT_TASK':
6252
+ await executeScriptTaskAttempt({
6253
+ tools: executeAttemptsOptions.tools,
6254
+ task,
6255
+ preparedContent: executeAttemptsOptions.preparedContent,
6256
+ parameters: executeAttemptsOptions.parameters,
6257
+ pipelineIdentification: executeAttemptsOptions.pipelineIdentification,
6258
+ $ongoingTaskResult,
6259
+ });
6260
+ return;
6261
+ case 'DIALOG_TASK':
6262
+ await executeDialogTaskAttempt({
6263
+ tools: executeAttemptsOptions.tools,
6264
+ task,
6265
+ parameters: executeAttemptsOptions.parameters,
6266
+ preparedContent: executeAttemptsOptions.preparedContent,
6267
+ priority: executeAttemptsOptions.priority,
6268
+ pipelineIdentification: executeAttemptsOptions.pipelineIdentification,
6269
+ $ongoingTaskResult,
6270
+ });
6271
+ return;
6272
+ default:
6273
+ throw new PipelineExecutionError(spaceTrim$1((block) => `
6274
+ Unknown execution type "${task.taskType}"
6275
+
6276
+ ${block(executeAttemptsOptions.pipelineIdentification)}
6107
6277
  `));
6108
- }
6109
- $ongoingTaskResult.$result = null;
6110
- $ongoingTaskResult.$resultString = null;
6111
- $ongoingTaskResult.$expectError = null;
6112
- if (isJokerAttempt) {
6113
- if (parameters[jokerParameterName] === undefined) {
6114
- throw new PipelineExecutionError(spaceTrim$1((block) => `
6115
- Joker parameter {${jokerParameterName}} not defined
6278
+ }
6279
+ }
6280
+ /**
6281
+ * Executes a simple templated task with no external tools.
6282
+ */
6283
+ function executeSimpleTaskAttempt(options) {
6284
+ const { preparedContent, parameters, $ongoingTaskResult } = options;
6285
+ $ongoingTaskResult.$resultString = templateParameters(preparedContent, parameters);
6286
+ }
6287
+ /**
6288
+ * Builds the prompt for a prompt task and calls the appropriate LLM variant.
6289
+ */
6290
+ async function executePromptTaskAttempt(options) {
6291
+ const { preparedPipeline, task, parameters, preparedContent, pipelineIdentification, llmTools, $ongoingTaskResult, } = options;
6292
+ const modelRequirements = {
6293
+ modelVariant: 'CHAT',
6294
+ ...(preparedPipeline.defaultModelRequirements || {}),
6295
+ ...(task.modelRequirements || {}),
6296
+ }; /* <- Note: [πŸ€›] */
6297
+ $ongoingTaskResult.$prompt = {
6298
+ title: task.title,
6299
+ pipelineUrl: `${preparedPipeline.pipelineUrl ? preparedPipeline.pipelineUrl : 'anonymous'}#${task.name
6300
+ // <- TODO: Here should be maybe also subformat index to distinguish between same task with different subformat values
6301
+ }`,
6302
+ parameters,
6303
+ content: preparedContent,
6304
+ modelRequirements,
6305
+ expectations: {
6306
+ ...(preparedPipeline.personas.find(({ name }) => name === task.personaName) || {}),
6307
+ ...task.expectations,
6308
+ },
6309
+ format: task.format,
6310
+ postprocessingFunctionNames: task.postprocessingFunctionNames,
6311
+ }; // <- TODO: Not very good type guard
6312
+ switch (modelRequirements.modelVariant) {
6313
+ case 'CHAT':
6314
+ $ongoingTaskResult.$chatResult = await llmTools.callChatModel(
6315
+ // <- TODO: [🧁] Check that `callChatModel` is defined
6316
+ $deepFreeze($ongoingTaskResult.$prompt));
6317
+ // TODO: [🍬] Destroy chatThread
6318
+ $ongoingTaskResult.$result = $ongoingTaskResult.$chatResult;
6319
+ $ongoingTaskResult.$resultString = $ongoingTaskResult.$chatResult.content;
6320
+ return;
6321
+ case 'COMPLETION':
6322
+ $ongoingTaskResult.$completionResult = await llmTools.callCompletionModel(
6323
+ // <- TODO: [🧁] Check that `callCompletionModel` is defined
6324
+ $deepFreeze($ongoingTaskResult.$prompt));
6325
+ $ongoingTaskResult.$result = $ongoingTaskResult.$completionResult;
6326
+ $ongoingTaskResult.$resultString = $ongoingTaskResult.$completionResult.content;
6327
+ return;
6328
+ case 'EMBEDDING':
6329
+ case 'IMAGE_GENERATION':
6330
+ throw new PipelineExecutionError(spaceTrim$1((block) => `
6331
+ ${modelRequirements.modelVariant} model can not be used in pipeline
6116
6332
 
6117
- ${block(pipelineIdentification)}
6118
- `));
6119
- // <- TODO: This is maybe `PipelineLogicError` which should be detected in `validatePipeline` and here just thrown as `UnexpectedError`
6120
- }
6121
- else {
6122
- $ongoingTaskResult.$resultString = parameters[jokerParameterName];
6123
- }
6124
- }
6125
- try {
6126
- if (!isJokerAttempt) {
6127
- taskType: switch (task.taskType) {
6128
- case 'SIMPLE_TASK':
6129
- $ongoingTaskResult.$resultString = templateParameters(preparedContent, parameters);
6130
- break taskType;
6131
- case 'PROMPT_TASK':
6132
- {
6133
- const modelRequirements = {
6134
- modelVariant: 'CHAT',
6135
- ...(preparedPipeline.defaultModelRequirements || {}),
6136
- ...(task.modelRequirements || {}),
6137
- }; /* <- Note: [πŸ€›] */
6138
- $ongoingTaskResult.$prompt = {
6139
- title: task.title,
6140
- pipelineUrl: `${preparedPipeline.pipelineUrl
6141
- ? preparedPipeline.pipelineUrl
6142
- : 'anonymous' /* <- TODO: [🧠] How to deal with anonymous pipelines, do here some auto-url like SHA-256 based ad-hoc identifier? */}#${task.name
6143
- // <- TODO: Here should be maybe also subformat index to distinguish between same task with different subformat values
6144
- }`,
6145
- parameters,
6146
- content: preparedContent,
6147
- modelRequirements,
6148
- expectations: {
6149
- ...(preparedPipeline.personas.find(({ name }) => name === task.personaName) || {}),
6150
- ...task.expectations,
6151
- },
6152
- format: task.format,
6153
- postprocessingFunctionNames: task.postprocessingFunctionNames,
6154
- }; // <- TODO: Not very good type guard
6155
- variant: switch (modelRequirements.modelVariant) {
6156
- case 'CHAT':
6157
- $ongoingTaskResult.$chatResult = await llmTools.callChatModel(
6158
- // <- TODO: [🧁] Check that `callChatModel` is defined
6159
- $deepFreeze($ongoingTaskResult.$prompt));
6160
- // TODO: [🍬] Destroy chatThread
6161
- $ongoingTaskResult.$result = $ongoingTaskResult.$chatResult;
6162
- $ongoingTaskResult.$resultString = $ongoingTaskResult.$chatResult.content;
6163
- break variant;
6164
- case 'COMPLETION':
6165
- $ongoingTaskResult.$completionResult = await llmTools.callCompletionModel(
6166
- // <- TODO: [🧁] Check that `callCompletionModel` is defined
6167
- $deepFreeze($ongoingTaskResult.$prompt));
6168
- $ongoingTaskResult.$result = $ongoingTaskResult.$completionResult;
6169
- $ongoingTaskResult.$resultString = $ongoingTaskResult.$completionResult.content;
6170
- break variant;
6171
- case 'EMBEDDING':
6172
- case 'IMAGE_GENERATION':
6173
- throw new PipelineExecutionError(spaceTrim$1((block) => `
6174
- ${modelRequirements.modelVariant} model can not be used in pipeline
6175
-
6176
- This should be catched during parsing
6177
-
6178
- ${block(pipelineIdentification)}
6179
-
6180
- `));
6181
- break variant;
6182
- // <- case [πŸ€–]:
6183
- default:
6184
- throw new PipelineExecutionError(spaceTrim$1((block) => `
6185
- Unknown model variant "${task.modelRequirements.modelVariant}"
6186
-
6187
- ${block(pipelineIdentification)}
6188
-
6189
- `));
6190
- }
6191
- }
6192
- break;
6193
- case 'SCRIPT_TASK':
6194
- if (arrayableToArray(tools.script).length === 0) {
6195
- throw new PipelineExecutionError(spaceTrim$1((block) => `
6196
- No script execution tools are available
6333
+ This should be catched during parsing
6197
6334
 
6198
- ${block(pipelineIdentification)}
6199
- `));
6200
- }
6201
- if (!task.contentLanguage) {
6202
- throw new PipelineExecutionError(spaceTrim$1((block) => `
6203
- Script language is not defined for SCRIPT TASK "${task.name}"
6335
+ ${block(pipelineIdentification)}
6204
6336
 
6205
- ${block(pipelineIdentification)}
6206
- `));
6207
- }
6208
- // TODO: DRY [☯]
6209
- scripts: for (const scriptTools of arrayableToArray(tools.script)) {
6210
- try {
6211
- $ongoingTaskResult.$resultString = await scriptTools.execute($deepFreeze({
6212
- scriptLanguage: task.contentLanguage,
6213
- script: preparedContent,
6214
- parameters,
6215
- }));
6216
- break scripts;
6217
- }
6218
- catch (error) {
6219
- assertsError(error);
6220
- if (error instanceof UnexpectedError) {
6221
- throw error;
6222
- }
6223
- $ongoingTaskResult.$scriptPipelineExecutionErrors.push(error);
6224
- }
6225
- }
6226
- if ($ongoingTaskResult.$resultString !== null) {
6227
- break taskType;
6228
- }
6229
- if ($ongoingTaskResult.$scriptPipelineExecutionErrors.length === 1) {
6230
- throw $ongoingTaskResult.$scriptPipelineExecutionErrors[0];
6231
- }
6232
- else {
6233
- throw new PipelineExecutionError(spaceTrim$1((block) => `
6234
- Script execution failed ${$ongoingTaskResult.$scriptPipelineExecutionErrors.length}x
6337
+ `));
6338
+ // <- case [πŸ€–]:
6339
+ default:
6340
+ throw new PipelineExecutionError(spaceTrim$1((block) => `
6341
+ Unknown model variant "${task.modelRequirements.modelVariant}"
6235
6342
 
6236
- ${block(pipelineIdentification)}
6343
+ ${block(pipelineIdentification)}
6237
6344
 
6238
- ${block($ongoingTaskResult.$scriptPipelineExecutionErrors
6239
- .map((error) => '- ' + error.message)
6240
- .join('\n\n'))}
6241
- `));
6242
- }
6243
- // Note: This line is unreachable because of the break taskType above
6244
- break taskType;
6245
- case 'DIALOG_TASK':
6246
- if (tools.userInterface === undefined) {
6247
- throw new PipelineExecutionError(spaceTrim$1((block) => `
6248
- User interface tools are not available
6345
+ `));
6346
+ }
6347
+ }
6348
+ /**
6349
+ * Executes a script task with the first script tool that succeeds.
6350
+ */
6351
+ async function executeScriptTaskAttempt(options) {
6352
+ const { tools, task, preparedContent, parameters, pipelineIdentification, $ongoingTaskResult } = options;
6353
+ const scriptExecutionTools = arrayableToArray(tools.script);
6354
+ if (scriptExecutionTools.length === 0) {
6355
+ throw new PipelineExecutionError(spaceTrim$1((block) => `
6356
+ No script execution tools are available
6249
6357
 
6250
- ${block(pipelineIdentification)}
6251
- `));
6252
- }
6253
- // TODO: [🌹] When making next attempt for `DIALOG TASK`, preserve the previous user input
6254
- $ongoingTaskResult.$resultString = await tools.userInterface.promptDialog($deepFreeze({
6255
- promptTitle: task.title,
6256
- promptMessage: templateParameters(task.description || '', parameters),
6257
- defaultValue: templateParameters(preparedContent, parameters),
6258
- // TODO: [🧠] Figure out how to define placeholder in .book.md file
6259
- placeholder: undefined,
6260
- priority,
6261
- }));
6262
- break taskType;
6263
- // <- case: [πŸ…±]
6264
- default:
6265
- throw new PipelineExecutionError(spaceTrim$1((block) => `
6266
- Unknown execution type "${task.taskType}"
6358
+ ${block(pipelineIdentification)}
6359
+ `));
6360
+ }
6361
+ if (!task.contentLanguage) {
6362
+ throw new PipelineExecutionError(spaceTrim$1((block) => `
6363
+ Script language is not defined for SCRIPT TASK "${task.name}"
6267
6364
 
6268
- ${block(pipelineIdentification)}
6269
- `));
6270
- }
6271
- }
6272
- if (!isJokerAttempt && task.postprocessingFunctionNames) {
6273
- for (const functionName of task.postprocessingFunctionNames) {
6274
- let postprocessingError = null;
6275
- scripts: for (const scriptTools of arrayableToArray(tools.script)) {
6276
- try {
6277
- $ongoingTaskResult.$resultString = await scriptTools.execute({
6278
- scriptLanguage: `javascript` /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
6279
- script: `${functionName}(resultString)`,
6280
- parameters: {
6281
- resultString: $ongoingTaskResult.$resultString || '',
6282
- // Note: No ...parametersForTask, because working with result only
6283
- },
6284
- });
6285
- postprocessingError = null;
6286
- break scripts;
6287
- }
6288
- catch (error) {
6289
- assertsError(error);
6290
- if (error instanceof UnexpectedError) {
6291
- throw error;
6292
- }
6293
- postprocessingError = error;
6294
- $ongoingTaskResult.$scriptPipelineExecutionErrors.push(error);
6295
- }
6296
- }
6297
- if (postprocessingError) {
6298
- throw postprocessingError;
6299
- }
6300
- }
6301
- }
6302
- // TODO: [πŸ’] Unite object for expecting amount and format
6303
- // Use the common validation function for both format and expectations
6304
- if (task.format || task.expectations) {
6305
- const validationResult = validatePromptResult({
6306
- resultString: $ongoingTaskResult.$resultString || '',
6307
- expectations: task.expectations,
6308
- format: task.format,
6309
- });
6310
- if (!validationResult.isValid) {
6311
- throw validationResult.error;
6312
- }
6313
- // Update the result string in case format processing modified it (e.g., JSON extraction)
6314
- $ongoingTaskResult.$resultString = validationResult.processedResultString;
6315
- }
6316
- break attempts;
6365
+ ${block(pipelineIdentification)}
6366
+ `));
6367
+ }
6368
+ for (const scriptTools of scriptExecutionTools) {
6369
+ try {
6370
+ $ongoingTaskResult.$resultString = await scriptTools.execute($deepFreeze({
6371
+ scriptLanguage: task.contentLanguage,
6372
+ script: preparedContent,
6373
+ parameters,
6374
+ }));
6375
+ return;
6317
6376
  }
6318
6377
  catch (error) {
6319
- if (!(error instanceof ExpectError)) {
6378
+ assertsError(error);
6379
+ if (error instanceof UnexpectedError) {
6320
6380
  throw error;
6321
6381
  }
6322
- $ongoingTaskResult.$expectError = error;
6323
- // Store each failed attempt
6324
- if (!Array.isArray($ongoingTaskResult.$failedResults)) {
6325
- $ongoingTaskResult.$failedResults = [];
6326
- }
6327
- $ongoingTaskResult.$failedResults.push({
6328
- attemptIndex,
6329
- result: $ongoingTaskResult.$resultString,
6330
- error: error,
6331
- });
6332
- // Report failed attempt
6333
- onProgress({
6334
- errors: [error],
6382
+ $ongoingTaskResult.$scriptPipelineExecutionErrors.push(error);
6383
+ }
6384
+ }
6385
+ throw createScriptExecutionFailure({
6386
+ pipelineIdentification,
6387
+ $ongoingTaskResult,
6388
+ });
6389
+ }
6390
+ /**
6391
+ * Creates the final script-task error after all script tools have failed.
6392
+ */
6393
+ function createScriptExecutionFailure(options) {
6394
+ const { pipelineIdentification, $ongoingTaskResult } = options;
6395
+ if ($ongoingTaskResult.$scriptPipelineExecutionErrors.length === 1) {
6396
+ return $ongoingTaskResult.$scriptPipelineExecutionErrors[0];
6397
+ }
6398
+ return new PipelineExecutionError(spaceTrim$1((block) => `
6399
+ Script execution failed ${$ongoingTaskResult.$scriptPipelineExecutionErrors.length}x
6400
+
6401
+ ${block(pipelineIdentification)}
6402
+
6403
+ ${block($ongoingTaskResult.$scriptPipelineExecutionErrors.map((error) => '- ' + error.message).join('\n\n'))}
6404
+ `));
6405
+ }
6406
+ /**
6407
+ * Executes a dialog task through the configured user-interface tools.
6408
+ */
6409
+ async function executeDialogTaskAttempt(options) {
6410
+ const { tools, task, parameters, preparedContent, priority, pipelineIdentification, $ongoingTaskResult } = options;
6411
+ if (tools.userInterface === undefined) {
6412
+ throw new PipelineExecutionError(spaceTrim$1((block) => `
6413
+ User interface tools are not available
6414
+
6415
+ ${block(pipelineIdentification)}
6416
+ `));
6417
+ }
6418
+ // TODO: [🌹] When making next attempt for `DIALOG TASK`, preserve the previous user input
6419
+ $ongoingTaskResult.$resultString = await tools.userInterface.promptDialog($deepFreeze({
6420
+ promptTitle: task.title,
6421
+ promptMessage: templateParameters(task.description || '', parameters),
6422
+ defaultValue: templateParameters(preparedContent, parameters),
6423
+ // TODO: [🧠] Figure out how to define placeholder in .book.md file
6424
+ placeholder: undefined,
6425
+ priority,
6426
+ }));
6427
+ }
6428
+ /**
6429
+ * Runs all configured postprocessing functions in order.
6430
+ */
6431
+ async function applyPostprocessingFunctions(options) {
6432
+ const { task } = options;
6433
+ if (!task.postprocessingFunctionNames) {
6434
+ return;
6435
+ }
6436
+ for (const functionName of task.postprocessingFunctionNames) {
6437
+ await executePostprocessingFunction({
6438
+ functionName,
6439
+ tools: options.tools,
6440
+ $ongoingTaskResult: options.$ongoingTaskResult,
6441
+ });
6442
+ }
6443
+ }
6444
+ /**
6445
+ * Executes one postprocessing function against the current result string.
6446
+ */
6447
+ async function executePostprocessingFunction(options) {
6448
+ const { functionName, tools, $ongoingTaskResult } = options;
6449
+ let postprocessingError = null;
6450
+ for (const scriptTools of arrayableToArray(tools.script)) {
6451
+ try {
6452
+ $ongoingTaskResult.$resultString = await scriptTools.execute({
6453
+ scriptLanguage: `javascript` /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
6454
+ script: `${functionName}(resultString)`,
6455
+ parameters: {
6456
+ resultString: $ongoingTaskResult.$resultString || '',
6457
+ // Note: No ...parametersForTask, because working with result only
6458
+ },
6335
6459
  });
6460
+ postprocessingError = null;
6461
+ return;
6336
6462
  }
6337
- finally {
6338
- if (!isJokerAttempt && task.taskType === 'PROMPT_TASK' && $ongoingTaskResult.$prompt) {
6339
- // Note: [2] When some expected parameter is not defined, error will occur in templateParameters
6340
- // In that case we don’t want to make a report about it because it’s not a llm execution error
6341
- const executionPromptReport = {
6342
- prompt: {
6343
- ...$ongoingTaskResult.$prompt,
6344
- // <- TODO: [🧠] How to pick everyhing except `pipelineUrl`
6345
- },
6346
- result: $ongoingTaskResult.$result || undefined,
6347
- error: $ongoingTaskResult.$expectError === null
6348
- ? undefined
6349
- : serializeError($ongoingTaskResult.$expectError),
6350
- };
6351
- $executionReport.promptExecutions.push(executionPromptReport);
6352
- if (logLlmCall) {
6353
- logLlmCall({
6354
- modelName: 'model' /* <- TODO: How to get model name from the report */,
6355
- report: executionPromptReport,
6356
- });
6357
- }
6463
+ catch (error) {
6464
+ assertsError(error);
6465
+ if (error instanceof UnexpectedError) {
6466
+ throw error;
6358
6467
  }
6468
+ postprocessingError = error;
6469
+ $ongoingTaskResult.$scriptPipelineExecutionErrors.push(error);
6359
6470
  }
6360
- if ($ongoingTaskResult.$expectError !== null && attemptIndex === maxAttempts - 1) {
6361
- // Note: Create a summary of all failures
6362
- const failuresSummary = $ongoingTaskResult.$failedResults
6363
- .map((failure) => spaceTrim$1((block) => {
6364
- var _a, _b;
6365
- return `
6366
- Attempt ${failure.attemptIndex + 1}:
6367
- Error ${((_a = failure.error) === null || _a === void 0 ? void 0 : _a.name) || ''}:
6368
- ${block((_b = failure.error) === null || _b === void 0 ? void 0 : _b.message.split(/\r?\n/).map((line) => `> ${line}`).join('\n'))}
6369
-
6370
- Result:
6371
- ${block(failure.result === null
6372
- ? 'null'
6373
- : spaceTrim$1(failure.result)
6374
- .split(/\r?\n/)
6375
- .map((line) => `> ${line}`)
6376
- .join('\n'))}
6377
- `;
6378
- }))
6379
- .join('\n\n---\n\n');
6380
- throw new PipelineExecutionError(spaceTrim$1((block) => {
6381
- var _a;
6382
- return `
6383
- LLM execution failed ${maxExecutionAttempts}x
6471
+ }
6472
+ if (postprocessingError) {
6473
+ throw postprocessingError;
6474
+ }
6475
+ }
6476
+ /**
6477
+ * Validates the current result string against expectations and format constraints.
6478
+ */
6479
+ function validateAttemptResult(options) {
6480
+ const { task, $ongoingTaskResult } = options;
6481
+ if (!task.format && !task.expectations) {
6482
+ return;
6483
+ }
6484
+ // TODO: [πŸ’] Unite object for expecting amount and format
6485
+ // Use the common validation function for both format and expectations
6486
+ const validationResult = validatePromptResult({
6487
+ resultString: $ongoingTaskResult.$resultString || '',
6488
+ expectations: task.expectations,
6489
+ format: task.format,
6490
+ });
6491
+ if (!validationResult.isValid) {
6492
+ throw validationResult.error;
6493
+ }
6494
+ // Update the result string in case format processing modified it (e.g., JSON extraction)
6495
+ $ongoingTaskResult.$resultString = validationResult.processedResultString;
6496
+ }
6497
+ /**
6498
+ * Stores one failed attempt and reports the expectation error upstream.
6499
+ */
6500
+ function recordFailedAttempt(options) {
6501
+ const { error, attemptIndex, onProgress, $ongoingTaskResult } = options;
6502
+ $ongoingTaskResult.$expectError = error;
6503
+ $ongoingTaskResult.$failedResults.push({
6504
+ attemptIndex,
6505
+ result: $ongoingTaskResult.$resultString,
6506
+ error,
6507
+ });
6508
+ onProgress({
6509
+ errors: [error],
6510
+ });
6511
+ }
6512
+ /**
6513
+ * Appends the prompt execution report for prompt-task attempts.
6514
+ */
6515
+ function reportPromptExecution(options) {
6516
+ const { attempt, task, $executionReport, logLlmCall, $ongoingTaskResult } = options;
6517
+ if (attempt.isJokerAttempt || task.taskType !== 'PROMPT_TASK' || !$ongoingTaskResult.$prompt) {
6518
+ return;
6519
+ }
6520
+ // Note: [2] When some expected parameter is not defined, error will occur in templateParameters
6521
+ // In that case we don’t want to make a report about it because it’s not a llm execution error
6522
+ const executionPromptReport = {
6523
+ prompt: {
6524
+ ...$ongoingTaskResult.$prompt,
6525
+ // <- TODO: [🧠] How to pick everyhing except `pipelineUrl`
6526
+ },
6527
+ result: $ongoingTaskResult.$result || undefined,
6528
+ error: $ongoingTaskResult.$expectError === null ? undefined : serializeError($ongoingTaskResult.$expectError),
6529
+ };
6530
+ $executionReport.promptExecutions.push(executionPromptReport);
6531
+ if (logLlmCall) {
6532
+ logLlmCall({
6533
+ modelName: 'model' /* <- TODO: How to get model name from the report */,
6534
+ report: executionPromptReport,
6535
+ });
6536
+ }
6537
+ }
6538
+ /**
6539
+ * Throws the aggregated retry error after the last regular attempt fails expectations.
6540
+ */
6541
+ function throwIfFinalAttemptFailed(options) {
6542
+ const { attemptIndex, maxAttempts, maxExecutionAttempts, pipelineIdentification, $ongoingTaskResult } = options;
6543
+ if ($ongoingTaskResult.$expectError === null || attemptIndex !== maxAttempts - 1) {
6544
+ return;
6545
+ }
6546
+ throw new PipelineExecutionError(spaceTrim$1((block) => {
6547
+ var _a;
6548
+ return `
6549
+ LLM execution failed ${maxExecutionAttempts}x
6384
6550
 
6385
- ${block(pipelineIdentification)}
6551
+ ${block(pipelineIdentification)}
6386
6552
 
6387
- The Prompt:
6388
- ${block((((_a = $ongoingTaskResult.$prompt) === null || _a === void 0 ? void 0 : _a.content) || '')
6389
- .split(/\r?\n/)
6390
- .map((line) => `> ${line}`)
6391
- .join('\n'))}
6553
+ The Prompt:
6554
+ ${block(quoteMultilineText(((_a = $ongoingTaskResult.$prompt) === null || _a === void 0 ? void 0 : _a.content) || ''))}
6392
6555
 
6393
- All Failed Attempts:
6394
- ${block(failuresSummary)}
6395
- `;
6396
- }));
6397
- }
6398
- }
6556
+ All Failed Attempts:
6557
+ ${block(createFailuresSummary($ongoingTaskResult.$failedResults))}
6558
+ `;
6559
+ }));
6560
+ }
6561
+ /**
6562
+ * Renders the retry history into the aggregated final error body.
6563
+ */
6564
+ function createFailuresSummary($failedResults) {
6565
+ return $failedResults
6566
+ .map((failure) => spaceTrim$1((block) => {
6567
+ var _a, _b;
6568
+ return `
6569
+ Attempt ${failure.attemptIndex + 1}:
6570
+ Error ${((_a = failure.error) === null || _a === void 0 ? void 0 : _a.name) || ''}:
6571
+ ${block(quoteMultilineText(((_b = failure.error) === null || _b === void 0 ? void 0 : _b.message) || ''))}
6572
+
6573
+ Result:
6574
+ ${block(failure.result === null ? 'null' : quoteMultilineText(spaceTrim$1(failure.result)))}
6575
+ `;
6576
+ }))
6577
+ .join('\n\n---\n\n');
6578
+ }
6579
+ /**
6580
+ * Formats multiline text as a quoted markdown block.
6581
+ */
6582
+ function quoteMultilineText(text) {
6583
+ return text
6584
+ .split(/\r?\n/)
6585
+ .map((line) => `> ${line}`)
6586
+ .join('\n');
6587
+ }
6588
+ /**
6589
+ * Returns the successful result string or raises an unexpected internal-state error.
6590
+ */
6591
+ function getSuccessfulResultString(options) {
6592
+ const { pipelineIdentification, $ongoingTaskResult } = options;
6399
6593
  if ($ongoingTaskResult.$resultString === null) {
6400
6594
  throw new UnexpectedError(spaceTrim$1((block) => `
6401
6595
  Something went wrong and prompt result is null
@@ -6405,7 +6599,6 @@ async function executeAttempts(options) {
6405
6599
  }
6406
6600
  return $ongoingTaskResult.$resultString;
6407
6601
  }
6408
- // TODO: Break into smaller functions
6409
6602
 
6410
6603
  /**
6411
6604
  * Executes a pipeline task that requires mapping or iterating over subvalues of a parameter (such as rows in a CSV).