@pixelbyte-software/pixcode 1.30.1 → 1.31.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 (205) hide show
  1. package/LICENSE +718 -718
  2. package/README.de.md +248 -248
  3. package/README.ja.md +240 -240
  4. package/README.ko.md +240 -240
  5. package/README.md +295 -285
  6. package/README.ru.md +248 -248
  7. package/README.tr.md +250 -250
  8. package/README.zh-CN.md +240 -240
  9. package/dist/api-docs.html +879 -879
  10. package/dist/assets/index-BRRJ47XQ.css +32 -0
  11. package/dist/assets/index-EQohwyiC.js +837 -0
  12. package/dist/clear-cache.html +85 -85
  13. package/dist/convert-icons.md +52 -52
  14. package/dist/favicon.png +0 -0
  15. package/dist/favicon.svg +7 -8
  16. package/dist/generate-icons.js +48 -48
  17. package/dist/icons/codex-white.svg +3 -3
  18. package/dist/icons/codex.svg +3 -3
  19. package/dist/icons/cursor-white.svg +11 -11
  20. package/dist/icons/icon-128x128.png +0 -0
  21. package/dist/icons/icon-128x128.svg +9 -12
  22. package/dist/icons/icon-144x144.png +0 -0
  23. package/dist/icons/icon-144x144.svg +9 -12
  24. package/dist/icons/icon-152x152.png +0 -0
  25. package/dist/icons/icon-152x152.svg +9 -12
  26. package/dist/icons/icon-192x192.png +0 -0
  27. package/dist/icons/icon-192x192.svg +9 -12
  28. package/dist/icons/icon-384x384.png +0 -0
  29. package/dist/icons/icon-384x384.svg +9 -12
  30. package/dist/icons/icon-512x512.png +0 -0
  31. package/dist/icons/icon-512x512.svg +9 -12
  32. package/dist/icons/icon-72x72.png +0 -0
  33. package/dist/icons/icon-72x72.svg +9 -12
  34. package/dist/icons/icon-96x96.png +0 -0
  35. package/dist/icons/icon-96x96.svg +9 -12
  36. package/dist/icons/icon-template.svg +9 -12
  37. package/dist/icons/qwen-ai-icon.png +0 -0
  38. package/dist/index.html +59 -49
  39. package/dist/logo.png +0 -0
  40. package/dist/logo.svg +11 -16
  41. package/dist/manifest.json +60 -60
  42. package/dist/sw.js +124 -124
  43. package/dist-server/server/cli.js +100 -97
  44. package/dist-server/server/cli.js.map +1 -1
  45. package/dist-server/server/daemon/manager.js +33 -33
  46. package/dist-server/server/daemon-manager.js +62 -62
  47. package/dist-server/server/database/db.js +114 -22
  48. package/dist-server/server/database/db.js.map +1 -1
  49. package/dist-server/server/database/schema.js +122 -89
  50. package/dist-server/server/database/schema.js.map +1 -1
  51. package/dist-server/server/gemini-cli.js +6 -1
  52. package/dist-server/server/gemini-cli.js.map +1 -1
  53. package/dist-server/server/index.js +234 -65
  54. package/dist-server/server/index.js.map +1 -1
  55. package/dist-server/server/modules/providers/list/claude/claude-auth.provider.js +29 -2
  56. package/dist-server/server/modules/providers/list/claude/claude-auth.provider.js.map +1 -1
  57. package/dist-server/server/modules/providers/list/codex/codex-auth.provider.js +22 -2
  58. package/dist-server/server/modules/providers/list/codex/codex-auth.provider.js.map +1 -1
  59. package/dist-server/server/modules/providers/list/cursor/cursor-auth.provider.js +2 -2
  60. package/dist-server/server/modules/providers/list/cursor/cursor-auth.provider.js.map +1 -1
  61. package/dist-server/server/modules/providers/list/gemini/gemini-auth.provider.js +14 -2
  62. package/dist-server/server/modules/providers/list/gemini/gemini-auth.provider.js.map +1 -1
  63. package/dist-server/server/modules/providers/list/qwen/qwen-auth.provider.js +132 -0
  64. package/dist-server/server/modules/providers/list/qwen/qwen-auth.provider.js.map +1 -0
  65. package/dist-server/server/modules/providers/list/qwen/qwen-mcp.provider.js +87 -0
  66. package/dist-server/server/modules/providers/list/qwen/qwen-mcp.provider.js.map +1 -0
  67. package/dist-server/server/modules/providers/list/qwen/qwen-sessions.provider.js +201 -0
  68. package/dist-server/server/modules/providers/list/qwen/qwen-sessions.provider.js.map +1 -0
  69. package/dist-server/server/modules/providers/list/qwen/qwen.provider.js +19 -0
  70. package/dist-server/server/modules/providers/list/qwen/qwen.provider.js.map +1 -0
  71. package/dist-server/server/modules/providers/provider.registry.js +2 -0
  72. package/dist-server/server/modules/providers/provider.registry.js.map +1 -1
  73. package/dist-server/server/modules/providers/provider.routes.js +310 -1
  74. package/dist-server/server/modules/providers/provider.routes.js.map +1 -1
  75. package/dist-server/server/projects.js +197 -6
  76. package/dist-server/server/projects.js.map +1 -1
  77. package/dist-server/server/qwen-code-cli.js +350 -0
  78. package/dist-server/server/qwen-code-cli.js.map +1 -0
  79. package/dist-server/server/qwen-response-handler.js +70 -0
  80. package/dist-server/server/qwen-response-handler.js.map +1 -0
  81. package/dist-server/server/routes/commands.js +25 -25
  82. package/dist-server/server/routes/git.js +17 -17
  83. package/dist-server/server/routes/network.js +116 -0
  84. package/dist-server/server/routes/network.js.map +1 -0
  85. package/dist-server/server/routes/projects.js +43 -0
  86. package/dist-server/server/routes/projects.js.map +1 -1
  87. package/dist-server/server/routes/qwen.js +23 -0
  88. package/dist-server/server/routes/qwen.js.map +1 -0
  89. package/dist-server/server/routes/taskmaster.js +419 -419
  90. package/dist-server/server/routes/telegram.js +119 -0
  91. package/dist-server/server/routes/telegram.js.map +1 -0
  92. package/dist-server/server/services/external-access.js +228 -0
  93. package/dist-server/server/services/external-access.js.map +1 -0
  94. package/dist-server/server/services/install-jobs.js +394 -0
  95. package/dist-server/server/services/install-jobs.js.map +1 -0
  96. package/dist-server/server/services/notification-orchestrator.js +19 -5
  97. package/dist-server/server/services/notification-orchestrator.js.map +1 -1
  98. package/dist-server/server/services/provider-credentials.js +154 -0
  99. package/dist-server/server/services/provider-credentials.js.map +1 -0
  100. package/dist-server/server/services/provider-models.js +218 -0
  101. package/dist-server/server/services/provider-models.js.map +1 -0
  102. package/dist-server/server/services/telegram/bot.js +259 -0
  103. package/dist-server/server/services/telegram/bot.js.map +1 -0
  104. package/dist-server/server/services/telegram/translations.js +160 -0
  105. package/dist-server/server/services/telegram/translations.js.map +1 -0
  106. package/dist-server/server/utils/port-access.js +196 -0
  107. package/dist-server/server/utils/port-access.js.map +1 -0
  108. package/dist-server/shared/modelConstants.js +18 -0
  109. package/dist-server/shared/modelConstants.js.map +1 -1
  110. package/package.json +177 -168
  111. package/scripts/fix-node-pty.js +67 -67
  112. package/server/claude-sdk.js +834 -834
  113. package/server/cli.js +940 -937
  114. package/server/constants/config.js +4 -4
  115. package/server/cursor-cli.js +342 -342
  116. package/server/daemon/manager.js +564 -564
  117. package/server/daemon-manager.js +920 -920
  118. package/server/database/db.js +696 -593
  119. package/server/database/schema.js +138 -102
  120. package/server/gemini-cli.js +475 -469
  121. package/server/gemini-response-handler.js +79 -79
  122. package/server/index.js +2730 -2557
  123. package/server/load-env.js +34 -34
  124. package/server/middleware/auth.js +132 -132
  125. package/server/modules/providers/list/claude/claude-auth.provider.ts +145 -123
  126. package/server/modules/providers/list/claude/claude-mcp.provider.ts +135 -135
  127. package/server/modules/providers/list/claude/claude-sessions.provider.ts +306 -306
  128. package/server/modules/providers/list/claude/claude.provider.ts +15 -15
  129. package/server/modules/providers/list/codex/codex-auth.provider.ts +115 -100
  130. package/server/modules/providers/list/codex/codex-mcp.provider.ts +135 -135
  131. package/server/modules/providers/list/codex/codex-sessions.provider.ts +319 -319
  132. package/server/modules/providers/list/codex/codex.provider.ts +15 -15
  133. package/server/modules/providers/list/cursor/cursor-auth.provider.ts +143 -143
  134. package/server/modules/providers/list/cursor/cursor-mcp.provider.ts +108 -108
  135. package/server/modules/providers/list/cursor/cursor-sessions.provider.ts +421 -421
  136. package/server/modules/providers/list/cursor/cursor.provider.ts +15 -15
  137. package/server/modules/providers/list/gemini/gemini-auth.provider.ts +163 -151
  138. package/server/modules/providers/list/gemini/gemini-mcp.provider.ts +110 -110
  139. package/server/modules/providers/list/gemini/gemini-sessions.provider.ts +227 -227
  140. package/server/modules/providers/list/gemini/gemini.provider.ts +15 -15
  141. package/server/modules/providers/list/qwen/qwen-auth.provider.ts +145 -0
  142. package/server/modules/providers/list/qwen/qwen-mcp.provider.ts +114 -0
  143. package/server/modules/providers/list/qwen/qwen-sessions.provider.ts +218 -0
  144. package/server/modules/providers/list/qwen/qwen.provider.ts +21 -0
  145. package/server/modules/providers/provider.registry.ts +38 -36
  146. package/server/modules/providers/provider.routes.ts +583 -217
  147. package/server/modules/providers/services/mcp.service.ts +94 -94
  148. package/server/modules/providers/services/provider-auth.service.ts +26 -26
  149. package/server/modules/providers/services/sessions.service.ts +45 -45
  150. package/server/modules/providers/shared/base/abstract.provider.ts +20 -20
  151. package/server/modules/providers/shared/mcp/mcp.provider.ts +151 -151
  152. package/server/modules/providers/tests/mcp.test.ts +293 -293
  153. package/server/openai-codex.js +426 -426
  154. package/server/projects.js +2993 -2792
  155. package/server/qwen-code-cli.js +392 -0
  156. package/server/qwen-response-handler.js +73 -0
  157. package/server/routes/agent.js +1245 -1245
  158. package/server/routes/auth.js +134 -134
  159. package/server/routes/codex.js +19 -19
  160. package/server/routes/commands.js +554 -554
  161. package/server/routes/cursor.js +52 -52
  162. package/server/routes/gemini.js +24 -24
  163. package/server/routes/git.js +1488 -1488
  164. package/server/routes/mcp-utils.js +31 -31
  165. package/server/routes/messages.js +61 -61
  166. package/server/routes/network.js +128 -0
  167. package/server/routes/plugins.js +307 -307
  168. package/server/routes/projects.js +675 -627
  169. package/server/routes/qwen.js +27 -0
  170. package/server/routes/settings.js +286 -286
  171. package/server/routes/taskmaster.js +1471 -1471
  172. package/server/routes/telegram.js +125 -0
  173. package/server/routes/user.js +123 -123
  174. package/server/services/external-access.js +240 -0
  175. package/server/services/install-jobs.js +410 -0
  176. package/server/services/notification-orchestrator.js +242 -227
  177. package/server/services/provider-credentials.js +151 -0
  178. package/server/services/provider-models.js +225 -0
  179. package/server/services/telegram/bot.js +280 -0
  180. package/server/services/telegram/translations.js +170 -0
  181. package/server/services/vapid-keys.js +35 -35
  182. package/server/sessionManager.js +225 -225
  183. package/server/shared/interfaces.ts +54 -54
  184. package/server/shared/types.ts +172 -172
  185. package/server/shared/utils.ts +193 -193
  186. package/server/tsconfig.json +36 -36
  187. package/server/utils/colors.js +21 -21
  188. package/server/utils/commandParser.js +303 -303
  189. package/server/utils/frontmatter.js +18 -18
  190. package/server/utils/gitConfig.js +34 -34
  191. package/server/utils/mcp-detector.js +147 -147
  192. package/server/utils/plugin-loader.js +457 -457
  193. package/server/utils/plugin-process-manager.js +184 -184
  194. package/server/utils/port-access.js +209 -0
  195. package/server/utils/runtime-paths.js +37 -37
  196. package/server/utils/taskmaster-websocket.js +128 -128
  197. package/server/utils/url-detection.js +71 -71
  198. package/server/vite-daemon.js +78 -78
  199. package/shared/modelConstants.js +117 -97
  200. package/shared/networkHosts.js +22 -22
  201. package/dist/assets/index-C2c9QNwK.css +0 -32
  202. package/dist/assets/index-DyXDZED-.js +0 -1277
  203. package/dist-server/server/routes/cli-auth.js +0 -25
  204. package/dist-server/server/routes/cli-auth.js.map +0 -1
  205. package/server/routes/cli-auth.js +0 -27
@@ -1,627 +1,675 @@
1
- import express from 'express';
2
- import { promises as fs } from 'fs';
3
- import path from 'path';
4
- import { spawn } from 'child_process';
5
- import os from 'os';
6
- import { addProjectManually } from '../projects.js';
7
-
8
- const router = express.Router();
9
-
10
- function sanitizeGitError(message, token) {
11
- if (!message || !token) return message;
12
- return message.replace(new RegExp(token.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'), '***');
13
- }
14
-
15
- // Configure allowed workspace root (defaults to user's home directory)
16
- export const WORKSPACES_ROOT = process.env.WORKSPACES_ROOT || os.homedir();
17
- export const WORKSPACES_BASE = path.resolve(
18
- process.env.WORKSPACES_BASE || path.join(WORKSPACES_ROOT, 'pixcode', 'projects')
19
- );
20
-
21
- // System-critical paths that should never be used as workspace directories
22
- export const FORBIDDEN_PATHS = [
23
- // Unix
24
- '/',
25
- '/etc',
26
- '/bin',
27
- '/sbin',
28
- '/usr',
29
- '/dev',
30
- '/proc',
31
- '/sys',
32
- '/var',
33
- '/boot',
34
- '/root',
35
- '/lib',
36
- '/lib64',
37
- '/opt',
38
- '/tmp',
39
- '/run',
40
- // Windows
41
- 'C:\\Windows',
42
- 'C:\\Program Files',
43
- 'C:\\Program Files (x86)',
44
- 'C:\\ProgramData',
45
- 'C:\\System Volume Information',
46
- 'C:\\$Recycle.Bin'
47
- ];
48
-
49
- const WINDOWS_ABSOLUTE_PATH_PATTERN = /^[A-Za-z]:[\\/]/;
50
-
51
- function isPathWithin(basePath, targetPath) {
52
- const normalizedBase = path.normalize(basePath);
53
- const normalizedTarget = path.normalize(targetPath);
54
- return (
55
- normalizedTarget === normalizedBase ||
56
- normalizedTarget.startsWith(normalizedBase + path.sep)
57
- );
58
- }
59
-
60
- async function realpathOrResolved(targetPath) {
61
- try {
62
- return await fs.realpath(targetPath);
63
- } catch (error) {
64
- if (error.code === 'ENOENT') {
65
- return path.resolve(targetPath);
66
- }
67
- throw error;
68
- }
69
- }
70
-
71
- export function normalizeWorkspacePath(requestedPath) {
72
- if (typeof requestedPath !== 'string') {
73
- return WORKSPACES_BASE;
74
- }
75
-
76
- const trimmedPath = requestedPath.trim();
77
- if (!trimmedPath) {
78
- return WORKSPACES_BASE;
79
- }
80
-
81
- if (trimmedPath === '~') {
82
- return WORKSPACES_BASE;
83
- }
84
-
85
- if (trimmedPath.startsWith('~/') || trimmedPath.startsWith('~\\')) {
86
- return path.join(WORKSPACES_BASE, trimmedPath.slice(2));
87
- }
88
-
89
- const isWindowsAbsolutePath = WINDOWS_ABSOLUTE_PATH_PATTERN.test(trimmedPath);
90
- if (!path.isAbsolute(trimmedPath) && !isWindowsAbsolutePath) {
91
- return path.join(WORKSPACES_BASE, trimmedPath);
92
- }
93
-
94
- return path.resolve(trimmedPath);
95
- }
96
-
97
- /**
98
- * Validates that a path is safe for workspace operations
99
- * @param {string} requestedPath - The path to validate
100
- * @returns {Promise<{valid: boolean, resolvedPath?: string, error?: string}>}
101
- */
102
- export async function validateWorkspacePath(requestedPath) {
103
- try {
104
- if (typeof requestedPath !== 'string' || requestedPath.trim().length === 0) {
105
- return {
106
- valid: false,
107
- error: 'Workspace path is required'
108
- };
109
- }
110
-
111
- // Resolve aliases and relative paths into a safe default base.
112
- // Example: "my-app" -> "<WORKSPACES_BASE>/my-app"
113
- const normalizedInputPath = normalizeWorkspacePath(requestedPath);
114
-
115
- // Resolve to absolute path
116
- let absolutePath = path.resolve(normalizedInputPath);
117
- let resolvedWorkspaceBase = await realpathOrResolved(WORKSPACES_BASE);
118
- resolvedWorkspaceBase = path.normalize(resolvedWorkspaceBase);
119
-
120
- // Check if path is a forbidden system directory
121
- const normalizedPath = path.normalize(absolutePath);
122
- if (FORBIDDEN_PATHS.includes(normalizedPath) || normalizedPath === '/') {
123
- const isRootWorkspaceException =
124
- (normalizedPath === '/root' || normalizedPath.startsWith('/root' + path.sep)) &&
125
- isPathWithin(resolvedWorkspaceBase, normalizedPath);
126
- if (isRootWorkspaceException) {
127
- // Allow /root/<base> carve-out for root installations.
128
- } else {
129
- return {
130
- valid: false,
131
- error: 'Cannot use system-critical directories as workspace locations'
132
- };
133
- }
134
- }
135
-
136
- // Additional check for paths starting with forbidden directories
137
- for (const forbidden of FORBIDDEN_PATHS) {
138
- const isInsideForbidden = normalizedPath === forbidden ||
139
- normalizedPath.startsWith(forbidden + path.sep);
140
-
141
- if (!isInsideForbidden) {
142
- continue;
143
- }
144
-
145
- const isRootWorkspaceException =
146
- (forbidden === '/root') && isPathWithin(resolvedWorkspaceBase, normalizedPath);
147
- if (isRootWorkspaceException) {
148
- continue;
149
- }
150
-
151
- // Exception: /var/tmp and similar user-accessible paths might be allowed
152
- // but /var itself and most /var subdirectories should be blocked
153
- if (forbidden === '/var' &&
154
- (normalizedPath.startsWith('/var/tmp') ||
155
- normalizedPath.startsWith('/var/folders'))) {
156
- continue; // Allow these specific cases
157
- }
158
-
159
- return {
160
- valid: false,
161
- error: `Cannot create workspace in system directory: ${forbidden}`
162
- };
163
- }
164
-
165
- // Try to resolve the real path (following symlinks)
166
- let realPath;
167
- try {
168
- // Check if path exists to resolve real path
169
- await fs.access(absolutePath);
170
- realPath = await fs.realpath(absolutePath);
171
- } catch (error) {
172
- if (error.code === 'ENOENT') {
173
- // Path doesn't exist yet - check parent directory
174
- let parentPath = path.dirname(absolutePath);
175
- try {
176
- const parentRealPath = await fs.realpath(parentPath);
177
-
178
- // Reconstruct the full path with real parent
179
- realPath = path.join(parentRealPath, path.basename(absolutePath));
180
- } catch (parentError) {
181
- if (parentError.code === 'ENOENT') {
182
- // Parent doesn't exist either - use the absolute path as-is
183
- // We'll validate it's within allowed root
184
- realPath = absolutePath;
185
- } else {
186
- throw parentError;
187
- }
188
- }
189
- } else {
190
- throw error;
191
- }
192
- }
193
-
194
- // Resolve the workspace root to its real path
195
- const resolvedWorkspaceRoot = await realpathOrResolved(WORKSPACES_ROOT);
196
-
197
- // Ensure the resolved path is contained within the allowed workspace root
198
- if (!isPathWithin(resolvedWorkspaceRoot, realPath)) {
199
- return {
200
- valid: false,
201
- error: `Workspace path must be within the allowed workspace root: ${WORKSPACES_ROOT}. For new projects, use ${WORKSPACES_BASE}`
202
- };
203
- }
204
-
205
- // Additional symlink check for existing paths
206
- try {
207
- await fs.access(absolutePath);
208
- const stats = await fs.lstat(absolutePath);
209
-
210
- if (stats.isSymbolicLink()) {
211
- // Verify symlink target is also within allowed root
212
- const linkTarget = await fs.readlink(absolutePath);
213
- const resolvedTarget = path.resolve(path.dirname(absolutePath), linkTarget);
214
- const realTarget = await fs.realpath(resolvedTarget);
215
-
216
- if (!isPathWithin(resolvedWorkspaceRoot, realTarget)) {
217
- return {
218
- valid: false,
219
- error: 'Symlink target is outside the allowed workspace root'
220
- };
221
- }
222
- }
223
- } catch (error) {
224
- if (error.code !== 'ENOENT') {
225
- throw error;
226
- }
227
- // Path doesn't exist - that's fine for new workspace creation
228
- }
229
-
230
- return {
231
- valid: true,
232
- resolvedPath: realPath
233
- };
234
-
235
- } catch (error) {
236
- return {
237
- valid: false,
238
- error: `Path validation failed: ${error.message}`
239
- };
240
- }
241
- }
242
-
243
- /**
244
- * Create a new workspace
245
- * POST /api/projects/create-workspace
246
- *
247
- * Body:
248
- * - workspaceType: 'existing' | 'new'
249
- * - path: string (workspace path)
250
- * - githubUrl?: string (optional, for new workspaces)
251
- * - githubTokenId?: number (optional, ID of stored token)
252
- * - newGithubToken?: string (optional, one-time token)
253
- */
254
- router.post('/create-workspace', async (req, res) => {
255
- try {
256
- const { workspaceType, path: workspacePath, githubUrl, githubTokenId, newGithubToken } = req.body;
257
-
258
- // Validate required fields
259
- if (!workspaceType || !workspacePath) {
260
- return res.status(400).json({ error: 'workspaceType and path are required' });
261
- }
262
-
263
- if (!['existing', 'new'].includes(workspaceType)) {
264
- return res.status(400).json({ error: 'workspaceType must be "existing" or "new"' });
265
- }
266
-
267
- // Validate path safety before any operations
268
- const validation = await validateWorkspacePath(workspacePath);
269
- if (!validation.valid) {
270
- return res.status(400).json({
271
- error: 'Invalid workspace path',
272
- details: validation.error
273
- });
274
- }
275
-
276
- const absolutePath = validation.resolvedPath;
277
-
278
- // Handle existing workspace
279
- if (workspaceType === 'existing') {
280
- // Check if the path exists
281
- try {
282
- await fs.access(absolutePath);
283
- const stats = await fs.stat(absolutePath);
284
-
285
- if (!stats.isDirectory()) {
286
- return res.status(400).json({ error: 'Path exists but is not a directory' });
287
- }
288
- } catch (error) {
289
- if (error.code === 'ENOENT') {
290
- return res.status(404).json({ error: 'Workspace path does not exist' });
291
- }
292
- throw error;
293
- }
294
-
295
- // Add the existing workspace to the project list
296
- const project = await addProjectManually(absolutePath);
297
-
298
- return res.json({
299
- success: true,
300
- project,
301
- message: 'Existing workspace added successfully'
302
- });
303
- }
304
-
305
- // Handle new workspace creation
306
- if (workspaceType === 'new') {
307
- // Create the directory if it doesn't exist
308
- await fs.mkdir(absolutePath, { recursive: true });
309
-
310
- // If GitHub URL is provided, clone the repository
311
- if (githubUrl) {
312
- let githubToken = null;
313
-
314
- // Get GitHub token if needed
315
- if (githubTokenId) {
316
- // Fetch token from database
317
- const token = await getGithubTokenById(githubTokenId, req.user.id);
318
- if (!token) {
319
- // Clean up created directory
320
- await fs.rm(absolutePath, { recursive: true, force: true });
321
- return res.status(404).json({ error: 'GitHub token not found' });
322
- }
323
- githubToken = token.github_token;
324
- } else if (newGithubToken) {
325
- githubToken = newGithubToken;
326
- }
327
-
328
- // Extract repo name from URL for the clone destination
329
- const normalizedUrl = githubUrl.replace(/\/+$/, '').replace(/\.git$/, '');
330
- const repoName = normalizedUrl.split('/').pop() || 'repository';
331
- const clonePath = path.join(absolutePath, repoName);
332
-
333
- // Check if clone destination already exists to prevent data loss
334
- try {
335
- await fs.access(clonePath);
336
- return res.status(409).json({
337
- error: 'Directory already exists',
338
- details: `The destination path "${clonePath}" already exists. Please choose a different location or remove the existing directory.`
339
- });
340
- } catch (err) {
341
- // Directory doesn't exist, which is what we want
342
- }
343
-
344
- // Clone the repository into a subfolder
345
- try {
346
- await cloneGitHubRepository(githubUrl, clonePath, githubToken);
347
- } catch (error) {
348
- // Only clean up if clone created partial data (check if dir exists and is empty or partial)
349
- try {
350
- const stats = await fs.stat(clonePath);
351
- if (stats.isDirectory()) {
352
- await fs.rm(clonePath, { recursive: true, force: true });
353
- }
354
- } catch (cleanupError) {
355
- // Directory doesn't exist or cleanup failed - ignore
356
- }
357
- throw new Error(`Failed to clone repository: ${error.message}`);
358
- }
359
-
360
- // Add the cloned repo path to the project list
361
- const project = await addProjectManually(clonePath);
362
-
363
- return res.json({
364
- success: true,
365
- project,
366
- message: 'New workspace created and repository cloned successfully'
367
- });
368
- }
369
-
370
- // Add the new workspace to the project list (no clone)
371
- const project = await addProjectManually(absolutePath);
372
-
373
- return res.json({
374
- success: true,
375
- project,
376
- message: 'New workspace created successfully'
377
- });
378
- }
379
-
380
- } catch (error) {
381
- console.error('Error creating workspace:', error);
382
- res.status(500).json({
383
- error: error.message || 'Failed to create workspace',
384
- details: process.env.NODE_ENV === 'development' ? error.stack : undefined
385
- });
386
- }
387
- });
388
-
389
- /**
390
- * Helper function to get GitHub token from database
391
- */
392
- async function getGithubTokenById(tokenId, userId) {
393
- const { db } = await import('../database/db.js');
394
-
395
- const credential = db.prepare(
396
- 'SELECT * FROM user_credentials WHERE id = ? AND user_id = ? AND credential_type = ? AND is_active = 1'
397
- ).get(tokenId, userId, 'github_token');
398
-
399
- // Return in the expected format (github_token field for compatibility)
400
- if (credential) {
401
- return {
402
- ...credential,
403
- github_token: credential.credential_value
404
- };
405
- }
406
-
407
- return null;
408
- }
409
-
410
- /**
411
- * Clone repository with progress streaming (SSE)
412
- * GET /api/projects/clone-progress
413
- */
414
- router.get('/clone-progress', async (req, res) => {
415
- const { path: workspacePath, githubUrl, githubTokenId, newGithubToken } = req.query;
416
-
417
- res.setHeader('Content-Type', 'text/event-stream');
418
- res.setHeader('Cache-Control', 'no-cache');
419
- res.setHeader('Connection', 'keep-alive');
420
- res.flushHeaders();
421
-
422
- const sendEvent = (type, data) => {
423
- res.write(`data: ${JSON.stringify({ type, ...data })}\n\n`);
424
- };
425
-
426
- try {
427
- if (!workspacePath || !githubUrl) {
428
- sendEvent('error', { message: 'workspacePath and githubUrl are required' });
429
- res.end();
430
- return;
431
- }
432
-
433
- const validation = await validateWorkspacePath(workspacePath);
434
- if (!validation.valid) {
435
- sendEvent('error', { message: validation.error });
436
- res.end();
437
- return;
438
- }
439
-
440
- const absolutePath = validation.resolvedPath;
441
-
442
- await fs.mkdir(absolutePath, { recursive: true });
443
-
444
- let githubToken = null;
445
- if (githubTokenId) {
446
- const token = await getGithubTokenById(parseInt(githubTokenId), req.user.id);
447
- if (!token) {
448
- await fs.rm(absolutePath, { recursive: true, force: true });
449
- sendEvent('error', { message: 'GitHub token not found' });
450
- res.end();
451
- return;
452
- }
453
- githubToken = token.github_token;
454
- } else if (newGithubToken) {
455
- githubToken = newGithubToken;
456
- }
457
-
458
- const normalizedUrl = githubUrl.replace(/\/+$/, '').replace(/\.git$/, '');
459
- const repoName = normalizedUrl.split('/').pop() || 'repository';
460
- const clonePath = path.join(absolutePath, repoName);
461
-
462
- // Check if clone destination already exists to prevent data loss
463
- try {
464
- await fs.access(clonePath);
465
- sendEvent('error', { message: `Directory "${repoName}" already exists. Please choose a different location or remove the existing directory.` });
466
- res.end();
467
- return;
468
- } catch (err) {
469
- // Directory doesn't exist, which is what we want
470
- }
471
-
472
- let cloneUrl = githubUrl;
473
- if (githubToken) {
474
- try {
475
- const url = new URL(githubUrl);
476
- url.username = githubToken;
477
- url.password = '';
478
- cloneUrl = url.toString();
479
- } catch (error) {
480
- // SSH URL or invalid - use as-is
481
- }
482
- }
483
-
484
- sendEvent('progress', { message: `Cloning into '${repoName}'...` });
485
-
486
- const gitProcess = spawn('git', ['clone', '--progress', cloneUrl, clonePath], {
487
- stdio: ['ignore', 'pipe', 'pipe'],
488
- env: {
489
- ...process.env,
490
- GIT_TERMINAL_PROMPT: '0'
491
- }
492
- });
493
-
494
- let lastError = '';
495
-
496
- gitProcess.stdout.on('data', (data) => {
497
- const message = data.toString().trim();
498
- if (message) {
499
- sendEvent('progress', { message });
500
- }
501
- });
502
-
503
- gitProcess.stderr.on('data', (data) => {
504
- const message = data.toString().trim();
505
- lastError = message;
506
- if (message) {
507
- sendEvent('progress', { message });
508
- }
509
- });
510
-
511
- gitProcess.on('close', async (code) => {
512
- if (code === 0) {
513
- try {
514
- const project = await addProjectManually(clonePath);
515
- sendEvent('complete', { project, message: 'Repository cloned successfully' });
516
- } catch (error) {
517
- sendEvent('error', { message: `Clone succeeded but failed to add project: ${error.message}` });
518
- }
519
- } else {
520
- const sanitizedError = sanitizeGitError(lastError, githubToken);
521
- let errorMessage = 'Git clone failed';
522
- if (lastError.includes('Authentication failed') || lastError.includes('could not read Username')) {
523
- errorMessage = 'Authentication failed. Please check your credentials.';
524
- } else if (lastError.includes('Repository not found')) {
525
- errorMessage = 'Repository not found. Please check the URL and ensure you have access.';
526
- } else if (lastError.includes('already exists')) {
527
- errorMessage = 'Directory already exists';
528
- } else if (sanitizedError) {
529
- errorMessage = sanitizedError;
530
- }
531
- try {
532
- await fs.rm(clonePath, { recursive: true, force: true });
533
- } catch (cleanupError) {
534
- console.error('Failed to clean up after clone failure:', sanitizeGitError(cleanupError.message, githubToken));
535
- }
536
- sendEvent('error', { message: errorMessage });
537
- }
538
- res.end();
539
- });
540
-
541
- gitProcess.on('error', (error) => {
542
- if (error.code === 'ENOENT') {
543
- sendEvent('error', { message: 'Git is not installed or not in PATH' });
544
- } else {
545
- sendEvent('error', { message: error.message });
546
- }
547
- res.end();
548
- });
549
-
550
- req.on('close', () => {
551
- gitProcess.kill();
552
- });
553
-
554
- } catch (error) {
555
- sendEvent('error', { message: error.message });
556
- res.end();
557
- }
558
- });
559
-
560
- /**
561
- * Helper function to clone a GitHub repository
562
- */
563
- function cloneGitHubRepository(githubUrl, destinationPath, githubToken = null) {
564
- return new Promise((resolve, reject) => {
565
- let cloneUrl = githubUrl;
566
-
567
- if (githubToken) {
568
- try {
569
- const url = new URL(githubUrl);
570
- url.username = githubToken;
571
- url.password = '';
572
- cloneUrl = url.toString();
573
- } catch (error) {
574
- // SSH URL - use as-is
575
- }
576
- }
577
-
578
- const gitProcess = spawn('git', ['clone', '--progress', cloneUrl, destinationPath], {
579
- stdio: ['ignore', 'pipe', 'pipe'],
580
- env: {
581
- ...process.env,
582
- GIT_TERMINAL_PROMPT: '0'
583
- }
584
- });
585
-
586
- let stdout = '';
587
- let stderr = '';
588
-
589
- gitProcess.stdout.on('data', (data) => {
590
- stdout += data.toString();
591
- });
592
-
593
- gitProcess.stderr.on('data', (data) => {
594
- stderr += data.toString();
595
- });
596
-
597
- gitProcess.on('close', (code) => {
598
- if (code === 0) {
599
- resolve({ stdout, stderr });
600
- } else {
601
- let errorMessage = 'Git clone failed';
602
-
603
- if (stderr.includes('Authentication failed') || stderr.includes('could not read Username')) {
604
- errorMessage = 'Authentication failed. Please check your GitHub token.';
605
- } else if (stderr.includes('Repository not found')) {
606
- errorMessage = 'Repository not found. Please check the URL and ensure you have access.';
607
- } else if (stderr.includes('already exists')) {
608
- errorMessage = 'Directory already exists';
609
- } else if (stderr) {
610
- errorMessage = stderr;
611
- }
612
-
613
- reject(new Error(errorMessage));
614
- }
615
- });
616
-
617
- gitProcess.on('error', (error) => {
618
- if (error.code === 'ENOENT') {
619
- reject(new Error('Git is not installed or not in PATH'));
620
- } else {
621
- reject(error);
622
- }
623
- });
624
- });
625
- }
626
-
627
- export default router;
1
+ import express from 'express';
2
+ import { promises as fs } from 'fs';
3
+ import path from 'path';
4
+ import { spawn } from 'child_process';
5
+ import os from 'os';
6
+ import { addProjectManually } from '../projects.js';
7
+
8
+ const router = express.Router();
9
+
10
+ function sanitizeGitError(message, token) {
11
+ if (!message || !token) return message;
12
+ return message.replace(new RegExp(token.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'), '***');
13
+ }
14
+
15
+ // Configure allowed workspace root (defaults to user's home directory)
16
+ export const WORKSPACES_ROOT = process.env.WORKSPACES_ROOT || os.homedir();
17
+ export const WORKSPACES_BASE = path.resolve(
18
+ process.env.WORKSPACES_BASE || path.join(WORKSPACES_ROOT, 'pixcode', 'projects')
19
+ );
20
+
21
+ // System-critical paths that should never be used as workspace directories
22
+ export const FORBIDDEN_PATHS = [
23
+ // Unix
24
+ '/',
25
+ '/etc',
26
+ '/bin',
27
+ '/sbin',
28
+ '/usr',
29
+ '/dev',
30
+ '/proc',
31
+ '/sys',
32
+ '/var',
33
+ '/boot',
34
+ '/root',
35
+ '/lib',
36
+ '/lib64',
37
+ '/opt',
38
+ '/tmp',
39
+ '/run',
40
+ // Windows
41
+ 'C:\\Windows',
42
+ 'C:\\Program Files',
43
+ 'C:\\Program Files (x86)',
44
+ 'C:\\ProgramData',
45
+ 'C:\\System Volume Information',
46
+ 'C:\\$Recycle.Bin'
47
+ ];
48
+
49
+ const WINDOWS_ABSOLUTE_PATH_PATTERN = /^[A-Za-z]:[\\/]/;
50
+
51
+ function isPathWithin(basePath, targetPath) {
52
+ const normalizedBase = path.normalize(basePath);
53
+ const normalizedTarget = path.normalize(targetPath);
54
+ return (
55
+ normalizedTarget === normalizedBase ||
56
+ normalizedTarget.startsWith(normalizedBase + path.sep)
57
+ );
58
+ }
59
+
60
+ async function realpathOrResolved(targetPath) {
61
+ try {
62
+ return await fs.realpath(targetPath);
63
+ } catch (error) {
64
+ if (error.code === 'ENOENT') {
65
+ return path.resolve(targetPath);
66
+ }
67
+ throw error;
68
+ }
69
+ }
70
+
71
+ export function normalizeWorkspacePath(requestedPath) {
72
+ if (typeof requestedPath !== 'string') {
73
+ return WORKSPACES_BASE;
74
+ }
75
+
76
+ const trimmedPath = requestedPath.trim();
77
+ if (!trimmedPath) {
78
+ return WORKSPACES_BASE;
79
+ }
80
+
81
+ if (trimmedPath === '~') {
82
+ return WORKSPACES_BASE;
83
+ }
84
+
85
+ if (trimmedPath.startsWith('~/') || trimmedPath.startsWith('~\\')) {
86
+ return path.join(WORKSPACES_BASE, trimmedPath.slice(2));
87
+ }
88
+
89
+ const isWindowsAbsolutePath = WINDOWS_ABSOLUTE_PATH_PATTERN.test(trimmedPath);
90
+ if (!path.isAbsolute(trimmedPath) && !isWindowsAbsolutePath) {
91
+ return path.join(WORKSPACES_BASE, trimmedPath);
92
+ }
93
+
94
+ return path.resolve(trimmedPath);
95
+ }
96
+
97
+ /**
98
+ * Validates that a path is safe for workspace operations
99
+ * @param {string} requestedPath - The path to validate
100
+ * @returns {Promise<{valid: boolean, resolvedPath?: string, error?: string}>}
101
+ */
102
+ export async function validateWorkspacePath(requestedPath) {
103
+ try {
104
+ if (typeof requestedPath !== 'string' || requestedPath.trim().length === 0) {
105
+ return {
106
+ valid: false,
107
+ error: 'Workspace path is required'
108
+ };
109
+ }
110
+
111
+ // Resolve aliases and relative paths into a safe default base.
112
+ // Example: "my-app" -> "<WORKSPACES_BASE>/my-app"
113
+ const normalizedInputPath = normalizeWorkspacePath(requestedPath);
114
+
115
+ // Resolve to absolute path
116
+ let absolutePath = path.resolve(normalizedInputPath);
117
+ let resolvedWorkspaceBase = await realpathOrResolved(WORKSPACES_BASE);
118
+ resolvedWorkspaceBase = path.normalize(resolvedWorkspaceBase);
119
+
120
+ // Check if path is a forbidden system directory
121
+ const normalizedPath = path.normalize(absolutePath);
122
+ if (FORBIDDEN_PATHS.includes(normalizedPath) || normalizedPath === '/') {
123
+ const isRootWorkspaceException =
124
+ (normalizedPath === '/root' || normalizedPath.startsWith('/root' + path.sep)) &&
125
+ isPathWithin(resolvedWorkspaceBase, normalizedPath);
126
+ if (isRootWorkspaceException) {
127
+ // Allow /root/<base> carve-out for root installations.
128
+ } else {
129
+ return {
130
+ valid: false,
131
+ error: 'Cannot use system-critical directories as workspace locations'
132
+ };
133
+ }
134
+ }
135
+
136
+ // Additional check for paths starting with forbidden directories
137
+ for (const forbidden of FORBIDDEN_PATHS) {
138
+ const isInsideForbidden = normalizedPath === forbidden ||
139
+ normalizedPath.startsWith(forbidden + path.sep);
140
+
141
+ if (!isInsideForbidden) {
142
+ continue;
143
+ }
144
+
145
+ const isRootWorkspaceException =
146
+ (forbidden === '/root') && isPathWithin(resolvedWorkspaceBase, normalizedPath);
147
+ if (isRootWorkspaceException) {
148
+ continue;
149
+ }
150
+
151
+ // Exception: /var/tmp and similar user-accessible paths might be allowed
152
+ // but /var itself and most /var subdirectories should be blocked
153
+ if (forbidden === '/var' &&
154
+ (normalizedPath.startsWith('/var/tmp') ||
155
+ normalizedPath.startsWith('/var/folders'))) {
156
+ continue; // Allow these specific cases
157
+ }
158
+
159
+ return {
160
+ valid: false,
161
+ error: `Cannot create workspace in system directory: ${forbidden}`
162
+ };
163
+ }
164
+
165
+ // Try to resolve the real path (following symlinks)
166
+ let realPath;
167
+ try {
168
+ // Check if path exists to resolve real path
169
+ await fs.access(absolutePath);
170
+ realPath = await fs.realpath(absolutePath);
171
+ } catch (error) {
172
+ if (error.code === 'ENOENT') {
173
+ // Path doesn't exist yet - check parent directory
174
+ let parentPath = path.dirname(absolutePath);
175
+ try {
176
+ const parentRealPath = await fs.realpath(parentPath);
177
+
178
+ // Reconstruct the full path with real parent
179
+ realPath = path.join(parentRealPath, path.basename(absolutePath));
180
+ } catch (parentError) {
181
+ if (parentError.code === 'ENOENT') {
182
+ // Parent doesn't exist either - use the absolute path as-is
183
+ // We'll validate it's within allowed root
184
+ realPath = absolutePath;
185
+ } else {
186
+ throw parentError;
187
+ }
188
+ }
189
+ } else {
190
+ throw error;
191
+ }
192
+ }
193
+
194
+ // Resolve the workspace root to its real path
195
+ const resolvedWorkspaceRoot = await realpathOrResolved(WORKSPACES_ROOT);
196
+
197
+ // Ensure the resolved path is contained within the allowed workspace root
198
+ if (!isPathWithin(resolvedWorkspaceRoot, realPath)) {
199
+ return {
200
+ valid: false,
201
+ error: `Workspace path must be within the allowed workspace root: ${WORKSPACES_ROOT}. For new projects, use ${WORKSPACES_BASE}`
202
+ };
203
+ }
204
+
205
+ // Additional symlink check for existing paths
206
+ try {
207
+ await fs.access(absolutePath);
208
+ const stats = await fs.lstat(absolutePath);
209
+
210
+ if (stats.isSymbolicLink()) {
211
+ // Verify symlink target is also within allowed root
212
+ const linkTarget = await fs.readlink(absolutePath);
213
+ const resolvedTarget = path.resolve(path.dirname(absolutePath), linkTarget);
214
+ const realTarget = await fs.realpath(resolvedTarget);
215
+
216
+ if (!isPathWithin(resolvedWorkspaceRoot, realTarget)) {
217
+ return {
218
+ valid: false,
219
+ error: 'Symlink target is outside the allowed workspace root'
220
+ };
221
+ }
222
+ }
223
+ } catch (error) {
224
+ if (error.code !== 'ENOENT') {
225
+ throw error;
226
+ }
227
+ // Path doesn't exist - that's fine for new workspace creation
228
+ }
229
+
230
+ return {
231
+ valid: true,
232
+ resolvedPath: realPath
233
+ };
234
+
235
+ } catch (error) {
236
+ return {
237
+ valid: false,
238
+ error: `Path validation failed: ${error.message}`
239
+ };
240
+ }
241
+ }
242
+
243
+ /**
244
+ * POST /api/projects/quick-start
245
+ *
246
+ * Zero-config project creation: picks the next available
247
+ * `pixcode-project-N` slot under WORKSPACES_BASE, creates the directory,
248
+ * registers it, and returns the project record. Used by the "start
249
+ * chatting without setting up a project first" landing flow — we want
250
+ * the user to type + send a message and have a real workspace appear
251
+ * underneath them without prompting for a name or path up front.
252
+ */
253
+ router.post('/quick-start', async (req, res) => {
254
+ try {
255
+ await fs.mkdir(WORKSPACES_BASE, { recursive: true });
256
+
257
+ let entries = [];
258
+ try {
259
+ entries = await fs.readdir(WORKSPACES_BASE, { withFileTypes: true });
260
+ } catch { /* empty is fine */ }
261
+ const taken = new Set(
262
+ entries.filter((e) => e.isDirectory()).map((e) => e.name.toLowerCase()),
263
+ );
264
+
265
+ let nextIndex = 1;
266
+ let name = '';
267
+ // eslint-disable-next-line no-constant-condition
268
+ while (true) {
269
+ const candidate = `pixcode-project-${nextIndex}`;
270
+ if (!taken.has(candidate.toLowerCase())) {
271
+ name = candidate;
272
+ break;
273
+ }
274
+ nextIndex += 1;
275
+ if (nextIndex > 9999) {
276
+ return res.status(500).json({ error: 'No free pixcode-project slot (exhausted 1..9999)' });
277
+ }
278
+ }
279
+
280
+ const absolutePath = path.join(WORKSPACES_BASE, name);
281
+ await fs.mkdir(absolutePath, { recursive: true });
282
+ const project = await addProjectManually(absolutePath);
283
+
284
+ res.json({ success: true, project, suggestedName: name });
285
+ } catch (error) {
286
+ console.error('[projects] quick-start failed:', error);
287
+ res.status(500).json({ error: error.message || 'Failed to quick-start project' });
288
+ }
289
+ });
290
+
291
+ /**
292
+ * Create a new workspace
293
+ * POST /api/projects/create-workspace
294
+ *
295
+ * Body:
296
+ * - workspaceType: 'existing' | 'new'
297
+ * - path: string (workspace path)
298
+ * - githubUrl?: string (optional, for new workspaces)
299
+ * - githubTokenId?: number (optional, ID of stored token)
300
+ * - newGithubToken?: string (optional, one-time token)
301
+ */
302
+ router.post('/create-workspace', async (req, res) => {
303
+ try {
304
+ const { workspaceType, path: workspacePath, githubUrl, githubTokenId, newGithubToken } = req.body;
305
+
306
+ // Validate required fields
307
+ if (!workspaceType || !workspacePath) {
308
+ return res.status(400).json({ error: 'workspaceType and path are required' });
309
+ }
310
+
311
+ if (!['existing', 'new'].includes(workspaceType)) {
312
+ return res.status(400).json({ error: 'workspaceType must be "existing" or "new"' });
313
+ }
314
+
315
+ // Validate path safety before any operations
316
+ const validation = await validateWorkspacePath(workspacePath);
317
+ if (!validation.valid) {
318
+ return res.status(400).json({
319
+ error: 'Invalid workspace path',
320
+ details: validation.error
321
+ });
322
+ }
323
+
324
+ const absolutePath = validation.resolvedPath;
325
+
326
+ // Handle existing workspace
327
+ if (workspaceType === 'existing') {
328
+ // Check if the path exists
329
+ try {
330
+ await fs.access(absolutePath);
331
+ const stats = await fs.stat(absolutePath);
332
+
333
+ if (!stats.isDirectory()) {
334
+ return res.status(400).json({ error: 'Path exists but is not a directory' });
335
+ }
336
+ } catch (error) {
337
+ if (error.code === 'ENOENT') {
338
+ return res.status(404).json({ error: 'Workspace path does not exist' });
339
+ }
340
+ throw error;
341
+ }
342
+
343
+ // Add the existing workspace to the project list
344
+ const project = await addProjectManually(absolutePath);
345
+
346
+ return res.json({
347
+ success: true,
348
+ project,
349
+ message: 'Existing workspace added successfully'
350
+ });
351
+ }
352
+
353
+ // Handle new workspace creation
354
+ if (workspaceType === 'new') {
355
+ // Create the directory if it doesn't exist
356
+ await fs.mkdir(absolutePath, { recursive: true });
357
+
358
+ // If GitHub URL is provided, clone the repository
359
+ if (githubUrl) {
360
+ let githubToken = null;
361
+
362
+ // Get GitHub token if needed
363
+ if (githubTokenId) {
364
+ // Fetch token from database
365
+ const token = await getGithubTokenById(githubTokenId, req.user.id);
366
+ if (!token) {
367
+ // Clean up created directory
368
+ await fs.rm(absolutePath, { recursive: true, force: true });
369
+ return res.status(404).json({ error: 'GitHub token not found' });
370
+ }
371
+ githubToken = token.github_token;
372
+ } else if (newGithubToken) {
373
+ githubToken = newGithubToken;
374
+ }
375
+
376
+ // Extract repo name from URL for the clone destination
377
+ const normalizedUrl = githubUrl.replace(/\/+$/, '').replace(/\.git$/, '');
378
+ const repoName = normalizedUrl.split('/').pop() || 'repository';
379
+ const clonePath = path.join(absolutePath, repoName);
380
+
381
+ // Check if clone destination already exists to prevent data loss
382
+ try {
383
+ await fs.access(clonePath);
384
+ return res.status(409).json({
385
+ error: 'Directory already exists',
386
+ details: `The destination path "${clonePath}" already exists. Please choose a different location or remove the existing directory.`
387
+ });
388
+ } catch (err) {
389
+ // Directory doesn't exist, which is what we want
390
+ }
391
+
392
+ // Clone the repository into a subfolder
393
+ try {
394
+ await cloneGitHubRepository(githubUrl, clonePath, githubToken);
395
+ } catch (error) {
396
+ // Only clean up if clone created partial data (check if dir exists and is empty or partial)
397
+ try {
398
+ const stats = await fs.stat(clonePath);
399
+ if (stats.isDirectory()) {
400
+ await fs.rm(clonePath, { recursive: true, force: true });
401
+ }
402
+ } catch (cleanupError) {
403
+ // Directory doesn't exist or cleanup failed - ignore
404
+ }
405
+ throw new Error(`Failed to clone repository: ${error.message}`);
406
+ }
407
+
408
+ // Add the cloned repo path to the project list
409
+ const project = await addProjectManually(clonePath);
410
+
411
+ return res.json({
412
+ success: true,
413
+ project,
414
+ message: 'New workspace created and repository cloned successfully'
415
+ });
416
+ }
417
+
418
+ // Add the new workspace to the project list (no clone)
419
+ const project = await addProjectManually(absolutePath);
420
+
421
+ return res.json({
422
+ success: true,
423
+ project,
424
+ message: 'New workspace created successfully'
425
+ });
426
+ }
427
+
428
+ } catch (error) {
429
+ console.error('Error creating workspace:', error);
430
+ res.status(500).json({
431
+ error: error.message || 'Failed to create workspace',
432
+ details: process.env.NODE_ENV === 'development' ? error.stack : undefined
433
+ });
434
+ }
435
+ });
436
+
437
+ /**
438
+ * Helper function to get GitHub token from database
439
+ */
440
+ async function getGithubTokenById(tokenId, userId) {
441
+ const { db } = await import('../database/db.js');
442
+
443
+ const credential = db.prepare(
444
+ 'SELECT * FROM user_credentials WHERE id = ? AND user_id = ? AND credential_type = ? AND is_active = 1'
445
+ ).get(tokenId, userId, 'github_token');
446
+
447
+ // Return in the expected format (github_token field for compatibility)
448
+ if (credential) {
449
+ return {
450
+ ...credential,
451
+ github_token: credential.credential_value
452
+ };
453
+ }
454
+
455
+ return null;
456
+ }
457
+
458
+ /**
459
+ * Clone repository with progress streaming (SSE)
460
+ * GET /api/projects/clone-progress
461
+ */
462
+ router.get('/clone-progress', async (req, res) => {
463
+ const { path: workspacePath, githubUrl, githubTokenId, newGithubToken } = req.query;
464
+
465
+ res.setHeader('Content-Type', 'text/event-stream');
466
+ res.setHeader('Cache-Control', 'no-cache');
467
+ res.setHeader('Connection', 'keep-alive');
468
+ res.flushHeaders();
469
+
470
+ const sendEvent = (type, data) => {
471
+ res.write(`data: ${JSON.stringify({ type, ...data })}\n\n`);
472
+ };
473
+
474
+ try {
475
+ if (!workspacePath || !githubUrl) {
476
+ sendEvent('error', { message: 'workspacePath and githubUrl are required' });
477
+ res.end();
478
+ return;
479
+ }
480
+
481
+ const validation = await validateWorkspacePath(workspacePath);
482
+ if (!validation.valid) {
483
+ sendEvent('error', { message: validation.error });
484
+ res.end();
485
+ return;
486
+ }
487
+
488
+ const absolutePath = validation.resolvedPath;
489
+
490
+ await fs.mkdir(absolutePath, { recursive: true });
491
+
492
+ let githubToken = null;
493
+ if (githubTokenId) {
494
+ const token = await getGithubTokenById(parseInt(githubTokenId), req.user.id);
495
+ if (!token) {
496
+ await fs.rm(absolutePath, { recursive: true, force: true });
497
+ sendEvent('error', { message: 'GitHub token not found' });
498
+ res.end();
499
+ return;
500
+ }
501
+ githubToken = token.github_token;
502
+ } else if (newGithubToken) {
503
+ githubToken = newGithubToken;
504
+ }
505
+
506
+ const normalizedUrl = githubUrl.replace(/\/+$/, '').replace(/\.git$/, '');
507
+ const repoName = normalizedUrl.split('/').pop() || 'repository';
508
+ const clonePath = path.join(absolutePath, repoName);
509
+
510
+ // Check if clone destination already exists to prevent data loss
511
+ try {
512
+ await fs.access(clonePath);
513
+ sendEvent('error', { message: `Directory "${repoName}" already exists. Please choose a different location or remove the existing directory.` });
514
+ res.end();
515
+ return;
516
+ } catch (err) {
517
+ // Directory doesn't exist, which is what we want
518
+ }
519
+
520
+ let cloneUrl = githubUrl;
521
+ if (githubToken) {
522
+ try {
523
+ const url = new URL(githubUrl);
524
+ url.username = githubToken;
525
+ url.password = '';
526
+ cloneUrl = url.toString();
527
+ } catch (error) {
528
+ // SSH URL or invalid - use as-is
529
+ }
530
+ }
531
+
532
+ sendEvent('progress', { message: `Cloning into '${repoName}'...` });
533
+
534
+ const gitProcess = spawn('git', ['clone', '--progress', cloneUrl, clonePath], {
535
+ stdio: ['ignore', 'pipe', 'pipe'],
536
+ env: {
537
+ ...process.env,
538
+ GIT_TERMINAL_PROMPT: '0'
539
+ }
540
+ });
541
+
542
+ let lastError = '';
543
+
544
+ gitProcess.stdout.on('data', (data) => {
545
+ const message = data.toString().trim();
546
+ if (message) {
547
+ sendEvent('progress', { message });
548
+ }
549
+ });
550
+
551
+ gitProcess.stderr.on('data', (data) => {
552
+ const message = data.toString().trim();
553
+ lastError = message;
554
+ if (message) {
555
+ sendEvent('progress', { message });
556
+ }
557
+ });
558
+
559
+ gitProcess.on('close', async (code) => {
560
+ if (code === 0) {
561
+ try {
562
+ const project = await addProjectManually(clonePath);
563
+ sendEvent('complete', { project, message: 'Repository cloned successfully' });
564
+ } catch (error) {
565
+ sendEvent('error', { message: `Clone succeeded but failed to add project: ${error.message}` });
566
+ }
567
+ } else {
568
+ const sanitizedError = sanitizeGitError(lastError, githubToken);
569
+ let errorMessage = 'Git clone failed';
570
+ if (lastError.includes('Authentication failed') || lastError.includes('could not read Username')) {
571
+ errorMessage = 'Authentication failed. Please check your credentials.';
572
+ } else if (lastError.includes('Repository not found')) {
573
+ errorMessage = 'Repository not found. Please check the URL and ensure you have access.';
574
+ } else if (lastError.includes('already exists')) {
575
+ errorMessage = 'Directory already exists';
576
+ } else if (sanitizedError) {
577
+ errorMessage = sanitizedError;
578
+ }
579
+ try {
580
+ await fs.rm(clonePath, { recursive: true, force: true });
581
+ } catch (cleanupError) {
582
+ console.error('Failed to clean up after clone failure:', sanitizeGitError(cleanupError.message, githubToken));
583
+ }
584
+ sendEvent('error', { message: errorMessage });
585
+ }
586
+ res.end();
587
+ });
588
+
589
+ gitProcess.on('error', (error) => {
590
+ if (error.code === 'ENOENT') {
591
+ sendEvent('error', { message: 'Git is not installed or not in PATH' });
592
+ } else {
593
+ sendEvent('error', { message: error.message });
594
+ }
595
+ res.end();
596
+ });
597
+
598
+ req.on('close', () => {
599
+ gitProcess.kill();
600
+ });
601
+
602
+ } catch (error) {
603
+ sendEvent('error', { message: error.message });
604
+ res.end();
605
+ }
606
+ });
607
+
608
+ /**
609
+ * Helper function to clone a GitHub repository
610
+ */
611
+ function cloneGitHubRepository(githubUrl, destinationPath, githubToken = null) {
612
+ return new Promise((resolve, reject) => {
613
+ let cloneUrl = githubUrl;
614
+
615
+ if (githubToken) {
616
+ try {
617
+ const url = new URL(githubUrl);
618
+ url.username = githubToken;
619
+ url.password = '';
620
+ cloneUrl = url.toString();
621
+ } catch (error) {
622
+ // SSH URL - use as-is
623
+ }
624
+ }
625
+
626
+ const gitProcess = spawn('git', ['clone', '--progress', cloneUrl, destinationPath], {
627
+ stdio: ['ignore', 'pipe', 'pipe'],
628
+ env: {
629
+ ...process.env,
630
+ GIT_TERMINAL_PROMPT: '0'
631
+ }
632
+ });
633
+
634
+ let stdout = '';
635
+ let stderr = '';
636
+
637
+ gitProcess.stdout.on('data', (data) => {
638
+ stdout += data.toString();
639
+ });
640
+
641
+ gitProcess.stderr.on('data', (data) => {
642
+ stderr += data.toString();
643
+ });
644
+
645
+ gitProcess.on('close', (code) => {
646
+ if (code === 0) {
647
+ resolve({ stdout, stderr });
648
+ } else {
649
+ let errorMessage = 'Git clone failed';
650
+
651
+ if (stderr.includes('Authentication failed') || stderr.includes('could not read Username')) {
652
+ errorMessage = 'Authentication failed. Please check your GitHub token.';
653
+ } else if (stderr.includes('Repository not found')) {
654
+ errorMessage = 'Repository not found. Please check the URL and ensure you have access.';
655
+ } else if (stderr.includes('already exists')) {
656
+ errorMessage = 'Directory already exists';
657
+ } else if (stderr) {
658
+ errorMessage = stderr;
659
+ }
660
+
661
+ reject(new Error(errorMessage));
662
+ }
663
+ });
664
+
665
+ gitProcess.on('error', (error) => {
666
+ if (error.code === 'ENOENT') {
667
+ reject(new Error('Git is not installed or not in PATH'));
668
+ } else {
669
+ reject(error);
670
+ }
671
+ });
672
+ });
673
+ }
674
+
675
+ export default router;