@anthropic-ai/sdk 0.92.0 → 0.94.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 (97) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/client.d.mts +73 -0
  3. package/client.d.mts.map +1 -1
  4. package/client.d.ts +73 -0
  5. package/client.d.ts.map +1 -1
  6. package/client.js +258 -9
  7. package/client.js.map +1 -1
  8. package/client.mjs +258 -9
  9. package/client.mjs.map +1 -1
  10. package/core/credentials.d.mts +131 -0
  11. package/core/credentials.d.mts.map +1 -0
  12. package/core/credentials.d.ts +131 -0
  13. package/core/credentials.d.ts.map +1 -0
  14. package/core/credentials.js +317 -0
  15. package/core/credentials.js.map +1 -0
  16. package/core/credentials.mjs +277 -0
  17. package/core/credentials.mjs.map +1 -0
  18. package/internal/utils/time.d.mts +3 -0
  19. package/internal/utils/time.d.mts.map +1 -0
  20. package/internal/utils/time.d.ts +3 -0
  21. package/internal/utils/time.d.ts.map +1 -0
  22. package/internal/utils/time.js +8 -0
  23. package/internal/utils/time.js.map +1 -0
  24. package/internal/utils/time.mjs +5 -0
  25. package/internal/utils/time.mjs.map +1 -0
  26. package/lib/credentials/credential-chain.d.mts +39 -0
  27. package/lib/credentials/credential-chain.d.mts.map +1 -0
  28. package/lib/credentials/credential-chain.d.ts +39 -0
  29. package/lib/credentials/credential-chain.d.ts.map +1 -0
  30. package/lib/credentials/credential-chain.js +245 -0
  31. package/lib/credentials/credential-chain.js.map +1 -0
  32. package/lib/credentials/credential-chain.mjs +208 -0
  33. package/lib/credentials/credential-chain.mjs.map +1 -0
  34. package/lib/credentials/identity-token.d.mts +11 -0
  35. package/lib/credentials/identity-token.d.mts.map +1 -0
  36. package/lib/credentials/identity-token.d.ts +11 -0
  37. package/lib/credentials/identity-token.d.ts.map +1 -0
  38. package/lib/credentials/identity-token.js +72 -0
  39. package/lib/credentials/identity-token.js.map +1 -0
  40. package/lib/credentials/identity-token.mjs +35 -0
  41. package/lib/credentials/identity-token.mjs.map +1 -0
  42. package/lib/credentials/oidc-federation.d.mts +40 -0
  43. package/lib/credentials/oidc-federation.d.mts.map +1 -0
  44. package/lib/credentials/oidc-federation.d.ts +40 -0
  45. package/lib/credentials/oidc-federation.d.ts.map +1 -0
  46. package/lib/credentials/oidc-federation.js +82 -0
  47. package/lib/credentials/oidc-federation.js.map +1 -0
  48. package/lib/credentials/oidc-federation.mjs +79 -0
  49. package/lib/credentials/oidc-federation.mjs.map +1 -0
  50. package/lib/credentials/token-cache.d.mts +54 -0
  51. package/lib/credentials/token-cache.d.mts.map +1 -0
  52. package/lib/credentials/token-cache.d.ts +54 -0
  53. package/lib/credentials/token-cache.d.ts.map +1 -0
  54. package/lib/credentials/token-cache.js +112 -0
  55. package/lib/credentials/token-cache.js.map +1 -0
  56. package/lib/credentials/token-cache.mjs +108 -0
  57. package/lib/credentials/token-cache.mjs.map +1 -0
  58. package/lib/credentials/types.d.mts +96 -0
  59. package/lib/credentials/types.d.mts.map +1 -0
  60. package/lib/credentials/types.d.ts +96 -0
  61. package/lib/credentials/types.d.ts.map +1 -0
  62. package/lib/credentials/types.js +266 -0
  63. package/lib/credentials/types.js.map +1 -0
  64. package/lib/credentials/types.mjs +224 -0
  65. package/lib/credentials/types.mjs.map +1 -0
  66. package/lib/credentials/user-oauth.d.mts +21 -0
  67. package/lib/credentials/user-oauth.d.mts.map +1 -0
  68. package/lib/credentials/user-oauth.d.ts +21 -0
  69. package/lib/credentials/user-oauth.d.ts.map +1 -0
  70. package/lib/credentials/user-oauth.js +130 -0
  71. package/lib/credentials/user-oauth.js.map +1 -0
  72. package/lib/credentials/user-oauth.mjs +94 -0
  73. package/lib/credentials/user-oauth.mjs.map +1 -0
  74. package/lib/credentials.d.mts +4 -0
  75. package/lib/credentials.d.mts.map +1 -0
  76. package/lib/credentials.d.ts +4 -0
  77. package/lib/credentials.d.ts.map +1 -0
  78. package/lib/credentials.js +8 -0
  79. package/lib/credentials.js.map +1 -0
  80. package/lib/credentials.mjs +3 -0
  81. package/lib/credentials.mjs.map +1 -0
  82. package/package.json +1 -1
  83. package/src/client.ts +342 -14
  84. package/src/core/credentials.ts +379 -0
  85. package/src/internal/utils/time.ts +4 -0
  86. package/src/lib/credentials/credential-chain.ts +292 -0
  87. package/src/lib/credentials/identity-token.ts +37 -0
  88. package/src/lib/credentials/oidc-federation.ts +140 -0
  89. package/src/lib/credentials/token-cache.ts +130 -0
  90. package/src/lib/credentials/types.ts +295 -0
  91. package/src/lib/credentials/user-oauth.ts +144 -0
  92. package/src/lib/credentials.ts +3 -0
  93. package/src/version.ts +1 -1
  94. package/version.d.mts +1 -1
  95. package/version.d.ts +1 -1
  96. package/version.js +1 -1
  97. package/version.mjs +1 -1
@@ -0,0 +1,144 @@
1
+ import type { Fetch } from '../../internal/builtin-types';
2
+ import { CREDENTIALS_FILE_VERSION, type AnthropicCredentials } from '../../core/credentials';
3
+ import type { AccessTokenProvider } from './types';
4
+ import {
5
+ GRANT_TYPE_REFRESH_TOKEN,
6
+ MANDATORY_REFRESH_THRESHOLD_IN_SECONDS,
7
+ OAUTH_API_BETA_HEADER,
8
+ TOKEN_ENDPOINT,
9
+ WorkloadIdentityError,
10
+ checkCredentialsFileSafety,
11
+ parseTokenResponse,
12
+ redactSensitive,
13
+ requireSecureTokenEndpoint,
14
+ writeCredentialsFileAtomic,
15
+ } from './types';
16
+ import { nowAsSeconds } from '../../internal/utils/time';
17
+ import { VERSION } from '../../version';
18
+
19
+ export type UserOAuthConfig = {
20
+ credentialsPath: string;
21
+ clientId?: string | undefined;
22
+ baseURL: string;
23
+ fetch: Fetch;
24
+ userAgent?: string | undefined;
25
+ onSafetyWarning?: ((msg: string) => void) | undefined;
26
+ };
27
+
28
+ /**
29
+ * Reads a user-oauth credential file. Returns the cached access token while
30
+ * fresh; on expiry performs a `refresh_token` grant and writes the new
31
+ * tokens back to the credentials file (atomic replace, fsync'd).
32
+ *
33
+ * If `clientId` is empty, the access token is treated as static — the
34
+ * credentials file is read on every call but no refresh is attempted, and
35
+ * an expired token without a `refresh_token` raises.
36
+ */
37
+ export function userOAuthProvider(config: UserOAuthConfig): AccessTokenProvider {
38
+ return async (opts) => {
39
+ const fs = await import('node:fs');
40
+
41
+ await checkCredentialsFileSafety(config.credentialsPath, config.onSafetyWarning);
42
+
43
+ let raw: string;
44
+ try {
45
+ raw = await fs.promises.readFile(config.credentialsPath, 'utf-8');
46
+ } catch (err) {
47
+ throw new WorkloadIdentityError(`Credentials file not found at ${config.credentialsPath}: ${err}`);
48
+ }
49
+ let creds: AnthropicCredentials;
50
+ try {
51
+ creds = JSON.parse(raw);
52
+ } catch (err) {
53
+ throw new WorkloadIdentityError(
54
+ `Credentials file at ${config.credentialsPath} is not valid JSON: ${err}`,
55
+ );
56
+ }
57
+
58
+ const accessToken = creds.access_token;
59
+ if (!accessToken) {
60
+ throw new WorkloadIdentityError(
61
+ `Credentials file at ${config.credentialsPath} must include 'access_token'`,
62
+ );
63
+ }
64
+
65
+ // Return cached token if still fresh (or no expiry info), unless the
66
+ // caller is forcing a refresh after a 401 — then go straight to refresh
67
+ // even if the file's expires_at still looks valid.
68
+ const expiresAt = creds.expires_at;
69
+ if (
70
+ !opts?.forceRefresh &&
71
+ (expiresAt == null || nowAsSeconds() < expiresAt - MANDATORY_REFRESH_THRESHOLD_IN_SECONDS)
72
+ ) {
73
+ return { token: accessToken, expiresAt: expiresAt ?? null };
74
+ }
75
+
76
+ const refreshToken = creds.refresh_token;
77
+ if (!config.clientId || !refreshToken) {
78
+ throw new WorkloadIdentityError(
79
+ `Access token at ${config.credentialsPath} has expired and no refresh is available ` +
80
+ `(client_id ${config.clientId ? 'set' : 'empty'}, refresh_token ${refreshToken ? 'set' : 'empty'})`,
81
+ );
82
+ }
83
+
84
+ requireSecureTokenEndpoint(config.baseURL);
85
+
86
+ const body: Record<string, string> = {
87
+ grant_type: GRANT_TYPE_REFRESH_TOKEN,
88
+ refresh_token: refreshToken,
89
+ client_id: config.clientId,
90
+ };
91
+
92
+ const url = `${config.baseURL}${TOKEN_ENDPOINT}`;
93
+ let resp: Response;
94
+ try {
95
+ resp = await config.fetch(url, {
96
+ method: 'POST',
97
+ headers: {
98
+ 'Content-Type': 'application/json',
99
+ 'anthropic-beta': OAUTH_API_BETA_HEADER,
100
+ 'User-Agent': config.userAgent || `anthropic-sdk-typescript/${VERSION} userOAuthProvider`,
101
+ },
102
+ body: JSON.stringify(body),
103
+ });
104
+ } catch (err) {
105
+ throw new WorkloadIdentityError(`User OAuth refresh failed to reach token endpoint: ${err}`);
106
+ }
107
+
108
+ const requestId = resp.headers.get('Request-Id');
109
+
110
+ if (!resp.ok) {
111
+ const text = await resp.text().catch(() => '');
112
+ throw new WorkloadIdentityError(
113
+ `User OAuth refresh failed (HTTP ${resp.status}): ${redactSensitive(text)}`,
114
+ resp.status,
115
+ redactSensitive(text),
116
+ requestId,
117
+ );
118
+ }
119
+
120
+ const data = await parseTokenResponse(resp, requestId);
121
+ const expiresIn = Number(data.expires_in);
122
+ if (!Number.isFinite(expiresIn)) {
123
+ throw new WorkloadIdentityError(
124
+ `User OAuth refresh response missing or invalid expires_in: ${JSON.stringify(redactSensitive(data))}`,
125
+ resp.status,
126
+ redactSensitive(data),
127
+ requestId,
128
+ );
129
+ }
130
+ const newExpiresAt = nowAsSeconds() + expiresIn;
131
+ const newRefreshToken = data.refresh_token || refreshToken;
132
+
133
+ await writeCredentialsFileAtomic(config.credentialsPath, {
134
+ ...creds,
135
+ version: CREDENTIALS_FILE_VERSION,
136
+ type: 'oauth_token',
137
+ access_token: data.access_token,
138
+ expires_at: newExpiresAt,
139
+ refresh_token: newRefreshToken,
140
+ });
141
+
142
+ return { token: data.access_token, expiresAt: newExpiresAt };
143
+ };
144
+ }
@@ -0,0 +1,3 @@
1
+ export { type AccessToken, type AccessTokenProvider } from './credentials/types';
2
+ export { type AnthropicConfig, type AuthenticationInfo, loadConfig } from '../core/credentials';
3
+ export { resolveCredentialsFromConfig } from './credentials/credential-chain';
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const VERSION = '0.92.0'; // x-release-please-version
1
+ export const VERSION = '0.94.0'; // x-release-please-version
package/version.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const VERSION = "0.92.0";
1
+ export declare const VERSION = "0.94.0";
2
2
  //# sourceMappingURL=version.d.mts.map
package/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const VERSION = "0.92.0";
1
+ export declare const VERSION = "0.94.0";
2
2
  //# sourceMappingURL=version.d.ts.map
package/version.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
- exports.VERSION = '0.92.0'; // x-release-please-version
4
+ exports.VERSION = '0.94.0'; // x-release-please-version
5
5
  //# sourceMappingURL=version.js.map
package/version.mjs CHANGED
@@ -1,2 +1,2 @@
1
- export const VERSION = '0.92.0'; // x-release-please-version
1
+ export const VERSION = '0.94.0'; // x-release-please-version
2
2
  //# sourceMappingURL=version.mjs.map