datakeen-session-react 1.1.202-dev.17 → 1.1.202-dev.19

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 (60) hide show
  1. package/dist/cjs/components/DatakeenSession.js +17 -16
  2. package/dist/cjs/components/card-capture-guide/CardCaptureGuide.js +139 -0
  3. package/dist/cjs/components/card-capture-guide/captureTelemetry.js +81 -0
  4. package/dist/cjs/components/card-capture-guide/guidance.js +48 -0
  5. package/dist/cjs/components/card-capture-guide/guideCore.js +389 -0
  6. package/dist/cjs/components/card-capture-guide/guideRuntime.js +118 -0
  7. package/dist/cjs/components/card-capture-guide/modelLoader.js +46 -0
  8. package/dist/cjs/components/card-capture-guide/useCaptureGuideThresholds.js +33 -0
  9. package/dist/cjs/components/card-capture-guide/useCardCaptureGuide.js +117 -0
  10. package/dist/cjs/components/card-capture-guide/useMinDisplay.js +30 -0
  11. package/dist/cjs/components/id-check/Photo.js +230 -122
  12. package/dist/cjs/components/id-check/card-capture/CardCapture.js +270 -0
  13. package/dist/cjs/components/id-check/card-capture/DynamicCardMask.js +85 -0
  14. package/dist/cjs/components/id-check/card-capture/geometry.js +119 -0
  15. package/dist/cjs/components/id-check/card-capture/guideTone.js +15 -0
  16. package/dist/cjs/components/jdi/JDIDocumentSelection.js +9 -0
  17. package/dist/cjs/components/session/DocumentCheck.js +19 -2
  18. package/dist/cjs/components/start-flow/Start.js +4 -0
  19. package/dist/cjs/context/CardCaptureGuideContext.js +16 -0
  20. package/dist/cjs/i18n/en.json.js +29 -0
  21. package/dist/cjs/i18n/es.json.js +29 -0
  22. package/dist/cjs/i18n/fr.json.js +29 -0
  23. package/dist/cjs/index.css.js +1 -1
  24. package/dist/cjs/index.js +13 -0
  25. package/dist/cjs/services/sessionService.js +43 -0
  26. package/dist/esm/components/DatakeenSession.js +17 -16
  27. package/dist/esm/components/card-capture-guide/CardCaptureGuide.js +135 -0
  28. package/dist/esm/components/card-capture-guide/captureTelemetry.js +78 -0
  29. package/dist/esm/components/card-capture-guide/guidance.js +44 -0
  30. package/dist/esm/components/card-capture-guide/guideCore.js +372 -0
  31. package/dist/esm/components/card-capture-guide/guideRuntime.js +115 -0
  32. package/dist/esm/components/card-capture-guide/modelLoader.js +42 -0
  33. package/dist/esm/components/card-capture-guide/useCaptureGuideThresholds.js +31 -0
  34. package/dist/esm/components/card-capture-guide/useCardCaptureGuide.js +115 -0
  35. package/dist/esm/components/card-capture-guide/useMinDisplay.js +28 -0
  36. package/dist/esm/components/id-check/Photo.js +232 -124
  37. package/dist/esm/components/id-check/card-capture/CardCapture.js +266 -0
  38. package/dist/esm/components/id-check/card-capture/DynamicCardMask.js +81 -0
  39. package/dist/esm/components/id-check/card-capture/geometry.js +110 -0
  40. package/dist/esm/components/id-check/card-capture/guideTone.js +13 -0
  41. package/dist/esm/components/jdi/JDIDocumentSelection.js +9 -0
  42. package/dist/esm/components/session/DocumentCheck.js +19 -2
  43. package/dist/esm/components/start-flow/Start.js +4 -0
  44. package/dist/esm/context/CardCaptureGuideContext.js +13 -0
  45. package/dist/esm/i18n/en.json.js +28 -1
  46. package/dist/esm/i18n/es.json.js +28 -1
  47. package/dist/esm/i18n/fr.json.js +28 -1
  48. package/dist/esm/index.css.js +1 -1
  49. package/dist/esm/index.js +5 -0
  50. package/dist/esm/services/sessionService.js +42 -1
  51. package/dist/types/components/card-capture-guide/CardCaptureGuide.d.ts +46 -0
  52. package/dist/types/components/card-capture-guide/guidance.d.ts +45 -0
  53. package/dist/types/components/card-capture-guide/guideCore.d.ts +96 -0
  54. package/dist/types/components/card-capture-guide/guideRuntime.d.ts +13 -0
  55. package/dist/types/components/card-capture-guide/modelLoader.d.ts +13 -0
  56. package/dist/types/components/card-capture-guide/useCardCaptureGuide.d.ts +38 -0
  57. package/dist/types/index.d.ts +6 -0
  58. package/dist/types/types/camera.d.ts +3 -0
  59. package/dist/types/types/session.d.ts +15 -1
  60. package/package.json +2 -1
@@ -0,0 +1,115 @@
1
+ import { __awaiter, __generator } from '../../node_modules/tslib/tslib.es6.js';
2
+ import { SIZE, decode, frameBrightness, measureQuality, letterbox } from './guideCore.js';
3
+ import { loadModelBytes, DEFAULT_MODEL_URL } from './modelLoader.js';
4
+
5
+ var IMAGENET_MEAN = [0.485, 0.456, 0.406];
6
+ var IMAGENET_STD = [0.229, 0.224, 0.225];
7
+ var PAD = 114; // letterbox gray, same as training
8
+ var QUALITY_MAX_SIDE = 640; // card long side (px) of the crop the quality metrics are measured on
9
+ var QUALITY_MIN_PRESENCE = 0.3;
10
+ function createGuideRuntime() {
11
+ return __awaiter(this, arguments, void 0, function (options) {
12
+ function preprocess(video, w, h) {
13
+ var lb = letterbox(w, h);
14
+ preCtx.fillStyle = "rgb(".concat(PAD, ",").concat(PAD, ",").concat(PAD, ")");
15
+ preCtx.fillRect(0, 0, SIZE, SIZE);
16
+ preCtx.drawImage(video, 0, 0, w, h, lb.ox, lb.oy, lb.nw, lb.nh);
17
+ var data = preCtx.getImageData(0, 0, SIZE, SIZE).data;
18
+ var n = SIZE * SIZE;
19
+ var chw = new Float32Array(3 * n);
20
+ for (var i = 0; i < n; i++) {
21
+ for (var c = 0; c < 3; c++)
22
+ chw[c * n + i] = (data[i * 4 + c] / 255 - IMAGENET_MEAN[c]) / IMAGENET_STD[c];
23
+ }
24
+ return { tensor: new ort.Tensor("float32", chw, [1, 3, SIZE, SIZE]), brightness: frameBrightness(data, SIZE, { x: lb.ox, y: lb.oy, w: lb.nw, h: lb.nh }) };
25
+ }
26
+ /** Crop the card's bounding box out of the video at <= QUALITY_MAX_SIDE px and measure blur/glare on it. */
27
+ function sampleQuality(video, w, h, corners) {
28
+ var px = corners.map(function (_a) {
29
+ var x = _a[0], y = _a[1];
30
+ return [x * w, y * h];
31
+ });
32
+ var xs = px.map(function (p) { return p[0]; });
33
+ var ys = px.map(function (p) { return p[1]; });
34
+ var pad = 0.04 * Math.max(Math.max.apply(Math, xs) - Math.min.apply(Math, xs), Math.max.apply(Math, ys) - Math.min.apply(Math, ys));
35
+ var bx = Math.max(0, Math.floor(Math.min.apply(Math, xs) - pad));
36
+ var by = Math.max(0, Math.floor(Math.min.apply(Math, ys) - pad));
37
+ var cw = Math.min(w, Math.ceil(Math.max.apply(Math, xs) + pad)) - bx;
38
+ var ch = Math.min(h, Math.ceil(Math.max.apply(Math, ys) + pad)) - by;
39
+ if (cw < 16 || ch < 16)
40
+ return null;
41
+ var s = Math.min(1, QUALITY_MAX_SIDE / Math.max(cw, ch));
42
+ crop.width = Math.max(1, Math.round(cw * s));
43
+ crop.height = Math.max(1, Math.round(ch * s));
44
+ cropCtx.drawImage(video, bx, by, cw, ch, 0, 0, crop.width, crop.height);
45
+ var frame = cropCtx.getImageData(0, 0, crop.width, crop.height);
46
+ var quad = px.map(function (_a) {
47
+ var x = _a[0], y = _a[1];
48
+ return [(x - bx) * s, (y - by) * s];
49
+ });
50
+ return measureQuality(frame, quad, s);
51
+ }
52
+ var ort, modelBytes, session, preCanvas, preCtx, crop, cropCtx;
53
+ var _a, _b;
54
+ if (options === void 0) { options = {}; }
55
+ return __generator(this, function (_c) {
56
+ switch (_c.label) {
57
+ case 0: return [4 /*yield*/, import('onnxruntime-web')];
58
+ case 1:
59
+ ort = _c.sent();
60
+ // The .wasm must match the installed JS glue exactly: pin the CDN fallback to the runtime's own version.
61
+ ort.env.wasm.wasmPaths =
62
+ (_a = options.wasmPaths) !== null && _a !== void 0 ? _a : "https://cdn.jsdelivr.net/npm/onnxruntime-web@".concat(ort.env.versions.web, "/dist/");
63
+ return [4 /*yield*/, loadModelBytes((_b = options.modelUrl) !== null && _b !== void 0 ? _b : DEFAULT_MODEL_URL)];
64
+ case 2:
65
+ modelBytes = _c.sent();
66
+ return [4 /*yield*/, ort.InferenceSession.create(modelBytes, {
67
+ executionProviders: ["wasm"],
68
+ graphOptimizationLevel: "all",
69
+ })];
70
+ case 3:
71
+ session = _c.sent();
72
+ preCanvas = document.createElement("canvas");
73
+ preCanvas.width = SIZE;
74
+ preCanvas.height = SIZE;
75
+ preCtx = preCanvas.getContext("2d", { willReadFrequently: true });
76
+ crop = document.createElement("canvas");
77
+ cropCtx = crop.getContext("2d", { willReadFrequently: true });
78
+ if (!preCtx || !cropCtx)
79
+ throw new Error("2D canvas context unavailable");
80
+ return [2 /*return*/, {
81
+ run: function (video) {
82
+ return __awaiter(this, void 0, void 0, function () {
83
+ var w, h, pre, out, decoded, quality;
84
+ return __generator(this, function (_a) {
85
+ switch (_a.label) {
86
+ case 0:
87
+ w = video.videoWidth;
88
+ h = video.videoHeight;
89
+ pre = preprocess(video, w, h);
90
+ return [4 /*yield*/, session.run({ input: pre.tensor })];
91
+ case 1:
92
+ out = _a.sent();
93
+ decoded = decode({
94
+ cls: out.cls.data,
95
+ heat: out.heat.data,
96
+ off: out.off.data,
97
+ vis: out.vis.data,
98
+ glob: out.glob.data,
99
+ }, w, h);
100
+ quality = decoded.presence >= QUALITY_MIN_PRESENCE ? sampleQuality(video, w, h, decoded.corners) : null;
101
+ return [2 /*return*/, { decoded: decoded, quality: quality, frameBrightness: pre.brightness }];
102
+ }
103
+ });
104
+ });
105
+ },
106
+ dispose: function () {
107
+ void session.release();
108
+ },
109
+ }];
110
+ }
111
+ });
112
+ });
113
+ }
114
+
115
+ export { DEFAULT_MODEL_URL, createGuideRuntime };
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Model download for the card guide, kept apart from the ONNX runtime so a screen can preload the weights
3
+ * (13.5 MB, the main wait on mobile) without pulling onnxruntime-web or the guide logic.
4
+ */
5
+ /**
6
+ * card-guide-v2b-20261006-weights.onnx (SHA-256 5eae0785…f48b), public Datakeen bucket, CORS `*`.
7
+ * Temporary location for testing: APP3-2118 moves it to the Datakeen CDN with a versioned name and a long cache.
8
+ */
9
+ var DEFAULT_MODEL_URL = "https://datakeen-public.s3.eu-west-3.amazonaws.com/FmyFye6vkg8rrzrtQkW3QADx6eyner1q2WAEVUSB/xyG08fbEmVzYFwezy8zzJvHRbfgWpA8spA0TNeJQ.onnx";
10
+ var cache = new Map();
11
+ /** Downloads the model once per URL and shares the bytes between the preload and the runtime. */
12
+ function loadModelBytes(url) {
13
+ if (url === void 0) { url = DEFAULT_MODEL_URL; }
14
+ var cached = cache.get(url);
15
+ if (cached)
16
+ return cached;
17
+ var pending = fetch(url, { mode: "cors", credentials: "omit" })
18
+ .then(function (res) {
19
+ if (!res.ok)
20
+ throw new Error("Card guide model download failed (".concat(res.status, ")"));
21
+ return res.arrayBuffer();
22
+ })
23
+ .then(function (buf) { return new Uint8Array(buf); });
24
+ // a failed download must not poison the cache: the next attempt retries
25
+ pending.catch(function () { return cache.delete(url); });
26
+ cache.set(url, pending);
27
+ return pending;
28
+ }
29
+ /** Starts the download ahead of the capture screen. Never throws: the capture screen retries or falls back. */
30
+ function preloadCardGuideModel(url) {
31
+ if (url === void 0) { url = DEFAULT_MODEL_URL; }
32
+ if (typeof fetch !== "function")
33
+ return;
34
+ try {
35
+ loadModelBytes(url).catch(function () { });
36
+ }
37
+ catch (_a) {
38
+ // a non-standard fetch can throw synchronously: preloading is best effort
39
+ }
40
+ }
41
+
42
+ export { DEFAULT_MODEL_URL, loadModelBytes, preloadCardGuideModel };
@@ -0,0 +1,31 @@
1
+ import { useState, useEffect } from 'react';
2
+ import { getCaptureGuideConfig } from '../../services/sessionService.js';
3
+
4
+ var EMPTY = {};
5
+ /**
6
+ * Card guide thresholds configured on the server (APP3-2119), read once per session. Any failure (older backend,
7
+ * network) leaves `{}`: the SDK defaults apply and the capture is never blocked.
8
+ */
9
+ function useCaptureGuideThresholds(sessionId) {
10
+ var _a = useState(EMPTY), thresholds = _a[0], setThresholds = _a[1];
11
+ useEffect(function () {
12
+ if (!sessionId || typeof getCaptureGuideConfig !== "function")
13
+ return;
14
+ var cancelled = false;
15
+ getCaptureGuideConfig(sessionId)
16
+ .then(function (config) {
17
+ if (!cancelled)
18
+ setThresholds(config.thresholds);
19
+ })
20
+ .catch(function () {
21
+ if (!cancelled)
22
+ setThresholds(EMPTY);
23
+ });
24
+ return function () {
25
+ cancelled = true;
26
+ };
27
+ }, [sessionId]);
28
+ return thresholds;
29
+ }
30
+
31
+ export { useCaptureGuideThresholds };
@@ -0,0 +1,115 @@
1
+ import { __awaiter, __generator } from '../../node_modules/tslib/tslib.es6.js';
2
+ import { useState, useRef, useEffect } from 'react';
3
+ import { GuideTracker, MESSAGES } from './guideCore.js';
4
+ import { toGuidanceEvent } from './guidance.js';
5
+ import { createGuideRuntime } from './guideRuntime.js';
6
+
7
+ var INFERENCE_INTERVAL_MS = 120; // ~8 fps is plenty for guidance and keeps the CPU/battery cost low
8
+ /** Drives the card guide on a <video>: model load, throttled inference loop, temporal tracking, guidance events. */
9
+ function useCardCaptureGuide(videoRef, _a) {
10
+ var _this = this;
11
+ var _b = _a === void 0 ? {} : _a, modelUrl = _b.modelUrl, wasmPaths = _b.wasmPaths, _c = _b.active, active = _c === void 0 ? true : _c, _d = _b.enabled, enabled = _d === void 0 ? true : _d, tracker = _b.tracker, resolveMessage = _b.resolveMessage, onGuidance = _b.onGuidance, onFrame = _b.onFrame, onReady = _b.onReady, onTiming = _b.onTiming;
12
+ var _e = useState(false), modelReady = _e[0], setModelReady = _e[1];
13
+ var _f = useState(null), guide = _f[0], setGuide = _f[1];
14
+ var _g = useState(null), event = _g[0], setEvent = _g[1];
15
+ var _h = useState(null), error = _h[0], setError = _h[1];
16
+ var runtimeRef = useRef(null);
17
+ var trackerRef = useRef(null);
18
+ if (!trackerRef.current)
19
+ trackerRef.current = new GuideTracker(tracker);
20
+ // Server thresholds may arrive after mount: rebuild the tracker (it only holds a few frames of state).
21
+ var trackerKey = JSON.stringify(tracker !== null && tracker !== void 0 ? tracker : {});
22
+ var appliedTrackerKey = useRef(trackerKey);
23
+ useEffect(function () {
24
+ if (appliedTrackerKey.current === trackerKey)
25
+ return;
26
+ appliedTrackerKey.current = trackerKey;
27
+ trackerRef.current = new GuideTracker(tracker);
28
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- trackerKey is the content of `tracker`
29
+ }, [trackerKey]);
30
+ var lastCode = useRef(null);
31
+ var cb = useRef({ resolveMessage: resolveMessage, onGuidance: onGuidance, onFrame: onFrame, onReady: onReady, onTiming: onTiming });
32
+ cb.current = { resolveMessage: resolveMessage, onGuidance: onGuidance, onFrame: onFrame, onReady: onReady, onTiming: onTiming };
33
+ useEffect(function () {
34
+ if (!active)
35
+ return;
36
+ var cancelled = false;
37
+ var loadStartedAt = performance.now();
38
+ createGuideRuntime({ modelUrl: modelUrl, wasmPaths: wasmPaths })
39
+ .then(function (rt) {
40
+ var _a, _b;
41
+ if (cancelled) {
42
+ rt.dispose();
43
+ return;
44
+ }
45
+ runtimeRef.current = rt;
46
+ (_b = (_a = cb.current).onTiming) === null || _b === void 0 ? void 0 : _b.call(_a, { kind: "load", ms: performance.now() - loadStartedAt });
47
+ setModelReady(true);
48
+ })
49
+ .catch(function (e) {
50
+ if (!cancelled)
51
+ setError(e instanceof Error ? e.message : String(e));
52
+ });
53
+ return function () {
54
+ var _a;
55
+ cancelled = true;
56
+ (_a = runtimeRef.current) === null || _a === void 0 ? void 0 : _a.dispose();
57
+ runtimeRef.current = null;
58
+ setModelReady(false);
59
+ };
60
+ }, [active, modelUrl, wasmPaths]);
61
+ useEffect(function () {
62
+ if (!modelReady || !enabled)
63
+ return;
64
+ var busy = false;
65
+ var timer = setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
66
+ var video, rt, runStartedAt, _a, decoded, quality, frameBrightness, now, g, code, message, ev, e_1;
67
+ var _b, _c, _d, _e, _f, _g, _h, _j;
68
+ return __generator(this, function (_k) {
69
+ switch (_k.label) {
70
+ case 0:
71
+ video = videoRef.current;
72
+ rt = runtimeRef.current;
73
+ if (busy || !rt || !video || video.videoWidth === 0)
74
+ return [2 /*return*/];
75
+ busy = true;
76
+ _k.label = 1;
77
+ case 1:
78
+ _k.trys.push([1, 3, 4, 5]);
79
+ runStartedAt = performance.now();
80
+ return [4 /*yield*/, rt.run(video)];
81
+ case 2:
82
+ _a = _k.sent(), decoded = _a.decoded, quality = _a.quality, frameBrightness = _a.frameBrightness;
83
+ now = performance.now();
84
+ (_c = (_b = cb.current).onTiming) === null || _c === void 0 ? void 0 : _c.call(_b, { kind: "inference", ms: now - runStartedAt });
85
+ g = trackerRef.current.update(decoded, video.videoWidth, video.videoHeight, now, quality, frameBrightness);
86
+ code = g.state;
87
+ message = cb.current.resolveMessage ? cb.current.resolveMessage(code) : MESSAGES[code];
88
+ ev = toGuidanceEvent(g, lastCode.current, message, now);
89
+ setGuide(g);
90
+ setEvent(ev);
91
+ (_e = (_d = cb.current).onFrame) === null || _e === void 0 ? void 0 : _e.call(_d, ev);
92
+ if (code !== lastCode.current) {
93
+ lastCode.current = code;
94
+ (_g = (_f = cb.current).onGuidance) === null || _g === void 0 ? void 0 : _g.call(_f, ev);
95
+ }
96
+ if (g.readyNow)
97
+ (_j = (_h = cb.current).onReady) === null || _j === void 0 ? void 0 : _j.call(_h, g);
98
+ return [3 /*break*/, 5];
99
+ case 3:
100
+ e_1 = _k.sent();
101
+ setError(e_1 instanceof Error ? e_1.message : String(e_1));
102
+ return [3 /*break*/, 5];
103
+ case 4:
104
+ busy = false;
105
+ return [7 /*endfinally*/];
106
+ case 5: return [2 /*return*/];
107
+ }
108
+ });
109
+ }); }, INFERENCE_INTERVAL_MS);
110
+ return function () { return clearInterval(timer); };
111
+ }, [modelReady, enabled, videoRef]);
112
+ return { modelReady: modelReady, guide: guide, event: event, error: error };
113
+ }
114
+
115
+ export { useCardCaptureGuide };
@@ -0,0 +1,28 @@
1
+ import { useState, useRef, useEffect } from 'react';
2
+
3
+ /**
4
+ * Returns `value`, but keeps each shown value on screen for at least `minMs` so guidance text does not flicker.
5
+ * A value superseded during the wait is skipped: only the latest one is shown once the wait is over.
6
+ */
7
+ function useMinDisplay(value, minMs) {
8
+ var _a = useState(value), shown = _a[0], setShown = _a[1];
9
+ var shownSince = useRef(Date.now());
10
+ useEffect(function () {
11
+ if (Object.is(value, shown))
12
+ return;
13
+ var apply = function () {
14
+ shownSince.current = Date.now();
15
+ setShown(value);
16
+ };
17
+ var wait = minMs - (Date.now() - shownSince.current);
18
+ if (wait <= 0) {
19
+ apply();
20
+ return;
21
+ }
22
+ var timer = setTimeout(apply, wait);
23
+ return function () { return clearTimeout(timer); };
24
+ }, [value, shown, minMs]);
25
+ return shown;
26
+ }
27
+
28
+ export { useMinDisplay };