@siteboon/claude-code-ui 1.8.12 → 1.9.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.
@@ -0,0 +1,572 @@
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
+
8
+ const __filename = fileURLToPath(import.meta.url);
9
+ const __dirname = path.dirname(__filename);
10
+
11
+ const router = express.Router();
12
+
13
+ /**
14
+ * Recursively scan directory for command files (.md)
15
+ * @param {string} dir - Directory to scan
16
+ * @param {string} baseDir - Base directory for relative paths
17
+ * @param {string} namespace - Namespace for commands (e.g., 'project', 'user')
18
+ * @returns {Promise<Array>} Array of command objects
19
+ */
20
+ async function scanCommandsDirectory(dir, baseDir, namespace) {
21
+ const commands = [];
22
+
23
+ try {
24
+ // Check if directory exists
25
+ await fs.access(dir);
26
+
27
+ const entries = await fs.readdir(dir, { withFileTypes: true });
28
+
29
+ for (const entry of entries) {
30
+ const fullPath = path.join(dir, entry.name);
31
+
32
+ if (entry.isDirectory()) {
33
+ // Recursively scan subdirectories
34
+ const subCommands = await scanCommandsDirectory(fullPath, baseDir, namespace);
35
+ commands.push(...subCommands);
36
+ } else if (entry.isFile() && entry.name.endsWith('.md')) {
37
+ // Parse markdown file for metadata
38
+ try {
39
+ const content = await fs.readFile(fullPath, 'utf8');
40
+ const { data: frontmatter, content: commandContent } = matter(content);
41
+
42
+ // Calculate relative path from baseDir for command name
43
+ const relativePath = path.relative(baseDir, fullPath);
44
+ // Remove .md extension and convert to command name
45
+ const commandName = '/' + relativePath.replace(/\.md$/, '').replace(/\\/g, '/');
46
+
47
+ // Extract description from frontmatter or first line of content
48
+ let description = frontmatter.description || '';
49
+ if (!description) {
50
+ const firstLine = commandContent.trim().split('\n')[0];
51
+ description = firstLine.replace(/^#+\s*/, '').trim();
52
+ }
53
+
54
+ commands.push({
55
+ name: commandName,
56
+ path: fullPath,
57
+ relativePath,
58
+ description,
59
+ namespace,
60
+ metadata: frontmatter
61
+ });
62
+ } catch (err) {
63
+ console.error(`Error parsing command file ${fullPath}:`, err.message);
64
+ }
65
+ }
66
+ }
67
+ } catch (err) {
68
+ // Directory doesn't exist or can't be accessed - this is okay
69
+ if (err.code !== 'ENOENT' && err.code !== 'EACCES') {
70
+ console.error(`Error scanning directory ${dir}:`, err.message);
71
+ }
72
+ }
73
+
74
+ return commands;
75
+ }
76
+
77
+ /**
78
+ * Built-in commands that are always available
79
+ */
80
+ const builtInCommands = [
81
+ {
82
+ name: '/help',
83
+ description: 'Show help documentation for Claude Code',
84
+ namespace: 'builtin',
85
+ metadata: { type: 'builtin' }
86
+ },
87
+ {
88
+ name: '/clear',
89
+ description: 'Clear the conversation history',
90
+ namespace: 'builtin',
91
+ metadata: { type: 'builtin' }
92
+ },
93
+ {
94
+ name: '/model',
95
+ description: 'Switch or view the current AI model',
96
+ namespace: 'builtin',
97
+ metadata: { type: 'builtin' }
98
+ },
99
+ {
100
+ name: '/cost',
101
+ description: 'Display token usage and cost information',
102
+ namespace: 'builtin',
103
+ metadata: { type: 'builtin' }
104
+ },
105
+ {
106
+ name: '/memory',
107
+ description: 'Open CLAUDE.md memory file for editing',
108
+ namespace: 'builtin',
109
+ metadata: { type: 'builtin' }
110
+ },
111
+ {
112
+ name: '/config',
113
+ description: 'Open settings and configuration',
114
+ namespace: 'builtin',
115
+ metadata: { type: 'builtin' }
116
+ },
117
+ {
118
+ name: '/status',
119
+ description: 'Show system status and version information',
120
+ namespace: 'builtin',
121
+ metadata: { type: 'builtin' }
122
+ },
123
+ {
124
+ name: '/rewind',
125
+ description: 'Rewind the conversation to a previous state',
126
+ namespace: 'builtin',
127
+ metadata: { type: 'builtin' }
128
+ }
129
+ ];
130
+
131
+ /**
132
+ * Built-in command handlers
133
+ * Each handler returns { type: 'builtin', action: string, data: any }
134
+ */
135
+ const builtInHandlers = {
136
+ '/help': async (args, context) => {
137
+ const helpText = `# Claude Code Commands
138
+
139
+ ## Built-in Commands
140
+
141
+ ${builtInCommands.map(cmd => `### ${cmd.name}
142
+ ${cmd.description}
143
+ `).join('\n')}
144
+
145
+ ## Custom Commands
146
+
147
+ Custom commands can be created in:
148
+ - Project: \`.claude/commands/\` (project-specific)
149
+ - User: \`~/.claude/commands/\` (available in all projects)
150
+
151
+ ### Command Syntax
152
+
153
+ - **Arguments**: Use \`$ARGUMENTS\` for all args or \`$1\`, \`$2\`, etc. for positional
154
+ - **File Includes**: Use \`@filename\` to include file contents
155
+ - **Bash Commands**: Use \`!command\` to execute bash commands
156
+
157
+ ### Examples
158
+
159
+ \`\`\`markdown
160
+ /mycommand arg1 arg2
161
+ \`\`\`
162
+ `;
163
+
164
+ return {
165
+ type: 'builtin',
166
+ action: 'help',
167
+ data: {
168
+ content: helpText,
169
+ format: 'markdown'
170
+ }
171
+ };
172
+ },
173
+
174
+ '/clear': async (args, context) => {
175
+ return {
176
+ type: 'builtin',
177
+ action: 'clear',
178
+ data: {
179
+ message: 'Conversation history cleared'
180
+ }
181
+ };
182
+ },
183
+
184
+ '/model': async (args, context) => {
185
+ // Read available models from config or defaults
186
+ const availableModels = {
187
+ claude: [
188
+ 'claude-sonnet-4.5',
189
+ 'claude-sonnet-4',
190
+ 'claude-opus-4',
191
+ 'claude-sonnet-3.5'
192
+ ],
193
+ cursor: [
194
+ 'gpt-5',
195
+ 'sonnet-4',
196
+ 'opus-4.1'
197
+ ]
198
+ };
199
+
200
+ const currentProvider = context?.provider || 'claude';
201
+ const currentModel = context?.model || 'claude-sonnet-4.5';
202
+
203
+ return {
204
+ type: 'builtin',
205
+ action: 'model',
206
+ data: {
207
+ current: {
208
+ provider: currentProvider,
209
+ model: currentModel
210
+ },
211
+ available: availableModels,
212
+ message: args.length > 0
213
+ ? `Switching to model: ${args[0]}`
214
+ : `Current model: ${currentModel}`
215
+ }
216
+ };
217
+ },
218
+
219
+ '/cost': async (args, context) => {
220
+ // Calculate token usage and cost
221
+ const sessionId = context?.sessionId;
222
+ const tokenUsage = context?.tokenUsage || { used: 0, total: 200000 };
223
+
224
+ const costPerMillion = {
225
+ 'claude-sonnet-4.5': { input: 3, output: 15 },
226
+ 'claude-sonnet-4': { input: 3, output: 15 },
227
+ 'claude-opus-4': { input: 15, output: 75 },
228
+ 'gpt-5': { input: 5, output: 15 }
229
+ };
230
+
231
+ const model = context?.model || 'claude-sonnet-4.5';
232
+ const rates = costPerMillion[model] || costPerMillion['claude-sonnet-4.5'];
233
+
234
+ // Estimate 70% input, 30% output
235
+ const estimatedInputTokens = Math.floor(tokenUsage.used * 0.7);
236
+ const estimatedOutputTokens = Math.floor(tokenUsage.used * 0.3);
237
+
238
+ const inputCost = (estimatedInputTokens / 1000000) * rates.input;
239
+ const outputCost = (estimatedOutputTokens / 1000000) * rates.output;
240
+ const totalCost = inputCost + outputCost;
241
+
242
+ return {
243
+ type: 'builtin',
244
+ action: 'cost',
245
+ data: {
246
+ tokenUsage: {
247
+ used: tokenUsage.used,
248
+ total: tokenUsage.total,
249
+ percentage: ((tokenUsage.used / tokenUsage.total) * 100).toFixed(1)
250
+ },
251
+ cost: {
252
+ input: inputCost.toFixed(4),
253
+ output: outputCost.toFixed(4),
254
+ total: totalCost.toFixed(4),
255
+ currency: 'USD'
256
+ },
257
+ model,
258
+ rates
259
+ }
260
+ };
261
+ },
262
+
263
+ '/status': async (args, context) => {
264
+ // Read version from package.json
265
+ const packageJsonPath = path.join(path.dirname(__dirname), '..', 'package.json');
266
+ let version = 'unknown';
267
+ let packageName = 'claude-code-ui';
268
+
269
+ try {
270
+ const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
271
+ version = packageJson.version;
272
+ packageName = packageJson.name;
273
+ } catch (err) {
274
+ console.error('Error reading package.json:', err);
275
+ }
276
+
277
+ const uptime = process.uptime();
278
+ const uptimeMinutes = Math.floor(uptime / 60);
279
+ const uptimeHours = Math.floor(uptimeMinutes / 60);
280
+ const uptimeFormatted = uptimeHours > 0
281
+ ? `${uptimeHours}h ${uptimeMinutes % 60}m`
282
+ : `${uptimeMinutes}m`;
283
+
284
+ return {
285
+ type: 'builtin',
286
+ action: 'status',
287
+ data: {
288
+ version,
289
+ packageName,
290
+ uptime: uptimeFormatted,
291
+ uptimeSeconds: Math.floor(uptime),
292
+ model: context?.model || 'claude-sonnet-4.5',
293
+ provider: context?.provider || 'claude',
294
+ nodeVersion: process.version,
295
+ platform: process.platform
296
+ }
297
+ };
298
+ },
299
+
300
+ '/memory': async (args, context) => {
301
+ const projectPath = context?.projectPath;
302
+
303
+ if (!projectPath) {
304
+ return {
305
+ type: 'builtin',
306
+ action: 'memory',
307
+ data: {
308
+ error: 'No project selected',
309
+ message: 'Please select a project to access its CLAUDE.md file'
310
+ }
311
+ };
312
+ }
313
+
314
+ const claudeMdPath = path.join(projectPath, 'CLAUDE.md');
315
+
316
+ // Check if CLAUDE.md exists
317
+ let exists = false;
318
+ try {
319
+ await fs.access(claudeMdPath);
320
+ exists = true;
321
+ } catch (err) {
322
+ // File doesn't exist
323
+ }
324
+
325
+ return {
326
+ type: 'builtin',
327
+ action: 'memory',
328
+ data: {
329
+ path: claudeMdPath,
330
+ exists,
331
+ message: exists
332
+ ? `Opening CLAUDE.md at ${claudeMdPath}`
333
+ : `CLAUDE.md not found at ${claudeMdPath}. Create it to store project-specific instructions.`
334
+ }
335
+ };
336
+ },
337
+
338
+ '/config': async (args, context) => {
339
+ return {
340
+ type: 'builtin',
341
+ action: 'config',
342
+ data: {
343
+ message: 'Opening settings...'
344
+ }
345
+ };
346
+ },
347
+
348
+ '/rewind': async (args, context) => {
349
+ const steps = args[0] ? parseInt(args[0]) : 1;
350
+
351
+ if (isNaN(steps) || steps < 1) {
352
+ return {
353
+ type: 'builtin',
354
+ action: 'rewind',
355
+ data: {
356
+ error: 'Invalid steps parameter',
357
+ message: 'Usage: /rewind [number] - Rewind conversation by N steps (default: 1)'
358
+ }
359
+ };
360
+ }
361
+
362
+ return {
363
+ type: 'builtin',
364
+ action: 'rewind',
365
+ data: {
366
+ steps,
367
+ message: `Rewinding conversation by ${steps} step${steps > 1 ? 's' : ''}...`
368
+ }
369
+ };
370
+ }
371
+ };
372
+
373
+ /**
374
+ * POST /api/commands/list
375
+ * List all available commands from project and user directories
376
+ */
377
+ router.post('/list', async (req, res) => {
378
+ try {
379
+ const { projectPath } = req.body;
380
+ const allCommands = [...builtInCommands];
381
+
382
+ // Scan project-level commands (.claude/commands/)
383
+ if (projectPath) {
384
+ const projectCommandsDir = path.join(projectPath, '.claude', 'commands');
385
+ const projectCommands = await scanCommandsDirectory(
386
+ projectCommandsDir,
387
+ projectCommandsDir,
388
+ 'project'
389
+ );
390
+ allCommands.push(...projectCommands);
391
+ }
392
+
393
+ // Scan user-level commands (~/.claude/commands/)
394
+ const homeDir = os.homedir();
395
+ const userCommandsDir = path.join(homeDir, '.claude', 'commands');
396
+ const userCommands = await scanCommandsDirectory(
397
+ userCommandsDir,
398
+ userCommandsDir,
399
+ 'user'
400
+ );
401
+ allCommands.push(...userCommands);
402
+
403
+ // Separate built-in and custom commands
404
+ const customCommands = allCommands.filter(cmd => cmd.namespace !== 'builtin');
405
+
406
+ // Sort commands alphabetically by name
407
+ customCommands.sort((a, b) => a.name.localeCompare(b.name));
408
+
409
+ res.json({
410
+ builtIn: builtInCommands,
411
+ custom: customCommands,
412
+ count: allCommands.length
413
+ });
414
+ } catch (error) {
415
+ console.error('Error listing commands:', error);
416
+ res.status(500).json({
417
+ error: 'Failed to list commands',
418
+ message: error.message
419
+ });
420
+ }
421
+ });
422
+
423
+ /**
424
+ * POST /api/commands/load
425
+ * Load a specific command file and return its content and metadata
426
+ */
427
+ router.post('/load', async (req, res) => {
428
+ try {
429
+ const { commandPath } = req.body;
430
+
431
+ if (!commandPath) {
432
+ return res.status(400).json({
433
+ error: 'Command path is required'
434
+ });
435
+ }
436
+
437
+ // Security: Prevent path traversal
438
+ const resolvedPath = path.resolve(commandPath);
439
+ if (!resolvedPath.startsWith(path.resolve(os.homedir())) &&
440
+ !resolvedPath.includes('.claude/commands')) {
441
+ return res.status(403).json({
442
+ error: 'Access denied',
443
+ message: 'Command must be in .claude/commands directory'
444
+ });
445
+ }
446
+
447
+ // Read and parse the command file
448
+ const content = await fs.readFile(commandPath, 'utf8');
449
+ const { data: metadata, content: commandContent } = matter(content);
450
+
451
+ res.json({
452
+ path: commandPath,
453
+ metadata,
454
+ content: commandContent
455
+ });
456
+ } catch (error) {
457
+ if (error.code === 'ENOENT') {
458
+ return res.status(404).json({
459
+ error: 'Command not found',
460
+ message: `Command file not found: ${req.body.commandPath}`
461
+ });
462
+ }
463
+
464
+ console.error('Error loading command:', error);
465
+ res.status(500).json({
466
+ error: 'Failed to load command',
467
+ message: error.message
468
+ });
469
+ }
470
+ });
471
+
472
+ /**
473
+ * POST /api/commands/execute
474
+ * Execute a command with argument replacement
475
+ * This endpoint prepares the command content but doesn't execute bash commands yet
476
+ * (that will be handled in the command parser utility)
477
+ */
478
+ router.post('/execute', async (req, res) => {
479
+ try {
480
+ const { commandName, commandPath, args = [], context = {} } = req.body;
481
+
482
+ if (!commandName) {
483
+ return res.status(400).json({
484
+ error: 'Command name is required'
485
+ });
486
+ }
487
+
488
+ // Handle built-in commands
489
+ const handler = builtInHandlers[commandName];
490
+ if (handler) {
491
+ try {
492
+ const result = await handler(args, context);
493
+ return res.json({
494
+ ...result,
495
+ command: commandName
496
+ });
497
+ } catch (error) {
498
+ console.error(`Error executing built-in command ${commandName}:`, error);
499
+ return res.status(500).json({
500
+ error: 'Command execution failed',
501
+ message: error.message,
502
+ command: commandName
503
+ });
504
+ }
505
+ }
506
+
507
+ // Handle custom commands
508
+ if (!commandPath) {
509
+ return res.status(400).json({
510
+ error: 'Command path is required for custom commands'
511
+ });
512
+ }
513
+
514
+ // Load command content
515
+ // Security: validate commandPath is within allowed directories
516
+ {
517
+ const resolvedPath = path.resolve(commandPath);
518
+ const userBase = path.resolve(path.join(os.homedir(), '.claude', 'commands'));
519
+ const projectBase = context?.projectPath
520
+ ? path.resolve(path.join(context.projectPath, '.claude', 'commands'))
521
+ : null;
522
+ const isUnder = (base) => {
523
+ const rel = path.relative(base, resolvedPath);
524
+ return rel !== '' && !rel.startsWith('..') && !path.isAbsolute(rel);
525
+ };
526
+ if (!(isUnder(userBase) || (projectBase && isUnder(projectBase)))) {
527
+ return res.status(403).json({
528
+ error: 'Access denied',
529
+ message: 'Command must be in .claude/commands directory'
530
+ });
531
+ }
532
+ }
533
+ const content = await fs.readFile(commandPath, 'utf8');
534
+ const { data: metadata, content: commandContent } = matter(content);
535
+ // Basic argument replacement (will be enhanced in command parser utility)
536
+ let processedContent = commandContent;
537
+
538
+ // Replace $ARGUMENTS with all arguments joined
539
+ const argsString = args.join(' ');
540
+ processedContent = processedContent.replace(/\$ARGUMENTS/g, argsString);
541
+
542
+ // Replace $1, $2, etc. with positional arguments
543
+ args.forEach((arg, index) => {
544
+ const placeholder = `$${index + 1}`;
545
+ processedContent = processedContent.replace(new RegExp(`\\${placeholder}\\b`, 'g'), arg);
546
+ });
547
+
548
+ res.json({
549
+ type: 'custom',
550
+ command: commandName,
551
+ content: processedContent,
552
+ metadata,
553
+ hasFileIncludes: processedContent.includes('@'),
554
+ hasBashCommands: processedContent.includes('!')
555
+ });
556
+ } catch (error) {
557
+ if (error.code === 'ENOENT') {
558
+ return res.status(404).json({
559
+ error: 'Command not found',
560
+ message: `Command file not found: ${req.body.commandPath}`
561
+ });
562
+ }
563
+
564
+ console.error('Error executing command:', error);
565
+ res.status(500).json({
566
+ error: 'Failed to execute command',
567
+ message: error.message
568
+ });
569
+ }
570
+ });
571
+
572
+ export default router;