@tkpdx01/ccc 1.2.5 → 1.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +34 -0
- package/Claude Code settings.txt +776 -776
- package/index.js +4 -4
- package/nul +1 -0
- package/package.json +39 -39
- package/settings-sample.json +4 -0
- package/src/commands/delete.js +66 -66
- package/src/commands/edit.js +120 -106
- package/src/commands/help.js +50 -52
- package/src/commands/import.js +356 -365
- package/src/commands/index.js +10 -10
- package/src/commands/list.js +46 -58
- package/src/commands/new.js +109 -143
- package/src/commands/show.js +68 -68
- package/src/commands/sync.js +93 -168
- package/src/commands/use.js +19 -19
- package/src/config.js +9 -9
- package/src/launch.js +69 -69
- package/src/parsers.js +154 -154
- package/src/profiles.js +182 -123
- package/src/utils.js +67 -82
package/src/parsers.js
CHANGED
|
@@ -1,154 +1,154 @@
|
|
|
1
|
-
// 解析 CC-Switch SQL 导出文件
|
|
2
|
-
export function parseCCSwitchSQL(content) {
|
|
3
|
-
const providers = [];
|
|
4
|
-
// 匹配 INSERT INTO "providers" 语句
|
|
5
|
-
const insertRegex = /INSERT INTO "providers" \([^)]+\) VALUES \(([^;]+)\);/g;
|
|
6
|
-
let match;
|
|
7
|
-
|
|
8
|
-
while ((match = insertRegex.exec(content)) !== null) {
|
|
9
|
-
try {
|
|
10
|
-
const valuesStr = match[1];
|
|
11
|
-
// 解析 VALUES 中的各个字段
|
|
12
|
-
// 格式: 'id', 'app_type', 'name', 'settings_config', 'website_url', ...
|
|
13
|
-
const values = [];
|
|
14
|
-
let current = '';
|
|
15
|
-
let inQuote = false;
|
|
16
|
-
let quoteChar = '';
|
|
17
|
-
let depth = 0;
|
|
18
|
-
|
|
19
|
-
for (let i = 0; i < valuesStr.length; i++) {
|
|
20
|
-
const char = valuesStr[i];
|
|
21
|
-
|
|
22
|
-
if (!inQuote && (char === "'" || char === '"')) {
|
|
23
|
-
inQuote = true;
|
|
24
|
-
quoteChar = char;
|
|
25
|
-
current += char;
|
|
26
|
-
} else if (inQuote && char === quoteChar && valuesStr[i-1] !== '\\') {
|
|
27
|
-
// 检查是否是转义的引号 ''
|
|
28
|
-
if (valuesStr[i+1] === quoteChar) {
|
|
29
|
-
current += char;
|
|
30
|
-
i++; // 跳过下一个引号
|
|
31
|
-
current += valuesStr[i];
|
|
32
|
-
} else {
|
|
33
|
-
inQuote = false;
|
|
34
|
-
quoteChar = '';
|
|
35
|
-
current += char;
|
|
36
|
-
}
|
|
37
|
-
} else if (!inQuote && char === ',' && depth === 0) {
|
|
38
|
-
values.push(current.trim());
|
|
39
|
-
current = '';
|
|
40
|
-
} else {
|
|
41
|
-
current += char;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
if (current.trim()) {
|
|
45
|
-
values.push(current.trim());
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// 清理值(去除引号)
|
|
49
|
-
const cleanValue = (v) => {
|
|
50
|
-
if (!v || v === 'NULL') return null;
|
|
51
|
-
if ((v.startsWith("'") && v.endsWith("'")) || (v.startsWith('"') && v.endsWith('"'))) {
|
|
52
|
-
return v.slice(1, -1).replace(/''/g, "'");
|
|
53
|
-
}
|
|
54
|
-
return v;
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const id = cleanValue(values[0]);
|
|
58
|
-
const appType = cleanValue(values[1]);
|
|
59
|
-
const name = cleanValue(values[2]);
|
|
60
|
-
const settingsConfigStr = cleanValue(values[3]);
|
|
61
|
-
const websiteUrl = cleanValue(values[4]);
|
|
62
|
-
|
|
63
|
-
// 只处理 claude 类型
|
|
64
|
-
if (appType === 'claude' && settingsConfigStr) {
|
|
65
|
-
try {
|
|
66
|
-
const settingsConfig = JSON.parse(settingsConfigStr);
|
|
67
|
-
providers.push({
|
|
68
|
-
id,
|
|
69
|
-
name,
|
|
70
|
-
websiteUrl,
|
|
71
|
-
settingsConfig
|
|
72
|
-
});
|
|
73
|
-
} catch (e) {
|
|
74
|
-
// JSON 解析失败,跳过
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
} catch (e) {
|
|
78
|
-
// 解析失败,跳过
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return providers;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// 解析 All API Hub JSON 导出文件
|
|
86
|
-
export function parseAllApiHubJSON(content) {
|
|
87
|
-
try {
|
|
88
|
-
const data = JSON.parse(content);
|
|
89
|
-
const accounts = data.accounts?.accounts || [];
|
|
90
|
-
|
|
91
|
-
return accounts.map(account => {
|
|
92
|
-
// 从 site_url 提取 base URL
|
|
93
|
-
let baseUrl = account.site_url;
|
|
94
|
-
if (!baseUrl.startsWith('http')) {
|
|
95
|
-
baseUrl = 'https://' + baseUrl;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// access_token 需要解码(Base64)然后作为 API key
|
|
99
|
-
let apiKey = '';
|
|
100
|
-
if (account.account_info?.access_token) {
|
|
101
|
-
// All API Hub 的 access_token 是加密的,我们使用原始值
|
|
102
|
-
// 实际上需要生成 sk- 格式的 token
|
|
103
|
-
// 这里我们用 site_url + username 来生成一个标识
|
|
104
|
-
apiKey = `sk-${account.account_info.access_token.replace(/[^a-zA-Z0-9]/g, '')}`;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return {
|
|
108
|
-
id: account.id,
|
|
109
|
-
name: account.site_name,
|
|
110
|
-
websiteUrl: baseUrl,
|
|
111
|
-
settingsConfig: {
|
|
112
|
-
env: {
|
|
113
|
-
ANTHROPIC_AUTH_TOKEN: apiKey,
|
|
114
|
-
ANTHROPIC_BASE_URL: baseUrl
|
|
115
|
-
}
|
|
116
|
-
},
|
|
117
|
-
// 额外的元数据
|
|
118
|
-
meta: {
|
|
119
|
-
siteType: account.site_type,
|
|
120
|
-
health: account.health?.status,
|
|
121
|
-
quota: account.account_info?.quota,
|
|
122
|
-
username: account.account_info?.username
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
});
|
|
126
|
-
} catch (e) {
|
|
127
|
-
return [];
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// 检测文件格式
|
|
132
|
-
export function detectFileFormat(content) {
|
|
133
|
-
// 检测 CC-Switch SQL 格式
|
|
134
|
-
if (content.includes('INSERT INTO "providers"') && content.includes('app_type')) {
|
|
135
|
-
return 'ccswitch';
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// 检测 All API Hub JSON 格式
|
|
139
|
-
try {
|
|
140
|
-
const data = JSON.parse(content);
|
|
141
|
-
if (data.accounts?.accounts && Array.isArray(data.accounts.accounts)) {
|
|
142
|
-
// 检查是否有 All API Hub 特有的字段
|
|
143
|
-
const firstAccount = data.accounts.accounts[0];
|
|
144
|
-
if (firstAccount && (firstAccount.site_name || firstAccount.site_url || firstAccount.account_info)) {
|
|
145
|
-
return 'allapihub';
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
} catch {
|
|
149
|
-
// 不是有效的 JSON
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return null;
|
|
153
|
-
}
|
|
154
|
-
|
|
1
|
+
// 解析 CC-Switch SQL 导出文件
|
|
2
|
+
export function parseCCSwitchSQL(content) {
|
|
3
|
+
const providers = [];
|
|
4
|
+
// 匹配 INSERT INTO "providers" 语句
|
|
5
|
+
const insertRegex = /INSERT INTO "providers" \([^)]+\) VALUES \(([^;]+)\);/g;
|
|
6
|
+
let match;
|
|
7
|
+
|
|
8
|
+
while ((match = insertRegex.exec(content)) !== null) {
|
|
9
|
+
try {
|
|
10
|
+
const valuesStr = match[1];
|
|
11
|
+
// 解析 VALUES 中的各个字段
|
|
12
|
+
// 格式: 'id', 'app_type', 'name', 'settings_config', 'website_url', ...
|
|
13
|
+
const values = [];
|
|
14
|
+
let current = '';
|
|
15
|
+
let inQuote = false;
|
|
16
|
+
let quoteChar = '';
|
|
17
|
+
let depth = 0;
|
|
18
|
+
|
|
19
|
+
for (let i = 0; i < valuesStr.length; i++) {
|
|
20
|
+
const char = valuesStr[i];
|
|
21
|
+
|
|
22
|
+
if (!inQuote && (char === "'" || char === '"')) {
|
|
23
|
+
inQuote = true;
|
|
24
|
+
quoteChar = char;
|
|
25
|
+
current += char;
|
|
26
|
+
} else if (inQuote && char === quoteChar && valuesStr[i-1] !== '\\') {
|
|
27
|
+
// 检查是否是转义的引号 ''
|
|
28
|
+
if (valuesStr[i+1] === quoteChar) {
|
|
29
|
+
current += char;
|
|
30
|
+
i++; // 跳过下一个引号
|
|
31
|
+
current += valuesStr[i];
|
|
32
|
+
} else {
|
|
33
|
+
inQuote = false;
|
|
34
|
+
quoteChar = '';
|
|
35
|
+
current += char;
|
|
36
|
+
}
|
|
37
|
+
} else if (!inQuote && char === ',' && depth === 0) {
|
|
38
|
+
values.push(current.trim());
|
|
39
|
+
current = '';
|
|
40
|
+
} else {
|
|
41
|
+
current += char;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (current.trim()) {
|
|
45
|
+
values.push(current.trim());
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 清理值(去除引号)
|
|
49
|
+
const cleanValue = (v) => {
|
|
50
|
+
if (!v || v === 'NULL') return null;
|
|
51
|
+
if ((v.startsWith("'") && v.endsWith("'")) || (v.startsWith('"') && v.endsWith('"'))) {
|
|
52
|
+
return v.slice(1, -1).replace(/''/g, "'");
|
|
53
|
+
}
|
|
54
|
+
return v;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const id = cleanValue(values[0]);
|
|
58
|
+
const appType = cleanValue(values[1]);
|
|
59
|
+
const name = cleanValue(values[2]);
|
|
60
|
+
const settingsConfigStr = cleanValue(values[3]);
|
|
61
|
+
const websiteUrl = cleanValue(values[4]);
|
|
62
|
+
|
|
63
|
+
// 只处理 claude 类型
|
|
64
|
+
if (appType === 'claude' && settingsConfigStr) {
|
|
65
|
+
try {
|
|
66
|
+
const settingsConfig = JSON.parse(settingsConfigStr);
|
|
67
|
+
providers.push({
|
|
68
|
+
id,
|
|
69
|
+
name,
|
|
70
|
+
websiteUrl,
|
|
71
|
+
settingsConfig
|
|
72
|
+
});
|
|
73
|
+
} catch (e) {
|
|
74
|
+
// JSON 解析失败,跳过
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
} catch (e) {
|
|
78
|
+
// 解析失败,跳过
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return providers;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 解析 All API Hub JSON 导出文件
|
|
86
|
+
export function parseAllApiHubJSON(content) {
|
|
87
|
+
try {
|
|
88
|
+
const data = JSON.parse(content);
|
|
89
|
+
const accounts = data.accounts?.accounts || [];
|
|
90
|
+
|
|
91
|
+
return accounts.map(account => {
|
|
92
|
+
// 从 site_url 提取 base URL
|
|
93
|
+
let baseUrl = account.site_url;
|
|
94
|
+
if (!baseUrl.startsWith('http')) {
|
|
95
|
+
baseUrl = 'https://' + baseUrl;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// access_token 需要解码(Base64)然后作为 API key
|
|
99
|
+
let apiKey = '';
|
|
100
|
+
if (account.account_info?.access_token) {
|
|
101
|
+
// All API Hub 的 access_token 是加密的,我们使用原始值
|
|
102
|
+
// 实际上需要生成 sk- 格式的 token
|
|
103
|
+
// 这里我们用 site_url + username 来生成一个标识
|
|
104
|
+
apiKey = `sk-${account.account_info.access_token.replace(/[^a-zA-Z0-9]/g, '')}`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
id: account.id,
|
|
109
|
+
name: account.site_name,
|
|
110
|
+
websiteUrl: baseUrl,
|
|
111
|
+
settingsConfig: {
|
|
112
|
+
env: {
|
|
113
|
+
ANTHROPIC_AUTH_TOKEN: apiKey,
|
|
114
|
+
ANTHROPIC_BASE_URL: baseUrl
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
// 额外的元数据
|
|
118
|
+
meta: {
|
|
119
|
+
siteType: account.site_type,
|
|
120
|
+
health: account.health?.status,
|
|
121
|
+
quota: account.account_info?.quota,
|
|
122
|
+
username: account.account_info?.username
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
});
|
|
126
|
+
} catch (e) {
|
|
127
|
+
return [];
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// 检测文件格式
|
|
132
|
+
export function detectFileFormat(content) {
|
|
133
|
+
// 检测 CC-Switch SQL 格式
|
|
134
|
+
if (content.includes('INSERT INTO "providers"') && content.includes('app_type')) {
|
|
135
|
+
return 'ccswitch';
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// 检测 All API Hub JSON 格式
|
|
139
|
+
try {
|
|
140
|
+
const data = JSON.parse(content);
|
|
141
|
+
if (data.accounts?.accounts && Array.isArray(data.accounts.accounts)) {
|
|
142
|
+
// 检查是否有 All API Hub 特有的字段
|
|
143
|
+
const firstAccount = data.accounts.accounts[0];
|
|
144
|
+
if (firstAccount && (firstAccount.site_name || firstAccount.site_url || firstAccount.account_info)) {
|
|
145
|
+
return 'allapihub';
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
} catch {
|
|
149
|
+
// 不是有效的 JSON
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
|
package/src/profiles.js
CHANGED
|
@@ -1,123 +1,182 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import { CONFIG_DIR, PROFILES_DIR, DEFAULT_FILE, CLAUDE_SETTINGS_PATH } from './config.js';
|
|
4
|
-
|
|
5
|
-
// 确保目录存在
|
|
6
|
-
export function ensureDirs() {
|
|
7
|
-
if (!fs.existsSync(CONFIG_DIR)) {
|
|
8
|
-
fs.mkdirSync(CONFIG_DIR, { recursive: true });
|
|
9
|
-
}
|
|
10
|
-
if (!fs.existsSync(PROFILES_DIR)) {
|
|
11
|
-
fs.mkdirSync(PROFILES_DIR, { recursive: true });
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// 获取所有 profiles(按 a-z 排序)
|
|
16
|
-
export function getProfiles() {
|
|
17
|
-
ensureDirs();
|
|
18
|
-
const files = fs.readdirSync(PROFILES_DIR);
|
|
19
|
-
return files
|
|
20
|
-
.filter(f => f.endsWith('.json'))
|
|
21
|
-
.map(f => f.replace('.json', ''))
|
|
22
|
-
.sort((a, b) => a.localeCompare(b, 'zh-CN', { sensitivity: 'base' }));
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// 获取带序号的 profiles 映射 { 序号: profileName }
|
|
26
|
-
export function getProfilesWithIndex() {
|
|
27
|
-
const profiles = getProfiles();
|
|
28
|
-
const map = {};
|
|
29
|
-
profiles.forEach((p, i) => {
|
|
30
|
-
map[i + 1] = p;
|
|
31
|
-
});
|
|
32
|
-
return { profiles, map };
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// 根据序号或名称解析 profile
|
|
36
|
-
export function resolveProfile(input) {
|
|
37
|
-
const { profiles, map } = getProfilesWithIndex();
|
|
38
|
-
|
|
39
|
-
// 尝试作为数字序号
|
|
40
|
-
const num = parseInt(input, 10);
|
|
41
|
-
if (!isNaN(num) && map[num]) {
|
|
42
|
-
return map[num];
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// 作为名称
|
|
46
|
-
if (profiles.includes(input)) {
|
|
47
|
-
return input;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return null;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// 获取默认 profile
|
|
54
|
-
export function getDefaultProfile() {
|
|
55
|
-
if (fs.existsSync(DEFAULT_FILE)) {
|
|
56
|
-
return fs.readFileSync(DEFAULT_FILE, 'utf-8').trim();
|
|
57
|
-
}
|
|
58
|
-
return null;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// 设置默认 profile
|
|
62
|
-
export function setDefaultProfile(name) {
|
|
63
|
-
ensureDirs();
|
|
64
|
-
fs.writeFileSync(DEFAULT_FILE, name);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// 获取 profile 路径
|
|
68
|
-
export function getProfilePath(name) {
|
|
69
|
-
return path.join(PROFILES_DIR, `${name}.json`);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// 检查 profile 是否存在
|
|
73
|
-
export function profileExists(name) {
|
|
74
|
-
return fs.existsSync(getProfilePath(name));
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// 获取 Claude 默认 settings 模板
|
|
78
|
-
export function getClaudeSettingsTemplate() {
|
|
79
|
-
if (fs.existsSync(CLAUDE_SETTINGS_PATH)) {
|
|
80
|
-
try {
|
|
81
|
-
return JSON.parse(fs.readFileSync(CLAUDE_SETTINGS_PATH, 'utf-8'));
|
|
82
|
-
} catch {
|
|
83
|
-
return null;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return null;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// 读取 profile 配置
|
|
90
|
-
export function readProfile(name) {
|
|
91
|
-
const profilePath = getProfilePath(name);
|
|
92
|
-
if (!fs.existsSync(profilePath)) {
|
|
93
|
-
return null;
|
|
94
|
-
}
|
|
95
|
-
try {
|
|
96
|
-
return JSON.parse(fs.readFileSync(profilePath, 'utf-8'));
|
|
97
|
-
} catch {
|
|
98
|
-
return null;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// 保存 profile 配置
|
|
103
|
-
export function saveProfile(name, settings) {
|
|
104
|
-
ensureDirs();
|
|
105
|
-
const profilePath = getProfilePath(name);
|
|
106
|
-
fs.writeFileSync(profilePath, JSON.stringify(settings, null, 2));
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
//
|
|
110
|
-
export function
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { CONFIG_DIR, PROFILES_DIR, DEFAULT_FILE, CLAUDE_SETTINGS_PATH } from './config.js';
|
|
4
|
+
|
|
5
|
+
// 确保目录存在
|
|
6
|
+
export function ensureDirs() {
|
|
7
|
+
if (!fs.existsSync(CONFIG_DIR)) {
|
|
8
|
+
fs.mkdirSync(CONFIG_DIR, { recursive: true });
|
|
9
|
+
}
|
|
10
|
+
if (!fs.existsSync(PROFILES_DIR)) {
|
|
11
|
+
fs.mkdirSync(PROFILES_DIR, { recursive: true });
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// 获取所有 profiles(按 a-z 排序)
|
|
16
|
+
export function getProfiles() {
|
|
17
|
+
ensureDirs();
|
|
18
|
+
const files = fs.readdirSync(PROFILES_DIR);
|
|
19
|
+
return files
|
|
20
|
+
.filter(f => f.endsWith('.json'))
|
|
21
|
+
.map(f => f.replace('.json', ''))
|
|
22
|
+
.sort((a, b) => a.localeCompare(b, 'zh-CN', { sensitivity: 'base' }));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// 获取带序号的 profiles 映射 { 序号: profileName }
|
|
26
|
+
export function getProfilesWithIndex() {
|
|
27
|
+
const profiles = getProfiles();
|
|
28
|
+
const map = {};
|
|
29
|
+
profiles.forEach((p, i) => {
|
|
30
|
+
map[i + 1] = p;
|
|
31
|
+
});
|
|
32
|
+
return { profiles, map };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// 根据序号或名称解析 profile
|
|
36
|
+
export function resolveProfile(input) {
|
|
37
|
+
const { profiles, map } = getProfilesWithIndex();
|
|
38
|
+
|
|
39
|
+
// 尝试作为数字序号
|
|
40
|
+
const num = parseInt(input, 10);
|
|
41
|
+
if (!isNaN(num) && map[num]) {
|
|
42
|
+
return map[num];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// 作为名称
|
|
46
|
+
if (profiles.includes(input)) {
|
|
47
|
+
return input;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 获取默认 profile
|
|
54
|
+
export function getDefaultProfile() {
|
|
55
|
+
if (fs.existsSync(DEFAULT_FILE)) {
|
|
56
|
+
return fs.readFileSync(DEFAULT_FILE, 'utf-8').trim();
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// 设置默认 profile
|
|
62
|
+
export function setDefaultProfile(name) {
|
|
63
|
+
ensureDirs();
|
|
64
|
+
fs.writeFileSync(DEFAULT_FILE, name);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// 获取 profile 路径
|
|
68
|
+
export function getProfilePath(name) {
|
|
69
|
+
return path.join(PROFILES_DIR, `${name}.json`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// 检查 profile 是否存在
|
|
73
|
+
export function profileExists(name) {
|
|
74
|
+
return fs.existsSync(getProfilePath(name));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// 获取 Claude 默认 settings 模板
|
|
78
|
+
export function getClaudeSettingsTemplate() {
|
|
79
|
+
if (fs.existsSync(CLAUDE_SETTINGS_PATH)) {
|
|
80
|
+
try {
|
|
81
|
+
return JSON.parse(fs.readFileSync(CLAUDE_SETTINGS_PATH, 'utf-8'));
|
|
82
|
+
} catch {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// 读取 profile 配置
|
|
90
|
+
export function readProfile(name) {
|
|
91
|
+
const profilePath = getProfilePath(name);
|
|
92
|
+
if (!fs.existsSync(profilePath)) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
return JSON.parse(fs.readFileSync(profilePath, 'utf-8'));
|
|
97
|
+
} catch {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// 保存 profile 配置
|
|
103
|
+
export function saveProfile(name, settings) {
|
|
104
|
+
ensureDirs();
|
|
105
|
+
const profilePath = getProfilePath(name);
|
|
106
|
+
fs.writeFileSync(profilePath, JSON.stringify(settings, null, 2));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// 创建基于主配置的 profile(复制 ~/.claude/settings.json 并设置 env)
|
|
110
|
+
export function createProfileFromTemplate(name, apiUrl, apiKey) {
|
|
111
|
+
const template = getClaudeSettingsTemplate() || {};
|
|
112
|
+
|
|
113
|
+
// 确保 env 对象存在
|
|
114
|
+
if (!template.env) {
|
|
115
|
+
template.env = {};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// 只设置 API 凭证到 env
|
|
119
|
+
template.env.ANTHROPIC_AUTH_TOKEN = apiKey;
|
|
120
|
+
template.env.ANTHROPIC_BASE_URL = apiUrl;
|
|
121
|
+
|
|
122
|
+
saveProfile(name, template);
|
|
123
|
+
return template;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// 同步主配置到 profile(保留 profile 的 API 凭证)
|
|
127
|
+
export function syncProfileWithTemplate(name) {
|
|
128
|
+
const template = getClaudeSettingsTemplate();
|
|
129
|
+
if (!template) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const currentProfile = readProfile(name);
|
|
134
|
+
if (!currentProfile) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// 保存当前 profile 的 API 凭证(支持新旧格式)
|
|
139
|
+
const currentEnv = currentProfile.env || {};
|
|
140
|
+
const apiKey = currentEnv.ANTHROPIC_AUTH_TOKEN || currentProfile.ANTHROPIC_AUTH_TOKEN || '';
|
|
141
|
+
const apiUrl = currentEnv.ANTHROPIC_BASE_URL || currentProfile.ANTHROPIC_BASE_URL || '';
|
|
142
|
+
|
|
143
|
+
// 复制主配置
|
|
144
|
+
const newProfile = { ...template };
|
|
145
|
+
|
|
146
|
+
// 确保 env 对象存在并保留 API 凭证
|
|
147
|
+
newProfile.env = { ...(template.env || {}), ANTHROPIC_AUTH_TOKEN: apiKey, ANTHROPIC_BASE_URL: apiUrl };
|
|
148
|
+
|
|
149
|
+
saveProfile(name, newProfile);
|
|
150
|
+
return newProfile;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// 从 profile 中提取 API 凭证
|
|
154
|
+
export function getProfileCredentials(name) {
|
|
155
|
+
const profile = readProfile(name);
|
|
156
|
+
if (!profile) {
|
|
157
|
+
return { apiKey: '', apiUrl: '' };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// 支持旧格式(直接在顶层)和新格式(在 env 中)
|
|
161
|
+
const env = profile.env || {};
|
|
162
|
+
return {
|
|
163
|
+
apiKey: env.ANTHROPIC_AUTH_TOKEN || profile.ANTHROPIC_AUTH_TOKEN || '',
|
|
164
|
+
apiUrl: env.ANTHROPIC_BASE_URL || profile.ANTHROPIC_BASE_URL || ''
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// 删除 profile
|
|
169
|
+
export function deleteProfile(name) {
|
|
170
|
+
const profilePath = getProfilePath(name);
|
|
171
|
+
if (fs.existsSync(profilePath)) {
|
|
172
|
+
fs.unlinkSync(profilePath);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// 清除默认 profile 设置
|
|
177
|
+
export function clearDefaultProfile() {
|
|
178
|
+
if (fs.existsSync(DEFAULT_FILE)) {
|
|
179
|
+
fs.unlinkSync(DEFAULT_FILE);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|