@tanstack/ai-bedrock 1.1.0 → 1.2.1

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 (34) hide show
  1. package/README.md +2 -0
  2. package/dist/esm/adapters/converse-text.js +17 -14
  3. package/dist/esm/adapters/converse-text.js.map +1 -1
  4. package/dist/esm/adapters/embedding.js +2 -5
  5. package/dist/esm/adapters/embedding.js.map +1 -1
  6. package/dist/esm/adapters/responses-text.d.ts +1 -1
  7. package/dist/esm/adapters/responses-text.js +3 -4
  8. package/dist/esm/adapters/responses-text.js.map +1 -1
  9. package/dist/esm/adapters/text.d.ts +1 -1
  10. package/dist/esm/adapters/text.js +3 -4
  11. package/dist/esm/adapters/text.js.map +1 -1
  12. package/dist/esm/converse/stream-processor.js +1 -1
  13. package/dist/esm/converse/stream-processor.js.map +1 -1
  14. package/dist/esm/utils/auth.d.ts +6 -0
  15. package/dist/esm/utils/auth.js +17 -5
  16. package/dist/esm/utils/auth.js.map +1 -1
  17. package/dist/esm/utils/client.js +1 -1
  18. package/dist/esm/utils/client.js.map +1 -1
  19. package/dist/esm/utils/openai-sigv4-fetch.d.ts +2 -2
  20. package/dist/esm/utils/openai-sigv4-fetch.js +12 -23
  21. package/dist/esm/utils/openai-sigv4-fetch.js.map +1 -1
  22. package/dist/esm/utils/sigv4.d.ts +28 -0
  23. package/dist/esm/utils/sigv4.js +133 -0
  24. package/dist/esm/utils/sigv4.js.map +1 -0
  25. package/package.json +22 -7
  26. package/src/adapters/converse-text.ts +23 -13
  27. package/src/adapters/embedding.ts +2 -2
  28. package/src/adapters/responses-text.ts +6 -4
  29. package/src/adapters/text.ts +6 -4
  30. package/src/converse/stream-processor.ts +2 -1
  31. package/src/utils/auth.ts +19 -3
  32. package/src/utils/client.ts +1 -1
  33. package/src/utils/openai-sigv4-fetch.ts +14 -34
  34. package/src/utils/sigv4.ts +202 -0
@@ -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
  })
@@ -289,7 +289,8 @@ export async function* processConverseStream(
289
289
  ? 'tool_calls'
290
290
  : stopReason === 'max_tokens'
291
291
  ? 'length'
292
- : stopReason === 'content_filtered'
292
+ : stopReason === 'content_filtered' ||
293
+ stopReason === 'guardrail_intervened'
293
294
  ? 'content_filter'
294
295
  : 'stop'
295
296
  continue
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
+ }