@eminent337/aery-ai 0.67.68

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 (178) hide show
  1. package/README.md +1317 -0
  2. package/dist/api-registry.d.ts +20 -0
  3. package/dist/api-registry.d.ts.map +1 -0
  4. package/dist/api-registry.js +44 -0
  5. package/dist/api-registry.js.map +1 -0
  6. package/dist/bedrock-provider.d.ts +5 -0
  7. package/dist/bedrock-provider.d.ts.map +1 -0
  8. package/dist/bedrock-provider.js +6 -0
  9. package/dist/bedrock-provider.js.map +1 -0
  10. package/dist/cli.d.ts +3 -0
  11. package/dist/cli.d.ts.map +1 -0
  12. package/dist/cli.js +116 -0
  13. package/dist/cli.js.map +1 -0
  14. package/dist/env-api-keys.d.ts +9 -0
  15. package/dist/env-api-keys.d.ts.map +1 -0
  16. package/dist/env-api-keys.js +107 -0
  17. package/dist/env-api-keys.js.map +1 -0
  18. package/dist/index.d.ts +26 -0
  19. package/dist/index.d.ts.map +1 -0
  20. package/dist/index.js +14 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/models.d.ts +24 -0
  23. package/dist/models.d.ts.map +1 -0
  24. package/dist/models.generated.d.ts +14630 -0
  25. package/dist/models.generated.d.ts.map +1 -0
  26. package/dist/models.generated.js +14383 -0
  27. package/dist/models.generated.js.map +1 -0
  28. package/dist/models.js +58 -0
  29. package/dist/models.js.map +1 -0
  30. package/dist/oauth.d.ts +2 -0
  31. package/dist/oauth.d.ts.map +1 -0
  32. package/dist/oauth.js +2 -0
  33. package/dist/oauth.js.map +1 -0
  34. package/dist/providers/amazon-bedrock.d.ts +38 -0
  35. package/dist/providers/amazon-bedrock.d.ts.map +1 -0
  36. package/dist/providers/amazon-bedrock.js +696 -0
  37. package/dist/providers/amazon-bedrock.js.map +1 -0
  38. package/dist/providers/anthropic.d.ts +54 -0
  39. package/dist/providers/anthropic.d.ts.map +1 -0
  40. package/dist/providers/anthropic.js +774 -0
  41. package/dist/providers/anthropic.js.map +1 -0
  42. package/dist/providers/azure-openai-responses.d.ts +15 -0
  43. package/dist/providers/azure-openai-responses.d.ts.map +1 -0
  44. package/dist/providers/azure-openai-responses.js +183 -0
  45. package/dist/providers/azure-openai-responses.js.map +1 -0
  46. package/dist/providers/faux.d.ts +56 -0
  47. package/dist/providers/faux.d.ts.map +1 -0
  48. package/dist/providers/faux.js +368 -0
  49. package/dist/providers/faux.js.map +1 -0
  50. package/dist/providers/github-copilot-headers.d.ts +8 -0
  51. package/dist/providers/github-copilot-headers.d.ts.map +1 -0
  52. package/dist/providers/github-copilot-headers.js +29 -0
  53. package/dist/providers/github-copilot-headers.js.map +1 -0
  54. package/dist/providers/google-gemini-cli.d.ts +74 -0
  55. package/dist/providers/google-gemini-cli.d.ts.map +1 -0
  56. package/dist/providers/google-gemini-cli.js +779 -0
  57. package/dist/providers/google-gemini-cli.js.map +1 -0
  58. package/dist/providers/google-shared.d.ts +65 -0
  59. package/dist/providers/google-shared.d.ts.map +1 -0
  60. package/dist/providers/google-shared.js +338 -0
  61. package/dist/providers/google-shared.js.map +1 -0
  62. package/dist/providers/google-vertex.d.ts +15 -0
  63. package/dist/providers/google-vertex.d.ts.map +1 -0
  64. package/dist/providers/google-vertex.js +420 -0
  65. package/dist/providers/google-vertex.js.map +1 -0
  66. package/dist/providers/google.d.ts +13 -0
  67. package/dist/providers/google.d.ts.map +1 -0
  68. package/dist/providers/google.js +399 -0
  69. package/dist/providers/google.js.map +1 -0
  70. package/dist/providers/mistral.d.ts +25 -0
  71. package/dist/providers/mistral.d.ts.map +1 -0
  72. package/dist/providers/mistral.js +534 -0
  73. package/dist/providers/mistral.js.map +1 -0
  74. package/dist/providers/openai-codex-responses.d.ts +11 -0
  75. package/dist/providers/openai-codex-responses.d.ts.map +1 -0
  76. package/dist/providers/openai-codex-responses.js +778 -0
  77. package/dist/providers/openai-codex-responses.js.map +1 -0
  78. package/dist/providers/openai-completions.d.ts +19 -0
  79. package/dist/providers/openai-completions.d.ts.map +1 -0
  80. package/dist/providers/openai-completions.js +854 -0
  81. package/dist/providers/openai-completions.js.map +1 -0
  82. package/dist/providers/openai-responses-shared.d.ts +18 -0
  83. package/dist/providers/openai-responses-shared.d.ts.map +1 -0
  84. package/dist/providers/openai-responses-shared.js +479 -0
  85. package/dist/providers/openai-responses-shared.js.map +1 -0
  86. package/dist/providers/openai-responses.d.ts +13 -0
  87. package/dist/providers/openai-responses.d.ts.map +1 -0
  88. package/dist/providers/openai-responses.js +203 -0
  89. package/dist/providers/openai-responses.js.map +1 -0
  90. package/dist/providers/register-builtins.d.ts +38 -0
  91. package/dist/providers/register-builtins.d.ts.map +1 -0
  92. package/dist/providers/register-builtins.js +261 -0
  93. package/dist/providers/register-builtins.js.map +1 -0
  94. package/dist/providers/simple-options.d.ts +8 -0
  95. package/dist/providers/simple-options.d.ts.map +1 -0
  96. package/dist/providers/simple-options.js +36 -0
  97. package/dist/providers/simple-options.js.map +1 -0
  98. package/dist/providers/transform-messages.d.ts +8 -0
  99. package/dist/providers/transform-messages.d.ts.map +1 -0
  100. package/dist/providers/transform-messages.js +182 -0
  101. package/dist/providers/transform-messages.js.map +1 -0
  102. package/dist/stream.d.ts +8 -0
  103. package/dist/stream.d.ts.map +1 -0
  104. package/dist/stream.js +27 -0
  105. package/dist/stream.js.map +1 -0
  106. package/dist/types.d.ts +366 -0
  107. package/dist/types.d.ts.map +1 -0
  108. package/dist/types.js +2 -0
  109. package/dist/types.js.map +1 -0
  110. package/dist/utils/event-stream.d.ts +21 -0
  111. package/dist/utils/event-stream.d.ts.map +1 -0
  112. package/dist/utils/event-stream.js +81 -0
  113. package/dist/utils/event-stream.js.map +1 -0
  114. package/dist/utils/hash.d.ts +3 -0
  115. package/dist/utils/hash.d.ts.map +1 -0
  116. package/dist/utils/hash.js +14 -0
  117. package/dist/utils/hash.js.map +1 -0
  118. package/dist/utils/headers.d.ts +2 -0
  119. package/dist/utils/headers.d.ts.map +1 -0
  120. package/dist/utils/headers.js +8 -0
  121. package/dist/utils/headers.js.map +1 -0
  122. package/dist/utils/json-parse.d.ts +9 -0
  123. package/dist/utils/json-parse.d.ts.map +1 -0
  124. package/dist/utils/json-parse.js +29 -0
  125. package/dist/utils/json-parse.js.map +1 -0
  126. package/dist/utils/oauth/anthropic.d.ts +25 -0
  127. package/dist/utils/oauth/anthropic.d.ts.map +1 -0
  128. package/dist/utils/oauth/anthropic.js +335 -0
  129. package/dist/utils/oauth/anthropic.js.map +1 -0
  130. package/dist/utils/oauth/github-copilot.d.ts +30 -0
  131. package/dist/utils/oauth/github-copilot.d.ts.map +1 -0
  132. package/dist/utils/oauth/github-copilot.js +292 -0
  133. package/dist/utils/oauth/github-copilot.js.map +1 -0
  134. package/dist/utils/oauth/google-antigravity.d.ts +26 -0
  135. package/dist/utils/oauth/google-antigravity.d.ts.map +1 -0
  136. package/dist/utils/oauth/google-antigravity.js +377 -0
  137. package/dist/utils/oauth/google-antigravity.js.map +1 -0
  138. package/dist/utils/oauth/google-gemini-cli.d.ts +26 -0
  139. package/dist/utils/oauth/google-gemini-cli.d.ts.map +1 -0
  140. package/dist/utils/oauth/google-gemini-cli.js +482 -0
  141. package/dist/utils/oauth/google-gemini-cli.js.map +1 -0
  142. package/dist/utils/oauth/index.d.ts +61 -0
  143. package/dist/utils/oauth/index.d.ts.map +1 -0
  144. package/dist/utils/oauth/index.js +131 -0
  145. package/dist/utils/oauth/index.js.map +1 -0
  146. package/dist/utils/oauth/oauth-page.d.ts +3 -0
  147. package/dist/utils/oauth/oauth-page.d.ts.map +1 -0
  148. package/dist/utils/oauth/oauth-page.js +105 -0
  149. package/dist/utils/oauth/oauth-page.js.map +1 -0
  150. package/dist/utils/oauth/openai-codex.d.ts +34 -0
  151. package/dist/utils/oauth/openai-codex.d.ts.map +1 -0
  152. package/dist/utils/oauth/openai-codex.js +374 -0
  153. package/dist/utils/oauth/openai-codex.js.map +1 -0
  154. package/dist/utils/oauth/pkce.d.ts +13 -0
  155. package/dist/utils/oauth/pkce.d.ts.map +1 -0
  156. package/dist/utils/oauth/pkce.js +31 -0
  157. package/dist/utils/oauth/pkce.js.map +1 -0
  158. package/dist/utils/oauth/types.d.ts +47 -0
  159. package/dist/utils/oauth/types.d.ts.map +1 -0
  160. package/dist/utils/oauth/types.js +2 -0
  161. package/dist/utils/oauth/types.js.map +1 -0
  162. package/dist/utils/overflow.d.ts +53 -0
  163. package/dist/utils/overflow.d.ts.map +1 -0
  164. package/dist/utils/overflow.js +132 -0
  165. package/dist/utils/overflow.js.map +1 -0
  166. package/dist/utils/sanitize-unicode.d.ts +22 -0
  167. package/dist/utils/sanitize-unicode.d.ts.map +1 -0
  168. package/dist/utils/sanitize-unicode.js +26 -0
  169. package/dist/utils/sanitize-unicode.js.map +1 -0
  170. package/dist/utils/typebox-helpers.d.ts +17 -0
  171. package/dist/utils/typebox-helpers.d.ts.map +1 -0
  172. package/dist/utils/typebox-helpers.js +21 -0
  173. package/dist/utils/typebox-helpers.js.map +1 -0
  174. package/dist/utils/validation.d.ts +18 -0
  175. package/dist/utils/validation.d.ts.map +1 -0
  176. package/dist/utils/validation.js +80 -0
  177. package/dist/utils/validation.js.map +1 -0
  178. package/package.json +114 -0
@@ -0,0 +1,696 @@
1
+ import { BedrockRuntimeClient, BedrockRuntimeServiceException, StopReason as BedrockStopReason, CachePointType, CacheTTL, ConversationRole, ConverseStreamCommand, ImageFormat, ToolResultStatus, } from "@aws-sdk/client-bedrock-runtime";
2
+ import { calculateCost } from "../models.js";
3
+ import { AssistantMessageEventStream } from "../utils/event-stream.js";
4
+ import { parseStreamingJson } from "../utils/json-parse.js";
5
+ import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
6
+ import { adjustMaxTokensForThinking, buildBaseOptions, clampReasoning } from "./simple-options.js";
7
+ import { transformMessages } from "./transform-messages.js";
8
+ export const streamBedrock = (model, context, options = {}) => {
9
+ const stream = new AssistantMessageEventStream();
10
+ (async () => {
11
+ const output = {
12
+ role: "assistant",
13
+ content: [],
14
+ api: "bedrock-converse-stream",
15
+ provider: model.provider,
16
+ model: model.id,
17
+ usage: {
18
+ input: 0,
19
+ output: 0,
20
+ cacheRead: 0,
21
+ cacheWrite: 0,
22
+ totalTokens: 0,
23
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
24
+ },
25
+ stopReason: "stop",
26
+ timestamp: Date.now(),
27
+ };
28
+ const blocks = output.content;
29
+ const config = {
30
+ profile: options.profile,
31
+ };
32
+ // Pass custom endpoint when the model has a non-default baseUrl.
33
+ // This enables VPC endpoints, proxy setups, and custom routing.
34
+ if (model.baseUrl) {
35
+ config.endpoint = model.baseUrl;
36
+ }
37
+ // Resolve bearer token for Bedrock API key auth.
38
+ const bearerToken = options.bearerToken || process.env.AWS_BEARER_TOKEN_BEDROCK || undefined;
39
+ const useBearerToken = bearerToken !== undefined && process.env.AWS_BEDROCK_SKIP_AUTH !== "1";
40
+ // in Node.js/Bun environment only
41
+ if (typeof process !== "undefined" && (process.versions?.node || process.versions?.bun)) {
42
+ // Region resolution: explicit option > env vars > SDK default chain.
43
+ // When AWS_PROFILE is set, we leave region undefined so the SDK can
44
+ // resovle it from aws profile configs. Otherwise fall back to us-east-1.
45
+ const explicitRegion = options.region || process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION;
46
+ if (explicitRegion) {
47
+ config.region = explicitRegion;
48
+ }
49
+ else if (!process.env.AWS_PROFILE) {
50
+ config.region = "us-east-1";
51
+ }
52
+ // Support proxies that don't need authentication
53
+ if (process.env.AWS_BEDROCK_SKIP_AUTH === "1") {
54
+ config.credentials = {
55
+ accessKeyId: "dummy-access-key",
56
+ secretAccessKey: "dummy-secret-key",
57
+ };
58
+ }
59
+ if (process.env.HTTP_PROXY ||
60
+ process.env.HTTPS_PROXY ||
61
+ process.env.NO_PROXY ||
62
+ process.env.http_proxy ||
63
+ process.env.https_proxy ||
64
+ process.env.no_proxy) {
65
+ const nodeHttpHandler = await import("@smithy/node-http-handler");
66
+ const proxyAgent = await import("proxy-agent");
67
+ const agent = new proxyAgent.ProxyAgent();
68
+ // Bedrock runtime uses NodeHttp2Handler by default since v3.798.0, which is based
69
+ // on `http2` module and has no support for http agent.
70
+ // Use NodeHttpHandler to support http agent.
71
+ config.requestHandler = new nodeHttpHandler.NodeHttpHandler({
72
+ httpAgent: agent,
73
+ httpsAgent: agent,
74
+ });
75
+ }
76
+ else if (process.env.AWS_BEDROCK_FORCE_HTTP1 === "1") {
77
+ // Some custom endpoints require HTTP/1.1 instead of HTTP/2
78
+ const nodeHttpHandler = await import("@smithy/node-http-handler");
79
+ config.requestHandler = new nodeHttpHandler.NodeHttpHandler();
80
+ }
81
+ }
82
+ else {
83
+ // Non-Node environment (browser): fall back to us-east-1 since
84
+ // there's no config file resolution available.
85
+ config.region = options.region || "us-east-1";
86
+ }
87
+ if (useBearerToken) {
88
+ config.token = { token: bearerToken };
89
+ config.authSchemePreference = ["httpBearerAuth"];
90
+ }
91
+ try {
92
+ const client = new BedrockRuntimeClient(config);
93
+ const cacheRetention = resolveCacheRetention(options.cacheRetention);
94
+ let commandInput = {
95
+ modelId: model.id,
96
+ messages: convertMessages(context, model, cacheRetention),
97
+ system: buildSystemPrompt(context.systemPrompt, model, cacheRetention),
98
+ inferenceConfig: {
99
+ ...(options.maxTokens !== undefined && { maxTokens: options.maxTokens }),
100
+ ...(options.temperature !== undefined && { temperature: options.temperature }),
101
+ },
102
+ toolConfig: convertToolConfig(context.tools, options.toolChoice),
103
+ additionalModelRequestFields: buildAdditionalModelRequestFields(model, options),
104
+ ...(options.requestMetadata !== undefined && { requestMetadata: options.requestMetadata }),
105
+ };
106
+ const nextCommandInput = await options?.onPayload?.(commandInput, model);
107
+ if (nextCommandInput !== undefined) {
108
+ commandInput = nextCommandInput;
109
+ }
110
+ const command = new ConverseStreamCommand(commandInput);
111
+ const response = await client.send(command, { abortSignal: options.signal });
112
+ if (response.$metadata.httpStatusCode !== undefined) {
113
+ const responseHeaders = {};
114
+ if (response.$metadata.requestId) {
115
+ responseHeaders["x-amzn-requestid"] = response.$metadata.requestId;
116
+ }
117
+ await options?.onResponse?.({ status: response.$metadata.httpStatusCode, headers: responseHeaders }, model);
118
+ }
119
+ for await (const item of response.stream) {
120
+ if (item.messageStart) {
121
+ if (item.messageStart.role !== ConversationRole.ASSISTANT) {
122
+ throw new Error("Unexpected assistant message start but got user message start instead");
123
+ }
124
+ stream.push({ type: "start", partial: output });
125
+ }
126
+ else if (item.contentBlockStart) {
127
+ handleContentBlockStart(item.contentBlockStart, blocks, output, stream);
128
+ }
129
+ else if (item.contentBlockDelta) {
130
+ handleContentBlockDelta(item.contentBlockDelta, blocks, output, stream);
131
+ }
132
+ else if (item.contentBlockStop) {
133
+ handleContentBlockStop(item.contentBlockStop, blocks, output, stream);
134
+ }
135
+ else if (item.messageStop) {
136
+ output.stopReason = mapStopReason(item.messageStop.stopReason);
137
+ }
138
+ else if (item.metadata) {
139
+ handleMetadata(item.metadata, model, output);
140
+ }
141
+ else if (item.internalServerException) {
142
+ throw item.internalServerException;
143
+ }
144
+ else if (item.modelStreamErrorException) {
145
+ throw item.modelStreamErrorException;
146
+ }
147
+ else if (item.validationException) {
148
+ throw item.validationException;
149
+ }
150
+ else if (item.throttlingException) {
151
+ throw item.throttlingException;
152
+ }
153
+ else if (item.serviceUnavailableException) {
154
+ throw item.serviceUnavailableException;
155
+ }
156
+ }
157
+ if (options.signal?.aborted) {
158
+ throw new Error("Request was aborted");
159
+ }
160
+ if (output.stopReason === "error" || output.stopReason === "aborted") {
161
+ throw new Error("An unknown error occurred");
162
+ }
163
+ stream.push({ type: "done", reason: output.stopReason, message: output });
164
+ stream.end();
165
+ }
166
+ catch (error) {
167
+ for (const block of output.content) {
168
+ delete block.index;
169
+ // partialJson is only a streaming scratch buffer; never persist it.
170
+ delete block.partialJson;
171
+ }
172
+ output.stopReason = options.signal?.aborted ? "aborted" : "error";
173
+ output.errorMessage = formatBedrockError(error);
174
+ stream.push({ type: "error", reason: output.stopReason, error: output });
175
+ stream.end();
176
+ }
177
+ })();
178
+ return stream;
179
+ };
180
+ /**
181
+ * Human-readable prefixes for Bedrock SDK exception names.
182
+ * The downstream retry logic in agent-session matches patterns like
183
+ * `server.?error` and `service.?unavailable`, so we preserve the legacy
184
+ * prefix format rather than using the raw SDK exception name.
185
+ */
186
+ const BEDROCK_ERROR_PREFIXES = {
187
+ InternalServerException: "Internal server error",
188
+ ModelStreamErrorException: "Model stream error",
189
+ ValidationException: "Validation error",
190
+ ThrottlingException: "Throttling error",
191
+ ServiceUnavailableException: "Service unavailable",
192
+ };
193
+ /**
194
+ * Format a Bedrock error with a human-readable prefix.
195
+ * AWS SDK exceptions (both from `client.send()` and from stream event items)
196
+ * extend BedrockRuntimeServiceException. We map the `.name` to a stable
197
+ * human-readable prefix so downstream consumers (retry logic, context-overflow
198
+ * detection) can distinguish error categories via simple string matching.
199
+ */
200
+ function formatBedrockError(error) {
201
+ const message = error instanceof Error ? error.message : JSON.stringify(error);
202
+ if (error instanceof BedrockRuntimeServiceException) {
203
+ const prefix = BEDROCK_ERROR_PREFIXES[error.name] ?? error.name;
204
+ return `${prefix}: ${message}`;
205
+ }
206
+ return message;
207
+ }
208
+ export const streamSimpleBedrock = (model, context, options) => {
209
+ const base = buildBaseOptions(model, options, undefined);
210
+ if (!options?.reasoning) {
211
+ return streamBedrock(model, context, { ...base, reasoning: undefined });
212
+ }
213
+ if (model.id.includes("anthropic.claude") || model.id.includes("anthropic/claude")) {
214
+ if (supportsAdaptiveThinking(model.id)) {
215
+ return streamBedrock(model, context, {
216
+ ...base,
217
+ reasoning: options.reasoning,
218
+ thinkingBudgets: options.thinkingBudgets,
219
+ });
220
+ }
221
+ const adjusted = adjustMaxTokensForThinking(base.maxTokens || 0, model.maxTokens, options.reasoning, options.thinkingBudgets);
222
+ return streamBedrock(model, context, {
223
+ ...base,
224
+ maxTokens: adjusted.maxTokens,
225
+ reasoning: options.reasoning,
226
+ thinkingBudgets: {
227
+ ...(options.thinkingBudgets || {}),
228
+ [clampReasoning(options.reasoning)]: adjusted.thinkingBudget,
229
+ },
230
+ });
231
+ }
232
+ return streamBedrock(model, context, {
233
+ ...base,
234
+ reasoning: options.reasoning,
235
+ thinkingBudgets: options.thinkingBudgets,
236
+ });
237
+ };
238
+ function handleContentBlockStart(event, blocks, output, stream) {
239
+ const index = event.contentBlockIndex;
240
+ const start = event.start;
241
+ if (start?.toolUse) {
242
+ const block = {
243
+ type: "toolCall",
244
+ id: start.toolUse.toolUseId || "",
245
+ name: start.toolUse.name || "",
246
+ arguments: {},
247
+ partialJson: "",
248
+ index,
249
+ };
250
+ output.content.push(block);
251
+ stream.push({ type: "toolcall_start", contentIndex: blocks.length - 1, partial: output });
252
+ }
253
+ }
254
+ function handleContentBlockDelta(event, blocks, output, stream) {
255
+ const contentBlockIndex = event.contentBlockIndex;
256
+ const delta = event.delta;
257
+ let index = blocks.findIndex((b) => b.index === contentBlockIndex);
258
+ let block = blocks[index];
259
+ if (delta?.text !== undefined) {
260
+ // If no text block exists yet, create one, as `handleContentBlockStart` is not sent for text blocks
261
+ if (!block) {
262
+ const newBlock = { type: "text", text: "", index: contentBlockIndex };
263
+ output.content.push(newBlock);
264
+ index = blocks.length - 1;
265
+ block = blocks[index];
266
+ stream.push({ type: "text_start", contentIndex: index, partial: output });
267
+ }
268
+ if (block.type === "text") {
269
+ block.text += delta.text;
270
+ stream.push({ type: "text_delta", contentIndex: index, delta: delta.text, partial: output });
271
+ }
272
+ }
273
+ else if (delta?.toolUse && block?.type === "toolCall") {
274
+ block.partialJson = (block.partialJson || "") + (delta.toolUse.input || "");
275
+ block.arguments = parseStreamingJson(block.partialJson);
276
+ stream.push({ type: "toolcall_delta", contentIndex: index, delta: delta.toolUse.input || "", partial: output });
277
+ }
278
+ else if (delta?.reasoningContent) {
279
+ let thinkingBlock = block;
280
+ let thinkingIndex = index;
281
+ if (!thinkingBlock) {
282
+ const newBlock = { type: "thinking", thinking: "", thinkingSignature: "", index: contentBlockIndex };
283
+ output.content.push(newBlock);
284
+ thinkingIndex = blocks.length - 1;
285
+ thinkingBlock = blocks[thinkingIndex];
286
+ stream.push({ type: "thinking_start", contentIndex: thinkingIndex, partial: output });
287
+ }
288
+ if (thinkingBlock?.type === "thinking") {
289
+ if (delta.reasoningContent.text) {
290
+ thinkingBlock.thinking += delta.reasoningContent.text;
291
+ stream.push({
292
+ type: "thinking_delta",
293
+ contentIndex: thinkingIndex,
294
+ delta: delta.reasoningContent.text,
295
+ partial: output,
296
+ });
297
+ }
298
+ if (delta.reasoningContent.signature) {
299
+ thinkingBlock.thinkingSignature =
300
+ (thinkingBlock.thinkingSignature || "") + delta.reasoningContent.signature;
301
+ }
302
+ }
303
+ }
304
+ }
305
+ function handleMetadata(event, model, output) {
306
+ if (event.usage) {
307
+ output.usage.input = event.usage.inputTokens || 0;
308
+ output.usage.output = event.usage.outputTokens || 0;
309
+ output.usage.cacheRead = event.usage.cacheReadInputTokens || 0;
310
+ output.usage.cacheWrite = event.usage.cacheWriteInputTokens || 0;
311
+ output.usage.totalTokens = event.usage.totalTokens || output.usage.input + output.usage.output;
312
+ calculateCost(model, output.usage);
313
+ }
314
+ }
315
+ function handleContentBlockStop(event, blocks, output, stream) {
316
+ const index = blocks.findIndex((b) => b.index === event.contentBlockIndex);
317
+ const block = blocks[index];
318
+ if (!block)
319
+ return;
320
+ delete block.index;
321
+ switch (block.type) {
322
+ case "text":
323
+ stream.push({ type: "text_end", contentIndex: index, content: block.text, partial: output });
324
+ break;
325
+ case "thinking":
326
+ stream.push({ type: "thinking_end", contentIndex: index, content: block.thinking, partial: output });
327
+ break;
328
+ case "toolCall":
329
+ block.arguments = parseStreamingJson(block.partialJson);
330
+ // Finalize in-place and strip the scratch buffer so replay only
331
+ // carries parsed arguments.
332
+ delete block.partialJson;
333
+ stream.push({ type: "toolcall_end", contentIndex: index, toolCall: block, partial: output });
334
+ break;
335
+ }
336
+ }
337
+ /**
338
+ * Check if the model supports adaptive thinking (Opus 4.6+, Sonnet 4.6).
339
+ */
340
+ function supportsAdaptiveThinking(modelId) {
341
+ return (modelId.includes("opus-4-6") ||
342
+ modelId.includes("opus-4.6") ||
343
+ modelId.includes("opus-4-7") ||
344
+ modelId.includes("opus-4.7") ||
345
+ modelId.includes("sonnet-4-6") ||
346
+ modelId.includes("sonnet-4.6"));
347
+ }
348
+ function mapThinkingLevelToEffort(level, modelId) {
349
+ switch (level) {
350
+ case "minimal":
351
+ case "low":
352
+ return "low";
353
+ case "medium":
354
+ return "medium";
355
+ case "high":
356
+ return "high";
357
+ case "xhigh":
358
+ if (modelId.includes("opus-4-6") || modelId.includes("opus-4.6")) {
359
+ return "max";
360
+ }
361
+ if (modelId.includes("opus-4-7") || modelId.includes("opus-4.7")) {
362
+ return "xhigh";
363
+ }
364
+ return "high";
365
+ default:
366
+ return "high";
367
+ }
368
+ }
369
+ /**
370
+ * Resolve cache retention preference.
371
+ * Defaults to "short" and uses PI_CACHE_RETENTION for backward compatibility.
372
+ */
373
+ function resolveCacheRetention(cacheRetention) {
374
+ if (cacheRetention) {
375
+ return cacheRetention;
376
+ }
377
+ if (typeof process !== "undefined" && process.env.PI_CACHE_RETENTION === "long") {
378
+ return "long";
379
+ }
380
+ return "short";
381
+ }
382
+ /**
383
+ * Check if the model supports prompt caching.
384
+ * Supported: Claude 3.5 Haiku, Claude 3.7 Sonnet, Claude 4.x models
385
+ *
386
+ * For base models and system-defined inference profiles the model ID / ARN
387
+ * contains the model name, so we can decide locally.
388
+ *
389
+ * For application inference profiles (whose ARNs don't contain the model name),
390
+ * set AWS_BEDROCK_FORCE_CACHE=1 to enable cache points. Amazon Nova models
391
+ * have automatic caching and don't need explicit cache points.
392
+ */
393
+ function supportsPromptCaching(model) {
394
+ const id = model.id.toLowerCase();
395
+ if (!id.includes("claude")) {
396
+ // Application inference profiles don't contain the model name in the ARN.
397
+ // Allow users to force cache points via environment variable.
398
+ if (typeof process !== "undefined" && process.env.AWS_BEDROCK_FORCE_CACHE === "1")
399
+ return true;
400
+ return false;
401
+ }
402
+ // Claude 4.x models (opus-4, sonnet-4, haiku-4)
403
+ if (id.includes("-4-") || id.includes("-4."))
404
+ return true;
405
+ // Claude 3.7 Sonnet
406
+ if (id.includes("claude-3-7-sonnet"))
407
+ return true;
408
+ // Claude 3.5 Haiku
409
+ if (id.includes("claude-3-5-haiku"))
410
+ return true;
411
+ return false;
412
+ }
413
+ /**
414
+ * Check if the model supports thinking signatures in reasoningContent.
415
+ * Only Anthropic Claude models support the signature field.
416
+ * Other models (OpenAI, Qwen, Minimax, Moonshot, etc.) reject it with:
417
+ * "This model doesn't support the reasoningContent.reasoningText.signature field"
418
+ */
419
+ function supportsThinkingSignature(model) {
420
+ const id = model.id.toLowerCase();
421
+ return id.includes("anthropic.claude") || id.includes("anthropic/claude");
422
+ }
423
+ function buildSystemPrompt(systemPrompt, model, cacheRetention) {
424
+ if (!systemPrompt)
425
+ return undefined;
426
+ const blocks = [{ text: sanitizeSurrogates(systemPrompt) }];
427
+ // Add cache point for supported Claude models when caching is enabled
428
+ if (cacheRetention !== "none" && supportsPromptCaching(model)) {
429
+ blocks.push({
430
+ cachePoint: { type: CachePointType.DEFAULT, ...(cacheRetention === "long" ? { ttl: CacheTTL.ONE_HOUR } : {}) },
431
+ });
432
+ }
433
+ return blocks;
434
+ }
435
+ function normalizeToolCallId(id) {
436
+ const sanitized = id.replace(/[^a-zA-Z0-9_-]/g, "_");
437
+ return sanitized.length > 64 ? sanitized.slice(0, 64) : sanitized;
438
+ }
439
+ function convertMessages(context, model, cacheRetention) {
440
+ const result = [];
441
+ const transformedMessages = transformMessages(context.messages, model, normalizeToolCallId);
442
+ for (let i = 0; i < transformedMessages.length; i++) {
443
+ const m = transformedMessages[i];
444
+ switch (m.role) {
445
+ case "user":
446
+ result.push({
447
+ role: ConversationRole.USER,
448
+ content: typeof m.content === "string"
449
+ ? [{ text: sanitizeSurrogates(m.content) }]
450
+ : m.content.map((c) => {
451
+ switch (c.type) {
452
+ case "text":
453
+ return { text: sanitizeSurrogates(c.text) };
454
+ case "image":
455
+ return { image: createImageBlock(c.mimeType, c.data) };
456
+ default:
457
+ throw new Error("Unknown user content type");
458
+ }
459
+ }),
460
+ });
461
+ break;
462
+ case "assistant": {
463
+ // Skip assistant messages with empty content (e.g., from aborted requests)
464
+ // Bedrock rejects messages with empty content arrays
465
+ if (m.content.length === 0) {
466
+ continue;
467
+ }
468
+ const contentBlocks = [];
469
+ for (const c of m.content) {
470
+ switch (c.type) {
471
+ case "text":
472
+ // Skip empty text blocks
473
+ if (c.text.trim().length === 0)
474
+ continue;
475
+ contentBlocks.push({ text: sanitizeSurrogates(c.text) });
476
+ break;
477
+ case "toolCall":
478
+ contentBlocks.push({
479
+ toolUse: { toolUseId: c.id, name: c.name, input: c.arguments },
480
+ });
481
+ break;
482
+ case "thinking":
483
+ // Skip empty thinking blocks
484
+ if (c.thinking.trim().length === 0)
485
+ continue;
486
+ // Only Anthropic models support the signature field in reasoningText.
487
+ // For other models, we omit the signature to avoid errors like:
488
+ // "This model doesn't support the reasoningContent.reasoningText.signature field"
489
+ if (supportsThinkingSignature(model)) {
490
+ // Signatures arrive after thinking deltas. If a partial or externally
491
+ // persisted message lacks a signature, Bedrock rejects the replayed
492
+ // reasoning block. Fall back to plain text, matching Anthropic.
493
+ if (!c.thinkingSignature || c.thinkingSignature.trim().length === 0) {
494
+ contentBlocks.push({ text: sanitizeSurrogates(c.thinking) });
495
+ }
496
+ else {
497
+ contentBlocks.push({
498
+ reasoningContent: {
499
+ reasoningText: {
500
+ text: sanitizeSurrogates(c.thinking),
501
+ signature: c.thinkingSignature,
502
+ },
503
+ },
504
+ });
505
+ }
506
+ }
507
+ else {
508
+ contentBlocks.push({
509
+ reasoningContent: {
510
+ reasoningText: { text: sanitizeSurrogates(c.thinking) },
511
+ },
512
+ });
513
+ }
514
+ break;
515
+ default:
516
+ throw new Error("Unknown assistant content type");
517
+ }
518
+ }
519
+ // Skip if all content blocks were filtered out
520
+ if (contentBlocks.length === 0) {
521
+ continue;
522
+ }
523
+ result.push({
524
+ role: ConversationRole.ASSISTANT,
525
+ content: contentBlocks,
526
+ });
527
+ break;
528
+ }
529
+ case "toolResult": {
530
+ // Collect all consecutive toolResult messages into a single user message
531
+ // Bedrock requires all tool results to be in one message
532
+ const toolResults = [];
533
+ // Add current tool result with all content blocks combined
534
+ toolResults.push({
535
+ toolResult: {
536
+ toolUseId: m.toolCallId,
537
+ content: m.content.map((c) => c.type === "image"
538
+ ? { image: createImageBlock(c.mimeType, c.data) }
539
+ : { text: sanitizeSurrogates(c.text) }),
540
+ status: m.isError ? ToolResultStatus.ERROR : ToolResultStatus.SUCCESS,
541
+ },
542
+ });
543
+ // Look ahead for consecutive toolResult messages
544
+ let j = i + 1;
545
+ while (j < transformedMessages.length && transformedMessages[j].role === "toolResult") {
546
+ const nextMsg = transformedMessages[j];
547
+ toolResults.push({
548
+ toolResult: {
549
+ toolUseId: nextMsg.toolCallId,
550
+ content: nextMsg.content.map((c) => c.type === "image"
551
+ ? { image: createImageBlock(c.mimeType, c.data) }
552
+ : { text: sanitizeSurrogates(c.text) }),
553
+ status: nextMsg.isError ? ToolResultStatus.ERROR : ToolResultStatus.SUCCESS,
554
+ },
555
+ });
556
+ j++;
557
+ }
558
+ // Skip the messages we've already processed
559
+ i = j - 1;
560
+ result.push({
561
+ role: ConversationRole.USER,
562
+ content: toolResults,
563
+ });
564
+ break;
565
+ }
566
+ default:
567
+ throw new Error("Unknown message role");
568
+ }
569
+ }
570
+ // Add cache point to the last user message for supported Claude models when caching is enabled
571
+ if (cacheRetention !== "none" && supportsPromptCaching(model) && result.length > 0) {
572
+ const lastMessage = result[result.length - 1];
573
+ if (lastMessage.role === ConversationRole.USER && lastMessage.content) {
574
+ lastMessage.content.push({
575
+ cachePoint: {
576
+ type: CachePointType.DEFAULT,
577
+ ...(cacheRetention === "long" ? { ttl: CacheTTL.ONE_HOUR } : {}),
578
+ },
579
+ });
580
+ }
581
+ }
582
+ return result;
583
+ }
584
+ function convertToolConfig(tools, toolChoice) {
585
+ if (!tools?.length || toolChoice === "none")
586
+ return undefined;
587
+ const bedrockTools = tools.map((tool) => ({
588
+ toolSpec: {
589
+ name: tool.name,
590
+ description: tool.description,
591
+ inputSchema: { json: tool.parameters },
592
+ },
593
+ }));
594
+ let bedrockToolChoice;
595
+ switch (toolChoice) {
596
+ case "auto":
597
+ bedrockToolChoice = { auto: {} };
598
+ break;
599
+ case "any":
600
+ bedrockToolChoice = { any: {} };
601
+ break;
602
+ default:
603
+ if (toolChoice?.type === "tool") {
604
+ bedrockToolChoice = { tool: { name: toolChoice.name } };
605
+ }
606
+ }
607
+ return { tools: bedrockTools, toolChoice: bedrockToolChoice };
608
+ }
609
+ function mapStopReason(reason) {
610
+ switch (reason) {
611
+ case BedrockStopReason.END_TURN:
612
+ case BedrockStopReason.STOP_SEQUENCE:
613
+ return "stop";
614
+ case BedrockStopReason.MAX_TOKENS:
615
+ case BedrockStopReason.MODEL_CONTEXT_WINDOW_EXCEEDED:
616
+ return "length";
617
+ case BedrockStopReason.TOOL_USE:
618
+ return "toolUse";
619
+ default:
620
+ return "error";
621
+ }
622
+ }
623
+ function isGovCloudBedrockTarget(model, options) {
624
+ const region = options.region || process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION;
625
+ if (region?.toLowerCase().startsWith("us-gov-")) {
626
+ return true;
627
+ }
628
+ const modelId = model.id.toLowerCase();
629
+ return modelId.startsWith("us-gov.") || modelId.startsWith("arn:aws-us-gov:");
630
+ }
631
+ function buildAdditionalModelRequestFields(model, options) {
632
+ if (!options.reasoning || !model.reasoning) {
633
+ return undefined;
634
+ }
635
+ if (model.id.includes("anthropic.claude") || model.id.includes("anthropic/claude")) {
636
+ // GovCloud Bedrock currently rejects the Claude thinking.display field.
637
+ // Omit it there until the GovCloud Converse schema catches up.
638
+ const display = isGovCloudBedrockTarget(model, options) ? undefined : (options.thinkingDisplay ?? "summarized");
639
+ const result = supportsAdaptiveThinking(model.id)
640
+ ? {
641
+ thinking: { type: "adaptive", ...(display !== undefined ? { display } : {}) },
642
+ output_config: { effort: mapThinkingLevelToEffort(options.reasoning, model.id) },
643
+ }
644
+ : (() => {
645
+ const defaultBudgets = {
646
+ minimal: 1024,
647
+ low: 2048,
648
+ medium: 8192,
649
+ high: 16384,
650
+ xhigh: 16384, // Claude doesn't support xhigh, clamp to high
651
+ };
652
+ // Custom budgets override defaults (xhigh not in ThinkingBudgets, use high)
653
+ const level = options.reasoning === "xhigh" ? "high" : options.reasoning;
654
+ const budget = options.thinkingBudgets?.[level] ?? defaultBudgets[options.reasoning];
655
+ return {
656
+ thinking: {
657
+ type: "enabled",
658
+ budget_tokens: budget,
659
+ ...(display !== undefined ? { display } : {}),
660
+ },
661
+ };
662
+ })();
663
+ if (!supportsAdaptiveThinking(model.id) && (options.interleavedThinking ?? true)) {
664
+ result.anthropic_beta = ["interleaved-thinking-2025-05-14"];
665
+ }
666
+ return result;
667
+ }
668
+ return undefined;
669
+ }
670
+ function createImageBlock(mimeType, data) {
671
+ let format;
672
+ switch (mimeType) {
673
+ case "image/jpeg":
674
+ case "image/jpg":
675
+ format = ImageFormat.JPEG;
676
+ break;
677
+ case "image/png":
678
+ format = ImageFormat.PNG;
679
+ break;
680
+ case "image/gif":
681
+ format = ImageFormat.GIF;
682
+ break;
683
+ case "image/webp":
684
+ format = ImageFormat.WEBP;
685
+ break;
686
+ default:
687
+ throw new Error(`Unknown image type: ${mimeType}`);
688
+ }
689
+ const binaryString = atob(data);
690
+ const bytes = new Uint8Array(binaryString.length);
691
+ for (let i = 0; i < binaryString.length; i++) {
692
+ bytes[i] = binaryString.charCodeAt(i);
693
+ }
694
+ return { source: { bytes }, format };
695
+ }
696
+ //# sourceMappingURL=amazon-bedrock.js.map