File size: 10,941 Bytes
a216373
 
 
 
d84a7e0
1b2b030
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ecc8d1
 
 
 
 
1b2b030
 
 
 
 
 
e91478b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b2b030
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7722eb9
1b2b030
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ecc8d1
7722eb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ecc8d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7722eb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ecc8d1
7722eb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ecc8d1
 
 
 
 
 
 
 
 
 
7722eb9
 
 
 
 
 
 
1b2b030
 
 
 
 
 
 
 
 
 
 
 
7722eb9
1b2b030
 
 
 
7722eb9
1b2b030
 
 
 
 
 
 
 
 
 
7722eb9
1b2b030
 
 
490b09b
 
1b2b030
 
 
 
 
 
e91478b
490b09b
 
 
 
e91478b
490b09b
 
 
a216373
 
 
490b09b
 
 
a216373
 
 
490b09b
a216373
e545be1
bed4e30
a216373
bed4e30
a216373
e545be1
a216373
bed4e30
 
e545be1
a216373
 
 
 
 
 
bed4e30
7722eb9
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
294
295
296
297
import express from 'express';
import { createServer } from 'http';
import { WebSocketServer, WebSocket } from 'ws';
import cors from 'cors';
import jwt from 'jsonwebtoken';
import { createClient } from '@supabase/supabase-js';

const PORT = 7860;
const SUPABASE_URL = process.env.SUPABASE_URL;
const SUPABASE_KEY = process.env.SUPABASE_SERVICE_KEY;
const CORE_URL = process.env.CORE_URL || "http://localhost:7862"; 

if (!SUPABASE_URL) { console.error("❌ Config Missing"); process.exit(1); }

const app = express();
const server = createServer(app);
const wss = new WebSocketServer({ noServer: true });
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);

const clients = new Map(); // UserId -> Set<WebSocket>

app.use(cors());
app.use(express.json({ limit: '50mb' }));

app.get('/', (req, res) => res.send('Gateway Active'));

// Internal Notification Webhook
app.post('/internal/notify', (req, res) => {
    const { user_id, type, message } = req.body;
    if (clients.has(user_id)) {
        clients.get(user_id).forEach(ws => {
            if (ws.readyState === WebSocket.OPEN) {
                ws.send(JSON.stringify({ type, message }));
                // Ensure UI reloads on significant notifications
                if (type === 'toast') ws.send(JSON.stringify({ type: 'reload_project' }));
            }
        });
        return res.json({ success: true });
    }
    res.json({ success: false });
});

// NEW: Internal MCP Query Webhook (Routes AI requests down to the local daemon)
app.post('/internal/mcp_query', (req, res) => {
    const { user_id, lead_id, payload } = req.body;
    if (clients.has(user_id)) {
        clients.get(user_id).forEach(ws => {
            if (ws.readyState === WebSocket.OPEN) {
                // Forward the exact payload to the daemon
                ws.send(JSON.stringify({ type: 'mcp_query', payload }));
            }
        });
        return res.json({ success: true });
    }
    res.json({ success: false, message: "User daemon not connected" });
});

// Verify JWT against DB Secret
async function verifyThrustToken(token) {
    const decoded = jwt.decode(token);
    if (!decoded || !decoded.sid) return null;

    const { data: session } = await supabase
        .from('user_sessions')
        .select('session_secret')
        .eq('id', decoded.sid)
        .single();

    if (!session) return null; 

    try {
        return jwt.verify(token, session.session_secret);
    } catch (e) {
        return null;
    }
}

// --- FETCH PAGINATED PROJECTS VIA JWT ---
app.get('/api/projects', async (req, res) => {
    const authHeader = req.headers.authorization;
    if (!authHeader?.startsWith('Bearer ')) return res.status(401).json({ error: 'Unauthorized' });

    const token = authHeader.split(' ')[1];
    const decoded = await verifyThrustToken(token);
    
    if (!decoded || !decoded.uid) return res.status(403).json({ error: 'Invalid Token' });

    // Pagination Logic (Default: 9 items per page for a 3x3 grid)
    const page = parseInt(req.query.page) || 1;
    const limit = parseInt(req.query.limit) || 9;
    const start = (page - 1) * limit;
    const end = start + limit - 1;

    const { data, count, error } = await supabase
        .from('leads')
        .select('*', { count: 'exact' })
        .eq('user_id', decoded.uid)
        .order('created_at', { ascending: false })
        .range(start, end);

    if (error) return res.status(500).json({ error: error.message });

    res.json({
        projects: data,
        total: count,
        page: page,
        totalPages: Math.ceil(count / limit) || 1
    });
});

// --- FETCH ACTIVE THRUST & TASKS (For backward compatibility) ---
app.get('/api/projects/:projectId/thrusts/active', async (req, res) => {
    const authHeader = req.headers.authorization;
    if (!authHeader?.startsWith('Bearer ')) return res.status(401).json({ error: 'Unauthorized' });

    const token = authHeader.split(' ')[1];
    const decoded = await verifyThrustToken(token);
    if (!decoded || !decoded.uid) return res.status(403).json({ error: 'Invalid Token' });

    const { projectId } = req.params;

    const { data, error } = await supabase
        .from('thrusts')
        .select('*, thrust_tasks(*)')
        .eq('lead_id', projectId)
        .eq('status', 'active')
        .order('created_at', { ascending: false })
        .limit(1);

    if (error) return res.status(500).json({ error: error.message });
    res.json(data);
});

// --- NEW: COMBINED MCP CONTEXT FETCH ---
app.get('/api/projects/:projectId/mcp-context', async (req, res) => {
    const authHeader = req.headers.authorization;
    if (!authHeader?.startsWith('Bearer ')) return res.status(401).json({ error: 'Unauthorized' });

    const token = authHeader.split(' ')[1];
    const decoded = await verifyThrustToken(token);
    if (!decoded || !decoded.uid) return res.status(403).json({ error: 'Invalid Token' });

    const { projectId } = req.params;
    const { prd, thrust, timeline } = req.query;

    let result = {};

    try {
        if (prd === 'true') {
            const { data } = await supabase.from('leads').select('requirements_doc').eq('id', projectId).single();
            result.prd = data?.requirements_doc || null;
        }

        if (thrust === 'true') {
            const { data } = await supabase.from('thrusts').select('*, tasks:thrust_tasks(*)').eq('lead_id', projectId).eq('status', 'active').order('created_at', { ascending: false }).limit(1).single();
            result.thrust = data || null;
        }

        if (timeline === 'true') {
            const { data } = await supabase.from('timeline_events').select('*').eq('lead_id', projectId).order('created_at', { ascending: false }).limit(20);
            result.timeline = data || [];
        }

        res.json(result);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

// --- MARK TASK COMPLETE & LOG TO TIMELINE ---
app.post('/api/projects/:projectId/tasks/:taskId/complete', async (req, res) => {
    const authHeader = req.headers.authorization;
    if (!authHeader?.startsWith('Bearer ')) return res.status(401).json({ error: 'Unauthorized' });

    const token = authHeader.split(' ')[1];
    const decoded = await verifyThrustToken(token);
    if (!decoded || !decoded.uid) return res.status(403).json({ error: 'Invalid Token' });

    const { projectId, taskId } = req.params;
    const { taskTitle } = req.body;

    try {
        // 1. Update Task Status
        const { error: updateError } = await supabase
            .from('thrust_tasks')
            .update({ is_completed: true, status: 'done' })
            .eq('id', taskId);

        if (updateError) throw updateError;

        // 2. Add to Timeline so AI sees it on next context sync
        const { error: timelineError } = await supabase
            .from('timeline_events')
            .insert({
                lead_id: projectId,
                title: "Task Completed",
                description: `User manually completed: ${taskTitle || 'a task'}`,
                type: "chore"
            });

        if (timelineError) throw timelineError;

        // 3. Notify the local WebSockets to show a toast and reload!
        if (clients.has(decoded.uid)) {
            clients.get(decoded.uid).forEach(ws => {
                if (ws.readyState === WebSocket.OPEN) {
                    ws.send(JSON.stringify({ type: 'toast', message: `✅ Task Completed: ${taskTitle || 'a task'}` }));
                    ws.send(JSON.stringify({ type: 'reload_project' }));
                }
            });
        }

        res.json({ success: true });
    } catch (error) {
        console.error("Task Completion Error:", error.message);
        res.status(500).json({ error: error.message });
    }
});

// WS Upgrade
server.on('upgrade', async (request, socket, head) => {
    const url = new URL(request.url, `http://${request.headers.host}`);
    const token = url.searchParams.get('token');

    if (!token) { socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n'); socket.destroy(); return; }

    const decodedData = await verifyThrustToken(token);

    if (!decodedData) { socket.write('HTTP/1.1 403 Forbidden\r\n\r\n'); socket.destroy(); return; }
  
    wss.handleUpgrade(request, socket, head, (ws) => {
        wss.emit('connection', ws, request,  decodedData.uid);
    });
});

// WS Logic
wss.on('connection', (ws, req, userId) => {
    if (!clients.has(userId)) clients.set(userId, new Set());
    clients.get(userId).add(ws);

    ws.isAlive = true;
    ws.on('pong', () => { ws.isAlive = true; });

    ws.on('message', async (message) => {
        try {
            const data = JSON.parse(message.toString());

            // 1. CHAT PROMPTS & OVERRIDES
            if (data.type === 'prompt') {
                ws.send(JSON.stringify({ type: 'status', status: 'thinking' }));
                const response = await fetch(`${CORE_URL}/process`, {
                    method: 'POST', headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ userId, projectId: data.projectId, prompt: data.content, context: data.context, task_type: 'chat' })
                });
                if (!response.ok) throw new Error("Core API Failed");
                const result = await response.json();
                ws.send(JSON.stringify({ type: 'response', text: result.text, should_reload: result.should_reload, usage: result.usage }));
            }

            // 2. BACKGROUND CONTEXT SYNC (SMART DEBOUNCED)
            if (data.type === 'context_sync') {
                const payloadData = data.data;
                const currentTime = new Date().toLocaleString(); // Anchor time for the AI
                
                const formattedPrompt = `[WORKSPACE UPDATE - CURRENT TIME: ${currentTime}]\n\nActivity Log (Recent Activity):\n${payloadData.buffer}\n\nGit Diffs (Modified Files):\n${payloadData.diffs}\n\nNew Untracked Files:\n${payloadData.new_files}`;
                
                await fetch(`${CORE_URL}/process`, {
                    method: 'POST', headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        userId: userId,
                        projectId: data.projectId,
                        prompt: formattedPrompt,
                        images: payloadData.images, 
                        task_type: 'log_ingestion'
                    })
                });
            }

        } catch (e) {
            console.error("WS Error", e);
        }
    });

    ws.on('close', () => {
        if (clients.has(userId)) clients.get(userId).delete(ws);
    });
});

setInterval(() => {
    wss.clients.forEach((ws) => {
        if (ws.isAlive === false) return ws.terminate();
        ws.isAlive = false;
        ws.ping();
    });
}, 30000);

server.listen(PORT, () => console.log(`🚀 Gateway on ${PORT}`));