THEZYZSTUDIO commited on
Commit
a9c08da
·
verified ·
1 Parent(s): 2d29b0d

Delete server.js

Browse files
Files changed (1) hide show
  1. server.js +0 -283
server.js DELETED
@@ -1,283 +0,0 @@
1
- const express = require('express');
2
- const http = require('http');
3
- const { Server } = require('socket.io');
4
- const cors = require('cors');
5
- const fs = require('fs').promises;
6
- const path = require('path');
7
- const { exec } = require('child_process');
8
-
9
- const app = express();
10
- app.use(cors());
11
- app.use(express.json());
12
-
13
- // Create a workspace directory
14
- const WORKSPACE_DIR = path.join(__dirname, 'workspace');
15
- async function ensureWorkspace() {
16
- try {
17
- await fs.access(WORKSPACE_DIR);
18
- } catch {
19
- await fs.mkdir(WORKSPACE_DIR, { recursive: true });
20
- // Create demo files
21
- await fs.writeFile(
22
- path.join(WORKSPACE_DIR, 'index.html'),
23
- `<!DOCTYPE html>
24
- <html lang="en">
25
- <head>
26
- <meta charset="UTF-8">
27
- <title>Demo App</title>
28
- </head>
29
- <body>
30
- <h1>Hello AgentIDE!</h1>
31
- <p>This is a sample project.</p>
32
- </body>
33
- </html>`
34
- );
35
- await fs.writeFile(
36
- path.join(WORKSPACE_DIR, 'app.js'),
37
- `// Main application file
38
- console.log('AgentIDE demo app starting...');
39
-
40
- function greet(name) {
41
- return \`Hello, \${name}!\`;
42
- }
43
-
44
- const result = greet('World');
45
- console.log(result);`
46
- );
47
- await fs.writeFile(
48
- path.join(WORKSPACE_DIR, 'styles.css'),
49
- `/* Demo styles */
50
- body {
51
- font-family: Arial, sans-serif;
52
- margin: 0;
53
- padding: 20px;
54
- background: #1e1e1e;
55
- color: #d4d4d4;
56
- }
57
-
58
- h1 {
59
- color: #4fc1ff;
60
- }`
61
- );
62
- await fs.writeFile(
63
- path.join(WORKSPACE_DIR, 'package.json'),
64
- JSON.stringify({
65
- name: 'demo-project',
66
- version: '1.0.0',
67
- scripts: {
68
- test: 'echo "Tests passed!" && exit 0'
69
- }
70
- }, null, 2)
71
- );
72
- await fs.mkdir(path.join(WORKSPACE_DIR, 'src'), { recursive: true });
73
- await fs.writeFile(
74
- path.join(WORKSPACE_DIR, 'src', 'utils.js'),
75
- `// Utility functions
76
- export function add(a, b) {
77
- return a + b;
78
- }
79
-
80
- export function multiply(a, b) {
81
- return a * b;
82
- }`
83
- );
84
- }
85
- }
86
-
87
- // File System API
88
- app.get('/api/files', async (req, res) => {
89
- try {
90
- const files = await readDirectory(WORKSPACE_DIR);
91
- res.json(files);
92
- } catch (error) {
93
- res.status(500).json({ error: error.message });
94
- }
95
- });
96
-
97
- app.get('/api/files/*', async (req, res) => {
98
- try {
99
- const filePath = path.join(WORKSPACE_DIR, req.params[0]);
100
- const content = await fs.readFile(filePath, 'utf-8');
101
- res.json({ content });
102
- } catch (error) {
103
- res.status(500).json({ error: error.message });
104
- }
105
- });
106
-
107
- app.post('/api/files/*', async (req, res) => {
108
- try {
109
- const filePath = path.join(WORKSPACE_DIR, req.params[0]);
110
- const dir = path.dirname(filePath);
111
- await fs.mkdir(dir, { recursive: true });
112
- await fs.writeFile(filePath, req.body.content, 'utf-8');
113
- res.json({ success: true });
114
- } catch (error) {
115
- res.status(500).json({ error: error.message });
116
- }
117
- });
118
-
119
- app.delete('/api/files/*', async (req, res) => {
120
- try {
121
- const filePath = path.join(WORKSPACE_DIR, req.params[0]);
122
- await fs.unlink(filePath);
123
- res.json({ success: true });
124
- } catch (error) {
125
- res.status(500).json({ error: error.message });
126
- }
127
- });
128
-
129
- app.post('/api/files/create-folder/*', async (req, res) => {
130
- try {
131
- const folderPath = path.join(WORKSPACE_DIR, req.params[0]);
132
- await fs.mkdir(folderPath, { recursive: true });
133
- res.json({ success: true });
134
- } catch (error) {
135
- res.status(500).json({ error: error.message });
136
- }
137
- });
138
-
139
- app.post('/api/files/rename', async (req, res) => {
140
- try {
141
- const { oldPath, newPath } = req.body;
142
- const fullOldPath = path.join(WORKSPACE_DIR, oldPath);
143
- const fullNewPath = path.join(WORKSPACE_DIR, newPath);
144
- await fs.rename(fullOldPath, fullNewPath);
145
- res.json({ success: true });
146
- } catch (error) {
147
- res.status(500).json({ error: error.message });
148
- }
149
- });
150
-
151
- // AI API
152
- app.post('/api/ai/chat', async (req, res) => {
153
- const { message, context } = req.body;
154
-
155
- // Simulated AI responses (in production, integrate with OpenAI/Anthropic)
156
- const responses = {
157
- 'summarize': {
158
- type: 'summary',
159
- content: `# Project Summary
160
-
161
- This is a demo project with the following structure:
162
- - **index.html**: Main HTML entry point
163
- - **app.js**: JavaScript application logic
164
- - **styles.css**: Styling definitions
165
- - **package.json**: Project configuration
166
- - **src/utils.js**: Utility functions
167
-
168
- The project appears to be a simple web application demonstrating basic functionality.`
169
- },
170
- 'explain': {
171
- type: 'explanation',
172
- content: `The code you've selected is a simple greeting function that takes a name parameter and returns a formatted string. It uses template literals for string interpolation.`
173
- },
174
- 'default': {
175
- type: 'response',
176
- content: `I've analyzed your request. Based on the project structure, I can help you with:
177
-
178
- 1. Creating new files and folders
179
- 2. Refactoring existing code
180
- 3. Generating unit tests
181
- 4. Explaining code snippets
182
- 5. Running commands and tests
183
-
184
- What would you like me to do?`
185
- }
186
- };
187
-
188
- // Simple keyword matching for demo
189
- let response = responses.default;
190
- if (message.toLowerCase().includes('summarize') || message.toLowerCase().includes('structure')) {
191
- response = responses.summarize;
192
- } else if (message.toLowerCase().includes('explain')) {
193
- response = responses.explain;
194
- }
195
-
196
- res.json(response);
197
- });
198
-
199
- // Execute command
200
- app.post('/api/execute', (req, res) => {
201
- const { command } = req.body;
202
-
203
- exec(command, { cwd: WORKSPACE_DIR }, (error, stdout, stderr) => {
204
- res.json({
205
- success: !error,
206
- output: stdout || stderr,
207
- error: error ? error.message : null
208
- });
209
- });
210
- });
211
-
212
- // Helper function to read directory recursively
213
- async function readDirectory(dir, basePath = '') {
214
- const entries = await fs.readdir(dir, { withFileTypes: true });
215
- const result = [];
216
-
217
- for (const entry of entries) {
218
- const fullPath = path.join(dir, entry.name);
219
- const relativePath = path.join(basePath, entry.name);
220
-
221
- if (entry.isDirectory()) {
222
- result.push({
223
- name: entry.name,
224
- path: relativePath,
225
- type: 'folder',
226
- children: await readDirectory(fullPath, relativePath)
227
- });
228
- } else {
229
- const stats = await fs.stat(fullPath);
230
- result.push({
231
- name: entry.name,
232
- path: relativePath,
233
- type: 'file',
234
- size: stats.size,
235
- extension: path.extname(entry.name).slice(1)
236
- });
237
- }
238
- }
239
-
240
- return result;
241
- }
242
-
243
- // WebSocket Server for terminal and real-time updates
244
- const server = http.createServer(app);
245
- const io = new Server(server, {
246
- cors: {
247
- origin: '*',
248
- methods: ['GET', 'POST']
249
- }
250
- });
251
-
252
- io.on('connection', (socket) => {
253
- console.log('Client connected');
254
-
255
- socket.on('terminal-input', (data) => {
256
- // Simulate terminal output
257
- exec(data.command, { cwd: WORKSPACE_DIR }, (error, stdout, stderr) => {
258
- socket.emit('terminal-output', {
259
- output: stdout || stderr || `Command: ${data.command}`,
260
- error: !!error
261
- });
262
- });
263
- });
264
-
265
- socket.on('file-change', (data) => {
266
- socket.broadcast.emit('file-updated', data);
267
- });
268
-
269
- socket.on('disconnect', () => {
270
- console.log('Client disconnected');
271
- });
272
- });
273
-
274
- // Start server
275
- ensureWorkspace().then(() => {
276
- const PORT = process.env.PORT || 3001;
277
- server.listen(PORT, () => {
278
- console.log(`AgentIDE server running on http://localhost:${PORT}`);
279
- console.log(`Workspace: ${WORKSPACE_DIR}`);
280
- });
281
- });
282
-
283
- module.exports = app;