everydaycats commited on
Commit
81bfb97
·
verified ·
1 Parent(s): 3b8e3e1

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +52 -30
app.js CHANGED
@@ -6,7 +6,8 @@ const axios = require('axios');
6
  const bodyParser = require('body-parser');
7
 
8
  const app = express();
9
- app.use(bodyParser.json());
 
10
 
11
  // ---------------------------------------------------------
12
  // 1. STATE MANAGEMENT
@@ -43,7 +44,7 @@ const verifyFirebaseUser = async (req, res, next) => {
43
  const debugMode = process.env.DEBUG_NO_AUTH === 'true';
44
 
45
  if (debugMode) {
46
- req.user = { uid: "debug_user_001" };
47
  return next();
48
  }
49
 
@@ -169,47 +170,64 @@ app.post('/verify', async (req, res) => {
169
  }
170
  });
171
 
172
- // --- NEW ENDPOINT: Feedback Forwarder ---
 
 
 
 
 
 
 
 
173
  app.post('/feedback', async (req, res) => {
174
- const { token, prompt, logs } = req.body;
 
175
 
176
- if (!token || !prompt) return res.status(400).json({ error: 'Token and prompt required' });
177
 
178
- // 1. Verify User Session
179
  const decoded = jwt.decode(token);
180
  if (!decoded || !decoded.uid || !decoded.projectId) {
181
  return res.status(401).json({ error: 'Malformed token' });
182
  }
183
 
 
184
  const secret = await getSessionSecret(decoded.uid, decoded.projectId);
185
  if (!secret) return res.status(404).json({ error: 'Session revoked' });
186
 
187
  try {
188
- jwt.verify(token, secret); // Validate signature
189
 
190
- const externalBase = process.env.EXTERNAL_SERVER_URL || 'https://httpbin.org/post';
191
- // Strip the query parameters or path from base if necessary, or just append
192
- // Assuming EXTERNAL_SERVER_URL is the root (e.g. https://my-ai-api.com)
193
- // We append /project/feedback
194
  const targetUrl = externalBase.replace(/\/$/, '') + '/project/feedback';
195
 
196
- console.log(`📨 Forwarding feedback for ${decoded.projectId} to ${targetUrl}`);
197
 
 
 
 
198
  const response = await axios.post(targetUrl, {
199
- user: decoded.uid,
200
- projectId: decoded.projectId,
201
- prompt: prompt,
202
- logs: logs || ""
203
  });
204
 
205
  return res.json({ success: true, externalResponse: response.data });
206
 
207
  } catch (err) {
208
- console.error("Feedback Error:", err.message);
209
- return res.status(502).json({ error: 'Failed to forward feedback to AI server' });
 
 
 
210
  }
211
  });
212
 
 
 
 
 
213
  app.post('/poll', async (req, res) => {
214
  const { token } = req.body;
215
 
@@ -221,31 +239,31 @@ app.post('/poll', async (req, res) => {
221
  }
222
 
223
  const secret = await getSessionSecret(decoded.uid, decoded.projectId);
224
-
225
- if (!secret) {
226
- return res.status(404).json({ error: 'Session revoked or not found' });
227
- }
228
 
229
  try {
230
  const verifiedData = jwt.verify(token, secret);
231
 
 
232
  const threeDaysInSeconds = 3 * 24 * 60 * 60;
233
  const nowInSeconds = Math.floor(Date.now() / 1000);
234
-
235
  if (verifiedData.iat && (nowInSeconds - verifiedData.iat > threeDaysInSeconds)) {
236
  return res.status(403).json({ error: 'Token expired (older than 3 days)' });
237
  }
238
 
239
- const externalUrl = process.env.EXTERNAL_SERVER_URL || 'https://httpbin.org/post';
 
 
240
 
241
  try {
242
- const response = await axios.post(externalUrl, {
243
  projectId: verifiedData.projectId
244
- // Just polling for tasks, no user prompt here anymore
245
  });
246
 
247
- return res.json({ status: 'success', externalResponse: response.data });
 
248
  } catch (extError) {
 
249
  return res.status(502).json({ error: 'External server error' });
250
  }
251
 
@@ -257,6 +275,10 @@ app.post('/poll', async (req, res) => {
257
  }
258
  });
259
 
 
 
 
 
260
  app.get('/cleanup', (req, res) => {
261
  const THRESHOLD = 1000 * 60 * 60;
262
  const now = Date.now();
@@ -309,10 +331,10 @@ app.post('/nullify', verifyFirebaseUser, async (req, res) => {
309
  });
310
 
311
  app.get('/', (req, res) => {
312
- res.send('Plugin Auth Server Running');
313
  });
314
 
315
- const PORT = process.env.PORT || 7860;
316
  app.listen(PORT, () => {
317
- console.log(`🚀 Server running on http://localhost:${PORT}`);
318
  });
 
6
  const bodyParser = require('body-parser');
7
 
8
  const app = express();
9
+ // Increased limit to support image/context uploads from Plugin
10
+ app.use(bodyParser.json({ limit: '50mb' }));
11
 
12
  // ---------------------------------------------------------
13
  // 1. STATE MANAGEMENT
 
44
  const debugMode = process.env.DEBUG_NO_AUTH === 'true';
45
 
46
  if (debugMode) {
47
+ req.user = { uid: "user_dev_001" };
48
  return next();
49
  }
50
 
 
170
  }
171
  });
172
 
173
+ // ---------------------------------------------------------
174
+ // PROXY ENDPOINTS (UPDATED)
175
+ // ---------------------------------------------------------
176
+
177
+ /**
178
+ * FEEDBACK FORWARDER
179
+ * Forwards everything (Prompt, Context, Images, TaskComplete)
180
+ * to the Main Core Server.
181
+ */
182
  app.post('/feedback', async (req, res) => {
183
+ // 1. Separate the Auth Token from the Data payload
184
+ const { token, ...pluginPayload } = req.body;
185
 
186
+ if (!token) return res.status(400).json({ error: 'Token required' });
187
 
188
+ // 2. Decode Token to Identify User/Project
189
  const decoded = jwt.decode(token);
190
  if (!decoded || !decoded.uid || !decoded.projectId) {
191
  return res.status(401).json({ error: 'Malformed token' });
192
  }
193
 
194
+ // 3. Verify Session exists
195
  const secret = await getSessionSecret(decoded.uid, decoded.projectId);
196
  if (!secret) return res.status(404).json({ error: 'Session revoked' });
197
 
198
  try {
199
+ jwt.verify(token, secret); // Validate Signature
200
 
201
+ // 4. Construct External URL
202
+ const externalBase = process.env.EXTERNAL_SERVER_URL || 'http://localhost:7860';
 
 
203
  const targetUrl = externalBase.replace(/\/$/, '') + '/project/feedback';
204
 
205
+ console.log(`📨 Forwarding feedback for ${decoded.projectId} (${decoded.uid})`);
206
 
207
+ // 5. Forward Payload with User/Project Injection
208
+ // We pass ...pluginPayload which includes:
209
+ // prompt, hierarchyContext, scriptContext, logContext, taskComplete, images
210
  const response = await axios.post(targetUrl, {
211
+ userId: decoded.uid, // Injected from Auth
212
+ projectId: decoded.projectId, // Injected from Auth
213
+ ...pluginPayload // Pass-through everything else from Plugin
 
214
  });
215
 
216
  return res.json({ success: true, externalResponse: response.data });
217
 
218
  } catch (err) {
219
+ console.error("Feedback Forward Error:", err.message);
220
+ if (err.response) {
221
+ return res.status(err.response.status).json(err.response.data);
222
+ }
223
+ return res.status(502).json({ error: 'Failed to forward feedback to Main AI server' });
224
  }
225
  });
226
 
227
+ /**
228
+ * POLLING FORWARDER
229
+ * Maps Plugin '/poll' -> Main Server '/project/ping'
230
+ */
231
  app.post('/poll', async (req, res) => {
232
  const { token } = req.body;
233
 
 
239
  }
240
 
241
  const secret = await getSessionSecret(decoded.uid, decoded.projectId);
242
+ if (!secret) return res.status(404).json({ error: 'Session revoked or not found' });
 
 
 
243
 
244
  try {
245
  const verifiedData = jwt.verify(token, secret);
246
 
247
+ // Expiry check
248
  const threeDaysInSeconds = 3 * 24 * 60 * 60;
249
  const nowInSeconds = Math.floor(Date.now() / 1000);
 
250
  if (verifiedData.iat && (nowInSeconds - verifiedData.iat > threeDaysInSeconds)) {
251
  return res.status(403).json({ error: 'Token expired (older than 3 days)' });
252
  }
253
 
254
+ const externalBase = process.env.EXTERNAL_SERVER_URL || 'http://localhost:7860';
255
+ // Note: Main Core Server uses /project/ping for task retrieval
256
+ const targetUrl = externalBase.replace(/\/$/, '') + '/project/ping';
257
 
258
  try {
259
+ const response = await axios.post(targetUrl, {
260
  projectId: verifiedData.projectId
 
261
  });
262
 
263
+ // Return raw response from Main Server (contains action: 'EXECUTE', code: '...')
264
+ return res.json(response.data);
265
  } catch (extError) {
266
+ console.error("Poll Forward Error:", extError.message);
267
  return res.status(502).json({ error: 'External server error' });
268
  }
269
 
 
275
  }
276
  });
277
 
278
+ // ---------------------------------------------------------
279
+ // MANAGEMENT ENDPOINTS
280
+ // ---------------------------------------------------------
281
+
282
  app.get('/cleanup', (req, res) => {
283
  const THRESHOLD = 1000 * 60 * 60;
284
  const now = Date.now();
 
331
  });
332
 
333
  app.get('/', (req, res) => {
334
+ res.send('Plugin Auth Proxy Running');
335
  });
336
 
337
+ const PORT = process.env.PORT || 7861; // Changed default port to avoid conflict if running locally
338
  app.listen(PORT, () => {
339
+ console.log(`🚀 Auth Proxy running on http://localhost:${PORT}`);
340
  });