agentstudio 0.1.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 (115) hide show
  1. package/.env +15 -0
  2. package/README.md +85 -0
  3. package/dist/bin/agentstudio.d.ts +3 -0
  4. package/dist/bin/agentstudio.d.ts.map +1 -0
  5. package/dist/bin/agentstudio.js +141 -0
  6. package/dist/bin/agentstudio.js.map +1 -0
  7. package/dist/index.d.ts +2 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +87 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/middleware/auth.d.ts +7 -0
  12. package/dist/middleware/auth.d.ts.map +1 -0
  13. package/dist/middleware/auth.js +21 -0
  14. package/dist/middleware/auth.js.map +1 -0
  15. package/dist/routes/agents.d.ts +4 -0
  16. package/dist/routes/agents.d.ts.map +1 -0
  17. package/dist/routes/agents.js +804 -0
  18. package/dist/routes/agents.js.map +1 -0
  19. package/dist/routes/auth.d.ts +4 -0
  20. package/dist/routes/auth.d.ts.map +1 -0
  21. package/dist/routes/auth.js +60 -0
  22. package/dist/routes/auth.js.map +1 -0
  23. package/dist/routes/files.d.ts +4 -0
  24. package/dist/routes/files.d.ts.map +1 -0
  25. package/dist/routes/files.js +301 -0
  26. package/dist/routes/files.js.map +1 -0
  27. package/dist/routes/mcp.d.ts +4 -0
  28. package/dist/routes/mcp.d.ts.map +1 -0
  29. package/dist/routes/mcp.js +652 -0
  30. package/dist/routes/mcp.js.map +1 -0
  31. package/dist/routes/media.d.ts +5 -0
  32. package/dist/routes/media.d.ts.map +1 -0
  33. package/dist/routes/media.js +117 -0
  34. package/dist/routes/media.js.map +1 -0
  35. package/dist/routes/slides.d.ts +4 -0
  36. package/dist/routes/slides.d.ts.map +1 -0
  37. package/dist/routes/slides.js +146 -0
  38. package/dist/routes/slides.js.map +1 -0
  39. package/dist/services/claudeSession.d.ts +83 -0
  40. package/dist/services/claudeSession.d.ts.map +1 -0
  41. package/dist/services/claudeSession.js +255 -0
  42. package/dist/services/claudeSession.js.map +1 -0
  43. package/dist/services/messageQueue.d.ts +31 -0
  44. package/dist/services/messageQueue.d.ts.map +1 -0
  45. package/dist/services/messageQueue.js +67 -0
  46. package/dist/services/messageQueue.js.map +1 -0
  47. package/dist/services/sessionManager.d.ts +132 -0
  48. package/dist/services/sessionManager.d.ts.map +1 -0
  49. package/dist/services/sessionManager.js +439 -0
  50. package/dist/services/sessionManager.js.map +1 -0
  51. package/dist/types/claude-history.d.ts +48 -0
  52. package/dist/types/claude-history.d.ts.map +1 -0
  53. package/dist/types/claude-history.js +2 -0
  54. package/dist/types/claude-history.js.map +1 -0
  55. package/dist/types/claude-versions.d.ts +31 -0
  56. package/dist/types/claude-versions.d.ts.map +1 -0
  57. package/dist/types/claude-versions.js +2 -0
  58. package/dist/types/claude-versions.js.map +1 -0
  59. package/dist/types/commands.d.ts +32 -0
  60. package/dist/types/commands.d.ts.map +1 -0
  61. package/dist/types/commands.js +2 -0
  62. package/dist/types/commands.js.map +1 -0
  63. package/dist/types/index.d.ts +81 -0
  64. package/dist/types/index.d.ts.map +1 -0
  65. package/dist/types/index.js +150 -0
  66. package/dist/types/index.js.map +1 -0
  67. package/dist/types/subagents.d.ts +88 -0
  68. package/dist/types/subagents.d.ts.map +1 -0
  69. package/dist/types/subagents.js +2 -0
  70. package/dist/types/subagents.js.map +1 -0
  71. package/dist/utils/agentStorage.d.ts +19 -0
  72. package/dist/utils/agentStorage.d.ts.map +1 -0
  73. package/dist/utils/agentStorage.js +110 -0
  74. package/dist/utils/agentStorage.js.map +1 -0
  75. package/dist/utils/claudeVersionStorage.d.ts +33 -0
  76. package/dist/utils/claudeVersionStorage.d.ts.map +1 -0
  77. package/dist/utils/claudeVersionStorage.js +168 -0
  78. package/dist/utils/claudeVersionStorage.js.map +1 -0
  79. package/dist/utils/jwt.d.ts +15 -0
  80. package/dist/utils/jwt.d.ts.map +1 -0
  81. package/dist/utils/jwt.js +28 -0
  82. package/dist/utils/jwt.js.map +1 -0
  83. package/dist/utils/projectMetadataStorage.d.ts +21 -0
  84. package/dist/utils/projectMetadataStorage.d.ts.map +1 -0
  85. package/dist/utils/projectMetadataStorage.js +68 -0
  86. package/dist/utils/projectMetadataStorage.js.map +1 -0
  87. package/frontend/dist/index.html +86 -0
  88. package/package.json +66 -0
  89. package/src/bin/agentstudio.ts +161 -0
  90. package/src/index.ts +100 -0
  91. package/src/middleware/auth.ts +26 -0
  92. package/src/routes/agents.ts +885 -0
  93. package/src/routes/auth.ts +73 -0
  94. package/src/routes/commands.ts.bak +441 -0
  95. package/src/routes/files.ts +352 -0
  96. package/src/routes/mcp.ts +751 -0
  97. package/src/routes/media.ts +140 -0
  98. package/src/routes/projects.ts.bak +601 -0
  99. package/src/routes/sessions.ts.bak +809 -0
  100. package/src/routes/settings.ts.bak +718 -0
  101. package/src/routes/slides.ts +170 -0
  102. package/src/routes/subagents.ts.bak +364 -0
  103. package/src/services/claudeSession.ts +293 -0
  104. package/src/services/messageQueue.ts +71 -0
  105. package/src/services/sessionManager.ts +532 -0
  106. package/src/types/claude-history.ts +50 -0
  107. package/src/types/claude-versions.ts +33 -0
  108. package/src/types/commands.ts +35 -0
  109. package/src/types/index.ts +248 -0
  110. package/src/types/subagents.ts +106 -0
  111. package/src/utils/agentStorage.ts +126 -0
  112. package/src/utils/claudeVersionStorage.ts +199 -0
  113. package/src/utils/jwt.ts +36 -0
  114. package/src/utils/projectMetadataStorage.ts +86 -0
  115. package/tsconfig.json +26 -0
@@ -0,0 +1,601 @@
1
+ import express from 'express';
2
+ import fs from 'fs';
3
+ import path from 'path';
4
+ import * as os from 'os';
5
+ import { promisify } from 'util';
6
+ import { ProjectMetadataStorage } from '../utils/projectMetadataStorage.js';
7
+ import { AgentStorage } from '../utils/agentStorage.js';
8
+
9
+ const router: express.Router = express.Router();
10
+ const readFile = promisify(fs.readFile);
11
+ const writeFile = promisify(fs.writeFile);
12
+ const mkdir = promisify(fs.mkdir);
13
+ const stat = promisify(fs.stat);
14
+
15
+ // Use the new project metadata storage
16
+ const projectStorage = new ProjectMetadataStorage();
17
+ // Global agent storage for agent management
18
+ const globalAgentStorage = new AgentStorage();
19
+
20
+ // Ensure directory exists
21
+ async function ensureDir(dirPath: string) {
22
+ try {
23
+ await mkdir(dirPath, { recursive: true });
24
+ } catch (error) {
25
+ // Directory already exists
26
+ }
27
+ }
28
+
29
+ // GET /api/projects - Get all projects
30
+ router.get('/', async (_req, res) => {
31
+ try {
32
+ const projects = projectStorage.getAllProjects();
33
+ res.json({ projects });
34
+ } catch (error) {
35
+ console.error('Error fetching projects:', error);
36
+ res.status(500).json({ error: 'Failed to fetch projects' });
37
+ }
38
+ });
39
+
40
+ // GET /api/projects/:dirName - Get specific project
41
+ router.get('/:dirName', async (req, res) => {
42
+ try {
43
+ const { dirName } = req.params;
44
+ const project = projectStorage.getProject(dirName);
45
+
46
+ if (!project) {
47
+ return res.status(404).json({ error: 'Project not found' });
48
+ }
49
+
50
+ res.json({ project });
51
+ } catch (error) {
52
+ console.error('Error fetching project:', error);
53
+ res.status(500).json({ error: 'Failed to fetch project' });
54
+ }
55
+ });
56
+
57
+ // POST /api/projects - Create new project
58
+ router.post('/', async (req, res) => {
59
+ try {
60
+ const { name, dirName, agentId, description, tags, metadata } = req.body;
61
+
62
+ if (!dirName) {
63
+ return res.status(400).json({ error: 'Directory name is required' });
64
+ }
65
+
66
+ // Check if project directory already exists
67
+ const existingProject = projectStorage.getProject(dirName);
68
+ if (existingProject) {
69
+ return res.status(409).json({ error: 'Project directory already exists' });
70
+ }
71
+
72
+ const projectMetadata = projectStorage.createProject(dirName, {
73
+ name: name || dirName,
74
+ description,
75
+ agentId,
76
+ tags,
77
+ metadata
78
+ });
79
+
80
+ const project = projectStorage.getProject(dirName);
81
+ res.json({ project, metadata: projectMetadata });
82
+ } catch (error) {
83
+ console.error('Error creating project:', error);
84
+ res.status(500).json({ error: 'Failed to create project' });
85
+ }
86
+ });
87
+
88
+ // PUT /api/projects/:dirName - Update project info
89
+ router.put('/:dirName', async (req, res) => {
90
+ try {
91
+ const { dirName } = req.params;
92
+ const { name, description, tags, metadata } = req.body;
93
+
94
+ const project = projectStorage.getProject(dirName);
95
+ if (!project) {
96
+ return res.status(404).json({ error: 'Project not found' });
97
+ }
98
+
99
+ // Update basic info
100
+ if (name !== undefined || description !== undefined) {
101
+ projectStorage.updateProjectInfo(dirName, { name, description });
102
+ }
103
+
104
+ // Update tags
105
+ if (tags !== undefined) {
106
+ projectStorage.updateProjectTags(dirName, tags);
107
+ }
108
+
109
+ // Update metadata
110
+ if (metadata !== undefined) {
111
+ projectStorage.updateProjectMetadata(dirName, metadata);
112
+ }
113
+
114
+ const updatedProject = projectStorage.getProject(dirName);
115
+ res.json({ project: updatedProject });
116
+ } catch (error) {
117
+ console.error('Error updating project:', error);
118
+ res.status(500).json({ error: 'Failed to update project' });
119
+ }
120
+ });
121
+
122
+ // DELETE /api/projects/:dirName - Delete project metadata
123
+ router.delete('/:dirName', async (req, res) => {
124
+ try {
125
+ const { dirName } = req.params;
126
+
127
+ const success = projectStorage.deleteProject(dirName);
128
+ if (!success) {
129
+ return res.status(404).json({ error: 'Project not found' });
130
+ }
131
+
132
+ res.json({ success: true });
133
+ } catch (error) {
134
+ console.error('Error deleting project:', error);
135
+ res.status(500).json({ error: 'Failed to delete project' });
136
+ }
137
+ });
138
+
139
+ // PUT /api/projects/:dirName/default-agent - Set default agent
140
+ router.put('/:dirName/default-agent', async (req, res) => {
141
+ try {
142
+ const { dirName } = req.params;
143
+ const { agentId } = req.body;
144
+
145
+ if (!agentId) {
146
+ return res.status(400).json({ error: 'Agent ID is required' });
147
+ }
148
+
149
+ projectStorage.setDefaultAgent(dirName, agentId);
150
+ const updatedProject = projectStorage.getProject(dirName);
151
+
152
+ res.json({ project: updatedProject });
153
+ } catch (error) {
154
+ console.error('Error setting default agent:', error);
155
+ res.status(500).json({ error: 'Failed to set default agent' });
156
+ }
157
+ });
158
+
159
+ // PUT /api/projects/:dirName/agents/:agentId - Enable/disable agent for project
160
+ router.put('/:dirName/agents/:agentId', async (req, res) => {
161
+ try {
162
+ const { dirName, agentId } = req.params;
163
+ const { enabled } = req.body;
164
+
165
+ if (typeof enabled !== 'boolean') {
166
+ return res.status(400).json({ error: 'Enabled must be a boolean' });
167
+ }
168
+
169
+ if (enabled) {
170
+ projectStorage.addAgentToProject(dirName, agentId);
171
+ } else {
172
+ projectStorage.removeAgentFromProject(dirName, agentId);
173
+ }
174
+
175
+ const updatedProject = projectStorage.getProject(dirName);
176
+ res.json({ project: updatedProject });
177
+ } catch (error) {
178
+ console.error('Error updating project agent:', error);
179
+ res.status(500).json({ error: 'Failed to update project agent' });
180
+ }
181
+ });
182
+
183
+
184
+ // GET /api/projects/:dirName/check-agent - Check if project needs agent selection
185
+ router.get('/:dirName/check-agent', async (req, res) => {
186
+ try {
187
+ const { dirName } = req.params;
188
+
189
+ const project = projectStorage.getProject(dirName);
190
+ if (!project) {
191
+ return res.status(404).json({ error: 'Project not found' });
192
+ }
193
+
194
+ const needsAgent = project.agents.length === 0 || !project.defaultAgent;
195
+
196
+ res.json({
197
+ needsAgent,
198
+ project: {
199
+ name: project.name,
200
+ path: project.path,
201
+ agents: project.agents,
202
+ defaultAgent: project.defaultAgent
203
+ }
204
+ });
205
+ } catch (error) {
206
+ console.error('Error checking project agent:', error);
207
+ res.status(500).json({ error: 'Failed to check project agent' });
208
+ }
209
+ });
210
+
211
+ // POST /api/projects/:dirName/select-agent - Select first agent for project
212
+ router.post('/:dirName/select-agent', async (req, res) => {
213
+ try {
214
+ const { dirName } = req.params;
215
+ const { agentId } = req.body;
216
+
217
+ if (!agentId) {
218
+ return res.status(400).json({ error: 'Agent ID is required' });
219
+ }
220
+
221
+ // Add the agent to the project and set it as default
222
+ projectStorage.addAgentToProject(dirName, agentId);
223
+ projectStorage.setDefaultAgent(dirName, agentId);
224
+
225
+ const updatedProject = projectStorage.getProject(dirName);
226
+
227
+ res.json({
228
+ success: true,
229
+ project: updatedProject
230
+ });
231
+ } catch (error) {
232
+ console.error('Error selecting agent for project:', error);
233
+ res.status(500).json({ error: 'Failed to select agent for project' });
234
+ }
235
+ });
236
+
237
+ // GET /api/projects/:dirName/claude-md - Get project CLAUDE.md content
238
+ router.get('/:dirName/claude-md', async (req, res) => {
239
+ try {
240
+ const { dirName } = req.params;
241
+
242
+ const project = projectStorage.getProject(dirName);
243
+ if (!project) {
244
+ return res.status(404).json({ error: 'Project not found' });
245
+ }
246
+
247
+ // Try to find CLAUDE.md in project directory first, then parent directory
248
+ let claudeFilePath = path.join(project.path, 'CLAUDE.md');
249
+ console.log('Looking for CLAUDE.md at:', claudeFilePath);
250
+
251
+ try {
252
+ const content = await readFile(claudeFilePath, 'utf-8');
253
+ console.log('Successfully read CLAUDE.md, content length:', content.length);
254
+ res.json({ content });
255
+ } catch (error: any) {
256
+ console.log('Error reading CLAUDE.md from project dir:', error.code);
257
+
258
+ if (error.code === 'ENOENT') {
259
+ // Try parent directory
260
+ const parentClaudeFilePath = path.join(path.dirname(project.path), 'CLAUDE.md');
261
+ console.log('Trying parent directory:', parentClaudeFilePath);
262
+
263
+ try {
264
+ const content = await readFile(parentClaudeFilePath, 'utf-8');
265
+ console.log('Successfully read CLAUDE.md from parent dir, content length:', content.length);
266
+ res.json({ content });
267
+ } catch (parentError: any) {
268
+ console.log('Error reading CLAUDE.md from parent dir:', parentError.code);
269
+ if (parentError.code === 'ENOENT') {
270
+ // File doesn't exist in either location, return empty content
271
+ res.json({ content: '' });
272
+ } else {
273
+ throw parentError;
274
+ }
275
+ }
276
+ } else {
277
+ throw error;
278
+ }
279
+ }
280
+ } catch (error) {
281
+ console.error('Error reading CLAUDE.md:', error);
282
+ res.status(500).json({ error: 'Failed to read CLAUDE.md file' });
283
+ }
284
+ });
285
+
286
+ // PUT /api/projects/:dirName/claude-md - Update project CLAUDE.md content
287
+ router.put('/:dirName/claude-md', async (req, res) => {
288
+ try {
289
+ const { dirName } = req.params;
290
+ const { content } = req.body;
291
+
292
+ if (typeof content !== 'string') {
293
+ return res.status(400).json({ error: 'Content must be a string' });
294
+ }
295
+
296
+ const project = projectStorage.getProject(dirName);
297
+ if (!project) {
298
+ return res.status(404).json({ error: 'Project not found' });
299
+ }
300
+
301
+ // Decide where to save the file - prefer project directory, but check if parent has existing file
302
+ let claudeFilePath = path.join(project.path, 'CLAUDE.md');
303
+ const parentClaudeFilePath = path.join(path.dirname(project.path), 'CLAUDE.md');
304
+
305
+ // Check if parent directory already has CLAUDE.md
306
+ try {
307
+ await stat(parentClaudeFilePath);
308
+ // Parent file exists, use that location
309
+ claudeFilePath = parentClaudeFilePath;
310
+ console.log('Using existing CLAUDE.md in parent directory:', claudeFilePath);
311
+ } catch (error: any) {
312
+ // Parent file doesn't exist, use project directory
313
+ console.log('Using project directory for CLAUDE.md:', claudeFilePath);
314
+ }
315
+
316
+ // Ensure directory exists
317
+ await ensureDir(path.dirname(claudeFilePath));
318
+
319
+ // Write the content
320
+ await writeFile(claudeFilePath, content, 'utf-8');
321
+
322
+ res.json({ success: true });
323
+ } catch (error) {
324
+ console.error('Error writing CLAUDE.md:', error);
325
+ res.status(500).json({ error: 'Failed to write CLAUDE.md file' });
326
+ }
327
+ });
328
+
329
+ // ========== ROUTES MIGRATED FROM AGENTS.TS ==========
330
+
331
+ // GET /api/projects/agents/:agentId - Get projects for a specific agent
332
+ router.get('/agents/:agentId', (req, res) => {
333
+ try {
334
+ const { agentId } = req.params;
335
+
336
+ // Verify agent exists
337
+ const agent = globalAgentStorage.getAgent(agentId);
338
+ if (!agent) {
339
+ return res.status(404).json({ error: 'Agent not found' });
340
+ }
341
+
342
+ // Get all projects and filter by agent
343
+ const allProjects = projectStorage.getAllProjects();
344
+ const agentProjects = allProjects.filter(project =>
345
+ project.agents.includes(agentId)
346
+ );
347
+
348
+ res.json({ projects: agentProjects });
349
+ } catch (error) {
350
+ console.error('Failed to get agent projects:', error);
351
+ res.status(500).json({ error: 'Failed to retrieve agent projects' });
352
+ }
353
+ });
354
+
355
+ // POST /api/projects/create - Create new project directory in ~/.claude/projects
356
+ router.post('/create', (req, res) => {
357
+ try {
358
+ const { agentId, projectName, parentDirectory, description } = req.body;
359
+
360
+ if (!agentId || !projectName) {
361
+ return res.status(400).json({ error: 'Agent ID and project name are required' });
362
+ }
363
+
364
+ // Verify agent exists
365
+ const agent = globalAgentStorage.getAgent(agentId);
366
+ if (!agent) {
367
+ return res.status(404).json({ error: 'Agent not found' });
368
+ }
369
+
370
+ // Use custom parent directory if provided, otherwise default to ~/claude-code-projects
371
+ let projectPath: string;
372
+ if (parentDirectory && parentDirectory !== '~/claude-code-projects') {
373
+ // Expand tilde if present
374
+ const expandedParent = parentDirectory.startsWith('~/')
375
+ ? path.join(os.homedir(), parentDirectory.slice(2))
376
+ : parentDirectory;
377
+ projectPath = path.join(expandedParent, projectName);
378
+ } else {
379
+ const homeDir = os.homedir();
380
+ const projectsDir = path.join(homeDir, 'claude-code-projects');
381
+ projectPath = path.join(projectsDir, projectName);
382
+
383
+ // Create projects directory if it doesn't exist
384
+ if (!fs.existsSync(projectsDir)) {
385
+ fs.mkdirSync(projectsDir, { recursive: true });
386
+ }
387
+ }
388
+
389
+ // Create project directory
390
+ if (!fs.existsSync(projectPath)) {
391
+ fs.mkdirSync(projectPath, { recursive: true });
392
+
393
+ // Create .cc-sessions directory and project metadata
394
+ const sessionsDir = path.join(projectPath, '.cc-sessions');
395
+ fs.mkdirSync(sessionsDir, { recursive: true });
396
+
397
+ const projectMetadata = {
398
+ name: projectName,
399
+ description: description || '',
400
+ agentId,
401
+ agentName: agent.name,
402
+ createdAt: new Date().toISOString(),
403
+ updatedAt: new Date().toISOString()
404
+ };
405
+
406
+ fs.writeFileSync(
407
+ path.join(sessionsDir, 'project.json'),
408
+ JSON.stringify(projectMetadata, null, 2)
409
+ );
410
+
411
+ // Create a basic README file
412
+ const readmeContent = `# ${projectName}
413
+
414
+ Created with ${agent.name} on ${new Date().toLocaleString()}
415
+
416
+ ${description ? `## Description\n${description}\n\n` : ''}This is your project workspace. You can:
417
+ - Store your files here
418
+ - Create subdirectories for organization
419
+ - Use this directory for your ${agent.name} sessions
420
+
421
+ The conversation history will be saved in \`.cc-sessions/${agentId}/\` within this directory.
422
+ `;
423
+
424
+ fs.writeFileSync(path.join(projectPath, 'README.md'), readmeContent);
425
+
426
+ // Add project path to agent's projects list
427
+ if (!agent.projects) {
428
+ agent.projects = [];
429
+ }
430
+ const normalizedPath = path.resolve(projectPath);
431
+ if (!agent.projects.includes(normalizedPath)) {
432
+ agent.projects.unshift(normalizedPath); // Add to beginning for most recent
433
+ agent.updatedAt = new Date().toISOString();
434
+ globalAgentStorage.saveAgent(agent);
435
+ }
436
+
437
+ // Return project info that matches frontend interface
438
+ const projectId = `${agentId}-${Buffer.from(normalizedPath).toString('base64').replace(/[+/=]/g, '').slice(-8)}`;
439
+
440
+ res.json({
441
+ success: true,
442
+ project: {
443
+ id: projectId,
444
+ name: projectName,
445
+ path: normalizedPath,
446
+ agentId,
447
+ agentName: agent.name,
448
+ agentIcon: agent.ui.icon,
449
+ agentColor: agent.ui.primaryColor,
450
+ createdAt: new Date().toISOString(),
451
+ lastAccessed: new Date().toISOString(),
452
+ description: description || ''
453
+ },
454
+ message: `Project "${projectName}" created successfully`
455
+ });
456
+ } else {
457
+ res.status(409).json({ error: 'Project directory already exists' });
458
+ }
459
+
460
+ } catch (error) {
461
+ console.error('Failed to create project:', error);
462
+ res.status(500).json({
463
+ error: 'Failed to create project directory',
464
+ details: error instanceof Error ? error.message : String(error)
465
+ });
466
+ }
467
+ });
468
+
469
+ // PUT /api/projects/by-id/:projectId - Update project metadata (legacy format support)
470
+ router.put('/by-id/:projectId', (req, res) => {
471
+ try {
472
+ const { projectId } = req.params;
473
+ const { description } = req.body;
474
+
475
+ // Find project by ID
476
+ const agents = globalAgentStorage.getAllAgents();
477
+ let targetProject = null;
478
+
479
+ for (const agent of agents) {
480
+ if (agent.projects && agent.projects.length > 0) {
481
+ for (const projectPath of agent.projects) {
482
+ const id = `${agent.id}-${Buffer.from(projectPath).toString('base64').replace(/[+/=]/g, '').slice(-8)}`;
483
+ if (id === projectId) {
484
+ targetProject = projectPath;
485
+ break;
486
+ }
487
+ }
488
+ if (targetProject) break;
489
+ }
490
+ }
491
+
492
+ if (!targetProject || !fs.existsSync(targetProject)) {
493
+ return res.status(404).json({ error: 'Project not found' });
494
+ }
495
+
496
+ // Update project metadata
497
+ const sessionsDir = path.join(targetProject, '.cc-sessions');
498
+ if (!fs.existsSync(sessionsDir)) {
499
+ fs.mkdirSync(sessionsDir, { recursive: true });
500
+ }
501
+
502
+ const metadataPath = path.join(sessionsDir, 'project.json');
503
+ let metadata = {};
504
+
505
+ if (fs.existsSync(metadataPath)) {
506
+ try {
507
+ metadata = JSON.parse(fs.readFileSync(metadataPath, 'utf-8'));
508
+ } catch (error) {
509
+ // Start with empty metadata if file is corrupted
510
+ }
511
+ }
512
+
513
+ const updatedMetadata = {
514
+ ...metadata,
515
+ description: description || '',
516
+ updatedAt: new Date().toISOString(),
517
+ createdAt: (metadata as any).createdAt || fs.statSync(targetProject).birthtime.toISOString()
518
+ };
519
+
520
+ fs.writeFileSync(metadataPath, JSON.stringify(updatedMetadata, null, 2));
521
+
522
+ res.json({
523
+ success: true,
524
+ message: 'Project updated successfully'
525
+ });
526
+
527
+ } catch (error) {
528
+ console.error('Failed to update project:', error);
529
+ res.status(500).json({ error: 'Failed to update project' });
530
+ }
531
+ });
532
+
533
+ // DELETE /api/projects/by-id/:projectId - Remove project from agent's list (legacy format support)
534
+ router.delete('/by-id/:projectId', (req, res) => {
535
+ try {
536
+ const { projectId } = req.params;
537
+
538
+ // Check if it's a new format project ID (starts with "project_")
539
+ if (projectId.startsWith('project_')) {
540
+ // Handle new project metadata format
541
+ const allProjects = projectStorage.getAllProjects();
542
+ const project = allProjects.find(p => p.id === projectId);
543
+
544
+ if (!project) {
545
+ return res.status(404).json({ error: 'Project not found' });
546
+ }
547
+
548
+ // Delete project metadata
549
+ const success = projectStorage.deleteProject(project.dirName);
550
+ if (!success) {
551
+ return res.status(404).json({ error: 'Project not found' });
552
+ }
553
+
554
+ res.json({
555
+ success: true,
556
+ message: 'Project removed successfully',
557
+ note: 'Project directory was not deleted from filesystem'
558
+ });
559
+ return;
560
+ }
561
+
562
+ // Handle legacy agent project format
563
+ const agents = globalAgentStorage.getAllAgents();
564
+ let targetProject = null;
565
+
566
+ for (const agent of agents) {
567
+ if (agent.projects && agent.projects.length > 0) {
568
+ for (let i = 0; i < agent.projects.length; i++) {
569
+ const projectPath = agent.projects[i];
570
+ const id = `${agent.id}-${Buffer.from(projectPath).toString('base64').replace(/[+/=]/g, '').slice(-8)}`;
571
+ if (id === projectId) {
572
+ targetProject = projectPath;
573
+
574
+ // Remove project from agent's projects list
575
+ agent.projects.splice(i, 1);
576
+ agent.updatedAt = new Date().toISOString();
577
+ globalAgentStorage.saveAgent(agent);
578
+ break;
579
+ }
580
+ }
581
+ if (targetProject) break;
582
+ }
583
+ }
584
+
585
+ if (!targetProject) {
586
+ return res.status(404).json({ error: 'Project not found' });
587
+ }
588
+
589
+ res.json({
590
+ success: true,
591
+ message: 'Project removed from list successfully',
592
+ note: 'Project directory was not deleted from filesystem'
593
+ });
594
+
595
+ } catch (error) {
596
+ console.error('Failed to delete project:', error);
597
+ res.status(500).json({ error: 'Failed to delete project' });
598
+ }
599
+ });
600
+
601
+ export default router;