@ai-sdk/amazon-bedrock 5.0.94 → 5.0.97

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.
@@ -16,7 +16,7 @@ export function isMistralModel(modelId: string): boolean {
16
16
  * Bedrock generates tool call IDs in formats like `tooluse_bpe71yCfRu2b5i-nKGDr5g`,
17
17
  * which are incompatible with Mistral's requirements.
18
18
  *
19
- * This function extracts the first 9 alphanumeric characters from the ID.
19
+ * This function hashes incompatible IDs into 9 alphanumeric characters.
20
20
  *
21
21
  * @param toolCallId - The original tool call ID from Bedrock
22
22
  * @param isMistral - Whether the model is a Mistral model
@@ -30,7 +30,40 @@ export function normalizeToolCallId(
30
30
  return toolCallId;
31
31
  }
32
32
 
33
- // Extract only alphanumeric characters and take first 9
34
- const alphanumericChars = toolCallId.replace(/[^a-zA-Z0-9]/g, '');
35
- return alphanumericChars.slice(0, 9);
33
+ if (/^[a-zA-Z0-9]{9}$/.test(toolCallId)) {
34
+ return toolCallId;
35
+ }
36
+
37
+ return convertToBase62Hash(toolCallId);
38
+ }
39
+
40
+ const base62Characters =
41
+ '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
42
+ const base62Length = BigInt(base62Characters.length);
43
+ const normalizedToolCallIdLength = 9;
44
+ const normalizedToolCallIdSpace =
45
+ base62Length ** BigInt(normalizedToolCallIdLength);
46
+ const fnvOffsetBasis64 = BigInt('14695981039346656037');
47
+ const fnvPrime64 = BigInt('1099511628211');
48
+ const fnv64BitMask = BigInt('18446744073709551615');
49
+
50
+ function convertToBase62Hash(value: string): string {
51
+ // FNV-1a 64-bit is deterministic across runtimes and gives the normalized ID
52
+ // access to the full 9-character base62 space.
53
+ let hash = fnvOffsetBasis64;
54
+
55
+ for (let i = 0; i < value.length; i++) {
56
+ hash ^= BigInt(value.charCodeAt(i));
57
+ hash = (hash * fnvPrime64) & fnv64BitMask;
58
+ }
59
+
60
+ let base62Value = hash % normalizedToolCallIdSpace;
61
+ let result = '';
62
+
63
+ for (let i = 0; i < normalizedToolCallIdLength; i++) {
64
+ result = base62Characters[Number(base62Value % base62Length)] + result;
65
+ base62Value /= base62Length;
66
+ }
67
+
68
+ return result;
36
69
  }