@cortexkit/anthropic-auth-core 1.22.0 → 2.0.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.
- package/README.md +2 -0
- package/dist/accounts.d.ts +52 -8
- package/dist/cachekeep.d.ts +27 -1
- package/dist/cch.d.ts +11 -1
- package/dist/claude-code.d.ts +23 -1
- package/dist/claustrum-enrollment.d.ts +72 -0
- package/dist/claustrum-scoped-roster.d.ts +27 -0
- package/dist/claustrum-scoped-runtime.d.ts +31 -0
- package/dist/claustrum-scoped.d.ts +53 -0
- package/dist/claustrum.d.ts +16 -82
- package/dist/commands/account.d.ts +38 -11
- package/dist/constants.d.ts +3 -2
- package/dist/custom-headers.d.ts +3 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +29700 -31
- package/dist/internal-types/LICENSE +21 -0
- package/dist/internal-types/commands/builtins.d.ts +71 -0
- package/dist/internal-types/commands/index.d.ts +10 -0
- package/dist/internal-types/commands/menu.d.ts +61 -0
- package/dist/internal-types/commands/model.d.ts +238 -0
- package/dist/internal-types/commands/pi.d.ts +23 -0
- package/dist/internal-types/commands/seam.d.ts +72 -0
- package/dist/internal-types/fs/refresh-file-lock.d.ts +33 -0
- package/dist/internal-types/logger/capture-sink.d.ts +14 -0
- package/dist/internal-types/logger/engine.d.ts +100 -0
- package/dist/internal-types/logger/index.d.ts +6 -0
- package/dist/internal-types/logger/redact.d.ts +11 -0
- package/dist/internal-types/pi-slot/index.d.ts +3 -0
- package/dist/internal-types/pi-slot/slot.d.ts +49 -0
- package/dist/internal-types/quota/codec.d.ts +11 -0
- package/dist/internal-types/quota/format.d.ts +46 -0
- package/dist/internal-types/quota/index.d.ts +10 -0
- package/dist/internal-types/quota/map.d.ts +63 -0
- package/dist/internal-types/quota/merge.d.ts +48 -0
- package/dist/internal-types/quota/projection.d.ts +64 -0
- package/dist/internal-types/store/attribution.d.ts +28 -0
- package/dist/internal-types/store/errors.d.ts +55 -0
- package/dist/internal-types/store/hooks.d.ts +12 -0
- package/dist/internal-types/store/identity.d.ts +39 -0
- package/dist/internal-types/store/index.d.ts +21 -0
- package/dist/internal-types/store/mutate.d.ts +189 -0
- package/dist/internal-types/store/pool.d.ts +196 -0
- package/dist/internal-types/store/provider-state.d.ts +131 -0
- package/dist/internal-types/store/publication.d.ts +47 -0
- package/dist/internal-types/store/pull.d.ts +36 -0
- package/dist/internal-types/store/refresh-lock.d.ts +63 -0
- package/dist/internal-types/store/refresh.d.ts +72 -0
- package/dist/internal-types/store/rows.d.ts +313 -0
- package/dist/internal-types/store/runtime.d.ts +36 -0
- package/dist/internal-types/store/schema.d.ts +443 -0
- package/dist/internal-types/store/settings.d.ts +63 -0
- package/dist/internal-types/store/torn.d.ts +78 -0
- package/dist/migration-source.d.ts +33 -0
- package/dist/model-remap.d.ts +27 -0
- package/dist/models.d.ts +60 -0
- package/dist/native-account-runtime.d.ts +121 -0
- package/dist/native-account-view.d.ts +126 -0
- package/dist/native-command-effects.d.ts +59 -0
- package/dist/native-credential-validation.d.ts +42 -0
- package/dist/native-custody-activation.d.ts +45 -0
- package/dist/native-custody.d.ts +114 -0
- package/dist/native-enrollment-authority.d.ts +2 -0
- package/dist/native-host-auth-read.d.ts +3 -0
- package/dist/native-host-auth-write.d.ts +51 -0
- package/dist/native-host-auth.d.ts +12 -0
- package/dist/native-local-credential-service.d.ts +76 -0
- package/dist/native-local-runtime-readers.d.ts +22 -0
- package/dist/native-lock-observer.d.ts +12 -0
- package/dist/native-menu-executor.d.ts +243 -0
- package/dist/native-menu-model.d.ts +142 -0
- package/dist/native-migration-proof.d.ts +28 -0
- package/dist/native-migration-source.d.ts +47 -0
- package/dist/native-migration.d.ts +66 -0
- package/dist/native-quota-codec.d.ts +32 -0
- package/dist/native-refresh-coordinator.d.ts +240 -0
- package/dist/native-roster-seed.d.ts +115 -0
- package/dist/native-runtime-registry.d.ts +19 -0
- package/dist/native-runtime.d.ts +204 -0
- package/dist/native-transient-account.d.ts +38 -0
- package/dist/native-ui.d.ts +45 -0
- package/dist/native-usage-publication.d.ts +11 -0
- package/dist/native-vault-admission.d.ts +8 -0
- package/dist/native-vault-connection.d.ts +33 -0
- package/dist/native-vault-paths.d.ts +13 -0
- package/dist/native-vault-runtime.d.ts +102 -0
- package/dist/native-vault-seed.d.ts +16 -0
- package/dist/oauth-profile.d.ts +2 -1
- package/dist/pool-authority.d.ts +159 -0
- package/dist/pool-binding.d.ts +32 -0
- package/dist/pool-paths.d.ts +21 -0
- package/dist/pool-store.d.ts +32 -0
- package/dist/prime.d.ts +11 -3
- package/dist/provider-error.d.ts +139 -0
- package/dist/quota-header-feed.d.ts +13 -0
- package/dist/quota-manager.d.ts +30 -5
- package/dist/relay.d.ts +22 -1
- package/dist/request-history.d.ts +34 -0
- package/dist/sse.d.ts +81 -0
- package/dist/sticky-routing.d.ts +29 -0
- package/package.json +9 -3
- package/dist/accounts.js +0 -3330
- package/dist/auth.js +0 -182
- package/dist/cache1h.js +0 -97
- package/dist/cachekeep-registry.js +0 -124
- package/dist/cachekeep.js +0 -578
- package/dist/cch.js +0 -127
- package/dist/claude-code.js +0 -388
- package/dist/claustrum.js +0 -529
- package/dist/commands/account.js +0 -239
- package/dist/constants.js +0 -82
- package/dist/dump.js +0 -616
- package/dist/fast.js +0 -66
- package/dist/json.js +0 -10
- package/dist/killswitch.js +0 -132
- package/dist/logger.js +0 -286
- package/dist/logging.js +0 -59
- package/dist/mid-conversation-output-config.js +0 -89
- package/dist/models.js +0 -141
- package/dist/network-errors.js +0 -27
- package/dist/oauth-profile.js +0 -54
- package/dist/pkce.js +0 -17
- package/dist/prime.js +0 -823
- package/dist/provider.js +0 -0
- package/dist/quota-header-feed.js +0 -279
- package/dist/quota-headers.js +0 -121
- package/dist/quota-manager.js +0 -805
- package/dist/quotas.js +0 -141
- package/dist/relay.js +0 -1243
- package/dist/routing.js +0 -94
- package/dist/start.js +0 -29
- package/dist/sticky-routing.js +0 -523
- package/dist/thinking-binding.js +0 -54
- package/dist/token-fingerprint.js +0 -8
package/dist/claude-code.js
DELETED
|
@@ -1,388 +0,0 @@
|
|
|
1
|
-
import { randomBytes, randomUUID } from 'node:crypto';
|
|
2
|
-
import { CLAUDE_CODE_ENTRYPOINT, CLAUDE_CODE_STAINLESS_PACKAGE_VERSION, CLAUDE_CODE_STAINLESS_RUNTIME_VERSION, CLAUDE_CODE_VERSION, FAST_MODE_BETA, USER_AGENT, } from './constants.js';
|
|
3
|
-
const IDENTITY_CACHE_LIMIT = 1_000;
|
|
4
|
-
const identityCache = new Map();
|
|
5
|
-
export function setBounded(map, key, value, limit = IDENTITY_CACHE_LIMIT) {
|
|
6
|
-
if (!map.has(key) && map.size >= limit) {
|
|
7
|
-
const oldest = map.keys().next().value;
|
|
8
|
-
if (oldest !== undefined)
|
|
9
|
-
map.delete(oldest);
|
|
10
|
-
}
|
|
11
|
-
map.set(key, value);
|
|
12
|
-
}
|
|
13
|
-
function clearCachedAccountUuid(key, identity) {
|
|
14
|
-
if (!identity.accountUuid)
|
|
15
|
-
return identity;
|
|
16
|
-
if (identityCache.get(key) !== identity) {
|
|
17
|
-
return { ...identity, accountUuid: undefined };
|
|
18
|
-
}
|
|
19
|
-
const cleared = { ...identity, accountUuid: undefined };
|
|
20
|
-
setBounded(identityCache, key, cleared);
|
|
21
|
-
return cleared;
|
|
22
|
-
}
|
|
23
|
-
export function getClaudeCodeIdentity(seed) {
|
|
24
|
-
const cacheKey = seed || 'anonymous';
|
|
25
|
-
const cached = identityCache.get(cacheKey);
|
|
26
|
-
if (cached)
|
|
27
|
-
return cached;
|
|
28
|
-
const identity = {
|
|
29
|
-
deviceId: randomBytes(32).toString('hex'),
|
|
30
|
-
sessionId: randomUUID(),
|
|
31
|
-
};
|
|
32
|
-
setBounded(identityCache, cacheKey, identity);
|
|
33
|
-
return identity;
|
|
34
|
-
}
|
|
35
|
-
const BOOTSTRAP_IDENTITY_CACHE_TTL_MS = 24 * 60 * 60_000;
|
|
36
|
-
const BOOTSTRAP_IDENTITY_NEGATIVE_TTL_MS = 5 * 60_000;
|
|
37
|
-
const bootstrapFetches = new Map();
|
|
38
|
-
const bootstrapResults = new Map();
|
|
39
|
-
export function resetClaudeCodeIdentityCachesForTest() {
|
|
40
|
-
identityCache.clear();
|
|
41
|
-
bootstrapFetches.clear();
|
|
42
|
-
bootstrapResults.clear();
|
|
43
|
-
}
|
|
44
|
-
function compatibilityCacheKey(accessToken) {
|
|
45
|
-
return `compat:${accessToken || 'anonymous'}`;
|
|
46
|
-
}
|
|
47
|
-
function explicitCacheKey(accountIdentity) {
|
|
48
|
-
return `identity:${accountIdentity}`;
|
|
49
|
-
}
|
|
50
|
-
function accountCacheKey(accountUuid, accountIdentity) {
|
|
51
|
-
return `account:${accountUuid}:${accountIdentity}`;
|
|
52
|
-
}
|
|
53
|
-
function adoptCachedIdentity(cached, accountIdentity, accountUuid) {
|
|
54
|
-
if (!cached)
|
|
55
|
-
return undefined;
|
|
56
|
-
if (cached.accountIdentity !== undefined &&
|
|
57
|
-
cached.accountIdentity !== accountIdentity) {
|
|
58
|
-
return undefined;
|
|
59
|
-
}
|
|
60
|
-
if (accountUuid !== undefined &&
|
|
61
|
-
cached.accountUuid !== undefined &&
|
|
62
|
-
cached.accountUuid !== accountUuid) {
|
|
63
|
-
return undefined;
|
|
64
|
-
}
|
|
65
|
-
if (cached.accountIdentity === accountIdentity &&
|
|
66
|
-
(accountUuid === undefined || cached.accountUuid === accountUuid)) {
|
|
67
|
-
return cached;
|
|
68
|
-
}
|
|
69
|
-
return {
|
|
70
|
-
...cached,
|
|
71
|
-
accountIdentity,
|
|
72
|
-
...(accountUuid !== undefined && { accountUuid }),
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
function cacheAccountIdentity(identity, accountIdentity, accountUuid) {
|
|
76
|
-
setBounded(identityCache, explicitCacheKey(accountIdentity), identity);
|
|
77
|
-
setBounded(identityCache, accountCacheKey(accountUuid, accountIdentity), identity);
|
|
78
|
-
}
|
|
79
|
-
async function fetchClaudeCodeAccountUuid(accessToken, model) {
|
|
80
|
-
if (!accessToken.startsWith('sk-ant-oat'))
|
|
81
|
-
return null;
|
|
82
|
-
const controller = new AbortController();
|
|
83
|
-
const timeout = setTimeout(() => controller.abort(), 5_000);
|
|
84
|
-
try {
|
|
85
|
-
const url = new URL('https://api.anthropic.com/api/claude_cli/bootstrap');
|
|
86
|
-
url.searchParams.set('entrypoint', CLAUDE_CODE_ENTRYPOINT);
|
|
87
|
-
if (model)
|
|
88
|
-
url.searchParams.set('model', model);
|
|
89
|
-
const response = await fetch(url, {
|
|
90
|
-
method: 'GET',
|
|
91
|
-
headers: {
|
|
92
|
-
accept: 'application/json, text/plain, */*',
|
|
93
|
-
authorization: `Bearer ${accessToken}`,
|
|
94
|
-
'content-type': 'application/json',
|
|
95
|
-
'anthropic-beta': 'oauth-2025-04-20',
|
|
96
|
-
'user-agent': `claude-code/${CLAUDE_CODE_VERSION}`,
|
|
97
|
-
},
|
|
98
|
-
signal: controller.signal,
|
|
99
|
-
});
|
|
100
|
-
if (!response.ok)
|
|
101
|
-
return null;
|
|
102
|
-
const data = (await response.json().catch(() => null));
|
|
103
|
-
const accountUuid = data?.oauth_account?.account_uuid;
|
|
104
|
-
return typeof accountUuid === 'string' && accountUuid ? accountUuid : null;
|
|
105
|
-
}
|
|
106
|
-
catch {
|
|
107
|
-
return null;
|
|
108
|
-
}
|
|
109
|
-
finally {
|
|
110
|
-
clearTimeout(timeout);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
export async function resolveClaudeCodeIdentity(accessToken, model, accountIdentity) {
|
|
114
|
-
const stableAccountIdentity = accountIdentity?.trim() || undefined;
|
|
115
|
-
const cacheKey = stableAccountIdentity
|
|
116
|
-
? explicitCacheKey(stableAccountIdentity)
|
|
117
|
-
: compatibilityCacheKey(accessToken);
|
|
118
|
-
let identity;
|
|
119
|
-
if (stableAccountIdentity) {
|
|
120
|
-
const cachedIdentity = adoptCachedIdentity(identityCache.get(cacheKey) ?? identityCache.get(accessToken), stableAccountIdentity);
|
|
121
|
-
identity = cachedIdentity ?? getClaudeCodeIdentity(cacheKey);
|
|
122
|
-
identity = adoptCachedIdentity(identity, stableAccountIdentity) ?? identity;
|
|
123
|
-
setBounded(identityCache, cacheKey, identity);
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
identity =
|
|
127
|
-
identityCache.get(cacheKey) ??
|
|
128
|
-
identityCache.get(accessToken) ??
|
|
129
|
-
getClaudeCodeIdentity(cacheKey);
|
|
130
|
-
setBounded(identityCache, cacheKey, identity);
|
|
131
|
-
setBounded(identityCache, accessToken, identity);
|
|
132
|
-
}
|
|
133
|
-
if (!accessToken.startsWith('sk-ant-oat'))
|
|
134
|
-
return identity;
|
|
135
|
-
const now = Date.now();
|
|
136
|
-
// A slot-stable identity survives account replacement; bootstrap is the
|
|
137
|
-
// account lookup that must be repeated for each credential presented to it.
|
|
138
|
-
const bootstrapKey = `${cacheKey}:${accessToken}`;
|
|
139
|
-
const cachedResult = bootstrapResults.get(bootstrapKey);
|
|
140
|
-
if (cachedResult && cachedResult.expiresAt > now) {
|
|
141
|
-
if (!cachedResult.accountUuid) {
|
|
142
|
-
identity = clearCachedAccountUuid(cacheKey, identity);
|
|
143
|
-
return identity;
|
|
144
|
-
}
|
|
145
|
-
if (!stableAccountIdentity) {
|
|
146
|
-
if (identity.accountUuid === cachedResult.accountUuid)
|
|
147
|
-
return identity;
|
|
148
|
-
identity = { ...identity, accountUuid: cachedResult.accountUuid };
|
|
149
|
-
setBounded(identityCache, bootstrapKey, identity);
|
|
150
|
-
return identity;
|
|
151
|
-
}
|
|
152
|
-
const cachedAccountIdentity = adoptCachedIdentity(identityCache.get(accountCacheKey(cachedResult.accountUuid, stableAccountIdentity)) ?? identityCache.get(`account:${cachedResult.accountUuid}`), stableAccountIdentity, cachedResult.accountUuid);
|
|
153
|
-
if (cachedAccountIdentity) {
|
|
154
|
-
identity = cachedAccountIdentity;
|
|
155
|
-
cacheAccountIdentity(identity, stableAccountIdentity, cachedResult.accountUuid);
|
|
156
|
-
return identity;
|
|
157
|
-
}
|
|
158
|
-
if (identity.accountUuid === cachedResult.accountUuid)
|
|
159
|
-
return identity;
|
|
160
|
-
identity = {
|
|
161
|
-
...identity,
|
|
162
|
-
accountIdentity: stableAccountIdentity,
|
|
163
|
-
accountUuid: cachedResult.accountUuid,
|
|
164
|
-
};
|
|
165
|
-
cacheAccountIdentity(identity, stableAccountIdentity, cachedResult.accountUuid);
|
|
166
|
-
return identity;
|
|
167
|
-
}
|
|
168
|
-
let fetchPromise = bootstrapFetches.get(bootstrapKey);
|
|
169
|
-
if (!fetchPromise) {
|
|
170
|
-
fetchPromise = fetchClaudeCodeAccountUuid(accessToken, model);
|
|
171
|
-
setBounded(bootstrapFetches, bootstrapKey, fetchPromise);
|
|
172
|
-
}
|
|
173
|
-
const accountUuid = await fetchPromise.finally(() => {
|
|
174
|
-
if (bootstrapFetches.get(bootstrapKey) === fetchPromise) {
|
|
175
|
-
bootstrapFetches.delete(bootstrapKey);
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
setBounded(bootstrapResults, bootstrapKey, {
|
|
179
|
-
accountUuid,
|
|
180
|
-
expiresAt: now +
|
|
181
|
-
(accountUuid
|
|
182
|
-
? BOOTSTRAP_IDENTITY_CACHE_TTL_MS
|
|
183
|
-
: BOOTSTRAP_IDENTITY_NEGATIVE_TTL_MS),
|
|
184
|
-
});
|
|
185
|
-
if (!accountUuid) {
|
|
186
|
-
identity = clearCachedAccountUuid(cacheKey, identity);
|
|
187
|
-
return identity;
|
|
188
|
-
}
|
|
189
|
-
if (!stableAccountIdentity) {
|
|
190
|
-
identity = { ...identity, accountUuid };
|
|
191
|
-
setBounded(identityCache, bootstrapKey, identity);
|
|
192
|
-
return identity;
|
|
193
|
-
}
|
|
194
|
-
const cachedAccountIdentity = adoptCachedIdentity(identityCache.get(accountCacheKey(accountUuid, stableAccountIdentity)) ??
|
|
195
|
-
identityCache.get(`account:${accountUuid}`), stableAccountIdentity, accountUuid);
|
|
196
|
-
if (cachedAccountIdentity) {
|
|
197
|
-
identity = cachedAccountIdentity;
|
|
198
|
-
cacheAccountIdentity(identity, stableAccountIdentity, accountUuid);
|
|
199
|
-
return identity;
|
|
200
|
-
}
|
|
201
|
-
identity = {
|
|
202
|
-
...identity,
|
|
203
|
-
accountIdentity: stableAccountIdentity,
|
|
204
|
-
accountUuid,
|
|
205
|
-
};
|
|
206
|
-
cacheAccountIdentity(identity, stableAccountIdentity, accountUuid);
|
|
207
|
-
return identity;
|
|
208
|
-
}
|
|
209
|
-
function isRecord(value) {
|
|
210
|
-
return value != null && typeof value === 'object' && !Array.isArray(value);
|
|
211
|
-
}
|
|
212
|
-
export function buildClaudeCodeMetadataUserId(identity) {
|
|
213
|
-
if (!identity.accountUuid)
|
|
214
|
-
return null;
|
|
215
|
-
return JSON.stringify({
|
|
216
|
-
device_id: identity.deviceId,
|
|
217
|
-
account_uuid: identity.accountUuid,
|
|
218
|
-
session_id: identity.sessionId,
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
export function applyClaudeCodeMetadata(body, identity) {
|
|
222
|
-
const userId = buildClaudeCodeMetadataUserId(identity);
|
|
223
|
-
if (!userId) {
|
|
224
|
-
if (isRecord(body.metadata))
|
|
225
|
-
delete body.metadata.user_id;
|
|
226
|
-
return false;
|
|
227
|
-
}
|
|
228
|
-
if (!isRecord(body.metadata))
|
|
229
|
-
body.metadata = {};
|
|
230
|
-
const metadata = body.metadata;
|
|
231
|
-
if (isRecord(metadata))
|
|
232
|
-
metadata.user_id = userId;
|
|
233
|
-
return true;
|
|
234
|
-
}
|
|
235
|
-
// Claude Code 2.1.258 sends redact-thinking-2026-02-12, but it suppresses
|
|
236
|
-
// thinking-block content that OpenCode displays; deliberately omitted after A/B proof on 2026-09-01.
|
|
237
|
-
export const CLAUDE_CODE_FULL_AGENT_BETAS = [
|
|
238
|
-
'oauth-2025-04-20',
|
|
239
|
-
'interleaved-thinking-2025-05-14',
|
|
240
|
-
'thinking-token-count-2026-05-13',
|
|
241
|
-
'context-management-2025-06-27',
|
|
242
|
-
'prompt-caching-scope-2026-01-05',
|
|
243
|
-
'claude-code-20250219',
|
|
244
|
-
'advisor-tool-2026-03-01',
|
|
245
|
-
'advanced-tool-use-2025-11-20',
|
|
246
|
-
'mid-conversation-system-2026-04-07',
|
|
247
|
-
'effort-2025-11-24',
|
|
248
|
-
'fallback-credit-2026-06-01',
|
|
249
|
-
'extended-cache-ttl-2025-04-11',
|
|
250
|
-
'cache-diagnosis-2026-04-07',
|
|
251
|
-
];
|
|
252
|
-
const CLAUDE_CODE_STRUCTURED_OUTPUT_BETAS = [
|
|
253
|
-
'oauth-2025-04-20',
|
|
254
|
-
'interleaved-thinking-2025-05-14',
|
|
255
|
-
'thinking-token-count-2026-05-13',
|
|
256
|
-
'context-management-2025-06-27',
|
|
257
|
-
'prompt-caching-scope-2026-01-05',
|
|
258
|
-
'advisor-tool-2026-03-01',
|
|
259
|
-
'structured-outputs-2025-12-15',
|
|
260
|
-
'cache-diagnosis-2026-04-07',
|
|
261
|
-
];
|
|
262
|
-
const CLAUDE_CODE_BASE_BETAS = [
|
|
263
|
-
'oauth-2025-04-20',
|
|
264
|
-
'interleaved-thinking-2025-05-14',
|
|
265
|
-
'thinking-token-count-2026-05-13',
|
|
266
|
-
'context-management-2025-06-27',
|
|
267
|
-
'prompt-caching-scope-2026-01-05',
|
|
268
|
-
'advisor-tool-2026-03-01',
|
|
269
|
-
'advanced-tool-use-2025-11-20',
|
|
270
|
-
'extended-cache-ttl-2025-04-11',
|
|
271
|
-
'cache-diagnosis-2026-04-07',
|
|
272
|
-
];
|
|
273
|
-
function hasStructuredOutput(body) {
|
|
274
|
-
const outputConfig = body.output_config;
|
|
275
|
-
const format = outputConfig?.format;
|
|
276
|
-
return format?.type === 'json_schema';
|
|
277
|
-
}
|
|
278
|
-
function hasFullAgentShape(body) {
|
|
279
|
-
return (Array.isArray(body.tools) &&
|
|
280
|
-
body.tools.length > 0 &&
|
|
281
|
-
Array.isArray(body.system) &&
|
|
282
|
-
isRecord(body.thinking) &&
|
|
283
|
-
isRecord(body.context_management) &&
|
|
284
|
-
isRecord(body.output_config) &&
|
|
285
|
-
isRecord(body.diagnostics));
|
|
286
|
-
}
|
|
287
|
-
export function selectClaudeCodeBetas(body, extraBetas = []) {
|
|
288
|
-
const selected = body
|
|
289
|
-
? hasFullAgentShape(body)
|
|
290
|
-
? [...CLAUDE_CODE_FULL_AGENT_BETAS]
|
|
291
|
-
: hasStructuredOutput(body)
|
|
292
|
-
? [...CLAUDE_CODE_STRUCTURED_OUTPUT_BETAS]
|
|
293
|
-
: [...CLAUDE_CODE_BASE_BETAS]
|
|
294
|
-
: [...CLAUDE_CODE_BASE_BETAS];
|
|
295
|
-
if (body?.speed === 'fast')
|
|
296
|
-
selected.push(FAST_MODE_BETA);
|
|
297
|
-
for (const beta of extraBetas) {
|
|
298
|
-
const trimmed = beta.trim();
|
|
299
|
-
if (trimmed)
|
|
300
|
-
selected.push(trimmed);
|
|
301
|
-
}
|
|
302
|
-
return [...new Set(selected)].join(',');
|
|
303
|
-
}
|
|
304
|
-
function stainlessOS() {
|
|
305
|
-
switch (process.platform) {
|
|
306
|
-
case 'darwin':
|
|
307
|
-
return 'MacOS';
|
|
308
|
-
case 'win32':
|
|
309
|
-
return 'Windows';
|
|
310
|
-
case 'linux':
|
|
311
|
-
return 'Linux';
|
|
312
|
-
case 'freebsd':
|
|
313
|
-
return 'FreeBSD';
|
|
314
|
-
default:
|
|
315
|
-
return 'Unknown';
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
function stainlessArch() {
|
|
319
|
-
switch (process.arch) {
|
|
320
|
-
case 'arm64':
|
|
321
|
-
return 'arm64';
|
|
322
|
-
case 'x64':
|
|
323
|
-
return 'x64';
|
|
324
|
-
case 'ia32':
|
|
325
|
-
return 'x32';
|
|
326
|
-
default:
|
|
327
|
-
return process.arch;
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
export function applyClaudeCodeHeaders(headers, accessToken, options = {}) {
|
|
331
|
-
const identity = options.identity ?? getClaudeCodeIdentity(accessToken);
|
|
332
|
-
const incomingBetas = (headers.get('anthropic-beta') ?? '')
|
|
333
|
-
.split(',')
|
|
334
|
-
.map((beta) => beta.trim())
|
|
335
|
-
.filter(Boolean);
|
|
336
|
-
const extraBetas = [...incomingBetas, ...(options.extraBetas ?? [])];
|
|
337
|
-
headers.set('accept', 'application/json');
|
|
338
|
-
headers.set('authorization', `Bearer ${accessToken}`);
|
|
339
|
-
headers.set('content-type', 'application/json');
|
|
340
|
-
headers.set('user-agent', USER_AGENT);
|
|
341
|
-
headers.set('anthropic-beta', selectClaudeCodeBetas(options.body, extraBetas));
|
|
342
|
-
headers.set('anthropic-dangerous-direct-browser-access', 'true');
|
|
343
|
-
headers.set('anthropic-version', '2023-06-01');
|
|
344
|
-
headers.set('x-app', 'cli');
|
|
345
|
-
headers.set('x-client-request-id', randomUUID());
|
|
346
|
-
headers.set('x-claude-code-session-id', identity.sessionId);
|
|
347
|
-
headers.set('x-stainless-arch', stainlessArch());
|
|
348
|
-
headers.set('x-stainless-lang', 'js');
|
|
349
|
-
headers.set('x-stainless-os', stainlessOS());
|
|
350
|
-
headers.set('x-stainless-package-version', CLAUDE_CODE_STAINLESS_PACKAGE_VERSION);
|
|
351
|
-
headers.set('x-stainless-retry-count', '0');
|
|
352
|
-
headers.set('x-stainless-runtime', 'node');
|
|
353
|
-
headers.set('x-stainless-runtime-version', CLAUDE_CODE_STAINLESS_RUNTIME_VERSION);
|
|
354
|
-
headers.set('x-stainless-timeout', '600');
|
|
355
|
-
headers.delete('x-api-key');
|
|
356
|
-
return headers;
|
|
357
|
-
}
|
|
358
|
-
const BODY_FIELD_ORDER = [
|
|
359
|
-
'model',
|
|
360
|
-
'messages',
|
|
361
|
-
'system',
|
|
362
|
-
'tools',
|
|
363
|
-
'tool_choice',
|
|
364
|
-
'metadata',
|
|
365
|
-
'max_tokens',
|
|
366
|
-
'temperature',
|
|
367
|
-
'thinking',
|
|
368
|
-
'context_management',
|
|
369
|
-
'output_config',
|
|
370
|
-
'diagnostics',
|
|
371
|
-
'stream',
|
|
372
|
-
'speed',
|
|
373
|
-
];
|
|
374
|
-
export function orderClaudeCodeBody(body) {
|
|
375
|
-
const ordered = {};
|
|
376
|
-
for (const key of BODY_FIELD_ORDER) {
|
|
377
|
-
if (Object.hasOwn(body, key))
|
|
378
|
-
ordered[key] = body[key];
|
|
379
|
-
}
|
|
380
|
-
for (const [key, value] of Object.entries(body)) {
|
|
381
|
-
if (!Object.hasOwn(ordered, key))
|
|
382
|
-
ordered[key] = value;
|
|
383
|
-
}
|
|
384
|
-
return ordered;
|
|
385
|
-
}
|
|
386
|
-
export function claudeCodeEntrypoint() {
|
|
387
|
-
return CLAUDE_CODE_ENTRYPOINT;
|
|
388
|
-
}
|