@pixelbyte-software/pixcode 1.30.2 → 1.31.1

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 (209) 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-BtOeB3cE.js +837 -0
  11. package/dist/assets/index-CDpePeIN.css +32 -0
  12. package/dist/assets/vendor-codemirror-CzYAOTxS.js +41 -0
  13. package/dist/clear-cache.html +85 -85
  14. package/dist/convert-icons.md +52 -52
  15. package/dist/favicon.png +0 -0
  16. package/dist/favicon.svg +7 -8
  17. package/dist/generate-icons.js +48 -48
  18. package/dist/icons/codex-white.svg +3 -3
  19. package/dist/icons/codex.svg +3 -3
  20. package/dist/icons/cursor-white.svg +11 -11
  21. package/dist/icons/icon-128x128.png +0 -0
  22. package/dist/icons/icon-128x128.svg +9 -12
  23. package/dist/icons/icon-144x144.png +0 -0
  24. package/dist/icons/icon-144x144.svg +9 -12
  25. package/dist/icons/icon-152x152.png +0 -0
  26. package/dist/icons/icon-152x152.svg +9 -12
  27. package/dist/icons/icon-192x192.png +0 -0
  28. package/dist/icons/icon-192x192.svg +9 -12
  29. package/dist/icons/icon-384x384.png +0 -0
  30. package/dist/icons/icon-384x384.svg +9 -12
  31. package/dist/icons/icon-512x512.png +0 -0
  32. package/dist/icons/icon-512x512.svg +9 -12
  33. package/dist/icons/icon-72x72.png +0 -0
  34. package/dist/icons/icon-72x72.svg +9 -12
  35. package/dist/icons/icon-96x96.png +0 -0
  36. package/dist/icons/icon-96x96.svg +9 -12
  37. package/dist/icons/icon-template.svg +9 -12
  38. package/dist/icons/qwen-ai-icon.png +0 -0
  39. package/dist/index.html +60 -50
  40. package/dist/logo.png +0 -0
  41. package/dist/logo.svg +11 -16
  42. package/dist/manifest.json +60 -60
  43. package/dist/sw.js +124 -124
  44. package/dist-server/server/claude-sdk.js +28 -5
  45. package/dist-server/server/claude-sdk.js.map +1 -1
  46. package/dist-server/server/cli.js +100 -97
  47. package/dist-server/server/cli.js.map +1 -1
  48. package/dist-server/server/daemon/manager.js +33 -33
  49. package/dist-server/server/daemon-manager.js +62 -62
  50. package/dist-server/server/database/db.js +114 -22
  51. package/dist-server/server/database/db.js.map +1 -1
  52. package/dist-server/server/database/schema.js +122 -89
  53. package/dist-server/server/database/schema.js.map +1 -1
  54. package/dist-server/server/gemini-cli.js +6 -1
  55. package/dist-server/server/gemini-cli.js.map +1 -1
  56. package/dist-server/server/index.js +346 -61
  57. package/dist-server/server/index.js.map +1 -1
  58. package/dist-server/server/modules/providers/list/claude/claude-auth.provider.js +29 -2
  59. package/dist-server/server/modules/providers/list/claude/claude-auth.provider.js.map +1 -1
  60. package/dist-server/server/modules/providers/list/codex/codex-auth.provider.js +22 -2
  61. package/dist-server/server/modules/providers/list/codex/codex-auth.provider.js.map +1 -1
  62. package/dist-server/server/modules/providers/list/cursor/cursor-auth.provider.js +2 -2
  63. package/dist-server/server/modules/providers/list/cursor/cursor-auth.provider.js.map +1 -1
  64. package/dist-server/server/modules/providers/list/gemini/gemini-auth.provider.js +14 -2
  65. package/dist-server/server/modules/providers/list/gemini/gemini-auth.provider.js.map +1 -1
  66. package/dist-server/server/modules/providers/list/qwen/qwen-auth.provider.js +132 -0
  67. package/dist-server/server/modules/providers/list/qwen/qwen-auth.provider.js.map +1 -0
  68. package/dist-server/server/modules/providers/list/qwen/qwen-mcp.provider.js +87 -0
  69. package/dist-server/server/modules/providers/list/qwen/qwen-mcp.provider.js.map +1 -0
  70. package/dist-server/server/modules/providers/list/qwen/qwen-sessions.provider.js +201 -0
  71. package/dist-server/server/modules/providers/list/qwen/qwen-sessions.provider.js.map +1 -0
  72. package/dist-server/server/modules/providers/list/qwen/qwen.provider.js +19 -0
  73. package/dist-server/server/modules/providers/list/qwen/qwen.provider.js.map +1 -0
  74. package/dist-server/server/modules/providers/provider.registry.js +2 -0
  75. package/dist-server/server/modules/providers/provider.registry.js.map +1 -1
  76. package/dist-server/server/modules/providers/provider.routes.js +478 -1
  77. package/dist-server/server/modules/providers/provider.routes.js.map +1 -1
  78. package/dist-server/server/modules/providers/shared/provider-configs.js +105 -0
  79. package/dist-server/server/modules/providers/shared/provider-configs.js.map +1 -0
  80. package/dist-server/server/projects.js +197 -6
  81. package/dist-server/server/projects.js.map +1 -1
  82. package/dist-server/server/qwen-code-cli.js +350 -0
  83. package/dist-server/server/qwen-code-cli.js.map +1 -0
  84. package/dist-server/server/qwen-response-handler.js +70 -0
  85. package/dist-server/server/qwen-response-handler.js.map +1 -0
  86. package/dist-server/server/routes/commands.js +25 -25
  87. package/dist-server/server/routes/git.js +17 -17
  88. package/dist-server/server/routes/network.js +116 -0
  89. package/dist-server/server/routes/network.js.map +1 -0
  90. package/dist-server/server/routes/projects.js +166 -1
  91. package/dist-server/server/routes/projects.js.map +1 -1
  92. package/dist-server/server/routes/qwen.js +23 -0
  93. package/dist-server/server/routes/qwen.js.map +1 -0
  94. package/dist-server/server/routes/taskmaster.js +419 -419
  95. package/dist-server/server/routes/telegram.js +119 -0
  96. package/dist-server/server/routes/telegram.js.map +1 -0
  97. package/dist-server/server/services/external-access.js +228 -0
  98. package/dist-server/server/services/external-access.js.map +1 -0
  99. package/dist-server/server/services/install-jobs.js +552 -0
  100. package/dist-server/server/services/install-jobs.js.map +1 -0
  101. package/dist-server/server/services/notification-orchestrator.js +19 -5
  102. package/dist-server/server/services/notification-orchestrator.js.map +1 -1
  103. package/dist-server/server/services/provider-credentials.js +154 -0
  104. package/dist-server/server/services/provider-credentials.js.map +1 -0
  105. package/dist-server/server/services/provider-models.js +218 -0
  106. package/dist-server/server/services/provider-models.js.map +1 -0
  107. package/dist-server/server/services/telegram/bot.js +259 -0
  108. package/dist-server/server/services/telegram/bot.js.map +1 -0
  109. package/dist-server/server/services/telegram/translations.js +160 -0
  110. package/dist-server/server/services/telegram/translations.js.map +1 -0
  111. package/dist-server/server/utils/port-access.js +196 -0
  112. package/dist-server/server/utils/port-access.js.map +1 -0
  113. package/dist-server/shared/modelConstants.js +18 -0
  114. package/dist-server/shared/modelConstants.js.map +1 -1
  115. package/package.json +177 -168
  116. package/scripts/fix-node-pty.js +67 -67
  117. package/server/claude-sdk.js +857 -834
  118. package/server/cli.js +940 -937
  119. package/server/constants/config.js +4 -4
  120. package/server/cursor-cli.js +342 -342
  121. package/server/daemon/manager.js +564 -564
  122. package/server/daemon-manager.js +920 -920
  123. package/server/database/db.js +696 -593
  124. package/server/database/schema.js +138 -102
  125. package/server/gemini-cli.js +475 -469
  126. package/server/gemini-response-handler.js +79 -79
  127. package/server/index.js +2854 -2556
  128. package/server/load-env.js +34 -34
  129. package/server/middleware/auth.js +132 -132
  130. package/server/modules/providers/list/claude/claude-auth.provider.ts +145 -123
  131. package/server/modules/providers/list/claude/claude-mcp.provider.ts +135 -135
  132. package/server/modules/providers/list/claude/claude-sessions.provider.ts +306 -306
  133. package/server/modules/providers/list/claude/claude.provider.ts +15 -15
  134. package/server/modules/providers/list/codex/codex-auth.provider.ts +115 -100
  135. package/server/modules/providers/list/codex/codex-mcp.provider.ts +135 -135
  136. package/server/modules/providers/list/codex/codex-sessions.provider.ts +319 -319
  137. package/server/modules/providers/list/codex/codex.provider.ts +15 -15
  138. package/server/modules/providers/list/cursor/cursor-auth.provider.ts +143 -143
  139. package/server/modules/providers/list/cursor/cursor-mcp.provider.ts +108 -108
  140. package/server/modules/providers/list/cursor/cursor-sessions.provider.ts +421 -421
  141. package/server/modules/providers/list/cursor/cursor.provider.ts +15 -15
  142. package/server/modules/providers/list/gemini/gemini-auth.provider.ts +163 -151
  143. package/server/modules/providers/list/gemini/gemini-mcp.provider.ts +110 -110
  144. package/server/modules/providers/list/gemini/gemini-sessions.provider.ts +227 -227
  145. package/server/modules/providers/list/gemini/gemini.provider.ts +15 -15
  146. package/server/modules/providers/list/qwen/qwen-auth.provider.ts +145 -0
  147. package/server/modules/providers/list/qwen/qwen-mcp.provider.ts +114 -0
  148. package/server/modules/providers/list/qwen/qwen-sessions.provider.ts +218 -0
  149. package/server/modules/providers/list/qwen/qwen.provider.ts +21 -0
  150. package/server/modules/providers/provider.registry.ts +38 -36
  151. package/server/modules/providers/provider.routes.ts +781 -217
  152. package/server/modules/providers/services/mcp.service.ts +94 -94
  153. package/server/modules/providers/services/provider-auth.service.ts +26 -26
  154. package/server/modules/providers/services/sessions.service.ts +45 -45
  155. package/server/modules/providers/shared/base/abstract.provider.ts +20 -20
  156. package/server/modules/providers/shared/mcp/mcp.provider.ts +151 -151
  157. package/server/modules/providers/shared/provider-configs.ts +118 -0
  158. package/server/modules/providers/tests/mcp.test.ts +293 -293
  159. package/server/openai-codex.js +426 -426
  160. package/server/projects.js +2993 -2792
  161. package/server/qwen-code-cli.js +392 -0
  162. package/server/qwen-response-handler.js +73 -0
  163. package/server/routes/agent.js +1245 -1245
  164. package/server/routes/auth.js +134 -134
  165. package/server/routes/codex.js +19 -19
  166. package/server/routes/commands.js +554 -554
  167. package/server/routes/cursor.js +52 -52
  168. package/server/routes/gemini.js +24 -24
  169. package/server/routes/git.js +1488 -1488
  170. package/server/routes/mcp-utils.js +31 -31
  171. package/server/routes/messages.js +61 -61
  172. package/server/routes/network.js +128 -0
  173. package/server/routes/plugins.js +307 -307
  174. package/server/routes/projects.js +795 -627
  175. package/server/routes/qwen.js +27 -0
  176. package/server/routes/settings.js +286 -286
  177. package/server/routes/taskmaster.js +1471 -1471
  178. package/server/routes/telegram.js +125 -0
  179. package/server/routes/user.js +123 -123
  180. package/server/services/external-access.js +240 -0
  181. package/server/services/install-jobs.js +569 -0
  182. package/server/services/notification-orchestrator.js +242 -227
  183. package/server/services/provider-credentials.js +151 -0
  184. package/server/services/provider-models.js +225 -0
  185. package/server/services/telegram/bot.js +280 -0
  186. package/server/services/telegram/translations.js +170 -0
  187. package/server/services/vapid-keys.js +35 -35
  188. package/server/sessionManager.js +225 -225
  189. package/server/shared/interfaces.ts +54 -54
  190. package/server/shared/types.ts +172 -172
  191. package/server/shared/utils.ts +193 -193
  192. package/server/tsconfig.json +36 -36
  193. package/server/utils/colors.js +21 -21
  194. package/server/utils/commandParser.js +303 -303
  195. package/server/utils/frontmatter.js +18 -18
  196. package/server/utils/gitConfig.js +34 -34
  197. package/server/utils/mcp-detector.js +147 -147
  198. package/server/utils/plugin-loader.js +457 -457
  199. package/server/utils/plugin-process-manager.js +184 -184
  200. package/server/utils/port-access.js +209 -0
  201. package/server/utils/runtime-paths.js +37 -37
  202. package/server/utils/taskmaster-websocket.js +128 -128
  203. package/server/utils/url-detection.js +71 -71
  204. package/server/vite-daemon.js +78 -78
  205. package/shared/modelConstants.js +117 -97
  206. package/shared/networkHosts.js +22 -22
  207. package/dist/assets/index-C2c9QNwK.css +0 -32
  208. package/dist/assets/index-DyXDZED-.js +0 -1277
  209. package/dist/assets/vendor-codemirror-NA4v81it.js +0 -41
@@ -1,627 +1,795 @@
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, extractProjectDirectory } 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
+ * Is this `pixcode-project-N` slot already in use? "In use" means the
245
+ * user has sent at least one message under any provider — presence of a
246
+ * session file under ~/.claude/projects/<encoded>/, ~/.codex/sessions/,
247
+ * or ~/.gemini/… is our signal. We keep it best-effort: if we can't
248
+ * probe a provider's session dir (no permissions, path missing), we
249
+ * treat it as "no sessions for this provider" rather than raise.
250
+ *
251
+ * Checking the on-disk workspace dir for files is NOT a reliable signal
252
+ * providers store their history outside the workspace, so a project
253
+ * that has had 20 messages still has an empty folder.
254
+ */
255
+ async function projectHasAnySessions(workspacePath) {
256
+ const home = os.homedir();
257
+ // encodeProjectName strips drive separators (C:\ → -C--…) and dots so
258
+ // `extractProjectDirectory` can round-trip. Using the same encoder as
259
+ // the rest of projects.js keeps us aligned with however Claude's CLI
260
+ // computes its per-project directory name.
261
+ const slug = workspacePath.replace(/[\\/:]/g, '-').replace(/\./g, '-');
262
+
263
+ const probes = [
264
+ // Claude Code: JSONL-per-session files under a per-project subdir.
265
+ path.join(home, '.claude', 'projects', slug),
266
+ // Codex writes session logs under ~/.codex/sessions — they're cross-project
267
+ // so we can't cheaply attribute them to a specific slot; skip.
268
+ // Gemini: same layout as Claude.
269
+ path.join(home, '.gemini', 'projects', slug),
270
+ // Qwen Code (Gemini fork): same layout.
271
+ path.join(home, '.qwen', 'projects', slug),
272
+ ];
273
+
274
+ for (const dir of probes) {
275
+ try {
276
+ const entries = await fs.readdir(dir);
277
+ if (entries.some((name) => name.endsWith('.jsonl') || name.endsWith('.json'))) {
278
+ return true;
279
+ }
280
+ } catch {
281
+ // Missing / unreadable dir just means "no sessions here", not fatal.
282
+ }
283
+ }
284
+ return false;
285
+ }
286
+
287
+ /**
288
+ * GET /api/projects/:projectName/dir-status
289
+ *
290
+ * Lightweight "does the workspace still exist on disk?" check used by
291
+ * the chat composer to detect deleted-directory sessions. We decode the
292
+ * project name back to an absolute path and stat it — a slug alone isn't
293
+ * useful because the user may have deleted the workspace while the
294
+ * session metadata still lives under ~/.<provider>/projects/.
295
+ *
296
+ * Returns `{ exists, path, isDirectory }` so the UI can lock the
297
+ * composer and surface a "directory deleted" warning instead of letting
298
+ * the user fire prompts into a void.
299
+ */
300
+ router.get('/:projectName/dir-status', async (req, res) => {
301
+ const { projectName } = req.params;
302
+ try {
303
+ const actualPath = await extractProjectDirectory(projectName);
304
+ if (!actualPath) {
305
+ return res.json({ exists: false, path: null, isDirectory: false });
306
+ }
307
+ try {
308
+ const stat = await fs.stat(actualPath);
309
+ return res.json({
310
+ exists: true,
311
+ path: actualPath,
312
+ isDirectory: stat.isDirectory(),
313
+ });
314
+ } catch (err) {
315
+ // ENOENT is the typical "user rm -rf'd the workspace" path.
316
+ if (err.code === 'ENOENT') {
317
+ return res.json({ exists: false, path: actualPath, isDirectory: false });
318
+ }
319
+ throw err;
320
+ }
321
+ } catch (error) {
322
+ console.error(`[projects] dir-status ${projectName}:`, error);
323
+ res.status(500).json({ error: error.message || 'Failed to check project directory' });
324
+ }
325
+ });
326
+
327
+ /**
328
+ * POST /api/projects/quick-start
329
+ *
330
+ * Zero-config project creation: **reuses** the first unused
331
+ * `pixcode-project-N` slot if one exists, otherwise creates the next
332
+ * free index. "Unused" = no session files on disk for any provider.
333
+ * Without reuse, clicking "New chat" rapidly stacks up pixcode-project-1
334
+ * through pixcode-project-N and litters the workspace — the UX we want
335
+ * matches ChatGPT's "New chat" which reuses the empty canvas until the
336
+ * user actually commits a message.
337
+ */
338
+ router.post('/quick-start', async (req, res) => {
339
+ try {
340
+ await fs.mkdir(WORKSPACES_BASE, { recursive: true });
341
+
342
+ let entries = [];
343
+ try {
344
+ entries = await fs.readdir(WORKSPACES_BASE, { withFileTypes: true });
345
+ } catch { /* empty is fine */ }
346
+
347
+ // Pixcode-owned slots, sorted by numeric index so reuse is deterministic
348
+ // and picks the lowest idle slot (pixcode-project-1 before -3).
349
+ const existingSlots = entries
350
+ .filter((e) => e.isDirectory() && /^pixcode-project-\d+$/i.test(e.name))
351
+ .map((e) => ({
352
+ name: e.name,
353
+ index: parseInt(e.name.split('-').pop(), 10) || 0,
354
+ }))
355
+ .sort((a, b) => a.index - b.index);
356
+
357
+ // 1. First pass: reuse the lowest-indexed slot that has no sessions.
358
+ for (const slot of existingSlots) {
359
+ const absolutePath = path.join(WORKSPACES_BASE, slot.name);
360
+ const used = await projectHasAnySessions(absolutePath);
361
+ if (!used) {
362
+ let project;
363
+ try {
364
+ project = await addProjectManually(absolutePath);
365
+ } catch (err) {
366
+ // addProjectManually throws when the project is already
367
+ // registered. That's fine — look it up via its encoded name
368
+ // instead of creating a duplicate.
369
+ const msg = err?.message || '';
370
+ if (!/already configured/i.test(msg)) throw err;
371
+ project = {
372
+ name: absolutePath.replace(/[\\/:]/g, '-').replace(/\./g, '-'),
373
+ path: absolutePath,
374
+ fullPath: absolutePath,
375
+ displayName: slot.name,
376
+ isManuallyAdded: true,
377
+ sessions: [],
378
+ cursorSessions: [],
379
+ };
380
+ }
381
+ return res.json({
382
+ success: true,
383
+ project,
384
+ suggestedName: slot.name,
385
+ reused: true,
386
+ });
387
+ }
388
+ }
389
+
390
+ // 2. No idle slot create the next free index above what exists.
391
+ const takenIndices = new Set(existingSlots.map((s) => s.index));
392
+ let nextIndex = 1;
393
+ while (takenIndices.has(nextIndex)) {
394
+ nextIndex += 1;
395
+ if (nextIndex > 9999) {
396
+ return res.status(500).json({ error: 'No free pixcode-project slot (exhausted 1..9999)' });
397
+ }
398
+ }
399
+ const name = `pixcode-project-${nextIndex}`;
400
+ const absolutePath = path.join(WORKSPACES_BASE, name);
401
+ await fs.mkdir(absolutePath, { recursive: true });
402
+ const project = await addProjectManually(absolutePath);
403
+
404
+ res.json({ success: true, project, suggestedName: name, reused: false });
405
+ } catch (error) {
406
+ console.error('[projects] quick-start failed:', error);
407
+ res.status(500).json({ error: error.message || 'Failed to quick-start project' });
408
+ }
409
+ });
410
+
411
+ /**
412
+ * Create a new workspace
413
+ * POST /api/projects/create-workspace
414
+ *
415
+ * Body:
416
+ * - workspaceType: 'existing' | 'new'
417
+ * - path: string (workspace path)
418
+ * - githubUrl?: string (optional, for new workspaces)
419
+ * - githubTokenId?: number (optional, ID of stored token)
420
+ * - newGithubToken?: string (optional, one-time token)
421
+ */
422
+ router.post('/create-workspace', async (req, res) => {
423
+ try {
424
+ const { workspaceType, path: workspacePath, githubUrl, githubTokenId, newGithubToken } = req.body;
425
+
426
+ // Validate required fields
427
+ if (!workspaceType || !workspacePath) {
428
+ return res.status(400).json({ error: 'workspaceType and path are required' });
429
+ }
430
+
431
+ if (!['existing', 'new'].includes(workspaceType)) {
432
+ return res.status(400).json({ error: 'workspaceType must be "existing" or "new"' });
433
+ }
434
+
435
+ // Validate path safety before any operations
436
+ const validation = await validateWorkspacePath(workspacePath);
437
+ if (!validation.valid) {
438
+ return res.status(400).json({
439
+ error: 'Invalid workspace path',
440
+ details: validation.error
441
+ });
442
+ }
443
+
444
+ const absolutePath = validation.resolvedPath;
445
+
446
+ // Handle existing workspace
447
+ if (workspaceType === 'existing') {
448
+ // Check if the path exists
449
+ try {
450
+ await fs.access(absolutePath);
451
+ const stats = await fs.stat(absolutePath);
452
+
453
+ if (!stats.isDirectory()) {
454
+ return res.status(400).json({ error: 'Path exists but is not a directory' });
455
+ }
456
+ } catch (error) {
457
+ if (error.code === 'ENOENT') {
458
+ return res.status(404).json({ error: 'Workspace path does not exist' });
459
+ }
460
+ throw error;
461
+ }
462
+
463
+ // Add the existing workspace to the project list
464
+ const project = await addProjectManually(absolutePath);
465
+
466
+ return res.json({
467
+ success: true,
468
+ project,
469
+ message: 'Existing workspace added successfully'
470
+ });
471
+ }
472
+
473
+ // Handle new workspace creation
474
+ if (workspaceType === 'new') {
475
+ // Create the directory if it doesn't exist
476
+ await fs.mkdir(absolutePath, { recursive: true });
477
+
478
+ // If GitHub URL is provided, clone the repository
479
+ if (githubUrl) {
480
+ let githubToken = null;
481
+
482
+ // Get GitHub token if needed
483
+ if (githubTokenId) {
484
+ // Fetch token from database
485
+ const token = await getGithubTokenById(githubTokenId, req.user.id);
486
+ if (!token) {
487
+ // Clean up created directory
488
+ await fs.rm(absolutePath, { recursive: true, force: true });
489
+ return res.status(404).json({ error: 'GitHub token not found' });
490
+ }
491
+ githubToken = token.github_token;
492
+ } else if (newGithubToken) {
493
+ githubToken = newGithubToken;
494
+ }
495
+
496
+ // Extract repo name from URL for the clone destination
497
+ const normalizedUrl = githubUrl.replace(/\/+$/, '').replace(/\.git$/, '');
498
+ const repoName = normalizedUrl.split('/').pop() || 'repository';
499
+ const clonePath = path.join(absolutePath, repoName);
500
+
501
+ // Check if clone destination already exists to prevent data loss
502
+ try {
503
+ await fs.access(clonePath);
504
+ return res.status(409).json({
505
+ error: 'Directory already exists',
506
+ details: `The destination path "${clonePath}" already exists. Please choose a different location or remove the existing directory.`
507
+ });
508
+ } catch (err) {
509
+ // Directory doesn't exist, which is what we want
510
+ }
511
+
512
+ // Clone the repository into a subfolder
513
+ try {
514
+ await cloneGitHubRepository(githubUrl, clonePath, githubToken);
515
+ } catch (error) {
516
+ // Only clean up if clone created partial data (check if dir exists and is empty or partial)
517
+ try {
518
+ const stats = await fs.stat(clonePath);
519
+ if (stats.isDirectory()) {
520
+ await fs.rm(clonePath, { recursive: true, force: true });
521
+ }
522
+ } catch (cleanupError) {
523
+ // Directory doesn't exist or cleanup failed - ignore
524
+ }
525
+ throw new Error(`Failed to clone repository: ${error.message}`);
526
+ }
527
+
528
+ // Add the cloned repo path to the project list
529
+ const project = await addProjectManually(clonePath);
530
+
531
+ return res.json({
532
+ success: true,
533
+ project,
534
+ message: 'New workspace created and repository cloned successfully'
535
+ });
536
+ }
537
+
538
+ // Add the new workspace to the project list (no clone)
539
+ const project = await addProjectManually(absolutePath);
540
+
541
+ return res.json({
542
+ success: true,
543
+ project,
544
+ message: 'New workspace created successfully'
545
+ });
546
+ }
547
+
548
+ } catch (error) {
549
+ console.error('Error creating workspace:', error);
550
+ res.status(500).json({
551
+ error: error.message || 'Failed to create workspace',
552
+ details: process.env.NODE_ENV === 'development' ? error.stack : undefined
553
+ });
554
+ }
555
+ });
556
+
557
+ /**
558
+ * Helper function to get GitHub token from database
559
+ */
560
+ async function getGithubTokenById(tokenId, userId) {
561
+ const { db } = await import('../database/db.js');
562
+
563
+ const credential = db.prepare(
564
+ 'SELECT * FROM user_credentials WHERE id = ? AND user_id = ? AND credential_type = ? AND is_active = 1'
565
+ ).get(tokenId, userId, 'github_token');
566
+
567
+ // Return in the expected format (github_token field for compatibility)
568
+ if (credential) {
569
+ return {
570
+ ...credential,
571
+ github_token: credential.credential_value
572
+ };
573
+ }
574
+
575
+ return null;
576
+ }
577
+
578
+ /**
579
+ * Clone repository with progress streaming (SSE)
580
+ * GET /api/projects/clone-progress
581
+ */
582
+ router.get('/clone-progress', async (req, res) => {
583
+ const { path: workspacePath, githubUrl, githubTokenId, newGithubToken } = req.query;
584
+
585
+ res.setHeader('Content-Type', 'text/event-stream');
586
+ res.setHeader('Cache-Control', 'no-cache');
587
+ res.setHeader('Connection', 'keep-alive');
588
+ res.flushHeaders();
589
+
590
+ const sendEvent = (type, data) => {
591
+ res.write(`data: ${JSON.stringify({ type, ...data })}\n\n`);
592
+ };
593
+
594
+ try {
595
+ if (!workspacePath || !githubUrl) {
596
+ sendEvent('error', { message: 'workspacePath and githubUrl are required' });
597
+ res.end();
598
+ return;
599
+ }
600
+
601
+ const validation = await validateWorkspacePath(workspacePath);
602
+ if (!validation.valid) {
603
+ sendEvent('error', { message: validation.error });
604
+ res.end();
605
+ return;
606
+ }
607
+
608
+ const absolutePath = validation.resolvedPath;
609
+
610
+ await fs.mkdir(absolutePath, { recursive: true });
611
+
612
+ let githubToken = null;
613
+ if (githubTokenId) {
614
+ const token = await getGithubTokenById(parseInt(githubTokenId), req.user.id);
615
+ if (!token) {
616
+ await fs.rm(absolutePath, { recursive: true, force: true });
617
+ sendEvent('error', { message: 'GitHub token not found' });
618
+ res.end();
619
+ return;
620
+ }
621
+ githubToken = token.github_token;
622
+ } else if (newGithubToken) {
623
+ githubToken = newGithubToken;
624
+ }
625
+
626
+ const normalizedUrl = githubUrl.replace(/\/+$/, '').replace(/\.git$/, '');
627
+ const repoName = normalizedUrl.split('/').pop() || 'repository';
628
+ const clonePath = path.join(absolutePath, repoName);
629
+
630
+ // Check if clone destination already exists to prevent data loss
631
+ try {
632
+ await fs.access(clonePath);
633
+ sendEvent('error', { message: `Directory "${repoName}" already exists. Please choose a different location or remove the existing directory.` });
634
+ res.end();
635
+ return;
636
+ } catch (err) {
637
+ // Directory doesn't exist, which is what we want
638
+ }
639
+
640
+ let cloneUrl = githubUrl;
641
+ if (githubToken) {
642
+ try {
643
+ const url = new URL(githubUrl);
644
+ url.username = githubToken;
645
+ url.password = '';
646
+ cloneUrl = url.toString();
647
+ } catch (error) {
648
+ // SSH URL or invalid - use as-is
649
+ }
650
+ }
651
+
652
+ sendEvent('progress', { message: `Cloning into '${repoName}'...` });
653
+
654
+ const gitProcess = spawn('git', ['clone', '--progress', cloneUrl, clonePath], {
655
+ stdio: ['ignore', 'pipe', 'pipe'],
656
+ env: {
657
+ ...process.env,
658
+ GIT_TERMINAL_PROMPT: '0'
659
+ }
660
+ });
661
+
662
+ let lastError = '';
663
+
664
+ gitProcess.stdout.on('data', (data) => {
665
+ const message = data.toString().trim();
666
+ if (message) {
667
+ sendEvent('progress', { message });
668
+ }
669
+ });
670
+
671
+ gitProcess.stderr.on('data', (data) => {
672
+ const message = data.toString().trim();
673
+ lastError = message;
674
+ if (message) {
675
+ sendEvent('progress', { message });
676
+ }
677
+ });
678
+
679
+ gitProcess.on('close', async (code) => {
680
+ if (code === 0) {
681
+ try {
682
+ const project = await addProjectManually(clonePath);
683
+ sendEvent('complete', { project, message: 'Repository cloned successfully' });
684
+ } catch (error) {
685
+ sendEvent('error', { message: `Clone succeeded but failed to add project: ${error.message}` });
686
+ }
687
+ } else {
688
+ const sanitizedError = sanitizeGitError(lastError, githubToken);
689
+ let errorMessage = 'Git clone failed';
690
+ if (lastError.includes('Authentication failed') || lastError.includes('could not read Username')) {
691
+ errorMessage = 'Authentication failed. Please check your credentials.';
692
+ } else if (lastError.includes('Repository not found')) {
693
+ errorMessage = 'Repository not found. Please check the URL and ensure you have access.';
694
+ } else if (lastError.includes('already exists')) {
695
+ errorMessage = 'Directory already exists';
696
+ } else if (sanitizedError) {
697
+ errorMessage = sanitizedError;
698
+ }
699
+ try {
700
+ await fs.rm(clonePath, { recursive: true, force: true });
701
+ } catch (cleanupError) {
702
+ console.error('Failed to clean up after clone failure:', sanitizeGitError(cleanupError.message, githubToken));
703
+ }
704
+ sendEvent('error', { message: errorMessage });
705
+ }
706
+ res.end();
707
+ });
708
+
709
+ gitProcess.on('error', (error) => {
710
+ if (error.code === 'ENOENT') {
711
+ sendEvent('error', { message: 'Git is not installed or not in PATH' });
712
+ } else {
713
+ sendEvent('error', { message: error.message });
714
+ }
715
+ res.end();
716
+ });
717
+
718
+ req.on('close', () => {
719
+ gitProcess.kill();
720
+ });
721
+
722
+ } catch (error) {
723
+ sendEvent('error', { message: error.message });
724
+ res.end();
725
+ }
726
+ });
727
+
728
+ /**
729
+ * Helper function to clone a GitHub repository
730
+ */
731
+ function cloneGitHubRepository(githubUrl, destinationPath, githubToken = null) {
732
+ return new Promise((resolve, reject) => {
733
+ let cloneUrl = githubUrl;
734
+
735
+ if (githubToken) {
736
+ try {
737
+ const url = new URL(githubUrl);
738
+ url.username = githubToken;
739
+ url.password = '';
740
+ cloneUrl = url.toString();
741
+ } catch (error) {
742
+ // SSH URL - use as-is
743
+ }
744
+ }
745
+
746
+ const gitProcess = spawn('git', ['clone', '--progress', cloneUrl, destinationPath], {
747
+ stdio: ['ignore', 'pipe', 'pipe'],
748
+ env: {
749
+ ...process.env,
750
+ GIT_TERMINAL_PROMPT: '0'
751
+ }
752
+ });
753
+
754
+ let stdout = '';
755
+ let stderr = '';
756
+
757
+ gitProcess.stdout.on('data', (data) => {
758
+ stdout += data.toString();
759
+ });
760
+
761
+ gitProcess.stderr.on('data', (data) => {
762
+ stderr += data.toString();
763
+ });
764
+
765
+ gitProcess.on('close', (code) => {
766
+ if (code === 0) {
767
+ resolve({ stdout, stderr });
768
+ } else {
769
+ let errorMessage = 'Git clone failed';
770
+
771
+ if (stderr.includes('Authentication failed') || stderr.includes('could not read Username')) {
772
+ errorMessage = 'Authentication failed. Please check your GitHub token.';
773
+ } else if (stderr.includes('Repository not found')) {
774
+ errorMessage = 'Repository not found. Please check the URL and ensure you have access.';
775
+ } else if (stderr.includes('already exists')) {
776
+ errorMessage = 'Directory already exists';
777
+ } else if (stderr) {
778
+ errorMessage = stderr;
779
+ }
780
+
781
+ reject(new Error(errorMessage));
782
+ }
783
+ });
784
+
785
+ gitProcess.on('error', (error) => {
786
+ if (error.code === 'ENOENT') {
787
+ reject(new Error('Git is not installed or not in PATH'));
788
+ } else {
789
+ reject(error);
790
+ }
791
+ });
792
+ });
793
+ }
794
+
795
+ export default router;