Update app.js
Browse files
app.js
CHANGED
|
@@ -23,7 +23,7 @@ const {
|
|
| 23 |
SUPABASE_URL,
|
| 24 |
SUPABASE_SERVICE_ROLE_KEY,
|
| 25 |
EXTERNAL_SERVER_URL = 'http://localhost:7860',
|
| 26 |
-
STORAGE_BUCKET = 'project-assets',
|
| 27 |
PORT = 7860
|
| 28 |
} = process.env;
|
| 29 |
|
|
@@ -223,7 +223,6 @@ app.post('/feedback', async (req, res) => {
|
|
| 223 |
...pluginPayload
|
| 224 |
});
|
| 225 |
|
| 226 |
-
// We expect the AI Server to return success immediately while working in background
|
| 227 |
return res.json({ success: true, externalResponse: response.data });
|
| 228 |
|
| 229 |
} catch (err) {
|
|
@@ -235,6 +234,33 @@ app.post('/feedback', async (req, res) => {
|
|
| 235 |
}
|
| 236 |
});
|
| 237 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
app.post('/poll', async (req, res) => {
|
| 239 |
const { token } = req.body;
|
| 240 |
|
|
@@ -260,6 +286,7 @@ app.post('/poll', async (req, res) => {
|
|
| 260 |
const targetUrl = EXTERNAL_SERVER_URL.replace(/\/$/, '') + '/project/ping';
|
| 261 |
|
| 262 |
try {
|
|
|
|
| 263 |
const response = await axios.post(targetUrl, {
|
| 264 |
projectId: verifiedData.projectId,
|
| 265 |
userId: verifiedData.uid
|
|
@@ -288,6 +315,7 @@ app.post('/project/delete', verifySupabaseUser, async (req, res) => {
|
|
| 288 |
console.log(`🗑️ Deleting Project: ${projectId} requested by ${userId}`);
|
| 289 |
|
| 290 |
try {
|
|
|
|
| 291 |
const { data: project, error: fetchError } = await supabase
|
| 292 |
.from('projects')
|
| 293 |
.select('user_id')
|
|
@@ -298,6 +326,7 @@ app.post('/project/delete', verifySupabaseUser, async (req, res) => {
|
|
| 298 |
return res.status(403).json({ error: "Unauthorized" });
|
| 299 |
}
|
| 300 |
|
|
|
|
| 301 |
const { error: chunkError } = await supabase
|
| 302 |
.from('message_chunks')
|
| 303 |
.delete()
|
|
@@ -307,6 +336,7 @@ app.post('/project/delete', verifySupabaseUser, async (req, res) => {
|
|
| 307 |
console.warn(`Warning: Failed to delete message chunks: ${chunkError.message}`);
|
| 308 |
}
|
| 309 |
|
|
|
|
| 310 |
const { error: dbError } = await supabase
|
| 311 |
.from('projects')
|
| 312 |
.delete()
|
|
@@ -314,14 +344,17 @@ app.post('/project/delete', verifySupabaseUser, async (req, res) => {
|
|
| 314 |
|
| 315 |
if (dbError) throw dbError;
|
| 316 |
|
|
|
|
| 317 |
if (STORAGE_BUCKET) {
|
| 318 |
const { data: files } = await supabase.storage.from(STORAGE_BUCKET).list(projectId);
|
|
|
|
| 319 |
if (files && files.length > 0) {
|
| 320 |
const filesToRemove = files.map(f => `${projectId}/${f.name}`);
|
| 321 |
await supabase.storage.from(STORAGE_BUCKET).remove(filesToRemove);
|
| 322 |
}
|
| 323 |
}
|
| 324 |
|
|
|
|
| 325 |
activeSessions.delete(`${userId}:${projectId}`);
|
| 326 |
for (const [key, val] of tempKeys.entries()) {
|
| 327 |
if (val.projectId === projectId) tempKeys.delete(key);
|
|
|
|
| 23 |
SUPABASE_URL,
|
| 24 |
SUPABASE_SERVICE_ROLE_KEY,
|
| 25 |
EXTERNAL_SERVER_URL = 'http://localhost:7860',
|
| 26 |
+
STORAGE_BUCKET = 'project-assets', // Default bucket name
|
| 27 |
PORT = 7860
|
| 28 |
} = process.env;
|
| 29 |
|
|
|
|
| 223 |
...pluginPayload
|
| 224 |
});
|
| 225 |
|
|
|
|
| 226 |
return res.json({ success: true, externalResponse: response.data });
|
| 227 |
|
| 228 |
} catch (err) {
|
|
|
|
| 234 |
}
|
| 235 |
});
|
| 236 |
|
| 237 |
+
// --- RETAINED FEEDBACK2 ENDPOINT ---
|
| 238 |
+
app.post('/feedback2', verifySupabaseUser, async (req, res) => {
|
| 239 |
+
const { projectId, prompt, images, ...otherPayload } = req.body;
|
| 240 |
+
const userId = req.user.id;
|
| 241 |
+
|
| 242 |
+
if (!projectId || !prompt) {
|
| 243 |
+
return res.status(400).json({ error: 'Missing projectId or prompt' });
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
const targetUrl = EXTERNAL_SERVER_URL.replace(/\/$/, '') + '/project/feedback';
|
| 247 |
+
|
| 248 |
+
try {
|
| 249 |
+
const response = await axios.post(targetUrl, {
|
| 250 |
+
userId: userId,
|
| 251 |
+
projectId: projectId,
|
| 252 |
+
prompt: prompt,
|
| 253 |
+
images: images || [],
|
| 254 |
+
...otherPayload
|
| 255 |
+
});
|
| 256 |
+
|
| 257 |
+
return res.json({ success: true, externalResponse: response.data });
|
| 258 |
+
} catch (err) {
|
| 259 |
+
console.error("Forward Error:", err.message);
|
| 260 |
+
return res.status(502).json({ error: 'Failed to forward' });
|
| 261 |
+
}
|
| 262 |
+
});
|
| 263 |
+
|
| 264 |
app.post('/poll', async (req, res) => {
|
| 265 |
const { token } = req.body;
|
| 266 |
|
|
|
|
| 286 |
const targetUrl = EXTERNAL_SERVER_URL.replace(/\/$/, '') + '/project/ping';
|
| 287 |
|
| 288 |
try {
|
| 289 |
+
// Forward ping to get commands AND stream
|
| 290 |
const response = await axios.post(targetUrl, {
|
| 291 |
projectId: verifiedData.projectId,
|
| 292 |
userId: verifiedData.uid
|
|
|
|
| 315 |
console.log(`🗑️ Deleting Project: ${projectId} requested by ${userId}`);
|
| 316 |
|
| 317 |
try {
|
| 318 |
+
// 1. Verify Ownership
|
| 319 |
const { data: project, error: fetchError } = await supabase
|
| 320 |
.from('projects')
|
| 321 |
.select('user_id')
|
|
|
|
| 326 |
return res.status(403).json({ error: "Unauthorized" });
|
| 327 |
}
|
| 328 |
|
| 329 |
+
// 2. Explicitly Delete Message Chunks
|
| 330 |
const { error: chunkError } = await supabase
|
| 331 |
.from('message_chunks')
|
| 332 |
.delete()
|
|
|
|
| 336 |
console.warn(`Warning: Failed to delete message chunks: ${chunkError.message}`);
|
| 337 |
}
|
| 338 |
|
| 339 |
+
// 3. Delete Project
|
| 340 |
const { error: dbError } = await supabase
|
| 341 |
.from('projects')
|
| 342 |
.delete()
|
|
|
|
| 344 |
|
| 345 |
if (dbError) throw dbError;
|
| 346 |
|
| 347 |
+
// 4. Delete from Supabase Storage
|
| 348 |
if (STORAGE_BUCKET) {
|
| 349 |
const { data: files } = await supabase.storage.from(STORAGE_BUCKET).list(projectId);
|
| 350 |
+
|
| 351 |
if (files && files.length > 0) {
|
| 352 |
const filesToRemove = files.map(f => `${projectId}/${f.name}`);
|
| 353 |
await supabase.storage.from(STORAGE_BUCKET).remove(filesToRemove);
|
| 354 |
}
|
| 355 |
}
|
| 356 |
|
| 357 |
+
// 5. Clear from Memory
|
| 358 |
activeSessions.delete(`${userId}:${projectId}`);
|
| 359 |
for (const [key, val] of tempKeys.entries()) {
|
| 360 |
if (val.projectId === projectId) tempKeys.delete(key);
|