@cortexkit/anthropic-auth-core
Advanced tools
| import { type AccountStorage, type RoutingMode } from './accounts.ts'; | ||
| export declare const CLAUDE_ROUTING_COMMAND_NAME = "claude-routing"; | ||
| export declare const ROUTING_MODES: readonly ["main-first", "fallback-first"]; | ||
| export declare const DEFAULT_ROUTING_MODE: RoutingMode; | ||
| type RoutingCommandAction = { | ||
| type: 'status'; | ||
| } | { | ||
| type: 'mode'; | ||
| mode: RoutingMode; | ||
| } | { | ||
| type: 'usage'; | ||
| }; | ||
| export declare function getRoutingMode(storage: AccountStorage | null): RoutingMode; | ||
| export declare function setRoutingMode(mode: RoutingMode, path?: string): Promise<AccountStorage>; | ||
| export declare function parseRoutingCommandAction(argumentsText: string): RoutingCommandAction; | ||
| export declare function buildRoutingStatusSummary(input?: { | ||
| mode?: RoutingMode; | ||
| }): string; | ||
| export declare function executeRoutingCommand(input: { | ||
| argumentsText: string; | ||
| mode?: RoutingMode; | ||
| }): string; | ||
| export {}; |
| import { getAccountStoragePath, loadAccounts, saveAccounts, } from "./accounts.js"; | ||
| export const CLAUDE_ROUTING_COMMAND_NAME = 'claude-routing'; | ||
| export const ROUTING_MODES = ['main-first', 'fallback-first']; | ||
| export const DEFAULT_ROUTING_MODE = 'main-first'; | ||
| const ROUTING_USAGE = 'Usage: `/claude-routing`, `/claude-routing main-first`, or `/claude-routing fallback-first`.'; | ||
| function normalizeRoutingMode(value) { | ||
| return typeof value === 'string' && | ||
| ROUTING_MODES.includes(value) | ||
| ? value | ||
| : DEFAULT_ROUTING_MODE; | ||
| } | ||
| export function getRoutingMode(storage) { | ||
| return normalizeRoutingMode(storage?.routing?.mode); | ||
| } | ||
| export async function setRoutingMode(mode, path = getAccountStoragePath()) { | ||
| const storage = (await loadAccounts(path)) ?? { | ||
| version: 1, | ||
| main: { type: 'opencode', provider: 'anthropic' }, | ||
| accounts: [], | ||
| }; | ||
| storage.routing = { | ||
| ...(storage.routing ?? {}), | ||
| mode, | ||
| }; | ||
| await saveAccounts(storage, path); | ||
| return storage; | ||
| } | ||
| export function parseRoutingCommandAction(argumentsText) { | ||
| const tokens = argumentsText.trim().toLowerCase().split(/\s+/).filter(Boolean); | ||
| if (tokens.length === 0) | ||
| return { type: 'status' }; | ||
| if (tokens.length === 1 && ROUTING_MODES.includes(tokens[0])) { | ||
| return { type: 'mode', mode: tokens[0] }; | ||
| } | ||
| if (tokens.length === 2 && | ||
| tokens[0] === 'mode' && | ||
| ROUTING_MODES.includes(tokens[1])) { | ||
| return { type: 'mode', mode: tokens[1] }; | ||
| } | ||
| return { type: 'usage' }; | ||
| } | ||
| function routingDescription(mode) { | ||
| if (mode === 'fallback-first') { | ||
| return 'Try usable fallback accounts before the main account. If no fallback succeeds, try the main account.'; | ||
| } | ||
| return 'Try the main account first. Use fallback accounts only when quota policy or fallback errors require it.'; | ||
| } | ||
| export function buildRoutingStatusSummary(input) { | ||
| const mode = input?.mode ?? DEFAULT_ROUTING_MODE; | ||
| return [ | ||
| '## Claude Routing Status', | ||
| '', | ||
| `- Mode: \`${mode}\``, | ||
| `- Behavior: ${routingDescription(mode)}`, | ||
| '', | ||
| ROUTING_USAGE, | ||
| ].join('\n'); | ||
| } | ||
| export function executeRoutingCommand(input) { | ||
| const action = parseRoutingCommandAction(input.argumentsText); | ||
| const mode = input.mode ?? DEFAULT_ROUTING_MODE; | ||
| if (action.type === 'status') | ||
| return buildRoutingStatusSummary({ mode }); | ||
| if (action.type === 'mode') { | ||
| return [ | ||
| '## Claude Routing Updated', | ||
| '', | ||
| `Mode updated to \`${action.mode}\`.`, | ||
| '', | ||
| ...buildRoutingStatusSummary({ mode: action.mode }).split('\n').slice(2), | ||
| ].join('\n'); | ||
| } | ||
| return [ | ||
| '## Claude Routing Usage', | ||
| '', | ||
| ROUTING_USAGE, | ||
| '', | ||
| ...buildRoutingStatusSummary({ mode }).split('\n').slice(2), | ||
| ].join('\n'); | ||
| } |
@@ -33,2 +33,3 @@ import { type Cache1hMode } from './constants.ts'; | ||
| }; | ||
| export type RoutingMode = 'main-first' | 'fallback-first'; | ||
| export type AccountStorage = { | ||
@@ -40,2 +41,5 @@ version: 1; | ||
| }; | ||
| routing?: { | ||
| mode?: RoutingMode; | ||
| }; | ||
| fallbackOn?: number[]; | ||
@@ -42,0 +46,0 @@ refresh?: { |
+1
-0
@@ -117,2 +117,3 @@ import { createHash, randomUUID } from 'node:crypto'; | ||
| main: { type: 'opencode', provider: 'anthropic' }, | ||
| routing: isRecord(value.routing) ? value.routing : undefined, | ||
| fallbackOn: Array.isArray(value.fallbackOn) | ||
@@ -119,0 +120,0 @@ ? value.fallbackOn.filter((status) => Number.isInteger(status)) |
+1
-0
@@ -14,1 +14,2 @@ export * from './accounts.ts'; | ||
| export * from './relay.ts'; | ||
| export * from './routing.ts'; |
+1
-0
@@ -14,1 +14,2 @@ export * from "./accounts.js"; | ||
| export * from "./relay.js"; | ||
| export * from "./routing.js"; |
+1
-1
| { | ||
| "name": "@cortexkit/anthropic-auth-core", | ||
| "version": "1.2.5", | ||
| "version": "1.3.0", | ||
| "type": "module", | ||
@@ -5,0 +5,0 @@ "repository": { |
+1
-1
| # @cortexkit/anthropic-auth-core | ||
| Shared Anthropic OAuth, quota, cache, cache-keepalive, fast mode, relay, dump, and request-signing helpers used by CortexKit's OpenCode and Pi integrations. | ||
| Shared Anthropic OAuth, quota, routing, cache, cache-keepalive, fast mode, relay, dump, and request-signing helpers used by CortexKit's OpenCode and Pi integrations. | ||
@@ -5,0 +5,0 @@ User-facing packages: |
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
162721
2.55%33
6.45%4018
2.81%