@ai-sdk/amazon-bedrock 3.0.70 → 3.0.72

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/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # @ai-sdk/amazon-bedrock
2
2
 
3
+ ## 3.0.72
4
+
5
+ ### Patch Changes
6
+
7
+ - 4f0e5af: Fix bedrock ConverseStream using /delta/stop_sequence
8
+
9
+ ## 3.0.71
10
+
11
+ ### Patch Changes
12
+
13
+ - 11cb33e: feat(provider/amazon-bedrock): expose stop_sequence in provider metadata
14
+
15
+ The Bedrock provider now exposes the specific stop sequence that triggered generation to halt via `providerMetadata.bedrock.stopSequence`. This is implemented by:
16
+
17
+ - Requesting `/stop_sequence` via `additionalModelResponseFieldPaths` in the API call
18
+ - Parsing the value from `additionalModelResponseFields.stop_sequence` in both generate and stream responses
19
+ - Exposing it as `stopSequence` in the provider metadata (returns `null` when no stop sequence was matched)
20
+
21
+ - 11cb33e: Add stop sequence support for amazon bedrock provider
22
+
3
23
  ## 3.0.70
4
24
 
5
25
  ### Patch Changes
package/dist/index.js CHANGED
@@ -30,7 +30,7 @@ module.exports = __toCommonJS(src_exports);
30
30
  var import_provider_utils8 = require("@ai-sdk/provider-utils");
31
31
 
32
32
  // src/version.ts
33
- var VERSION = true ? "3.0.70" : "0.0.0-test";
33
+ var VERSION = true ? "3.0.72" : "0.0.0-test";
34
34
 
35
35
  // src/bedrock-provider.ts
36
36
  var import_internal2 = require("@ai-sdk/anthropic/internal");
@@ -871,11 +871,15 @@ var BedrockChatLanguageModel = class {
871
871
  additionalModelRequestFields: __,
872
872
  ...filteredBedrockOptions
873
873
  } = (providerOptions == null ? void 0 : providerOptions.bedrock) || {};
874
+ const additionalModelResponseFieldPaths = isAnthropicModel ? ["/delta/stop_sequence"] : void 0;
874
875
  return {
875
876
  command: {
876
877
  system,
877
878
  messages,
878
879
  additionalModelRequestFields: bedrockOptions.additionalModelRequestFields,
880
+ ...additionalModelResponseFieldPaths && {
881
+ additionalModelResponseFieldPaths
882
+ },
879
883
  ...Object.keys(inferenceConfig).length > 0 && {
880
884
  inferenceConfig
881
885
  },
@@ -893,7 +897,7 @@ var BedrockChatLanguageModel = class {
893
897
  return (0, import_provider_utils4.combineHeaders)(await (0, import_provider_utils4.resolve)(this.config.headers), headers);
894
898
  }
895
899
  async doGenerate(options) {
896
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
900
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
897
901
  const {
898
902
  command: args,
899
903
  warnings,
@@ -967,15 +971,17 @@ var BedrockChatLanguageModel = class {
967
971
  }
968
972
  }
969
973
  }
970
- const providerMetadata = response.trace || response.usage || isJsonResponseFromTool ? {
974
+ const stopSequence = (_j = (_i = (_h = response.additionalModelResponseFields) == null ? void 0 : _h.delta) == null ? void 0 : _i.stop_sequence) != null ? _j : null;
975
+ const providerMetadata = response.trace || response.usage || isJsonResponseFromTool || stopSequence ? {
971
976
  bedrock: {
972
977
  ...response.trace && typeof response.trace === "object" ? { trace: response.trace } : {},
973
- ...((_h = response.usage) == null ? void 0 : _h.cacheWriteInputTokens) != null && {
978
+ ...((_k = response.usage) == null ? void 0 : _k.cacheWriteInputTokens) != null && {
974
979
  usage: {
975
980
  cacheWriteInputTokens: response.usage.cacheWriteInputTokens
976
981
  }
977
982
  },
978
- ...isJsonResponseFromTool && { isJsonResponseFromTool: true }
983
+ ...isJsonResponseFromTool && { isJsonResponseFromTool: true },
984
+ stopSequence
979
985
  }
980
986
  } : void 0;
981
987
  return {
@@ -985,10 +991,10 @@ var BedrockChatLanguageModel = class {
985
991
  isJsonResponseFromTool
986
992
  ),
987
993
  usage: {
988
- inputTokens: (_i = response.usage) == null ? void 0 : _i.inputTokens,
989
- outputTokens: (_j = response.usage) == null ? void 0 : _j.outputTokens,
990
- totalTokens: ((_k = response.usage) == null ? void 0 : _k.inputTokens) + ((_l = response.usage) == null ? void 0 : _l.outputTokens),
991
- cachedInputTokens: (_n = (_m = response.usage) == null ? void 0 : _m.cacheReadInputTokens) != null ? _n : void 0
994
+ inputTokens: (_l = response.usage) == null ? void 0 : _l.inputTokens,
995
+ outputTokens: (_m = response.usage) == null ? void 0 : _m.outputTokens,
996
+ totalTokens: ((_n = response.usage) == null ? void 0 : _n.inputTokens) + ((_o = response.usage) == null ? void 0 : _o.outputTokens),
997
+ cachedInputTokens: (_q = (_p = response.usage) == null ? void 0 : _p.cacheReadInputTokens) != null ? _q : void 0
992
998
  },
993
999
  response: {
994
1000
  // TODO add id, timestamp, etc
@@ -1025,6 +1031,7 @@ var BedrockChatLanguageModel = class {
1025
1031
  };
1026
1032
  let providerMetadata = void 0;
1027
1033
  let isJsonResponseFromTool = false;
1034
+ let stopSequence = null;
1028
1035
  const contentBlocks = {};
1029
1036
  return {
1030
1037
  stream: response.pipeThrough(
@@ -1033,7 +1040,7 @@ var BedrockChatLanguageModel = class {
1033
1040
  controller.enqueue({ type: "stream-start", warnings });
1034
1041
  },
1035
1042
  transform(chunk, controller) {
1036
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
1043
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t;
1037
1044
  function enqueueError(bedrockError) {
1038
1045
  finishReason = "error";
1039
1046
  controller.enqueue({ type: "error", error: bedrockError });
@@ -1067,13 +1074,14 @@ var BedrockChatLanguageModel = class {
1067
1074
  value.messageStop.stopReason,
1068
1075
  isJsonResponseFromTool
1069
1076
  );
1077
+ stopSequence = (_c = (_b = (_a = value.messageStop.additionalModelResponseFields) == null ? void 0 : _a.delta) == null ? void 0 : _b.stop_sequence) != null ? _c : null;
1070
1078
  }
1071
1079
  if (value.metadata) {
1072
- usage.inputTokens = (_b = (_a = value.metadata.usage) == null ? void 0 : _a.inputTokens) != null ? _b : usage.inputTokens;
1073
- usage.outputTokens = (_d = (_c = value.metadata.usage) == null ? void 0 : _c.outputTokens) != null ? _d : usage.outputTokens;
1074
- usage.totalTokens = ((_e = usage.inputTokens) != null ? _e : 0) + ((_f = usage.outputTokens) != null ? _f : 0);
1075
- usage.cachedInputTokens = (_h = (_g = value.metadata.usage) == null ? void 0 : _g.cacheReadInputTokens) != null ? _h : usage.cachedInputTokens;
1076
- const cacheUsage = ((_i = value.metadata.usage) == null ? void 0 : _i.cacheWriteInputTokens) != null ? {
1080
+ usage.inputTokens = (_e = (_d = value.metadata.usage) == null ? void 0 : _d.inputTokens) != null ? _e : usage.inputTokens;
1081
+ usage.outputTokens = (_g = (_f = value.metadata.usage) == null ? void 0 : _f.outputTokens) != null ? _g : usage.outputTokens;
1082
+ usage.totalTokens = ((_h = usage.inputTokens) != null ? _h : 0) + ((_i = usage.outputTokens) != null ? _i : 0);
1083
+ usage.cachedInputTokens = (_k = (_j = value.metadata.usage) == null ? void 0 : _j.cacheReadInputTokens) != null ? _k : usage.cachedInputTokens;
1084
+ const cacheUsage = ((_l = value.metadata.usage) == null ? void 0 : _l.cacheWriteInputTokens) != null ? {
1077
1085
  usage: {
1078
1086
  cacheWriteInputTokens: value.metadata.usage.cacheWriteInputTokens
1079
1087
  }
@@ -1090,7 +1098,7 @@ var BedrockChatLanguageModel = class {
1090
1098
  };
1091
1099
  }
1092
1100
  }
1093
- if (((_j = value.contentBlockStart) == null ? void 0 : _j.contentBlockIndex) != null && !((_l = (_k = value.contentBlockStart) == null ? void 0 : _k.start) == null ? void 0 : _l.toolUse)) {
1101
+ if (((_m = value.contentBlockStart) == null ? void 0 : _m.contentBlockIndex) != null && !((_o = (_n = value.contentBlockStart) == null ? void 0 : _n.start) == null ? void 0 : _o.toolUse)) {
1094
1102
  const blockIndex = value.contentBlockStart.contentBlockIndex;
1095
1103
  contentBlocks[blockIndex] = { type: "text" };
1096
1104
  controller.enqueue({
@@ -1098,7 +1106,7 @@ var BedrockChatLanguageModel = class {
1098
1106
  id: String(blockIndex)
1099
1107
  });
1100
1108
  }
1101
- if (((_m = value.contentBlockDelta) == null ? void 0 : _m.delta) && "text" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.text) {
1109
+ if (((_p = value.contentBlockDelta) == null ? void 0 : _p.delta) && "text" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.text) {
1102
1110
  const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
1103
1111
  if (contentBlocks[blockIndex] == null) {
1104
1112
  contentBlocks[blockIndex] = { type: "text" };
@@ -1113,7 +1121,7 @@ var BedrockChatLanguageModel = class {
1113
1121
  delta: value.contentBlockDelta.delta.text
1114
1122
  });
1115
1123
  }
1116
- if (((_n = value.contentBlockStop) == null ? void 0 : _n.contentBlockIndex) != null) {
1124
+ if (((_q = value.contentBlockStop) == null ? void 0 : _q.contentBlockIndex) != null) {
1117
1125
  const blockIndex = value.contentBlockStop.contentBlockIndex;
1118
1126
  const contentBlock = contentBlocks[blockIndex];
1119
1127
  if (contentBlock != null) {
@@ -1159,7 +1167,7 @@ var BedrockChatLanguageModel = class {
1159
1167
  delete contentBlocks[blockIndex];
1160
1168
  }
1161
1169
  }
1162
- if (((_o = value.contentBlockDelta) == null ? void 0 : _o.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
1170
+ if (((_r = value.contentBlockDelta) == null ? void 0 : _r.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
1163
1171
  const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
1164
1172
  const reasoningContent = value.contentBlockDelta.delta.reasoningContent;
1165
1173
  if ("text" in reasoningContent && reasoningContent.text) {
@@ -1200,7 +1208,7 @@ var BedrockChatLanguageModel = class {
1200
1208
  }
1201
1209
  }
1202
1210
  const contentBlockStart = value.contentBlockStart;
1203
- if (((_p = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _p.toolUse) != null) {
1211
+ if (((_s = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _s.toolUse) != null) {
1204
1212
  const toolUse = contentBlockStart.start.toolUse;
1205
1213
  const blockIndex = contentBlockStart.contentBlockIndex;
1206
1214
  const isJsonResponseTool = usesJsonResponseTool && toolUse.name === "json";
@@ -1224,7 +1232,7 @@ var BedrockChatLanguageModel = class {
1224
1232
  const blockIndex = contentBlockDelta.contentBlockIndex;
1225
1233
  const contentBlock = contentBlocks[blockIndex];
1226
1234
  if ((contentBlock == null ? void 0 : contentBlock.type) === "tool-call") {
1227
- const delta = (_q = contentBlockDelta.delta.toolUse.input) != null ? _q : "";
1235
+ const delta = (_t = contentBlockDelta.delta.toolUse.input) != null ? _t : "";
1228
1236
  if (!contentBlock.isJsonResponseTool) {
1229
1237
  controller.enqueue({
1230
1238
  type: "tool-input-delta",
@@ -1237,16 +1245,22 @@ var BedrockChatLanguageModel = class {
1237
1245
  }
1238
1246
  },
1239
1247
  flush(controller) {
1240
- if (isJsonResponseFromTool) {
1248
+ if (isJsonResponseFromTool || stopSequence != null) {
1241
1249
  if (providerMetadata) {
1242
1250
  providerMetadata.bedrock = {
1243
1251
  ...providerMetadata.bedrock,
1244
- isJsonResponseFromTool: true
1252
+ ...isJsonResponseFromTool && {
1253
+ isJsonResponseFromTool: true
1254
+ },
1255
+ stopSequence
1245
1256
  };
1246
1257
  } else {
1247
1258
  providerMetadata = {
1248
1259
  bedrock: {
1249
- isJsonResponseFromTool: true
1260
+ ...isJsonResponseFromTool && {
1261
+ isJsonResponseFromTool: true
1262
+ },
1263
+ stopSequence
1250
1264
  }
1251
1265
  };
1252
1266
  }
@@ -1273,6 +1287,11 @@ var BedrockStopReasonSchema = import_v43.z.union([
1273
1287
  import_v43.z.enum(BEDROCK_STOP_REASONS),
1274
1288
  import_v43.z.string()
1275
1289
  ]);
1290
+ var BedrockAdditionalModelResponseFieldsSchema = import_v43.z.object({
1291
+ delta: import_v43.z.object({
1292
+ stop_sequence: import_v43.z.string().nullish()
1293
+ }).nullish()
1294
+ }).catchall(import_v43.z.unknown());
1276
1295
  var BedrockToolUseSchema = import_v43.z.object({
1277
1296
  toolUseId: import_v43.z.string(),
1278
1297
  name: import_v43.z.string(),
@@ -1309,6 +1328,7 @@ var BedrockResponseSchema = import_v43.z.object({
1309
1328
  })
1310
1329
  }),
1311
1330
  stopReason: BedrockStopReasonSchema,
1331
+ additionalModelResponseFields: BedrockAdditionalModelResponseFieldsSchema.nullish(),
1312
1332
  trace: import_v43.z.unknown().nullish(),
1313
1333
  usage: import_v43.z.object({
1314
1334
  inputTokens: import_v43.z.number(),
@@ -1348,7 +1368,7 @@ var BedrockStreamSchema = import_v43.z.object({
1348
1368
  }).nullish(),
1349
1369
  internalServerException: import_v43.z.record(import_v43.z.string(), import_v43.z.unknown()).nullish(),
1350
1370
  messageStop: import_v43.z.object({
1351
- additionalModelResponseFields: import_v43.z.record(import_v43.z.string(), import_v43.z.unknown()).nullish(),
1371
+ additionalModelResponseFields: BedrockAdditionalModelResponseFieldsSchema.nullish(),
1352
1372
  stopReason: BedrockStopReasonSchema
1353
1373
  }).nullish(),
1354
1374
  metadata: import_v43.z.object({