@camstack/addon-post-analysis 0.1.1

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.
Files changed (80) hide show
  1. package/dist/embedding-encoder/index.js +928 -0
  2. package/dist/embedding-encoder/index.js.map +1 -0
  3. package/dist/embedding-encoder/index.mjs +888 -0
  4. package/dist/embedding-encoder/index.mjs.map +1 -0
  5. package/dist/enrichment-engine/index.js +774 -0
  6. package/dist/enrichment-engine/index.js.map +1 -0
  7. package/dist/enrichment-engine/index.mjs +774 -0
  8. package/dist/enrichment-engine/index.mjs.map +1 -0
  9. package/dist/ffmpeg-config-DRONlBsj.mjs +56 -0
  10. package/dist/ffmpeg-config-DRONlBsj.mjs.map +1 -0
  11. package/dist/ffmpeg-config-uANz3sV5.js +73 -0
  12. package/dist/ffmpeg-config-uANz3sV5.js.map +1 -0
  13. package/dist/pipeline-analytics/@mf-types/compiled-types/pipeline-analytics/widgets/AudioHistoryChart.d.ts +4 -0
  14. package/dist/pipeline-analytics/@mf-types/compiled-types/pipeline-analytics/widgets/AudioMetricsPanel.d.ts +10 -0
  15. package/dist/pipeline-analytics/@mf-types/compiled-types/pipeline-analytics/widgets/DetectionHistoryChart.d.ts +4 -0
  16. package/dist/pipeline-analytics/@mf-types/compiled-types/pipeline-analytics/widgets/MotionHistoryChart.d.ts +4 -0
  17. package/dist/pipeline-analytics/@mf-types/compiled-types/pipeline-analytics/widgets/OccupancyHistoryChart.d.ts +4 -0
  18. package/dist/pipeline-analytics/@mf-types/compiled-types/pipeline-analytics/widgets/OccupancyPanel.d.ts +10 -0
  19. package/dist/pipeline-analytics/@mf-types/compiled-types/pipeline-analytics/widgets/chart-utils.d.ts +97 -0
  20. package/dist/pipeline-analytics/@mf-types/compiled-types/pipeline-analytics/widgets/index.d.ts +27 -0
  21. package/dist/pipeline-analytics/@mf-types/widgets.d.ts +2 -0
  22. package/dist/pipeline-analytics/@mf-types.d.ts +3 -0
  23. package/dist/pipeline-analytics/@mf-types.zip +0 -0
  24. package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare___mf_0_camstack_mf_1_sdk__loadShare__.mjs-h5aXOPSA.mjs +12 -0
  25. package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare___mf_0_camstack_mf_1_types__loadShare__.mjs-46iNwbxa.mjs +16 -0
  26. package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare___mf_0_camstack_mf_1_ui_mf_2_library__loadShare__.mjs-Db6yh4op.mjs +15 -0
  27. package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare___mf_0_tanstack_mf_1_react_mf_2_query__loadShare__.mjs-DoWbefqS.mjs +104 -0
  28. package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare___mf_0_trpc_mf_1_client__loadShare__.mjs-52bfkwC8.mjs +85 -0
  29. package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare___mf_0_trpc_mf_1_react_mf_2_query__loadShare__.mjs-CVrnrGED.mjs +62 -0
  30. package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare__react__loadShare__.mjs-DuO9h7li.mjs +85 -0
  31. package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare__react__loadShare__.mjs_commonjs-proxy-CmqNjq44.mjs +29 -0
  32. package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare__react_mf_1_jsx_mf_2_runtime__loadShare__.mjs-BsyrX6NO.mjs +36 -0
  33. package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare__react_mf_2_dom__loadShare__.mjs-Dp8hqYOB.mjs +45 -0
  34. package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare__react_mf_2_dom__loadShare__.mjs_commonjs-proxy-CA8cCIEl.mjs +6 -0
  35. package/dist/pipeline-analytics/__mfe_internal__addon_pipeline_analytics_widgets__loadShare__react_mf_2_dom_mf_1_client__loadShare__.mjs-BZjEt71l.mjs +34 -0
  36. package/dist/pipeline-analytics/_stub.js +1397 -0
  37. package/dist/pipeline-analytics/_virtual_mf-localSharedImportMap___mfe_internal__addon_pipeline_analytics_widgets-Bfo-or09.mjs +156 -0
  38. package/dist/pipeline-analytics/client-DdXDZxzK.mjs +10063 -0
  39. package/dist/pipeline-analytics/getErrorShape-BPSzUA7W-TlK8ipWe.mjs +211 -0
  40. package/dist/pipeline-analytics/hostInit-CqUKNfOr.mjs +168 -0
  41. package/dist/pipeline-analytics/index-B4OKsa9p.mjs +2603 -0
  42. package/dist/pipeline-analytics/index-B7r1koiV.mjs +19109 -0
  43. package/dist/pipeline-analytics/index-CRN37Hr9.mjs +15195 -0
  44. package/dist/pipeline-analytics/index-DRx99znz.mjs +1617 -0
  45. package/dist/pipeline-analytics/index-DyYvUfc7.mjs +725 -0
  46. package/dist/pipeline-analytics/index-k0CA0h_r.mjs +185 -0
  47. package/dist/pipeline-analytics/index-kIgjN-uq.mjs +435 -0
  48. package/dist/pipeline-analytics/index-xncRG7-x.mjs +2713 -0
  49. package/dist/pipeline-analytics/index.js +2563 -0
  50. package/dist/pipeline-analytics/index.js.map +1 -0
  51. package/dist/pipeline-analytics/index.mjs +2564 -0
  52. package/dist/pipeline-analytics/index.mjs.map +1 -0
  53. package/dist/pipeline-analytics/jsx-runtime-4ro1c69i.mjs +55 -0
  54. package/dist/pipeline-analytics/remoteEntry.js +2973 -0
  55. package/dist/pipeline-analytics/virtualExposes-8FzWTdq3.mjs +42 -0
  56. package/dist/playlist-generator-EhPaB7Hn.js +48 -0
  57. package/dist/playlist-generator-EhPaB7Hn.js.map +1 -0
  58. package/dist/playlist-generator-VTkgn53O.mjs +48 -0
  59. package/dist/playlist-generator-VTkgn53O.mjs.map +1 -0
  60. package/dist/recording/index.js +257 -0
  61. package/dist/recording/index.js.map +1 -0
  62. package/dist/recording/index.mjs +235 -0
  63. package/dist/recording/index.mjs.map +1 -0
  64. package/dist/recording-coordinator-C2sATEhe.js +1052 -0
  65. package/dist/recording-coordinator-C2sATEhe.js.map +1 -0
  66. package/dist/recording-coordinator-DuP3BUTV.mjs +1012 -0
  67. package/dist/recording-coordinator-DuP3BUTV.mjs.map +1 -0
  68. package/dist/recording-db-gOgaoQh0.js +348 -0
  69. package/dist/recording-db-gOgaoQh0.js.map +1 -0
  70. package/dist/recording-db-lIkSMTLq.mjs +348 -0
  71. package/dist/recording-db-lIkSMTLq.mjs.map +1 -0
  72. package/dist/recording-service-facade-B9lG6OFn.mjs +123 -0
  73. package/dist/recording-service-facade-B9lG6OFn.mjs.map +1 -0
  74. package/dist/recording-service-facade-Do1PKlAL.js +123 -0
  75. package/dist/recording-service-facade-Do1PKlAL.js.map +1 -0
  76. package/dist/storage-estimator-CRpoQc9j.js +72 -0
  77. package/dist/storage-estimator-CRpoQc9j.js.map +1 -0
  78. package/dist/storage-estimator-DzD8gWJH.mjs +72 -0
  79. package/dist/storage-estimator-DzD8gWJH.mjs.map +1 -0
  80. package/package.json +148 -0
@@ -0,0 +1,774 @@
1
+ import { EventCategory, createEvent, BaseAddon, asJsonObject } from "@camstack/types";
2
+ import sharp from "sharp";
3
+ import { z } from "zod";
4
+ const DEFAULT_ENRICHMENT_CONFIG = {
5
+ enabled: false,
6
+ embedding: {
7
+ enabled: false,
8
+ modelId: "clip-vit-b32",
9
+ agentId: "local",
10
+ runtime: "node",
11
+ backend: "cpu",
12
+ classes: [],
13
+ minConfidence: 0.5,
14
+ maxPerSecPerCamera: 1,
15
+ cropStrategy: "first",
16
+ retentionDays: 30
17
+ },
18
+ sceneMonitor: {
19
+ enabled: false,
20
+ modelId: "clip-vit-b32",
21
+ pollIntervalSec: 10,
22
+ hysteresisCount: 3
23
+ },
24
+ activitySummary: {
25
+ enabled: false,
26
+ intervalSec: 60,
27
+ activityThresholds: {
28
+ low: 2,
29
+ medium: 10,
30
+ high: 30
31
+ }
32
+ }
33
+ };
34
+ async function extractCrop(frameData, frameWidth, frameHeight, bbox) {
35
+ const rawLeft = Math.round(bbox.x * frameWidth);
36
+ const rawTop = Math.round(bbox.y * frameHeight);
37
+ const rawWidth = Math.round(bbox.w * frameWidth);
38
+ const rawHeight = Math.round(bbox.h * frameHeight);
39
+ const left = Math.max(0, Math.min(rawLeft, frameWidth - 1));
40
+ const top = Math.max(0, Math.min(rawTop, frameHeight - 1));
41
+ const width = Math.max(1, Math.min(rawWidth, frameWidth - left));
42
+ const height = Math.max(1, Math.min(rawHeight, frameHeight - top));
43
+ const crop = await sharp(frameData, {
44
+ raw: { width: frameWidth, height: frameHeight, channels: 3 }
45
+ }).extract({ left, top, width, height }).jpeg({ quality: 90 }).toBuffer();
46
+ return { crop, width, height };
47
+ }
48
+ class EmbeddingDispatcher {
49
+ config;
50
+ encoders;
51
+ eventBus;
52
+ logger;
53
+ lastEmbedTime = /* @__PURE__ */ new Map();
54
+ pendingCrops = /* @__PURE__ */ new Map();
55
+ flushTimer = null;
56
+ unsubscribe = null;
57
+ _processedCount = 0;
58
+ _totalInferenceMs = 0;
59
+ _encoderIndex = 0;
60
+ constructor(deps) {
61
+ this.config = deps.config;
62
+ this.encoders = deps.encoders;
63
+ this.eventBus = deps.eventBus;
64
+ this.logger = deps.logger;
65
+ }
66
+ async start() {
67
+ if (!this.config.enabled) {
68
+ this.logger.info("EmbeddingDispatcher disabled");
69
+ return;
70
+ }
71
+ this.unsubscribe = this.eventBus.subscribe(
72
+ { category: EventCategory.DetectionResult },
73
+ (event) => {
74
+ void this.handleDetectionResult(event);
75
+ }
76
+ );
77
+ if (this.config.cropStrategy === "best-confidence") {
78
+ this.flushTimer = setInterval(() => {
79
+ void this.flushPending();
80
+ }, 1e3);
81
+ }
82
+ this.logger.info("EmbeddingDispatcher started", { meta: { strategy: this.config.cropStrategy, maxPerSec: this.config.maxPerSecPerCamera } });
83
+ }
84
+ async stop() {
85
+ this.unsubscribe?.();
86
+ this.unsubscribe = null;
87
+ if (this.flushTimer) {
88
+ clearInterval(this.flushTimer);
89
+ this.flushTimer = null;
90
+ }
91
+ await this.flushPending();
92
+ }
93
+ get processedCount() {
94
+ return this._processedCount;
95
+ }
96
+ get avgInferenceMs() {
97
+ return this._processedCount > 0 ? this._totalInferenceMs / this._processedCount : 0;
98
+ }
99
+ get queueDepth() {
100
+ return this.pendingCrops.size;
101
+ }
102
+ async handleDetectionResult(event) {
103
+ const data = event.data;
104
+ const deviceId = event.source.id !== void 0 ? String(event.source.id) : "";
105
+ if (!deviceId) return;
106
+ const detections = data.analysisResults ?? [];
107
+ const pipelineResult = data.pipelineResult;
108
+ for (const det of detections) {
109
+ const detection = det.detection;
110
+ if (!detection) continue;
111
+ if (this.config.classes.length > 0 && !this.config.classes.includes(detection.class)) continue;
112
+ if (detection.score < this.config.minConfidence) continue;
113
+ const now = Date.now();
114
+ const minInterval = 1e3 / this.config.maxPerSecPerCamera;
115
+ const lastTime = this.lastEmbedTime.get(deviceId) ?? 0;
116
+ if (now - lastTime < minInterval) continue;
117
+ const trackId = detection.trackId ?? `${deviceId}-${now}`;
118
+ const frameData = pipelineResult.frameData;
119
+ const frameWidth = pipelineResult.imageWidth;
120
+ const frameHeight = pipelineResult.imageHeight;
121
+ if (!frameData || !frameWidth || !frameHeight) continue;
122
+ const pending = {
123
+ trackId,
124
+ deviceId,
125
+ class: detection.class,
126
+ confidence: detection.score,
127
+ frameData,
128
+ frameWidth,
129
+ frameHeight,
130
+ bbox: detection.bbox,
131
+ receivedAt: now
132
+ };
133
+ switch (this.config.cropStrategy) {
134
+ case "first": {
135
+ if (!this.pendingCrops.has(trackId)) {
136
+ this.pendingCrops.set(trackId, pending);
137
+ void this.processOne(pending);
138
+ }
139
+ break;
140
+ }
141
+ case "best-confidence": {
142
+ const existing = this.pendingCrops.get(trackId);
143
+ if (!existing || pending.confidence > existing.confidence) {
144
+ this.pendingCrops.set(trackId, pending);
145
+ }
146
+ break;
147
+ }
148
+ case "track-end": {
149
+ const state = det.objectState?.state;
150
+ if (state === "leaving") {
151
+ this.pendingCrops.set(trackId, pending);
152
+ void this.processOne(pending);
153
+ } else {
154
+ const existing = this.pendingCrops.get(trackId);
155
+ if (!existing || pending.confidence > existing.confidence) {
156
+ this.pendingCrops.set(trackId, pending);
157
+ }
158
+ }
159
+ break;
160
+ }
161
+ }
162
+ }
163
+ }
164
+ async flushPending() {
165
+ const now = Date.now();
166
+ const toFlush = [];
167
+ for (const [trackId, pending] of this.pendingCrops) {
168
+ if (now - pending.receivedAt > 3e3) {
169
+ toFlush.push(pending);
170
+ this.pendingCrops.delete(trackId);
171
+ }
172
+ }
173
+ await Promise.all(toFlush.map((p) => this.processOne(p)));
174
+ }
175
+ async processOne(pending) {
176
+ if (this.encoders.length === 0) return;
177
+ try {
178
+ const { crop, width, height } = await extractCrop(
179
+ pending.frameData,
180
+ pending.frameWidth,
181
+ pending.frameHeight,
182
+ pending.bbox
183
+ );
184
+ const encoder = this.encoders[this._encoderIndex % this.encoders.length];
185
+ this._encoderIndex++;
186
+ const { embedding: _embedding, inferenceMs } = await encoder.encode(crop, width, height);
187
+ const info = encoder.getInfo();
188
+ this._processedCount++;
189
+ this._totalInferenceMs += inferenceMs;
190
+ this.lastEmbedTime.set(pending.deviceId, Date.now());
191
+ this.pendingCrops.delete(pending.trackId);
192
+ const embeddingId = `${pending.deviceId}/${pending.trackId}/${Date.now()}`;
193
+ const payload = {
194
+ deviceId: Number(pending.deviceId),
195
+ trackId: pending.trackId,
196
+ class: pending.class,
197
+ embeddingId,
198
+ modelId: info.modelId,
199
+ embeddingDim: info.embeddingDim,
200
+ inferenceMs,
201
+ timestamp: Date.now()
202
+ };
203
+ this.eventBus.emit(createEvent(
204
+ EventCategory.EnrichmentEmbeddingStored,
205
+ { type: "addon", id: "enrichment-engine" },
206
+ payload
207
+ ));
208
+ this.logger.debug("Embedded track", {
209
+ tags: { deviceId: Number(pending.deviceId) },
210
+ meta: { class: pending.class, trackId: pending.trackId, inferenceMs: Number(inferenceMs.toFixed(1)) }
211
+ });
212
+ } catch (err) {
213
+ this.logger.warn("Failed to embed track", {
214
+ tags: { deviceId: Number(pending.deviceId) },
215
+ meta: { trackId: pending.trackId, error: String(err) }
216
+ });
217
+ }
218
+ }
219
+ }
220
+ const TrackPositionSchema = z.object({
221
+ x: z.number(),
222
+ y: z.number(),
223
+ timestamp: z.number(),
224
+ bbox: z.tuple([z.number(), z.number(), z.number(), z.number()])
225
+ });
226
+ const TrackSnapshotSchema = z.object({
227
+ timestamp: z.number(),
228
+ position: TrackPositionSchema,
229
+ thumbnailPath: z.string()
230
+ });
231
+ z.object({
232
+ trackId: z.string(),
233
+ deviceId: z.string(),
234
+ className: z.string(),
235
+ label: z.string().optional(),
236
+ firstSeen: z.number(),
237
+ lastSeen: z.number(),
238
+ positions: z.array(TrackPositionSchema),
239
+ snapshots: z.array(TrackSnapshotSchema),
240
+ totalDistance: z.number(),
241
+ zonesVisited: z.array(z.string()),
242
+ active: z.boolean()
243
+ });
244
+ const SceneMonitorConfigSchema = z.object({
245
+ monitors: z.array(z.object({
246
+ id: z.string(),
247
+ label: z.string(),
248
+ roi: z.object({ x: z.number(), y: z.number(), w: z.number(), h: z.number() }),
249
+ enabled: z.boolean(),
250
+ states: z.array(z.object({
251
+ id: z.string(),
252
+ label: z.string(),
253
+ prompt: z.string().optional(),
254
+ textPrompts: z.array(z.string()).optional(),
255
+ referenceEmbeddings: z.array(z.array(z.number())).optional(),
256
+ threshold: z.number().optional(),
257
+ notify: z.boolean().optional(),
258
+ severity: z.enum(["info", "warning", "alert"]).optional()
259
+ }))
260
+ }))
261
+ });
262
+ class TextEmbeddingCache {
263
+ cache = /* @__PURE__ */ new Map();
264
+ get(key) {
265
+ return this.cache.get(key);
266
+ }
267
+ set(key, embedding) {
268
+ this.cache.set(key, embedding);
269
+ }
270
+ has(key) {
271
+ return this.cache.has(key);
272
+ }
273
+ /**
274
+ * Deletes all entries whose key starts with `${monitorId}:`.
275
+ * Used when a scene monitor's text prompts are updated.
276
+ */
277
+ invalidateMonitor(monitorId) {
278
+ const prefix = `${monitorId}:`;
279
+ for (const key of this.cache.keys()) {
280
+ if (key.startsWith(prefix)) {
281
+ this.cache.delete(key);
282
+ }
283
+ }
284
+ }
285
+ clear() {
286
+ this.cache.clear();
287
+ }
288
+ get size() {
289
+ return this.cache.size;
290
+ }
291
+ }
292
+ class SceneStateWorker {
293
+ config;
294
+ encoders;
295
+ eventBus;
296
+ store;
297
+ streamBrokerRegistry;
298
+ logger;
299
+ textCache = new TextEmbeddingCache();
300
+ monitorStates = /* @__PURE__ */ new Map();
301
+ pollTimer = null;
302
+ _activeCount = 0;
303
+ constructor(deps) {
304
+ this.config = deps.config;
305
+ this.encoders = deps.encoders;
306
+ this.eventBus = deps.eventBus;
307
+ this.store = deps.store;
308
+ this.streamBrokerRegistry = deps.streamBrokerRegistry;
309
+ this.logger = deps.logger;
310
+ }
311
+ async start() {
312
+ if (!this.config.enabled) {
313
+ this.logger.info("SceneStateWorker disabled");
314
+ return;
315
+ }
316
+ this.pollTimer = setInterval(
317
+ () => {
318
+ void this.pollAll();
319
+ },
320
+ this.config.pollIntervalSec * 1e3
321
+ );
322
+ this.logger.info("SceneStateWorker started", { meta: { pollIntervalSec: this.config.pollIntervalSec, hysteresis: this.config.hysteresisCount } });
323
+ }
324
+ async stop() {
325
+ if (this.pollTimer) {
326
+ clearInterval(this.pollTimer);
327
+ this.pollTimer = null;
328
+ }
329
+ }
330
+ get activeCount() {
331
+ return this._activeCount;
332
+ }
333
+ async pollAll() {
334
+ const rawConfigs = await this.store.query.query({ collection: "device-settings", filter: {
335
+ where: { id: "enrichment:scene-monitor:%" }
336
+ } });
337
+ const allConfigs = rawConfigs.map((r) => ({ id: r.id, data: SceneMonitorConfigSchema.parse(r.data) }));
338
+ const deviceMonitors = /* @__PURE__ */ new Map();
339
+ for (const record of allConfigs) {
340
+ const deviceId = record.id.replace("enrichment:scene-monitor:", "");
341
+ const config = record.data;
342
+ const active = config.monitors.filter((m) => m.enabled);
343
+ if (active.length > 0) {
344
+ deviceMonitors.set(deviceId, active);
345
+ }
346
+ }
347
+ this._activeCount = [...deviceMonitors.values()].reduce((sum, ms) => sum + ms.length, 0);
348
+ await Promise.all(
349
+ [...deviceMonitors.entries()].map(
350
+ ([deviceId, monitors]) => this.pollCamera(deviceId, monitors)
351
+ )
352
+ );
353
+ }
354
+ async pollCamera(deviceId, monitors) {
355
+ if (this.encoders.length === 0) return;
356
+ try {
357
+ const snapshot = await this.streamBrokerRegistry.getSnapshot(deviceId);
358
+ if (!snapshot) return;
359
+ const encoder = this.encoders[0];
360
+ for (const monitor of monitors) {
361
+ try {
362
+ await this.processMonitor(deviceId, monitor, snapshot, encoder);
363
+ } catch (err) {
364
+ this.logger.warn("SceneState error", { tags: { deviceId: Number(deviceId) }, meta: { monitorLabel: monitor.label, error: String(err) } });
365
+ }
366
+ }
367
+ } catch (err) {
368
+ this.logger.warn("Failed to capture snapshot", { tags: { deviceId: Number(deviceId) }, meta: { error: String(err) } });
369
+ }
370
+ }
371
+ async processMonitor(deviceId, monitor, snapshot, encoder) {
372
+ const bbox = {
373
+ x: monitor.roi.x * snapshot.width,
374
+ y: monitor.roi.y * snapshot.height,
375
+ w: monitor.roi.w * snapshot.width,
376
+ h: monitor.roi.h * snapshot.height
377
+ };
378
+ const { crop, width, height } = await extractCrop(snapshot.data, snapshot.width, snapshot.height, bbox);
379
+ const { embedding: imageEmb } = await encoder.encode(crop, width, height);
380
+ let bestState = null;
381
+ let bestScore = -1;
382
+ for (const state of monitor.states) {
383
+ let score = 0;
384
+ if (state.referenceEmbeddings && state.referenceEmbeddings.length > 0) {
385
+ for (const refEmb of state.referenceEmbeddings) {
386
+ const ref = new Float32Array(refEmb);
387
+ const sim = cosineSimilarity(imageEmb, ref);
388
+ score = Math.max(score, sim);
389
+ }
390
+ } else if (state.textPrompts && state.textPrompts.length > 0) {
391
+ for (const prompt of state.textPrompts) {
392
+ const cacheKey = `${monitor.id}:${state.id}:${prompt}`;
393
+ let textEmb = this.textCache.get(cacheKey);
394
+ if (!textEmb) {
395
+ const result = await encoder.encodeText(prompt);
396
+ textEmb = result.embedding;
397
+ this.textCache.set(cacheKey, textEmb);
398
+ }
399
+ const sim = cosineSimilarity(imageEmb, textEmb);
400
+ score = Math.max(score, sim);
401
+ }
402
+ }
403
+ if (score > bestScore) {
404
+ bestScore = score;
405
+ bestState = state.label;
406
+ }
407
+ }
408
+ if (!bestState) return;
409
+ const key = `${deviceId}:${monitor.id}`;
410
+ let ms = this.monitorStates.get(key);
411
+ if (!ms) {
412
+ ms = { currentState: null, pendingState: null, pendingCount: 0, pendingConfidence: 0 };
413
+ this.monitorStates.set(key, ms);
414
+ }
415
+ if (bestState === ms.pendingState) {
416
+ ms.pendingCount++;
417
+ ms.pendingConfidence = bestScore;
418
+ } else {
419
+ ms.pendingState = bestState;
420
+ ms.pendingCount = 1;
421
+ ms.pendingConfidence = bestScore;
422
+ }
423
+ if (ms.pendingCount < this.config.hysteresisCount) return;
424
+ if (bestState === ms.currentState) return;
425
+ const previousState = ms.currentState ?? "unknown";
426
+ ms.currentState = bestState;
427
+ ms.pendingState = null;
428
+ ms.pendingCount = 0;
429
+ const payload = {
430
+ deviceId: Number(deviceId),
431
+ monitorId: monitor.id,
432
+ monitorLabel: monitor.label,
433
+ previousState,
434
+ currentState: bestState,
435
+ confidence: bestScore,
436
+ timestamp: Date.now()
437
+ };
438
+ const data = { ...payload };
439
+ this.eventBus.emit({
440
+ id: `scene-state-${deviceId}-${monitor.id}-${Date.now()}`,
441
+ category: EventCategory.EnrichmentSceneStateChanged,
442
+ source: { type: "device", id: deviceId },
443
+ timestamp: /* @__PURE__ */ new Date(),
444
+ data
445
+ });
446
+ this.logger.info("Scene state changed", {
447
+ tags: { deviceId: Number(deviceId) },
448
+ meta: { monitorLabel: monitor.label, previousState, currentState: bestState, confidence: Number(bestScore.toFixed(2)) }
449
+ });
450
+ }
451
+ }
452
+ function cosineSimilarity(a, b) {
453
+ let dot = 0;
454
+ let normA = 0;
455
+ let normB = 0;
456
+ for (let i = 0; i < a.length; i++) {
457
+ dot += a[i] * b[i];
458
+ normA += a[i] * a[i];
459
+ normB += b[i] * b[i];
460
+ }
461
+ return dot / (Math.sqrt(normA) * Math.sqrt(normB));
462
+ }
463
+ class ActivitySummaryWorker {
464
+ config;
465
+ eventBus;
466
+ store;
467
+ logger;
468
+ buffers = /* @__PURE__ */ new Map();
469
+ summaryTimer = null;
470
+ unsubDetection = null;
471
+ unsubSceneState = null;
472
+ _lastSummary = null;
473
+ constructor(deps) {
474
+ this.config = deps.config;
475
+ this.eventBus = deps.eventBus;
476
+ this.store = deps.store;
477
+ this.logger = deps.logger;
478
+ }
479
+ async start() {
480
+ if (!this.config.enabled) {
481
+ this.logger.info("ActivitySummaryWorker disabled");
482
+ return;
483
+ }
484
+ this.unsubDetection = this.eventBus.subscribe(
485
+ { category: EventCategory.DetectionResult },
486
+ (event) => {
487
+ this.handleDetection(event);
488
+ }
489
+ );
490
+ this.unsubSceneState = this.eventBus.subscribe(
491
+ { category: EventCategory.EnrichmentSceneStateChanged },
492
+ (event) => {
493
+ this.handleSceneStateChange(event);
494
+ }
495
+ );
496
+ this.summaryTimer = setInterval(
497
+ () => {
498
+ void this.emitSummaries();
499
+ },
500
+ this.config.intervalSec * 1e3
501
+ );
502
+ this.logger.info("ActivitySummaryWorker started", { meta: { intervalSec: this.config.intervalSec } });
503
+ }
504
+ async stop() {
505
+ this.unsubDetection?.();
506
+ this.unsubSceneState?.();
507
+ if (this.summaryTimer) {
508
+ clearInterval(this.summaryTimer);
509
+ this.summaryTimer = null;
510
+ }
511
+ await this.emitSummaries();
512
+ }
513
+ get lastSummary() {
514
+ return this._lastSummary;
515
+ }
516
+ getBuffer(deviceId) {
517
+ let buf = this.buffers.get(deviceId);
518
+ if (!buf) {
519
+ buf = { tracks: /* @__PURE__ */ new Map(), zoneEvents: [], stateChanges: [] };
520
+ this.buffers.set(deviceId, buf);
521
+ }
522
+ return buf;
523
+ }
524
+ handleDetection(event) {
525
+ const deviceId = event.source.id !== void 0 ? String(event.source.id) : "";
526
+ if (!deviceId) return;
527
+ const results = event.data.analysisResults ?? [];
528
+ const buf = this.getBuffer(deviceId);
529
+ const now = Date.now();
530
+ for (const det of results) {
531
+ const detection = det.detection;
532
+ if (!detection) continue;
533
+ const trackId = detection.trackId ?? `unknown-${now}`;
534
+ const existing = buf.tracks.get(trackId);
535
+ if (existing) {
536
+ existing.lastSeen = now;
537
+ } else {
538
+ buf.tracks.set(trackId, {
539
+ class: detection.class,
540
+ deviceId,
541
+ firstSeen: now,
542
+ lastSeen: now,
543
+ zones: /* @__PURE__ */ new Set()
544
+ });
545
+ }
546
+ const zoneEvents = det.zoneEvents ?? [];
547
+ for (const ze of zoneEvents) {
548
+ const track = buf.tracks.get(trackId);
549
+ if (track) track.zones.add(ze.zoneId);
550
+ if (ze.type === "zone-enter" || ze.type === "zone-exit") {
551
+ buf.zoneEvents.push({
552
+ type: ze.type === "zone-enter" ? "enter" : "exit",
553
+ zoneId: ze.zoneId,
554
+ trackId,
555
+ timestamp: ze.timestamp ?? now
556
+ });
557
+ }
558
+ }
559
+ }
560
+ }
561
+ handleSceneStateChange(event) {
562
+ const deviceId = event.source.id !== void 0 ? String(event.source.id) : "";
563
+ if (!deviceId) return;
564
+ const data = event.data;
565
+ const buf = this.getBuffer(deviceId);
566
+ buf.stateChanges.push({
567
+ monitorId: data.monitorId,
568
+ from: data.previousState,
569
+ to: data.currentState,
570
+ timestamp: data.timestamp
571
+ });
572
+ }
573
+ async emitSummaries() {
574
+ const now = Date.now();
575
+ for (const [deviceId, buf] of this.buffers) {
576
+ if (buf.tracks.size === 0 && buf.zoneEvents.length === 0 && buf.stateChanges.length === 0) {
577
+ continue;
578
+ }
579
+ const objectCounts = {};
580
+ for (const track of buf.tracks.values()) {
581
+ objectCounts[track.class] = (objectCounts[track.class] ?? 0) + 1;
582
+ }
583
+ const zoneMap = /* @__PURE__ */ new Map();
584
+ for (const ze of buf.zoneEvents) {
585
+ let z2 = zoneMap.get(ze.zoneId);
586
+ if (!z2) {
587
+ z2 = { entries: 0, exits: 0, dwellTimes: [] };
588
+ zoneMap.set(ze.zoneId, z2);
589
+ }
590
+ if (ze.type === "enter") z2.entries++;
591
+ else z2.exits++;
592
+ }
593
+ const zoneActivity = [...zoneMap.entries()].map(([zoneId, z2]) => ({
594
+ zoneId,
595
+ entries: z2.entries,
596
+ exits: z2.exits,
597
+ avgDwellMs: z2.dwellTimes.length > 0 ? z2.dwellTimes.reduce((a, b) => a + b, 0) / z2.dwellTimes.length : 0
598
+ }));
599
+ const totalEvents = buf.tracks.size + buf.zoneEvents.length;
600
+ const eventsPerMin = totalEvents / (this.config.intervalSec / 60);
601
+ const activityLevel = eventsPerMin >= this.config.activityThresholds.high ? "high" : eventsPerMin >= this.config.activityThresholds.medium ? "medium" : eventsPerMin >= this.config.activityThresholds.low ? "low" : "none";
602
+ const summary = {
603
+ deviceId: Number(deviceId),
604
+ periodStart: now - this.config.intervalSec * 1e3,
605
+ periodEnd: now,
606
+ objectCounts,
607
+ zoneActivity,
608
+ stateChanges: [...buf.stateChanges],
609
+ activityLevel
610
+ };
611
+ this._lastSummary = summary;
612
+ this.eventBus.emit(createEvent(
613
+ EventCategory.EnrichmentActivitySummary,
614
+ { type: "device", id: deviceId },
615
+ summary
616
+ ));
617
+ try {
618
+ await this.store.insert.mutate({ collection: "addon-settings", record: {
619
+ id: `${deviceId}:${now}`,
620
+ data: { ...summary }
621
+ } });
622
+ } catch {
623
+ }
624
+ buf.tracks.clear();
625
+ buf.zoneEvents.length = 0;
626
+ buf.stateChanges.length = 0;
627
+ }
628
+ }
629
+ }
630
+ class EnrichmentEngineAddon extends BaseAddon {
631
+ embeddingDispatcher = null;
632
+ sceneStateWorker = null;
633
+ activitySummary = null;
634
+ currentFlags = {
635
+ embeddingEnabled: true,
636
+ sceneMonitorEnabled: true,
637
+ activitySummaryEnabled: true
638
+ };
639
+ constructor() {
640
+ super({});
641
+ }
642
+ async onInitialize() {
643
+ const config = await this.loadConfig();
644
+ const encoderCollection = this.capabilities?.getCollection?.("embedding-encoder") ?? [];
645
+ const streamBrokerRaw = this.capabilities?.get?.("stream-broker");
646
+ this.embeddingDispatcher = new EmbeddingDispatcher({
647
+ config: config.embedding,
648
+ encoders: encoderCollection,
649
+ eventBus: this.ctx.eventBus,
650
+ logger: this.ctx.logger.child("EmbeddingDispatcher")
651
+ });
652
+ this.sceneStateWorker = new SceneStateWorker({
653
+ config: config.sceneMonitor,
654
+ encoders: encoderCollection,
655
+ eventBus: this.ctx.eventBus,
656
+ store: this.ctx.api.settingsStore,
657
+ streamBrokerRegistry: streamBrokerRaw ?? { getSnapshot: async () => null },
658
+ logger: this.ctx.logger.child("SceneStateWorker")
659
+ });
660
+ this.activitySummary = new ActivitySummaryWorker({
661
+ config: config.activitySummary,
662
+ eventBus: this.ctx.eventBus,
663
+ store: this.ctx.api.settingsStore,
664
+ logger: this.ctx.logger.child("ActivitySummary")
665
+ });
666
+ this.currentFlags = {
667
+ embeddingEnabled: config.embedding.enabled,
668
+ sceneMonitorEnabled: config.sceneMonitor.enabled,
669
+ activitySummaryEnabled: config.activitySummary.enabled
670
+ };
671
+ await this.embeddingDispatcher.start();
672
+ await this.sceneStateWorker.start();
673
+ await this.activitySummary.start();
674
+ this.ctx.logger.info("Enrichment engine initialized with 3 workers");
675
+ }
676
+ async onShutdown() {
677
+ await this.embeddingDispatcher?.stop();
678
+ await this.sceneStateWorker?.stop();
679
+ await this.activitySummary?.stop();
680
+ this.embeddingDispatcher = null;
681
+ this.sceneStateWorker = null;
682
+ this.activitySummary = null;
683
+ }
684
+ // ── Three-level settings API (Phase 3) ─────────────────────────────
685
+ //
686
+ // Enrichment engine is post-detection infra. The three boolean toggle
687
+ // flags (embedding/scene/activity) live in `getGlobalSettings` until
688
+ // the dedicated post-detection UI exists. The underlying
689
+ // EnrichmentConfig blob (stored opaquely in 'addon-settings' →
690
+ // 'enrichment:global') is a separate concern — the flags below mirror
691
+ // the `.enabled` field of each worker's config.
692
+ globalSettingsSchema() {
693
+ return this.schema({
694
+ sections: [
695
+ {
696
+ id: "enrichment-engine-settings",
697
+ title: "Enrichment Engine",
698
+ columns: 2,
699
+ fields: [
700
+ {
701
+ type: "boolean",
702
+ key: "embeddingEnabled",
703
+ label: "Embedding Enabled",
704
+ description: "Compute face/object embeddings from detection crops.",
705
+ default: true
706
+ },
707
+ {
708
+ type: "boolean",
709
+ key: "sceneMonitorEnabled",
710
+ label: "Scene Monitor Enabled",
711
+ description: "Run periodic scene state capture for change detection.",
712
+ default: true
713
+ },
714
+ {
715
+ type: "boolean",
716
+ key: "activitySummaryEnabled",
717
+ label: "Activity Summary Enabled",
718
+ description: "Aggregate hourly activity summaries per camera.",
719
+ default: true
720
+ }
721
+ ]
722
+ }
723
+ ]
724
+ });
725
+ }
726
+ async updateGlobalSettings(patch) {
727
+ await this.ctx?.settings?.writeAddonStore(patch);
728
+ const prev = this.currentFlags;
729
+ const next = {
730
+ embeddingEnabled: typeof patch["embeddingEnabled"] === "boolean" ? patch["embeddingEnabled"] : prev.embeddingEnabled,
731
+ sceneMonitorEnabled: typeof patch["sceneMonitorEnabled"] === "boolean" ? patch["sceneMonitorEnabled"] : prev.sceneMonitorEnabled,
732
+ activitySummaryEnabled: typeof patch["activitySummaryEnabled"] === "boolean" ? patch["activitySummaryEnabled"] : prev.activitySummaryEnabled
733
+ };
734
+ this.currentFlags = next;
735
+ if (prev.embeddingEnabled && !next.embeddingEnabled) {
736
+ this.ctx?.logger.info("Stopping embedding dispatcher (disabled via config)");
737
+ await this.embeddingDispatcher?.stop();
738
+ } else if (!prev.embeddingEnabled && next.embeddingEnabled) {
739
+ this.ctx?.logger.info("Starting embedding dispatcher (enabled via config)");
740
+ await this.embeddingDispatcher?.start();
741
+ }
742
+ if (prev.sceneMonitorEnabled && !next.sceneMonitorEnabled) {
743
+ this.ctx?.logger.info("Stopping scene state worker (disabled via config)");
744
+ await this.sceneStateWorker?.stop();
745
+ } else if (!prev.sceneMonitorEnabled && next.sceneMonitorEnabled) {
746
+ this.ctx?.logger.info("Starting scene state worker (enabled via config)");
747
+ await this.sceneStateWorker?.start();
748
+ }
749
+ if (prev.activitySummaryEnabled && !next.activitySummaryEnabled) {
750
+ this.ctx?.logger.info("Stopping activity summary worker (disabled via config)");
751
+ await this.activitySummary?.stop();
752
+ } else if (!prev.activitySummaryEnabled && next.activitySummaryEnabled) {
753
+ this.ctx?.logger.info("Starting activity summary worker (enabled via config)");
754
+ await this.activitySummary?.start();
755
+ }
756
+ this.ctx?.logger.info("Enrichment engine flags updated", { meta: { flags: this.currentFlags } });
757
+ }
758
+ async loadConfig() {
759
+ try {
760
+ const stored = asJsonObject(await this.ctx.api?.settingsStore.get.query({ collection: "addon-settings", key: "enrichment:global" }));
761
+ if (stored) {
762
+ const merged = { ...DEFAULT_ENRICHMENT_CONFIG, ...stored };
763
+ return merged;
764
+ }
765
+ } catch {
766
+ }
767
+ return DEFAULT_ENRICHMENT_CONFIG;
768
+ }
769
+ }
770
+ export {
771
+ EnrichmentEngineAddon,
772
+ EnrichmentEngineAddon as default
773
+ };
774
+ //# sourceMappingURL=index.mjs.map