@effect/ai-amazon-bedrock 0.6.2 → 0.7.1

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 (48) hide show
  1. package/AmazonBedrockTool/package.json +6 -0
  2. package/dist/cjs/AmazonBedrockClient.js +71 -145
  3. package/dist/cjs/AmazonBedrockClient.js.map +1 -1
  4. package/dist/cjs/AmazonBedrockLanguageModel.js +844 -337
  5. package/dist/cjs/AmazonBedrockLanguageModel.js.map +1 -1
  6. package/dist/cjs/AmazonBedrockSchema.js +94 -39
  7. package/dist/cjs/AmazonBedrockSchema.js.map +1 -1
  8. package/dist/cjs/AmazonBedrockTool.js +53 -0
  9. package/dist/cjs/AmazonBedrockTool.js.map +1 -0
  10. package/dist/cjs/EventStreamEncoding.js +5 -4
  11. package/dist/cjs/EventStreamEncoding.js.map +1 -1
  12. package/dist/cjs/index.js +3 -1
  13. package/dist/cjs/internal/utilities.js +3 -1
  14. package/dist/cjs/internal/utilities.js.map +1 -1
  15. package/dist/dts/AmazonBedrockClient.d.ts +34 -34
  16. package/dist/dts/AmazonBedrockClient.d.ts.map +1 -1
  17. package/dist/dts/AmazonBedrockLanguageModel.d.ts +72 -20
  18. package/dist/dts/AmazonBedrockLanguageModel.d.ts.map +1 -1
  19. package/dist/dts/AmazonBedrockSchema.d.ts +543 -102
  20. package/dist/dts/AmazonBedrockSchema.d.ts.map +1 -1
  21. package/dist/dts/AmazonBedrockTool.d.ts +184 -0
  22. package/dist/dts/AmazonBedrockTool.d.ts.map +1 -0
  23. package/dist/dts/EventStreamEncoding.d.ts +2 -2
  24. package/dist/dts/EventStreamEncoding.d.ts.map +1 -1
  25. package/dist/dts/index.d.ts +4 -0
  26. package/dist/dts/index.d.ts.map +1 -1
  27. package/dist/esm/AmazonBedrockClient.js +71 -144
  28. package/dist/esm/AmazonBedrockClient.js.map +1 -1
  29. package/dist/esm/AmazonBedrockLanguageModel.js +841 -333
  30. package/dist/esm/AmazonBedrockLanguageModel.js.map +1 -1
  31. package/dist/esm/AmazonBedrockSchema.js +93 -37
  32. package/dist/esm/AmazonBedrockSchema.js.map +1 -1
  33. package/dist/esm/AmazonBedrockTool.js +45 -0
  34. package/dist/esm/AmazonBedrockTool.js.map +1 -0
  35. package/dist/esm/EventStreamEncoding.js +5 -3
  36. package/dist/esm/EventStreamEncoding.js.map +1 -1
  37. package/dist/esm/index.js +4 -0
  38. package/dist/esm/index.js.map +1 -1
  39. package/dist/esm/internal/utilities.js +2 -0
  40. package/dist/esm/internal/utilities.js.map +1 -1
  41. package/package.json +13 -4
  42. package/src/AmazonBedrockClient.ts +144 -241
  43. package/src/AmazonBedrockLanguageModel.ts +978 -372
  44. package/src/AmazonBedrockSchema.ts +134 -46
  45. package/src/AmazonBedrockTool.ts +251 -0
  46. package/src/EventStreamEncoding.ts +80 -73
  47. package/src/index.ts +5 -0
  48. package/src/internal/utilities.ts +6 -3
@@ -3,30 +3,34 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.withConfigOverride = exports.model = exports.make = exports.layer = exports.ProviderMetadata = exports.Config = void 0;
7
- var _AiError = require("@effect/ai/AiError");
8
- var AiLanguageModel = _interopRequireWildcard(require("@effect/ai/AiLanguageModel"));
9
- var AiModel = _interopRequireWildcard(require("@effect/ai/AiModel"));
10
- var AiResponse = _interopRequireWildcard(require("@effect/ai/AiResponse"));
11
- var _AiTelemetry = require("@effect/ai/AiTelemetry");
12
- var Arr = _interopRequireWildcard(require("effect/Array"));
6
+ exports.withConfigOverride = exports.model = exports.make = exports.layer = exports.Config = void 0;
7
+ var _AnthropicLanguageModel = require("@effect/ai-anthropic/AnthropicLanguageModel");
8
+ var AnthropicTool = _interopRequireWildcard(require("@effect/ai-anthropic/AnthropicTool"));
9
+ var AiError = _interopRequireWildcard(require("@effect/ai/AiError"));
10
+ var LanguageModel = _interopRequireWildcard(require("@effect/ai/LanguageModel"));
11
+ var AiModel = _interopRequireWildcard(require("@effect/ai/Model"));
12
+ var _Telemetry = require("@effect/ai/Telemetry");
13
+ var Tool = _interopRequireWildcard(require("@effect/ai/Tool"));
13
14
  var Context = _interopRequireWildcard(require("effect/Context"));
15
+ var DateTime = _interopRequireWildcard(require("effect/DateTime"));
14
16
  var Effect = _interopRequireWildcard(require("effect/Effect"));
15
17
  var Encoding = _interopRequireWildcard(require("effect/Encoding"));
16
18
  var _Function = require("effect/Function");
17
19
  var Layer = _interopRequireWildcard(require("effect/Layer"));
18
- var Option = _interopRequireWildcard(require("effect/Option"));
19
20
  var Predicate = _interopRequireWildcard(require("effect/Predicate"));
20
21
  var Stream = _interopRequireWildcard(require("effect/Stream"));
21
22
  var _AmazonBedrockClient = require("./AmazonBedrockClient.js");
23
+ var _AmazonBedrockSchema = require("./AmazonBedrockSchema.js");
22
24
  var InternalUtilities = _interopRequireWildcard(require("./internal/utilities.js"));
23
25
  function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
24
26
  /**
25
27
  * @since 1.0.0
26
28
  */
27
29
 
28
- const constDisableValidation = {
29
- disableValidation: true
30
+ const BEDROCK_CACHE_POINT = {
31
+ cachePoint: {
32
+ type: "default"
33
+ }
30
34
  };
31
35
  // =============================================================================
32
36
  // Configuration
@@ -42,23 +46,14 @@ class Config extends /*#__PURE__*/Context.Tag("@effect/ai-amazon-bedrock/AmazonB
42
46
  static getOrUndefined = /*#__PURE__*/Effect.map(/*#__PURE__*/Effect.context(), context => context.unsafeMap.get(Config.key));
43
47
  }
44
48
  // =============================================================================
45
- // Amazon Bedrock Provider Metadata
46
- // =============================================================================
47
- /**
48
- * @since 1.0.0
49
- * @category Context
50
- */
51
- exports.Config = Config;
52
- class ProviderMetadata extends /*#__PURE__*/Context.Tag(InternalUtilities.ProviderMetadataKey)() {}
53
- // =============================================================================
54
49
  // Amazon Bedrock Language Model
55
50
  // =============================================================================
56
51
  /**
57
52
  * @since 1.0.0
58
53
  * @category AiModels
59
54
  */
60
- exports.ProviderMetadata = ProviderMetadata;
61
- const model = (model, config) => AiModel.make(layer({
55
+ exports.Config = Config;
56
+ const model = (model, config) => AiModel.make("amazon-bedrock", layer({
62
57
  model,
63
58
  config
64
59
  }));
@@ -69,107 +64,109 @@ const model = (model, config) => AiModel.make(layer({
69
64
  exports.model = model;
70
65
  const make = exports.make = /*#__PURE__*/Effect.fnUntraced(function* (options) {
71
66
  const client = yield* _AmazonBedrockClient.AmazonBedrockClient;
72
- const makeToolConfig = options => {
73
- if (options.tools.length === 0) return undefined;
74
- const isStructured = options.tools.length === 1 && options.tools[0].structured;
75
- const tools = options.tools.map(tool => ({
76
- toolSpec: {
77
- name: tool.name,
78
- description: tool.description,
79
- inputSchema: {
80
- json: tool.parameters
81
- }
82
- }
83
- }));
84
- const toolConfig = {
85
- tools
86
- };
87
- if (isStructured) {
88
- toolConfig.toolChoice = {
89
- tool: {
90
- name: options.tools[0].name
91
- }
92
- };
93
- return toolConfig;
94
- }
95
- if (options.toolChoice === "auto") {
96
- toolConfig.toolChoice = {
97
- auto: {}
98
- };
99
- } else if (options.toolChoice === "none") {
100
- toolConfig.toolChoice = undefined;
101
- } else if (options.toolChoice === "required") {
102
- toolConfig.toolChoice = {
103
- any: {}
104
- };
105
- } else {
106
- toolConfig.toolChoice = {
107
- tool: {
108
- name: options.toolChoice.tool
109
- }
110
- };
111
- }
112
- return toolConfig;
113
- };
114
- const makeRequest = Effect.fnUntraced(function* (method, {
115
- prompt,
116
- system,
117
- ...rest
118
- }) {
67
+ const makeRequest = Effect.fnUntraced(function* (providerOptions) {
119
68
  const context = yield* Effect.context();
120
- const messages = yield* makeMessages(method, prompt);
121
- const toolConfig = makeToolConfig(rest);
122
- return (0, _Function.identity)({
69
+ const config = {
123
70
  modelId: options.model,
124
71
  ...options.config,
125
- ...context.unsafeMap.get(Config.key),
126
- // TODO: re-evaluate a better way to do this
127
- system: system.pipe(Option.map(text => [{
128
- text
129
- }]), Option.getOrUndefined),
72
+ ...context.unsafeMap.get(Config.key)
73
+ };
74
+ const {
130
75
  messages,
76
+ system
77
+ } = yield* prepareMessages(providerOptions);
78
+ const {
79
+ additionalTools,
80
+ betas,
131
81
  toolConfig
132
- });
82
+ } = yield* prepareTools(providerOptions, config);
83
+ const responseFormat = providerOptions.responseFormat;
84
+ const request = {
85
+ ...config,
86
+ system,
87
+ messages,
88
+ // Handle tool configuration
89
+ ...(responseFormat.type === "json" ? {
90
+ toolConfig: {
91
+ tools: [{
92
+ toolSpec: {
93
+ name: responseFormat.objectName,
94
+ description: Tool.getDescriptionFromSchemaAst(responseFormat.schema.ast) ?? "Respond with a JSON object",
95
+ inputSchema: {
96
+ json: Tool.getJsonSchemaFromSchemaAst(responseFormat.schema.ast)
97
+ }
98
+ }
99
+ }],
100
+ toolChoice: {
101
+ tool: {
102
+ name: responseFormat.objectName
103
+ }
104
+ }
105
+ }
106
+ } : Predicate.isNotUndefined(toolConfig.tools) && toolConfig.tools.length > 0 ? {
107
+ toolConfig
108
+ } : {}),
109
+ // Handle additional model request fields
110
+ ...(Predicate.isNotUndefined(additionalTools) ? {
111
+ additionalModelRequestFields: {
112
+ ...config.additionalModelRequestFields,
113
+ ...additionalTools
114
+ }
115
+ } : {})
116
+ };
117
+ return {
118
+ betas,
119
+ request
120
+ };
133
121
  });
134
- return yield* AiLanguageModel.make({
135
- generateText: Effect.fnUntraced(function* (_options) {
136
- const request = yield* makeRequest("generateText", _options);
137
- annotateRequest(_options.span, request);
138
- const rawResponse = yield* client.client.converse(request);
139
- const context = yield* Effect.context();
140
- const model = Option.match(Context.getOption(context, Config), {
141
- onNone: () => options.model,
142
- onSome: ({
143
- modelId
144
- }) => modelId ?? options.model
122
+ return yield* LanguageModel.make({
123
+ generateText: Effect.fnUntraced(function* (options) {
124
+ const {
125
+ betas,
126
+ request
127
+ } = yield* makeRequest(options);
128
+ annotateRequest(options.span, request);
129
+ const anthropicBeta = betas.size > 0 ? Array.from(betas).join(",") : undefined;
130
+ const rawResponse = yield* client.converse({
131
+ params: {
132
+ "anthropic-beta": anthropicBeta
133
+ },
134
+ payload: request
145
135
  });
146
- annotateChatResponse(_options.span, model, rawResponse);
147
- const response = yield* makeResponse(rawResponse, model);
136
+ annotateResponse(options.span, request, rawResponse);
137
+ return yield* makeResponse(request, rawResponse, options);
138
+ }),
139
+ streamText: Effect.fnUntraced(function* (options) {
140
+ const {
141
+ betas,
142
+ request
143
+ } = yield* makeRequest(options);
144
+ annotateRequest(options.span, request);
145
+ const anthropicBeta = betas.size > 0 ? Array.from(betas).join(",") : undefined;
146
+ const stream = client.converseStream({
147
+ params: {
148
+ "anthropic-beta": anthropicBeta
149
+ },
150
+ payload: request
151
+ });
152
+ return {
153
+ request,
154
+ stream
155
+ };
156
+ }, (effect, options) => effect.pipe(Effect.flatMap(({
157
+ request,
158
+ stream
159
+ }) => makeStreamResponse(request, stream, options)), Stream.unwrap, Stream.map(response => {
160
+ annotateStreamResponse(options.span, response);
148
161
  return response;
149
- }, Effect.catchAll(cause => _AiError.AiError.is(cause) ? cause : new _AiError.AiError({
150
- module: "AmazonBedrockLanguageModel",
151
- method: "generateText",
152
- description: "An error occurred",
153
- cause
154
- }))),
155
- streamText(_options) {
156
- return makeRequest("streamText", _options).pipe(Effect.tap(request => annotateRequest(_options.span, request)), Effect.map(client.stream), Stream.unwrap, Stream.map(response => {
157
- annotateStreamResponse(_options.span, response);
158
- return response;
159
- }), Stream.catchAll(cause => _AiError.AiError.is(cause) ? Effect.fail(cause) : Effect.fail(new _AiError.AiError({
160
- module: "AmazonBedrockLanguageModel",
161
- method: "streamText",
162
- description: "An error occurred",
163
- cause
164
- }))));
165
- }
162
+ })))
166
163
  });
167
164
  });
168
165
  /**
169
166
  * @since 1.0.0
170
167
  * @category Layers
171
168
  */
172
- const layer = options => Layer.effect(AiLanguageModel.AiLanguageModel, make({
169
+ const layer = options => Layer.effect(LanguageModel.LanguageModel, make({
173
170
  model: options.model,
174
171
  config: options.config
175
172
  }));
@@ -182,88 +179,163 @@ const withConfigOverride = exports.withConfigOverride = /*#__PURE__*/(0, _Functi
182
179
  ...config,
183
180
  ...overrides
184
181
  })));
185
- const groupMessages = prompt => {
182
+ // =============================================================================
183
+ // Prompt Conversion
184
+ // =============================================================================
185
+ const prepareMessages = /*#__PURE__*/Effect.fnUntraced(function* (options) {
186
+ const groups = groupMessages(options.prompt);
187
+ const system = [];
186
188
  const messages = [];
187
- let current = undefined;
188
- for (const message of prompt.messages) {
189
- switch (message._tag) {
190
- case "AssistantMessage":
189
+ let documentCounter = 0;
190
+ const nextDocumentName = () => `document-${++documentCounter}`;
191
+ for (let i = 0; i < groups.length; i++) {
192
+ const group = groups[i];
193
+ const isLastGroup = i === groups.length - 1;
194
+ switch (group.type) {
195
+ case "system":
191
196
  {
192
- if (current?.type !== "assistant") {
193
- current = {
194
- type: "assistant",
195
- messages: []
196
- };
197
- messages.push(current);
197
+ if (messages.length > 0) {
198
+ return yield* new AiError.MalformedInput({
199
+ module: "AmazonBedrockLanguageModel",
200
+ method: "prepareMessages",
201
+ description: "Multiple system messages separated by user / assistant messages"
202
+ });
203
+ }
204
+ for (const message of group.messages) {
205
+ system.push({
206
+ text: message.content
207
+ });
208
+ if (Predicate.isNotUndefined(getCachePoint(message))) {
209
+ system.push(BEDROCK_CACHE_POINT);
210
+ }
198
211
  }
199
- current.messages.push(message);
200
212
  break;
201
213
  }
202
- case "ToolMessage":
203
- case "UserMessage":
214
+ case "user":
204
215
  {
205
- if (current?.type !== "user") {
206
- current = {
207
- type: "user",
208
- messages: []
209
- };
210
- messages.push(current);
216
+ const content = [];
217
+ for (const message of group.messages) {
218
+ switch (message.role) {
219
+ case "user":
220
+ {
221
+ for (let j = 0; j < message.content.length; j++) {
222
+ const part = message.content[j];
223
+ switch (part.type) {
224
+ case "text":
225
+ {
226
+ content.push({
227
+ text: part.text
228
+ });
229
+ break;
230
+ }
231
+ case "file":
232
+ {
233
+ if (part.data instanceof URL) {
234
+ // TODO(Max): support this
235
+ return yield* new AiError.MalformedInput({
236
+ module: "AmazonBedrockLanguageModel",
237
+ method: "makeMessages",
238
+ description: "File URL inputs are not supported at this time"
239
+ });
240
+ }
241
+ if (part.mediaType.startsWith("image/")) {
242
+ content.push({
243
+ image: {
244
+ format: yield* getImageFormat(part.mediaType),
245
+ source: {
246
+ bytes: convertToBase64(part.data)
247
+ }
248
+ }
249
+ });
250
+ } else {
251
+ content.push({
252
+ document: {
253
+ format: yield* getDocumentFormat(part.mediaType),
254
+ name: nextDocumentName(),
255
+ source: {
256
+ bytes: convertToBase64(part.data)
257
+ }
258
+ }
259
+ });
260
+ }
261
+ break;
262
+ }
263
+ }
264
+ }
265
+ break;
266
+ }
267
+ case "tool":
268
+ {
269
+ for (const part of message.content) {
270
+ content.push({
271
+ toolResult: {
272
+ toolUseId: part.id,
273
+ content: [{
274
+ text: JSON.stringify(part.result)
275
+ }]
276
+ }
277
+ });
278
+ }
279
+ break;
280
+ }
281
+ }
211
282
  }
212
- current.messages.push(message);
283
+ messages.push({
284
+ role: "user",
285
+ content
286
+ });
213
287
  break;
214
288
  }
215
- }
216
- }
217
- return messages;
218
- };
219
- let fileCounter = 0;
220
- const makeMessages = /*#__PURE__*/Effect.fnUntraced(function* (method, prompt) {
221
- const messages = [];
222
- const groups = groupMessages(prompt);
223
- for (let i = 0; i < groups.length; i++) {
224
- const group = groups[i];
225
- const isLastGroup = i === groups.length - 1;
226
- switch (group.type) {
227
289
  case "assistant":
228
290
  {
229
291
  const content = [];
230
292
  for (let j = 0; j < group.messages.length; j++) {
231
293
  const message = group.messages[j];
232
294
  const isLastMessage = j === group.messages.length - 1;
233
- for (let k = 0; k < message.parts.length; k++) {
234
- const part = message.parts[k];
235
- const isLastPart = k === message.parts.length - 1;
236
- switch (part._tag) {
237
- case "TextPart":
295
+ for (let k = 0; k < message.content.length; k++) {
296
+ const part = message.content[k];
297
+ const isLastPart = k === message.content.length - 1;
298
+ switch (part.type) {
299
+ case "text":
238
300
  {
301
+ // Skip empty text blocks
302
+ if (part.text.trim().length === 0) {
303
+ break;
304
+ }
239
305
  content.push({
306
+ // Amazon Bedrock does not allow trailing whitespace in
307
+ // assistant content blocks
240
308
  text: trimIfLast(isLastGroup, isLastMessage, isLastPart, part.text)
241
309
  });
242
310
  break;
243
311
  }
244
- case "ReasoningPart":
312
+ case "reasoning":
245
313
  {
246
- content.push({
247
- reasoningContent: {
248
- reasoningText: {
249
- text: trimIfLast(isLastGroup, isLastMessage, isLastPart, part.reasoningText),
250
- signature: part.signature
251
- }
314
+ const options = part.options.bedrock;
315
+ if (Predicate.isNotUndefined(options)) {
316
+ if (options.type === "thinking") {
317
+ content.push({
318
+ reasoningContent: {
319
+ reasoningText: {
320
+ // Amazon Bedrock does not allow trailing whitespace in
321
+ // assistant content blocks
322
+ text: trimIfLast(isLastGroup, isLastMessage, isLastPart, part.text),
323
+ signature: options.signature
324
+ }
325
+ }
326
+ });
252
327
  }
253
- });
254
- break;
255
- }
256
- case "RedactedReasoningPart":
257
- {
258
- const redactedContent = Encoding.encodeBase64(part.redactedText);
259
- content.push({
260
- reasoningContent: {
261
- redactedContent
328
+ if (options.type === "redacted_thinking") {
329
+ content.push({
330
+ reasoningContent: {
331
+ redactedContent: options.redactedData
332
+ }
333
+ });
262
334
  }
263
- });
335
+ }
264
336
  break;
265
337
  }
266
- case "ToolCallPart":
338
+ case "tool-call":
267
339
  {
268
340
  content.push({
269
341
  toolUse: {
@@ -276,6 +348,9 @@ const makeMessages = /*#__PURE__*/Effect.fnUntraced(function* (method, prompt) {
276
348
  }
277
349
  }
278
350
  }
351
+ if (getCachePoint(message)) {
352
+ content.push(BEDROCK_CACHE_POINT);
353
+ }
279
354
  }
280
355
  messages.push({
281
356
  role: "assistant",
@@ -283,189 +358,528 @@ const makeMessages = /*#__PURE__*/Effect.fnUntraced(function* (method, prompt) {
283
358
  });
284
359
  break;
285
360
  }
286
- case "user":
361
+ }
362
+ }
363
+ return {
364
+ system,
365
+ messages
366
+ };
367
+ });
368
+ // =============================================================================
369
+ // Response Conversion
370
+ // =============================================================================
371
+ const makeResponse = /*#__PURE__*/Effect.fnUntraced(function* (request, response, options) {
372
+ const parts = [];
373
+ parts.push({
374
+ type: "response-metadata",
375
+ modelId: request.modelId,
376
+ timestamp: DateTime.formatIso(yield* DateTime.now)
377
+ });
378
+ for (const part of response.output.message.content) {
379
+ switch (part.type) {
380
+ case "text":
287
381
  {
288
- const content = [];
289
- for (let j = 0; j < group.messages.length; j++) {
290
- const message = group.messages[j];
291
- switch (message._tag) {
292
- case "ToolMessage":
293
- {
294
- for (let k = 0; k < message.parts.length; k++) {
295
- const part = message.parts[k];
296
- // TODO: support advanced tool result content parts
297
- content.push({
298
- toolResult: {
299
- toolUseId: part.id,
300
- content: [{
301
- text: JSON.stringify(part.result)
302
- }]
382
+ // The text parts should only be added to the response here if the
383
+ // response format is `"text"`. If the response format is `"json"`,
384
+ // then the text parts must instead be added to the response when a
385
+ // tool call is received.
386
+ if (options.responseFormat.type === "text") {
387
+ parts.push({
388
+ type: "text",
389
+ text: part.text
390
+ });
391
+ }
392
+ break;
393
+ }
394
+ case "reasoningContent":
395
+ {
396
+ if (part.reasoningContent.type === "reasoning") {
397
+ const signature = part.reasoningContent.reasoningText.signature;
398
+ parts.push({
399
+ type: "reasoning",
400
+ text: part.reasoningContent.reasoningText.text,
401
+ metadata: Predicate.isNotUndefined(signature) ? {
402
+ bedrock: {
403
+ type: "thinking",
404
+ signature
405
+ }
406
+ } : undefined
407
+ });
408
+ }
409
+ if (part.reasoningContent.type === "redacted-reasoning") {
410
+ parts.push({
411
+ type: "reasoning",
412
+ text: "",
413
+ metadata: {
414
+ bedrock: {
415
+ type: "redacted_thinking",
416
+ redactedData: part.reasoningContent.redactedContent
417
+ }
418
+ }
419
+ });
420
+ }
421
+ break;
422
+ }
423
+ case "toolUse":
424
+ {
425
+ // When a `"json"` response format is requested, the JSON that we need
426
+ // will be returned by the tool call injected into the request
427
+ if (options.responseFormat.type === "json") {
428
+ parts.push({
429
+ type: "text",
430
+ text: JSON.stringify(part.toolUse.input)
431
+ });
432
+ } else {
433
+ const providerTool = AnthropicTool.getProviderDefinedToolName(part.toolUse.name);
434
+ const name = Predicate.isNotUndefined(providerTool) ? providerTool : part.toolUse.name;
435
+ const providerName = Predicate.isNotUndefined(providerTool) ? part.toolUse.name : undefined;
436
+ parts.push({
437
+ type: "tool-call",
438
+ id: part.toolUse.toolUseId,
439
+ name,
440
+ params: part.toolUse.input,
441
+ providerName,
442
+ providerExecuted: false
443
+ });
444
+ }
445
+ break;
446
+ }
447
+ }
448
+ }
449
+ const finishReason = InternalUtilities.resolveFinishReason(response.stopReason);
450
+ parts.push({
451
+ type: "finish",
452
+ reason: finishReason,
453
+ usage: {
454
+ inputTokens: response.usage.inputTokens,
455
+ outputTokens: response.usage.outputTokens,
456
+ totalTokens: response.usage.totalTokens,
457
+ cachedInputTokens: response.usage.cacheReadInputTokens
458
+ },
459
+ metadata: {
460
+ bedrock: {
461
+ trace: response.trace,
462
+ usage: {
463
+ cacheWriteInputTokens: response.usage.cacheWriteInputTokens
464
+ }
465
+ }
466
+ }
467
+ });
468
+ return parts;
469
+ });
470
+ const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* (request, stream, options) {
471
+ const contentBlocks = {};
472
+ let trace = undefined;
473
+ let cacheWriteInputTokens = undefined;
474
+ const usage = {
475
+ inputTokens: undefined,
476
+ outputTokens: undefined,
477
+ totalTokens: undefined
478
+ };
479
+ return stream.pipe(Stream.mapEffect(Effect.fnUntraced(function* (event) {
480
+ const parts = [];
481
+ switch (event.type) {
482
+ case "messageStart":
483
+ {
484
+ parts.push({
485
+ type: "response-metadata",
486
+ modelId: request.modelId,
487
+ timestamp: DateTime.formatIso(yield* DateTime.now)
488
+ });
489
+ break;
490
+ }
491
+ case "messageStop":
492
+ {
493
+ const reason = InternalUtilities.resolveFinishReason(event.messageStop.stopReason);
494
+ parts.push({
495
+ type: "finish",
496
+ reason,
497
+ usage,
498
+ metadata: {
499
+ bedrock: {
500
+ trace,
501
+ usage: {
502
+ cacheWriteInputTokens
503
+ }
504
+ }
505
+ }
506
+ });
507
+ break;
508
+ }
509
+ case "contentBlockStart":
510
+ {
511
+ const index = event.contentBlockStart.contentBlockIndex;
512
+ const block = event.contentBlockStart;
513
+ if (Predicate.isNotUndefined(block.start.toolUse)) {
514
+ const toolUse = block.start.toolUse;
515
+ const toolName = toolUse.name;
516
+ const providerTool = AnthropicTool.getProviderDefinedToolName(toolName);
517
+ const name = Predicate.isNotUndefined(providerTool) ? providerTool : toolName;
518
+ const providerName = Predicate.isNotUndefined(providerTool) ? toolName : undefined;
519
+ contentBlocks[index] = {
520
+ type: "tool-call",
521
+ id: toolUse.toolUseId,
522
+ name,
523
+ params: "",
524
+ providerName,
525
+ providerExecuted: false
526
+ };
527
+ // Only emit tool param delta events for text responses
528
+ if (options.responseFormat.type === "text") {
529
+ parts.push({
530
+ type: "tool-params-start",
531
+ id: toolUse.toolUseId,
532
+ name: toolUse.name,
533
+ providerExecuted: false
534
+ });
535
+ }
536
+ } else {
537
+ contentBlocks[index] = {
538
+ type: "text"
539
+ };
540
+ parts.push({
541
+ type: "text-start",
542
+ id: index.toString()
543
+ });
544
+ }
545
+ break;
546
+ }
547
+ case "contentBlockDelta":
548
+ {
549
+ const index = event.contentBlockDelta.contentBlockIndex;
550
+ const delta = event.contentBlockDelta.delta;
551
+ switch (delta.type) {
552
+ case "text":
553
+ {
554
+ const block = contentBlocks[index];
555
+ if (Predicate.isUndefined(block)) {
556
+ contentBlocks[index] = {
557
+ type: "text"
558
+ };
559
+ // Only emit text delta events for text responses
560
+ if (options.responseFormat.type === "text") {
561
+ parts.push({
562
+ type: "text-start",
563
+ id: index.toString()
564
+ });
565
+ }
566
+ }
567
+ // Only emit text delta events for text responses
568
+ if (options.responseFormat.type === "text") {
569
+ parts.push({
570
+ type: "text-delta",
571
+ id: index.toString(),
572
+ delta: delta.text
573
+ });
574
+ }
575
+ break;
576
+ }
577
+ case "reasoningContent":
578
+ {
579
+ if ("text" in delta.reasoningContent) {
580
+ const block = contentBlocks[index];
581
+ if (Predicate.isUndefined(block)) {
582
+ contentBlocks[index] = {
583
+ type: "reasoning"
584
+ };
585
+ parts.push({
586
+ type: "reasoning-start",
587
+ id: index.toString()
588
+ });
589
+ }
590
+ parts.push({
591
+ type: "reasoning-delta",
592
+ id: index.toString(),
593
+ delta: delta.reasoningContent.text
594
+ });
595
+ } else if ("signature" in delta.reasoningContent) {
596
+ parts.push({
597
+ type: "reasoning-delta",
598
+ id: index.toString(),
599
+ delta: "",
600
+ metadata: {
601
+ bedrock: {
602
+ type: "thinking",
603
+ signature: delta.reasoningContent.signature
604
+ }
605
+ }
606
+ });
607
+ } else {
608
+ parts.push({
609
+ type: "reasoning-delta",
610
+ id: index.toString(),
611
+ delta: "",
612
+ metadata: {
613
+ bedrock: {
614
+ type: "redacted_thinking",
615
+ redactedData: delta.reasoningContent.redactedContent
303
616
  }
617
+ }
618
+ });
619
+ }
620
+ break;
621
+ }
622
+ case "toolUse":
623
+ {
624
+ const block = contentBlocks[index];
625
+ if (Predicate.isNotUndefined(block) && block.type === "tool-call") {
626
+ const params = delta.toolUse.input;
627
+ // Only emit tool params delta events for text responses
628
+ if (options.responseFormat.type === "text") {
629
+ parts.push({
630
+ type: "tool-params-delta",
631
+ id: block.id,
632
+ delta: params
633
+ });
634
+ }
635
+ block.params += params;
636
+ }
637
+ break;
638
+ }
639
+ }
640
+ break;
641
+ }
642
+ case "contentBlockStop":
643
+ {
644
+ const index = event.contentBlockStop.contentBlockIndex;
645
+ const block = contentBlocks[index];
646
+ if (Predicate.isNotUndefined(block)) {
647
+ switch (block.type) {
648
+ case "text":
649
+ {
650
+ // Only emit text end events for text responses
651
+ if (options.responseFormat.type === "text") {
652
+ parts.push({
653
+ type: "text-end",
654
+ id: index.toString()
304
655
  });
305
656
  }
306
657
  break;
307
658
  }
308
- case "UserMessage":
659
+ case "reasoning":
309
660
  {
310
- for (let k = 0; k < message.parts.length; k++) {
311
- const part = message.parts[k];
312
- switch (part._tag) {
313
- case "FilePart":
314
- {
315
- if (Predicate.isUndefined(part.mediaType)) {
316
- return yield* new _AiError.AiError({
317
- module: "AmazonBedrockLanguageModel",
318
- method,
319
- description: "No media type specified for image"
320
- });
321
- }
322
- content.push({
323
- document: {
324
- name: part.name ?? `file-${fileCounter++}`,
325
- format: part.mediaType.split("/")?.[1],
326
- source: {
327
- bytes: Encoding.encodeBase64(part.data)
328
- }
329
- }
330
- });
331
- break;
332
- }
333
- case "FileUrlPart":
334
- {
335
- // TODO: maybe auto-download images from URL
336
- return yield* new _AiError.AiError({
337
- module: "AmazonBedrockLanguageModel",
338
- method,
339
- description: "File URLs are not supported at this time"
340
- });
341
- }
342
- case "ImagePart":
343
- {
344
- if (Predicate.isUndefined(part.mediaType)) {
345
- return yield* new _AiError.AiError({
346
- module: "AmazonBedrockLanguageModel",
347
- method,
348
- description: "No media type specified for image"
349
- });
350
- }
351
- content.push({
352
- image: {
353
- format: part.mediaType.split("/")?.[1],
354
- source: {
355
- bytes: Encoding.encodeBase64(part.data)
356
- }
357
- }
358
- });
359
- break;
360
- }
361
- case "ImageUrlPart":
362
- {
363
- // TODO: maybe auto-download images from URL
364
- return yield* new _AiError.AiError({
365
- module: "AmazonBedrockLanguageModel",
366
- method,
367
- description: "Image URLs are not supported at this time"
368
- });
369
- }
370
- case "TextPart":
371
- {
372
- content.push({
373
- text: part.text
374
- });
375
- break;
376
- }
377
- }
661
+ parts.push({
662
+ type: "reasoning-end",
663
+ id: index.toString()
664
+ });
665
+ break;
666
+ }
667
+ case "tool-call":
668
+ {
669
+ // Only emit tool param events for text responses - for JSON
670
+ // responses, the structured output will be emitted as text
671
+ if (options.responseFormat.type === "text") {
672
+ parts.push({
673
+ type: "tool-params-end",
674
+ id: block.id
675
+ });
676
+ const toolName = block.name;
677
+ const toolParams = block.params;
678
+ const params = yield* Effect.try({
679
+ try: () => Tool.unsafeSecureJsonParse(toolParams),
680
+ catch: cause => new AiError.MalformedOutput({
681
+ module: "AmazonBedrockLanguageModel",
682
+ method: "makeStreamResponse",
683
+ description: "Failed to securely parse tool call parameters " + `for tool '${toolName}':\nParameters: ${toolParams}`,
684
+ cause
685
+ })
686
+ });
687
+ parts.push({
688
+ type: "tool-call",
689
+ id: block.id,
690
+ name: toolName,
691
+ params,
692
+ providerName: block.providerName,
693
+ providerExecuted: block.providerExecuted
694
+ });
695
+ } else {
696
+ parts.push({
697
+ type: "text-start",
698
+ id: index.toString()
699
+ });
700
+ parts.push({
701
+ type: "text-delta",
702
+ id: index.toString(),
703
+ delta: block.params
704
+ });
705
+ parts.push({
706
+ type: "text-end",
707
+ id: index.toString()
708
+ });
378
709
  }
379
710
  break;
380
711
  }
381
712
  }
713
+ delete contentBlocks[index];
382
714
  }
383
- messages.push({
384
- role: "user",
385
- content
715
+ break;
716
+ }
717
+ case "metadata":
718
+ {
719
+ usage.inputTokens = event.metadata.usage.inputTokens;
720
+ usage.outputTokens = event.metadata.usage.outputTokens;
721
+ usage.totalTokens = event.metadata.usage.totalTokens;
722
+ usage.cachedInputTokens = event.metadata.usage.cacheReadInputTokens;
723
+ if (Predicate.isNotUndefined(event.metadata.usage.cacheWriteInputTokens)) {
724
+ cacheWriteInputTokens = event.metadata.usage.cacheWriteInputTokens;
725
+ }
726
+ if (Predicate.isNotUndefined(event.metadata.trace)) {
727
+ trace = event.metadata.trace;
728
+ }
729
+ break;
730
+ }
731
+ case "internalServerException":
732
+ {
733
+ parts.push({
734
+ type: "error",
735
+ error: event.internalServerException
736
+ });
737
+ break;
738
+ }
739
+ case "modelStreamErrorException":
740
+ {
741
+ parts.push({
742
+ type: "error",
743
+ error: event.modelStreamErrorException
744
+ });
745
+ break;
746
+ }
747
+ case "serviceUnavailableException":
748
+ {
749
+ parts.push({
750
+ type: "error",
751
+ error: event.serviceUnavailableException
752
+ });
753
+ break;
754
+ }
755
+ case "throttlingException":
756
+ {
757
+ parts.push({
758
+ type: "error",
759
+ error: event.throttlingException
760
+ });
761
+ break;
762
+ }
763
+ case "validationException":
764
+ {
765
+ parts.push({
766
+ type: "error",
767
+ error: event.validationException
386
768
  });
387
769
  break;
388
770
  }
389
771
  }
390
- }
391
- if (Arr.isNonEmptyReadonlyArray(messages)) {
392
- return messages;
393
- }
394
- return yield* new _AiError.AiError({
395
- module: "AmazonBedrockLanguageModel",
396
- method,
397
- description: "Prompt contained no messages"
398
- });
772
+ return parts;
773
+ })), Stream.flattenIterables);
399
774
  });
400
- const makeResponse = /*#__PURE__*/Effect.fnUntraced(function* (response, modelId) {
401
- const parts = [];
402
- parts.push(new AiResponse.MetadataPart({
403
- model: modelId
404
- }, constDisableValidation));
405
- const finishReason = InternalUtilities.resolveFinishReason(response.stopReason);
406
- const metadata = {
407
- metrics: response.metrics
408
- };
409
- if (Predicate.isNotUndefined(response.trace)) {
410
- metadata.trace = response.trace;
775
+ // =============================================================================
776
+ // Tool Calling
777
+ // =============================================================================
778
+ const prepareTools = /*#__PURE__*/Effect.fnUntraced(function* (options, config) {
779
+ const betas = new Set();
780
+ if (options.tools.length === 0) {
781
+ return {
782
+ toolConfig: {},
783
+ betas
784
+ };
411
785
  }
412
- if (Predicate.isNotUndefined(response.performanceConfig)) {
413
- metadata.performanceConfig = response.performanceConfig;
786
+ const isAnthropicModel = config.modelId.includes("anthropic.");
787
+ const userDefinedTools = [];
788
+ const providerDefinedTools = [];
789
+ for (const tool of options.tools) {
790
+ if (Tool.isUserDefined(tool)) {
791
+ userDefinedTools.push(tool);
792
+ } else {
793
+ providerDefinedTools.push(tool);
794
+ }
414
795
  }
415
- parts.push(new AiResponse.FinishPart({
416
- reason: finishReason,
417
- usage: {
418
- inputTokens: response.usage.inputTokens,
419
- outputTokens: response.usage.outputTokens,
420
- totalTokens: response.usage.totalTokens,
421
- reasoningTokens: 0,
422
- cacheReadInputTokens: response.usage.cacheReadInputTokens ?? 0,
423
- cacheWriteInputTokens: response.usage.cacheWriteInputTokens ?? 0
424
- },
425
- providerMetadata: {
426
- [InternalUtilities.ProviderMetadataKey]: metadata
796
+ const hasAnthropicTools = isAnthropicModel && providerDefinedTools.length > 0;
797
+ let tools = [];
798
+ let additionalTools = undefined;
799
+ // Handle Anthropic provider-defined tools for Anthropic models on Bedrock
800
+ if (hasAnthropicTools) {
801
+ const prepared = yield* (0, _AnthropicLanguageModel.prepareTools)(options, {});
802
+ prepared.betas.forEach(beta => betas.add(beta));
803
+ if (Predicate.isNotUndefined(prepared.toolChoice)) {
804
+ // For Anthropic tools on Bedrock, only the 'tool_choice' goes into
805
+ // the `additionalModelRequestFields` parameter of the request, while
806
+ // the tool definitions themselves are sent in the usual `toolConfig`
807
+ additionalTools = {
808
+ tool_choice: prepared.toolChoice
809
+ };
427
810
  }
428
- }, constDisableValidation));
429
- for (const part of response.output.message.content) {
430
- if ("text" in part) {
431
- parts.push(new AiResponse.TextPart({
432
- text: part.text
433
- }, constDisableValidation));
811
+ // Handle conversion of provider-defined tools to Amazon Bedrock tool definitions
812
+ for (const providerDefinedTool of providerDefinedTools) {
813
+ tools.push({
814
+ toolSpec: {
815
+ name: providerDefinedTool.providerName,
816
+ description: Tool.getDescription(providerDefinedTool),
817
+ inputSchema: {
818
+ json: Tool.getJsonSchema(providerDefinedTool)
819
+ }
820
+ }
821
+ });
434
822
  }
435
- if ("reasoningContent" in part) {
436
- const content = part.reasoningContent;
437
- if ("reasoningText" in content) {
438
- parts.push(new AiResponse.ReasoningPart({
439
- reasoningText: content.reasoningText.text,
440
- signature: content.reasoningText.signature
441
- }, constDisableValidation));
442
- }
443
- if ("redactedContent" in content) {
444
- parts.push(new AiResponse.RedactedReasoningPart({
445
- redactedText: Encoding.encodeBase64(content.redactedContent)
446
- }, constDisableValidation));
823
+ }
824
+ // Handle conversion of user-defined tools to Amazon Bedrock tool definitions
825
+ for (const tool of userDefinedTools) {
826
+ tools.push({
827
+ toolSpec: {
828
+ name: tool.name,
829
+ description: Tool.getDescription(tool),
830
+ inputSchema: {
831
+ json: Tool.getJsonSchema(tool)
832
+ }
447
833
  }
448
- }
449
- if ("toolUse" in part) {
450
- parts.push(AiResponse.ToolCallPart.fromUnknown({
451
- id: part.toolUse.toolUseId,
452
- name: part.toolUse.name,
453
- params: part.toolUse.input
454
- }));
834
+ });
835
+ }
836
+ // Handle resolution of tool choice for **Amazon Bedrock** user-defined tools.
837
+ // The tool choice for Anthropic provider-defined tools is resolved above and
838
+ // inserted into the additional tool configuration object.
839
+ let toolChoice = undefined;
840
+ if (!hasAnthropicTools && tools.length > 0 && Predicate.isNotUndefined(options.toolChoice)) {
841
+ if (options.toolChoice === "none") {
842
+ tools.length = 0;
843
+ toolChoice = undefined;
844
+ } else if (options.toolChoice === "auto") {
845
+ toolChoice = {
846
+ auto: {}
847
+ };
848
+ } else if (options.toolChoice === "required") {
849
+ toolChoice = {
850
+ any: {}
851
+ };
852
+ } else if ("tool" in options.toolChoice) {
853
+ toolChoice = {
854
+ tool: {
855
+ name: options.toolChoice.tool
856
+ }
857
+ };
858
+ } else {
859
+ const allowedTools = new Set(options.toolChoice.oneOf);
860
+ tools = tools.filter(tool => allowedTools.has(tool.toolSpec?.name));
861
+ toolChoice = options.toolChoice.mode === "auto" ? {
862
+ auto: {}
863
+ } : {
864
+ any: {}
865
+ };
455
866
  }
456
867
  }
457
- return new AiResponse.AiResponse({
458
- parts
459
- }, constDisableValidation);
868
+ const toolConfig = tools.length > 0 ? {
869
+ tools,
870
+ toolChoice
871
+ } : {};
872
+ return {
873
+ additionalTools,
874
+ betas,
875
+ toolConfig
876
+ };
460
877
  });
461
- /**
462
- * Amazon Bedrock does not allow trailing whitespace in pre-fillled assistant
463
- * responses, so we trim the final text part here if it's the last message in
464
- * the group.
465
- */
466
- const trimIfLast = (isLastGroup, isLastMessage, isLastPart, text) => isLastGroup && isLastMessage && isLastPart ? text.trim() : text;
878
+ // =============================================================================
879
+ // Telemetry
880
+ // =============================================================================
467
881
  const annotateRequest = (span, request) => {
468
- (0, _AiTelemetry.addGenAIAnnotations)(span, {
882
+ (0, _Telemetry.addGenAIAnnotations)(span, {
469
883
  system: "anthropic",
470
884
  operation: {
471
885
  name: "chat"
@@ -479,10 +893,10 @@ const annotateRequest = (span, request) => {
479
893
  }
480
894
  });
481
895
  };
482
- const annotateChatResponse = (span, modelId, response) => {
483
- (0, _AiTelemetry.addGenAIAnnotations)(span, {
896
+ const annotateResponse = (span, request, response) => {
897
+ (0, _Telemetry.addGenAIAnnotations)(span, {
484
898
  response: {
485
- model: modelId,
899
+ model: request.modelId,
486
900
  finishReasons: response.stopReason ? [response.stopReason] : undefined
487
901
  },
488
902
  usage: {
@@ -491,19 +905,112 @@ const annotateChatResponse = (span, modelId, response) => {
491
905
  }
492
906
  });
493
907
  };
494
- const annotateStreamResponse = (span, response) => {
495
- const metadataPart = response.parts.find(part => part._tag === "MetadataPart");
496
- const finishPart = response.parts.find(part => part._tag === "FinishPart");
497
- (0, _AiTelemetry.addGenAIAnnotations)(span, {
498
- response: {
499
- id: metadataPart?.id,
500
- model: metadataPart?.model,
501
- finishReasons: finishPart?.reason ? [finishPart.reason] : undefined
502
- },
503
- usage: {
504
- inputTokens: finishPart?.usage.inputTokens,
505
- outputTokens: finishPart?.usage.outputTokens
908
+ const annotateStreamResponse = (span, part) => {
909
+ if (part.type === "response-metadata") {
910
+ (0, _Telemetry.addGenAIAnnotations)(span, {
911
+ response: {
912
+ id: part.id,
913
+ model: part.modelId
914
+ }
915
+ });
916
+ }
917
+ if (part.type === "finish") {
918
+ (0, _Telemetry.addGenAIAnnotations)(span, {
919
+ response: {
920
+ finishReasons: [part.reason]
921
+ },
922
+ usage: {
923
+ inputTokens: part.usage.inputTokens,
924
+ outputTokens: part.usage.outputTokens
925
+ }
926
+ });
927
+ }
928
+ };
929
+ const groupMessages = prompt => {
930
+ const messages = [];
931
+ let current = undefined;
932
+ for (const message of prompt.content) {
933
+ switch (message.role) {
934
+ case "system":
935
+ {
936
+ if (current?.type !== "system") {
937
+ current = {
938
+ type: "system",
939
+ messages: []
940
+ };
941
+ messages.push(current);
942
+ }
943
+ current.messages.push(message);
944
+ break;
945
+ }
946
+ case "assistant":
947
+ {
948
+ if (current?.type !== "assistant") {
949
+ current = {
950
+ type: "assistant",
951
+ messages: []
952
+ };
953
+ messages.push(current);
954
+ }
955
+ current.messages.push(message);
956
+ break;
957
+ }
958
+ case "tool":
959
+ case "user":
960
+ {
961
+ if (current?.type !== "user") {
962
+ current = {
963
+ type: "user",
964
+ messages: []
965
+ };
966
+ messages.push(current);
967
+ }
968
+ current.messages.push(message);
969
+ break;
970
+ }
506
971
  }
507
- });
972
+ }
973
+ return messages;
508
974
  };
975
+ /**
976
+ * Amazon Bedrock does not allow trailing whitespace in pre-fillled assistant
977
+ * responses, so we trim the final text part here if it's the last message in
978
+ * the group.
979
+ */
980
+ const trimIfLast = (isLastGroup, isLastMessage, isLastPart, text) => isLastGroup && isLastMessage && isLastPart ? text.trim() : text;
981
+ const getCachePoint = part => part.options.bedrock?.cachePoint;
982
+ const convertToBase64 = data => typeof data === "string" ? data : Encoding.encodeBase64(data);
983
+ const DOCUMENT_MIME_TYPES = {
984
+ "application/pdf": "pdf",
985
+ "text/csv": "csv",
986
+ "application/msword": "doc",
987
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
988
+ "application/vnd.ms-excel": "xls",
989
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
990
+ "text/html": "html",
991
+ "text/plain": "txt",
992
+ "text/markdown": "md"
993
+ };
994
+ const getDocumentFormat = /*#__PURE__*/Effect.fnUntraced(function* (mediaType) {
995
+ const format = DOCUMENT_MIME_TYPES[mediaType];
996
+ if (Predicate.isUndefined(format)) {
997
+ return yield* new AiError.MalformedInput({
998
+ module: "AmazonBedrockLanguageModel",
999
+ method: "getDocumentFormat",
1000
+ description: `Unsupported document MIME type: ${mediaType} - expected ` + `one of: ${Object.keys(DOCUMENT_MIME_TYPES)}`
1001
+ });
1002
+ }
1003
+ return format;
1004
+ });
1005
+ const getImageFormat = /*#__PURE__*/Effect.fnUntraced(function* (mediaType) {
1006
+ const format = _AmazonBedrockSchema.ImageFormat.literals.find(format => mediaType === `image/${format}`);
1007
+ if (Predicate.isUndefined(format)) {
1008
+ return yield* new AiError.MalformedInput({
1009
+ module: "AmazonBedrockLanguageModel",
1010
+ method: "getImageFormat",
1011
+ description: `Unsupported image MIME type: ${mediaType} - expected ` + `one of: ${_AmazonBedrockSchema.ImageFormat.literals.map(format => `image/${format}`).join(",")}`
1012
+ });
1013
+ }
1014
+ return format;
1015
+ });
509
1016
  //# sourceMappingURL=AmazonBedrockLanguageModel.js.map