File size: 12,227 Bytes
d75ac2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env node

/**
 * QR Detection Manager for OpenClaw AI
 * MANDATORY QR Wait/Notify Implementation
 * 
 * When WhatsApp login requires QR code scan:
 * - STOP all debug operations
 * - Wait for QR code scan
 * - Clear user prompts
 * - Only continue after successful scan
 */

const fs = require('fs');
const path = require('path');
const { WebSocket } = require('ws');
const readline = require('readline');

class QRDetectionManager {
    constructor() {
        this.ws = null;
        this.isPaused = false;
        this.qrDetected = false;
        this.qrSourcePath = null;
        this.scanCompleted = false;
        this.timeout = null;
        this.qrTimeout = 300000; // 5 minutes timeout
        
        // Setup structured logging
        this.log = (level, message, data = {}) => {
            const logEntry = {
                timestamp: new Date().toISOString(),
                level,
                module: 'qr-detection-manager',
                message,
                ...data
            };
            console.log(JSON.stringify(logEntry));
        };
        
        this.log('info', 'QR Detection Manager initialized');
    }
    
    async connectWebSocket(spaceUrl) {
        try {
            // Handle spaceUrl being just a hostname or full URL
            let host = spaceUrl.replace(/^https?:\/\//, '').replace(/\/$/, '');
            const wsUrl = `wss://${host}`;
            const fullWsUrl = `${wsUrl}/queue/join`;
            
            this.log('info', 'Connecting to WebSocket', { url: fullWsUrl });
            
            this.ws = new WebSocket(fullWsUrl);
            
            this.ws.on('open', () => {
                this.log('info', 'WebSocket connection established');
                this.startMonitoring();
            });
            
            this.ws.on('message', (data) => {
                this.handleWebSocketMessage(data);
            });
            
            this.ws.on('error', (error) => {
                this.log('error', 'WebSocket error', { error: error.message });
            });
            
            this.ws.on('close', () => {
                this.log('info', 'WebSocket connection closed');
            });
            
        } catch (error) {
            this.log('error', 'Failed to connect to WebSocket', { error: error.message });
        }
    }

    handleWebSocketMessage(data) {
        // Placeholder for future WS message handling if needed
        // Currently we rely mostly on log/file monitoring
    }

    startMonitoring() {
        this.log('info', 'Starting QR code monitoring');
        
        // Send initial ping to keep connection alive
        const pingInterval = setInterval(() => {
            if (this.ws && this.ws.readyState === WebSocket.OPEN) {
                this.ws.ping();
            } else {
                clearInterval(pingInterval);
            }
        }, 30000);
        
        // Watch for QR code detection
        this.setupQRDetection();
    }
    
    setupQRDetection() {
        this.log('info', 'Setting up QR code detection');
        
        // Start timeout for QR scan
        this.timeout = setTimeout(() => {
            if (!this.scanCompleted) {
                this.log('warning', 'QR scan timeout reached');
                this.outputQRPrompt('❌ QR scan timeout. Please restart the process.', 'timeout');
                process.exit(1);
            }
        }, this.qrTimeout);
        
        // Monitor for QR code in logs or filesystem
        this.monitorForQR();
    }
    
    monitorForQR() {
        const homeDir = process.env.HOME || '/home/node';
        // Check for QR code file in actual HF Spaces paths
        const qrCheckInterval = setInterval(() => {
            if (this.scanCompleted) {
                clearInterval(qrCheckInterval);
                return;
            }

            // Check actual QR code file locations for HF Spaces OpenClaw
            const qrPaths = [
                path.join(homeDir, '.openclaw/credentials/whatsapp/qr.png'),
                path.join(homeDir, '.openclaw/workspace/qr.png'),
                path.join(homeDir, 'logs/qr.png'),
            ];

            for (const qrPath of qrPaths) {
                if (fs.existsSync(qrPath)) {
                    this.qrSourcePath = qrPath;
                    this.handleQRDetected(qrPath);
                    break;
                }
            }

            // Also check for QR code in recent logs
            this.checkLogsForQR();
        }, 2000); // Check every 2 seconds
    }
    
    checkLogsForQR() {
        try {
            const homeDir = process.env.HOME || '/home/node';
            const logPaths = [
                path.join(homeDir, 'logs/app.log'),
                path.join(homeDir, '.openclaw/workspace/startup.log'),
                path.join(homeDir, '.openclaw/workspace/sync.log'),
            ];
            
            for (const logPath of logPaths) {
                if (fs.existsSync(logPath)) {
                    const logContent = fs.readFileSync(logPath, 'utf8');
                    if (this.isQRInLogContent(logContent)) {
                        this.handleQRDetected('log');
                        break;
                    }
                }
            }
        } catch (error) {
            // Ignore log reading errors
        }
    }
    
    isQRInLogContent(content) {
        // Look for QR-related log entries
        const qrPatterns = [
            /qr code/i,
            /scan.*qr/i,
            /please scan/i,
            /waiting.*qr/i,
            /login.*qr/i,
            /whatsapp.*qr/i,
            /authentication.*qr/i
        ];
        
        return qrPatterns.some(pattern => pattern.test(content));
    }
    
    handleQRDetected(source) {
        if (this.qrDetected) {
            return; // Already detected
        }
        
        this.qrDetected = true;
        this.log('info', 'QR code detected', { source });
        
        // MANDATORY: Stop all debug operations
        this.isPaused = true;
        
        // MANDATORY: Clear user prompts
        this.outputQRPrompt('⏳ Waiting for WhatsApp QR code scan...', 'waiting');
        this.outputQRPrompt('πŸ“± Please scan the QR code with your phone to continue.', 'qr');
        
        // Start monitoring for scan completion
        this.monitorScanCompletion();
    }
    
    outputQRPrompt(message, type) {
        // Clear console for better visibility
        process.stdout.write('\x1b[2J\x1b[0f');
        
        // Output formatted QR prompt
        const separator = '='.repeat(60);
        console.log(`\n${separator}`);
        console.log(`πŸ” WHATSAPP LOGIN REQUIRED`);
        console.log(`${separator}\n`);
        console.log(message);
        console.log(`\n${separator}`);
        
        // Add visual indicators based on type
        if (type === 'waiting') {
            console.log('⏳ Operation paused - waiting for QR scan...');
        } else if (type === 'qr') {
            console.log('πŸ“± Use your WhatsApp app to scan the QR code');
        } else if (type === 'success') {
            console.log('βœ… QR scan completed successfully!');
        } else if (type === 'timeout') {
            console.log('❌ QR scan timeout - please try again');
        }
        
        console.log(`${separator}\n`);
        
        // Also log as JSON for structured processing
        this.log(type === 'success' ? 'info' : 'warning', 'QR prompt output', { 
            message, 
            type,
            isPaused: this.isPaused 
        });
    }
    
    monitorScanCompletion() {
        this.log('info', 'Monitoring for QR scan completion');
        
        // Monitor for scan completion signals
        const completionCheck = setInterval(() => {
            if (this.checkScanCompletion()) {
                clearInterval(completionCheck);
                this.handleScanCompleted();
            }
        }, 1000);
    }
    
    checkScanCompletion() {
        const homeDir = process.env.HOME || '/home/node';

        // 1. Check if QR file was removed (only if we know which file was detected)
        if (this.qrSourcePath && !fs.existsSync(this.qrSourcePath)) {
            return true;
        }

        // 2. Check for successful login in logs
        try {
            const logPaths = [
                path.join(homeDir, 'logs/app.log'),
                path.join(homeDir, '.openclaw/workspace/startup.log'),
                path.join(homeDir, '.openclaw/workspace/sync.log'),
            ];

            for (const logPath of logPaths) {
                if (fs.existsSync(logPath)) {
                    const logContent = fs.readFileSync(logPath, 'utf8');
                    if (this.isLoginInLogContent(logContent)) {
                        return true;
                    }
                }
            }
        } catch (error) {
            // Ignore log reading errors
        }

        // 3. Check for WhatsApp session/creds files in actual HF Spaces paths
        const sessionPaths = [
            path.join(homeDir, '.openclaw/credentials/whatsapp/creds.json'),
            path.join(homeDir, '.openclaw/credentials/whatsapp/session.json'),
        ];

        for (const sessionPath of sessionPaths) {
            if (fs.existsSync(sessionPath)) {
                return true;
            }
        }

        return false;
    }
    
    isLoginInLogContent(content) {
        // Look for successful login patterns
        const loginPatterns = [
            /login.*successful/i,
            /authentication.*success/i,
            /session.*established/i,
            /connected.*whatsapp/i,
            /qr.*scanned/i,
            /scan.*completed/i,
            /user.*authenticated/i
        ];
        
        return loginPatterns.some(pattern => pattern.test(content));
    }
    
    handleScanCompleted() {
        this.scanCompleted = true;
        this.isPaused = false;
        
        // Clear timeout
        if (this.timeout) {
            clearTimeout(this.timeout);
        }
        
        // MANDATORY: Clear success notification
        this.outputQRPrompt('βœ… QR code scanned successfully. Login completed.', 'success');
        
        this.log('info', 'QR scan completed, resuming operations');
        
        // Wait a moment for user to see the success message
        setTimeout(() => {
            // Exit the process to allow main application to continue
            process.exit(0);
        }, 3000);
    }
    
    async waitForQRScan() {
        return new Promise((resolve, reject) => {
            const checkInterval = setInterval(() => {
                if (this.scanCompleted) {
                    clearInterval(checkInterval);
                    resolve();
                }
            }, 1000);
            
            // Timeout after 5 minutes
            setTimeout(() => {
                clearInterval(checkInterval);
                reject(new Error('QR scan timeout'));
            }, this.qrTimeout);
        });
    }
    
    close() {
        if (this.ws) {
            this.ws.close();
        }
        if (this.timeout) {
            clearTimeout(this.timeout);
        }
        this.log('info', 'QR Detection Manager closed');
    }
}

// Command line interface
async function main() {
    const args = process.argv.slice(2);
    const spaceUrl = args[0] || process.env.SPACE_HOST || '';
    
    const manager = new QRDetectionManager();
    
    try {
        await manager.connectWebSocket(spaceUrl);
        
        // Keep the process running
        process.on('SIGINT', () => {
            manager.log('info', 'Received SIGINT, shutting down gracefully');
            manager.close();
            process.exit(0);
        });
        
        process.on('SIGTERM', () => {
            manager.log('info', 'Received SIGTERM, shutting down gracefully');
            manager.close();
            process.exit(0);
        });
        
    } catch (error) {
        manager.log('error', 'QR Detection Manager failed', { error: error.message });
        process.exit(1);
    }
}

if (require.main === module) {
    main();
}

module.exports = QRDetectionManager;