@idmission/websdk-core 0.2.0-rc.2 → 0.5.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.
Files changed (209) hide show
  1. package/README.md +27 -1
  2. package/dist/attestation/clientAttestation.d.ts +5 -0
  3. package/dist/attestation/clientAttestation.d.ts.map +1 -1
  4. package/dist/attestation/clientAttestation.js +4 -4
  5. package/dist/attestation/collectClientAttestation.d.ts +3 -1
  6. package/dist/attestation/collectClientAttestation.d.ts.map +1 -1
  7. package/dist/attestation/collectClientAttestation.js +4 -3
  8. package/dist/camera/acquireCamera.d.ts +5 -0
  9. package/dist/camera/acquireCamera.d.ts.map +1 -1
  10. package/dist/camera/acquireCamera.js +25 -5
  11. package/dist/captureProgress/actor.d.ts +30 -0
  12. package/dist/captureProgress/actor.d.ts.map +1 -0
  13. package/dist/captureProgress/actor.js +754 -0
  14. package/dist/captureProgress/encoder.d.ts +7 -0
  15. package/dist/captureProgress/encoder.d.ts.map +1 -0
  16. package/dist/captureProgress/encoder.js +195 -0
  17. package/dist/captureProgress/index.d.ts +4 -0
  18. package/dist/captureProgress/index.d.ts.map +1 -0
  19. package/dist/captureProgress/index.js +45 -0
  20. package/dist/captureProgress/internal.d.ts +3 -0
  21. package/dist/captureProgress/internal.d.ts.map +1 -0
  22. package/dist/captureProgress/internal.js +1 -0
  23. package/dist/captureProgress/protocol.d.ts +224 -0
  24. package/dist/captureProgress/protocol.d.ts.map +1 -0
  25. package/dist/captureProgress/protocol.js +106 -0
  26. package/dist/captureProgress/recorder.d.ts +32 -0
  27. package/dist/captureProgress/recorder.d.ts.map +1 -0
  28. package/dist/captureProgress/recorder.js +554 -0
  29. package/dist/captureProgress/transport.d.ts +33 -0
  30. package/dist/captureProgress/transport.d.ts.map +1 -0
  31. package/dist/captureProgress/transport.js +109 -0
  32. package/dist/captureProgress/types.d.ts +35 -0
  33. package/dist/captureProgress/types.d.ts.map +1 -0
  34. package/dist/captureProgress/types.js +27 -0
  35. package/dist/client/IdmissionClient.d.ts +8 -1
  36. package/dist/client/IdmissionClient.d.ts.map +1 -1
  37. package/dist/client/IdmissionClient.js +96 -27
  38. package/dist/client/approval.d.ts +15 -0
  39. package/dist/client/approval.d.ts.map +1 -0
  40. package/dist/client/approval.js +19 -0
  41. package/dist/client/artifacts.d.ts +2 -0
  42. package/dist/client/artifacts.d.ts.map +1 -1
  43. package/dist/client/documentFileMetadata.d.ts +13 -0
  44. package/dist/client/documentFileMetadata.d.ts.map +1 -0
  45. package/dist/client/documentFileMetadata.js +51 -0
  46. package/dist/client/index.d.ts +3 -0
  47. package/dist/client/index.d.ts.map +1 -1
  48. package/dist/client/index.js +2 -0
  49. package/dist/client/payload.d.ts +3 -0
  50. package/dist/client/payload.d.ts.map +1 -1
  51. package/dist/client/payload.js +3 -1
  52. package/dist/client/submissionMetadata.d.ts +4 -1
  53. package/dist/client/submissionMetadata.d.ts.map +1 -1
  54. package/dist/client/submissionMetadata.js +13 -8
  55. package/dist/client/types.d.ts +8 -0
  56. package/dist/client/types.d.ts.map +1 -1
  57. package/dist/client/validateSession.d.ts +2 -0
  58. package/dist/client/validateSession.d.ts.map +1 -1
  59. package/dist/client/validateSession.js +53 -24
  60. package/dist/documentCapture/autoCaptureEngine.d.ts +106 -0
  61. package/dist/documentCapture/autoCaptureEngine.d.ts.map +1 -0
  62. package/dist/documentCapture/autoCaptureEngine.js +475 -0
  63. package/dist/documentCapture/autoCaptureGeometry.d.ts +514 -0
  64. package/dist/documentCapture/autoCaptureGeometry.d.ts.map +1 -0
  65. package/dist/documentCapture/autoCaptureGeometry.js +475 -0
  66. package/dist/documentCapture/autoCaptureGuidance.d.ts +61 -0
  67. package/dist/documentCapture/autoCaptureGuidance.d.ts.map +1 -0
  68. package/dist/documentCapture/autoCaptureGuidance.js +73 -0
  69. package/dist/documentCapture/autoCaptureMetadata.d.ts +193 -0
  70. package/dist/documentCapture/autoCaptureMetadata.d.ts.map +1 -0
  71. package/dist/documentCapture/autoCaptureMetadata.js +200 -0
  72. package/dist/documentCapture/autoCaptureOptions.d.ts +211 -0
  73. package/dist/documentCapture/autoCaptureOptions.d.ts.map +1 -0
  74. package/dist/documentCapture/autoCaptureOptions.js +177 -0
  75. package/dist/documentCapture/autoCaptureShutter.d.ts +162 -0
  76. package/dist/documentCapture/autoCaptureShutter.d.ts.map +1 -0
  77. package/dist/documentCapture/autoCaptureShutter.js +183 -0
  78. package/dist/documentCapture/controller.d.ts +38 -1
  79. package/dist/documentCapture/controller.d.ts.map +1 -1
  80. package/dist/documentCapture/controller.js +620 -40
  81. package/dist/documentCapture/index.d.ts +8 -2
  82. package/dist/documentCapture/index.d.ts.map +1 -1
  83. package/dist/documentCapture/index.js +6 -0
  84. package/dist/documentCapture/reducer.d.ts +26 -1
  85. package/dist/documentCapture/reducer.d.ts.map +1 -1
  86. package/dist/documentCapture/reducer.js +43 -0
  87. package/dist/documentCapture/state.d.ts +118 -0
  88. package/dist/documentCapture/state.d.ts.map +1 -1
  89. package/dist/documentCapture/state.js +24 -0
  90. package/dist/idCapture/controller.d.ts +24 -1
  91. package/dist/idCapture/controller.d.ts.map +1 -1
  92. package/dist/idCapture/controller.js +111 -13
  93. package/dist/idCapture/index.d.ts +1 -1
  94. package/dist/idCapture/index.d.ts.map +1 -1
  95. package/dist/idCapture/index.js +12 -1
  96. package/dist/idCapture/pipeline.d.ts +30 -0
  97. package/dist/idCapture/pipeline.d.ts.map +1 -1
  98. package/dist/idCapture/pipeline.js +33 -10
  99. package/dist/idCapture/state.d.ts +1 -3
  100. package/dist/idCapture/state.d.ts.map +1 -1
  101. package/dist/idCapture/submit.d.ts.map +1 -1
  102. package/dist/idCapture/submit.js +35 -1
  103. package/dist/idCapture/types.d.ts +2 -0
  104. package/dist/idCapture/types.d.ts.map +1 -1
  105. package/dist/index.d.ts +2 -0
  106. package/dist/index.d.ts.map +1 -1
  107. package/dist/index.js +1 -0
  108. package/dist/liveness/challengeGeometry.d.ts +48 -17
  109. package/dist/liveness/challengeGeometry.d.ts.map +1 -1
  110. package/dist/liveness/challengeGeometry.js +101 -54
  111. package/dist/models/DocumentDetection.d.ts +29 -1
  112. package/dist/models/DocumentDetection.d.ts.map +1 -1
  113. package/dist/models/DocumentDetection.js +75 -7
  114. package/dist/models/defaults/DocumentDetectorLiteRT.d.ts +2 -2
  115. package/dist/models/defaults/DocumentDetectorLiteRT.d.ts.map +1 -1
  116. package/dist/models/defaults/DocumentDetectorLiteRT.js +27 -2
  117. package/dist/models/defaults/HandLandmarkLiteRT.d.ts +3 -0
  118. package/dist/models/defaults/HandLandmarkLiteRT.d.ts.map +1 -0
  119. package/dist/models/defaults/HandLandmarkLiteRT.js +5 -0
  120. package/dist/models/defaults/IrisLandmarkLiteRT.d.ts +3 -0
  121. package/dist/models/defaults/IrisLandmarkLiteRT.d.ts.map +1 -0
  122. package/dist/models/defaults/IrisLandmarkLiteRT.js +5 -0
  123. package/dist/models/defaults/PalmDetectionLiteRT.d.ts +3 -0
  124. package/dist/models/defaults/PalmDetectionLiteRT.d.ts.map +1 -0
  125. package/dist/models/defaults/PalmDetectionLiteRT.js +5 -0
  126. package/dist/models/defaults/PoaDocumentDetectorLiteRT.d.ts +3 -0
  127. package/dist/models/defaults/PoaDocumentDetectorLiteRT.d.ts.map +1 -0
  128. package/dist/models/defaults/PoaDocumentDetectorLiteRT.js +19 -0
  129. package/dist/models/defaults/index.d.ts +13 -1
  130. package/dist/models/defaults/index.d.ts.map +1 -1
  131. package/dist/models/defaults/index.js +16 -0
  132. package/dist/models/preloadModels.d.ts +24 -16
  133. package/dist/models/preloadModels.d.ts.map +1 -1
  134. package/dist/models/preloadModels.js +33 -3
  135. package/dist/models/providers/litert/FaceLandmarker.d.ts +8 -1
  136. package/dist/models/providers/litert/FaceLandmarker.d.ts.map +1 -1
  137. package/dist/models/providers/litert/FaceLandmarker.js +95 -3
  138. package/dist/models/providers/litert/HandOverFaceDetector.d.ts +27 -0
  139. package/dist/models/providers/litert/HandOverFaceDetector.d.ts.map +1 -0
  140. package/dist/models/providers/litert/HandOverFaceDetector.js +162 -0
  141. package/dist/models/providers/litert/blazeFacePrePost.js +4 -2
  142. package/dist/models/providers/litert/docDetectPrePost.d.ts.map +1 -1
  143. package/dist/models/providers/litert/docDetectPrePost.js +20 -3
  144. package/dist/models/providers/litert/handLandmarkPrePost.d.ts +20 -0
  145. package/dist/models/providers/litert/handLandmarkPrePost.d.ts.map +1 -0
  146. package/dist/models/providers/litert/handLandmarkPrePost.js +72 -0
  147. package/dist/models/providers/litert/handOverFace.d.ts +44 -0
  148. package/dist/models/providers/litert/handOverFace.d.ts.map +1 -0
  149. package/dist/models/providers/litert/handOverFace.js +139 -0
  150. package/dist/models/providers/litert/irisPrePost.d.ts +38 -0
  151. package/dist/models/providers/litert/irisPrePost.d.ts.map +1 -0
  152. package/dist/models/providers/litert/irisPrePost.js +101 -0
  153. package/dist/models/providers/litert/palmPrePost.d.ts +55 -0
  154. package/dist/models/providers/litert/palmPrePost.d.ts.map +1 -0
  155. package/dist/models/providers/litert/palmPrePost.js +151 -0
  156. package/dist/sdkVersions.d.ts +18 -0
  157. package/dist/sdkVersions.d.ts.map +1 -0
  158. package/dist/sdkVersions.js +8 -0
  159. package/dist/selfieCapture/captureQuality.d.ts +43 -0
  160. package/dist/selfieCapture/captureQuality.d.ts.map +1 -0
  161. package/dist/selfieCapture/captureQuality.js +87 -0
  162. package/dist/selfieCapture/challengeRunner.d.ts +9 -2
  163. package/dist/selfieCapture/challengeRunner.d.ts.map +1 -1
  164. package/dist/selfieCapture/challengeRunner.js +2 -5
  165. package/dist/selfieCapture/controller.d.ts +62 -1
  166. package/dist/selfieCapture/controller.d.ts.map +1 -1
  167. package/dist/selfieCapture/controller.js +470 -105
  168. package/dist/selfieCapture/faceTracking.d.ts +69 -1
  169. package/dist/selfieCapture/faceTracking.d.ts.map +1 -1
  170. package/dist/selfieCapture/faceTracking.js +134 -6
  171. package/dist/selfieCapture/guidance.d.ts +64 -0
  172. package/dist/selfieCapture/guidance.d.ts.map +1 -1
  173. package/dist/selfieCapture/guidance.js +74 -0
  174. package/dist/selfieCapture/index.d.ts +4 -3
  175. package/dist/selfieCapture/index.d.ts.map +1 -1
  176. package/dist/selfieCapture/index.js +2 -2
  177. package/dist/selfieCapture/livenessMachine.d.ts +2 -0
  178. package/dist/selfieCapture/livenessMachine.d.ts.map +1 -1
  179. package/dist/selfieCapture/livenessMachine.js +8 -0
  180. package/dist/selfieCapture/pipeline.d.ts +8 -0
  181. package/dist/selfieCapture/pipeline.d.ts.map +1 -1
  182. package/dist/selfieCapture/pipeline.js +2 -2
  183. package/dist/selfieCapture/reducer.d.ts +7 -0
  184. package/dist/selfieCapture/reducer.d.ts.map +1 -1
  185. package/dist/selfieCapture/reducer.js +45 -4
  186. package/dist/selfieCapture/state.d.ts +6 -0
  187. package/dist/selfieCapture/state.d.ts.map +1 -1
  188. package/dist/selfieCapture/state.js +1 -0
  189. package/dist/signature/frameDecode.d.ts.map +1 -1
  190. package/dist/signature/frameDecode.js +29 -0
  191. package/dist/signature/metadata.d.ts +10 -1
  192. package/dist/signature/metadata.d.ts.map +1 -1
  193. package/dist/signature/quality.d.ts +14 -6
  194. package/dist/signature/quality.d.ts.map +1 -1
  195. package/dist/signature/quality.js +13 -4
  196. package/dist/signatureCapture/controller.d.ts +2 -1
  197. package/dist/signatureCapture/controller.d.ts.map +1 -1
  198. package/dist/signatureCapture/controller.js +118 -23
  199. package/dist/signatureCapture/pipeline.d.ts +14 -2
  200. package/dist/signatureCapture/pipeline.d.ts.map +1 -1
  201. package/dist/signatureCapture/pipeline.js +36 -8
  202. package/dist/signatureCapture/state.d.ts +15 -0
  203. package/dist/signatureCapture/state.d.ts.map +1 -1
  204. package/dist/version.d.ts +1 -1
  205. package/dist/version.d.ts.map +1 -1
  206. package/dist/version.js +1 -1
  207. package/dist/videoIdCapture/controller.d.ts.map +1 -1
  208. package/dist/videoIdCapture/controller.js +64 -3
  209. package/package.json +10 -2
@@ -0,0 +1,475 @@
1
+ /**
2
+ * The two-sided window on `fill`, seeded by #343 and fixed by #380 on real
3
+ * frames.
4
+ *
5
+ * THE TWO SIDES NOW MEASURE AGAINST DIFFERENT RECTANGLES. `FILL_MIN` is still
6
+ * taken against the GUIDE RECT: it asks whether the page is big enough to read,
7
+ * relative to the box the applicant was asked to fill. `FILL_MAX` is taken
8
+ * against the whole CAMERA FRAME, because the saved image is the whole frame
9
+ * (see `defaultCaptureFrame` in controller.ts). Against the guide it forced the
10
+ * page to fit inside the A4 box — on a 16:9 webcam a column ~40% of the width —
11
+ * though everything around the box is saved too.
12
+ *
13
+ * It is not redundant with `touchesFrameEdge`, and the proof is the two live
14
+ * desktop captures below: pages clipped top and bottom whose boxes stop 0.87%
15
+ * SHORT of the frame edges, so the clamp never fires. Camera-relative they read
16
+ * 0.983. The re-seated 0.975 sits between those and the largest camera-relative
17
+ * fill any frame reaches while clearing every other check on the saved device
18
+ * recordings (0.974 of 270), so it keeps rejecting the clips and costs no
19
+ * capture there. That gap is ~0.01 wide, like #380's — an operating point, not
20
+ * a separation.
21
+ *
22
+ * The #380 notes below describe the guide-relative 0.99 it replaces.
23
+ *
24
+ * `FILL_MAX` is the anti-crop gate: a capture zoomed into the address block has
25
+ * paper edge to edge, so its fill runs to ~1.0 while a complete sheet on
26
+ * #343's ground truth reads 0.720-0.974. `FILL_MAX` also has to absorb the skew
27
+ * budget (~0.03-0.05 at 10°), which #343 chose to spend here rather than gate
28
+ * or de-skew.
29
+ *
30
+ * `FILL_MIN` is the only thing between an applicant and a sharp unreadable
31
+ * capture — #344 found focus stays flat at 0.86-0.93 on a sheet shrunk to 0.4
32
+ * linear, because the crop IS the box, so focus can never catch "too far".
33
+ *
34
+ * **Fixed by #380** on 4 real handheld recordings (1079x1920 iPhone, printed
35
+ * Carta invoice, 17.5 fps, ~1000 frames), measured on frames that also pass
36
+ * stability — the only population the gate ever acts on:
37
+ *
38
+ * FILL_MAX 0.99 complete sheets reach p95 0.987 / max 0.996, a held capture
39
+ * cropped to the address block reads min 0.994 with 64 of 77
40
+ * frames pinned at exactly 1.0000. At 0.99, 0% of the crop
41
+ * passes and 3.7% of complete frames are rejected. #343's seed
42
+ * survived contact with a phone.
43
+ * FILL_MIN 0.70 the seed of 0.55 accepted **100%** of the too-far frames.
44
+ * A sheet held at a distance where its address is unreadable
45
+ * reads 0.574-0.593; readable framings bottom out at 0.710.
46
+ * At 0.70: 0% of readable rejected, 7.9% of too-far accepted
47
+ * (all of them transition frames, none from the held segment).
48
+ *
49
+ * Both distributions OVERLAP — complete max 0.996 against crop min 0.994,
50
+ * readable min 0.710 against too-far max 0.721. These are operating points in
51
+ * a ~0.01-wide gap, not separators, and that is the honest description of what
52
+ * this quantity can do. #380's simulation is what makes them defensible: over
53
+ * all four recordings the gate fires within a second on a good frame and never
54
+ * fires while the sheet is cropped.
55
+ *
56
+ * Measured on ONE phone, ONE sheet, Carta only (no A4 was available) — and the
57
+ * skew budget #343 estimated at 0.03-0.05 is still UNMEASURED, which matters
58
+ * because FILL_MAX has only 3.7% margin and tilt eats exactly that.
59
+ */
60
+ export const POA_FILL_MAX = 0.975;
61
+ export const POA_FILL_MIN = 0.7;
62
+ /**
63
+ * Focus floor, seeded by #344 in a POA-SPECIFIC slot rather than reusing
64
+ * `defaultFocusThresholds.singlePage` — that slot is keyed by
65
+ * `detectedDocumentType` and #361 established class 22 still owns paper IDs,
66
+ * and its 0.3 mobile value was a verbatim copy of the idCard entry (`55e1d527`,
67
+ * 2024-12-12) that was never tuned: it passes 5 of 11 visibly-blurred frames.
68
+ * 11 real POA captures score 0.871-0.921, so 0.6 left 1.45x headroom.
69
+ *
70
+ * **#380 took the fallback.** #344's corpus is stills an applicant chose to
71
+ * send; live handheld frames sit lower. On the 272 frames of #380's recordings
72
+ * that would have captured but for focus, 0.6 rejects 4.8% and 0.5 rejects
73
+ * 1.8%, with p05 at 0.670 — 0.6 is biting into the live tail. The distribution
74
+ * is bimodal (motion-blurred frames land at 0.29-0.50), so 0.5 still catches
75
+ * everything genuinely blurred.
76
+ */
77
+ export const POA_FOCUS_THRESHOLDS = { desktop: 0, mobile: 0.5 };
78
+ /**
79
+ * Confidence floor before a box is trusted for geometry at all. Mirrors
80
+ * `passportBooklet.ts`'s MIN_BOOKLET_SCORE: the detector emits low-score false
81
+ * positives that would otherwise corrupt every margin below.
82
+ *
83
+ * **Raised from 0.3 to 0.55 by #380, and this is the single most consequential
84
+ * constant on this page.** At 0.3 a box scoring 0.31 is trusted exactly as much
85
+ * as one scoring 0.85 — and the low-scoring boxes are the SPILLED ones, which
86
+ * #343 established are geometrically indistinguishable from a correct box
87
+ * (both are large, both have small margins) and cannot be told apart by any
88
+ * fill threshold. #380 watched that leak: on a recording of a sheet held too
89
+ * far away to read, the fill window alone still let 6 frames through to
90
+ * capture, every one of them on a POA box scoring under 0.50.
91
+ *
92
+ * 0.55 closes that leak completely at zero cost — across #380's four
93
+ * recordings the two well-framed ones still fire at 0.0s and 0.8s, unchanged.
94
+ * The margin is thin (readable holds have a p05 of 0.563), so this is an
95
+ * operating point on one device; the DIRECTION — that 0.3 is far too low to
96
+ * gate a capture on — is the durable part.
97
+ */
98
+ export const POA_MIN_SCORE = 0.55;
99
+ /**
100
+ * Labels the framing geometry may be measured on, in priority order.
101
+ *
102
+ * `POA` (LiteRT class 23) is the specialized class, but #342 established that
103
+ * the class is only a "there is paper here" trigger — it holds ~0.81-0.83 on a
104
+ * sheet cropped to a quarter of its area — so the anti-crop guarantee rests
105
+ * entirely on the geometry below. The fallbacks let the same instrument run
106
+ * against the SHIPPING detector, which has no POA class, so the overlay is
107
+ * useful before #354 lands the official export.
108
+ *
109
+ * `Document back` is in the list because real POA sheets read as one. On a
110
+ * printed utility bill the detector routinely scores it ABOVE the POA class
111
+ * (0.73 vs 0.59 on the sheet that prompted this), which is unsurprising: a
112
+ * dense page of text and a card back look alike to a detector trained mostly
113
+ * on IDs. Without it, a frame where POA dipped under `POA_MIN_SCORE` fell all
114
+ * the way through to "no document detected" while the model was confidently
115
+ * looking straight at the page.
116
+ *
117
+ * It does NOT widen what an applicant can submit, which is the objection to
118
+ * check before extending this list: `Document` is already the last fallback,
119
+ * so a generic ID document has always qualified. Excluding only the BACK of
120
+ * one was an asymmetry, not a guard. Nothing here classifies the document —
121
+ * that is the backend's job — and auto-capture is assistance, not enforcement
122
+ * (#345): the cost of a wrong label is a well-framed photo of the wrong page,
123
+ * which is exactly what the manual shutter produces today.
124
+ *
125
+ * Order matters and this goes LAST: priority is by label, so `POA` still wins
126
+ * whenever it clears the floor, and this only decides frames that would
127
+ * otherwise have had no target at all.
128
+ */
129
+ export const POA_TARGET_LABELS = [
130
+ 'POA',
131
+ 'Single page',
132
+ 'Document',
133
+ 'Document back',
134
+ ];
135
+ /**
136
+ * Every gate the loop can fail, in the provisional precedence order the
137
+ * overlay renders them in.
138
+ *
139
+ * `tooClose` leads because "move back" is what the AMEX failure needs. It and
140
+ * `tooFar` are the two sides of #343's one window, and on `fill` alone they
141
+ * cannot both fire — but frame-edge contact can put a small sliver of sheet in
142
+ * both at once. `tooClose` winning is then the correct instruction: moving
143
+ * closer cannot bring paper that is off the frame back into view. Stability and focus sit last because they are meaningless until the
144
+ * sheet is framed — and #344 made `outOfFocus`-last a REQUIREMENT rather than
145
+ * a preference: focus is only computed on a frame that already has a box, and
146
+ * the score is non-monotonic, recovering at severe blur.
147
+ *
148
+ * Ruled by #345. `notStable` sits ABOVE `outOfFocus`, reversing the earlier
149
+ * provisional order, on #383's finding that sub-0.5 focus frames carry REAL
150
+ * motion (box IoU 0.910 vs 0.967, detection 0.664 vs 0.738): on paper a focus
151
+ * failure IS a motion failure, so "hold still" is the instruction that fixes
152
+ * it and "out of focus" is a paraphrase the applicant cannot act on
153
+ * differently. It also matches `idCapture`, which ends NOT_STABLE ->
154
+ * OUT_OF_FOCUS.
155
+ *
156
+ * `outOfFocus`-last is additionally STRUCTURAL, not a preference:
157
+ * `focusThresholdMet` initialises `false` and is only assigned inside the
158
+ * good-frame branch, so promoting focus up the ladder would report "out of
159
+ * focus" on frames where focus was never measured.
160
+ *
161
+ * `noDocument` and `tooFar` stay separate codes even though they always
162
+ * co-fire — an empty fill window returns `tooFar: true` by design, because the
163
+ * hard detection floor makes "no box" the far end of the range rather than an
164
+ * error. Distinct codes cost nothing and give the capture metadata a real
165
+ * distinction; they deliberately do NOT share a string (#346).
166
+ */
167
+ export const POA_GATE_ORDER = [
168
+ 'noDocument',
169
+ 'tooClose',
170
+ 'tooFar',
171
+ 'notStable',
172
+ 'outOfFocus',
173
+ ];
174
+ /**
175
+ * The rectangle the applicant is actually asked to fill.
176
+ *
177
+ * This is the largest CENTRED sub-rect of the camera frame matching the guide
178
+ * box's aspect ratio, and gating on it rather than on the raw frame is the
179
+ * whole point:
180
+ *
181
+ * - Under `snapToGuides` (the default) the feed is sized to the guide rect
182
+ * under `object-fit: cover`, so this IS the pixels on screen. On a 9:16
183
+ * phone at A4 that is 100% of frame width by 79.5% of height; on a 16:9
184
+ * webcam it is 39.8% of WIDTH by 100% of height.
185
+ * - Under `fullscreen` the guide box is drawn inside a full-bleed feed, so
186
+ * this is a close approximation of it rather than exactly it — still far
187
+ * better than grading the whole frame.
188
+ *
189
+ * Grading the raw frame instead made `max(fillWidth, fillHeight)` pick the
190
+ * axis the applicant does not control on any landscape camera: a sheet filling
191
+ * a portrait guide box read `fillHeight ≈ 0.98` and passed, while
192
+ * `fillWidth ≈ 0.49` — half the capture being background — was never looked at.
193
+ * It also meant the submitted image contained pixels the applicant never saw.
194
+ *
195
+ * With no `aspectRatio` there is no guide to fill, so the frame is the rect.
196
+ */
197
+ export function computeGuideRect(frameWidth, frameHeight, aspectRatio) {
198
+ const frame = { x: 0, y: 0, width: frameWidth, height: frameHeight };
199
+ if (frameWidth <= 0 ||
200
+ frameHeight <= 0 ||
201
+ !aspectRatio ||
202
+ !Number.isFinite(aspectRatio) ||
203
+ aspectRatio <= 0) {
204
+ return frame;
205
+ }
206
+ const frameAspect = frameWidth / frameHeight;
207
+ // Guide narrower than the frame -> height binds and the sides are cropped.
208
+ const width = aspectRatio < frameAspect ? frameHeight * aspectRatio : frameWidth;
209
+ const height = aspectRatio < frameAspect ? frameHeight : frameWidth / aspectRatio;
210
+ return {
211
+ x: (frameWidth - width) / 2,
212
+ y: (frameHeight - height) / 2,
213
+ width,
214
+ height,
215
+ };
216
+ }
217
+ /**
218
+ * Measure a detection box against the guide rect.
219
+ *
220
+ * Margins and fills are fractions OF THE GUIDE RECT, so a sheet wider than the
221
+ * guide reads `fill > 1` and a sheet spilling past an edge reads a NEGATIVE
222
+ * margin. Both are wanted: they are what makes "it does not fit in the box"
223
+ * trip `FILL_MAX` instead of quietly passing.
224
+ */
225
+ export function computePoaFraming(box, rect) {
226
+ // A zero-sized rect yields no meaningful ratio. Report zeroes rather than
227
+ // emitting Infinity/NaN into the overlay; `fill: 0` fails `FILL_MIN` on its
228
+ // own, so the frame is rejected without a special case.
229
+ if (rect.width <= 0 || rect.height <= 0) {
230
+ return {
231
+ leftMargin: 0,
232
+ rightMargin: 0,
233
+ topMargin: 0,
234
+ bottomMargin: 0,
235
+ minMargin: 0,
236
+ fill: 0,
237
+ fillWidth: 0,
238
+ fillHeight: 0,
239
+ };
240
+ }
241
+ const leftMargin = (box.xMin - rect.x) / rect.width;
242
+ const rightMargin = (rect.x + rect.width - box.xMax) / rect.width;
243
+ const topMargin = (box.yMin - rect.y) / rect.height;
244
+ const bottomMargin = (rect.y + rect.height - box.yMax) / rect.height;
245
+ const minMargin = Math.min(leftMargin, rightMargin, topMargin, bottomMargin);
246
+ const fillWidth = box.width / rect.width;
247
+ const fillHeight = box.height / rect.height;
248
+ const fill = Math.max(fillWidth, fillHeight);
249
+ return {
250
+ leftMargin,
251
+ rightMargin,
252
+ topMargin,
253
+ bottomMargin,
254
+ minMargin,
255
+ fill,
256
+ fillWidth,
257
+ fillHeight,
258
+ };
259
+ }
260
+ /**
261
+ * How close to a camera-frame edge a box coordinate may sit and still count as
262
+ * touching it, as a fraction of that axis.
263
+ *
264
+ * FLOAT SAFETY, NOT A TUNING KNOB. The LiteRT decoder clamps every coordinate
265
+ * to the frame, so a box over paper that continues past the edge lands on the
266
+ * edge EXACTLY; this only absorbs rounding. It is about a pixel on a 1080-wide
267
+ * frame.
268
+ *
269
+ * Deliberately not widened to a box-encoding grid step. On the five saved
270
+ * #380-era device recordings, frames passing today's gate go from 66 rejected
271
+ * at the exact clamp to 134 at one grid step — and every one of the extra 68 is
272
+ * a sheet whose edge IS inside the frame, which is friction with nothing bought.
273
+ */
274
+ export const POA_FRAME_EDGE_EPSILON = 0.001;
275
+ /**
276
+ * Whether a detection box touches an edge of the CAMERA frame — the paper
277
+ * continues past what the camera can see.
278
+ *
279
+ * `fill` cannot see this. It measures how big the box is, and a sheet slid
280
+ * sideways off the frame just produces a narrower box: the detector clamps to
281
+ * the frame, and on a portrait phone an A4 guide spans the camera's full width,
282
+ * so on that axis `fillW` can never exceed 1.0 however much paper is hidden.
283
+ * `max(fillW, fillH)` then hands the verdict to the other axis. Replayed
284
+ * through the gate, a sheet with HALF its width off the frame read `fill 0.90`
285
+ * and captured — which is what demo testers reported, and what #380's crop
286
+ * recording never exercised: it zoomed in on the address block, putting paper
287
+ * edge to edge on both axes.
288
+ *
289
+ * Not the per-edge margin gate #343 deleted. That measured distance to the
290
+ * GUIDE rect, where the complete-vs-clipped separation was a third of a grid
291
+ * step and drowned in quantization. This asks a yes/no question the decoder's
292
+ * clamp answers exactly — the box is on the frame edge or it is not.
293
+ */
294
+ export function touchesFrameEdge(box, frameWidth, frameHeight) {
295
+ if (frameWidth <= 0 || frameHeight <= 0)
296
+ return false;
297
+ return (box.xMin / frameWidth <= POA_FRAME_EDGE_EPSILON ||
298
+ box.yMin / frameHeight <= POA_FRAME_EDGE_EPSILON ||
299
+ 1 - box.xMax / frameWidth <= POA_FRAME_EDGE_EPSILON ||
300
+ 1 - box.yMax / frameHeight <= POA_FRAME_EDGE_EPSILON);
301
+ }
302
+ /**
303
+ * Aggregate a window of `fill` readings, newest first, and take the verdict.
304
+ *
305
+ * The verdict is taken on however many frames are present rather than waiting
306
+ * for a full window: the window fills in under a second, and `notStable`
307
+ * already holds the capture until the IoU window is complete, so a second
308
+ * "not enough frames" state would only be a way for the two to disagree.
309
+ *
310
+ * `edgeContacts` is `touchesFrameEdge` per frame, same order and same window.
311
+ * ANY contact in the window trips `tooClose`, not just this frame's: on the
312
+ * saved device recordings contact flickers, with 15 frames that miss the edge
313
+ * sitting inside contact episodes. A per-frame check would let a crop through
314
+ * on exactly those frames, and the burst window would bank one.
315
+ *
316
+ * `frameFills` is `fill` measured against the camera frame, same order and
317
+ * window; `fillMax` is compared with its mean. It defaults to `fills`, which is
318
+ * exact whenever the guide rect is the whole frame.
319
+ */
320
+ export function computePoaFillWindow(fills, options, edgeContacts = [], frameFills = fills) {
321
+ const { fillMin, fillMax } = options;
322
+ const framesNeeded = options.framesNeeded ?? fills.length;
323
+ if (fills.length === 0) {
324
+ return {
325
+ mean: 0,
326
+ instant: 0,
327
+ min: 0,
328
+ max: 0,
329
+ spread: 0,
330
+ framesTracked: 0,
331
+ framesNeeded,
332
+ fillMin,
333
+ fillMax,
334
+ frameMean: 0,
335
+ frameEdgeContact: false,
336
+ // No box is the FAR end of the range, not an error state: #342 found a
337
+ // hard detection floor at 0.40 linear scale, past which the model emits
338
+ // nothing at all.
339
+ tooClose: false,
340
+ tooFar: true,
341
+ };
342
+ }
343
+ let sum = 0;
344
+ let min = fills[0];
345
+ let max = fills[0];
346
+ for (const fill of fills) {
347
+ sum += fill;
348
+ if (fill < min)
349
+ min = fill;
350
+ if (fill > max)
351
+ max = fill;
352
+ }
353
+ const mean = sum / fills.length;
354
+ const frameMean = frameFills.length
355
+ ? frameFills.reduce((total, fill) => total + fill, 0) / frameFills.length
356
+ : mean;
357
+ const frameEdgeContact = edgeContacts.some(Boolean);
358
+ return {
359
+ mean,
360
+ instant: fills[0],
361
+ min,
362
+ max,
363
+ spread: max - min,
364
+ framesTracked: fills.length,
365
+ framesNeeded,
366
+ fillMin,
367
+ fillMax,
368
+ frameMean,
369
+ frameEdgeContact,
370
+ tooClose: frameMean > fillMax || frameEdgeContact,
371
+ tooFar: mean < fillMin,
372
+ };
373
+ }
374
+ /**
375
+ * Peak score per emitted label.
376
+ *
377
+ * Keyed by the label STRING the model emitted rather than by a fixed set of
378
+ * per-class fields (IdCapture's `idCardFrontDetectionScore` &c.), so a class
379
+ * the map has not learned yet still shows up — as `class_23` if #354's export
380
+ * lands before the label map does. #341: this is why the still-open class
381
+ * question (#339) cannot ripple into the overlay's field list.
382
+ */
383
+ export function peakScoresByLabel(detectedObjects) {
384
+ const scores = {};
385
+ for (const obj of detectedObjects) {
386
+ const previous = scores[obj.label];
387
+ if (previous === undefined || obj.score > previous) {
388
+ scores[obj.label] = obj.score;
389
+ }
390
+ }
391
+ return scores;
392
+ }
393
+ /**
394
+ * Pick the box the framing geometry is measured on: the highest-scoring box
395
+ * carrying the first label in `labels` that has any candidate above
396
+ * `minScore`. Priority is by LABEL, not by score, so a stray high-confidence
397
+ * `Document` on a POA sheet cannot displace the POA box.
398
+ */
399
+ export function selectPoaTarget(detectedObjects, labels = POA_TARGET_LABELS, minScore = POA_MIN_SCORE) {
400
+ for (const label of labels) {
401
+ let best = null;
402
+ for (const obj of detectedObjects) {
403
+ if (obj.label !== label)
404
+ continue;
405
+ if (obj.score < minScore)
406
+ continue;
407
+ if (obj.box.width <= 0 || obj.box.height <= 0)
408
+ continue;
409
+ if (!best || obj.score > best.score)
410
+ best = obj;
411
+ }
412
+ if (best)
413
+ return { label: best.label, score: best.score, box: best.box };
414
+ }
415
+ return null;
416
+ }
417
+ /**
418
+ * Every failing gate, in `POA_GATE_ORDER`. All of them, not just the winner:
419
+ * a one-message ladder is what #345 decides, and the operator needs to see
420
+ * what else was true when it decided.
421
+ *
422
+ * A `null` focus verdict means the focus model is not loaded (or #344 rules it
423
+ * out as a gate) — an absent signal is not a failing one.
424
+ */
425
+ /**
426
+ * The winning guidance code for this frame, or null when every gate passes.
427
+ *
428
+ * Exactly one code wins, and it is the first failure in `POA_GATE_ORDER`.
429
+ */
430
+ export function winningPoaGate(inputs) {
431
+ return failingPoaGates(inputs)[0] ?? null;
432
+ }
433
+ /**
434
+ * Whether this frame satisfies every gate — the shutter's whole condition.
435
+ *
436
+ * Derived from `failingPoaGates` rather than re-tested, so the thing the
437
+ * shutter fires on and the thing the overlay renders cannot disagree (#345).
438
+ *
439
+ * Thin only on its face: a satisfied frame already implies `stabilityFrames`
440
+ * consecutive frames of IoU >= the stability threshold PLUS a `stabilityFrames`
441
+ * mean of `fill` — roughly 0.7 s of evidence at 17.3 fps. The gates' own
442
+ * windows ARE the accumulator, which is why no second integrator is stacked on
443
+ * top of them.
444
+ */
445
+ export function isAutoCaptureSatisfied(inputs) {
446
+ return failingPoaGates(inputs).length === 0;
447
+ }
448
+ export function failingPoaGates(inputs) {
449
+ const failing = [];
450
+ if (!inputs.framing) {
451
+ failing.push('noDocument');
452
+ // Below the detection floor the sheet is genuinely too far away (#342:
453
+ // nothing at all is emitted at 0.30 linear scale), so "too far" rides
454
+ // along with "no document" rather than being unreportable. An empty window
455
+ // reports `tooFar` for the same reason, so this reads it rather than
456
+ // hard-coding a second copy of the rule.
457
+ }
458
+ if (inputs.fillWindow.tooClose)
459
+ failing.push('tooClose');
460
+ if (inputs.fillWindow.tooFar)
461
+ failing.push('tooFar');
462
+ return appendQualityGates(failing, inputs);
463
+ }
464
+ function appendQualityGates(failing, inputs) {
465
+ if (!inputs.isStable)
466
+ failing.push('notStable');
467
+ // Focus ABSENT is not a failing gate. A focus model that failed to load
468
+ // reports `null`, and the loop then captures on framing alone — defensible
469
+ // because focus rejects only 1.8% of otherwise-eligible frames and cannot
470
+ // catch the failure that matters (a sharp, unreadable, too-far sheet). The
471
+ // two arms stay separable in the field through `autoCaptureStatus`.
472
+ if (inputs.focusThresholdMet === false)
473
+ failing.push('outOfFocus');
474
+ return failing;
475
+ }
@@ -0,0 +1,61 @@
1
+ import { type StabilizerState } from '../utils/stabilizer';
2
+ import { type PoaGate, type PoaGateInputs } from './autoCaptureGeometry';
3
+ /**
4
+ * What the pill says.
5
+ *
6
+ * `'none'` is the pre-first-frame value, mirroring `idCapture` and
7
+ * `selfieCapture`, which both initialise their stabilizer at NONE.
8
+ * `'holdStill'` is the single satisfied code.
9
+ */
10
+ export type PoaGuidanceCode = 'none' | 'holdStill' | PoaGate;
11
+ export type PoaGuidanceState = StabilizerState<PoaGuidanceCode>;
12
+ /**
13
+ * Seeded one whole min-hold in the PAST, so the first real message is gated by
14
+ * the 250 ms debounce alone.
15
+ *
16
+ * `displayedAt: 0` reads like "long ago" and is not. The stabilizer is
17
+ * advanced with `timeRunning` — a clock that STARTS at zero — so `now -
18
+ * displayedAt >= minHoldMs` was `timeRunning >= 1800`, and the applicant got
19
+ * no instruction at all for the first 1.8 seconds of a capture. `idCapture`
20
+ * and `selfieCapture` initialise the same way and get away with it because
21
+ * they advance on a wall clock, where zero really is long ago.
22
+ *
23
+ * Derived from the constant rather than written as a literal: the two must not
24
+ * drift, since the whole point is that this is exactly one hold in the past.
25
+ */
26
+ export declare function createPoaGuidanceState(): PoaGuidanceState;
27
+ /**
28
+ * This frame's raw code, before stabilization.
29
+ *
30
+ * Derived from the same `failingPoaGates` call the shutter's predicate uses, so
31
+ * "what the applicant is told" and "whether it captured" cannot disagree about
32
+ * which gates passed.
33
+ */
34
+ export declare function poaGuidanceCode(inputs: PoaGateInputs): PoaGuidanceCode;
35
+ /**
36
+ * Advance the displayed message by one frame.
37
+ *
38
+ * `now` is the frame's own `timeRunning`, not a clock read — the same reason
39
+ * the shutter takes its time from the frame.
40
+ *
41
+ * Accepted consequence, worth knowing before someone reports it as a bug: with
42
+ * a shutter that fires within ~1 s the sequence is `none` -> first real code at
43
+ * ~250 ms -> capture at ~1000 ms, so `holdStill` DOES NOT REACH THE SCREEN on a
44
+ * clean capture. The applicant's feedback is the review screen, not a 200 ms
45
+ * flash of green. `holdStill` still earns its place: it is what displays when a
46
+ * struggling applicant finally gets the framing right.
47
+ */
48
+ export declare function advancePoaGuidance(state: PoaGuidanceState, inputs: PoaGateInputs, now: number): PoaGuidanceState;
49
+ /** What the overlay needs to tell "wrong threshold" from "stuck message". */
50
+ export type PoaGuidanceDiagnostics = {
51
+ /** This frame's ladder winner, before the debounce. */
52
+ raw: PoaGuidanceCode;
53
+ /** What is waiting out the 250 ms debounce. */
54
+ pendingCandidate: PoaGuidanceCode;
55
+ /** What is actually on screen. */
56
+ displayed: PoaGuidanceCode;
57
+ /** How long `displayed` has been up, ms. */
58
+ displayedForMs: number;
59
+ };
60
+ export declare function poaGuidanceDiagnostics(state: PoaGuidanceState, raw: PoaGuidanceCode, now: number): PoaGuidanceDiagnostics;
61
+ //# sourceMappingURL=autoCaptureGuidance.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"autoCaptureGuidance.d.ts","sourceRoot":"","sources":["../../src/documentCapture/autoCaptureGuidance.ts"],"names":[],"mappings":"AAmBA,OAAO,EAGL,KAAK,eAAe,EAErB,MAAM,qBAAqB,CAAA;AAC5B,OAAO,EAGL,KAAK,OAAO,EACZ,KAAK,aAAa,EACnB,MAAM,uBAAuB,CAAA;AAE9B;;;;;;GAMG;AACH,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,WAAW,GAAG,OAAO,CAAA;AAE5D,MAAM,MAAM,gBAAgB,GAAG,eAAe,CAAC,eAAe,CAAC,CAAA;AAE/D;;;;;;;;;;;;;GAaG;AACH,wBAAgB,sBAAsB,IAAI,gBAAgB,CAEzD;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,aAAa,GAAG,eAAe,CAItE;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,gBAAgB,EACvB,MAAM,EAAE,aAAa,EACrB,GAAG,EAAE,MAAM,GACV,gBAAgB,CAElB;AAED,6EAA6E;AAC7E,MAAM,MAAM,sBAAsB,GAAG;IACnC,uDAAuD;IACvD,GAAG,EAAE,eAAe,CAAA;IACpB,+CAA+C;IAC/C,gBAAgB,EAAE,eAAe,CAAA;IACjC,kCAAkC;IAClC,SAAS,EAAE,eAAe,CAAA;IAC1B,4CAA4C;IAC5C,cAAc,EAAE,MAAM,CAAA;CACvB,CAAA;AAED,wBAAgB,sBAAsB,CACpC,KAAK,EAAE,gBAAgB,EACvB,GAAG,EAAE,eAAe,EACpB,GAAG,EAAE,MAAM,GACV,sBAAsB,CAOxB"}
@@ -0,0 +1,73 @@
1
+ // The guidance ladder's rendered half (#345, #346).
2
+ //
3
+ // The ladder produces one code per frame; this routes it through the shared
4
+ // stabilizer before anything renders it.
5
+ //
6
+ // THE ROUTING IS LOAD-BEARING, NOT PLUMBING. #383's ruling that focus needs no
7
+ // hold, no hysteresis and no aggregation is correct ONLY because the focus
8
+ // excursions are 1-3 frames — measured at [55, 56, 56, 57, 57, 57, 59, 59, 59,
9
+ // 59, 61, 61, 61, 114, 114, 119, 171, 174] ms, longest 174 ms — and 0 of 18
10
+ // reach the 250 ms debounce. A ladder that renders the raw per-frame verdict
11
+ // re-opens #383 immediately, AND IT WILL PRESENT AS A THRESHOLD PROBLEM WHEN IT
12
+ // IS A PLUMBING ONE.
13
+ //
14
+ // Corollary the implementation must preserve: the stabilizer governs the
15
+ // MESSAGE, never the SHUTTER. The shutter reads live gates, so a capture can
16
+ // fire while a stale "Move back" is still on screen. That is `idCapture`'s
17
+ // shipped behaviour — guidance is display-only, deliberately downstream of the
18
+ // good-frame test — not a POA wart.
19
+ import { createStabilizerState, stabilizeStep, MESSAGE_MIN_HOLD_MS, } from '../utils/stabilizer';
20
+ import { isAutoCaptureSatisfied, winningPoaGate, } from './autoCaptureGeometry';
21
+ /**
22
+ * Seeded one whole min-hold in the PAST, so the first real message is gated by
23
+ * the 250 ms debounce alone.
24
+ *
25
+ * `displayedAt: 0` reads like "long ago" and is not. The stabilizer is
26
+ * advanced with `timeRunning` — a clock that STARTS at zero — so `now -
27
+ * displayedAt >= minHoldMs` was `timeRunning >= 1800`, and the applicant got
28
+ * no instruction at all for the first 1.8 seconds of a capture. `idCapture`
29
+ * and `selfieCapture` initialise the same way and get away with it because
30
+ * they advance on a wall clock, where zero really is long ago.
31
+ *
32
+ * Derived from the constant rather than written as a literal: the two must not
33
+ * drift, since the whole point is that this is exactly one hold in the past.
34
+ */
35
+ export function createPoaGuidanceState() {
36
+ return createStabilizerState('none', -MESSAGE_MIN_HOLD_MS);
37
+ }
38
+ /**
39
+ * This frame's raw code, before stabilization.
40
+ *
41
+ * Derived from the same `failingPoaGates` call the shutter's predicate uses, so
42
+ * "what the applicant is told" and "whether it captured" cannot disagree about
43
+ * which gates passed.
44
+ */
45
+ export function poaGuidanceCode(inputs) {
46
+ return isAutoCaptureSatisfied(inputs)
47
+ ? 'holdStill'
48
+ : (winningPoaGate(inputs) ?? 'holdStill');
49
+ }
50
+ /**
51
+ * Advance the displayed message by one frame.
52
+ *
53
+ * `now` is the frame's own `timeRunning`, not a clock read — the same reason
54
+ * the shutter takes its time from the frame.
55
+ *
56
+ * Accepted consequence, worth knowing before someone reports it as a bug: with
57
+ * a shutter that fires within ~1 s the sequence is `none` -> first real code at
58
+ * ~250 ms -> capture at ~1000 ms, so `holdStill` DOES NOT REACH THE SCREEN on a
59
+ * clean capture. The applicant's feedback is the review screen, not a 200 ms
60
+ * flash of green. `holdStill` still earns its place: it is what displays when a
61
+ * struggling applicant finally gets the framing right.
62
+ */
63
+ export function advancePoaGuidance(state, inputs, now) {
64
+ return stabilizeStep(state, poaGuidanceCode(inputs), now);
65
+ }
66
+ export function poaGuidanceDiagnostics(state, raw, now) {
67
+ return {
68
+ raw,
69
+ pendingCandidate: state.pendingCandidate,
70
+ displayed: state.displayed,
71
+ displayedForMs: Math.max(0, now - state.displayedAt),
72
+ };
73
+ }