@ai-sdk/amazon-bedrock 3.0.69 → 3.0.71

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