@pixelbyte-software/pixcode 1.35.2 → 1.35.4

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 (228) hide show
  1. package/LICENSE +718 -718
  2. package/README.de.md +248 -248
  3. package/README.ja.md +240 -240
  4. package/README.ko.md +240 -240
  5. package/README.md +303 -303
  6. package/README.ru.md +248 -248
  7. package/README.tr.md +250 -250
  8. package/README.zh-CN.md +240 -240
  9. package/dist/api-docs.html +548 -548
  10. package/dist/assets/index-BwmhA_le.css +32 -0
  11. package/dist/assets/{index-D1-AIL_5.js → index-CyxRiNt0.js} +182 -182
  12. package/dist/clear-cache.html +85 -85
  13. package/dist/convert-icons.md +52 -52
  14. package/dist/favicon.svg +8 -8
  15. package/dist/generate-icons.js +48 -48
  16. package/dist/icons/codex-white.svg +3 -3
  17. package/dist/icons/codex.svg +3 -3
  18. package/dist/icons/cursor-white.svg +11 -11
  19. package/dist/icons/icon-128x128.svg +9 -9
  20. package/dist/icons/icon-144x144.svg +9 -9
  21. package/dist/icons/icon-152x152.svg +9 -9
  22. package/dist/icons/icon-192x192.svg +9 -9
  23. package/dist/icons/icon-384x384.svg +9 -9
  24. package/dist/icons/icon-512x512.svg +9 -9
  25. package/dist/icons/icon-72x72.svg +9 -9
  26. package/dist/icons/icon-96x96.svg +9 -9
  27. package/dist/icons/icon-template.svg +9 -9
  28. package/dist/icons/qwen-logo.svg +14 -14
  29. package/dist/index.html +59 -59
  30. package/dist/logo.svg +12 -12
  31. package/dist/manifest.json +60 -60
  32. package/dist/openapi.yaml +1693 -1693
  33. package/dist/sw.js +124 -124
  34. package/dist-server/server/cli.js +96 -96
  35. package/dist-server/server/cli.js.map +1 -1
  36. package/dist-server/server/cursor-cli.js.map +1 -1
  37. package/dist-server/server/daemon/manager.js +33 -33
  38. package/dist-server/server/daemon-manager.js +64 -64
  39. package/dist-server/server/gemini-cli.js +4 -4
  40. package/dist-server/server/gemini-cli.js.map +1 -1
  41. package/dist-server/server/index.js +11 -11
  42. package/dist-server/server/index.js.map +1 -1
  43. package/dist-server/server/load-env.js.map +1 -1
  44. package/dist-server/server/middleware/auth.js.map +1 -1
  45. package/dist-server/server/modules/orchestration/tasks/orchestration-task.routes.js.map +1 -1
  46. package/dist-server/server/modules/providers/list/claude/claude-auth.provider.js +1 -1
  47. package/dist-server/server/modules/providers/list/claude/claude-auth.provider.js.map +1 -1
  48. package/dist-server/server/modules/providers/list/codex/codex-auth.provider.js +1 -1
  49. package/dist-server/server/modules/providers/list/codex/codex-auth.provider.js.map +1 -1
  50. package/dist-server/server/modules/providers/list/gemini/gemini-auth.provider.js +1 -1
  51. package/dist-server/server/modules/providers/list/gemini/gemini-auth.provider.js.map +1 -1
  52. package/dist-server/server/modules/providers/list/opencode/opencode-auth.provider.js +1 -1
  53. package/dist-server/server/modules/providers/list/opencode/opencode-auth.provider.js.map +1 -1
  54. package/dist-server/server/modules/providers/list/qwen/qwen-auth.provider.js +1 -1
  55. package/dist-server/server/modules/providers/list/qwen/qwen-auth.provider.js.map +1 -1
  56. package/dist-server/server/modules/providers/provider.routes.js +3 -6
  57. package/dist-server/server/modules/providers/provider.routes.js.map +1 -1
  58. package/dist-server/server/opencode-cli.js +1 -1
  59. package/dist-server/server/opencode-cli.js.map +1 -1
  60. package/dist-server/server/projects.js +2 -3
  61. package/dist-server/server/projects.js.map +1 -1
  62. package/dist-server/server/qwen-code-cli.js +1 -1
  63. package/dist-server/server/qwen-code-cli.js.map +1 -1
  64. package/dist-server/server/routes/agent.js +3 -3
  65. package/dist-server/server/routes/agent.js.map +1 -1
  66. package/dist-server/server/routes/auth.js.map +1 -1
  67. package/dist-server/server/routes/codex.js.map +1 -1
  68. package/dist-server/server/routes/commands.js +26 -26
  69. package/dist-server/server/routes/commands.js.map +1 -1
  70. package/dist-server/server/routes/cursor.js +1 -1
  71. package/dist-server/server/routes/cursor.js.map +1 -1
  72. package/dist-server/server/routes/gemini.js.map +1 -1
  73. package/dist-server/server/routes/git.js +18 -18
  74. package/dist-server/server/routes/git.js.map +1 -1
  75. package/dist-server/server/routes/mcp-utils.js.map +1 -1
  76. package/dist-server/server/routes/messages.js.map +1 -1
  77. package/dist-server/server/routes/network.js +1 -1
  78. package/dist-server/server/routes/network.js.map +1 -1
  79. package/dist-server/server/routes/plugins.js +2 -2
  80. package/dist-server/server/routes/plugins.js.map +1 -1
  81. package/dist-server/server/routes/projects.js +1 -1
  82. package/dist-server/server/routes/projects.js.map +1 -1
  83. package/dist-server/server/routes/settings.js.map +1 -1
  84. package/dist-server/server/routes/taskmaster.js +423 -424
  85. package/dist-server/server/routes/taskmaster.js.map +1 -1
  86. package/dist-server/server/routes/user.js +1 -1
  87. package/dist-server/server/routes/user.js.map +1 -1
  88. package/dist-server/server/services/external-access.js +0 -1
  89. package/dist-server/server/services/external-access.js.map +1 -1
  90. package/dist-server/server/services/notification-orchestrator.js.map +1 -1
  91. package/dist-server/server/utils/commandParser.js.map +1 -1
  92. package/dist-server/server/utils/plugin-process-manager.js.map +1 -1
  93. package/dist-server/server/vite-daemon.js.map +1 -1
  94. package/package.json +180 -180
  95. package/scripts/fix-node-pty.js +67 -67
  96. package/scripts/smoke/a2a-roundtrip.mjs +167 -167
  97. package/scripts/smoke/orchestration-api.mjs +172 -172
  98. package/scripts/smoke/orchestration-live-run.mjs +176 -176
  99. package/server/claude-sdk.js +898 -898
  100. package/server/cli.js +936 -935
  101. package/server/constants/config.js +4 -4
  102. package/server/cursor-cli.js +344 -342
  103. package/server/daemon/manager.js +564 -564
  104. package/server/daemon-manager.js +959 -959
  105. package/server/database/db.js +794 -794
  106. package/server/database/json-store.js +197 -197
  107. package/server/gemini-cli.js +536 -535
  108. package/server/gemini-response-handler.js +79 -79
  109. package/server/index.js +3138 -3135
  110. package/server/load-env.js +35 -34
  111. package/server/middleware/auth.js +174 -173
  112. package/server/modules/orchestration/a2a/adapter-registry.ts +108 -108
  113. package/server/modules/orchestration/a2a/adapters/abstract-a2a.adapter.ts +55 -55
  114. package/server/modules/orchestration/a2a/adapters/claude-code.adapter.ts +284 -284
  115. package/server/modules/orchestration/a2a/adapters/codex.adapter.ts +244 -244
  116. package/server/modules/orchestration/a2a/adapters/cursor.adapter.ts +249 -249
  117. package/server/modules/orchestration/a2a/adapters/gemini.adapter.ts +248 -248
  118. package/server/modules/orchestration/a2a/adapters/opencode.adapter.ts +248 -248
  119. package/server/modules/orchestration/a2a/adapters/qwen.adapter.ts +248 -248
  120. package/server/modules/orchestration/a2a/routes.ts +577 -577
  121. package/server/modules/orchestration/a2a/task-store.ts +178 -178
  122. package/server/modules/orchestration/a2a/types.ts +125 -125
  123. package/server/modules/orchestration/a2a/validator.ts +113 -113
  124. package/server/modules/orchestration/index.ts +66 -66
  125. package/server/modules/orchestration/preview/port-watcher.ts +112 -112
  126. package/server/modules/orchestration/preview/preview-proxy.ts +60 -60
  127. package/server/modules/orchestration/preview/types.ts +19 -19
  128. package/server/modules/orchestration/tasks/orchestration-task-store.ts +45 -45
  129. package/server/modules/orchestration/tasks/orchestration-task.routes.ts +74 -73
  130. package/server/modules/orchestration/tasks/orchestration-task.service.ts +145 -145
  131. package/server/modules/orchestration/tasks/orchestration-task.types.ts +29 -29
  132. package/server/modules/orchestration/workflows/built-in-workflows.ts +127 -127
  133. package/server/modules/orchestration/workflows/workflow-runner.ts +1206 -1206
  134. package/server/modules/orchestration/workflows/workflow-store.ts +97 -97
  135. package/server/modules/orchestration/workflows/workflow.routes.ts +169 -169
  136. package/server/modules/orchestration/workflows/workflow.types.ts +70 -70
  137. package/server/modules/orchestration/workflows/workspace-target.ts +120 -120
  138. package/server/modules/orchestration/workspace/docker-workspace.ts +135 -135
  139. package/server/modules/orchestration/workspace/path-safety.ts +55 -55
  140. package/server/modules/orchestration/workspace/types.ts +52 -52
  141. package/server/modules/orchestration/workspace/workspace-manager.ts +97 -97
  142. package/server/modules/orchestration/workspace/worktree-workspace.ts +125 -125
  143. package/server/modules/providers/index.ts +2 -2
  144. package/server/modules/providers/list/claude/claude-auth.provider.ts +146 -145
  145. package/server/modules/providers/list/claude/claude-mcp.provider.ts +135 -135
  146. package/server/modules/providers/list/claude/claude-sessions.provider.ts +306 -306
  147. package/server/modules/providers/list/claude/claude.provider.ts +15 -15
  148. package/server/modules/providers/list/codex/codex-auth.provider.ts +116 -115
  149. package/server/modules/providers/list/codex/codex-mcp.provider.ts +135 -135
  150. package/server/modules/providers/list/codex/codex-sessions.provider.ts +319 -319
  151. package/server/modules/providers/list/codex/codex.provider.ts +15 -15
  152. package/server/modules/providers/list/cursor/cursor-auth.provider.ts +143 -143
  153. package/server/modules/providers/list/cursor/cursor-mcp.provider.ts +108 -108
  154. package/server/modules/providers/list/cursor/cursor-sessions.provider.ts +421 -421
  155. package/server/modules/providers/list/cursor/cursor.provider.ts +15 -15
  156. package/server/modules/providers/list/gemini/gemini-auth.provider.ts +164 -163
  157. package/server/modules/providers/list/gemini/gemini-mcp.provider.ts +110 -110
  158. package/server/modules/providers/list/gemini/gemini-sessions.provider.ts +227 -227
  159. package/server/modules/providers/list/gemini/gemini.provider.ts +15 -15
  160. package/server/modules/providers/list/opencode/opencode-auth.provider.ts +131 -130
  161. package/server/modules/providers/list/opencode/opencode-mcp.provider.ts +126 -126
  162. package/server/modules/providers/list/opencode/opencode-sessions.provider.ts +232 -232
  163. package/server/modules/providers/list/opencode/opencode.provider.ts +29 -29
  164. package/server/modules/providers/list/qwen/qwen-auth.provider.ts +146 -145
  165. package/server/modules/providers/list/qwen/qwen-mcp.provider.ts +114 -114
  166. package/server/modules/providers/list/qwen/qwen-sessions.provider.ts +265 -265
  167. package/server/modules/providers/list/qwen/qwen.provider.ts +21 -21
  168. package/server/modules/providers/provider.registry.ts +40 -40
  169. package/server/modules/providers/provider.routes.ts +822 -819
  170. package/server/modules/providers/services/mcp.service.ts +86 -86
  171. package/server/modules/providers/services/provider-auth.service.ts +26 -26
  172. package/server/modules/providers/services/sessions.service.ts +45 -45
  173. package/server/modules/providers/shared/base/abstract.provider.ts +20 -20
  174. package/server/modules/providers/shared/mcp/mcp.provider.ts +151 -151
  175. package/server/modules/providers/shared/provider-configs.ts +142 -142
  176. package/server/modules/providers/tests/mcp.test.ts +293 -293
  177. package/server/openai-codex.js +462 -462
  178. package/server/opencode-cli.js +460 -459
  179. package/server/opencode-response-handler.js +107 -107
  180. package/server/projects.js +3106 -3105
  181. package/server/qwen-code-cli.js +396 -395
  182. package/server/qwen-response-handler.js +73 -73
  183. package/server/routes/agent.js +1367 -1365
  184. package/server/routes/auth.js +139 -138
  185. package/server/routes/codex.js +20 -19
  186. package/server/routes/commands.js +556 -554
  187. package/server/routes/cursor.js +54 -52
  188. package/server/routes/gemini.js +25 -24
  189. package/server/routes/git.js +1490 -1488
  190. package/server/routes/mcp-utils.js +32 -31
  191. package/server/routes/messages.js +62 -61
  192. package/server/routes/network.js +121 -120
  193. package/server/routes/plugins.js +320 -318
  194. package/server/routes/projects.js +917 -915
  195. package/server/routes/qwen.js +27 -27
  196. package/server/routes/settings.js +287 -286
  197. package/server/routes/taskmaster.js +1498 -1496
  198. package/server/routes/telegram.js +125 -125
  199. package/server/routes/user.js +125 -123
  200. package/server/services/external-access.js +171 -171
  201. package/server/services/install-jobs.js +571 -571
  202. package/server/services/notification-orchestrator.js +244 -242
  203. package/server/services/provider-credentials.js +189 -189
  204. package/server/services/provider-models.js +381 -381
  205. package/server/services/telegram/bot.js +279 -279
  206. package/server/services/telegram/telegram-http-client.js +130 -130
  207. package/server/services/telegram/translations.js +170 -170
  208. package/server/services/vapid-keys.js +36 -36
  209. package/server/sessionManager.js +225 -225
  210. package/server/shared/interfaces.ts +54 -54
  211. package/server/shared/types.ts +172 -172
  212. package/server/shared/utils.ts +193 -193
  213. package/server/tsconfig.json +36 -36
  214. package/server/utils/colors.js +21 -21
  215. package/server/utils/commandParser.js +305 -303
  216. package/server/utils/frontmatter.js +18 -18
  217. package/server/utils/gitConfig.js +34 -34
  218. package/server/utils/mcp-detector.js +147 -147
  219. package/server/utils/plugin-loader.js +457 -457
  220. package/server/utils/plugin-process-manager.js +185 -184
  221. package/server/utils/port-access.js +209 -209
  222. package/server/utils/runtime-paths.js +37 -37
  223. package/server/utils/taskmaster-websocket.js +128 -128
  224. package/server/utils/url-detection.js +71 -71
  225. package/server/vite-daemon.js +79 -78
  226. package/shared/modelConstants.js +162 -162
  227. package/shared/networkHosts.js +22 -22
  228. package/dist/assets/index-B8w57E1r.css +0 -32
@@ -1,554 +1,556 @@
1
- import express from 'express';
2
- import { promises as fs } from 'fs';
3
- import path from 'path';
4
- import os from 'os';
5
- import { CLAUDE_MODELS, CURSOR_MODELS, CODEX_MODELS } from '../../shared/modelConstants.js';
6
- import { parseFrontmatter } from '../utils/frontmatter.js';
7
- import { findAppRoot, getModuleDir } from '../utils/runtime-paths.js';
8
-
9
- const __dirname = getModuleDir(import.meta.url);
10
- // This route reads the top-level package.json for the status command, so it needs the real
11
- // app root even after compilation moves the route file under dist-server/server/routes.
12
- const APP_ROOT = findAppRoot(__dirname);
13
-
14
- const router = express.Router();
15
-
16
- /**
17
- * Recursively scan directory for command files (.md)
18
- * @param {string} dir - Directory to scan
19
- * @param {string} baseDir - Base directory for relative paths
20
- * @param {string} namespace - Namespace for commands (e.g., 'project', 'user')
21
- * @returns {Promise<Array>} Array of command objects
22
- */
23
- async function scanCommandsDirectory(dir, baseDir, namespace) {
24
- const commands = [];
25
-
26
- try {
27
- // Check if directory exists
28
- await fs.access(dir);
29
-
30
- const entries = await fs.readdir(dir, { withFileTypes: true });
31
-
32
- for (const entry of entries) {
33
- const fullPath = path.join(dir, entry.name);
34
-
35
- if (entry.isDirectory()) {
36
- // Recursively scan subdirectories
37
- const subCommands = await scanCommandsDirectory(fullPath, baseDir, namespace);
38
- commands.push(...subCommands);
39
- } else if (entry.isFile() && entry.name.endsWith('.md')) {
40
- // Parse markdown file for metadata
41
- try {
42
- const content = await fs.readFile(fullPath, 'utf8');
43
- const { data: frontmatter, content: commandContent } = parseFrontmatter(content);
44
-
45
- // Calculate relative path from baseDir for command name
46
- const relativePath = path.relative(baseDir, fullPath);
47
- // Remove .md extension and convert to command name
48
- const commandName = '/' + relativePath.replace(/\.md$/, '').replace(/\\/g, '/');
49
-
50
- // Extract description from frontmatter or first line of content
51
- let description = frontmatter.description || '';
52
- if (!description) {
53
- const firstLine = commandContent.trim().split('\n')[0];
54
- description = firstLine.replace(/^#+\s*/, '').trim();
55
- }
56
-
57
- commands.push({
58
- name: commandName,
59
- path: fullPath,
60
- relativePath,
61
- description,
62
- namespace,
63
- metadata: frontmatter
64
- });
65
- } catch (err) {
66
- console.error(`Error parsing command file ${fullPath}:`, err.message);
67
- }
68
- }
69
- }
70
- } catch (err) {
71
- // Directory doesn't exist or can't be accessed - this is okay
72
- if (err.code !== 'ENOENT' && err.code !== 'EACCES') {
73
- console.error(`Error scanning directory ${dir}:`, err.message);
74
- }
75
- }
76
-
77
- return commands;
78
- }
79
-
80
- /**
81
- * Built-in commands that are always available
82
- */
83
- const builtInCommands = [
84
- {
85
- name: '/help',
86
- description: 'Show help documentation for Claude Code',
87
- namespace: 'builtin',
88
- metadata: { type: 'builtin' }
89
- },
90
- {
91
- name: '/clear',
92
- description: 'Clear the conversation history',
93
- namespace: 'builtin',
94
- metadata: { type: 'builtin' }
95
- },
96
- {
97
- name: '/model',
98
- description: 'Switch or view the current AI model',
99
- namespace: 'builtin',
100
- metadata: { type: 'builtin' }
101
- },
102
- {
103
- name: '/cost',
104
- description: 'Display token usage and cost information',
105
- namespace: 'builtin',
106
- metadata: { type: 'builtin' }
107
- },
108
- {
109
- name: '/memory',
110
- description: 'Open CLAUDE.md memory file for editing',
111
- namespace: 'builtin',
112
- metadata: { type: 'builtin' }
113
- },
114
- {
115
- name: '/config',
116
- description: 'Open settings and configuration',
117
- namespace: 'builtin',
118
- metadata: { type: 'builtin' }
119
- },
120
- {
121
- name: '/status',
122
- description: 'Show system status and version information',
123
- namespace: 'builtin',
124
- metadata: { type: 'builtin' }
125
- },
126
- {
127
- name: '/rewind',
128
- description: 'Rewind the conversation to a previous state',
129
- namespace: 'builtin',
130
- metadata: { type: 'builtin' }
131
- }
132
- ];
133
-
134
- /**
135
- * Built-in command handlers
136
- * Each handler returns { type: 'builtin', action: string, data: any }
137
- */
138
- const builtInHandlers = {
139
- '/help': async (args, context) => {
140
- const helpText = `# Claude Code Commands
141
-
142
- ## Built-in Commands
143
-
144
- ${builtInCommands.map(cmd => `### ${cmd.name}
145
- ${cmd.description}
146
- `).join('\n')}
147
-
148
- ## Custom Commands
149
-
150
- Custom commands can be created in:
151
- - Project: \`.claude/commands/\` (project-specific)
152
- - User: \`~/.claude/commands/\` (available in all projects)
153
-
154
- ### Command Syntax
155
-
156
- - **Arguments**: Use \`$ARGUMENTS\` for all args or \`$1\`, \`$2\`, etc. for positional
157
- - **File Includes**: Use \`@filename\` to include file contents
158
- - **Bash Commands**: Use \`!command\` to execute bash commands
159
-
160
- ### Examples
161
-
162
- \`\`\`markdown
163
- /mycommand arg1 arg2
164
- \`\`\`
165
- `;
166
-
167
- return {
168
- type: 'builtin',
169
- action: 'help',
170
- data: {
171
- content: helpText,
172
- format: 'markdown'
173
- }
174
- };
175
- },
176
-
177
- '/clear': async (args, context) => {
178
- return {
179
- type: 'builtin',
180
- action: 'clear',
181
- data: {
182
- message: 'Conversation history cleared'
183
- }
184
- };
185
- },
186
-
187
- '/model': async (args, context) => {
188
- // Read available models from centralized constants
189
- const availableModels = {
190
- claude: CLAUDE_MODELS.OPTIONS.map(o => o.value),
191
- cursor: CURSOR_MODELS.OPTIONS.map(o => o.value),
192
- codex: CODEX_MODELS.OPTIONS.map(o => o.value)
193
- };
194
-
195
- const currentProvider = context?.provider || 'claude';
196
- const currentModel = context?.model || CLAUDE_MODELS.DEFAULT;
197
-
198
- return {
199
- type: 'builtin',
200
- action: 'model',
201
- data: {
202
- current: {
203
- provider: currentProvider,
204
- model: currentModel
205
- },
206
- available: availableModels,
207
- message: args.length > 0
208
- ? `Switching to model: ${args[0]}`
209
- : `Current model: ${currentModel}`
210
- }
211
- };
212
- },
213
-
214
- '/cost': async (args, context) => {
215
- const tokenUsage = context?.tokenUsage || {};
216
- const provider = context?.provider || 'claude';
217
- const model =
218
- context?.model ||
219
- (provider === 'cursor'
220
- ? CURSOR_MODELS.DEFAULT
221
- : provider === 'codex'
222
- ? CODEX_MODELS.DEFAULT
223
- : CLAUDE_MODELS.DEFAULT);
224
-
225
- const used = Number(tokenUsage.used ?? tokenUsage.totalUsed ?? tokenUsage.total_tokens ?? 0) || 0;
226
- const total =
227
- Number(
228
- tokenUsage.total ??
229
- tokenUsage.contextWindow ??
230
- parseInt(process.env.CONTEXT_WINDOW || '160000', 10),
231
- ) || 160000;
232
- const percentage = total > 0 ? Number(((used / total) * 100).toFixed(1)) : 0;
233
-
234
- const inputTokensRaw =
235
- Number(
236
- tokenUsage.inputTokens ??
237
- tokenUsage.input ??
238
- tokenUsage.cumulativeInputTokens ??
239
- tokenUsage.promptTokens ??
240
- 0,
241
- ) || 0;
242
- const outputTokens =
243
- Number(
244
- tokenUsage.outputTokens ??
245
- tokenUsage.output ??
246
- tokenUsage.cumulativeOutputTokens ??
247
- tokenUsage.completionTokens ??
248
- 0,
249
- ) || 0;
250
- const cacheTokens =
251
- Number(
252
- tokenUsage.cacheReadTokens ??
253
- tokenUsage.cacheCreationTokens ??
254
- tokenUsage.cacheTokens ??
255
- tokenUsage.cachedTokens ??
256
- 0,
257
- ) || 0;
258
-
259
- // If we only have total used tokens, treat them as input for display/estimation.
260
- const inputTokens =
261
- inputTokensRaw > 0 || outputTokens > 0 || cacheTokens > 0 ? inputTokensRaw + cacheTokens : used;
262
-
263
- // Rough default rates by provider (USD / 1M tokens).
264
- const pricingByProvider = {
265
- claude: { input: 3, output: 15 },
266
- cursor: { input: 3, output: 15 },
267
- codex: { input: 1.5, output: 6 },
268
- };
269
- const rates = pricingByProvider[provider] || pricingByProvider.claude;
270
-
271
- const inputCost = (inputTokens / 1_000_000) * rates.input;
272
- const outputCost = (outputTokens / 1_000_000) * rates.output;
273
- const totalCost = inputCost + outputCost;
274
-
275
- return {
276
- type: 'builtin',
277
- action: 'cost',
278
- data: {
279
- tokenUsage: {
280
- used,
281
- total,
282
- percentage,
283
- },
284
- cost: {
285
- input: inputCost.toFixed(4),
286
- output: outputCost.toFixed(4),
287
- total: totalCost.toFixed(4),
288
- },
289
- model,
290
- },
291
- };
292
- },
293
-
294
- '/status': async (args, context) => {
295
- // Read version from package.json
296
- const packageJsonPath = path.join(APP_ROOT, 'package.json');
297
- let version = 'unknown';
298
- let packageName = '@pixelbyte-software/pixcode';
299
-
300
- try {
301
- const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
302
- version = packageJson.version;
303
- packageName = packageJson.name;
304
- } catch (err) {
305
- console.error('Error reading package.json:', err);
306
- }
307
-
308
- const uptime = process.uptime();
309
- const uptimeMinutes = Math.floor(uptime / 60);
310
- const uptimeHours = Math.floor(uptimeMinutes / 60);
311
- const uptimeFormatted = uptimeHours > 0
312
- ? `${uptimeHours}h ${uptimeMinutes % 60}m`
313
- : `${uptimeMinutes}m`;
314
-
315
- return {
316
- type: 'builtin',
317
- action: 'status',
318
- data: {
319
- version,
320
- packageName,
321
- uptime: uptimeFormatted,
322
- uptimeSeconds: Math.floor(uptime),
323
- model: context?.model || 'claude-sonnet-4.5',
324
- provider: context?.provider || 'claude',
325
- nodeVersion: process.version,
326
- platform: process.platform
327
- }
328
- };
329
- },
330
-
331
- '/memory': async (args, context) => {
332
- const projectPath = context?.projectPath;
333
-
334
- if (!projectPath) {
335
- return {
336
- type: 'builtin',
337
- action: 'memory',
338
- data: {
339
- error: 'No project selected',
340
- message: 'Please select a project to access its CLAUDE.md file'
341
- }
342
- };
343
- }
344
-
345
- const claudeMdPath = path.join(projectPath, 'CLAUDE.md');
346
-
347
- // Check if CLAUDE.md exists
348
- let exists = false;
349
- try {
350
- await fs.access(claudeMdPath);
351
- exists = true;
352
- } catch (err) {
353
- // File doesn't exist
354
- }
355
-
356
- return {
357
- type: 'builtin',
358
- action: 'memory',
359
- data: {
360
- path: claudeMdPath,
361
- exists,
362
- message: exists
363
- ? `Opening CLAUDE.md at ${claudeMdPath}`
364
- : `CLAUDE.md not found at ${claudeMdPath}. Create it to store project-specific instructions.`
365
- }
366
- };
367
- },
368
-
369
- '/config': async (args, context) => {
370
- return {
371
- type: 'builtin',
372
- action: 'config',
373
- data: {
374
- message: 'Opening settings...'
375
- }
376
- };
377
- },
378
-
379
- '/rewind': async (args, context) => {
380
- const steps = args[0] ? parseInt(args[0]) : 1;
381
-
382
- if (isNaN(steps) || steps < 1) {
383
- return {
384
- type: 'builtin',
385
- action: 'rewind',
386
- data: {
387
- error: 'Invalid steps parameter',
388
- message: 'Usage: /rewind [number] - Rewind conversation by N steps (default: 1)'
389
- }
390
- };
391
- }
392
-
393
- return {
394
- type: 'builtin',
395
- action: 'rewind',
396
- data: {
397
- steps,
398
- message: `Rewinding conversation by ${steps} step${steps > 1 ? 's' : ''}...`
399
- }
400
- };
401
- }
402
- };
403
-
404
- /**
405
- * POST /api/commands/list
406
- * List all available commands from project and user directories
407
- */
408
- router.post('/list', async (req, res) => {
409
- try {
410
- const { projectPath } = req.body;
411
- const allCommands = [...builtInCommands];
412
-
413
- // Scan project-level commands (.claude/commands/)
414
- if (projectPath) {
415
- const projectCommandsDir = path.join(projectPath, '.claude', 'commands');
416
- const projectCommands = await scanCommandsDirectory(
417
- projectCommandsDir,
418
- projectCommandsDir,
419
- 'project'
420
- );
421
- allCommands.push(...projectCommands);
422
- }
423
-
424
- // Scan user-level commands (~/.claude/commands/)
425
- const homeDir = os.homedir();
426
- const userCommandsDir = path.join(homeDir, '.claude', 'commands');
427
- const userCommands = await scanCommandsDirectory(
428
- userCommandsDir,
429
- userCommandsDir,
430
- 'user'
431
- );
432
- allCommands.push(...userCommands);
433
-
434
- // Separate built-in and custom commands
435
- const customCommands = allCommands.filter(cmd => cmd.namespace !== 'builtin');
436
-
437
- // Sort commands alphabetically by name
438
- customCommands.sort((a, b) => a.name.localeCompare(b.name));
439
-
440
- res.json({
441
- builtIn: builtInCommands,
442
- custom: customCommands,
443
- count: allCommands.length
444
- });
445
- } catch (error) {
446
- console.error('Error listing commands:', error);
447
- res.status(500).json({
448
- error: 'Failed to list commands',
449
- message: error.message
450
- });
451
- }
452
- });
453
-
454
- /**
455
- * POST /api/commands/execute
456
- * Execute a command with argument replacement
457
- * This endpoint prepares the command content but doesn't execute bash commands yet
458
- * (that will be handled in the command parser utility)
459
- */
460
- router.post('/execute', async (req, res) => {
461
- try {
462
- const { commandName, commandPath, args = [], context = {} } = req.body;
463
-
464
- if (!commandName) {
465
- return res.status(400).json({
466
- error: 'Command name is required'
467
- });
468
- }
469
-
470
- // Handle built-in commands
471
- const handler = builtInHandlers[commandName];
472
- if (handler) {
473
- try {
474
- const result = await handler(args, context);
475
- return res.json({
476
- ...result,
477
- command: commandName
478
- });
479
- } catch (error) {
480
- console.error(`Error executing built-in command ${commandName}:`, error);
481
- return res.status(500).json({
482
- error: 'Command execution failed',
483
- message: error.message,
484
- command: commandName
485
- });
486
- }
487
- }
488
-
489
- // Handle custom commands
490
- if (!commandPath) {
491
- return res.status(400).json({
492
- error: 'Command path is required for custom commands'
493
- });
494
- }
495
-
496
- // Load command content
497
- // Security: validate commandPath is within allowed directories
498
- {
499
- const resolvedPath = path.resolve(commandPath);
500
- const userBase = path.resolve(path.join(os.homedir(), '.claude', 'commands'));
501
- const projectBase = context?.projectPath
502
- ? path.resolve(path.join(context.projectPath, '.claude', 'commands'))
503
- : null;
504
- const isUnder = (base) => {
505
- const rel = path.relative(base, resolvedPath);
506
- return rel !== '' && !rel.startsWith('..') && !path.isAbsolute(rel);
507
- };
508
- if (!(isUnder(userBase) || (projectBase && isUnder(projectBase)))) {
509
- return res.status(403).json({
510
- error: 'Access denied',
511
- message: 'Command must be in .claude/commands directory'
512
- });
513
- }
514
- }
515
- const content = await fs.readFile(commandPath, 'utf8');
516
- const { data: metadata, content: commandContent } = parseFrontmatter(content);
517
- // Basic argument replacement (will be enhanced in command parser utility)
518
- let processedContent = commandContent;
519
-
520
- // Replace $ARGUMENTS with all arguments joined
521
- const argsString = args.join(' ');
522
- processedContent = processedContent.replace(/\$ARGUMENTS/g, argsString);
523
-
524
- // Replace $1, $2, etc. with positional arguments
525
- args.forEach((arg, index) => {
526
- const placeholder = `$${index + 1}`;
527
- processedContent = processedContent.replace(new RegExp(`\\${placeholder}\\b`, 'g'), arg);
528
- });
529
-
530
- res.json({
531
- type: 'custom',
532
- command: commandName,
533
- content: processedContent,
534
- metadata,
535
- hasFileIncludes: processedContent.includes('@'),
536
- hasBashCommands: processedContent.includes('!')
537
- });
538
- } catch (error) {
539
- if (error.code === 'ENOENT') {
540
- return res.status(404).json({
541
- error: 'Command not found',
542
- message: `Command file not found: ${req.body.commandPath}`
543
- });
544
- }
545
-
546
- console.error('Error executing command:', error);
547
- res.status(500).json({
548
- error: 'Failed to execute command',
549
- message: error.message
550
- });
551
- }
552
- });
553
-
554
- export default router;
1
+ import { promises as fs } from 'fs';
2
+ import path from 'path';
3
+ import os from 'os';
4
+
5
+ import express from 'express';
6
+
7
+ import { CLAUDE_MODELS, CURSOR_MODELS, CODEX_MODELS } from '../../shared/modelConstants.js';
8
+ import { parseFrontmatter } from '../utils/frontmatter.js';
9
+ import { findAppRoot, getModuleDir } from '../utils/runtime-paths.js';
10
+
11
+ const __dirname = getModuleDir(import.meta.url);
12
+ // This route reads the top-level package.json for the status command, so it needs the real
13
+ // app root even after compilation moves the route file under dist-server/server/routes.
14
+ const APP_ROOT = findAppRoot(__dirname);
15
+
16
+ const router = express.Router();
17
+
18
+ /**
19
+ * Recursively scan directory for command files (.md)
20
+ * @param {string} dir - Directory to scan
21
+ * @param {string} baseDir - Base directory for relative paths
22
+ * @param {string} namespace - Namespace for commands (e.g., 'project', 'user')
23
+ * @returns {Promise<Array>} Array of command objects
24
+ */
25
+ async function scanCommandsDirectory(dir, baseDir, namespace) {
26
+ const commands = [];
27
+
28
+ try {
29
+ // Check if directory exists
30
+ await fs.access(dir);
31
+
32
+ const entries = await fs.readdir(dir, { withFileTypes: true });
33
+
34
+ for (const entry of entries) {
35
+ const fullPath = path.join(dir, entry.name);
36
+
37
+ if (entry.isDirectory()) {
38
+ // Recursively scan subdirectories
39
+ const subCommands = await scanCommandsDirectory(fullPath, baseDir, namespace);
40
+ commands.push(...subCommands);
41
+ } else if (entry.isFile() && entry.name.endsWith('.md')) {
42
+ // Parse markdown file for metadata
43
+ try {
44
+ const content = await fs.readFile(fullPath, 'utf8');
45
+ const { data: frontmatter, content: commandContent } = parseFrontmatter(content);
46
+
47
+ // Calculate relative path from baseDir for command name
48
+ const relativePath = path.relative(baseDir, fullPath);
49
+ // Remove .md extension and convert to command name
50
+ const commandName = '/' + relativePath.replace(/\.md$/, '').replace(/\\/g, '/');
51
+
52
+ // Extract description from frontmatter or first line of content
53
+ let description = frontmatter.description || '';
54
+ if (!description) {
55
+ const firstLine = commandContent.trim().split('\n')[0];
56
+ description = firstLine.replace(/^#+\s*/, '').trim();
57
+ }
58
+
59
+ commands.push({
60
+ name: commandName,
61
+ path: fullPath,
62
+ relativePath,
63
+ description,
64
+ namespace,
65
+ metadata: frontmatter
66
+ });
67
+ } catch (err) {
68
+ console.error(`Error parsing command file ${fullPath}:`, err.message);
69
+ }
70
+ }
71
+ }
72
+ } catch (err) {
73
+ // Directory doesn't exist or can't be accessed - this is okay
74
+ if (err.code !== 'ENOENT' && err.code !== 'EACCES') {
75
+ console.error(`Error scanning directory ${dir}:`, err.message);
76
+ }
77
+ }
78
+
79
+ return commands;
80
+ }
81
+
82
+ /**
83
+ * Built-in commands that are always available
84
+ */
85
+ const builtInCommands = [
86
+ {
87
+ name: '/help',
88
+ description: 'Show help documentation for Claude Code',
89
+ namespace: 'builtin',
90
+ metadata: { type: 'builtin' }
91
+ },
92
+ {
93
+ name: '/clear',
94
+ description: 'Clear the conversation history',
95
+ namespace: 'builtin',
96
+ metadata: { type: 'builtin' }
97
+ },
98
+ {
99
+ name: '/model',
100
+ description: 'Switch or view the current AI model',
101
+ namespace: 'builtin',
102
+ metadata: { type: 'builtin' }
103
+ },
104
+ {
105
+ name: '/cost',
106
+ description: 'Display token usage and cost information',
107
+ namespace: 'builtin',
108
+ metadata: { type: 'builtin' }
109
+ },
110
+ {
111
+ name: '/memory',
112
+ description: 'Open CLAUDE.md memory file for editing',
113
+ namespace: 'builtin',
114
+ metadata: { type: 'builtin' }
115
+ },
116
+ {
117
+ name: '/config',
118
+ description: 'Open settings and configuration',
119
+ namespace: 'builtin',
120
+ metadata: { type: 'builtin' }
121
+ },
122
+ {
123
+ name: '/status',
124
+ description: 'Show system status and version information',
125
+ namespace: 'builtin',
126
+ metadata: { type: 'builtin' }
127
+ },
128
+ {
129
+ name: '/rewind',
130
+ description: 'Rewind the conversation to a previous state',
131
+ namespace: 'builtin',
132
+ metadata: { type: 'builtin' }
133
+ }
134
+ ];
135
+
136
+ /**
137
+ * Built-in command handlers
138
+ * Each handler returns { type: 'builtin', action: string, data: any }
139
+ */
140
+ const builtInHandlers = {
141
+ '/help': async (args, context) => {
142
+ const helpText = `# Claude Code Commands
143
+
144
+ ## Built-in Commands
145
+
146
+ ${builtInCommands.map(cmd => `### ${cmd.name}
147
+ ${cmd.description}
148
+ `).join('\n')}
149
+
150
+ ## Custom Commands
151
+
152
+ Custom commands can be created in:
153
+ - Project: \`.claude/commands/\` (project-specific)
154
+ - User: \`~/.claude/commands/\` (available in all projects)
155
+
156
+ ### Command Syntax
157
+
158
+ - **Arguments**: Use \`$ARGUMENTS\` for all args or \`$1\`, \`$2\`, etc. for positional
159
+ - **File Includes**: Use \`@filename\` to include file contents
160
+ - **Bash Commands**: Use \`!command\` to execute bash commands
161
+
162
+ ### Examples
163
+
164
+ \`\`\`markdown
165
+ /mycommand arg1 arg2
166
+ \`\`\`
167
+ `;
168
+
169
+ return {
170
+ type: 'builtin',
171
+ action: 'help',
172
+ data: {
173
+ content: helpText,
174
+ format: 'markdown'
175
+ }
176
+ };
177
+ },
178
+
179
+ '/clear': async (args, context) => {
180
+ return {
181
+ type: 'builtin',
182
+ action: 'clear',
183
+ data: {
184
+ message: 'Conversation history cleared'
185
+ }
186
+ };
187
+ },
188
+
189
+ '/model': async (args, context) => {
190
+ // Read available models from centralized constants
191
+ const availableModels = {
192
+ claude: CLAUDE_MODELS.OPTIONS.map(o => o.value),
193
+ cursor: CURSOR_MODELS.OPTIONS.map(o => o.value),
194
+ codex: CODEX_MODELS.OPTIONS.map(o => o.value)
195
+ };
196
+
197
+ const currentProvider = context?.provider || 'claude';
198
+ const currentModel = context?.model || CLAUDE_MODELS.DEFAULT;
199
+
200
+ return {
201
+ type: 'builtin',
202
+ action: 'model',
203
+ data: {
204
+ current: {
205
+ provider: currentProvider,
206
+ model: currentModel
207
+ },
208
+ available: availableModels,
209
+ message: args.length > 0
210
+ ? `Switching to model: ${args[0]}`
211
+ : `Current model: ${currentModel}`
212
+ }
213
+ };
214
+ },
215
+
216
+ '/cost': async (args, context) => {
217
+ const tokenUsage = context?.tokenUsage || {};
218
+ const provider = context?.provider || 'claude';
219
+ const model =
220
+ context?.model ||
221
+ (provider === 'cursor'
222
+ ? CURSOR_MODELS.DEFAULT
223
+ : provider === 'codex'
224
+ ? CODEX_MODELS.DEFAULT
225
+ : CLAUDE_MODELS.DEFAULT);
226
+
227
+ const used = Number(tokenUsage.used ?? tokenUsage.totalUsed ?? tokenUsage.total_tokens ?? 0) || 0;
228
+ const total =
229
+ Number(
230
+ tokenUsage.total ??
231
+ tokenUsage.contextWindow ??
232
+ parseInt(process.env.CONTEXT_WINDOW || '160000', 10),
233
+ ) || 160000;
234
+ const percentage = total > 0 ? Number(((used / total) * 100).toFixed(1)) : 0;
235
+
236
+ const inputTokensRaw =
237
+ Number(
238
+ tokenUsage.inputTokens ??
239
+ tokenUsage.input ??
240
+ tokenUsage.cumulativeInputTokens ??
241
+ tokenUsage.promptTokens ??
242
+ 0,
243
+ ) || 0;
244
+ const outputTokens =
245
+ Number(
246
+ tokenUsage.outputTokens ??
247
+ tokenUsage.output ??
248
+ tokenUsage.cumulativeOutputTokens ??
249
+ tokenUsage.completionTokens ??
250
+ 0,
251
+ ) || 0;
252
+ const cacheTokens =
253
+ Number(
254
+ tokenUsage.cacheReadTokens ??
255
+ tokenUsage.cacheCreationTokens ??
256
+ tokenUsage.cacheTokens ??
257
+ tokenUsage.cachedTokens ??
258
+ 0,
259
+ ) || 0;
260
+
261
+ // If we only have total used tokens, treat them as input for display/estimation.
262
+ const inputTokens =
263
+ inputTokensRaw > 0 || outputTokens > 0 || cacheTokens > 0 ? inputTokensRaw + cacheTokens : used;
264
+
265
+ // Rough default rates by provider (USD / 1M tokens).
266
+ const pricingByProvider = {
267
+ claude: { input: 3, output: 15 },
268
+ cursor: { input: 3, output: 15 },
269
+ codex: { input: 1.5, output: 6 },
270
+ };
271
+ const rates = pricingByProvider[provider] || pricingByProvider.claude;
272
+
273
+ const inputCost = (inputTokens / 1_000_000) * rates.input;
274
+ const outputCost = (outputTokens / 1_000_000) * rates.output;
275
+ const totalCost = inputCost + outputCost;
276
+
277
+ return {
278
+ type: 'builtin',
279
+ action: 'cost',
280
+ data: {
281
+ tokenUsage: {
282
+ used,
283
+ total,
284
+ percentage,
285
+ },
286
+ cost: {
287
+ input: inputCost.toFixed(4),
288
+ output: outputCost.toFixed(4),
289
+ total: totalCost.toFixed(4),
290
+ },
291
+ model,
292
+ },
293
+ };
294
+ },
295
+
296
+ '/status': async (args, context) => {
297
+ // Read version from package.json
298
+ const packageJsonPath = path.join(APP_ROOT, 'package.json');
299
+ let version = 'unknown';
300
+ let packageName = '@pixelbyte-software/pixcode';
301
+
302
+ try {
303
+ const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
304
+ version = packageJson.version;
305
+ packageName = packageJson.name;
306
+ } catch (err) {
307
+ console.error('Error reading package.json:', err);
308
+ }
309
+
310
+ const uptime = process.uptime();
311
+ const uptimeMinutes = Math.floor(uptime / 60);
312
+ const uptimeHours = Math.floor(uptimeMinutes / 60);
313
+ const uptimeFormatted = uptimeHours > 0
314
+ ? `${uptimeHours}h ${uptimeMinutes % 60}m`
315
+ : `${uptimeMinutes}m`;
316
+
317
+ return {
318
+ type: 'builtin',
319
+ action: 'status',
320
+ data: {
321
+ version,
322
+ packageName,
323
+ uptime: uptimeFormatted,
324
+ uptimeSeconds: Math.floor(uptime),
325
+ model: context?.model || 'claude-sonnet-4.5',
326
+ provider: context?.provider || 'claude',
327
+ nodeVersion: process.version,
328
+ platform: process.platform
329
+ }
330
+ };
331
+ },
332
+
333
+ '/memory': async (args, context) => {
334
+ const projectPath = context?.projectPath;
335
+
336
+ if (!projectPath) {
337
+ return {
338
+ type: 'builtin',
339
+ action: 'memory',
340
+ data: {
341
+ error: 'No project selected',
342
+ message: 'Please select a project to access its CLAUDE.md file'
343
+ }
344
+ };
345
+ }
346
+
347
+ const claudeMdPath = path.join(projectPath, 'CLAUDE.md');
348
+
349
+ // Check if CLAUDE.md exists
350
+ let exists = false;
351
+ try {
352
+ await fs.access(claudeMdPath);
353
+ exists = true;
354
+ } catch (err) {
355
+ // File doesn't exist
356
+ }
357
+
358
+ return {
359
+ type: 'builtin',
360
+ action: 'memory',
361
+ data: {
362
+ path: claudeMdPath,
363
+ exists,
364
+ message: exists
365
+ ? `Opening CLAUDE.md at ${claudeMdPath}`
366
+ : `CLAUDE.md not found at ${claudeMdPath}. Create it to store project-specific instructions.`
367
+ }
368
+ };
369
+ },
370
+
371
+ '/config': async (args, context) => {
372
+ return {
373
+ type: 'builtin',
374
+ action: 'config',
375
+ data: {
376
+ message: 'Opening settings...'
377
+ }
378
+ };
379
+ },
380
+
381
+ '/rewind': async (args, context) => {
382
+ const steps = args[0] ? parseInt(args[0]) : 1;
383
+
384
+ if (isNaN(steps) || steps < 1) {
385
+ return {
386
+ type: 'builtin',
387
+ action: 'rewind',
388
+ data: {
389
+ error: 'Invalid steps parameter',
390
+ message: 'Usage: /rewind [number] - Rewind conversation by N steps (default: 1)'
391
+ }
392
+ };
393
+ }
394
+
395
+ return {
396
+ type: 'builtin',
397
+ action: 'rewind',
398
+ data: {
399
+ steps,
400
+ message: `Rewinding conversation by ${steps} step${steps > 1 ? 's' : ''}...`
401
+ }
402
+ };
403
+ }
404
+ };
405
+
406
+ /**
407
+ * POST /api/commands/list
408
+ * List all available commands from project and user directories
409
+ */
410
+ router.post('/list', async (req, res) => {
411
+ try {
412
+ const { projectPath } = req.body;
413
+ const allCommands = [...builtInCommands];
414
+
415
+ // Scan project-level commands (.claude/commands/)
416
+ if (projectPath) {
417
+ const projectCommandsDir = path.join(projectPath, '.claude', 'commands');
418
+ const projectCommands = await scanCommandsDirectory(
419
+ projectCommandsDir,
420
+ projectCommandsDir,
421
+ 'project'
422
+ );
423
+ allCommands.push(...projectCommands);
424
+ }
425
+
426
+ // Scan user-level commands (~/.claude/commands/)
427
+ const homeDir = os.homedir();
428
+ const userCommandsDir = path.join(homeDir, '.claude', 'commands');
429
+ const userCommands = await scanCommandsDirectory(
430
+ userCommandsDir,
431
+ userCommandsDir,
432
+ 'user'
433
+ );
434
+ allCommands.push(...userCommands);
435
+
436
+ // Separate built-in and custom commands
437
+ const customCommands = allCommands.filter(cmd => cmd.namespace !== 'builtin');
438
+
439
+ // Sort commands alphabetically by name
440
+ customCommands.sort((a, b) => a.name.localeCompare(b.name));
441
+
442
+ res.json({
443
+ builtIn: builtInCommands,
444
+ custom: customCommands,
445
+ count: allCommands.length
446
+ });
447
+ } catch (error) {
448
+ console.error('Error listing commands:', error);
449
+ res.status(500).json({
450
+ error: 'Failed to list commands',
451
+ message: error.message
452
+ });
453
+ }
454
+ });
455
+
456
+ /**
457
+ * POST /api/commands/execute
458
+ * Execute a command with argument replacement
459
+ * This endpoint prepares the command content but doesn't execute bash commands yet
460
+ * (that will be handled in the command parser utility)
461
+ */
462
+ router.post('/execute', async (req, res) => {
463
+ try {
464
+ const { commandName, commandPath, args = [], context = {} } = req.body;
465
+
466
+ if (!commandName) {
467
+ return res.status(400).json({
468
+ error: 'Command name is required'
469
+ });
470
+ }
471
+
472
+ // Handle built-in commands
473
+ const handler = builtInHandlers[commandName];
474
+ if (handler) {
475
+ try {
476
+ const result = await handler(args, context);
477
+ return res.json({
478
+ ...result,
479
+ command: commandName
480
+ });
481
+ } catch (error) {
482
+ console.error(`Error executing built-in command ${commandName}:`, error);
483
+ return res.status(500).json({
484
+ error: 'Command execution failed',
485
+ message: error.message,
486
+ command: commandName
487
+ });
488
+ }
489
+ }
490
+
491
+ // Handle custom commands
492
+ if (!commandPath) {
493
+ return res.status(400).json({
494
+ error: 'Command path is required for custom commands'
495
+ });
496
+ }
497
+
498
+ // Load command content
499
+ // Security: validate commandPath is within allowed directories
500
+ {
501
+ const resolvedPath = path.resolve(commandPath);
502
+ const userBase = path.resolve(path.join(os.homedir(), '.claude', 'commands'));
503
+ const projectBase = context?.projectPath
504
+ ? path.resolve(path.join(context.projectPath, '.claude', 'commands'))
505
+ : null;
506
+ const isUnder = (base) => {
507
+ const rel = path.relative(base, resolvedPath);
508
+ return rel !== '' && !rel.startsWith('..') && !path.isAbsolute(rel);
509
+ };
510
+ if (!(isUnder(userBase) || (projectBase && isUnder(projectBase)))) {
511
+ return res.status(403).json({
512
+ error: 'Access denied',
513
+ message: 'Command must be in .claude/commands directory'
514
+ });
515
+ }
516
+ }
517
+ const content = await fs.readFile(commandPath, 'utf8');
518
+ const { data: metadata, content: commandContent } = parseFrontmatter(content);
519
+ // Basic argument replacement (will be enhanced in command parser utility)
520
+ let processedContent = commandContent;
521
+
522
+ // Replace $ARGUMENTS with all arguments joined
523
+ const argsString = args.join(' ');
524
+ processedContent = processedContent.replace(/\$ARGUMENTS/g, argsString);
525
+
526
+ // Replace $1, $2, etc. with positional arguments
527
+ args.forEach((arg, index) => {
528
+ const placeholder = `$${index + 1}`;
529
+ processedContent = processedContent.replace(new RegExp(`\\${placeholder}\\b`, 'g'), arg);
530
+ });
531
+
532
+ res.json({
533
+ type: 'custom',
534
+ command: commandName,
535
+ content: processedContent,
536
+ metadata,
537
+ hasFileIncludes: processedContent.includes('@'),
538
+ hasBashCommands: processedContent.includes('!')
539
+ });
540
+ } catch (error) {
541
+ if (error.code === 'ENOENT') {
542
+ return res.status(404).json({
543
+ error: 'Command not found',
544
+ message: `Command file not found: ${req.body.commandPath}`
545
+ });
546
+ }
547
+
548
+ console.error('Error executing command:', error);
549
+ res.status(500).json({
550
+ error: 'Failed to execute command',
551
+ message: error.message
552
+ });
553
+ }
554
+ });
555
+
556
+ export default router;