@anthropic-ai/aws-sdk 0.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 (278) hide show
  1. package/.github/logo.svg +3 -0
  2. package/CHANGELOG.md +15 -0
  3. package/LICENSE +8 -0
  4. package/README.md +0 -0
  5. package/client.d.mts +109 -0
  6. package/client.d.mts.map +1 -0
  7. package/client.d.ts +109 -0
  8. package/client.d.ts.map +1 -0
  9. package/client.js +158 -0
  10. package/client.js.map +1 -0
  11. package/client.mjs +120 -0
  12. package/client.mjs.map +1 -0
  13. package/core/auth.d.mts +15 -0
  14. package/core/auth.d.mts.map +1 -0
  15. package/core/auth.d.ts +15 -0
  16. package/core/auth.d.ts.map +1 -0
  17. package/core/auth.js +113 -0
  18. package/core/auth.js.map +1 -0
  19. package/core/auth.mjs +73 -0
  20. package/core/auth.mjs.map +1 -0
  21. package/core/error.d.mts +2 -0
  22. package/core/error.d.mts.map +1 -0
  23. package/core/error.d.ts +2 -0
  24. package/core/error.d.ts.map +1 -0
  25. package/core/error.js +18 -0
  26. package/core/error.js.map +1 -0
  27. package/core/error.mjs +2 -0
  28. package/core/error.mjs.map +1 -0
  29. package/core/pagination.d.mts +2 -0
  30. package/core/pagination.d.mts.map +1 -0
  31. package/core/pagination.d.ts +2 -0
  32. package/core/pagination.d.ts.map +1 -0
  33. package/core/pagination.js +18 -0
  34. package/core/pagination.js.map +1 -0
  35. package/core/pagination.mjs +2 -0
  36. package/core/pagination.mjs.map +1 -0
  37. package/core/streaming.d.mts +2 -0
  38. package/core/streaming.d.mts.map +1 -0
  39. package/core/streaming.d.ts +2 -0
  40. package/core/streaming.d.ts.map +1 -0
  41. package/core/streaming.js +18 -0
  42. package/core/streaming.js.map +1 -0
  43. package/core/streaming.mjs +2 -0
  44. package/core/streaming.mjs.map +1 -0
  45. package/index.d.mts +4 -0
  46. package/index.d.mts.map +1 -0
  47. package/index.d.ts +4 -0
  48. package/index.d.ts.map +1 -0
  49. package/index.js +25 -0
  50. package/index.js.map +1 -0
  51. package/index.mjs +3 -0
  52. package/index.mjs.map +1 -0
  53. package/internal/builtin-types.d.mts +73 -0
  54. package/internal/builtin-types.d.mts.map +1 -0
  55. package/internal/builtin-types.d.ts +73 -0
  56. package/internal/builtin-types.d.ts.map +1 -0
  57. package/internal/builtin-types.js +4 -0
  58. package/internal/builtin-types.js.map +1 -0
  59. package/internal/builtin-types.mjs +3 -0
  60. package/internal/builtin-types.mjs.map +1 -0
  61. package/internal/constants.d.mts +5 -0
  62. package/internal/constants.d.mts.map +1 -0
  63. package/internal/constants.d.ts +5 -0
  64. package/internal/constants.d.ts.map +1 -0
  65. package/internal/constants.js +18 -0
  66. package/internal/constants.js.map +1 -0
  67. package/internal/constants.mjs +15 -0
  68. package/internal/constants.mjs.map +1 -0
  69. package/internal/decoders/jsonl.d.mts +10 -0
  70. package/internal/decoders/jsonl.d.mts.map +1 -0
  71. package/internal/decoders/jsonl.d.ts +10 -0
  72. package/internal/decoders/jsonl.d.ts.map +1 -0
  73. package/internal/decoders/jsonl.js +39 -0
  74. package/internal/decoders/jsonl.js.map +1 -0
  75. package/internal/decoders/jsonl.mjs +35 -0
  76. package/internal/decoders/jsonl.mjs.map +1 -0
  77. package/internal/decoders/line.d.mts +17 -0
  78. package/internal/decoders/line.d.mts.map +1 -0
  79. package/internal/decoders/line.d.ts +17 -0
  80. package/internal/decoders/line.d.ts.map +1 -0
  81. package/internal/decoders/line.js +123 -0
  82. package/internal/decoders/line.js.map +1 -0
  83. package/internal/decoders/line.mjs +118 -0
  84. package/internal/decoders/line.mjs.map +1 -0
  85. package/internal/errors.d.mts +3 -0
  86. package/internal/errors.d.mts.map +1 -0
  87. package/internal/errors.d.ts +3 -0
  88. package/internal/errors.d.ts.map +1 -0
  89. package/internal/errors.js +41 -0
  90. package/internal/errors.js.map +1 -0
  91. package/internal/errors.mjs +36 -0
  92. package/internal/errors.mjs.map +1 -0
  93. package/internal/headers.d.mts +22 -0
  94. package/internal/headers.d.mts.map +1 -0
  95. package/internal/headers.d.ts +22 -0
  96. package/internal/headers.d.ts.map +1 -0
  97. package/internal/headers.js +79 -0
  98. package/internal/headers.js.map +1 -0
  99. package/internal/headers.mjs +74 -0
  100. package/internal/headers.mjs.map +1 -0
  101. package/internal/parse.d.mts +17 -0
  102. package/internal/parse.d.mts.map +1 -0
  103. package/internal/parse.d.ts +17 -0
  104. package/internal/parse.d.ts.map +1 -0
  105. package/internal/parse.js +60 -0
  106. package/internal/parse.js.map +1 -0
  107. package/internal/parse.mjs +56 -0
  108. package/internal/parse.mjs.map +1 -0
  109. package/internal/request-options.d.mts +77 -0
  110. package/internal/request-options.d.mts.map +1 -0
  111. package/internal/request-options.d.ts +77 -0
  112. package/internal/request-options.d.ts.map +1 -0
  113. package/internal/request-options.js +14 -0
  114. package/internal/request-options.js.map +1 -0
  115. package/internal/request-options.mjs +10 -0
  116. package/internal/request-options.mjs.map +1 -0
  117. package/internal/shim-types.d.mts +17 -0
  118. package/internal/shim-types.d.mts.map +1 -0
  119. package/internal/shim-types.d.ts +17 -0
  120. package/internal/shim-types.d.ts.map +1 -0
  121. package/internal/shim-types.js +4 -0
  122. package/internal/shim-types.js.map +1 -0
  123. package/internal/shim-types.mjs +3 -0
  124. package/internal/shim-types.mjs.map +1 -0
  125. package/internal/shims.d.mts +20 -0
  126. package/internal/shims.d.mts.map +1 -0
  127. package/internal/shims.d.ts +20 -0
  128. package/internal/shims.d.ts.map +1 -0
  129. package/internal/shims.js +92 -0
  130. package/internal/shims.js.map +1 -0
  131. package/internal/shims.mjs +85 -0
  132. package/internal/shims.mjs.map +1 -0
  133. package/internal/stream-utils.d.mts +8 -0
  134. package/internal/stream-utils.d.mts.map +1 -0
  135. package/internal/stream-utils.d.ts +8 -0
  136. package/internal/stream-utils.d.ts.map +1 -0
  137. package/internal/stream-utils.js +38 -0
  138. package/internal/stream-utils.js.map +1 -0
  139. package/internal/stream-utils.mjs +35 -0
  140. package/internal/stream-utils.mjs.map +1 -0
  141. package/internal/to-file.d.mts +45 -0
  142. package/internal/to-file.d.mts.map +1 -0
  143. package/internal/to-file.d.ts +45 -0
  144. package/internal/to-file.d.ts.map +1 -0
  145. package/internal/to-file.js +96 -0
  146. package/internal/to-file.js.map +1 -0
  147. package/internal/to-file.mjs +93 -0
  148. package/internal/to-file.mjs.map +1 -0
  149. package/internal/types.d.mts +69 -0
  150. package/internal/types.d.mts.map +1 -0
  151. package/internal/types.d.ts +69 -0
  152. package/internal/types.d.ts.map +1 -0
  153. package/internal/types.js +4 -0
  154. package/internal/types.js.map +1 -0
  155. package/internal/types.mjs +3 -0
  156. package/internal/types.mjs.map +1 -0
  157. package/internal/uploads.d.mts +42 -0
  158. package/internal/uploads.d.mts.map +1 -0
  159. package/internal/uploads.d.ts +42 -0
  160. package/internal/uploads.d.ts.map +1 -0
  161. package/internal/uploads.js +145 -0
  162. package/internal/uploads.js.map +1 -0
  163. package/internal/uploads.mjs +135 -0
  164. package/internal/uploads.mjs.map +1 -0
  165. package/internal/utils/base64.d.mts +3 -0
  166. package/internal/utils/base64.d.mts.map +1 -0
  167. package/internal/utils/base64.d.ts +3 -0
  168. package/internal/utils/base64.d.ts.map +1 -0
  169. package/internal/utils/base64.js +38 -0
  170. package/internal/utils/base64.js.map +1 -0
  171. package/internal/utils/base64.mjs +33 -0
  172. package/internal/utils/base64.mjs.map +1 -0
  173. package/internal/utils/bytes.d.mts +4 -0
  174. package/internal/utils/bytes.d.mts.map +1 -0
  175. package/internal/utils/bytes.d.ts +4 -0
  176. package/internal/utils/bytes.d.ts.map +1 -0
  177. package/internal/utils/bytes.js +31 -0
  178. package/internal/utils/bytes.js.map +1 -0
  179. package/internal/utils/bytes.mjs +26 -0
  180. package/internal/utils/bytes.mjs.map +1 -0
  181. package/internal/utils/env.d.mts +9 -0
  182. package/internal/utils/env.d.mts.map +1 -0
  183. package/internal/utils/env.d.ts +9 -0
  184. package/internal/utils/env.d.ts.map +1 -0
  185. package/internal/utils/env.js +22 -0
  186. package/internal/utils/env.js.map +1 -0
  187. package/internal/utils/env.mjs +18 -0
  188. package/internal/utils/env.mjs.map +1 -0
  189. package/internal/utils/log.d.mts +37 -0
  190. package/internal/utils/log.d.mts.map +1 -0
  191. package/internal/utils/log.d.ts +37 -0
  192. package/internal/utils/log.d.ts.map +1 -0
  193. package/internal/utils/log.js +86 -0
  194. package/internal/utils/log.js.map +1 -0
  195. package/internal/utils/log.mjs +80 -0
  196. package/internal/utils/log.mjs.map +1 -0
  197. package/internal/utils/path.d.mts +15 -0
  198. package/internal/utils/path.d.mts.map +1 -0
  199. package/internal/utils/path.d.ts +15 -0
  200. package/internal/utils/path.d.ts.map +1 -0
  201. package/internal/utils/path.js +79 -0
  202. package/internal/utils/path.js.map +1 -0
  203. package/internal/utils/path.mjs +74 -0
  204. package/internal/utils/path.mjs.map +1 -0
  205. package/internal/utils/query.d.mts +5 -0
  206. package/internal/utils/query.d.mts.map +1 -0
  207. package/internal/utils/query.d.ts +5 -0
  208. package/internal/utils/query.d.ts.map +1 -0
  209. package/internal/utils/query.js +23 -0
  210. package/internal/utils/query.js.map +1 -0
  211. package/internal/utils/query.mjs +20 -0
  212. package/internal/utils/query.mjs.map +1 -0
  213. package/internal/utils/sleep.d.mts +2 -0
  214. package/internal/utils/sleep.d.mts.map +1 -0
  215. package/internal/utils/sleep.d.ts +2 -0
  216. package/internal/utils/sleep.d.ts.map +1 -0
  217. package/internal/utils/sleep.js +7 -0
  218. package/internal/utils/sleep.js.map +1 -0
  219. package/internal/utils/sleep.mjs +3 -0
  220. package/internal/utils/sleep.mjs.map +1 -0
  221. package/internal/utils/uuid.d.mts +5 -0
  222. package/internal/utils/uuid.d.mts.map +1 -0
  223. package/internal/utils/uuid.d.ts +5 -0
  224. package/internal/utils/uuid.d.ts.map +1 -0
  225. package/internal/utils/uuid.js +19 -0
  226. package/internal/utils/uuid.js.map +1 -0
  227. package/internal/utils/uuid.mjs +15 -0
  228. package/internal/utils/uuid.mjs.map +1 -0
  229. package/internal/utils/values.d.mts +19 -0
  230. package/internal/utils/values.d.mts.map +1 -0
  231. package/internal/utils/values.d.ts +19 -0
  232. package/internal/utils/values.d.ts.map +1 -0
  233. package/internal/utils/values.js +119 -0
  234. package/internal/utils/values.js.map +1 -0
  235. package/internal/utils/values.mjs +100 -0
  236. package/internal/utils/values.mjs.map +1 -0
  237. package/internal/utils.d.mts +8 -0
  238. package/internal/utils.d.mts.map +1 -0
  239. package/internal/utils.d.ts +8 -0
  240. package/internal/utils.d.ts.map +1 -0
  241. package/internal/utils.js +25 -0
  242. package/internal/utils.js.map +1 -0
  243. package/internal/utils.mjs +9 -0
  244. package/internal/utils.mjs.map +1 -0
  245. package/package.json +87 -0
  246. package/src/client.ts +239 -0
  247. package/src/core/auth.ts +101 -0
  248. package/src/core/error.ts +1 -0
  249. package/src/core/pagination.ts +1 -0
  250. package/src/core/streaming.ts +1 -0
  251. package/src/index.ts +4 -0
  252. package/src/internal/README.md +3 -0
  253. package/src/internal/builtin-types.ts +93 -0
  254. package/src/internal/constants.ts +15 -0
  255. package/src/internal/decoders/jsonl.ts +48 -0
  256. package/src/internal/decoders/line.ts +135 -0
  257. package/src/internal/detect-platform.ts +196 -0
  258. package/src/internal/errors.ts +33 -0
  259. package/src/internal/headers.ts +99 -0
  260. package/src/internal/parse.ts +90 -0
  261. package/src/internal/request-options.ts +93 -0
  262. package/src/internal/shim-types.ts +26 -0
  263. package/src/internal/shims.ts +107 -0
  264. package/src/internal/stream-utils.ts +32 -0
  265. package/src/internal/to-file.ts +159 -0
  266. package/src/internal/types.ts +95 -0
  267. package/src/internal/uploads.ts +204 -0
  268. package/src/internal/utils/base64.ts +40 -0
  269. package/src/internal/utils/bytes.ts +32 -0
  270. package/src/internal/utils/env.ts +18 -0
  271. package/src/internal/utils/log.ts +127 -0
  272. package/src/internal/utils/path.ts +88 -0
  273. package/src/internal/utils/query.ts +23 -0
  274. package/src/internal/utils/sleep.ts +3 -0
  275. package/src/internal/utils/uuid.ts +17 -0
  276. package/src/internal/utils/values.ts +112 -0
  277. package/src/internal/utils.ts +9 -0
  278. package/src/tsconfig.json +11 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/internal/utils.ts"],"names":[],"mappings":"AAAA,sFAAsF"}
package/package.json ADDED
@@ -0,0 +1,87 @@
1
+ {
2
+ "name": "@anthropic-ai/aws-sdk",
3
+ "version": "0.2.0",
4
+ "description": "The official TypeScript library for the Anthropic AWS API",
5
+ "author": "Anthropic <support@anthropic.com>",
6
+ "types": "./index.d.ts",
7
+ "main": "./index.js",
8
+ "type": "commonjs",
9
+ "repository": "github:anthropics/anthropic-sdk-typescript",
10
+ "license": "MIT",
11
+ "packageManager": "yarn@1.22.21",
12
+ "private": false,
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "scripts": {
17
+ "test": "jest",
18
+ "build": "bash ./build",
19
+ "format": "prettier --write --cache --cache-strategy metadata . !dist",
20
+ "tsn": "ts-node -r tsconfig-paths/register",
21
+ "lint": "eslint --ext ts,js .",
22
+ "fix": "eslint --fix --ext ts,js ."
23
+ },
24
+ "dependencies": {
25
+ "@anthropic-ai/sdk": ">=0.50.3 <1",
26
+ "@aws-crypto/sha256-js": "^4.0.0",
27
+ "@aws-sdk/credential-providers": "^3.796.0",
28
+ "@smithy/fetch-http-handler": "^5.0.4",
29
+ "@smithy/protocol-http": "^3.0.6",
30
+ "@smithy/signature-v4": "^3.1.1",
31
+ "@smithy/types": "^2.3.4"
32
+ },
33
+ "imports": {
34
+ "@anthropic-ai/aws-sdk": ".",
35
+ "@anthropic-ai/aws-sdk/*": "./src/*"
36
+ },
37
+ "exports": {
38
+ ".": {
39
+ "require": {
40
+ "types": "./index.d.ts",
41
+ "default": "./index.js"
42
+ },
43
+ "types": "./index.d.mts",
44
+ "default": "./index.mjs"
45
+ },
46
+ "./.github/*.mjs": {
47
+ "default": "./.github/*.mjs"
48
+ },
49
+ "./.github/*.js": {
50
+ "default": "./.github/*.js"
51
+ },
52
+ "./.github/*": {
53
+ "import": "./.github/*.mjs",
54
+ "require": "./.github/*.js"
55
+ },
56
+ "./client": {
57
+ "import": "./client.mjs",
58
+ "require": "./client.js"
59
+ },
60
+ "./client.js": {
61
+ "default": "./client.js"
62
+ },
63
+ "./client.mjs": {
64
+ "default": "./client.mjs"
65
+ },
66
+ "./core/*.mjs": {
67
+ "default": "./core/*.mjs"
68
+ },
69
+ "./core/*.js": {
70
+ "default": "./core/*.js"
71
+ },
72
+ "./core/*": {
73
+ "import": "./core/*.mjs",
74
+ "require": "./core/*.js"
75
+ },
76
+ "./index": {
77
+ "import": "./index.mjs",
78
+ "require": "./index.js"
79
+ },
80
+ "./index.js": {
81
+ "default": "./index.js"
82
+ },
83
+ "./index.mjs": {
84
+ "default": "./index.mjs"
85
+ }
86
+ }
87
+ }
package/src/client.ts ADDED
@@ -0,0 +1,239 @@
1
+ import type { NullableHeaders } from './internal/headers';
2
+ import { buildHeaders } from './internal/headers';
3
+ import * as Errors from './core/error';
4
+ import { readEnv } from './internal/utils';
5
+ import { Anthropic, ClientOptions } from '@anthropic-ai/sdk/client';
6
+ export { BaseAnthropic } from '@anthropic-ai/sdk/client';
7
+ import { AwsCredentialIdentityProvider } from '@smithy/types';
8
+ import { getAuthHeaders } from './core/auth';
9
+ import { FinalRequestOptions } from './internal/request-options';
10
+ import { FinalizedRequestInit } from './internal/types';
11
+
12
+ const DEFAULT_SERVICE_NAME = 'aws-external-anthropic';
13
+
14
+ export interface AwsClientOptions extends ClientOptions {
15
+ /**
16
+ * AWS region for the API gateway.
17
+ *
18
+ * Resolved by precedence: `awsRegion` arg > `AWS_REGION` env var > `AWS_DEFAULT_REGION` env var.
19
+ */
20
+ awsRegion?: string | undefined;
21
+
22
+ /**
23
+ * API key for x-api-key authentication.
24
+ *
25
+ * Takes precedence over AWS credential options. If neither `apiKey` nor
26
+ * AWS credentials are provided, falls back to the `ANTHROPIC_AWS_API_KEY`
27
+ * environment variable, then to the default AWS credential chain.
28
+ */
29
+ apiKey?: string | undefined;
30
+
31
+ /**
32
+ * AWS access key ID for SigV4 authentication.
33
+ *
34
+ * Must be provided together with `awsSecretAccessKey`.
35
+ */
36
+ awsAccessKey?: string | null | undefined;
37
+
38
+ /**
39
+ * AWS secret access key for SigV4 authentication.
40
+ *
41
+ * Must be provided together with `awsAccessKey`.
42
+ */
43
+ awsSecretAccessKey?: string | null | undefined;
44
+
45
+ /**
46
+ * AWS session token for temporary credentials.
47
+ */
48
+ awsSessionToken?: string | null | undefined;
49
+
50
+ /**
51
+ * AWS named profile for credential resolution.
52
+ *
53
+ * When set, credentials are loaded from the AWS credential chain
54
+ * using this profile.
55
+ */
56
+ awsProfile?: string | undefined;
57
+
58
+ /**
59
+ * Custom provider chain resolver for AWS credentials.
60
+ * Useful for non-Node environments, like edge workers, where the default
61
+ * credential provider chain may not work.
62
+ */
63
+ providerChainResolver?: (() => Promise<AwsCredentialIdentityProvider>) | null;
64
+
65
+ /**
66
+ * Workspace ID sent on every request as the `anthropic-workspace-id` header.
67
+ *
68
+ * Resolved by precedence: `workspaceId` arg > `ANTHROPIC_AWS_WORKSPACE_ID` env var.
69
+ */
70
+ workspaceId?: string | undefined;
71
+
72
+ /**
73
+ * Skip authentication for requests. This is useful when you have a gateway
74
+ * or proxy that handles authentication on your behalf.
75
+ *
76
+ * @default false
77
+ */
78
+ skipAuth?: boolean;
79
+ }
80
+
81
+ /** API Client for interfacing with the Anthropic AWS API. */
82
+ export class AnthropicAws extends Anthropic {
83
+ awsRegion: string | undefined;
84
+ awsAccessKey: string | null;
85
+ awsSecretAccessKey: string | null;
86
+ awsSessionToken: string | null;
87
+ awsProfile: string | null;
88
+ providerChainResolver: (() => Promise<AwsCredentialIdentityProvider>) | null;
89
+ workspaceId: string | undefined;
90
+ skipAuth: boolean = false;
91
+
92
+ private _useSigV4: boolean;
93
+
94
+ /**
95
+ * API Client for interfacing with the Anthropic AWS API.
96
+ *
97
+ * Auth is resolved by precedence: `apiKey` constructor arg > explicit AWS
98
+ * credentials > `awsProfile` > `ANTHROPIC_AWS_API_KEY` env var > default
99
+ * AWS credential chain.
100
+ *
101
+ * @param {string | undefined} [opts.apiKey] - API key for x-api-key authentication.
102
+ * @param {string | null | undefined} [opts.awsAccessKey] - AWS access key ID for SigV4 authentication.
103
+ * @param {string | null | undefined} [opts.awsSecretAccessKey] - AWS secret access key for SigV4 authentication.
104
+ * @param {string | null | undefined} [opts.awsSessionToken] - AWS session token for temporary credentials.
105
+ * @param {string | undefined} [opts.awsProfile] - AWS named profile for credential resolution.
106
+ * @param {string | undefined} [opts.awsRegion] - AWS region. Resolved by precedence: arg > `AWS_REGION` env > `AWS_DEFAULT_REGION` env.
107
+ * @param {(() => Promise<AwsCredentialIdentityProvider>) | null} [opts.providerChainResolver] - Custom provider chain resolver for AWS credentials.
108
+ * @param {string | undefined} [opts.workspaceId] - Workspace ID sent as `anthropic-workspace-id` header. Resolved by precedence: arg > `ANTHROPIC_AWS_WORKSPACE_ID` env var.
109
+ * @param {string} [opts.baseURL=process.env['ANTHROPIC_AWS_BASE_URL'] ?? https://aws-external-anthropic.{awsRegion}.api.aws] - Override the default base URL for the API.
110
+ * @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
111
+ * @param {MergedRequestInit} [opts.fetchOptions] - Additional `RequestInit` options to be passed to `fetch` calls.
112
+ * @param {Fetch} [opts.fetch] - Specify a custom `fetch` function implementation.
113
+ * @param {number} [opts.maxRetries=2] - The maximum number of times the client will retry a request.
114
+ * @param {HeadersLike} opts.defaultHeaders - Default headers to include with every request to the API.
115
+ * @param {Record<string, string | undefined>} opts.defaultQuery - Default query parameters to include with every request to the API.
116
+ * @param {boolean} [opts.dangerouslyAllowBrowser=false] - By default, client-side use of this library is not allowed, as it risks exposing your secret API credentials to attackers.
117
+ * @param {boolean} [opts.skipAuth=false] - Skip authentication for requests. This is useful when you have a gateway or proxy that handles authentication on your behalf.
118
+ */
119
+ constructor({
120
+ awsRegion,
121
+ baseURL,
122
+ apiKey,
123
+ awsAccessKey = null,
124
+ awsSecretAccessKey = null,
125
+ awsSessionToken = null,
126
+ awsProfile,
127
+ providerChainResolver = null,
128
+ workspaceId,
129
+ skipAuth = false,
130
+ ...opts
131
+ }: AwsClientOptions = {}) {
132
+ // Region resolution: arg > AWS_REGION env > AWS_DEFAULT_REGION env
133
+ const resolvedRegion = awsRegion ?? readEnv('AWS_REGION') ?? readEnv('AWS_DEFAULT_REGION');
134
+
135
+ const resolvedBaseURL =
136
+ baseURL ??
137
+ readEnv('ANTHROPIC_AWS_BASE_URL') ??
138
+ (resolvedRegion ? `https://aws-external-anthropic.${resolvedRegion}.api.aws` : undefined);
139
+
140
+ if (!resolvedBaseURL && !skipAuth) {
141
+ throw new Errors.AnthropicError(
142
+ 'No AWS region or base URL found. Set `awsRegion` in the constructor, the `AWS_REGION` / `AWS_DEFAULT_REGION` environment variable, or provide a `baseURL` / `ANTHROPIC_AWS_BASE_URL` environment variable.',
143
+ );
144
+ }
145
+
146
+ // Precedence-based auth resolution:
147
+ // 1. apiKey constructor arg
148
+ // 2. awsAccessKey/awsSecretAccessKey constructor args (SigV4)
149
+ // 3. awsProfile constructor arg (SigV4)
150
+ // 4. ANTHROPIC_AWS_API_KEY env var
151
+ // 5. Default AWS credential chain (SigV4)
152
+ const hasExplicitApiKey = apiKey != null;
153
+ const hasPartialAwsCreds = (awsAccessKey != null) !== (awsSecretAccessKey != null);
154
+ if (hasPartialAwsCreds) {
155
+ throw new Errors.AnthropicError(
156
+ '`awsAccessKey` and `awsSecretAccessKey` must be provided together. You provided only one.',
157
+ );
158
+ }
159
+ const hasExplicitAwsCreds = awsAccessKey != null && awsSecretAccessKey != null;
160
+ const hasAwsProfile = awsProfile != null;
161
+
162
+ let resolvedApiKey: string | undefined;
163
+ if (hasExplicitApiKey) {
164
+ resolvedApiKey = apiKey;
165
+ } else if (!hasExplicitAwsCreds && !hasAwsProfile) {
166
+ resolvedApiKey = readEnv('ANTHROPIC_AWS_API_KEY') ?? undefined;
167
+ }
168
+
169
+ const resolvedWorkspaceId = workspaceId ?? readEnv('ANTHROPIC_AWS_WORKSPACE_ID');
170
+ if (!resolvedWorkspaceId && !skipAuth) {
171
+ throw new Errors.AnthropicError(
172
+ 'No workspace ID found. Set `workspaceId` in the constructor or the `ANTHROPIC_AWS_WORKSPACE_ID` environment variable.',
173
+ );
174
+ }
175
+
176
+ super({
177
+ apiKey: resolvedApiKey,
178
+ baseURL: resolvedBaseURL,
179
+ ...opts,
180
+ defaultHeaders: buildHeaders([{ 'anthropic-workspace-id': resolvedWorkspaceId }, opts.defaultHeaders]),
181
+ });
182
+
183
+ this.awsRegion = resolvedRegion;
184
+ this.awsAccessKey = awsAccessKey;
185
+ this.awsSecretAccessKey = awsSecretAccessKey;
186
+ this.awsSessionToken = awsSessionToken;
187
+ this.awsProfile = awsProfile ?? null;
188
+ this.providerChainResolver = providerChainResolver;
189
+ this.workspaceId = resolvedWorkspaceId;
190
+ this.skipAuth = skipAuth;
191
+ this._useSigV4 = resolvedApiKey == null;
192
+ }
193
+
194
+ protected override async authHeaders(opts: FinalRequestOptions): Promise<NullableHeaders | undefined> {
195
+ if (this.skipAuth) {
196
+ return undefined;
197
+ }
198
+
199
+ if (!this._useSigV4) {
200
+ // API key mode — use inherited x-api-key auth
201
+ return super.authHeaders(opts);
202
+ }
203
+
204
+ // SigV4 mode — auth is handled in prepareRequest since it needs the full request
205
+ return undefined;
206
+ }
207
+
208
+ protected override validateHeaders(): void {
209
+ // Auth validation is handled in the constructor and prepareRequest
210
+ }
211
+
212
+ protected override async prepareRequest(
213
+ request: FinalizedRequestInit,
214
+ { url, options }: { url: string; options: FinalRequestOptions },
215
+ ): Promise<void> {
216
+ if (this.skipAuth || !this._useSigV4) {
217
+ return;
218
+ }
219
+
220
+ const regionName = this.awsRegion;
221
+ if (!regionName) {
222
+ throw new Errors.AnthropicError(
223
+ 'No AWS region found. Set `awsRegion` in the constructor or the `AWS_REGION` / `AWS_DEFAULT_REGION` environment variable.',
224
+ );
225
+ }
226
+
227
+ const headers = await getAuthHeaders(request, {
228
+ url,
229
+ regionName,
230
+ serviceName: DEFAULT_SERVICE_NAME,
231
+ awsAccessKey: this.awsAccessKey,
232
+ awsSecretAccessKey: this.awsSecretAccessKey,
233
+ awsSessionToken: this.awsSessionToken,
234
+ awsProfile: this.awsProfile,
235
+ providerChainResolver: this.providerChainResolver,
236
+ });
237
+ request.headers = buildHeaders([headers, request.headers]).values;
238
+ }
239
+ }
@@ -0,0 +1,101 @@
1
+ import { Sha256 } from '@aws-crypto/sha256-js';
2
+ import { FetchHttpHandler } from '@smithy/fetch-http-handler';
3
+ import { HttpRequest } from '@smithy/protocol-http';
4
+ import { SignatureV4 } from '@smithy/signature-v4';
5
+ import { AwsCredentialIdentityProvider } from '@smithy/types';
6
+ import assert from 'assert';
7
+ import { MergedRequestInit } from '../internal/types';
8
+
9
+ export type AuthProps = {
10
+ url: string;
11
+ regionName: string;
12
+ serviceName: string;
13
+ awsAccessKey: string | null | undefined;
14
+ awsSecretAccessKey: string | null | undefined;
15
+ awsSessionToken: string | null | undefined;
16
+ awsProfile?: string | null | undefined;
17
+ fetchOptions?: MergedRequestInit | undefined;
18
+ providerChainResolver?: (() => Promise<AwsCredentialIdentityProvider>) | null;
19
+ };
20
+
21
+ const defaultProviderChainResolver = (profile?: string | null): Promise<AwsCredentialIdentityProvider> =>
22
+ import('@aws-sdk/credential-providers')
23
+ .then(({ fromNodeProviderChain }) =>
24
+ fromNodeProviderChain({
25
+ ...(profile != null ? { profile } : {}),
26
+ clientConfig: {
27
+ requestHandler: new FetchHttpHandler({
28
+ requestInit: (httpRequest) => {
29
+ return {
30
+ ...httpRequest,
31
+ } as RequestInit;
32
+ },
33
+ }),
34
+ },
35
+ }),
36
+ )
37
+ .catch((error) => {
38
+ throw new Error(
39
+ `Failed to import '@aws-sdk/credential-providers'. ` +
40
+ `You can provide a custom \`providerChainResolver\` in the client options if your runtime does not have access to '@aws-sdk/credential-providers': ` +
41
+ `\`new AnthropicAws({ providerChainResolver })\` ` +
42
+ `Original error: ${error.message}`,
43
+ );
44
+ });
45
+
46
+ export const getAuthHeaders = async (req: RequestInit, props: AuthProps): Promise<Record<string, string>> => {
47
+ assert(req.method, 'Expected request method property to be set');
48
+
49
+ let credentials;
50
+ if (props.awsAccessKey && props.awsSecretAccessKey) {
51
+ credentials = {
52
+ accessKeyId: props.awsAccessKey,
53
+ secretAccessKey: props.awsSecretAccessKey,
54
+ ...(props.awsSessionToken != null && { sessionToken: props.awsSessionToken }),
55
+ };
56
+ } else if (props.providerChainResolver) {
57
+ const provider = await props.providerChainResolver();
58
+ credentials = await provider();
59
+ } else {
60
+ const provider = await defaultProviderChainResolver(props.awsProfile);
61
+ credentials = await provider();
62
+ }
63
+
64
+ const signer = new SignatureV4({
65
+ service: props.serviceName,
66
+ region: props.regionName,
67
+ credentials,
68
+ sha256: Sha256,
69
+ });
70
+
71
+ const url = new URL(props.url);
72
+
73
+ const headers =
74
+ !req.headers ? {}
75
+ : Symbol.iterator in req.headers ?
76
+ Object.fromEntries(Array.from(req.headers).map((header) => [...header]))
77
+ : { ...req.headers };
78
+
79
+ // The connection header may be stripped by a proxy somewhere, so the receiver
80
+ // of this message may not see this header, so we remove it from the set of headers
81
+ // that are signed.
82
+ delete headers['connection'];
83
+ headers['host'] = url.hostname;
84
+
85
+ const query: Record<string, string> = {};
86
+ url.searchParams.forEach((value, key) => {
87
+ query[key] = value;
88
+ });
89
+
90
+ const request = new HttpRequest({
91
+ method: req.method.toUpperCase(),
92
+ protocol: url.protocol,
93
+ path: url.pathname,
94
+ query,
95
+ headers,
96
+ body: req.body,
97
+ });
98
+
99
+ const signed = await signer.sign(request);
100
+ return signed.headers;
101
+ };
@@ -0,0 +1 @@
1
+ export * from '@anthropic-ai/sdk/core/error';
@@ -0,0 +1 @@
1
+ export * from '@anthropic-ai/sdk/core/pagination';
@@ -0,0 +1 @@
1
+ export * from '@anthropic-ai/sdk/core/streaming';
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ // Auth internals are shared via symlink, not via package exports.
2
+ export * from './client';
3
+ export { AnthropicAws as default } from './client';
4
+ export type { AwsClientOptions } from './client';
@@ -0,0 +1,3 @@
1
+ # `internal`
2
+
3
+ The modules in this directory are not importable outside this package and will change between releases.
@@ -0,0 +1,93 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ export type Fetch = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
4
+
5
+ /**
6
+ * An alias to the builtin `RequestInit` type so we can
7
+ * easily alias it in import statements if there are name clashes.
8
+ *
9
+ * https://developer.mozilla.org/docs/Web/API/RequestInit
10
+ */
11
+ type _RequestInit = RequestInit;
12
+
13
+ /**
14
+ * An alias to the builtin `Response` type so we can
15
+ * easily alias it in import statements if there are name clashes.
16
+ *
17
+ * https://developer.mozilla.org/docs/Web/API/Response
18
+ */
19
+ type _Response = Response;
20
+
21
+ /**
22
+ * The type for the first argument to `fetch`.
23
+ *
24
+ * https://developer.mozilla.org/docs/Web/API/Window/fetch#resource
25
+ */
26
+ type _RequestInfo = Request | URL | string;
27
+
28
+ /**
29
+ * The type for constructing `RequestInit` Headers.
30
+ *
31
+ * https://developer.mozilla.org/docs/Web/API/RequestInit#setting_headers
32
+ */
33
+ type _HeadersInit = RequestInit['headers'];
34
+
35
+ /**
36
+ * The type for constructing `RequestInit` body.
37
+ *
38
+ * https://developer.mozilla.org/docs/Web/API/RequestInit#body
39
+ */
40
+ type _BodyInit = RequestInit['body'];
41
+
42
+ /**
43
+ * An alias to the builtin `Array<T>` type so we can
44
+ * easily alias it in import statements if there are name clashes.
45
+ */
46
+ type _Array<T> = Array<T>;
47
+
48
+ /**
49
+ * An alias to the builtin `Record<K, T>` type so we can
50
+ * easily alias it in import statements if there are name clashes.
51
+ */
52
+ type _Record<K extends keyof any, T> = Record<K, T>;
53
+
54
+ export type {
55
+ _Array as Array,
56
+ _BodyInit as BodyInit,
57
+ _HeadersInit as HeadersInit,
58
+ _Record as Record,
59
+ _RequestInfo as RequestInfo,
60
+ _RequestInit as RequestInit,
61
+ _Response as Response,
62
+ };
63
+
64
+ /**
65
+ * A copy of the builtin `EndingType` type as it isn't fully supported in certain
66
+ * environments and attempting to reference the global version will error.
67
+ *
68
+ * https://github.com/microsoft/TypeScript/blob/49ad1a3917a0ea57f5ff248159256e12bb1cb705/src/lib/dom.generated.d.ts#L27941
69
+ */
70
+ type EndingType = 'native' | 'transparent';
71
+
72
+ /**
73
+ * A copy of the builtin `BlobPropertyBag` type as it isn't fully supported in certain
74
+ * environments and attempting to reference the global version will error.
75
+ *
76
+ * https://github.com/microsoft/TypeScript/blob/49ad1a3917a0ea57f5ff248159256e12bb1cb705/src/lib/dom.generated.d.ts#L154
77
+ * https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob#options
78
+ */
79
+ export interface BlobPropertyBag {
80
+ endings?: EndingType;
81
+ type?: string;
82
+ }
83
+
84
+ /**
85
+ * A copy of the builtin `FilePropertyBag` type as it isn't fully supported in certain
86
+ * environments and attempting to reference the global version will error.
87
+ *
88
+ * https://github.com/microsoft/TypeScript/blob/49ad1a3917a0ea57f5ff248159256e12bb1cb705/src/lib/dom.generated.d.ts#L503
89
+ * https://developer.mozilla.org/en-US/docs/Web/API/File/File#options
90
+ */
91
+ export interface FilePropertyBag extends BlobPropertyBag {
92
+ lastModified?: number;
93
+ }
@@ -0,0 +1,15 @@
1
+ // File containing shared constants
2
+
3
+ /**
4
+ * Model-specific timeout constraints for non-streaming requests
5
+ */
6
+ export const MODEL_NONSTREAMING_TOKENS: Record<string, number> = {
7
+ 'claude-opus-4-20250514': 8192,
8
+ 'claude-opus-4-0': 8192,
9
+ 'claude-4-opus-20250514': 8192,
10
+ 'anthropic.claude-opus-4-20250514-v1:0': 8192,
11
+ 'claude-opus-4@20250514': 8192,
12
+ 'claude-opus-4-1-20250805': 8192,
13
+ 'anthropic.claude-opus-4-1-20250805-v1:0': 8192,
14
+ 'claude-opus-4-1@20250805': 8192,
15
+ };
@@ -0,0 +1,48 @@
1
+ import { AnthropicError } from '../../core/error';
2
+ import { ReadableStreamToAsyncIterable } from '../shims';
3
+ import { LineDecoder, type Bytes } from './line';
4
+
5
+ export class JSONLDecoder<T> {
6
+ controller: AbortController;
7
+
8
+ constructor(
9
+ private iterator: AsyncIterableIterator<Bytes>,
10
+ controller: AbortController,
11
+ ) {
12
+ this.controller = controller;
13
+ }
14
+
15
+ private async *decoder(): AsyncIterator<T, any, undefined> {
16
+ const lineDecoder = new LineDecoder();
17
+ for await (const chunk of this.iterator) {
18
+ for (const line of lineDecoder.decode(chunk)) {
19
+ yield JSON.parse(line) as T;
20
+ }
21
+ }
22
+
23
+ for (const line of lineDecoder.flush()) {
24
+ yield JSON.parse(line) as T;
25
+ }
26
+ }
27
+
28
+ [Symbol.asyncIterator](): AsyncIterator<T> {
29
+ return this.decoder();
30
+ }
31
+
32
+ static fromResponse<T>(response: Response, controller: AbortController): JSONLDecoder<T> {
33
+ if (!response.body) {
34
+ controller.abort();
35
+ if (
36
+ typeof (globalThis as any).navigator !== 'undefined' &&
37
+ (globalThis as any).navigator.product === 'ReactNative'
38
+ ) {
39
+ throw new AnthropicError(
40
+ `The default react-native fetch implementation does not support streaming. Please use expo/fetch: https://docs.expo.dev/versions/latest/sdk/expo/#expofetch-api`,
41
+ );
42
+ }
43
+ throw new AnthropicError(`Attempted to iterate over a response with no body`);
44
+ }
45
+
46
+ return new JSONLDecoder(ReadableStreamToAsyncIterable<Bytes>(response.body), controller);
47
+ }
48
+ }