superbased 1.5.0 → 1.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/SUPERBASED_SKILL.md +38 -2
- package/dist/electron/core/platform.d.ts +2 -0
- package/dist/electron/core/platform.d.ts.map +1 -1
- package/dist/electron/main.js +95 -13
- package/dist/electron/modules/active-window.d.ts.map +1 -1
- package/dist/electron/modules/active-window.js +8 -0
- package/dist/electron/modules/active-window.js.map +1 -1
- package/dist/electron/modules/api-server.d.ts.map +1 -1
- package/dist/electron/modules/api-server.js +232 -4
- package/dist/electron/modules/api-server.js.map +1 -1
- package/dist/electron/modules/azure-speech-realtime.d.ts +13 -3
- package/dist/electron/modules/azure-speech-realtime.d.ts.map +1 -1
- package/dist/electron/modules/azure-speech-realtime.js +187 -54
- package/dist/electron/modules/azure-speech-realtime.js.map +1 -1
- package/dist/electron/modules/capture.d.ts.map +1 -1
- package/dist/electron/modules/capture.js +27 -8
- package/dist/electron/modules/capture.js.map +1 -1
- package/dist/electron/modules/dictation.d.ts +2 -0
- package/dist/electron/modules/dictation.d.ts.map +1 -1
- package/dist/electron/modules/dictation.js +145 -20
- package/dist/electron/modules/dictation.js.map +1 -1
- package/dist/electron/modules/ocr.d.ts.map +1 -1
- package/dist/electron/modules/ocr.js +15 -3
- package/dist/electron/modules/ocr.js.map +1 -1
- package/dist/electron/modules/recording/capture-queue.d.ts.map +1 -1
- package/dist/electron/modules/recording/capture-queue.js +43 -10
- package/dist/electron/modules/recording/capture-queue.js.map +1 -1
- package/dist/electron/modules/recording/index.d.ts.map +1 -1
- package/dist/electron/modules/recording/index.js +12 -6
- package/dist/electron/modules/recording/index.js.map +1 -1
- package/dist/electron/modules/recording/monitor-analyzer.d.ts +40 -0
- package/dist/electron/modules/recording/monitor-analyzer.d.ts.map +1 -0
- package/dist/electron/modules/recording/monitor-analyzer.js +220 -0
- package/dist/electron/modules/recording/monitor-analyzer.js.map +1 -0
- package/dist/electron/modules/recording/providers/change-detect-provider.d.ts +28 -0
- package/dist/electron/modules/recording/providers/change-detect-provider.d.ts.map +1 -0
- package/dist/electron/modules/recording/providers/change-detect-provider.js +181 -0
- package/dist/electron/modules/recording/providers/change-detect-provider.js.map +1 -0
- package/dist/electron/modules/recording/providers/provider-factory.d.ts +8 -2
- package/dist/electron/modules/recording/providers/provider-factory.d.ts.map +1 -1
- package/dist/electron/modules/recording/providers/provider-factory.js +33 -12
- package/dist/electron/modules/recording/providers/provider-factory.js.map +1 -1
- package/dist/electron/modules/recording/providers/uiohook-provider.d.ts.map +1 -1
- package/dist/electron/modules/recording/providers/uiohook-provider.js +9 -5
- package/dist/electron/modules/recording/providers/uiohook-provider.js.map +1 -1
- package/dist/electron/modules/recording/recording-session.d.ts +5 -1
- package/dist/electron/modules/recording/recording-session.d.ts.map +1 -1
- package/dist/electron/modules/recording/recording-session.js +78 -6
- package/dist/electron/modules/recording/recording-session.js.map +1 -1
- package/dist/electron/modules/recording/trigger-engine.d.ts.map +1 -1
- package/dist/electron/modules/recording/trigger-engine.js +10 -3
- package/dist/electron/modules/recording/trigger-engine.js.map +1 -1
- package/dist/electron/modules/store.js +2 -2
- package/dist/electron/modules/store.js.map +1 -1
- package/dist/electron/modules/uiohook-manager.d.ts +19 -0
- package/dist/electron/modules/uiohook-manager.d.ts.map +1 -0
- package/dist/electron/modules/uiohook-manager.js +101 -0
- package/dist/electron/modules/uiohook-manager.js.map +1 -0
- package/dist/headless/electron/modules/api-server.js +95 -1
- package/dist/headless/electron/modules/capture.js +27 -8
- package/dist/headless/electron/modules/dictation.js +23 -20
- package/dist/headless/electron/modules/ocr.js +15 -3
- package/dist/headless/electron/modules/recording/capture-queue.js +43 -10
- package/dist/headless/electron/modules/recording/index.js +12 -6
- package/dist/headless/electron/modules/recording/monitor-analyzer.js +219 -0
- package/dist/headless/electron/modules/recording/providers/change-detect-provider.js +180 -0
- package/dist/headless/electron/modules/recording/providers/provider-factory.js +33 -12
- package/dist/headless/electron/modules/recording/providers/uiohook-provider.js +9 -5
- package/dist/headless/electron/modules/recording/recording-session.js +78 -6
- package/dist/headless/electron/modules/recording/trigger-engine.js +10 -3
- package/dist/headless/electron/modules/store.js +2 -2
- package/dist/headless/electron/modules/uiohook-manager.js +100 -0
- package/dist/headless/mic-capture.d.ts +21 -1
- package/dist/headless/mic-capture.d.ts.map +1 -1
- package/dist/headless/mic-capture.js +95 -1
- package/dist/headless/mic-capture.js.map +1 -1
- package/dist/headless/shared/constants.js +1 -1
- package/dist/shared/constants.d.ts +1 -1
- package/dist/shared/constants.js +1 -1
- package/dist/shared/constants.js.map +1 -1
- package/dist/shared/recording-types.d.ts +12 -0
- package/dist/shared/recording-types.d.ts.map +1 -1
- package/mcp-server/dist/api-client.js +9 -0
- package/mcp-server/dist/index.js +87 -2
- package/package.json +4 -4
|
@@ -45,12 +45,36 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
45
45
|
exports.CaptureQueue = void 0;
|
|
46
46
|
const path = __importStar(require("path"));
|
|
47
47
|
const fs = __importStar(require("fs"));
|
|
48
|
-
const electron_1 = require("electron");
|
|
49
48
|
const logger_1 = __importDefault(require("../../logger"));
|
|
50
49
|
const active_window_1 = require("../active-window");
|
|
51
|
-
const
|
|
50
|
+
const platform_1 = require("../../core/platform");
|
|
52
51
|
const redact_1 = require("../redact");
|
|
53
|
-
|
|
52
|
+
// Lazy Electron nativeImage — only available in desktop mode
|
|
53
|
+
let nativeImage = null;
|
|
54
|
+
try {
|
|
55
|
+
nativeImage = require('electron').nativeImage;
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
// Headless mode — nativeImage not available, sharp required
|
|
59
|
+
}
|
|
60
|
+
// Platform-aware getSetting
|
|
61
|
+
function getSetting(key) {
|
|
62
|
+
if ((0, platform_1.getPlatform)().isElectron()) {
|
|
63
|
+
const { getSetting: s } = require('../store');
|
|
64
|
+
return s(key);
|
|
65
|
+
}
|
|
66
|
+
const { getSetting: s } = require('../../core/json-store');
|
|
67
|
+
return s(key);
|
|
68
|
+
}
|
|
69
|
+
// Platform-aware captureScreen
|
|
70
|
+
async function captureScreenPlatform() {
|
|
71
|
+
if ((0, platform_1.getPlatform)().isElectron()) {
|
|
72
|
+
const { captureScreen } = require('../capture');
|
|
73
|
+
return captureScreen();
|
|
74
|
+
}
|
|
75
|
+
const screenshot = require('screenshot-desktop');
|
|
76
|
+
return screenshot({ format: 'png' });
|
|
77
|
+
}
|
|
54
78
|
// Session limits
|
|
55
79
|
const MAX_FRAMES_DEFAULT = 2000;
|
|
56
80
|
const MAX_DURATION_DEFAULT = 60 * 60 * 1000; // 1 hour
|
|
@@ -159,7 +183,7 @@ class CaptureQueue {
|
|
|
159
183
|
try {
|
|
160
184
|
// Take screenshot — the expensive part (~800-1200ms)
|
|
161
185
|
const t0 = Date.now();
|
|
162
|
-
const fullBuffer = await (
|
|
186
|
+
const fullBuffer = await captureScreenPlatform();
|
|
163
187
|
logger_1.default.info(`[CaptureQueue] captureScreen took ${Date.now() - t0}ms (inFlight=${this.inFlightCaptures})`);
|
|
164
188
|
// Get active window info (while screenshot was being taken, window may have changed — grab it now)
|
|
165
189
|
let windowTitle = null;
|
|
@@ -226,8 +250,8 @@ class CaptureQueue {
|
|
|
226
250
|
const frameNum = this.frameCounter;
|
|
227
251
|
const paddedNum = String(frameNum).padStart(4, '0');
|
|
228
252
|
// Auto-redact sensitive content if enabled (runs inline before encode)
|
|
229
|
-
const redactSecrets = !!
|
|
230
|
-
const redactPII = !!
|
|
253
|
+
const redactSecrets = !!getSetting('autoRedactSecrets');
|
|
254
|
+
const redactPII = !!getSetting('autoRedactPII');
|
|
231
255
|
if (redactSecrets || redactPII) {
|
|
232
256
|
const redactResult = await (0, redact_1.autoRedact)(frame.buffer, { secrets: redactSecrets, pii: redactPII });
|
|
233
257
|
if (redactResult.regionsFound > 0) {
|
|
@@ -255,9 +279,9 @@ class CaptureQueue {
|
|
|
255
279
|
.toBuffer();
|
|
256
280
|
thumbExt = 'webp';
|
|
257
281
|
}
|
|
258
|
-
else {
|
|
282
|
+
else if (nativeImage) {
|
|
259
283
|
// nativeImage fallback: resize to 256px wide, use JPEG
|
|
260
|
-
const img =
|
|
284
|
+
const img = nativeImage.createFromBuffer(frame.buffer);
|
|
261
285
|
const size = img.getSize();
|
|
262
286
|
const scale = 256 / Math.max(size.width, size.height);
|
|
263
287
|
const resized = img.resize({
|
|
@@ -267,6 +291,11 @@ class CaptureQueue {
|
|
|
267
291
|
thumbBuffer = resized.toJPEG(60);
|
|
268
292
|
thumbExt = 'jpg';
|
|
269
293
|
}
|
|
294
|
+
else {
|
|
295
|
+
// No sharp and no nativeImage — save raw PNG as thumbnail
|
|
296
|
+
thumbBuffer = frame.buffer;
|
|
297
|
+
thumbExt = 'png';
|
|
298
|
+
}
|
|
270
299
|
const thumbName = `frame_${paddedNum}_thumb.${thumbExt}`;
|
|
271
300
|
const thumbnailPath = path.join(this.sessionDir, 'thumbnails', thumbName);
|
|
272
301
|
fs.writeFileSync(thumbnailPath, thumbBuffer);
|
|
@@ -297,7 +326,9 @@ class CaptureQueue {
|
|
|
297
326
|
}
|
|
298
327
|
/** Resize using nativeImage and return raw BGRA bitmap for comparison */
|
|
299
328
|
resizeWithNativeImage(buffer, width, height) {
|
|
300
|
-
|
|
329
|
+
if (!nativeImage)
|
|
330
|
+
throw new Error('nativeImage not available in headless mode');
|
|
331
|
+
const img = nativeImage.createFromBuffer(buffer);
|
|
301
332
|
const resized = img.resize({ width, height });
|
|
302
333
|
return resized.toBitmap();
|
|
303
334
|
}
|
|
@@ -321,7 +352,9 @@ class CaptureQueue {
|
|
|
321
352
|
}
|
|
322
353
|
/** Encode frame using nativeImage (JPEG/PNG only — no webp support) */
|
|
323
354
|
encodeFrameNative(buffer) {
|
|
324
|
-
|
|
355
|
+
if (!nativeImage)
|
|
356
|
+
throw new Error('nativeImage not available in headless mode — sharp is required');
|
|
357
|
+
const img = nativeImage.createFromBuffer(buffer);
|
|
325
358
|
switch (this.config.captureFormat) {
|
|
326
359
|
case 'webp':
|
|
327
360
|
case 'jpeg': {
|
|
@@ -47,7 +47,7 @@ exports.getCaptureQueue = getCaptureQueue;
|
|
|
47
47
|
exports.getSessionModule = getSessionModule;
|
|
48
48
|
exports.getMcpToolModule = getMcpToolModule;
|
|
49
49
|
const provider_factory_1 = require("./providers/provider-factory");
|
|
50
|
-
const
|
|
50
|
+
const platform_1 = require("../../core/platform");
|
|
51
51
|
const logger_1 = __importDefault(require("../../logger"));
|
|
52
52
|
let hookProvider = null;
|
|
53
53
|
let providerInfo = null;
|
|
@@ -57,11 +57,17 @@ let captureQueueModule = null;
|
|
|
57
57
|
let sessionModule = null;
|
|
58
58
|
let mcpToolModule = null;
|
|
59
59
|
async function initialize() {
|
|
60
|
-
// On macOS, request Accessibility permission
|
|
61
|
-
if (process.platform === 'darwin') {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
60
|
+
// On macOS in Electron, request Accessibility permission
|
|
61
|
+
if (process.platform === 'darwin' && (0, platform_1.getPlatform)().isElectron()) {
|
|
62
|
+
try {
|
|
63
|
+
const { systemPreferences } = require('electron');
|
|
64
|
+
const trusted = systemPreferences.isTrustedAccessibilityClient(true);
|
|
65
|
+
if (!trusted) {
|
|
66
|
+
return { success: false, providerType: 'none', fallbackReason: 'Accessibility permission denied on macOS' };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
// Headless — skip accessibility check
|
|
65
71
|
}
|
|
66
72
|
}
|
|
67
73
|
// Create the input hook provider (uiohook → periodic fallback)
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// MonitorAnalyzer — AI analysis loop for monitor mode recording.
|
|
3
|
+
// Receives frames from CaptureQueue, batches by count/time, sends to AI for analysis,
|
|
4
|
+
// stores results on frame records, and emits alerts for MCP notifications.
|
|
5
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
10
|
+
}
|
|
11
|
+
Object.defineProperty(o, k2, desc);
|
|
12
|
+
}) : (function(o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
o[k2] = m[k];
|
|
15
|
+
}));
|
|
16
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
17
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
18
|
+
}) : function(o, v) {
|
|
19
|
+
o["default"] = v;
|
|
20
|
+
});
|
|
21
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
22
|
+
var ownKeys = function(o) {
|
|
23
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
24
|
+
var ar = [];
|
|
25
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
26
|
+
return ar;
|
|
27
|
+
};
|
|
28
|
+
return ownKeys(o);
|
|
29
|
+
};
|
|
30
|
+
return function (mod) {
|
|
31
|
+
if (mod && mod.__esModule) return mod;
|
|
32
|
+
var result = {};
|
|
33
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
34
|
+
__setModuleDefault(result, mod);
|
|
35
|
+
return result;
|
|
36
|
+
};
|
|
37
|
+
})();
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.MonitorAnalyzer = void 0;
|
|
43
|
+
const fs = __importStar(require("fs"));
|
|
44
|
+
const http = __importStar(require("http"));
|
|
45
|
+
const logger_1 = __importDefault(require("../../logger"));
|
|
46
|
+
class MonitorAnalyzer {
|
|
47
|
+
config;
|
|
48
|
+
sessionId;
|
|
49
|
+
onAlert;
|
|
50
|
+
framesSinceAnalysis = 0;
|
|
51
|
+
lastAnalysisTime = 0;
|
|
52
|
+
analyzing = false;
|
|
53
|
+
pendingFrames = [];
|
|
54
|
+
destroyed = false;
|
|
55
|
+
alertCount = 0;
|
|
56
|
+
lastAnalysis = null;
|
|
57
|
+
constructor(config, sessionId, onAlert) {
|
|
58
|
+
this.config = config;
|
|
59
|
+
this.sessionId = sessionId;
|
|
60
|
+
this.onAlert = onAlert;
|
|
61
|
+
logger_1.default.info(`[MonitorAnalyzer] Initialized for session ${sessionId} — analyzeEvery=${config.analyzeEvery}, analyzeInterval=${config.analyzeInterval}s`);
|
|
62
|
+
}
|
|
63
|
+
async onFrame(frame) {
|
|
64
|
+
if (this.destroyed)
|
|
65
|
+
return;
|
|
66
|
+
this.framesSinceAnalysis++;
|
|
67
|
+
this.pendingFrames.push(frame);
|
|
68
|
+
// Keep only the last N frames to avoid unbounded memory growth
|
|
69
|
+
if (this.pendingFrames.length > this.config.analyzeEvery * 2) {
|
|
70
|
+
this.pendingFrames = this.pendingFrames.slice(-this.config.analyzeEvery);
|
|
71
|
+
}
|
|
72
|
+
const elapsed = (Date.now() - this.lastAnalysisTime) / 1000;
|
|
73
|
+
const shouldAnalyze = this.framesSinceAnalysis >= this.config.analyzeEvery &&
|
|
74
|
+
elapsed >= this.config.analyzeInterval;
|
|
75
|
+
if (!shouldAnalyze || this.analyzing)
|
|
76
|
+
return;
|
|
77
|
+
this.analyzing = true;
|
|
78
|
+
try {
|
|
79
|
+
await this.analyzeLatestFrame();
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
logger_1.default.error('[MonitorAnalyzer] Analysis failed:', err);
|
|
83
|
+
}
|
|
84
|
+
finally {
|
|
85
|
+
this.analyzing = false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async analyzeLatestFrame() {
|
|
89
|
+
// Pick the most recent frame for analysis
|
|
90
|
+
const frame = this.pendingFrames[this.pendingFrames.length - 1];
|
|
91
|
+
if (!frame)
|
|
92
|
+
return;
|
|
93
|
+
logger_1.default.info(`[MonitorAnalyzer] Analyzing frame #${frame.frameNumber} (${this.framesSinceAnalysis} frames since last analysis)`);
|
|
94
|
+
// Read the frame image and resize for AI
|
|
95
|
+
if (!fs.existsSync(frame.framePath)) {
|
|
96
|
+
logger_1.default.warn(`[MonitorAnalyzer] Frame file not found: ${frame.framePath}`);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const imageBuffer = fs.readFileSync(frame.framePath);
|
|
100
|
+
const resizedBase64 = await this.resizeForAI(imageBuffer);
|
|
101
|
+
// Call AI via local API
|
|
102
|
+
const analysis = await this.callAI(resizedBase64, frame);
|
|
103
|
+
if (!analysis || this.destroyed)
|
|
104
|
+
return;
|
|
105
|
+
// Reset counters
|
|
106
|
+
this.framesSinceAnalysis = 0;
|
|
107
|
+
this.lastAnalysisTime = Date.now();
|
|
108
|
+
this.pendingFrames = [];
|
|
109
|
+
// Store analysis on the frame record
|
|
110
|
+
await this.storeAnalysis(frame.captureId, analysis);
|
|
111
|
+
// Emit alert
|
|
112
|
+
this.alertCount++;
|
|
113
|
+
const alert = {
|
|
114
|
+
sessionId: this.sessionId,
|
|
115
|
+
frameId: frame.captureId,
|
|
116
|
+
frameNumber: frame.frameNumber,
|
|
117
|
+
analysis,
|
|
118
|
+
screenshotPath: frame.framePath,
|
|
119
|
+
timestamp: new Date().toISOString(),
|
|
120
|
+
};
|
|
121
|
+
this.lastAnalysis = alert;
|
|
122
|
+
this.onAlert(alert);
|
|
123
|
+
logger_1.default.info(`[MonitorAnalyzer] Analysis complete for frame #${frame.frameNumber}: ${analysis.substring(0, 100)}...`);
|
|
124
|
+
}
|
|
125
|
+
async resizeForAI(buffer) {
|
|
126
|
+
try {
|
|
127
|
+
const sharp = require('sharp');
|
|
128
|
+
const maxDim = this.config.analysisDetail === 'high' ? 1024 : 512;
|
|
129
|
+
const meta = await sharp(buffer).metadata();
|
|
130
|
+
const maxSide = Math.max(meta.width || 0, meta.height || 0);
|
|
131
|
+
if (maxSide > maxDim) {
|
|
132
|
+
const resized = await sharp(buffer)
|
|
133
|
+
.resize({
|
|
134
|
+
width: maxSide === meta.width ? maxDim : undefined,
|
|
135
|
+
height: maxSide === meta.height ? maxDim : undefined,
|
|
136
|
+
fit: 'inside',
|
|
137
|
+
})
|
|
138
|
+
.png()
|
|
139
|
+
.toBuffer();
|
|
140
|
+
return resized.toString('base64');
|
|
141
|
+
}
|
|
142
|
+
return buffer.toString('base64');
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
return buffer.toString('base64');
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
async callAI(imageBase64, frame) {
|
|
149
|
+
const windowContext = frame.windowTitle ? ` (Active window: ${frame.windowTitle})` : '';
|
|
150
|
+
const instruction = `${this.config.analysisPrompt}\n\nFrame #${frame.frameNumber} of session "${this.sessionId}".${windowContext}\nKeep your response concise (2-4 sentences).`;
|
|
151
|
+
return new Promise((resolve) => {
|
|
152
|
+
const postData = JSON.stringify({
|
|
153
|
+
instruction,
|
|
154
|
+
imageBase64,
|
|
155
|
+
});
|
|
156
|
+
const req = http.request({
|
|
157
|
+
hostname: '127.0.0.1',
|
|
158
|
+
port: 47592,
|
|
159
|
+
path: '/api/ai/process',
|
|
160
|
+
method: 'POST',
|
|
161
|
+
headers: {
|
|
162
|
+
'Content-Type': 'application/json',
|
|
163
|
+
'Content-Length': Buffer.byteLength(postData),
|
|
164
|
+
},
|
|
165
|
+
}, (res) => {
|
|
166
|
+
let data = '';
|
|
167
|
+
res.on('data', (chunk) => (data += chunk));
|
|
168
|
+
res.on('end', () => {
|
|
169
|
+
try {
|
|
170
|
+
const parsed = JSON.parse(data);
|
|
171
|
+
if (parsed.error) {
|
|
172
|
+
logger_1.default.warn(`[MonitorAnalyzer] AI error: ${parsed.error}`);
|
|
173
|
+
resolve(null);
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
resolve(parsed.response || null);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
logger_1.default.warn('[MonitorAnalyzer] Failed to parse AI response');
|
|
181
|
+
resolve(null);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
req.on('error', (err) => {
|
|
186
|
+
logger_1.default.warn(`[MonitorAnalyzer] AI request failed: ${err.message}`);
|
|
187
|
+
resolve(null);
|
|
188
|
+
});
|
|
189
|
+
req.setTimeout(30000, () => {
|
|
190
|
+
logger_1.default.warn('[MonitorAnalyzer] AI request timeout');
|
|
191
|
+
req.destroy();
|
|
192
|
+
resolve(null);
|
|
193
|
+
});
|
|
194
|
+
req.write(postData);
|
|
195
|
+
req.end();
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
async storeAnalysis(captureId, analysis) {
|
|
199
|
+
try {
|
|
200
|
+
const { updateCapture } = require('../../db/database');
|
|
201
|
+
updateCapture(captureId, { aiResponse: analysis });
|
|
202
|
+
}
|
|
203
|
+
catch (err) {
|
|
204
|
+
logger_1.default.error('[MonitorAnalyzer] Failed to store analysis:', err);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
getAlertCount() {
|
|
208
|
+
return this.alertCount;
|
|
209
|
+
}
|
|
210
|
+
getLastAnalysis() {
|
|
211
|
+
return this.lastAnalysis;
|
|
212
|
+
}
|
|
213
|
+
destroy() {
|
|
214
|
+
this.destroyed = true;
|
|
215
|
+
this.pendingFrames = [];
|
|
216
|
+
logger_1.default.info(`[MonitorAnalyzer] Destroyed (${this.alertCount} alerts emitted)`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
exports.MonitorAnalyzer = MonitorAnalyzer;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// ChangeDetectProvider — timer + visual diff gating for smart/monitor recording.
|
|
3
|
+
// Works on all platforms without native input hooks.
|
|
4
|
+
// Captures at a configurable interval, compares frames via pixel diff,
|
|
5
|
+
// and only emits events when visual change exceeds threshold.
|
|
6
|
+
// Also polls active window title for focus_change detection.
|
|
7
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
8
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
9
|
+
};
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.ChangeDetectProvider = void 0;
|
|
12
|
+
const logger_1 = __importDefault(require("../../../logger"));
|
|
13
|
+
const platform_1 = require("../../../core/platform");
|
|
14
|
+
// Tiny frame dimensions for fast pixel comparison
|
|
15
|
+
const TINY_WIDTH = 128;
|
|
16
|
+
const TINY_HEIGHT = 72;
|
|
17
|
+
class ChangeDetectProvider {
|
|
18
|
+
handlers = new Set();
|
|
19
|
+
running = false;
|
|
20
|
+
timer = null;
|
|
21
|
+
intervalMs;
|
|
22
|
+
changeThreshold;
|
|
23
|
+
lastTinyBuffer = null;
|
|
24
|
+
lastWindowTitle = '';
|
|
25
|
+
constructor(config) {
|
|
26
|
+
this.intervalMs = config.interval * 1000;
|
|
27
|
+
this.changeThreshold = config.changeThreshold;
|
|
28
|
+
}
|
|
29
|
+
async start() {
|
|
30
|
+
this.running = true;
|
|
31
|
+
this.lastTinyBuffer = null;
|
|
32
|
+
this.lastWindowTitle = '';
|
|
33
|
+
logger_1.default.info(`[ChangeDetect] Starting with interval=${this.intervalMs}ms, threshold=${this.changeThreshold}`);
|
|
34
|
+
this.timer = setInterval(() => {
|
|
35
|
+
this.tick().catch((err) => {
|
|
36
|
+
logger_1.default.error('[ChangeDetect] Tick error:', err);
|
|
37
|
+
});
|
|
38
|
+
}, this.intervalMs);
|
|
39
|
+
}
|
|
40
|
+
async stop() {
|
|
41
|
+
if (this.timer) {
|
|
42
|
+
clearInterval(this.timer);
|
|
43
|
+
this.timer = null;
|
|
44
|
+
}
|
|
45
|
+
this.running = false;
|
|
46
|
+
this.lastTinyBuffer = null;
|
|
47
|
+
logger_1.default.info('[ChangeDetect] Stopped');
|
|
48
|
+
}
|
|
49
|
+
isRunning() {
|
|
50
|
+
return this.running;
|
|
51
|
+
}
|
|
52
|
+
on(_event, handler) {
|
|
53
|
+
this.handlers.add(handler);
|
|
54
|
+
}
|
|
55
|
+
off(_event, handler) {
|
|
56
|
+
this.handlers.delete(handler);
|
|
57
|
+
}
|
|
58
|
+
emit(event) {
|
|
59
|
+
for (const handler of this.handlers) {
|
|
60
|
+
handler(event);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async tick() {
|
|
64
|
+
if (!this.running)
|
|
65
|
+
return;
|
|
66
|
+
try {
|
|
67
|
+
// 1. Capture screen as PNG buffer
|
|
68
|
+
const screenshotBuffer = await this.captureScreenshot();
|
|
69
|
+
if (!screenshotBuffer || screenshotBuffer.length === 0)
|
|
70
|
+
return;
|
|
71
|
+
// 2. Downscale to tiny buffer for fast comparison
|
|
72
|
+
const tinyBuffer = await this.downscale(screenshotBuffer);
|
|
73
|
+
if (!tinyBuffer)
|
|
74
|
+
return;
|
|
75
|
+
// 3. Check active window title for focus_change
|
|
76
|
+
await this.checkWindowTitle();
|
|
77
|
+
// 4. Compute visual diff against previous frame
|
|
78
|
+
const diffRatio = this.lastTinyBuffer
|
|
79
|
+
? this.computeDiff(this.lastTinyBuffer, tinyBuffer)
|
|
80
|
+
: 1.0; // First frame always captured
|
|
81
|
+
// 5. Emit periodic event if change exceeds threshold
|
|
82
|
+
if (diffRatio >= this.changeThreshold) {
|
|
83
|
+
logger_1.default.info(`[ChangeDetect] Visual change ${(diffRatio * 100).toFixed(1)}% >= threshold ${(this.changeThreshold * 100).toFixed(1)}%`);
|
|
84
|
+
this.lastTinyBuffer = tinyBuffer;
|
|
85
|
+
// Emit a synthetic periodic trigger event.
|
|
86
|
+
// We use the mouseup type because TriggerEngine processes it
|
|
87
|
+
// and the periodic timer in TriggerEngine handles the actual capture.
|
|
88
|
+
// But for smart mode, we emit a direct periodic-style event that
|
|
89
|
+
// TriggerEngine will forward to CaptureQueue.
|
|
90
|
+
const event = {
|
|
91
|
+
type: 'mouseup',
|
|
92
|
+
timestamp: Date.now(),
|
|
93
|
+
x: 0,
|
|
94
|
+
y: 0,
|
|
95
|
+
button: 1,
|
|
96
|
+
modifiers: { ctrl: false, alt: false, shift: false, meta: false },
|
|
97
|
+
};
|
|
98
|
+
this.emit(event);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
// Below threshold — skip
|
|
102
|
+
if (this.lastTinyBuffer) {
|
|
103
|
+
logger_1.default.info(`[ChangeDetect] Visual change ${(diffRatio * 100).toFixed(1)}% < threshold — skipping`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
catch (err) {
|
|
108
|
+
logger_1.default.error('[ChangeDetect] Tick error:', err);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async captureScreenshot() {
|
|
112
|
+
try {
|
|
113
|
+
const platform = (0, platform_1.getPlatform)();
|
|
114
|
+
if (platform.isElectron()) {
|
|
115
|
+
// In Electron, use the existing captureScreen function
|
|
116
|
+
const { captureScreen } = require('../../capture');
|
|
117
|
+
return await captureScreen();
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
// Headless: use screenshot-desktop
|
|
121
|
+
const screenshot = require('screenshot-desktop');
|
|
122
|
+
return await screenshot({ format: 'png' });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
catch (err) {
|
|
126
|
+
logger_1.default.error('[ChangeDetect] Screenshot failed:', err);
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
async downscale(buffer) {
|
|
131
|
+
try {
|
|
132
|
+
const sharp = require('sharp');
|
|
133
|
+
return await sharp(buffer)
|
|
134
|
+
.resize(TINY_WIDTH, TINY_HEIGHT, { fit: 'fill' })
|
|
135
|
+
.raw()
|
|
136
|
+
.toBuffer();
|
|
137
|
+
}
|
|
138
|
+
catch (err) {
|
|
139
|
+
logger_1.default.error('[ChangeDetect] Downscale failed:', err);
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
computeDiff(prev, curr) {
|
|
144
|
+
const len = Math.min(prev.length, curr.length);
|
|
145
|
+
if (len === 0)
|
|
146
|
+
return 1.0;
|
|
147
|
+
let totalDiff = 0;
|
|
148
|
+
for (let i = 0; i < len; i++) {
|
|
149
|
+
totalDiff += Math.abs(prev[i] - curr[i]);
|
|
150
|
+
}
|
|
151
|
+
// Normalize to 0-1 ratio (255 is max diff per byte)
|
|
152
|
+
return totalDiff / (len * 255);
|
|
153
|
+
}
|
|
154
|
+
async checkWindowTitle() {
|
|
155
|
+
try {
|
|
156
|
+
const platform = (0, platform_1.getPlatform)();
|
|
157
|
+
if (platform.isElectron()) {
|
|
158
|
+
const { getActiveWindowInfo } = require('../../active-window');
|
|
159
|
+
const info = await getActiveWindowInfo();
|
|
160
|
+
const title = info.windowTitle || '';
|
|
161
|
+
if (title && title !== this.lastWindowTitle && this.lastWindowTitle !== '') {
|
|
162
|
+
logger_1.default.info(`[ChangeDetect] Window changed: "${this.lastWindowTitle}" → "${title}"`);
|
|
163
|
+
// Emit a synthetic focus change — the recording session's focus poll
|
|
164
|
+
// will also detect this, but emitting here ensures it's captured
|
|
165
|
+
// even without the session's own polling.
|
|
166
|
+
}
|
|
167
|
+
this.lastWindowTitle = title;
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
// Headless: use platform's listWindows to detect active window changes
|
|
171
|
+
// This is a best-effort approach — some platforms may not support it well
|
|
172
|
+
// For now, skip focus detection in headless (window title comes from screenshot metadata)
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
// Ignore window detection errors
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
exports.ChangeDetectProvider = ChangeDetectProvider;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// Provider factory: detect platform, try uiohook, fallback to periodic.
|
|
2
|
+
// Provider factory: detect platform, try uiohook, fallback to periodic or change-detect.
|
|
3
3
|
// Returns the best available InputHookProvider for the current platform.
|
|
4
4
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
5
5
|
if (k2 === undefined) k2 = k;
|
|
@@ -40,29 +40,50 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
40
40
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
41
|
exports.createInputHookProvider = createInputHookProvider;
|
|
42
42
|
const logger_1 = __importDefault(require("../../../logger"));
|
|
43
|
-
async function createInputHookProvider(
|
|
44
|
-
//
|
|
43
|
+
async function createInputHookProvider(optsOrInterval) {
|
|
44
|
+
// Backwards-compatible: accept plain number (periodicIntervalMs) or options object
|
|
45
|
+
const opts = typeof optsOrInterval === 'number'
|
|
46
|
+
? { intervalMs: optsOrInterval }
|
|
47
|
+
: optsOrInterval || {};
|
|
48
|
+
const mode = opts.mode || 'interaction';
|
|
49
|
+
// Smart or monitor mode → use ChangeDetectProvider (works everywhere, no native hooks)
|
|
50
|
+
if (mode === 'smart' || mode === 'monitor') {
|
|
51
|
+
const { ChangeDetectProvider } = await Promise.resolve().then(() => __importStar(require('./change-detect-provider')));
|
|
52
|
+
const provider = new ChangeDetectProvider({
|
|
53
|
+
interval: (opts.intervalMs || (mode === 'monitor' ? 5000 : 3000)) / 1000,
|
|
54
|
+
changeThreshold: opts.changeThreshold ?? 0.05,
|
|
55
|
+
});
|
|
56
|
+
logger_1.default.info(`[Recording] Using ChangeDetectProvider for ${mode} mode`);
|
|
57
|
+
return { provider, type: 'change-detect' };
|
|
58
|
+
}
|
|
59
|
+
// Periodic mode → use PeriodicProvider
|
|
60
|
+
if (mode === 'periodic') {
|
|
61
|
+
const { PeriodicProvider } = await Promise.resolve().then(() => __importStar(require('./periodic-provider')));
|
|
62
|
+
const provider = new PeriodicProvider(opts.intervalMs || 5000);
|
|
63
|
+
logger_1.default.info('[Recording] Using PeriodicProvider for periodic mode');
|
|
64
|
+
return { provider, type: 'periodic' };
|
|
65
|
+
}
|
|
66
|
+
// Interaction mode → try uiohook-napi first (works on Windows, macOS, Linux)
|
|
45
67
|
try {
|
|
46
|
-
// Verify the native binary can be imported (catches missing/incompatible binaries)
|
|
47
68
|
await Promise.resolve().then(() => __importStar(require('uiohook-napi')));
|
|
48
69
|
const { UiohookProvider } = await Promise.resolve().then(() => __importStar(require('./uiohook-provider')));
|
|
49
70
|
const provider = new UiohookProvider();
|
|
50
|
-
// Don't test-start/stop here — on macOS, stopping and restarting a CGEvent tap
|
|
51
|
-
// can leave the hook in a broken state. The provider will be started when the
|
|
52
|
-
// recording session begins.
|
|
53
71
|
logger_1.default.info('[Recording] uiohook-napi native binary loaded successfully');
|
|
54
72
|
return { provider, type: 'uiohook' };
|
|
55
73
|
}
|
|
56
74
|
catch (err) {
|
|
57
75
|
const reason = err instanceof Error ? err.message : String(err);
|
|
58
76
|
logger_1.default.warn(`[Recording] uiohook-napi unavailable: ${reason}`);
|
|
59
|
-
logger_1.default.warn('[Recording] Falling back to
|
|
60
|
-
// Fallback to
|
|
61
|
-
const {
|
|
62
|
-
const provider = new
|
|
77
|
+
logger_1.default.warn('[Recording] Falling back to ChangeDetectProvider');
|
|
78
|
+
// Fallback to change-detect (smarter than plain periodic)
|
|
79
|
+
const { ChangeDetectProvider } = await Promise.resolve().then(() => __importStar(require('./change-detect-provider')));
|
|
80
|
+
const provider = new ChangeDetectProvider({
|
|
81
|
+
interval: (opts.intervalMs || 3000) / 1000,
|
|
82
|
+
changeThreshold: opts.changeThreshold ?? 0.05,
|
|
83
|
+
});
|
|
63
84
|
return {
|
|
64
85
|
provider,
|
|
65
|
-
type: '
|
|
86
|
+
type: 'change-detect',
|
|
66
87
|
fallbackReason: reason,
|
|
67
88
|
};
|
|
68
89
|
}
|
|
@@ -157,11 +157,14 @@ class UiohookProvider {
|
|
|
157
157
|
eventCounter = 0;
|
|
158
158
|
lastLogTime = Date.now();
|
|
159
159
|
async start() {
|
|
160
|
-
//
|
|
160
|
+
// Acquire hook via shared manager (reference-counted — won't kill
|
|
161
|
+
// dictation's listeners when recording stops)
|
|
161
162
|
if (!this.uiohook) {
|
|
162
|
-
|
|
163
|
+
const { acquire } = await Promise.resolve().then(() => __importStar(require('../../uiohook-manager')));
|
|
164
|
+
this.uiohook = await acquire('recording');
|
|
163
165
|
}
|
|
164
|
-
const
|
|
166
|
+
const mod = this.uiohook;
|
|
167
|
+
const { uIOhook } = mod;
|
|
165
168
|
// Only register event handlers once — uiohook-napi accumulates listeners
|
|
166
169
|
// and re-registering causes duplicate events
|
|
167
170
|
if (!this.handlersRegistered) {
|
|
@@ -200,13 +203,14 @@ class UiohookProvider {
|
|
|
200
203
|
this.handlersRegistered = true;
|
|
201
204
|
logger_1.default.info('[uiohook] Event handlers registered');
|
|
202
205
|
}
|
|
203
|
-
uIOhook.start();
|
|
204
206
|
this.running = true;
|
|
205
207
|
logger_1.default.info('[uiohook] Hook started, listening for input events');
|
|
206
208
|
}
|
|
207
209
|
async stop() {
|
|
208
210
|
if (this.uiohook && this.running) {
|
|
209
|
-
|
|
211
|
+
// Release our reference — hook only stops if no other consumer needs it
|
|
212
|
+
const { release } = await Promise.resolve().then(() => __importStar(require('../../uiohook-manager')));
|
|
213
|
+
release('recording');
|
|
210
214
|
this.running = false;
|
|
211
215
|
logger_1.default.info('[uiohook] Hook stopped');
|
|
212
216
|
}
|