@elizaos/plugin-vision 1.2.1 → 2.0.0-alpha.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/LICENSE +21 -0
- package/build.config.ts +53 -53
- package/dist/index.js +6716 -67
- package/dist/index.js.map +33 -1
- package/dist/workers/florence2-worker.js +111763 -307
- package/dist/workers/florence2-worker.js.map +92 -1
- package/dist/workers/ocr-worker.js +119177 -339
- package/dist/workers/ocr-worker.js.map +137 -1
- package/dist/workers/screen-capture-worker.js +350 -418
- package/dist/workers/screen-capture-worker.js.map +11 -1
- package/package.json +15 -20
- package/README.md +0 -270
- package/dist/action.d.ts +0 -8
- package/dist/action.js +0 -1212
- package/dist/action.js.map +0 -1
- package/dist/audio-capture-stream.d.ts +0 -42
- package/dist/audio-capture-stream.js +0 -516
- package/dist/audio-capture-stream.js.map +0 -1
- package/dist/audio-capture.d.ts +0 -25
- package/dist/audio-capture.js +0 -412
- package/dist/audio-capture.js.map +0 -1
- package/dist/basic.test.d.ts +0 -1
- package/dist/basic.test.js +0 -97
- package/dist/basic.test.js.map +0 -1
- package/dist/config.d.ts +0 -73
- package/dist/config.js +0 -254
- package/dist/config.js.map +0 -1
- package/dist/entity-tracker.d.ts +0 -32
- package/dist/entity-tracker.js +0 -361
- package/dist/entity-tracker.js.map +0 -1
- package/dist/errors.d.ts +0 -67
- package/dist/errors.js +0 -395
- package/dist/errors.js.map +0 -1
- package/dist/face-recognition.d.ts +0 -31
- package/dist/face-recognition.js +0 -332
- package/dist/face-recognition.js.map +0 -1
- package/dist/florence2-local.d.ts +0 -25
- package/dist/florence2-local.js +0 -280
- package/dist/florence2-local.js.map +0 -1
- package/dist/florence2-model.d.ts +0 -36
- package/dist/florence2-model.js +0 -503
- package/dist/florence2-model.js.map +0 -1
- package/dist/index.d.ts +0 -3
- package/dist/ocr-service-real.d.ts +0 -32
- package/dist/ocr-service-real.js +0 -396
- package/dist/ocr-service-real.js.map +0 -1
- package/dist/ocr-service.d.ts +0 -28
- package/dist/ocr-service.js +0 -216
- package/dist/ocr-service.js.map +0 -1
- package/dist/provider.d.ts +0 -2
- package/dist/provider.js +0 -285
- package/dist/provider.js.map +0 -1
- package/dist/screen-capture.d.ts +0 -16
- package/dist/screen-capture.js +0 -302
- package/dist/screen-capture.js.map +0 -1
- package/dist/service.d.ts +0 -73
- package/dist/service.js +0 -1662
- package/dist/service.js.map +0 -1
- package/dist/tests/e2e/index.d.ts +0 -8
- package/dist/tests/e2e/index.js +0 -33
- package/dist/tests/e2e/index.js.map +0 -1
- package/dist/tests/e2e/run-local.d.ts +0 -2
- package/dist/tests/e2e/run-local.js +0 -166
- package/dist/tests/e2e/run-local.js.map +0 -1
- package/dist/tests/e2e/screen-vision.d.ts +0 -11
- package/dist/tests/e2e/screen-vision.js +0 -384
- package/dist/tests/e2e/screen-vision.js.map +0 -1
- package/dist/tests/e2e/vision-autonomy.d.ts +0 -11
- package/dist/tests/e2e/vision-autonomy.js +0 -375
- package/dist/tests/e2e/vision-autonomy.js.map +0 -1
- package/dist/tests/e2e/vision-basic.d.ts +0 -11
- package/dist/tests/e2e/vision-basic.js +0 -434
- package/dist/tests/e2e/vision-basic.js.map +0 -1
- package/dist/tests/e2e/vision-capture-log.d.ts +0 -11
- package/dist/tests/e2e/vision-capture-log.js +0 -302
- package/dist/tests/e2e/vision-capture-log.js.map +0 -1
- package/dist/tests/e2e/vision-runtime.d.ts +0 -11
- package/dist/tests/e2e/vision-runtime.js +0 -357
- package/dist/tests/e2e/vision-runtime.js.map +0 -1
- package/dist/tests/e2e/vision-worker-tests.d.ts +0 -11
- package/dist/tests/e2e/vision-worker-tests.js +0 -466
- package/dist/tests/e2e/vision-worker-tests.js.map +0 -1
- package/dist/tests/test-pattern-generator.d.ts +0 -40
- package/dist/tests/test-pattern-generator.js +0 -191
- package/dist/tests/test-pattern-generator.js.map +0 -1
- package/dist/tests.d.ts +0 -3
- package/dist/tests.js +0 -11
- package/dist/tests.js.map +0 -1
- package/dist/types.d.ts +0 -222
- package/dist/types.js +0 -16
- package/dist/types.js.map +0 -1
- package/dist/vision-models.d.ts +0 -47
- package/dist/vision-models.js +0 -501
- package/dist/vision-models.js.map +0 -1
- package/dist/vision-worker-manager.d.ts +0 -61
- package/dist/vision-worker-manager.js +0 -668
- package/dist/vision-worker-manager.js.map +0 -1
- package/dist/workers/florence2-worker-simple.d.ts +0 -13
- package/dist/workers/florence2-worker-simple.js +0 -121
- package/dist/workers/florence2-worker-simple.js.map +0 -1
- package/dist/workers/florence2-worker.d.ts +0 -1
- package/dist/workers/ocr-worker.d.ts +0 -1
- package/dist/workers/screen-capture-worker.d.ts +0 -1
- package/dist/workers/worker-logger.d.ts +0 -9
- package/dist/workers/worker-logger.js +0 -95
- package/dist/workers/worker-logger.js.map +0 -1
package/dist/errors.js
DELETED
|
@@ -1,395 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
var __assign = (this && this.__assign) || function () {
|
|
18
|
-
__assign = Object.assign || function(t) {
|
|
19
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
-
s = arguments[i];
|
|
21
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
-
t[p] = s[p];
|
|
23
|
-
}
|
|
24
|
-
return t;
|
|
25
|
-
};
|
|
26
|
-
return __assign.apply(this, arguments);
|
|
27
|
-
};
|
|
28
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
29
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
30
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
31
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
32
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
33
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
34
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
35
|
-
});
|
|
36
|
-
};
|
|
37
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
38
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
39
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
40
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
41
|
-
function step(op) {
|
|
42
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
43
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
44
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
45
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
46
|
-
switch (op[0]) {
|
|
47
|
-
case 0: case 1: t = op; break;
|
|
48
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
49
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
50
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
51
|
-
default:
|
|
52
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
53
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
54
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
55
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
56
|
-
if (t[2]) _.ops.pop();
|
|
57
|
-
_.trys.pop(); continue;
|
|
58
|
-
}
|
|
59
|
-
op = body.call(thisArg, _);
|
|
60
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
61
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
65
|
-
exports.VisionErrorHandler = exports.CircuitBreaker = exports.ErrorRecoveryManager = exports.APIError = exports.ConfigurationError = exports.ProcessingError = exports.ModelInitializationError = exports.ScreenCaptureError = exports.CameraError = exports.VisionError = void 0;
|
|
66
|
-
var core_1 = require("@elizaos/core");
|
|
67
|
-
// Base error class for all vision-related errors
|
|
68
|
-
var VisionError = /** @class */ (function (_super) {
|
|
69
|
-
__extends(VisionError, _super);
|
|
70
|
-
function VisionError(message, code, recoverable, context) {
|
|
71
|
-
if (recoverable === void 0) { recoverable = false; }
|
|
72
|
-
var _this = _super.call(this, message) || this;
|
|
73
|
-
_this.name = 'VisionError';
|
|
74
|
-
_this.code = code;
|
|
75
|
-
_this.recoverable = recoverable;
|
|
76
|
-
_this.context = context;
|
|
77
|
-
// Ensure proper prototype chain
|
|
78
|
-
Object.setPrototypeOf(_this, VisionError.prototype);
|
|
79
|
-
return _this;
|
|
80
|
-
}
|
|
81
|
-
return VisionError;
|
|
82
|
-
}(Error));
|
|
83
|
-
exports.VisionError = VisionError;
|
|
84
|
-
// Specific error types
|
|
85
|
-
var CameraError = /** @class */ (function (_super) {
|
|
86
|
-
__extends(CameraError, _super);
|
|
87
|
-
function CameraError(message, context) {
|
|
88
|
-
var _this = _super.call(this, message, 'CAMERA_ERROR', true, context) || this;
|
|
89
|
-
_this.name = 'CameraError';
|
|
90
|
-
return _this;
|
|
91
|
-
}
|
|
92
|
-
return CameraError;
|
|
93
|
-
}(VisionError));
|
|
94
|
-
exports.CameraError = CameraError;
|
|
95
|
-
var ScreenCaptureError = /** @class */ (function (_super) {
|
|
96
|
-
__extends(ScreenCaptureError, _super);
|
|
97
|
-
function ScreenCaptureError(message, context) {
|
|
98
|
-
var _this = _super.call(this, message, 'SCREEN_CAPTURE_ERROR', true, context) || this;
|
|
99
|
-
_this.name = 'ScreenCaptureError';
|
|
100
|
-
return _this;
|
|
101
|
-
}
|
|
102
|
-
return ScreenCaptureError;
|
|
103
|
-
}(VisionError));
|
|
104
|
-
exports.ScreenCaptureError = ScreenCaptureError;
|
|
105
|
-
var ModelInitializationError = /** @class */ (function (_super) {
|
|
106
|
-
__extends(ModelInitializationError, _super);
|
|
107
|
-
function ModelInitializationError(message, modelName, context) {
|
|
108
|
-
var _this = _super.call(this, message, 'MODEL_INIT_ERROR', false, __assign(__assign({}, context), { modelName: modelName })) || this;
|
|
109
|
-
_this.name = 'ModelInitializationError';
|
|
110
|
-
return _this;
|
|
111
|
-
}
|
|
112
|
-
return ModelInitializationError;
|
|
113
|
-
}(VisionError));
|
|
114
|
-
exports.ModelInitializationError = ModelInitializationError;
|
|
115
|
-
var ProcessingError = /** @class */ (function (_super) {
|
|
116
|
-
__extends(ProcessingError, _super);
|
|
117
|
-
function ProcessingError(message, context) {
|
|
118
|
-
var _this = _super.call(this, message, 'PROCESSING_ERROR', true, context) || this;
|
|
119
|
-
_this.name = 'ProcessingError';
|
|
120
|
-
return _this;
|
|
121
|
-
}
|
|
122
|
-
return ProcessingError;
|
|
123
|
-
}(VisionError));
|
|
124
|
-
exports.ProcessingError = ProcessingError;
|
|
125
|
-
var ConfigurationError = /** @class */ (function (_super) {
|
|
126
|
-
__extends(ConfigurationError, _super);
|
|
127
|
-
function ConfigurationError(message, context) {
|
|
128
|
-
var _this = _super.call(this, message, 'CONFIG_ERROR', false, context) || this;
|
|
129
|
-
_this.name = 'ConfigurationError';
|
|
130
|
-
return _this;
|
|
131
|
-
}
|
|
132
|
-
return ConfigurationError;
|
|
133
|
-
}(VisionError));
|
|
134
|
-
exports.ConfigurationError = ConfigurationError;
|
|
135
|
-
var APIError = /** @class */ (function (_super) {
|
|
136
|
-
__extends(APIError, _super);
|
|
137
|
-
function APIError(message, statusCode, endpoint, context) {
|
|
138
|
-
var _this = _super.call(this, message, 'API_ERROR', true, __assign(__assign({}, context), { statusCode: statusCode, endpoint: endpoint })) || this;
|
|
139
|
-
_this.name = 'APIError';
|
|
140
|
-
_this.statusCode = statusCode;
|
|
141
|
-
_this.endpoint = endpoint;
|
|
142
|
-
return _this;
|
|
143
|
-
}
|
|
144
|
-
return APIError;
|
|
145
|
-
}(VisionError));
|
|
146
|
-
exports.APIError = APIError;
|
|
147
|
-
var ErrorRecoveryManager = /** @class */ (function () {
|
|
148
|
-
function ErrorRecoveryManager() {
|
|
149
|
-
this.strategies = new Map();
|
|
150
|
-
this.errorCounts = new Map();
|
|
151
|
-
this.maxRetries = 3;
|
|
152
|
-
this.registerDefaultStrategies();
|
|
153
|
-
}
|
|
154
|
-
ErrorRecoveryManager.prototype.registerDefaultStrategies = function () {
|
|
155
|
-
var _this = this;
|
|
156
|
-
// Camera recovery strategy
|
|
157
|
-
this.registerStrategy('CAMERA_ERROR', {
|
|
158
|
-
canRecover: function (error) {
|
|
159
|
-
var count = _this.errorCounts.get(error.code) || 0;
|
|
160
|
-
return count < _this.maxRetries;
|
|
161
|
-
},
|
|
162
|
-
recover: function (error) { return __awaiter(_this, void 0, void 0, function () {
|
|
163
|
-
var _this = this;
|
|
164
|
-
return __generator(this, function (_a) {
|
|
165
|
-
switch (_a.label) {
|
|
166
|
-
case 0:
|
|
167
|
-
core_1.logger.warn('[ErrorRecovery] Attempting camera recovery:', error.message);
|
|
168
|
-
// Wait before retry
|
|
169
|
-
return [4 /*yield*/, new Promise(function (resolve) {
|
|
170
|
-
return setTimeout(resolve, 1000 * (_this.errorCounts.get(error.code) || 1));
|
|
171
|
-
})];
|
|
172
|
-
case 1:
|
|
173
|
-
// Wait before retry
|
|
174
|
-
_a.sent();
|
|
175
|
-
core_1.logger.info('[ErrorRecovery] Camera recovery attempt complete');
|
|
176
|
-
return [2 /*return*/];
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
}); },
|
|
180
|
-
});
|
|
181
|
-
// API recovery strategy
|
|
182
|
-
this.registerStrategy('API_ERROR', {
|
|
183
|
-
canRecover: function (error) {
|
|
184
|
-
var apiError = error;
|
|
185
|
-
// Don't retry on client errors (4xx)
|
|
186
|
-
if (apiError.statusCode && apiError.statusCode >= 400 && apiError.statusCode < 500) {
|
|
187
|
-
return false;
|
|
188
|
-
}
|
|
189
|
-
var count = _this.errorCounts.get("".concat(error.code, "_").concat(apiError.endpoint)) || 0;
|
|
190
|
-
return count < _this.maxRetries;
|
|
191
|
-
},
|
|
192
|
-
recover: function (error) { return __awaiter(_this, void 0, void 0, function () {
|
|
193
|
-
var apiError, delay;
|
|
194
|
-
return __generator(this, function (_a) {
|
|
195
|
-
switch (_a.label) {
|
|
196
|
-
case 0:
|
|
197
|
-
apiError = error;
|
|
198
|
-
core_1.logger.warn('[ErrorRecovery] API error recovery:', error.message);
|
|
199
|
-
delay = Math.min(1000 * Math.pow(2, this.errorCounts.get("".concat(error.code, "_").concat(apiError.endpoint)) || 0), 30000);
|
|
200
|
-
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, delay); })];
|
|
201
|
-
case 1:
|
|
202
|
-
_a.sent();
|
|
203
|
-
return [2 /*return*/];
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
}); },
|
|
207
|
-
});
|
|
208
|
-
// Screen capture recovery
|
|
209
|
-
this.registerStrategy('SCREEN_CAPTURE_ERROR', {
|
|
210
|
-
canRecover: function () { return true; },
|
|
211
|
-
recover: function (error) { return __awaiter(_this, void 0, void 0, function () {
|
|
212
|
-
return __generator(this, function (_a) {
|
|
213
|
-
switch (_a.label) {
|
|
214
|
-
case 0:
|
|
215
|
-
core_1.logger.warn('[ErrorRecovery] Screen capture recovery:', error.message);
|
|
216
|
-
// Brief pause before retry
|
|
217
|
-
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 500); })];
|
|
218
|
-
case 1:
|
|
219
|
-
// Brief pause before retry
|
|
220
|
-
_a.sent();
|
|
221
|
-
return [2 /*return*/];
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
}); },
|
|
225
|
-
});
|
|
226
|
-
};
|
|
227
|
-
ErrorRecoveryManager.prototype.registerStrategy = function (errorCode, strategy) {
|
|
228
|
-
this.strategies.set(errorCode, strategy);
|
|
229
|
-
};
|
|
230
|
-
ErrorRecoveryManager.prototype.handleError = function (error) {
|
|
231
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
232
|
-
var errorKey, currentCount, strategy, recoveryError_1;
|
|
233
|
-
var _a;
|
|
234
|
-
return __generator(this, function (_b) {
|
|
235
|
-
switch (_b.label) {
|
|
236
|
-
case 0:
|
|
237
|
-
core_1.logger.error("[ErrorRecovery] Handling ".concat(error.name, ":"), error.message, error.context);
|
|
238
|
-
errorKey = error.code + (((_a = error.context) === null || _a === void 0 ? void 0 : _a.endpoint) || '');
|
|
239
|
-
currentCount = this.errorCounts.get(errorKey) || 0;
|
|
240
|
-
this.errorCounts.set(errorKey, currentCount + 1);
|
|
241
|
-
// Check if we can recover
|
|
242
|
-
if (!error.recoverable) {
|
|
243
|
-
core_1.logger.error('[ErrorRecovery] Error is not recoverable');
|
|
244
|
-
return [2 /*return*/, false];
|
|
245
|
-
}
|
|
246
|
-
strategy = this.strategies.get(error.code);
|
|
247
|
-
if (!strategy) {
|
|
248
|
-
core_1.logger.warn('[ErrorRecovery] No recovery strategy for error code:', error.code);
|
|
249
|
-
return [2 /*return*/, false];
|
|
250
|
-
}
|
|
251
|
-
if (!strategy.canRecover(error)) {
|
|
252
|
-
core_1.logger.error('[ErrorRecovery] Recovery strategy cannot handle this error');
|
|
253
|
-
return [2 /*return*/, false];
|
|
254
|
-
}
|
|
255
|
-
_b.label = 1;
|
|
256
|
-
case 1:
|
|
257
|
-
_b.trys.push([1, 3, , 4]);
|
|
258
|
-
return [4 /*yield*/, strategy.recover(error)];
|
|
259
|
-
case 2:
|
|
260
|
-
_b.sent();
|
|
261
|
-
core_1.logger.info('[ErrorRecovery] Recovery successful');
|
|
262
|
-
return [2 /*return*/, true];
|
|
263
|
-
case 3:
|
|
264
|
-
recoveryError_1 = _b.sent();
|
|
265
|
-
core_1.logger.error('[ErrorRecovery] Recovery failed:', recoveryError_1);
|
|
266
|
-
return [2 /*return*/, false];
|
|
267
|
-
case 4: return [2 /*return*/];
|
|
268
|
-
}
|
|
269
|
-
});
|
|
270
|
-
});
|
|
271
|
-
};
|
|
272
|
-
ErrorRecoveryManager.prototype.resetErrorCount = function (errorCode) {
|
|
273
|
-
this.errorCounts.delete(errorCode);
|
|
274
|
-
};
|
|
275
|
-
ErrorRecoveryManager.prototype.resetAllCounts = function () {
|
|
276
|
-
this.errorCounts.clear();
|
|
277
|
-
};
|
|
278
|
-
return ErrorRecoveryManager;
|
|
279
|
-
}());
|
|
280
|
-
exports.ErrorRecoveryManager = ErrorRecoveryManager;
|
|
281
|
-
// Circuit breaker for external services
|
|
282
|
-
var CircuitBreaker = /** @class */ (function () {
|
|
283
|
-
function CircuitBreaker(threshold, timeout, // 1 minute
|
|
284
|
-
name) {
|
|
285
|
-
if (threshold === void 0) { threshold = 5; }
|
|
286
|
-
if (timeout === void 0) { timeout = 60000; }
|
|
287
|
-
this.threshold = threshold;
|
|
288
|
-
this.timeout = timeout;
|
|
289
|
-
this.name = name;
|
|
290
|
-
this.failures = 0;
|
|
291
|
-
this.lastFailureTime = 0;
|
|
292
|
-
this.state = 'closed';
|
|
293
|
-
}
|
|
294
|
-
CircuitBreaker.prototype.execute = function (operation) {
|
|
295
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
296
|
-
var result, error_1;
|
|
297
|
-
return __generator(this, function (_a) {
|
|
298
|
-
switch (_a.label) {
|
|
299
|
-
case 0:
|
|
300
|
-
if (this.state === 'open') {
|
|
301
|
-
if (Date.now() - this.lastFailureTime > this.timeout) {
|
|
302
|
-
this.state = 'half-open';
|
|
303
|
-
core_1.logger.info("[CircuitBreaker] ".concat(this.name, " entering half-open state"));
|
|
304
|
-
}
|
|
305
|
-
else {
|
|
306
|
-
throw new VisionError("Circuit breaker is open for ".concat(this.name), 'CIRCUIT_BREAKER_OPEN', true);
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
_a.label = 1;
|
|
310
|
-
case 1:
|
|
311
|
-
_a.trys.push([1, 3, , 4]);
|
|
312
|
-
return [4 /*yield*/, operation()];
|
|
313
|
-
case 2:
|
|
314
|
-
result = _a.sent();
|
|
315
|
-
this.onSuccess();
|
|
316
|
-
return [2 /*return*/, result];
|
|
317
|
-
case 3:
|
|
318
|
-
error_1 = _a.sent();
|
|
319
|
-
this.onFailure();
|
|
320
|
-
throw error_1;
|
|
321
|
-
case 4: return [2 /*return*/];
|
|
322
|
-
}
|
|
323
|
-
});
|
|
324
|
-
});
|
|
325
|
-
};
|
|
326
|
-
CircuitBreaker.prototype.onSuccess = function () {
|
|
327
|
-
if (this.state === 'half-open') {
|
|
328
|
-
core_1.logger.info("[CircuitBreaker] ".concat(this.name, " recovered, closing circuit"));
|
|
329
|
-
}
|
|
330
|
-
this.failures = 0;
|
|
331
|
-
this.state = 'closed';
|
|
332
|
-
};
|
|
333
|
-
CircuitBreaker.prototype.onFailure = function () {
|
|
334
|
-
this.failures++;
|
|
335
|
-
this.lastFailureTime = Date.now();
|
|
336
|
-
if (this.failures >= this.threshold) {
|
|
337
|
-
this.state = 'open';
|
|
338
|
-
core_1.logger.error("[CircuitBreaker] ".concat(this.name, " threshold exceeded, opening circuit"));
|
|
339
|
-
}
|
|
340
|
-
};
|
|
341
|
-
CircuitBreaker.prototype.getState = function () {
|
|
342
|
-
return this.state;
|
|
343
|
-
};
|
|
344
|
-
CircuitBreaker.prototype.reset = function () {
|
|
345
|
-
this.failures = 0;
|
|
346
|
-
this.state = 'closed';
|
|
347
|
-
core_1.logger.info("[CircuitBreaker] ".concat(this.name, " reset"));
|
|
348
|
-
};
|
|
349
|
-
return CircuitBreaker;
|
|
350
|
-
}());
|
|
351
|
-
exports.CircuitBreaker = CircuitBreaker;
|
|
352
|
-
// Global error handler
|
|
353
|
-
var VisionErrorHandler = /** @class */ (function () {
|
|
354
|
-
function VisionErrorHandler() {
|
|
355
|
-
this.circuitBreakers = new Map();
|
|
356
|
-
this.recoveryManager = new ErrorRecoveryManager();
|
|
357
|
-
}
|
|
358
|
-
VisionErrorHandler.getInstance = function () {
|
|
359
|
-
if (!VisionErrorHandler.instance) {
|
|
360
|
-
VisionErrorHandler.instance = new VisionErrorHandler();
|
|
361
|
-
}
|
|
362
|
-
return VisionErrorHandler.instance;
|
|
363
|
-
};
|
|
364
|
-
VisionErrorHandler.prototype.getCircuitBreaker = function (name, threshold, timeout) {
|
|
365
|
-
if (threshold === void 0) { threshold = 5; }
|
|
366
|
-
if (timeout === void 0) { timeout = 60000; }
|
|
367
|
-
if (!this.circuitBreakers.has(name)) {
|
|
368
|
-
this.circuitBreakers.set(name, new CircuitBreaker(threshold, timeout, name));
|
|
369
|
-
}
|
|
370
|
-
return this.circuitBreakers.get(name);
|
|
371
|
-
};
|
|
372
|
-
VisionErrorHandler.prototype.handle = function (error) {
|
|
373
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
374
|
-
return __generator(this, function (_a) {
|
|
375
|
-
// Convert to VisionError if needed
|
|
376
|
-
if (!(error instanceof VisionError)) {
|
|
377
|
-
error = new ProcessingError(error.message || 'Unknown error', { originalError: error });
|
|
378
|
-
}
|
|
379
|
-
return [2 /*return*/, this.recoveryManager.handleError(error)];
|
|
380
|
-
});
|
|
381
|
-
});
|
|
382
|
-
};
|
|
383
|
-
VisionErrorHandler.prototype.resetCircuitBreaker = function (name) {
|
|
384
|
-
var breaker = this.circuitBreakers.get(name);
|
|
385
|
-
if (breaker) {
|
|
386
|
-
breaker.reset();
|
|
387
|
-
}
|
|
388
|
-
};
|
|
389
|
-
VisionErrorHandler.prototype.resetAllCircuitBreakers = function () {
|
|
390
|
-
this.circuitBreakers.forEach(function (breaker) { return breaker.reset(); });
|
|
391
|
-
};
|
|
392
|
-
return VisionErrorHandler;
|
|
393
|
-
}());
|
|
394
|
-
exports.VisionErrorHandler = VisionErrorHandler;
|
|
395
|
-
//# sourceMappingURL=errors.js.map
|
package/dist/errors.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,sCAAuC;AAEvC,iDAAiD;AACjD;IAAiC,+BAAK;IAKpC,qBAAY,OAAe,EAAE,IAAY,EAAE,WAAmB,EAAE,OAA6B;QAAlD,4BAAA,EAAA,mBAAmB;QAC5D,YAAA,MAAK,YAAC,OAAO,CAAC,SAAC;QACf,KAAI,CAAC,IAAI,GAAG,aAAa,CAAC;QAC1B,KAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,KAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,KAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QAEvB,gCAAgC;QAChC,MAAM,CAAC,cAAc,CAAC,KAAI,EAAE,WAAW,CAAC,SAAS,CAAC,CAAC;;IACrD,CAA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K,GAAG,IAAI,eAAe,CAAC,KAAK,CAAC,OAAO,IAAI,eAAe,EAAE,EAAE,aAAa,EAAE,KAAK,EAAE,CAAC,CAAC;gBAC1F,CAAC;gBAED,sBAAO,IAAI,CAAC,eAAe,CAAC,WAAW,CAAC,KAAK,CAAC,EAAC;;;KAChD;IAED,gDAAmB,GAAnB,UAAoB,IAAY;QAC9B,IAAM,OAAO,GAAG,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAC/C,IAAI,OAAO,EAAE,CAAC;YACZ,OAAO,CAAC,KAAK,EAAE,CAAC;QAClB,CAAC;IACH,CAAC;IAED,oDAAuB,GAAvB;QACE,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,UAAC,OAAO,IAAK,OAAA,OAAO,CAAC,KAAK,EAAE,EAAf,CAAe,CAAC,CAAC;IAC7D,CAAC;IACH,yBAAC;AAAD,CAAC,AA1CD,IA0CC;AA1CY,gDAAkB","sourcesContent":["import { logger } from '@elizaos/core';\n\n// Base error class for all vision-related errors\nexport class VisionError extends Error {\n public readonly code: string;\n public readonly context?: Record<string, any>;\n public readonly recoverable: boolean;\n\n constructor(message: string, code: string, recoverable = false, context?: Record<string, any>) {\n super(message);\n this.name = 'VisionError';\n this.code = code;\n this.recoverable = recoverable;\n this.context = context;\n\n // Ensure proper prototype chain\n Object.setPrototypeOf(this, VisionError.prototype);\n }\n}\n\n// Specific error types\nexport class CameraError extends VisionError {\n constructor(message: string, context?: Record<string, any>) {\n super(message, 'CAMERA_ERROR', true, context);\n this.name = 'CameraError';\n }\n}\n\nexport class ScreenCaptureError extends VisionError {\n constructor(message: string, context?: Record<string, any>) {\n super(message, 'SCREEN_CAPTURE_ERROR', true, context);\n this.name = 'ScreenCaptureError';\n }\n}\n\nexport class ModelInitializationError extends VisionError {\n constructor(message: string, modelName: string, context?: Record<string, any>) {\n super(message, 'MODEL_INIT_ERROR', false, { ...context, modelName });\n this.name = 'ModelInitializationError';\n }\n}\n\nexport class ProcessingError extends VisionError {\n constructor(message: string, context?: Record<string, any>) {\n super(message, 'PROCESSING_ERROR', true, context);\n this.name = 'ProcessingError';\n }\n}\n\nexport class ConfigurationError extends VisionError {\n constructor(message: string, context?: Record<string, any>) {\n super(message, 'CONFIG_ERROR', false, context);\n this.name = 'ConfigurationError';\n }\n}\n\nexport class APIError extends VisionError {\n public readonly statusCode?: number;\n public readonly endpoint?: string;\n\n constructor(\n message: string,\n statusCode?: number,\n endpoint?: string,\n context?: Record<string, any>\n ) {\n super(message, 'API_ERROR', true, { ...context, statusCode, endpoint });\n this.name = 'APIError';\n this.statusCode = statusCode;\n this.endpoint = endpoint;\n }\n}\n\n// Error recovery strategies\nexport interface RecoveryStrategy {\n canRecover(error: VisionError): boolean;\n recover(error: VisionError): Promise<void>;\n}\n\nexport class ErrorRecoveryManager {\n private strategies: Map<string, RecoveryStrategy> = new Map();\n private errorCounts: Map<string, number> = new Map();\n private readonly maxRetries = 3;\n\n constructor() {\n this.registerDefaultStrategies();\n }\n\n private registerDefaultStrategies(): void {\n // Camera recovery strategy\n this.registerStrategy('CAMERA_ERROR', {\n canRecover: (error) => {\n const count = this.errorCounts.get(error.code) || 0;\n return count < this.maxRetries;\n },\n recover: async (error) => {\n logger.warn('[ErrorRecovery] Attempting camera recovery:', error.message);\n // Wait before retry\n await new Promise((resolve) =>\n setTimeout(resolve, 1000 * (this.errorCounts.get(error.code) || 1))\n );\n logger.info('[ErrorRecovery] Camera recovery attempt complete');\n },\n });\n\n // API recovery strategy\n this.registerStrategy('API_ERROR', {\n canRecover: (error) => {\n const apiError = error as APIError;\n // Don't retry on client errors (4xx)\n if (apiError.statusCode && apiError.statusCode >= 400 && apiError.statusCode < 500) {\n return false;\n }\n const count = this.errorCounts.get(`${error.code}_${apiError.endpoint}`) || 0;\n return count < this.maxRetries;\n },\n recover: async (error) => {\n const apiError = error as APIError;\n logger.warn('[ErrorRecovery] API error recovery:', error.message);\n // Exponential backoff\n const delay = Math.min(\n 1000 * Math.pow(2, this.errorCounts.get(`${error.code}_${apiError.endpoint}`) || 0),\n 30000\n );\n await new Promise((resolve) => setTimeout(resolve, delay));\n },\n });\n\n // Screen capture recovery\n this.registerStrategy('SCREEN_CAPTURE_ERROR', {\n canRecover: () => true,\n recover: async (error) => {\n logger.warn('[ErrorRecovery] Screen capture recovery:', error.message);\n // Brief pause before retry\n await new Promise((resolve) => setTimeout(resolve, 500));\n },\n });\n }\n\n registerStrategy(errorCode: string, strategy: RecoveryStrategy): void {\n this.strategies.set(errorCode, strategy);\n }\n\n async handleError(error: VisionError): Promise<boolean> {\n logger.error(`[ErrorRecovery] Handling ${error.name}:`, error.message, error.context);\n\n // Track error occurrences\n const errorKey = error.code + (error.context?.endpoint || '');\n const currentCount = this.errorCounts.get(errorKey) || 0;\n this.errorCounts.set(errorKey, currentCount + 1);\n\n // Check if we can recover\n if (!error.recoverable) {\n logger.error('[ErrorRecovery] Error is not recoverable');\n return false;\n }\n\n const strategy = this.strategies.get(error.code);\n if (!strategy) {\n logger.warn('[ErrorRecovery] No recovery strategy for error code:', error.code);\n return false;\n }\n\n if (!strategy.canRecover(error)) {\n logger.error('[ErrorRecovery] Recovery strategy cannot handle this error');\n return false;\n }\n\n try {\n await strategy.recover(error);\n logger.info('[ErrorRecovery] Recovery successful');\n return true;\n } catch (recoveryError) {\n logger.error('[ErrorRecovery] Recovery failed:', recoveryError);\n return false;\n }\n }\n\n resetErrorCount(errorCode: string): void {\n this.errorCounts.delete(errorCode);\n }\n\n resetAllCounts(): void {\n this.errorCounts.clear();\n }\n}\n\n// Circuit breaker for external services\nexport class CircuitBreaker {\n private failures = 0;\n private lastFailureTime = 0;\n private state: 'closed' | 'open' | 'half-open' = 'closed';\n\n constructor(\n private readonly threshold = 5,\n private readonly timeout = 60000, // 1 minute\n private readonly name: string\n ) {}\n\n async execute<T>(operation: () => Promise<T>): Promise<T> {\n if (this.state === 'open') {\n if (Date.now() - this.lastFailureTime > this.timeout) {\n this.state = 'half-open';\n logger.info(`[CircuitBreaker] ${this.name} entering half-open state`);\n } else {\n throw new VisionError(\n `Circuit breaker is open for ${this.name}`,\n 'CIRCUIT_BREAKER_OPEN',\n true\n );\n }\n }\n\n try {\n const result = await operation();\n this.onSuccess();\n return result;\n } catch (error) {\n this.onFailure();\n throw error;\n }\n }\n\n private onSuccess(): void {\n if (this.state === 'half-open') {\n logger.info(`[CircuitBreaker] ${this.name} recovered, closing circuit`);\n }\n this.failures = 0;\n this.state = 'closed';\n }\n\n private onFailure(): void {\n this.failures++;\n this.lastFailureTime = Date.now();\n\n if (this.failures >= this.threshold) {\n this.state = 'open';\n logger.error(`[CircuitBreaker] ${this.name} threshold exceeded, opening circuit`);\n }\n }\n\n getState(): string {\n return this.state;\n }\n\n reset(): void {\n this.failures = 0;\n this.state = 'closed';\n logger.info(`[CircuitBreaker] ${this.name} reset`);\n }\n}\n\n// Global error handler\nexport class VisionErrorHandler {\n private static instance: VisionErrorHandler;\n private recoveryManager: ErrorRecoveryManager;\n private circuitBreakers: Map<string, CircuitBreaker> = new Map();\n\n private constructor() {\n this.recoveryManager = new ErrorRecoveryManager();\n }\n\n static getInstance(): VisionErrorHandler {\n if (!VisionErrorHandler.instance) {\n VisionErrorHandler.instance = new VisionErrorHandler();\n }\n return VisionErrorHandler.instance;\n }\n\n getCircuitBreaker(name: string, threshold = 5, timeout = 60000): CircuitBreaker {\n if (!this.circuitBreakers.has(name)) {\n this.circuitBreakers.set(name, new CircuitBreaker(threshold, timeout, name));\n }\n return this.circuitBreakers.get(name)!;\n }\n\n async handle(error: any): Promise<boolean> {\n // Convert to VisionError if needed\n if (!(error instanceof VisionError)) {\n error = new ProcessingError(error.message || 'Unknown error', { originalError: error });\n }\n\n return this.recoveryManager.handleError(error);\n }\n\n resetCircuitBreaker(name: string): void {\n const breaker = this.circuitBreakers.get(name);\n if (breaker) {\n breaker.reset();\n }\n }\n\n resetAllCircuitBreakers(): void {\n this.circuitBreakers.forEach((breaker) => breaker.reset());\n }\n}\n"]}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import * as faceapi from 'face-api.js';
|
|
2
|
-
import type { FaceProfile } from './types';
|
|
3
|
-
export declare class FaceRecognition {
|
|
4
|
-
private initialized;
|
|
5
|
-
private faceLibrary;
|
|
6
|
-
private modelPath;
|
|
7
|
-
private readonly FACE_MATCH_THRESHOLD;
|
|
8
|
-
private readonly MIN_FACE_SIZE;
|
|
9
|
-
initialize(): Promise<void>;
|
|
10
|
-
detectFaces(imageData: Buffer, width: number, height: number): Promise<Array<{
|
|
11
|
-
detection: faceapi.FaceDetection;
|
|
12
|
-
landmarks: faceapi.FaceLandmarks68;
|
|
13
|
-
descriptor: Float32Array;
|
|
14
|
-
expressions: faceapi.FaceExpressions;
|
|
15
|
-
ageGender?: {
|
|
16
|
-
age: number;
|
|
17
|
-
gender: string;
|
|
18
|
-
genderProbability: number;
|
|
19
|
-
};
|
|
20
|
-
}>>;
|
|
21
|
-
recognizeFace(descriptor: Float32Array): Promise<{
|
|
22
|
-
profileId: string;
|
|
23
|
-
distance: number;
|
|
24
|
-
} | null>;
|
|
25
|
-
addOrUpdateFace(descriptor: Float32Array, attributes?: Partial<FaceProfile>): Promise<string>;
|
|
26
|
-
getFaceProfile(profileId: string): FaceProfile | undefined;
|
|
27
|
-
getAllProfiles(): FaceProfile[];
|
|
28
|
-
private euclideanDistance;
|
|
29
|
-
saveFaceLibrary(path: string): Promise<void>;
|
|
30
|
-
loadFaceLibrary(path: string): Promise<void>;
|
|
31
|
-
}
|