-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
293 lines (250 loc) · 8.73 KB
/
server.js
File metadata and controls
293 lines (250 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import express from 'express';
import multer from 'multer';
import cors from 'cors';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { existsSync, mkdirSync, writeFileSync } from 'fs';
import OpenAI from 'openai';
import dotenv from 'dotenv';
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
const PORT = 3001;
// Enable CORS for all origins in development
app.use(cors({
origin: true,
credentials: true
}));
app.use(express.json({ limit: '50mb' }));
// Create uploads directory
const uploadsDir = join(__dirname, 'uploads');
if (!existsSync(uploadsDir)) {
mkdirSync(uploadsDir, { recursive: true });
}
// Configure multer for file uploads
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, uploadsDir);
},
filename: (req, file, cb) => {
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
const ext = file.originalname.split('.').pop();
cb(null, `${uniqueSuffix}.${ext}`);
}
});
const upload = multer({
storage,
limits: { fileSize: 50 * 1024 * 1024 }, // 50MB limit
fileFilter: (req, file, cb) => {
const allowedTypes = [
'application/pdf',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.ms-powerpoint',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'image/jpeg',
'image/png',
'image/gif',
'image/webp'
];
if (allowedTypes.includes(file.mimetype)) {
cb(null, true);
} else {
cb(new Error(`File type ${file.mimetype} not allowed`), false);
}
}
});
// Serve uploaded files
app.use('/uploads', express.static(uploadsDir));
// File upload endpoint
app.post('/api/upload', upload.single('file'), (req, res) => {
if (!req.file) {
return res.status(400).json({ error: 'No file uploaded' });
}
// Return relative URL so it works with any host (localhost, ngrok, etc)
const fileUrl = `/uploads/${req.file.filename}`;
res.json({
success: true,
filename: req.file.filename,
originalName: req.file.originalname,
mimetype: req.file.mimetype,
size: req.file.size,
url: fileUrl
});
});
// Health check
app.get('/api/health', (req, res) => {
res.json({ status: 'ok', uploads: uploadsDir });
});
// OpenRouter client for AI image generation
const openrouterClient = new OpenAI({
baseURL: 'https://openrouter.ai/api/v1',
apiKey: process.env.OPENROUTER_API_KEY,
});
// AI Image generation endpoint
app.post('/api/generate-image', async (req, res) => {
const { prompt, image, systemPrompt } = req.body;
if (!prompt && !image) {
return res.status(400).json({ error: 'Prompt or image is required' });
}
if (!process.env.OPENROUTER_API_KEY) {
return res.status(500).json({ error: 'OPENROUTER_API_KEY not configured' });
}
try {
console.log(`🎨 Generating image with prompt: "${prompt || '(none)'}"${image ? ' (with input image)' : ''}`);
// Build message content - text only or multimodal with image
let messageContent;
if (image) {
// Multimodal request with image input
let fullPrompt;
if (systemPrompt && prompt) {
fullPrompt = `${systemPrompt}\n\nThe user's instructions: ${prompt}`;
} else if (systemPrompt) {
fullPrompt = systemPrompt;
} else if (prompt) {
fullPrompt = `Generate an image: ${prompt}`;
} else {
fullPrompt = 'Generate a polished version of this sketch/drawing.';
}
messageContent = [
{
type: 'text',
text: fullPrompt,
},
{
type: 'image_url',
image_url: {
url: image, // Already a data URL from canvas
},
},
];
} else {
// Text-only request
messageContent = `Generate an image: ${prompt}`;
}
const response = await openrouterClient.chat.completions.create({
model: 'google/gemini-3-pro-image-preview',
messages: [
{
role: 'user',
content: messageContent,
},
],
});
// Extract the image from the response
const message = response.choices[0]?.message;
if (!message) {
return res.status(500).json({ error: 'No response from AI' });
}
// Log text content if any
if (message.content) {
console.log('Text response:', message.content);
}
// Check for images in message.images (Gemini format)
if (message.images && Array.isArray(message.images)) {
for (const imageData of message.images) {
if (imageData.type === 'image_url' && imageData.image_url?.url?.startsWith('data:image')) {
const dataUrl = imageData.image_url.url;
const base64Data = dataUrl.split(',')[1];
const buffer = Buffer.from(base64Data, 'base64');
const filename = `ai-${Date.now()}.png`;
const filepath = join(uploadsDir, filename);
writeFileSync(filepath, buffer);
const imageUrl = `http://localhost:${PORT}/uploads/${filename}`;
console.log(`✅ Image saved: ${imageUrl}`);
return res.json({ success: true, url: imageUrl });
}
}
}
// If no image found, return error
return res.status(500).json({
error: 'No image in response',
response: message.content?.substring(0, 500) || 'No content'
});
} catch (error) {
console.error('AI Image generation error:', error);
return res.status(500).json({
error: error.message || 'Failed to generate image'
});
}
});
// AI Web generation endpoint (Claude Opus 4.5)
app.post('/api/generate-web', async (req, res) => {
const { prompt } = req.body;
if (!prompt) {
return res.status(400).json({ error: 'Prompt is required' });
}
if (!process.env.OPENROUTER_API_KEY) {
return res.status(500).json({ error: 'OPENROUTER_API_KEY not configured' });
}
try {
console.log(`🌐 Generating web content with prompt: "${prompt.substring(0, 100)}..."`);
const response = await openrouterClient.chat.completions.create({
model: 'anthropic/claude-sonnet-4.5',
messages: [
{
role: 'system',
content: `You are an expert web developer. Generate a complete, self-contained HTML page based on the user's request.
Rules:
- Return ONLY the HTML code, no explanations
- Include all CSS in a <style> tag in the <head>
- Include all JavaScript in a <script> tag before </body>
- CRITICAL: The content will be displayed in an iframe of unknown size. It MUST scale to fit the viewport perfectly:
* Use width: 100vw and height: 100vh for the main container
* Use relative units (%, vw, vh, vmin, vmax) instead of fixed pixels
* Center content both vertically and horizontally
* Remove all margins and padding from html/body (margin: 0; padding: 0)
* Use "overflow: hidden" on body to prevent scrollbars
- Make it visually appealing with modern CSS
- The page should be fully functional and interactive
- Use a dark theme by default unless specified otherwise
- Do NOT use any external resources (CDNs, images, fonts) - everything must be inline
- Start with <!DOCTYPE html> and end with </html>`
},
{
role: 'user',
content: prompt,
},
],
});
const message = response.choices[0]?.message;
if (!message || !message.content) {
return res.status(500).json({ error: 'No response from AI' });
}
// Extract HTML from response (handle markdown code blocks)
let html = message.content;
// Remove markdown code blocks if present
const htmlMatch = html.match(/```html\n?([\s\S]*?)```/);
if (htmlMatch) {
html = htmlMatch[1];
} else {
// Try without language specifier
const codeMatch = html.match(/```\n?([\s\S]*?)```/);
if (codeMatch) {
html = codeMatch[1];
}
}
// Ensure it starts with DOCTYPE
if (!html.trim().toLowerCase().startsWith('<!doctype')) {
// Wrap in basic HTML structure if needed
if (!html.includes('<html')) {
html = `<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body>${html}</body></html>`;
}
}
console.log(`✅ Generated ${html.length} bytes of HTML`);
return res.json({ success: true, html });
} catch (error) {
console.error('AI Web generation error:', error);
return res.status(500).json({
error: error.message || 'Failed to generate web content'
});
}
});
app.listen(PORT, () => {
console.log(`📁 File server running at http://localhost:${PORT}`);
console.log(` Upload endpoint: POST /api/upload`);
console.log(` Files served at: /uploads/`);
});