@ai-sdk/amazon-bedrock 3.0.0-alpha.9 → 3.0.0-beta.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.
package/dist/index.mjs CHANGED
@@ -157,7 +157,7 @@ function prepareTools({
157
157
  name: tool.name,
158
158
  description: tool.description,
159
159
  inputSchema: {
160
- json: tool.parameters
160
+ json: tool.inputSchema
161
161
  }
162
162
  }
163
163
  });
@@ -207,12 +207,7 @@ function prepareTools({
207
207
  import {
208
208
  UnsupportedFunctionalityError as UnsupportedFunctionalityError2
209
209
  } from "@ai-sdk/provider";
210
- import {
211
- convertToBase64,
212
- createIdGenerator,
213
- parseProviderOptions
214
- } from "@ai-sdk/provider-utils";
215
- var generateFileId = createIdGenerator({ prefix: "file", size: 16 });
210
+ import { convertToBase64, parseProviderOptions } from "@ai-sdk/provider-utils";
216
211
  function getCachePoint(providerMetadata) {
217
212
  var _a;
218
213
  return (_a = providerMetadata == null ? void 0 : providerMetadata.bedrock) == null ? void 0 : _a.cachePoint;
@@ -222,6 +217,8 @@ async function convertToBedrockChatMessages(prompt) {
222
217
  const blocks = groupIntoBlocks(prompt);
223
218
  let system = [];
224
219
  const messages = [];
220
+ let documentCounter = 0;
221
+ const generateDocumentName = () => `document-${++documentCounter}`;
225
222
  for (let i = 0; i < blocks.length; i++) {
226
223
  const block = blocks[i];
227
224
  const isLastBlock = i === blocks.length - 1;
@@ -276,7 +273,7 @@ async function convertToBedrockChatMessages(prompt) {
276
273
  format: (_d = (_c = part.mediaType) == null ? void 0 : _c.split(
277
274
  "/"
278
275
  )) == null ? void 0 : _d[1],
279
- name: generateFileId(),
276
+ name: generateDocumentName(),
280
277
  source: { bytes: convertToBase64(part.data) }
281
278
  }
282
279
  });
@@ -288,36 +285,49 @@ async function convertToBedrockChatMessages(prompt) {
288
285
  break;
289
286
  }
290
287
  case "tool": {
291
- for (let i2 = 0; i2 < content.length; i2++) {
292
- const part = content[i2];
293
- const toolResultContent = part.content != void 0 ? part.content.map((part2) => {
294
- switch (part2.type) {
295
- case "text":
296
- return {
297
- text: part2.text
298
- };
299
- case "image":
300
- if (!part2.mediaType) {
301
- throw new Error(
302
- "Image mime type is required in tool result part content"
303
- );
304
- }
305
- const format = part2.mediaType.split("/")[1];
306
- if (!isBedrockImageFormat(format)) {
307
- throw new Error(
308
- `Unsupported image format: ${format}`
309
- );
310
- }
311
- return {
312
- image: {
313
- format,
314
- source: {
315
- bytes: part2.data
288
+ for (const part of content) {
289
+ let toolResultContent;
290
+ const output = part.output;
291
+ switch (output.type) {
292
+ case "content": {
293
+ toolResultContent = output.value.map((contentPart) => {
294
+ switch (contentPart.type) {
295
+ case "text":
296
+ return { text: contentPart.text };
297
+ case "media":
298
+ if (!contentPart.mediaType.startsWith("image/")) {
299
+ throw new UnsupportedFunctionalityError2({
300
+ functionality: `media type: ${contentPart.mediaType}`
301
+ });
316
302
  }
317
- }
318
- };
303
+ const format = contentPart.mediaType.split("/")[1];
304
+ if (!isBedrockImageFormat(format)) {
305
+ throw new UnsupportedFunctionalityError2({
306
+ functionality: `media type: ${contentPart.mediaType}`
307
+ });
308
+ }
309
+ return {
310
+ image: {
311
+ format,
312
+ source: { bytes: contentPart.data }
313
+ }
314
+ };
315
+ }
316
+ });
317
+ break;
319
318
  }
320
- }) : [{ text: JSON.stringify(part.result) }];
319
+ case "text":
320
+ case "error-text":
321
+ toolResultContent = [{ text: output.value }];
322
+ break;
323
+ case "json":
324
+ case "error-json":
325
+ default:
326
+ toolResultContent = [
327
+ { text: JSON.stringify(output.value) }
328
+ ];
329
+ break;
330
+ }
321
331
  bedrockContent.push({
322
332
  toolResult: {
323
333
  toolUseId: part.toolCallId,
@@ -406,7 +416,7 @@ async function convertToBedrockChatMessages(prompt) {
406
416
  toolUse: {
407
417
  toolUseId: part.toolCallId,
408
418
  name: part.toolName,
409
- input: part.args
419
+ input: part.input
410
420
  }
411
421
  });
412
422
  break;
@@ -675,10 +685,9 @@ var BedrockChatLanguageModel = class {
675
685
  if (part.toolUse) {
676
686
  content.push({
677
687
  type: "tool-call",
678
- toolCallType: "function",
679
688
  toolCallId: (_c = (_b = part.toolUse) == null ? void 0 : _b.toolUseId) != null ? _c : this.config.generateId(),
680
689
  toolName: (_e = (_d = part.toolUse) == null ? void 0 : _d.name) != null ? _e : `tool-${this.config.generateId()}`,
681
- args: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : "")
690
+ input: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : "")
682
691
  });
683
692
  }
684
693
  }
@@ -736,7 +745,7 @@ var BedrockChatLanguageModel = class {
736
745
  totalTokens: void 0
737
746
  };
738
747
  let providerMetadata = void 0;
739
- const toolCallContentBlocks = {};
748
+ const contentBlocks = {};
740
749
  return {
741
750
  stream: response.pipeThrough(
742
751
  new TransformStream({
@@ -744,11 +753,14 @@ var BedrockChatLanguageModel = class {
744
753
  controller.enqueue({ type: "stream-start", warnings });
745
754
  },
746
755
  transform(chunk, controller) {
747
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
756
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
748
757
  function enqueueError(bedrockError) {
749
758
  finishReason = "error";
750
759
  controller.enqueue({ type: "error", error: bedrockError });
751
760
  }
761
+ if (options.includeRawChunks) {
762
+ controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
763
+ }
752
764
  if (!chunk.success) {
753
765
  enqueueError(chunk.error);
754
766
  return;
@@ -797,78 +809,126 @@ var BedrockChatLanguageModel = class {
797
809
  };
798
810
  }
799
811
  }
800
- if (((_j = value.contentBlockDelta) == null ? void 0 : _j.delta) && "text" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.text) {
812
+ if (((_j = value.contentBlockStart) == null ? void 0 : _j.contentBlockIndex) != null && !((_l = (_k = value.contentBlockStart) == null ? void 0 : _k.start) == null ? void 0 : _l.toolUse)) {
813
+ const blockIndex = value.contentBlockStart.contentBlockIndex;
814
+ contentBlocks[blockIndex] = { type: "text" };
815
+ controller.enqueue({
816
+ type: "text-start",
817
+ id: String(blockIndex)
818
+ });
819
+ }
820
+ if (((_m = value.contentBlockDelta) == null ? void 0 : _m.delta) && "text" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.text) {
821
+ const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
822
+ if (contentBlocks[blockIndex] == null) {
823
+ contentBlocks[blockIndex] = { type: "text" };
824
+ controller.enqueue({
825
+ type: "text-start",
826
+ id: String(blockIndex)
827
+ });
828
+ }
801
829
  controller.enqueue({
802
- type: "text",
803
- text: value.contentBlockDelta.delta.text
830
+ type: "text-delta",
831
+ id: String(blockIndex),
832
+ delta: value.contentBlockDelta.delta.text
804
833
  });
805
834
  }
806
- if (((_k = value.contentBlockDelta) == null ? void 0 : _k.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
835
+ if (((_n = value.contentBlockStop) == null ? void 0 : _n.contentBlockIndex) != null) {
836
+ const blockIndex = value.contentBlockStop.contentBlockIndex;
837
+ const contentBlock = contentBlocks[blockIndex];
838
+ if (contentBlock != null) {
839
+ if (contentBlock.type === "reasoning") {
840
+ controller.enqueue({
841
+ type: "reasoning-end",
842
+ id: String(blockIndex)
843
+ });
844
+ } else if (contentBlock.type === "text") {
845
+ controller.enqueue({
846
+ type: "text-end",
847
+ id: String(blockIndex)
848
+ });
849
+ } else if (contentBlock.type === "tool-call") {
850
+ controller.enqueue({
851
+ type: "tool-input-end",
852
+ id: contentBlock.toolCallId
853
+ });
854
+ controller.enqueue({
855
+ type: "tool-call",
856
+ toolCallId: contentBlock.toolCallId,
857
+ toolName: contentBlock.toolName,
858
+ input: contentBlock.jsonText
859
+ });
860
+ }
861
+ delete contentBlocks[blockIndex];
862
+ }
863
+ }
864
+ if (((_o = value.contentBlockDelta) == null ? void 0 : _o.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
865
+ const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
807
866
  const reasoningContent = value.contentBlockDelta.delta.reasoningContent;
808
867
  if ("text" in reasoningContent && reasoningContent.text) {
868
+ if (contentBlocks[blockIndex] == null) {
869
+ contentBlocks[blockIndex] = { type: "reasoning" };
870
+ controller.enqueue({
871
+ type: "reasoning-start",
872
+ id: String(blockIndex)
873
+ });
874
+ }
809
875
  controller.enqueue({
810
- type: "reasoning",
811
- text: reasoningContent.text
876
+ type: "reasoning-delta",
877
+ id: String(blockIndex),
878
+ delta: reasoningContent.text
812
879
  });
813
880
  } else if ("signature" in reasoningContent && reasoningContent.signature) {
814
881
  controller.enqueue({
815
- type: "reasoning",
816
- text: "",
882
+ type: "reasoning-delta",
883
+ id: String(blockIndex),
884
+ delta: "",
817
885
  providerMetadata: {
818
886
  bedrock: {
819
887
  signature: reasoningContent.signature
820
888
  }
821
889
  }
822
890
  });
823
- controller.enqueue({ type: "reasoning-part-finish" });
824
891
  } else if ("data" in reasoningContent && reasoningContent.data) {
825
892
  controller.enqueue({
826
- type: "reasoning",
827
- text: "",
893
+ type: "reasoning-delta",
894
+ id: String(blockIndex),
895
+ delta: "",
828
896
  providerMetadata: {
829
897
  bedrock: {
830
898
  redactedData: reasoningContent.data
831
899
  }
832
900
  }
833
901
  });
834
- controller.enqueue({ type: "reasoning-part-finish" });
835
902
  }
836
903
  }
837
904
  const contentBlockStart = value.contentBlockStart;
838
- if (((_l = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _l.toolUse) != null) {
905
+ if (((_p = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _p.toolUse) != null) {
839
906
  const toolUse = contentBlockStart.start.toolUse;
840
- toolCallContentBlocks[contentBlockStart.contentBlockIndex] = {
907
+ const blockIndex = contentBlockStart.contentBlockIndex;
908
+ contentBlocks[blockIndex] = {
909
+ type: "tool-call",
841
910
  toolCallId: toolUse.toolUseId,
842
911
  toolName: toolUse.name,
843
912
  jsonText: ""
844
913
  };
845
- }
846
- const contentBlockDelta = value.contentBlockDelta;
847
- if ((contentBlockDelta == null ? void 0 : contentBlockDelta.delta) && "toolUse" in contentBlockDelta.delta && contentBlockDelta.delta.toolUse) {
848
- const contentBlock = toolCallContentBlocks[contentBlockDelta.contentBlockIndex];
849
- const delta = (_m = contentBlockDelta.delta.toolUse.input) != null ? _m : "";
850
914
  controller.enqueue({
851
- type: "tool-call-delta",
852
- toolCallType: "function",
853
- toolCallId: contentBlock.toolCallId,
854
- toolName: contentBlock.toolName,
855
- argsTextDelta: delta
915
+ type: "tool-input-start",
916
+ id: toolUse.toolUseId,
917
+ toolName: toolUse.name
856
918
  });
857
- contentBlock.jsonText += delta;
858
919
  }
859
- const contentBlockStop = value.contentBlockStop;
860
- if (contentBlockStop != null) {
861
- const index = contentBlockStop.contentBlockIndex;
862
- const contentBlock = toolCallContentBlocks[index];
863
- if (contentBlock != null) {
920
+ const contentBlockDelta = value.contentBlockDelta;
921
+ if ((contentBlockDelta == null ? void 0 : contentBlockDelta.delta) && "toolUse" in contentBlockDelta.delta && contentBlockDelta.delta.toolUse) {
922
+ const blockIndex = contentBlockDelta.contentBlockIndex;
923
+ const contentBlock = contentBlocks[blockIndex];
924
+ if ((contentBlock == null ? void 0 : contentBlock.type) === "tool-call") {
925
+ const delta = (_q = contentBlockDelta.delta.toolUse.input) != null ? _q : "";
864
926
  controller.enqueue({
865
- type: "tool-call",
866
- toolCallType: "function",
867
- toolCallId: contentBlock.toolCallId,
868
- toolName: contentBlock.toolName,
869
- args: contentBlock.jsonText
927
+ type: "tool-input-delta",
928
+ id: contentBlock.toolCallId,
929
+ delta
870
930
  });
871
- delete toolCallContentBlocks[index];
931
+ contentBlock.jsonText += delta;
872
932
  }
873
933
  }
874
934
  },