@ateam-ai-mobile/expo 0.1.22 → 0.1.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/aiLive/provider.d.mts +4 -1
- package/dist/aiLive/provider.d.ts +4 -1
- package/dist/aiLive/provider.js +161 -12
- package/dist/aiLive/provider.js.map +1 -1
- package/dist/aiLive/provider.mjs +3 -1
- package/dist/bootstrap.js +159 -12
- package/dist/bootstrap.js.map +1 -1
- package/dist/bootstrap.mjs +1 -1
- package/dist/{chunk-PV2EHH2P.mjs → chunk-PQUTEM3C.mjs} +161 -13
- package/dist/chunk-PQUTEM3C.mjs.map +1 -0
- package/package.json +5 -4
- package/src/aiLive/motionGate.ts +87 -0
- package/src/aiLive/provider.ts +54 -13
- package/src/aiLive/signature.ts +33 -0
- package/dist/chunk-PV2EHH2P.mjs.map +0 -1
package/dist/bootstrap.mjs
CHANGED
|
@@ -81,7 +81,103 @@ function base64ToBytes(b64) {
|
|
|
81
81
|
return o === out.length ? out : out.subarray(0, o);
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
+
// src/aiLive/motionGate.ts
|
|
85
|
+
var SIG_W = 16;
|
|
86
|
+
var SIG_H = 12;
|
|
87
|
+
var SLACK_SEC = 0.15;
|
|
88
|
+
var SENSITIVITY = Object.freeze({ low: 0.06, medium: 0.03, high: 0.015 });
|
|
89
|
+
var GATE_DEFAULTS = Object.freeze({ enabled: true, sensitivity: "medium", preRollSec: 1, postRollSec: 1, heartbeatSec: 8 });
|
|
90
|
+
function difference(a, b) {
|
|
91
|
+
if (!a || !b || a.length !== b.length || !a.length) return 1;
|
|
92
|
+
let sum = 0;
|
|
93
|
+
for (let i = 0; i < a.length; i++) sum += Math.abs(a[i] - b[i]);
|
|
94
|
+
return sum / (a.length * 255);
|
|
95
|
+
}
|
|
96
|
+
function gateConfigFrom(d) {
|
|
97
|
+
const c = { ...GATE_DEFAULTS };
|
|
98
|
+
if (!d || typeof d !== "object") return c;
|
|
99
|
+
if (typeof d.enabled === "boolean") c.enabled = d.enabled;
|
|
100
|
+
if (typeof d.sensitivity === "string" && Object.prototype.hasOwnProperty.call(SENSITIVITY, d.sensitivity)) c.sensitivity = d.sensitivity;
|
|
101
|
+
for (const k of ["preRollSec", "postRollSec", "heartbeatSec"]) {
|
|
102
|
+
const v = Number(d[k]);
|
|
103
|
+
if (Number.isFinite(v) && v >= 0 && v <= 60) c[k] = v;
|
|
104
|
+
}
|
|
105
|
+
return c;
|
|
106
|
+
}
|
|
107
|
+
function createMotionGate(cfg = {}) {
|
|
108
|
+
const c = { ...GATE_DEFAULTS, ...cfg };
|
|
109
|
+
const threshold = SENSITIVITY[c.sensitivity] ?? SENSITIVITY.medium;
|
|
110
|
+
let prevSig = null;
|
|
111
|
+
let lastChangeT = -Infinity;
|
|
112
|
+
let lastSentT = -Infinity;
|
|
113
|
+
let held = [];
|
|
114
|
+
const n = { offered: 0, sent: 0, held: 0, heartbeats: 0, changes: 0 };
|
|
115
|
+
const sendNow = (t, item, reason, withPreRoll = false) => {
|
|
116
|
+
const out = withPreRoll ? held.filter((h) => t - h.t <= c.preRollSec + SLACK_SEC).map((h) => h.item) : [];
|
|
117
|
+
held = [];
|
|
118
|
+
out.push(item);
|
|
119
|
+
lastSentT = t;
|
|
120
|
+
n.sent += out.length;
|
|
121
|
+
return { send: out, reason };
|
|
122
|
+
};
|
|
123
|
+
return {
|
|
124
|
+
offer({ t, sig, item }) {
|
|
125
|
+
n.offered++;
|
|
126
|
+
if (!c.enabled) {
|
|
127
|
+
lastSentT = t;
|
|
128
|
+
n.sent++;
|
|
129
|
+
return { send: [item], reason: "off" };
|
|
130
|
+
}
|
|
131
|
+
const first = prevSig == null;
|
|
132
|
+
const d = difference(prevSig, sig);
|
|
133
|
+
prevSig = sig;
|
|
134
|
+
if (first) return sendNow(t, item, "first");
|
|
135
|
+
if (d > threshold) {
|
|
136
|
+
lastChangeT = t;
|
|
137
|
+
n.changes++;
|
|
138
|
+
return sendNow(t, item, "change", true);
|
|
139
|
+
}
|
|
140
|
+
if (t - lastChangeT <= c.postRollSec + SLACK_SEC) return sendNow(t, item, "after");
|
|
141
|
+
if (t - lastSentT >= c.heartbeatSec) {
|
|
142
|
+
n.heartbeats++;
|
|
143
|
+
return sendNow(t, item, "heartbeat");
|
|
144
|
+
}
|
|
145
|
+
held.push({ t, item });
|
|
146
|
+
while (held.length && t - held[0].t > c.preRollSec + 1) held.shift();
|
|
147
|
+
n.held++;
|
|
148
|
+
return { send: [], reason: "quiet" };
|
|
149
|
+
},
|
|
150
|
+
counters: () => ({ ...n }),
|
|
151
|
+
config: () => ({ ...c })
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// src/aiLive/signature.ts
|
|
156
|
+
import { decode } from "jpeg-js";
|
|
157
|
+
var SIG_JPEG_SIDE = 64;
|
|
158
|
+
function signatureFromJpeg(b64) {
|
|
159
|
+
const img = decode(base64ToBytes(b64), { useTArray: true, formatAsRGBA: true, maxResolutionInMP: 1 });
|
|
160
|
+
const { width: w, height: h, data } = img;
|
|
161
|
+
if (!w || !h) throw new Error("empty picture");
|
|
162
|
+
const sum = new Float64Array(SIG_W * SIG_H);
|
|
163
|
+
const cnt = new Uint32Array(SIG_W * SIG_H);
|
|
164
|
+
for (let y = 0; y < h; y++) {
|
|
165
|
+
const cy = Math.min(SIG_H - 1, Math.floor(y * SIG_H / h));
|
|
166
|
+
for (let x = 0; x < w; x++) {
|
|
167
|
+
const cx = Math.min(SIG_W - 1, Math.floor(x * SIG_W / w));
|
|
168
|
+
const i = (y * w + x) * 4;
|
|
169
|
+
const k = cy * SIG_W + cx;
|
|
170
|
+
sum[k] += 0.299 * data[i] + 0.587 * data[i + 1] + 0.114 * data[i + 2];
|
|
171
|
+
cnt[k]++;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
const out = new Uint8Array(SIG_W * SIG_H);
|
|
175
|
+
for (let k = 0; k < out.length; k++) out[k] = cnt[k] ? Math.round(sum[k] / cnt[k]) : 0;
|
|
176
|
+
return out;
|
|
177
|
+
}
|
|
178
|
+
|
|
84
179
|
// src/aiLive/provider.ts
|
|
180
|
+
var PHONE_MAX_PRE_ROLL_SEC = 5;
|
|
85
181
|
var LIVE_KIND = "ai-models.live";
|
|
86
182
|
var FRESH_MS = 3e3;
|
|
87
183
|
var MAX_BUFFERED = 512 * 1024;
|
|
@@ -95,13 +191,14 @@ var defaultAiLiveDeps = () => ({
|
|
|
95
191
|
token: getToken,
|
|
96
192
|
tenant: getTenant,
|
|
97
193
|
jpegById,
|
|
194
|
+
signatureById: async (id) => signatureFromJpeg(await jpegById(id, SIG_JPEG_SIDE, 0.6)),
|
|
98
195
|
now: Date.now,
|
|
99
196
|
setInterval: (fn, ms) => setInterval(fn, ms),
|
|
100
197
|
clearInterval: (h) => clearInterval(h),
|
|
101
198
|
setTimeout: (fn, ms) => setTimeout(fn, ms),
|
|
102
199
|
clearTimeout: (h) => clearTimeout(h)
|
|
103
200
|
});
|
|
104
|
-
var idle = () => ({ state: "idle", camera: null, sent: 0, skipped: 0, lastEpisode: null, lastError: null });
|
|
201
|
+
var idle = () => ({ state: "idle", camera: null, sent: 0, skipped: 0, heldBack: 0, lastEpisode: null, lastError: null });
|
|
105
202
|
function createAiLiveProvider(overrides = {}) {
|
|
106
203
|
const deps = { ...defaultAiLiveDeps(), ...overrides };
|
|
107
204
|
let owner = null;
|
|
@@ -117,9 +214,11 @@ function createAiLiveProvider(overrides = {}) {
|
|
|
117
214
|
let closingOnPurpose = false;
|
|
118
215
|
let seq = 0;
|
|
119
216
|
let busy = false;
|
|
217
|
+
let owedGaps = 0;
|
|
120
218
|
let gen = 0;
|
|
121
219
|
let lastFrameId = null;
|
|
122
220
|
let camera = DEFAULT_CAMERA;
|
|
221
|
+
let gate = null;
|
|
123
222
|
let lastOwner = null;
|
|
124
223
|
const episodeCbs = /* @__PURE__ */ new Map();
|
|
125
224
|
const snapCbs = /* @__PURE__ */ new Map();
|
|
@@ -162,7 +261,6 @@ function createAiLiveProvider(overrides = {}) {
|
|
|
162
261
|
}
|
|
163
262
|
}
|
|
164
263
|
async function slot() {
|
|
165
|
-
const mySeq = seq++;
|
|
166
264
|
const g = gen;
|
|
167
265
|
const sock = ws;
|
|
168
266
|
const current = () => g === gen && ws === sock && sock?.readyState === 1;
|
|
@@ -171,13 +269,22 @@ function createAiLiveProvider(overrides = {}) {
|
|
|
171
269
|
if (g === gen) void teardown("stopped", CONSENT_TAKEN_BACK);
|
|
172
270
|
return true;
|
|
173
271
|
};
|
|
272
|
+
const gap = (lastError) => {
|
|
273
|
+
seq++;
|
|
274
|
+
set(lastError ? { skipped: snap.skipped + 1, lastError } : { skipped: snap.skipped + 1 });
|
|
275
|
+
};
|
|
174
276
|
if (consentGone()) return;
|
|
175
|
-
if (busy
|
|
277
|
+
if (busy && current()) {
|
|
278
|
+
owedGaps++;
|
|
176
279
|
set({ skipped: snap.skipped + 1 });
|
|
177
280
|
return;
|
|
178
281
|
}
|
|
282
|
+
if (busy || !current()) {
|
|
283
|
+
gap();
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
179
286
|
if ((sock.bufferedAmount || 0) > MAX_BUFFERED) {
|
|
180
|
-
|
|
287
|
+
gap();
|
|
181
288
|
return;
|
|
182
289
|
}
|
|
183
290
|
busy = true;
|
|
@@ -188,19 +295,52 @@ function createAiLiveProvider(overrides = {}) {
|
|
|
188
295
|
const id = f?.handle?.id;
|
|
189
296
|
const t = Number(f?.timestamp);
|
|
190
297
|
if (!id || id === lastFrameId || !(deps.now() - t < FRESH_MS)) {
|
|
191
|
-
|
|
298
|
+
gap();
|
|
192
299
|
return;
|
|
193
300
|
}
|
|
194
|
-
const b64 = await deps.jpegById(id, camera.maxPx, 0.7);
|
|
195
|
-
if (!current() || consentGone()) return;
|
|
196
|
-
const frame = encodeFrame({ seq: mySeq, tCapture: t, payload: base64ToBytes(b64) });
|
|
197
|
-
sock.send(frame.buffer);
|
|
198
301
|
lastFrameId = id;
|
|
199
|
-
|
|
302
|
+
let toSend = [{ id, t }];
|
|
303
|
+
const myGate = gate;
|
|
304
|
+
if (myGate) {
|
|
305
|
+
let sig = null;
|
|
306
|
+
try {
|
|
307
|
+
sig = await deps.signatureById(id);
|
|
308
|
+
} catch {
|
|
309
|
+
sig = null;
|
|
310
|
+
}
|
|
311
|
+
if (!current() || consentGone()) return;
|
|
312
|
+
toSend = myGate.offer({ t: t / 1e3, sig, item: { id, t } }).send;
|
|
313
|
+
if (!toSend.length) {
|
|
314
|
+
set({ heldBack: (snap.heldBack ?? 0) + 1 });
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
for (const it of toSend) {
|
|
319
|
+
if (!current() || consentGone()) return;
|
|
320
|
+
if ((sock.bufferedAmount || 0) > MAX_BUFFERED) {
|
|
321
|
+
gap();
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
let b64;
|
|
325
|
+
try {
|
|
326
|
+
b64 = await deps.jpegById(it.id, camera.maxPx, 0.7);
|
|
327
|
+
} catch (e) {
|
|
328
|
+
if (g === gen) gap(String(e?.message || e));
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
if (!current() || consentGone()) return;
|
|
332
|
+
const frame = encodeFrame({ seq: seq++, tCapture: it.t, payload: base64ToBytes(b64) });
|
|
333
|
+
sock.send(frame.buffer);
|
|
334
|
+
set({ sent: snap.sent + 1 });
|
|
335
|
+
}
|
|
200
336
|
} catch (e) {
|
|
201
|
-
if (g === gen)
|
|
337
|
+
if (g === gen) gap(String(e?.message || e));
|
|
202
338
|
} finally {
|
|
203
|
-
if (g === gen)
|
|
339
|
+
if (g === gen) {
|
|
340
|
+
busy = false;
|
|
341
|
+
seq += owedGaps;
|
|
342
|
+
owedGaps = 0;
|
|
343
|
+
}
|
|
204
344
|
}
|
|
205
345
|
}
|
|
206
346
|
function openSocket() {
|
|
@@ -245,6 +385,11 @@ function createAiLiveProvider(overrides = {}) {
|
|
|
245
385
|
camera = c && Number(c.fps) > 0 && Number(c.maxPx) > 0 ? { fps: Number(c.fps), maxPx: Number(c.maxPx) } : DEFAULT_CAMERA;
|
|
246
386
|
return;
|
|
247
387
|
}
|
|
388
|
+
if (ev.event === "motion" && !opened) {
|
|
389
|
+
const c = gateConfigFrom(ev.data);
|
|
390
|
+
gate = c.enabled ? createMotionGate({ ...c, preRollSec: Math.min(c.preRollSec, PHONE_MAX_PRE_ROLL_SEC) }) : null;
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
248
393
|
if (ev.event === "opened" && !opened) {
|
|
249
394
|
opened = true;
|
|
250
395
|
deps.clearTimeout(gaveUp);
|
|
@@ -351,8 +496,10 @@ function createAiLiveProvider(overrides = {}) {
|
|
|
351
496
|
allowed = typeof ctx.allowed === "function" ? ctx.allowed : () => false;
|
|
352
497
|
gen++;
|
|
353
498
|
seq = 0;
|
|
499
|
+
owedGaps = 0;
|
|
354
500
|
lastFrameId = null;
|
|
355
501
|
busy = false;
|
|
502
|
+
gate = null;
|
|
356
503
|
const g = gen;
|
|
357
504
|
const cameraOfScreen = ctx.visual;
|
|
358
505
|
snap = { ...idle(), camera: cam };
|
|
@@ -417,10 +564,11 @@ function createAiLiveProvider(overrides = {}) {
|
|
|
417
564
|
}
|
|
418
565
|
|
|
419
566
|
export {
|
|
567
|
+
PHONE_MAX_PRE_ROLL_SEC,
|
|
420
568
|
LIVE_KIND,
|
|
421
569
|
OPEN_TIMEOUT_MS,
|
|
422
570
|
CLOSE_WAIT_MS,
|
|
423
571
|
defaultAiLiveDeps,
|
|
424
572
|
createAiLiveProvider
|
|
425
573
|
};
|
|
426
|
-
//# sourceMappingURL=chunk-
|
|
574
|
+
//# sourceMappingURL=chunk-PQUTEM3C.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/aiLive/provider.ts","../src/aiLive/streamProtocol.ts","../src/aiLive/motionGate.ts","../src/aiLive/signature.ts"],"sourcesContent":["/**\n * The AI live provider: this phone's camera → the workspace's live session → the chosen AI,\n * and what the AI saw back to the screen. Installed by the host once at startup:\n * import { createAiLiveProvider } from '@ateam-ai-mobile/expo/ai-live';\n * setAiLiveProvider(createAiLiveProvider());\n * A plugin reaches it only through props.aiLive,\n * whose gate (plugin-runtime aiLiveHost) has already checked the two-sided consent. The gate\n * passes that check as ctx.allowed, and it is asked again right before every picture: once the\n * person takes the yes back, nothing more is sent and the session stops (GPT review 2026-10-30).\n *\n * One camera, one sender: while one screen sends, another's start() is refused by name.\n *\n * ONE CONNECTION IS THE SESSION (owner direction 2026-10-30). start():\n * 1. opens Core's stream endpoint (/ws/stream, kind \"ai-models.live\", the login in the\n * subprotocol) asking for a session of its own (params.session \"own\"). That one round trip\n * IS starting the person's live session: Core starts it with its own $0.50 cap, refuses in\n * plain words when the person already has one running (never joins), and says how to send\n * ({\"event\":\"session\"}: the rate and size the setup was tested at) before \"opened\";\n * 2. turns on the screen's held camera on the requested side;\n * 3. sends one picture per slot at that rate and size, from the newest fresh frame, as a JPEG\n * made by native (jpegById). A slot with no fresh frame, or while the socket is still busy,\n * sends nothing and still takes its sequence number, so Core reports it as a gap;\n * 4. {\"event\":\"episode\"} from Core → the screen's onEpisode callbacks.\n * THE MOTION GATE (owner 2026-11-01, GPT review 2026-11-01): when Core sends {\"event\":\"motion\"} (the workspace's\n * Live tab settings) before \"opened\", each slot first takes the frame's signature (a tiny JPEG decoded here,\n * signature.ts) and the gate (motionGate.ts, the same rule as the computer's camera window) decides: while\n * nothing changes the picture is held back, takes NO sequence number (it is not lost, Core must not count a\n * gap) and costs nothing; one goes anyway every heartbeat so the AI still looks; on a change the held last\n * second goes first, then the change, then one more second after it. Core that sends no \"motion\" gets every\n * picture, as before.\n * stop() closes the connection and the camera; Core ends the session when the connection ends,\n * whatever ended it (a stop, the screen closing, the app killed, the network lost).\n */\nimport { getCoreWsUrl, getTenant, getToken } from '@ateam-ai-mobile/core';\nimport { CONSENT_TAKEN_BACK } from '@ateam-ai-mobile/plugin-runtime';\nimport type { AiLiveEpisode, AiLiveProvider, AiLiveSnapshot, AiLiveStartOptions, DeviceState, Subscription } from '@ateam-ai-mobile/plugin-runtime';\nimport { jpegById as nativeJpegById } from '../vision';\nimport { CLOSE_MESSAGE, STREAM_PATH, SUBPROTOCOL, base64ToBytes, describeClose, encodeFrame, openMessage, parseServerEvent } from './streamProtocol';\nimport { createMotionGate, gateConfigFrom } from './motionGate';\n\n// The gate holds frame ids, not pictures: a held id must still resolve in the native ring when it is sent. The ring\n// keeps 12 frames (AdaVisionModule.swift ringCap), 6 s at the fastest stream rate (500 ms), so a longer pre-roll from\n// Core would send only its tail. 5 s stays inside it with margin.\nexport const PHONE_MAX_PRE_ROLL_SEC = 5;\nimport { SIG_JPEG_SIDE, signatureFromJpeg } from './signature';\n\nexport const LIVE_KIND = 'ai-models.live';\n/** A frame older than this is not \"what the camera sees now\" and is not sent. */\nconst FRESH_MS = 3000;\n/** Bytes still waiting in the socket above which a slot is skipped rather than queued. */\nconst MAX_BUFFERED = 512 * 1024;\nconst DEFAULT_CAMERA = { fps: 1, maxPx: 768 };\n\ntype Visual = DeviceState['visual'];\n\nexport interface AiLiveDeps {\n WebSocketImpl: any;\n wsBase(): string;\n token(): string | null;\n tenant(): string | null;\n jpegById(id: string, maxSide: number, quality: number): Promise<string>;\n /** The frame's signature for the motion gate (signature.ts); null = could not take it (the picture is then sent). */\n signatureById(id: string): Promise<ArrayLike<number> | null>;\n now(): number;\n setInterval(fn: () => void, ms: number): any;\n clearInterval(handle: any): void;\n setTimeout(fn: () => void, ms: number): any;\n clearTimeout(handle: any): void;\n}\n\n/** Core answers \"opened\" in one round trip (it starts the session first). Past this, the start gives up\n * and closes the connection, so a start can never hang a screen's queue. */\nexport const OPEN_TIMEOUT_MS = 20_000;\n/** A stop waits this long, at most, for its connection to finish closing: Core ends the session when it\n * sees the close, so a restart right after a stop must not open the next one before (Codex on #53).\n * Past this the transport is taken as broken, and Core's own 60 s rule ends that session. */\nexport const CLOSE_WAIT_MS = 3_000;\n\nconst isEpisode = (d: any): d is AiLiveEpisode =>\n !!d && typeof d === 'object' && typeof d.type === 'string' && typeof d.class === 'string' &&\n typeof d.episodeId === 'string' && typeof d.actorId === 'string' && d.actorId.length > 0 && Number.isFinite(d.at);\n\nexport const defaultAiLiveDeps = (): AiLiveDeps => ({\n WebSocketImpl: (globalThis as any).WebSocket,\n wsBase: getCoreWsUrl,\n token: getToken,\n tenant: getTenant,\n jpegById: nativeJpegById,\n signatureById: async (id) => signatureFromJpeg(await nativeJpegById(id, SIG_JPEG_SIDE, 0.6)),\n now: Date.now,\n setInterval: (fn, ms) => setInterval(fn, ms),\n clearInterval: (h) => clearInterval(h),\n setTimeout: (fn, ms) => setTimeout(fn, ms),\n clearTimeout: (h) => clearTimeout(h),\n});\n\nconst idle = (): AiLiveSnapshot => ({ state: 'idle', camera: null, sent: 0, skipped: 0, heldBack: 0, lastEpisode: null, lastError: null });\ntype GateItem = { id: string; t: number };\n\nexport function createAiLiveProvider(overrides: Partial<AiLiveDeps> = {}): AiLiveProvider {\n const deps: AiLiveDeps = { ...defaultAiLiveDeps(), ...overrides };\n let owner: string | null = null; // the screen sending now\n let snap: AiLiveSnapshot = idle();\n let ws: any = null;\n let loop: any = null;\n let visual: Visual | null = null;\n let allowed: () => boolean = () => false; // the two-sided consent, asked before every picture\n let stopReason: string | null = null; // why the last teardown happened, known before it finishes\n // A teardown still finishing (the camera, the connection): a start waits for it, so a quick restart neither\n // reads the old session as its own nor is cleared by it afterwards (Codex on #53). Every teardown is\n // tracked, the ones the provider starts itself (consent gone, the connection dropped) included.\n let tearingDown: Promise<void> | null = null;\n // Screens whose start is waiting for that teardown, before they own anything: a stop meanwhile cancels\n // the start (it would otherwise go ahead after the screen had stopped or closed; Codex on #53).\n const waitingToStart = new Map<string, { cancelled: boolean }>();\n /** Each connection's \"it has closed\": a stop waits for it (CLOSE_WAIT_MS at most). */\n const closedOf = new WeakMap<object, Promise<void>>();\n let closingOnPurpose = false;\n let seq = 0;\n let busy = false;\n let owedGaps = 0; // slots skipped while one was still working: numbered when it ends\n // Which session a slot belongs to. A slot awaits the camera and the JPEG maker; when the screen\n // stops (or another starts) meanwhile, that slot's picture must go nowhere, not into the next one.\n let gen = 0;\n let lastFrameId: string | null = null;\n let camera = DEFAULT_CAMERA;\n let gate: ReturnType<typeof createMotionGate<GateItem>> | null = null; // this session's motion gate, if Core sent one\n let lastOwner: string | null = null; // whose the current snapshot is (it outlives a stop)\n // Each screen hears only its own session: callbacks are kept per holder.\n const episodeCbs = new Map<string, Set<(ev: AiLiveEpisode) => void>>();\n const snapCbs = new Map<string, Set<(s: AiLiveSnapshot) => void>>();\n const sendingCbs = new Set<() => void>();\n let wasSending = false;\n const add = <T>(m: Map<string, Set<T>>, who: string, cb: T): Subscription => {\n let set = m.get(who);\n if (!set) { set = new Set(); m.set(who, set); }\n set.add(cb);\n return { unsubscribe: () => { set!.delete(cb); } };\n };\n\n const set = (patch: Partial<AiLiveSnapshot>) => {\n snap = { ...snap, ...patch };\n for (const cb of (lastOwner && snapCbs.get(lastOwner)) || []) { try { cb(snap); } catch { /* a listener's error is its own */ } }\n const sending = snap.state === 'starting' || snap.state === 'sending';\n if (sending !== wasSending) { wasSending = sending; for (const cb of sendingCbs) { try { cb(); } catch { /* idem */ } } }\n };\n\n function stopTimers() {\n if (loop != null) { deps.clearInterval(loop); loop = null; }\n }\n\n async function slot() {\n const g = gen;\n const sock = ws;\n const current = () => g === gen && ws === sock && sock?.readyState === 1;\n const consentGone = () => { if (allowed()) return false; if (g === gen) void teardown('stopped', CONSENT_TAKEN_BACK); return true; };\n // Every slot that does not send takes a number, so Core sees the gap; a picture the motion gate holds back\n // takes none (it was looked at, nothing changed: not lost).\n const gap = (lastError?: string) => { seq++; set(lastError ? { skipped: snap.skipped + 1, lastError } : { skipped: snap.skipped + 1 }); };\n if (consentGone()) return;\n // A slot that finds the previous one still working is a gap AFTER that one's pictures: its number is taken\n // when the running slot finishes, so numbers stay in capture order even when encoding is slower than the timer.\n if (busy && current()) { owedGaps++; set({ skipped: snap.skipped + 1 }); return; }\n if (busy || !current()) { gap(); return; }\n if ((sock.bufferedAmount || 0) > MAX_BUFFERED) { gap(); return; }\n busy = true;\n try {\n const v = await visual!.getSnapshot();\n if (!current()) return;\n const f: any = v?.latest;\n const id = f?.handle?.id;\n const t = Number(f?.timestamp);\n if (!id || id === lastFrameId || !(deps.now() - t < FRESH_MS)) { gap(); return; }\n lastFrameId = id;\n let toSend: GateItem[] = [{ id, t }];\n const myGate = gate;\n if (myGate) {\n let sig: ArrayLike<number> | null = null;\n try { sig = await deps.signatureById(id); } catch { sig = null; } // no signature: counts as a change, sent\n if (!current() || consentGone()) return;\n toSend = myGate.offer({ t: t / 1000, sig, item: { id, t } }).send;\n if (!toSend.length) { set({ heldBack: (snap.heldBack ?? 0) + 1 }); return; }\n }\n // Oldest first: the held last second (on a change), then this picture.\n for (const it of toSend) {\n if (!current() || consentGone()) return;\n if ((sock.bufferedAmount || 0) > MAX_BUFFERED) { gap(); continue; }\n let b64: string;\n try { b64 = await deps.jpegById(it.id, camera.maxPx, 0.7); } catch (e: any) { if (g === gen) gap(String(e?.message || e)); continue; }\n if (!current() || consentGone()) return;\n const frame = encodeFrame({ seq: seq++, tCapture: it.t, payload: base64ToBytes(b64) });\n sock.send(frame.buffer);\n set({ sent: snap.sent + 1 });\n }\n } catch (e: any) {\n if (g === gen) gap(String(e?.message || e));\n } finally {\n if (g === gen) { busy = false; seq += owedGaps; owedGaps = 0; }\n }\n }\n\n function openSocket(): Promise<void> {\n return new Promise((resolve, reject) => {\n const token = deps.token();\n const tenant = deps.tenant();\n if (!token || !tenant) { reject(new Error('not signed in to a workspace')); return; }\n const url = `${deps.wsBase()}${STREAM_PATH}?tenant=${encodeURIComponent(tenant)}`;\n let opened = false;\n const sock = new deps.WebSocketImpl(url, [SUBPROTOCOL, `adas.bearer.${token}`]);\n ws = sock;\n sock.binaryType = 'arraybuffer';\n let markClosed: () => void = () => {};\n closedOf.set(sock, new Promise<void>((r) => { markClosed = r; }));\n const gaveUp = deps.setTimeout(() => {\n if (opened) return;\n reject(new Error('Core did not answer in time. Try again.'));\n try { sock.close(); } catch { /* closing anyway */ }\n }, OPEN_TIMEOUT_MS);\n // Its own session: opening IS starting it, and Core ends it when this connection ends.\n sock.onopen = () => { try { sock.send(openMessage(LIVE_KIND, { session: 'own', source: 'phone' })); } catch (e) { reject(e); } };\n sock.onmessage = (m: any) => {\n // Only the current connection speaks for the current session. A message from a connection\n // already stopped or replaced (it can still be queued after a stop, a restart or a switch to\n // another screen) is dropped whole: its episode never reaches a newer screen, and its \"session\"\n // or \"opened\" never touches a newer connection (GPT code review v5).\n if (ws !== sock) return;\n const ev = parseServerEvent(m?.data);\n if (!ev) return;\n if (ev.event === 'session' && !opened) {\n const c = ev.data?.camera;\n camera = c && Number(c.fps) > 0 && Number(c.maxPx) > 0 ? { fps: Number(c.fps), maxPx: Number(c.maxPx) } : DEFAULT_CAMERA;\n return;\n }\n if (ev.event === 'motion' && !opened) { const c = gateConfigFrom(ev.data); gate = c.enabled ? createMotionGate<GateItem>({ ...c, preRollSec: Math.min(c.preRollSec, PHONE_MAX_PRE_ROLL_SEC) }) : null; return; }\n if (ev.event === 'opened' && !opened) { opened = true; deps.clearTimeout(gaveUp); resolve(); return; }\n // Only a whole episode reaches the screen: what happened, of which class, and whose camera saw it\n // (actorId: live sessions are per person). Anything less is dropped, never passed on half-made.\n if (ev.event === 'episode' && isEpisode(ev.data)) {\n const episode = ev.data;\n set({ lastEpisode: episode });\n for (const cb of (owner && episodeCbs.get(owner)) || []) { try { cb(episode); } catch { /* a listener's error is its own */ } }\n }\n };\n sock.onerror = () => { /* the close that follows says why */ };\n sock.onclose = (c: any) => {\n markClosed();\n const why = describeClose(Number(c?.code), c?.reason);\n if (!opened) { deps.clearTimeout(gaveUp); reject(new Error(why)); return; }\n if (closingOnPurpose || ws !== sock) return;\n void teardown('error', why);\n };\n });\n }\n\n function teardown(state: 'stopped' | 'error', lastError: string | null): Promise<void> {\n // One at a time: a second stop while one is finishing joins it (Codex on #53). Two at once could\n // finish out of order and the first, resuming late, would stop a session started in between.\n if (tearingDown) return tearingDown;\n const p = teardownNow(state, lastError);\n tearingDown = p;\n void p.then(() => {}, () => {}).then(() => { if (tearingDown === p) tearingDown = null; });\n return p;\n }\n\n // Closing the connection ends the session (Core); the camera is this phone's to stop.\n async function teardownNow(state: 'stopped' | 'error', lastError: string | null) {\n stopReason = lastError;\n allowed = () => false;\n gen++;\n stopTimers();\n const sock = ws;\n ws = null;\n closingOnPurpose = true;\n try { if (sock && sock.readyState === 1) sock.send(CLOSE_MESSAGE); } catch { /* closing anyway */ }\n try { sock?.close?.(); } catch { /* already closed */ }\n closingOnPurpose = false;\n const v = visual;\n visual = null;\n // The camera off, and the connection really closed (so Core has ended its session) before this stop\n // is done: a start right after it then never meets the old session (Codex on #53).\n let gaveUp: any = null;\n const closed = sock && closedOf.get(sock);\n await Promise.all([\n (async () => { try { await v?.stop?.(); } catch { /* the camera hold is released by the gate too */ } })(),\n closed ? Promise.race([closed, new Promise<void>((r) => { gaveUp = deps.setTimeout(r, CLOSE_WAIT_MS); })]) : Promise.resolve(),\n ]);\n if (gaveUp != null) deps.clearTimeout(gaveUp);\n owner = null;\n set({ state, lastError });\n }\n\n return {\n async start(opts: AiLiveStartOptions, who: string, ctx: { visual: Visual; allowed: () => boolean }) {\n if (tearingDown) {\n const wait = { cancelled: false };\n waitingToStart.set(who, wait);\n try {\n while (tearingDown) { try { await tearingDown; } catch { /* it stopped either way */ } }\n } finally {\n if (waitingToStart.get(who) === wait) waitingToStart.delete(who);\n }\n if (wait.cancelled) throw Object.assign(new Error('aiLive: stopped before it started.'), { code: 'STOPPED' });\n }\n if (owner && owner !== who) throw Object.assign(new Error('aiLive: another screen is sending to AI right now. Close it first.'), { code: 'CONFLICT' });\n if (owner === who) return snap;\n const cam = opts?.camera === 'front' ? 'front' : 'back';\n if (opts?.camera && opts.camera !== 'front' && opts.camera !== 'back') throw new Error(`aiLive: unknown camera \"${String(opts.camera)}\" — use \"front\" or \"back\"`);\n owner = who;\n lastOwner = who;\n visual = ctx.visual;\n allowed = typeof ctx.allowed === 'function' ? ctx.allowed : () => false;\n gen++; seq = 0; owedGaps = 0; lastFrameId = null; busy = false; gate = null;\n const g = gen;\n const cameraOfScreen = ctx.visual;\n snap = { ...idle(), camera: cam };\n set({ state: 'starting' });\n // Starting takes several awaits, and the screen may be stopped, or the yes taken back,\n // during any of them (Codex P1 on #53). After each: a stop already tore it down, so say why\n // and go no further; a yes taken back unannounced tears it down here.\n const stillStarting = async () => {\n if (g === gen && !allowed()) await teardown('stopped', CONSENT_TAKEN_BACK);\n if (g !== gen) throw Object.assign(new Error(stopReason || 'aiLive: stopped before it started sending.'), { code: 'STOPPED' });\n };\n try {\n await stillStarting();\n // One round trip: the connection starts the session (or Core refuses, in its own words), and\n // says how to send. Before the camera, so a refusal never turns the camera on.\n camera = DEFAULT_CAMERA;\n // Checked again with nothing in between: a stop that landed while the check above resolved has\n // already torn this start down, and opening now would start a session nobody closes (Codex on #53).\n if (g !== gen) throw Object.assign(new Error(stopReason || 'aiLive: stopped before it started sending.'), { code: 'STOPPED' });\n await openSocket();\n await stillStarting();\n await cameraOfScreen.start({ intent: 'stream', facing: cam } as any);\n if (g !== gen) { try { await cameraOfScreen.stop?.(); } catch { /* released by the gate too */ } }\n await stillStarting();\n // Again with nothing in between: a stop that landed while the last check resolved must not see\n // pictures start or the state flip back to \"sending\" (Codex on #53).\n if (g !== gen) throw Object.assign(new Error(stopReason || 'aiLive: stopped before it started sending.'), { code: 'STOPPED' });\n loop = deps.setInterval(() => { void slot(); }, Math.max(200, Math.round(1000 / camera.fps)));\n set({ state: 'sending' });\n return snap;\n } catch (e: any) {\n if (g !== gen) {\n // Already torn down by a stop: report why it stopped, not the closed socket.\n throw e?.code === 'STOPPED' ? e : Object.assign(new Error(stopReason || 'aiLive: stopped before it started sending.'), { code: 'STOPPED' });\n }\n await teardown('error', String(e?.message || e));\n throw e;\n }\n },\n async stop(who: string, why?: string) {\n const waiting = waitingToStart.get(who);\n if (waiting) waiting.cancelled = true;\n if (owner !== who) return;\n await teardown('stopped', why ?? null);\n },\n getSnapshot: (who: string) => (lastOwner === who ? snap : idle()),\n onEpisode: (cb, who) => add(episodeCbs, who, cb),\n subscribe: (cb, who) => add(snapCbs, who, cb),\n forget(who: string) {\n episodeCbs.delete(who);\n snapCbs.delete(who);\n if (lastOwner === who && owner === null) { lastOwner = null; snap = idle(); }\n },\n isSending: () => snap.state === 'starting' || snap.state === 'sending',\n subscribeSending(cb) {\n sendingCbs.add(cb);\n return { unsubscribe: () => { sendingCbs.delete(cb); } } as Subscription;\n },\n };\n}\n","/**\n * The wire protocol of Core's binary stream endpoint (/ws/stream), for the AI live sender.\n *\n * A PORT of Core's `packages/media-stream/src/index.js` (@ateam-ai/media-stream, not on npm\n * yet), limited to what a sender needs. The frame bytes are pinned by\n * tests/aiLiveStreamProtocol.test.mjs against the layout that file documents, so a drift is\n * a red test, not a silent mismatch. DEAD — replace with an import of @ateam-ai/media-stream\n * once Core publishes it.\n *\n * Frame envelope (bytes): 0 version (1) · 1 flags · 2-5 seq uint32 BE (one per sampling slot\n * of the SENDER, taken or skipped) · 6-13 tCapture float64 BE (ms since the epoch) · 14- payload.\n */\nexport const STREAM_PATH = '/ws/stream';\nexport const SUBPROTOCOL = 'adas.v1';\nexport const HEADER_BYTES = 14;\nconst ENVELOPE_VERSION = 1;\n\nexport const CLOSE = Object.freeze({\n AUTH_MISSING: 4001,\n AUTH_FAILED: 4002,\n NO_SUCH_WORKSPACE: 4003,\n NOT_ENTITLED: 4004,\n BAD_REQUEST: 4400,\n UNKNOWN_KIND: 4404,\n TIMEOUT: 4408,\n OVER_LIMIT: 4429,\n TOO_BIG: 1009,\n CONSUMER_FAILING: 1011,\n});\n\nexport function encodeFrame({ seq, tCapture, payload }: { seq: number; tCapture: number; payload: Uint8Array }): Uint8Array {\n const out = new Uint8Array(HEADER_BYTES + payload.length);\n const v = new DataView(out.buffer);\n v.setUint8(0, ENVELOPE_VERSION);\n v.setUint8(1, 0);\n v.setUint32(2, seq >>> 0, false);\n v.setFloat64(6, tCapture, false);\n out.set(payload, HEADER_BYTES);\n return out;\n}\n\nexport function openMessage(kind: string, params?: Record<string, unknown>): string {\n return JSON.stringify(params ? { type: 'open', kind, params } : { type: 'open', kind });\n}\n\nexport const CLOSE_MESSAGE = JSON.stringify({ type: 'close' });\n\n/** A server text frame → { event, data }, or null for anything that is not an event. */\nexport function parseServerEvent(text: unknown): { event: string; data: any } | null {\n if (typeof text !== 'string') return null;\n try {\n const m = JSON.parse(text);\n return m && typeof m.event === 'string' ? { event: m.event, data: m.data } : null;\n } catch {\n return null;\n }\n}\n\nconst WHY: Record<number, string> = {\n [CLOSE.AUTH_MISSING]: 'The login or the workspace was missing. Sign in again and try once more.',\n [CLOSE.AUTH_FAILED]: 'The login was refused. Sign in again and try once more.',\n [CLOSE.NO_SUCH_WORKSPACE]: 'This workspace was not found.',\n [CLOSE.NOT_ENTITLED]: 'This workspace may not stream right now.',\n [CLOSE.TIMEOUT]: 'The connection to the AI timed out.',\n [CLOSE.OVER_LIMIT]: 'Too many cameras are sending to this workspace. Stop another one first.',\n [CLOSE.TOO_BIG]: 'A picture was too big to send.',\n [CLOSE.CONSUMER_FAILING]: 'The AI stopped taking pictures. Check the live session in AI Models.',\n};\n\n/** A close as one plain sentence: the server's own words for a refused open. */\nexport function describeClose(code: number, reason?: string): string {\n if (code === CLOSE.BAD_REQUEST && reason) return reason;\n return WHY[code] || (reason ? reason : `The connection to the AI closed (${code}).`);\n}\n\nconst B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';\nconst B64_INDEX = (() => { const t = new Int16Array(256).fill(-1); for (let i = 0; i < 64; i++) t[B64.charCodeAt(i)] = i; return t; })();\n\n/** Base64 (the native module's JPEG) → bytes. Ignores padding and line breaks. */\nexport function base64ToBytes(b64: string): Uint8Array {\n const clean = b64.replace(/[^A-Za-z0-9+/]/g, '');\n const out = new Uint8Array(Math.floor((clean.length * 3) / 4));\n let o = 0, acc = 0, bits = 0;\n for (let i = 0; i < clean.length; i++) {\n acc = (acc << 6) | B64_INDEX[clean.charCodeAt(i)];\n bits += 6;\n if (bits >= 8) { bits -= 8; out[o++] = (acc >> bits) & 0xff; }\n }\n return o === out.length ? out : out.subarray(0, o);\n}\n","/**\n * The motion gate (owner 2026-11-01): the phone decides, for free, whether anything on the camera changed, and only\n * then do pictures go to the AI. The SAME RULE as the computer's camera window and the engine's test replay\n * (ai-dev-assistant connectors/ai-models-mcp/ui-dist/ai-models/motionGate.js), held to it by the same test cases\n * (tests/aiLiveMotionGate.test.mjs is a copy of that repo's tests/motionGate.test.js). Change one, change both.\n *\n * Pure: no clock, no camera. Each sampled picture comes with its capture time and its SIGNATURE: a tiny grey\n * thumbnail (SIG_W × SIG_H bytes, 0..255, signature.ts), and the gate says which pictures to send now.\n * quiet nothing changed: the picture is held (pre-roll), not sent; except one every heartbeatSec, so the AI\n * still LOOKS now and then (GPT review 2026-11-01: someone lying still must still be seen, and an\n * episode stays observed; a quiet stretch never means \"it ended\").\n * change the picture differs from the previous one by more than the sensitivity: the held pictures of the last\n * preRollSec go first, oldest first, then this one.\n * after pictures keep going for postRollSec after the last change.\n * The settings come from Core ({\"event\":\"motion\"}), set on the Live tab of the AI Models page.\n */\nexport const SIG_W = 16;\nexport const SIG_H = 12;\n// A camera's pictures are not exactly 1 s apart (timers drift by milliseconds): \"1 s before / after\" allows this much\n// more, so a picture taken 1.003 s before a change still counts as the second before it (seen live on dev, 2026-11-01).\nexport const SLACK_SEC = 0.15;\nexport const SENSITIVITY = Object.freeze({ low: 0.06, medium: 0.03, high: 0.015 });\nexport type Sensitivity = keyof typeof SENSITIVITY;\nexport interface MotionGateConfig { enabled: boolean; sensitivity: Sensitivity; preRollSec: number; postRollSec: number; heartbeatSec: number }\nexport const GATE_DEFAULTS: Readonly<MotionGateConfig> = Object.freeze({ enabled: true, sensitivity: 'medium', preRollSec: 1, postRollSec: 1, heartbeatSec: 8 });\nexport type GateReason = 'first' | 'change' | 'after' | 'heartbeat' | 'quiet' | 'off';\nexport interface GateCounters { offered: number; sent: number; held: number; heartbeats: number; changes: number }\n\n/** 0..1: how different two signatures are (mean absolute difference). A missing one counts as fully different. */\nexport function difference(a: ArrayLike<number> | null | undefined, b: ArrayLike<number> | null | undefined): number {\n if (!a || !b || a.length !== b.length || !a.length) return 1;\n let sum = 0;\n for (let i = 0; i < a.length; i++) sum += Math.abs(a[i] - b[i]);\n return sum / (a.length * 255);\n}\n\n/** Only the fields Core may set, each checked; anything else keeps the default. */\nexport function gateConfigFrom(d: any): MotionGateConfig {\n const c: MotionGateConfig = { ...GATE_DEFAULTS };\n if (!d || typeof d !== 'object') return c;\n if (typeof d.enabled === 'boolean') c.enabled = d.enabled;\n if (typeof d.sensitivity === 'string' && Object.prototype.hasOwnProperty.call(SENSITIVITY, d.sensitivity)) c.sensitivity = d.sensitivity as Sensitivity;\n for (const k of ['preRollSec', 'postRollSec', 'heartbeatSec'] as const) {\n const v = Number(d[k]);\n if (Number.isFinite(v) && v >= 0 && v <= 60) c[k] = v;\n }\n return c;\n}\n\nexport function createMotionGate<T>(cfg: Partial<MotionGateConfig> = {}) {\n const c: MotionGateConfig = { ...GATE_DEFAULTS, ...cfg };\n const threshold = SENSITIVITY[c.sensitivity] ?? SENSITIVITY.medium;\n let prevSig: ArrayLike<number> | null = null;\n let lastChangeT = -Infinity;\n let lastSentT = -Infinity;\n let held: { t: number; item: T }[] = [];\n const n: GateCounters = { offered: 0, sent: 0, held: 0, heartbeats: 0, changes: 0 };\n\n const sendNow = (t: number, item: T, reason: GateReason, withPreRoll = false) => {\n const out = withPreRoll ? held.filter((h) => t - h.t <= c.preRollSec + SLACK_SEC).map((h) => h.item) : [];\n held = [];\n out.push(item);\n lastSentT = t;\n n.sent += out.length;\n return { send: out, reason };\n };\n\n return {\n offer({ t, sig, item }: { t: number; sig: ArrayLike<number> | null; item: T }): { send: T[]; reason: GateReason } {\n n.offered++;\n if (!c.enabled) { lastSentT = t; n.sent++; return { send: [item], reason: 'off' }; }\n const first = prevSig == null;\n const d = difference(prevSig, sig);\n prevSig = sig;\n if (first) return sendNow(t, item, 'first');\n if (d > threshold) { lastChangeT = t; n.changes++; return sendNow(t, item, 'change', true); }\n if (t - lastChangeT <= c.postRollSec + SLACK_SEC) return sendNow(t, item, 'after');\n if (t - lastSentT >= c.heartbeatSec) { n.heartbeats++; return sendNow(t, item, 'heartbeat'); }\n held.push({ t, item });\n while (held.length && t - held[0].t > c.preRollSec + 1) held.shift();\n n.held++;\n return { send: [], reason: 'quiet' };\n },\n counters: (): GateCounters => ({ ...n }),\n config: (): MotionGateConfig => ({ ...c }),\n };\n}\n","/**\n * A camera frame's SIGNATURE for the motion gate: a SIG_W × SIG_H grey thumbnail (0..255), the same measure the\n * computer's camera window takes from a tiny canvas. The native module gives JS no pixels, only JPEGs (jpegById),\n * so the phone asks native for a very small JPEG of the frame and decodes it here (jpeg-js, pure JS, no new app\n * build). Each cell is the mean luma (Rec. 601) of the pixels that fall in it.\n */\nimport { decode } from 'jpeg-js';\nimport { base64ToBytes } from './streamProtocol';\nimport { SIG_H, SIG_W } from './motionGate';\n\n/** The longer side of the JPEG asked of native for a signature: small enough to be cheap, big enough to average. */\nexport const SIG_JPEG_SIDE = 64;\n\nexport function signatureFromJpeg(b64: string): Uint8Array {\n const img = decode(base64ToBytes(b64), { useTArray: true, formatAsRGBA: true, maxResolutionInMP: 1 });\n const { width: w, height: h, data } = img;\n if (!w || !h) throw new Error('empty picture');\n const sum = new Float64Array(SIG_W * SIG_H);\n const cnt = new Uint32Array(SIG_W * SIG_H);\n for (let y = 0; y < h; y++) {\n const cy = Math.min(SIG_H - 1, Math.floor((y * SIG_H) / h));\n for (let x = 0; x < w; x++) {\n const cx = Math.min(SIG_W - 1, Math.floor((x * SIG_W) / w));\n const i = (y * w + x) * 4;\n const k = cy * SIG_W + cx;\n sum[k] += 0.299 * data[i] + 0.587 * data[i + 1] + 0.114 * data[i + 2];\n cnt[k]++;\n }\n }\n const out = new Uint8Array(SIG_W * SIG_H);\n for (let k = 0; k < out.length; k++) out[k] = cnt[k] ? Math.round(sum[k] / cnt[k]) : 0;\n return out;\n}\n"],"mappings":";;;;;AAiCA,SAAS,cAAc,WAAW,gBAAgB;AAClD,SAAS,0BAA0B;;;ACtB5B,IAAM,cAAc;AACpB,IAAM,cAAc;AACpB,IAAM,eAAe;AAC5B,IAAM,mBAAmB;AAElB,IAAM,QAAQ,OAAO,OAAO;AAAA,EACjC,cAAc;AAAA,EACd,aAAa;AAAA,EACb,mBAAmB;AAAA,EACnB,cAAc;AAAA,EACd,aAAa;AAAA,EACb,cAAc;AAAA,EACd,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,SAAS;AAAA,EACT,kBAAkB;AACpB,CAAC;AAEM,SAAS,YAAY,EAAE,KAAK,UAAU,QAAQ,GAAuE;AAC1H,QAAM,MAAM,IAAI,WAAW,eAAe,QAAQ,MAAM;AACxD,QAAM,IAAI,IAAI,SAAS,IAAI,MAAM;AACjC,IAAE,SAAS,GAAG,gBAAgB;AAC9B,IAAE,SAAS,GAAG,CAAC;AACf,IAAE,UAAU,GAAG,QAAQ,GAAG,KAAK;AAC/B,IAAE,WAAW,GAAG,UAAU,KAAK;AAC/B,MAAI,IAAI,SAAS,YAAY;AAC7B,SAAO;AACT;AAEO,SAAS,YAAY,MAAc,QAA0C;AAClF,SAAO,KAAK,UAAU,SAAS,EAAE,MAAM,QAAQ,MAAM,OAAO,IAAI,EAAE,MAAM,QAAQ,KAAK,CAAC;AACxF;AAEO,IAAM,gBAAgB,KAAK,UAAU,EAAE,MAAM,QAAQ,CAAC;AAGtD,SAAS,iBAAiB,MAAoD;AACnF,MAAI,OAAO,SAAS,SAAU,QAAO;AACrC,MAAI;AACF,UAAM,IAAI,KAAK,MAAM,IA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IACA,MAAM,KAAK,KAAa,KAAc;AACpC,YAAM,UAAU,eAAe,IAAI,GAAG;AACtC,UAAI,QAAS,SAAQ,YAAY;AACjC,UAAI,UAAU,IAAK;AACnB,YAAM,SAAS,WAAW,OAAO,IAAI;AAAA,IACvC;AAAA,IACA,aAAa,CAAC,QAAiB,cAAc,MAAM,OAAO,KAAK;AAAA,IAC/D,WAAW,CAAC,IAAI,QAAQ,IAAI,YAAY,KAAK,EAAE;AAAA,IAC/C,WAAW,CAAC,IAAI,QAAQ,IAAI,SAAS,KAAK,EAAE;AAAA,IAC5C,OAAO,KAAa;AAClB,iBAAW,OAAO,GAAG;AACrB,cAAQ,OAAO,GAAG;AAClB,UAAI,cAAc,OAAO,UAAU,MAAM;AAAE,oBAAY;AAAM,eAAO,KAAK;AAAA,MAAG;AAAA,IAC9E;AAAA,IACA,WAAW,MAAM,KAAK,UAAU,cAAc,KAAK,UAAU;AAAA,IAC7D,iBAAiB,IAAI;AACnB,iBAAW,IAAI,EAAE;AACjB,aAAO,EAAE,aAAa,MAAM;AAAE,mBAAW,OAAO,EAAE;AAAA,MAAG,EAAE;AAAA,IACzD;AAAA,EACF;AACF;","names":["set"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ateam-ai-mobile/expo",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.24",
|
|
4
4
|
"description": "Expo-backed providers for the ADAS SDK \u2014 haptics, bundle disk cache, attachment cache, device bridge.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -9,9 +9,10 @@
|
|
|
9
9
|
"build": "tsup"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@ateam-ai-mobile/core": "0.1.
|
|
13
|
-
"@ateam-ai-mobile/plugin-runtime": "0.1.
|
|
14
|
-
"@ateam-ai-mobile/ui": "0.1.
|
|
12
|
+
"@ateam-ai-mobile/core": "0.1.24",
|
|
13
|
+
"@ateam-ai-mobile/plugin-runtime": "0.1.24",
|
|
14
|
+
"@ateam-ai-mobile/ui": "0.1.24",
|
|
15
|
+
"jpeg-js": "^0.4.4",
|
|
15
16
|
"zustand": "^5.0.0"
|
|
16
17
|
},
|
|
17
18
|
"peerDependencies": {
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The motion gate (owner 2026-11-01): the phone decides, for free, whether anything on the camera changed, and only
|
|
3
|
+
* then do pictures go to the AI. The SAME RULE as the computer's camera window and the engine's test replay
|
|
4
|
+
* (ai-dev-assistant connectors/ai-models-mcp/ui-dist/ai-models/motionGate.js), held to it by the same test cases
|
|
5
|
+
* (tests/aiLiveMotionGate.test.mjs is a copy of that repo's tests/motionGate.test.js). Change one, change both.
|
|
6
|
+
*
|
|
7
|
+
* Pure: no clock, no camera. Each sampled picture comes with its capture time and its SIGNATURE: a tiny grey
|
|
8
|
+
* thumbnail (SIG_W × SIG_H bytes, 0..255, signature.ts), and the gate says which pictures to send now.
|
|
9
|
+
* quiet nothing changed: the picture is held (pre-roll), not sent; except one every heartbeatSec, so the AI
|
|
10
|
+
* still LOOKS now and then (GPT review 2026-11-01: someone lying still must still be seen, and an
|
|
11
|
+
* episode stays observed; a quiet stretch never means "it ended").
|
|
12
|
+
* change the picture differs from the previous one by more than the sensitivity: the held pictures of the last
|
|
13
|
+
* preRollSec go first, oldest first, then this one.
|
|
14
|
+
* after pictures keep going for postRollSec after the last change.
|
|
15
|
+
* The settings come from Core ({"event":"motion"}), set on the Live tab of the AI Models page.
|
|
16
|
+
*/
|
|
17
|
+
export const SIG_W = 16;
|
|
18
|
+
export const SIG_H = 12;
|
|
19
|
+
// A camera's pictures are not exactly 1 s apart (timers drift by milliseconds): "1 s before / after" allows this much
|
|
20
|
+
// more, so a picture taken 1.003 s before a change still counts as the second before it (seen live on dev, 2026-11-01).
|
|
21
|
+
export const SLACK_SEC = 0.15;
|
|
22
|
+
export const SENSITIVITY = Object.freeze({ low: 0.06, medium: 0.03, high: 0.015 });
|
|
23
|
+
export type Sensitivity = keyof typeof SENSITIVITY;
|
|
24
|
+
export interface MotionGateConfig { enabled: boolean; sensitivity: Sensitivity; preRollSec: number; postRollSec: number; heartbeatSec: number }
|
|
25
|
+
export const GATE_DEFAULTS: Readonly<MotionGateConfig> = Object.freeze({ enabled: true, sensitivity: 'medium', preRollSec: 1, postRollSec: 1, heartbeatSec: 8 });
|
|
26
|
+
export type GateReason = 'first' | 'change' | 'after' | 'heartbeat' | 'quiet' | 'off';
|
|
27
|
+
export interface GateCounters { offered: number; sent: number; held: number; heartbeats: number; changes: number }
|
|
28
|
+
|
|
29
|
+
/** 0..1: how different two signatures are (mean absolute difference). A missing one counts as fully different. */
|
|
30
|
+
export function difference(a: ArrayLike<number> | null | undefined, b: ArrayLike<number> | null | undefined): number {
|
|
31
|
+
if (!a || !b || a.length !== b.length || !a.length) return 1;
|
|
32
|
+
let sum = 0;
|
|
33
|
+
for (let i = 0; i < a.length; i++) sum += Math.abs(a[i] - b[i]);
|
|
34
|
+
return sum / (a.length * 255);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Only the fields Core may set, each checked; anything else keeps the default. */
|
|
38
|
+
export function gateConfigFrom(d: any): MotionGateConfig {
|
|
39
|
+
const c: MotionGateConfig = { ...GATE_DEFAULTS };
|
|
40
|
+
if (!d || typeof d !== 'object') return c;
|
|
41
|
+
if (typeof d.enabled === 'boolean') c.enabled = d.enabled;
|
|
42
|
+
if (typeof d.sensitivity === 'string' && Object.prototype.hasOwnProperty.call(SENSITIVITY, d.sensitivity)) c.sensitivity = d.sensitivity as Sensitivity;
|
|
43
|
+
for (const k of ['preRollSec', 'postRollSec', 'heartbeatSec'] as const) {
|
|
44
|
+
const v = Number(d[k]);
|
|
45
|
+
if (Number.isFinite(v) && v >= 0 && v <= 60) c[k] = v;
|
|
46
|
+
}
|
|
47
|
+
return c;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function createMotionGate<T>(cfg: Partial<MotionGateConfig> = {}) {
|
|
51
|
+
const c: MotionGateConfig = { ...GATE_DEFAULTS, ...cfg };
|
|
52
|
+
const threshold = SENSITIVITY[c.sensitivity] ?? SENSITIVITY.medium;
|
|
53
|
+
let prevSig: ArrayLike<number> | null = null;
|
|
54
|
+
let lastChangeT = -Infinity;
|
|
55
|
+
let lastSentT = -Infinity;
|
|
56
|
+
let held: { t: number; item: T }[] = [];
|
|
57
|
+
const n: GateCounters = { offered: 0, sent: 0, held: 0, heartbeats: 0, changes: 0 };
|
|
58
|
+
|
|
59
|
+
const sendNow = (t: number, item: T, reason: GateReason, withPreRoll = false) => {
|
|
60
|
+
const out = withPreRoll ? held.filter((h) => t - h.t <= c.preRollSec + SLACK_SEC).map((h) => h.item) : [];
|
|
61
|
+
held = [];
|
|
62
|
+
out.push(item);
|
|
63
|
+
lastSentT = t;
|
|
64
|
+
n.sent += out.length;
|
|
65
|
+
return { send: out, reason };
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
offer({ t, sig, item }: { t: number; sig: ArrayLike<number> | null; item: T }): { send: T[]; reason: GateReason } {
|
|
70
|
+
n.offered++;
|
|
71
|
+
if (!c.enabled) { lastSentT = t; n.sent++; return { send: [item], reason: 'off' }; }
|
|
72
|
+
const first = prevSig == null;
|
|
73
|
+
const d = difference(prevSig, sig);
|
|
74
|
+
prevSig = sig;
|
|
75
|
+
if (first) return sendNow(t, item, 'first');
|
|
76
|
+
if (d > threshold) { lastChangeT = t; n.changes++; return sendNow(t, item, 'change', true); }
|
|
77
|
+
if (t - lastChangeT <= c.postRollSec + SLACK_SEC) return sendNow(t, item, 'after');
|
|
78
|
+
if (t - lastSentT >= c.heartbeatSec) { n.heartbeats++; return sendNow(t, item, 'heartbeat'); }
|
|
79
|
+
held.push({ t, item });
|
|
80
|
+
while (held.length && t - held[0].t > c.preRollSec + 1) held.shift();
|
|
81
|
+
n.held++;
|
|
82
|
+
return { send: [], reason: 'quiet' };
|
|
83
|
+
},
|
|
84
|
+
counters: (): GateCounters => ({ ...n }),
|
|
85
|
+
config: (): MotionGateConfig => ({ ...c }),
|
|
86
|
+
};
|
|
87
|
+
}
|
package/src/aiLive/provider.ts
CHANGED
|
@@ -21,6 +21,13 @@
|
|
|
21
21
|
* made by native (jpegById). A slot with no fresh frame, or while the socket is still busy,
|
|
22
22
|
* sends nothing and still takes its sequence number, so Core reports it as a gap;
|
|
23
23
|
* 4. {"event":"episode"} from Core → the screen's onEpisode callbacks.
|
|
24
|
+
* THE MOTION GATE (owner 2026-11-01, GPT review 2026-11-01): when Core sends {"event":"motion"} (the workspace's
|
|
25
|
+
* Live tab settings) before "opened", each slot first takes the frame's signature (a tiny JPEG decoded here,
|
|
26
|
+
* signature.ts) and the gate (motionGate.ts, the same rule as the computer's camera window) decides: while
|
|
27
|
+
* nothing changes the picture is held back, takes NO sequence number (it is not lost, Core must not count a
|
|
28
|
+
* gap) and costs nothing; one goes anyway every heartbeat so the AI still looks; on a change the held last
|
|
29
|
+
* second goes first, then the change, then one more second after it. Core that sends no "motion" gets every
|
|
30
|
+
* picture, as before.
|
|
24
31
|
* stop() closes the connection and the camera; Core ends the session when the connection ends,
|
|
25
32
|
* whatever ended it (a stop, the screen closing, the app killed, the network lost).
|
|
26
33
|
*/
|
|
@@ -29,6 +36,13 @@ import { CONSENT_TAKEN_BACK } from '@ateam-ai-mobile/plugin-runtime';
|
|
|
29
36
|
import type { AiLiveEpisode, AiLiveProvider, AiLiveSnapshot, AiLiveStartOptions, DeviceState, Subscription } from '@ateam-ai-mobile/plugin-runtime';
|
|
30
37
|
import { jpegById as nativeJpegById } from '../vision';
|
|
31
38
|
import { CLOSE_MESSAGE, STREAM_PATH, SUBPROTOCOL, base64ToBytes, describeClose, encodeFrame, openMessage, parseServerEvent } from './streamProtocol';
|
|
39
|
+
import { createMotionGate, gateConfigFrom } from './motionGate';
|
|
40
|
+
|
|
41
|
+
// The gate holds frame ids, not pictures: a held id must still resolve in the native ring when it is sent. The ring
|
|
42
|
+
// keeps 12 frames (AdaVisionModule.swift ringCap), 6 s at the fastest stream rate (500 ms), so a longer pre-roll from
|
|
43
|
+
// Core would send only its tail. 5 s stays inside it with margin.
|
|
44
|
+
export const PHONE_MAX_PRE_ROLL_SEC = 5;
|
|
45
|
+
import { SIG_JPEG_SIDE, signatureFromJpeg } from './signature';
|
|
32
46
|
|
|
33
47
|
export const LIVE_KIND = 'ai-models.live';
|
|
34
48
|
/** A frame older than this is not "what the camera sees now" and is not sent. */
|
|
@@ -45,6 +59,8 @@ export interface AiLiveDeps {
|
|
|
45
59
|
token(): string | null;
|
|
46
60
|
tenant(): string | null;
|
|
47
61
|
jpegById(id: string, maxSide: number, quality: number): Promise<string>;
|
|
62
|
+
/** The frame's signature for the motion gate (signature.ts); null = could not take it (the picture is then sent). */
|
|
63
|
+
signatureById(id: string): Promise<ArrayLike<number> | null>;
|
|
48
64
|
now(): number;
|
|
49
65
|
setInterval(fn: () => void, ms: number): any;
|
|
50
66
|
clearInterval(handle: any): void;
|
|
@@ -70,6 +86,7 @@ export const defaultAiLiveDeps = (): AiLiveDeps => ({
|
|
|
70
86
|
token: getToken,
|
|
71
87
|
tenant: getTenant,
|
|
72
88
|
jpegById: nativeJpegById,
|
|
89
|
+
signatureById: async (id) => signatureFromJpeg(await nativeJpegById(id, SIG_JPEG_SIDE, 0.6)),
|
|
73
90
|
now: Date.now,
|
|
74
91
|
setInterval: (fn, ms) => setInterval(fn, ms),
|
|
75
92
|
clearInterval: (h) => clearInterval(h),
|
|
@@ -77,7 +94,8 @@ export const defaultAiLiveDeps = (): AiLiveDeps => ({
|
|
|
77
94
|
clearTimeout: (h) => clearTimeout(h),
|
|
78
95
|
});
|
|
79
96
|
|
|
80
|
-
const idle = (): AiLiveSnapshot => ({ state: 'idle', camera: null, sent: 0, skipped: 0, lastEpisode: null, lastError: null });
|
|
97
|
+
const idle = (): AiLiveSnapshot => ({ state: 'idle', camera: null, sent: 0, skipped: 0, heldBack: 0, lastEpisode: null, lastError: null });
|
|
98
|
+
type GateItem = { id: string; t: number };
|
|
81
99
|
|
|
82
100
|
export function createAiLiveProvider(overrides: Partial<AiLiveDeps> = {}): AiLiveProvider {
|
|
83
101
|
const deps: AiLiveDeps = { ...defaultAiLiveDeps(), ...overrides };
|
|
@@ -100,11 +118,13 @@ export function createAiLiveProvider(overrides: Partial<AiLiveDeps> = {}): AiLiv
|
|
|
100
118
|
let closingOnPurpose = false;
|
|
101
119
|
let seq = 0;
|
|
102
120
|
let busy = false;
|
|
121
|
+
let owedGaps = 0; // slots skipped while one was still working: numbered when it ends
|
|
103
122
|
// Which session a slot belongs to. A slot awaits the camera and the JPEG maker; when the screen
|
|
104
123
|
// stops (or another starts) meanwhile, that slot's picture must go nowhere, not into the next one.
|
|
105
124
|
let gen = 0;
|
|
106
125
|
let lastFrameId: string | null = null;
|
|
107
126
|
let camera = DEFAULT_CAMERA;
|
|
127
|
+
let gate: ReturnType<typeof createMotionGate<GateItem>> | null = null; // this session's motion gate, if Core sent one
|
|
108
128
|
let lastOwner: string | null = null; // whose the current snapshot is (it outlives a stop)
|
|
109
129
|
// Each screen hears only its own session: callbacks are kept per holder.
|
|
110
130
|
const episodeCbs = new Map<string, Set<(ev: AiLiveEpisode) => void>>();
|
|
@@ -130,14 +150,19 @@ export function createAiLiveProvider(overrides: Partial<AiLiveDeps> = {}): AiLiv
|
|
|
130
150
|
}
|
|
131
151
|
|
|
132
152
|
async function slot() {
|
|
133
|
-
const mySeq = seq++; // every slot takes a number, sent or not
|
|
134
153
|
const g = gen;
|
|
135
154
|
const sock = ws;
|
|
136
155
|
const current = () => g === gen && ws === sock && sock?.readyState === 1;
|
|
137
156
|
const consentGone = () => { if (allowed()) return false; if (g === gen) void teardown('stopped', CONSENT_TAKEN_BACK); return true; };
|
|
157
|
+
// Every slot that does not send takes a number, so Core sees the gap; a picture the motion gate holds back
|
|
158
|
+
// takes none (it was looked at, nothing changed: not lost).
|
|
159
|
+
const gap = (lastError?: string) => { seq++; set(lastError ? { skipped: snap.skipped + 1, lastError } : { skipped: snap.skipped + 1 }); };
|
|
138
160
|
if (consentGone()) return;
|
|
139
|
-
|
|
140
|
-
|
|
161
|
+
// A slot that finds the previous one still working is a gap AFTER that one's pictures: its number is taken
|
|
162
|
+
// when the running slot finishes, so numbers stay in capture order even when encoding is slower than the timer.
|
|
163
|
+
if (busy && current()) { owedGaps++; set({ skipped: snap.skipped + 1 }); return; }
|
|
164
|
+
if (busy || !current()) { gap(); return; }
|
|
165
|
+
if ((sock.bufferedAmount || 0) > MAX_BUFFERED) { gap(); return; }
|
|
141
166
|
busy = true;
|
|
142
167
|
try {
|
|
143
168
|
const v = await visual!.getSnapshot();
|
|
@@ -145,17 +170,32 @@ export function createAiLiveProvider(overrides: Partial<AiLiveDeps> = {}): AiLiv
|
|
|
145
170
|
const f: any = v?.latest;
|
|
146
171
|
const id = f?.handle?.id;
|
|
147
172
|
const t = Number(f?.timestamp);
|
|
148
|
-
if (!id || id === lastFrameId || !(deps.now() - t < FRESH_MS)) {
|
|
149
|
-
const b64 = await deps.jpegById(id, camera.maxPx, 0.7);
|
|
150
|
-
if (!current() || consentGone()) return;
|
|
151
|
-
const frame = encodeFrame({ seq: mySeq, tCapture: t, payload: base64ToBytes(b64) });
|
|
152
|
-
sock.send(frame.buffer);
|
|
173
|
+
if (!id || id === lastFrameId || !(deps.now() - t < FRESH_MS)) { gap(); return; }
|
|
153
174
|
lastFrameId = id;
|
|
154
|
-
|
|
175
|
+
let toSend: GateItem[] = [{ id, t }];
|
|
176
|
+
const myGate = gate;
|
|
177
|
+
if (myGate) {
|
|
178
|
+
let sig: ArrayLike<number> | null = null;
|
|
179
|
+
try { sig = await deps.signatureById(id); } catch { sig = null; } // no signature: counts as a change, sent
|
|
180
|
+
if (!current() || consentGone()) return;
|
|
181
|
+
toSend = myGate.offer({ t: t / 1000, sig, item: { id, t } }).send;
|
|
182
|
+
if (!toSend.length) { set({ heldBack: (snap.heldBack ?? 0) + 1 }); return; }
|
|
183
|
+
}
|
|
184
|
+
// Oldest first: the held last second (on a change), then this picture.
|
|
185
|
+
for (const it of toSend) {
|
|
186
|
+
if (!current() || consentGone()) return;
|
|
187
|
+
if ((sock.bufferedAmount || 0) > MAX_BUFFERED) { gap(); continue; }
|
|
188
|
+
let b64: string;
|
|
189
|
+
try { b64 = await deps.jpegById(it.id, camera.maxPx, 0.7); } catch (e: any) { if (g === gen) gap(String(e?.message || e)); continue; }
|
|
190
|
+
if (!current() || consentGone()) return;
|
|
191
|
+
const frame = encodeFrame({ seq: seq++, tCapture: it.t, payload: base64ToBytes(b64) });
|
|
192
|
+
sock.send(frame.buffer);
|
|
193
|
+
set({ sent: snap.sent + 1 });
|
|
194
|
+
}
|
|
155
195
|
} catch (e: any) {
|
|
156
|
-
if (g === gen)
|
|
196
|
+
if (g === gen) gap(String(e?.message || e));
|
|
157
197
|
} finally {
|
|
158
|
-
if (g === gen) busy = false;
|
|
198
|
+
if (g === gen) { busy = false; seq += owedGaps; owedGaps = 0; }
|
|
159
199
|
}
|
|
160
200
|
}
|
|
161
201
|
|
|
@@ -191,6 +231,7 @@ export function createAiLiveProvider(overrides: Partial<AiLiveDeps> = {}): AiLiv
|
|
|
191
231
|
camera = c && Number(c.fps) > 0 && Number(c.maxPx) > 0 ? { fps: Number(c.fps), maxPx: Number(c.maxPx) } : DEFAULT_CAMERA;
|
|
192
232
|
return;
|
|
193
233
|
}
|
|
234
|
+
if (ev.event === 'motion' && !opened) { const c = gateConfigFrom(ev.data); gate = c.enabled ? createMotionGate<GateItem>({ ...c, preRollSec: Math.min(c.preRollSec, PHONE_MAX_PRE_ROLL_SEC) }) : null; return; }
|
|
194
235
|
if (ev.event === 'opened' && !opened) { opened = true; deps.clearTimeout(gaveUp); resolve(); return; }
|
|
195
236
|
// Only a whole episode reaches the screen: what happened, of which class, and whose camera saw it
|
|
196
237
|
// (actorId: live sessions are per person). Anything less is dropped, never passed on half-made.
|
|
@@ -268,7 +309,7 @@ export function createAiLiveProvider(overrides: Partial<AiLiveDeps> = {}): AiLiv
|
|
|
268
309
|
lastOwner = who;
|
|
269
310
|
visual = ctx.visual;
|
|
270
311
|
allowed = typeof ctx.allowed === 'function' ? ctx.allowed : () => false;
|
|
271
|
-
gen++; seq = 0; lastFrameId = null; busy = false;
|
|
312
|
+
gen++; seq = 0; owedGaps = 0; lastFrameId = null; busy = false; gate = null;
|
|
272
313
|
const g = gen;
|
|
273
314
|
const cameraOfScreen = ctx.visual;
|
|
274
315
|
snap = { ...idle(), camera: cam };
|