forked from subodhkc/llmverify-npm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.js
More file actions
394 lines (351 loc) · 14.9 KB
/
Copy pathmonitor.js
File metadata and controls
394 lines (351 loc) · 14.9 KB
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
386
387
388
389
390
391
392
393
394
#!/usr/bin/env node
/**
* llmverify Chat Monitor
* Monitors clipboard and verifies AI responses automatically
*/
const http = require('http');
const { exec } = require('child_process');
const { promisify } = require('util');
const execAsync = promisify(exec);
const SERVER_URL = 'http://localhost:9009';
let lastClipboard = '';
// Colors for terminal output
const colors = {
reset: '\x1b[0m',
cyan: '\x1b[36m',
green: '\x1b[32m',
yellow: '\x1b[33m',
red: '\x1b[31m',
gray: '\x1b[90m'
};
function log(message, color = 'reset') {
console.log(`${colors[color]}${message}${colors.reset}`);
}
// Risk level definitions and thresholds
const RISK_LEVELS = {
LOW: { min: 0, max: 25, color: 'green', action: 'Safe to use' },
MODERATE: { min: 26, max: 50, color: 'yellow', action: 'Review recommended' },
HIGH: { min: 51, max: 75, color: 'red', action: 'Fix before using' },
CRITICAL: { min: 76, max: 100, color: 'red', action: 'Do not use' }
};
function explainRisk(result) {
const riskScore = Math.round(result.result.risk.overall * 100 * 10) / 10;
const riskLevel = result.result.risk.level.toUpperCase();
const findings = result.summary.findings || [];
const explanations = [];
// Explain what the score means
const levelInfo = RISK_LEVELS[riskLevel];
explanations.push(`Risk Score ${riskScore}% means: ${levelInfo.action}`);
explanations.push(`Range: ${levelInfo.min}-${levelInfo.max}% is ${riskLevel}`);
// Break down the risk factors
const breakdown = result.result.risk.breakdown || {};
if (breakdown.hallucination > 0.1) {
explanations.push(`Hallucination risk: ${Math.round(breakdown.hallucination * 100)}% - May contain unverified claims`);
}
if (breakdown.consistency < 0.9) {
explanations.push(`Consistency: ${Math.round(breakdown.consistency * 100)}% - Internal contradictions detected`);
}
if (breakdown.security > 0.1) {
explanations.push(`Security risk: ${Math.round(breakdown.security * 100)}% - May contain unsafe content`);
}
// Provide actionable suggestions
const suggestions = [];
if (findings.length > 0) {
suggestions.push('How to lower risk:');
if (findings.some(f => f.type === 'hallucination')) {
suggestions.push(' - Verify factual claims with reliable sources');
suggestions.push(' - Ask AI to cite sources for specific facts');
}
if (findings.some(f => f.type === 'security')) {
suggestions.push(' - Remove or sanitize sensitive information');
suggestions.push(' - Avoid using commands or code without review');
}
if (findings.some(f => f.type === 'consistency')) {
suggestions.push(' - Check for contradictory statements');
suggestions.push(' - Ask AI to clarify conflicting information');
}
if (findings.some(f => f.type === 'pii')) {
suggestions.push(' - Remove personal identifiable information');
suggestions.push(' - Use generic examples instead of real data');
}
} else if (riskScore > 15) {
suggestions.push('How to lower risk:');
suggestions.push(' - Ask AI to be more specific and factual');
suggestions.push(' - Request sources for important claims');
suggestions.push(' - Break complex responses into smaller parts');
}
return { explanations, suggestions };
}
async function checkServer() {
return new Promise((resolve) => {
const req = http.get(`${SERVER_URL}/health`, (res) => {
resolve(res.statusCode === 200);
});
req.on('error', (err) => {
if (err.code === 'ECONNREFUSED') {
log('\nERROR: Cannot connect to server', 'red');
log('', 'reset');
log('The llmverify server is not running.', 'yellow');
log('', 'reset');
log('SOLUTION:', 'cyan');
log(' 1. Open a new terminal', 'gray');
log(' 2. Run: npm run serve', 'gray');
log(' 3. Wait for "Running on http://localhost:9009"', 'gray');
log(' 4. Then restart this monitor', 'gray');
log('', 'reset');
log('If port 9009 is already in use:', 'yellow');
log(' Run: npm run serve:force', 'gray');
log('', 'reset');
} else if (err.code === 'ETIMEDOUT') {
log('\nERROR: Server connection timeout', 'red');
log('', 'reset');
log('The server is not responding.', 'yellow');
log('', 'reset');
log('SOLUTION:', 'cyan');
log(' 1. Check if server is running: npm run serve', 'gray');
log(' 2. Check your firewall settings', 'gray');
log(' 3. Ensure port 9009 is not blocked', 'gray');
log('', 'reset');
} else {
log(`\nERROR: ${err.message}`, 'red');
log('', 'reset');
log('SOLUTION:', 'cyan');
log(' See docs/ERROR-GUIDE.md for detailed troubleshooting', 'gray');
log('', 'reset');
}
resolve(false);
});
req.setTimeout(5000, () => {
req.destroy();
resolve(false);
});
});
}
async function verifyContent(content) {
return new Promise((resolve, reject) => {
const data = JSON.stringify({ content });
const options = {
hostname: 'localhost',
port: 9009,
path: '/verify',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data)
},
timeout: 10000
};
const req = http.request(options, (res) => {
let body = '';
res.on('data', chunk => body += chunk);
res.on('end', () => {
try {
if (body) {
const result = JSON.parse(body);
resolve(result);
} else {
reject(new Error('EMPTY_RESPONSE'));
}
} catch (err) {
reject(new Error('PARSE_ERROR'));
}
});
});
req.on('error', (err) => {
if (err.code === 'ECONNREFUSED') {
reject(new Error('SERVER_NOT_RUNNING'));
} else if (err.code === 'ETIMEDOUT') {
reject(new Error('CONNECTION_TIMEOUT'));
} else if (err.code === 'ECONNRESET') {
reject(new Error('CONNECTION_RESET'));
} else {
reject(new Error(`NETWORK_ERROR: ${err.message}`));
}
});
req.on('timeout', () => {
req.destroy();
reject(new Error('REQUEST_TIMEOUT'));
});
try {
req.write(data);
req.end();
} catch (err) {
reject(new Error(`WRITE_ERROR: ${err.message}`));
}
});
}
async function getClipboard() {
try {
const { stdout } = await execAsync('powershell -command "Get-Clipboard"');
return stdout.trim();
} catch {
return '';
}
}
async function monitor() {
console.clear();
log('╔══════════════════════════════════════════════════════════════════════════════╗', 'cyan');
log('║ llmverify Chat Monitor - AI Response Verification ║', 'cyan');
log('╚══════════════════════════════════════════════════════════════════════════════╝', 'cyan');
log('');
log('Checking server...', 'yellow');
const serverOk = await checkServer();
if (!serverOk) {
log('ERROR: Server not running!', 'red');
log('Start server with: npm run serve', 'yellow');
process.exit(1);
}
log('Server OK', 'green');
log('');
log('╔══════════════════════════════════════════════════════════════════════════════╗', 'green');
log('║ READY - Copy AI responses to see verification scores ║', 'green');
log('╚══════════════════════════════════════════════════════════════════════════════╝', 'green');
log('');
log('Instructions:', 'cyan');
log(' 1. Select AI response in chat');
log(' 2. Copy it (Ctrl+C)');
log(' 3. Verification score appears below automatically');
log('');
log(' Press Ctrl+C to stop', 'gray');
log('');
log('Waiting for AI responses...', 'yellow');
log('');
// Monitor clipboard
setInterval(async () => {
try {
const clipboard = await getClipboard();
if (clipboard && clipboard !== lastClipboard && clipboard.length > 50) {
lastClipboard = clipboard;
log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━', 'gray');
log('VERIFYING: Checking AI response...', 'cyan');
try {
const result = await verifyContent(clipboard);
// Check if server returned an error
if (result.error || !result.result || !result.result.risk) {
const errorMsg = result.error || result.message || 'Unknown error';
log('');
log('ERROR: Server returned an error', 'red');
log('', 'reset');
log(errorMsg, 'yellow');
// Check for content too large error
if (errorMsg.includes('exceeds maximum size') || errorMsg.includes('too large')) {
log('', 'reset');
log('SOLUTION:', 'cyan');
log(' 1. Content is too large to verify at once', 'gray');
log(' 2. Copy and verify smaller sections separately', 'gray');
log(' 3. Break your AI response into chunks', 'gray');
}
log('');
return; // Exit this verification attempt
}
const riskLevel = result.result.risk.level.toUpperCase();
const riskScore = Math.round(result.result.risk.overall * 100 * 10) / 10;
const verdict = result.summary.verdict;
const color = riskLevel === 'LOW' ? 'green' :
riskLevel === 'MODERATE' ? 'yellow' : 'red';
log('');
log('╔══════════════════════════════════════════════════════════════════════════════╗', color);
log('║ VERIFICATION RESULT ║', color);
log('╚══════════════════════════════════════════════════════════════════════════════╝', color);
log('');
log(` Verdict: ${verdict}`, color);
log(` Risk Level: ${riskLevel}`, color);
log(` Risk Score: ${riskScore}%`, color);
log(` Explanation: ${result.summary.explanation}`);
if (result.summary.findings && result.summary.findings.length > 0) {
log('');
log(' Findings:', 'red');
result.summary.findings.forEach(finding => {
log(` - ${finding.message || finding}`, 'red');
});
}
// Add detailed risk explanation
const { explanations, suggestions } = explainRisk(result);
if (explanations.length > 0) {
log('');
log(' Understanding Your Risk Score:', 'cyan');
explanations.forEach(exp => {
log(` ${exp}`, 'gray');
});
}
if (suggestions.length > 0) {
log('');
suggestions.forEach(sug => {
if (sug.startsWith('How to')) {
log(` ${sug}`, 'yellow');
} else {
log(` ${sug}`, 'gray');
}
});
}
log('');
log(` Timestamp: ${new Date().toLocaleString()}`, 'gray');
log('');
} catch (error) {
log('');
log('ERROR: Verification failed', 'red');
log('', 'reset');
const errorMsg = error.message;
if (errorMsg === 'SERVER_NOT_RUNNING') {
log('The server stopped responding.', 'yellow');
log('', 'reset');
log('SOLUTION:', 'cyan');
log(' 1. Check Terminal 1 - is the server still running?', 'gray');
log(' 2. If not, restart it: npm run serve', 'gray');
log(' 3. Then copy the AI response again', 'gray');
} else if (errorMsg === 'CONNECTION_TIMEOUT') {
log('Server took too long to respond.', 'yellow');
log('', 'reset');
log('SOLUTION:', 'cyan');
log(' 1. The content might be too long', 'gray');
log(' 2. Try copying a shorter section', 'gray');
log(' 3. Or wait and try again', 'gray');
} else if (errorMsg === 'CONNECTION_RESET') {
log('Connection was interrupted.', 'yellow');
log('', 'reset');
log('SOLUTION:', 'cyan');
log(' 1. Restart the server: npm run serve:force', 'gray');
log(' 2. Then restart this monitor', 'gray');
} else if (errorMsg === 'REQUEST_TIMEOUT') {
log('Verification took too long (>10 seconds).', 'yellow');
log('', 'reset');
log('SOLUTION:', 'cyan');
log(' 1. Content might be too large', 'gray');
log(' 2. Try verifying smaller sections', 'gray');
log(' 3. Check server performance', 'gray');
} else if (errorMsg === 'EMPTY_RESPONSE') {
log('Server returned no data.', 'yellow');
log('', 'reset');
log('SOLUTION:', 'cyan');
log(' 1. Restart server: npm run serve:force', 'gray');
log(' 2. Check server logs for errors', 'gray');
} else if (errorMsg === 'PARSE_ERROR') {
log('Server response was invalid.', 'yellow');
log('', 'reset');
log('SOLUTION:', 'cyan');
log(' 1. Restart server: npm run serve:force', 'gray');
log(' 2. Update llmverify: npm update llmverify', 'gray');
} else {
log(`${errorMsg}`, 'yellow');
log('', 'reset');
log('SOLUTION:', 'cyan');
log(' See docs/ERROR-GUIDE.md for detailed troubleshooting', 'gray');
}
log('');
}
}
} catch (error) {
// Ignore clipboard errors
}
}, 500);
}
// Handle Ctrl+C
process.on('SIGINT', () => {
log('\nMonitor stopped', 'yellow');
process.exit(0);
});
// Start monitoring
monitor().catch(error => {
log(`ERROR: ${error.message}`, 'red');
process.exit(1);
});