aixxai 1.7.8 → 1.7.10

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.
@@ -1,374 +1,374 @@
1
- /**
2
- * agent配置写入工具
3
- *
4
- * 负责把AIXX的供应商配置写进本地agent的配置文件,实现"零配置"。
5
- * 当前支持两个agent:
6
- * 1. ZCode -> ~/.zcode/v2/config.json (加两个供应商:AIXX-Claude / AIXX-OpenAI)
7
- * 2. Claude Code-> ~/.claude/settings.json (往env块写 ANTHROPIC_BASE_URL / ANTHROPIC_AUTH_TOKEN)
8
- *
9
- * ⛔ 绝对禁止改写 ZCode 的 setting.json(~/.zcode/v2/setting.json)!
10
- * 历史教训(2026-08-09 重大事故):
11
- * 早期版本曾把 setting.json 的 providerFamilyDomain 改成 "aixx",而 ZCode
12
- * 只接受内置家族名(zai/bigmodel),非法值直接导致 ZCode 崩溃、任务/项目丢失。
13
- * 本工具从此只允许操作 ZCode 的 config.json(加供应商),绝不碰 setting.json。
14
- *
15
- * 三条铁律(安全要求):
16
- * 1. 写入前必须备份原文件到 ~/.aixx/backups/
17
- * 2. 只做 JSON 读-改-写,绝不整体覆盖
18
- * 3. 所有写入都加 "_aixx_managed": true 标记,重复 install 只更新 key 不重建(幂等)
19
- *
20
- * 纯Node标准库,零依赖。
21
- */
22
-
23
- import {
24
- existsSync,
25
- readFileSync,
26
- writeFileSync,
27
- mkdirSync,
28
- copyFileSync,
29
- } from 'fs';
30
- import { homedir } from 'os';
31
- import { join, dirname } from 'path';
32
- import { randomUUID } from 'crypto';
33
-
34
- // AIXX后端地址(去掉/v1,因为ZCode的baseURL要写到主机根,OpenAI的写到/v1)
35
- // 会话16换门:域名网关入口(HTTPS加密),旧IP:8080仅存量兼容
36
- const AIXX_HOST = 'https://console.aixx.ai';
37
-
38
- // 用 _aixx_managed 标记找供应商时,按"作用域"区分,避免Claude/OpenAI两条记混
39
- const MANAGED_FLAG = '_aixx_managed';
40
-
41
- /**
42
- * 生成时间戳字符串,用于备份文件名
43
- * 形如 20260808-153045
44
- */
45
- function timestamp() {
46
- const d = new Date();
47
- const pad = (n) => String(n).padStart(2, '0');
48
- return (
49
- d.getFullYear() +
50
- pad(d.getMonth() + 1) +
51
- pad(d.getDate()) +
52
- '-' +
53
- pad(d.getHours()) +
54
- pad(d.getMinutes()) +
55
- pad(d.getSeconds())
56
- );
57
- }
58
-
59
- /**
60
- * 备份一个文件到 ~/.aixx/backups/<原名>-<时间戳>.json
61
- * 如果源文件不存在,直接返回null(没东西可备份,属正常)。
62
- * 如果同一秒内多次备份(时间戳相同),自动加序号后缀避免覆盖。
63
- * 返回值:
64
- * - string:备份文件路径(成功)
65
- * - null:源文件不存在(无需备份,可继续写入)
66
- * - false:源文件存在但备份失败(铁律1:必须中止写入)
67
- */
68
- function backupFile(srcPath) {
69
- if (!existsSync(srcPath)) return null;
70
- const backupDir = join(homedir(), '.aixx', 'backups');
71
- if (!existsSync(backupDir)) mkdirSync(backupDir, { recursive: true });
72
- const base = srcPath
73
- .split(/[\\/]/)
74
- .pop()
75
- .replace(/\.json$/, '');
76
- // 时间戳精确到秒;若已存在同名备份,加序号 -1, -2 ... 避免覆盖
77
- let dest = join(backupDir, `${base}-${timestamp()}.json`);
78
- let seq = 1;
79
- while (existsSync(dest)) {
80
- dest = join(backupDir, `${base}-${timestamp()}-${seq}.json`);
81
- seq++;
82
- }
83
- try {
84
- copyFileSync(srcPath, dest);
85
- return dest;
86
- } catch (e) {
87
- // 铁律1:备份失败绝不继续写入。返回false让调用方中止。
88
- console.log(` ⚠️ 备份失败: ${e.message}`);
89
- return false;
90
- }
91
- }
92
-
93
- /**
94
- * 安全读取并解析JSON文件。
95
- * 失败返回 null(绝不抛出,调用方自行处理)。
96
- */
97
- function readJsonSafe(filePath) {
98
- try {
99
- const raw = readFileSync(filePath, 'utf-8');
100
- return JSON.parse(raw);
101
- } catch (e) {
102
- return null;
103
- }
104
- }
105
-
106
- /**
107
- * 在 ZCode config.json 的 provider 里,找出带 _aixx_managed 且 scope 匹配的供应商key。
108
- * 返回供应商在config里的key(如 "aixx-claude-xxxx-uuid"),找不到返回null。
109
- */
110
- function findManagedProvider(config, scope) {
111
- const providers = config?.provider;
112
- if (!providers || typeof providers !== 'object') return null;
113
- for (const [key, val] of Object.entries(providers)) {
114
- if (val && val[MANAGED_FLAG] === scope) {
115
- return key;
116
- }
117
- }
118
- return null;
119
- }
120
-
121
- /**
122
- * AIXX-Claude 供应商的模型清单(Anthropic协议,调Claude系)
123
- * limit.context / limit.output 是 ZCode 必填字段,单位 token
124
- */
125
- function buildClaudeModels() {
126
- return {
127
- 'claude-sonnet-4-20250514': { limit: { context: 200000, output: 8192 } },
128
- 'claude-haiku-4-5-20251001': { limit: { context: 200000, output: 8192 } },
129
- 'claude-opus-4-8': { limit: { context: 200000, output: 8192 } },
130
- };
131
- }
132
-
133
- /**
134
- * AIXX-OpenAI 供应商的模型清单(OpenAI协议,调国产/DeepSeek/GLM/Kimi)
135
- */
136
- function buildOpenaiModels() {
137
- return {
138
- 'deepseek-chat': { limit: { context: 64000, output: 8192 } },
139
- 'deepseek-reasoner': { limit: { context: 64000, output: 8192 } },
140
- 'glm-4-flash': { limit: { context: 128000, output: 4096 } },
141
- 'glm-4-plus': { limit: { context: 128000, output: 4096 } },
142
- 'moonshot-v1-8k': { limit: { context: 8000, output: 4096 } },
143
- };
144
- }
145
-
146
- /**
147
- * 构造一个 AIXX-Claude 供应商对象(Anthropic协议)
148
- */
149
- function buildClaudeProvider(apiKey) {
150
- return {
151
- name: 'AIXX(Claude)',
152
- kind: 'anthropic',
153
- options: {
154
- apiKey,
155
- baseURL: AIXX_HOST,
156
- apiKeyRequired: true,
157
- },
158
- enabled: true,
159
- source: 'custom',
160
- [MANAGED_FLAG]: 'claude', // 幂等标记 + 作用域
161
- models: buildClaudeModels(),
162
- };
163
- }
164
-
165
- /**
166
- * 构造一个 AIXX-OpenAI 供应商对象(OpenAI协议)
167
- */
168
- function buildOpenaiProvider(apiKey) {
169
- return {
170
- name: 'AIXX(国产/DeepSeek)',
171
- kind: 'openai',
172
- options: {
173
- apiKey,
174
- baseURL: AIXX_HOST + '/v1',
175
- apiKeyRequired: true,
176
- },
177
- enabled: true,
178
- source: 'custom',
179
- [MANAGED_FLAG]: 'openai', // 幂等标记 + 作用域
180
- models: buildOpenaiModels(),
181
- };
182
- }
183
-
184
- /**
185
- * 写入 ZCode 配置。
186
- *
187
- * 行为:
188
- * - 如果 ~/.zcode/v2/config.json 不存在 → 直接返回false(没装ZCode)
189
- * - 备份原文件
190
- * - 读-改-写:往 provider 里塞两个AIXX供应商
191
- * - 幂等:如果已有 _aixx_managed 供应商,只更新 apiKey(保留用户可能改过的name/enabled)
192
- * - 任何一步失败都不崩溃,返回false
193
- *
194
- * 返回 { configured: true/false, note: '...' }
195
- */
196
- export function configureZCode(apiKey) {
197
- const zcodeConfigPath = join(homedir(), '.zcode', 'v2', 'config.json');
198
-
199
- if (!existsSync(zcodeConfigPath)) {
200
- return { configured: false, note: '未检测到ZCode(~/.zcode/v2/config.json不存在),跳过' };
201
- }
202
-
203
- // 读取并解析
204
- const config = readJsonSafe(zcodeConfigPath);
205
- if (!config || typeof config !== 'object') {
206
- console.log(' ⚠️ ZCode config.json 解析失败,跳过ZCode配置(避免破坏文件)');
207
- return { configured: false, note: 'ZCode config.json 解析失败' };
208
- }
209
-
210
- // 确保 provider 对象存在
211
- if (!config.provider || typeof config.provider !== 'object') {
212
- config.provider = {};
213
- }
214
-
215
- // 备份原文件
216
- const backup = backupFile(zcodeConfigPath);
217
- if (backup === false) {
218
- // 铁律1:备份失败,绝不写入(避免破坏用户现有配置)
219
- console.log(' ⚠️ 配置备份失败,请手动备份后再试(已跳过ZCode配置)');
220
- return { configured: false, note: '配置备份失败,请手动备份后再试' };
221
- }
222
- if (backup) {
223
- console.log(` 📦 已备份ZCode配置: ${backup}`);
224
- }
225
-
226
- let updated = 0;
227
-
228
- // --- AIXX-Claude 供应商 ---
229
- const claudeKey = findManagedProvider(config, 'claude');
230
- if (claudeKey) {
231
- // 幂等:只更新key(如果用户改过name/enabled,保留)
232
- const existing = config.provider[claudeKey];
233
- existing.options = existing.options || {};
234
- existing.options.apiKey = apiKey;
235
- existing.options.baseURL = AIXX_HOST;
236
- existing.options.apiKeyRequired = true;
237
- // 模型清单刷成最新(用户不太会改,但万一后端加了新模型能跟上)
238
- existing.models = buildClaudeModels();
239
- existing.kind = 'anthropic';
240
- existing[MANAGED_FLAG] = 'claude';
241
- console.log(' ✏️ 更新已有AIXX(Claude)供应商(key已刷新)');
242
- updated++;
243
- } else {
244
- // 新增:用一个稳定的UUID做key
245
- const newKey = `aixx-claude-${randomUUID()}`;
246
- config.provider[newKey] = buildClaudeProvider(apiKey);
247
- console.log(' ➕ 新增AIXX(Claude)供应商');
248
- updated++;
249
- }
250
-
251
- // --- AIXX-OpenAI 供应商 ---
252
- const openaiKey = findManagedProvider(config, 'openai');
253
- if (openaiKey) {
254
- const existing = config.provider[openaiKey];
255
- existing.options = existing.options || {};
256
- existing.options.apiKey = apiKey;
257
- existing.options.baseURL = AIXX_HOST + '/v1';
258
- existing.options.apiKeyRequired = true;
259
- existing.models = buildOpenaiModels();
260
- existing.kind = 'openai';
261
- existing[MANAGED_FLAG] = 'openai';
262
- console.log(' ✏️ 更新已有AIXX(国产/DeepSeek)供应商(key已刷新)');
263
- updated++;
264
- } else {
265
- const newKey = `aixx-openai-${randomUUID()}`;
266
- config.provider[newKey] = buildOpenaiProvider(apiKey);
267
- console.log(' ➕ 新增AIXX(国产/DeepSeek)供应商');
268
- updated++;
269
- }
270
-
271
- // 写回
272
- try {
273
- writeFileSync(zcodeConfigPath, JSON.stringify(config, null, 2));
274
- return {
275
- configured: true,
276
- note: `已配置ZCode供应商,请重启ZCode并在模型选择里选AIXX`,
277
- updated,
278
- };
279
- } catch (e) {
280
- console.log(` ⚠️ 写入ZCode配置失败: ${e.message}`);
281
- return { configured: false, note: `写入失败: ${e.message}` };
282
- }
283
- }
284
-
285
- /**
286
- * 写入 Claude Code 配置。
287
- *
288
- * 往 ~/.claude/settings.json 的 env 块写:
289
- * ANTHROPIC_BASE_URL / ANTHROPIC_AUTH_TOKEN
290
- *
291
- * 注意:这会覆盖用户已有的 Claude 配置(如果已经配过别的baseURL/token)。
292
- * 所以要先检测是否有冲突,冲突时只更新key但保留提示。
293
- *
294
- * 返回 { configured: true/false, note: '...', overwritten: true/false }
295
- */
296
- export function configureClaudeCode(apiKey) {
297
- const claudeSettingsPath = join(homedir(), '.claude', 'settings.json');
298
-
299
- if (!existsSync(claudeSettingsPath)) {
300
- return { configured: false, note: '未检测到Claude Code(~/.claude/settings.json不存在),跳过' };
301
- }
302
-
303
- const settings = readJsonSafe(claudeSettingsPath);
304
- if (!settings || typeof settings !== 'object') {
305
- console.log(' ⚠️ Claude settings.json 解析失败,跳过Claude Code配置');
306
- return { configured: false, note: 'Claude settings.json 解析失败' };
307
- }
308
-
309
- // 备份
310
- const backup = backupFile(claudeSettingsPath);
311
- if (backup === false) {
312
- // 铁律1:备份失败,绝不写入(避免破坏用户现有配置)
313
- console.log(' ⚠️ 配置备份失败,请手动备份后再试(已跳过Claude Code配置)');
314
- return { configured: false, note: '配置备份失败,请手动备份后再试' };
315
- }
316
- if (backup) {
317
- console.log(` 📦 已备份Claude Code配置: ${backup}`);
318
- }
319
-
320
- // 检测是否已有冲突配置(不是AIXX的baseURL)
321
- let overwritten = false;
322
- const oldBase = settings?.env?.ANTHROPIC_BASE_URL;
323
- if (oldBase && oldBase !== AIXX_HOST) {
324
- overwritten = true;
325
- }
326
-
327
- // 幂等 + 写入
328
- settings.env = settings.env || {};
329
- settings.env.ANTHROPIC_BASE_URL = AIXX_HOST;
330
- settings.env.ANTHROPIC_AUTH_TOKEN = apiKey;
331
-
332
- try {
333
- writeFileSync(claudeSettingsPath, JSON.stringify(settings, null, 2));
334
- return {
335
- configured: true,
336
- note: overwritten
337
- ? '已配置Claude Code(覆盖了你原有的Claude供应商,已备份)'
338
- : '已配置Claude Code',
339
- overwritten,
340
- };
341
- } catch (e) {
342
- console.log(` ⚠️ 写入Claude Code配置失败: ${e.message}`);
343
- return { configured: false, note: `写入失败: ${e.message}` };
344
- }
345
- }
346
-
347
- /**
348
- * 入口:自动检测并配置所有能找到的agent。
349
- * 由 install.js 在拿到apiKey后调用。
350
- *
351
- * 返回 { zcode, claude } 两个结果对象。
352
- */
353
- export function configureAgents(apiKey) {
354
- console.log('\n🔌 配置本地agent(零配置核心)...');
355
-
356
- const zcode = configureZCode(apiKey);
357
- if (zcode.configured) {
358
- console.log(` ✅ ZCode: ${zcode.note}`);
359
- } else {
360
- console.log(` ⏭️ ZCode: ${zcode.note}`);
361
- }
362
-
363
- const claude = configureClaudeCode(apiKey);
364
- if (claude.configured) {
365
- console.log(` ✅ Claude Code: ${claude.note}`);
366
- if (claude.overwritten) {
367
- console.log(' ⚠️ 注意:你原有的Claude供应商被AIXX覆盖了。如需恢复,去备份目录找原配置。');
368
- }
369
- } else {
370
- console.log(` ⏭️ Claude Code: ${claude.note}`);
371
- }
372
-
373
- return { zcode, claude };
374
- }
1
+ /**
2
+ * agent配置写入工具
3
+ *
4
+ * 负责把AIXX的供应商配置写进本地agent的配置文件,实现"零配置"。
5
+ * 当前支持两个agent:
6
+ * 1. ZCode -> ~/.zcode/v2/config.json (加两个供应商:AIXX-Claude / AIXX-OpenAI)
7
+ * 2. Claude Code-> ~/.claude/settings.json (往env块写 ANTHROPIC_BASE_URL / ANTHROPIC_AUTH_TOKEN)
8
+ *
9
+ * ⛔ 绝对禁止改写 ZCode 的 setting.json(~/.zcode/v2/setting.json)!
10
+ * 历史教训(2026-08-09 重大事故):
11
+ * 早期版本曾把 setting.json 的 providerFamilyDomain 改成 "aixx",而 ZCode
12
+ * 只接受内置家族名(zai/bigmodel),非法值直接导致 ZCode 崩溃、任务/项目丢失。
13
+ * 本工具从此只允许操作 ZCode 的 config.json(加供应商),绝不碰 setting.json。
14
+ *
15
+ * 三条铁律(安全要求):
16
+ * 1. 写入前必须备份原文件到 ~/.aixx/backups/
17
+ * 2. 只做 JSON 读-改-写,绝不整体覆盖
18
+ * 3. 所有写入都加 "_aixx_managed": true 标记,重复 install 只更新 key 不重建(幂等)
19
+ *
20
+ * 纯Node标准库,零依赖。
21
+ */
22
+
23
+ import {
24
+ existsSync,
25
+ readFileSync,
26
+ writeFileSync,
27
+ mkdirSync,
28
+ copyFileSync,
29
+ } from 'fs';
30
+ import { homedir } from 'os';
31
+ import { join, dirname } from 'path';
32
+ import { randomUUID } from 'crypto';
33
+
34
+ // AIXX后端地址(去掉/v1,因为ZCode的baseURL要写到主机根,OpenAI的写到/v1)
35
+ // 会话16换门:域名网关入口(HTTPS加密),旧IP:8080仅存量兼容
36
+ const AIXX_HOST = 'https://console.aixx.ai';
37
+
38
+ // 用 _aixx_managed 标记找供应商时,按"作用域"区分,避免Claude/OpenAI两条记混
39
+ const MANAGED_FLAG = '_aixx_managed';
40
+
41
+ /**
42
+ * 生成时间戳字符串,用于备份文件名
43
+ * 形如 20260808-153045
44
+ */
45
+ function timestamp() {
46
+ const d = new Date();
47
+ const pad = (n) => String(n).padStart(2, '0');
48
+ return (
49
+ d.getFullYear() +
50
+ pad(d.getMonth() + 1) +
51
+ pad(d.getDate()) +
52
+ '-' +
53
+ pad(d.getHours()) +
54
+ pad(d.getMinutes()) +
55
+ pad(d.getSeconds())
56
+ );
57
+ }
58
+
59
+ /**
60
+ * 备份一个文件到 ~/.aixx/backups/<原名>-<时间戳>.json
61
+ * 如果源文件不存在,直接返回null(没东西可备份,属正常)。
62
+ * 如果同一秒内多次备份(时间戳相同),自动加序号后缀避免覆盖。
63
+ * 返回值:
64
+ * - string:备份文件路径(成功)
65
+ * - null:源文件不存在(无需备份,可继续写入)
66
+ * - false:源文件存在但备份失败(铁律1:必须中止写入)
67
+ */
68
+ function backupFile(srcPath) {
69
+ if (!existsSync(srcPath)) return null;
70
+ const backupDir = join(homedir(), '.aixx', 'backups');
71
+ if (!existsSync(backupDir)) mkdirSync(backupDir, { recursive: true });
72
+ const base = srcPath
73
+ .split(/[\\/]/)
74
+ .pop()
75
+ .replace(/\.json$/, '');
76
+ // 时间戳精确到秒;若已存在同名备份,加序号 -1, -2 ... 避免覆盖
77
+ let dest = join(backupDir, `${base}-${timestamp()}.json`);
78
+ let seq = 1;
79
+ while (existsSync(dest)) {
80
+ dest = join(backupDir, `${base}-${timestamp()}-${seq}.json`);
81
+ seq++;
82
+ }
83
+ try {
84
+ copyFileSync(srcPath, dest);
85
+ return dest;
86
+ } catch (e) {
87
+ // 铁律1:备份失败绝不继续写入。返回false让调用方中止。
88
+ console.log(` ⚠️ 备份失败: ${e.message}`);
89
+ return false;
90
+ }
91
+ }
92
+
93
+ /**
94
+ * 安全读取并解析JSON文件。
95
+ * 失败返回 null(绝不抛出,调用方自行处理)。
96
+ */
97
+ function readJsonSafe(filePath) {
98
+ try {
99
+ const raw = readFileSync(filePath, 'utf-8');
100
+ return JSON.parse(raw);
101
+ } catch (e) {
102
+ return null;
103
+ }
104
+ }
105
+
106
+ /**
107
+ * 在 ZCode config.json 的 provider 里,找出带 _aixx_managed 且 scope 匹配的供应商key。
108
+ * 返回供应商在config里的key(如 "aixx-claude-xxxx-uuid"),找不到返回null。
109
+ */
110
+ function findManagedProvider(config, scope) {
111
+ const providers = config?.provider;
112
+ if (!providers || typeof providers !== 'object') return null;
113
+ for (const [key, val] of Object.entries(providers)) {
114
+ if (val && val[MANAGED_FLAG] === scope) {
115
+ return key;
116
+ }
117
+ }
118
+ return null;
119
+ }
120
+
121
+ /**
122
+ * AIXX-Claude 供应商的模型清单(Anthropic协议,调Claude系)
123
+ * limit.context / limit.output 是 ZCode 必填字段,单位 token
124
+ */
125
+ function buildClaudeModels() {
126
+ return {
127
+ 'claude-sonnet-4-20250514': { limit: { context: 200000, output: 8192 } },
128
+ 'claude-haiku-4-5-20251001': { limit: { context: 200000, output: 8192 } },
129
+ 'claude-opus-4-8': { limit: { context: 200000, output: 8192 } },
130
+ };
131
+ }
132
+
133
+ /**
134
+ * AIXX-OpenAI 供应商的模型清单(OpenAI协议,调国产/DeepSeek/GLM/Kimi)
135
+ */
136
+ function buildOpenaiModels() {
137
+ return {
138
+ 'deepseek-chat': { limit: { context: 64000, output: 8192 } },
139
+ 'deepseek-reasoner': { limit: { context: 64000, output: 8192 } },
140
+ 'glm-5.3-flash': { limit: { context: 128000, output: 128000 } },
141
+ 'glm-4-plus': { limit: { context: 128000, output: 4096 } },
142
+ 'moonshot-v1-8k': { limit: { context: 8000, output: 4096 } },
143
+ };
144
+ }
145
+
146
+ /**
147
+ * 构造一个 AIXX-Claude 供应商对象(Anthropic协议)
148
+ */
149
+ function buildClaudeProvider(apiKey) {
150
+ return {
151
+ name: 'AIXX(Claude)',
152
+ kind: 'anthropic',
153
+ options: {
154
+ apiKey,
155
+ baseURL: AIXX_HOST,
156
+ apiKeyRequired: true,
157
+ },
158
+ enabled: true,
159
+ source: 'custom',
160
+ [MANAGED_FLAG]: 'claude', // 幂等标记 + 作用域
161
+ models: buildClaudeModels(),
162
+ };
163
+ }
164
+
165
+ /**
166
+ * 构造一个 AIXX-OpenAI 供应商对象(OpenAI协议)
167
+ */
168
+ function buildOpenaiProvider(apiKey) {
169
+ return {
170
+ name: 'AIXX(国产/DeepSeek)',
171
+ kind: 'openai',
172
+ options: {
173
+ apiKey,
174
+ baseURL: AIXX_HOST + '/v1',
175
+ apiKeyRequired: true,
176
+ },
177
+ enabled: true,
178
+ source: 'custom',
179
+ [MANAGED_FLAG]: 'openai', // 幂等标记 + 作用域
180
+ models: buildOpenaiModels(),
181
+ };
182
+ }
183
+
184
+ /**
185
+ * 写入 ZCode 配置。
186
+ *
187
+ * 行为:
188
+ * - 如果 ~/.zcode/v2/config.json 不存在 → 直接返回false(没装ZCode)
189
+ * - 备份原文件
190
+ * - 读-改-写:往 provider 里塞两个AIXX供应商
191
+ * - 幂等:如果已有 _aixx_managed 供应商,只更新 apiKey(保留用户可能改过的name/enabled)
192
+ * - 任何一步失败都不崩溃,返回false
193
+ *
194
+ * 返回 { configured: true/false, note: '...' }
195
+ */
196
+ export function configureZCode(apiKey) {
197
+ const zcodeConfigPath = join(homedir(), '.zcode', 'v2', 'config.json');
198
+
199
+ if (!existsSync(zcodeConfigPath)) {
200
+ return { configured: false, note: '未检测到ZCode(~/.zcode/v2/config.json不存在),跳过' };
201
+ }
202
+
203
+ // 读取并解析
204
+ const config = readJsonSafe(zcodeConfigPath);
205
+ if (!config || typeof config !== 'object') {
206
+ console.log(' ⚠️ ZCode config.json 解析失败,跳过ZCode配置(避免破坏文件)');
207
+ return { configured: false, note: 'ZCode config.json 解析失败' };
208
+ }
209
+
210
+ // 确保 provider 对象存在
211
+ if (!config.provider || typeof config.provider !== 'object') {
212
+ config.provider = {};
213
+ }
214
+
215
+ // 备份原文件
216
+ const backup = backupFile(zcodeConfigPath);
217
+ if (backup === false) {
218
+ // 铁律1:备份失败,绝不写入(避免破坏用户现有配置)
219
+ console.log(' ⚠️ 配置备份失败,请手动备份后再试(已跳过ZCode配置)');
220
+ return { configured: false, note: '配置备份失败,请手动备份后再试' };
221
+ }
222
+ if (backup) {
223
+ console.log(` 📦 已备份ZCode配置: ${backup}`);
224
+ }
225
+
226
+ let updated = 0;
227
+
228
+ // --- AIXX-Claude 供应商 ---
229
+ const claudeKey = findManagedProvider(config, 'claude');
230
+ if (claudeKey) {
231
+ // 幂等:只更新key(如果用户改过name/enabled,保留)
232
+ const existing = config.provider[claudeKey];
233
+ existing.options = existing.options || {};
234
+ existing.options.apiKey = apiKey;
235
+ existing.options.baseURL = AIXX_HOST;
236
+ existing.options.apiKeyRequired = true;
237
+ // 模型清单刷成最新(用户不太会改,但万一后端加了新模型能跟上)
238
+ existing.models = buildClaudeModels();
239
+ existing.kind = 'anthropic';
240
+ existing[MANAGED_FLAG] = 'claude';
241
+ console.log(' ✏️ 更新已有AIXX(Claude)供应商(key已刷新)');
242
+ updated++;
243
+ } else {
244
+ // 新增:用一个稳定的UUID做key
245
+ const newKey = `aixx-claude-${randomUUID()}`;
246
+ config.provider[newKey] = buildClaudeProvider(apiKey);
247
+ console.log(' ➕ 新增AIXX(Claude)供应商');
248
+ updated++;
249
+ }
250
+
251
+ // --- AIXX-OpenAI 供应商 ---
252
+ const openaiKey = findManagedProvider(config, 'openai');
253
+ if (openaiKey) {
254
+ const existing = config.provider[openaiKey];
255
+ existing.options = existing.options || {};
256
+ existing.options.apiKey = apiKey;
257
+ existing.options.baseURL = AIXX_HOST + '/v1';
258
+ existing.options.apiKeyRequired = true;
259
+ existing.models = buildOpenaiModels();
260
+ existing.kind = 'openai';
261
+ existing[MANAGED_FLAG] = 'openai';
262
+ console.log(' ✏️ 更新已有AIXX(国产/DeepSeek)供应商(key已刷新)');
263
+ updated++;
264
+ } else {
265
+ const newKey = `aixx-openai-${randomUUID()}`;
266
+ config.provider[newKey] = buildOpenaiProvider(apiKey);
267
+ console.log(' ➕ 新增AIXX(国产/DeepSeek)供应商');
268
+ updated++;
269
+ }
270
+
271
+ // 写回
272
+ try {
273
+ writeFileSync(zcodeConfigPath, JSON.stringify(config, null, 2));
274
+ return {
275
+ configured: true,
276
+ note: `已配置ZCode供应商,请重启ZCode并在模型选择里选AIXX`,
277
+ updated,
278
+ };
279
+ } catch (e) {
280
+ console.log(` ⚠️ 写入ZCode配置失败: ${e.message}`);
281
+ return { configured: false, note: `写入失败: ${e.message}` };
282
+ }
283
+ }
284
+
285
+ /**
286
+ * 写入 Claude Code 配置。
287
+ *
288
+ * 往 ~/.claude/settings.json 的 env 块写:
289
+ * ANTHROPIC_BASE_URL / ANTHROPIC_AUTH_TOKEN
290
+ *
291
+ * 注意:这会覆盖用户已有的 Claude 配置(如果已经配过别的baseURL/token)。
292
+ * 所以要先检测是否有冲突,冲突时只更新key但保留提示。
293
+ *
294
+ * 返回 { configured: true/false, note: '...', overwritten: true/false }
295
+ */
296
+ export function configureClaudeCode(apiKey) {
297
+ const claudeSettingsPath = join(homedir(), '.claude', 'settings.json');
298
+
299
+ if (!existsSync(claudeSettingsPath)) {
300
+ return { configured: false, note: '未检测到Claude Code(~/.claude/settings.json不存在),跳过' };
301
+ }
302
+
303
+ const settings = readJsonSafe(claudeSettingsPath);
304
+ if (!settings || typeof settings !== 'object') {
305
+ console.log(' ⚠️ Claude settings.json 解析失败,跳过Claude Code配置');
306
+ return { configured: false, note: 'Claude settings.json 解析失败' };
307
+ }
308
+
309
+ // 备份
310
+ const backup = backupFile(claudeSettingsPath);
311
+ if (backup === false) {
312
+ // 铁律1:备份失败,绝不写入(避免破坏用户现有配置)
313
+ console.log(' ⚠️ 配置备份失败,请手动备份后再试(已跳过Claude Code配置)');
314
+ return { configured: false, note: '配置备份失败,请手动备份后再试' };
315
+ }
316
+ if (backup) {
317
+ console.log(` 📦 已备份Claude Code配置: ${backup}`);
318
+ }
319
+
320
+ // 检测是否已有冲突配置(不是AIXX的baseURL)
321
+ let overwritten = false;
322
+ const oldBase = settings?.env?.ANTHROPIC_BASE_URL;
323
+ if (oldBase && oldBase !== AIXX_HOST) {
324
+ overwritten = true;
325
+ }
326
+
327
+ // 幂等 + 写入
328
+ settings.env = settings.env || {};
329
+ settings.env.ANTHROPIC_BASE_URL = AIXX_HOST;
330
+ settings.env.ANTHROPIC_AUTH_TOKEN = apiKey;
331
+
332
+ try {
333
+ writeFileSync(claudeSettingsPath, JSON.stringify(settings, null, 2));
334
+ return {
335
+ configured: true,
336
+ note: overwritten
337
+ ? '已配置Claude Code(覆盖了你原有的Claude供应商,已备份)'
338
+ : '已配置Claude Code',
339
+ overwritten,
340
+ };
341
+ } catch (e) {
342
+ console.log(` ⚠️ 写入Claude Code配置失败: ${e.message}`);
343
+ return { configured: false, note: `写入失败: ${e.message}` };
344
+ }
345
+ }
346
+
347
+ /**
348
+ * 入口:自动检测并配置所有能找到的agent。
349
+ * 由 install.js 在拿到apiKey后调用。
350
+ *
351
+ * 返回 { zcode, claude } 两个结果对象。
352
+ */
353
+ export function configureAgents(apiKey) {
354
+ console.log('\n🔌 配置本地agent(零配置核心)...');
355
+
356
+ const zcode = configureZCode(apiKey);
357
+ if (zcode.configured) {
358
+ console.log(` ✅ ZCode: ${zcode.note}`);
359
+ } else {
360
+ console.log(` ⏭️ ZCode: ${zcode.note}`);
361
+ }
362
+
363
+ const claude = configureClaudeCode(apiKey);
364
+ if (claude.configured) {
365
+ console.log(` ✅ Claude Code: ${claude.note}`);
366
+ if (claude.overwritten) {
367
+ console.log(' ⚠️ 注意:你原有的Claude供应商被AIXX覆盖了。如需恢复,去备份目录找原配置。');
368
+ }
369
+ } else {
370
+ console.log(` ⏭️ Claude Code: ${claude.note}`);
371
+ }
372
+
373
+ return { zcode, claude };
374
+ }