@llumiverse/drivers 0.25.0-dev.20260204.110528Z → 0.25.1-dev.20260210.145152Z

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 (224) hide show
  1. package/lib/cjs/adobe/firefly.js +120 -0
  2. package/lib/cjs/adobe/firefly.js.map +1 -0
  3. package/lib/cjs/azure/azure_foundry.js +432 -0
  4. package/lib/cjs/azure/azure_foundry.js.map +1 -0
  5. package/lib/cjs/bedrock/converse.js +285 -0
  6. package/lib/cjs/bedrock/converse.js.map +1 -0
  7. package/lib/cjs/bedrock/index.js +1091 -0
  8. package/lib/cjs/bedrock/index.js.map +1 -0
  9. package/lib/cjs/bedrock/nova-image-payload.js +207 -0
  10. package/lib/cjs/bedrock/nova-image-payload.js.map +1 -0
  11. package/lib/cjs/bedrock/payloads.js +3 -0
  12. package/lib/cjs/bedrock/payloads.js.map +1 -0
  13. package/lib/cjs/bedrock/s3.js +107 -0
  14. package/lib/cjs/bedrock/s3.js.map +1 -0
  15. package/lib/cjs/bedrock/twelvelabs.js +87 -0
  16. package/lib/cjs/bedrock/twelvelabs.js.map +1 -0
  17. package/lib/cjs/groq/index.js +323 -0
  18. package/lib/cjs/groq/index.js.map +1 -0
  19. package/lib/cjs/huggingface_ie.js +201 -0
  20. package/lib/cjs/huggingface_ie.js.map +1 -0
  21. package/lib/cjs/index.js +31 -0
  22. package/lib/cjs/index.js.map +1 -0
  23. package/lib/cjs/mistral/index.js +173 -0
  24. package/lib/cjs/mistral/index.js.map +1 -0
  25. package/lib/cjs/mistral/types.js +83 -0
  26. package/lib/cjs/mistral/types.js.map +1 -0
  27. package/lib/cjs/openai/azure_openai.js +72 -0
  28. package/lib/cjs/openai/azure_openai.js.map +1 -0
  29. package/lib/cjs/openai/index.js +665 -0
  30. package/lib/cjs/openai/index.js.map +1 -0
  31. package/lib/cjs/openai/openai.js +21 -0
  32. package/lib/cjs/openai/openai.js.map +1 -0
  33. package/lib/cjs/openai/openai_compatible.js +62 -0
  34. package/lib/cjs/openai/openai_compatible.js.map +1 -0
  35. package/lib/cjs/openai/openai_format.js +131 -0
  36. package/lib/cjs/openai/openai_format.js.map +1 -0
  37. package/lib/cjs/package.json +3 -0
  38. package/lib/cjs/replicate.js +275 -0
  39. package/lib/cjs/replicate.js.map +1 -0
  40. package/lib/cjs/test-driver/TestErrorCompletionStream.js +20 -0
  41. package/lib/cjs/test-driver/TestErrorCompletionStream.js.map +1 -0
  42. package/lib/cjs/test-driver/TestValidationErrorCompletionStream.js +24 -0
  43. package/lib/cjs/test-driver/TestValidationErrorCompletionStream.js.map +1 -0
  44. package/lib/cjs/test-driver/index.js +109 -0
  45. package/lib/cjs/test-driver/index.js.map +1 -0
  46. package/lib/cjs/test-driver/utils.js +30 -0
  47. package/lib/cjs/test-driver/utils.js.map +1 -0
  48. package/lib/cjs/togetherai/index.js +126 -0
  49. package/lib/cjs/togetherai/index.js.map +1 -0
  50. package/lib/cjs/togetherai/interfaces.js +3 -0
  51. package/lib/cjs/togetherai/interfaces.js.map +1 -0
  52. package/lib/cjs/vertexai/debug.js +12 -0
  53. package/lib/cjs/vertexai/debug.js.map +1 -0
  54. package/lib/cjs/vertexai/embeddings/embeddings-image.js +27 -0
  55. package/lib/cjs/vertexai/embeddings/embeddings-image.js.map +1 -0
  56. package/lib/cjs/vertexai/embeddings/embeddings-text.js +23 -0
  57. package/lib/cjs/vertexai/embeddings/embeddings-text.js.map +1 -0
  58. package/lib/cjs/vertexai/index.js +576 -0
  59. package/lib/cjs/vertexai/index.js.map +1 -0
  60. package/lib/cjs/vertexai/models/claude.js +485 -0
  61. package/lib/cjs/vertexai/models/claude.js.map +1 -0
  62. package/lib/cjs/vertexai/models/gemini.js +871 -0
  63. package/lib/cjs/vertexai/models/gemini.js.map +1 -0
  64. package/lib/cjs/vertexai/models/imagen.js +303 -0
  65. package/lib/cjs/vertexai/models/imagen.js.map +1 -0
  66. package/lib/cjs/vertexai/models/llama.js +183 -0
  67. package/lib/cjs/vertexai/models/llama.js.map +1 -0
  68. package/lib/cjs/vertexai/models.js +35 -0
  69. package/lib/cjs/vertexai/models.js.map +1 -0
  70. package/lib/cjs/watsonx/index.js +161 -0
  71. package/lib/cjs/watsonx/index.js.map +1 -0
  72. package/lib/cjs/watsonx/interfaces.js +3 -0
  73. package/lib/cjs/watsonx/interfaces.js.map +1 -0
  74. package/lib/cjs/xai/index.js +65 -0
  75. package/lib/cjs/xai/index.js.map +1 -0
  76. package/lib/esm/adobe/firefly.js +116 -0
  77. package/lib/esm/adobe/firefly.js.map +1 -0
  78. package/lib/esm/azure/azure_foundry.js +426 -0
  79. package/lib/esm/azure/azure_foundry.js.map +1 -0
  80. package/lib/esm/bedrock/converse.js +278 -0
  81. package/lib/esm/bedrock/converse.js.map +1 -0
  82. package/lib/esm/bedrock/index.js +1087 -0
  83. package/lib/esm/bedrock/index.js.map +1 -0
  84. package/lib/esm/bedrock/nova-image-payload.js +203 -0
  85. package/lib/esm/bedrock/nova-image-payload.js.map +1 -0
  86. package/lib/esm/bedrock/payloads.js +2 -0
  87. package/lib/esm/bedrock/payloads.js.map +1 -0
  88. package/lib/esm/bedrock/s3.js +99 -0
  89. package/lib/esm/bedrock/s3.js.map +1 -0
  90. package/lib/esm/bedrock/twelvelabs.js +84 -0
  91. package/lib/esm/bedrock/twelvelabs.js.map +1 -0
  92. package/lib/esm/groq/index.js +316 -0
  93. package/lib/esm/groq/index.js.map +1 -0
  94. package/lib/esm/huggingface_ie.js +197 -0
  95. package/lib/esm/huggingface_ie.js.map +1 -0
  96. package/lib/esm/index.js +15 -0
  97. package/lib/esm/index.js.map +1 -0
  98. package/lib/esm/mistral/index.js +169 -0
  99. package/lib/esm/mistral/index.js.map +1 -0
  100. package/lib/esm/mistral/types.js +80 -0
  101. package/lib/esm/mistral/types.js.map +1 -0
  102. package/lib/esm/openai/azure_openai.js +68 -0
  103. package/lib/esm/openai/azure_openai.js.map +1 -0
  104. package/lib/esm/openai/index.js +660 -0
  105. package/lib/esm/openai/index.js.map +1 -0
  106. package/lib/esm/openai/openai.js +14 -0
  107. package/lib/esm/openai/openai.js.map +1 -0
  108. package/lib/esm/openai/openai_compatible.js +55 -0
  109. package/lib/esm/openai/openai_compatible.js.map +1 -0
  110. package/lib/esm/openai/openai_format.js +127 -0
  111. package/lib/esm/openai/openai_format.js.map +1 -0
  112. package/lib/esm/replicate.js +268 -0
  113. package/lib/esm/replicate.js.map +1 -0
  114. package/lib/esm/test-driver/TestErrorCompletionStream.js +16 -0
  115. package/lib/esm/test-driver/TestErrorCompletionStream.js.map +1 -0
  116. package/lib/esm/test-driver/TestValidationErrorCompletionStream.js +20 -0
  117. package/lib/esm/test-driver/TestValidationErrorCompletionStream.js.map +1 -0
  118. package/lib/esm/test-driver/index.js +91 -0
  119. package/lib/esm/test-driver/index.js.map +1 -0
  120. package/lib/esm/test-driver/utils.js +25 -0
  121. package/lib/esm/test-driver/utils.js.map +1 -0
  122. package/lib/esm/togetherai/index.js +122 -0
  123. package/lib/esm/togetherai/index.js.map +1 -0
  124. package/lib/esm/togetherai/interfaces.js +2 -0
  125. package/lib/esm/togetherai/interfaces.js.map +1 -0
  126. package/lib/esm/vertexai/debug.js +6 -0
  127. package/lib/esm/vertexai/debug.js.map +1 -0
  128. package/lib/esm/vertexai/embeddings/embeddings-image.js +24 -0
  129. package/lib/esm/vertexai/embeddings/embeddings-image.js.map +1 -0
  130. package/lib/esm/vertexai/embeddings/embeddings-text.js +20 -0
  131. package/lib/esm/vertexai/embeddings/embeddings-text.js.map +1 -0
  132. package/lib/esm/vertexai/index.js +571 -0
  133. package/lib/esm/vertexai/index.js.map +1 -0
  134. package/lib/esm/vertexai/models/claude.js +479 -0
  135. package/lib/esm/vertexai/models/claude.js.map +1 -0
  136. package/lib/esm/vertexai/models/gemini.js +866 -0
  137. package/lib/esm/vertexai/models/gemini.js.map +1 -0
  138. package/lib/esm/vertexai/models/imagen.js +299 -0
  139. package/lib/esm/vertexai/models/imagen.js.map +1 -0
  140. package/lib/esm/vertexai/models/llama.js +179 -0
  141. package/lib/esm/vertexai/models/llama.js.map +1 -0
  142. package/lib/esm/vertexai/models.js +32 -0
  143. package/lib/esm/vertexai/models.js.map +1 -0
  144. package/lib/esm/watsonx/index.js +157 -0
  145. package/lib/esm/watsonx/index.js.map +1 -0
  146. package/lib/esm/watsonx/interfaces.js +2 -0
  147. package/lib/esm/watsonx/interfaces.js.map +1 -0
  148. package/lib/esm/xai/index.js +58 -0
  149. package/lib/esm/xai/index.js.map +1 -0
  150. package/lib/types/adobe/firefly.d.ts +30 -0
  151. package/lib/types/adobe/firefly.d.ts.map +1 -0
  152. package/lib/types/azure/azure_foundry.d.ts +52 -0
  153. package/lib/types/azure/azure_foundry.d.ts.map +1 -0
  154. package/lib/types/bedrock/converse.d.ts +9 -0
  155. package/lib/types/bedrock/converse.d.ts.map +1 -0
  156. package/lib/types/bedrock/index.d.ts +68 -0
  157. package/lib/types/bedrock/index.d.ts.map +1 -0
  158. package/lib/types/bedrock/nova-image-payload.d.ts +74 -0
  159. package/lib/types/bedrock/nova-image-payload.d.ts.map +1 -0
  160. package/lib/types/bedrock/payloads.d.ts +12 -0
  161. package/lib/types/bedrock/payloads.d.ts.map +1 -0
  162. package/lib/types/bedrock/s3.d.ts +23 -0
  163. package/lib/types/bedrock/s3.d.ts.map +1 -0
  164. package/lib/types/bedrock/twelvelabs.d.ts +50 -0
  165. package/lib/types/bedrock/twelvelabs.d.ts.map +1 -0
  166. package/lib/types/groq/index.d.ts +27 -0
  167. package/lib/types/groq/index.d.ts.map +1 -0
  168. package/lib/types/huggingface_ie.d.ts +35 -0
  169. package/lib/types/huggingface_ie.d.ts.map +1 -0
  170. package/lib/types/index.d.ts +15 -0
  171. package/lib/types/index.d.ts.map +1 -0
  172. package/lib/types/mistral/index.d.ts +25 -0
  173. package/lib/types/mistral/index.d.ts.map +1 -0
  174. package/lib/types/mistral/types.d.ts +132 -0
  175. package/lib/types/mistral/types.d.ts.map +1 -0
  176. package/lib/types/openai/azure_openai.d.ts +25 -0
  177. package/lib/types/openai/azure_openai.d.ts.map +1 -0
  178. package/lib/types/openai/index.d.ts +31 -0
  179. package/lib/types/openai/index.d.ts.map +1 -0
  180. package/lib/types/openai/openai.d.ts +15 -0
  181. package/lib/types/openai/openai.d.ts.map +1 -0
  182. package/lib/types/openai/openai_compatible.d.ts +26 -0
  183. package/lib/types/openai/openai_compatible.d.ts.map +1 -0
  184. package/lib/types/openai/openai_format.d.ts +21 -0
  185. package/lib/types/openai/openai_format.d.ts.map +1 -0
  186. package/lib/types/replicate.d.ts +48 -0
  187. package/lib/types/replicate.d.ts.map +1 -0
  188. package/lib/types/test-driver/TestErrorCompletionStream.d.ts +9 -0
  189. package/lib/types/test-driver/TestErrorCompletionStream.d.ts.map +1 -0
  190. package/lib/types/test-driver/TestValidationErrorCompletionStream.d.ts +9 -0
  191. package/lib/types/test-driver/TestValidationErrorCompletionStream.d.ts.map +1 -0
  192. package/lib/types/test-driver/index.d.ts +24 -0
  193. package/lib/types/test-driver/index.d.ts.map +1 -0
  194. package/lib/types/test-driver/utils.d.ts +5 -0
  195. package/lib/types/test-driver/utils.d.ts.map +1 -0
  196. package/lib/types/togetherai/index.d.ts +23 -0
  197. package/lib/types/togetherai/index.d.ts.map +1 -0
  198. package/lib/types/togetherai/interfaces.d.ts +96 -0
  199. package/lib/types/togetherai/interfaces.d.ts.map +1 -0
  200. package/lib/types/vertexai/debug.d.ts +2 -0
  201. package/lib/types/vertexai/debug.d.ts.map +1 -0
  202. package/lib/types/vertexai/embeddings/embeddings-image.d.ts +11 -0
  203. package/lib/types/vertexai/embeddings/embeddings-image.d.ts.map +1 -0
  204. package/lib/types/vertexai/embeddings/embeddings-text.d.ts +10 -0
  205. package/lib/types/vertexai/embeddings/embeddings-text.d.ts.map +1 -0
  206. package/lib/types/vertexai/index.d.ts +65 -0
  207. package/lib/types/vertexai/index.d.ts.map +1 -0
  208. package/lib/types/vertexai/models/claude.d.ts +28 -0
  209. package/lib/types/vertexai/models/claude.d.ts.map +1 -0
  210. package/lib/types/vertexai/models/gemini.d.ts +18 -0
  211. package/lib/types/vertexai/models/gemini.d.ts.map +1 -0
  212. package/lib/types/vertexai/models/imagen.d.ts +75 -0
  213. package/lib/types/vertexai/models/imagen.d.ts.map +1 -0
  214. package/lib/types/vertexai/models/llama.d.ts +20 -0
  215. package/lib/types/vertexai/models/llama.d.ts.map +1 -0
  216. package/lib/types/vertexai/models.d.ts +15 -0
  217. package/lib/types/vertexai/models.d.ts.map +1 -0
  218. package/lib/types/watsonx/index.d.ts +27 -0
  219. package/lib/types/watsonx/index.d.ts.map +1 -0
  220. package/lib/types/watsonx/interfaces.d.ts +65 -0
  221. package/lib/types/watsonx/interfaces.d.ts.map +1 -0
  222. package/lib/types/xai/index.d.ts +18 -0
  223. package/lib/types/xai/index.d.ts.map +1 -0
  224. package/package.json +3 -3
@@ -0,0 +1,1091 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BedrockDriver = void 0;
4
+ const client_bedrock_1 = require("@aws-sdk/client-bedrock");
5
+ const client_bedrock_runtime_1 = require("@aws-sdk/client-bedrock-runtime");
6
+ const client_s3_1 = require("@aws-sdk/client-s3");
7
+ const core_1 = require("@llumiverse/core");
8
+ const async_1 = require("@llumiverse/core/async");
9
+ const formatters_1 = require("@llumiverse/core/formatters");
10
+ const mnemonist_1 = require("mnemonist");
11
+ const converse_js_1 = require("./converse.js");
12
+ const nova_image_payload_js_1 = require("./nova-image-payload.js");
13
+ const s3_js_1 = require("./s3.js");
14
+ const twelvelabs_js_1 = require("./twelvelabs.js");
15
+ const supportStreamingCache = new mnemonist_1.LRUCache(4096);
16
+ var BedrockModelType;
17
+ (function (BedrockModelType) {
18
+ BedrockModelType["FoundationModel"] = "foundation-model";
19
+ BedrockModelType["InferenceProfile"] = "inference-profile";
20
+ BedrockModelType["CustomModel"] = "custom-model";
21
+ BedrockModelType["Unknown"] = "unknown";
22
+ })(BedrockModelType || (BedrockModelType = {}));
23
+ ;
24
+ function converseFinishReason(reason) {
25
+ //Possible values:
26
+ //end_turn | tool_use | max_tokens | stop_sequence | guardrail_intervened | content_filtered
27
+ if (!reason)
28
+ return undefined;
29
+ switch (reason) {
30
+ case 'end_turn': return "stop";
31
+ case 'max_tokens': return "length";
32
+ default: return reason;
33
+ }
34
+ }
35
+ //Used to get a max_token value when not specified in the model options. Claude requires it to be set.
36
+ function maxTokenFallbackClaude(option) {
37
+ const modelOptions = option.model_options;
38
+ if (modelOptions && typeof modelOptions.max_tokens === "number") {
39
+ return modelOptions.max_tokens;
40
+ }
41
+ else {
42
+ const thinking_budget = modelOptions?.thinking_budget_tokens ?? 0;
43
+ let maxSupportedTokens = (0, core_1.getMaxTokensLimitBedrock)(option.model) ?? 8192; // Should always return a number for claude, 8192 is to satisfy the TypeScript type checker;
44
+ // Fallback to the default max tokens limit for the model
45
+ if (option.model.includes('claude-3-7-sonnet') && (modelOptions?.thinking_budget_tokens ?? 0) < 48000) {
46
+ maxSupportedTokens = 64000; // Claude 3.7 can go up to 128k with a beta header, but when no max tokens is specified, we default to 64k.
47
+ }
48
+ return Math.min(16000 + thinking_budget, maxSupportedTokens); // Cap to 16k, to avoid taking up too much context window and quota.
49
+ }
50
+ }
51
+ class BedrockDriver extends core_1.AbstractDriver {
52
+ static PROVIDER = "bedrock";
53
+ provider = BedrockDriver.PROVIDER;
54
+ _executor;
55
+ _service;
56
+ _service_region;
57
+ constructor(options) {
58
+ super(options);
59
+ if (!options.region) {
60
+ throw new Error("No region found. Set the region in the environment's endpoint URL.");
61
+ }
62
+ }
63
+ getExecutor() {
64
+ if (!this._executor) {
65
+ this._executor = new client_bedrock_runtime_1.BedrockRuntime({
66
+ region: this.options.region,
67
+ credentials: this.options.credentials,
68
+ });
69
+ }
70
+ return this._executor;
71
+ }
72
+ getService(region = this.options.region) {
73
+ if (!this._service || this._service_region != region) {
74
+ this._service = new client_bedrock_1.Bedrock({
75
+ region: region,
76
+ credentials: this.options.credentials,
77
+ });
78
+ this._service_region = region;
79
+ }
80
+ return this._service;
81
+ }
82
+ async formatPrompt(segments, opts) {
83
+ if (opts.model.includes("canvas")) {
84
+ return await (0, formatters_1.formatNovaPrompt)(segments, opts.result_schema);
85
+ }
86
+ if (opts.model.includes("twelvelabs.pegasus")) {
87
+ return await (0, twelvelabs_js_1.formatTwelvelabsPegasusPrompt)(segments, opts);
88
+ }
89
+ return await (0, converse_js_1.formatConversePrompt)(segments, opts);
90
+ }
91
+ getExtractedExecution(result, _prompt, options) {
92
+ let resultText = "";
93
+ let reasoning = "";
94
+ if (result.output?.message?.content) {
95
+ for (const content of result.output.message.content) {
96
+ // Get text output
97
+ if (content.text) {
98
+ resultText += content.text;
99
+ }
100
+ else if (content.reasoningContent) {
101
+ // Get reasoning content only if include_thoughts is true
102
+ const claudeOptions = options?.model_options;
103
+ if (claudeOptions?.include_thoughts) {
104
+ if (content.reasoningContent.reasoningText) {
105
+ reasoning += content.reasoningContent.reasoningText.text;
106
+ }
107
+ else if (content.reasoningContent.redactedContent) {
108
+ // Handle redacted thinking content
109
+ const redactedData = new TextDecoder().decode(content.reasoningContent.redactedContent);
110
+ reasoning += `[Redacted thinking: ${redactedData}]`;
111
+ }
112
+ }
113
+ else {
114
+ this.logger.info("[Bedrock] Not outputting reasoning content as include_thoughts is false");
115
+ }
116
+ }
117
+ else {
118
+ // Get content block type
119
+ const type = Object.keys(content).find(key => key !== '$unknown' && content[key] !== undefined);
120
+ this.logger.info({ type }, "[Bedrock] Unsupported content response type:");
121
+ }
122
+ }
123
+ // Add spacing if we have reasoning content
124
+ if (reasoning) {
125
+ reasoning += '\n\n';
126
+ }
127
+ }
128
+ const completionResult = {
129
+ result: reasoning + resultText ? [{ type: "text", value: reasoning + resultText }] : [],
130
+ token_usage: {
131
+ prompt: result.usage?.inputTokens,
132
+ result: result.usage?.outputTokens,
133
+ total: result.usage?.totalTokens,
134
+ },
135
+ finish_reason: converseFinishReason(result.stopReason),
136
+ };
137
+ return completionResult;
138
+ }
139
+ ;
140
+ getExtractedStream(result, _prompt, options) {
141
+ let output = "";
142
+ let reasoning = "";
143
+ let stop_reason = "";
144
+ let token_usage;
145
+ // Check if we should include thoughts
146
+ const shouldIncludeThoughts = options && options.model_options?.include_thoughts;
147
+ // Handle content block start events (for reasoning blocks)
148
+ if (result.contentBlockStart) {
149
+ // Handle redacted content at block start
150
+ if (result.contentBlockStart.start && 'reasoningContent' in result.contentBlockStart.start && shouldIncludeThoughts) {
151
+ const reasoningStart = result.contentBlockStart.start;
152
+ if (reasoningStart.reasoningContent?.redactedContent) {
153
+ const redactedData = new TextDecoder().decode(reasoningStart.reasoningContent.redactedContent);
154
+ reasoning = `[Redacted thinking: ${redactedData}]`;
155
+ }
156
+ }
157
+ }
158
+ // Handle content block deltas (text and reasoning)
159
+ if (result.contentBlockDelta) {
160
+ const delta = result.contentBlockDelta.delta;
161
+ if (delta?.text) {
162
+ output = delta.text;
163
+ }
164
+ else if (delta?.reasoningContent && shouldIncludeThoughts) {
165
+ if (delta.reasoningContent.text) {
166
+ reasoning = delta.reasoningContent.text;
167
+ }
168
+ else if (delta.reasoningContent.redactedContent) {
169
+ const redactedData = new TextDecoder().decode(delta.reasoningContent.redactedContent);
170
+ reasoning = `[Redacted thinking: ${redactedData}]`;
171
+ }
172
+ else if (delta.reasoningContent.signature) {
173
+ // Handle signature updates for reasoning content - end of thinking
174
+ reasoning = "\n\n";
175
+ // Putting logging here so it only triggers once.
176
+ this.logger.info("[Bedrock] Not outputting reasoning content as include_thoughts is false");
177
+ }
178
+ }
179
+ else if (delta) {
180
+ // Get content block type
181
+ const type = Object.keys(delta).find(key => key !== '$unknown' && delta[key] !== undefined);
182
+ this.logger.info({ type }, "[Bedrock] Unsupported content response type:");
183
+ }
184
+ }
185
+ // Handle content block stop events
186
+ if (result.contentBlockStop) {
187
+ // Content block ended - could be end of reasoning or text block
188
+ // Add minimal spacing for reasoning blocks if not already present
189
+ if (reasoning && !reasoning.endsWith('\n\n') && shouldIncludeThoughts) {
190
+ reasoning += '\n\n';
191
+ }
192
+ }
193
+ if (result.messageStop) {
194
+ stop_reason = result.messageStop.stopReason ?? "";
195
+ }
196
+ if (result.metadata) {
197
+ token_usage = {
198
+ prompt: result.metadata.usage?.inputTokens,
199
+ result: result.metadata.usage?.outputTokens,
200
+ total: result.metadata.usage?.totalTokens,
201
+ };
202
+ }
203
+ const completionResult = {
204
+ result: reasoning + output ? [{ type: "text", value: reasoning + output }] : [],
205
+ token_usage: token_usage,
206
+ finish_reason: converseFinishReason(stop_reason),
207
+ };
208
+ return completionResult;
209
+ }
210
+ ;
211
+ extractRegion(modelString, defaultRegion) {
212
+ // Match region in full ARN pattern
213
+ const arnMatch = modelString.match(/arn:aws[^:]*:bedrock:([^:]+):/);
214
+ if (arnMatch) {
215
+ return arnMatch[1];
216
+ }
217
+ // Match common AWS regions directly in string
218
+ const regionMatch = modelString.match(/(?:us|eu|ap|sa|ca|me|af)[-](east|west|central|south|north|southeast|southwest|northeast|northwest)[-][1-9]/);
219
+ if (regionMatch) {
220
+ return regionMatch[0];
221
+ }
222
+ return defaultRegion;
223
+ }
224
+ async getCanStream(model, type) {
225
+ let canStream = false;
226
+ let error = null;
227
+ const region = this.extractRegion(model, this.options.region);
228
+ if (type == BedrockModelType.FoundationModel || type == BedrockModelType.Unknown) {
229
+ try {
230
+ const response = await this.getService(region).getFoundationModel({
231
+ modelIdentifier: model
232
+ });
233
+ canStream = response.modelDetails?.responseStreamingSupported ?? false;
234
+ return canStream;
235
+ }
236
+ catch (e) {
237
+ error = e;
238
+ }
239
+ }
240
+ if (type == BedrockModelType.InferenceProfile || type == BedrockModelType.Unknown) {
241
+ try {
242
+ const response = await this.getService(region).getInferenceProfile({
243
+ inferenceProfileIdentifier: model
244
+ });
245
+ canStream = await this.getCanStream(response.models?.[0].modelArn ?? "", BedrockModelType.FoundationModel);
246
+ return canStream;
247
+ }
248
+ catch (e) {
249
+ error = e;
250
+ }
251
+ }
252
+ if (type == BedrockModelType.CustomModel || type == BedrockModelType.Unknown) {
253
+ try {
254
+ const response = await this.getService(region).getCustomModel({
255
+ modelIdentifier: model
256
+ });
257
+ canStream = await this.getCanStream(response.baseModelArn ?? "", BedrockModelType.FoundationModel);
258
+ return canStream;
259
+ }
260
+ catch (e) {
261
+ error = e;
262
+ }
263
+ }
264
+ if (error) {
265
+ console.warn("Error on canStream check for model: " + model + " region detected: " + region, error);
266
+ }
267
+ return canStream;
268
+ }
269
+ async canStream(options) {
270
+ // // TwelveLabs Pegasus supports streaming according to the documentation
271
+ // if (options.model.includes("twelvelabs.pegasus")) {
272
+ // return true;
273
+ // }
274
+ let canStream = supportStreamingCache.get(options.model);
275
+ if (canStream == null) {
276
+ let type = BedrockModelType.Unknown;
277
+ if (options.model.includes("foundation-model")) {
278
+ type = BedrockModelType.FoundationModel;
279
+ }
280
+ else if (options.model.includes("inference-profile")) {
281
+ type = BedrockModelType.InferenceProfile;
282
+ }
283
+ else if (options.model.includes("custom-model")) {
284
+ type = BedrockModelType.CustomModel;
285
+ }
286
+ canStream = await this.getCanStream(options.model, type);
287
+ supportStreamingCache.set(options.model, canStream);
288
+ }
289
+ return canStream;
290
+ }
291
+ /**
292
+ * Build conversation context after streaming completion.
293
+ * Reconstructs the assistant message from accumulated results and applies stripping.
294
+ */
295
+ buildStreamingConversation(prompt, result, toolUse, options) {
296
+ // Only handle ConverseRequest prompts (not NovaMessagesPrompt or TwelvelabsPegasusRequest)
297
+ if (options.model.includes("canvas") || options.model.includes("twelvelabs.pegasus")) {
298
+ return undefined;
299
+ }
300
+ const conversePrompt = prompt;
301
+ const completionResults = result;
302
+ // Convert accumulated results to text content for assistant message
303
+ const textContent = completionResults
304
+ .map(r => {
305
+ switch (r.type) {
306
+ case 'text':
307
+ return r.value;
308
+ case 'json':
309
+ return typeof r.value === 'string' ? r.value : JSON.stringify(r.value);
310
+ case 'image':
311
+ // Skip images in conversation - they're in the result
312
+ return '';
313
+ default:
314
+ return String(r.value || '');
315
+ }
316
+ })
317
+ .join('');
318
+ // Deserialize any base64-encoded binary data back to Uint8Array
319
+ const incomingConversation = (0, core_1.deserializeBinaryFromStorage)(options.conversation);
320
+ // Start with the conversation from options combined with the prompt
321
+ let conversation = updateConversation(incomingConversation, conversePrompt);
322
+ // Build assistant message content
323
+ const messageContent = [];
324
+ if (textContent) {
325
+ messageContent.push({ text: textContent });
326
+ }
327
+ // Add tool use blocks if present
328
+ if (toolUse && toolUse.length > 0) {
329
+ for (const tool of toolUse) {
330
+ messageContent.push({
331
+ toolUse: {
332
+ toolUseId: tool.id,
333
+ name: tool.tool_name,
334
+ input: tool.tool_input,
335
+ }
336
+ });
337
+ }
338
+ }
339
+ // Add assistant message
340
+ const assistantMessage = {
341
+ messages: [{
342
+ content: messageContent.length > 0 ? messageContent : [{ text: '' }],
343
+ role: "assistant"
344
+ }],
345
+ modelId: conversePrompt.modelId,
346
+ };
347
+ conversation = updateConversation(conversation, assistantMessage);
348
+ // Increment turn counter
349
+ conversation = (0, core_1.incrementConversationTurn)(conversation);
350
+ // Apply stripping based on options
351
+ const currentTurn = (0, core_1.getConversationMeta)(conversation).turnNumber;
352
+ const stripOptions = {
353
+ keepForTurns: options.stripImagesAfterTurns ?? Infinity,
354
+ currentTurn,
355
+ textMaxTokens: options.stripTextMaxTokens
356
+ };
357
+ let processedConversation = (0, core_1.stripBinaryFromConversation)(conversation, stripOptions);
358
+ processedConversation = (0, core_1.truncateLargeTextInConversation)(processedConversation, stripOptions);
359
+ return processedConversation;
360
+ }
361
+ async requestTextCompletion(prompt, options) {
362
+ // Handle Twelvelabs Pegasus models
363
+ if (options.model.includes("twelvelabs.pegasus")) {
364
+ return this.requestTwelvelabsPegasusCompletion(prompt, options);
365
+ }
366
+ // Handle other Bedrock models that use Converse API
367
+ const conversePrompt = prompt;
368
+ // Deserialize any base64-encoded binary data back to Uint8Array before API call
369
+ const incomingConversation = (0, core_1.deserializeBinaryFromStorage)(options.conversation);
370
+ let conversation = updateConversation(incomingConversation, conversePrompt);
371
+ const payload = this.preparePayload(conversation, options);
372
+ const executor = this.getExecutor();
373
+ const res = await executor.converse({
374
+ ...payload,
375
+ });
376
+ conversation = updateConversation(conversation, {
377
+ messages: [res.output?.message ?? { content: [{ text: "" }], role: "assistant" }],
378
+ modelId: conversePrompt.modelId,
379
+ });
380
+ // Increment turn counter for deferred stripping
381
+ conversation = (0, core_1.incrementConversationTurn)(conversation);
382
+ let tool_use = undefined;
383
+ //Get tool requests, we check tool use regardless of finish reason, as you can hit length and still get a valid response.
384
+ tool_use = res.output?.message?.content?.reduce((tools, c) => {
385
+ if (c.toolUse) {
386
+ tools.push({
387
+ tool_name: c.toolUse.name ?? "",
388
+ tool_input: c.toolUse.input,
389
+ id: c.toolUse.toolUseId ?? "",
390
+ });
391
+ }
392
+ return tools;
393
+ }, []);
394
+ //If no tools were used, set to undefined
395
+ if (tool_use && tool_use.length == 0) {
396
+ tool_use = undefined;
397
+ }
398
+ // Strip/serialize binary data based on options.stripImagesAfterTurns
399
+ const currentTurn = (0, core_1.getConversationMeta)(conversation).turnNumber;
400
+ const stripOptions = {
401
+ keepForTurns: options.stripImagesAfterTurns ?? Infinity,
402
+ currentTurn,
403
+ textMaxTokens: options.stripTextMaxTokens
404
+ };
405
+ let processedConversation = (0, core_1.stripBinaryFromConversation)(conversation, stripOptions);
406
+ // Truncate large text content if configured
407
+ processedConversation = (0, core_1.truncateLargeTextInConversation)(processedConversation, stripOptions);
408
+ const completion = {
409
+ ...this.getExtractedExecution(res, conversePrompt, options),
410
+ original_response: options.include_original_response ? res : undefined,
411
+ conversation: processedConversation,
412
+ tool_use: tool_use,
413
+ };
414
+ return completion;
415
+ }
416
+ async requestTwelvelabsPegasusCompletion(prompt, options) {
417
+ const executor = this.getExecutor();
418
+ const res = await executor.invokeModel({
419
+ modelId: options.model,
420
+ contentType: "application/json",
421
+ accept: "application/json",
422
+ body: JSON.stringify(prompt),
423
+ });
424
+ const decoder = new TextDecoder();
425
+ const body = decoder.decode(res.body);
426
+ const result = JSON.parse(body);
427
+ // Extract the response according to TwelveLabs Pegasus format
428
+ let finishReason;
429
+ switch (result.finishReason) {
430
+ case "stop":
431
+ finishReason = "stop";
432
+ break;
433
+ case "length":
434
+ finishReason = "length";
435
+ break;
436
+ default:
437
+ finishReason = result.finishReason;
438
+ }
439
+ return {
440
+ result: result.message ? [{ type: "text", value: result.message }] : [],
441
+ finish_reason: finishReason,
442
+ original_response: options.include_original_response ? result : undefined,
443
+ };
444
+ }
445
+ async requestTwelvelabsPegasusCompletionStream(prompt, options) {
446
+ const executor = this.getExecutor();
447
+ const res = await executor.invokeModelWithResponseStream({
448
+ modelId: options.model,
449
+ contentType: "application/json",
450
+ accept: "application/json",
451
+ body: JSON.stringify(prompt),
452
+ });
453
+ if (!res.body) {
454
+ throw new Error("[Bedrock] Stream not found in response");
455
+ }
456
+ return (0, async_1.transformAsyncIterator)(res.body, (chunk) => {
457
+ if (chunk.chunk?.bytes) {
458
+ const decoder = new TextDecoder();
459
+ const body = decoder.decode(chunk.chunk.bytes);
460
+ try {
461
+ const result = JSON.parse(body);
462
+ // Extract streaming response according to TwelveLabs Pegasus format
463
+ let finishReason;
464
+ if (result.finishReason) {
465
+ switch (result.finishReason) {
466
+ case "stop":
467
+ finishReason = "stop";
468
+ break;
469
+ case "length":
470
+ finishReason = "length";
471
+ break;
472
+ default:
473
+ finishReason = result.finishReason;
474
+ }
475
+ }
476
+ return {
477
+ result: result.delta || result.message ? [{ type: "text", value: result.delta || result.message || "" }] : [],
478
+ finish_reason: finishReason,
479
+ };
480
+ }
481
+ catch (error) {
482
+ // If JSON parsing fails, return empty chunk
483
+ return {
484
+ result: [],
485
+ };
486
+ }
487
+ }
488
+ return {
489
+ result: [],
490
+ };
491
+ });
492
+ }
493
+ async requestTextCompletionStream(prompt, options) {
494
+ // Handle Twelvelabs Pegasus models
495
+ if (options.model.includes("twelvelabs.pegasus")) {
496
+ return this.requestTwelvelabsPegasusCompletionStream(prompt, options);
497
+ }
498
+ // Handle other Bedrock models that use Converse API
499
+ const conversePrompt = prompt;
500
+ // Include conversation history (same as non-streaming)
501
+ // Deserialize any base64-encoded binary data back to Uint8Array before API call
502
+ const incomingConversation = (0, core_1.deserializeBinaryFromStorage)(options.conversation);
503
+ const conversation = updateConversation(incomingConversation, conversePrompt);
504
+ const payload = this.preparePayload(conversation, options);
505
+ const executor = this.getExecutor();
506
+ return executor.converseStream({
507
+ ...payload,
508
+ }).then((res) => {
509
+ const stream = res.stream;
510
+ if (!stream) {
511
+ throw new Error("[Bedrock] Stream not found in response");
512
+ }
513
+ return (0, async_1.transformAsyncIterator)(stream, (streamSegment) => {
514
+ return this.getExtractedStream(streamSegment, conversePrompt, options);
515
+ });
516
+ }).catch((err) => {
517
+ this.logger.error({ error: err }, "[Bedrock] Failed to stream");
518
+ throw err;
519
+ });
520
+ }
521
+ preparePayload(prompt, options) {
522
+ const model_options = options.model_options ?? { _option_id: "text-fallback" };
523
+ let additionalField = {};
524
+ let supportsJSONPrefill = false;
525
+ if (options.model.includes("amazon")) {
526
+ supportsJSONPrefill = true;
527
+ //Titan models also exists but does not support any additional options
528
+ if (options.model.includes("nova")) {
529
+ additionalField = { inferenceConfig: { topK: model_options.top_k } };
530
+ }
531
+ }
532
+ else if (options.model.includes("claude")) {
533
+ const claude_options = model_options;
534
+ const thinking = claude_options.thinking_mode ?? false;
535
+ supportsJSONPrefill = !thinking;
536
+ if (options.model.includes("claude-3-7") || options.model.includes("-4-")) {
537
+ additionalField = {
538
+ ...additionalField,
539
+ reasoning_config: {
540
+ type: thinking ? "enabled" : "disabled",
541
+ budget_tokens: thinking ? (claude_options.thinking_budget_tokens ?? 1024) : undefined,
542
+ }
543
+ };
544
+ if (thinking && options.model.includes("claude-3-7-sonnet") &&
545
+ ((claude_options.max_tokens ?? 0) > 64000 || (claude_options.thinking_budget_tokens ?? 0) > 64000)) {
546
+ additionalField = {
547
+ ...additionalField,
548
+ anthropic_beta: ["output-128k-2025-02-19"]
549
+ };
550
+ }
551
+ }
552
+ //Needs max_tokens to be set
553
+ if (!model_options.max_tokens) {
554
+ model_options.max_tokens = maxTokenFallbackClaude(options);
555
+ }
556
+ additionalField = { ...additionalField, top_k: model_options.top_k };
557
+ }
558
+ else if (options.model.includes("meta")) {
559
+ //LLaMA models support no additional options
560
+ }
561
+ else if (options.model.includes("mistral")) {
562
+ //7B instruct and 8x7B instruct
563
+ if (options.model.includes("7b")) {
564
+ additionalField = { top_k: model_options.top_k };
565
+ //Does not support system messages
566
+ if (prompt.system && prompt.system?.length != 0) {
567
+ prompt.messages?.push((0, converse_js_1.converseSystemToMessages)(prompt.system));
568
+ prompt.system = undefined;
569
+ prompt.messages = (0, converse_js_1.converseConcatMessages)(prompt.messages);
570
+ }
571
+ }
572
+ else {
573
+ //Other models such as Mistral Small,Large and Large 2
574
+ //Support no additional fields.
575
+ }
576
+ }
577
+ else if (options.model.includes("ai21")) {
578
+ //Jamba models support no additional options
579
+ //Jurassic 2 models do.
580
+ if (options.model.includes("j2")) {
581
+ additionalField = {
582
+ presencePenalty: { scale: model_options.presence_penalty },
583
+ frequencyPenalty: { scale: model_options.frequency_penalty },
584
+ };
585
+ //Does not support system messages
586
+ if (prompt.system && prompt.system?.length != 0) {
587
+ prompt.messages?.push((0, converse_js_1.converseSystemToMessages)(prompt.system));
588
+ prompt.system = undefined;
589
+ prompt.messages = (0, converse_js_1.converseConcatMessages)(prompt.messages);
590
+ }
591
+ }
592
+ }
593
+ else if (options.model.includes("cohere.command")) {
594
+ // If last message is "```json", remove it.
595
+ //Command R and R plus
596
+ if (options.model.includes("cohere.command-r")) {
597
+ additionalField = {
598
+ k: model_options.top_k,
599
+ frequency_penalty: model_options.frequency_penalty,
600
+ presence_penalty: model_options.presence_penalty,
601
+ };
602
+ }
603
+ else {
604
+ // Command non-R
605
+ additionalField = { k: model_options.top_k };
606
+ //Does not support system messages
607
+ if (prompt.system && prompt.system?.length != 0) {
608
+ prompt.messages?.push((0, converse_js_1.converseSystemToMessages)(prompt.system));
609
+ prompt.system = undefined;
610
+ prompt.messages = (0, converse_js_1.converseConcatMessages)(prompt.messages);
611
+ }
612
+ }
613
+ }
614
+ else if (options.model.includes("palmyra")) {
615
+ const palmyraOptions = model_options;
616
+ additionalField = {
617
+ seed: palmyraOptions?.seed,
618
+ presence_penalty: palmyraOptions?.presence_penalty,
619
+ frequency_penalty: palmyraOptions?.frequency_penalty,
620
+ min_tokens: palmyraOptions?.min_tokens,
621
+ };
622
+ }
623
+ else if (options.model.includes("deepseek")) {
624
+ //DeepSeek models support no additional options
625
+ }
626
+ else if (options.model.includes("gpt-oss")) {
627
+ const gptOssOptions = model_options;
628
+ additionalField = {
629
+ reasoning_effort: gptOssOptions?.reasoning_effort,
630
+ };
631
+ }
632
+ //If last message is "```json", add corresponding ``` as a stop sequence.
633
+ if (prompt.messages && prompt.messages.length > 0) {
634
+ if (prompt.messages[prompt.messages.length - 1].content?.[0].text === "```json") {
635
+ const stopSeq = model_options.stop_sequence;
636
+ if (!stopSeq) {
637
+ model_options.stop_sequence = ["```"];
638
+ }
639
+ else if (!stopSeq.includes("```")) {
640
+ stopSeq.push("```");
641
+ model_options.stop_sequence = stopSeq;
642
+ }
643
+ }
644
+ }
645
+ const tool_defs = getToolDefinitions(options.tools);
646
+ // Use prefill when there is a schema and tools are not being used
647
+ if (supportsJSONPrefill && options.result_schema && !tool_defs) {
648
+ prompt.messages = (0, converse_js_1.converseJSONprefill)(prompt.messages);
649
+ }
650
+ // Clean undefined values from additionalField since AWS Bedrock requires valid JSON
651
+ // and will throw an exception for unrecognized parameters
652
+ const cleanedAdditionalFields = removeUndefinedValues(additionalField);
653
+ const cleanedModelOptions = removeUndefinedValues({
654
+ maxTokens: model_options.max_tokens,
655
+ temperature: model_options.temperature,
656
+ topP: model_options.top_p,
657
+ stopSequences: model_options.stop_sequence,
658
+ });
659
+ //Construct the final request payload
660
+ // We only add fields that are defined to avoid AWS errors
661
+ const request = {
662
+ modelId: options.model,
663
+ };
664
+ if (prompt.messages) {
665
+ request.messages = prompt.messages;
666
+ }
667
+ if (prompt.system) {
668
+ request.system = prompt.system;
669
+ }
670
+ if (Object.keys(cleanedModelOptions).length > 0) {
671
+ request.inferenceConfig = cleanedModelOptions;
672
+ }
673
+ if (Object.keys(cleanedAdditionalFields).length > 0) {
674
+ request.additionalModelRequestFields = cleanedAdditionalFields;
675
+ }
676
+ if (tool_defs?.length) {
677
+ request.toolConfig = {
678
+ tools: tool_defs,
679
+ };
680
+ }
681
+ return request;
682
+ }
683
+ isImageModel(model) {
684
+ return model.includes("titan-image") || model.includes("stable-diffusion") || model.includes("nova-canvas");
685
+ }
686
+ async requestImageGeneration(prompt, options) {
687
+ if (options.model_options?._option_id !== "bedrock-nova-canvas") {
688
+ this.logger.warn({ options: options.model_options }, "Invalid model options");
689
+ }
690
+ const model_options = options.model_options;
691
+ const executor = this.getExecutor();
692
+ const taskType = model_options.taskType ?? nova_image_payload_js_1.NovaImageGenerationTaskType.TEXT_IMAGE;
693
+ this.logger.info("Task type: " + taskType);
694
+ if (typeof prompt === "string") {
695
+ throw new Error("Bad prompt format");
696
+ }
697
+ const payload = await (0, nova_image_payload_js_1.formatNovaImageGenerationPayload)(taskType, prompt, options);
698
+ const res = await executor.invokeModel({
699
+ modelId: options.model,
700
+ contentType: "application/json",
701
+ accept: "application/json",
702
+ body: JSON.stringify(payload),
703
+ }, {
704
+ requestTimeout: 60000 * 5
705
+ });
706
+ const decoder = new TextDecoder();
707
+ const body = decoder.decode(res.body);
708
+ const bedrockResult = JSON.parse(body);
709
+ return {
710
+ error: bedrockResult.error,
711
+ result: bedrockResult.images.map((image) => ({
712
+ type: "image",
713
+ value: image
714
+ }))
715
+ };
716
+ }
717
+ async startTraining(dataset, options) {
718
+ //convert options.params to Record<string, string>
719
+ const params = {};
720
+ for (const [key, value] of Object.entries(options.params || {})) {
721
+ params[key] = String(value);
722
+ }
723
+ if (!this.options.training_bucket) {
724
+ throw new Error("Training cannot nbe used since the 'training_bucket' property was not specified in driver options");
725
+ }
726
+ const s3 = new client_s3_1.S3Client({ region: this.options.region, credentials: this.options.credentials });
727
+ const stream = await dataset.getStream();
728
+ const upload = await (0, s3_js_1.forceUploadFile)(s3, stream, this.options.training_bucket, dataset.name);
729
+ const service = this.getService();
730
+ const response = await service.send(new client_bedrock_1.CreateModelCustomizationJobCommand({
731
+ jobName: options.name + "-job",
732
+ customModelName: options.name,
733
+ roleArn: this.options.training_role_arn || undefined,
734
+ baseModelIdentifier: options.model,
735
+ clientRequestToken: "llumiverse-" + Date.now(),
736
+ trainingDataConfig: {
737
+ s3Uri: `s3://${upload.Bucket}/${upload.Key}`,
738
+ },
739
+ outputDataConfig: undefined,
740
+ hyperParameters: params,
741
+ //TODO not supported?
742
+ //customizationType: "FINE_TUNING",
743
+ }));
744
+ const job = await service.send(new client_bedrock_1.GetModelCustomizationJobCommand({
745
+ jobIdentifier: response.jobArn
746
+ }));
747
+ return jobInfo(job, response.jobArn);
748
+ }
749
+ async cancelTraining(jobId) {
750
+ const service = this.getService();
751
+ await service.send(new client_bedrock_1.StopModelCustomizationJobCommand({
752
+ jobIdentifier: jobId
753
+ }));
754
+ const job = await service.send(new client_bedrock_1.GetModelCustomizationJobCommand({
755
+ jobIdentifier: jobId
756
+ }));
757
+ return jobInfo(job, jobId);
758
+ }
759
+ async getTrainingJob(jobId) {
760
+ const service = this.getService();
761
+ const job = await service.send(new client_bedrock_1.GetModelCustomizationJobCommand({
762
+ jobIdentifier: jobId
763
+ }));
764
+ return jobInfo(job, jobId);
765
+ }
766
+ // ===================== management API ==================
767
+ async validateConnection() {
768
+ const service = this.getService();
769
+ this.logger.debug("[Bedrock] validating connection", service.config.credentials.name);
770
+ //return true as if the client has been initialized, it means the connection is valid
771
+ return true;
772
+ }
773
+ async listTrainableModels() {
774
+ this.logger.debug("[Bedrock] listing trainable models");
775
+ return this._listModels(m => m.customizationsSupported ? m.customizationsSupported.includes("FINE_TUNING") : false);
776
+ }
777
+ async listModels() {
778
+ this.logger.debug("[Bedrock] listing models");
779
+ // exclude trainable models since they are not executable
780
+ // exclude embedding models, not to be used for typical completions.
781
+ const filter = (m) => (m.inferenceTypesSupported?.includes("ON_DEMAND") && !m.outputModalities?.includes("EMBEDDING")) ?? false;
782
+ return this._listModels(filter);
783
+ }
784
+ async _listModels(foundationFilter) {
785
+ const service = this.getService();
786
+ const [foundationModelsList, customModelsList, inferenceProfilesList] = await Promise.all([
787
+ service.listFoundationModels({}).catch(() => {
788
+ this.logger.warn("[Bedrock] Can't list foundation models. Check if the user has the right permissions.");
789
+ return undefined;
790
+ }),
791
+ service.listCustomModels({}).catch(() => {
792
+ this.logger.warn("[Bedrock] Can't list custom models. Check if the user has the right permissions.");
793
+ return undefined;
794
+ }),
795
+ service.listInferenceProfiles({}).catch(() => {
796
+ this.logger.warn("[Bedrock] Can't list inference profiles. Check if the user has the right permissions.");
797
+ return undefined;
798
+ }),
799
+ ]);
800
+ if (!foundationModelsList?.modelSummaries) {
801
+ throw new Error("Foundation models not found");
802
+ }
803
+ let foundationModels = foundationModelsList.modelSummaries || [];
804
+ if (foundationFilter) {
805
+ foundationModels = foundationModels.filter(foundationFilter);
806
+ }
807
+ const supportedPublishers = ["amazon", "anthropic", "cohere", "ai21",
808
+ "mistral", "meta", "deepseek", "writer",
809
+ "openai", "twelvelabs", "qwen"];
810
+ const unsupportedModelsByPublisher = {
811
+ amazon: ["titan-image-generator", "nova-reel", "nova-sonic", "rerank"],
812
+ anthropic: [],
813
+ cohere: ["rerank", "embed"],
814
+ ai21: [],
815
+ mistral: [],
816
+ meta: [],
817
+ deepseek: [],
818
+ writer: [],
819
+ openai: [],
820
+ twelvelabs: ["marengo"],
821
+ qwen: [],
822
+ };
823
+ // Helper function to check if model should be filtered out
824
+ const shouldIncludeModel = (modelId, providerName) => {
825
+ if (!modelId || !providerName)
826
+ return false;
827
+ const normalizedProvider = providerName.toLowerCase();
828
+ // Check if provider is supported
829
+ const isProviderSupported = supportedPublishers.some(provider => normalizedProvider.includes(provider));
830
+ if (!isProviderSupported)
831
+ return false;
832
+ // Check if model is in the unsupported list for its provider
833
+ for (const provider of supportedPublishers) {
834
+ if (normalizedProvider.includes(provider)) {
835
+ const unsupportedModels = unsupportedModelsByPublisher[provider] || [];
836
+ return !unsupportedModels.some(unsupported => modelId.toLowerCase().includes(unsupported));
837
+ }
838
+ }
839
+ return true;
840
+ };
841
+ foundationModels = foundationModels.filter(m => shouldIncludeModel(m.modelId, m.providerName));
842
+ const aiModels = foundationModels.map((m) => {
843
+ if (!m.modelId) {
844
+ throw new Error("modelId not found");
845
+ }
846
+ const modelCapability = (0, core_1.getModelCapabilities)(m.modelArn ?? m.modelId, this.provider);
847
+ const model = {
848
+ id: m.modelArn ?? m.modelId,
849
+ name: `${m.providerName} ${m.modelName}`,
850
+ provider: this.provider,
851
+ owner: m.providerName,
852
+ can_stream: m.responseStreamingSupported ?? false,
853
+ input_modalities: m.inputModalities ? formatAmazonModalities(m.inputModalities) : (0, core_1.modelModalitiesToArray)(modelCapability.input),
854
+ output_modalities: m.outputModalities ? formatAmazonModalities(m.outputModalities) : (0, core_1.modelModalitiesToArray)(modelCapability.input),
855
+ tool_support: modelCapability.tool_support,
856
+ };
857
+ return model;
858
+ });
859
+ //add custom models
860
+ if (customModelsList?.modelSummaries) {
861
+ customModelsList.modelSummaries.forEach((m) => {
862
+ if (!m.modelArn) {
863
+ throw new Error("Model ID not found");
864
+ }
865
+ const modelCapability = (0, core_1.getModelCapabilities)(m.modelArn, this.provider);
866
+ const model = {
867
+ id: m.modelArn,
868
+ name: m.modelName ?? m.modelArn,
869
+ provider: this.provider,
870
+ owner: "custom",
871
+ description: `Custom model from ${m.baseModelName}`,
872
+ is_custom: true,
873
+ input_modalities: (0, core_1.modelModalitiesToArray)(modelCapability.input),
874
+ output_modalities: (0, core_1.modelModalitiesToArray)(modelCapability.output),
875
+ tool_support: modelCapability.tool_support,
876
+ };
877
+ aiModels.push(model);
878
+ this.validateConnection;
879
+ });
880
+ }
881
+ //add inference profiles
882
+ if (inferenceProfilesList?.inferenceProfileSummaries) {
883
+ inferenceProfilesList.inferenceProfileSummaries.forEach((p) => {
884
+ if (!p.inferenceProfileArn) {
885
+ throw new Error("Profile ARN not found");
886
+ }
887
+ // Apply the same filtering logic to inference profiles based on their name
888
+ const profileId = p.inferenceProfileId || "";
889
+ const profileName = p.inferenceProfileName || "";
890
+ // Extract provider name from profile name or ID
891
+ let providerName = "";
892
+ for (const provider of supportedPublishers) {
893
+ if (profileName.toLowerCase().includes(provider) || profileId.toLowerCase().includes(provider)) {
894
+ providerName = provider;
895
+ break;
896
+ }
897
+ }
898
+ const modelCapability = (0, core_1.getModelCapabilities)(p.inferenceProfileArn ?? p.inferenceProfileId, this.provider);
899
+ if (providerName && shouldIncludeModel(profileId, providerName)) {
900
+ const model = {
901
+ id: p.inferenceProfileArn ?? p.inferenceProfileId,
902
+ name: p.inferenceProfileName ?? p.inferenceProfileArn,
903
+ provider: this.provider,
904
+ owner: providerName,
905
+ input_modalities: (0, core_1.modelModalitiesToArray)(modelCapability.input),
906
+ output_modalities: (0, core_1.modelModalitiesToArray)(modelCapability.output),
907
+ tool_support: modelCapability.tool_support,
908
+ };
909
+ aiModels.push(model);
910
+ }
911
+ });
912
+ }
913
+ return aiModels;
914
+ }
915
+ async generateEmbeddings({ text, image, model }) {
916
+ this.logger.info("[Bedrock] Generating embeddings with model " + model);
917
+ // Handle TwelveLabs Marengo models
918
+ if (model?.includes("twelvelabs.marengo")) {
919
+ return this.generateTwelvelabsMarengoEmbeddings({ text, image, model });
920
+ }
921
+ // Handle other Bedrock embedding models
922
+ const defaultModel = image ? "amazon.titan-embed-image-v1" : "amazon.titan-embed-text-v2:0";
923
+ const modelID = model ?? defaultModel;
924
+ const invokeBody = {
925
+ inputText: text,
926
+ inputImage: image
927
+ };
928
+ const executor = this.getExecutor();
929
+ const res = await executor.invokeModel({
930
+ modelId: modelID,
931
+ contentType: "application/json",
932
+ body: JSON.stringify(invokeBody),
933
+ });
934
+ const decoder = new TextDecoder();
935
+ const body = decoder.decode(res.body);
936
+ const result = JSON.parse(body);
937
+ if (!result.embedding) {
938
+ throw new Error("Embeddings not found");
939
+ }
940
+ return {
941
+ values: result.embedding,
942
+ model: modelID,
943
+ token_count: result.inputTextTokenCount
944
+ };
945
+ }
946
+ async generateTwelvelabsMarengoEmbeddings({ text, image, model }) {
947
+ const executor = this.getExecutor();
948
+ // Prepare the request payload for TwelveLabs Marengo
949
+ let invokeBody = {
950
+ inputType: "text"
951
+ };
952
+ if (text) {
953
+ invokeBody.inputText = text;
954
+ invokeBody.inputType = "text";
955
+ }
956
+ if (image) {
957
+ // For the embeddings interface, image is expected to be base64
958
+ invokeBody.mediaSource = {
959
+ base64String: image
960
+ };
961
+ invokeBody.inputType = "image";
962
+ }
963
+ const res = await executor.invokeModel({
964
+ modelId: model,
965
+ contentType: "application/json",
966
+ accept: "application/json",
967
+ body: JSON.stringify(invokeBody),
968
+ });
969
+ const decoder = new TextDecoder();
970
+ const body = decoder.decode(res.body);
971
+ const result = JSON.parse(body);
972
+ // TwelveLabs Marengo returns embedding data
973
+ if (!result.embedding) {
974
+ throw new Error("Embeddings not found in TwelveLabs Marengo response");
975
+ }
976
+ return {
977
+ values: result.embedding,
978
+ model: model,
979
+ // TwelveLabs Marengo doesn't return token count in the same way
980
+ token_count: undefined
981
+ };
982
+ }
983
+ }
984
+ exports.BedrockDriver = BedrockDriver;
985
+ function jobInfo(job, jobId) {
986
+ const jobStatus = job.status;
987
+ let status = core_1.TrainingJobStatus.running;
988
+ let details;
989
+ if (jobStatus === client_bedrock_1.ModelCustomizationJobStatus.COMPLETED) {
990
+ status = core_1.TrainingJobStatus.succeeded;
991
+ }
992
+ else if (jobStatus === client_bedrock_1.ModelCustomizationJobStatus.FAILED) {
993
+ status = core_1.TrainingJobStatus.failed;
994
+ details = job.failureMessage || "error";
995
+ }
996
+ else if (jobStatus === client_bedrock_1.ModelCustomizationJobStatus.STOPPED) {
997
+ status = core_1.TrainingJobStatus.cancelled;
998
+ }
999
+ else {
1000
+ status = core_1.TrainingJobStatus.running;
1001
+ details = jobStatus;
1002
+ }
1003
+ job.baseModelArn;
1004
+ return {
1005
+ id: jobId,
1006
+ model: job.outputModelArn,
1007
+ status,
1008
+ details
1009
+ };
1010
+ }
1011
+ function getToolDefinitions(tools) {
1012
+ return tools ? tools.map(getToolDefinition) : undefined;
1013
+ }
1014
+ function getToolDefinition(tool) {
1015
+ return {
1016
+ toolSpec: {
1017
+ name: tool.name,
1018
+ description: tool.description,
1019
+ inputSchema: {
1020
+ json: tool.input_schema,
1021
+ }
1022
+ }
1023
+ };
1024
+ }
1025
+ /**
1026
+ * Recursively removes undefined values from an object.
1027
+ * AWS Bedrock's additionalModelRequestFields must be valid JSON, and undefined is not valid JSON.
1028
+ * Any unrecognized parameters will cause an exception.
1029
+ */
1030
+ function removeUndefinedValues(obj) {
1031
+ if (obj === null || typeof obj !== 'object' || Array.isArray(obj)) {
1032
+ return obj;
1033
+ }
1034
+ const cleaned = {};
1035
+ for (const [key, value] of Object.entries(obj)) {
1036
+ if (value !== undefined) {
1037
+ if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
1038
+ const cleanedNested = removeUndefinedValues(value);
1039
+ // Only include nested objects if they have properties after cleaning
1040
+ if (Object.keys(cleanedNested).length > 0) {
1041
+ cleaned[key] = cleanedNested;
1042
+ }
1043
+ }
1044
+ else {
1045
+ cleaned[key] = value;
1046
+ }
1047
+ }
1048
+ }
1049
+ return cleaned;
1050
+ }
1051
+ /**
1052
+ * Update the conversation messages
1053
+ * @param prompt
1054
+ * @param response
1055
+ * @returns
1056
+ */
1057
+ function updateConversation(conversation, prompt) {
1058
+ const combinedMessages = [...(conversation?.messages || []), ...(prompt.messages || [])];
1059
+ const combinedSystem = prompt.system || conversation?.system;
1060
+ return {
1061
+ modelId: prompt?.modelId || conversation?.modelId,
1062
+ messages: combinedMessages.length > 0 ? combinedMessages : [],
1063
+ system: combinedSystem && combinedSystem.length > 0 ? combinedSystem : undefined,
1064
+ };
1065
+ }
1066
+ function formatAmazonModalities(modalities) {
1067
+ const standardizedModalities = [];
1068
+ for (const modality of modalities) {
1069
+ if (modality === client_bedrock_1.ModelModality.TEXT) {
1070
+ standardizedModalities.push("text");
1071
+ }
1072
+ else if (modality === client_bedrock_1.ModelModality.IMAGE) {
1073
+ standardizedModalities.push("image");
1074
+ }
1075
+ else if (modality === client_bedrock_1.ModelModality.EMBEDDING) {
1076
+ standardizedModalities.push("embedding");
1077
+ }
1078
+ else if (modality == "SPEECH") {
1079
+ standardizedModalities.push("audio");
1080
+ }
1081
+ else if (modality == "VIDEO") {
1082
+ standardizedModalities.push("video");
1083
+ }
1084
+ else {
1085
+ // Handle other modalities as needed
1086
+ standardizedModalities.push(modality.toString().toLowerCase());
1087
+ }
1088
+ }
1089
+ return standardizedModalities;
1090
+ }
1091
+ //# sourceMappingURL=index.js.map