@cortexkit/anthropic-auth-core 1.2.5 → 1.3.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 +1 -1
- package/dist/accounts.d.ts +4 -0
- package/dist/accounts.js +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/routing.d.ts +23 -0
- package/dist/routing.js +80 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @cortexkit/anthropic-auth-core
|
|
2
2
|
|
|
3
|
-
Shared Anthropic OAuth, quota, cache, cache-keepalive, fast mode, relay, dump, and request-signing helpers used by CortexKit's OpenCode and Pi integrations.
|
|
3
|
+
Shared Anthropic OAuth, quota, routing, cache, cache-keepalive, fast mode, relay, dump, and request-signing helpers used by CortexKit's OpenCode and Pi integrations.
|
|
4
4
|
|
|
5
5
|
User-facing packages:
|
|
6
6
|
|
package/dist/accounts.d.ts
CHANGED
|
@@ -30,12 +30,16 @@ export type AccountQuotaWindow = {
|
|
|
30
30
|
resetsAt?: string;
|
|
31
31
|
checkedAt: number;
|
|
32
32
|
};
|
|
33
|
+
export type RoutingMode = 'main-first' | 'fallback-first';
|
|
33
34
|
export type AccountStorage = {
|
|
34
35
|
version: 1;
|
|
35
36
|
main?: {
|
|
36
37
|
type: 'opencode';
|
|
37
38
|
provider: 'anthropic';
|
|
38
39
|
};
|
|
40
|
+
routing?: {
|
|
41
|
+
mode?: RoutingMode;
|
|
42
|
+
};
|
|
39
43
|
fallbackOn?: number[];
|
|
40
44
|
refresh?: {
|
|
41
45
|
enabled?: boolean;
|
package/dist/accounts.js
CHANGED
|
@@ -114,6 +114,7 @@ function normalizeStorage(value) {
|
|
|
114
114
|
return {
|
|
115
115
|
version: 1,
|
|
116
116
|
main: { type: 'opencode', provider: 'anthropic' },
|
|
117
|
+
routing: isRecord(value.routing) ? value.routing : undefined,
|
|
117
118
|
fallbackOn: Array.isArray(value.fallbackOn)
|
|
118
119
|
? value.fallbackOn.filter((status) => Number.isInteger(status))
|
|
119
120
|
: undefined,
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type AccountStorage, type RoutingMode } from './accounts.ts';
|
|
2
|
+
export declare const CLAUDE_ROUTING_COMMAND_NAME = "claude-routing";
|
|
3
|
+
export declare const ROUTING_MODES: readonly ["main-first", "fallback-first"];
|
|
4
|
+
export declare const DEFAULT_ROUTING_MODE: RoutingMode;
|
|
5
|
+
type RoutingCommandAction = {
|
|
6
|
+
type: 'status';
|
|
7
|
+
} | {
|
|
8
|
+
type: 'mode';
|
|
9
|
+
mode: RoutingMode;
|
|
10
|
+
} | {
|
|
11
|
+
type: 'usage';
|
|
12
|
+
};
|
|
13
|
+
export declare function getRoutingMode(storage: AccountStorage | null): RoutingMode;
|
|
14
|
+
export declare function setRoutingMode(mode: RoutingMode, path?: string): Promise<AccountStorage>;
|
|
15
|
+
export declare function parseRoutingCommandAction(argumentsText: string): RoutingCommandAction;
|
|
16
|
+
export declare function buildRoutingStatusSummary(input?: {
|
|
17
|
+
mode?: RoutingMode;
|
|
18
|
+
}): string;
|
|
19
|
+
export declare function executeRoutingCommand(input: {
|
|
20
|
+
argumentsText: string;
|
|
21
|
+
mode?: RoutingMode;
|
|
22
|
+
}): string;
|
|
23
|
+
export {};
|
package/dist/routing.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { getAccountStoragePath, loadAccounts, saveAccounts, } from "./accounts.js";
|
|
2
|
+
export const CLAUDE_ROUTING_COMMAND_NAME = 'claude-routing';
|
|
3
|
+
export const ROUTING_MODES = ['main-first', 'fallback-first'];
|
|
4
|
+
export const DEFAULT_ROUTING_MODE = 'main-first';
|
|
5
|
+
const ROUTING_USAGE = 'Usage: `/claude-routing`, `/claude-routing main-first`, or `/claude-routing fallback-first`.';
|
|
6
|
+
function normalizeRoutingMode(value) {
|
|
7
|
+
return typeof value === 'string' &&
|
|
8
|
+
ROUTING_MODES.includes(value)
|
|
9
|
+
? value
|
|
10
|
+
: DEFAULT_ROUTING_MODE;
|
|
11
|
+
}
|
|
12
|
+
export function getRoutingMode(storage) {
|
|
13
|
+
return normalizeRoutingMode(storage?.routing?.mode);
|
|
14
|
+
}
|
|
15
|
+
export async function setRoutingMode(mode, path = getAccountStoragePath()) {
|
|
16
|
+
const storage = (await loadAccounts(path)) ?? {
|
|
17
|
+
version: 1,
|
|
18
|
+
main: { type: 'opencode', provider: 'anthropic' },
|
|
19
|
+
accounts: [],
|
|
20
|
+
};
|
|
21
|
+
storage.routing = {
|
|
22
|
+
...(storage.routing ?? {}),
|
|
23
|
+
mode,
|
|
24
|
+
};
|
|
25
|
+
await saveAccounts(storage, path);
|
|
26
|
+
return storage;
|
|
27
|
+
}
|
|
28
|
+
export function parseRoutingCommandAction(argumentsText) {
|
|
29
|
+
const tokens = argumentsText.trim().toLowerCase().split(/\s+/).filter(Boolean);
|
|
30
|
+
if (tokens.length === 0)
|
|
31
|
+
return { type: 'status' };
|
|
32
|
+
if (tokens.length === 1 && ROUTING_MODES.includes(tokens[0])) {
|
|
33
|
+
return { type: 'mode', mode: tokens[0] };
|
|
34
|
+
}
|
|
35
|
+
if (tokens.length === 2 &&
|
|
36
|
+
tokens[0] === 'mode' &&
|
|
37
|
+
ROUTING_MODES.includes(tokens[1])) {
|
|
38
|
+
return { type: 'mode', mode: tokens[1] };
|
|
39
|
+
}
|
|
40
|
+
return { type: 'usage' };
|
|
41
|
+
}
|
|
42
|
+
function routingDescription(mode) {
|
|
43
|
+
if (mode === 'fallback-first') {
|
|
44
|
+
return 'Try usable fallback accounts before the main account. If no fallback succeeds, try the main account.';
|
|
45
|
+
}
|
|
46
|
+
return 'Try the main account first. Use fallback accounts only when quota policy or fallback errors require it.';
|
|
47
|
+
}
|
|
48
|
+
export function buildRoutingStatusSummary(input) {
|
|
49
|
+
const mode = input?.mode ?? DEFAULT_ROUTING_MODE;
|
|
50
|
+
return [
|
|
51
|
+
'## Claude Routing Status',
|
|
52
|
+
'',
|
|
53
|
+
`- Mode: \`${mode}\``,
|
|
54
|
+
`- Behavior: ${routingDescription(mode)}`,
|
|
55
|
+
'',
|
|
56
|
+
ROUTING_USAGE,
|
|
57
|
+
].join('\n');
|
|
58
|
+
}
|
|
59
|
+
export function executeRoutingCommand(input) {
|
|
60
|
+
const action = parseRoutingCommandAction(input.argumentsText);
|
|
61
|
+
const mode = input.mode ?? DEFAULT_ROUTING_MODE;
|
|
62
|
+
if (action.type === 'status')
|
|
63
|
+
return buildRoutingStatusSummary({ mode });
|
|
64
|
+
if (action.type === 'mode') {
|
|
65
|
+
return [
|
|
66
|
+
'## Claude Routing Updated',
|
|
67
|
+
'',
|
|
68
|
+
`Mode updated to \`${action.mode}\`.`,
|
|
69
|
+
'',
|
|
70
|
+
...buildRoutingStatusSummary({ mode: action.mode }).split('\n').slice(2),
|
|
71
|
+
].join('\n');
|
|
72
|
+
}
|
|
73
|
+
return [
|
|
74
|
+
'## Claude Routing Usage',
|
|
75
|
+
'',
|
|
76
|
+
ROUTING_USAGE,
|
|
77
|
+
'',
|
|
78
|
+
...buildRoutingStatusSummary({ mode }).split('\n').slice(2),
|
|
79
|
+
].join('\n');
|
|
80
|
+
}
|