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