everydaytok commited on
Commit
71eb220
·
verified ·
1 Parent(s): a9cf784

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +31 -21
app.js CHANGED
@@ -593,31 +593,41 @@ app.post('/project/ping', async (req, res) => {
593
  });
594
  */
595
 
596
- app.post('/poll', async (req, res) => {
597
- const { token } = req.body;
598
- if (!token) return res.status(400).json({ error: 'Token required' });
599
-
600
- const decoded = jwt.decode(token);
601
- if (!decoded) return res.status(401).json({ error: 'Malformed token' });
602
-
603
- const secret = await getSessionSecret(decoded.uid, decoded.projectId);
604
- if (!secret) return res.status(404).json({ error: 'Session revoked' });
605
 
606
- try {
607
- jwt.verify(token, secret);
608
- const targetUrl = EXTERNAL_SERVER_URL.replace(/\/$/, '') + '/project/ping';
609
-
610
- // UPDATE: Send isFrontend: true so we get the text snapshot for the plugin UI
611
- const response = await axios.post(targetUrl, {
612
- projectId: decoded.projectId,
613
- userId: decoded.uid,
614
- isFrontend: true
615
- });
 
 
 
 
 
616
 
617
- return res.json(response.data);
618
- } catch (err) { return res.status(403).json({ error: 'Invalid Token' }); }
 
 
 
 
 
 
 
 
 
 
619
  });
620
 
 
621
  app.post('/human/override', validateRequest, async (req, res) => {
622
  const { projectId, instruction, userId } = req.body;
623
  try {
 
593
  });
594
  */
595
 
596
+ app.post('/project/ping', async (req, res) => {
597
+ const { projectId, userId, isFrontend } = req.body;
598
+ if (!projectId || !userId) return res.status(400).json({ error: "Missing IDs" });
 
 
 
 
 
 
599
 
600
+ const project = await StateManager.getProject(projectId);
601
+ if (!project || project.userId !== userId) return res.json({ action: "IDLE" });
602
+
603
+ // 1. BASE RESPONSE (Visuals)
604
+ // Both Frontend and Plugin need this to show "Thinking..." or text generation
605
+ const response = {
606
+ action: "IDLE",
607
+ status: StateManager.getStatus(projectId),
608
+ snapshot: StateManager.getSnapshot(projectId)
609
+ };
610
+
611
+ // 2. EXECUTOR LOGIC (Plugin Only)
612
+ // Only pop commands if this is NOT the passive frontend viewer
613
+ if (!isFrontend) {
614
+ const command = await StateManager.popCommand(projectId);
615
 
616
+ // We don't need 'streamBuffers' anymore since we use 'snapshot' for visuals.
617
+ // But if you have legacy stream logic, we can leave it or remove it.
618
+ // For this hybrid model, snapshot is king.
619
+
620
+ if (command) {
621
+ response.action = command.type;
622
+ response.target = command.payload;
623
+ response.code = command.type === 'EXECUTE' ? command.payload : null;
624
+ }
625
+ }
626
+
627
+ res.json(response);
628
  });
629
 
630
+
631
  app.post('/human/override', validateRequest, async (req, res) => {
632
  const { projectId, instruction, userId } = req.body;
633
  try {