upfynai-code 2.5.1 → 2.6.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.
@@ -1,601 +1,598 @@
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 Upfyn-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 = `# Upfyn-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
- '/cost': async (args, context) => {
213
- const tokenUsage = context?.tokenUsage || {};
214
- const provider = context?.provider || 'claude';
215
- const model =
216
- context?.model ||
217
- (provider === 'cursor'
218
- ? CURSOR_MODELS.DEFAULT
219
- : provider === 'codex'
220
- ? CODEX_MODELS.DEFAULT
221
- : CLAUDE_MODELS.DEFAULT);
222
-
223
- const used = Number(tokenUsage.used ?? tokenUsage.totalUsed ?? tokenUsage.total_tokens ?? 0) || 0;
224
- const total =
225
- Number(
226
- tokenUsage.total ??
227
- tokenUsage.contextWindow ??
228
- parseInt(process.env.CONTEXT_WINDOW || '160000', 10),
229
- ) || 160000;
230
- const percentage = total > 0 ? Number(((used / total) * 100).toFixed(1)) : 0;
231
-
232
- const inputTokensRaw =
233
- Number(
234
- tokenUsage.inputTokens ??
235
- tokenUsage.input ??
236
- tokenUsage.cumulativeInputTokens ??
237
- tokenUsage.promptTokens ??
238
- 0,
239
- ) || 0;
240
- const outputTokens =
241
- Number(
242
- tokenUsage.outputTokens ??
243
- tokenUsage.output ??
244
- tokenUsage.cumulativeOutputTokens ??
245
- tokenUsage.completionTokens ??
246
- 0,
247
- ) || 0;
248
- const cacheTokens =
249
- Number(
250
- tokenUsage.cacheReadTokens ??
251
- tokenUsage.cacheCreationTokens ??
252
- tokenUsage.cacheTokens ??
253
- tokenUsage.cachedTokens ??
254
- 0,
255
- ) || 0;
256
-
257
- // If we only have total used tokens, treat them as input for display/estimation.
258
- const inputTokens =
259
- inputTokensRaw > 0 || outputTokens > 0 || cacheTokens > 0 ? inputTokensRaw + cacheTokens : used;
260
-
261
- // Rough default rates by provider (USD / 1M tokens).
262
- const pricingByProvider = {
263
- claude: { input: 3, output: 15 },
264
- cursor: { input: 3, output: 15 },
265
- codex: { input: 1.5, output: 6 },
266
- };
267
- const rates = pricingByProvider[provider] || pricingByProvider.claude;
268
-
269
- const inputCost = (inputTokens / 1_000_000) * rates.input;
270
- const outputCost = (outputTokens / 1_000_000) * rates.output;
271
- const totalCost = inputCost + outputCost;
272
-
273
- return {
274
- type: 'builtin',
275
- action: 'cost',
276
- data: {
277
- tokenUsage: {
278
- used,
279
- total,
280
- percentage,
281
- },
282
- cost: {
283
- input: inputCost.toFixed(4),
284
- output: outputCost.toFixed(4),
285
- total: totalCost.toFixed(4),
286
- },
287
- model,
288
- },
289
- };
290
- },
291
-
292
- '/status': async (args, context) => {
293
- // Read version from package.json
294
- const packageJsonPath = path.join(path.dirname(__dirname), '..', 'package.json');
295
- let version = 'unknown';
296
- let packageName = 'upfynai-code';
297
-
298
- try {
299
- const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
300
- version = packageJson.version;
301
- packageName = packageJson.name;
302
- } catch (err) {
303
- console.error('Error reading package.json:', err);
304
- }
305
-
306
- const uptime = process.uptime();
307
- const uptimeMinutes = Math.floor(uptime / 60);
308
- const uptimeHours = Math.floor(uptimeMinutes / 60);
309
- const uptimeFormatted = uptimeHours > 0
310
- ? `${uptimeHours}h ${uptimeMinutes % 60}m`
311
- : `${uptimeMinutes}m`;
312
-
313
- return {
314
- type: 'builtin',
315
- action: 'status',
316
- data: {
317
- version,
318
- packageName,
319
- uptime: uptimeFormatted,
320
- uptimeSeconds: Math.floor(uptime),
321
- model: context?.model || 'claude-sonnet-4.5',
322
- provider: context?.provider || 'claude',
323
- nodeVersion: process.version,
324
- platform: process.platform
325
- }
326
- };
327
- },
328
-
329
- '/memory': async (args, context) => {
330
- const projectPath = context?.projectPath;
331
-
332
- if (!projectPath) {
333
- return {
334
- type: 'builtin',
335
- action: 'memory',
336
- data: {
337
- error: 'No project selected',
338
- message: 'Please select a project to access its CLAUDE.md file'
339
- }
340
- };
341
- }
342
-
343
- const claudeMdPath = path.join(projectPath, 'CLAUDE.md');
344
-
345
- // Check if CLAUDE.md exists
346
- let exists = false;
347
- try {
348
- await fs.access(claudeMdPath);
349
- exists = true;
350
- } catch (err) {
351
- // File doesn't exist
352
- }
353
-
354
- return {
355
- type: 'builtin',
356
- action: 'memory',
357
- data: {
358
- path: claudeMdPath,
359
- exists,
360
- message: exists
361
- ? `Opening CLAUDE.md at ${claudeMdPath}`
362
- : `CLAUDE.md not found at ${claudeMdPath}. Create it to store project-specific instructions.`
363
- }
364
- };
365
- },
366
-
367
- '/config': async (args, context) => {
368
- return {
369
- type: 'builtin',
370
- action: 'config',
371
- data: {
372
- message: 'Opening settings...'
373
- }
374
- };
375
- },
376
-
377
- '/rewind': async (args, context) => {
378
- const steps = args[0] ? parseInt(args[0]) : 1;
379
-
380
- if (isNaN(steps) || steps < 1) {
381
- return {
382
- type: 'builtin',
383
- action: 'rewind',
384
- data: {
385
- error: 'Invalid steps parameter',
386
- message: 'Usage: /rewind [number] - Rewind conversation by N steps (default: 1)'
387
- }
388
- };
389
- }
390
-
391
- return {
392
- type: 'builtin',
393
- action: 'rewind',
394
- data: {
395
- steps,
396
- message: `Rewinding conversation by ${steps} step${steps > 1 ? 's' : ''}...`
397
- }
398
- };
399
- }
400
- };
401
-
402
- /**
403
- * POST /api/commands/list
404
- * List all available commands from project and user directories
405
- */
406
- router.post('/list', async (req, res) => {
407
- try {
408
- const { projectPath } = req.body;
409
- const allCommands = [...builtInCommands];
410
-
411
- // Scan project-level commands (.claude/commands/)
412
- if (projectPath) {
413
- const projectCommandsDir = path.join(projectPath, '.claude', 'commands');
414
- const projectCommands = await scanCommandsDirectory(
415
- projectCommandsDir,
416
- projectCommandsDir,
417
- 'project'
418
- );
419
- allCommands.push(...projectCommands);
420
- }
421
-
422
- // Scan user-level commands (~/.claude/commands/)
423
- const homeDir = os.homedir();
424
- const userCommandsDir = path.join(homeDir, '.claude', 'commands');
425
- const userCommands = await scanCommandsDirectory(
426
- userCommandsDir,
427
- userCommandsDir,
428
- 'user'
429
- );
430
- allCommands.push(...userCommands);
431
-
432
- // Separate built-in and custom commands
433
- const customCommands = allCommands.filter(cmd => cmd.namespace !== 'builtin');
434
-
435
- // Sort commands alphabetically by name
436
- customCommands.sort((a, b) => a.name.localeCompare(b.name));
437
-
438
- res.json({
439
- builtIn: builtInCommands,
440
- custom: customCommands,
441
- count: allCommands.length
442
- });
443
- } catch (error) {
444
- // command list error
445
- res.status(500).json({
446
- error: 'Failed to list commands',
447
- message: 'An error occurred'
448
- });
449
- }
450
- });
451
-
452
- /**
453
- * POST /api/commands/load
454
- * Load a specific command file and return its content and metadata
455
- */
456
- router.post('/load', async (req, res) => {
457
- try {
458
- const { commandPath } = req.body;
459
-
460
- if (!commandPath) {
461
- return res.status(400).json({
462
- error: 'Command path is required'
463
- });
464
- }
465
-
466
- // Security: Prevent path traversal
467
- const resolvedPath = path.resolve(commandPath);
468
- if (!resolvedPath.startsWith(path.resolve(os.homedir())) &&
469
- !resolvedPath.includes('.claude/commands')) {
470
- return res.status(403).json({
471
- error: 'Access denied',
472
- message: 'Command must be in .claude/commands directory'
473
- });
474
- }
475
-
476
- // Read and parse the command file
477
- const content = await fs.readFile(commandPath, 'utf8');
478
- const { data: metadata, content: commandContent } = matter(content);
479
-
480
- res.json({
481
- path: commandPath,
482
- metadata,
483
- content: commandContent
484
- });
485
- } catch (error) {
486
- if (error.code === 'ENOENT') {
487
- return res.status(404).json({
488
- error: 'Command not found',
489
- message: `Command file not found: ${req.body.commandPath}`
490
- });
491
- }
492
-
493
- // command load error
494
- res.status(500).json({
495
- error: 'Failed to load command',
496
- message: 'An error occurred'
497
- });
498
- }
499
- });
500
-
501
- /**
502
- * POST /api/commands/execute
503
- * Execute a command with argument replacement
504
- * This endpoint prepares the command content but doesn't execute bash commands yet
505
- * (that will be handled in the command parser utility)
506
- */
507
- router.post('/execute', async (req, res) => {
508
- try {
509
- const { commandName, commandPath, args = [], context = {} } = req.body;
510
-
511
- if (!commandName) {
512
- return res.status(400).json({
513
- error: 'Command name is required'
514
- });
515
- }
516
-
517
- // Handle built-in commands
518
- const handler = builtInHandlers[commandName];
519
- if (handler) {
520
- try {
521
- const result = await handler(args, context);
522
- return res.json({
523
- ...result,
524
- command: commandName
525
- });
526
- } catch (error) {
527
- // built-in command error
528
- return res.status(500).json({
529
- error: 'Command execution failed',
530
- message: 'An error occurred',
531
- command: commandName
532
- });
533
- }
534
- }
535
-
536
- // Handle custom commands
537
- if (!commandPath) {
538
- return res.status(400).json({
539
- error: 'Command path is required for custom commands'
540
- });
541
- }
542
-
543
- // Load command content
544
- // Security: validate commandPath is within allowed directories
545
- {
546
- const resolvedPath = path.resolve(commandPath);
547
- const userBase = path.resolve(path.join(os.homedir(), '.claude', 'commands'));
548
- const projectBase = context?.projectPath
549
- ? path.resolve(path.join(context.projectPath, '.claude', 'commands'))
550
- : null;
551
- const isUnder = (base) => {
552
- const rel = path.relative(base, resolvedPath);
553
- return rel !== '' && !rel.startsWith('..') && !path.isAbsolute(rel);
554
- };
555
- if (!(isUnder(userBase) || (projectBase && isUnder(projectBase)))) {
556
- return res.status(403).json({
557
- error: 'Access denied',
558
- message: 'Command must be in .claude/commands directory'
559
- });
560
- }
561
- }
562
- const content = await fs.readFile(commandPath, 'utf8');
563
- const { data: metadata, content: commandContent } = matter(content);
564
- // Basic argument replacement (will be enhanced in command parser utility)
565
- let processedContent = commandContent;
566
-
567
- // Replace $ARGUMENTS with all arguments joined
568
- const argsString = args.join(' ');
569
- processedContent = processedContent.replace(/\$ARGUMENTS/g, argsString);
570
-
571
- // Replace $1, $2, etc. with positional arguments
572
- args.forEach((arg, index) => {
573
- const placeholder = `$${index + 1}`;
574
- processedContent = processedContent.replace(new RegExp(`\\${placeholder}\\b`, 'g'), arg);
575
- });
576
-
577
- res.json({
578
- type: 'custom',
579
- command: commandName,
580
- content: processedContent,
581
- metadata,
582
- hasFileIncludes: processedContent.includes('@'),
583
- hasBashCommands: processedContent.includes('!')
584
- });
585
- } catch (error) {
586
- if (error.code === 'ENOENT') {
587
- return res.status(404).json({
588
- error: 'Command not found',
589
- message: `Command file not found: ${req.body.commandPath}`
590
- });
591
- }
592
-
593
- // command execution error
594
- res.status(500).json({
595
- error: 'Failed to execute command',
596
- message: 'An error occurred'
597
- });
598
- }
599
- });
600
-
601
- export default router;
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
+ }
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
+ }
71
+ }
72
+
73
+ return commands;
74
+ }
75
+
76
+ /**
77
+ * Built-in commands that are always available
78
+ */
79
+ const builtInCommands = [
80
+ {
81
+ name: '/help',
82
+ description: 'Show help documentation for Upfyn-Code',
83
+ namespace: 'builtin',
84
+ metadata: { type: 'builtin' }
85
+ },
86
+ {
87
+ name: '/clear',
88
+ description: 'Clear the conversation history',
89
+ namespace: 'builtin',
90
+ metadata: { type: 'builtin' }
91
+ },
92
+ {
93
+ name: '/model',
94
+ description: 'Switch or view the current AI model',
95
+ namespace: 'builtin',
96
+ metadata: { type: 'builtin' }
97
+ },
98
+ {
99
+ name: '/cost',
100
+ description: 'Display token usage and cost information',
101
+ namespace: 'builtin',
102
+ metadata: { type: 'builtin' }
103
+ },
104
+ {
105
+ name: '/memory',
106
+ description: 'Open CLAUDE.md memory file for editing',
107
+ namespace: 'builtin',
108
+ metadata: { type: 'builtin' }
109
+ },
110
+ {
111
+ name: '/config',
112
+ description: 'Open settings and configuration',
113
+ namespace: 'builtin',
114
+ metadata: { type: 'builtin' }
115
+ },
116
+ {
117
+ name: '/status',
118
+ description: 'Show system status and version information',
119
+ namespace: 'builtin',
120
+ metadata: { type: 'builtin' }
121
+ },
122
+ {
123
+ name: '/rewind',
124
+ description: 'Rewind the conversation to a previous state',
125
+ namespace: 'builtin',
126
+ metadata: { type: 'builtin' }
127
+ }
128
+ ];
129
+
130
+ /**
131
+ * Built-in command handlers
132
+ * Each handler returns { type: 'builtin', action: string, data: any }
133
+ */
134
+ const builtInHandlers = {
135
+ '/help': async (args, context) => {
136
+ const helpText = `# Upfyn-Code Commands
137
+
138
+ ## Built-in Commands
139
+
140
+ ${builtInCommands.map(cmd => `### ${cmd.name}
141
+ ${cmd.description}
142
+ `).join('\n')}
143
+
144
+ ## Custom Commands
145
+
146
+ Custom commands can be created in:
147
+ - Project: \`.claude/commands/\` (project-specific)
148
+ - User: \`~/.claude/commands/\` (available in all projects)
149
+
150
+ ### Command Syntax
151
+
152
+ - **Arguments**: Use \`$ARGUMENTS\` for all args or \`$1\`, \`$2\`, etc. for positional
153
+ - **File Includes**: Use \`@filename\` to include file contents
154
+ - **Bash Commands**: Use \`!command\` to execute bash commands
155
+
156
+ ### Examples
157
+
158
+ \`\`\`markdown
159
+ /mycommand arg1 arg2
160
+ \`\`\`
161
+ `;
162
+
163
+ return {
164
+ type: 'builtin',
165
+ action: 'help',
166
+ data: {
167
+ content: helpText,
168
+ format: 'markdown'
169
+ }
170
+ };
171
+ },
172
+
173
+ '/clear': async (args, context) => {
174
+ return {
175
+ type: 'builtin',
176
+ action: 'clear',
177
+ data: {
178
+ message: 'Conversation history cleared'
179
+ }
180
+ };
181
+ },
182
+
183
+ '/model': async (args, context) => {
184
+ // Read available models from centralized constants
185
+ const availableModels = {
186
+ claude: CLAUDE_MODELS.OPTIONS.map(o => o.value),
187
+ cursor: CURSOR_MODELS.OPTIONS.map(o => o.value),
188
+ codex: CODEX_MODELS.OPTIONS.map(o => o.value)
189
+ };
190
+
191
+ const currentProvider = context?.provider || 'claude';
192
+ const currentModel = context?.model || CLAUDE_MODELS.DEFAULT;
193
+
194
+ return {
195
+ type: 'builtin',
196
+ action: 'model',
197
+ data: {
198
+ current: {
199
+ provider: currentProvider,
200
+ model: currentModel
201
+ },
202
+ available: availableModels,
203
+ message: args.length > 0
204
+ ? `Switching to model: ${args[0]}`
205
+ : `Current model: ${currentModel}`
206
+ }
207
+ };
208
+ },
209
+
210
+ '/cost': async (args, context) => {
211
+ const tokenUsage = context?.tokenUsage || {};
212
+ const provider = context?.provider || 'claude';
213
+ const model =
214
+ context?.model ||
215
+ (provider === 'cursor'
216
+ ? CURSOR_MODELS.DEFAULT
217
+ : provider === 'codex'
218
+ ? CODEX_MODELS.DEFAULT
219
+ : CLAUDE_MODELS.DEFAULT);
220
+
221
+ const used = Number(tokenUsage.used ?? tokenUsage.totalUsed ?? tokenUsage.total_tokens ?? 0) || 0;
222
+ const total =
223
+ Number(
224
+ tokenUsage.total ??
225
+ tokenUsage.contextWindow ??
226
+ parseInt(process.env.CONTEXT_WINDOW || '160000', 10),
227
+ ) || 160000;
228
+ const percentage = total > 0 ? Number(((used / total) * 100).toFixed(1)) : 0;
229
+
230
+ const inputTokensRaw =
231
+ Number(
232
+ tokenUsage.inputTokens ??
233
+ tokenUsage.input ??
234
+ tokenUsage.cumulativeInputTokens ??
235
+ tokenUsage.promptTokens ??
236
+ 0,
237
+ ) || 0;
238
+ const outputTokens =
239
+ Number(
240
+ tokenUsage.outputTokens ??
241
+ tokenUsage.output ??
242
+ tokenUsage.cumulativeOutputTokens ??
243
+ tokenUsage.completionTokens ??
244
+ 0,
245
+ ) || 0;
246
+ const cacheTokens =
247
+ Number(
248
+ tokenUsage.cacheReadTokens ??
249
+ tokenUsage.cacheCreationTokens ??
250
+ tokenUsage.cacheTokens ??
251
+ tokenUsage.cachedTokens ??
252
+ 0,
253
+ ) || 0;
254
+
255
+ // If we only have total used tokens, treat them as input for display/estimation.
256
+ const inputTokens =
257
+ inputTokensRaw > 0 || outputTokens > 0 || cacheTokens > 0 ? inputTokensRaw + cacheTokens : used;
258
+
259
+ // Rough default rates by provider (USD / 1M tokens).
260
+ const pricingByProvider = {
261
+ claude: { input: 3, output: 15 },
262
+ cursor: { input: 3, output: 15 },
263
+ codex: { input: 1.5, output: 6 },
264
+ };
265
+ const rates = pricingByProvider[provider] || pricingByProvider.claude;
266
+
267
+ const inputCost = (inputTokens / 1_000_000) * rates.input;
268
+ const outputCost = (outputTokens / 1_000_000) * rates.output;
269
+ const totalCost = inputCost + outputCost;
270
+
271
+ return {
272
+ type: 'builtin',
273
+ action: 'cost',
274
+ data: {
275
+ tokenUsage: {
276
+ used,
277
+ total,
278
+ percentage,
279
+ },
280
+ cost: {
281
+ input: inputCost.toFixed(4),
282
+ output: outputCost.toFixed(4),
283
+ total: totalCost.toFixed(4),
284
+ },
285
+ model,
286
+ },
287
+ };
288
+ },
289
+
290
+ '/status': async (args, context) => {
291
+ // Read version from package.json
292
+ const packageJsonPath = path.join(path.dirname(__dirname), '..', 'package.json');
293
+ let version = 'unknown';
294
+ let packageName = 'upfynai-code';
295
+
296
+ try {
297
+ const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
298
+ version = packageJson.version;
299
+ packageName = packageJson.name;
300
+ } catch (err) {
301
+ }
302
+
303
+ const uptime = process.uptime();
304
+ const uptimeMinutes = Math.floor(uptime / 60);
305
+ const uptimeHours = Math.floor(uptimeMinutes / 60);
306
+ const uptimeFormatted = uptimeHours > 0
307
+ ? `${uptimeHours}h ${uptimeMinutes % 60}m`
308
+ : `${uptimeMinutes}m`;
309
+
310
+ return {
311
+ type: 'builtin',
312
+ action: 'status',
313
+ data: {
314
+ version,
315
+ packageName,
316
+ uptime: uptimeFormatted,
317
+ uptimeSeconds: Math.floor(uptime),
318
+ model: context?.model || 'claude-sonnet-4.5',
319
+ provider: context?.provider || 'claude',
320
+ nodeVersion: process.version,
321
+ platform: process.platform
322
+ }
323
+ };
324
+ },
325
+
326
+ '/memory': async (args, context) => {
327
+ const projectPath = context?.projectPath;
328
+
329
+ if (!projectPath) {
330
+ return {
331
+ type: 'builtin',
332
+ action: 'memory',
333
+ data: {
334
+ error: 'No project selected',
335
+ message: 'Please select a project to access its CLAUDE.md file'
336
+ }
337
+ };
338
+ }
339
+
340
+ const claudeMdPath = path.join(projectPath, 'CLAUDE.md');
341
+
342
+ // Check if CLAUDE.md exists
343
+ let exists = false;
344
+ try {
345
+ await fs.access(claudeMdPath);
346
+ exists = true;
347
+ } catch (err) {
348
+ // File doesn't exist
349
+ }
350
+
351
+ return {
352
+ type: 'builtin',
353
+ action: 'memory',
354
+ data: {
355
+ path: claudeMdPath,
356
+ exists,
357
+ message: exists
358
+ ? `Opening CLAUDE.md at ${claudeMdPath}`
359
+ : `CLAUDE.md not found at ${claudeMdPath}. Create it to store project-specific instructions.`
360
+ }
361
+ };
362
+ },
363
+
364
+ '/config': async (args, context) => {
365
+ return {
366
+ type: 'builtin',
367
+ action: 'config',
368
+ data: {
369
+ message: 'Opening settings...'
370
+ }
371
+ };
372
+ },
373
+
374
+ '/rewind': async (args, context) => {
375
+ const steps = args[0] ? parseInt(args[0]) : 1;
376
+
377
+ if (isNaN(steps) || steps < 1) {
378
+ return {
379
+ type: 'builtin',
380
+ action: 'rewind',
381
+ data: {
382
+ error: 'Invalid steps parameter',
383
+ message: 'Usage: /rewind [number] - Rewind conversation by N steps (default: 1)'
384
+ }
385
+ };
386
+ }
387
+
388
+ return {
389
+ type: 'builtin',
390
+ action: 'rewind',
391
+ data: {
392
+ steps,
393
+ message: `Rewinding conversation by ${steps} step${steps > 1 ? 's' : ''}...`
394
+ }
395
+ };
396
+ }
397
+ };
398
+
399
+ /**
400
+ * POST /api/commands/list
401
+ * List all available commands from project and user directories
402
+ */
403
+ router.post('/list', async (req, res) => {
404
+ try {
405
+ const { projectPath } = req.body;
406
+ const allCommands = [...builtInCommands];
407
+
408
+ // Scan project-level commands (.claude/commands/)
409
+ if (projectPath) {
410
+ const projectCommandsDir = path.join(projectPath, '.claude', 'commands');
411
+ const projectCommands = await scanCommandsDirectory(
412
+ projectCommandsDir,
413
+ projectCommandsDir,
414
+ 'project'
415
+ );
416
+ allCommands.push(...projectCommands);
417
+ }
418
+
419
+ // Scan user-level commands (~/.claude/commands/)
420
+ const homeDir = os.homedir();
421
+ const userCommandsDir = path.join(homeDir, '.claude', 'commands');
422
+ const userCommands = await scanCommandsDirectory(
423
+ userCommandsDir,
424
+ userCommandsDir,
425
+ 'user'
426
+ );
427
+ allCommands.push(...userCommands);
428
+
429
+ // Separate built-in and custom commands
430
+ const customCommands = allCommands.filter(cmd => cmd.namespace !== 'builtin');
431
+
432
+ // Sort commands alphabetically by name
433
+ customCommands.sort((a, b) => a.name.localeCompare(b.name));
434
+
435
+ res.json({
436
+ builtIn: builtInCommands,
437
+ custom: customCommands,
438
+ count: allCommands.length
439
+ });
440
+ } catch (error) {
441
+ // command list error
442
+ res.status(500).json({
443
+ error: 'Failed to list commands',
444
+ message: 'An error occurred'
445
+ });
446
+ }
447
+ });
448
+
449
+ /**
450
+ * POST /api/commands/load
451
+ * Load a specific command file and return its content and metadata
452
+ */
453
+ router.post('/load', async (req, res) => {
454
+ try {
455
+ const { commandPath } = req.body;
456
+
457
+ if (!commandPath) {
458
+ return res.status(400).json({
459
+ error: 'Command path is required'
460
+ });
461
+ }
462
+
463
+ // Security: Prevent path traversal
464
+ const resolvedPath = path.resolve(commandPath);
465
+ if (!resolvedPath.startsWith(path.resolve(os.homedir())) &&
466
+ !resolvedPath.includes('.claude/commands')) {
467
+ return res.status(403).json({
468
+ error: 'Access denied',
469
+ message: 'Command must be in .claude/commands directory'
470
+ });
471
+ }
472
+
473
+ // Read and parse the command file
474
+ const content = await fs.readFile(commandPath, 'utf8');
475
+ const { data: metadata, content: commandContent } = matter(content);
476
+
477
+ res.json({
478
+ path: commandPath,
479
+ metadata,
480
+ content: commandContent
481
+ });
482
+ } catch (error) {
483
+ if (error.code === 'ENOENT') {
484
+ return res.status(404).json({
485
+ error: 'Command not found',
486
+ message: `Command file not found: ${req.body.commandPath}`
487
+ });
488
+ }
489
+
490
+ // command load error
491
+ res.status(500).json({
492
+ error: 'Failed to load command',
493
+ message: 'An error occurred'
494
+ });
495
+ }
496
+ });
497
+
498
+ /**
499
+ * POST /api/commands/execute
500
+ * Execute a command with argument replacement
501
+ * This endpoint prepares the command content but doesn't execute bash commands yet
502
+ * (that will be handled in the command parser utility)
503
+ */
504
+ router.post('/execute', async (req, res) => {
505
+ try {
506
+ const { commandName, commandPath, args = [], context = {} } = req.body;
507
+
508
+ if (!commandName) {
509
+ return res.status(400).json({
510
+ error: 'Command name is required'
511
+ });
512
+ }
513
+
514
+ // Handle built-in commands
515
+ const handler = builtInHandlers[commandName];
516
+ if (handler) {
517
+ try {
518
+ const result = await handler(args, context);
519
+ return res.json({
520
+ ...result,
521
+ command: commandName
522
+ });
523
+ } catch (error) {
524
+ // built-in command error
525
+ return res.status(500).json({
526
+ error: 'Command execution failed',
527
+ message: 'An error occurred',
528
+ command: commandName
529
+ });
530
+ }
531
+ }
532
+
533
+ // Handle custom commands
534
+ if (!commandPath) {
535
+ return res.status(400).json({
536
+ error: 'Command path is required for custom commands'
537
+ });
538
+ }
539
+
540
+ // Load command content
541
+ // Security: validate commandPath is within allowed directories
542
+ {
543
+ const resolvedPath = path.resolve(commandPath);
544
+ const userBase = path.resolve(path.join(os.homedir(), '.claude', 'commands'));
545
+ const projectBase = context?.projectPath
546
+ ? path.resolve(path.join(context.projectPath, '.claude', 'commands'))
547
+ : null;
548
+ const isUnder = (base) => {
549
+ const rel = path.relative(base, resolvedPath);
550
+ return rel !== '' && !rel.startsWith('..') && !path.isAbsolute(rel);
551
+ };
552
+ if (!(isUnder(userBase) || (projectBase && isUnder(projectBase)))) {
553
+ return res.status(403).json({
554
+ error: 'Access denied',
555
+ message: 'Command must be in .claude/commands directory'
556
+ });
557
+ }
558
+ }
559
+ const content = await fs.readFile(commandPath, 'utf8');
560
+ const { data: metadata, content: commandContent } = matter(content);
561
+ // Basic argument replacement (will be enhanced in command parser utility)
562
+ let processedContent = commandContent;
563
+
564
+ // Replace $ARGUMENTS with all arguments joined
565
+ const argsString = args.join(' ');
566
+ processedContent = processedContent.replace(/\$ARGUMENTS/g, argsString);
567
+
568
+ // Replace $1, $2, etc. with positional arguments
569
+ args.forEach((arg, index) => {
570
+ const placeholder = `$${index + 1}`;
571
+ processedContent = processedContent.replace(new RegExp(`\\${placeholder}\\b`, 'g'), arg);
572
+ });
573
+
574
+ res.json({
575
+ type: 'custom',
576
+ command: commandName,
577
+ content: processedContent,
578
+ metadata,
579
+ hasFileIncludes: processedContent.includes('@'),
580
+ hasBashCommands: processedContent.includes('!')
581
+ });
582
+ } catch (error) {
583
+ if (error.code === 'ENOENT') {
584
+ return res.status(404).json({
585
+ error: 'Command not found',
586
+ message: `Command file not found: ${req.body.commandPath}`
587
+ });
588
+ }
589
+
590
+ // command execution error
591
+ res.status(500).json({
592
+ error: 'Failed to execute command',
593
+ message: 'An error occurred'
594
+ });
595
+ }
596
+ });
597
+
598
+ export default router;