@cloudbase/cli 2.8.0-beta.4 → 2.8.0-beta.5

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 (46) hide show
  1. package/.augment-guidelines +119 -0
  2. package/.clinerules/cloudbase-rules.mdc +119 -0
  3. package/.cursor/rules/cloudbase-rules.mdc +119 -0
  4. package/.mcp.json +11 -0
  5. package/CLAUDE.md +119 -0
  6. package/README.md +13 -1
  7. package/bin/cloudbase-mcp.js +24 -0
  8. package/bin/tcb.js +0 -2
  9. package/cloudbaserc.json +3 -0
  10. package/lib/commands/ai/index.js +172 -0
  11. package/lib/commands/cloudrun/base.js +2 -2
  12. package/lib/commands/index.js +1 -0
  13. package/lib/commands/utils.js +10 -4
  14. package/lib/utils/ai/banner.js +88 -0
  15. package/lib/utils/ai/config.js +254 -0
  16. package/lib/utils/ai/const.js +150 -0
  17. package/lib/utils/ai/ensureFiles.js +26 -0
  18. package/lib/utils/ai/envLocalManager.js +144 -0
  19. package/lib/utils/ai/router.js +1089 -0
  20. package/lib/utils/ai/setup.js +550 -0
  21. package/package.json +11 -3
  22. package/rules/cloudbase-platform.mdc +44 -0
  23. package/rules/database.mdc +25 -0
  24. package/rules/miniprogram-development.mdc +61 -0
  25. package/rules/ui-design.mdc +24 -0
  26. package/rules/web-development.mdc +44 -0
  27. package/rules/workflows.mdc +30 -0
  28. package/specs/ai-cli-bootstrap/QWEN.md +196 -0
  29. package/specs/ai-cli-bootstrap/design.md +185 -0
  30. package/specs/ai-cli-bootstrap/requirements.md +51 -0
  31. package/specs/ai-cli-bootstrap/tasks.md +70 -0
  32. package/specs/ai-cli-bootstrap/technical-docs.md +421 -0
  33. package/specs/mcp-global-bin/design.md +57 -0
  34. package/specs/mcp-global-bin/requirements.md +43 -0
  35. package/specs/mcp-global-bin/tasks.md +54 -0
  36. package/types/commands/ai/index.d.ts +23 -0
  37. package/types/commands/index.d.ts +1 -0
  38. package/types/commands/utils.d.ts +6 -0
  39. package/types/utils/ai/banner.d.ts +2 -0
  40. package/types/utils/ai/config.d.ts +79 -0
  41. package/types/utils/ai/const.d.ts +323 -0
  42. package/types/utils/ai/ensureFiles.d.ts +1 -0
  43. package/types/utils/ai/envLocalManager.d.ts +23 -0
  44. package/types/utils/ai/router.d.ts +45 -0
  45. package/types/utils/ai/setup.d.ts +23 -0
  46. package/types/utils/config.d.ts +1 -0
@@ -0,0 +1,1089 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
35
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
36
+ var m = o[Symbol.asyncIterator], i;
37
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
38
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
39
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
40
+ };
41
+ var __importDefault = (this && this.__importDefault) || function (mod) {
42
+ return (mod && mod.__esModule) ? mod : { "default": mod };
43
+ };
44
+ Object.defineProperty(exports, "__esModule", { value: true });
45
+ exports.AICommandRouter = void 0;
46
+ const child_process_1 = require("child_process");
47
+ const config_1 = require("./config");
48
+ const error_1 = require("../../error");
49
+ const inquirer_1 = __importDefault(require("inquirer"));
50
+ const chalk_1 = __importDefault(require("chalk"));
51
+ const const_1 = require("./const");
52
+ const utils_1 = require("../../commands/utils");
53
+ const auth_1 = require("../../auth");
54
+ const IDE_FILE_MAPPINGS = {
55
+ "cursor": [
56
+ ".cursor/rules/cloudbase-rules.mdc",
57
+ ".cursor/mcp.json"
58
+ ],
59
+ "windsurf": [
60
+ ".windsurf/rules/cloudbase-rules.md"
61
+ ],
62
+ "codebuddy": [
63
+ ".rules/cloudbase-rules.md"
64
+ ],
65
+ "claude-code": [
66
+ "CLAUDE.md",
67
+ ".mcp.json"
68
+ ],
69
+ "cline": [
70
+ ".clinerules/cloudbase-rules.mdc"
71
+ ],
72
+ "gemini-cli": [
73
+ ".gemini/GEMINI.md",
74
+ ".gemini/settings.json"
75
+ ],
76
+ "opencode": [
77
+ ".opencode.json"
78
+ ],
79
+ "qwen-code": [
80
+ ".qwen/QWEN.md",
81
+ ".qwen/settings.json"
82
+ ],
83
+ "baidu-comate": [
84
+ ".comate/rules/cloudbase-rules.mdr",
85
+ ".comate/rules/cloudbaase-rules.mdr",
86
+ ".comate/mcp.json"
87
+ ],
88
+ "openai-codex-cli": [
89
+ ".codex/config.toml",
90
+ "AGENTS.md",
91
+ ],
92
+ "augment-code": [
93
+ ".augment-guidelines"
94
+ ],
95
+ "github-copilot": [
96
+ ".github/copilot-instructions.md"
97
+ ],
98
+ "roocode": [
99
+ ".roo/rules/cloudbaase-rules.md",
100
+ ".roo/mcp.json"
101
+ ],
102
+ "tongyi-lingma": [
103
+ ".lingma/rules/cloudbaase-rules.md"
104
+ ],
105
+ "trae": [
106
+ ".trae/rules/cloudbase-rules.md"
107
+ ],
108
+ "vscode": [
109
+ ".vscode/mcp.json",
110
+ ".vscode/settings.json"
111
+ ],
112
+ "aider": [
113
+ "mcp.json"
114
+ ]
115
+ };
116
+ class AICommandRouter {
117
+ constructor() {
118
+ this.configManager = new config_1.AIConfigManager();
119
+ }
120
+ execute({ agent, addtionalArgs, log }) {
121
+ var _a;
122
+ return __awaiter(this, void 0, void 0, function* () {
123
+ if ((0, config_1.isValidAgent)(agent) !== true) {
124
+ log.error(`❌ 无效的 agent: ${agent}`);
125
+ return;
126
+ }
127
+ const config = yield this.configManager.loadConfig();
128
+ if (!config) {
129
+ log.warn("⚠️ 未检测到 AI ToolKit CLI 配置,请运行 'tcb ai --setup' 进行配置");
130
+ return;
131
+ }
132
+ const agentUpperCased = agent.toUpperCase();
133
+ const agentConfig = (_a = config.agents) === null || _a === void 0 ? void 0 : _a[agent];
134
+ if (!agentConfig) {
135
+ log.warn(`⚠️ 未找到 ${agentUpperCased} 配置,请运行 tcb ai --setup 进行配置`);
136
+ return;
137
+ }
138
+ log.info(`🚀 启动 ${chalk_1.default.bold(agentUpperCased)} AI CLI 工具`);
139
+ try {
140
+ yield this.checkToolkitConfig(agent, log);
141
+ }
142
+ catch (e) {
143
+ log.warn('⚠️ 云开发功能检查失败,但 AI 工具仍可正常使用');
144
+ }
145
+ const isValid = yield this.validateAgentConfig(agent, agentConfig, log);
146
+ if (!isValid) {
147
+ log.warn(`⚠️ ${agent.toUpperCase()} 配置无效,请重新配置`);
148
+ return;
149
+ }
150
+ this.executeAgentWithConfig(agent, agentConfig, this.parseArgs(addtionalArgs), log);
151
+ });
152
+ }
153
+ checkToolkitConfig(agent, log) {
154
+ return __awaiter(this, void 0, void 0, function* () {
155
+ const { missingFiles } = yield this.configManager.checkToolkitConfig(agent);
156
+ if (missingFiles.length > 0) {
157
+ const shouldDownload = yield this.promptForTemplateDownload(log);
158
+ if (shouldDownload) {
159
+ log.log('');
160
+ yield this.downloadTemplate(log);
161
+ log.log('');
162
+ }
163
+ else {
164
+ log.info("💡 请运行 'tcb ai --setup' 或访问官方文档进行配置");
165
+ const docUrls = {
166
+ claude: 'https://docs.cloudbase.net/ai/cloudbase-ai-toolkit/ide-setup/claude-code',
167
+ codex: 'https://docs.cloudbase.net/ai/cloudbase-ai-toolkit/ide-setup/codex',
168
+ gemini: 'https://docs.cloudbase.net/ai/cloudbase-ai-toolkit/ide-setup/gemini'
169
+ };
170
+ if (docUrls[agent]) {
171
+ log.info(`📖 文档: ${docUrls[agent]}`);
172
+ }
173
+ }
174
+ }
175
+ });
176
+ }
177
+ promptForTemplateDownload(log) {
178
+ return __awaiter(this, void 0, void 0, function* () {
179
+ try {
180
+ const { downloadTemplate } = yield inquirer_1.default.prompt([
181
+ {
182
+ type: 'confirm',
183
+ name: 'downloadTemplate',
184
+ message: '是否下载 CloudBase AI ToolKit 模板?(建议下载以获取完整的开发体验)',
185
+ default: true
186
+ }
187
+ ]);
188
+ return downloadTemplate;
189
+ }
190
+ catch (error) {
191
+ log.debug('交互提示失败,跳过模板下载');
192
+ return false;
193
+ }
194
+ });
195
+ }
196
+ downloadTemplate(log) {
197
+ return __awaiter(this, void 0, void 0, function* () {
198
+ try {
199
+ const { templateType } = yield inquirer_1.default.prompt([
200
+ {
201
+ type: 'list',
202
+ name: 'templateType',
203
+ message: '选择要下载的模板类型:',
204
+ choices: [
205
+ { name: '🚀 Web 应用 - React + CloudBase', value: 'react' },
206
+ { name: '🟢 Web 应用 - Vue + CloudBase', value: 'vue' },
207
+ { name: '🟦 微信小程序 + CloudBase', value: 'miniprogram' },
208
+ { name: '🌈 跨端应用 - UniApp + CloudBase', value: 'uniapp' },
209
+ { name: '🧩 只下载 AI 规则和配置', value: 'rules' }
210
+ ],
211
+ default: 'rules'
212
+ }
213
+ ]);
214
+ const shouldCheckOverwrite = yield this.shouldCheckOverwrite(templateType);
215
+ let overwrite = false;
216
+ if (shouldCheckOverwrite) {
217
+ const { confirmOverwrite } = yield inquirer_1.default.prompt([
218
+ {
219
+ type: 'confirm',
220
+ name: 'confirmOverwrite',
221
+ message: '检测到已存在文件,是否覆盖?',
222
+ default: false
223
+ }
224
+ ]);
225
+ overwrite = confirmOverwrite;
226
+ }
227
+ log.info(`📦 正在下载并解压 ${templateType} 模板...`);
228
+ yield this.downloadAndExtractTemplate(templateType, overwrite, log);
229
+ log.info(`✅ ${templateType} 模板配置完成`);
230
+ }
231
+ catch (error) {
232
+ log.error(`❌ 配置失败: ${error.message}`);
233
+ throw new error_1.CloudBaseError('模板下载失败', { original: error });
234
+ }
235
+ });
236
+ }
237
+ downloadAndExtractTemplate(templateType, overwrite, log) {
238
+ var _a, e_1, _b, _c;
239
+ return __awaiter(this, void 0, void 0, function* () {
240
+ const fs = yield Promise.resolve().then(() => __importStar(require('fs-extra')));
241
+ const path = yield Promise.resolve().then(() => __importStar(require('path')));
242
+ const os = yield Promise.resolve().then(() => __importStar(require('os')));
243
+ const unzipper = yield Promise.resolve().then(() => __importStar(require('unzipper')));
244
+ const https = yield Promise.resolve().then(() => __importStar(require('https')));
245
+ let ConfigParser;
246
+ try {
247
+ ConfigParser = (yield Promise.resolve().then(() => __importStar(require('@cloudbase/toolbox')))).ConfigParser;
248
+ }
249
+ catch (_d) {
250
+ ConfigParser = undefined;
251
+ }
252
+ const templateMap = {
253
+ rules: {
254
+ url: 'https://static.cloudbase.net/cloudbase-examples/web-cloudbase-project.zip'
255
+ },
256
+ react: {
257
+ url: 'https://static.cloudbase.net/cloudbase-examples/web-cloudbase-react-template.zip'
258
+ },
259
+ vue: {
260
+ url: 'https://static.cloudbase.net/cloudbase-examples/web-cloudbase-vue-template.zip'
261
+ },
262
+ miniprogram: {
263
+ url: 'https://static.cloudbase.net/cloudbase-examples/miniprogram-cloudbase-miniprogram-template.zip'
264
+ },
265
+ uniapp: {
266
+ url: 'https://static.cloudbase.net/cloudbase-examples/universal-cloudbase-uniapp-template.zip'
267
+ }
268
+ };
269
+ const template = templateMap[templateType];
270
+ if (!template)
271
+ throw new error_1.CloudBaseError('未知模板类型');
272
+ const tmpDir = os.tmpdir();
273
+ const zipPath = path.join(tmpDir, `cloudbase-template-${templateType}-${Date.now()}.zip`);
274
+ yield new Promise((resolve, reject) => {
275
+ const file = fs.createWriteStream(zipPath);
276
+ https
277
+ .get(template.url, (response) => {
278
+ if (response.statusCode !== 200) {
279
+ reject(new Error(`下载失败: ${response.statusCode}`));
280
+ return;
281
+ }
282
+ response.pipe(file);
283
+ file.on('finish', () => {
284
+ file.close();
285
+ resolve();
286
+ });
287
+ })
288
+ .on('error', (err) => {
289
+ fs.unlink(zipPath);
290
+ reject(err);
291
+ });
292
+ });
293
+ const extractDir = process.cwd();
294
+ const zipStream = fs.createReadStream(zipPath).pipe(unzipper.Parse({ forceStream: true }));
295
+ try {
296
+ for (var _e = true, _f = __asyncValues(zipStream), _g; _g = yield _f.next(), _a = _g.done, !_a;) {
297
+ _c = _g.value;
298
+ _e = false;
299
+ try {
300
+ const entry = _c;
301
+ const entryPath = entry.path;
302
+ const destPath = path.join(extractDir, entryPath);
303
+ if (entry.type === 'Directory') {
304
+ yield fs.ensureDir(destPath);
305
+ entry.autodrain();
306
+ continue;
307
+ }
308
+ if (entryPath === 'cloudbaserc.json' && (yield fs.pathExists(destPath))) {
309
+ try {
310
+ const templateContent = yield entry.buffer();
311
+ const templateJson = JSON.parse(templateContent.toString('utf8'));
312
+ const localJson = yield fs.readJson(destPath);
313
+ const deepMerge = (target, source) => {
314
+ if (typeof target !== 'object' ||
315
+ typeof source !== 'object' ||
316
+ !target ||
317
+ !source)
318
+ return target;
319
+ const result = Object.assign(Object.assign({}, source), target);
320
+ for (const key of Object.keys(source)) {
321
+ if (key in target &&
322
+ typeof target[key] === 'object' &&
323
+ typeof source[key] === 'object') {
324
+ result[key] = deepMerge(target[key], source[key]);
325
+ }
326
+ }
327
+ return result;
328
+ };
329
+ let merged = deepMerge(localJson, templateJson);
330
+ yield fs.writeJson(destPath, merged, { spaces: 2 });
331
+ }
332
+ catch (e) {
333
+ log.warn('cloudbaserc.json 合并失败,已跳过: ' + e.message);
334
+ }
335
+ continue;
336
+ }
337
+ if (!overwrite && (yield fs.pathExists(destPath))) {
338
+ entry.autodrain();
339
+ continue;
340
+ }
341
+ yield fs.ensureDir(path.dirname(destPath));
342
+ yield new Promise((res, rej) => {
343
+ entry.pipe(fs.createWriteStream(destPath)).on('finish', res).on('error', rej);
344
+ });
345
+ }
346
+ finally {
347
+ _e = true;
348
+ }
349
+ }
350
+ }
351
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
352
+ finally {
353
+ try {
354
+ if (!_e && !_a && (_b = _f.return)) yield _b.call(_f);
355
+ }
356
+ finally { if (e_1) throw e_1.error; }
357
+ }
358
+ yield fs.unlink(zipPath);
359
+ yield this.modifyMCPConfigs(extractDir, log);
360
+ });
361
+ }
362
+ validateTemplateIntegrity(templateType, extractDir, log) {
363
+ return __awaiter(this, void 0, void 0, function* () {
364
+ const fs = yield Promise.resolve().then(() => __importStar(require('fs-extra')));
365
+ const path = yield Promise.resolve().then(() => __importStar(require('path')));
366
+ const requiredFiles = {
367
+ rules: ['.mcp.json', 'CLAUDE.md'],
368
+ react: ['package.json', 'src/', 'public/', 'cloudbaserc.json'],
369
+ vue: ['package.json', 'src/', 'public/', 'cloudbaserc.json'],
370
+ miniprogram: ['app.js', 'app.json', 'pages/', 'cloudbaserc.json'],
371
+ uniapp: ['pages.json', 'manifest.json', 'pages/', 'cloudbaserc.json']
372
+ };
373
+ const files = requiredFiles[templateType] || [];
374
+ const missingFiles = [];
375
+ for (const file of files) {
376
+ const filePath = path.join(extractDir, file);
377
+ if (!(yield fs.pathExists(filePath))) {
378
+ missingFiles.push(file);
379
+ }
380
+ }
381
+ if (missingFiles.length > 0) {
382
+ log.warn(`⚠️ 模板解压不完整,缺失文件: ${missingFiles.join(', ')}`);
383
+ log.info('💡 建议重新下载模板或检查网络连接');
384
+ if (templateType === 'miniprogram' && missingFiles.some((f) => f.includes('pages/'))) {
385
+ log.warn('⚠️ 小程序 pages 文件夹缺失,可能影响项目正常运行');
386
+ log.info('💡 请重新运行 tcb ai 并选择下载 miniprogram 模板');
387
+ }
388
+ }
389
+ else {
390
+ log.info('✅ 模板完整性校验通过,所有必要文件已解压');
391
+ }
392
+ });
393
+ }
394
+ getInstallCommand(agent) {
395
+ switch (agent) {
396
+ case 'claude':
397
+ return {
398
+ success: true,
399
+ command: 'npm',
400
+ args: ['install', '-g', '@anthropic-ai/claude-code'],
401
+ message: 'npm install -g @anthropic-ai/claude-cli'
402
+ };
403
+ case 'qwen':
404
+ return {
405
+ success: true,
406
+ command: 'npm',
407
+ args: ['install', '-g', '@qwen-code/qwen-code'],
408
+ message: 'npm install -g @qwen-code/qwen-code'
409
+ };
410
+ case 'codex':
411
+ return {
412
+ success: true,
413
+ command: 'npm',
414
+ args: ['install', '-g', '@openai/codex-cli'],
415
+ message: 'npm install -g @openai/codex-cli'
416
+ };
417
+ case 'gemini':
418
+ return {
419
+ success: true,
420
+ command: 'npm',
421
+ args: ['install', '-g', '@google/gemini-cli'],
422
+ message: 'npm install -g @google/gemini-cli'
423
+ };
424
+ default:
425
+ return { success: false, message: `# 请查阅 ${agent} 的官方安装文档` };
426
+ }
427
+ }
428
+ shouldCheckOverwrite(templateType) {
429
+ return __awaiter(this, void 0, void 0, function* () {
430
+ const fs = yield Promise.resolve().then(() => __importStar(require('fs-extra')));
431
+ return (yield fs.pathExists('.mcp.json')) || (yield fs.pathExists('CLAUDE.md'));
432
+ });
433
+ }
434
+ executeCommand(command, args, env, log) {
435
+ return __awaiter(this, void 0, void 0, function* () {
436
+ return new Promise((resolve, reject) => {
437
+ const child = (0, child_process_1.spawn)(command, args, {
438
+ stdio: 'inherit',
439
+ env,
440
+ shell: process.platform === 'win32'
441
+ });
442
+ child.on('close', (code) => {
443
+ if (code === 0) {
444
+ resolve();
445
+ }
446
+ else {
447
+ const errorMsg = `命令执行失败: ${command} ${args.join(' ')} (退出码: ${code})`;
448
+ log.error(`❌ ${errorMsg}`);
449
+ if (code === 127) {
450
+ log.info('💡 命令未找到,请检查是否正确安装');
451
+ log.info(`📦 安装命令: ${this.getInstallCommand(command).message}`);
452
+ }
453
+ else if (code === 1) {
454
+ log.info('💡 可能是参数错误或配置问题');
455
+ log.info('🔧 请检查 API 密钥、模型名称等配置是否正确');
456
+ }
457
+ reject(new Error(errorMsg));
458
+ }
459
+ });
460
+ child.on('error', (err) => {
461
+ const errorMsg = `启动失败: ${command} (${err.message})`;
462
+ log.error(`❌ ${errorMsg}`);
463
+ if (err.message.includes('ENOENT')) {
464
+ log.info('💡 命令不存在,请先安装对应的 AI CLI 工具');
465
+ log.info(`📦 安装命令: ${this.getInstallCommand(command).message}`);
466
+ }
467
+ else if (err.message.includes('EACCES')) {
468
+ log.info('💡 权限不足,请检查文件权限或使用管理员权限');
469
+ }
470
+ reject(new Error(errorMsg));
471
+ });
472
+ });
473
+ });
474
+ }
475
+ isToolAvailable(command) {
476
+ return __awaiter(this, void 0, void 0, function* () {
477
+ return new Promise((resolve) => {
478
+ const child = (0, child_process_1.spawn)(command, ['--version'], {
479
+ stdio: 'pipe',
480
+ shell: true
481
+ });
482
+ child.on('close', (code) => {
483
+ resolve(code === 0);
484
+ });
485
+ child.on('error', () => {
486
+ resolve(false);
487
+ });
488
+ });
489
+ });
490
+ }
491
+ validateAgentConfig(agent, agentConfig, log) {
492
+ return __awaiter(this, void 0, void 0, function* () {
493
+ if (!agentConfig) {
494
+ log.error('❌ Agent 配置为空');
495
+ return false;
496
+ }
497
+ const validateResult = (0, const_1.getAgentConfigValidator)(agent)(agentConfig);
498
+ if (validateResult.success === true) {
499
+ log.debug('✅ Agent 配置验证通过');
500
+ return true;
501
+ }
502
+ else {
503
+ log.error(`❌ ${agent} 配置无效: ${validateResult.error}`);
504
+ return false;
505
+ }
506
+ });
507
+ }
508
+ executeAgentWithConfig(agent, agentConfig, additionalArgs, log) {
509
+ return __awaiter(this, void 0, void 0, function* () {
510
+ switch (agent) {
511
+ case const_1.CLAUDE.value:
512
+ if (agentConfig.type === 'cloudbase') {
513
+ return yield this.executeClaudeCloudbaseAgent(agentConfig, additionalArgs, log);
514
+ }
515
+ else {
516
+ return yield this.executeClaudeAgent(agentConfig, additionalArgs, log);
517
+ }
518
+ case const_1.QWEN.value:
519
+ if (agentConfig.type === 'cloudbase') {
520
+ return yield this.executeQwenCloudbaseAgent(agentConfig, additionalArgs, log);
521
+ }
522
+ else {
523
+ return yield this.executeQwenAgent(agentConfig, additionalArgs, log);
524
+ }
525
+ case const_1.CODEX.value:
526
+ if (agentConfig.type === 'cloudbase') {
527
+ return yield this.executeCodexCloudbaseAgent(agentConfig, additionalArgs, log);
528
+ }
529
+ else {
530
+ return yield this.executeCodexAgent(agentConfig, additionalArgs, log);
531
+ }
532
+ case const_1.AIDER.value:
533
+ if (agentConfig.type === 'cloudbase') {
534
+ return yield this.executeAiderCloudbaseAgent(agentConfig, additionalArgs, log);
535
+ }
536
+ else {
537
+ return yield this.executeAiderAgent(agentConfig, additionalArgs, log);
538
+ }
539
+ default:
540
+ throw new Error(`不支持的 AI 工具: ${agent}`);
541
+ }
542
+ });
543
+ }
544
+ executeClaudeAgent({ apiKey, baseUrl }, additionalArgs, log) {
545
+ return __awaiter(this, void 0, void 0, function* () {
546
+ yield this.ensureClaudeCode(log);
547
+ yield this.executeCommand('claude', additionalArgs, Object.assign(Object.assign({}, process.env), { ANTHROPIC_AUTH_TOKEN: apiKey, ANTHROPIC_BASE_URL: baseUrl }), log);
548
+ });
549
+ }
550
+ executeQwenAgent({ apiKey, baseUrl, model }, additionalArgs, log) {
551
+ return __awaiter(this, void 0, void 0, function* () {
552
+ yield this.ensureQwenCode(log);
553
+ yield this.executeCommand('qwen', additionalArgs, Object.assign(Object.assign({}, process.env), { OPENAI_API_KEY: apiKey, OPENAI_BASE_URL: baseUrl, OPENAI_MODEL: model }), log);
554
+ });
555
+ }
556
+ executeQwenCloudbaseAgent({ provider, model }, additionalArgs, log) {
557
+ return __awaiter(this, void 0, void 0, function* () {
558
+ yield this.ensureQwenCode(log);
559
+ const envId = yield (0, config_1.createConfigParser)().get('envId');
560
+ yield (0, auth_1.checkLogin)();
561
+ const credential = yield (0, utils_1.getCredential)({}, {});
562
+ const accessToken = yield (0, utils_1.rawFetchAccessToken)({
563
+ envId,
564
+ secretId: credential.secretId,
565
+ secretKey: credential.secretKey,
566
+ token: credential.token
567
+ });
568
+ if (!accessToken.access_token) {
569
+ log.error(`获取云开发 Access Token 失败,请运行 tcb login 后再重试,${JSON.stringify(accessToken)}`);
570
+ process.exit(1);
571
+ }
572
+ const baseUrl = `https://${envId}.api.tcloudbasegateway.com/v1/ai/${provider}`;
573
+ const apiKey = accessToken.access_token;
574
+ yield this.executeCommand('qwen', additionalArgs, Object.assign(Object.assign({}, process.env), { OPENAI_API_KEY: apiKey, OPENAI_BASE_URL: baseUrl, OPENAI_MODEL: model }), log);
575
+ });
576
+ }
577
+ executeClaudeCloudbaseAgent({ provider, model, transformer }, additionalArgs, log) {
578
+ return __awaiter(this, void 0, void 0, function* () {
579
+ yield this.ensureClaudeCodeRouter(log);
580
+ yield this.ensureClaudeCode(log);
581
+ yield this.configureClaudeCodeRouter(provider, model, transformer, log);
582
+ yield this.restartClaudeCodeRouter(log);
583
+ yield this.executeCommand('ccr', ['code', ...additionalArgs], Object.assign({}, process.env), log);
584
+ });
585
+ }
586
+ restartClaudeCodeRouter(log) {
587
+ return __awaiter(this, void 0, void 0, function* () {
588
+ try {
589
+ yield this.executeCommand('ccr', ['restart'], process.env, log);
590
+ }
591
+ catch (e) {
592
+ log.error('执行 ccr restart 失败,尝试执行 ccr stop && ccr start');
593
+ yield this.executeCommand('ccr', ['stop'], process.env, log);
594
+ this.executeCommand('ccr', ['start'], process.env, log);
595
+ const max = 3;
596
+ let current = 0;
597
+ while (current < max) {
598
+ current++;
599
+ if (current > max) {
600
+ throw new Error('ccr 重启完成失败');
601
+ }
602
+ log.info(`${current}/${max}等待 ccr 重启完成...`);
603
+ const isRunning = yield this.isClaudeCodeRouterRunning();
604
+ if (isRunning) {
605
+ log.info('ccr 重启完成');
606
+ break;
607
+ }
608
+ }
609
+ }
610
+ });
611
+ }
612
+ isClaudeCodeRouterRunning() {
613
+ return __awaiter(this, void 0, void 0, function* () {
614
+ return new Promise((resolve) => {
615
+ const child = (0, child_process_1.spawn)('ccr', ['status'], {
616
+ stdio: 'pipe',
617
+ env: process.env,
618
+ shell: process.platform === 'win32'
619
+ });
620
+ child.stdout.on('data', (data) => {
621
+ const str = data.toString();
622
+ if (str.includes('Status: Running')) {
623
+ resolve(true);
624
+ child.kill();
625
+ }
626
+ });
627
+ setTimeout(() => {
628
+ resolve(false);
629
+ child.kill();
630
+ }, 3000);
631
+ });
632
+ });
633
+ }
634
+ configureClaudeCodeRouter(provider, model, transformer, log) {
635
+ return __awaiter(this, void 0, void 0, function* () {
636
+ const fs = yield Promise.resolve().then(() => __importStar(require('fs-extra')));
637
+ const envId = yield (0, config_1.createConfigParser)().get('envId');
638
+ yield (0, auth_1.checkLogin)();
639
+ const credential = yield (0, utils_1.getCredential)({}, {});
640
+ const accessToken = yield (0, utils_1.rawFetchAccessToken)({
641
+ envId,
642
+ secretId: credential.secretId,
643
+ secretKey: credential.secretKey,
644
+ token: credential.token
645
+ });
646
+ if (!accessToken.access_token) {
647
+ log.error(`获取云开发 Access Token 失败,请运行 tcb login 后再重试,${JSON.stringify(accessToken)}`);
648
+ process.exit(1);
649
+ }
650
+ const cloudbaseProvider = {
651
+ name: 'cloudbase',
652
+ api_base_url: `https://${envId}.api.tcloudbasegateway.com/v1/ai/${provider}/chat/completions`,
653
+ api_key: accessToken.access_token,
654
+ models: [model]
655
+ };
656
+ if (transformer) {
657
+ cloudbaseProvider.transformer = { use: [transformer] };
658
+ }
659
+ yield fs.ensureFile(const_1.CLAUDE_CODE_ROUTER_CONFIG_PATH);
660
+ const claudeCodeRouterConfig = yield fs.readFile(const_1.CLAUDE_CODE_ROUTER_CONFIG_PATH, 'utf-8');
661
+ if (claudeCodeRouterConfig.trim().length === 0) {
662
+ log.debug('🛠️ claude-code-router 配置为空,写入 Cloudbase 配置...');
663
+ yield fs.writeJson(const_1.CLAUDE_CODE_ROUTER_CONFIG_PATH, { Providers: [cloudbaseProvider] });
664
+ log.debug('✅ claude-code-router 配置完成');
665
+ }
666
+ else {
667
+ const config = safeParseJson(claudeCodeRouterConfig);
668
+ if (!config) {
669
+ const { shouldOverwrite } = yield inquirer_1.default.prompt([
670
+ {
671
+ type: 'confirm',
672
+ name: 'shouldOverwrite',
673
+ message: 'claude-code-router 配置文件非合法 JSON ,是否覆盖?'
674
+ }
675
+ ]);
676
+ if (shouldOverwrite) {
677
+ yield fs.writeJson(const_1.CLAUDE_CODE_ROUTER_CONFIG_PATH, {
678
+ Providers: [cloudbaseProvider]
679
+ }, { spaces: 2 });
680
+ log.debug('✅ claude-code-router 配置完成');
681
+ }
682
+ else {
683
+ log.error('❌ claude-code-router 配置文件非合法 JSON ,请手动修复');
684
+ process.exit(1);
685
+ }
686
+ }
687
+ if (typeof config !== 'object' || config === null) {
688
+ log.error('❌ 未知 claude-code-router 配置文件格式请手动修复');
689
+ process.exit(1);
690
+ }
691
+ if (!('Providers' in config)) {
692
+ config.Providers = [cloudbaseProvider];
693
+ }
694
+ if (!Array.isArray(config.Providers)) {
695
+ log.error('❌ claude-code-router 配置文件 Providers 字段非数组类型,请手动修复');
696
+ process.exit(1);
697
+ }
698
+ const providers = config.Providers;
699
+ const index = providers.findIndex((p) => typeof p === 'object' && (p === null || p === void 0 ? void 0 : p.name) === 'cloudbase');
700
+ if (index !== -1) {
701
+ providers[index] = cloudbaseProvider;
702
+ }
703
+ else {
704
+ providers.push(cloudbaseProvider);
705
+ }
706
+ if (!('Router' in config) ||
707
+ typeof config.Router !== 'object' ||
708
+ config.Router === null) {
709
+ config.Router = {};
710
+ }
711
+ config.Router.default = 'cloudbase,deepseek-v3';
712
+ yield fs.writeJson(const_1.CLAUDE_CODE_ROUTER_CONFIG_PATH, config, { spaces: 2 });
713
+ log.debug('✅ claude-code-router 配置完成');
714
+ }
715
+ });
716
+ }
717
+ parseArgs(args) {
718
+ return args.filter((arg) => typeof arg === 'string' && arg.trim().length > 0);
719
+ }
720
+ ensureClaudeCodeRouter(log) {
721
+ return __awaiter(this, void 0, void 0, function* () {
722
+ const isAvailable = yield new Promise((resolve) => {
723
+ const child = (0, child_process_1.spawn)('ccr', ['--version'], {
724
+ stdio: 'pipe',
725
+ shell: true
726
+ });
727
+ child.stdout.on('data', (data) => {
728
+ data.toString().includes('Usage: ccr [command]') && resolve(true);
729
+ });
730
+ child.on('close', (code) => {
731
+ resolve(code === 0);
732
+ });
733
+ child.on('error', () => {
734
+ resolve(false);
735
+ });
736
+ });
737
+ if (isAvailable) {
738
+ log.debug('claude code router 已安装!');
739
+ }
740
+ else {
741
+ const { shouldInstall } = yield inquirer_1.default.prompt([
742
+ {
743
+ type: 'confirm',
744
+ name: 'shouldInstall',
745
+ message: 'AI 开发缺少 claude code router 依赖,是否安装?'
746
+ }
747
+ ]);
748
+ if (shouldInstall) {
749
+ yield this.executeCommand('npm', ['install', '-g', '@musistudio/claude-code-router'], process.env, log);
750
+ log.info('✅ claude-code-router 安装完成');
751
+ }
752
+ else {
753
+ log.info('❌ claude code router 未安装,请手动安装');
754
+ process.exit(1);
755
+ }
756
+ }
757
+ });
758
+ }
759
+ ensureClaudeCode(log) {
760
+ return __awaiter(this, void 0, void 0, function* () {
761
+ if (yield this.isToolAvailable('claude')) {
762
+ log.debug('claude code 已安装!');
763
+ }
764
+ else {
765
+ const { shouldInstall } = yield inquirer_1.default.prompt([
766
+ {
767
+ type: 'confirm',
768
+ name: 'shouldInstall',
769
+ message: 'AI 开发缺少 claude code 依赖,是否安装?'
770
+ }
771
+ ]);
772
+ if (shouldInstall) {
773
+ yield this.executeCommand('npm', ['install', '-g', '@anthropic-ai/claude-code'], process.env, log);
774
+ log.info('✅ claude code 安装完成');
775
+ }
776
+ else {
777
+ log.info('❌ claude code 未安装,请手动安装');
778
+ process.exit(1);
779
+ }
780
+ }
781
+ });
782
+ }
783
+ ensureQwenCode(log) {
784
+ return __awaiter(this, void 0, void 0, function* () {
785
+ if (yield this.isToolAvailable('qwen')) {
786
+ log.debug('qwen code 已安装!');
787
+ }
788
+ else {
789
+ const { shouldInstall } = yield inquirer_1.default.prompt([
790
+ {
791
+ type: 'confirm',
792
+ name: 'shouldInstall',
793
+ message: 'AI 开发缺少 qwen code 依赖,是否安装?'
794
+ }
795
+ ]);
796
+ if (shouldInstall) {
797
+ yield this.executeCommand('npm', ['install', '-g', '@qwen-code/qwen-code'], process.env, log);
798
+ log.info('✅ qwen code 安装完成');
799
+ }
800
+ else {
801
+ log.info('❌ qwen code 未安装,请手动安装');
802
+ process.exit(1);
803
+ }
804
+ }
805
+ });
806
+ }
807
+ executeCodexAgent({ apiKey, baseUrl, model }, additionalArgs, log) {
808
+ return __awaiter(this, void 0, void 0, function* () {
809
+ yield this.ensureCodexCode(log);
810
+ const codexArgs = [
811
+ ...(model ? ['--config', `model=${model}`] : []),
812
+ '--config',
813
+ 'model_providers.custom.name=Custom OpenAI',
814
+ ...(baseUrl ? ['--config', `model_providers.custom.base_url=${baseUrl}`] : []),
815
+ '--config',
816
+ 'model_providers.custom.env_key=OPENAI_API_KEY',
817
+ '--config',
818
+ 'model_provider=custom',
819
+ ...additionalArgs
820
+ ];
821
+ yield this.executeCommand('codex', codexArgs, Object.assign(Object.assign({}, process.env), { OPENAI_API_KEY: apiKey }), log);
822
+ });
823
+ }
824
+ executeCodexCloudbaseAgent({ provider, model }, additionalArgs, log) {
825
+ return __awaiter(this, void 0, void 0, function* () {
826
+ yield this.ensureCodexCode(log);
827
+ const envId = yield (0, config_1.createConfigParser)().get('envId');
828
+ yield (0, auth_1.checkLogin)();
829
+ const credential = yield (0, utils_1.getCredential)({}, {});
830
+ const accessToken = yield (0, utils_1.rawFetchAccessToken)({
831
+ envId,
832
+ secretId: credential.secretId,
833
+ secretKey: credential.secretKey,
834
+ token: credential.token
835
+ });
836
+ if (!accessToken.access_token) {
837
+ log.error(`获取云开发 Access Token 失败,请运行 tcb login 后再重试,${JSON.stringify(accessToken)}`);
838
+ process.exit(1);
839
+ }
840
+ const baseUrl = `https://${envId}.api.tcloudbasegateway.com/v1/ai/${provider}`;
841
+ const apiKey = accessToken.access_token;
842
+ const codexArgs = [
843
+ '--config',
844
+ `model=${model}`,
845
+ '--config',
846
+ 'model_providers.cloudbase.name=CloudBase AI',
847
+ '--config',
848
+ `model_providers.cloudbase.base_url=${baseUrl}`,
849
+ '--config',
850
+ 'model_providers.cloudbase.env_key=CLOUDBASE_ACCESS_TOKEN',
851
+ '--config',
852
+ 'model_provider=cloudbase',
853
+ ...additionalArgs
854
+ ];
855
+ yield this.executeCommand('codex', codexArgs, Object.assign(Object.assign({}, process.env), { CLOUDBASE_ACCESS_TOKEN: apiKey }), log);
856
+ });
857
+ }
858
+ ensureCodexCode(log) {
859
+ return __awaiter(this, void 0, void 0, function* () {
860
+ if (yield this.isToolAvailable('codex')) {
861
+ log.debug('codex 已安装!');
862
+ }
863
+ else {
864
+ const { shouldInstall } = yield inquirer_1.default.prompt([
865
+ {
866
+ type: 'confirm',
867
+ name: 'shouldInstall',
868
+ message: 'AI 开发缺少 codex 依赖,是否安装?'
869
+ }
870
+ ]);
871
+ if (shouldInstall) {
872
+ yield this.executeCommand('npm', ['install', '-g', '@openai/codex'], process.env, log);
873
+ log.info('✅ codex 安装完成');
874
+ }
875
+ else {
876
+ log.info('❌ codex 未安装,请手动安装');
877
+ process.exit(1);
878
+ }
879
+ }
880
+ });
881
+ }
882
+ executeAiderAgent(config, additionalArgs, log) {
883
+ return __awaiter(this, void 0, void 0, function* () {
884
+ yield this.ensureAider(log);
885
+ const { apiKey, baseUrl, model } = config;
886
+ const aiderArgs = ['--model', `openai/${model}`, ...additionalArgs];
887
+ const env = Object.assign(Object.assign({}, process.env), { OPENAI_API_KEY: apiKey, OPENAI_API_BASE: baseUrl });
888
+ yield this.executeCommand('aider', aiderArgs, env, log);
889
+ });
890
+ }
891
+ executeAiderCloudbaseAgent(config, additionalArgs, log) {
892
+ return __awaiter(this, void 0, void 0, function* () {
893
+ yield this.ensureAider(log);
894
+ const { provider, model } = config;
895
+ const envId = yield (0, config_1.createConfigParser)().get('envId');
896
+ yield (0, auth_1.checkLogin)();
897
+ const credential = yield (0, utils_1.getCredential)({}, {});
898
+ const accessToken = yield (0, utils_1.rawFetchAccessToken)({
899
+ envId,
900
+ secretId: credential.secretId,
901
+ secretKey: credential.secretKey,
902
+ token: credential.token
903
+ });
904
+ if (!accessToken.access_token) {
905
+ log.error(`获取云开发 Access Token 失败,请运行 tcb login 后再重试,${JSON.stringify(accessToken)}`);
906
+ process.exit(1);
907
+ }
908
+ const baseUrl = `https://${envId}.api.tcloudbasegateway.com/v1/ai/${provider}`;
909
+ const apiKey = accessToken.access_token;
910
+ const aiderArgs = ['--model', `openai/${model}`, ...additionalArgs];
911
+ const env = Object.assign(Object.assign({}, process.env), { OPENAI_API_KEY: apiKey, OPENAI_API_BASE: baseUrl });
912
+ yield this.executeCommand('aider', aiderArgs, env, log);
913
+ });
914
+ }
915
+ ensureAider(log) {
916
+ return __awaiter(this, void 0, void 0, function* () {
917
+ if (yield this.isToolAvailable('aider')) {
918
+ log.debug('aider 已安装!');
919
+ }
920
+ else {
921
+ const { shouldInstall } = yield inquirer_1.default.prompt([
922
+ {
923
+ type: 'confirm',
924
+ name: 'shouldInstall',
925
+ message: 'AI 开发缺少 aider 依赖,是否安装?'
926
+ }
927
+ ]);
928
+ if (shouldInstall) {
929
+ log.info('正在安装 aider...');
930
+ const platform = process.platform;
931
+ if (platform === 'win32') {
932
+ yield this.executeCommand('powershell', ['-c', 'irm https://aider.chat/install.ps1 | iex'], process.env, log);
933
+ }
934
+ else {
935
+ yield this.executeCommand('sh', ['-c', 'curl -LsSf https://aider.chat/install.sh | sh'], process.env, log);
936
+ }
937
+ log.info('✅ aider 安装完成');
938
+ }
939
+ else {
940
+ log.info('❌ aider 未安装,请手动安装');
941
+ process.exit(1);
942
+ }
943
+ }
944
+ });
945
+ }
946
+ modifyMCPConfigs(extractDir, log) {
947
+ return __awaiter(this, void 0, void 0, function* () {
948
+ const fs = yield Promise.resolve().then(() => __importStar(require('fs-extra')));
949
+ const path = yield Promise.resolve().then(() => __importStar(require('path')));
950
+ try {
951
+ log.info('🔧 正在修改 MCP 配置文件...');
952
+ for (const [ide, files] of Object.entries(IDE_FILE_MAPPINGS)) {
953
+ for (const file of files) {
954
+ const filePath = path.join(extractDir, file);
955
+ if (yield fs.pathExists(filePath)) {
956
+ if (file.endsWith('.json')) {
957
+ yield this.modifyMCPJsonFile(filePath, log);
958
+ }
959
+ else if (file.endsWith('.toml')) {
960
+ yield this.modifyMCPTomlFile(filePath, log);
961
+ }
962
+ }
963
+ }
964
+ }
965
+ log.info('✅ MCP 配置文件修改完成');
966
+ }
967
+ catch (error) {
968
+ log.warn(`⚠️ MCP 配置文件修改失败: ${error.message}`);
969
+ }
970
+ });
971
+ }
972
+ modifyMCPJsonFile(filePath, log) {
973
+ return __awaiter(this, void 0, void 0, function* () {
974
+ const fs = yield Promise.resolve().then(() => __importStar(require('fs-extra')));
975
+ try {
976
+ const content = yield fs.readFile(filePath, 'utf-8');
977
+ const config = JSON.parse(content);
978
+ let modified = false;
979
+ const modifyCommands = (obj) => {
980
+ if (typeof obj !== 'object' || obj === null) {
981
+ return obj;
982
+ }
983
+ if (Array.isArray(obj)) {
984
+ return obj.map(item => modifyCommands(item));
985
+ }
986
+ const result = Object.assign({}, obj);
987
+ if (result.command === 'npx' && Array.isArray(result.args)) {
988
+ const argsStr = result.args.join(' ');
989
+ if (argsStr.includes('npm-global-exec@latest') && argsStr.includes('@cloudbase/cloudbase-mcp@latest')) {
990
+ result.command = 'cloudbase-mcp';
991
+ result.args = [];
992
+ modified = true;
993
+ log.debug(`修改配置文件 ${filePath}: npx -> cloudbase-mcp`);
994
+ }
995
+ }
996
+ for (const [key, value] of Object.entries(result)) {
997
+ result[key] = modifyCommands(value);
998
+ }
999
+ return result;
1000
+ };
1001
+ const modifiedConfig = modifyCommands(config);
1002
+ if (modified) {
1003
+ yield fs.writeJson(filePath, modifiedConfig, { spaces: 2 });
1004
+ log.debug(`✅ 已修改 ${filePath}`);
1005
+ }
1006
+ }
1007
+ catch (error) {
1008
+ log.warn(`⚠️ 修改配置文件 ${filePath} 失败: ${error.message}`);
1009
+ }
1010
+ });
1011
+ }
1012
+ modifyMCPTomlFile(filePath, log) {
1013
+ return __awaiter(this, void 0, void 0, function* () {
1014
+ const fs = yield Promise.resolve().then(() => __importStar(require('fs-extra')));
1015
+ const toml = require('toml');
1016
+ try {
1017
+ const content = yield fs.readFile(filePath, 'utf-8');
1018
+ const config = toml.parse(content);
1019
+ let modified = false;
1020
+ const modifyCommands = (obj) => {
1021
+ if (typeof obj !== 'object' || obj === null) {
1022
+ return obj;
1023
+ }
1024
+ if (Array.isArray(obj)) {
1025
+ return obj.map(item => modifyCommands(item));
1026
+ }
1027
+ const result = Object.assign({}, obj);
1028
+ if (result.command === 'npx' && Array.isArray(result.args)) {
1029
+ const argsStr = result.args.join(' ');
1030
+ if (argsStr.includes('@cloudbase/cloudbase-mcp@latest')) {
1031
+ result.command = 'cloudbase-mcp';
1032
+ result.args = [];
1033
+ modified = true;
1034
+ log.debug(`修改配置文件 ${filePath}: npx -> cloudbase-mcp`);
1035
+ }
1036
+ }
1037
+ for (const [key, value] of Object.entries(result)) {
1038
+ result[key] = modifyCommands(value);
1039
+ }
1040
+ return result;
1041
+ };
1042
+ const modifiedConfig = modifyCommands(config);
1043
+ if (modified) {
1044
+ const tomlString = this.objectToToml(modifiedConfig);
1045
+ yield fs.writeFile(filePath, tomlString, 'utf-8');
1046
+ log.debug(`✅ 已修改 ${filePath}`);
1047
+ }
1048
+ }
1049
+ catch (error) {
1050
+ log.warn(`⚠️ 修改配置文件 ${filePath} 失败: ${error.message}`);
1051
+ }
1052
+ });
1053
+ }
1054
+ objectToToml(obj, prefix = '') {
1055
+ const lines = [];
1056
+ for (const [key, value] of Object.entries(obj)) {
1057
+ const fullKey = prefix ? `${prefix}.${key}` : key;
1058
+ if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
1059
+ lines.push(`[${fullKey}]`);
1060
+ lines.push(this.objectToToml(value, fullKey));
1061
+ }
1062
+ else if (Array.isArray(value)) {
1063
+ const arrayStr = value.map(item => {
1064
+ if (typeof item === 'string') {
1065
+ return `"${item}"`;
1066
+ }
1067
+ return item;
1068
+ }).join(', ');
1069
+ lines.push(`${key} = [${arrayStr}]`);
1070
+ }
1071
+ else if (typeof value === 'string') {
1072
+ lines.push(`${key} = "${value}"`);
1073
+ }
1074
+ else {
1075
+ lines.push(`${key} = ${value}`);
1076
+ }
1077
+ }
1078
+ return lines.join('\n');
1079
+ }
1080
+ }
1081
+ exports.AICommandRouter = AICommandRouter;
1082
+ function safeParseJson(json) {
1083
+ try {
1084
+ return JSON.parse(json);
1085
+ }
1086
+ catch (_) {
1087
+ return null;
1088
+ }
1089
+ }