ccjk 14.2.0 → 14.2.2

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