@popmelt.com/core 0.16.0 → 0.18.0

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.
@@ -73,6 +73,28 @@ type ImageObjectContext = {
73
73
  height: number;
74
74
  };
75
75
  };
76
+ /** Coordinates refer to the decoded video frame, not its responsive DOM box. */
77
+ type VideoObjectContext = {
78
+ src: string;
79
+ width: number;
80
+ height: number;
81
+ duration: number;
82
+ currentTime: number;
83
+ normalizedBox: {
84
+ x: number;
85
+ y: number;
86
+ width: number;
87
+ height: number;
88
+ };
89
+ box: {
90
+ x: number;
91
+ y: number;
92
+ width: number;
93
+ height: number;
94
+ };
95
+ trackStart: number;
96
+ trackEnd: number;
97
+ };
76
98
  type SubjectFeedbackAttachment = {
77
99
  /** Zero-based index in this annotation's transported images. */
78
100
  cropImageIndex?: number;
@@ -91,6 +113,7 @@ type ElementInfo = {
91
113
  reactComponentPath?: string[];
92
114
  reactSource?: ReactSourceLocation;
93
115
  imageObject?: ImageObjectContext;
116
+ videoObject?: VideoObjectContext;
94
117
  /** The real DOM surface, rather than the subject's hidden projection. */
95
118
  sourceElement?: ElementInfo;
96
119
  context?: string;
@@ -184,6 +207,87 @@ type AnnotationAttachment = {
184
207
  size: number;
185
208
  kind: 'svg' | 'text';
186
209
  };
210
+ type InteractionEpisodeElementState = {
211
+ selector?: string;
212
+ tagName: string;
213
+ className?: string;
214
+ role?: string;
215
+ ariaLabel?: string;
216
+ text?: string;
217
+ value?: string;
218
+ checked?: boolean;
219
+ disabled?: boolean;
220
+ expanded?: boolean;
221
+ pressed?: boolean;
222
+ selected?: boolean;
223
+ rect?: {
224
+ x: number;
225
+ y: number;
226
+ width: number;
227
+ height: number;
228
+ };
229
+ subject?: {
230
+ adapter: string;
231
+ key: string;
232
+ label: string;
233
+ context?: string;
234
+ data?: Record<string, string>;
235
+ };
236
+ media?: {
237
+ kind: 'image';
238
+ src?: string;
239
+ complete: boolean;
240
+ naturalWidth: number;
241
+ naturalHeight: number;
242
+ } | {
243
+ kind: 'video';
244
+ src?: string;
245
+ currentTime: number;
246
+ duration?: number;
247
+ paused: boolean;
248
+ } | {
249
+ kind: 'canvas';
250
+ width: number;
251
+ height: number;
252
+ };
253
+ };
254
+ type InteractionEpisodeChange = {
255
+ kind: 'added' | 'removed' | 'updated';
256
+ element: InteractionEpisodeElementState;
257
+ };
258
+ type InteractionEpisodeStep = {
259
+ id: string;
260
+ action: {
261
+ kind: 'click' | 'drag' | 'input' | 'change' | 'submit' | 'keydown' | 'scroll' | 'play' | 'pause' | 'seek';
262
+ label: string;
263
+ key?: string;
264
+ target?: InteractionEpisodeElementState;
265
+ };
266
+ startedAt: number;
267
+ completedAt: number;
268
+ outcome: 'changed' | 'no-observable-change';
269
+ changes: InteractionEpisodeChange[];
270
+ result?: InteractionEpisodeElementState;
271
+ visuals?: {
272
+ beforeFrameId?: string;
273
+ afterFrameId?: string;
274
+ };
275
+ };
276
+ type InteractionEpisodeFrame = {
277
+ id: string;
278
+ /** Index in the annotation's out-of-band image list. */
279
+ imageIndex: number;
280
+ fingerprint: string;
281
+ };
282
+ /** A compact, semantic account of actions performed while Comment passes through. */
283
+ type InteractionEpisode = {
284
+ version: 1;
285
+ startedAt: number;
286
+ completedAt: number;
287
+ subject?: InteractionEpisodeElementState;
288
+ steps: InteractionEpisodeStep[];
289
+ frames?: InteractionEpisodeFrame[];
290
+ };
187
291
  type Annotation = {
188
292
  id: string;
189
293
  type: AnnotationType;
@@ -210,6 +314,7 @@ type Annotation = {
210
314
  linkedAnchor?: 'top-left' | 'bottom-left';
211
315
  imageCount?: number;
212
316
  attachments?: AnnotationAttachment[];
317
+ interactionEpisode?: InteractionEpisode;
213
318
  };
214
319
  type BridgeStatus = {
215
320
  ok: boolean;
@@ -234,6 +339,12 @@ type PopmeltSubjectBounds = {
234
339
  width: number;
235
340
  height: number;
236
341
  };
342
+ type PopmeltSubjectTrackingAnchor = {
343
+ /** Horizontal position normalized within the subject bounds. */
344
+ x: number;
345
+ /** Vertical position normalized within the subject bounds. */
346
+ y: number;
347
+ };
237
348
  type PopmeltSubject = {
238
349
  key: string;
239
350
  label: string;
@@ -245,6 +356,9 @@ type PopmeltSubject = {
245
356
  /** Live owning surface; never serialized or included in model context. */
246
357
  sourceElement?: Element;
247
358
  imageObject?: ImageObjectContext;
359
+ videoObject?: VideoObjectContext;
360
+ /** Optional provider-neutral foreground anchors used by motion tracking. */
361
+ trackingAnchors?: readonly PopmeltSubjectTrackingAnchor[];
248
362
  };
249
363
  type PopmeltSurfaceSnapshot = {
250
364
  element: HTMLCanvasElement;
@@ -266,12 +380,16 @@ type PopmeltSubjectCreationSession = {
266
380
  type PopmeltSubjectAdapter = {
267
381
  id: string;
268
382
  /** Surface ownership; an explicit image adapter replaces Core's default. */
269
- surfaceKind?: 'image';
383
+ surfaceKind?: 'image' | 'video';
384
+ /** Resolve once the adapter's persisted subject index is available. */
385
+ whenReady?(): Promise<void>;
270
386
  hitTest(clientPoint: {
271
387
  x: number;
272
388
  y: number;
273
389
  }): PopmeltSubject | null;
274
390
  resolve(key: string): PopmeltSubject | null;
391
+ /** Recreate a missing durable subject from annotation-captured context. */
392
+ restoreSubject?(key: string, elementInfo: ElementInfo): PopmeltSubject | null;
275
393
  /** Optional clean subject crop, without Popmelt's overlay. */
276
394
  captureSubject?(key: string): Blob | null | Promise<Blob | null>;
277
395
  /** Optional bitmap capture for renderer surfaces that DOM cloning cannot read. */
@@ -285,6 +403,8 @@ type PopmeltSubjectAdapter = {
285
403
  renameSubject?(key: string, label: string): PopmeltSubject | null;
286
404
  /** Replace a subject's viewport bounds, projected back into adapter space. */
287
405
  updateSubjectBounds?(key: string, bounds: PopmeltSubjectBounds): PopmeltSubject | null;
406
+ /** Replace optional foreground anchors normalized within subject bounds. */
407
+ updateSubjectTrackingAnchors?(key: string, anchors: readonly PopmeltSubjectTrackingAnchor[]): PopmeltSubject | null;
288
408
  /** Remove a user-created subject or hide an adapter-owned subject. */
289
409
  removeSubject?(key: string): void;
290
410
  };
@@ -398,6 +518,7 @@ declare class ImageSubjectAdapter implements PopmeltSubjectAdapter {
398
518
  y: number;
399
519
  }): PopmeltSubject | null;
400
520
  resolve(key: string): PopmeltSubject | null;
521
+ restoreSubject(key: string, elementInfo: ElementInfo): PopmeltSubject | null;
401
522
  private imageObjectContext;
402
523
  captureSubject(key: string): Promise<Blob | null>;
403
524
  beginCreate(clientPoint: {
@@ -423,4 +544,205 @@ declare class ImageSubjectAdapter implements PopmeltSubjectAdapter {
423
544
  }
424
545
  declare function createImageSubjectAdapter(options?: ImageSubjectAdapterOptions): ImageSubjectAdapter;
425
546
 
426
- export { type Annotation as A, type BridgeStatus as B, type ClaudeResponse as C, type DomModification as D, type ElementInfo as E, type ImageRecognitionProvider as I, type OpaqueTargetReference as O, type PopmeltSubjectAdapter as P, type ReactSourceLocation as R, type SubjectFeedbackAttachment as S, type TextModification as T, type ImageSubjectAdapterOptions as a, type AnnotationAttachment as b, type StyleModification as c, type SpacingTokenChange as d, type AnnotationType as e, type ImageObjectContext as f, type Point as g, type PopmeltSubject as h, type PopmeltSubjectBounds as i, type PopmeltSubjectCreationSession as j, type PopmeltSurfaceSnapshot as k, type StyleChange as l, type ToolType as m, type ImageObjectBox as n, type BrowserImageSubjectStoreOptions as o, type ImageContentHasher as p, type ImageObjectObservation as q, ImageSubjectAdapter as r, type ImageSubjectStore as s, type PersistedImageAnalysis as t, type PersistedImageSubject as u, type PersistedImageSuppression as v, createBrowserImageSubjectStore as w, createImageSubjectAdapter as x, createMemoryImageSubjectStore as y };
547
+ type VideoObjectBox = ImageObjectBox;
548
+ type VideoTrackingAnchor = PopmeltSubjectTrackingAnchor;
549
+ type VideoTrackKeyframe = {
550
+ time: number;
551
+ box: VideoObjectBox;
552
+ score: number;
553
+ source: 'manual' | 'tracked';
554
+ };
555
+ type VideoTrackObservation = {
556
+ time: number;
557
+ box: VideoObjectBox;
558
+ score: number;
559
+ };
560
+ type VideoTrackingAttempt = {
561
+ /** Expand the normal local search crop during reacquisition. */
562
+ searchScale?: number;
563
+ };
564
+ type VideoTrackSegment = {
565
+ /** First media time at which this object is confidently observable. */
566
+ start: number;
567
+ /** Last confidently observable media time. Omitted while the segment is open. */
568
+ end?: number;
569
+ };
570
+ type VideoTrackingSeed = {
571
+ video: HTMLVideoElement;
572
+ time: number;
573
+ box: VideoObjectBox;
574
+ anchors?: readonly VideoTrackingAnchor[];
575
+ };
576
+ type VideoTrackingSession = {
577
+ track(video: HTMLVideoElement, time: number, attempt?: VideoTrackingAttempt): Promise<VideoTrackObservation | null>;
578
+ reseed?(seed: VideoTrackingSeed): void | Promise<void>;
579
+ dispose?(): void | Promise<void>;
580
+ };
581
+ type VideoTrackSmoother = {
582
+ /** Builds one presentation frame from a chronological durable observation. */
583
+ smooth(frame: VideoTrackKeyframe): VideoTrackKeyframe;
584
+ };
585
+ type VideoMotionState = {
586
+ visible: boolean;
587
+ misses: number;
588
+ hits: number;
589
+ lostAt?: number;
590
+ };
591
+ type VideoMotionPresenceDecision = {
592
+ state: VideoMotionState;
593
+ action: 'accept' | 'hold' | 'close' | 'reopen';
594
+ };
595
+ type VideoMotionAttempt = {
596
+ framesPerSecond: number;
597
+ tracking: VideoTrackingAttempt;
598
+ };
599
+ type VideoBackwardAnalysisOptions = {
600
+ source: HTMLVideoElement;
601
+ seedTime: number;
602
+ keyframes: readonly VideoTrackKeyframe[];
603
+ anchors: readonly VideoTrackingAnchor[];
604
+ maximumFramesPerSecond: number;
605
+ cancelled: () => boolean;
606
+ forwardWork: () => Promise<void> | undefined;
607
+ };
608
+ type VideoBackwardAnalysisResult = {
609
+ earliestTime: number;
610
+ keyframes: VideoTrackKeyframe[];
611
+ };
612
+ /**
613
+ * Pixel-derived motion policy supplied by a perception extension. Core invokes
614
+ * it, then stores and renders the resulting durable and presentation tracks.
615
+ */
616
+ type VideoMotionPolicy = {
617
+ createSmoother(): VideoTrackSmoother;
618
+ attempt(options: {
619
+ state: VideoMotionState;
620
+ time: number;
621
+ maximumFramesPerSecond: number;
622
+ }): VideoMotionAttempt;
623
+ observe(options: {
624
+ state: VideoMotionState;
625
+ time: number;
626
+ observation: VideoTrackObservation | null;
627
+ }): VideoMotionPresenceDecision;
628
+ analyzeBackward(options: VideoBackwardAnalysisOptions): Promise<VideoBackwardAnalysisResult>;
629
+ };
630
+ /** Optional local perception boundary. Core owns tracks and authoring. */
631
+ type VideoTrackingProvider = {
632
+ cacheKey: string;
633
+ configurationKey: string;
634
+ motion: VideoMotionPolicy;
635
+ createSession(seed: VideoTrackingSeed): Promise<VideoTrackingSession>;
636
+ dispose?(): void | Promise<void>;
637
+ };
638
+ type PersistedVideoTrack = {
639
+ id: string;
640
+ label: string;
641
+ origin: 'manual' | 'detected';
642
+ keyframes: readonly VideoTrackKeyframe[];
643
+ /** See-derived display geometry, persisted so Core can replay it alone. */
644
+ presentationKeyframes?: readonly VideoTrackKeyframe[];
645
+ anchors?: readonly VideoTrackingAnchor[];
646
+ /** Explicit visible spans. Missing on schema-v1 tracks, which migrate open-ended. */
647
+ segments?: readonly VideoTrackSegment[];
648
+ /** Furthest media time already covered by forward inference. */
649
+ analyzedThrough?: number;
650
+ createdAt: number;
651
+ updatedAt: number;
652
+ };
653
+ type PersistedVideoAnalysis = {
654
+ schemaVersion: 1 | 2 | 3;
655
+ source: string;
656
+ duration: number;
657
+ tracks: readonly PersistedVideoTrack[];
658
+ updatedAt: number;
659
+ };
660
+ type VideoSubjectStore = {
661
+ load(key: string): PersistedVideoAnalysis | null | Promise<PersistedVideoAnalysis | null>;
662
+ save(key: string, analysis: PersistedVideoAnalysis): void | Promise<void>;
663
+ remove?(key: string): void | Promise<void>;
664
+ };
665
+
666
+ type VideoSubjectAdapterOptions = {
667
+ tracking?: VideoTrackingProvider;
668
+ selector?: string;
669
+ root?: Document | Element;
670
+ observe?: boolean;
671
+ id?: string;
672
+ store?: VideoSubjectStore;
673
+ maxTrackingFramesPerSecond?: number;
674
+ onError?: (error: unknown, video: HTMLVideoElement) => void;
675
+ };
676
+ declare class VideoSubjectAdapter implements PopmeltSubjectAdapter {
677
+ private readonly options;
678
+ readonly surfaceKind: "video";
679
+ readonly id: string;
680
+ private readonly records;
681
+ private readonly videoKeys;
682
+ private readonly metadata;
683
+ private readonly analyzedSources;
684
+ private readonly listeners;
685
+ private readonly frameHandles;
686
+ private readonly persistTimers;
687
+ private readonly writeChains;
688
+ private readonly pendingWrites;
689
+ private readonly observer;
690
+ private refreshQueued;
691
+ private destroyed;
692
+ private manualSequence;
693
+ private activeTrackingKey;
694
+ private readyPromise;
695
+ constructor(options?: VideoSubjectAdapterOptions);
696
+ whenReady(): Promise<void>;
697
+ listSubjects(): readonly PopmeltSubject[];
698
+ refresh(): Promise<void>;
699
+ hitTest(point: {
700
+ x: number;
701
+ y: number;
702
+ }): PopmeltSubject | null;
703
+ resolve(key: string): PopmeltSubject | null;
704
+ captureSubject(key: string): Promise<Blob | null>;
705
+ beginCreate(point: {
706
+ x: number;
707
+ y: number;
708
+ }): PopmeltSubjectCreationSession | null;
709
+ renameSubject(key: string, label: string): PopmeltSubject | null;
710
+ updateSubjectBounds(key: string, bounds: PopmeltSubjectBounds): PopmeltSubject | null;
711
+ updateSubjectTrackingAnchors(key: string, anchors: readonly PopmeltSubjectTrackingAnchor[]): PopmeltSubject | null;
712
+ removeSubject(key: string): void;
713
+ destroy(): Promise<void>;
714
+ private analyze;
715
+ private addVideoListeners;
716
+ private removeVideoListeners;
717
+ private scheduleFrame;
718
+ private trackVideo;
719
+ private startSession;
720
+ private reseed;
721
+ private failTracking;
722
+ private activeRecordFor;
723
+ private activateMostRecentRecord;
724
+ private activateTrackingRecord;
725
+ private scheduleBackwardAnalysis;
726
+ private cancelBackwardAnalysis;
727
+ private runBackwardAnalysis;
728
+ private acceptObservation;
729
+ private openSegment;
730
+ private closeSegment;
731
+ private insertKeyframe;
732
+ private rebuildPresentationTrack;
733
+ private clearLiveFrames;
734
+ private videoObjectContext;
735
+ private replaceVideoRecords;
736
+ private persistVideo;
737
+ private schedulePersistence;
738
+ private flushScheduledPersistence;
739
+ private matchingVideos;
740
+ private videoIdentity;
741
+ private newObjectId;
742
+ private removeVideoRecords;
743
+ private removeVideo;
744
+ private queueRefresh;
745
+ }
746
+ declare function createVideoSubjectAdapter(options?: VideoSubjectAdapterOptions): VideoSubjectAdapter;
747
+
748
+ export { type VideoTrackingAttempt as $, type Annotation as A, type BridgeStatus as B, type ClaudeResponse as C, type DomModification as D, type ElementInfo as E, type PersistedImageSuppression as F, type PersistedVideoAnalysis as G, type PersistedVideoTrack as H, type ImageRecognitionProvider as I, type VideoBackwardAnalysisOptions as J, type VideoBackwardAnalysisResult as K, type VideoMotionAttempt as L, type VideoMotionPolicy as M, type VideoMotionPresenceDecision as N, type OpaqueTargetReference as O, type PopmeltSubjectAdapter as P, type VideoMotionState as Q, type ReactSourceLocation as R, type SubjectFeedbackAttachment as S, type TextModification as T, VideoSubjectAdapter as U, type VideoTrackingProvider as V, type VideoTrackKeyframe as W, type VideoTrackObservation as X, type VideoTrackSegment as Y, type VideoTrackSmoother as Z, type VideoTrackingAnchor as _, type ImageSubjectAdapterOptions as a, type VideoTrackingSeed as a0, type VideoTrackingSession as a1, createBrowserImageSubjectStore as a2, createImageSubjectAdapter as a3, createMemoryImageSubjectStore as a4, createVideoSubjectAdapter as a5, type VideoSubjectAdapterOptions as b, type AnnotationAttachment as c, type StyleModification as d, type SpacingTokenChange as e, type AnnotationType as f, type ImageObjectContext as g, type Point as h, type PopmeltSubject as i, type PopmeltSubjectBounds as j, type PopmeltSubjectCreationSession as k, type PopmeltSubjectTrackingAnchor as l, type PopmeltSurfaceSnapshot as m, type StyleChange as n, type ToolType as o, type VideoObjectContext as p, type ImageObjectBox as q, type VideoSubjectStore as r, type VideoObjectBox as s, type BrowserImageSubjectStoreOptions as t, type ImageContentHasher as u, type ImageObjectObservation as v, ImageSubjectAdapter as w, type ImageSubjectStore as x, type PersistedImageAnalysis as y, type PersistedImageSubject as z };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@popmelt.com/core",
3
- "version": "0.16.0",
3
+ "version": "0.18.0",
4
4
  "description": "A tiny, powerful design layer for humans and AI",
5
5
  "license": "PolyForm-Shield-1.0.0",
6
6
  "author": "Popmelt <reb@popmelt.com> (https://popmelt.com)",
@@ -1,2 +0,0 @@
1
- "use client";
2
- var b=(r,e=0,t=1)=>Math.min(t,Math.max(e,r));function z(r){let{width:e,height:t}=r.rect,s=Math.max(1,r.naturalWidth),n=Math.max(1,r.naturalHeight),i=Math.min(e/s,t/n),o=Math.max(e/s,t/n);switch(r.objectFit){case"contain":return{width:s*i,height:n*i};case"cover":return{width:s*o,height:n*o};case"none":return{width:s,height:n};case"scale-down":{let a=Math.min(1,i);return{width:s*a,height:n*a}}default:return{width:e,height:t}}}function A(r,e){let t=r.trim().toLowerCase();if(t.endsWith("%")){let s=Number.parseFloat(t);return Number.isFinite(s)?e*(s/100):0}if(t.endsWith("px")){let s=Number.parseFloat(t);return Number.isFinite(s)?s:0}return t==="center"?e/2:t==="right"||t==="bottom"?e:0}function $(r,e,t){let s=r.objectPosition.trim().split(/\s+/),n=s[0]||"50%",i=s[1]||"50%";return{x:A(n,r.rect.width-e),y:A(i,r.rect.height-t)}}function x(r){let e=r.getBoundingClientRect();if(e.width<=0||e.height<=0||r.naturalWidth<=0||r.naturalHeight<=0)return null;let t=r.ownerDocument.defaultView?.getComputedStyle(r)??getComputedStyle(r),s={rect:e,naturalWidth:r.naturalWidth,naturalHeight:r.naturalHeight,objectFit:t.objectFit||"fill",objectPosition:t.objectPosition||"50% 50%"},n=z(s);return{layout:s,rendered:n,position:$(s,n.width,n.height)}}function j(r,e){let t=x(r);if(!t)return null;let{layout:s,rendered:n,position:i}=t;if(e.x<s.rect.left||e.x>s.rect.left+s.rect.width||e.y<s.rect.top||e.y>s.rect.top+s.rect.height)return null;let o=(e.x-s.rect.left-i.x)/n.width,a=(e.y-s.rect.top-i.y)/n.height;return o>=0&&o<=1&&a>=0&&a<=1?{x:o,y:a}:null}function T(r,e){let t=x(r);if(!t)return null;let{layout:s,rendered:n,position:i}=t,o=(e.left-s.rect.left-i.x)/n.width,a=(e.top-s.rect.top-i.y)/n.height,l=(e.left+e.width-s.rect.left-i.x)/n.width,c=(e.top+e.height-s.rect.top-i.y)/n.height,d=b(Math.min(o,l)),u=b(Math.min(a,c)),m=b(Math.max(o,l)),h=b(Math.max(a,c));return m<=d||h<=u?null:{left:d,top:u,width:m-d,height:h-u}}function M(r,e){if(![e.left,e.top,e.width,e.height].every(Number.isFinite))return null;let t=x(r);if(!t)return null;let{layout:s,rendered:n,position:i}=t,{rect:o}=s,a=o.left+i.x+b(e.left)*n.width,l=o.top+i.y+b(e.top)*n.height,c=o.left+i.x+b(e.left+Math.max(0,e.width))*n.width,d=o.top+i.y+b(e.top+Math.max(0,e.height))*n.height,u=Math.max(o.left,a),m=Math.max(o.top,l),h=Math.min(o.left+o.width,c),p=Math.min(o.top+o.height,d);return h<=u||p<=m?null:{left:u,top:m,width:h-u,height:p-m}}var D="popmelt:see:v1";function P(r){if(!r||typeof r!="object")return!1;let e=r;return["left","top","width","height"].every(t=>typeof e[t]=="number"&&Number.isFinite(e[t]))}function W(r){if(!r||typeof r!="object")return!1;let e=r;return typeof e.id=="string"&&typeof e.label=="string"&&typeof e.score=="number"&&(e.origin==="detected"||e.origin==="manual")&&P(e.box)&&(e.detectedBox===void 0||P(e.detectedBox))}function F(r){if(!r||typeof r!="object")return!1;let e=r;return typeof e.detectedLabel=="string"&&P(e.detectedBox)}function B(r){if(!r||typeof r!="object")return!1;let e=r;return e.schemaVersion===1&&typeof e.source=="string"&&typeof e.imageHash=="string"&&typeof e.inferenceKey=="string"&&typeof e.dedupeKey=="string"&&typeof e.updatedAt=="number"&&Array.isArray(e.subjects)&&e.subjects.every(W)&&(e.suppressions===void 0||Array.isArray(e.suppressions)&&e.suppressions.every(F))}function Y(r={}){let e=r.namespace??D,t=()=>r.storage??window.localStorage,s=a=>`${e}:${a}`,n=a=>`${s(a)}:history`,i=a=>{try{let l=t().getItem(s(a));if(!l)return null;let c=JSON.parse(l);return B(c)?c:null}catch{return null}},o=a=>{try{let l=t().getItem(n(a));if(!l)return[];let c=JSON.parse(l);return Array.isArray(c)?c.filter(B).slice(0,10):[]}catch{return[]}};return{load:i,save(a,l){let c=i(a);if(c&&c.imageHash!==l.imageHash){let d=o(a).filter(u=>u.imageHash!==c.imageHash||u.updatedAt!==c.updatedAt);t().setItem(n(a),JSON.stringify([c,...d].slice(0,10)))}t().setItem(s(a),JSON.stringify(l))},remove(a){t().removeItem(s(a)),t().removeItem(n(a))},loadHistory:o}}function G(){let r=new Map,e=new Map;return{load:t=>r.get(t)??null,save:(t,s)=>{let n=r.get(t);if(n&&n.imageHash!==s.imageHash){let i=e.get(t)??[];e.set(t,[n,...i.filter(o=>o.imageHash!==n.imageHash||o.updatedAt!==n.updatedAt)].slice(0,10))}r.set(t,s)},remove:t=>{r.delete(t),e.delete(t)},loadHistory:t=>e.get(t)??[]}}function K(r){return{label:r.label,score:r.score,box:{...r.box}}}var H="img";function L(r){let e=2166136261;for(let t=0;t<r.length;t+=1)e^=r.charCodeAt(t),e=Math.imul(e,16777619);return(e>>>0).toString(36)}function R(r){return new URL(g(r),r.ownerDocument.baseURI).href}async function N(r){let e=R(r);try{let t=await fetch(e,{cache:"force-cache",credentials:"same-origin"});if(!t.ok)throw new Error(`Unable to hash image (${t.status})`);let s=await t.arrayBuffer();if(!globalThis.crypto?.subtle)throw new Error("SubtleCrypto unavailable");let n=await globalThis.crypto.subtle.digest("SHA-256",s);return Array.from(new Uint8Array(n),i=>i.toString(16).padStart(2,"0")).join("")}catch{return`source:${L(e)}:${r.naturalWidth}x${r.naturalHeight}`}}function g(r){return r.currentSrc||r.src||r.getAttribute("src")||""}function U(r){return r.complete&&r.naturalWidth>0&&r.naturalHeight>0?Promise.resolve():new Promise((e,t)=>{let s=()=>{i(),e()},n=()=>{i(),t(new Error(`Unable to load image: ${g(r)}`))},i=()=>{r.removeEventListener("load",s),r.removeEventListener("error",n)};r.addEventListener("load",s,{once:!0}),r.addEventListener("error",n,{once:!0})})}function _(r,e){return{id:e.id,image:r,detection:K(e),detectedLabel:e.detectedLabel,detectedBox:e.detectedBox?{...e.detectedBox}:void 0,boundsOverridden:e.boundsOverridden,manual:e.origin==="manual"}}function q(r){return{id:r.id,label:r.detection.label,detectedLabel:r.detectedLabel,score:r.detection.score,box:{...r.detection.box},detectedBox:r.detectedBox?{...r.detectedBox}:void 0,origin:r.manual?"manual":"detected",boundsOverridden:r.boundsOverridden}}function V(r){return r.origin==="manual"||r.boundsOverridden===!0||!!(r.detectedLabel&&r.label!==r.detectedLabel)}function J(r,e){return e.x>=r.left&&e.x<=r.left+r.width&&e.y>=r.top&&e.y<=r.top+r.height}var O=class{constructor(e={}){this.options=e;this.surfaceKind="image";this.records=new Map;this.imageKeys=new Map;this.analyzedSources=new WeakMap;this.pending=new WeakMap;this.analysisMetadata=new WeakMap;this.suppressions=new WeakMap;this.writeChains=new WeakMap;this.pendingWrites=new Set;this.refreshQueued=!1;this.destroyed=!1;this.manualSequence=0;this.id=e.id??"see:image",this.readyPromise=this.refresh();let t=e.root??document;this.observer=e.observe!==!1&&typeof MutationObserver<"u"?new MutationObserver(()=>this.queueRefresh()):null,this.observer?.observe(t,{childList:!0,subtree:!0,attributes:!0,attributeFilter:["src","srcset","sizes"]})}whenReady(){return this.readyPromise}listSubjects(){let e=[];for(let t of this.records.keys()){let s=this.resolve(t);s&&e.push(s)}return e}async refresh(){if(this.destroyed)return;let e=this.options.root??document,t=this.options.selector??H,s=new Set;e instanceof HTMLImageElement&&e.matches(t)&&s.add(e);for(let n of e.querySelectorAll(t))n instanceof HTMLImageElement&&s.add(n);for(let n of this.imageKeys.keys())(!s.has(n)||!n.isConnected)&&this.removeImage(n);if(this.options.recognition)for(let n of s)await this.analyze(n);else await Promise.all(Array.from(s,n=>this.analyze(n)))}hitTest(e){let t=null;for(let s of this.records.keys()){let n=this.resolve(s),i=this.records.get(s);if(!n||!i||!J(n.bounds,e))continue;let o=n.bounds.width*n.bounds.height;(!t||o<t.area||o===t.area&&i.detection.score>t.score)&&(t={subject:n,area:o,score:i.detection.score})}return t?.subject??null}resolve(e){let t=this.records.get(e);if(!t||!t.image.isConnected)return null;let s=M(t.image,t.detection.box);if(!s)return null;let n=t.image.getBoundingClientRect(),i=Math.round(t.detection.score*1e3)/1e3;return{key:e,label:t.detection.label,sourceElement:t.image,imageObject:this.imageObjectContext(t),bounds:s,surfaceBounds:{left:n.left,top:n.top,width:n.width,height:n.height},context:`${t.detection.label} ${t.manual?"labelled":"detected"} in ${t.image.alt||"image"}`,data:{kind:"image-object",label:t.detection.label,origin:t.manual?"manual":"detected",...t.detectedLabel?{detectedLabel:t.detectedLabel}:{},score:String(i),imageSrc:g(t.image)}}}imageObjectContext(e){let{image:t,detection:{box:s}}=e;return{src:g(t),alt:t.alt,width:t.naturalWidth,height:t.naturalHeight,normalizedBox:{x:s.left,y:s.top,width:s.width,height:s.height},box:{x:s.left*t.naturalWidth,y:s.top*t.naturalHeight,width:s.width*t.naturalWidth,height:s.height*t.naturalHeight}}}async captureSubject(e){let t=this.records.get(e);if(!t?.image.isConnected||!t.image.complete)return null;let{box:s}=this.imageObjectContext(t);if(s.width<=0||s.height<=0)return null;let n=Math.min(1,1024/Math.max(s.width,s.height)),i=document.createElement("canvas");i.width=Math.max(1,Math.round(s.width*n)),i.height=Math.max(1,Math.round(s.height*n));let o=i.getContext("2d");return o?(o.drawImage(t.image,s.x,s.y,s.width,s.height,0,0,i.width,i.height),new Promise((a,l)=>{try{i.toBlob(a,"image/webp",.95)}catch(c){l(c)}})):null}beginCreate(e){let t=this.matchingImages();for(let s=t.length-1;s>=0;s-=1){let n=t[s];if(!n)continue;let i=j(n,e);if(!i)continue;let o=g(n),a=this.imageIdentity(n,o),l=`manual:${this.newObjectId()}`,c=`${a}:${l}`,d={label:"object",score:1,box:{left:i.x,top:i.y,width:0,height:0}},u=!1,m=!1,h=()=>{if(m)return;m=!0,this.records.set(c,{id:l,image:n,detection:d,manual:!0});let p=this.imageKeys.get(n)??new Set;p.add(c),this.imageKeys.set(n,p)};return{key:c,update:p=>{if(u)return null;h();let y=this.clampedImagePoint(n,p);return y?(d.box={left:Math.min(i.x,y.x),top:Math.min(i.y,y.y),width:Math.abs(y.x-i.x),height:Math.abs(y.y-i.y)},this.resolve(c)):this.resolve(c)},commit:()=>u?null:(h(),this.persistImage(n),this.resolve(c)),cancel:()=>{u||(u=!0,m&&this.removeSubject(c))}}}return null}renameSubject(e,t){let s=this.records.get(e),n=t.trim();return!s||!n?null:(!s.manual&&!s.detectedLabel&&(s.detectedLabel=s.detection.label),s.detection.label=n,this.persistImage(s.image),this.resolve(e))}updateSubjectBounds(e,t){let s=this.records.get(e);if(!s)return null;let n=T(s.image,t);return n?(s.detection.box=n,s.boundsOverridden=!0,this.persistImage(s.image),this.resolve(e)):null}removeSubject(e){let t=this.records.get(e);if(!t)return;if(!t.manual){let n=this.suppressions.get(t.image)??[];n.push({detectedLabel:t.detectedLabel??t.detection.label,detectedBox:{...t.detectedBox??t.detection.box}}),this.suppressions.set(t.image,n)}this.records.delete(e);let s=this.imageKeys.get(t.image);s?.delete(e),s?.size===0&&this.imageKeys.delete(t.image),this.persistImage(t.image)}async destroy(){this.destroyed||(this.destroyed=!0,this.observer?.disconnect(),this.records.clear(),this.imageKeys.clear(),await Promise.allSettled(this.pendingWrites),await this.options.recognition?.dispose?.())}queueRefresh(){this.refreshQueued||this.destroyed||(this.refreshQueued=!0,queueMicrotask(()=>{this.refreshQueued=!1,this.readyPromise=this.refresh()}))}async analyze(e){let t=g(e);if(!t||this.analyzedSources.get(e)===t)return;let s=this.pending.get(e);if(s)return s.source===t?s.promise:(await s.promise,this.analyze(e));this.analyzedSources.has(e)&&this.removeImage(e);let n={source:t,promise:Promise.resolve()},i=(async()=>{try{if(await U(e),this.destroyed)return;let o=R(e),a=this.imageIdentity(e,t),l=`${this.id}:${a}`,c=this.options.recognition,d=this.options.store?await this.options.store.load(l):null;if(this.destroyed||g(e)!==t)return;let u=c?.cacheKey??d?.inferenceKey??"",m=c?.configurationKey??d?.dedupeKey??"",h=d?.source===o,p=!!(e.dataset.popmeltMediaId||e.dataset.popmeltSeeId),y=h||p;if(d&&y){let S=new Map(d.subjects.map(f=>[f.id,f]));for(let f of this.imageSubjects(e))S.set(f.id,f);this.replaceImageRecords(e,a,[...S.values()]),this.suppressions.set(e,[...d.suppressions??[]]),this.analysisMetadata.set(e,{storeKey:l,source:o,imageHash:d.imageHash,inferenceKey:d.inferenceKey,dedupeKey:d.dedupeKey})}if(this.options.hydrateOnly){this.analyzedSources.set(e,t);return}let I=this.options.store?await(this.options.imageHasher??N)(e):"ephemeral";if(this.destroyed||g(e)!==t)return;if(!!(d&&y&&d.imageHash===I&&(!c||d.inferenceKey===u&&d.dedupeKey===m))){this.analysisMetadata.set(e,{storeKey:l,source:o,imageHash:I,inferenceKey:u,dedupeKey:m}),this.analyzedSources.set(e,t),await this.saveImage(e);return}let v=y&&d?.imageHash===I;if(d&&!v){let S=new Set(d.subjects.map(w=>w.id)),f=d.subjects.filter(V);this.replaceImageRecords(e,a,[...f,...this.imageSubjects(e).filter(w=>!S.has(w.id))]),this.suppressions.set(e,[])}if(this.analysisMetadata.set(e,{storeKey:l,source:o,imageHash:I,inferenceKey:v?d.inferenceKey:"",dedupeKey:v?d.dedupeKey:""}),!c){this.analyzedSources.set(e,t),await this.saveImage(e);return}let C=await c.detect(e);if(this.destroyed||g(e)!==t)return;let E=this.suppressions.get(e)??[],k=c.reconcile(C,this.imageSubjects(e),E);this.replaceImageRecords(e,a,k),this.suppressions.set(e,[...E]),this.analysisMetadata.set(e,{storeKey:l,source:o,imageHash:I,inferenceKey:u,dedupeKey:m}),this.analyzedSources.set(e,t),await this.saveImage(e)}catch(o){this.options.onError?.(o,e)}finally{this.pending.get(e)===n&&this.pending.delete(e)}})();return n.promise=i,this.pending.set(e,n),i}replaceImageRecords(e,t,s){this.removeImage(e);let n=new Set;for(let i of s){let o=`${t}:${i.id}`;n.add(o),this.records.set(o,_(e,i))}this.imageKeys.set(e,n)}imageIdentity(e,t){if(e.dataset.popmeltMediaId)return e.dataset.popmeltMediaId;if(e.dataset.popmeltSeeId)return e.dataset.popmeltSeeId;let s=new URL(t,e.ownerDocument.baseURI).href,n=this.options.root??e.ownerDocument,i=this.options.selector??H,o=Array.from(n.querySelectorAll(i)).filter(l=>g(l)===t),a=Math.max(0,o.indexOf(e));return`${L(s)}:${a}`}matchingImages(){let e=this.options.root??document,t=this.options.selector??H,s=[];e instanceof HTMLImageElement&&e.matches(t)&&s.push(e);for(let n of e.querySelectorAll(t))n instanceof HTMLImageElement&&s.push(n);return s}clampedImagePoint(e,t){let s=M(e,{left:0,top:0,width:1,height:1});return s?j(e,{x:Math.max(s.left,Math.min(s.left+s.width,t.x)),y:Math.max(s.top,Math.min(s.top+s.height,t.y))}):null}newObjectId(){let e=globalThis.crypto?.randomUUID?.();return e||L(`${Date.now()}:${this.manualSequence++}:${Math.random().toString(36)}`)}persistImage(e){let t=this.persistencePayload(e);if(!t)return Promise.resolve();let n=(this.writeChains.get(e)??Promise.resolve()).catch(()=>{}).then(()=>t.store.save(t.key,t.analysis)).catch(i=>{this.options.onError?.(i,e)});return this.writeChains.set(e,n),this.pendingWrites.add(n),n.finally(()=>this.pendingWrites.delete(n)),n}async saveImage(e){await this.persistImage(e)}imageSubjects(e){return Array.from(this.imageKeys.get(e)??[]).map(t=>this.records.get(t)).filter(t=>!!t).map(q)}persistencePayload(e){let t=this.options.store,s=this.analysisMetadata.get(e);if(!t||!s)return null;let n=this.imageSubjects(e),i={schemaVersion:1,source:s.source,imageHash:s.imageHash,inferenceKey:s.inferenceKey,dedupeKey:s.dedupeKey,subjects:n,suppressions:this.suppressions.get(e)??[],updatedAt:Date.now()};return{store:t,key:s.storeKey,analysis:i}}removeImage(e){for(let t of this.imageKeys.get(e)??[])this.records.delete(t);this.imageKeys.delete(e),this.analyzedSources.delete(e),this.analysisMetadata.delete(e),this.suppressions.delete(e)}};function se(r={}){return new O(r)}export{j as a,T as b,M as c,Y as d,G as e,O as f,se as g};