@promptbook/openai 0.102.0-1 → 0.102.0-11

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 (115) hide show
  1. package/esm/index.es.js +189 -74
  2. package/esm/index.es.js.map +1 -1
  3. package/esm/typings/books/index.d.ts +81 -0
  4. package/esm/typings/src/_packages/components.index.d.ts +21 -13
  5. package/esm/typings/src/_packages/markdown-utils.index.d.ts +4 -0
  6. package/esm/typings/src/_packages/types.index.d.ts +13 -9
  7. package/esm/typings/src/_packages/utils.index.d.ts +12 -14
  8. package/esm/typings/src/book-2.0/agent-source/createAgentModelRequirements.d.ts +1 -1
  9. package/esm/typings/src/book-2.0/utils/generateGravatarUrl.d.ts +1 -1
  10. package/esm/typings/src/book-2.0/utils/generatePlaceholderAgentProfileImageUrl.d.ts +1 -1
  11. package/esm/typings/src/book-components/BookEditor/BookEditor.d.ts +1 -1
  12. package/esm/typings/src/book-components/BookEditor/BookEditorInner.d.ts +1 -1
  13. package/esm/typings/src/book-components/Chat/Chat/Chat.d.ts +1 -5
  14. package/esm/typings/src/book-components/Chat/Chat/ChatMessageItem.d.ts +35 -0
  15. package/esm/typings/src/book-components/Chat/Chat/ChatProps.d.ts +29 -7
  16. package/esm/typings/src/book-components/Chat/Chat/constants.d.ts +7 -0
  17. package/esm/typings/src/book-components/Chat/LlmChat/LlmChatProps.d.ts +19 -1
  18. package/esm/typings/src/book-components/Chat/MockedChat/MockedChat.d.ts +1 -1
  19. package/esm/typings/src/book-components/Chat/MockedChat/constants.d.ts +1 -1
  20. package/esm/typings/src/book-components/Chat/save/_common/ChatSaveFormatDefinition.d.ts +13 -0
  21. package/esm/typings/src/book-components/Chat/save/_common/getChatSaveFormatDefinitions.d.ts +8 -0
  22. package/esm/typings/src/book-components/Chat/save/_common/string_chat_format_name.d.ts +6 -0
  23. package/esm/typings/src/book-components/Chat/save/html/htmlSaveFormatDefinition.d.ts +12 -0
  24. package/esm/typings/src/book-components/Chat/save/index.d.ts +39 -0
  25. package/esm/typings/src/book-components/Chat/save/json/jsonSaveFormatDefinition.d.ts +12 -0
  26. package/esm/typings/src/book-components/Chat/save/markdown/mdSaveFormatDefinition.d.ts +12 -0
  27. package/esm/typings/src/book-components/Chat/save/pdf/pdfSaveFormatDefinition.d.ts +12 -0
  28. package/esm/typings/src/book-components/Chat/save/text/txtSaveFormatDefinition.d.ts +12 -0
  29. package/esm/typings/src/book-components/Chat/types/ChatMessage.d.ts +31 -5
  30. package/esm/typings/src/book-components/Chat/types/ChatParticipant.d.ts +3 -3
  31. package/esm/typings/src/book-components/Chat/utils/exportChatHistory.d.ts +3 -0
  32. package/esm/typings/src/book-components/icons/AttachmentIcon.d.ts +11 -0
  33. package/esm/typings/src/book-components/icons/CloseIcon.d.ts +11 -0
  34. package/esm/typings/src/execution/LlmExecutionToolsConstructor.d.ts +1 -1
  35. package/esm/typings/src/execution/execution-report/countWorkingDuration.d.ts +1 -1
  36. package/esm/typings/src/llm-providers/_common/register/$llmToolsMetadataRegister.d.ts +1 -1
  37. package/esm/typings/src/llm-providers/_common/register/$llmToolsRegister.d.ts +1 -1
  38. package/esm/typings/src/llm-providers/_common/register/$registeredLlmToolsMessage.d.ts +1 -2
  39. package/esm/typings/src/llm-providers/_common/register/LlmToolsConfiguration.d.ts +1 -1
  40. package/esm/typings/src/llm-providers/_common/register/LlmToolsMetadata.d.ts +1 -1
  41. package/esm/typings/src/llm-providers/_multiple/joinLlmExecutionTools.d.ts +1 -1
  42. package/esm/typings/src/llm-providers/agent/register-configuration.d.ts +1 -1
  43. package/esm/typings/src/llm-providers/agent/register-constructor.d.ts +1 -1
  44. package/esm/typings/src/llm-providers/anthropic-claude/register-configuration.d.ts +1 -1
  45. package/esm/typings/src/llm-providers/anthropic-claude/register-constructor.d.ts +1 -1
  46. package/esm/typings/src/llm-providers/azure-openai/register-configuration.d.ts +1 -1
  47. package/esm/typings/src/llm-providers/azure-openai/register-constructor.d.ts +1 -1
  48. package/esm/typings/src/llm-providers/deepseek/register-configuration.d.ts +1 -1
  49. package/esm/typings/src/llm-providers/deepseek/register-constructor.d.ts +1 -1
  50. package/esm/typings/src/llm-providers/google/register-configuration.d.ts +1 -1
  51. package/esm/typings/src/llm-providers/google/register-constructor.d.ts +1 -1
  52. package/esm/typings/src/llm-providers/ollama/register-configuration.d.ts +1 -1
  53. package/esm/typings/src/llm-providers/ollama/register-constructor.d.ts +1 -1
  54. package/esm/typings/src/llm-providers/openai/OpenAiAssistantExecutionTools.d.ts +1 -3
  55. package/esm/typings/src/llm-providers/openai/OpenAiCompatibleExecutionTools.d.ts +5 -5
  56. package/esm/typings/src/llm-providers/openai/register-configuration.d.ts +1 -1
  57. package/esm/typings/src/llm-providers/openai/register-constructor.d.ts +1 -1
  58. package/esm/typings/src/remote-server/ui/ServerApp.d.ts +13 -0
  59. package/esm/typings/src/remote-server/ui/renderServerIndexHtml.d.ts +7 -0
  60. package/esm/typings/src/remote-server/ui/types.d.ts +16 -0
  61. package/esm/typings/src/scrapers/_boilerplate/register-constructor.d.ts +1 -1
  62. package/esm/typings/src/scrapers/_boilerplate/register-metadata.d.ts +1 -1
  63. package/esm/typings/src/scrapers/_common/register/$scrapersMetadataRegister.d.ts +1 -1
  64. package/esm/typings/src/scrapers/_common/register/$scrapersRegister.d.ts +1 -1
  65. package/esm/typings/src/scrapers/_common/register/ScraperAndConverterMetadata.d.ts +2 -4
  66. package/esm/typings/src/scrapers/_common/register/ScraperConstructor.d.ts +1 -1
  67. package/esm/typings/src/scrapers/document/register-constructor.d.ts +1 -1
  68. package/esm/typings/src/scrapers/document/register-metadata.d.ts +1 -1
  69. package/esm/typings/src/scrapers/document-legacy/register-constructor.d.ts +1 -1
  70. package/esm/typings/src/scrapers/document-legacy/register-metadata.d.ts +1 -1
  71. package/esm/typings/src/scrapers/markdown/register-constructor.d.ts +1 -1
  72. package/esm/typings/src/scrapers/markdown/register-metadata.d.ts +1 -1
  73. package/esm/typings/src/scrapers/markitdown/register-constructor.d.ts +1 -1
  74. package/esm/typings/src/scrapers/markitdown/register-metadata.d.ts +1 -1
  75. package/esm/typings/src/scrapers/pdf/register-constructor.d.ts +1 -1
  76. package/esm/typings/src/scrapers/pdf/register-metadata.d.ts +1 -1
  77. package/esm/typings/src/scrapers/website/register-constructor.d.ts +1 -1
  78. package/esm/typings/src/scrapers/website/register-metadata.d.ts +1 -1
  79. package/esm/typings/src/scripting/javascript/postprocessing-functions.d.ts +6 -6
  80. package/esm/typings/src/types/Prompt.d.ts +5 -0
  81. package/esm/typings/src/utils/markdown/createMarkdownChart.d.ts +1 -1
  82. package/esm/typings/src/utils/markdown/humanizeAiText.d.ts +1 -1
  83. package/esm/typings/src/utils/markdown/humanizeAiTextEllipsis.d.ts +1 -1
  84. package/esm/typings/src/utils/markdown/humanizeAiTextEmdashed.d.ts +1 -1
  85. package/esm/typings/src/utils/markdown/humanizeAiTextQuotes.d.ts +1 -1
  86. package/esm/typings/src/utils/markdown/humanizeAiTextWhitespace.d.ts +1 -1
  87. package/esm/typings/src/utils/markdown/prettifyMarkdown.d.ts +2 -0
  88. package/esm/typings/src/utils/markdown/promptbookifyAiText.d.ts +1 -1
  89. package/esm/typings/src/utils/{trimCodeBlock.d.ts → markdown/trimCodeBlock.d.ts} +1 -1
  90. package/esm/typings/src/utils/{trimEndOfCodeBlock.d.ts → markdown/trimEndOfCodeBlock.d.ts} +1 -1
  91. package/esm/typings/src/utils/{$Register.d.ts → misc/$Register.d.ts} +2 -2
  92. package/esm/typings/src/utils/{$getCurrentDate.d.ts → misc/$getCurrentDate.d.ts} +1 -1
  93. package/esm/typings/src/utils/{arrayableToArray.d.ts → misc/arrayableToArray.d.ts} +1 -1
  94. package/esm/typings/src/utils/misc/debounce.d.ts +5 -0
  95. package/esm/typings/src/utils/{emojis.d.ts → misc/emojis.d.ts} +1 -1
  96. package/esm/typings/src/utils/misc/injectCssModuleIntoShadowRoot.d.ts +20 -0
  97. package/esm/typings/src/version.d.ts +1 -1
  98. package/package.json +2 -2
  99. package/umd/index.umd.js +189 -74
  100. package/umd/index.umd.js.map +1 -1
  101. package/esm/typings/src/book-components/BookEditor/injectCssModuleIntoShadowRoot.d.ts +0 -11
  102. package/esm/typings/src/book-components/Chat/save/savePlugins.d.ts +0 -105
  103. /package/esm/typings/src/{utils/arrayableToArray.test.d.ts → book-2.0/agent-source/createAgentModelRequirementsWithCommitments.delete.test.d.ts} +0 -0
  104. /package/esm/typings/src/utils/{trimCodeBlock.test.d.ts → markdown/trimCodeBlock.test.d.ts} +0 -0
  105. /package/esm/typings/src/utils/{trimEndOfCodeBlock.test.d.ts → markdown/trimEndOfCodeBlock.test.d.ts} +0 -0
  106. /package/esm/typings/src/utils/{FromtoItems.d.ts → misc/FromtoItems.d.ts} +0 -0
  107. /package/esm/typings/src/utils/{markdown/prettifyMarkdown.test.d.ts → misc/arrayableToArray.test.d.ts} +0 -0
  108. /package/esm/typings/src/utils/{parseNumber.d.ts → misc/parseNumber.d.ts} +0 -0
  109. /package/esm/typings/src/utils/{parseNumber.test.d.ts → misc/parseNumber.test.d.ts} +0 -0
  110. /package/esm/typings/src/utils/{removeEmojis.d.ts → normalization/removeEmojis.d.ts} +0 -0
  111. /package/esm/typings/src/utils/{removeEmojis.test.d.ts → normalization/removeEmojis.test.d.ts} +0 -0
  112. /package/esm/typings/src/utils/{removeQuotes.d.ts → normalization/removeQuotes.d.ts} +0 -0
  113. /package/esm/typings/src/utils/{removeQuotes.test.d.ts → normalization/removeQuotes.test.d.ts} +0 -0
  114. /package/esm/typings/src/utils/{unwrapResult.d.ts → normalization/unwrapResult.d.ts} +0 -0
  115. /package/esm/typings/src/utils/{unwrapResult.test.d.ts → normalization/unwrapResult.test.d.ts} +0 -0
package/umd/index.umd.js CHANGED
@@ -25,7 +25,7 @@
25
25
  * @generated
26
26
  * @see https://github.com/webgptorg/promptbook
27
27
  */
28
- const PROMPTBOOK_ENGINE_VERSION = '0.102.0-1';
28
+ const PROMPTBOOK_ENGINE_VERSION = '0.102.0-11';
29
29
  /**
30
30
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
31
31
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -2010,6 +2010,7 @@
2010
2010
  * @public exported from `@promptbook/openai`
2011
2011
  */
2012
2012
  class OpenAiCompatibleExecutionTools {
2013
+ // Removed retriedUnsupportedParameters and attemptHistory instance fields
2013
2014
  /**
2014
2015
  * Creates OpenAI compatible Execution Tools.
2015
2016
  *
@@ -2021,10 +2022,6 @@
2021
2022
  * OpenAI API client.
2022
2023
  */
2023
2024
  this.client = null;
2024
- /**
2025
- * Tracks models and parameters that have already been retried to prevent infinite loops
2026
- */
2027
- this.retriedUnsupportedParameters = new Set();
2028
2025
  // TODO: Allow configuring rate limits via options
2029
2026
  this.limiter = new Bottleneck__default["default"]({
2030
2027
  minTime: 60000 / (this.options.maxRequestsPerMinute || DEFAULT_MAX_REQUESTS_PER_MINUTE),
@@ -2086,12 +2083,16 @@
2086
2083
  * Calls OpenAI compatible API to use a chat model.
2087
2084
  */
2088
2085
  async callChatModel(prompt) {
2089
- return this.callChatModelWithRetry(prompt, prompt.modelRequirements);
2086
+ // Deep clone prompt and modelRequirements to avoid mutation across calls
2087
+ const clonedPrompt = JSON.parse(JSON.stringify(prompt));
2088
+ // Use local Set for retried parameters to ensure independence and thread safety
2089
+ const retriedUnsupportedParameters = new Set();
2090
+ return this.callChatModelWithRetry(clonedPrompt, clonedPrompt.modelRequirements, [], retriedUnsupportedParameters);
2090
2091
  }
2091
2092
  /**
2092
2093
  * Internal method that handles parameter retry for chat model calls
2093
2094
  */
2094
- async callChatModelWithRetry(prompt, currentModelRequirements) {
2095
+ async callChatModelWithRetry(prompt, currentModelRequirements, attemptStack = [], retriedUnsupportedParameters = new Set()) {
2095
2096
  var _a;
2096
2097
  if (this.options.isVerbose) {
2097
2098
  console.info(`💬 ${this.title} callChatModel call`, { prompt, currentModelRequirements });
@@ -2118,6 +2119,14 @@
2118
2119
  // <- TODO: [🚸] Not all models are compatible with JSON mode
2119
2120
  // > 'response_format' of type 'json_object' is not supported with this model.
2120
2121
  const rawPromptContent = templateParameters(content, { ...parameters, modelName });
2122
+ // Convert thread to OpenAI format if present
2123
+ let threadMessages = [];
2124
+ if ('thread' in prompt && Array.isArray(prompt.thread)) {
2125
+ threadMessages = prompt.thread.map((msg) => ({
2126
+ role: msg.role === 'assistant' ? 'assistant' : 'user',
2127
+ content: msg.content,
2128
+ }));
2129
+ }
2121
2130
  const rawRequest = {
2122
2131
  ...modelSettings,
2123
2132
  messages: [
@@ -2129,6 +2138,7 @@
2129
2138
  content: currentModelRequirements.systemMessage,
2130
2139
  },
2131
2140
  ]),
2141
+ ...threadMessages,
2132
2142
  {
2133
2143
  role: 'user',
2134
2144
  content: rawPromptContent,
@@ -2189,6 +2199,17 @@
2189
2199
  assertsError(error);
2190
2200
  // Check if this is an unsupported parameter error
2191
2201
  if (!isUnsupportedParameterError(error)) {
2202
+ // If we have attemptStack, include it in the error message
2203
+ if (attemptStack.length > 0) {
2204
+ throw new PipelineExecutionError(`All attempts failed. Attempt history:\n` +
2205
+ attemptStack
2206
+ .map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
2207
+ (a.unsupportedParameter ? `, Stripped: ${a.unsupportedParameter}` : '') +
2208
+ `, Error: ${a.errorMessage}` +
2209
+ (a.stripped ? ' (stripped and retried)' : ''))
2210
+ .join('\n') +
2211
+ `\nFinal error: ${error.message}`);
2212
+ }
2192
2213
  throw error;
2193
2214
  }
2194
2215
  // Parse which parameter is unsupported
@@ -2201,34 +2222,54 @@
2201
2222
  }
2202
2223
  // Create a unique key for this model + parameter combination to prevent infinite loops
2203
2224
  const retryKey = `${modelName}-${unsupportedParameter}`;
2204
- if (this.retriedUnsupportedParameters.has(retryKey)) {
2205
- // Already retried this parameter, throw the error
2206
- if (this.options.isVerbose) {
2207
- console.warn(colors__default["default"].bgRed('Error'), `Parameter '${unsupportedParameter}' for model '${modelName}' already retried once, throwing error:`, error.message);
2208
- }
2209
- throw error;
2225
+ if (retriedUnsupportedParameters.has(retryKey)) {
2226
+ // Already retried this parameter, throw the error with attemptStack
2227
+ attemptStack.push({
2228
+ modelName,
2229
+ unsupportedParameter,
2230
+ errorMessage: error.message,
2231
+ stripped: true,
2232
+ });
2233
+ throw new PipelineExecutionError(`All attempts failed. Attempt history:\n` +
2234
+ attemptStack
2235
+ .map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
2236
+ (a.unsupportedParameter ? `, Stripped: ${a.unsupportedParameter}` : '') +
2237
+ `, Error: ${a.errorMessage}` +
2238
+ (a.stripped ? ' (stripped and retried)' : ''))
2239
+ .join('\n') +
2240
+ `\nFinal error: ${error.message}`);
2210
2241
  }
2211
2242
  // Mark this parameter as retried
2212
- this.retriedUnsupportedParameters.add(retryKey);
2243
+ retriedUnsupportedParameters.add(retryKey);
2213
2244
  // Log warning in verbose mode
2214
2245
  if (this.options.isVerbose) {
2215
2246
  console.warn(colors__default["default"].bgYellow('Warning'), `Removing unsupported parameter '${unsupportedParameter}' for model '${modelName}' and retrying request`);
2216
2247
  }
2248
+ // Add to attemptStack
2249
+ attemptStack.push({
2250
+ modelName,
2251
+ unsupportedParameter,
2252
+ errorMessage: error.message,
2253
+ stripped: true,
2254
+ });
2217
2255
  // Remove the unsupported parameter and retry
2218
2256
  const modifiedModelRequirements = removeUnsupportedModelRequirement(currentModelRequirements, unsupportedParameter);
2219
- return this.callChatModelWithRetry(prompt, modifiedModelRequirements);
2257
+ return this.callChatModelWithRetry(prompt, modifiedModelRequirements, attemptStack, retriedUnsupportedParameters);
2220
2258
  }
2221
2259
  }
2222
2260
  /**
2223
2261
  * Calls OpenAI API to use a complete model.
2224
2262
  */
2225
2263
  async callCompletionModel(prompt) {
2226
- return this.callCompletionModelWithRetry(prompt, prompt.modelRequirements);
2264
+ // Deep clone prompt and modelRequirements to avoid mutation across calls
2265
+ const clonedPrompt = JSON.parse(JSON.stringify(prompt));
2266
+ const retriedUnsupportedParameters = new Set();
2267
+ return this.callCompletionModelWithRetry(clonedPrompt, clonedPrompt.modelRequirements, [], retriedUnsupportedParameters);
2227
2268
  }
2228
2269
  /**
2229
2270
  * Internal method that handles parameter retry for completion model calls
2230
2271
  */
2231
- async callCompletionModelWithRetry(prompt, currentModelRequirements) {
2272
+ async callCompletionModelWithRetry(prompt, currentModelRequirements, attemptStack = [], retriedUnsupportedParameters = new Set()) {
2232
2273
  var _a;
2233
2274
  if (this.options.isVerbose) {
2234
2275
  console.info(`🖋 ${this.title} callCompletionModel call`, { prompt, currentModelRequirements });
@@ -2244,8 +2285,6 @@
2244
2285
  model: modelName,
2245
2286
  max_tokens: currentModelRequirements.maxTokens,
2246
2287
  temperature: currentModelRequirements.temperature,
2247
- // <- TODO: [🈁] Use `seed` here AND/OR use is `isDeterministic` for entire execution tools
2248
- // <- Note: [🧆]
2249
2288
  };
2250
2289
  const rawPromptContent = templateParameters(content, { ...parameters, modelName });
2251
2290
  const rawRequest = {
@@ -2275,7 +2314,6 @@
2275
2314
  throw new PipelineExecutionError(`No choises from ${this.title}`);
2276
2315
  }
2277
2316
  if (rawResponse.choices.length > 1) {
2278
- // TODO: This should be maybe only warning
2279
2317
  throw new PipelineExecutionError(`More than one choise from ${this.title}`);
2280
2318
  }
2281
2319
  const resultContent = rawResponse.choices[0].text;
@@ -2295,17 +2333,24 @@
2295
2333
  rawPromptContent,
2296
2334
  rawRequest,
2297
2335
  rawResponse,
2298
- // <- [🗯]
2299
2336
  },
2300
2337
  });
2301
2338
  }
2302
2339
  catch (error) {
2303
2340
  assertsError(error);
2304
- // Check if this is an unsupported parameter error
2305
2341
  if (!isUnsupportedParameterError(error)) {
2342
+ if (attemptStack.length > 0) {
2343
+ throw new PipelineExecutionError(`All attempts failed. Attempt history:\n` +
2344
+ attemptStack
2345
+ .map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
2346
+ (a.unsupportedParameter ? `, Stripped: ${a.unsupportedParameter}` : '') +
2347
+ `, Error: ${a.errorMessage}` +
2348
+ (a.stripped ? ' (stripped and retried)' : ''))
2349
+ .join('\n') +
2350
+ `\nFinal error: ${error.message}`);
2351
+ }
2306
2352
  throw error;
2307
2353
  }
2308
- // Parse which parameter is unsupported
2309
2354
  const unsupportedParameter = parseUnsupportedParameterError(error.message);
2310
2355
  if (!unsupportedParameter) {
2311
2356
  if (this.options.isVerbose) {
@@ -2313,40 +2358,59 @@
2313
2358
  }
2314
2359
  throw error;
2315
2360
  }
2316
- // Create a unique key for this model + parameter combination to prevent infinite loops
2317
2361
  const retryKey = `${modelName}-${unsupportedParameter}`;
2318
- if (this.retriedUnsupportedParameters.has(retryKey)) {
2319
- // Already retried this parameter, throw the error
2320
- if (this.options.isVerbose) {
2321
- console.warn(colors__default["default"].bgRed('Error'), `Parameter '${unsupportedParameter}' for model '${modelName}' already retried once, throwing error:`, error.message);
2322
- }
2323
- throw error;
2362
+ if (retriedUnsupportedParameters.has(retryKey)) {
2363
+ attemptStack.push({
2364
+ modelName,
2365
+ unsupportedParameter,
2366
+ errorMessage: error.message,
2367
+ stripped: true,
2368
+ });
2369
+ throw new PipelineExecutionError(`All attempts failed. Attempt history:\n` +
2370
+ attemptStack
2371
+ .map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
2372
+ (a.unsupportedParameter ? `, Stripped: ${a.unsupportedParameter}` : '') +
2373
+ `, Error: ${a.errorMessage}` +
2374
+ (a.stripped ? ' (stripped and retried)' : ''))
2375
+ .join('\n') +
2376
+ `\nFinal error: ${error.message}`);
2324
2377
  }
2325
- // Mark this parameter as retried
2326
- this.retriedUnsupportedParameters.add(retryKey);
2327
- // Log warning in verbose mode
2378
+ retriedUnsupportedParameters.add(retryKey);
2328
2379
  if (this.options.isVerbose) {
2329
2380
  console.warn(colors__default["default"].bgYellow('Warning'), `Removing unsupported parameter '${unsupportedParameter}' for model '${modelName}' and retrying request`);
2330
2381
  }
2331
- // Remove the unsupported parameter and retry
2382
+ attemptStack.push({
2383
+ modelName,
2384
+ unsupportedParameter,
2385
+ errorMessage: error.message,
2386
+ stripped: true,
2387
+ });
2332
2388
  const modifiedModelRequirements = removeUnsupportedModelRequirement(currentModelRequirements, unsupportedParameter);
2333
- return this.callCompletionModelWithRetry(prompt, modifiedModelRequirements);
2389
+ return this.callCompletionModelWithRetry(prompt, modifiedModelRequirements, attemptStack, retriedUnsupportedParameters);
2334
2390
  }
2335
2391
  }
2336
2392
  /**
2337
2393
  * Calls OpenAI compatible API to use a embedding model
2338
2394
  */
2339
2395
  async callEmbeddingModel(prompt) {
2396
+ // Deep clone prompt and modelRequirements to avoid mutation across calls
2397
+ const clonedPrompt = JSON.parse(JSON.stringify(prompt));
2398
+ const retriedUnsupportedParameters = new Set();
2399
+ return this.callEmbeddingModelWithRetry(clonedPrompt, clonedPrompt.modelRequirements, [], retriedUnsupportedParameters);
2400
+ }
2401
+ /**
2402
+ * Internal method that handles parameter retry for embedding model calls
2403
+ */
2404
+ async callEmbeddingModelWithRetry(prompt, currentModelRequirements, attemptStack = [], retriedUnsupportedParameters = new Set()) {
2340
2405
  if (this.options.isVerbose) {
2341
- console.info(`🖋 ${this.title} embedding call`, { prompt });
2406
+ console.info(`🖋 ${this.title} embedding call`, { prompt, currentModelRequirements });
2342
2407
  }
2343
- const { content, parameters, modelRequirements } = prompt;
2408
+ const { content, parameters } = prompt;
2344
2409
  const client = await this.getClient();
2345
- // TODO: [☂] Use here more modelRequirements
2346
- if (modelRequirements.modelVariant !== 'EMBEDDING') {
2410
+ if (currentModelRequirements.modelVariant !== 'EMBEDDING') {
2347
2411
  throw new PipelineExecutionError('Use embed only for EMBEDDING variant');
2348
2412
  }
2349
- const modelName = modelRequirements.modelName || this.getDefaultEmbeddingModel().modelName;
2413
+ const modelName = currentModelRequirements.modelName || this.getDefaultEmbeddingModel().modelName;
2350
2414
  const rawPromptContent = templateParameters(content, { ...parameters, modelName });
2351
2415
  const rawRequest = {
2352
2416
  input: rawPromptContent,
@@ -2356,44 +2420,95 @@
2356
2420
  if (this.options.isVerbose) {
2357
2421
  console.info(colors__default["default"].bgWhite('rawRequest'), JSON.stringify(rawRequest, null, 4));
2358
2422
  }
2359
- const rawResponse = await this.limiter
2360
- .schedule(() => this.makeRequestWithNetworkRetry(() => client.embeddings.create(rawRequest)))
2361
- .catch((error) => {
2362
- assertsError(error);
2423
+ try {
2424
+ const rawResponse = await this.limiter
2425
+ .schedule(() => this.makeRequestWithNetworkRetry(() => client.embeddings.create(rawRequest)))
2426
+ .catch((error) => {
2427
+ assertsError(error);
2428
+ if (this.options.isVerbose) {
2429
+ console.info(colors__default["default"].bgRed('error'), error);
2430
+ }
2431
+ throw error;
2432
+ });
2363
2433
  if (this.options.isVerbose) {
2364
- console.info(colors__default["default"].bgRed('error'), error);
2434
+ console.info(colors__default["default"].bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
2365
2435
  }
2366
- throw error;
2367
- });
2368
- if (this.options.isVerbose) {
2369
- console.info(colors__default["default"].bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
2436
+ const complete = $getCurrentDate();
2437
+ if (rawResponse.data.length !== 1) {
2438
+ throw new PipelineExecutionError(`Expected exactly 1 data item in response, got ${rawResponse.data.length}`);
2439
+ }
2440
+ const resultContent = rawResponse.data[0].embedding;
2441
+ const usage = this.computeUsage(content || '', '', rawResponse);
2442
+ return exportJson({
2443
+ name: 'promptResult',
2444
+ message: `Result of \`OpenAiCompatibleExecutionTools.callEmbeddingModel\``,
2445
+ order: [],
2446
+ value: {
2447
+ content: resultContent,
2448
+ modelName: rawResponse.model || modelName,
2449
+ timing: {
2450
+ start,
2451
+ complete,
2452
+ },
2453
+ usage,
2454
+ rawPromptContent,
2455
+ rawRequest,
2456
+ rawResponse,
2457
+ },
2458
+ });
2370
2459
  }
2371
- const complete = $getCurrentDate();
2372
- if (rawResponse.data.length !== 1) {
2373
- throw new PipelineExecutionError(`Expected exactly 1 data item in response, got ${rawResponse.data.length}`);
2460
+ catch (error) {
2461
+ assertsError(error);
2462
+ if (!isUnsupportedParameterError(error)) {
2463
+ if (attemptStack.length > 0) {
2464
+ throw new PipelineExecutionError(`All attempts failed. Attempt history:\n` +
2465
+ attemptStack
2466
+ .map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
2467
+ (a.unsupportedParameter ? `, Stripped: ${a.unsupportedParameter}` : '') +
2468
+ `, Error: ${a.errorMessage}` +
2469
+ (a.stripped ? ' (stripped and retried)' : ''))
2470
+ .join('\n') +
2471
+ `\nFinal error: ${error.message}`);
2472
+ }
2473
+ throw error;
2474
+ }
2475
+ const unsupportedParameter = parseUnsupportedParameterError(error.message);
2476
+ if (!unsupportedParameter) {
2477
+ if (this.options.isVerbose) {
2478
+ console.warn(colors__default["default"].bgYellow('Warning'), 'Could not parse unsupported parameter from error:', error.message);
2479
+ }
2480
+ throw error;
2481
+ }
2482
+ const retryKey = `${modelName}-${unsupportedParameter}`;
2483
+ if (retriedUnsupportedParameters.has(retryKey)) {
2484
+ attemptStack.push({
2485
+ modelName,
2486
+ unsupportedParameter,
2487
+ errorMessage: error.message,
2488
+ stripped: true,
2489
+ });
2490
+ throw new PipelineExecutionError(`All attempts failed. Attempt history:\n` +
2491
+ attemptStack
2492
+ .map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
2493
+ (a.unsupportedParameter ? `, Stripped: ${a.unsupportedParameter}` : '') +
2494
+ `, Error: ${a.errorMessage}` +
2495
+ (a.stripped ? ' (stripped and retried)' : ''))
2496
+ .join('\n') +
2497
+ `\nFinal error: ${error.message}`);
2498
+ }
2499
+ retriedUnsupportedParameters.add(retryKey);
2500
+ if (this.options.isVerbose) {
2501
+ console.warn(colors__default["default"].bgYellow('Warning'), `Removing unsupported parameter '${unsupportedParameter}' for model '${modelName}' and retrying request`);
2502
+ }
2503
+ attemptStack.push({
2504
+ modelName,
2505
+ unsupportedParameter,
2506
+ errorMessage: error.message,
2507
+ stripped: true,
2508
+ });
2509
+ const modifiedModelRequirements = removeUnsupportedModelRequirement(currentModelRequirements, unsupportedParameter);
2510
+ return this.callEmbeddingModelWithRetry(prompt, modifiedModelRequirements, attemptStack, retriedUnsupportedParameters);
2374
2511
  }
2375
- const resultContent = rawResponse.data[0].embedding;
2376
- const usage = this.computeUsage(content || '', '',
2377
- // <- Note: Embedding does not have result content
2378
- rawResponse);
2379
- return exportJson({
2380
- name: 'promptResult',
2381
- message: `Result of \`OpenAiCompatibleExecutionTools.callEmbeddingModel\``,
2382
- order: [],
2383
- value: {
2384
- content: resultContent,
2385
- modelName: rawResponse.model || modelName,
2386
- timing: {
2387
- start,
2388
- complete,
2389
- },
2390
- usage,
2391
- rawPromptContent,
2392
- rawRequest,
2393
- rawResponse,
2394
- // <- [🗯]
2395
- },
2396
- });
2397
2512
  }
2398
2513
  // <- Note: [🤖] callXxxModel
2399
2514
  /**