everydaycats commited on
Commit
0472344
·
verified ·
1 Parent(s): 328efbf

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +51 -23
app.js CHANGED
@@ -5,8 +5,11 @@ const { v4: uuidv4 } = require('uuid');
5
  const axios = require('axios');
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
  // ---------------------------------------------------------
@@ -175,42 +178,34 @@ app.post('/verify', async (req, res) => {
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 });
@@ -224,9 +219,47 @@ app.post('/feedback', async (req, res) => {
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;
@@ -244,7 +277,6 @@ app.post('/poll', async (req, res) => {
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)) {
@@ -252,18 +284,14 @@ app.post('/poll', async (req, res) => {
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
- console.log(targetUrl);
258
 
259
  try {
260
  const response = await axios.post(targetUrl, {
261
- // projectId: verifiedData.projectId
262
  projectId: verifiedData.projectId,
263
  userId: verifiedData.uid
264
  });
265
 
266
- // Return raw response from Main Server (contains action: 'EXECUTE', code: '...')
267
  return res.json(response.data);
268
  } catch (extError) {
269
  console.error("Poll Forward Error:", extError.message);
@@ -337,7 +365,7 @@ app.get('/', (req, res) => {
337
  res.send('Plugin Auth Proxy Running');
338
  });
339
 
340
- const PORT = process.env.PORT || 7860; // Changed default port to avoid conflict if running locally
341
  app.listen(PORT, () => {
342
  console.log(`🚀 Auth Proxy running on http://localhost:${PORT}`);
343
  });
 
5
  const axios = require('axios');
6
  const bodyParser = require('body-parser');
7
 
8
+ // Add CORS if your dashboard is on a different port (optional but recommended for dashboard access)
9
+ const cors = require('cors');
10
+
11
  const app = express();
12
+ app.use(cors()); // Allow Dashboard requests
13
  app.use(bodyParser.json({ limit: '50mb' }));
14
 
15
  // ---------------------------------------------------------
 
178
  // ---------------------------------------------------------
179
 
180
  /**
181
+ * FEEDBACK FORWARDER (PLUGIN)
182
+ * Authenticates via Plugin JWT.
 
183
  */
184
  app.post('/feedback', async (req, res) => {
 
185
  const { token, ...pluginPayload } = req.body;
186
 
187
  if (!token) return res.status(400).json({ error: 'Token required' });
188
 
 
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
  const secret = await getSessionSecret(decoded.uid, decoded.projectId);
195
  if (!secret) return res.status(404).json({ error: 'Session revoked' });
196
 
197
  try {
198
+ jwt.verify(token, secret);
199
 
 
200
  const externalBase = process.env.EXTERNAL_SERVER_URL || 'http://localhost:7860';
201
  const targetUrl = externalBase.replace(/\/$/, '') + '/project/feedback';
202
 
203
+ console.log(`📨 Forwarding PLUGIN feedback for ${decoded.projectId} (${decoded.uid})`);
204
 
 
 
 
205
  const response = await axios.post(targetUrl, {
206
+ userId: decoded.uid,
207
+ projectId: decoded.projectId,
208
+ ...pluginPayload
209
  });
210
 
211
  return res.json({ success: true, externalResponse: response.data });
 
219
  }
220
  });
221
 
222
+ /**
223
+ * FEEDBACK FORWARDER (DASHBOARD)
224
+ * Authenticates via Firebase ID Token (Header).
225
+ * Endpoint: /feedback2
226
+ */
227
+ app.post('/feedback2', verifyFirebaseUser, async (req, res) => {
228
+ const { projectId, prompt, ...otherPayload } = req.body;
229
+ const userId = req.user.uid; // Verified from Firebase Token
230
+
231
+ if (!projectId || !prompt) {
232
+ return res.status(400).json({ error: 'Missing projectId or prompt' });
233
+ }
234
+
235
+ const externalBase = process.env.EXTERNAL_SERVER_URL || 'http://localhost:7860';
236
+ const targetUrl = externalBase.replace(/\/$/, '') + '/project/feedback';
237
+
238
+ console.log(`📨 Forwarding DASHBOARD feedback for ${projectId} (User: ${userId})`);
239
+
240
+ try {
241
+ // We inject the authenticated userId here so the Core server knows
242
+ // it was verified by the Auth Proxy.
243
+ const response = await axios.post(targetUrl, {
244
+ userId: userId,
245
+ projectId: projectId,
246
+ prompt: prompt,
247
+ ...otherPayload // images, etc.
248
+ });
249
+
250
+ return res.json({ success: true, externalResponse: response.data });
251
+
252
+ } catch (err) {
253
+ console.error("Dashboard Feedback Forward Error:", err.message);
254
+ if (err.response) {
255
+ return res.status(err.response.status).json(err.response.data);
256
+ }
257
+ return res.status(502).json({ error: 'Failed to forward feedback to Main AI server' });
258
+ }
259
+ });
260
+
261
  /**
262
  * POLLING FORWARDER
 
263
  */
264
  app.post('/poll', async (req, res) => {
265
  const { token } = req.body;
 
277
  try {
278
  const verifiedData = jwt.verify(token, secret);
279
 
 
280
  const threeDaysInSeconds = 3 * 24 * 60 * 60;
281
  const nowInSeconds = Math.floor(Date.now() / 1000);
282
  if (verifiedData.iat && (nowInSeconds - verifiedData.iat > threeDaysInSeconds)) {
 
284
  }
285
 
286
  const externalBase = process.env.EXTERNAL_SERVER_URL || 'http://localhost:7860';
 
287
  const targetUrl = externalBase.replace(/\/$/, '') + '/project/ping';
 
288
 
289
  try {
290
  const response = await axios.post(targetUrl, {
 
291
  projectId: verifiedData.projectId,
292
  userId: verifiedData.uid
293
  });
294
 
 
295
  return res.json(response.data);
296
  } catch (extError) {
297
  console.error("Poll Forward Error:", extError.message);
 
365
  res.send('Plugin Auth Proxy Running');
366
  });
367
 
368
+ const PORT = process.env.PORT || 7860;
369
  app.listen(PORT, () => {
370
  console.log(`🚀 Auth Proxy running on http://localhost:${PORT}`);
371
  });