@cortexkit/pi-anthropic-auth 1.0.0 → 1.1.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.
package/README.md CHANGED
@@ -40,7 +40,7 @@ Pi state is stored separately from OpenCode at:
40
40
 
41
41
  Override the path with `PI_ANTHROPIC_AUTH_FILE`. The package also respects `PI_AGENT_DIR` when deriving the default sidecar path.
42
42
 
43
- The sidecar uses the same JSON shape as the OpenCode package, including `claudeCache`, `dump`, `relay`, and fallback `accounts` blocks.
43
+ The sidecar uses the same JSON shape as the OpenCode package, including `claudeCache`, `claudeFast`, `dump`, `relay`, and fallback `accounts` blocks.
44
44
 
45
45
  ## Commands
46
46
 
@@ -56,10 +56,14 @@ The sidecar uses the same JSON shape as the OpenCode package, including `claudeC
56
56
  /claude-dump on
57
57
  /claude-dump off
58
58
 
59
+ /claude-fast
60
+ /claude-fast on
61
+ /claude-fast off
62
+
59
63
  /claude-quota
60
64
  ```
61
65
 
62
- `/claude-quota` reports sidecar fallback account quota state from `~/.pi/agent/anthropic-auth.json`.
66
+ `/claude-quota` reports sidecar fallback account quota state from `~/.pi/agent/anthropic-auth.json`. `/claude-fast on` adds Anthropic `speed: "fast"` plus the `fast-mode-2026-02-01` beta header for supported Opus models (`claude-opus-4-6` and `claude-opus-4-7`).
63
67
 
64
68
  ## Relay
65
69
 
package/dist/commands.js CHANGED
@@ -1,4 +1,4 @@
1
- import { buildClaudeQuotaSummary, buildFallbackQuotaSummaries, executeCache1hCommand, executeDumpCommand, getCache1hPersistentMode, isCache1hPersistentlyEnabled, isDumpPersistentlyEnabled, loadAccounts, parseCache1hCommandAction, parseDumpCommandAction, setCache1hPersistentEnabled, setCache1hPersistentMode, setDumpPersistentEnabled, } from '@cortexkit/anthropic-auth-core';
1
+ import { buildClaudeQuotaSummary, buildFallbackQuotaSummaries, executeCache1hCommand, executeDumpCommand, executeFastModeCommand, getCache1hPersistentMode, isCache1hPersistentlyEnabled, isDumpPersistentlyEnabled, isFastModePersistentlyEnabled, loadAccounts, parseCache1hCommandAction, parseDumpCommandAction, parseFastModeCommandAction, setCache1hPersistentEnabled, setCache1hPersistentMode, setDumpPersistentEnabled, setFastModePersistentEnabled, } from '@cortexkit/anthropic-auth-core';
2
2
  import { getPiAccountStoragePath } from "./paths.js";
3
3
  function notify(ctx, message, kind = 'info') {
4
4
  ctx.ui.notify(message, kind);
@@ -55,6 +55,30 @@ export function registerCommands(pi) {
55
55
  notify(ctx, executeDumpCommand({ argumentsText: args ?? '', enabled: nextEnabled }), action.type === 'usage' ? 'warning' : 'info');
56
56
  },
57
57
  });
58
+ pi.registerCommand('claude-fast', {
59
+ description: 'Show or configure Anthropic fast mode for supported Opus models',
60
+ handler: async (args, ctx) => {
61
+ const path = getPiAccountStoragePath();
62
+ const storage = await loadAccounts(path);
63
+ const action = parseFastModeCommandAction(args ?? '');
64
+ const enabled = isFastModePersistentlyEnabled(storage);
65
+ const nextEnabled = action.type === 'enable'
66
+ ? true
67
+ : action.type === 'disable'
68
+ ? false
69
+ : enabled;
70
+ if (action.type === 'enable') {
71
+ await setFastModePersistentEnabled(true, path);
72
+ }
73
+ else if (action.type === 'disable') {
74
+ await setFastModePersistentEnabled(false, path);
75
+ }
76
+ notify(ctx, executeFastModeCommand({
77
+ argumentsText: args ?? '',
78
+ enabled: nextEnabled,
79
+ }), action.type === 'usage' ? 'warning' : 'info');
80
+ },
81
+ });
58
82
  pi.registerCommand('claude-quota', {
59
83
  description: 'Show Claude quota state for fallback accounts',
60
84
  handler: async (_args, ctx) => {
package/dist/convert.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type Cache1hMode } from '@cortexkit/anthropic-auth-core';
1
+ import { type Cache1hMode, type ClaudeCodeIdentity } from '@cortexkit/anthropic-auth-core';
2
2
  import type { Context, SimpleStreamOptions, Tool } from '@mariozechner/pi-ai';
3
3
  export type AnthropicRequestBody = {
4
4
  model: string;
@@ -14,12 +14,13 @@ export type AnthropicRequestBody = {
14
14
  cache_control?: {
15
15
  type: 'ephemeral';
16
16
  };
17
+ speed?: 'fast';
17
18
  };
18
19
  export declare function fromClaudeCodeToolName(name: string, tools?: Tool[]): string;
19
20
  export declare function buildAnthropicRequest(modelId: string, context: Context, options: SimpleStreamOptions | undefined, cache: {
20
21
  enabled: boolean;
21
22
  mode: Cache1hMode;
22
- }): Promise<{
23
+ }, fastModeEnabled?: boolean, identity?: ClaudeCodeIdentity): Promise<{
23
24
  body: AnthropicRequestBody;
24
25
  bodyText: string;
25
26
  }>;
package/dist/convert.js CHANGED
@@ -1,4 +1,4 @@
1
- import { buildBillingHeaderValue, CLAUDE_CODE_ENTRYPOINT, CLAUDE_CODE_IDENTITY, signRequestBody, } from '@cortexkit/anthropic-auth-core';
1
+ import { applyClaudeCodeMetadata, buildBillingHeaderValue, CLAUDE_CODE_ENTRYPOINT, CLAUDE_CODE_IDENTITY, isFastModeSupportedModel, orderClaudeCodeBody, signRequestBody, } from '@cortexkit/anthropic-auth-core';
2
2
  const CLAUDE_CODE_TOOLS = new Map([
3
3
  'Read',
4
4
  'Write',
@@ -187,7 +187,7 @@ function applyCacheMode(body, enabled, mode) {
187
187
  body.cache_control = { type: 'ephemeral' };
188
188
  addTtl(body);
189
189
  }
190
- export async function buildAnthropicRequest(modelId, context, options, cache) {
190
+ export async function buildAnthropicRequest(modelId, context, options, cache, fastModeEnabled = false, identity) {
191
191
  const messages = convertMessages(context.messages);
192
192
  const system = [
193
193
  {
@@ -209,6 +209,9 @@ export async function buildAnthropicRequest(modelId, context, options, cache) {
209
209
  const tools = convertTools(context.tools);
210
210
  if (tools?.length)
211
211
  body.tools = tools;
212
+ if (fastModeEnabled && isFastModeSupportedModel(modelId)) {
213
+ body.speed = 'fast';
214
+ }
212
215
  if (options?.reasoning) {
213
216
  const budgets = {
214
217
  minimal: 1024,
@@ -226,7 +229,9 @@ export async function buildAnthropicRequest(modelId, context, options, cache) {
226
229
  }
227
230
  addEphemeralCacheControl(body);
228
231
  applyCacheMode(body, cache.enabled, cache.mode);
229
- const unsigned = JSON.stringify(body);
232
+ if (identity)
233
+ applyClaudeCodeMetadata(body, identity);
234
+ const unsigned = JSON.stringify(orderClaudeCodeBody(body));
230
235
  const bodyText = await signRequestBody(unsigned);
231
236
  return { body, bodyText };
232
237
  }
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { authorize, CLIENT_ID, exchange, TOKEN_URL, } from '@cortexkit/anthropic-auth-core';
1
+ import { authorize, exchange, refreshClaudeOAuthToken, } from '@cortexkit/anthropic-auth-core';
2
2
  import { registerCommands } from "./commands.js";
3
3
  import { streamCortexKitAnthropic } from "./stream.js";
4
4
  async function loginAnthropic(callbacks) {
@@ -18,27 +18,13 @@ async function loginAnthropic(callbacks) {
18
18
  };
19
19
  }
20
20
  async function refreshAnthropicToken(credentials) {
21
- const response = await fetch(TOKEN_URL, {
22
- method: 'POST',
23
- headers: {
24
- 'Content-Type': 'application/json',
25
- Accept: 'application/json, text/plain, */*',
26
- 'User-Agent': 'axios/1.13.6',
27
- },
28
- body: JSON.stringify({
29
- grant_type: 'refresh_token',
30
- client_id: CLIENT_ID,
31
- refresh_token: credentials.refresh,
32
- }),
21
+ const refreshed = await refreshClaudeOAuthToken({
22
+ refreshToken: credentials.refresh,
33
23
  });
34
- if (!response.ok) {
35
- throw new Error(`Anthropic OAuth refresh failed: ${await response.text()}`);
36
- }
37
- const json = (await response.json());
38
24
  return {
39
- refresh: json.refresh_token ?? credentials.refresh,
40
- access: json.access_token,
41
- expires: Date.now() + json.expires_in * 1000,
25
+ refresh: refreshed.refresh,
26
+ access: refreshed.access,
27
+ expires: refreshed.expires,
42
28
  };
43
29
  }
44
30
  export default function cortexKitPiAnthropicAuth(pi) {
package/dist/stream.js CHANGED
@@ -1,4 +1,4 @@
1
- import { FallbackAccountManager, getCache1hPersistentMode, getRelayConfig, isCache1hPersistentlyEnabled, loadAccounts, REQUIRED_BETAS, sendViaRelay, shouldFallbackStatus, USER_AGENT, } from '@cortexkit/anthropic-auth-core';
1
+ import { applyClaudeCodeHeaders, FAST_MODE_BETA, FallbackAccountManager, getCache1hPersistentMode, getRelayConfig, isCache1hPersistentlyEnabled, isFastModePersistentlyEnabled, loadAccounts, mergeAnthropicBetas, resolveClaudeCodeIdentity, sendViaRelay, shouldFallbackStatus, } from '@cortexkit/anthropic-auth-core';
2
2
  import { calculateCost, createAssistantMessageEventStream, } from '@mariozechner/pi-ai';
3
3
  import { buildAnthropicRequest, fromClaudeCodeToolName } from "./convert.js";
4
4
  import { getPiAccountStoragePath } from "./paths.js";
@@ -80,22 +80,24 @@ async function* parseSse(response) {
80
80
  }
81
81
  async function sendAnthropicRequest(options) {
82
82
  const storage = await loadAccounts(options.storagePath);
83
- const headers = new Headers({
84
- accept: 'application/json',
85
- 'content-type': 'application/json',
86
- authorization: `Bearer ${options.accessToken}`,
87
- 'anthropic-beta': REQUIRED_BETAS.join(','),
88
- 'user-agent': USER_AGENT,
89
- 'x-app': 'cli',
83
+ const identity = await resolveClaudeCodeIdentity(options.accessToken, options.model.id);
84
+ const { body, bodyText } = await buildAnthropicRequest(options.model.id, options.context, options.streamOptions, {
85
+ enabled: isCache1hPersistentlyEnabled(storage),
86
+ mode: getCache1hPersistentMode(storage),
87
+ }, isFastModePersistentlyEnabled(storage), identity);
88
+ const headers = applyClaudeCodeHeaders(new Headers(), options.accessToken, {
89
+ body,
90
+ identity,
90
91
  });
91
- if (options.streamOptions?.sessionId) {
92
- headers.set('x-session-affinity', options.streamOptions.sessionId);
92
+ if (body.speed === 'fast') {
93
+ headers.set('anthropic-beta', mergeAnthropicBetas(headers.get('anthropic-beta'), [FAST_MODE_BETA]));
93
94
  }
95
+ const relayAffinity = options.streamOptions?.sessionId ?? null;
94
96
  const input = new URL('/v1/messages?beta=true', options.model.baseUrl);
95
97
  const init = {
96
98
  method: 'POST',
97
99
  headers,
98
- body: options.bodyText,
100
+ body: bodyText,
99
101
  signal: options.streamOptions?.signal,
100
102
  };
101
103
  return sendViaRelay({
@@ -103,8 +105,9 @@ async function sendAnthropicRequest(options) {
103
105
  input,
104
106
  init,
105
107
  headers,
106
- body: options.bodyText,
108
+ body: bodyText,
107
109
  fallback: () => fetch(input, init),
110
+ affinity: relayAffinity,
108
111
  });
109
112
  }
110
113
  async function firstStreamingError(response) {
@@ -169,16 +172,10 @@ export function streamCortexKitAnthropic(model, context, options) {
169
172
  if (!accessToken)
170
173
  throw new Error('Missing Anthropic OAuth access token');
171
174
  const storagePath = getPiAccountStoragePath();
172
- const storage = await loadAccounts(storagePath);
173
- const { bodyText } = await buildAnthropicRequest(model.id, context, options, {
174
- enabled: isCache1hPersistentlyEnabled(storage),
175
- mode: getCache1hPersistentMode(storage),
176
- });
177
175
  const response = await executeWithFallback({
178
176
  model,
179
177
  context,
180
178
  streamOptions: options,
181
- bodyText,
182
179
  primaryAccessToken: accessToken,
183
180
  storagePath,
184
181
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/pi-anthropic-auth",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -35,7 +35,7 @@
35
35
  "prepublishOnly": "bun run build"
36
36
  },
37
37
  "dependencies": {
38
- "@cortexkit/anthropic-auth-core": "1.0.0"
38
+ "@cortexkit/anthropic-auth-core": "1.1.1"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "@mariozechner/pi-ai": "*",