@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,397 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const os = require('os');
4
+ const toml = require('toml');
5
+
6
+ // Codex 配置文件路径
7
+ function getConfigPath() {
8
+ return path.join(os.homedir(), '.codex', 'config.toml');
9
+ }
10
+
11
+ function getAuthPath() {
12
+ return path.join(os.homedir(), '.codex', 'auth.json');
13
+ }
14
+
15
+ // 备份文件路径
16
+ function getConfigBackupPath() {
17
+ return path.join(os.homedir(), '.codex', 'config.toml.cc-tool-backup');
18
+ }
19
+
20
+ function getAuthBackupPath() {
21
+ return path.join(os.homedir(), '.codex', 'auth.json.cc-tool-backup');
22
+ }
23
+
24
+ // 检查配置文件是否存在
25
+ function configExists() {
26
+ return fs.existsSync(getConfigPath());
27
+ }
28
+
29
+ function authExists() {
30
+ return fs.existsSync(getAuthPath());
31
+ }
32
+
33
+ // 检查是否已经有备份
34
+ function hasBackup() {
35
+ return fs.existsSync(getConfigBackupPath()) || fs.existsSync(getAuthBackupPath());
36
+ }
37
+
38
+ // 读取 config.toml
39
+ function readConfig() {
40
+ try {
41
+ const content = fs.readFileSync(getConfigPath(), 'utf8');
42
+ return toml.parse(content);
43
+ } catch (err) {
44
+ throw new Error('Failed to read config.toml: ' + err.message);
45
+ }
46
+ }
47
+
48
+ // 将配置对象转换为 TOML 字符串
49
+ function configToToml(config) {
50
+ let content = `# Codex Configuration
51
+ # Managed by Coding-Tool (Proxy Mode)
52
+
53
+ `;
54
+
55
+ // 写入顶级字段
56
+ for (const [key, value] of Object.entries(config)) {
57
+ if (key === 'model_providers') continue; // 稍后处理
58
+ if (typeof value === 'string') {
59
+ content += `${key} = "${value}"\n`;
60
+ } else if (typeof value === 'boolean') {
61
+ content += `${key} = ${value}\n`;
62
+ } else if (typeof value === 'number') {
63
+ content += `${key} = ${value}\n`;
64
+ }
65
+ }
66
+
67
+ content += '\n';
68
+
69
+ // 写入 model_providers
70
+ if (config.model_providers) {
71
+ for (const [providerKey, providerConfig] of Object.entries(config.model_providers)) {
72
+ content += `[model_providers.${providerKey}]\n`;
73
+ for (const [key, value] of Object.entries(providerConfig)) {
74
+ if (typeof value === 'string') {
75
+ content += `${key} = "${value}"\n`;
76
+ } else if (typeof value === 'boolean') {
77
+ content += `${key} = ${value}\n`;
78
+ } else if (typeof value === 'number') {
79
+ content += `${key} = ${value}\n`;
80
+ }
81
+ }
82
+ content += '\n';
83
+ }
84
+ }
85
+
86
+ return content;
87
+ }
88
+
89
+ // 写入 config.toml
90
+ function writeConfig(config) {
91
+ try {
92
+ const content = configToToml(config);
93
+ fs.writeFileSync(getConfigPath(), content, 'utf8');
94
+ } catch (err) {
95
+ throw new Error('Failed to write config.toml: ' + err.message);
96
+ }
97
+ }
98
+
99
+ // 读取 auth.json
100
+ function readAuth() {
101
+ try {
102
+ if (!authExists()) {
103
+ return {};
104
+ }
105
+ const content = fs.readFileSync(getAuthPath(), 'utf8');
106
+ return JSON.parse(content);
107
+ } catch (err) {
108
+ throw new Error('Failed to read auth.json: ' + err.message);
109
+ }
110
+ }
111
+
112
+ // 写入 auth.json
113
+ function writeAuth(auth) {
114
+ try {
115
+ const content = JSON.stringify(auth, null, 2);
116
+ fs.writeFileSync(getAuthPath(), content, 'utf8');
117
+ } catch (err) {
118
+ throw new Error('Failed to write auth.json: ' + err.message);
119
+ }
120
+ }
121
+
122
+ // 备份当前配置
123
+ function backupSettings() {
124
+ try {
125
+ if (!configExists()) {
126
+ throw new Error('config.toml not found');
127
+ }
128
+
129
+ // 如果已经有备份,不覆盖
130
+ if (hasBackup()) {
131
+ console.log('Backup already exists, skipping backup');
132
+ return { success: true, alreadyExists: true };
133
+ }
134
+
135
+ // 备份 config.toml
136
+ const configContent = fs.readFileSync(getConfigPath(), 'utf8');
137
+ fs.writeFileSync(getConfigBackupPath(), configContent, 'utf8');
138
+
139
+ // 备份 auth.json (如果存在)
140
+ if (authExists()) {
141
+ const authContent = fs.readFileSync(getAuthPath(), 'utf8');
142
+ fs.writeFileSync(getAuthBackupPath(), authContent, 'utf8');
143
+ }
144
+
145
+ console.log('Codex settings backed up');
146
+ return { success: true, alreadyExists: false };
147
+ } catch (err) {
148
+ throw new Error('Failed to backup settings: ' + err.message);
149
+ }
150
+ }
151
+
152
+ // 恢复配置
153
+ function restoreSettings() {
154
+ try {
155
+ if (!hasBackup()) {
156
+ throw new Error('No backup found');
157
+ }
158
+
159
+ // 恢复 config.toml
160
+ if (fs.existsSync(getConfigBackupPath())) {
161
+ const content = fs.readFileSync(getConfigBackupPath(), 'utf8');
162
+ fs.writeFileSync(getConfigPath(), content, 'utf8');
163
+ fs.unlinkSync(getConfigBackupPath());
164
+ }
165
+
166
+ // 恢复 auth.json
167
+ if (fs.existsSync(getAuthBackupPath())) {
168
+ const content = fs.readFileSync(getAuthBackupPath(), 'utf8');
169
+ fs.writeFileSync(getAuthPath(), content, 'utf8');
170
+ fs.unlinkSync(getAuthBackupPath());
171
+ }
172
+
173
+ // 清理 shell 配置文件中的环境变量(可选,不影响恢复结果)
174
+ removeEnvFromShell('CC_PROXY_KEY');
175
+
176
+ console.log('Codex settings restored from backup');
177
+ return { success: true };
178
+ } catch (err) {
179
+ throw new Error('Failed to restore settings: ' + err.message);
180
+ }
181
+ }
182
+
183
+ // 获取用户的 shell 配置文件路径
184
+ function getShellConfigPath() {
185
+ const shell = process.env.SHELL || '';
186
+ if (shell.includes('zsh')) {
187
+ return path.join(os.homedir(), '.zshrc');
188
+ } else if (shell.includes('bash')) {
189
+ // macOS 使用 .bash_profile,Linux 使用 .bashrc
190
+ const bashProfile = path.join(os.homedir(), '.bash_profile');
191
+ const bashrc = path.join(os.homedir(), '.bashrc');
192
+ if (fs.existsSync(bashProfile)) {
193
+ return bashProfile;
194
+ }
195
+ return bashrc;
196
+ }
197
+ // 默认使用 .zshrc (macOS 默认)
198
+ return path.join(os.homedir(), '.zshrc');
199
+ }
200
+
201
+ // 注入环境变量到 shell 配置文件
202
+ function injectEnvToShell(envName, envValue) {
203
+ const configPath = getShellConfigPath();
204
+ const exportLine = `export ${envName}="${envValue}"`;
205
+ // 使用更具体的标记,包含环境变量名,方便后续精确移除
206
+ const marker = `# Added by Coding-Tool for Codex [${envName}]`;
207
+
208
+ try {
209
+ let content = '';
210
+ if (fs.existsSync(configPath)) {
211
+ content = fs.readFileSync(configPath, 'utf8');
212
+ }
213
+
214
+ // 检查是否已经存在这个环境变量配置
215
+ const regex = new RegExp(`^export ${envName}=`, 'm');
216
+ const alreadyExists = regex.test(content);
217
+
218
+ if (alreadyExists) {
219
+ // 已存在,替换它(保留原有的标记注释)
220
+ content = content.replace(
221
+ new RegExp(`^(# Added by Coding-Tool for Codex \\[${envName}\\]\n)?export ${envName}=.*$`, 'm'),
222
+ `${marker}\n${exportLine}`
223
+ );
224
+ } else {
225
+ // 不存在,追加到文件末尾
226
+ content = content.trimEnd() + `\n\n${marker}\n${exportLine}\n`;
227
+ }
228
+
229
+ fs.writeFileSync(configPath, content, 'utf8');
230
+ return { success: true, path: configPath, isFirstTime: !alreadyExists };
231
+ } catch (err) {
232
+ // 不抛出错误,只是警告,因为这不是致命问题
233
+ console.warn(`[Codex] Failed to inject env to shell config: ${err.message}`);
234
+ return { success: false, error: err.message, isFirstTime: false };
235
+ }
236
+ }
237
+
238
+ // 从 shell 配置文件移除环境变量
239
+ function removeEnvFromShell(envName) {
240
+ const configPath = getShellConfigPath();
241
+
242
+ try {
243
+ if (!fs.existsSync(configPath)) {
244
+ return { success: true };
245
+ }
246
+
247
+ let content = fs.readFileSync(configPath, 'utf8');
248
+
249
+ // 移除具体标记的环境变量(推荐方式)
250
+ content = content.replace(
251
+ new RegExp(`\\n?# Added by Coding-Tool for Codex \\[${envName}\\]\\nexport ${envName}=.*\\n?`, 'g'),
252
+ '\n'
253
+ );
254
+
255
+ // 如果没有标记,也尝试移除(兼容旧数据)
256
+ content = content.replace(
257
+ new RegExp(`^export ${envName}=.*\\n?`, 'gm'),
258
+ ''
259
+ );
260
+
261
+ // 清理多余的空行
262
+ content = content.replace(/\n\n\n+/g, '\n\n');
263
+
264
+ fs.writeFileSync(configPath, content, 'utf8');
265
+ return { success: true };
266
+ } catch (err) {
267
+ console.warn(`[Codex] Failed to remove env from shell config: ${err.message}`);
268
+ return { success: false, error: err.message };
269
+ }
270
+ }
271
+
272
+ // 设置代理配置
273
+ function setProxyConfig(proxyPort) {
274
+ try {
275
+ // 先备份
276
+ backupSettings();
277
+
278
+ // 读取当前配置
279
+ const config = readConfig();
280
+
281
+ // 设置 model_provider 为 proxy
282
+ config.model_provider = 'cc-proxy';
283
+
284
+ // 确保 model_providers 对象存在
285
+ if (!config.model_providers) {
286
+ config.model_providers = {};
287
+ }
288
+
289
+ // 添加代理 provider
290
+ config.model_providers['cc-proxy'] = {
291
+ name: 'cc-proxy',
292
+ base_url: `http://127.0.0.1:${proxyPort}/v1`,
293
+ wire_api: 'responses',
294
+ env_key: 'CC_PROXY_KEY'
295
+ };
296
+
297
+ // 写入配置
298
+ writeConfig(config);
299
+
300
+ // 写入 auth.json
301
+ const auth = readAuth();
302
+ auth.CC_PROXY_KEY = 'PROXY_KEY';
303
+ writeAuth(auth);
304
+
305
+ // 注入环境变量到 shell 配置文件(解决某些系统环境变量优先级问题)
306
+ const shellInjectResult = injectEnvToShell('CC_PROXY_KEY', 'PROXY_KEY');
307
+
308
+ // 获取 shell 配置文件路径用于提示信息
309
+ const shellConfigPath = getShellConfigPath();
310
+ const sourceCommand = process.env.SHELL?.includes('zsh') ? 'source ~/.zshrc' : 'source ~/.bashrc';
311
+
312
+ console.log(`Codex settings updated to use proxy on port ${proxyPort}`);
313
+ return {
314
+ success: true,
315
+ port: proxyPort,
316
+ envInjected: shellInjectResult.success,
317
+ isFirstTime: shellInjectResult.isFirstTime,
318
+ shellConfigPath: shellConfigPath,
319
+ sourceCommand: sourceCommand
320
+ };
321
+ } catch (err) {
322
+ throw new Error('Failed to set proxy config: ' + err.message);
323
+ }
324
+ }
325
+
326
+ // 检查当前是否是代理配置
327
+ function isProxyConfig() {
328
+ try {
329
+ if (!configExists()) {
330
+ return false;
331
+ }
332
+
333
+ const config = readConfig();
334
+
335
+ // 检查是否使用 cc-proxy provider
336
+ if (config.model_provider === 'cc-proxy') {
337
+ return true;
338
+ }
339
+
340
+ // 检查当前 provider 的 base_url 是否指向本地代理
341
+ const currentProvider = config.model_provider;
342
+ if (currentProvider && config.model_providers && config.model_providers[currentProvider]) {
343
+ const baseUrl = config.model_providers[currentProvider].base_url || '';
344
+ if (baseUrl.includes('127.0.0.1') && baseUrl.includes('10089')) {
345
+ return true;
346
+ }
347
+ }
348
+
349
+ return false;
350
+ } catch (err) {
351
+ return false;
352
+ }
353
+ }
354
+
355
+ // 获取当前代理端口(如果是代理配置)
356
+ function getCurrentProxyPort() {
357
+ try {
358
+ if (!isProxyConfig()) {
359
+ return null;
360
+ }
361
+
362
+ const config = readConfig();
363
+ const proxyProvider = config.model_providers?.['cc-proxy'];
364
+ if (proxyProvider) {
365
+ const baseUrl = proxyProvider.base_url || '';
366
+ const match = baseUrl.match(/:(\d+)/);
367
+ return match ? parseInt(match[1]) : null;
368
+ }
369
+
370
+ return null;
371
+ } catch (err) {
372
+ return null;
373
+ }
374
+ }
375
+
376
+ module.exports = {
377
+ getConfigPath,
378
+ getAuthPath,
379
+ getConfigBackupPath,
380
+ getAuthBackupPath,
381
+ configExists,
382
+ authExists,
383
+ hasBackup,
384
+ readConfig,
385
+ writeConfig,
386
+ readAuth,
387
+ writeAuth,
388
+ backupSettings,
389
+ restoreSettings,
390
+ setProxyConfig,
391
+ isProxyConfig,
392
+ getCurrentProxyPort,
393
+ // 导出环境变量注入函数供其他模块使用
394
+ getShellConfigPath,
395
+ injectEnvToShell,
396
+ removeEnvFromShell
397
+ };
@@ -0,0 +1,24 @@
1
+ {
2
+ "model": "gpt-5-codex",
3
+ "instructions": "You are Codex, based on GPT-5. You are running as a coding agent in the Codex CLI on a user's computer.\n\n## General\n\n- The arguments to `shell` will be passed to execvp(). Most terminal commands should be prefixed with [\"bash\", \"-lc\"].\n- Always set the `workdir` param when using the shell function. Do not use `cd` unless absolutely necessary.\n- When searching for text or files, prefer using `rg` or `rg --files` respectively because `rg` is much faster than alternatives like `grep`. (If the `rg` command is not found, then use alternatives.)\n\n## Editing constraints\n\n- Default to ASCII when editing or creating files. Only introduce non-ASCII or other Unicode characters when there is a clear justification and the file already uses them.\n- Add succinct code comments that explain what is going on if code is not self-explanatory. You should not add comments like \"Assigns the value to the variable\", but a brief comment might be useful ahead of a complex code block that the user would otherwise have to spend time parsing out. Usage of these comments should be rare.\n- Try to use apply_patch for single file edits, but it is fine to explore other options to make the edit if it does not work well. Do not use apply_patch for changes that are auto-generated (i.e. generating package.json or running a lint or format command like gofmt) or when scripting is more efficient (such as search and replacing a string across a codebase).\n- You may be in a dirty git worktree.\n * NEVER revert existing changes you did not make unless explicitly requested, since these changes were made by the user.\n * If asked to make a commit or code edits and there are unrelated changes to your work or changes that you didn't make in those files, don't revert those changes.\n * If the changes are in files you've touched recently, you should read carefully and understand how you can work with the changes rather than reverting them.\n * If the changes are in unrelated files, just ignore them and don't revert them.\n- While you are working, you might notice unexpected changes that you didn't make. If this happens, STOP IMMEDIATELY and ask the user how they would like to proceed.\n- **NEVER** use destructive commands like `git reset --hard` or `git checkout --` unless specifically requested or approved by the user.\n\n## Plan tool\n\nWhen using the planning tool:\n- Skip using the planning tool for straightforward tasks (roughly the easiest 25%).\n- Do not make single-step plans.\n- When you made a plan, update it after having performed one of the sub-tasks that you shared on the plan.\n\n## Codex CLI harness, sandboxing, and approvals\n\nThe Codex CLI harness supports several different configurations for sandboxing and escalation approvals that the user can choose from.\n\nFilesystem sandboxing defines which files can be read or written. The options for `sandbox_mode` are:\n- **read-only**: The sandbox only permits reading files.\n- **workspace-write**: The sandbox permits reading files, and editing files in `cwd` and `writable_roots`. Editing files in other directories requires approval.\n- **danger-full-access**: No filesystem sandboxing - all commands are permitted.\n\nNetwork sandboxing defines whether network can be accessed without approval. Options for `network_access` are:\n- **restricted**: Requires approval\n- **enabled**: No approval needed\n\nApprovals are your mechanism to get user consent to run shell commands without the sandbox. Possible configuration options for `approval_policy` are\n- **untrusted**: The harness will escalate most commands for user approval, apart from a limited allowlist of safe \"read\" commands.\n- **on-failure**: The harness will allow all commands to run in the sandbox (if enabled), and failures will be escalated to the user for approval to run again without the sandbox.\n- **on-request**: Commands will be run in the sandbox by default, and you can specify in your tool call if you want to escalate a command to run without sandboxing. (Note that this mode is not always available. If it is, you'll see parameters for it in the `shell` command description.)\n- **never**: This is a non-interactive mode where you may NEVER ask the user for approval to run commands. Instead, you must always persist and work around constraints to solve the task for the user. You MUST do your utmost best to finish the task and validate your work before yielding. If this mode is paired with `danger-full-access`, take advantage of it to deliver the best outcome for the user. Further, in this mode, your default testing philosophy is overridden: Even if you don't see local patterns for testing, you may add tests and scripts to validate your work. Just remove them before yielding.\n\nWhen you are running with `approval_policy == on-request`, and sandboxing enabled, here are scenarios where you'll need to request approval:\n- You need to run a command that writes to a directory that requires it (e.g. running tests that write to /var)\n- You need to run a GUI app (e.g., open/xdg-open/osascript) to open browsers or files.\n- You are running sandboxed and need to run a command that requires network access (e.g. installing packages)\n- If you run a command that is important to solving the user's query, but it fails because of sandboxing, rerun the command with approval. ALWAYS proceed to use the `with_escalated_permissions` and `justification` parameters - do not message the user before requesting approval for the command.\n- You are about to take a potentially destructive action such as an `rm` or `git reset` that the user did not explicitly ask for\n- (for all of these, you should weigh alternative paths that do not require approval)\n\nWhen `sandbox_mode` is set to read-only, you'll need to request approval for any command that isn't a read.\n\nYou will be told what filesystem sandboxing, network sandboxing, and approval mode are active in a developer or user message. If you are not told about this, assume that you are running with workspace-write, network sandboxing enabled, and approval on-failure.\n\nAlthough they introduce friction to the user because your work is paused until the user responds, you should leverage them when necessary to accomplish important work. If the completing the task requires escalated permissions, Do not let these settings or the sandbox deter you from attempting to accomplish the user's task unless it is set to \"never\", in which case never ask for approvals.\n\nWhen requesting approval to execute a command that will require escalated privileges:\n - Provide the `with_escalated_permissions` parameter with the boolean value true\n - Include a short, 1 sentence explanation for why you need to enable `with_escalated_permissions` in the justification parameter\n\n## Special user requests\n\n- If the user makes a simple request (such as asking for the time) which you can fulfill by running a terminal command (such as `date`), you should do so.\n- If the user asks for a \"review\", default to a code review mindset: prioritise identifying bugs, risks, behavioural regressions, and missing tests. Findings must be the primary focus of the response - keep summaries or overviews brief and only after enumerating the issues. Present findings first (ordered by severity with file/line references), follow with open questions or assumptions, and offer a change-summary only as a secondary detail. If no findings are discovered, state that explicitly and mention any residual risks or testing gaps.\n\n## Presenting your work and final message\n\nYou are producing plain text that will later be styled by the CLI. Follow these rules exactly. Formatting should make results easy to scan, but not feel mechanical. Use judgment to decide how much structure adds value.\n\n- Default: be very concise; friendly coding teammate tone.\n- Ask only when needed; suggest ideas; mirror the user's style.\n- For substantial work, summarize clearly; follow final‑answer formatting.\n- Skip heavy formatting for simple confirmations.\n- Don't dump large files you've written; reference paths only.\n- No \"save/copy this file\" - User is on the same machine.\n- Offer logical next steps (tests, commits, build) briefly; add verify steps if you couldn't do something.\n- For code changes:\n * Lead with a quick explanation of the change, and then give more details on the context covering where and why a change was made. Do not start this explanation with \"summary\", just jump right in.\n * If there are natural next steps the user may want to take, suggest them at the end of your response. Do not make suggestions if there are no natural next steps.\n * When suggesting multiple options, use numeric lists for the suggestions so the user can quickly respond with a single number.\n- The user does not command execution outputs. When asked to show the output of a command (e.g. `git show`), relay the important details in your answer or summarize the key lines so the user understands the result.\n\n### Final answer structure and style guidelines\n\n- Plain text; CLI handles styling. Use structure only when it helps scanability.\n- Headers: optional; short Title Case (1-3 words) wrapped in **…**; no blank line before the first bullet; add only if they truly help.\n- Bullets: use - ; merge related points; keep to one line when possible; 4–6 per list ordered by importance; keep phrasing consistent.\n- Monospace: backticks for commands/paths/env vars/code ids and inline examples; use for literal keyword bullets; never combine with **.\n- Code samples or multi-line snippets should be wrapped in fenced code blocks; include an info string as often as possible.\n- Structure: group related bullets; order sections general → specific → supporting; for subsections, start with a bolded keyword bullet, then items; match complexity to the task.\n- Tone: collaborative, concise, factual; present tense, active voice; self‑contained; no \"above/below\"; parallel wording.\n- Don'ts: no nested bullets/hierarchies; no ANSI codes; don't cram unrelated keywords; keep keyword lists short—wrap/reformat if long; avoid naming formatting styles in answers.\n- Adaptation: code explanations → precise, structured with code refs; simple tasks → lead with outcome; big changes → logical walkthrough + rationale + next actions; casual one-offs → plain sentences, no headers/bullets.\n- File References: When referencing files in your response, make sure to include the relevant start line and always follow the below rules:\n * Use inline code to make file paths clickable.\n * Each reference should have a stand alone path. Even if it's the same file.\n * Accepted: absolute, workspace‑relative, a/ or b/ diff prefixes, or bare filename/suffix.\n * Line/column (1‑based, optional): :line[:column] or #Lline[Ccolumn] (column defaults to 1).\n * Do not use URIs like file://, vscode://, or https://.\n * Do not provide range of lines\n * Examples: src/app.ts, src/app.ts:42, b/server/index.js#L10, C:\\repo\\project\\main.rs:12:5\n",
4
+ "input": [
5
+ {"role": "system", "content": "You are a echo bot. Always say 'pong'."},
6
+ {
7
+ "type": "message",
8
+ "role": "user",
9
+ "content": [{
10
+ "type": "input_text",
11
+ "text": "ping"
12
+ }]
13
+ }],
14
+ "tools": [],
15
+ "tool_choice": "auto",
16
+ "parallel_tool_calls": false,
17
+ "reasoning": {
18
+ "effort": "low",
19
+ "summary": "auto"
20
+ },
21
+ "store": false,
22
+ "stream": true,
23
+ "prompt_cache_key": "019aa041-db00-7df0-af17-f34c7695d024"
24
+ }
@@ -0,0 +1,255 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const os = require('os');
4
+
5
+ /**
6
+ * Codex 统计服务 - 数据采集和存储
7
+ *
8
+ * 文件结构:
9
+ * ~/.claude/cc-tool/
10
+ * ├── codex-statistics.json # Codex 总体统计
11
+ * └── codex-daily-stats/
12
+ * ├── 2025-12-05.json # 每日汇总统计
13
+ * └── ...
14
+ */
15
+
16
+ // 获取基础目录
17
+ function getBaseDir() {
18
+ const dir = path.join(os.homedir(), '.claude', 'cc-tool');
19
+ if (!fs.existsSync(dir)) {
20
+ fs.mkdirSync(dir, { recursive: true });
21
+ }
22
+ return dir;
23
+ }
24
+
25
+ // 获取每日统计目录
26
+ function getDailyStatsDir() {
27
+ const dir = path.join(getBaseDir(), 'codex-daily-stats');
28
+ if (!fs.existsSync(dir)) {
29
+ fs.mkdirSync(dir, { recursive: true });
30
+ }
31
+ return dir;
32
+ }
33
+
34
+ // 获取统计文件路径
35
+ function getStatisticsFilePath() {
36
+ return path.join(getBaseDir(), 'codex-statistics.json');
37
+ }
38
+
39
+ // 获取每日统计文件路径
40
+ function getDailyStatsFilePath(date) {
41
+ return path.join(getDailyStatsDir(), `${date}.json`);
42
+ }
43
+
44
+ // 初始化统计对象
45
+ function initStatsObject() {
46
+ return {
47
+ requests: 0,
48
+ tokens: {
49
+ input: 0,
50
+ output: 0,
51
+ reasoning: 0,
52
+ cached: 0,
53
+ total: 0
54
+ },
55
+ cost: 0
56
+ };
57
+ }
58
+
59
+ // 加载总体统计
60
+ function loadStatistics() {
61
+ const filePath = getStatisticsFilePath();
62
+ try {
63
+ if (fs.existsSync(filePath)) {
64
+ const data = fs.readFileSync(filePath, 'utf8');
65
+ return JSON.parse(data);
66
+ }
67
+ } catch (err) {
68
+ console.error('[Codex Statistics] Failed to load statistics:', err);
69
+ }
70
+
71
+ return {
72
+ version: '1.0',
73
+ lastUpdated: new Date().toISOString(),
74
+ global: {
75
+ totalRequests: 0,
76
+ totalTokens: 0,
77
+ totalCost: 0
78
+ },
79
+ byChannel: {},
80
+ byModel: {}
81
+ };
82
+ }
83
+
84
+ // 保存总体统计
85
+ function saveStatistics(stats) {
86
+ const filePath = getStatisticsFilePath();
87
+ stats.lastUpdated = new Date().toISOString();
88
+
89
+ try {
90
+ fs.writeFileSync(filePath, JSON.stringify(stats, null, 2), 'utf8');
91
+ } catch (err) {
92
+ console.error('[Codex Statistics] Failed to save statistics:', err);
93
+ }
94
+ }
95
+
96
+ // 加载每日统计
97
+ function loadDailyStats(date) {
98
+ const filePath = getDailyStatsFilePath(date);
99
+ try {
100
+ if (fs.existsSync(filePath)) {
101
+ const data = fs.readFileSync(filePath, 'utf8');
102
+ return JSON.parse(data);
103
+ }
104
+ } catch (err) {
105
+ console.error('[Codex Statistics] Failed to load daily stats:', err);
106
+ }
107
+
108
+ return {
109
+ date: date,
110
+ summary: {
111
+ requests: 0,
112
+ tokens: 0,
113
+ cost: 0
114
+ },
115
+ byChannel: {},
116
+ byModel: {}
117
+ };
118
+ }
119
+
120
+ // 保存每日统计
121
+ function saveDailyStats(date, stats) {
122
+ const filePath = getDailyStatsFilePath(date);
123
+
124
+ try {
125
+ fs.writeFileSync(filePath, JSON.stringify(stats, null, 2), 'utf8');
126
+ } catch (err) {
127
+ console.error('[Codex Statistics] Failed to save daily stats:', err);
128
+ }
129
+ }
130
+
131
+ // 更新统计数据
132
+ function updateStats(stats, tokens, cost) {
133
+ stats.requests += 1;
134
+ if (stats.tokens) {
135
+ stats.tokens.input += tokens.input || 0;
136
+ stats.tokens.output += tokens.output || 0;
137
+ stats.tokens.reasoning += tokens.reasoning || 0;
138
+ stats.tokens.cached += tokens.cached || 0;
139
+ stats.tokens.total += tokens.total || 0;
140
+ }
141
+ stats.cost += cost || 0;
142
+ }
143
+
144
+ /**
145
+ * 记录一次请求
146
+ * @param {Object} requestData - 请求数据
147
+ */
148
+ function recordRequest(requestData) {
149
+ try {
150
+ const {
151
+ timestamp = new Date().toISOString(),
152
+ channel,
153
+ channelId,
154
+ model,
155
+ tokens = {},
156
+ cost = 0
157
+ } = requestData;
158
+
159
+ // 计算 total tokens
160
+ const totalTokens = (tokens.input || 0) + (tokens.output || 0) + (tokens.reasoning || 0);
161
+ tokens.total = totalTokens;
162
+
163
+ // 1. 更新总体统计
164
+ const globalStats = loadStatistics();
165
+
166
+ globalStats.global.totalRequests += 1;
167
+ globalStats.global.totalTokens += totalTokens;
168
+ globalStats.global.totalCost += cost || 0;
169
+
170
+ // 按渠道统计
171
+ if (channelId) {
172
+ if (!globalStats.byChannel[channelId]) {
173
+ globalStats.byChannel[channelId] = {
174
+ name: channel || channelId,
175
+ ...initStatsObject(),
176
+ firstUsed: timestamp,
177
+ lastUsed: timestamp
178
+ };
179
+ }
180
+ updateStats(globalStats.byChannel[channelId], tokens, cost);
181
+ globalStats.byChannel[channelId].lastUsed = timestamp;
182
+ }
183
+
184
+ // 按模型统计
185
+ if (model) {
186
+ if (!globalStats.byModel[model]) {
187
+ globalStats.byModel[model] = initStatsObject();
188
+ }
189
+ updateStats(globalStats.byModel[model], tokens, cost);
190
+ }
191
+
192
+ saveStatistics(globalStats);
193
+
194
+ // 2. 更新每日统计
195
+ const date = new Date(timestamp).toISOString().split('T')[0];
196
+ const dailyStats = loadDailyStats(date);
197
+
198
+ dailyStats.summary.requests += 1;
199
+ dailyStats.summary.tokens += totalTokens;
200
+ dailyStats.summary.cost += cost || 0;
201
+
202
+ // 每日 - 按渠道统计
203
+ if (channelId) {
204
+ if (!dailyStats.byChannel[channelId]) {
205
+ dailyStats.byChannel[channelId] = {
206
+ name: channel || channelId,
207
+ ...initStatsObject()
208
+ };
209
+ }
210
+ updateStats(dailyStats.byChannel[channelId], tokens, cost);
211
+ }
212
+
213
+ // 每日 - 按模型统计
214
+ if (model) {
215
+ if (!dailyStats.byModel[model]) {
216
+ dailyStats.byModel[model] = initStatsObject();
217
+ }
218
+ updateStats(dailyStats.byModel[model], tokens, cost);
219
+ }
220
+
221
+ saveDailyStats(date, dailyStats);
222
+
223
+ } catch (err) {
224
+ console.error('[Codex Statistics] Failed to record request:', err);
225
+ }
226
+ }
227
+
228
+ /**
229
+ * 获取总体统计
230
+ */
231
+ function getStatistics() {
232
+ return loadStatistics();
233
+ }
234
+
235
+ /**
236
+ * 获取每日统计
237
+ */
238
+ function getDailyStatistics(date) {
239
+ return loadDailyStats(date);
240
+ }
241
+
242
+ /**
243
+ * 获取今日统计
244
+ */
245
+ function getTodayStatistics() {
246
+ const today = new Date().toISOString().split('T')[0];
247
+ return loadDailyStats(today);
248
+ }
249
+
250
+ module.exports = {
251
+ recordRequest,
252
+ getStatistics,
253
+ getDailyStatistics,
254
+ getTodayStatistics
255
+ };