claude-wec 1.0.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.
- package/LICENSE +675 -0
- package/README.md +371 -0
- package/dist/api-docs.html +879 -0
- package/dist/assets/KaTeX_AMS-Regular-BQhdFMY1.woff2 +0 -0
- package/dist/assets/KaTeX_AMS-Regular-DMm9YOAa.woff +0 -0
- package/dist/assets/KaTeX_AMS-Regular-DRggAlZN.ttf +0 -0
- package/dist/assets/KaTeX_Caligraphic-Bold-ATXxdsX0.ttf +0 -0
- package/dist/assets/KaTeX_Caligraphic-Bold-BEiXGLvX.woff +0 -0
- package/dist/assets/KaTeX_Caligraphic-Bold-Dq_IR9rO.woff2 +0 -0
- package/dist/assets/KaTeX_Caligraphic-Regular-CTRA-rTL.woff +0 -0
- package/dist/assets/KaTeX_Caligraphic-Regular-Di6jR-x-.woff2 +0 -0
- package/dist/assets/KaTeX_Caligraphic-Regular-wX97UBjC.ttf +0 -0
- package/dist/assets/KaTeX_Fraktur-Bold-BdnERNNW.ttf +0 -0
- package/dist/assets/KaTeX_Fraktur-Bold-BsDP51OF.woff +0 -0
- package/dist/assets/KaTeX_Fraktur-Bold-CL6g_b3V.woff2 +0 -0
- package/dist/assets/KaTeX_Fraktur-Regular-CB_wures.ttf +0 -0
- package/dist/assets/KaTeX_Fraktur-Regular-CTYiF6lA.woff2 +0 -0
- package/dist/assets/KaTeX_Fraktur-Regular-Dxdc4cR9.woff +0 -0
- package/dist/assets/KaTeX_Main-Bold-Cx986IdX.woff2 +0 -0
- package/dist/assets/KaTeX_Main-Bold-Jm3AIy58.woff +0 -0
- package/dist/assets/KaTeX_Main-Bold-waoOVXN0.ttf +0 -0
- package/dist/assets/KaTeX_Main-BoldItalic-DxDJ3AOS.woff2 +0 -0
- package/dist/assets/KaTeX_Main-BoldItalic-DzxPMmG6.ttf +0 -0
- package/dist/assets/KaTeX_Main-BoldItalic-SpSLRI95.woff +0 -0
- package/dist/assets/KaTeX_Main-Italic-3WenGoN9.ttf +0 -0
- package/dist/assets/KaTeX_Main-Italic-BMLOBm91.woff +0 -0
- package/dist/assets/KaTeX_Main-Italic-NWA7e6Wa.woff2 +0 -0
- package/dist/assets/KaTeX_Main-Regular-B22Nviop.woff2 +0 -0
- package/dist/assets/KaTeX_Main-Regular-Dr94JaBh.woff +0 -0
- package/dist/assets/KaTeX_Main-Regular-ypZvNtVU.ttf +0 -0
- package/dist/assets/KaTeX_Math-BoldItalic-B3XSjfu4.ttf +0 -0
- package/dist/assets/KaTeX_Math-BoldItalic-CZnvNsCZ.woff2 +0 -0
- package/dist/assets/KaTeX_Math-BoldItalic-iY-2wyZ7.woff +0 -0
- package/dist/assets/KaTeX_Math-Italic-DA0__PXp.woff +0 -0
- package/dist/assets/KaTeX_Math-Italic-flOr_0UB.ttf +0 -0
- package/dist/assets/KaTeX_Math-Italic-t53AETM-.woff2 +0 -0
- package/dist/assets/KaTeX_SansSerif-Bold-CFMepnvq.ttf +0 -0
- package/dist/assets/KaTeX_SansSerif-Bold-D1sUS0GD.woff2 +0 -0
- package/dist/assets/KaTeX_SansSerif-Bold-DbIhKOiC.woff +0 -0
- package/dist/assets/KaTeX_SansSerif-Italic-C3H0VqGB.woff2 +0 -0
- package/dist/assets/KaTeX_SansSerif-Italic-DN2j7dab.woff +0 -0
- package/dist/assets/KaTeX_SansSerif-Italic-YYjJ1zSn.ttf +0 -0
- package/dist/assets/KaTeX_SansSerif-Regular-BNo7hRIc.ttf +0 -0
- package/dist/assets/KaTeX_SansSerif-Regular-CS6fqUqJ.woff +0 -0
- package/dist/assets/KaTeX_SansSerif-Regular-DDBCnlJ7.woff2 +0 -0
- package/dist/assets/KaTeX_Script-Regular-C5JkGWo-.ttf +0 -0
- package/dist/assets/KaTeX_Script-Regular-D3wIWfF6.woff2 +0 -0
- package/dist/assets/KaTeX_Script-Regular-D5yQViql.woff +0 -0
- package/dist/assets/KaTeX_Size1-Regular-C195tn64.woff +0 -0
- package/dist/assets/KaTeX_Size1-Regular-Dbsnue_I.ttf +0 -0
- package/dist/assets/KaTeX_Size1-Regular-mCD8mA8B.woff2 +0 -0
- package/dist/assets/KaTeX_Size2-Regular-B7gKUWhC.ttf +0 -0
- package/dist/assets/KaTeX_Size2-Regular-Dy4dx90m.woff2 +0 -0
- package/dist/assets/KaTeX_Size2-Regular-oD1tc_U0.woff +0 -0
- package/dist/assets/KaTeX_Size3-Regular-CTq5MqoE.woff +0 -0
- package/dist/assets/KaTeX_Size3-Regular-DgpXs0kz.ttf +0 -0
- package/dist/assets/KaTeX_Size4-Regular-BF-4gkZK.woff +0 -0
- package/dist/assets/KaTeX_Size4-Regular-DWFBv043.ttf +0 -0
- package/dist/assets/KaTeX_Size4-Regular-Dl5lxZxV.woff2 +0 -0
- package/dist/assets/KaTeX_Typewriter-Regular-C0xS9mPB.woff +0 -0
- package/dist/assets/KaTeX_Typewriter-Regular-CO6r4hn1.woff2 +0 -0
- package/dist/assets/KaTeX_Typewriter-Regular-D3Ib7_Hf.ttf +0 -0
- package/dist/assets/index-cIxJ4RXb.js +1226 -0
- package/dist/assets/index-oyEz69sP.css +32 -0
- package/dist/assets/vendor-codemirror-CJLzwpLB.js +39 -0
- package/dist/assets/vendor-react-DcyRfQm3.js +59 -0
- package/dist/assets/vendor-xterm-DfaPXD3y.js +66 -0
- package/dist/clear-cache.html +85 -0
- package/dist/convert-icons.md +53 -0
- package/dist/favicon.png +0 -0
- package/dist/favicon.svg +9 -0
- package/dist/generate-icons.js +49 -0
- package/dist/icons/claude-ai-icon.svg +1 -0
- package/dist/icons/codex-white.svg +3 -0
- package/dist/icons/codex.svg +3 -0
- package/dist/icons/cursor-white.svg +12 -0
- package/dist/icons/cursor.svg +1 -0
- package/dist/icons/generate-icons.md +19 -0
- package/dist/icons/icon-128x128.png +0 -0
- package/dist/icons/icon-128x128.svg +12 -0
- package/dist/icons/icon-144x144.png +0 -0
- package/dist/icons/icon-144x144.svg +12 -0
- package/dist/icons/icon-152x152.png +0 -0
- package/dist/icons/icon-152x152.svg +12 -0
- package/dist/icons/icon-192x192.png +0 -0
- package/dist/icons/icon-192x192.svg +12 -0
- package/dist/icons/icon-384x384.png +0 -0
- package/dist/icons/icon-384x384.svg +12 -0
- package/dist/icons/icon-512x512.png +0 -0
- package/dist/icons/icon-512x512.svg +12 -0
- package/dist/icons/icon-72x72.png +0 -0
- package/dist/icons/icon-72x72.svg +12 -0
- package/dist/icons/icon-96x96.png +0 -0
- package/dist/icons/icon-96x96.svg +12 -0
- package/dist/icons/icon-template.svg +12 -0
- package/dist/index.html +52 -0
- package/dist/logo-128.png +0 -0
- package/dist/logo-256.png +0 -0
- package/dist/logo-32.png +0 -0
- package/dist/logo-512.png +0 -0
- package/dist/logo-64.png +0 -0
- package/dist/logo.svg +17 -0
- package/dist/manifest.json +61 -0
- package/dist/screenshots/cli-selection.png +0 -0
- package/dist/screenshots/desktop-main.png +0 -0
- package/dist/screenshots/mobile-chat.png +0 -0
- package/dist/screenshots/tools-modal.png +0 -0
- package/dist/sw.js +49 -0
- package/package.json +109 -0
- package/server/claude-sdk.js +721 -0
- package/server/cli.js +327 -0
- package/server/cursor-cli.js +267 -0
- package/server/database/auth.db +0 -0
- package/server/database/db.js +361 -0
- package/server/database/init.sql +52 -0
- package/server/index.js +1747 -0
- package/server/middleware/auth.js +111 -0
- package/server/openai-codex.js +389 -0
- package/server/projects.js +1604 -0
- package/server/routes/agent.js +1230 -0
- package/server/routes/auth.js +135 -0
- package/server/routes/cli-auth.js +263 -0
- package/server/routes/codex.js +345 -0
- package/server/routes/commands.js +521 -0
- package/server/routes/cursor.js +795 -0
- package/server/routes/git.js +1128 -0
- package/server/routes/mcp-utils.js +48 -0
- package/server/routes/mcp.js +552 -0
- package/server/routes/projects.js +378 -0
- package/server/routes/settings.js +178 -0
- package/server/routes/taskmaster.js +1963 -0
- package/server/routes/user.js +106 -0
- package/server/utils/commandParser.js +303 -0
- package/server/utils/gitConfig.js +24 -0
- package/server/utils/mcp-detector.js +198 -0
- package/server/utils/taskmaster-websocket.js +129 -0
- package/shared/modelConstants.js +65 -0
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { promises as fs } from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
5
|
+
import os from 'os';
|
|
6
|
+
import matter from 'gray-matter';
|
|
7
|
+
import { CLAUDE_MODELS, CURSOR_MODELS, CODEX_MODELS } from '../../shared/modelConstants.js';
|
|
8
|
+
|
|
9
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
10
|
+
const __dirname = path.dirname(__filename);
|
|
11
|
+
|
|
12
|
+
const router = express.Router();
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Recursively scan directory for command files (.md)
|
|
16
|
+
* @param {string} dir - Directory to scan
|
|
17
|
+
* @param {string} baseDir - Base directory for relative paths
|
|
18
|
+
* @param {string} namespace - Namespace for commands (e.g., 'project', 'user')
|
|
19
|
+
* @returns {Promise<Array>} Array of command objects
|
|
20
|
+
*/
|
|
21
|
+
async function scanCommandsDirectory(dir, baseDir, namespace) {
|
|
22
|
+
const commands = [];
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
// Check if directory exists
|
|
26
|
+
await fs.access(dir);
|
|
27
|
+
|
|
28
|
+
const entries = await fs.readdir(dir, { withFileTypes: true });
|
|
29
|
+
|
|
30
|
+
for (const entry of entries) {
|
|
31
|
+
const fullPath = path.join(dir, entry.name);
|
|
32
|
+
|
|
33
|
+
if (entry.isDirectory()) {
|
|
34
|
+
// Recursively scan subdirectories
|
|
35
|
+
const subCommands = await scanCommandsDirectory(fullPath, baseDir, namespace);
|
|
36
|
+
commands.push(...subCommands);
|
|
37
|
+
} else if (entry.isFile() && entry.name.endsWith('.md')) {
|
|
38
|
+
// Parse markdown file for metadata
|
|
39
|
+
try {
|
|
40
|
+
const content = await fs.readFile(fullPath, 'utf8');
|
|
41
|
+
const { data: frontmatter, content: commandContent } = matter(content);
|
|
42
|
+
|
|
43
|
+
// Calculate relative path from baseDir for command name
|
|
44
|
+
const relativePath = path.relative(baseDir, fullPath);
|
|
45
|
+
// Remove .md extension and convert to command name
|
|
46
|
+
const commandName = '/' + relativePath.replace(/\.md$/, '').replace(/\\/g, '/');
|
|
47
|
+
|
|
48
|
+
// Extract description from frontmatter or first line of content
|
|
49
|
+
let description = frontmatter.description || '';
|
|
50
|
+
if (!description) {
|
|
51
|
+
const firstLine = commandContent.trim().split('\n')[0];
|
|
52
|
+
description = firstLine.replace(/^#+\s*/, '').trim();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
commands.push({
|
|
56
|
+
name: commandName,
|
|
57
|
+
path: fullPath,
|
|
58
|
+
relativePath,
|
|
59
|
+
description,
|
|
60
|
+
namespace,
|
|
61
|
+
metadata: frontmatter
|
|
62
|
+
});
|
|
63
|
+
} catch (err) {
|
|
64
|
+
console.error(`Error parsing command file ${fullPath}:`, err.message);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
} catch (err) {
|
|
69
|
+
// Directory doesn't exist or can't be accessed - this is okay
|
|
70
|
+
if (err.code !== 'ENOENT' && err.code !== 'EACCES') {
|
|
71
|
+
console.error(`Error scanning directory ${dir}:`, err.message);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return commands;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Built-in commands that are always available
|
|
80
|
+
*/
|
|
81
|
+
const builtInCommands = [
|
|
82
|
+
{
|
|
83
|
+
name: '/help',
|
|
84
|
+
description: 'Show help documentation for Claude Code',
|
|
85
|
+
namespace: 'builtin',
|
|
86
|
+
metadata: { type: 'builtin' }
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: '/clear',
|
|
90
|
+
description: 'Clear the conversation history',
|
|
91
|
+
namespace: 'builtin',
|
|
92
|
+
metadata: { type: 'builtin' }
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
name: '/model',
|
|
96
|
+
description: 'Switch or view the current AI model',
|
|
97
|
+
namespace: 'builtin',
|
|
98
|
+
metadata: { type: 'builtin' }
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
name: '/cost',
|
|
102
|
+
description: 'Display token usage and cost information',
|
|
103
|
+
namespace: 'builtin',
|
|
104
|
+
metadata: { type: 'builtin' }
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
name: '/memory',
|
|
108
|
+
description: 'Open CLAUDE.md memory file for editing',
|
|
109
|
+
namespace: 'builtin',
|
|
110
|
+
metadata: { type: 'builtin' }
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
name: '/config',
|
|
114
|
+
description: 'Open settings and configuration',
|
|
115
|
+
namespace: 'builtin',
|
|
116
|
+
metadata: { type: 'builtin' }
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
name: '/status',
|
|
120
|
+
description: 'Show system status and version information',
|
|
121
|
+
namespace: 'builtin',
|
|
122
|
+
metadata: { type: 'builtin' }
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
name: '/rewind',
|
|
126
|
+
description: 'Rewind the conversation to a previous state',
|
|
127
|
+
namespace: 'builtin',
|
|
128
|
+
metadata: { type: 'builtin' }
|
|
129
|
+
}
|
|
130
|
+
];
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Built-in command handlers
|
|
134
|
+
* Each handler returns { type: 'builtin', action: string, data: any }
|
|
135
|
+
*/
|
|
136
|
+
const builtInHandlers = {
|
|
137
|
+
'/help': async (args, context) => {
|
|
138
|
+
const helpText = `# Claude Code Commands
|
|
139
|
+
|
|
140
|
+
## Built-in Commands
|
|
141
|
+
|
|
142
|
+
${builtInCommands.map(cmd => `### ${cmd.name}
|
|
143
|
+
${cmd.description}
|
|
144
|
+
`).join('\n')}
|
|
145
|
+
|
|
146
|
+
## Custom Commands
|
|
147
|
+
|
|
148
|
+
Custom commands can be created in:
|
|
149
|
+
- Project: \`.claude/commands/\` (project-specific)
|
|
150
|
+
- User: \`~/.claude/commands/\` (available in all projects)
|
|
151
|
+
|
|
152
|
+
### Command Syntax
|
|
153
|
+
|
|
154
|
+
- **Arguments**: Use \`$ARGUMENTS\` for all args or \`$1\`, \`$2\`, etc. for positional
|
|
155
|
+
- **File Includes**: Use \`@filename\` to include file contents
|
|
156
|
+
- **Bash Commands**: Use \`!command\` to execute bash commands
|
|
157
|
+
|
|
158
|
+
### Examples
|
|
159
|
+
|
|
160
|
+
\`\`\`markdown
|
|
161
|
+
/mycommand arg1 arg2
|
|
162
|
+
\`\`\`
|
|
163
|
+
`;
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
type: 'builtin',
|
|
167
|
+
action: 'help',
|
|
168
|
+
data: {
|
|
169
|
+
content: helpText,
|
|
170
|
+
format: 'markdown'
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
},
|
|
174
|
+
|
|
175
|
+
'/clear': async (args, context) => {
|
|
176
|
+
return {
|
|
177
|
+
type: 'builtin',
|
|
178
|
+
action: 'clear',
|
|
179
|
+
data: {
|
|
180
|
+
message: 'Conversation history cleared'
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
},
|
|
184
|
+
|
|
185
|
+
'/model': async (args, context) => {
|
|
186
|
+
// Read available models from centralized constants
|
|
187
|
+
const availableModels = {
|
|
188
|
+
claude: CLAUDE_MODELS.OPTIONS.map(o => o.value),
|
|
189
|
+
cursor: CURSOR_MODELS.OPTIONS.map(o => o.value),
|
|
190
|
+
codex: CODEX_MODELS.OPTIONS.map(o => o.value)
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const currentProvider = context?.provider || 'claude';
|
|
194
|
+
const currentModel = context?.model || CLAUDE_MODELS.DEFAULT;
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
type: 'builtin',
|
|
198
|
+
action: 'model',
|
|
199
|
+
data: {
|
|
200
|
+
current: {
|
|
201
|
+
provider: currentProvider,
|
|
202
|
+
model: currentModel
|
|
203
|
+
},
|
|
204
|
+
available: availableModels,
|
|
205
|
+
message: args.length > 0
|
|
206
|
+
? `Switching to model: ${args[0]}`
|
|
207
|
+
: `Current model: ${currentModel}`
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
},
|
|
211
|
+
|
|
212
|
+
'/status': async (args, context) => {
|
|
213
|
+
// Read version from package.json
|
|
214
|
+
const packageJsonPath = path.join(path.dirname(__dirname), '..', 'package.json');
|
|
215
|
+
let version = 'unknown';
|
|
216
|
+
let packageName = 'claude-wec';
|
|
217
|
+
|
|
218
|
+
try {
|
|
219
|
+
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
|
|
220
|
+
version = packageJson.version;
|
|
221
|
+
packageName = packageJson.name;
|
|
222
|
+
} catch (err) {
|
|
223
|
+
console.error('Error reading package.json:', err);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const uptime = process.uptime();
|
|
227
|
+
const uptimeMinutes = Math.floor(uptime / 60);
|
|
228
|
+
const uptimeHours = Math.floor(uptimeMinutes / 60);
|
|
229
|
+
const uptimeFormatted = uptimeHours > 0
|
|
230
|
+
? `${uptimeHours}h ${uptimeMinutes % 60}m`
|
|
231
|
+
: `${uptimeMinutes}m`;
|
|
232
|
+
|
|
233
|
+
return {
|
|
234
|
+
type: 'builtin',
|
|
235
|
+
action: 'status',
|
|
236
|
+
data: {
|
|
237
|
+
version,
|
|
238
|
+
packageName,
|
|
239
|
+
uptime: uptimeFormatted,
|
|
240
|
+
uptimeSeconds: Math.floor(uptime),
|
|
241
|
+
model: context?.model || 'claude-sonnet-4.5',
|
|
242
|
+
provider: context?.provider || 'claude',
|
|
243
|
+
nodeVersion: process.version,
|
|
244
|
+
platform: process.platform
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
},
|
|
248
|
+
|
|
249
|
+
'/memory': async (args, context) => {
|
|
250
|
+
const projectPath = context?.projectPath;
|
|
251
|
+
|
|
252
|
+
if (!projectPath) {
|
|
253
|
+
return {
|
|
254
|
+
type: 'builtin',
|
|
255
|
+
action: 'memory',
|
|
256
|
+
data: {
|
|
257
|
+
error: 'No project selected',
|
|
258
|
+
message: 'Please select a project to access its CLAUDE.md file'
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const claudeMdPath = path.join(projectPath, 'CLAUDE.md');
|
|
264
|
+
|
|
265
|
+
// Check if CLAUDE.md exists
|
|
266
|
+
let exists = false;
|
|
267
|
+
try {
|
|
268
|
+
await fs.access(claudeMdPath);
|
|
269
|
+
exists = true;
|
|
270
|
+
} catch (err) {
|
|
271
|
+
// File doesn't exist
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return {
|
|
275
|
+
type: 'builtin',
|
|
276
|
+
action: 'memory',
|
|
277
|
+
data: {
|
|
278
|
+
path: claudeMdPath,
|
|
279
|
+
exists,
|
|
280
|
+
message: exists
|
|
281
|
+
? `Opening CLAUDE.md at ${claudeMdPath}`
|
|
282
|
+
: `CLAUDE.md not found at ${claudeMdPath}. Create it to store project-specific instructions.`
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
},
|
|
286
|
+
|
|
287
|
+
'/config': async (args, context) => {
|
|
288
|
+
return {
|
|
289
|
+
type: 'builtin',
|
|
290
|
+
action: 'config',
|
|
291
|
+
data: {
|
|
292
|
+
message: 'Opening settings...'
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
},
|
|
296
|
+
|
|
297
|
+
'/rewind': async (args, context) => {
|
|
298
|
+
const steps = args[0] ? parseInt(args[0]) : 1;
|
|
299
|
+
|
|
300
|
+
if (isNaN(steps) || steps < 1) {
|
|
301
|
+
return {
|
|
302
|
+
type: 'builtin',
|
|
303
|
+
action: 'rewind',
|
|
304
|
+
data: {
|
|
305
|
+
error: 'Invalid steps parameter',
|
|
306
|
+
message: 'Usage: /rewind [number] - Rewind conversation by N steps (default: 1)'
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
return {
|
|
312
|
+
type: 'builtin',
|
|
313
|
+
action: 'rewind',
|
|
314
|
+
data: {
|
|
315
|
+
steps,
|
|
316
|
+
message: `Rewinding conversation by ${steps} step${steps > 1 ? 's' : ''}...`
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* POST /api/commands/list
|
|
324
|
+
* List all available commands from project and user directories
|
|
325
|
+
*/
|
|
326
|
+
router.post('/list', async (req, res) => {
|
|
327
|
+
try {
|
|
328
|
+
const { projectPath } = req.body;
|
|
329
|
+
const allCommands = [...builtInCommands];
|
|
330
|
+
|
|
331
|
+
// Scan project-level commands (.claude/commands/)
|
|
332
|
+
if (projectPath) {
|
|
333
|
+
const projectCommandsDir = path.join(projectPath, '.claude', 'commands');
|
|
334
|
+
const projectCommands = await scanCommandsDirectory(
|
|
335
|
+
projectCommandsDir,
|
|
336
|
+
projectCommandsDir,
|
|
337
|
+
'project'
|
|
338
|
+
);
|
|
339
|
+
allCommands.push(...projectCommands);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Scan user-level commands (~/.claude/commands/)
|
|
343
|
+
const homeDir = os.homedir();
|
|
344
|
+
const userCommandsDir = path.join(homeDir, '.claude', 'commands');
|
|
345
|
+
const userCommands = await scanCommandsDirectory(
|
|
346
|
+
userCommandsDir,
|
|
347
|
+
userCommandsDir,
|
|
348
|
+
'user'
|
|
349
|
+
);
|
|
350
|
+
allCommands.push(...userCommands);
|
|
351
|
+
|
|
352
|
+
// Separate built-in and custom commands
|
|
353
|
+
const customCommands = allCommands.filter(cmd => cmd.namespace !== 'builtin');
|
|
354
|
+
|
|
355
|
+
// Sort commands alphabetically by name
|
|
356
|
+
customCommands.sort((a, b) => a.name.localeCompare(b.name));
|
|
357
|
+
|
|
358
|
+
res.json({
|
|
359
|
+
builtIn: builtInCommands,
|
|
360
|
+
custom: customCommands,
|
|
361
|
+
count: allCommands.length
|
|
362
|
+
});
|
|
363
|
+
} catch (error) {
|
|
364
|
+
console.error('Error listing commands:', error);
|
|
365
|
+
res.status(500).json({
|
|
366
|
+
error: 'Failed to list commands',
|
|
367
|
+
message: error.message
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* POST /api/commands/load
|
|
374
|
+
* Load a specific command file and return its content and metadata
|
|
375
|
+
*/
|
|
376
|
+
router.post('/load', async (req, res) => {
|
|
377
|
+
try {
|
|
378
|
+
const { commandPath } = req.body;
|
|
379
|
+
|
|
380
|
+
if (!commandPath) {
|
|
381
|
+
return res.status(400).json({
|
|
382
|
+
error: 'Command path is required'
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// Security: Prevent path traversal
|
|
387
|
+
const resolvedPath = path.resolve(commandPath);
|
|
388
|
+
if (!resolvedPath.startsWith(path.resolve(os.homedir())) &&
|
|
389
|
+
!resolvedPath.includes('.claude/commands')) {
|
|
390
|
+
return res.status(403).json({
|
|
391
|
+
error: 'Access denied',
|
|
392
|
+
message: 'Command must be in .claude/commands directory'
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// Read and parse the command file
|
|
397
|
+
const content = await fs.readFile(commandPath, 'utf8');
|
|
398
|
+
const { data: metadata, content: commandContent } = matter(content);
|
|
399
|
+
|
|
400
|
+
res.json({
|
|
401
|
+
path: commandPath,
|
|
402
|
+
metadata,
|
|
403
|
+
content: commandContent
|
|
404
|
+
});
|
|
405
|
+
} catch (error) {
|
|
406
|
+
if (error.code === 'ENOENT') {
|
|
407
|
+
return res.status(404).json({
|
|
408
|
+
error: 'Command not found',
|
|
409
|
+
message: `Command file not found: ${req.body.commandPath}`
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
console.error('Error loading command:', error);
|
|
414
|
+
res.status(500).json({
|
|
415
|
+
error: 'Failed to load command',
|
|
416
|
+
message: error.message
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* POST /api/commands/execute
|
|
423
|
+
* Execute a command with argument replacement
|
|
424
|
+
* This endpoint prepares the command content but doesn't execute bash commands yet
|
|
425
|
+
* (that will be handled in the command parser utility)
|
|
426
|
+
*/
|
|
427
|
+
router.post('/execute', async (req, res) => {
|
|
428
|
+
try {
|
|
429
|
+
const { commandName, commandPath, args = [], context = {} } = req.body;
|
|
430
|
+
|
|
431
|
+
if (!commandName) {
|
|
432
|
+
return res.status(400).json({
|
|
433
|
+
error: 'Command name is required'
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// Handle built-in commands
|
|
438
|
+
const handler = builtInHandlers[commandName];
|
|
439
|
+
if (handler) {
|
|
440
|
+
try {
|
|
441
|
+
const result = await handler(args, context);
|
|
442
|
+
return res.json({
|
|
443
|
+
...result,
|
|
444
|
+
command: commandName
|
|
445
|
+
});
|
|
446
|
+
} catch (error) {
|
|
447
|
+
console.error(`Error executing built-in command ${commandName}:`, error);
|
|
448
|
+
return res.status(500).json({
|
|
449
|
+
error: 'Command execution failed',
|
|
450
|
+
message: error.message,
|
|
451
|
+
command: commandName
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// Handle custom commands
|
|
457
|
+
if (!commandPath) {
|
|
458
|
+
return res.status(400).json({
|
|
459
|
+
error: 'Command path is required for custom commands'
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// Load command content
|
|
464
|
+
// Security: validate commandPath is within allowed directories
|
|
465
|
+
{
|
|
466
|
+
const resolvedPath = path.resolve(commandPath);
|
|
467
|
+
const userBase = path.resolve(path.join(os.homedir(), '.claude', 'commands'));
|
|
468
|
+
const projectBase = context?.projectPath
|
|
469
|
+
? path.resolve(path.join(context.projectPath, '.claude', 'commands'))
|
|
470
|
+
: null;
|
|
471
|
+
const isUnder = (base) => {
|
|
472
|
+
const rel = path.relative(base, resolvedPath);
|
|
473
|
+
return rel !== '' && !rel.startsWith('..') && !path.isAbsolute(rel);
|
|
474
|
+
};
|
|
475
|
+
if (!(isUnder(userBase) || (projectBase && isUnder(projectBase)))) {
|
|
476
|
+
return res.status(403).json({
|
|
477
|
+
error: 'Access denied',
|
|
478
|
+
message: 'Command must be in .claude/commands directory'
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
const content = await fs.readFile(commandPath, 'utf8');
|
|
483
|
+
const { data: metadata, content: commandContent } = matter(content);
|
|
484
|
+
// Basic argument replacement (will be enhanced in command parser utility)
|
|
485
|
+
let processedContent = commandContent;
|
|
486
|
+
|
|
487
|
+
// Replace $ARGUMENTS with all arguments joined
|
|
488
|
+
const argsString = args.join(' ');
|
|
489
|
+
processedContent = processedContent.replace(/\$ARGUMENTS/g, argsString);
|
|
490
|
+
|
|
491
|
+
// Replace $1, $2, etc. with positional arguments
|
|
492
|
+
args.forEach((arg, index) => {
|
|
493
|
+
const placeholder = `$${index + 1}`;
|
|
494
|
+
processedContent = processedContent.replace(new RegExp(`\\${placeholder}\\b`, 'g'), arg);
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
res.json({
|
|
498
|
+
type: 'custom',
|
|
499
|
+
command: commandName,
|
|
500
|
+
content: processedContent,
|
|
501
|
+
metadata,
|
|
502
|
+
hasFileIncludes: processedContent.includes('@'),
|
|
503
|
+
hasBashCommands: processedContent.includes('!')
|
|
504
|
+
});
|
|
505
|
+
} catch (error) {
|
|
506
|
+
if (error.code === 'ENOENT') {
|
|
507
|
+
return res.status(404).json({
|
|
508
|
+
error: 'Command not found',
|
|
509
|
+
message: `Command file not found: ${req.body.commandPath}`
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
console.error('Error executing command:', error);
|
|
514
|
+
res.status(500).json({
|
|
515
|
+
error: 'Failed to execute command',
|
|
516
|
+
message: error.message
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
export default router;
|