@adversity/coding-tool-x 2.2.0

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 (125) hide show
  1. package/CHANGELOG.md +333 -0
  2. package/LICENSE +21 -0
  3. package/README.md +404 -0
  4. package/bin/ctx.js +8 -0
  5. package/dist/web/assets/index-D1AYlFLZ.js +3220 -0
  6. package/dist/web/assets/index-aL3cKxSK.css +41 -0
  7. package/dist/web/favicon.ico +0 -0
  8. package/dist/web/index.html +14 -0
  9. package/dist/web/logo.png +0 -0
  10. package/docs/CHANGELOG.md +582 -0
  11. package/docs/DIRECTORY_MIGRATION.md +112 -0
  12. package/docs/PROJECT_STRUCTURE.md +396 -0
  13. package/docs/bannel.png +0 -0
  14. package/docs/home.png +0 -0
  15. package/docs/logo.png +0 -0
  16. package/docs/multi-channel-load-balancing.md +249 -0
  17. package/package.json +73 -0
  18. package/src/commands/channels.js +504 -0
  19. package/src/commands/cli-type.js +99 -0
  20. package/src/commands/daemon.js +286 -0
  21. package/src/commands/doctor.js +332 -0
  22. package/src/commands/list.js +222 -0
  23. package/src/commands/logs.js +259 -0
  24. package/src/commands/port-config.js +115 -0
  25. package/src/commands/proxy-control.js +258 -0
  26. package/src/commands/proxy.js +152 -0
  27. package/src/commands/resume.js +137 -0
  28. package/src/commands/search.js +190 -0
  29. package/src/commands/stats.js +224 -0
  30. package/src/commands/switch.js +48 -0
  31. package/src/commands/toggle-proxy.js +222 -0
  32. package/src/commands/ui.js +92 -0
  33. package/src/commands/workspace.js +454 -0
  34. package/src/config/default.js +40 -0
  35. package/src/config/loader.js +75 -0
  36. package/src/config/paths.js +121 -0
  37. package/src/index.js +373 -0
  38. package/src/reset-config.js +92 -0
  39. package/src/server/api/agents.js +248 -0
  40. package/src/server/api/aliases.js +36 -0
  41. package/src/server/api/channels.js +258 -0
  42. package/src/server/api/claude-hooks.js +480 -0
  43. package/src/server/api/codex-channels.js +312 -0
  44. package/src/server/api/codex-projects.js +91 -0
  45. package/src/server/api/codex-proxy.js +182 -0
  46. package/src/server/api/codex-sessions.js +491 -0
  47. package/src/server/api/codex-statistics.js +57 -0
  48. package/src/server/api/commands.js +245 -0
  49. package/src/server/api/config-templates.js +182 -0
  50. package/src/server/api/config.js +147 -0
  51. package/src/server/api/convert.js +127 -0
  52. package/src/server/api/dashboard.js +125 -0
  53. package/src/server/api/env.js +144 -0
  54. package/src/server/api/favorites.js +77 -0
  55. package/src/server/api/gemini-channels.js +261 -0
  56. package/src/server/api/gemini-projects.js +91 -0
  57. package/src/server/api/gemini-proxy.js +160 -0
  58. package/src/server/api/gemini-sessions.js +397 -0
  59. package/src/server/api/gemini-statistics.js +57 -0
  60. package/src/server/api/health-check.js +118 -0
  61. package/src/server/api/mcp.js +336 -0
  62. package/src/server/api/pm2-autostart.js +269 -0
  63. package/src/server/api/projects.js +124 -0
  64. package/src/server/api/prompts.js +279 -0
  65. package/src/server/api/proxy.js +235 -0
  66. package/src/server/api/rules.js +271 -0
  67. package/src/server/api/sessions.js +595 -0
  68. package/src/server/api/settings.js +61 -0
  69. package/src/server/api/skills.js +305 -0
  70. package/src/server/api/statistics.js +91 -0
  71. package/src/server/api/terminal.js +202 -0
  72. package/src/server/api/ui-config.js +64 -0
  73. package/src/server/api/workspaces.js +407 -0
  74. package/src/server/codex-proxy-server.js +538 -0
  75. package/src/server/dev-server.js +26 -0
  76. package/src/server/gemini-proxy-server.js +518 -0
  77. package/src/server/index.js +305 -0
  78. package/src/server/proxy-server.js +469 -0
  79. package/src/server/services/agents-service.js +354 -0
  80. package/src/server/services/alias.js +71 -0
  81. package/src/server/services/channel-health.js +234 -0
  82. package/src/server/services/channel-scheduler.js +234 -0
  83. package/src/server/services/channels.js +347 -0
  84. package/src/server/services/codex-channels.js +625 -0
  85. package/src/server/services/codex-config.js +90 -0
  86. package/src/server/services/codex-parser.js +322 -0
  87. package/src/server/services/codex-sessions.js +665 -0
  88. package/src/server/services/codex-settings-manager.js +397 -0
  89. package/src/server/services/codex-speed-test-template.json +24 -0
  90. package/src/server/services/codex-statistics-service.js +255 -0
  91. package/src/server/services/commands-service.js +360 -0
  92. package/src/server/services/config-templates-service.js +732 -0
  93. package/src/server/services/env-checker.js +307 -0
  94. package/src/server/services/env-manager.js +300 -0
  95. package/src/server/services/favorites.js +163 -0
  96. package/src/server/services/gemini-channels.js +333 -0
  97. package/src/server/services/gemini-config.js +73 -0
  98. package/src/server/services/gemini-sessions.js +689 -0
  99. package/src/server/services/gemini-settings-manager.js +263 -0
  100. package/src/server/services/gemini-statistics-service.js +253 -0
  101. package/src/server/services/health-check.js +399 -0
  102. package/src/server/services/mcp-service.js +1188 -0
  103. package/src/server/services/prompts-service.js +492 -0
  104. package/src/server/services/proxy-runtime.js +79 -0
  105. package/src/server/services/pty-manager.js +435 -0
  106. package/src/server/services/rules-service.js +401 -0
  107. package/src/server/services/session-cache.js +127 -0
  108. package/src/server/services/session-converter.js +577 -0
  109. package/src/server/services/sessions.js +757 -0
  110. package/src/server/services/settings-manager.js +163 -0
  111. package/src/server/services/skill-service.js +965 -0
  112. package/src/server/services/speed-test.js +545 -0
  113. package/src/server/services/statistics-service.js +386 -0
  114. package/src/server/services/terminal-commands.js +155 -0
  115. package/src/server/services/terminal-config.js +140 -0
  116. package/src/server/services/terminal-detector.js +306 -0
  117. package/src/server/services/ui-config.js +130 -0
  118. package/src/server/services/workspace-service.js +662 -0
  119. package/src/server/utils/pricing.js +41 -0
  120. package/src/server/websocket-server.js +557 -0
  121. package/src/ui/menu.js +129 -0
  122. package/src/ui/prompts.js +100 -0
  123. package/src/utils/format.js +43 -0
  124. package/src/utils/port-helper.js +94 -0
  125. package/src/utils/session.js +239 -0
@@ -0,0 +1,222 @@
1
+ // 列出会话命令
2
+ const chalk = require('chalk');
3
+ const ora = require('ora');
4
+ const inquirer = require('inquirer');
5
+ const { getAllSessions, parseSessionInfoFast } = require('../utils/session');
6
+ const { formatTime, formatSize, truncate } = require('../utils/format');
7
+ const { promptSelectSession, promptForkConfirm } = require('../ui/prompts');
8
+ const { resumeSession } = require('./resume');
9
+ const { loadAliases } = require('../server/services/alias');
10
+ const { getRecentSessions } = require('../server/services/sessions');
11
+
12
+ /**
13
+ * 列出会话
14
+ */
15
+ async function listSessions(config, limit = null) {
16
+ const maxSessions = limit || config.maxDisplaySessions;
17
+ const spinner = ora('加载会话列表...').start();
18
+
19
+ const sessions = getAllSessions(config).slice(0, maxSessions);
20
+
21
+ if (sessions.length === 0) {
22
+ spinner.fail('暂无会话记录');
23
+ return [];
24
+ }
25
+
26
+ spinner.text = '解析会话信息...';
27
+
28
+ // 加载别名
29
+ const aliases = loadAliases();
30
+
31
+ const choices = sessions.map((session, index) => {
32
+ const info = parseSessionInfoFast(session.filePath);
33
+ const time = formatTime(session.mtime);
34
+ const size = formatSize(session.size);
35
+ const alias = aliases[session.sessionId];
36
+
37
+ // 构建显示名称 - 清爽的单行布局
38
+ let displayName = '';
39
+
40
+ // 格式:序号. [别名] 时间 │ 大小 │ 分支 │ 第一条消息
41
+ displayName += chalk.bold.white(`${index + 1}. `);
42
+
43
+ // 如果有别名,优先显示别名
44
+ if (alias) {
45
+ displayName += chalk.yellow.bold(`[${alias}] `);
46
+ }
47
+
48
+ displayName += chalk.cyan(`${time.padEnd(10)}`);
49
+ displayName += chalk.gray(` │ ${size.padEnd(9)}`);
50
+
51
+ if (info.gitBranch) {
52
+ const branchName = info.gitBranch
53
+ .replace('feature/', '')
54
+ .replace('feat/', '')
55
+ .replace('fix/', '')
56
+ .substring(0, 25);
57
+ displayName += chalk.green(` │ ${branchName.padEnd(25)}`);
58
+ } else {
59
+ displayName += chalk.gray(` │ ${''.padEnd(25)}`);
60
+ }
61
+
62
+ // 只显示第一条用户消息(你说明这个会话是干嘛的)
63
+ if (info.firstMessage && info.firstMessage !== 'Warmup') {
64
+ const firstMsg = truncate(info.firstMessage, 50);
65
+ displayName += chalk.gray(' │ ') + chalk.white(firstMsg);
66
+ }
67
+
68
+ return {
69
+ name: displayName,
70
+ value: session.sessionId,
71
+ short: alias ? `${alias} (${session.sessionId.substring(0, 8)})` : `会话 ${session.sessionId.substring(0, 8)}`,
72
+ };
73
+ });
74
+
75
+ spinner.stop();
76
+ spinner.clear();
77
+
78
+ // 清屏并重新显示,避免之前的输出干扰
79
+ console.clear();
80
+ console.log(chalk.green(`\n✨ 找到 ${sessions.length} 个会话\n`));
81
+
82
+ return choices;
83
+ }
84
+
85
+ /**
86
+ * 列出跨项目的最近会话
87
+ */
88
+ async function listRecentSessionsAcrossProjects(config, limit = null) {
89
+ const maxSessions = limit || 15; // 默认显示15条最新对话
90
+ const spinner = ora('加载最新对话...').start();
91
+
92
+ const sessions = getRecentSessions(config, maxSessions);
93
+
94
+ if (sessions.length === 0) {
95
+ spinner.fail('暂无会话记录');
96
+ return [];
97
+ }
98
+
99
+ spinner.text = '解析会话信息...';
100
+
101
+ const choices = sessions.map((session, index) => {
102
+ const time = formatTime(session.mtime);
103
+ const size = formatSize(session.size);
104
+ const alias = session.alias;
105
+
106
+ // 构建显示名称 - 清爽的单行布局
107
+ let displayName = '';
108
+
109
+ // 格式:序号. [项目名] [别名] 时间 │ 大小 │ 分支 │ 第一条消息
110
+ displayName += chalk.bold.white(`${index + 1}. `);
111
+
112
+ // 项目名(高亮显示)
113
+ const projectName = session.projectDisplayName || session.projectName;
114
+ displayName += chalk.magenta.bold(`[${projectName}] `);
115
+
116
+ // 如果有别名,显示别名
117
+ if (alias) {
118
+ displayName += chalk.yellow.bold(`[${alias}] `);
119
+ }
120
+
121
+ displayName += chalk.cyan(`${time.padEnd(10)}`);
122
+ displayName += chalk.gray(` │ ${size.padEnd(9)}`);
123
+
124
+ if (session.gitBranch) {
125
+ const branchName = session.gitBranch
126
+ .replace('feature/', '')
127
+ .replace('feat/', '')
128
+ .replace('fix/', '')
129
+ .substring(0, 25);
130
+ displayName += chalk.green(` │ ${branchName.padEnd(25)}`);
131
+ } else {
132
+ displayName += chalk.gray(` │ ${''.padEnd(25)}`);
133
+ }
134
+
135
+ // 只显示第一条用户消息
136
+ if (session.firstMessage && session.firstMessage !== 'Warmup') {
137
+ const firstMsg = truncate(session.firstMessage, 50);
138
+ displayName += chalk.gray(' │ ') + chalk.white(firstMsg);
139
+ }
140
+
141
+ return {
142
+ name: displayName,
143
+ value: { sessionId: session.sessionId, projectName: session.projectName },
144
+ short: alias ? `${alias} (${session.sessionId.substring(0, 8)})` : `会话 ${session.sessionId.substring(0, 8)}`,
145
+ };
146
+ });
147
+
148
+ spinner.stop();
149
+ spinner.clear();
150
+
151
+ // 清屏并重新显示,避免之前的输出干扰
152
+ console.clear();
153
+ console.log(chalk.green(`\n✨ 找到 ${sessions.length} 个最新对话(跨所有项目)\n`));
154
+
155
+ return choices;
156
+ }
157
+
158
+ /**
159
+ * 处理列出会话
160
+ */
161
+ async function handleList(config, switchProjectCallback, crossProject = false) {
162
+ while (true) {
163
+ // 根据模式选择不同的列表函数
164
+ const choices = crossProject
165
+ ? await listRecentSessionsAcrossProjects(config)
166
+ : await listSessions(config);
167
+
168
+ if (choices.length === 0) {
169
+ return;
170
+ }
171
+
172
+ // 添加操作选项
173
+ choices.push(new inquirer.Separator(chalk.gray('─'.repeat(50))));
174
+ choices.push({ name: chalk.blue('↩️ 返回主菜单'), value: 'back' });
175
+
176
+ // 跨项目模式不显示切换项目选项(因为已经是跨所有项目了)
177
+ if (!crossProject) {
178
+ choices.push({ name: chalk.magenta('🔀 切换项目'), value: 'switch' });
179
+ }
180
+
181
+ const selected = await promptSelectSession(choices);
182
+
183
+ if (selected === 'back') {
184
+ return;
185
+ }
186
+
187
+ if (selected === 'switch') {
188
+ const switched = await switchProjectCallback();
189
+ if (!switched) {
190
+ return; // 用户取消切换,返回主菜单
191
+ }
192
+ continue; // 切换后重新加载列表
193
+ }
194
+
195
+ // 跨项目模式:selected 是 { sessionId, projectName }
196
+ // 单项目模式:selected 是 sessionId
197
+ let sessionId, projectName;
198
+ if (crossProject) {
199
+ sessionId = selected.sessionId;
200
+ projectName = selected.projectName;
201
+ // 切换到该项目
202
+ config.currentProject = projectName;
203
+ } else {
204
+ sessionId = selected;
205
+ }
206
+
207
+ // 询问是否 fork
208
+ const action = await promptForkConfirm();
209
+
210
+ if (action === 'back') {
211
+ continue; // 返回列表重新选择
212
+ }
213
+
214
+ const fork = action === 'fork';
215
+ await resumeSession(config, sessionId, fork);
216
+ }
217
+ }
218
+
219
+ module.exports = {
220
+ listSessions,
221
+ handleList,
222
+ };
@@ -0,0 +1,259 @@
1
+ const chalk = require('chalk');
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ const os = require('os');
5
+ const { spawn } = require('child_process');
6
+
7
+ const LOGS_DIR = path.join(os.homedir(), '.claude/logs');
8
+
9
+ const LOG_FILES = {
10
+ ui: 'cc-tool-out.log',
11
+ claude: 'claude-proxy.log',
12
+ codex: 'codex-proxy.log',
13
+ gemini: 'gemini-proxy.log'
14
+ };
15
+
16
+ /**
17
+ * 确保日志目录存在
18
+ */
19
+ function ensureLogsDir() {
20
+ if (!fs.existsSync(LOGS_DIR)) {
21
+ fs.mkdirSync(LOGS_DIR, { recursive: true });
22
+ }
23
+ }
24
+
25
+ /**
26
+ * 查看日志
27
+ */
28
+ async function handleLogs(type = null, options = {}) {
29
+ ensureLogsDir();
30
+
31
+ const lines = options.lines || 50;
32
+ const follow = options.follow || false;
33
+ const clear = options.clear || false;
34
+
35
+ // 如果是清空日志
36
+ if (clear) {
37
+ return clearLogs(type);
38
+ }
39
+
40
+ // 如果没有指定类型,显示所有日志
41
+ if (!type) {
42
+ return showAllLogs(lines, follow);
43
+ }
44
+
45
+ // 显示特定类型的日志
46
+ const logFile = LOG_FILES[type];
47
+ if (!logFile) {
48
+ console.error(chalk.red(`\n❌ 无效的日志类型: ${type}\n`));
49
+ console.log(chalk.gray('支持的类型: ui, claude, codex, gemini\n'));
50
+ process.exit(1);
51
+ }
52
+
53
+ const logPath = path.join(LOGS_DIR, logFile);
54
+
55
+ // 检查日志文件是否存在
56
+ if (!fs.existsSync(logPath)) {
57
+ console.log(chalk.yellow(`\n⚠️ ${type} 日志文件不存在\n`));
58
+ console.log(chalk.gray(`日志路径: ${logPath}\n`));
59
+ return;
60
+ }
61
+
62
+ console.log(chalk.cyan(`\n📋 ${type.toUpperCase()} 日志 ${follow ? '(实时)' : `(最近 ${lines} 行)`}\n`));
63
+ console.log(chalk.gray(`═`.repeat(60)) + '\n');
64
+
65
+ if (follow) {
66
+ // 实时跟踪日志
67
+ tailFile(logPath);
68
+ } else {
69
+ // 显示最后 N 行
70
+ showLastLines(logPath, lines);
71
+ }
72
+ }
73
+
74
+ /**
75
+ * 显示所有日志(合并)
76
+ */
77
+ function showAllLogs(lines, follow) {
78
+ console.log(chalk.cyan(`\n📋 所有日志 ${follow ? '(实时)' : `(最近 ${lines} 行)`}\n`));
79
+ console.log(chalk.gray(`═`.repeat(60)) + '\n');
80
+
81
+ const allLogs = [];
82
+
83
+ // 读取所有日志文件
84
+ Object.entries(LOG_FILES).forEach(([type, filename]) => {
85
+ const logPath = path.join(LOGS_DIR, filename);
86
+ if (fs.existsSync(logPath)) {
87
+ try {
88
+ const content = fs.readFileSync(logPath, 'utf8');
89
+ const logLines = content.trim().split('\n').filter(line => line.trim());
90
+
91
+ logLines.forEach(line => {
92
+ allLogs.push({
93
+ type,
94
+ line,
95
+ // 尝试从日志中提取时间戳
96
+ timestamp: extractTimestamp(line) || Date.now()
97
+ });
98
+ });
99
+ } catch (err) {
100
+ // 忽略读取错误
101
+ }
102
+ }
103
+ });
104
+
105
+ // 按时间戳排序
106
+ allLogs.sort((a, b) => a.timestamp - b.timestamp);
107
+
108
+ // 只显示最后 N 行
109
+ const recentLogs = allLogs.slice(-lines);
110
+
111
+ recentLogs.forEach(log => {
112
+ const typeColor = getTypeColor(log.type);
113
+ const typeLabel = `[${log.type.toUpperCase()}]`.padEnd(10);
114
+ console.log(typeColor(typeLabel) + chalk.gray(log.line));
115
+ });
116
+
117
+ console.log(chalk.gray(`\n═`.repeat(60)));
118
+ console.log(chalk.gray(`\n💡 使用 `) + chalk.cyan(`ctx logs ${Object.keys(LOG_FILES).join('|')}`) + chalk.gray(` 查看特定类型日志\n`));
119
+ }
120
+
121
+ /**
122
+ * 显示文件最后 N 行
123
+ */
124
+ function showLastLines(filePath, lines) {
125
+ try {
126
+ const content = fs.readFileSync(filePath, 'utf8');
127
+ const allLines = content.trim().split('\n');
128
+ const lastLines = allLines.slice(-lines);
129
+
130
+ lastLines.forEach(line => {
131
+ if (line.trim()) {
132
+ console.log(line);
133
+ }
134
+ });
135
+
136
+ console.log(chalk.gray(`\n═`.repeat(60)));
137
+ console.log(chalk.gray(`\n💡 使用 `) + chalk.cyan(`ctx logs <type> --follow`) + chalk.gray(` 实时跟踪日志\n`));
138
+ } catch (err) {
139
+ console.error(chalk.red(`读取日志失败: ${err.message}\n`));
140
+ process.exit(1);
141
+ }
142
+ }
143
+
144
+ /**
145
+ * 实时跟踪日志文件
146
+ */
147
+ function tailFile(filePath) {
148
+ console.log(chalk.gray('按 Ctrl+C 停止跟踪\n'));
149
+
150
+ const tail = spawn('tail', ['-f', filePath]);
151
+
152
+ tail.stdout.on('data', (data) => {
153
+ process.stdout.write(data.toString());
154
+ });
155
+
156
+ tail.stderr.on('data', (data) => {
157
+ process.stderr.write(chalk.red(data.toString()));
158
+ });
159
+
160
+ tail.on('error', (err) => {
161
+ console.error(chalk.red(`\n❌ 跟踪日志失败: ${err.message}\n`));
162
+ process.exit(1);
163
+ });
164
+
165
+ // 处理退出信号
166
+ process.on('SIGINT', () => {
167
+ tail.kill();
168
+ console.log(chalk.gray('\n\n已停止跟踪日志\n'));
169
+ process.exit(0);
170
+ });
171
+ }
172
+
173
+ /**
174
+ * 清空日志
175
+ */
176
+ function clearLogs(type) {
177
+ if (!type) {
178
+ // 清空所有日志
179
+ console.log(chalk.cyan('\n🗑️ 清空所有日志...\n'));
180
+
181
+ let cleared = 0;
182
+ Object.entries(LOG_FILES).forEach(([logType, filename]) => {
183
+ const logPath = path.join(LOGS_DIR, filename);
184
+ if (fs.existsSync(logPath)) {
185
+ try {
186
+ fs.writeFileSync(logPath, '');
187
+ console.log(chalk.green(`✅ ${logType} 日志已清空`));
188
+ cleared++;
189
+ } catch (err) {
190
+ console.log(chalk.red(`❌ ${logType} 日志清空失败: ${err.message}`));
191
+ }
192
+ }
193
+ });
194
+
195
+ console.log(chalk.green(`\n✅ 共清空 ${cleared} 个日志文件\n`));
196
+ } else {
197
+ // 清空特定类型日志
198
+ const logFile = LOG_FILES[type];
199
+ if (!logFile) {
200
+ console.error(chalk.red(`\n❌ 无效的日志类型: ${type}\n`));
201
+ process.exit(1);
202
+ }
203
+
204
+ const logPath = path.join(LOGS_DIR, logFile);
205
+ if (fs.existsSync(logPath)) {
206
+ try {
207
+ fs.writeFileSync(logPath, '');
208
+ console.log(chalk.green(`\n✅ ${type} 日志已清空\n`));
209
+ } catch (err) {
210
+ console.error(chalk.red(`\n❌ 清空失败: ${err.message}\n`));
211
+ process.exit(1);
212
+ }
213
+ } else {
214
+ console.log(chalk.yellow(`\n⚠️ ${type} 日志文件不存在\n`));
215
+ }
216
+ }
217
+ }
218
+
219
+ /**
220
+ * 从日志行中提取时间戳
221
+ */
222
+ function extractTimestamp(line) {
223
+ // 尝试匹配常见的时间戳格式
224
+ const patterns = [
225
+ /^(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})/, // YYYY-MM-DD HH:MM:SS
226
+ /^\[(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})/, // [YYYY-MM-DDTHH:MM:SS
227
+ /^(\d{2}:\d{2}:\d{2})/ // HH:MM:SS
228
+ ];
229
+
230
+ for (const pattern of patterns) {
231
+ const match = line.match(pattern);
232
+ if (match) {
233
+ try {
234
+ return new Date(match[1]).getTime();
235
+ } catch (err) {
236
+ // 忽略解析错误
237
+ }
238
+ }
239
+ }
240
+
241
+ return null;
242
+ }
243
+
244
+ /**
245
+ * 获取类型颜色
246
+ */
247
+ function getTypeColor(type) {
248
+ const colors = {
249
+ ui: chalk.blue,
250
+ claude: chalk.green,
251
+ codex: chalk.cyan,
252
+ gemini: chalk.magenta
253
+ };
254
+ return colors[type] || chalk.gray;
255
+ }
256
+
257
+ module.exports = {
258
+ handleLogs
259
+ };
@@ -0,0 +1,115 @@
1
+ // 端口配置命令
2
+ const chalk = require('chalk');
3
+ const inquirer = require('inquirer');
4
+ const os = require('os');
5
+ const { loadConfig, saveConfig } = require('../config/loader');
6
+
7
+ /**
8
+ * 配置端口
9
+ */
10
+ async function handlePortConfig() {
11
+ console.clear();
12
+ console.log(chalk.bold.cyan('\n╔═══════════════════════════════════════╗'));
13
+ console.log(chalk.bold.cyan('║ 端口配置 ║'));
14
+ console.log(chalk.bold.cyan('╚═══════════════════════════════════════╝\n'));
15
+
16
+ const config = loadConfig();
17
+
18
+ console.log(chalk.cyan('当前端口配置:'));
19
+ console.log(chalk.gray(`• Web UI 页面端口: ${config.ports.webUI} (同时用于 WebSocket)`));
20
+ console.log(chalk.gray(`• Claude 代理端口: ${config.ports.proxy}`));
21
+ console.log(chalk.gray(`• Codex 代理端口: ${config.ports.codexProxy || 10089}`));
22
+ console.log(chalk.gray(`• Gemini 代理端口: ${config.ports.geminiProxy || 10090}\n`));
23
+
24
+ console.log(chalk.yellow('说明:'));
25
+ console.log(chalk.gray('• 端口范围: 1024-65535'));
26
+ console.log(chalk.gray('• 修改后需要重启相关服务才能生效'));
27
+ console.log(chalk.gray('• 如果端口被占用,请修改为其他端口\n'));
28
+
29
+ const answers = await inquirer.prompt([
30
+ {
31
+ type: 'input',
32
+ name: 'webUI',
33
+ message: 'Web UI 页面端口 (同时用于 WebSocket):',
34
+ default: config.ports.webUI,
35
+ validate: (input) => {
36
+ const port = parseInt(input);
37
+ if (isNaN(port) || port < 1024 || port > 65535) {
38
+ return '端口必须是 1024-65535 之间的数字';
39
+ }
40
+ return true;
41
+ },
42
+ },
43
+ {
44
+ type: 'input',
45
+ name: 'proxy',
46
+ message: 'Claude 代理服务端口:',
47
+ default: config.ports.proxy,
48
+ validate: (input) => {
49
+ const port = parseInt(input);
50
+ if (isNaN(port) || port < 1024 || port > 65535) {
51
+ return '端口必须是 1024-65535 之间的数字';
52
+ }
53
+ return true;
54
+ },
55
+ },
56
+ {
57
+ type: 'input',
58
+ name: 'codexProxy',
59
+ message: 'Codex 代理服务端口:',
60
+ default: config.ports.codexProxy || 10089,
61
+ validate: (input) => {
62
+ const port = parseInt(input);
63
+ if (isNaN(port) || port < 1024 || port > 65535) {
64
+ return '端口必须是 1024-65535 之间的数字';
65
+ }
66
+ return true;
67
+ },
68
+ },
69
+ {
70
+ type: 'input',
71
+ name: 'geminiProxy',
72
+ message: 'Gemini 代理服务端口:',
73
+ default: config.ports.geminiProxy || 10090,
74
+ validate: (input) => {
75
+ const port = parseInt(input);
76
+ if (isNaN(port) || port < 1024 || port > 65535) {
77
+ return '端口必须是 1024-65535 之间的数字';
78
+ }
79
+ return true;
80
+ },
81
+ },
82
+ ]);
83
+
84
+ // 更新配置
85
+ config.ports = {
86
+ webUI: parseInt(answers.webUI),
87
+ proxy: parseInt(answers.proxy),
88
+ codexProxy: parseInt(answers.codexProxy),
89
+ geminiProxy: parseInt(answers.geminiProxy),
90
+ };
91
+
92
+ // 保存配置(保留其余字段)
93
+ saveConfig({
94
+ ...config,
95
+ projectsDir: config.projectsDir.replace(os.homedir(), '~'),
96
+ ports: config.ports,
97
+ });
98
+
99
+ console.log(chalk.green('\n✅ 端口配置已保存\n'));
100
+ console.log(chalk.yellow('⚠️ 提示:'));
101
+ console.log(chalk.gray('• 如果 Web UI 正在运行,请重启以使用新端口'));
102
+ console.log(chalk.gray('• 如果动态切换已开启,请关闭后重新开启\n'));
103
+
104
+ await inquirer.prompt([
105
+ {
106
+ type: 'input',
107
+ name: 'continue',
108
+ message: '按回车继续...',
109
+ },
110
+ ]);
111
+ }
112
+
113
+ module.exports = {
114
+ handlePortConfig,
115
+ };