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.
Files changed (137) hide show
  1. package/LICENSE +675 -0
  2. package/README.md +371 -0
  3. package/dist/api-docs.html +879 -0
  4. package/dist/assets/KaTeX_AMS-Regular-BQhdFMY1.woff2 +0 -0
  5. package/dist/assets/KaTeX_AMS-Regular-DMm9YOAa.woff +0 -0
  6. package/dist/assets/KaTeX_AMS-Regular-DRggAlZN.ttf +0 -0
  7. package/dist/assets/KaTeX_Caligraphic-Bold-ATXxdsX0.ttf +0 -0
  8. package/dist/assets/KaTeX_Caligraphic-Bold-BEiXGLvX.woff +0 -0
  9. package/dist/assets/KaTeX_Caligraphic-Bold-Dq_IR9rO.woff2 +0 -0
  10. package/dist/assets/KaTeX_Caligraphic-Regular-CTRA-rTL.woff +0 -0
  11. package/dist/assets/KaTeX_Caligraphic-Regular-Di6jR-x-.woff2 +0 -0
  12. package/dist/assets/KaTeX_Caligraphic-Regular-wX97UBjC.ttf +0 -0
  13. package/dist/assets/KaTeX_Fraktur-Bold-BdnERNNW.ttf +0 -0
  14. package/dist/assets/KaTeX_Fraktur-Bold-BsDP51OF.woff +0 -0
  15. package/dist/assets/KaTeX_Fraktur-Bold-CL6g_b3V.woff2 +0 -0
  16. package/dist/assets/KaTeX_Fraktur-Regular-CB_wures.ttf +0 -0
  17. package/dist/assets/KaTeX_Fraktur-Regular-CTYiF6lA.woff2 +0 -0
  18. package/dist/assets/KaTeX_Fraktur-Regular-Dxdc4cR9.woff +0 -0
  19. package/dist/assets/KaTeX_Main-Bold-Cx986IdX.woff2 +0 -0
  20. package/dist/assets/KaTeX_Main-Bold-Jm3AIy58.woff +0 -0
  21. package/dist/assets/KaTeX_Main-Bold-waoOVXN0.ttf +0 -0
  22. package/dist/assets/KaTeX_Main-BoldItalic-DxDJ3AOS.woff2 +0 -0
  23. package/dist/assets/KaTeX_Main-BoldItalic-DzxPMmG6.ttf +0 -0
  24. package/dist/assets/KaTeX_Main-BoldItalic-SpSLRI95.woff +0 -0
  25. package/dist/assets/KaTeX_Main-Italic-3WenGoN9.ttf +0 -0
  26. package/dist/assets/KaTeX_Main-Italic-BMLOBm91.woff +0 -0
  27. package/dist/assets/KaTeX_Main-Italic-NWA7e6Wa.woff2 +0 -0
  28. package/dist/assets/KaTeX_Main-Regular-B22Nviop.woff2 +0 -0
  29. package/dist/assets/KaTeX_Main-Regular-Dr94JaBh.woff +0 -0
  30. package/dist/assets/KaTeX_Main-Regular-ypZvNtVU.ttf +0 -0
  31. package/dist/assets/KaTeX_Math-BoldItalic-B3XSjfu4.ttf +0 -0
  32. package/dist/assets/KaTeX_Math-BoldItalic-CZnvNsCZ.woff2 +0 -0
  33. package/dist/assets/KaTeX_Math-BoldItalic-iY-2wyZ7.woff +0 -0
  34. package/dist/assets/KaTeX_Math-Italic-DA0__PXp.woff +0 -0
  35. package/dist/assets/KaTeX_Math-Italic-flOr_0UB.ttf +0 -0
  36. package/dist/assets/KaTeX_Math-Italic-t53AETM-.woff2 +0 -0
  37. package/dist/assets/KaTeX_SansSerif-Bold-CFMepnvq.ttf +0 -0
  38. package/dist/assets/KaTeX_SansSerif-Bold-D1sUS0GD.woff2 +0 -0
  39. package/dist/assets/KaTeX_SansSerif-Bold-DbIhKOiC.woff +0 -0
  40. package/dist/assets/KaTeX_SansSerif-Italic-C3H0VqGB.woff2 +0 -0
  41. package/dist/assets/KaTeX_SansSerif-Italic-DN2j7dab.woff +0 -0
  42. package/dist/assets/KaTeX_SansSerif-Italic-YYjJ1zSn.ttf +0 -0
  43. package/dist/assets/KaTeX_SansSerif-Regular-BNo7hRIc.ttf +0 -0
  44. package/dist/assets/KaTeX_SansSerif-Regular-CS6fqUqJ.woff +0 -0
  45. package/dist/assets/KaTeX_SansSerif-Regular-DDBCnlJ7.woff2 +0 -0
  46. package/dist/assets/KaTeX_Script-Regular-C5JkGWo-.ttf +0 -0
  47. package/dist/assets/KaTeX_Script-Regular-D3wIWfF6.woff2 +0 -0
  48. package/dist/assets/KaTeX_Script-Regular-D5yQViql.woff +0 -0
  49. package/dist/assets/KaTeX_Size1-Regular-C195tn64.woff +0 -0
  50. package/dist/assets/KaTeX_Size1-Regular-Dbsnue_I.ttf +0 -0
  51. package/dist/assets/KaTeX_Size1-Regular-mCD8mA8B.woff2 +0 -0
  52. package/dist/assets/KaTeX_Size2-Regular-B7gKUWhC.ttf +0 -0
  53. package/dist/assets/KaTeX_Size2-Regular-Dy4dx90m.woff2 +0 -0
  54. package/dist/assets/KaTeX_Size2-Regular-oD1tc_U0.woff +0 -0
  55. package/dist/assets/KaTeX_Size3-Regular-CTq5MqoE.woff +0 -0
  56. package/dist/assets/KaTeX_Size3-Regular-DgpXs0kz.ttf +0 -0
  57. package/dist/assets/KaTeX_Size4-Regular-BF-4gkZK.woff +0 -0
  58. package/dist/assets/KaTeX_Size4-Regular-DWFBv043.ttf +0 -0
  59. package/dist/assets/KaTeX_Size4-Regular-Dl5lxZxV.woff2 +0 -0
  60. package/dist/assets/KaTeX_Typewriter-Regular-C0xS9mPB.woff +0 -0
  61. package/dist/assets/KaTeX_Typewriter-Regular-CO6r4hn1.woff2 +0 -0
  62. package/dist/assets/KaTeX_Typewriter-Regular-D3Ib7_Hf.ttf +0 -0
  63. package/dist/assets/index-cIxJ4RXb.js +1226 -0
  64. package/dist/assets/index-oyEz69sP.css +32 -0
  65. package/dist/assets/vendor-codemirror-CJLzwpLB.js +39 -0
  66. package/dist/assets/vendor-react-DcyRfQm3.js +59 -0
  67. package/dist/assets/vendor-xterm-DfaPXD3y.js +66 -0
  68. package/dist/clear-cache.html +85 -0
  69. package/dist/convert-icons.md +53 -0
  70. package/dist/favicon.png +0 -0
  71. package/dist/favicon.svg +9 -0
  72. package/dist/generate-icons.js +49 -0
  73. package/dist/icons/claude-ai-icon.svg +1 -0
  74. package/dist/icons/codex-white.svg +3 -0
  75. package/dist/icons/codex.svg +3 -0
  76. package/dist/icons/cursor-white.svg +12 -0
  77. package/dist/icons/cursor.svg +1 -0
  78. package/dist/icons/generate-icons.md +19 -0
  79. package/dist/icons/icon-128x128.png +0 -0
  80. package/dist/icons/icon-128x128.svg +12 -0
  81. package/dist/icons/icon-144x144.png +0 -0
  82. package/dist/icons/icon-144x144.svg +12 -0
  83. package/dist/icons/icon-152x152.png +0 -0
  84. package/dist/icons/icon-152x152.svg +12 -0
  85. package/dist/icons/icon-192x192.png +0 -0
  86. package/dist/icons/icon-192x192.svg +12 -0
  87. package/dist/icons/icon-384x384.png +0 -0
  88. package/dist/icons/icon-384x384.svg +12 -0
  89. package/dist/icons/icon-512x512.png +0 -0
  90. package/dist/icons/icon-512x512.svg +12 -0
  91. package/dist/icons/icon-72x72.png +0 -0
  92. package/dist/icons/icon-72x72.svg +12 -0
  93. package/dist/icons/icon-96x96.png +0 -0
  94. package/dist/icons/icon-96x96.svg +12 -0
  95. package/dist/icons/icon-template.svg +12 -0
  96. package/dist/index.html +52 -0
  97. package/dist/logo-128.png +0 -0
  98. package/dist/logo-256.png +0 -0
  99. package/dist/logo-32.png +0 -0
  100. package/dist/logo-512.png +0 -0
  101. package/dist/logo-64.png +0 -0
  102. package/dist/logo.svg +17 -0
  103. package/dist/manifest.json +61 -0
  104. package/dist/screenshots/cli-selection.png +0 -0
  105. package/dist/screenshots/desktop-main.png +0 -0
  106. package/dist/screenshots/mobile-chat.png +0 -0
  107. package/dist/screenshots/tools-modal.png +0 -0
  108. package/dist/sw.js +49 -0
  109. package/package.json +109 -0
  110. package/server/claude-sdk.js +721 -0
  111. package/server/cli.js +327 -0
  112. package/server/cursor-cli.js +267 -0
  113. package/server/database/auth.db +0 -0
  114. package/server/database/db.js +361 -0
  115. package/server/database/init.sql +52 -0
  116. package/server/index.js +1747 -0
  117. package/server/middleware/auth.js +111 -0
  118. package/server/openai-codex.js +389 -0
  119. package/server/projects.js +1604 -0
  120. package/server/routes/agent.js +1230 -0
  121. package/server/routes/auth.js +135 -0
  122. package/server/routes/cli-auth.js +263 -0
  123. package/server/routes/codex.js +345 -0
  124. package/server/routes/commands.js +521 -0
  125. package/server/routes/cursor.js +795 -0
  126. package/server/routes/git.js +1128 -0
  127. package/server/routes/mcp-utils.js +48 -0
  128. package/server/routes/mcp.js +552 -0
  129. package/server/routes/projects.js +378 -0
  130. package/server/routes/settings.js +178 -0
  131. package/server/routes/taskmaster.js +1963 -0
  132. package/server/routes/user.js +106 -0
  133. package/server/utils/commandParser.js +303 -0
  134. package/server/utils/gitConfig.js +24 -0
  135. package/server/utils/mcp-detector.js +198 -0
  136. package/server/utils/taskmaster-websocket.js +129 -0
  137. package/shared/modelConstants.js +65 -0
package/server/cli.js ADDED
@@ -0,0 +1,327 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Claude Code UI CLI
4
+ *
5
+ * Provides command-line utilities for managing Claude Code UI
6
+ *
7
+ * Commands:
8
+ * (no args) - Start the server (default)
9
+ * start - Start the server
10
+ * status - Show configuration and data locations
11
+ * help - Show help information
12
+ * version - Show version information
13
+ */
14
+
15
+ import fs from 'fs';
16
+ import path from 'path';
17
+ import os from 'os';
18
+ import { fileURLToPath } from 'url';
19
+ import { dirname } from 'path';
20
+
21
+ const __filename = fileURLToPath(import.meta.url);
22
+ const __dirname = dirname(__filename);
23
+
24
+ // ANSI color codes for terminal output
25
+ const colors = {
26
+ reset: '\x1b[0m',
27
+ bright: '\x1b[1m',
28
+ dim: '\x1b[2m',
29
+
30
+ // Foreground colors
31
+ cyan: '\x1b[36m',
32
+ green: '\x1b[32m',
33
+ yellow: '\x1b[33m',
34
+ blue: '\x1b[34m',
35
+ magenta: '\x1b[35m',
36
+ white: '\x1b[37m',
37
+ gray: '\x1b[90m',
38
+ };
39
+
40
+ // Helper to colorize text
41
+ const c = {
42
+ info: (text) => `${colors.cyan}${text}${colors.reset}`,
43
+ ok: (text) => `${colors.green}${text}${colors.reset}`,
44
+ warn: (text) => `${colors.yellow}${text}${colors.reset}`,
45
+ error: (text) => `${colors.yellow}${text}${colors.reset}`,
46
+ tip: (text) => `${colors.blue}${text}${colors.reset}`,
47
+ bright: (text) => `${colors.bright}${text}${colors.reset}`,
48
+ dim: (text) => `${colors.dim}${text}${colors.reset}`,
49
+ };
50
+
51
+ // Load package.json for version info
52
+ const packageJsonPath = path.join(__dirname, '../package.json');
53
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
54
+
55
+ // Load environment variables from .env file if it exists
56
+ function loadEnvFile() {
57
+ try {
58
+ const envPath = path.join(__dirname, '../.env');
59
+ const envFile = fs.readFileSync(envPath, 'utf8');
60
+ envFile.split('\n').forEach(line => {
61
+ const trimmedLine = line.trim();
62
+ if (trimmedLine && !trimmedLine.startsWith('#')) {
63
+ const [key, ...valueParts] = trimmedLine.split('=');
64
+ if (key && valueParts.length > 0 && !process.env[key]) {
65
+ process.env[key] = valueParts.join('=').trim();
66
+ }
67
+ }
68
+ });
69
+ } catch (e) {
70
+ // .env file is optional
71
+ }
72
+ }
73
+
74
+ // Get the database path (same logic as db.js)
75
+ function getDatabasePath() {
76
+ loadEnvFile();
77
+ return process.env.DATABASE_PATH || path.join(__dirname, 'database', 'auth.db');
78
+ }
79
+
80
+ // Get the installation directory
81
+ function getInstallDir() {
82
+ return path.join(__dirname, '..');
83
+ }
84
+
85
+ // Show status command
86
+ function showStatus() {
87
+ console.log(`\n${c.bright('Claude Code UI - Status')}\n`);
88
+ console.log(c.dim('═'.repeat(60)));
89
+
90
+ // Version info
91
+ console.log(`\n${c.info('[INFO]')} Version: ${c.bright(packageJson.version)}`);
92
+
93
+ // Installation location
94
+ const installDir = getInstallDir();
95
+ console.log(`\n${c.info('[INFO]')} Installation Directory:`);
96
+ console.log(` ${c.dim(installDir)}`);
97
+
98
+ // Database location
99
+ const dbPath = getDatabasePath();
100
+ const dbExists = fs.existsSync(dbPath);
101
+ console.log(`\n${c.info('[INFO]')} Database Location:`);
102
+ console.log(` ${c.dim(dbPath)}`);
103
+ console.log(` Status: ${dbExists ? c.ok('[OK] Exists') : c.warn('[WARN] Not created yet (will be created on first run)')}`);
104
+
105
+ if (dbExists) {
106
+ const stats = fs.statSync(dbPath);
107
+ console.log(` Size: ${c.dim((stats.size / 1024).toFixed(2) + ' KB')}`);
108
+ console.log(` Modified: ${c.dim(stats.mtime.toLocaleString())}`);
109
+ }
110
+
111
+ // Environment variables
112
+ console.log(`\n${c.info('[INFO]')} Configuration:`);
113
+ console.log(` PORT: ${c.bright(process.env.PORT || '3001')} ${c.dim(process.env.PORT ? '' : '(default)')}`);
114
+ console.log(` DATABASE_PATH: ${c.dim(process.env.DATABASE_PATH || '(using default location)')}`);
115
+ console.log(` CLAUDE_CLI_PATH: ${c.dim(process.env.CLAUDE_CLI_PATH || 'claude (default)')}`);
116
+ console.log(` CONTEXT_WINDOW: ${c.dim(process.env.CONTEXT_WINDOW || '160000 (default)')}`);
117
+
118
+ // Claude projects folder
119
+ const claudeProjectsPath = path.join(os.homedir(), '.claude', 'projects');
120
+ const projectsExists = fs.existsSync(claudeProjectsPath);
121
+ console.log(`\n${c.info('[INFO]')} Claude Projects Folder:`);
122
+ console.log(` ${c.dim(claudeProjectsPath)}`);
123
+ console.log(` Status: ${projectsExists ? c.ok('[OK] Exists') : c.warn('[WARN] Not found')}`);
124
+
125
+ // Config file location
126
+ const envFilePath = path.join(__dirname, '../.env');
127
+ const envExists = fs.existsSync(envFilePath);
128
+ console.log(`\n${c.info('[INFO]')} Configuration File:`);
129
+ console.log(` ${c.dim(envFilePath)}`);
130
+ console.log(` Status: ${envExists ? c.ok('[OK] Exists') : c.warn('[WARN] Not found (using defaults)')}`);
131
+
132
+ console.log('\n' + c.dim('═'.repeat(60)));
133
+ console.log(`\n${c.tip('[TIP]')} Hints:`);
134
+ console.log(` ${c.dim('>')} Use ${c.bright('weccli --port 8080')} to run on a custom port`);
135
+ console.log(` ${c.dim('>')} Use ${c.bright('weccli --database-path /path/to/db')} for custom database`);
136
+ console.log(` ${c.dim('>')} Run ${c.bright('weccli help')} for all options`);
137
+ console.log(` ${c.dim('>')} Access the UI at http://localhost:${process.env.PORT || '3001'}\n`);
138
+ }
139
+
140
+ // Show help
141
+ function showHelp() {
142
+ console.log(`
143
+ ╔═══════════════════════════════════════════════════════════════╗
144
+ ║ Claude Code UI - Command Line Tool ║
145
+ ╚═══════════════════════════════════════════════════════════════╝
146
+
147
+ Usage:
148
+ claude-wec [command] [options]
149
+ weccli [command] [options]
150
+
151
+ Commands:
152
+ start Start the Claude Code UI server (default)
153
+ status Show configuration and data locations
154
+ update Update to the latest version
155
+ help Show this help information
156
+ version Show version information
157
+
158
+ Options:
159
+ -p, --port <port> Set server port (default: 3001)
160
+ --database-path <path> Set custom database location
161
+ -h, --help Show this help information
162
+ -v, --version Show version information
163
+
164
+ Examples:
165
+ $ weccli # Start with defaults
166
+ $ weccli --port 8080 # Start on port 8080
167
+ $ weccli -p 3000 # Short form for port
168
+ $ weccli start --port 4000 # Explicit start command
169
+ $ weccli status # Show configuration
170
+
171
+ Environment Variables:
172
+ PORT Set server port (default: 3001)
173
+ DATABASE_PATH Set custom database location
174
+ CLAUDE_CLI_PATH Set custom Claude CLI path
175
+ CONTEXT_WINDOW Set context window size (default: 160000)
176
+
177
+ Documentation:
178
+ ${packageJson.homepage || 'https://github.com/siteboon/claudecodeui'}
179
+
180
+ Report Issues:
181
+ ${packageJson.bugs?.url || 'https://github.com/siteboon/claudecodeui/issues'}
182
+ `);
183
+ }
184
+
185
+ // Show version
186
+ function showVersion() {
187
+ console.log(`${packageJson.version}`);
188
+ }
189
+
190
+ // Compare semver versions, returns true if v1 > v2
191
+ function isNewerVersion(v1, v2) {
192
+ const parts1 = v1.split('.').map(Number);
193
+ const parts2 = v2.split('.').map(Number);
194
+ for (let i = 0; i < 3; i++) {
195
+ if (parts1[i] > parts2[i]) return true;
196
+ if (parts1[i] < parts2[i]) return false;
197
+ }
198
+ return false;
199
+ }
200
+
201
+ // Check for updates
202
+ async function checkForUpdates(silent = false) {
203
+ try {
204
+ const { execSync } = await import('child_process');
205
+ const latestVersion = execSync('npm show claude-wec version', { encoding: 'utf8' }).trim();
206
+ const currentVersion = packageJson.version;
207
+
208
+ if (isNewerVersion(latestVersion, currentVersion)) {
209
+ console.log(`\n${c.warn('[UPDATE]')} New version available: ${c.bright(latestVersion)} (current: ${currentVersion})`);
210
+ console.log(` Run ${c.bright('weccli update')} to update\n`);
211
+ return { hasUpdate: true, latestVersion, currentVersion };
212
+ } else if (!silent) {
213
+ console.log(`${c.ok('[OK]')} You are on the latest version (${currentVersion})`);
214
+ }
215
+ return { hasUpdate: false, latestVersion, currentVersion };
216
+ } catch (e) {
217
+ if (!silent) {
218
+ console.log(`${c.warn('[WARN]')} Could not check for updates`);
219
+ }
220
+ return { hasUpdate: false, error: e.message };
221
+ }
222
+ }
223
+
224
+ // Update the package
225
+ async function updatePackage() {
226
+ try {
227
+ const { execSync } = await import('child_process');
228
+ console.log(`${c.info('[INFO]')} Checking for updates...`);
229
+
230
+ const { hasUpdate, latestVersion, currentVersion } = await checkForUpdates(true);
231
+
232
+ if (!hasUpdate) {
233
+ console.log(`${c.ok('[OK]')} Already on the latest version (${currentVersion})`);
234
+ return;
235
+ }
236
+
237
+ console.log(`${c.info('[INFO]')} Updating from ${currentVersion} to ${latestVersion}...`);
238
+ execSync('npm update -g claude-wec', { stdio: 'inherit' });
239
+ console.log(`${c.ok('[OK]')} Update complete! Restart weccli to use the new version.`);
240
+ } catch (e) {
241
+ console.error(`${c.error('[ERROR]')} Update failed: ${e.message}`);
242
+ console.log(`${c.tip('[TIP]')} Try running manually: npm update -g claude-wec`);
243
+ }
244
+ }
245
+
246
+ // Start the server
247
+ async function startServer() {
248
+ // Check for updates silently on startup
249
+ checkForUpdates(true);
250
+
251
+ // Import and run the server
252
+ await import('./index.js');
253
+ }
254
+
255
+ // Parse CLI arguments
256
+ function parseArgs(args) {
257
+ const parsed = { command: 'start', options: {} };
258
+
259
+ for (let i = 0; i < args.length; i++) {
260
+ const arg = args[i];
261
+
262
+ if (arg === '--port' || arg === '-p') {
263
+ parsed.options.port = args[++i];
264
+ } else if (arg.startsWith('--port=')) {
265
+ parsed.options.port = arg.split('=')[1];
266
+ } else if (arg === '--database-path') {
267
+ parsed.options.databasePath = args[++i];
268
+ } else if (arg.startsWith('--database-path=')) {
269
+ parsed.options.databasePath = arg.split('=')[1];
270
+ } else if (arg === '--help' || arg === '-h') {
271
+ parsed.command = 'help';
272
+ } else if (arg === '--version' || arg === '-v') {
273
+ parsed.command = 'version';
274
+ } else if (!arg.startsWith('-')) {
275
+ parsed.command = arg;
276
+ }
277
+ }
278
+
279
+ return parsed;
280
+ }
281
+
282
+ // Main CLI handler
283
+ async function main() {
284
+ const args = process.argv.slice(2);
285
+ const { command, options } = parseArgs(args);
286
+
287
+ // Apply CLI options to environment variables
288
+ if (options.port) {
289
+ process.env.PORT = options.port;
290
+ }
291
+ if (options.databasePath) {
292
+ process.env.DATABASE_PATH = options.databasePath;
293
+ }
294
+
295
+ switch (command) {
296
+ case 'start':
297
+ await startServer();
298
+ break;
299
+ case 'status':
300
+ case 'info':
301
+ showStatus();
302
+ break;
303
+ case 'help':
304
+ case '-h':
305
+ case '--help':
306
+ showHelp();
307
+ break;
308
+ case 'version':
309
+ case '-v':
310
+ case '--version':
311
+ showVersion();
312
+ break;
313
+ case 'update':
314
+ await updatePackage();
315
+ break;
316
+ default:
317
+ console.error(`\n❌ Unknown command: ${command}`);
318
+ console.log(' Run "weccli help" for usage information.\n');
319
+ process.exit(1);
320
+ }
321
+ }
322
+
323
+ // Run the CLI
324
+ main().catch(error => {
325
+ console.error('\n❌ Error:', error.message);
326
+ process.exit(1);
327
+ });
@@ -0,0 +1,267 @@
1
+ import { spawn } from 'child_process';
2
+ import crossSpawn from 'cross-spawn';
3
+ import { promises as fs } from 'fs';
4
+ import path from 'path';
5
+ import os from 'os';
6
+
7
+ // Use cross-spawn on Windows for better command execution
8
+ const spawnFunction = process.platform === 'win32' ? crossSpawn : spawn;
9
+
10
+ let activeCursorProcesses = new Map(); // Track active processes by session ID
11
+
12
+ async function spawnCursor(command, options = {}, ws) {
13
+ return new Promise(async (resolve, reject) => {
14
+ const { sessionId, projectPath, cwd, resume, toolsSettings, skipPermissions, model, images } = options;
15
+ let capturedSessionId = sessionId; // Track session ID throughout the process
16
+ let sessionCreatedSent = false; // Track if we've already sent session-created event
17
+ let messageBuffer = ''; // Buffer for accumulating assistant messages
18
+
19
+ // Use tools settings passed from frontend, or defaults
20
+ const settings = toolsSettings || {
21
+ allowedShellCommands: [],
22
+ skipPermissions: false
23
+ };
24
+
25
+ // Build Cursor CLI command
26
+ const args = [];
27
+
28
+ // Build flags allowing both resume and prompt together (reply in existing session)
29
+ // Treat presence of sessionId as intention to resume, regardless of resume flag
30
+ if (sessionId) {
31
+ args.push('--resume=' + sessionId);
32
+ }
33
+
34
+ if (command && command.trim()) {
35
+ // Provide a prompt (works for both new and resumed sessions)
36
+ args.push('-p', command);
37
+
38
+ // Add model flag if specified (only meaningful for new sessions; harmless on resume)
39
+ if (!sessionId && model) {
40
+ args.push('--model', model);
41
+ }
42
+
43
+ // Request streaming JSON when we are providing a prompt
44
+ args.push('--output-format', 'stream-json');
45
+ }
46
+
47
+ // Add skip permissions flag if enabled
48
+ if (skipPermissions || settings.skipPermissions) {
49
+ args.push('-f');
50
+ console.log('⚠️ Using -f flag (skip permissions)');
51
+ }
52
+
53
+ // Use cwd (actual project directory) instead of projectPath
54
+ const workingDir = cwd || projectPath || process.cwd();
55
+
56
+ console.log('Spawning Cursor CLI:', 'cursor-agent', args.join(' '));
57
+ console.log('Working directory:', workingDir);
58
+ console.log('Session info - Input sessionId:', sessionId, 'Resume:', resume);
59
+
60
+ const cursorProcess = spawnFunction('cursor-agent', args, {
61
+ cwd: workingDir,
62
+ stdio: ['pipe', 'pipe', 'pipe'],
63
+ env: { ...process.env } // Inherit all environment variables
64
+ });
65
+
66
+ // Store process reference for potential abort
67
+ const processKey = capturedSessionId || Date.now().toString();
68
+ activeCursorProcesses.set(processKey, cursorProcess);
69
+
70
+ // Handle stdout (streaming JSON responses)
71
+ cursorProcess.stdout.on('data', (data) => {
72
+ const rawOutput = data.toString();
73
+ console.log('📤 Cursor CLI stdout:', rawOutput);
74
+
75
+ const lines = rawOutput.split('\n').filter(line => line.trim());
76
+
77
+ for (const line of lines) {
78
+ try {
79
+ const response = JSON.parse(line);
80
+ console.log('📄 Parsed JSON response:', response);
81
+
82
+ // Handle different message types
83
+ switch (response.type) {
84
+ case 'system':
85
+ if (response.subtype === 'init') {
86
+ // Capture session ID
87
+ if (response.session_id && !capturedSessionId) {
88
+ capturedSessionId = response.session_id;
89
+ console.log('📝 Captured session ID:', capturedSessionId);
90
+
91
+ // Update process key with captured session ID
92
+ if (processKey !== capturedSessionId) {
93
+ activeCursorProcesses.delete(processKey);
94
+ activeCursorProcesses.set(capturedSessionId, cursorProcess);
95
+ }
96
+
97
+ // Set session ID on writer (for API endpoint compatibility)
98
+ if (ws.setSessionId && typeof ws.setSessionId === 'function') {
99
+ ws.setSessionId(capturedSessionId);
100
+ }
101
+
102
+ // Send session-created event only once for new sessions
103
+ if (!sessionId && !sessionCreatedSent) {
104
+ sessionCreatedSent = true;
105
+ ws.send({
106
+ type: 'session-created',
107
+ sessionId: capturedSessionId,
108
+ model: response.model,
109
+ cwd: response.cwd
110
+ });
111
+ }
112
+ }
113
+
114
+ // Send system info to frontend
115
+ ws.send({
116
+ type: 'cursor-system',
117
+ data: response
118
+ });
119
+ }
120
+ break;
121
+
122
+ case 'user':
123
+ // Forward user message
124
+ ws.send({
125
+ type: 'cursor-user',
126
+ data: response
127
+ });
128
+ break;
129
+
130
+ case 'assistant':
131
+ // Accumulate assistant message chunks
132
+ if (response.message && response.message.content && response.message.content.length > 0) {
133
+ const textContent = response.message.content[0].text;
134
+ messageBuffer += textContent;
135
+
136
+ // Send as Claude-compatible format for frontend
137
+ ws.send({
138
+ type: 'claude-response',
139
+ data: {
140
+ type: 'content_block_delta',
141
+ delta: {
142
+ type: 'text_delta',
143
+ text: textContent
144
+ }
145
+ }
146
+ });
147
+ }
148
+ break;
149
+
150
+ case 'result':
151
+ // Session complete
152
+ console.log('Cursor session result:', response);
153
+
154
+ // Send final message if we have buffered content
155
+ if (messageBuffer) {
156
+ ws.send({
157
+ type: 'claude-response',
158
+ data: {
159
+ type: 'content_block_stop'
160
+ }
161
+ });
162
+ }
163
+
164
+ // Send completion event
165
+ ws.send({
166
+ type: 'cursor-result',
167
+ sessionId: capturedSessionId || sessionId,
168
+ data: response,
169
+ success: response.subtype === 'success'
170
+ });
171
+ break;
172
+
173
+ default:
174
+ // Forward any other message types
175
+ ws.send({
176
+ type: 'cursor-response',
177
+ data: response
178
+ });
179
+ }
180
+ } catch (parseError) {
181
+ console.log('📄 Non-JSON response:', line);
182
+ // If not JSON, send as raw text
183
+ ws.send({
184
+ type: 'cursor-output',
185
+ data: line
186
+ });
187
+ }
188
+ }
189
+ });
190
+
191
+ // Handle stderr
192
+ cursorProcess.stderr.on('data', (data) => {
193
+ console.error('Cursor CLI stderr:', data.toString());
194
+ ws.send({
195
+ type: 'cursor-error',
196
+ error: data.toString()
197
+ });
198
+ });
199
+
200
+ // Handle process completion
201
+ cursorProcess.on('close', async (code) => {
202
+ console.log(`Cursor CLI process exited with code ${code}`);
203
+
204
+ // Clean up process reference
205
+ const finalSessionId = capturedSessionId || sessionId || processKey;
206
+ activeCursorProcesses.delete(finalSessionId);
207
+
208
+ ws.send({
209
+ type: 'claude-complete',
210
+ sessionId: finalSessionId,
211
+ exitCode: code,
212
+ isNewSession: !sessionId && !!command // Flag to indicate this was a new session
213
+ });
214
+
215
+ if (code === 0) {
216
+ resolve();
217
+ } else {
218
+ reject(new Error(`Cursor CLI exited with code ${code}`));
219
+ }
220
+ });
221
+
222
+ // Handle process errors
223
+ cursorProcess.on('error', (error) => {
224
+ console.error('Cursor CLI process error:', error);
225
+
226
+ // Clean up process reference on error
227
+ const finalSessionId = capturedSessionId || sessionId || processKey;
228
+ activeCursorProcesses.delete(finalSessionId);
229
+
230
+ ws.send({
231
+ type: 'cursor-error',
232
+ error: error.message
233
+ });
234
+
235
+ reject(error);
236
+ });
237
+
238
+ // Close stdin since Cursor doesn't need interactive input
239
+ cursorProcess.stdin.end();
240
+ });
241
+ }
242
+
243
+ function abortCursorSession(sessionId) {
244
+ const process = activeCursorProcesses.get(sessionId);
245
+ if (process) {
246
+ console.log(`🛑 Aborting Cursor session: ${sessionId}`);
247
+ process.kill('SIGTERM');
248
+ activeCursorProcesses.delete(sessionId);
249
+ return true;
250
+ }
251
+ return false;
252
+ }
253
+
254
+ function isCursorSessionActive(sessionId) {
255
+ return activeCursorProcesses.has(sessionId);
256
+ }
257
+
258
+ function getActiveCursorSessions() {
259
+ return Array.from(activeCursorProcesses.keys());
260
+ }
261
+
262
+ export {
263
+ spawnCursor,
264
+ abortCursorSession,
265
+ isCursorSessionActive,
266
+ getActiveCursorSessions
267
+ };
Binary file