@cortexkit/anthropic-auth-core 1.21.0 → 1.22.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/dist/auth.js CHANGED
@@ -1,4 +1,6 @@
1
+ import { assertNotCustodyTombstone } from './claustrum.js';
1
2
  import { AUTHORIZE_URLS, AXIOS_USER_AGENT, CLIENT_ID, CODE_CALLBACK_URL, OAUTH_SCOPES, REFRESH_SCOPE, TOKEN_URL, } from './constants.js';
3
+ import { isTransientNetworkError } from './network-errors.js';
2
4
  import { generatePKCE } from './pkce.js';
3
5
  export function parseRetryAfterHeader(value) {
4
6
  if (!value)
@@ -33,17 +35,8 @@ export class ClaudeOAuthRefreshError extends Error {
33
35
  this.retryAfter = parseRetryAfterHeader(retryAfterHeader ?? undefined);
34
36
  }
35
37
  }
36
- function isTransientNetworkError(error) {
37
- if (!(error instanceof Error))
38
- return false;
39
- const code = error.code;
40
- return (error.message.includes('fetch failed') ||
41
- code === 'ECONNRESET' ||
42
- code === 'ECONNREFUSED' ||
43
- code === 'ETIMEDOUT' ||
44
- code === 'UND_ERR_CONNECT_TIMEOUT');
45
- }
46
38
  export async function refreshClaudeOAuthToken(input) {
39
+ assertNotCustodyTombstone(input.refreshToken, 'anthropic');
47
40
  const fetchImpl = input.fetchImpl ?? fetch;
48
41
  const maxRetries = input.maxRetries ?? 2;
49
42
  const baseDelayMs = input.baseDelayMs ?? 500;
package/dist/cch.d.ts CHANGED
@@ -7,10 +7,18 @@ type Message = {
7
7
  };
8
8
  export declare const CCH_PATTERN: RegExp;
9
9
  /**
10
- * Extract text from the first user message's first text block.
11
- * Kept for diagnostics/backward-compatible tests; CCH signing no longer uses it.
10
+ * Extract the visible prompt from the first user message. Claude Code prepends
11
+ * reminder blocks and derives its version suffix from the final text block.
12
12
  */
13
13
  export declare function extractFirstUserMessageText(messages: Message[]): string;
14
+ export declare class ClaudeCodeFirstUserTextTracker {
15
+ #private;
16
+ readonly limit: number;
17
+ constructor(limit?: number);
18
+ resolve(sessionId: string, messages: Message[], pin?: boolean): string;
19
+ has(sessionId: string): boolean;
20
+ clear(): void;
21
+ }
14
22
  /**
15
23
  * Compute Claude Code's cch token over the final serialized request body.
16
24
  *
@@ -23,16 +31,12 @@ export declare function resetBillingHeaderCCH(bodyString: string): string;
23
31
  export declare function extractBillingHeaderCCH(bodyString: string): string | null;
24
32
  export declare function signRequestBody(bodyString: string): Promise<string>;
25
33
  /**
26
- * Compute a stable 3-character suffix for cc_version.
27
- *
28
- * The captured Claude Code version uses its observed build hash. Custom/future
29
- * versions get a deterministic version-only suffix so date changes never rotate
30
- * the billing header and bust prompt-cache prefixes.
34
+ * Compute Claude Code's 3-character suffix for cc_version.
31
35
  */
32
- export declare function computeVersionSuffix(version?: string, _date?: Date): string;
36
+ export declare function computeCcVersionSuffix(firstUserText: string, version?: string): string;
33
37
  /**
34
38
  * Build the billing header with a cch placeholder.
35
39
  * signRequestBody() must run after final request serialization to replace it.
36
40
  */
37
- export declare function buildBillingHeaderValue(_messages: Message[], version: string | undefined, entrypoint: string, date?: Date): string;
41
+ export declare function buildBillingHeaderValue(messages: Message[], version: string | undefined, entrypoint: string, pinnedFirstUserText?: string): string;
38
42
  export {};
package/dist/cch.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { createHash } from 'node:crypto';
2
2
  import xxhashInit from 'xxhash-wasm';
3
- import { CLAUDE_CODE_BUILD_HASH, CLAUDE_CODE_VERSION } from './constants.js';
4
- const CCH_SEED = 0x6e52736ac806831en;
3
+ import { CCH_POSITIONS, CCH_SALT, CLAUDE_CODE_VERSION } from './constants.js';
4
+ const CCH_SEED = 0x4d659218e32a3268n;
5
5
  const CCH_PLACEHOLDER = 'cch=00000;';
6
6
  export const CCH_PATTERN = /\bcch=([0-9a-f]{5});/;
7
7
  const BILLING_HEADER_CCH_PATTERN = /("system":\[\{"type":"text","text":"x-anthropic-billing-header: cc_version=[^;"]+; cc_entrypoint=[^;"]+; )cch=([0-9a-f]{5});/;
@@ -18,8 +18,8 @@ async function ensureXxhash() {
18
18
  await xxhashPromise;
19
19
  }
20
20
  /**
21
- * Extract text from the first user message's first text block.
22
- * Kept for diagnostics/backward-compatible tests; CCH signing no longer uses it.
21
+ * Extract the visible prompt from the first user message. Claude Code prepends
22
+ * reminder blocks and derives its version suffix from the final text block.
23
23
  */
24
24
  export function extractFirstUserMessageText(messages) {
25
25
  const userMsg = messages.find((message) => message.role === 'user');
@@ -29,12 +29,48 @@ export function extractFirstUserMessageText(messages) {
29
29
  if (typeof content === 'string')
30
30
  return content;
31
31
  if (Array.isArray(content)) {
32
- const textBlock = content.find((block) => block.type === 'text');
33
- if (textBlock?.text)
34
- return textBlock.text;
32
+ const textBlocks = content.filter((block) => block.type === 'text');
33
+ const commandBlock = textBlocks.find((block) => block.text?.includes('<command-name>'));
34
+ if (commandBlock?.text) {
35
+ return commandBlock.text.slice(commandBlock.text.indexOf('<command-name>'));
36
+ }
37
+ const visibleBlock = textBlocks.findLast((block) => typeof block.text === 'string' && block.text.length > 0);
38
+ if (visibleBlock?.text)
39
+ return visibleBlock.text;
35
40
  }
36
41
  return '';
37
42
  }
43
+ export class ClaudeCodeFirstUserTextTracker {
44
+ limit;
45
+ #values = new Map();
46
+ constructor(limit = 1_000) {
47
+ this.limit = limit;
48
+ }
49
+ resolve(sessionId, messages, pin = true) {
50
+ const existing = this.#values.get(sessionId);
51
+ if (existing !== undefined) {
52
+ this.#values.delete(sessionId);
53
+ this.#values.set(sessionId, existing);
54
+ return existing;
55
+ }
56
+ const value = extractFirstUserMessageText(messages);
57
+ if (pin) {
58
+ if (!this.#values.has(sessionId) && this.#values.size >= this.limit) {
59
+ const oldest = this.#values.keys().next().value;
60
+ if (oldest !== undefined)
61
+ this.#values.delete(oldest);
62
+ }
63
+ this.#values.set(sessionId, value);
64
+ }
65
+ return value;
66
+ }
67
+ has(sessionId) {
68
+ return this.#values.has(sessionId);
69
+ }
70
+ clear() {
71
+ this.#values.clear();
72
+ }
73
+ }
38
74
  /**
39
75
  * Compute Claude Code's cch token over the final serialized request body.
40
76
  *
@@ -61,27 +97,29 @@ export async function signRequestBody(bodyString) {
61
97
  if (!BILLING_HEADER_CCH_PATTERN.test(bodyString))
62
98
  return bodyString;
63
99
  const unsignedBodyString = resetBillingHeaderCCH(bodyString);
64
- const token = await computeCCH(new TextEncoder().encode(unsignedBodyString));
100
+ const canonicalBody = JSON.parse(unsignedBodyString);
101
+ if ('model' in canonicalBody)
102
+ canonicalBody.model = '';
103
+ delete canonicalBody.max_tokens;
104
+ const token = await computeCCH(new TextEncoder().encode(JSON.stringify(canonicalBody)));
65
105
  return unsignedBodyString.replace(BILLING_HEADER_CCH_PLACEHOLDER_PATTERN, `$1cch=${token};`);
66
106
  }
67
107
  /**
68
- * Compute a stable 3-character suffix for cc_version.
69
- *
70
- * The captured Claude Code version uses its observed build hash. Custom/future
71
- * versions get a deterministic version-only suffix so date changes never rotate
72
- * the billing header and bust prompt-cache prefixes.
108
+ * Compute Claude Code's 3-character suffix for cc_version.
73
109
  */
74
- export function computeVersionSuffix(version = CLAUDE_CODE_VERSION, _date = new Date()) {
75
- if (version === CLAUDE_CODE_VERSION)
76
- return CLAUDE_CODE_BUILD_HASH;
77
- return createHash('sha256').update(version).digest('hex').slice(0, 3);
110
+ export function computeCcVersionSuffix(firstUserText, version = CLAUDE_CODE_VERSION) {
111
+ const sampledText = CCH_POSITIONS.map((position) => firstUserText[position] ?? '0').join('');
112
+ return createHash('sha256')
113
+ .update(`${CCH_SALT}${sampledText}${version}`)
114
+ .digest('hex')
115
+ .slice(0, 3);
78
116
  }
79
117
  /**
80
118
  * Build the billing header with a cch placeholder.
81
119
  * signRequestBody() must run after final request serialization to replace it.
82
120
  */
83
- export function buildBillingHeaderValue(_messages, version = CLAUDE_CODE_VERSION, entrypoint, date = new Date()) {
84
- const suffix = computeVersionSuffix(version, date);
121
+ export function buildBillingHeaderValue(messages, version = CLAUDE_CODE_VERSION, entrypoint, pinnedFirstUserText) {
122
+ const suffix = computeCcVersionSuffix(pinnedFirstUserText ?? extractFirstUserMessageText(messages), version);
85
123
  return ('x-anthropic-billing-header: ' +
86
124
  `cc_version=${version}.${suffix}; ` +
87
125
  `cc_entrypoint=${entrypoint}; ` +
@@ -10,7 +10,7 @@ export declare function resetClaudeCodeIdentityCachesForTest(): void;
10
10
  export declare function resolveClaudeCodeIdentity(accessToken: string, model: string | undefined, accountIdentity: string | undefined): Promise<ClaudeCodeIdentity>;
11
11
  export declare function buildClaudeCodeMetadataUserId(identity: ClaudeCodeIdentity): string | null;
12
12
  export declare function applyClaudeCodeMetadata(body: Record<string, unknown>, identity: ClaudeCodeIdentity): boolean;
13
- export declare const CLAUDE_CODE_FULL_AGENT_BETAS: readonly ['oauth-2025-04-20', 'interleaved-thinking-2025-05-14', 'thinking-token-count-2026-05-13', 'context-management-2025-06-27', 'prompt-caching-scope-2026-01-05', 'claude-code-20250219', 'advisor-tool-2026-03-01', 'advanced-tool-use-2025-11-20', 'extended-cache-ttl-2025-04-11', 'cache-diagnosis-2026-04-07'];
13
+ export declare const CLAUDE_CODE_FULL_AGENT_BETAS: readonly ['oauth-2025-04-20', 'interleaved-thinking-2025-05-14', 'thinking-token-count-2026-05-13', 'context-management-2025-06-27', 'prompt-caching-scope-2026-01-05', 'claude-code-20250219', 'advisor-tool-2026-03-01', 'advanced-tool-use-2025-11-20', 'mid-conversation-system-2026-04-07', 'effort-2025-11-24', 'fallback-credit-2026-06-01', 'extended-cache-ttl-2025-04-11', 'cache-diagnosis-2026-04-07'];
14
14
  export declare function selectClaudeCodeBetas(body?: Record<string, unknown> | null, extraBetas?: string[]): string;
15
15
  export declare function applyClaudeCodeHeaders(headers: Headers, accessToken: string, options?: {
16
16
  body?: Record<string, unknown> | null;
@@ -18,5 +18,4 @@ export declare function applyClaudeCodeHeaders(headers: Headers, accessToken: st
18
18
  extraBetas?: string[];
19
19
  }): Headers;
20
20
  export declare function orderClaudeCodeBody<T extends Record<string, unknown>>(body: T): T;
21
- export declare function claudeCodeBuildHash(): string;
22
21
  export declare function claudeCodeEntrypoint(): string;
@@ -1,5 +1,5 @@
1
1
  import { randomBytes, randomUUID } from 'node:crypto';
2
- import { CLAUDE_CODE_BUILD_HASH, CLAUDE_CODE_ENTRYPOINT, CLAUDE_CODE_STAINLESS_PACKAGE_VERSION, CLAUDE_CODE_STAINLESS_RUNTIME_VERSION, CLAUDE_CODE_VERSION, FAST_MODE_BETA, USER_AGENT, } from './constants.js';
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
3
  const IDENTITY_CACHE_LIMIT = 1_000;
4
4
  const identityCache = new Map();
5
5
  export function setBounded(map, key, value, limit = IDENTITY_CACHE_LIMIT) {
@@ -232,6 +232,8 @@ export function applyClaudeCodeMetadata(body, identity) {
232
232
  metadata.user_id = userId;
233
233
  return true;
234
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.
235
237
  export const CLAUDE_CODE_FULL_AGENT_BETAS = [
236
238
  'oauth-2025-04-20',
237
239
  'interleaved-thinking-2025-05-14',
@@ -241,6 +243,9 @@ export const CLAUDE_CODE_FULL_AGENT_BETAS = [
241
243
  'claude-code-20250219',
242
244
  'advisor-tool-2026-03-01',
243
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',
244
249
  'extended-cache-ttl-2025-04-11',
245
250
  'cache-diagnosis-2026-04-07',
246
251
  ];
@@ -378,9 +383,6 @@ export function orderClaudeCodeBody(body) {
378
383
  }
379
384
  return ordered;
380
385
  }
381
- export function claudeCodeBuildHash() {
382
- return CLAUDE_CODE_BUILD_HASH;
383
- }
384
386
  export function claudeCodeEntrypoint() {
385
387
  return CLAUDE_CODE_ENTRYPOINT;
386
388
  }
@@ -0,0 +1,109 @@
1
+ import { type BindIdentity, type CatalogEntry, type CloseRouteOptions, type ManagedCallOptions, type ManagedCloseRouteOptions, type RequestOptions, type RouteHandle, type RouteOpenOptions, type RouteTarget, SubcClient, type SubscribeOptions, type Subscription } from '@cortexkit/subc-client';
2
+ export type ClaustrumEndpoint = {
3
+ host: string;
4
+ port: number;
5
+ };
6
+ export type ClaustrumDetection = {
7
+ status: 'available';
8
+ schema: number;
9
+ wireVersion: number;
10
+ endpoints: ClaustrumEndpoint[];
11
+ } | {
12
+ status: 'absent';
13
+ path: string;
14
+ } | {
15
+ status: 'malformed';
16
+ path: string;
17
+ reason: string;
18
+ };
19
+ export declare function getDefaultClaustrumConnectionPath(): string;
20
+ export declare function detectClaustrumConnection(path?: string): Promise<ClaustrumDetection>;
21
+ export declare const CUSTODY_TOMBSTONE_PREFIX = "claustrum-tombstone:v1:";
22
+ export declare function custodyTombstoneKey(provider: string): string;
23
+ export declare function isCustodyTombstoneValue(value: unknown): value is string;
24
+ export declare function isCustodyTombstoneOAuth(auth: unknown, provider: string): boolean;
25
+ export declare class CustodyTombstoneRefreshError extends Error {
26
+ readonly provider: string;
27
+ readonly code = "custody_tombstone_refresh";
28
+ constructor(provider: string);
29
+ }
30
+ export declare function assertNotCustodyTombstone(refreshToken: unknown, provider: string): void;
31
+ export type CustodyHandleAccount = {
32
+ label: string;
33
+ handle: string;
34
+ credentialId: string;
35
+ };
36
+ export type CustodyHandleManifest = {
37
+ provider: string;
38
+ serve: string;
39
+ shape: string;
40
+ accounts: CustodyHandleAccount[];
41
+ };
42
+ export declare function isValidCustodyHandle(value: unknown): value is string;
43
+ export declare function readCustodyHandles(json: unknown, provider: string): CustodyHandleManifest;
44
+ export type ClaustrumClientOptions = {
45
+ connectionFile?: string;
46
+ handshakeTimeoutMs?: number;
47
+ connector?: ClaustrumConnector;
48
+ };
49
+ export type ClaustrumConnector = (options: {
50
+ connectionFile: string;
51
+ handshakeTimeoutMs?: number;
52
+ }) => Promise<SubcClient>;
53
+ export declare class ClaustrumClient {
54
+ #private;
55
+ private constructor();
56
+ static connect(options?: ClaustrumClientOptions): Promise<ClaustrumClient>;
57
+ catalogList(moduleId?: string): Promise<CatalogEntry[]>;
58
+ routeOpen(target: RouteTarget, identity: BindIdentity, options?: Omit<RouteOpenOptions, 'consumerIdentity'>): Promise<RouteHandle>;
59
+ request(handle: RouteHandle, body: unknown, options?: RequestOptions): Promise<unknown>;
60
+ call<Response = unknown>(moduleId: string, method: string, params?: unknown, options?: Omit<ManagedCallOptions, 'consumerIdentity'>): Promise<Response>;
61
+ subscribe(handle: RouteHandle, body: unknown, onEvent: (event: Uint8Array) => void, options?: SubscribeOptions): Subscription;
62
+ closeRoute(handle: RouteHandle, options?: CloseRouteOptions): Promise<void>;
63
+ closeManagedRoute(target: Extract<RouteTarget, {
64
+ kind: 'management_surface' | 'tool_provider';
65
+ }>, identity: BindIdentity, options?: Omit<ManagedCloseRouteOptions, 'consumerIdentity'>): Promise<void>;
66
+ close(): void;
67
+ }
68
+ export declare function connectClaustrumClient(options?: ClaustrumClientOptions): Promise<ClaustrumClient>;
69
+ export declare const CLAUSTRUM_MODULE_ID = "claustrum";
70
+ export declare const DEFAULT_CLAUSTRUM_CREDENTIAL_MIN_TTL_MS = 120000;
71
+ export declare const ERROR_CLASS_WIRE_SET: readonly ['transient', 'permanent', 'auth_required', 'context_overflow'];
72
+ export type ClaustrumCredentialErrorClass = (typeof ERROR_CLASS_WIRE_SET)[number];
73
+ export type ClaustrumCredentialErrorAction = 'gone' | 'reauth' | 'retry' | 'reduce_and_retry';
74
+ export type ClaustrumCredential = {
75
+ payload: string;
76
+ expiresAtMs: number | null;
77
+ recordVersion: number;
78
+ projectId?: string;
79
+ accountId?: string;
80
+ };
81
+ export type ClaustrumServedCredential = Pick<ClaustrumCredential, 'recordVersion'>;
82
+ export type ClaustrumReporterSource = 'direct' | 'relay_status_field' | 'relay_message_parse';
83
+ export declare class ClaustrumCredentialError extends Error {
84
+ readonly code: string;
85
+ readonly action: ClaustrumCredentialErrorAction;
86
+ readonly errorClass: ClaustrumCredentialErrorClass;
87
+ constructor(message: string, code: string, errorClass: ClaustrumCredentialErrorClass, action: ClaustrumCredentialErrorAction);
88
+ }
89
+ export type ClaustrumCredentialCacheOptions = {
90
+ identity?: BindIdentity;
91
+ now?: () => number;
92
+ minTtlMs?: number;
93
+ };
94
+ export declare class ClaustrumCredentialCache {
95
+ #private;
96
+ constructor(client: ClaustrumClient, options?: ClaustrumCredentialCacheOptions);
97
+ get(handle: string, minTtlMs?: number): Promise<ClaustrumCredential>;
98
+ peek(handle: string): ClaustrumCredential | undefined;
99
+ seedForTest(handle: string, credential: ClaustrumCredential): void;
100
+ reduceMinTtlMs(): number;
101
+ reportAuthFailure(handle: string, providerStatus: number, servedCredential: ClaustrumServedCredential, reporterSource?: ClaustrumReporterSource): Promise<void>;
102
+ reportAuthFailure(handle: string, servedCredential: ClaustrumServedCredential, providerStatus?: number, reporterSource?: ClaustrumReporterSource): Promise<void>;
103
+ invalidate(handle: string, recordVersion?: number): void;
104
+ close(): void;
105
+ }
106
+ export type ConnectClaustrumCredentialCacheOptions = ClaustrumClientOptions & ClaustrumCredentialCacheOptions & {
107
+ enabled?: boolean;
108
+ };
109
+ export declare function connectClaustrumCredentialCache(options?: ConnectClaustrumCredentialCacheOptions): Promise<ClaustrumCredentialCache | null>;