返回 ViMax
server.mjs
根目录 / web / server.mjs
1 import {createReadStream, existsSync} from 'node:fs';
2 import {readFile} from 'node:fs/promises';
3 import {createServer} from 'node:http';
4 import path from 'node:path';
5 import {spawn} from 'node:child_process';
6 import {fileURLToPath} from 'node:url';
7 import {readAgentConfig, saveAgentConfig} from './config-store.mjs';
8 import {
9 artifactContentType,
10 deleteSession,
11 listSessionArtifacts,
12 readSessionHistory,
13 readSessionState,
14 resolveArtifactPath,
15 storeWorkspaceUpload,
16 } from './server-lib.mjs';
17
18 const webRoot = path.dirname(fileURLToPath(import.meta.url));
19 const repoRoot = path.resolve(process.env.VIMAX_WORKSPACE_ROOT || path.resolve(webRoot, '..'));
20 const isDev = process.argv.includes('--dev');
21 const host = process.env.VIMAX_WEB_HOST || '127.0.0.1';
22 const port = Number(process.env.VIMAX_WEB_PORT || 4173);
23 const configuredUploadLimit = Number(process.env.VIMAX_WEB_UPLOAD_MAX_BYTES || 100 * 1024 * 1024);
24 const uploadMaxBytes = Number.isFinite(configuredUploadLimit) && configuredUploadLimit > 0
25 ? configuredUploadLimit
26 : 100 * 1024 * 1024;
27 const subscribers = new Set();
28 let agentProcess = null;
29 let activeSessionId = '';
30
31 let vite = null;
32
33 const server = createServer(async (request, response) => {
34 const url = new URL(request.url || '/', `http://${request.headers.host || `${host}:${port}`}`);
35 try {
36 if (url.pathname === '/api/events' && request.method === 'GET') {
37 return openEventStream(request, response);
38 }
39 if (url.pathname === '/api/sessions' && request.method === 'GET') {
40 return sendJson(response, 200, await readSessionState(repoRoot));
41 }
42 if (url.pathname === '/api/config' && request.method === 'GET') {
43 return sendJson(response, 200, await readAgentConfig(repoRoot));
44 }
45 if (url.pathname === '/api/config' && request.method === 'PUT') {
46 const config = await saveAgentConfig(repoRoot, await readJsonBody(request));
47 stopAgent('config');
48 return sendJson(response, 200, config);
49 }
50 if (url.pathname === '/api/sessions' && request.method === 'DELETE') {
51 const sessionId = url.searchParams.get('session') || '';
52 const current = await readSessionState(repoRoot);
53 if (!current.sessions.some((session) => session.sessionId === sessionId)) {
54 return sendJson(response, 404, {error: 'Project not found'});
55 }
56 if (sessionId === activeSessionId) stopAgent('delete');
57 const state = await deleteSession(repoRoot, sessionId);
58 activeSessionId = state.activeSessionId;
59 broadcast({type: 'sessions_changed', ...state});
60 return sendJson(response, 200, state);
61 }
62 if (url.pathname === '/api/history' && request.method === 'GET') {
63 return sendJson(response, 200, {messages: await readSessionHistory(repoRoot, url.searchParams.get('session') || '')});
64 }
65 if (url.pathname === '/api/artifacts' && request.method === 'GET') {
66 return sendJson(response, 200, {artifacts: await listSessionArtifacts(repoRoot, url.searchParams.get('session') || '')});
67 }
68 if (url.pathname === '/api/artifact' && request.method === 'GET') {
69 return streamArtifact(response, url.searchParams.get('session') || '', url.searchParams.get('path') || '');
70 }
71 if (url.pathname === '/api/uploads' && request.method === 'POST') {
72 const sessionId = url.searchParams.get('session') || '';
73 const fileName = url.searchParams.get('name') || '';
74 const current = await readSessionState(repoRoot);
75 if (!current.sessions.some((session) => session.sessionId === sessionId)) {
76 return sendJson(response, 404, {error: 'Project not found'});
77 }
78 const declaredSize = Number(request.headers['content-length'] || 0);
79 if (declaredSize > uploadMaxBytes) {
80 return sendJson(response, 413, {error: `File exceeds the ${formatByteLimit(uploadMaxBytes)} upload limit`});
81 }
82 const data = await readBinaryBody(request, uploadMaxBytes);
83 const file = await storeWorkspaceUpload(repoRoot, sessionId, fileName, data);
84 return sendJson(response, 201, {file});
85 }
86 if (url.pathname === '/api/agent/start' && request.method === 'POST') {
87 const body = await readJsonBody(request);
88 const sessionId = typeof body.sessionId === 'string' ? body.sessionId : '';
89 const projectName = typeof body.projectName === 'string' ? body.projectName.trim() : '';
90 if (projectName.length > 64) {
91 return sendJson(response, 400, {error: 'Project name must be 64 characters or fewer'});
92 }
93 await startAgent({newSession: body.newSession === true, sessionId, projectName});
94 return sendJson(response, 200, {ok: true});
95 }
96 if (url.pathname === '/api/messages' && request.method === 'POST') {
97 const body = await readJsonBody(request);
98 const text = String(body.text || '').trim();
99 if (!text) return sendJson(response, 400, {error: 'Message text is required'});
100 if (!agentProcess?.stdin.writable) return sendJson(response, 409, {error: 'Agent is not running'});
101 agentProcess.stdin.write(`${text}\n`);
102 return sendJson(response, 202, {ok: true});
103 }
104 if (url.pathname === '/api/agent/stop' && request.method === 'POST') {
105 stopAgent('user');
106 return sendJson(response, 200, {ok: true});
107 }
108 if (url.pathname === '/api/health' && request.method === 'GET') {
109 return sendJson(response, 200, {ok: true, agentRunning: Boolean(agentProcess), activeSessionId});
110 }
111 if (url.pathname === '/assets/vimax.png' && request.method === 'GET') {
112 response.writeHead(200, {'Content-Type': 'image/png', 'Cache-Control': 'public, max-age=3600'});
113 createReadStream(process.env.VIMAX_LOGO_PATH || path.join(repoRoot, 'assets', 'vimax.png')).pipe(response);
114 return;
115 }
116 if (vite) {
117 vite.middlewares(request, response, () => sendJson(response, 404, {error: 'Not found'}));
118 return;
119 }
120 return serveProductionApp(response, url.pathname);
121 } catch (error) {
122 const status = Number(error?.statusCode) || 500;
123 sendJson(response, status, {error: error instanceof Error ? error.message : String(error)});
124 }
125 });
126
127 if (isDev) {
128 vite = await (await import('vite')).createServer({
129 root: webRoot,
130 server: {middlewareMode: true, hmr: {server}},
131 appType: 'spa',
132 });
133 }
134
135 server.listen(port, host, () => {
136 console.log(`ViMax Web: http://${host}:${port}`);
137 });
138
139 process.on('SIGINT', shutdown);
140 process.on('SIGTERM', shutdown);
141
142 async function startAgent({newSession, sessionId, projectName = ''}) {
143 if (newSession && sessionId) throw new Error('Choose either a new or existing session');
144 stopAgent('switch');
145 const {command, args} = agentCommand();
146 const sessionArgs = newSession
147 ? ['--new-session', ...(projectName ? ['--new-session-name', projectName] : [])]
148 : sessionId
149 ? ['--session', sessionId]
150 : [];
151 activeSessionId = sessionId;
152 const agentEntry = process.env.VIMAX_AGENT_ENTRY || 'main_agent.py';
153 const child = spawn(command, [...args, agentEntry, '--jsonl', '--stdin-repl', ...sessionArgs], {
154 cwd: repoRoot,
155 env: process.env,
156 stdio: ['pipe', 'pipe', 'pipe'],
157 });
158 agentProcess = child;
159 let childStdoutBuffer = '';
160 broadcast({type: 'bridge_status', status: 'starting', message: newSession ? 'Creating workspace' : 'Opening workspace'});
161 child.stdout.setEncoding('utf8');
162 child.stdout.on('data', (chunk) => {
163 if (agentProcess !== child) return;
164 childStdoutBuffer += String(chunk);
165 const lines = childStdoutBuffer.split(/\r?\n/);
166 childStdoutBuffer = lines.pop() || '';
167 for (const line of lines) consumeAgentLine(line);
168 });
169 child.stderr.setEncoding('utf8');
170 child.stderr.on('data', (chunk) => {
171 if (agentProcess !== child) return;
172 for (const line of String(chunk).split(/\r?\n/)) {
173 if (line.trim()) broadcast({type: 'terminal', stream: 'stderr', line});
174 }
175 });
176 child.on('error', (error) => {
177 if (agentProcess !== child) return;
178 broadcast({type: 'error', message: `Agent process error: ${error.message}`});
179 });
180 child.on('exit', (code, signal) => {
181 if (agentProcess !== child) return;
182 agentProcess = null;
183 broadcast({
184 type: 'bridge_status',
185 status: code === 0 || signal === 'SIGTERM' ? 'stopped' : 'error',
186 message: signal ? `Agent stopped by ${signal}` : `Agent exited with code ${code ?? 0}`,
187 });
188 });
189 setTimeout(async () => {
190 if (agentProcess !== child) return;
191 const state = await readSessionState(repoRoot);
192 activeSessionId = state.activeSessionId || sessionId || activeSessionId;
193 broadcast({type: 'sessions_changed', ...state, activeSessionId});
194 broadcast({type: 'bridge_status', status: 'ready', message: 'Agent ready'});
195 }, 350);
196 }
197
198 function consumeAgentLine(line) {
199 if (!line.trim()) return;
200 try {
201 const event = JSON.parse(line);
202 if (event.type === 'session') activeSessionId = event.session?.active_session_id || activeSessionId;
203 broadcast(event);
204 if (event.type === 'session') {
205 readSessionState(repoRoot).then((state) => broadcast({type: 'sessions_changed', ...state}));
206 }
207 } catch {
208 broadcast({type: 'terminal', stream: 'stdout', line});
209 }
210 }
211
212 function openEventStream(request, response) {
213 response.writeHead(200, {
214 'Content-Type': 'text/event-stream',
215 'Cache-Control': 'no-cache, no-transform',
216 Connection: 'keep-alive',
217 'X-Accel-Buffering': 'no',
218 });
219 response.write(`data: ${JSON.stringify({type: 'bridge_status', status: agentProcess ? 'ready' : 'idle', message: agentProcess ? 'Agent connected' : 'Agent idle'})}\n\n`);
220 subscribers.add(response);
221 const heartbeat = setInterval(() => response.write(': keepalive\n\n'), 15_000);
222 request.on('close', () => {
223 clearInterval(heartbeat);
224 subscribers.delete(response);
225 });
226 }
227
228 function broadcast(event) {
229 const payload = `data: ${JSON.stringify(event)}\n\n`;
230 for (const subscriber of subscribers) subscriber.write(payload);
231 }
232
233 function stopAgent(reason) {
234 if (!agentProcess) return;
235 const child = agentProcess;
236 agentProcess = null;
237 child.kill('SIGTERM');
238 const message = reason === 'switch'
239 ? 'Switching workspace'
240 : reason === 'config'
241 ? 'Configuration updated'
242 : 'Generation stopped';
243 broadcast({type: 'bridge_status', status: 'stopped', message});
244 }
245
246 function agentCommand() {
247 if (process.env.VIMAX_AGENT_COMMAND) {
248 return {command: process.env.VIMAX_AGENT_COMMAND, args: splitArgs(process.env.VIMAX_AGENT_ARGS || '')};
249 }
250 const configuredPython = process.env.VIMAX_PYTHON_CMD;
251 if (configuredPython) return {command: configuredPython, args: []};
252 const bundledUv = process.env.VIMAX_UV_CMD || path.join(process.env.HOME || '', '.local', 'bin', 'uv');
253 if (bundledUv && existsSync(bundledUv)) return {command: bundledUv, args: ['run', 'python']};
254 const venvPython = path.join(repoRoot, '.venv', 'bin', 'python3');
255 if (existsSync(venvPython)) return {command: venvPython, args: []};
256 return {command: 'uv', args: ['run', 'python']};
257 }
258
259 function splitArgs(value) {
260 return value.split(/\s+/).map((part) => part.trim()).filter(Boolean);
261 }
262
263 async function readJsonBody(request) {
264 const chunks = [];
265 for await (const chunk of request) chunks.push(chunk);
266 if (!chunks.length) return {};
267 const text = Buffer.concat(chunks).toString('utf8');
268 if (text.length > 1_000_000) throw new Error('Request body is too large');
269 return JSON.parse(text);
270 }
271
272 async function readBinaryBody(request, maxBytes) {
273 const chunks = [];
274 let size = 0;
275 for await (const chunk of request) {
276 size += chunk.length;
277 if (size > maxBytes) {
278 const error = new Error(`File exceeds the ${formatByteLimit(maxBytes)} upload limit`);
279 error.statusCode = 413;
280 throw error;
281 }
282 chunks.push(chunk);
283 }
284 return Buffer.concat(chunks, size);
285 }
286
287 function formatByteLimit(bytes) {
288 return `${Math.max(1, Math.round(bytes / (1024 * 1024)))} MB`;
289 }
290
291 function sendJson(response, status, payload) {
292 if (response.writableEnded) return;
293 response.writeHead(status, {'Content-Type': 'application/json; charset=utf-8'});
294 response.end(JSON.stringify(payload));
295 }
296
297 async function streamArtifact(response, sessionId, relativePath) {
298 const filePath = resolveArtifactPath(repoRoot, sessionId, relativePath);
299 if (!existsSync(filePath)) return sendJson(response, 404, {error: 'Artifact not found'});
300 response.writeHead(200, {
301 'Content-Type': artifactContentType(filePath),
302 'Cache-Control': 'private, max-age=60',
303 });
304 createReadStream(filePath).pipe(response);
305 }
306
307 async function serveProductionApp(response, pathname) {
308 const requested = pathname === '/' ? 'index.html' : pathname.replace(/^\/+/, '');
309 const candidate = path.resolve(webRoot, 'dist', requested);
310 const distRoot = path.resolve(webRoot, 'dist');
311 const safeCandidate = candidate.startsWith(`${distRoot}${path.sep}`) ? candidate : path.join(distRoot, 'index.html');
312 const filePath = existsSync(safeCandidate) ? safeCandidate : path.join(distRoot, 'index.html');
313 const body = await readFile(filePath);
314 response.writeHead(200, {'Content-Type': artifactContentType(filePath)});
315 response.end(body);
316 }
317
318 function shutdown() {
319 stopAgent('shutdown');
320 server.close(() => process.exit(0));
321 setTimeout(() => process.exit(0), 1_000).unref();
322 }
323
323 lines Plain Text