@wengine-ai/claude-code-router-shared 2.0.26 → 2.3.235

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 musistudio
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,219 @@
1
+ export declare const CLIENT_IDS: readonly ["claudeCode", "codex", "pi", "qwenCode", "opencode"];
2
+ export type ClientId = (typeof CLIENT_IDS)[number];
3
+ export type ClientAction = "enable" | "disable" | "restore";
4
+ export interface ClientRoutingConfig {
5
+ extendedContextRatio?: number;
6
+ }
7
+ export interface ClientConfig {
8
+ enabled?: boolean;
9
+ managed?: boolean;
10
+ configPath?: string;
11
+ modelAlias?: string;
12
+ activeAccountId?: string;
13
+ autoSwitchAccounts?: boolean;
14
+ autoRefreshTokens?: boolean;
15
+ routing?: ClientRoutingConfig;
16
+ quota?: {
17
+ limit5h?: number;
18
+ limit7d?: number;
19
+ };
20
+ }
21
+ export type ClientsConfig = Partial<Record<ClientId, ClientConfig>>;
22
+ export interface ClientStatus {
23
+ id: ClientId;
24
+ name: string;
25
+ enabled: boolean;
26
+ managed: boolean;
27
+ configPath: string;
28
+ exists: boolean;
29
+ activeModel?: string;
30
+ modelAlias?: string;
31
+ details?: string;
32
+ }
33
+ export interface ClientOperationResult {
34
+ id: ClientId;
35
+ action: ClientAction;
36
+ success: boolean;
37
+ status?: ClientStatus;
38
+ error?: string;
39
+ }
40
+ export interface ClientApplyResult {
41
+ success: boolean;
42
+ results: ClientOperationResult[];
43
+ clients: ClientStatus[];
44
+ config: Record<string, any>;
45
+ }
46
+ export interface CodexAccount {
47
+ id: string;
48
+ label: string;
49
+ email?: string;
50
+ plan?: string;
51
+ accountId?: string;
52
+ authMode?: string;
53
+ active: boolean;
54
+ createdAt: string;
55
+ updatedAt: string;
56
+ lastUsedAt?: string;
57
+ limitedUntil?: string;
58
+ limitedWindow?: "5h" | "7d" | "unknown";
59
+ limitedReason?: string;
60
+ lastLimitedAt?: string;
61
+ usage?: {
62
+ used5h: number;
63
+ used7d: number;
64
+ limit5h?: number;
65
+ limit7d?: number;
66
+ reset5h?: string;
67
+ reset7d?: string;
68
+ };
69
+ }
70
+ export interface CodexAccountsResult {
71
+ accounts: CodexAccount[];
72
+ activeAccountId?: string;
73
+ authPath: string;
74
+ }
75
+ export interface CodexAccountOperationResult extends CodexAccountsResult {
76
+ success: boolean;
77
+ account?: CodexAccount;
78
+ switchedAccount?: CodexAccount;
79
+ config: Record<string, any>;
80
+ }
81
+ export interface CodexRefreshTokenExportResult {
82
+ success: boolean;
83
+ account: CodexAccount;
84
+ refreshToken: string;
85
+ refreshedAt?: string;
86
+ source: "managed" | "current";
87
+ }
88
+ export interface CodexTokenRefreshResult {
89
+ id: string;
90
+ label: string;
91
+ refreshed: boolean;
92
+ error?: string;
93
+ }
94
+ interface ResolvedClientConfig extends Omit<Required<ClientConfig>, "routing"> {
95
+ routing?: ClientRoutingConfig;
96
+ }
97
+ interface ClientOperationOptions {
98
+ updateEnabled?: boolean;
99
+ }
100
+ export declare const DEFAULT_CONTEXT_WINDOW = 200000;
101
+ /**
102
+ * Resolve the configured context window (in tokens) from the global config,
103
+ * falling back to DEFAULT_CONTEXT_WINDOW. Accepts a number or a numeric string.
104
+ */
105
+ export declare function getContextWindow(config: Record<string, any>): number;
106
+ export declare function getDefaultClientsConfig(): ClientsConfig;
107
+ export declare function getClientConfig(config: Record<string, any>, id: ClientId): ResolvedClientConfig;
108
+ export declare function isClientId(value: string): value is ClientId;
109
+ export declare function isClientEnabled(config: Record<string, any>, id: ClientId): boolean;
110
+ /**
111
+ * Apply ccr takeover settings (base URL, auth token, model family routing,
112
+ * auto-compact, status line) to a project's `.claude/settings.local.json`,
113
+ * mirroring what `claudeCodeAdapter.enable` does for `~/.claude/settings.json`.
114
+ */
115
+ export declare function applyCcrProjectTakeover(settings: Record<string, any>, config: Record<string, any>, projectPath?: string): void;
116
+ /**
117
+ * Remove ccr-managed fields from a project's `.claude/settings.local.json`,
118
+ * preserving any unrelated settings (permissions, hooks, etc.).
119
+ */
120
+ export declare function removeCcrProjectTakeover(settings: Record<string, any>, projectPath?: string, config?: Record<string, any>): void;
121
+ /**
122
+ * Whether a project's `.claude/settings.local.json` is currently taken over by ccr.
123
+ */
124
+ export declare function isCcrProjectTakeoverActive(settings: Record<string, any>): boolean;
125
+ export declare function listCodexAccounts(config: Record<string, any>): CodexAccountsResult;
126
+ export declare function getActiveCodexAccount(config: Record<string, any>): CodexAccount | undefined;
127
+ export declare function markActiveCodexAccountLimitedAndSwitch(config: Record<string, any>, reason?: string, retryAfterSeconds?: number): CodexAccountOperationResult;
128
+ export declare function importCurrentCodexAccount(config: Record<string, any>, label?: string): CodexAccountOperationResult;
129
+ export declare function exportCodexRefreshToken(config: Record<string, any>, accountId?: string): CodexRefreshTokenExportResult;
130
+ export declare function importCodexAccountFromRefreshToken(config: Record<string, any>, refreshToken: string, label?: string): Promise<CodexAccountOperationResult>;
131
+ /**
132
+ * Exchange a managed Codex account's stored refresh_token for a fresh
133
+ * access_token/refresh_token pair and persist the result, independent of
134
+ * whether the account is currently active.
135
+ *
136
+ * If the account is the active one and `~/.codex/auth.json` holds a newer
137
+ * `last_refresh` (e.g. the official Codex CLI already refreshed it during
138
+ * use), that newer refresh_token is exchanged instead so the stored snapshot
139
+ * never refreshes with a stale/already-rotated token. The refreshed tokens
140
+ * are written back to the managed snapshot and, for the active account,
141
+ * also synced to `~/.codex/auth.json`.
142
+ */
143
+ export declare function refreshCodexAccountTokens(config: Record<string, any>, accountId: string): Promise<CodexAccountOperationResult>;
144
+ /**
145
+ * Refresh tokens for every managed Codex account whose access_token is close
146
+ * to expiry or whose tokens haven't been refreshed in a long time, regardless
147
+ * of whether the account is currently active. Intended to be called
148
+ * periodically so refresh_tokens never go unused long enough to expire.
149
+ */
150
+ export declare function refreshDueCodexAccounts(config: Record<string, any>, options?: {
151
+ marginSeconds?: number;
152
+ maxAgeMs?: number;
153
+ }): Promise<CodexTokenRefreshResult[]>;
154
+ export declare function activateCodexAccount(config: Record<string, any>, accountId: string): CodexAccountOperationResult;
155
+ export declare function deleteCodexAccount(config: Record<string, any>, accountId: string): CodexAccountOperationResult;
156
+ /**
157
+ * Enable ccr takeover for a single project's pi configuration: register the
158
+ * global ccr provider (idempotent), trust the project folder, and point the
159
+ * project's `.pi/settings.json` `defaultProvider`/`defaultModel` at ccr. Other
160
+ * keys in `.pi/settings.json` are preserved.
161
+ */
162
+ export declare function applyPiProjectTakeover(projectPath: string, config: Record<string, any>): void;
163
+ /**
164
+ * Disable ccr takeover for a project's pi configuration by clearing the ccr
165
+ * `defaultProvider`/`defaultModel` from `.pi/settings.json` (removing the file
166
+ * if nothing else remains). The shared global provider definition and trust
167
+ * entry are intentionally left in place, since other projects may rely on them.
168
+ */
169
+ export declare function removePiProjectTakeover(projectPath: string): void;
170
+ /** Whether a project's `.pi/settings.json` currently routes pi through ccr. */
171
+ export declare function isPiProjectTakeoverActive(projectPath: string): boolean;
172
+ /**
173
+ * Enable ccr takeover for a project's qwen configuration: trust the folder and
174
+ * write the ccr Anthropic provider/model selection into the project's
175
+ * workspace-scoped `.qwen/settings.json` (self-contained — unlike pi, qwen's
176
+ * workspace settings carry the provider definition too).
177
+ */
178
+ export declare function applyQwenProjectTakeover(projectPath: string, config: Record<string, any>): void;
179
+ /**
180
+ * Disable ccr takeover for a project's qwen configuration by removing the
181
+ * ccr-managed fields from its workspace `.qwen/settings.json` (deleting the file
182
+ * when nothing meaningful remains). The global trust entry is left in place.
183
+ */
184
+ export declare function removeQwenProjectTakeover(projectPath: string): void;
185
+ /** Whether a project's `.qwen/settings.json` currently routes qwen through ccr. */
186
+ export declare function isQwenProjectTakeoverActive(projectPath: string): boolean;
187
+ /**
188
+ * Enable ccr takeover for a project's opencode configuration by writing the ccr
189
+ * Anthropic provider/model into the project's `opencode.json`. opencode has no
190
+ * trust gate, so nothing else is needed.
191
+ */
192
+ export declare function applyOpencodeProjectTakeover(projectPath: string, config: Record<string, any>): void;
193
+ /**
194
+ * Disable ccr takeover for a project's opencode configuration by removing the
195
+ * ccr-managed fields from its `opencode.json` (deleting the file when nothing
196
+ * meaningful remains).
197
+ */
198
+ export declare function removeOpencodeProjectTakeover(projectPath: string): void;
199
+ /** Whether a project's `opencode.json` currently routes opencode through ccr. */
200
+ export declare function isOpencodeProjectTakeoverActive(projectPath: string): boolean;
201
+ /**
202
+ * Clients that support *project-level* ccr takeover (writing a project-scoped
203
+ * config file). Claude Code uses `.claude/settings.local.json`; pi uses
204
+ * `.pi/settings.json`; qwen-code uses `.qwen/settings.json`; opencode uses
205
+ * `opencode.json`. Codex is intentionally excluded — its config
206
+ * (`~/.codex/config.toml`) is global-only, so it can only be taken over from the
207
+ * Clients page, not per project.
208
+ */
209
+ export declare const PROJECT_TAKEOVER_CLIENT_IDS: ClientId[];
210
+ /** Type guard for {@link PROJECT_TAKEOVER_CLIENT_IDS}. */
211
+ export declare function isProjectTakeoverClient(value: string): value is ClientId;
212
+ export declare function listClientStatuses(config: Record<string, any>): ClientStatus[];
213
+ export declare function enableClient(config: Record<string, any>, id: ClientId, options?: ClientOperationOptions): ClientOperationResult;
214
+ export declare function disableClient(config: Record<string, any>, id: ClientId, options?: ClientOperationOptions): ClientOperationResult;
215
+ export declare function restoreClient(config: Record<string, any>, id: ClientId, options?: ClientOperationOptions): ClientOperationResult;
216
+ export declare function applyClientSelection(config: Record<string, any>, enabledIds: string[]): ClientApplyResult;
217
+ export declare function enableConfiguredClients(config: Record<string, any>): ClientApplyResult;
218
+ export declare function disableConfiguredClients(config: Record<string, any>): ClientApplyResult;
219
+ export {};
@@ -3,8 +3,24 @@ export declare const CONFIG_FILE: string;
3
3
  export declare const PLUGINS_DIR: string;
4
4
  export declare const PRESETS_DIR: string;
5
5
  export declare const PID_FILE: string;
6
+ export declare const CLIENT_STATE_FILE: string;
6
7
  export declare const REFERENCE_COUNT_FILE: string;
7
8
  export declare const CLAUDE_PROJECTS_DIR: string;
9
+ /**
10
+ * Convert an absolute project path to the Claude Code project folder id
11
+ * (matches the directory naming convention used under ~/.claude/projects/<id>).
12
+ */
13
+ export declare function getClaudeProjectId(projectPath: string): string;
14
+ /**
15
+ * Get the CCR project-level config directory for a given project path.
16
+ * This is where per-project Router overrides are stored:
17
+ * ~/.claude-code-router/<project-id>/config.json
18
+ */
19
+ export declare function getProjectConfigDir(projectPath?: string): string;
20
+ /**
21
+ * Get the CCR project-level config file path for a given project path.
22
+ */
23
+ export declare function getProjectConfigPath(projectPath?: string): string;
8
24
  export interface DefaultConfig {
9
25
  LOG: boolean;
10
26
  OPENAI_API_KEY: string;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export * from "./constants";
2
+ export * from "./client-integrations";
3
+ export * from "./projectConfig";
2
4
  export * from './preset/types';
3
5
  export * from './preset/sensitiveFields';
4
6
  export * from './preset/merge';