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