ccjk 14.2.0 → 14.2.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.
Files changed (55) hide show
  1. package/dist/chunks/api-cli.mjs +3 -2
  2. package/dist/chunks/api-config-selector.mjs +8 -6
  3. package/dist/chunks/auto-updater.mjs +1 -1
  4. package/dist/chunks/ccjk-agents.mjs +2 -2
  5. package/dist/chunks/ccjk-all.mjs +6 -6
  6. package/dist/chunks/ccjk-hooks.mjs +2 -2
  7. package/dist/chunks/ccjk-mcp.mjs +5 -5
  8. package/dist/chunks/ccjk-setup.mjs +4 -4
  9. package/dist/chunks/ccjk-skills.mjs +2 -2
  10. package/dist/chunks/ccr.mjs +11 -9
  11. package/dist/chunks/check-updates.mjs +2 -1
  12. package/dist/chunks/claude-code-incremental-manager.mjs +8 -6
  13. package/dist/chunks/claude-config.mjs +594 -62
  14. package/dist/chunks/claude-config2.mjs +62 -0
  15. package/dist/chunks/clavue-config.mjs +1390 -0
  16. package/dist/chunks/code-type-resolver.mjs +1 -1
  17. package/dist/chunks/codex-config-switch.mjs +1 -0
  18. package/dist/chunks/codex-provider-manager.mjs +2 -1
  19. package/dist/chunks/codex.mjs +4 -3
  20. package/dist/chunks/config-switch.mjs +6 -4
  21. package/dist/chunks/config.mjs +7 -1973
  22. package/dist/chunks/config2.mjs +8 -7
  23. package/dist/chunks/config3.mjs +1 -0
  24. package/dist/chunks/doctor.mjs +7 -6
  25. package/dist/chunks/features.mjs +9 -7
  26. package/dist/chunks/index10.mjs +14 -5379
  27. package/dist/chunks/index9.mjs +5379 -14
  28. package/dist/chunks/init.mjs +12 -10
  29. package/dist/chunks/installer.mjs +7 -5
  30. package/dist/chunks/interview.mjs +1 -1
  31. package/dist/chunks/mcp-cli.mjs +23 -22
  32. package/dist/chunks/mcp.mjs +8 -7
  33. package/dist/chunks/package.mjs +1 -1
  34. package/dist/chunks/platform.mjs +1 -1
  35. package/dist/chunks/quick-provider.mjs +7 -5
  36. package/dist/chunks/quick-setup.mjs +7 -5
  37. package/dist/chunks/simple-config.mjs +3 -2
  38. package/dist/chunks/slash-commands.mjs +1 -1
  39. package/dist/chunks/thinking.mjs +1 -1
  40. package/dist/chunks/uninstall.mjs +1 -1
  41. package/dist/chunks/update.mjs +10 -9
  42. package/dist/chunks/version-checker.mjs +1 -1
  43. package/dist/chunks/zero-config.mjs +4 -3
  44. package/dist/cli.mjs +3 -3
  45. package/dist/index.mjs +7 -6
  46. package/dist/shared/{ccjk.DGllfVCZ.mjs → ccjk.BtrioX1Z.mjs} +1 -1
  47. package/dist/shared/{ccjk.DOw7Fawt.mjs → ccjk.C1Be3aJN.mjs} +2 -2
  48. package/dist/shared/{ccjk.BCzOWT1L.mjs → ccjk.C94P8gCY.mjs} +2 -2
  49. package/dist/shared/{ccjk.Cgv_cFVX.mjs → ccjk.CTr4yCZ1.mjs} +2 -2
  50. package/dist/shared/{ccjk.Cv13QsGp.mjs → ccjk.CoCHVXl3.mjs} +1 -1
  51. package/dist/shared/{ccjk.f3TBLJSt.mjs → ccjk.CwGZSTAK.mjs} +7 -7
  52. package/dist/shared/{ccjk.CfKJnpbB.mjs → ccjk.D-magaEx.mjs} +2 -2
  53. package/dist/shared/{ccjk.CbWVbtb9.mjs → ccjk.DhJ1kyDR.mjs} +1 -1
  54. package/dist/shared/{ccjk.zFGcZT7Y.mjs → ccjk.OJKHVSOb.mjs} +1 -1
  55. package/package.json +43 -40
@@ -0,0 +1,1390 @@
1
+ import { chmodSync } from 'node:fs';
2
+ import { ZCF_CONFIG_FILE, ZCF_CONFIG_DIR, CLAVUE_CONFIG_FILE, CLAVUE_CREDENTIALS_FILE, CLAVUE_SETTINGS_FILE } from './constants.mjs';
3
+ import { d as dayjs } from '../shared/ccjk.RyizuzOI.mjs';
4
+ import { readDefaultTomlConfig, createDefaultTomlConfig, writeTomlConfig } from './ccjk-config.mjs';
5
+ import { r as resolveClaudeFamilySettingsTarget, n as normalizeClaudeFamilySettings } from '../shared/ccjk.DDL-4C-k.mjs';
6
+ import { h as clearLegacyTopLevelRuntimeSettings, o as overwriteModelSettings } from './config.mjs';
7
+ import { ensureDir, exists, copyFile } from './fs-operations.mjs';
8
+ import { readJsonConfig, writeJsonConfig } from './json-config.mjs';
9
+ import { j as join } from '../shared/ccjk.bQ7Dh1g4.mjs';
10
+
11
+ class ClaudeCodeConfigManager {
12
+ static CONFIG_FILE = ZCF_CONFIG_FILE;
13
+ static LEGACY_CONFIG_FILE = join(ZCF_CONFIG_DIR, "claude-code-configs.json");
14
+ /**
15
+ * Ensure configuration directory exists
16
+ */
17
+ static ensureConfigDir() {
18
+ ensureDir(ZCF_CONFIG_DIR);
19
+ }
20
+ /**
21
+ * Read TOML configuration
22
+ */
23
+ static readTomlConfig() {
24
+ return readDefaultTomlConfig();
25
+ }
26
+ /**
27
+ * Load TOML configuration, falling back to default when missing
28
+ */
29
+ static loadTomlConfig() {
30
+ const existingConfig = this.readTomlConfig();
31
+ if (existingConfig) {
32
+ return existingConfig;
33
+ }
34
+ return createDefaultTomlConfig();
35
+ }
36
+ /**
37
+ * Migrate legacy JSON-based configuration into TOML storage
38
+ */
39
+ static migrateFromLegacyConfig() {
40
+ if (!exists(this.LEGACY_CONFIG_FILE)) {
41
+ return null;
42
+ }
43
+ try {
44
+ const legacyConfig = readJsonConfig(this.LEGACY_CONFIG_FILE);
45
+ if (!legacyConfig) {
46
+ return null;
47
+ }
48
+ const normalizedProfiles = {};
49
+ const existingKeys = /* @__PURE__ */ new Set();
50
+ let migratedCurrentKey = "";
51
+ Object.entries(legacyConfig.profiles || {}).forEach(([legacyKey, profile]) => {
52
+ const sourceProfile = profile;
53
+ const name = sourceProfile.name?.trim() || legacyKey;
54
+ const baseKey = this.generateProfileId(name);
55
+ let uniqueKey = baseKey || legacyKey;
56
+ let suffix = 2;
57
+ while (existingKeys.has(uniqueKey)) {
58
+ uniqueKey = `${baseKey || legacyKey}-${suffix++}`;
59
+ }
60
+ existingKeys.add(uniqueKey);
61
+ const sanitizedProfile = this.sanitizeProfile({
62
+ ...sourceProfile,
63
+ name
64
+ });
65
+ normalizedProfiles[uniqueKey] = {
66
+ ...sanitizedProfile,
67
+ id: uniqueKey
68
+ };
69
+ if (legacyConfig.currentProfileId === legacyKey || legacyConfig.currentProfileId === sourceProfile.id) {
70
+ migratedCurrentKey = uniqueKey;
71
+ }
72
+ });
73
+ if (!migratedCurrentKey && legacyConfig.currentProfileId) {
74
+ const fallbackKey = this.generateProfileId(legacyConfig.currentProfileId);
75
+ if (existingKeys.has(fallbackKey)) {
76
+ migratedCurrentKey = fallbackKey;
77
+ }
78
+ }
79
+ if (!migratedCurrentKey && existingKeys.size > 0) {
80
+ migratedCurrentKey = Array.from(existingKeys)[0];
81
+ }
82
+ const migratedConfig = {
83
+ currentProfileId: migratedCurrentKey,
84
+ profiles: normalizedProfiles
85
+ };
86
+ this.writeConfig(migratedConfig);
87
+ return migratedConfig;
88
+ } catch (error) {
89
+ console.error("Failed to migrate legacy Claude Code config:", error);
90
+ return null;
91
+ }
92
+ }
93
+ /**
94
+ * Read configuration
95
+ */
96
+ static readConfig() {
97
+ try {
98
+ const tomlConfig = readDefaultTomlConfig();
99
+ if (!tomlConfig || !tomlConfig.claudeCode) {
100
+ return this.migrateFromLegacyConfig();
101
+ }
102
+ const { claudeCode } = tomlConfig;
103
+ const rawProfiles = claudeCode.profiles || {};
104
+ const sanitizedProfiles = Object.fromEntries(
105
+ Object.entries(rawProfiles).map(([key, profile]) => {
106
+ const storedProfile = this.sanitizeProfile({
107
+ ...profile,
108
+ name: profile.name || key
109
+ });
110
+ return [key, { ...storedProfile, id: key }];
111
+ })
112
+ );
113
+ const configData = {
114
+ currentProfileId: claudeCode.currentProfile || "",
115
+ profiles: sanitizedProfiles
116
+ };
117
+ if (Object.keys(configData.profiles).length === 0) {
118
+ const migrated = this.migrateFromLegacyConfig();
119
+ if (migrated) {
120
+ return migrated;
121
+ }
122
+ }
123
+ return configData;
124
+ } catch (error) {
125
+ console.error("Failed to read Claude Code config:", error);
126
+ return null;
127
+ }
128
+ }
129
+ /**
130
+ * Write configuration
131
+ */
132
+ static writeConfig(config) {
133
+ try {
134
+ this.ensureConfigDir();
135
+ const keyMap = /* @__PURE__ */ new Map();
136
+ const sanitizedProfiles = Object.fromEntries(
137
+ Object.entries(config.profiles).map(([key, profile]) => {
138
+ const normalizedName = profile.name?.trim() || key;
139
+ const profileKey = this.generateProfileId(normalizedName);
140
+ keyMap.set(key, profileKey);
141
+ const sanitizedProfile = this.sanitizeProfile({
142
+ ...profile,
143
+ name: normalizedName
144
+ });
145
+ return [profileKey, sanitizedProfile];
146
+ })
147
+ );
148
+ const tomlConfig = this.loadTomlConfig();
149
+ const nextTomlConfig = {
150
+ ...tomlConfig,
151
+ claudeCode: {
152
+ ...tomlConfig.claudeCode,
153
+ currentProfile: keyMap.get(config.currentProfileId) || config.currentProfileId,
154
+ profiles: sanitizedProfiles
155
+ }
156
+ };
157
+ writeTomlConfig(this.CONFIG_FILE, nextTomlConfig);
158
+ } catch (error) {
159
+ console.error("Failed to write Claude Code config:", error);
160
+ throw new Error(`Failed to write config: ${error instanceof Error ? error.message : String(error)}`);
161
+ }
162
+ }
163
+ /**
164
+ * Create empty configuration
165
+ */
166
+ static createEmptyConfig() {
167
+ return {
168
+ currentProfileId: "",
169
+ profiles: {}
170
+ };
171
+ }
172
+ static settingsMatchProfile(settings, profile) {
173
+ const env = settings?.env || {};
174
+ if (!profile) {
175
+ return !settings?.model && !env.ANTHROPIC_MODEL && !env.ANTHROPIC_DEFAULT_HAIKU_MODEL && !env.ANTHROPIC_DEFAULT_SONNET_MODEL && !env.ANTHROPIC_DEFAULT_OPUS_MODEL;
176
+ }
177
+ const expectedPrimary = profile.primaryModel?.trim();
178
+ const expectedHaiku = profile.defaultHaikuModel?.trim();
179
+ const expectedSonnet = profile.defaultSonnetModel?.trim();
180
+ const expectedOpus = profile.defaultOpusModel?.trim();
181
+ const hasExplicitModelConfig = Boolean(expectedPrimary || expectedHaiku || expectedSonnet || expectedOpus);
182
+ if (!hasExplicitModelConfig) {
183
+ return !settings?.model && (env.ANTHROPIC_MODEL === "" || env.ANTHROPIC_MODEL === void 0) && env.ANTHROPIC_DEFAULT_HAIKU_MODEL === void 0 && env.ANTHROPIC_DEFAULT_SONNET_MODEL === void 0 && env.ANTHROPIC_DEFAULT_OPUS_MODEL === void 0;
184
+ }
185
+ const hasAdaptiveRouting = Boolean(expectedHaiku || expectedSonnet || expectedOpus);
186
+ if (hasAdaptiveRouting) {
187
+ return !settings?.model && env.ANTHROPIC_MODEL === void 0 && env.ANTHROPIC_DEFAULT_HAIKU_MODEL === expectedHaiku && env.ANTHROPIC_SMALL_FAST_MODEL === expectedHaiku && env.ANTHROPIC_DEFAULT_SONNET_MODEL === expectedSonnet && env.ANTHROPIC_DEFAULT_OPUS_MODEL === expectedOpus;
188
+ }
189
+ return settings?.model === expectedPrimary && env.ANTHROPIC_MODEL === void 0 && env.ANTHROPIC_DEFAULT_HAIKU_MODEL === expectedHaiku && env.ANTHROPIC_SMALL_FAST_MODEL === expectedHaiku && env.ANTHROPIC_DEFAULT_SONNET_MODEL === expectedSonnet && env.ANTHROPIC_DEFAULT_OPUS_MODEL === expectedOpus;
190
+ }
191
+ static async syncCurrentProfileToSettings() {
192
+ const currentProfile = this.getCurrentProfile();
193
+ await this.applyProfileSettings(currentProfile);
194
+ }
195
+ /**
196
+ * Apply profile settings to Claude Code runtime
197
+ */
198
+ static async applyProfileSettings(profile) {
199
+ const { ensureI18nInitialized, i18n } = await import('./index2.mjs');
200
+ ensureI18nInitialized();
201
+ const target = resolveClaudeFamilySettingsTarget();
202
+ try {
203
+ if (!profile) {
204
+ const { switchToOfficialLogin } = await import('./config.mjs').then(function (n) { return n.k; });
205
+ switchToOfficialLogin(target.codeTool);
206
+ return;
207
+ }
208
+ const { readJsonConfig: readJsonConfig2, writeJsonConfig } = await import('./json-config.mjs');
209
+ const settings = readJsonConfig2(target.settingsFile) || {};
210
+ clearLegacyTopLevelRuntimeSettings(settings);
211
+ if (!settings.env)
212
+ settings.env = {};
213
+ let shouldRestartCcr = false;
214
+ if (profile.authType === "api_key") {
215
+ settings.env.ANTHROPIC_API_KEY = profile.apiKey;
216
+ delete settings.env.ANTHROPIC_AUTH_TOKEN;
217
+ } else if (profile.authType === "auth_token") {
218
+ settings.env.ANTHROPIC_AUTH_TOKEN = profile.apiKey;
219
+ delete settings.env.ANTHROPIC_API_KEY;
220
+ } else if (profile.authType === "ccr_proxy") {
221
+ const { readCcrConfig } = await import('./config2.mjs');
222
+ const ccrConfig = readCcrConfig();
223
+ if (!ccrConfig) {
224
+ throw new Error(i18n.t("ccr:ccrNotConfigured") || "CCR proxy configuration not found");
225
+ }
226
+ const host = ccrConfig.HOST || "127.0.0.1";
227
+ const port = ccrConfig.PORT || 3456;
228
+ const apiKey = ccrConfig.APIKEY || "sk-ccjk-x-ccr";
229
+ settings.env.ANTHROPIC_BASE_URL = `http://${host}:${port}`;
230
+ settings.env.ANTHROPIC_API_KEY = apiKey;
231
+ delete settings.env.ANTHROPIC_AUTH_TOKEN;
232
+ shouldRestartCcr = true;
233
+ }
234
+ if (profile.authType !== "ccr_proxy") {
235
+ if (profile.baseUrl)
236
+ settings.env.ANTHROPIC_BASE_URL = profile.baseUrl;
237
+ else
238
+ delete settings.env.ANTHROPIC_BASE_URL;
239
+ }
240
+ const hasModelConfig = Boolean(
241
+ profile.primaryModel || profile.defaultHaikuModel || profile.defaultSonnetModel || profile.defaultOpusModel
242
+ );
243
+ const modelMode = hasModelConfig ? "override" : "reset";
244
+ overwriteModelSettings(settings, {
245
+ primaryModel: profile.primaryModel,
246
+ haikuModel: profile.defaultHaikuModel,
247
+ sonnetModel: profile.defaultSonnetModel,
248
+ opusModel: profile.defaultOpusModel
249
+ }, modelMode);
250
+ normalizeClaudeFamilySettings(settings);
251
+ writeJsonConfig(target.settingsFile, settings);
252
+ const { setPrimaryApiKey, addCompletedOnboarding } = await import('./claude-config.mjs').then(function (n) { return n.n; });
253
+ setPrimaryApiKey(target.codeTool);
254
+ addCompletedOnboarding(target.codeTool);
255
+ let verifiedSettings = readJsonConfig2(target.settingsFile) || {};
256
+ if (!this.settingsMatchProfile(verifiedSettings, profile)) {
257
+ const repairedSettings = readJsonConfig2(target.settingsFile) || {};
258
+ clearLegacyTopLevelRuntimeSettings(repairedSettings);
259
+ repairedSettings.env = repairedSettings.env || {};
260
+ if (profile?.authType === "api_key") {
261
+ repairedSettings.env.ANTHROPIC_API_KEY = profile.apiKey;
262
+ delete repairedSettings.env.ANTHROPIC_AUTH_TOKEN;
263
+ } else if (profile?.authType === "auth_token") {
264
+ repairedSettings.env.ANTHROPIC_AUTH_TOKEN = profile.apiKey;
265
+ delete repairedSettings.env.ANTHROPIC_API_KEY;
266
+ }
267
+ if (profile?.authType !== "ccr_proxy") {
268
+ if (profile?.baseUrl)
269
+ repairedSettings.env.ANTHROPIC_BASE_URL = profile.baseUrl;
270
+ else
271
+ delete repairedSettings.env.ANTHROPIC_BASE_URL;
272
+ }
273
+ overwriteModelSettings(repairedSettings, {
274
+ primaryModel: profile?.primaryModel,
275
+ haikuModel: profile?.defaultHaikuModel,
276
+ sonnetModel: profile?.defaultSonnetModel,
277
+ opusModel: profile?.defaultOpusModel
278
+ }, profile ? modelMode : "reset");
279
+ normalizeClaudeFamilySettings(repairedSettings);
280
+ writeJsonConfig(target.settingsFile, repairedSettings);
281
+ verifiedSettings = readJsonConfig2(target.settingsFile) || {};
282
+ }
283
+ if (!this.settingsMatchProfile(verifiedSettings, profile)) {
284
+ throw new Error("settings.json verification failed after applying current profile");
285
+ }
286
+ if (shouldRestartCcr) {
287
+ const { runCcrRestart } = await import('./commands.mjs');
288
+ await runCcrRestart();
289
+ }
290
+ } catch (error) {
291
+ const reason = error instanceof Error ? error.message : String(error);
292
+ throw new Error(`${i18n.t("multi-config:failedToApplySettings")}: ${reason}`);
293
+ }
294
+ }
295
+ static async applyCurrentProfile() {
296
+ await this.syncCurrentProfileToSettings();
297
+ }
298
+ /**
299
+ * Remove unsupported fields from profile payload
300
+ */
301
+ static sanitizeProfile(profile) {
302
+ const sanitized = {
303
+ name: profile.name,
304
+ authType: profile.authType
305
+ };
306
+ if (profile.provider)
307
+ sanitized.provider = profile.provider;
308
+ if (profile.apiKey)
309
+ sanitized.apiKey = profile.apiKey;
310
+ if (profile.baseUrl)
311
+ sanitized.baseUrl = profile.baseUrl;
312
+ if (profile.primaryModel)
313
+ sanitized.primaryModel = profile.primaryModel;
314
+ if (profile.defaultHaikuModel)
315
+ sanitized.defaultHaikuModel = profile.defaultHaikuModel;
316
+ if (profile.defaultSonnetModel)
317
+ sanitized.defaultSonnetModel = profile.defaultSonnetModel;
318
+ if (profile.defaultOpusModel)
319
+ sanitized.defaultOpusModel = profile.defaultOpusModel;
320
+ return sanitized;
321
+ }
322
+ /**
323
+ * Backup configuration
324
+ */
325
+ static backupConfig() {
326
+ try {
327
+ if (!exists(this.CONFIG_FILE)) {
328
+ return null;
329
+ }
330
+ const timestamp = dayjs().format("YYYY-MM-DD_HH-mm-ss");
331
+ const backupPath = join(ZCF_CONFIG_DIR, `config.backup.${timestamp}.toml`);
332
+ copyFile(this.CONFIG_FILE, backupPath);
333
+ return backupPath;
334
+ } catch (error) {
335
+ console.error("Failed to backup Claude Code config:", error);
336
+ return null;
337
+ }
338
+ }
339
+ /**
340
+ * Add configuration
341
+ */
342
+ static async addProfile(profile) {
343
+ try {
344
+ const validationErrors = this.validateProfile(profile);
345
+ if (validationErrors.length > 0) {
346
+ return {
347
+ success: false,
348
+ error: `Validation failed: ${validationErrors.join(", ")}`
349
+ };
350
+ }
351
+ const backupPath = this.backupConfig();
352
+ let config = this.readConfig();
353
+ if (!config) {
354
+ config = this.createEmptyConfig();
355
+ }
356
+ if (profile.id && config.profiles[profile.id]) {
357
+ return {
358
+ success: false,
359
+ error: `Profile with ID "${profile.id}" already exists`,
360
+ backupPath: backupPath || void 0
361
+ };
362
+ }
363
+ const normalizedName = profile.name.trim();
364
+ const profileKey = this.generateProfileId(normalizedName);
365
+ const existingNames = Object.values(config.profiles).map((p) => p.name || "");
366
+ if (config.profiles[profileKey] || existingNames.some((name) => name.toLowerCase() === normalizedName.toLowerCase())) {
367
+ return {
368
+ success: false,
369
+ error: `Profile with name "${profile.name}" already exists`,
370
+ backupPath: backupPath || void 0
371
+ };
372
+ }
373
+ const sanitizedProfile = this.sanitizeProfile({
374
+ ...profile,
375
+ name: normalizedName
376
+ });
377
+ const runtimeProfile = {
378
+ ...sanitizedProfile,
379
+ id: profileKey
380
+ };
381
+ config.profiles[profileKey] = runtimeProfile;
382
+ if (!config.currentProfileId) {
383
+ config.currentProfileId = profileKey;
384
+ }
385
+ this.writeConfig(config);
386
+ if (config.currentProfileId === profileKey) {
387
+ await this.syncCurrentProfileToSettings();
388
+ }
389
+ return {
390
+ success: true,
391
+ backupPath: backupPath || void 0,
392
+ addedProfile: runtimeProfile
393
+ };
394
+ } catch (error) {
395
+ return {
396
+ success: false,
397
+ error: error instanceof Error ? error.message : String(error)
398
+ };
399
+ }
400
+ }
401
+ /**
402
+ * Update configuration
403
+ */
404
+ static async updateProfile(id, data) {
405
+ try {
406
+ const validationErrors = this.validateProfile(data, true);
407
+ if (validationErrors.length > 0) {
408
+ return {
409
+ success: false,
410
+ error: `Validation failed: ${validationErrors.join(", ")}`
411
+ };
412
+ }
413
+ const backupPath = this.backupConfig();
414
+ const config = this.readConfig();
415
+ if (!config || !config.profiles[id]) {
416
+ return {
417
+ success: false,
418
+ error: `Profile with ID "${id}" not found`,
419
+ backupPath: backupPath || void 0
420
+ };
421
+ }
422
+ const existingProfile = config.profiles[id];
423
+ const nextName = data.name !== void 0 ? data.name.trim() : existingProfile.name;
424
+ const nextKey = this.generateProfileId(nextName);
425
+ const nameChanged = nextKey !== id;
426
+ if (nameChanged) {
427
+ const duplicateName = Object.entries(config.profiles).some(([key, profile]) => key !== id && (profile.name || "").toLowerCase() === nextName.toLowerCase());
428
+ if (duplicateName || config.profiles[nextKey]) {
429
+ return {
430
+ success: false,
431
+ error: `Profile with name "${data.name}" already exists`,
432
+ backupPath: backupPath || void 0
433
+ };
434
+ }
435
+ }
436
+ const mergedProfile = this.sanitizeProfile({
437
+ ...existingProfile,
438
+ ...data,
439
+ name: nextName
440
+ });
441
+ if (nameChanged) {
442
+ delete config.profiles[id];
443
+ config.profiles[nextKey] = {
444
+ ...mergedProfile,
445
+ id: nextKey
446
+ };
447
+ if (config.currentProfileId === id) {
448
+ config.currentProfileId = nextKey;
449
+ }
450
+ } else {
451
+ config.profiles[id] = {
452
+ ...mergedProfile,
453
+ id
454
+ };
455
+ }
456
+ this.writeConfig(config);
457
+ if (config.currentProfileId === (nameChanged ? nextKey : id)) {
458
+ await this.syncCurrentProfileToSettings();
459
+ }
460
+ return {
461
+ success: true,
462
+ backupPath: backupPath || void 0,
463
+ updatedProfile: {
464
+ ...mergedProfile,
465
+ id: nameChanged ? nextKey : id
466
+ }
467
+ };
468
+ } catch (error) {
469
+ return {
470
+ success: false,
471
+ error: error instanceof Error ? error.message : String(error)
472
+ };
473
+ }
474
+ }
475
+ /**
476
+ * Delete configuration
477
+ */
478
+ static async deleteProfile(id) {
479
+ try {
480
+ const backupPath = this.backupConfig();
481
+ const config = this.readConfig();
482
+ if (!config || !config.profiles[id]) {
483
+ return {
484
+ success: false,
485
+ error: `Profile with ID "${id}" not found`,
486
+ backupPath: backupPath || void 0
487
+ };
488
+ }
489
+ const profileCount = Object.keys(config.profiles).length;
490
+ if (profileCount === 1) {
491
+ return {
492
+ success: false,
493
+ error: "Cannot delete the last profile. At least one profile must remain.",
494
+ backupPath: backupPath || void 0
495
+ };
496
+ }
497
+ delete config.profiles[id];
498
+ if (config.currentProfileId === id) {
499
+ const remainingIds = Object.keys(config.profiles);
500
+ config.currentProfileId = remainingIds[0];
501
+ }
502
+ this.writeConfig(config);
503
+ if (config.currentProfileId) {
504
+ await this.syncCurrentProfileToSettings();
505
+ }
506
+ return {
507
+ success: true,
508
+ backupPath: backupPath || void 0,
509
+ remainingProfiles: Object.entries(config.profiles).map(([key, profile]) => ({
510
+ ...profile,
511
+ id: key
512
+ }))
513
+ };
514
+ } catch (error) {
515
+ return {
516
+ success: false,
517
+ error: error instanceof Error ? error.message : String(error)
518
+ };
519
+ }
520
+ }
521
+ /**
522
+ * Delete multiple configurations
523
+ */
524
+ static async deleteProfiles(ids) {
525
+ try {
526
+ const backupPath = this.backupConfig();
527
+ const config = this.readConfig();
528
+ if (!config) {
529
+ return {
530
+ success: false,
531
+ error: "No configuration found",
532
+ backupPath: backupPath || void 0
533
+ };
534
+ }
535
+ const missingIds = ids.filter((id) => !config.profiles[id]);
536
+ if (missingIds.length > 0) {
537
+ return {
538
+ success: false,
539
+ error: `Profiles not found: ${missingIds.join(", ")}`,
540
+ backupPath: backupPath || void 0
541
+ };
542
+ }
543
+ const remainingCount = Object.keys(config.profiles).length - ids.length;
544
+ if (remainingCount === 0) {
545
+ return {
546
+ success: false,
547
+ error: "Cannot delete all profiles. At least one profile must remain.",
548
+ backupPath: backupPath || void 0
549
+ };
550
+ }
551
+ let newCurrentProfileId;
552
+ ids.forEach((id) => {
553
+ delete config.profiles[id];
554
+ });
555
+ if (ids.includes(config.currentProfileId)) {
556
+ const remainingIds = Object.keys(config.profiles);
557
+ config.currentProfileId = remainingIds[0];
558
+ newCurrentProfileId = config.currentProfileId;
559
+ }
560
+ this.writeConfig(config);
561
+ if (config.currentProfileId) {
562
+ await this.syncCurrentProfileToSettings();
563
+ }
564
+ return {
565
+ success: true,
566
+ backupPath: backupPath || void 0,
567
+ newCurrentProfileId,
568
+ deletedProfiles: ids,
569
+ remainingProfiles: Object.entries(config.profiles).map(([key, profile]) => ({
570
+ ...profile,
571
+ id: key
572
+ }))
573
+ };
574
+ } catch (error) {
575
+ return {
576
+ success: false,
577
+ error: error instanceof Error ? error.message : String(error)
578
+ };
579
+ }
580
+ }
581
+ /**
582
+ * Generate profile ID from name
583
+ */
584
+ static generateProfileId(name) {
585
+ return name.trim().toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "") || "profile";
586
+ }
587
+ /**
588
+ * Switch configuration
589
+ */
590
+ static async switchProfile(id) {
591
+ try {
592
+ const config = this.readConfig();
593
+ if (!config || !config.profiles[id]) {
594
+ return {
595
+ success: false,
596
+ error: "Profile not found"
597
+ };
598
+ }
599
+ if (config.currentProfileId === id) {
600
+ return { success: true };
601
+ }
602
+ config.currentProfileId = id;
603
+ this.writeConfig(config);
604
+ await this.syncCurrentProfileToSettings();
605
+ return { success: true };
606
+ } catch (error) {
607
+ return {
608
+ success: false,
609
+ error: error instanceof Error ? error.message : String(error)
610
+ };
611
+ }
612
+ }
613
+ /**
614
+ * List all configurations
615
+ */
616
+ static listProfiles() {
617
+ const config = this.readConfig();
618
+ if (!config) {
619
+ return [];
620
+ }
621
+ return Object.values(config.profiles);
622
+ }
623
+ /**
624
+ * Get current configuration
625
+ */
626
+ static getCurrentProfile() {
627
+ const config = this.readConfig();
628
+ if (!config || !config.currentProfileId) {
629
+ return null;
630
+ }
631
+ return config.profiles[config.currentProfileId] || null;
632
+ }
633
+ /**
634
+ * Get configuration by ID
635
+ */
636
+ static getProfileById(id) {
637
+ const config = this.readConfig();
638
+ if (!config) {
639
+ return null;
640
+ }
641
+ return config.profiles[id] || null;
642
+ }
643
+ /**
644
+ * Get configuration by name
645
+ */
646
+ static getProfileByName(name) {
647
+ const config = this.readConfig();
648
+ if (!config) {
649
+ return null;
650
+ }
651
+ return Object.values(config.profiles).find((p) => p.name === name) || null;
652
+ }
653
+ /**
654
+ * Sync CCR configuration
655
+ */
656
+ static async syncCcrProfile() {
657
+ try {
658
+ const { readCcrConfig } = await import('./config2.mjs');
659
+ const ccrConfig = readCcrConfig();
660
+ if (!ccrConfig) {
661
+ await this.ensureCcrProfileExists(ccrConfig);
662
+ return;
663
+ }
664
+ await this.ensureCcrProfileExists(ccrConfig);
665
+ } catch (error) {
666
+ console.error("Failed to sync CCR profile:", error);
667
+ }
668
+ }
669
+ /**
670
+ * 确保CCR配置文件存在
671
+ */
672
+ static async ensureCcrProfileExists(ccrConfig) {
673
+ const config = this.readConfig() || this.createEmptyConfig();
674
+ const ccrProfileId = "ccr-proxy";
675
+ const existingCcrProfile = config.profiles[ccrProfileId];
676
+ if (!ccrConfig) {
677
+ if (existingCcrProfile) {
678
+ delete config.profiles[ccrProfileId];
679
+ if (config.currentProfileId === ccrProfileId) {
680
+ const remainingIds = Object.keys(config.profiles);
681
+ config.currentProfileId = remainingIds[0] || "";
682
+ }
683
+ this.writeConfig(config);
684
+ }
685
+ return;
686
+ }
687
+ const host = ccrConfig.HOST || "127.0.0.1";
688
+ const port = ccrConfig.PORT || 3456;
689
+ const apiKey = ccrConfig.APIKEY || "sk-ccjk-x-ccr";
690
+ const baseUrl = `http://${host}:${port}`;
691
+ const ccrProfile = {
692
+ name: "CCR Proxy",
693
+ authType: "ccr_proxy",
694
+ baseUrl,
695
+ apiKey
696
+ };
697
+ config.profiles[ccrProfileId] = {
698
+ ...ccrProfile,
699
+ id: ccrProfileId
700
+ };
701
+ if (!config.currentProfileId) {
702
+ config.currentProfileId = ccrProfileId;
703
+ }
704
+ this.writeConfig(config);
705
+ }
706
+ /**
707
+ * Switch to official login
708
+ */
709
+ static async switchToOfficial() {
710
+ try {
711
+ const config = this.readConfig();
712
+ if (!config) {
713
+ return { success: true };
714
+ }
715
+ config.currentProfileId = "";
716
+ this.writeConfig(config);
717
+ await this.applyProfileSettings(null);
718
+ return { success: true };
719
+ } catch (error) {
720
+ return {
721
+ success: false,
722
+ error: error instanceof Error ? error.message : String(error)
723
+ };
724
+ }
725
+ }
726
+ /**
727
+ * Switch to CCR proxy
728
+ */
729
+ static async switchToCcr() {
730
+ try {
731
+ await this.syncCcrProfile();
732
+ const config = this.readConfig();
733
+ if (!config || !config.profiles["ccr-proxy"]) {
734
+ return {
735
+ success: false,
736
+ error: "CCR proxy configuration not found. Please configure CCR first."
737
+ };
738
+ }
739
+ return await this.switchProfile("ccr-proxy");
740
+ } catch (error) {
741
+ return {
742
+ success: false,
743
+ error: error instanceof Error ? error.message : String(error)
744
+ };
745
+ }
746
+ }
747
+ /**
748
+ * Validate configuration
749
+ */
750
+ static validateProfile(profile, isUpdate = false) {
751
+ const errors = [];
752
+ if (!isUpdate && (!profile.name || typeof profile.name !== "string" || profile.name.trim() === "")) {
753
+ errors.push("Profile name is required");
754
+ }
755
+ if (profile.name && typeof profile.name !== "string") {
756
+ errors.push("Profile name must be a string");
757
+ }
758
+ if (profile.authType && !["api_key", "auth_token", "ccr_proxy"].includes(profile.authType)) {
759
+ errors.push("Invalid auth type. Must be one of: api_key, auth_token, ccr_proxy");
760
+ }
761
+ if (profile.authType === "api_key" || profile.authType === "auth_token") {
762
+ if (!profile.apiKey || typeof profile.apiKey !== "string" || profile.apiKey.trim() === "") {
763
+ errors.push("API key is required for api_key and auth_token types");
764
+ }
765
+ }
766
+ if (profile.baseUrl) {
767
+ try {
768
+ new URL(profile.baseUrl);
769
+ } catch {
770
+ errors.push("Invalid base URL format");
771
+ }
772
+ }
773
+ return errors;
774
+ }
775
+ /**
776
+ * 检查是否为最后一个配置
777
+ */
778
+ static isLastProfile(id) {
779
+ const config = this.readConfig();
780
+ if (!config || !config.profiles[id]) {
781
+ return false;
782
+ }
783
+ return Object.keys(config.profiles).length === 1;
784
+ }
785
+ }
786
+
787
+ const claudeCodeConfigManager = {
788
+ __proto__: null,
789
+ ClaudeCodeConfigManager: ClaudeCodeConfigManager
790
+ };
791
+
792
+ function readClavueConfig() {
793
+ return readJsonConfig(CLAVUE_CONFIG_FILE);
794
+ }
795
+ function writeClavueConfig(config) {
796
+ writeJsonConfig(CLAVUE_CONFIG_FILE, config);
797
+ }
798
+ function normalizeMyclaudeProviderProfile(profile) {
799
+ return {
800
+ ...profile,
801
+ ...describeMyclaudeProviderProfile(profile)
802
+ };
803
+ }
804
+ function normalizeClavueBaseUrl(url) {
805
+ if (typeof url !== "string") {
806
+ return void 0;
807
+ }
808
+ const normalized = url.trim().replace(/\/v1(?:\/messages|\/responses)?\/?$/i, "").replace(/\/+$/, "");
809
+ return normalized || void 0;
810
+ }
811
+ function normalizeClavueAuthType(authType) {
812
+ return authType === "auth_token" ? "auth_token" : "api_key";
813
+ }
814
+ function inferClavueProviderId(provider, baseUrl) {
815
+ const providerId = typeof provider === "string" ? provider.trim() : "";
816
+ if (providerId && providerId !== "ccr_proxy") {
817
+ return providerId;
818
+ }
819
+ if (baseUrl === "https://api.anthropic.com") {
820
+ return "anthropic";
821
+ }
822
+ return "custom";
823
+ }
824
+ function isOpenAiFamilyModel(model) {
825
+ return /^(?:gpt-|o\d|codex|kimi|glm|moonshot|deepseek|qwen|doubao|yi-|minimax)/i.test(model.trim());
826
+ }
827
+ function inferClavueModelMode(profile) {
828
+ if (profile.authType === "ccr_proxy" || profile.mode === "ccr-proxy") {
829
+ return "hybrid_compatible";
830
+ }
831
+ const routedModels = [
832
+ profile.primaryModel,
833
+ profile.model,
834
+ profile.defaultHaikuModel,
835
+ profile.fastModel,
836
+ profile.defaultSonnetModel,
837
+ profile.defaultOpusModel
838
+ ].filter((model) => typeof model === "string" && model.trim().length > 0);
839
+ if (routedModels.length > 0 && routedModels.every(isOpenAiFamilyModel)) {
840
+ return "openai_native";
841
+ }
842
+ if (routedModels.length > 0 && !routedModels.some(isOpenAiFamilyModel)) {
843
+ return "anthropic_native";
844
+ }
845
+ if (routedModels.length > 0) {
846
+ return "hybrid_compatible";
847
+ }
848
+ if (profile.mode === "openai-native") {
849
+ return "openai_native";
850
+ }
851
+ if (!profile.baseUrl || profile.provider === "anthropic" || profile.mode === "official") {
852
+ return "anthropic_native";
853
+ }
854
+ return routedModels.some(isOpenAiFamilyModel) ? "hybrid_compatible" : "anthropic_native";
855
+ }
856
+ function getClavueRoutingPresetId(profile) {
857
+ const mode = inferClavueModelMode(profile);
858
+ if (mode === "openai_native") {
859
+ return "gpt_5_4_codex";
860
+ }
861
+ if (mode === "anthropic_native") {
862
+ return "claude_code_heritage";
863
+ }
864
+ return "custom";
865
+ }
866
+ function isCcjkClavueProfile(profile) {
867
+ return profile.provenance?.kind === "imported" && profile.provenance.sourceId === "ccjk";
868
+ }
869
+ const CCJK_CLAVUE_PROFILE_ID_PREFIX = "ccjk-";
870
+ function getSourceProfileId(profile) {
871
+ const id = typeof profile.id === "string" ? profile.id.trim() : "";
872
+ if (id) {
873
+ return id;
874
+ }
875
+ const name = typeof profile.name === "string" ? profile.name.trim() : "";
876
+ return name || "profile";
877
+ }
878
+ function getCcjkExternalProfileId(profile) {
879
+ const externalProfileId = profile.provenance?.externalProfileId;
880
+ if (typeof externalProfileId === "string" && externalProfileId.trim()) {
881
+ return externalProfileId.trim();
882
+ }
883
+ const profileId = typeof profile.id === "string" ? profile.id.trim() : "";
884
+ if (profileId.startsWith(CCJK_CLAVUE_PROFILE_ID_PREFIX) && profileId.length > CCJK_CLAVUE_PROFILE_ID_PREFIX.length) {
885
+ return profileId.slice(CCJK_CLAVUE_PROFILE_ID_PREFIX.length);
886
+ }
887
+ return profileId || "profile";
888
+ }
889
+ function getCcjkClavueProfileId(externalProfileId) {
890
+ const cleanExternalId = externalProfileId.trim() || "profile";
891
+ return cleanExternalId.startsWith(CCJK_CLAVUE_PROFILE_ID_PREFIX) ? cleanExternalId : `${CCJK_CLAVUE_PROFILE_ID_PREFIX}${cleanExternalId}`;
892
+ }
893
+ function getUniqueClavueProfileId(baseProfileId, reservedProfileIds) {
894
+ if (!reservedProfileIds.has(baseProfileId)) {
895
+ return baseProfileId;
896
+ }
897
+ let index = 2;
898
+ let candidate = `${baseProfileId}-${index}`;
899
+ while (reservedProfileIds.has(candidate)) {
900
+ index += 1;
901
+ candidate = `${baseProfileId}-${index}`;
902
+ }
903
+ return candidate;
904
+ }
905
+ function buildExistingCcjkProfileByExternalId(existingProfiles) {
906
+ const existingByExternalId = /* @__PURE__ */ new Map();
907
+ for (const profile of existingProfiles) {
908
+ if (!isCcjkClavueProfile(profile)) {
909
+ continue;
910
+ }
911
+ const externalProfileId = getCcjkExternalProfileId(profile);
912
+ const current = existingByExternalId.get(externalProfileId);
913
+ if (!current || profile.id.startsWith(CCJK_CLAVUE_PROFILE_ID_PREFIX)) {
914
+ existingByExternalId.set(externalProfileId, profile);
915
+ }
916
+ }
917
+ return existingByExternalId;
918
+ }
919
+ function resolveClavueProviderProfiles(profiles, existingProfiles) {
920
+ const existingCcjkByExternalId = buildExistingCcjkProfileByExternalId(existingProfiles);
921
+ const reservedProfileIds = new Set(existingProfiles.filter((profile) => !isCcjkClavueProfile(profile)).map((profile) => profile.id));
922
+ return profiles.map((profile) => {
923
+ const externalProfileId = getSourceProfileId(profile);
924
+ const existing = existingCcjkByExternalId.get(externalProfileId);
925
+ const canReuseExistingManagedId = Boolean(
926
+ existing && existing.id.startsWith(CCJK_CLAVUE_PROFILE_ID_PREFIX) && !reservedProfileIds.has(existing.id)
927
+ );
928
+ const clavueProfileId = canReuseExistingManagedId ? existing.id : getUniqueClavueProfileId(getCcjkClavueProfileId(externalProfileId), reservedProfileIds);
929
+ reservedProfileIds.add(clavueProfileId);
930
+ return {
931
+ source: profile,
932
+ clavueProfileId,
933
+ existing
934
+ };
935
+ });
936
+ }
937
+ function findResolvedClavueProfile(profiles, profileId) {
938
+ const requestedProfileId = typeof profileId === "string" ? profileId.trim() : "";
939
+ if (!requestedProfileId) {
940
+ return void 0;
941
+ }
942
+ return profiles.find((profile) => {
943
+ return profile.clavueProfileId === requestedProfileId || getSourceProfileId(profile.source) === requestedProfileId;
944
+ });
945
+ }
946
+ function resolveClavueActiveProviderProfileId(config, activeProfileId) {
947
+ const requestedProfileId = typeof activeProfileId === "string" ? activeProfileId.trim() : "";
948
+ if (!requestedProfileId) {
949
+ return void 0;
950
+ }
951
+ const existingProfiles = getClavueProviderProfiles(config);
952
+ if (existingProfiles.some((profile) => profile.id === requestedProfileId)) {
953
+ return requestedProfileId;
954
+ }
955
+ return existingProfiles.find((profile) => {
956
+ return isCcjkClavueProfile(profile) && getCcjkExternalProfileId(profile) === requestedProfileId;
957
+ })?.id || requestedProfileId;
958
+ }
959
+ function createClavueModelRouting(profile) {
960
+ const primaryModel = (profile.primaryModel || profile.model || "").trim();
961
+ const haikuModel = (profile.defaultHaikuModel || profile.fastModel || "").trim();
962
+ const sonnetModel = (profile.defaultSonnetModel || primaryModel).trim();
963
+ const opusModel = (profile.defaultOpusModel || primaryModel).trim();
964
+ const executionModel = sonnetModel || primaryModel;
965
+ return {
966
+ presetId: getClavueRoutingPresetId(profile),
967
+ primaryModel,
968
+ subagentModel: executionModel && executionModel !== primaryModel ? executionModel : "",
969
+ smallFastModel: haikuModel,
970
+ planModel: opusModel || primaryModel,
971
+ exploreModel: executionModel,
972
+ generalModel: executionModel,
973
+ teamModel: executionModel,
974
+ guideModel: opusModel || primaryModel
975
+ };
976
+ }
977
+ function toClavueProviderProfile(profile, clavueProfileId, existing) {
978
+ const now = Date.now();
979
+ const normalizedBaseUrl = normalizeClavueBaseUrl(profile.baseUrl);
980
+ const createdAt = typeof existing?.createdAt === "number" ? existing.createdAt : now;
981
+ return {
982
+ id: clavueProfileId,
983
+ name: profile.name,
984
+ providerId: inferClavueProviderId(profile.provider, normalizedBaseUrl),
985
+ modelMode: inferClavueModelMode(profile),
986
+ ...normalizedBaseUrl ? { baseUrl: normalizedBaseUrl } : {},
987
+ authType: normalizeClavueAuthType(profile.authType),
988
+ modelRouting: createClavueModelRouting(profile),
989
+ provenance: {
990
+ kind: "imported",
991
+ sourceId: "ccjk",
992
+ importedAt: typeof existing?.provenance?.importedAt === "number" ? existing.provenance.importedAt : createdAt,
993
+ externalProfileId: getSourceProfileId(profile)
994
+ },
995
+ createdAt,
996
+ updatedAt: now
997
+ };
998
+ }
999
+ function readClavueCredentialsConfig() {
1000
+ return readJsonConfig(CLAVUE_CREDENTIALS_FILE) || {};
1001
+ }
1002
+ function writeClavueCredentialsConfig(config) {
1003
+ writeJsonConfig(CLAVUE_CREDENTIALS_FILE, config);
1004
+ try {
1005
+ chmodSync(CLAVUE_CREDENTIALS_FILE, 384);
1006
+ } catch {
1007
+ }
1008
+ }
1009
+ function syncClavueProviderCredentials(profiles, replaceProfileIds = /* @__PURE__ */ new Set()) {
1010
+ const existingCredentials = readClavueCredentialsConfig();
1011
+ const providerProfiles = { ...existingCredentials.providerProfiles || {} };
1012
+ for (const profileId of replaceProfileIds) {
1013
+ delete providerProfiles[profileId];
1014
+ }
1015
+ for (const profile of profiles) {
1016
+ const credential = typeof profile.source.apiKey === "string" ? profile.source.apiKey.trim() : "";
1017
+ if (!profile.clavueProfileId || !credential) {
1018
+ continue;
1019
+ }
1020
+ providerProfiles[profile.clavueProfileId] = {
1021
+ credential,
1022
+ authType: normalizeClavueAuthType(profile.source.authType)
1023
+ };
1024
+ }
1025
+ writeClavueCredentialsConfig({
1026
+ ...existingCredentials,
1027
+ providerProfiles: Object.keys(providerProfiles).length > 0 ? providerProfiles : void 0
1028
+ });
1029
+ }
1030
+ function getClavueProviderProfiles(config) {
1031
+ return Array.isArray(config?.clavueProviderProfiles) ? config.clavueProviderProfiles : [];
1032
+ }
1033
+ function getLegacyMyclaudeProviderProfiles(config) {
1034
+ return Array.isArray(config?.myclaudeProviderProfiles) ? config.myclaudeProviderProfiles : [];
1035
+ }
1036
+ function toLegacyProviderProfile(profile) {
1037
+ const routing = profile.modelRouting || createClavueModelRouting({ id: profile.id, name: profile.name, provider: profile.providerId });
1038
+ return {
1039
+ id: isCcjkClavueProfile(profile) ? getCcjkExternalProfileId(profile) : profile.id,
1040
+ name: profile.name,
1041
+ provider: profile.providerId || "custom",
1042
+ baseUrl: profile.baseUrl,
1043
+ model: routing.primaryModel,
1044
+ fastModel: routing.smallFastModel,
1045
+ authType: profile.authType,
1046
+ primaryModel: routing.primaryModel,
1047
+ defaultHaikuModel: routing.smallFastModel,
1048
+ defaultSonnetModel: routing.generalModel || routing.subagentModel,
1049
+ defaultOpusModel: routing.planModel,
1050
+ ...describeMyclaudeProviderProfile({
1051
+ authType: profile.authType,
1052
+ baseUrl: profile.baseUrl,
1053
+ mode: profile.modelMode === "openai_native" ? "openai-native" : profile.modelMode === "hybrid_compatible" ? "ccr-proxy" : "official"
1054
+ })
1055
+ };
1056
+ }
1057
+ function normalizeModelSlot(model) {
1058
+ return typeof model === "string" && model.trim() ? model.trim() : void 0;
1059
+ }
1060
+ function resolveClavueModelSelectionSlots(options) {
1061
+ if (options.reset) {
1062
+ return {};
1063
+ }
1064
+ const selectedModel = normalizeModelSlot(options.selectedModel);
1065
+ if (selectedModel) {
1066
+ return {
1067
+ primaryModel: selectedModel,
1068
+ haikuModel: selectedModel,
1069
+ sonnetModel: selectedModel,
1070
+ opusModel: selectedModel
1071
+ };
1072
+ }
1073
+ const primaryModel = normalizeModelSlot(options.primaryModel);
1074
+ const haikuModel = normalizeModelSlot(options.haikuModel);
1075
+ const sonnetModel = normalizeModelSlot(options.sonnetModel);
1076
+ const opusModel = normalizeModelSlot(options.opusModel);
1077
+ if (primaryModel) {
1078
+ return {
1079
+ primaryModel,
1080
+ haikuModel: haikuModel || primaryModel,
1081
+ sonnetModel: sonnetModel || primaryModel,
1082
+ opusModel: opusModel || primaryModel
1083
+ };
1084
+ }
1085
+ return {
1086
+ haikuModel,
1087
+ sonnetModel,
1088
+ opusModel
1089
+ };
1090
+ }
1091
+ function getClavueActiveProfile(config) {
1092
+ const activeId = config?.clavueActiveProviderProfileId || config?.myclaudeActiveProviderProfileId || "";
1093
+ const legacyProfiles = getLegacyMyclaudeProviderProfiles(config);
1094
+ const activeLegacyProfile = legacyProfiles.find((profile) => profile.id === activeId);
1095
+ if (activeLegacyProfile) {
1096
+ return activeLegacyProfile;
1097
+ }
1098
+ const nativeProfiles = getClavueProviderProfiles(config);
1099
+ const activeNativeProfile = nativeProfiles.find((profile) => profile.id === activeId);
1100
+ if (activeNativeProfile) {
1101
+ return toLegacyProviderProfile(activeNativeProfile);
1102
+ }
1103
+ return nativeProfiles.map(toLegacyProviderProfile).find((profile) => profile.id === activeId) || null;
1104
+ }
1105
+ function syncClavueActiveProviderModelSelection(options) {
1106
+ const config = readClavueConfig();
1107
+ const activeId = config?.clavueActiveProviderProfileId || config?.myclaudeActiveProviderProfileId || "";
1108
+ if (!config || !activeId) {
1109
+ return false;
1110
+ }
1111
+ const slots = resolveClavueModelSelectionSlots(options);
1112
+ const nativeProfiles = getClavueProviderProfiles(config);
1113
+ const nativeProfileIndex = nativeProfiles.findIndex((profile) => profile.id === activeId);
1114
+ if (nativeProfileIndex >= 0) {
1115
+ const currentProfile = nativeProfiles[nativeProfileIndex];
1116
+ const routingProfile = {
1117
+ id: currentProfile.id,
1118
+ name: currentProfile.name,
1119
+ provider: currentProfile.providerId,
1120
+ baseUrl: currentProfile.baseUrl,
1121
+ authType: currentProfile.authType,
1122
+ model: slots.primaryModel,
1123
+ fastModel: slots.haikuModel,
1124
+ primaryModel: slots.primaryModel,
1125
+ defaultHaikuModel: slots.haikuModel,
1126
+ defaultSonnetModel: slots.sonnetModel,
1127
+ defaultOpusModel: slots.opusModel,
1128
+ mode: currentProfile.modelMode === "openai_native" ? "openai-native" : currentProfile.modelMode === "hybrid_compatible" ? "ccr-proxy" : "official"
1129
+ };
1130
+ const nextProfile = {
1131
+ ...currentProfile,
1132
+ modelMode: options.reset ? currentProfile.modelMode : inferClavueModelMode(routingProfile),
1133
+ modelRouting: createClavueModelRouting(routingProfile),
1134
+ updatedAt: Date.now()
1135
+ };
1136
+ config.clavueProviderProfiles = nativeProfiles.map((profile, index) => index === nativeProfileIndex ? nextProfile : profile);
1137
+ delete config.myclaudeProviderProfiles;
1138
+ delete config.myclaudeActiveProviderProfileId;
1139
+ writeClavueConfig(config);
1140
+ syncMyclaudeActiveProfileToSettings(toLegacyProviderProfile(nextProfile));
1141
+ return true;
1142
+ }
1143
+ const legacyProfiles = getLegacyMyclaudeProviderProfiles(config);
1144
+ const legacyProfileIndex = legacyProfiles.findIndex((profile) => profile.id === activeId);
1145
+ if (legacyProfileIndex < 0) {
1146
+ return false;
1147
+ }
1148
+ const updatedProfiles = legacyProfiles.map((profile, index) => {
1149
+ if (index !== legacyProfileIndex) {
1150
+ return profile;
1151
+ }
1152
+ return {
1153
+ ...profile,
1154
+ model: slots.primaryModel,
1155
+ fastModel: slots.haikuModel,
1156
+ primaryModel: slots.primaryModel,
1157
+ defaultHaikuModel: slots.haikuModel,
1158
+ defaultSonnetModel: slots.sonnetModel,
1159
+ defaultOpusModel: slots.opusModel
1160
+ };
1161
+ });
1162
+ setMyclaudeProviderProfiles(updatedProfiles, activeId);
1163
+ return true;
1164
+ }
1165
+ function setMyclaudeProviderProfiles(profiles, activeProfileId) {
1166
+ const config = readClavueConfig() || { mcpServers: {} };
1167
+ const normalizedProfiles = profiles.map(normalizeMyclaudeProviderProfile);
1168
+ const existingProfiles = getClavueProviderProfiles(config);
1169
+ const resolvedProfiles = resolveClavueProviderProfiles(normalizedProfiles, existingProfiles);
1170
+ const selectedProfile = findResolvedClavueProfile(
1171
+ resolvedProfiles,
1172
+ activeProfileId ?? (normalizedProfiles[0] ? getSourceProfileId(normalizedProfiles[0]) : void 0)
1173
+ );
1174
+ const nextActiveProfileId = selectedProfile?.clavueProfileId;
1175
+ const existingCcjkProfileIds = new Set(existingProfiles.filter(isCcjkClavueProfile).map((profile) => profile.id));
1176
+ const preservedProfiles = existingProfiles.filter((profile) => !isCcjkClavueProfile(profile));
1177
+ const preservedProfileIds = new Set(preservedProfiles.map((profile) => profile.id));
1178
+ const replacedCredentialIds = new Set([...existingCcjkProfileIds].filter((profileId) => !preservedProfileIds.has(profileId)));
1179
+ const clavueProfiles = resolvedProfiles.map((profile) => toClavueProviderProfile(
1180
+ profile.source,
1181
+ profile.clavueProfileId,
1182
+ profile.existing
1183
+ ));
1184
+ config.clavueProviderProfiles = [...preservedProfiles, ...clavueProfiles];
1185
+ if (nextActiveProfileId) {
1186
+ config.clavueActiveProviderProfileId = nextActiveProfileId;
1187
+ } else {
1188
+ delete config.clavueActiveProviderProfileId;
1189
+ }
1190
+ delete config.myclaudeProviderProfiles;
1191
+ delete config.myclaudeActiveProviderProfileId;
1192
+ writeClavueConfig(config);
1193
+ syncClavueProviderCredentials(resolvedProfiles, replacedCredentialIds);
1194
+ const activeProfile = selectedProfile?.source || null;
1195
+ syncMyclaudeActiveProfileToSettings(activeProfile);
1196
+ return nextActiveProfileId;
1197
+ }
1198
+ function setMyclaudeActiveProviderProfile(activeProfileId) {
1199
+ const config = readClavueConfig() || { mcpServers: {} };
1200
+ const nextActiveProfileId = resolveClavueActiveProviderProfileId(config, activeProfileId);
1201
+ if (nextActiveProfileId) {
1202
+ config.clavueActiveProviderProfileId = nextActiveProfileId;
1203
+ } else {
1204
+ delete config.clavueActiveProviderProfileId;
1205
+ }
1206
+ delete config.myclaudeActiveProviderProfileId;
1207
+ writeClavueConfig(config);
1208
+ syncMyclaudeActiveProfileToSettings(getClavueActiveProfile(config));
1209
+ }
1210
+ function detectMyclaudeProviderMode(profile) {
1211
+ if (profile.authType === "ccr_proxy") {
1212
+ return "ccr-proxy";
1213
+ }
1214
+ if (profile.baseUrl) {
1215
+ return "openai-native";
1216
+ }
1217
+ return "official";
1218
+ }
1219
+ function describeMyclaudeProviderProfile(profile) {
1220
+ const mode = profile.mode || detectMyclaudeProviderMode({
1221
+ authType: profile.authType,
1222
+ baseUrl: profile.baseUrl
1223
+ });
1224
+ return { mode };
1225
+ }
1226
+ function buildMyclaudeProviderPresentation(profile) {
1227
+ const mode = profile.mode || detectMyclaudeProviderMode({
1228
+ authType: profile.authType,
1229
+ baseUrl: profile.baseUrl
1230
+ });
1231
+ const hasAdaptiveRouting = Boolean(profile.defaultHaikuModel || profile.defaultSonnetModel || profile.defaultOpusModel);
1232
+ const hasPrimaryModel = Boolean(profile.primaryModel || profile.model);
1233
+ const modeLabel = mode === "ccr-proxy" ? "CCR-proxy" : mode === "openai-native" ? "OpenAI-native" : "Anthropic-native";
1234
+ const routeLabel = mode === "ccr-proxy" ? profile.baseUrl ? `Claude-family route through CCR \xB7 ${profile.baseUrl}` : "Claude-family route through CCR" : mode === "openai-native" ? profile.baseUrl ? `OpenAI-family route through a compatible gateway \xB7 ${profile.baseUrl}` : "OpenAI-family route through a compatible gateway" : "Official Anthropic route";
1235
+ const strategyLabel = hasAdaptiveRouting ? "Custom routing \xB7 Advanced custom routing. Validate carefully when mixing model families." : hasPrimaryModel ? "Single-model override \xB7 Primary model is pinned for the active profile." : "Native runtime default \xB7 Runtime follows the official provider defaults.";
1236
+ return {
1237
+ modeLabel,
1238
+ sourceLabel: "Imported from ccjk \xB7 Reusable profile imported from the compatible ccjk configuration.",
1239
+ routeLabel,
1240
+ strategyLabel
1241
+ };
1242
+ }
1243
+ function toMyclaudeProviderProfile(profile, existing) {
1244
+ return {
1245
+ id: profile.id || existing?.id || profile.name,
1246
+ name: profile.name,
1247
+ provider: profile.provider || existing?.provider || "custom",
1248
+ apiKey: profile.apiKey,
1249
+ baseUrl: profile.baseUrl,
1250
+ model: profile.primaryModel,
1251
+ fastModel: profile.defaultHaikuModel,
1252
+ authType: profile.authType,
1253
+ primaryModel: profile.primaryModel,
1254
+ defaultHaikuModel: profile.defaultHaikuModel,
1255
+ defaultSonnetModel: profile.defaultSonnetModel,
1256
+ defaultOpusModel: profile.defaultOpusModel,
1257
+ ...describeMyclaudeProviderProfile({
1258
+ authType: profile.authType,
1259
+ baseUrl: profile.baseUrl,
1260
+ mode: existing?.mode
1261
+ })
1262
+ };
1263
+ }
1264
+ function syncMyclaudeActiveProfileToSettings(profile) {
1265
+ const settings = readJsonConfig(CLAVUE_SETTINGS_FILE) || {};
1266
+ settings.env = settings.env || {};
1267
+ clearLegacyTopLevelRuntimeSettings(settings);
1268
+ delete settings.env.ANTHROPIC_API_KEY;
1269
+ delete settings.env.ANTHROPIC_AUTH_TOKEN;
1270
+ if (profile?.baseUrl) {
1271
+ settings.env.ANTHROPIC_BASE_URL = normalizeClavueBaseUrl(profile.baseUrl);
1272
+ } else {
1273
+ delete settings.env.ANTHROPIC_BASE_URL;
1274
+ }
1275
+ overwriteModelSettings(settings, {
1276
+ primaryModel: typeof profile?.primaryModel === "string" ? profile.primaryModel : typeof profile?.model === "string" ? profile.model : void 0,
1277
+ haikuModel: typeof profile?.defaultHaikuModel === "string" ? profile.defaultHaikuModel : typeof profile?.fastModel === "string" ? profile.fastModel : void 0,
1278
+ sonnetModel: typeof profile?.defaultSonnetModel === "string" ? profile.defaultSonnetModel : void 0,
1279
+ opusModel: typeof profile?.defaultOpusModel === "string" ? profile.defaultOpusModel : void 0
1280
+ }, profile ? "override" : "reset");
1281
+ const primaryModel = typeof profile?.primaryModel === "string" ? profile.primaryModel.trim() : typeof profile?.model === "string" ? profile.model.trim() : "";
1282
+ const subagentModel = typeof profile?.defaultSonnetModel === "string" ? profile.defaultSonnetModel.trim() : "";
1283
+ const hasAdaptiveRouting = Boolean(
1284
+ typeof profile?.defaultHaikuModel === "string" && profile.defaultHaikuModel.trim() || typeof profile?.defaultSonnetModel === "string" && profile.defaultSonnetModel.trim() || typeof profile?.defaultOpusModel === "string" && profile.defaultOpusModel.trim()
1285
+ );
1286
+ if (primaryModel && !hasAdaptiveRouting) {
1287
+ settings.env.ANTHROPIC_MODEL = primaryModel;
1288
+ settings.env.ANTHROPIC_CUSTOM_MODEL_OPTION = primaryModel;
1289
+ settings.model = primaryModel;
1290
+ } else if (primaryModel) {
1291
+ settings.env.ANTHROPIC_CUSTOM_MODEL_OPTION = primaryModel;
1292
+ delete settings.env.ANTHROPIC_MODEL;
1293
+ delete settings.model;
1294
+ } else if (profile) {
1295
+ delete settings.env.ANTHROPIC_MODEL;
1296
+ delete settings.env.ANTHROPIC_CUSTOM_MODEL_OPTION;
1297
+ delete settings.model;
1298
+ } else {
1299
+ delete settings.env.ANTHROPIC_CUSTOM_MODEL_OPTION;
1300
+ delete settings.model;
1301
+ }
1302
+ if (subagentModel) {
1303
+ settings.env.CLAUDE_CODE_SUBAGENT_MODEL = subagentModel;
1304
+ } else {
1305
+ delete settings.env.CLAUDE_CODE_SUBAGENT_MODEL;
1306
+ }
1307
+ normalizeClaudeFamilySettings(settings);
1308
+ writeJsonConfig(CLAVUE_SETTINGS_FILE, settings);
1309
+ }
1310
+ function syncMyclaudeProviderProfilesFromClaudeConfig(configData) {
1311
+ if (!configData) {
1312
+ clearMyclaudeProviderProfiles();
1313
+ return {
1314
+ activeProfileId: "",
1315
+ activeProfile: null,
1316
+ profiles: []
1317
+ };
1318
+ }
1319
+ const existingConfig = readClavueConfig();
1320
+ const existingProfiles = getLegacyMyclaudeProviderProfiles(existingConfig).length > 0 ? getLegacyMyclaudeProviderProfiles(existingConfig) : getClavueProviderProfiles(existingConfig).map(toLegacyProviderProfile);
1321
+ const existingById = new Map(existingProfiles.map((profile) => [String(profile.id), profile]));
1322
+ const profiles = Object.entries(configData.profiles).map(([id, profile]) => toMyclaudeProviderProfile({ ...profile, id }, existingById.get(id)));
1323
+ const activeProfileId = configData.currentProfileId ?? "";
1324
+ const activeProfile = profiles.find((profile) => profile.id === activeProfileId) || null;
1325
+ const activeClavueProfileId = setMyclaudeProviderProfiles(profiles, activeProfileId);
1326
+ return {
1327
+ activeProfileId: activeClavueProfileId || "",
1328
+ activeProfile,
1329
+ profiles
1330
+ };
1331
+ }
1332
+ function syncMyclaudeProviderProfilesFromCurrentClaudeConfig() {
1333
+ const configData = ClaudeCodeConfigManager.readConfig();
1334
+ return syncMyclaudeProviderProfilesFromClaudeConfig(configData);
1335
+ }
1336
+ function clearMyclaudeProviderProfiles() {
1337
+ const config = readClavueConfig();
1338
+ let preservedActiveProfile = null;
1339
+ if (config) {
1340
+ const existingProfiles = getClavueProviderProfiles(config);
1341
+ const removedIds = /* @__PURE__ */ new Set([
1342
+ ...existingProfiles.filter(isCcjkClavueProfile).map((profile) => profile.id),
1343
+ ...getLegacyMyclaudeProviderProfiles(config).map((profile) => profile.id)
1344
+ ]);
1345
+ const preservedProfiles = existingProfiles.filter((profile) => !removedIds.has(profile.id));
1346
+ const currentActiveId = config.clavueActiveProviderProfileId || config.myclaudeActiveProviderProfileId;
1347
+ const nextActiveId = preservedProfiles.some((profile) => profile.id === currentActiveId) ? currentActiveId : preservedProfiles[0]?.id;
1348
+ if (preservedProfiles.length > 0) {
1349
+ config.clavueProviderProfiles = preservedProfiles;
1350
+ config.clavueActiveProviderProfileId = nextActiveId;
1351
+ preservedActiveProfile = preservedProfiles.find((profile) => profile.id === nextActiveId) ? toLegacyProviderProfile(preservedProfiles.find((profile) => profile.id === nextActiveId)) : null;
1352
+ } else {
1353
+ delete config.clavueProviderProfiles;
1354
+ delete config.clavueActiveProviderProfileId;
1355
+ }
1356
+ delete config.myclaudeProviderProfiles;
1357
+ delete config.myclaudeActiveProviderProfileId;
1358
+ writeClavueConfig(config);
1359
+ if (removedIds.size > 0) {
1360
+ const credentials = readClavueCredentialsConfig();
1361
+ if (credentials.providerProfiles) {
1362
+ const nextProviderProfiles = { ...credentials.providerProfiles };
1363
+ for (const profileId of removedIds) {
1364
+ delete nextProviderProfiles[profileId];
1365
+ }
1366
+ writeClavueCredentialsConfig({
1367
+ ...credentials,
1368
+ providerProfiles: Object.keys(nextProviderProfiles).length > 0 ? nextProviderProfiles : void 0
1369
+ });
1370
+ }
1371
+ }
1372
+ }
1373
+ syncMyclaudeActiveProfileToSettings(preservedActiveProfile);
1374
+ }
1375
+
1376
+ const clavueConfig = {
1377
+ __proto__: null,
1378
+ buildMyclaudeProviderPresentation: buildMyclaudeProviderPresentation,
1379
+ clearMyclaudeProviderProfiles: clearMyclaudeProviderProfiles,
1380
+ describeMyclaudeProviderProfile: describeMyclaudeProviderProfile,
1381
+ readClavueConfig: readClavueConfig,
1382
+ setMyclaudeActiveProviderProfile: setMyclaudeActiveProviderProfile,
1383
+ setMyclaudeProviderProfiles: setMyclaudeProviderProfiles,
1384
+ syncClavueActiveProviderModelSelection: syncClavueActiveProviderModelSelection,
1385
+ syncMyclaudeProviderProfilesFromClaudeConfig: syncMyclaudeProviderProfilesFromClaudeConfig,
1386
+ syncMyclaudeProviderProfilesFromCurrentClaudeConfig: syncMyclaudeProviderProfilesFromCurrentClaudeConfig,
1387
+ writeClavueConfig: writeClavueConfig
1388
+ };
1389
+
1390
+ export { ClaudeCodeConfigManager as C, setMyclaudeActiveProviderProfile as a, syncMyclaudeProviderProfilesFromClaudeConfig as b, setMyclaudeProviderProfiles as c, clearMyclaudeProviderProfiles as d, buildMyclaudeProviderPresentation as e, claudeCodeConfigManager as f, clavueConfig as g, syncClavueActiveProviderModelSelection as s };