@tanstack/ai-bedrock 1.0.0 → 1.2.0

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 (36) hide show
  1. package/README.md +2 -0
  2. package/dist/esm/adapters/converse-text.d.ts +2 -2
  3. package/dist/esm/adapters/converse-text.js +21 -20
  4. package/dist/esm/adapters/converse-text.js.map +1 -1
  5. package/dist/esm/adapters/embedding.d.ts +1 -1
  6. package/dist/esm/adapters/embedding.js +7 -10
  7. package/dist/esm/adapters/embedding.js.map +1 -1
  8. package/dist/esm/adapters/responses-text.d.ts +1 -1
  9. package/dist/esm/adapters/responses-text.js +3 -4
  10. package/dist/esm/adapters/responses-text.js.map +1 -1
  11. package/dist/esm/adapters/text.d.ts +1 -1
  12. package/dist/esm/adapters/text.js +3 -4
  13. package/dist/esm/adapters/text.js.map +1 -1
  14. package/dist/esm/converse/usage.js +3 -1
  15. package/dist/esm/converse/usage.js.map +1 -1
  16. package/dist/esm/utils/auth.d.ts +6 -0
  17. package/dist/esm/utils/auth.js +17 -5
  18. package/dist/esm/utils/auth.js.map +1 -1
  19. package/dist/esm/utils/client.js +1 -1
  20. package/dist/esm/utils/client.js.map +1 -1
  21. package/dist/esm/utils/openai-sigv4-fetch.d.ts +2 -2
  22. package/dist/esm/utils/openai-sigv4-fetch.js +12 -23
  23. package/dist/esm/utils/openai-sigv4-fetch.js.map +1 -1
  24. package/dist/esm/utils/sigv4.d.ts +28 -0
  25. package/dist/esm/utils/sigv4.js +133 -0
  26. package/dist/esm/utils/sigv4.js.map +1 -0
  27. package/package.json +22 -7
  28. package/src/adapters/converse-text.ts +25 -13
  29. package/src/adapters/embedding.ts +7 -5
  30. package/src/adapters/responses-text.ts +6 -4
  31. package/src/adapters/text.ts +6 -4
  32. package/src/converse/usage.ts +5 -0
  33. package/src/utils/auth.ts +19 -3
  34. package/src/utils/client.ts +1 -1
  35. package/src/utils/openai-sigv4-fetch.ts +14 -34
  36. package/src/utils/sigv4.ts +202 -0
@@ -5,7 +5,7 @@ import {
5
5
  } from '@tanstack/ai'
6
6
  import { BaseTextAdapter } from '@tanstack/ai/adapters'
7
7
  import { toRunErrorPayload } from '@tanstack/ai/adapter-internals'
8
- import { resolveBedrockAuth } from '../utils/auth'
8
+ import { importOptionalPeer, resolveBedrockAuth } from '../utils/auth'
9
9
  import {
10
10
  toConverseMessages,
11
11
  toolBlocksToText,
@@ -148,7 +148,7 @@ export class BedrockConverseTextAdapter<
148
148
  */
149
149
  protected importBedrockRuntime(): Promise<typeof BedrockRuntime> {
150
150
  const mod = '@aws-sdk/client-bedrock-runtime'
151
- return import(/* @vite-ignore */ mod) as Promise<typeof BedrockRuntime>
151
+ return importOptionalPeer<typeof BedrockRuntime>(mod)
152
152
  }
153
153
 
154
154
  /**
@@ -241,12 +241,13 @@ export class BedrockConverseTextAdapter<
241
241
 
242
242
  protected async sendStream(
243
243
  input: ConverseStreamCommandInput,
244
+ signal?: AbortSignal | null,
244
245
  ): Promise<AsyncIterable<ConverseStreamOutput>> {
245
246
  const { ConverseStreamCommand } = await this.importBedrockRuntime()
246
247
  const client = await this.getClient()
247
248
  const command = new ConverseStreamCommand(input)
248
249
  addRestoreDocuments(command, input)
249
- const res = await client.send(command)
250
+ const res = await client.send(command, { abortSignal: signal ?? undefined })
250
251
  if (!res.stream) {
251
252
  throw new Error('Bedrock Converse: empty stream response')
252
253
  }
@@ -255,12 +256,13 @@ export class BedrockConverseTextAdapter<
255
256
 
256
257
  protected async send(
257
258
  input: ConverseCommandInput,
259
+ signal?: AbortSignal | null,
258
260
  ): Promise<ConverseCommandOutput> {
259
261
  const { ConverseCommand } = await this.importBedrockRuntime()
260
262
  const client = await this.getClient()
261
263
  const command = new ConverseCommand(input)
262
264
  addRestoreDocuments(command, input)
263
- return client.send(command)
265
+ return client.send(command, { abortSignal: signal ?? undefined })
264
266
  }
265
267
 
266
268
  // ---------------------------------------------------------------------------
@@ -281,7 +283,7 @@ export class BedrockConverseTextAdapter<
281
283
  if (!input.toolConfig && input.messages) {
282
284
  input.messages = toolBlocksToText(input.messages)
283
285
  }
284
- const stream = await this.sendStream(input)
286
+ const stream = await this.sendStream(input, options.request?.signal)
285
287
  for await (const chunk of processConverseStream(
286
288
  stream,
287
289
  () => this.generateId(),
@@ -344,25 +346,31 @@ export class BedrockConverseTextAdapter<
344
346
  ...this.buildInput(requestOptions),
345
347
  toolConfig: buildStructuredToolConfig(outputSchema),
346
348
  }
347
- const res = await this.send(input)
349
+ const res = await this.send(input, chatOptions.request?.signal)
350
+ const usage = res.usage && buildConverseUsage(res.usage)
348
351
  // A forced tool call cut off at the output cap carries partial input,
349
352
  // which would read like a schema failure (issue #1426).
350
353
  if (res.stopReason === 'max_tokens') {
351
- throw new Error(
352
- `${this.name}.structuredOutput: the response was cut off because the maximum token limit was reached (stopReason=max_tokens); raise modelOptions.max_completion_tokens`,
354
+ throw Object.assign(
355
+ new Error(
356
+ `${this.name}.structuredOutput: the response was cut off because the maximum token limit was reached (stopReason=max_tokens); raise modelOptions.max_completion_tokens`,
357
+ ),
358
+ usage && { usage },
353
359
  )
354
360
  }
355
361
  const structured = extractStructuredToolInput(res)
356
362
  if (structured === undefined) {
357
- throw new Error(
358
- `${this.name}.structuredOutput: response contained no forced-tool output`,
363
+ throw Object.assign(
364
+ new Error(
365
+ `${this.name}.structuredOutput: response contained no forced-tool output`,
366
+ ),
367
+ usage && { usage },
359
368
  )
360
369
  }
361
- const usage = res.usage
362
370
  return {
363
371
  data: structured,
364
372
  rawText: JSON.stringify(structured),
365
- ...(usage && { usage: buildConverseUsage(usage) }),
373
+ ...(usage && { usage }),
366
374
  }
367
375
  } catch (error: unknown) {
368
376
  chatOptions.logger.errors(`${this.name}.structuredOutput fatal`, {
@@ -408,7 +416,7 @@ export class BedrockConverseTextAdapter<
408
416
  ...this.buildInput(requestOptions),
409
417
  toolConfig: buildStructuredToolConfig(outputSchema),
410
418
  }
411
- const stream = await this.sendStream(input)
419
+ const stream = await this.sendStream(input, chatOptions.request?.signal)
412
420
 
413
421
  // The forced tool streams its `input` as partial-JSON fragments inside
414
422
  // `contentBlockDelta.delta.toolUse.input`. We surface them as
@@ -518,6 +526,7 @@ export class BedrockConverseTextAdapter<
518
526
  message,
519
527
  code: 'max_tokens',
520
528
  error: { message, code: 'max_tokens' },
529
+ ...(usage && { usage }),
521
530
  }
522
531
  return
523
532
  }
@@ -535,6 +544,7 @@ export class BedrockConverseTextAdapter<
535
544
  message: `${this.name}.structuredOutputStream: response contained no content`,
536
545
  code: 'empty-response',
537
546
  },
547
+ ...(usage && { usage }),
538
548
  }
539
549
  return
540
550
  }
@@ -555,6 +565,7 @@ export class BedrockConverseTextAdapter<
555
565
  message: 'Failed to parse structured output as JSON',
556
566
  code: 'parse-error',
557
567
  },
568
+ ...(usage && { usage }),
558
569
  }
559
570
  return
560
571
  }
@@ -613,6 +624,7 @@ export class BedrockConverseTextAdapter<
613
624
  message: errorPayload.message,
614
625
  ...(errorPayload.code !== undefined && { code: errorPayload.code }),
615
626
  },
627
+ ...(usage && { usage }),
616
628
  }
617
629
  }
618
630
  }
@@ -6,7 +6,7 @@ import {
6
6
  resolveEmbeddingInput,
7
7
  unsupportedFileSourceError,
8
8
  } from '@tanstack/ai'
9
- import { resolveBedrockAuth } from '../utils/auth'
9
+ import { importOptionalPeer, resolveBedrockAuth } from '../utils/auth'
10
10
  import { BEDROCK_EMBEDDING_MODELS } from '../model-meta'
11
11
  import type * as BedrockRuntime from '@aws-sdk/client-bedrock-runtime'
12
12
  import type {
@@ -104,7 +104,7 @@ export class BedrockEmbeddingAdapter<
104
104
  */
105
105
  protected importBedrockRuntime(): Promise<typeof BedrockRuntime> {
106
106
  const mod = '@aws-sdk/client-bedrock-runtime'
107
- return import(/* @vite-ignore */ mod) as Promise<typeof BedrockRuntime>
107
+ return importOptionalPeer<typeof BedrockRuntime>(mod)
108
108
  }
109
109
 
110
110
  /**
@@ -170,6 +170,7 @@ export class BedrockEmbeddingAdapter<
170
170
  protected async invokeModel(
171
171
  modelId: string,
172
172
  body: Record<string, unknown>,
173
+ abortSignal?: AbortSignal,
173
174
  ): Promise<unknown> {
174
175
  const { InvokeModelCommand } = await this.importBedrockRuntime()
175
176
  const client = await this.getClient()
@@ -180,6 +181,7 @@ export class BedrockEmbeddingAdapter<
180
181
  accept: 'application/json',
181
182
  body: JSON.stringify(body),
182
183
  }),
184
+ abortSignal ? { abortSignal } : {},
183
185
  )
184
186
  return JSON.parse(new TextDecoder().decode(response.body))
185
187
  }
@@ -254,7 +256,7 @@ export class BedrockEmbeddingAdapter<
254
256
  if (dimensions !== undefined) body.dimensions = dimensions
255
257
  if (normalize !== undefined) body.normalize = normalize
256
258
  return readTitanEmbeddingBody(
257
- await this.invokeModel(model, body),
259
+ await this.invokeModel(model, body, options.abortSignal),
258
260
  `${this.name} ${model}`,
259
261
  )
260
262
  },
@@ -299,7 +301,7 @@ export class BedrockEmbeddingAdapter<
299
301
  const image = item.images[0]
300
302
  if (image) body.inputImage = toTitanInputImage(image, model)
301
303
  return readTitanEmbeddingBody(
302
- await this.invokeModel(model, body),
304
+ await this.invokeModel(model, body, options.abortSignal),
303
305
  `${this.name} ${model}`,
304
306
  )
305
307
  },
@@ -343,7 +345,7 @@ export class BedrockEmbeddingAdapter<
343
345
  }
344
346
  if (truncate !== undefined) body.truncate = truncate
345
347
  return readCohereEmbeddingBody(
346
- await this.invokeModel(model, body),
348
+ await this.invokeModel(model, body, options.abortSignal),
347
349
  `${this.name} ${model}`,
348
350
  )
349
351
  },
@@ -1,5 +1,7 @@
1
- import OpenAI from 'openai'
2
- import { OpenAIBaseResponsesTextAdapter } from '@tanstack/openai-base'
1
+ import {
2
+ OpenAIBaseResponsesTextAdapter,
3
+ createOpenAIClient,
4
+ } from '@tanstack/openai-base'
3
5
  import {
4
6
  isBedrockOpenAIContextOverflow,
5
7
  withBedrockDefaults,
@@ -38,7 +40,7 @@ type ResolveToolCapabilities<TModel extends string> =
38
40
 
39
41
  /**
40
42
  * Bedrock Responses adapter. Drives mantle's OpenAI-compatible `/responses`
41
- * endpoint via the OpenAI SDK (`client.responses.create`) — the same base
43
+ * endpoint via the OpenAI-compatible client (`client.responses.create`) — the same base
42
44
  * class ai-openai's `openaiText` uses. Responses is mantle-only, so the
43
45
  * constructor forces the mantle baseURL.
44
46
  */
@@ -73,7 +75,7 @@ export class BedrockResponsesTextAdapter<
73
75
  // Responses is mantle-only — force the mantle base URL (an explicit
74
76
  // config.baseURL still wins, e.g. E2E pointing at aimock).
75
77
  const options = withBedrockDefaults(config, 'mantle', model)
76
- super(model, 'bedrock-responses', new OpenAI(options), {
78
+ super(model, 'bedrock-responses', createOpenAIClient(options), {
77
79
  ...config,
78
80
  fetch: options.fetch,
79
81
  })
@@ -1,5 +1,7 @@
1
- import OpenAI from 'openai'
2
- import { OpenAIBaseChatCompletionsTextAdapter } from '@tanstack/openai-base'
1
+ import {
2
+ OpenAIBaseChatCompletionsTextAdapter,
3
+ createOpenAIClient,
4
+ } from '@tanstack/openai-base'
3
5
  import {
4
6
  isBedrockOpenAIContextOverflow,
5
7
  withBedrockDefaults,
@@ -38,7 +40,7 @@ type ResolveToolCapabilities<TModel extends string> =
38
40
 
39
41
  /**
40
42
  * Bedrock Chat Completions adapter. Drives Bedrock's OpenAI-compatible
41
- * `/chat/completions` endpoint via the OpenAI SDK with a baseURL override
43
+ * `/chat/completions` endpoint via the OpenAI-compatible client with a baseURL override
42
44
  * (same pattern as ai-groq). Tool conversion, streaming, structured output,
43
45
  * and the agent loop come from the base.
44
46
  */
@@ -72,7 +74,7 @@ export class BedrockTextAdapter<
72
74
  constructor(config: BedrockTextConfig, model: TModel) {
73
75
  // No `forced` -> honors config.endpoint ('runtime' default, 'mantle' allowed).
74
76
  const options = withBedrockDefaults(config, undefined, model)
75
- super(model, 'bedrock', new OpenAI(options), {
77
+ super(model, 'bedrock', createOpenAIClient(options), {
76
78
  ...config,
77
79
  fetch: options.fetch,
78
80
  })
@@ -15,6 +15,10 @@ import type { TokenUsage as ConverseTokenUsage } from '@aws-sdk/client-bedrock-r
15
15
  export function buildConverseUsage(usage: ConverseTokenUsage): TokenUsage {
16
16
  const cachedTokens = usage.cacheReadInputTokens
17
17
  const cacheWriteTokens = usage.cacheWriteInputTokens
18
+ // The 1-hour part of the cache write.
19
+ const cacheWrite1hTokens = usage.cacheDetails?.find(
20
+ (detail) => detail.ttl === '1h',
21
+ )?.inputTokens
18
22
  const promptTokens =
19
23
  (usage.inputTokens ?? 0) + (cachedTokens ?? 0) + (cacheWriteTokens ?? 0)
20
24
  const completionTokens = usage.outputTokens ?? 0
@@ -31,6 +35,7 @@ export function buildConverseUsage(usage: ConverseTokenUsage): TokenUsage {
31
35
  const promptTokensDetails = {
32
36
  ...(cachedTokens !== undefined ? { cachedTokens } : {}),
33
37
  ...(cacheWriteTokens !== undefined ? { cacheWriteTokens } : {}),
38
+ ...(cacheWrite1hTokens !== undefined ? { cacheWrite1hTokens } : {}),
34
39
  }
35
40
  if (Object.keys(promptTokensDetails).length > 0) {
36
41
  result.promptTokensDetails = promptTokensDetails
package/src/utils/auth.ts CHANGED
@@ -17,6 +17,23 @@ export type ResolvedBedrockAuth =
17
17
  credentials: AwsCredentialIdentityProvider
18
18
  }
19
19
 
20
+ /**
21
+ * Loads an optional peer dependency of this package (the AWS SDK parts). If
22
+ * the import fails, throws an error that names the package to install. The
23
+ * original error is kept as `cause`.
24
+ */
25
+ export async function importOptionalPeer<T>(mod: string): Promise<T> {
26
+ try {
27
+ return (await import(/* @vite-ignore */ mod)) as T
28
+ } catch (cause) {
29
+ throw new Error(
30
+ `@tanstack/ai-bedrock could not load "${mod}". It is an optional peer ` +
31
+ `dependency. Install it: npm install ${mod}`,
32
+ { cause },
33
+ )
34
+ }
35
+ }
36
+
20
37
  const DEFAULT_REGION = 'us-east-1'
21
38
 
22
39
  function readApiKeyFromEnv(): string | undefined {
@@ -71,9 +88,8 @@ export function resolveBedrockAuth(
71
88
  // type-only reference (erased at emit) so we keep full typing.
72
89
  credentials: async (...args) => {
73
90
  const mod = '@aws-sdk/credential-providers'
74
- const { fromNodeProviderChain } = (await import(
75
- /* @vite-ignore */ mod
76
- )) as typeof CredentialProviders
91
+ const { fromNodeProviderChain } =
92
+ await importOptionalPeer<typeof CredentialProviders>(mod)
77
93
  return fromNodeProviderChain()(...args)
78
94
  },
79
95
  }
@@ -25,7 +25,7 @@ export interface BedrockClientConfig extends Omit<
25
25
  }
26
26
 
27
27
  const DEFAULT_REGION = 'us-east-1'
28
- /** OpenAI SDK requires a non-empty apiKey even when a signed fetch overrides Authorization. */
28
+ /** The OpenAI client requires a non-empty apiKey even when a signed fetch overrides Authorization. */
29
29
  const SIGV4_PLACEHOLDER_KEY = 'bedrock-sigv4'
30
30
 
31
31
  function buildBaseURL(
@@ -1,25 +1,16 @@
1
- import { SignatureV4 } from '@smithy/signature-v4'
2
- import { Sha256 } from '@aws-crypto/sha256-js'
3
- import type { HttpRequest } from '@smithy/types'
1
+ import { signSigV4 } from './sigv4'
4
2
  import type { ResolvedBedrockAuth } from './auth'
5
3
 
6
4
  type FetchLike = typeof fetch
7
5
 
8
6
  /**
9
- * Wraps a fetch so each request is SigV4-signed via the AWS signer that ships
10
- * with `@aws-sdk/client-bedrock-runtime`. Replaces the old aws-sigv4-fetch peer.
7
+ * Wraps a fetch so each request is SigV4-signed with the resolved AWS
8
+ * credentials (WebCrypto signer in `./sigv4`).
11
9
  */
12
10
  export function createSigV4Fetch(
13
11
  auth: Extract<ResolvedBedrockAuth, { kind: 'sigv4' }>,
14
12
  baseFetch: FetchLike = fetch,
15
13
  ): FetchLike {
16
- const signer = new SignatureV4({
17
- service: auth.service,
18
- region: auth.region,
19
- credentials: auth.credentials,
20
- sha256: Sha256,
21
- })
22
-
23
14
  return async (input, init) => {
24
15
  // Request.toString() returns '[object Request]', not the URL — use .url instead.
25
16
  const href =
@@ -29,27 +20,16 @@ export function createSigV4Fetch(
29
20
  ? input.url
30
21
  : input.toString()
31
22
  const url = new URL(href)
32
- const headers: Record<string, string> = {}
33
- new Headers(init?.headers).forEach((v, k) => (headers[k] = v))
34
- headers['host'] = url.host
35
-
36
- const body = init?.body ?? undefined
37
-
38
- // Construct a plain object satisfying the @smithy/types HttpRequest interface —
39
- // no @smithy/protocol-http needed.
40
- const request: HttpRequest = {
41
- method: init?.method ?? 'GET',
42
- protocol: url.protocol,
43
- hostname: url.hostname,
44
- path: url.pathname + url.search,
45
- headers,
46
- body,
47
- }
48
-
49
- const signed = await signer.sign(request)
50
- return baseFetch(url.toString(), {
51
- ...init,
52
- headers: signed.headers as Record<string, string>,
53
- })
23
+ const headers = await signSigV4(
24
+ {
25
+ method: init?.method ?? 'GET',
26
+ url,
27
+ headers: init?.headers,
28
+ body: init?.body,
29
+ },
30
+ await auth.credentials(),
31
+ { service: auth.service, region: auth.region },
32
+ )
33
+ return baseFetch(url.toString(), { ...init, headers })
54
34
  }
55
35
  }
@@ -0,0 +1,202 @@
1
+ import type { AwsCredentialIdentity } from '@smithy/types'
2
+
3
+ // SigV4 header signing with WebCrypto. This is a port of the part of
4
+ // `@smithy/signature-v4` `sign()` that the Bedrock fetch wrapper uses: default
5
+ // options (path escape on, `x-amz-content-sha256` on), headers only.
6
+ // ponytail: no signing-key cache. Add one if the 5 HMAC steps per request show up in a profile.
7
+
8
+ const EMPTY_SHA256 =
9
+ 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
10
+
11
+ // SigV4 never signs these headers. Same list as `@smithy/signature-v4`.
12
+ const UNSIGNABLE_HEADERS = new Set([
13
+ 'authorization',
14
+ 'cache-control',
15
+ 'connection',
16
+ 'expect',
17
+ 'from',
18
+ 'keep-alive',
19
+ 'max-forwards',
20
+ 'pragma',
21
+ 'referer',
22
+ 'te',
23
+ 'trailer',
24
+ 'transfer-encoding',
25
+ 'upgrade',
26
+ 'user-agent',
27
+ 'x-amzn-trace-id',
28
+ ])
29
+
30
+ const encoder = new TextEncoder()
31
+
32
+ function toHex(bytes: ArrayBuffer) {
33
+ return Array.from(new Uint8Array(bytes), (byte) =>
34
+ byte.toString(16).padStart(2, '0'),
35
+ ).join('')
36
+ }
37
+
38
+ async function sha256Hex(data: BufferSource) {
39
+ return toHex(await crypto.subtle.digest('SHA-256', data))
40
+ }
41
+
42
+ async function hmac(key: BufferSource, data: string) {
43
+ const cryptoKey = await crypto.subtle.importKey(
44
+ 'raw',
45
+ key,
46
+ { name: 'HMAC', hash: 'SHA-256' },
47
+ false,
48
+ ['sign'],
49
+ )
50
+ return crypto.subtle.sign('HMAC', cryptoKey, encoder.encode(data))
51
+ }
52
+
53
+ // encodeURIComponent also encodes ! ' ( ) * for RFC 3986.
54
+ function escapeUri(value: string) {
55
+ return encodeURIComponent(value).replace(
56
+ /[!'()*]/g,
57
+ (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`,
58
+ )
59
+ }
60
+
61
+ // Remove empty, "." and ".." segments, then encode the path a second time.
62
+ // The wire path is already encoded once, and AWS (not S3) wants it twice.
63
+ function canonicalPath(pathname: string) {
64
+ const segments: Array<string> = []
65
+ for (const segment of pathname.split('/')) {
66
+ if (segment === '' || segment === '.') continue
67
+ if (segment === '..') segments.pop()
68
+ else segments.push(segment)
69
+ }
70
+ const trailingSlash = segments.length > 0 && pathname.endsWith('/') ? '/' : ''
71
+ return escapeUri(`/${segments.join('/')}${trailingSlash}`).replace(
72
+ /%2F/g,
73
+ '/',
74
+ )
75
+ }
76
+
77
+ // Decode each pair, encode it with the SigV4 rules, sort by key, then by value.
78
+ function canonicalQuery(search: string) {
79
+ const pairs = search
80
+ .slice(1)
81
+ .split('&')
82
+ .filter((pair) => pair !== '')
83
+ .map((pair) => {
84
+ const equals = pair.indexOf('=')
85
+ const key = equals === -1 ? pair : pair.slice(0, equals)
86
+ const value = equals === -1 ? '' : pair.slice(equals + 1)
87
+ return [
88
+ escapeUri(decodeURIComponent(key)),
89
+ escapeUri(decodeURIComponent(value)),
90
+ ]
91
+ })
92
+ pairs.sort(([keyA = '', valueA = ''], [keyB = '', valueB = '']) => {
93
+ if (keyA !== keyB) return keyA < keyB ? -1 : 1
94
+ if (valueA === valueB) return 0
95
+ return valueA < valueB ? -1 : 1
96
+ })
97
+ return pairs.map(([key, value]) => `${key}=${value}`).join('&')
98
+ }
99
+
100
+ // A stream, Blob, or FormData body cannot be hashed before it is sent.
101
+ async function payloadHash(body: BodyInit | null | undefined) {
102
+ if (body === null || body === undefined) return EMPTY_SHA256
103
+ if (typeof body === 'string') return sha256Hex(encoder.encode(body))
104
+ if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) {
105
+ return sha256Hex(body)
106
+ }
107
+ return 'UNSIGNED-PAYLOAD'
108
+ }
109
+
110
+ /**
111
+ * Signs an HTTP request with AWS Signature Version 4 (header auth).
112
+ *
113
+ * @param request - The request to sign. `host` always comes from `url`.
114
+ * @param credentials - Resolved AWS credentials. `sessionToken` adds `x-amz-security-token`.
115
+ * @param options - SigV4 `service` and `region`. `date` defaults to now.
116
+ * @returns The request headers plus `host`, `x-amz-date`,
117
+ * `x-amz-content-sha256`, `x-amz-security-token` (with a session token),
118
+ * and `authorization`.
119
+ *
120
+ * @example
121
+ * const headers = await signSigV4(
122
+ * { method: 'POST', url, headers: { 'content-type': 'application/json' }, body },
123
+ * await credentials(),
124
+ * { service: 'bedrock', region: 'us-east-1' },
125
+ * )
126
+ */
127
+ export async function signSigV4(
128
+ request: {
129
+ method: string
130
+ url: URL | string
131
+ headers?: HeadersInit
132
+ body?: BodyInit | null
133
+ },
134
+ credentials: AwsCredentialIdentity,
135
+ options: { service: string; region: string; date?: Date },
136
+ ) {
137
+ const url = new URL(request.url)
138
+ const headers = new Headers(request.headers)
139
+ headers.delete('authorization')
140
+ headers.delete('date')
141
+ headers.set('host', url.host)
142
+
143
+ const longDate = (options.date ?? new Date())
144
+ .toISOString()
145
+ .replace(/\.\d{3}Z$/, 'Z')
146
+ .replace(/[-:]/g, '')
147
+ const shortDate = longDate.slice(0, 8)
148
+ const scope = `${shortDate}/${options.region}/${options.service}/aws4_request`
149
+ headers.set('x-amz-date', longDate)
150
+ if (credentials.sessionToken) {
151
+ headers.set('x-amz-security-token', credentials.sessionToken)
152
+ }
153
+ const bodyHash =
154
+ headers.get('x-amz-content-sha256') ?? (await payloadHash(request.body))
155
+ headers.set('x-amz-content-sha256', bodyHash)
156
+
157
+ // Headers iterate with lowercase names in sorted order, as SigV4 needs.
158
+ const signed: Array<[string, string]> = []
159
+ for (const [name, value] of headers) {
160
+ const isUnsignable =
161
+ UNSIGNABLE_HEADERS.has(name) || /^(proxy|sec)-/.test(name)
162
+ if (isUnsignable) continue
163
+ signed.push([name, value.trim().replace(/\s+/g, ' ')])
164
+ }
165
+ const signedHeaders = signed.map(([name]) => name).join(';')
166
+ const canonicalRequest = [
167
+ request.method,
168
+ canonicalPath(url.pathname),
169
+ canonicalQuery(url.search),
170
+ ...signed.map(([name, value]) => `${name}:${value}`),
171
+ '',
172
+ signedHeaders,
173
+ bodyHash,
174
+ ].join('\n')
175
+
176
+ const stringToSign = [
177
+ 'AWS4-HMAC-SHA256',
178
+ longDate,
179
+ scope,
180
+ await sha256Hex(encoder.encode(canonicalRequest)),
181
+ ].join('\n')
182
+ let signingKey: BufferSource = encoder.encode(
183
+ `AWS4${credentials.secretAccessKey}`,
184
+ )
185
+ const scopeParts = [
186
+ shortDate,
187
+ options.region,
188
+ options.service,
189
+ 'aws4_request',
190
+ ]
191
+ for (const part of scopeParts) {
192
+ signingKey = await hmac(signingKey, part)
193
+ }
194
+ const signature = toHex(await hmac(signingKey, stringToSign))
195
+
196
+ headers.set(
197
+ 'authorization',
198
+ `AWS4-HMAC-SHA256 Credential=${credentials.accessKeyId}/${scope}, ` +
199
+ `SignedHeaders=${signedHeaders}, Signature=${signature}`,
200
+ )
201
+ return headers
202
+ }