Repository navigation
Expand file tree
/
Copy pathscreenpipe-handler.js
More file actions
372 lines (310 loc) · 12.6 KB
/
Copy pathscreenpipe-handler.js
File metadata and controls
372 lines (310 loc) · 12.6 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
const screenshot = require('screenshot-desktop');
const { QdrantClient } = require('@qdrant/js-client-rest');
const Tesseract = require('tesseract.js');
const sharp = require('sharp');
const { v4: uuidv4 } = require('uuid');
const path = require('path');
const fs = require('fs').promises;
class ScreenpipeHandler {
constructor() {
this.qdrantClient = null;
this.collectionName = 'screenshots';
this.isInitialized = false;
this.screenshotDir = path.join(__dirname, 'screenshots');
this.embeddingModel = 'text-embedding-ada-002';
this.openaiApiKey = null;
}
async initialize(openaiApiKey) {
try {
this.openaiApiKey = openaiApiKey;
// Create screenshots directory if it doesn't exist
await this.ensureScreenshotDir();
// Initialize Qdrant client
this.qdrantClient = new QdrantClient({
url: process.env.QDRANT_URL || 'http://localhost:6333'
});
// Check if Qdrant is running
try {
await this.qdrantClient.getCollections();
console.log('✅ Qdrant connection established');
} catch (error) {
console.error('❌ Failed to connect to Qdrant. Make sure Qdrant is running on http://localhost:6333');
console.error('You can start Qdrant with: docker run -p 6333:6333 qdrant/qdrant');
return false;
}
// Create collection if it doesn't exist
await this.createCollection();
// Initialize Tesseract
await Tesseract.createWorker();
this.isInitialized = true;
console.log('✅ Screenpipe handler initialized successfully');
return true;
} catch (error) {
console.error('❌ Failed to initialize screenpipe handler:', error);
return false;
}
}
async ensureScreenshotDir() {
try {
await fs.access(this.screenshotDir);
} catch {
await fs.mkdir(this.screenshotDir, { recursive: true });
}
}
async createCollection() {
try {
const collections = await this.qdrantClient.getCollections();
const collectionExists = collections.collections.some(
col => col.name === this.collectionName
);
if (!collectionExists) {
await this.qdrantClient.createCollection(this.collectionName, {
vectors: {
size: 1536,
distance: 'Cosine'
}
});
console.log(`✅ Created Qdrant collection: ${this.collectionName}`);
} else {
console.log(`✅ Using existing Qdrant collection: ${this.collectionName}`);
}
} catch (error) {
console.error('❌ Failed to create collection:', error);
throw error;
}
}
async captureScreenshot() {
if (!this.isInitialized) {
throw new Error('Screenpipe handler not initialized');
}
try {
console.log('📸 Capturing screenshot...');
// Capture screenshot using screenshot-desktop
const screenshotBuffer = await screenshot();
// Generate unique filename
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const filename = `screenshot-${timestamp}.png`;
const filepath = path.join(this.screenshotDir, filename);
// Save screenshot
await fs.writeFile(filepath, screenshotBuffer);
console.log(`✅ Screenshot saved: ${filepath}`);
return { filepath, filename, timestamp };
} catch (error) {
console.error('❌ Failed to capture screenshot:', error);
throw error;
}
}
async extractTextFromImage(imagePath) {
try {
console.log('🔍 Extracting text from image...');
// Optimize image for OCR
const optimizedImageBuffer = await sharp(imagePath)
.resize(1920, null, { withoutEnlargement: true })
.png()
.toBuffer();
// Perform OCR
const worker = await Tesseract.createWorker();
await worker.loadLanguage('eng');
await worker.initialize('eng');
const { data: { text } } = await worker.recognize(optimizedImageBuffer);
await worker.terminate();
console.log(`✅ Extracted ${text.length} characters of text`);
return text.trim();
} catch (error) {
console.error('❌ Failed to extract text from image:', error);
return '';
}
}
async generateEmbedding(text) {
if (!text || text.length === 0) {
return null;
}
try {
console.log('🧠 Generating embedding...');
const response = await fetch('https://api.openai.com/v1/embeddings', {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.openaiApiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
input: text,
model: this.embeddingModel
})
});
if (!response.ok) {
throw new Error(`OpenAI API error: ${response.status}`);
}
const data = await response.json();
const embedding = data.data[0].embedding;
console.log(`✅ Generated embedding with ${embedding.length} dimensions`);
return embedding;
} catch (error) {
console.error('❌ Failed to generate embedding:', error);
return null;
}
}
async storeScreenshot(filepath, filename, text, embedding, timestamp) {
if (!embedding) {
console.log('⚠️ No embedding generated, skipping storage');
return null;
}
try {
const id = uuidv4();
const payload = {
id,
filename,
filepath,
text,
timestamp,
created_at: new Date().toISOString()
};
await this.qdrantClient.upsert(this.collectionName, {
points: [{
id,
vector: embedding,
payload
}]
});
console.log(`✅ Stored screenshot in Qdrant with ID: ${id}`);
return id;
} catch (error) {
console.error('❌ Failed to store screenshot in Qdrant:', error);
throw error;
}
}
async captureAndProcess() {
try {
// Capture screenshot
const { filepath, filename, timestamp } = await this.captureScreenshot();
// Extract text
const text = await this.extractTextFromImage(filepath);
// Generate embedding
const embedding = await this.generateEmbedding(text);
// Store in Qdrant
const id = await this.storeScreenshot(filepath, filename, text, embedding, timestamp);
// Clean up old screenshots if we have too many (keep last 1000)
try {
await this.cleanupOldScreenshots(1000);
} catch (cleanupError) {
console.warn('⚠️ Failed to cleanup old screenshots:', cleanupError.message);
}
return {
id,
filepath,
filename,
text,
timestamp,
success: true
};
} catch (error) {
console.error('❌ Failed to capture and process screenshot:', error);
return {
success: false,
error: error.message
};
}
}
async searchScreenshots(query, limit = 5) {
if (!this.isInitialized) {
throw new Error('Screenpipe handler not initialized');
}
try {
console.log(`🔍 Searching screenshots for: "${query}"`);
// Generate embedding for the query
const queryEmbedding = await this.generateEmbedding(query);
if (!queryEmbedding) {
throw new Error('Failed to generate embedding for query');
}
// Search in Qdrant
const searchResult = await this.qdrantClient.search(this.collectionName, {
vector: queryEmbedding,
limit,
with_payload: true,
with_vectors: false
});
console.log(`✅ Found ${searchResult.length} relevant screenshots`);
return searchResult;
} catch (error) {
console.error('❌ Failed to search screenshots:', error);
throw error;
}
}
async cleanupOldScreenshots(maxScreenshots = 1000) {
try {
const collectionInfo = await this.qdrantClient.getCollection(this.collectionName);
const count = collectionInfo.points_count;
if (count <= maxScreenshots) {
return { deleted: 0, remaining: count };
}
const toDelete = count - maxScreenshots;
console.log(`🧹 Cleaning up ${toDelete} old screenshots (keeping ${maxScreenshots} most recent)`);
// Get all points sorted by creation time (oldest first)
const allPoints = await this.qdrantClient.scroll(this.collectionName, {
limit: count,
with_payload: true,
with_vectors: false
});
// Sort by creation time and get oldest ones
const sortedPoints = allPoints.points.sort((a, b) => {
const timeA = new Date(a.payload.created_at).getTime();
const timeB = new Date(b.payload.created_at).getTime();
return timeA - timeB;
});
const pointsToDelete = sortedPoints.slice(0, toDelete);
const idsToDelete = pointsToDelete.map(p => p.id);
// Delete from Qdrant
await this.qdrantClient.delete(this.collectionName, {
points: idsToDelete
});
// Delete files
for (const point of pointsToDelete) {
try {
await fs.unlink(point.payload.filepath);
} catch (fileError) {
console.warn(`⚠️ Failed to delete file: ${point.payload.filepath}`);
}
}
console.log(`✅ Cleaned up ${toDelete} old screenshots`);
return { deleted: toDelete, remaining: maxScreenshots };
} catch (error) {
console.error('❌ Failed to cleanup old screenshots:', error);
throw error;
}
}
async startPeriodicCapture(intervalMinutes = 0.083) { // 5 seconds default
if (!this.isInitialized) {
throw new Error('Screenpipe handler not initialized');
}
const intervalMs = intervalMinutes * 60 * 1000;
const intervalSeconds = intervalMinutes * 60;
console.log(`🔄 Starting frequent screenshot capture every ${intervalSeconds} seconds`);
this.captureInterval = setInterval(async () => {
try {
await this.captureAndProcess();
} catch (error) {
console.error('❌ Periodic capture failed:', error);
}
}, intervalMs);
// Capture immediately
await this.captureAndProcess();
}
stopPeriodicCapture() {
if (this.captureInterval) {
clearInterval(this.captureInterval);
this.captureInterval = null;
console.log('⏹️ Stopped periodic screenshot capture');
}
}
async cleanup() {
this.stopPeriodicCapture();
if (this.qdrantClient) {
try {
await this.qdrantClient.close();
} catch (error) {
console.warn('⚠️ Failed to close Qdrant client:', error.message);
}
}
}
}
module.exports = ScreenpipeHandler;