@eminent337/aery-ai 0.67.78 → 0.67.80

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