@ateam-ai-mobile/expo 0.1.11 → 0.1.13
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/bootstrap.js +44 -27
- package/dist/bootstrap.js.map +1 -1
- package/dist/bootstrap.mjs +3 -3
- package/dist/{chunk-C7M5WAHA.mjs → chunk-42FP5DTO.mjs} +17 -5
- package/dist/chunk-42FP5DTO.mjs.map +1 -0
- package/dist/{chunk-PCAEUX2N.mjs → chunk-6JX222YX.mjs} +21 -10
- package/dist/chunk-6JX222YX.mjs.map +1 -0
- package/dist/{chunk-GF2MPTAB.mjs → chunk-DLDG77DX.mjs} +2 -2
- package/dist/device/createDeviceProvider.js +28 -23
- package/dist/device/createDeviceProvider.js.map +1 -1
- package/dist/device/createDeviceProvider.mjs +2 -2
- package/dist/index.d.mts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +16 -4
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/vision/index.d.mts +10 -3
- package/dist/vision/index.d.ts +10 -3
- package/dist/vision/index.js +20 -9
- package/dist/vision/index.js.map +1 -1
- package/dist/vision/index.mjs +1 -1
- package/ios/AdaVisionModule.swift +55 -13
- package/package.json +4 -4
- package/src/device-state/domains/visual.ts +7 -2
- package/src/device-state/visualEngine.ts +15 -2
- package/src/vision/index.ts +34 -10
- package/dist/chunk-C7M5WAHA.mjs.map +0 -1
- package/dist/chunk-PCAEUX2N.mjs.map +0 -1
- /package/dist/{chunk-GF2MPTAB.mjs.map → chunk-DLDG77DX.mjs.map} +0 -0
package/dist/bootstrap.mjs
CHANGED
|
@@ -4,11 +4,11 @@ import {
|
|
|
4
4
|
import {
|
|
5
5
|
createDeviceState,
|
|
6
6
|
setupExpoProviders
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-42FP5DTO.mjs";
|
|
8
8
|
import {
|
|
9
9
|
createDeviceProvider
|
|
10
|
-
} from "./chunk-
|
|
11
|
-
import "./chunk-
|
|
10
|
+
} from "./chunk-DLDG77DX.mjs";
|
|
11
|
+
import "./chunk-6JX222YX.mjs";
|
|
12
12
|
import "./chunk-6LXF3JVX.mjs";
|
|
13
13
|
import "./chunk-FF6ZZZXH.mjs";
|
|
14
14
|
import {
|
|
@@ -951,12 +951,20 @@ var VisualEngine = class {
|
|
|
951
951
|
this.context = p;
|
|
952
952
|
}
|
|
953
953
|
// ── session ──
|
|
954
|
-
startSession(intent) {
|
|
955
|
-
if (this.session)
|
|
954
|
+
startSession(intent, facing = "back") {
|
|
955
|
+
if (this.session) {
|
|
956
|
+
if (this.session.facing === facing) return this.session;
|
|
957
|
+
this.session = { ...this.session, facing };
|
|
958
|
+
this.frames = [];
|
|
959
|
+
this.cameraReady = false;
|
|
960
|
+
this.notify();
|
|
961
|
+
return this.session;
|
|
962
|
+
}
|
|
956
963
|
this.session = {
|
|
957
964
|
id: `visual_${Date.now()}`,
|
|
958
965
|
intent: PROFILES[intent] ? intent : "navigation",
|
|
959
966
|
startedAt: Date.now(),
|
|
967
|
+
facing,
|
|
960
968
|
suspended: false
|
|
961
969
|
};
|
|
962
970
|
this.notify();
|
|
@@ -1144,7 +1152,7 @@ function cameraState() {
|
|
|
1144
1152
|
// would let a guide speak about a scene nothing is watching.
|
|
1145
1153
|
available: !!session,
|
|
1146
1154
|
active: visualEngine.isCameraReady(),
|
|
1147
|
-
facing: session ?
|
|
1155
|
+
facing: session ? session.facing : "unknown"
|
|
1148
1156
|
};
|
|
1149
1157
|
}
|
|
1150
1158
|
function createVisualDomain() {
|
|
@@ -1162,7 +1170,11 @@ function createVisualDomain() {
|
|
|
1162
1170
|
};
|
|
1163
1171
|
},
|
|
1164
1172
|
async start(options) {
|
|
1165
|
-
const
|
|
1173
|
+
const facing = options?.facing ?? "back";
|
|
1174
|
+
if (facing !== "front" && facing !== "back") {
|
|
1175
|
+
throw unsupported(`visual.start facing "${String(facing)}" \u2014 use "front" or "back"`);
|
|
1176
|
+
}
|
|
1177
|
+
const s = visualEngine.startSession(options?.intent || "navigation", facing);
|
|
1166
1178
|
visualEngine.setContextProvider({ heading: currentHeading });
|
|
1167
1179
|
void startHeading("visual");
|
|
1168
1180
|
return { id: s.id, intent: s.intent, startedAt: s.startedAt };
|
|
@@ -1795,4 +1807,4 @@ export {
|
|
|
1795
1807
|
parseWalkJsonl,
|
|
1796
1808
|
setupExpoProviders
|
|
1797
1809
|
};
|
|
1798
|
-
//# sourceMappingURL=chunk-
|
|
1810
|
+
//# sourceMappingURL=chunk-42FP5DTO.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/haptics.ts","../src/bundleStorage.ts","../src/attachmentCache.ts","../src/device-state/domains/deviceContext.ts","../src/device-state/quality.ts","../src/device-state/events.ts","../src/device-state/domains/connectivity.ts","../src/device-state/domains/capability.ts","../src/device-state/domains/interaction.ts","../src/device-state/headingSource.ts","../src/device-state/turnDetector.ts","../src/device-state/spatialEvents.ts","../src/device-state/domains/spatial.ts","../src/device-state/visualEngine.ts","../src/device-state/domains/visual.ts","../src/device-state/activityEngine.ts","../src/device-state/activityClassifier.ts","../src/device-state/domains/activity.ts","../src/device-state/index.ts","../src/camera/CameraTaskHost.tsx","../src/device-state/replay.ts"],"sourcesContent":["/**\n * @ateam-ai-mobile/expo — Expo-backed providers for the ADAS SDK.\n *\n * Bare-RN hosts skip this package entirely (core + plugin-runtime have safe\n * defaults); Expo hosts call setupExpoProviders() once at startup to light\n * up haptics, persistent bundle caching, attachment previews, and the\n * expo modules remote bundles may require.\n */\nimport * as ExpoWebBrowser from 'expo-web-browser';\nimport * as ExpoLinking from 'expo-linking';\nimport * as ExpoClipboard from 'expo-clipboard';\nimport AsyncStorage from '@react-native-async-storage/async-storage';\nimport { setAcsCacheStorage } from '@ateam-ai-mobile/core';\nimport {\n setHapticsProvider,\n setBundleStorage,\n registerBundleModules,\n setPluginNativeProvider,\n type PluginNative,\n} from '@ateam-ai-mobile/plugin-runtime';\nimport { setClipboardProvider } from '@ateam-ai-mobile/ui';\n\nimport { expoHaptics } from './haptics';\nimport { ExpoBundleStorage } from './bundleStorage';\nimport { ExpoAttachmentCache } from './attachmentCache';\n\nexport { expoHaptics } from './haptics';\nexport { ExpoBundleStorage } from './bundleStorage';\nexport { ExpoAttachmentCache } from './attachmentCache';\n// NOTE: createBridge (device capabilities bridge) is intentionally NOT\n// exported here — it statically imports the optional expo modules\n// (calendar/contacts/location/…), which would break Metro builds in hosts\n// that don't install them. Install those modules and import it from the\n// subpath: `import { createBridge } from '@ateam-ai-mobile/expo/bridge'`.\n\n/**\n * Wire all Expo providers in one call. Runs before initAdas() is fine —\n * these registries are independent of the core config.\n *\n * Returns the attachment cache so the host can pass it to initAdas():\n * const { attachmentCache } = setupExpoProviders();\n * initAdas({ ..., attachmentCache });\n */\nexport function setupExpoProviders(opts?: {\n /** Extra modules remote bundles may require(), e.g. react-native-webview.\n * Static imports must live in the HOST (it owns the native build). */\n bundleModules?: Record<string, any>;\n /** AsyncStorage key overrides — pass your pre-SDK keys when migrating an\n * existing app so users keep their attachment previews / bundle cache. */\n attachmentIndexKey?: string;\n bundleIndexKey?: string;\n /**\n * The device-primitive implementation plugins receive as `props.device`.\n * Build it with `createDeviceProvider(...)` from `@ateam-ai-mobile/expo/device`\n * and pass it here so the SDK owns the implementation and this call registers\n * it (setPluginNativeProvider). It is injected — not imported by this entry —\n * because createDeviceProvider statically pulls optional expo modules that a\n * host without them cannot build (same reason createBridge is a subpath).\n */\n deviceProvider?: PluginNative;\n}): { attachmentCache: ExpoAttachmentCache } {\n setHapticsProvider(expoHaptics);\n if (opts?.deviceProvider) setPluginNativeProvider(opts.deviceProvider);\n setBundleStorage(new ExpoBundleStorage({ indexKey: opts?.bundleIndexKey }));\n setClipboardProvider({\n setString: (text: string) => ExpoClipboard.setStringAsync(text).then(() => {}),\n });\n registerBundleModules({\n 'expo-web-browser': ExpoWebBrowser,\n 'expo-linking': ExpoLinking,\n ...(opts?.bundleModules || {}),\n });\n // Persist the ACS home-actions feed across cold starts.\n setAcsCacheStorage({\n getItem: (k: string) => AsyncStorage.getItem(k),\n setItem: (k: string, v: string) => AsyncStorage.setItem(k, v),\n });\n return { attachmentCache: new ExpoAttachmentCache({ indexKey: opts?.attachmentIndexKey }) };\n}\n\n// Device Bridge v2 — the semantic state engines this build provides.\n// The host passes this to setDeviceStateProvider(); per-plugin gating by\n// manifest.stateDomains happens in the SDK.\nexport { createDeviceState, IMPLEMENTED_DOMAINS } from './device-state';\nexport type { DeviceStateCollectors } from './device-state';\n// The mounted half of the Visual engine. The app renders this ONCE; it draws\n// nothing until a visual session is open. Required for deviceState.visual to\n// produce frames at all — expo-camera has no headless capture.\n// The rolling frame timeline. Exposed so the host's native.vision layer can\n// resolve a frame id to its on-device file (frameById) and write a measured\n// quality back onto the frame (setFrameQuality) — see visualEngine.ts.\nexport { visualEngine } from './device-state/visualEngine';\nexport type { VisualFrame } from './device-state/visualEngine';\n// The mounted camera surface behind scanner.scanOnce() and\n// camera.recordVideo(). Mount ONCE near the app root; it renders nothing\n// until a plugin asks for a scan or a recording. Without it both APIs\n// reject immediately with UNAVAILABLE — deliberately, since a plugin must\n// never await a promise nothing can settle.\nexport { CameraTaskHost } from './camera/CameraTaskHost';\nexport { cameraTasks } from './camera/cameraTasks';\n// Replay — run guidance logic against a RECORDED WALK instead of a phone.\n// See docs/SPEC_device_walk_recorder.md for the fixture format.\nexport { createReplayDeviceState, parseWalkJsonl } from './device-state/replay';\nexport type { WalkFixture, WalkSample, WalkRefusal, ReplayClock, ReplaySession } from './device-state/replay';\n","/**\n * expo-haptics implementation of the plugin HapticsAPI.\n */\nimport * as Haptics from 'expo-haptics';\nimport type { HapticsAPI } from '@ateam-ai-mobile/plugin-runtime';\n\nexport const expoHaptics: HapticsAPI = {\n impact(style) {\n const map = {\n light: Haptics.ImpactFeedbackStyle.Light,\n medium: Haptics.ImpactFeedbackStyle.Medium,\n heavy: Haptics.ImpactFeedbackStyle.Heavy,\n } as const;\n Haptics.impactAsync(map[style]).catch(() => {});\n },\n notification(type) {\n const map = {\n success: Haptics.NotificationFeedbackType.Success,\n warning: Haptics.NotificationFeedbackType.Warning,\n error: Haptics.NotificationFeedbackType.Error,\n } as const;\n Haptics.notificationAsync(map[type]).catch(() => {});\n },\n selection() {\n Haptics.selectionAsync().catch(() => {});\n },\n // Shorthand for notification('error') — the form plugins actually call.\n error() {\n Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error).catch(() => {});\n },\n};\n","/**\n * Disk-backed BundleStorage: bundle files in the Expo cache directory,\n * index in AsyncStorage. Mirrors the reference app's cache semantics\n * (index version key, files under plugin-bundles/).\n */\nimport * as FileSystem from 'expo-file-system/legacy';\nimport AsyncStorage from '@react-native-async-storage/async-storage';\nimport type { BundleStorage, BundleCacheEntry } from '@ateam-ai-mobile/plugin-runtime';\n\nconst CACHE_DIR = `${FileSystem.cacheDirectory}plugin-bundles/`;\nconst DEFAULT_CACHE_INDEX_KEY = 'adas-plugin-bundles-index-v1';\n\nexport class ExpoBundleStorage implements BundleStorage {\n private dirReady = false;\n private indexKey: string;\n\n /** Pass `indexKey` when migrating from an app that already persisted a\n * bundle index under a different key — keeps the existing cache warm. */\n constructor(opts?: { indexKey?: string }) {\n this.indexKey = opts?.indexKey || DEFAULT_CACHE_INDEX_KEY;\n }\n\n private async ensureDir(): Promise<void> {\n if (this.dirReady) return;\n try {\n const info = await FileSystem.getInfoAsync(CACHE_DIR);\n if (!info.exists) {\n await FileSystem.makeDirectoryAsync(CACHE_DIR, { intermediates: true });\n }\n } catch (err) {\n console.warn('[ExpoBundleStorage] cache dir error:', err);\n }\n this.dirReady = true;\n }\n\n async loadIndex(): Promise<Record<string, BundleCacheEntry>> {\n try {\n const raw = await AsyncStorage.getItem(this.indexKey);\n return raw ? JSON.parse(raw) : {};\n } catch {\n return {};\n }\n }\n\n async saveIndex(index: Record<string, BundleCacheEntry>): Promise<void> {\n try {\n await AsyncStorage.setItem(this.indexKey, JSON.stringify(index));\n } catch (err) {\n console.warn('[ExpoBundleStorage] index save error:', err);\n }\n }\n\n async readBundle(localPath: string): Promise<string | null> {\n try {\n const info = await FileSystem.getInfoAsync(localPath);\n if (!info.exists) return null;\n return await FileSystem.readAsStringAsync(localPath);\n } catch {\n return null; // file gone — caller re-downloads\n }\n }\n\n async writeBundle(key: string, content: string): Promise<string> {\n await this.ensureDir();\n const localPath = `${CACHE_DIR}${key}`;\n await FileSystem.writeAsStringAsync(localPath, content);\n return localPath;\n }\n\n async deleteBundle(localPath: string): Promise<void> {\n await FileSystem.deleteAsync(localPath, { idempotent: true }).catch(() => {});\n }\n\n async clearAll(): Promise<void> {\n await FileSystem.deleteAsync(CACHE_DIR, { idempotent: true }).catch(() => {});\n await AsyncStorage.removeItem(this.indexKey).catch(() => {});\n this.dirReady = false;\n }\n}\n","/**\n * Expo implementation of @ateam-ai-mobile/core's AttachmentCache: attachment bytes on\n * disk, index in AsyncStorage. Mirrors the reference app's chat attachment\n * cache (last-100 retention).\n */\nimport * as FileSystem from 'expo-file-system/legacy';\nimport AsyncStorage from '@react-native-async-storage/async-storage';\nimport type { AttachmentCache, AttachmentPreview, OutgoingAttachment } from '@ateam-ai-mobile/core';\n\nconst DEFAULT_INDEX_KEY = 'adas-chat-attachment-index-v1';\nconst DIR = `${FileSystem.documentDirectory}chat-attachments/`;\nconst MAX_JOBS = 100;\n\ninterface CachedAttachment { mimeType: string; name: string; filePath: string }\n\nexport class ExpoAttachmentCache implements AttachmentCache {\n private index: Record<string, CachedAttachment[]> = {};\n private loaded: Promise<void>;\n private indexKey: string;\n\n /** Pass `indexKey` when migrating from an app that already persisted an\n * index under a different AsyncStorage key — otherwise existing users\n * lose every cached preview on upgrade. */\n constructor(opts?: { indexKey?: string }) {\n const indexKey = opts?.indexKey || DEFAULT_INDEX_KEY;\n this.indexKey = indexKey;\n this.loaded = (async () => {\n try {\n const info = await FileSystem.getInfoAsync(DIR);\n if (!info.exists) await FileSystem.makeDirectoryAsync(DIR, { intermediates: true });\n const raw = await AsyncStorage.getItem(indexKey);\n if (raw) this.index = JSON.parse(raw);\n } catch {}\n })();\n }\n\n async ready(): Promise<void> {\n await this.loaded;\n }\n\n async save(jobId: string, attachments: OutgoingAttachment[]): Promise<void> {\n await this.loaded;\n const entries: CachedAttachment[] = [];\n for (let i = 0; i < attachments.length; i++) {\n const att = attachments[i];\n const ext = att.mimeType.startsWith('image/') ? att.mimeType.split('/')[1] || 'jpg' : 'bin';\n const filePath = `${DIR}${jobId}_${i}.${ext}`;\n try {\n await FileSystem.writeAsStringAsync(filePath, att.data, { encoding: FileSystem.EncodingType.Base64 });\n entries.push({ mimeType: att.mimeType, name: att.name, filePath });\n } catch (err) {\n console.warn('[ExpoAttachmentCache] write failed:', err);\n }\n }\n if (entries.length === 0) return;\n\n this.index[jobId] = entries;\n // Keep only the most recent MAX_JOBS jobs; delete evicted files.\n const keys = Object.keys(this.index);\n if (keys.length > MAX_JOBS) {\n for (const k of keys.slice(0, keys.length - MAX_JOBS)) {\n const old = this.index[k];\n if (old) for (const o of old) FileSystem.deleteAsync(o.filePath, { idempotent: true }).catch(() => {});\n delete this.index[k];\n }\n }\n try {\n await AsyncStorage.setItem(this.indexKey, JSON.stringify(this.index));\n } catch {}\n }\n\n get(jobId: string): AttachmentPreview[] | undefined {\n const entries = this.index[jobId];\n if (!entries || entries.length === 0) return undefined;\n return entries.map(e => ({\n mimeType: e.mimeType,\n name: e.name,\n previewUri: e.filePath,\n }));\n }\n}\n","// ── deviceContext ──────────────────────────────────────────────────────────\n// power ✓ thermal ✗ (needs a native module) memory/storage ✗\nimport * as ExpoBattery from 'expo-battery';\nimport type { DeviceState, DeviceContextSnapshot } from '@ateam-ai-mobile/plugin-runtime';\nimport { qualityFrom, qualityUnknown } from '../quality';\nimport { stateEvent } from '../events';\n\nexport function createDeviceContextDomain(): DeviceState['deviceContext'] {\n return {\n async getSnapshot(): Promise<DeviceContextSnapshot> {\n const observedAt = Date.now();\n try {\n const [level, state, lowPower] = await Promise.all([\n ExpoBattery.getBatteryLevelAsync(),\n ExpoBattery.getBatteryStateAsync(),\n ExpoBattery.isLowPowerModeEnabledAsync(),\n ]);\n return {\n power: {\n // expo-battery returns -1 when it cannot tell. That is not a level.\n batteryLevel: typeof level === 'number' && level >= 0 ? level : null,\n charging:\n state === ExpoBattery.BatteryState.CHARGING ||\n state === ExpoBattery.BatteryState.FULL,\n lowPowerMode: lowPower,\n },\n // No Expo API for either. iOS needs ProcessInfo.thermalState, Android\n // PowerManager.getThermalStatus — a native module that does not exist.\n thermal: { state: 'unknown' },\n resources: { memoryPressure: 'unknown', storagePressure: 'unknown' },\n quality: qualityFrom(observedAt),\n };\n } catch {\n // The battery read failing is rare and uninteresting; reporting it as\n // 0% would not be.\n return {\n power: { batteryLevel: null, charging: null, lowPowerMode: null },\n thermal: { state: 'unknown' },\n resources: { memoryPressure: 'unknown', storagePressure: 'unknown' },\n quality: qualityUnknown(),\n };\n }\n },\n\n // Real events, not a placeholder: expo-battery exposes native listeners,\n // so a plugin that subscribes genuinely hears about a change rather than\n // sitting on a subscription that can never fire. Nudges without a payload:\n // no Device Context payload schema is decided, so the subscriber re-reads.\n subscribe(listener) {\n const subs = [\n ExpoBattery.addBatteryLevelListener(() => listener(stateEvent('deviceContext', 'batteryLevelChanged'))),\n ExpoBattery.addBatteryStateListener(() => listener(stateEvent('deviceContext', 'chargingChanged'))),\n ];\n return { unsubscribe: () => subs.forEach(x => x?.remove?.()) };\n },\n };\n}\n","/**\n * The honesty helpers — one place, shared by every domain module.\n *\n * A snapshot's `quality` says how much to trust it. R5 centralizes the three\n * ways a domain answers honestly here so no domain re-implements them:\n * - qualityFrom(observedAt) a live read, freshness stamped from its age.\n * - qualityUnknown() \"nothing was observed\" — never dressed as fresh.\n * - unsupported(what) an ACTION this build can't honour, refused by name.\n *\n * The `unknown` freshness shape is NOT re-declared here: it is the single\n * canonical `neverObserved()` from the contract package, so the engine, the gate\n * (deviceStateHost) and the types can never drift into three subtly different\n * \"unknown\"s.\n */\nimport { neverObserved } from '@ateam-ai-mobile/plugin-runtime';\nimport type { StateQuality } from '@ateam-ai-mobile/plugin-runtime';\n\n// ── Freshness ────────────────────────────────────────────────────────────\n//\n// Every snapshot carries when it was observed. `observedAt` is stamped at read\n// time by a live-reading domain; a caching domain passes its cache timestamp\n// instead. This is the one place the fresh/aging/stale thresholds live.\n\nexport const FRESH_MS = 10_000;\nexport const AGING_MS = 60_000;\n\nexport function qualityFrom(\n observedAt: number,\n confidence?: StateQuality['confidence'],\n): StateQuality {\n const ageMs = Date.now() - observedAt;\n return {\n freshness: {\n state: ageMs < FRESH_MS ? 'fresh' : ageMs < AGING_MS ? 'aging' : 'stale',\n observedAt,\n ageMs,\n },\n ...(confidence ? { confidence } : {}),\n };\n}\n\n/** Age genuinely unknown — treated as stale, never as fresh. */\nexport function qualityUnknown(): StateQuality {\n return { freshness: neverObserved() };\n}\n\n/**\n * Refuse, by name, with the frozen error code.\n *\n * Used where the contract declares a method this build cannot honour. The\n * alternative — a no-op that resolves — is the fabrication pattern in method\n * form: the caller cannot tell \"did nothing\" from \"worked\".\n */\nexport function unsupported(what: string): Error {\n const err: any = new Error(\n `${what} is not implemented in this build — the native engine behind it does not exist yet.`,\n );\n err.code = 'UNSUPPORTED';\n return err;\n}\n","/**\n * The ONE event envelope every domain emits, and the one way listeners are held.\n *\n * The frozen shape is `StateEvent { domain, name, timestamp, payload }`. Three\n * domains were born emitting `{ type: '…' }` instead, so a plugin written\n * against the contract — `e.name === 'appStateChanged'` — never matched, and\n * `e.name.endsWith(…)` threw on `undefined`. Building every event here is what\n * keeps a second shape from growing back.\n *\n * Type-only imports, so the pure modules that use this (the spatial event\n * detector) stay testable under plain `node --test`.\n */\nimport type { DeviceBridgeDomain, StateEvent, Subscription } from '@ateam-ai-mobile/plugin-runtime';\n\n/**\n * Build an event. `payload` is omitted, not `undefined`, when there is none —\n * an event that is only a nudge (\"re-read the snapshot\") says so by shape.\n * `timestamp` is when the change was recognised; it defaults to now, and replay\n * passes its virtual clock.\n */\nexport function stateEvent<TDomain extends DeviceBridgeDomain, TName extends string, TPayload = never>(\n domain: TDomain,\n name: TName,\n payload?: TPayload,\n timestamp: number = Date.now(),\n): StateEvent<TName, TPayload> & { domain: TDomain } {\n return payload === undefined\n ? { domain, name, timestamp }\n : { domain, name, timestamp, payload };\n}\n\n/**\n * Listeners, each isolated from the others. A listener that throws must not\n * stop the next one hearing the event, nor the engine that emitted it.\n */\nexport function listenerSet<E>() {\n const listeners = new Set<(event: E) => void>();\n return {\n add(fn: (event: E) => void): Subscription {\n listeners.add(fn);\n return { unsubscribe: () => { listeners.delete(fn); } };\n },\n emit(event: E): void {\n for (const fn of listeners) {\n try { fn(event); } catch { /* a bad listener is not the engine's problem */ }\n }\n },\n };\n}\n","// ── connectivity ─────────────────────────────────────────────────────────\n// reachability ✓ transport ✓ quality/stability/metered ✗\nimport type { DeviceState, ConnectivitySnapshot } from '@ateam-ai-mobile/plugin-runtime';\nimport type { DeviceStateCollectors } from '../collectors';\nimport { qualityFrom, qualityUnknown, unsupported } from '../quality';\n\nexport function createConnectivityDomain(collectors: DeviceStateCollectors): DeviceState['connectivity'] {\n return {\n async getSnapshot(): Promise<ConnectivitySnapshot> {\n const observedAt = Date.now();\n try {\n if (!collectors.connectivity) throw unsupported('connectivity collector');\n const data: any = await collectors.connectivity.collectConnectivity();\n const online = data?.is_connected === true;\n const type = String(data?.type || '').toLowerCase();\n\n return {\n reachability: data?.is_connected == null ? 'unknown' : online ? 'online' : 'offline',\n transport:\n type === 'wifi' ? 'wifi'\n : type === 'cellular' ? 'cellular'\n : type === 'ethernet' ? 'ethernet'\n : online ? 'other'\n : data?.is_connected === false ? 'none'\n : 'unknown',\n // All four below need measurement nobody takes yet. `unknown` is the\n // designed answer — \"return unknown rather than stale precision\".\n qualityClass: 'unknown',\n stability: 'unknown',\n latencyMs: null,\n bandwidthClass: 'unknown',\n metered: null,\n constrained: null,\n quality: qualityFrom(observedAt),\n };\n } catch {\n return {\n reachability: 'unknown',\n transport: 'unknown',\n qualityClass: 'unknown',\n stability: 'unknown',\n latencyMs: null,\n bandwidthClass: 'unknown',\n metered: null,\n constrained: null,\n quality: qualityUnknown(),\n };\n }\n },\n\n // Intent selects a native sampling strategy — latency probes, quality\n // classification. Nothing here samples anything, so accepting an intent\n // would be accepting an instruction we do not carry out.\n async start() { throw unsupported('connectivity.start'); },\n async stop() { throw unsupported('connectivity.stop'); },\n // A subscription that can never fire is worse than none: the caller\n // reasonably reads silence as \"nothing changed\".\n subscribe() { throw unsupported('connectivity.subscribe'); },\n };\n}\n","// ── capability ───────────────────────────────────────────────────────────\n// permission ✓ availability ✗ restriction ✗\nimport type { DeviceState, CapabilitySnapshot, CapabilityStatus } from '@ateam-ai-mobile/plugin-runtime';\nimport type { DeviceStateCollectors } from '../collectors';\nimport { qualityFrom, unsupported } from '../quality';\n\n/**\n * The bridge's permission model is 4-valued (`not_requested | granted | denied\n * | not_needed`); the frozen one is 7-valued. The extra states are real OS\n * distinctions nothing currently reads, so they map to `unknown` rather than\n * being collapsed into `denied` — \"restricted by policy\" and \"the user said no\"\n * are different problems with different remedies, and guessing between them\n * would put a wrong instruction in front of the user.\n */\nfunction statusFrom(granted: boolean | null): CapabilityStatus {\n return {\n support: 'unknown',\n permission: granted === null ? 'unknown' : granted ? 'granted' : 'denied',\n availability: 'unknown',\n };\n}\n\nconst UNKNOWN_STATUS: CapabilityStatus = {\n support: 'unknown',\n permission: 'unknown',\n availability: 'unknown',\n};\n\nexport function createCapabilityDomain(collectors: DeviceStateCollectors): DeviceState['capability'] {\n return {\n async getSnapshot(): Promise<CapabilitySnapshot> {\n const observedAt = Date.now();\n const locationGranted = collectors.location\n ? await collectors.location.hasPermission().catch(() => null)\n : null;\n\n return {\n // Only location has a real reader today. The rest are declared by the\n // contract and answered honestly rather than omitted.\n camera: UNKNOWN_STATUS,\n microphone: UNKNOWN_STATUS,\n location: {\n ...statusFrom(locationGranted),\n // iOS 14+ distinguishes precise from approximate; nothing reads it.\n precision: 'unknown',\n backgroundPermission: 'unknown',\n },\n motion: UNKNOWN_STATUS,\n bluetooth: UNKNOWN_STATUS,\n uwb: UNKNOWN_STATUS,\n nfc: UNKNOWN_STATUS,\n quality: qualityFrom(observedAt),\n } as CapabilitySnapshot;\n },\n\n // No OS permission-change stream is wired. Same reasoning as connectivity:\n // silence would be indistinguishable from stability.\n subscribe() { throw unsupported('capability.subscribe'); },\n };\n}\n","// ── interaction ──────────────────────────────────────────────────────────\n// app lifecycle ✓ screenOn ✗ audio ✗\nimport { AppState } from 'react-native';\nimport type { DeviceState, InteractionSnapshot } from '@ateam-ai-mobile/plugin-runtime';\nimport { qualityFrom } from '../quality';\nimport { stateEvent } from '../events';\n\n/**\n * Lifecycle only, and honest about the rest.\n *\n * `screenOn` is NOT derivable from AppState: RN reports `inactive` for a\n * notification shade, a control centre swipe, an incoming call AND a locked\n * screen. Mapping `inactive` to \"screen off\" would be wrong most of the time\n * it fired — so it stays null until a native module can answer it.\n *\n * The whole `audio` block reports unknown for the same reason. `interruption`\n * carries the distinction that matters: `none` means \"checked, nothing is\n * interrupting\", `unknown` means \"no knowledge\" — and we have the latter.\n */\nexport function createInteractionDomain(): DeviceState['interaction'] {\n const mapState = (s: string): InteractionSnapshot['lifecycle']['appState'] =>\n s === 'active' ? 'foreground'\n : s === 'background' ? 'background'\n : s === 'inactive' ? 'inactive'\n : 'unknown';\n\n return {\n async getSnapshot(): Promise<InteractionSnapshot> {\n const observedAt = Date.now();\n return {\n lifecycle: {\n appState: mapState(AppState.currentState as string),\n screenOn: null,\n userActive: null,\n idleDurationMs: null,\n },\n audio: {\n captureActive: null,\n playbackActive: null,\n route: 'unknown',\n interruption: 'unknown',\n },\n // The voice block is OMITTED rather than filled with nulls: the design\n // says a platform that cannot report voice session state leaves the\n // group out instead of guessing `false` for each field. The app's own\n // voice store knows this, but it is not reachable from the SDK.\n quality: qualityFrom(observedAt),\n };\n },\n\n // Real: AppState genuinely emits on every foreground/background change,\n // which is the one part of this domain that works.\n subscribe(listener) {\n const sub = AppState.addEventListener('change', next => {\n listener(stateEvent('interaction', 'appStateChanged', { appState: mapState(next as string) }));\n });\n return { unsubscribe: () => sub.remove() };\n },\n };\n}\n","/**\n * The compass, kept warm — and the turn detector fed from it.\n *\n * `getHeadingAsync()` is not usable on the capture path: it starts a watch,\n * waits for a reading and stops it, which costs hundreds of milliseconds — per\n * frame. So this holds ONE subscription while a session is open and callers\n * read the last value synchronously.\n *\n * Returns null until a reading arrives, and null means NOT MEASURED. A heading\n * of 0 is due north, so an unstarted compass must never report 0.\n *\n * WHY THE TURN DETECTOR IS FED HERE. A turn happens BETWEEN reads — it is an\n * event, not a state — so a detector driven by sampled snapshots would miss\n * exactly the moment a guide needs. It runs on the compass callback instead.\n * The logic itself lives in `turnDetector.ts`, free of any Expo import, so it\n * can be tested against a scripted walk. A completed turn is also PUSHED to\n * `onTurn` listeners — spatial publishes it as an event the moment it lands.\n *\n * SHARED, SO COUNTED. Spatial and visual both keep the compass warm, and each\n * used to stop it on its own `stop()`: visual's stop killed the compass — and\n * wiped the turn history — under a live spatial session. Each caller now names\n * itself, and the watch stops only when the last one leaves.\n */\nimport * as ExpoLocation from 'expo-location';\n\nimport { TurnDetector, type TurnEvent } from './turnDetector';\nimport { listenerSet } from './events';\n\nexport type { TurnEvent };\nexport { signedDelta } from './turnDetector';\n\n/** Who is holding the compass open. */\nexport type HeadingOwner = 'spatial' | 'visual';\n\ninterface HeadingReading {\n headingDeg: number | null;\n /**\n * iOS calibration rank: 3 high, 2 medium, 1 low, 0 none. NOT degrees of\n * error, which is why it is never surfaced as `headingAccuracyDeg` — that\n * field wants degrees, and converting a rank into one would be a fabricated\n * error bar. It drives CONFIDENCE instead, which is what it can honestly say.\n */\n calibration: number | null;\n timestamp: number;\n}\n\nlet latest: HeadingReading | null = null;\nlet sub: { remove(): void } | null = null;\n/** The watch being opened, so two owners starting at once open ONE. */\nlet opening: Promise<void> | null = null;\nconst owners = new Set<HeadingOwner>();\nconst turns = new TurnDetector();\nconst turnListeners = listenerSet<TurnEvent>();\n\n/** Start watching on behalf of `owner`. Idempotent per owner. */\nexport function startHeading(owner: HeadingOwner): Promise<void> {\n owners.add(owner);\n if (sub) return Promise.resolve();\n opening ??= open().finally(() => { opening = null; });\n return opening;\n}\n\nasync function open(): Promise<void> {\n try {\n const s = await ExpoLocation.watchHeadingAsync(h => {\n // A reading landing after the last owner left is not kept.\n if (!owners.size) return;\n // trueHeading is -1 until the compass calibrates; magHeading is the\n // fallback the OS itself recommends.\n const raw = h.trueHeading >= 0 ? h.trueHeading : h.magHeading;\n const deg = typeof raw === 'number' && raw >= 0 ? raw : null;\n const cal = typeof h.accuracy === 'number' && h.accuracy >= 0 ? h.accuracy : null;\n const now = Date.now();\n latest = { headingDeg: deg, calibration: cal, timestamp: now };\n const turn = turns.push(deg, now);\n if (turn) turnListeners.emit(turn);\n });\n // Every owner may have left while the OS was starting the watch.\n if (!owners.size) { s.remove(); return; }\n sub = s;\n } catch {\n // No compass, or permission refused. Callers get headingDeg: null, which\n // is the honest answer and does not stop anything else working.\n sub = null;\n }\n}\n\n/** Release `owner`'s hold. The watch stops only when no owner is left. */\nexport function stopHeading(owner: HeadingOwner): void {\n owners.delete(owner);\n if (owners.size) return;\n sub?.remove();\n sub = null;\n // Clear everything: a heading from a finished session describes a direction\n // the user may have long since turned away from, and a turn from a previous\n // walk is not something a new one should report.\n latest = null;\n turns.reset();\n}\n\n/** Hear each completed turn as it completes. Signed: positive = right. */\nexport function onTurn(fn: (turn: TurnEvent) => void) {\n return turnListeners.add(fn);\n}\n\n/** The newest reading, or null if the compass has said nothing yet. */\nexport function currentHeading(): { headingDeg: number | null } | null {\n return latest ? { headingDeg: latest.headingDeg } : null;\n}\n\n/** The newest reading with its calibration and age. */\nexport function currentHeadingDetail(): HeadingReading | null {\n return latest;\n}\n\n/** The most recent completed turn. Signed: positive = clockwise = right. */\nexport function lastMeaningfulTurn(): TurnEvent | null {\n return turns.last();\n}\n","/**\n * Turning, and which way.\n *\n * \"Turn right at the corner\" and \"you have just turned left\" are the two\n * sentences a walking guide says most, and neither can be read off a single\n * heading — they need heading OVER TIME. This is that, as a pure state machine\n * with no Expo import, so it can be tested against a scripted walk instead of\n * against a phone in someone's hand.\n *\n * SIGN IS THE WHOLE POINT. Compass degrees increase clockwise, so a positive\n * delta is a turn to the RIGHT and a negative one to the LEFT. A caller that\n * wants only the magnitude can take the absolute value; a caller handed a\n * magnitude cannot recover the side, and \"turn 90°\" without the side is worse\n * than saying nothing.\n *\n * THE HARD PART IS NOT DETECTING TURNS, IT IS NOT INVENTING THEM. A phone held\n * by a walking person swings 15–20° with every stride, and a compass drifts.\n * Compare against a fixed origin and the drift eventually crosses any threshold\n * on its own, so the machine re-baselines whenever the heading holds steady:\n * \"straight ahead\" is wherever you have been pointing for the last couple of\n * seconds, not wherever you were pointing when the session opened.\n */\n\n/** A completed turn. `deg` is signed: **positive = clockwise = right**. */\nexport interface TurnEvent {\n deg: number;\n /** When the turn was recognised as complete. */\n timestamp: number;\n}\n\nexport interface TurnDetectorOptions {\n /**\n * How far the heading must swing before it counts as a turn.\n *\n * 35° is a judgement, not a measurement: a street corner is ~90°, a gentle\n * bend in a corridor 30–45°, and hand-sway while walking routinely 15–20°.\n * Lower and the detector narrates the user's arm.\n */\n minTurnDeg?: number;\n /** Within this of the reference counts as \"still pointing the same way\". */\n stableDeg?: number;\n /** How long it must stay there before the reference moves. */\n stableMs?: number;\n}\n\n/** Signed shortest arc from `a` to `b`, in (-180, 180]. */\nexport function signedDelta(a: number, b: number): number {\n return ((b - a + 540) % 360) - 180;\n}\n\nexport class TurnDetector {\n private readonly minTurnDeg: number;\n private readonly stableDeg: number;\n private readonly stableMs: number;\n\n /** The heading currently treated as \"straight ahead\". */\n private refDeg: number | null = null;\n private stableSince = 0;\n private lastTurn: TurnEvent | null = null;\n\n constructor(opts: TurnDetectorOptions = {}) {\n this.minTurnDeg = opts.minTurnDeg ?? 35;\n this.stableDeg = opts.stableDeg ?? 12;\n this.stableMs = opts.stableMs ?? 2500;\n }\n\n /** Feed one compass reading. `null` degrees means the compass said nothing. */\n push(deg: number | null, now: number): TurnEvent | null {\n if (deg === null) return null;\n\n if (this.refDeg === null) {\n this.refDeg = deg;\n this.stableSince = now;\n return null;\n }\n\n const delta = signedDelta(this.refDeg, deg);\n\n if (Math.abs(delta) >= this.minTurnDeg) {\n // Re-baseline immediately, so a continuous 180° about-face is reported as\n // the turns it is made of. Left as one growing delta it would flip sign\n // at the halfway point and report a right turn for a left one.\n this.lastTurn = { deg: Math.round(delta), timestamp: now };\n this.refDeg = deg;\n this.stableSince = now;\n return this.lastTurn;\n }\n\n if (Math.abs(delta) > this.stableDeg) {\n // Moving, but not far enough yet. Hold the reference and keep watching —\n // this is what lets a slow 90° turn accumulate instead of being\n // re-baselined away one degree at a time.\n this.stableSince = now;\n return null;\n }\n\n // Steady. After a while, treat here as the new straight-ahead.\n if (now - this.stableSince >= this.stableMs) {\n this.refDeg = deg;\n this.stableSince = now;\n }\n return null;\n }\n\n /**\n * The most recent completed turn, or null if none this session.\n *\n * It does NOT expire on its own: the age is derivable from `timestamp`, and\n * \"recent enough to mention\" is a product judgement this layer must not make\n * on the caller's behalf.\n */\n last(): TurnEvent | null {\n return this.lastTurn;\n }\n\n reset(): void {\n this.refDeg = null;\n this.stableSince = 0;\n this.lastTurn = null;\n }\n}\n","/**\n * Spatial events, derived from spatial snapshots.\n *\n * ONE MODEL, NOT TWO. An event is what changed between two snapshots — the\n * same snapshots `getSnapshot()` returns — so an event can never say \"moving\"\n * while a read says \"stationary\", and no threshold here has a second copy\n * somewhere else. The thresholds a read uses (the stationary speed, the GPS\n * confidence bands, distance along a path) live in this file and the domain\n * imports them, rather than the other way round, so the detector can be tested\n * without an Expo import: the live engine feeds it on every fix, every turn and\n * a one-second tick; replay feeds it recorded samples on a virtual clock.\n *\n * THE HARD PART IS NOT DETECTING CHANGES, IT IS NOT INVENTING THEM (the turn\n * detector's lesson, again). GPS speed wobbles around any threshold, and a fix\n * band flickers when accuracy sits near a boundary. So every state change must\n * HOLD before it is reported, and position changes must beat the fix's own\n * error bar. Holds delay an event on purpose; `since` in the payload carries\n * the real onset.\n *\n * A HOLD IS HELD BY FIXES, NOT BY THE CLOCK ALONE. The one-second tick re-reads\n * the SAME fix, so a hold measured only in time would let one outlier fix\n * followed by a gap — sparse fixes at Balanced accuracy, a wifi jump read as\n * speed — become \"you started moving\". A state settles only once two distinct\n * fixes have shown it; after that the tick may complete the hold, which is what\n * it is for (\"stationary for five seconds\" completes while nothing arrives).\n *\n * Type-only imports from plugin-runtime, so `node --test` can load this file.\n */\nimport type {\n Confidence,\n SpatialEvent,\n SpatialIntent,\n SpatialPoint,\n SpatialSnapshot,\n SpatialStateEvent,\n} from '@ateam-ai-mobile/plugin-runtime';\nimport { stateEvent } from './events';\nimport { distanceOf } from '../geo';\n\n/**\n * Below this the user is standing still.\n *\n * 0.4 m/s is well under a slow walk (~1.2 m/s) and above the jitter a\n * stationary phone's GPS produces. The point of the band is that a guide\n * must not say \"keep going straight\" to someone who has stopped.\n */\nexport const STATIONARY_MPS = 0.4;\n\n/**\n * Only claim stationary/moving when a speed was actually obtained. With no\n * speed the truthful answer is `unknown`, not `stationary` — \"you have\n * stopped\" is a false statement, not a neutral default.\n */\nexport function movementStateOf(speedMps: number | null): 'stationary' | 'moving' | 'unknown' {\n return speedMps === null ? 'unknown' : speedMps < STATIONARY_MPS ? 'stationary' : 'moving';\n}\n\n/**\n * The GPS fix's confidence band, from metres of horizontal error. ~10m is a\n * good open-sky fix; beyond ~50m the position is street-level at best and\n * useless for \"turn at the next corner\".\n */\nexport function fixConfidence(accuracyM: number | null): Confidence['level'] {\n return accuracyM === null ? 'unknown' : accuracyM <= 10 ? 'high' : accuracyM <= 50 ? 'medium' : 'low';\n}\n\n\nexport interface SpatialEventProfile {\n /** How long `moving` must hold before `movementStarted`. */\n movingHoldMs: number;\n /** How long `stationary` must hold, after moving, before `movementStopped`. */\n stoppedHoldMs: number;\n /** How long a new GPS confidence band must hold before it is reported. */\n confidenceHoldMs: number;\n /** Floor for `positionChanged`, metres. The real threshold is\n * max(this, moveThresholdM(anchor, fix)) — both fixes' uncertainty — so a\n * loose fix on EITHER end needs a bigger move. */\n minMoveM: number;\n}\n\n/**\n * Intent → how eager the events are.\n *\n * These are the engine's judgement, not the freeze's — the architecture doc\n * says only \"thresholds are profile-dependent\". Recorded here so the reasoning\n * is visible and arguable rather than buried. The intent changes ONLY these\n * holds, not sampling: there is no native strategy layer for it to reach.\n *\n * - `navigation`: a guide needs \"you've stopped\" soon enough to act on, but a\n * pause at a crossing is not a stop worth announcing — 3 s to start, 5 s to\n * stop. 10 m is about one building frontage.\n * - `fitness`: same movement holds; a slower confidence hold, because a runner\n * under trees dips in and out of a band constantly and nobody needs to hear it.\n * - `preciseLocate`: the caller is deliberately pinning a position, so the\n * holds are shortest and a few metres counts.\n * - `geofence` / `lowPowerTracking`: coarse by nature — tens of metres, and a\n * state must hold for a while before it is worth waking anyone for.\n */\nexport const SPATIAL_EVENT_PROFILES: Record<SpatialIntent, SpatialEventProfile> = {\n navigation: { movingHoldMs: 3000, stoppedHoldMs: 5000, confidenceHoldMs: 3000, minMoveM: 10 },\n fitness: { movingHoldMs: 3000, stoppedHoldMs: 5000, confidenceHoldMs: 5000, minMoveM: 10 },\n preciseLocate: { movingHoldMs: 2000, stoppedHoldMs: 3000, confidenceHoldMs: 2000, minMoveM: 3 },\n geofence: { movingHoldMs: 10000, stoppedHoldMs: 30000, confidenceHoldMs: 10000, minMoveM: 25 },\n lowPowerTracking: { movingHoldMs: 10000, stoppedHoldMs: 30000, confidenceHoldMs: 10000, minMoveM: 50 },\n};\n\n/** The profile for an intent; an unrecognised one gets `navigation`'s. */\n/**\n * The move two fixes can vouch for. A fix is a circle, not a point, and so is\n * the anchor it is compared with: two fixes of a phone lying still differ by up\n * to BOTH their errors. Combined (root-sum-square, each accuracyM ~ one radius)\n * and doubled, like a single fix's 2× before. No estimate = 0 (the floor rules).\n * Anchor ±100 m → fix ±5 m: a 20 m difference is noise, not a walk.\n */\nexport function moveThresholdM(anchor: { accuracyM?: number | null }, fix: { accuracyM?: number | null }): number {\n const a = anchor.accuracyM ?? 0, b = fix.accuracyM ?? 0;\n return 2 * Math.sqrt(a * a + b * b);\n}\n\nexport function spatialEventProfile(intent: string | null | undefined): SpatialEventProfile {\n return SPATIAL_EVENT_PROFILES[intent as SpatialIntent] ?? SPATIAL_EVENT_PROFILES.navigation;\n}\n\ntype Band = 'high' | 'medium' | 'low';\nconst RANK: Record<Band, number> = { high: 3, medium: 2, low: 1 };\ntype PayloadOf<N extends SpatialEvent> = Extract<SpatialStateEvent, { name: N }>['payload'];\n\n/** Distinct fixes a state must be seen on before its hold can complete. */\nconst MIN_FIXES = 2;\n\n/**\n * An unbroken run of one raw state: when it began, and how many distinct fixes\n * (by `position.timestamp`) have shown it. A re-read of the same fix extends\n * the run's time, never its evidence.\n */\ninterface Run<S> { state: S; since: number; fixAt: number | null; fixes: number }\n\nfunction extend<S>(run: Run<S> | null, state: S, fixAt: number | null, now: number): Run<S> {\n if (!run || run.state !== state) return { state, since: now, fixAt, fixes: fixAt === null ? 0 : 1 };\n if (fixAt !== null && fixAt !== run.fixAt) { run.fixAt = fixAt; run.fixes++; }\n return run;\n}\n\nconst settled = (run: Run<unknown>, now: number, holdMs: number): boolean =>\n run.fixes >= MIN_FIXES && now - run.since >= holdMs;\n\n/** Which fix a snapshot was built from — its identity when counting evidence. */\nconst fixAt = (s: SpatialSnapshot): number | null => s.position?.timestamp ?? null;\n\nexport class SpatialEventDetector {\n private profile: SpatialEventProfile;\n\n /** The movement state last settled on. Only MEASURED states settle. */\n private movement: 'stationary' | 'moving' | 'unknown' = 'unknown';\n private run: Run<'stationary' | 'moving' | 'unknown'> | null = null;\n\n /** The completed turn already on record — reported turns, and the one the\n * session opened onto. Only a turn with a new timestamp is news. */\n private lastTurnAt: number | null = null;\n\n /** The GPS band last settled on; null until the session's first band. */\n private band: Band | null = null;\n private bandRun: Run<Band> | null = null;\n\n /** The last position REPORTED — distance is measured from here, not from\n * the previous fix, so a slow walk still adds up to a change. */\n private anchor: SpatialPoint | null = null;\n\n private staleArmed = true;\n\n constructor(profile: SpatialEventProfile = SPATIAL_EVENT_PROFILES.navigation) {\n this.profile = profile;\n }\n\n /**\n * Forget everything — a new session, or replay seeking backwards. Emits\n * nothing. `turnOnRecordAt` is the timestamp of the turn already completed\n * when the session opens (a visual session shares the compass): history, not\n * news. Seeded from the turn itself rather than compared against a clock, so\n * it holds whichever clock the turn was stamped with — epoch live, the\n * recording's epoch in replay, where `now` is virtual.\n */\n reset(profile?: SpatialEventProfile, turnOnRecordAt: number | null = null): void {\n if (profile) this.profile = profile;\n this.movement = 'unknown';\n this.run = null;\n this.lastTurnAt = turnOnRecordAt;\n this.band = null;\n this.bandRun = null;\n this.anchor = null;\n this.staleArmed = true;\n }\n\n /**\n * Feed the newest snapshot. Returns what changed since the last one, in this\n * order: movement, turn, position, confidence, staleness. Fields are read\n * defensively — replay hands over recorded snapshots verbatim, and a recording\n * may lack a block the live engine always fills.\n */\n push(snapshot: SpatialSnapshot, now: number): SpatialStateEvent[] {\n const out: SpatialStateEvent[] = [];\n const emit: Emit = (name, payload) => {\n // The payload is checked against its name by `Emit`; the union cannot\n // follow a generic name through stateEvent's return, hence the assertion.\n out.push(stateEvent('spatial', name, payload as unknown, now) as SpatialStateEvent);\n };\n this.movementFrom(snapshot, now, emit);\n this.turnFrom(snapshot, emit);\n this.positionFrom(snapshot, emit);\n this.confidenceFrom(snapshot, now, emit);\n this.staleFrom(snapshot, emit);\n return out;\n }\n\n private movementFrom(s: SpatialSnapshot, now: number, emit: Emit): void {\n const state = s.movement?.state ?? 'unknown';\n this.run = extend(this.run, state, fixAt(s), now);\n if (state === this.movement) return;\n\n if (state === 'moving' && settled(this.run, now, this.profile.movingHoldMs)) {\n emit('movementStarted', {\n previous: this.movement === 'stationary' ? 'stationary' : 'unknown',\n speedMps: s.movement?.speedMps ?? null,\n bearingDeg: s.movement?.bearingDeg ?? null,\n since: this.run.since,\n });\n this.movement = 'moving';\n } else if (state === 'stationary' && settled(this.run, now, this.profile.stoppedHoldMs)) {\n if (this.movement === 'moving') emit('movementStopped', { previous: 'moving', since: this.run.since });\n this.movement = 'stationary';\n }\n // `unknown` never settles. Losing the speed is not evidence of stopping, so\n // moving → unknown says nothing, and a later measured `stationary` is still\n // reported as the stop it is.\n }\n\n private turnFrom(s: SpatialSnapshot, emit: Emit): void {\n const at = s.changes?.timestamp ?? null;\n const deg = s.changes?.lastMeaningfulTurnDeg ?? null;\n // A compass reset clears the turn to null. That is not a new turn.\n if (at === null || deg === null || at === this.lastTurnAt) return;\n this.lastTurnAt = at;\n emit('lastMeaningfulTurnChanged', { lastMeaningfulTurnDeg: deg, timestamp: at });\n }\n\n private positionFrom(s: SpatialSnapshot, emit: Emit): void {\n const p = s.position ?? null;\n if (!p) return;\n if (!this.anchor) {\n emit('positionChanged', { position: { ...p }, previous: null, distanceM: null });\n this.anchor = p;\n return;\n }\n const d = distanceOf([this.anchor, p]);\n if (d < Math.max(this.profile.minMoveM, moveThresholdM(this.anchor, p))) {\n // Not a move — but if this fix is far more precise than the anchor, keep\n // IT as the reference (silently): measuring later moves from a ±100 m\n // anchor would hide real ones behind the old fix's error.\n const aNew = p.accuracyM ?? null, aOld = this.anchor.accuracyM ?? null;\n if (aNew != null && aOld != null && aNew * 2 < aOld) this.anchor = p;\n return;\n }\n emit('positionChanged', { position: { ...p }, previous: { ...this.anchor }, distanceM: d });\n this.anchor = p;\n }\n\n private confidenceFrom(s: SpatialSnapshot, now: number, emit: Emit): void {\n const p = s.position ?? null;\n if (!p) return;\n const level = fixConfidence(p.accuracyM ?? null);\n // A fix with no accuracy estimate says nothing about the band — the same\n // way the snapshot's confidence ignores an `unknown` part.\n if (level === 'unknown') return;\n // The first band is where the session starts, not a change.\n if (this.band === null) { this.band = level; return; }\n if (level === this.band) { this.bandRun = null; return; }\n this.bandRun = extend(this.bandRun, level, fixAt(s), now);\n if (!settled(this.bandRun, now, this.profile.confidenceHoldMs)) return;\n emit('positionConfidenceChanged', {\n level,\n previous: this.band,\n accuracyM: p.accuracyM ?? null,\n direction: RANK[level] < RANK[this.band] ? 'dropped' : 'recovered',\n });\n this.band = level;\n this.bandRun = null;\n }\n\n private staleFrom(s: SpatialSnapshot, emit: Emit): void {\n const f = s.quality?.freshness;\n if (!f) return;\n // Re-armed only by a FRESH read: `aging` on the way back is not recovery.\n if (f.state === 'fresh') { this.staleArmed = true; return; }\n if (f.state !== 'stale' || !this.staleArmed) return;\n emit('spatialStateStale', { observedAt: f.observedAt, ageMs: f.ageMs });\n this.staleArmed = false;\n }\n}\n\ntype Emit = <N extends SpatialEvent>(name: N, payload: PayloadOf<N>) => void;\n","// ── spatial ──────────────────────────────────────────────────────────────\n// position ✓ path ✓ heading ✓ turns ✓ events ✓ (while a session is open) speed ~derived\nimport type {\n DeviceState,\n SpatialIntent,\n SpatialSnapshot,\n SpatialStateEvent,\n SpatialPoint,\n StateQuality,\n Subscription,\n} from '@ateam-ai-mobile/plugin-runtime';\nimport { startHeading, stopHeading, currentHeadingDetail, lastMeaningfulTurn, onTurn } from '../headingSource';\nimport type { DeviceStateCollectors, SpeedRef, WatchAccuracy } from '../collectors';\nimport { qualityFrom, qualityUnknown, unsupported } from '../quality';\nimport { distanceOf } from '../../geo';\nimport { listenerSet } from '../events';\nimport {\n SpatialEventDetector,\n fixConfidence,\n movementStateOf,\n spatialEventProfile,\n} from '../spatialEvents';\n\n/**\n * Position, movement, facing and a short recent path.\n *\n * The design's performance rule is that navigation reads come from a WARM\n * native cache, not a fresh GPS+compass call per invocation. There is no such\n * cache today, so `getSnapshot` takes a fix — correct but not cheap. That is\n * why `start()` matters: it opens a `watchPosition` stream AND a compass watch,\n * and every later snapshot serves the newest streamed values instead, which is\n * the warm path this layer can offer without a native module.\n *\n * TWO DIRECTIONS, NOT ONE, and conflating them is the bug this replaced.\n *\n * `orientation.headingDeg` — where the phone is POINTING. The compass.\n * `movement.bearingDeg` — where the user is GOING. Course over ground.\n *\n * They disagree constantly and each answers a different sentence. \"Turn right\n * at the corner\" is decided by the compass; \"you are heading north on Main\n * Street\" by the course. The previous version assigned the same variable to\n * both, which is only right by accident, and that variable was permanently\n * null because the streaming collector dropped the fields.\n *\n * TURNS ARE SIGNED. `changes.lastMeaningfulTurnDeg` is positive for clockwise\n * (right) and negative for anticlockwise (left), so a guide can say which way\n * the user just went without a second field to consult. The freeze names the\n * field but states no convention; this is a decision, recorded here.\n *\n * EVENTS ARE THE SNAPSHOT, DIFFED. `subscribe` publishes what changed between\n * successive warm snapshots — the same ones `getSnapshot` returns — via the\n * pure detector in spatialEvents.ts. It is fed on every fix, the moment a turn\n * completes, and once a second, so a transition that happens between fixes\n * (fixes stopping is itself one: the state goes stale) is still noticed.\n */\n/** The GPS tier each intent's stream runs at. Navigation is the one that needs\n * \"which side of the street\"; everything else trades precision for battery. */\nconst WATCH_ACCURACY: Record<SpatialIntent, WatchAccuracy> = {\n navigation: 'navigation',\n preciseLocate: 'high',\n fitness: 'high',\n geofence: 'balanced',\n lowPowerTracking: 'low',\n};\n\nexport function createSpatialDomain(\n collectors: DeviceStateCollectors,\n speedRef: SpeedRef,\n): DeviceState['spatial'] {\n // The warm cache, when a session is open. Newest last, bounded — an unbounded\n // path on a walking user grows without limit.\n const RECENT_PATH_MAX = 60;\n\n let recentPath: SpatialPoint[] = [];\n let latest: SpatialPoint | null = null;\n /** Speed and course from the newest STREAMED fix — see `latest`. */\n let latestSpeed: number | null = null;\n let latestCourse: number | null = null;\n let stopWatch: (() => void) | null = null;\n /** The running session. A second `start()` joins it, and is told so. */\n let session: { id: string; intent: SpatialIntent; startedAt: number } | null = null;\n\n const detector = new SpatialEventDetector();\n const listeners = listenerSet<SpatialStateEvent>();\n let ticker: ReturnType<typeof setInterval> | null = null;\n let turnSub: Subscription | null = null;\n\n const nonNegative = (v: unknown): number | null =>\n typeof v === 'number' && v >= 0 ? v : null;\n\n /**\n * Speed from the path when the OS declined to estimate one.\n *\n * Only over the last two points and only if they are close in time: two fixes\n * a minute apart give an average that describes neither moment. Returns null\n * rather than a number the caller cannot tell apart from a measured one —\n * the confidence on the snapshot is what marks it as derived.\n */\n const derivedSpeed = (): number | null => {\n if (recentPath.length < 2) return null;\n const b = recentPath[recentPath.length - 1];\n const a = recentPath[recentPath.length - 2];\n const dtS = (b.timestamp - a.timestamp) / 1000;\n if (dtS <= 0 || dtS > 10) return null;\n return distanceOf([a, b]) / dtS;\n };\n\n /** Course from the path, for the same reason and with the same caution. */\n const derivedCourse = (): number | null => {\n if (recentPath.length < 2) return null;\n const b = recentPath[recentPath.length - 1];\n const a = recentPath[recentPath.length - 2];\n if (b.timestamp - a.timestamp > 10_000) return null;\n // Too small a step is noise, not a direction.\n if (distanceOf([a, b]) < 3) return null;\n return bearingBetween(a, b);\n };\n\n /**\n * The snapshot for a fix — the ONE builder, used by the read and by the\n * event detector alike. Synchronous: everything it needs is already in hand.\n */\n const snapshotFrom = (\n point: SpatialPoint,\n measuredSpeed: number | null,\n measuredCourse: number | null,\n ): SpatialSnapshot => {\n // Fall back to the path only where the OS said nothing.\n let speedMps = measuredSpeed;\n let courseDeg = measuredCourse;\n let derived = false;\n if (speedMps === null) { const d = derivedSpeed(); if (d !== null) { speedMps = d; derived = true; } }\n if (courseDeg === null) { const d = derivedCourse(); if (d !== null) { courseDeg = d; derived = true; } }\n\n const heading = currentHeadingDetail();\n const turn = lastMeaningfulTurn();\n\n return {\n position: point,\n movement: {\n speedMps,\n bearingDeg: courseDeg,\n // Needs two points from the SAME session; without a stream there is\n // nothing to measure against, and a distance of 0 would read as\n // \"the user has not moved\".\n distanceRecentM: recentPath.length >= 2 ? distanceOf(recentPath) : null,\n state: movementStateOf(speedMps),\n },\n orientation: {\n headingDeg: heading?.headingDeg ?? null,\n // NOT the calibration rank. iOS reports 0–3 (none…high), which is not\n // a number of degrees, and converting one to the other would be an\n // invented error bar. The rank drives `confidence` below instead.\n headingAccuracyDeg: null,\n },\n changes: {\n lastMeaningfulTurnDeg: turn ? turn.deg : null,\n timestamp: turn ? turn.timestamp : null,\n },\n recentPath: [...recentPath],\n quality: qualityFrom(\n point.timestamp,\n spatialConfidence(point.accuracyM ?? null, heading?.calibration ?? null, derived),\n ),\n };\n };\n\n /** \"No position\" — honest nulls, never a (0,0). The compass may still know\n * which way the phone points, so heading is kept when it has one. */\n const noFix = (): SpatialSnapshot => ({\n position: null,\n movement: { speedMps: null, bearingDeg: null, distanceRecentM: null, state: 'unknown' },\n orientation: { headingDeg: currentHeadingDetail()?.headingDeg ?? null },\n recentPath: [],\n quality: qualityUnknown(),\n });\n\n /** The warm snapshot, or null before the session's first streamed fix. */\n const warmSnapshot = (): SpatialSnapshot | null =>\n latest ? snapshotFrom(latest, latestSpeed, latestCourse) : null;\n\n /**\n * Diff the snapshot a read would get RIGHT NOW against the last one, and\n * publish what changed. Before the first fix there is nothing to diff; a\n * turn completed meanwhile is not lost, it is reported with the first fix.\n */\n const observe = () => {\n const snapshot = warmSnapshot();\n if (!snapshot) return;\n for (const event of detector.push(snapshot, Date.now())) listeners.emit(event);\n };\n\n /**\n * Re-diff once a second while a session is open. Fixes alone are not enough:\n * the OS may pause them while the phone is still, and \"the state has gone\n * stale\" or \"you have been stationary for five seconds\" are exactly the\n * transitions that complete while nothing arrives. A tick re-reads the same\n * fix, so it only completes a state the fixes already showed — it never\n * turns one outlier fix into an event (spatialEvents.ts, MIN_FIXES).\n */\n const startTicking = () => {\n ticker = setInterval(observe, 1000);\n };\n\n const stopTicking = () => {\n if (ticker) clearInterval(ticker);\n ticker = null;\n };\n\n return {\n async getSnapshot(): Promise<SpatialSnapshot> {\n // Serve the warm fix when a session is streaming; otherwise take one.\n const warm = warmSnapshot();\n if (warm) return warm;\n // A session is open but its stream has not delivered yet: say so NOW.\n // A one-shot fix here would make every read wait on a cold GPS (seconds),\n // stalling whatever loop reads us — and the stream answers soon anyway.\n if (session) return noFix();\n\n let point: SpatialPoint;\n let speedMps: number | null;\n let courseDeg: number | null;\n try {\n if (!collectors.location) throw unsupported('location collector');\n const fix: any = await collectors.location.getPosition({ accuracy: 'low' });\n point = {\n latitude: fix.latitude,\n longitude: fix.longitude,\n altitude: fix.altitude ?? null,\n accuracyM: nonNegative(fix.accuracy),\n timestamp: fix.timestamp ?? Date.now(),\n };\n speedMps = nonNegative(fix.speed);\n courseDeg = nonNegative(fix.course);\n } catch {\n // Permission refused, or no fix. Not a position at (0,0).\n return noFix();\n }\n return snapshotFrom(point, speedMps, courseDeg);\n },\n\n /**\n * Open the warm window: position stream + compass + event feed.\n *\n * The intent selects how eager the EVENTS are (spatialEvents.ts profiles)\n * and the GPS TIER the stream runs at (WATCH_ACCURACY): navigation pays the\n * battery for BestForNavigation; low-power tracking does not.\n *\n * A second `start()` while one is open joins the running session and is\n * told ITS intent, not handed back the one it asked for: answering with the\n * caller's intent would claim a profile that is not in force.\n */\n async start(options) {\n if (session) return { ...session };\n if (!collectors.location) throw unsupported('location collector');\n const startedAt = Date.now();\n session = { id: `spatial_${startedAt}`, intent: options.intent, startedAt };\n // A turn the compass completed before this session (a visual session\n // shares it) is already on record, not news.\n detector.reset(spatialEventProfile(options.intent), lastMeaningfulTurn()?.timestamp ?? null);\n // The compass is what makes heading and turn detection possible, and it\n // has to run for the session's whole life: a turn happens between reads,\n // so a compass started on demand would miss exactly the event a guide\n // most needs. Shared with a visual session if one is open.\n void startHeading('spatial');\n // A turn is published the moment it completes, not on the next tick.\n turnSub = onTurn(observe);\n stopWatch = collectors.location.watchPosition((pos: any) => {\n const p: SpatialPoint = {\n latitude: pos.latitude,\n longitude: pos.longitude,\n altitude: pos.altitude ?? null,\n accuracyM: nonNegative(pos.accuracy),\n timestamp: pos.timestamp ?? Date.now(),\n };\n latest = p;\n latestSpeed = nonNegative(pos.speed);\n latestCourse = nonNegative(pos.course);\n speedRef.mps = latestSpeed;\n speedRef.at = p.timestamp;\n recentPath = [...recentPath, p].slice(-RECENT_PATH_MAX);\n observe();\n }, { accuracy: WATCH_ACCURACY[options.intent] ?? 'balanced' });\n startTicking();\n return { ...session };\n },\n\n async stop() {\n stopWatch?.();\n stopWatch = null;\n session = null;\n stopTicking();\n turnSub?.unsubscribe();\n turnSub = null;\n // Listeners are KEPT: a subscription outlives a session, and hears the\n // next one. Only what this session observed is forgotten.\n detector.reset();\n // The path belongs to the session that recorded it. Keeping it would let\n // a later snapshot report movement from a walk that ended hours ago.\n recentPath = [];\n latest = null;\n latestSpeed = null;\n latestCourse = null;\n speedRef.mps = null;\n speedRef.at = 0;\n // Releases only spatial's hold — a visual session keeps the compass.\n stopHeading('spatial');\n },\n\n /**\n * The six spatial events (see `SpatialEvent`). Subscribing opens no sensor:\n * events flow only while a session is open, and a subscription made before\n * `start()` hears that session from its first fix.\n */\n subscribe(listener) {\n return listeners.add(listener);\n },\n };\n}\n\n/**\n * How much to trust this spatial reading.\n *\n * Three things degrade it and they are not interchangeable: a loose GPS fix\n * (the position is vague), an uncalibrated compass (the FACING is vague, which\n * is what breaks \"turn left\"), and values derived from the path rather than\n * measured by the OS. The lowest of the three wins, because a caller acting on\n * the snapshot is exposed to the worst part of it, not the average.\n *\n * This is the signal that decides Ada Guide's modality switch: indoors the fix\n * degrades, confidence drops, and that drop is what makes the guide ask for the\n * camera instead of guessing from a GPS fix it should not trust.\n */\nfunction spatialConfidence(\n accuracyM: number | null,\n calibration: number | null,\n derived: boolean,\n): StateQuality['confidence'] {\n const rank = (v: 'high' | 'medium' | 'low' | 'unknown') =>\n v === 'high' ? 3 : v === 'medium' ? 2 : v === 'low' ? 1 : 0;\n\n // The fix band is shared with positionConfidenceChanged — one set of numbers.\n const fromFix = fixConfidence(accuracyM);\n\n // iOS calibration rank: 3 high, 2 medium, 1 low, 0 none.\n const fromCompass: 'high' | 'medium' | 'low' | 'unknown' =\n calibration === null ? 'unknown'\n : calibration >= 3 ? 'high'\n : calibration >= 2 ? 'medium'\n : 'low';\n\n const parts = [fromFix, fromCompass, ...(derived ? (['low'] as const) : [])];\n const known = parts.filter(p => p !== 'unknown');\n if (!known.length) return { level: 'unknown' };\n const worst = known.reduce((a, b) => (rank(a) <= rank(b) ? a : b));\n return { level: worst };\n}\n\n/** Initial bearing from `a` to `b`, degrees clockwise from north. */\nfunction bearingBetween(a: SpatialPoint, b: SpatialPoint): number {\n const toRad = (d: number) => (d * Math.PI) / 180;\n const la1 = toRad(a.latitude), la2 = toRad(b.latitude);\n const dLon = toRad(b.longitude - a.longitude);\n const y = Math.sin(dLon) * Math.cos(la2);\n const x = Math.cos(la1) * Math.sin(la2) - Math.sin(la1) * Math.cos(la2) * Math.cos(dLon);\n return (((Math.atan2(y, x) * 180) / Math.PI) + 360) % 360;\n}\n","/**\n * The Visual engine — a rolling frame timeline the Visual domain reads.\n *\n * WHY THIS IS SHAPED ODDLY. expo-camera can only produce frames through a\n * MOUNTED `<CameraView>`; there is no headless capture API. So the engine is\n * split in two:\n *\n * this module — the state: the ring, the session, the intent, the cadence.\n * Pure TS, no React, so the domain and tests can use it.\n * VisualEngineHost — a component the app mounts once. When a session is open\n * it mounts the camera and feeds frames in; otherwise it\n * renders nothing and the camera is off.\n *\n * The design's rule is that `getSnapshot()` must be a CACHED read that does not\n * open the camera or take a picture — \"avoid repeatedly starting the camera or\n * capturing a new image for every plugin-tool call during an interactive\n * session\". The ring is what makes that possible: capture runs on the engine's\n * own cadence, and reads serve whatever is already there.\n *\n * THE CADENCE IS THE ENGINE'S CHOICE, NOT THE PLUGIN'S. A plugin declares an\n * INTENT — `navigation`, `document`, `ocr` — and this picks the interval. That\n * is the whole point of the semantic layer: a caller asking for frames every\n * 500ms is specifying an implementation, and cannot know the analysis latency,\n * the battery state or the thermal headroom. Intent survives those changing;\n * a number does not.\n *\n * EACH FRAME CARRIES ITS ORIENTATION — heading, read at capture time. Without\n * it a frame cannot support a guidance instruction: \"turn left at the corner\"\n * needs to know which way the lens faced. Reading it afterwards would pair a\n * frame with a heading from after the user turned, which is how a guide\n * describes the wrong junction with confidence.\n *\n * POSITION IS NOT ON THE FRAME, and that is the freeze's decision, not an\n * omission. VisualFrameRef (API_FREEZE §4) carries `orientation` only; where\n * the user IS belongs to Spatial. A caller that needs both correlates them by\n * `timestamp` — both snapshots carry one. Duplicating position here would\n * create a second copy that can disagree with Spatial's, which is precisely\n * the two-descriptions-of-one-thing problem this whole layer exists to end.\n *\n * NOT IMPLEMENTED, and reported as `unknown`/null rather than guessed:\n * per-frame blur, exposure and obstruction (need image analysis), and\n * pitch/roll (need device-motion, not installed). A frame too dark to use must\n * not be described as `exposure: 'good'` because nothing looked at it.\n */\nimport type { StateQuality } from '@ateam-ai-mobile/plugin-runtime';\n\n/** A frame in the ring. `uri` is a local file the app can read; nothing is uploaded. */\nexport interface VisualFrame {\n handle: { id: string; mimeType: string; expiresAt: number | null };\n timestamp: number;\n /**\n * WHERE THE CAMERA WAS POINTING, stamped at capture time.\n *\n * A frame without this is nearly useless for guidance: \"turn left at the\n * corner\" needs to know which way the lens faced and where the user stood.\n * And it has to be captured WITH the frame, not read afterwards — by the\n * time a snapshot is read the user has walked on, and pairing a frame with a\n * later heading is how a guide describes the wrong junction confidently.\n *\n * Every field is nullable and null means NOT MEASURED. `headingDeg: 0` is due\n * north, so a missing compass must not collapse to 0.\n */\n orientation?: {\n /** True heading if available, else magnetic. Null when the compass said nothing. */\n headingDeg?: number | null;\n /** Not measured — needs device-motion, which is not installed. */\n pitchDeg?: number | null;\n rollDeg?: number | null;\n };\n quality?: { blur?: string; exposure?: string; obstructed?: boolean | null };\n}\n\n/**\n * Supplies the spatial context stamped onto each frame.\n *\n * Injected rather than imported for the same reason the collectors are, and\n * because it must be CHEAP: it is read once per capture, on the engine's\n * cadence. Both getters return null rather than throwing — a frame with no\n * heading is still a usable frame, and losing the picture because the compass\n * was busy would be the wrong trade.\n */\nexport interface VisualContextProvider {\n heading(): { headingDeg: number | null } | null;\n}\n\nexport type VisualIntentName =\n | 'navigation' | 'document' | 'ocr' | 'shopping'\n | 'inspection' | 'accessibility' | 'stream' | 'highQuality';\n\n/**\n * Intent → capture cadence and window.\n *\n * These are the engine's judgement, not the freeze's — the docs say native\n * owns sampling and deliberately do not fix numbers. Recorded here so the\n * reasoning is visible and arguable rather than buried:\n *\n * - `navigation`: a walking user's scene changes continuously, so a steady\n * cadence is right; ~1s is the ceiling at which a guidance instruction is\n * still about the present. Faster mostly queues behind the analysis.\n * - `document` / `ocr`: the subject is held still and the user is aiming. A\n * slow cadence is enough, and a big window is pointless.\n * - `stream`: the fastest we offer, for a caller that really is watching.\n * - `highQuality`: rare, deliberate, single good frame.\n */\nconst PROFILES: Record<VisualIntentName, { intervalMs: number; windowMs: number }> = {\n navigation: { intervalMs: 1000, windowMs: 5000 },\n accessibility: { intervalMs: 1000, windowMs: 5000 },\n stream: { intervalMs: 500, windowMs: 5000 },\n inspection: { intervalMs: 2000, windowMs: 8000 },\n shopping: { intervalMs: 2000, windowMs: 5000 },\n document: { intervalMs: 3000, windowMs: 3000 },\n ocr: { intervalMs: 3000, windowMs: 3000 },\n highQuality: { intervalMs: 5000, windowMs: 5000 },\n};\n\nexport interface VisualSessionState {\n id: string;\n intent: VisualIntentName;\n startedAt: number;\n /** The camera the session reads. The host opens THIS one. */\n facing: 'front' | 'back';\n /** Set while the app is backgrounded. The session SURVIVES; the camera does not. */\n suspended: boolean;\n}\n\ntype Listener = () => void;\n\nclass VisualEngine {\n private frames: VisualFrame[] = [];\n private session: VisualSessionState | null = null;\n private listeners = new Set<Listener>();\n /** Set by the host component while a camera is actually mounted and ready. */\n private cameraReady = false;\n /** Optional: supplies heading/position stamped onto each frame. */\n private context: VisualContextProvider | null = null;\n\n /** Host DI. Without it frames carry no orientation, and say so with nulls. */\n setContextProvider(p: VisualContextProvider | null): void { this.context = p; }\n\n // ── session ──\n\n startSession(intent: VisualIntentName, facing: 'front' | 'back' = 'back'): VisualSessionState {\n if (this.session) {\n if (this.session.facing === facing) return this.session;\n // Same session, other camera. A NEW object, so a host holding the old one\n // sees the change; the frames are dropped because they came from the\n // other camera.\n this.session = { ...this.session, facing };\n this.frames = [];\n this.cameraReady = false;\n this.notify();\n return this.session;\n }\n this.session = {\n id: `visual_${Date.now()}`,\n intent: PROFILES[intent] ? intent : 'navigation',\n startedAt: Date.now(),\n facing,\n suspended: false,\n };\n this.notify();\n return this.session;\n }\n\n stopSession(): void {\n this.session = null;\n // The timeline belongs to the session. Keeping it would let a later read\n // describe a room the user left.\n this.frames = [];\n this.cameraReady = false;\n this.notify();\n }\n\n /**\n * Background: suspend, do not end.\n *\n * iOS tears the camera down when the app leaves the foreground. The session\n * is a STATE that survives that; what does not survive is the claim that the\n * camera is live. While suspended the ring is kept but ages — a read gets\n * old frames clearly marked old, rather than a fresh-looking lie.\n */\n suspend(): void {\n if (!this.session || this.session.suspended) return;\n this.session.suspended = true;\n this.cameraReady = false;\n this.notify();\n }\n\n resume(): void {\n if (!this.session?.suspended) return;\n this.session.suspended = false;\n this.notify();\n }\n\n getSession(): VisualSessionState | null { return this.session; }\n\n /** Is a camera mounted and ready right now? Only the host knows. */\n setCameraReady(ready: boolean): void {\n this.cameraReady = ready;\n this.notify();\n }\n isCameraReady(): boolean { return this.cameraReady && !this.session?.suspended; }\n\n // ── the ring ──\n\n profile() {\n const intent = this.session?.intent || 'navigation';\n return PROFILES[intent];\n }\n\n /**\n * Feed from the native vision session (VisionSession, iOS) — the FAST frame\n * source that replaces the takePictureAsync loop. The frame is IN-MEMORY: the\n * native ring holds the pixels keyed by `id`, and native.vision resolves that\n * id inside the trusted layer. No `uri` crosses here; the quality is a REAL\n * measurement (not `unknown`), and `expiresAt` is the ring's real eviction time.\n * The public VisualFrameRef strip to id-only is owned by the device-state session.\n */\n pushFrameMeta(meta: {\n id: string;\n timestamp: number;\n expiresAt?: number | null;\n quality?: { blur?: string; exposure?: string; obstructed?: boolean | null };\n }): void {\n let heading: { headingDeg: number | null } | null = null;\n try { heading = this.context?.heading() ?? null; } catch { /* a frame without a heading beats no frame */ }\n\n this.frames.push({\n // In-memory: no file. native.vision resolves the id inside the native ring.\n handle: { id: meta.id, mimeType: 'image/none', expiresAt: meta.expiresAt ?? null },\n timestamp: meta.timestamp,\n orientation: { headingDeg: heading?.headingDeg ?? null, pitchDeg: null, rollDeg: null },\n quality: meta.quality ?? { blur: 'unknown', exposure: 'unknown', obstructed: null },\n });\n this.evict();\n this.notify();\n }\n\n // The native ring's actual retention (ms). The engine's effective window is\n // min(intent window, retention) — set by whoever owns the clamp semantics; this\n // just stores the input so that logic has one source of truth to read.\n private _retentionMs: number | null = null;\n setRetentionMs(ms: number | null): void { this._retentionMs = ms && ms > 0 ? ms : null; }\n retentionMs(): number | null { return this._retentionMs; }\n\n /**\n * How far back the timeline ACTUALLY reaches: `min(intent window, native\n * retention)`.\n *\n * The intent asks for a window; native decides what it can afford to keep.\n * Full-res luma at ~24fps is tens of megabytes for a few seconds, so the ring\n * is memory-bounded and shrinks under pressure — `retentionMs` is native\n * publishing what it is really holding.\n *\n * EVERY FRAME IN THE TIMELINE CAN BE ANALYSED. That is the one sentence a\n * plugin author has to hold, and this clamp is what makes it true. The\n * alternative — listing frames past retention marked \"not analysable\" — was\n * considered and rejected: a caller would have to check a flag before every\n * use, and forgetting throws mid-guidance on a frame the model can see\n * listed. Present-but-unusable is the same trap as an empty rowset reading\n * as \"no rows matched\".\n *\n * If per-frame history beyond retention is ever needed, the shape is an\n * AGGREGATE on the snapshot (\"the view has been unusable for N ms\"), never a\n * per-entry flag.\n */\n effectiveWindowMs(): number {\n const intent = this.profile().windowMs;\n const retention = this._retentionMs;\n return retention === null ? intent : Math.min(intent, retention);\n }\n\n /**\n * Time-based, not count-based: the design specifies a rolling WINDOW.\n *\n * Evicts on the EFFECTIVE window, so a frame whose pixels native has already\n * dropped leaves the timeline at the same moment. Without this, `frameById`\n * would start returning null for frames still being listed.\n */\n private evict(): void {\n const cutoff = Date.now() - this.effectiveWindowMs();\n this.frames = this.frames.filter(f => f.timestamp >= cutoff);\n }\n\n timeline(): { windowMs: number; frames: VisualFrame[] } {\n this.evict();\n // The EFFECTIVE window, never the intent's. Reporting the intent would have\n // a guide reason about 8 seconds of history it does not have — precisely\n // the confusion the clamp exists to prevent.\n return { windowMs: this.effectiveWindowMs(), frames: [...this.frames] };\n }\n\n latest(): VisualFrame | null {\n this.evict();\n return this.frames.length ? this.frames[this.frames.length - 1] : null;\n }\n\n /**\n * Resolve a frame handle id (from a VisualFrameRef) back to the live frame so\n * the mechanical layer — native.vision — can read its on-device file. The\n * whole contract of that layer is that it takes an id, never pixels; this is\n * where an id becomes a `handle.uri` it can open. Returns null when the id is\n * unknown OR its frame has aged out of the rolling window (the cache file may\n * also be gone by then) — a real \"I can't look at that frame\" answer, which\n * the caller must treat as distinct from \"I looked and found nothing\".\n */\n frameById(id: string): VisualFrame | null {\n this.evict();\n return this.frames.find(f => f.handle.id === id) ?? null;\n }\n\n /**\n * Record a MEASURED quality back onto a frame (native.vision.assessQuality).\n * Frames are minted with `quality: unknown` because nothing looks at the\n * pixels at capture time; this is how a real measurement replaces that so a\n * later read of VisualFrameRef.quality reflects it. No-op if the frame has\n * aged out — we never resurrect an evicted frame just to label it.\n */\n setFrameQuality(id: string, quality: NonNullable<VisualFrame['quality']>): void {\n const f = this.frames.find(fr => fr.handle.id === id);\n if (!f) return;\n f.quality = quality;\n this.notify();\n }\n\n // ── change notification ──\n\n subscribe(fn: Listener): () => void {\n this.listeners.add(fn);\n return () => this.listeners.delete(fn);\n }\n private notify(): void {\n for (const fn of this.listeners) { try { fn(); } catch { /* a bad listener is not the engine's problem */ } }\n }\n}\n\n/** One engine per app. The camera is a singleton resource; so is this. */\nexport const visualEngine = new VisualEngine();\n\n/** Freshness for a visual read — exported so the domain and host agree. */\nexport function frameQuality(frame: VisualFrame | null): StateQuality {\n if (!frame) return { freshness: { state: 'unknown', observedAt: null, ageMs: null } };\n const ageMs = Date.now() - frame.timestamp;\n return {\n freshness: {\n // Tighter than the other domains on purpose: a two-second-old frame of a\n // moving scene is not \"fresh\" in any useful sense.\n state: ageMs < 2000 ? 'fresh' : ageMs < 10_000 ? 'aging' : 'stale',\n observedAt: frame.timestamp,\n ageMs,\n },\n confidence: { level: 'unknown' },\n };\n}\n","// ── visual ───────────────────────────────────────────────────────────────\n// frames ✓ heading ✓ freshness ✓ blur/exposure ✗\n//\n// Visual owns the semantic frame timeline/retention: the rolling ring, opaque\n// frame ids, and the freshness the frames carry (frameQuality) — see\n// visualEngine.ts. Opaque ids only above L1; pixels never cross to JS.\nimport type { DeviceState, VisualSnapshot } from '@ateam-ai-mobile/plugin-runtime';\nimport { visualEngine, frameQuality } from '../visualEngine';\nimport { currentHeading, startHeading, stopHeading } from '../headingSource';\nimport { unsupported } from '../quality';\nimport { stateEvent } from '../events';\n\n/**\n * The camera block of a visual snapshot — one builder, so the read and the\n * change event cannot describe the camera two ways.\n */\nfunction cameraState(): VisualSnapshot['camera'] {\n const session = visualEngine.getSession();\n return {\n // `available` = the app could use a camera at all. `active` = one is\n // running RIGHT NOW. They differ exactly when it matters: a pocketed\n // phone is available but not active, and answering `true` to both\n // would let a guide speak about a scene nothing is watching.\n available: !!session,\n active: visualEngine.isCameraReady(),\n facing: session ? session.facing : 'unknown',\n };\n}\n\n/**\n * Reads the rolling timeline the engine maintains. See visualEngine.ts for why\n * the capture half lives in a mounted component.\n *\n * `getSnapshot` NEVER opens the camera and never takes a picture — that is the\n * design's explicit performance rule, and the reason a guidance loop can ask\n * every turn without paying for it. What it costs is one array read.\n */\nexport function createVisualDomain(): DeviceState['visual'] {\n return {\n async getSnapshot(): Promise<VisualSnapshot> {\n const latest = visualEngine.latest();\n const timeline = visualEngine.timeline();\n\n return {\n camera: cameraState(),\n // null, not a stale frame dressed as current. A caller with no usable\n // frame must see nothing rather than something old.\n latest: latest as any,\n recentTimeline: timeline as any,\n quality: frameQuality(latest),\n };\n },\n\n async start(options) {\n const facing = options?.facing ?? 'back';\n if (facing !== 'front' && facing !== 'back') {\n // Refused, never quietly swapped for the other camera.\n throw unsupported(`visual.start facing \"${String(facing)}\" — use \"front\" or \"back\"`);\n }\n const s = visualEngine.startSession((options?.intent as any) || 'navigation', facing);\n // Warm the compass alongside the camera. Started here rather than at app\n // launch: a magnetometer subscription running all day for a feature\n // nobody opened is exactly the kind of cost this layer is meant to avoid.\n visualEngine.setContextProvider({ heading: currentHeading });\n void startHeading('visual');\n return { id: s.id, intent: s.intent, startedAt: s.startedAt } as any;\n },\n\n async stop() {\n visualEngine.stopSession();\n visualEngine.setContextProvider(null);\n // Releases only visual's hold — a spatial session keeps the compass.\n stopHeading('visual');\n },\n\n /**\n * A deliberate one-shot. Distinct from getSnapshot because it is an\n * OPERATION: it waits for a frame rather than serving the newest one.\n *\n * Throws when it cannot, rather than returning an empty result — the\n * design is explicit that capture is an operation and reads are state.\n */\n async capture() {\n if (!visualEngine.getSession()) {\n throw unsupported('visual.capture without an open session — call start({ intent }) first');\n }\n if (!visualEngine.isCameraReady()) {\n const err: any = new Error(\n 'The camera is not running right now — the app may be in the background.',\n );\n err.code = 'TEMPORARILY_UNAVAILABLE';\n throw err;\n }\n // Wait briefly for the NEXT frame rather than returning the last one:\n // \"capture\" means now, and the newest ring entry may be a second old.\n const before = visualEngine.latest()?.timestamp ?? 0;\n const deadline = Date.now() + 4000;\n while (Date.now() < deadline) {\n const f = visualEngine.latest();\n if (f && f.timestamp > before) return { frame: f } as any;\n await new Promise(r => setTimeout(r, 100));\n }\n const err: any = new Error('No frame arrived within 4s.');\n err.code = 'TIMEOUT';\n throw err;\n },\n\n /**\n * Fires when the CAMERA block changes — a session opening or closing, the\n * camera coming up or going down. The engine notifies on every frame too,\n * and forwarding that made this a raw frame stream, which the freeze's core\n * rule 4 forbids. A frame arriving is what `getSnapshot()` is for.\n */\n subscribe(listener) {\n let last = JSON.stringify(cameraState());\n const off = visualEngine.subscribe(() => {\n const camera = cameraState();\n const key = JSON.stringify(camera);\n if (key === last) return;\n last = key;\n listener(stateEvent('visual', 'visualStateChanged', { camera }));\n });\n return { unsubscribe: off };\n },\n };\n}\n","/**\n * The Activity engine — steps and motion, sampled into short windows.\n *\n * Sensors only. Every judgement made from them lives in `activityClassifier`,\n * which is pure and tested; this file's whole job is to keep two subscriptions\n * alive and turn their streams into the three numbers the classifier wants.\n *\n * IT IS SESSION-SCOPED, and that is a power decision rather than a stylistic\n * one. An accelerometer left running is a background drain and a pedometer\n * subscription is a permission-visible one, so neither starts until a plugin\n * declares it needs Activity. `getSnapshot()` with no session open returns an\n * explicit unknown rather than quietly switching the sensors on — a read must\n * not have that side effect.\n */\nimport type { HumanActivityValue } from './activityClassifier';\n\n/**\n * expo-sensors is loaded LAZILY, on the first `start()`, and never at import.\n *\n * This module is reachable from app startup, and expo-sensors is a NATIVE\n * module: an app binary built before it was added does not contain it. A\n * top-level import that threw during evaluation would not degrade the Activity\n * domain — it would fail the bundle and black-screen the whole app, on exactly\n * the builds that predate this change. So the failure is contained here, where\n * it can be reported as \"no sensors\" instead.\n */\nlet sensors: any = null;\nlet sensorsMissing = false;\n\nfunction loadSensors(): any {\n if (sensors || sensorsMissing) return sensors;\n try {\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n sensors = require('expo-sensors');\n } catch {\n sensorsMissing = true;\n sensors = null;\n }\n return sensors;\n}\n\n/** Accelerometer cadence. 5 Hz is plenty for pose and for a variance band. */\nconst ACCEL_INTERVAL_MS = 200;\n/** Motion window. Long enough to smooth a stride, short enough to be current. */\nconst MOTION_WINDOW_MS = 3000;\n/**\n * Step window.\n *\n * 20s, because cadence over a shorter window swings wildly at the start and end\n * of walking — someone who takes four steps and stops would otherwise read as\n * a steady 120 spm for a moment.\n */\nconst STEP_WINDOW_MS = 20_000;\n/** Low-pass coefficient isolating gravity from the raw accelerometer. */\nconst GRAVITY_ALPHA = 0.1;\n\ninterface StepSample { steps: number; t: number }\n\nclass ActivityEngine {\n private accelSub: { remove(): void } | null = null;\n private stepSub: { remove(): void } | null = null;\n\n private magnitudes: Array<{ m: number; t: number }> = [];\n private gravity: { x: number; y: number; z: number } | null = null;\n private steps: StepSample[] = [];\n\n private sessionId: string | null = null;\n private startedAt = 0;\n /** When the CURRENT reading last changed — feeds `human.since`. */\n private pedometerAvailable: boolean | null = null;\n\n isRunning(): boolean { return this.sessionId !== null; }\n session(): { id: string; startedAt: number } | null {\n return this.sessionId ? { id: this.sessionId, startedAt: this.startedAt } : null;\n }\n\n /**\n * @param classify how to name the current activity — injected so the engine\n * keeps no opinion of its own and the classifier stays pure and testable.\n */\n async start(classify: () => HumanActivityValue): Promise<{ id: string; startedAt: number }> {\n if (this.sessionId) return this.session()!;\n this.sessionId = `activity_${Date.now()}`;\n this.startedAt = Date.now();\n\n const s = loadSensors();\n if (!s) {\n // An app binary built before expo-sensors was added. The session is open\n // — stopping it would be a lie in the other direction — but nothing\n // samples, so every window stays empty and the classifier says `unknown`.\n this.pedometerAvailable = false;\n this.startTicking(classify);\n return this.session()!;\n }\n const { Accelerometer, Pedometer } = s;\n\n Accelerometer.setUpdateInterval(ACCEL_INTERVAL_MS);\n this.accelSub = Accelerometer.addListener(({ x, y, z }: { x: number; y: number; z: number }) => {\n const now = Date.now();\n // Raw magnitude includes gravity, so a still phone sits at ~1g and the\n // variance of this series is the motion signal.\n this.magnitudes.push({ m: Math.sqrt(x * x + y * y + z * z), t: now });\n const cutoff = now - MOTION_WINDOW_MS;\n if (this.magnitudes.length > 8 && this.magnitudes[0].t < cutoff) {\n this.magnitudes = this.magnitudes.filter(s => s.t >= cutoff);\n }\n // Gravity by low-pass: what survives smoothing is the constant pull, and\n // that is what says which way up the phone is.\n this.gravity = this.gravity\n ? {\n x: this.gravity.x + GRAVITY_ALPHA * (x - this.gravity.x),\n y: this.gravity.y + GRAVITY_ALPHA * (y - this.gravity.y),\n z: this.gravity.z + GRAVITY_ALPHA * (z - this.gravity.z),\n }\n : { x, y, z };\n });\n\n try {\n this.pedometerAvailable = await Pedometer.isAvailableAsync();\n } catch {\n this.pedometerAvailable = false;\n }\n if (this.pedometerAvailable) {\n try {\n this.stepSub = Pedometer.watchStepCount(({ steps }: { steps: number }) => {\n this.steps.push({ steps, t: Date.now() });\n const cutoff = Date.now() - STEP_WINDOW_MS;\n if (this.steps.length > 2 && this.steps[0].t < cutoff) {\n this.steps = this.steps.filter(s => s.t >= cutoff);\n }\n });\n } catch {\n // Permission refused, most likely (iOS Motion & Fitness). Cadence stays\n // null, the classifier says `unknown`, and nothing pretends otherwise.\n this.stepSub = null;\n this.pedometerAvailable = false;\n }\n }\n\n this.startTicking(classify);\n return this.session()!;\n }\n\n stop(): void {\n this.accelSub?.remove();\n this.stepSub?.remove();\n this.accelSub = null;\n this.stepSub = null;\n this.sessionId = null;\n this.stopTicking();\n // The windows belong to the session. Keeping them would let a later read\n // report a walk that finished before the phone went in a drawer.\n this.magnitudes = [];\n this.steps = [];\n this.gravity = null;\n }\n\n /** Variance of accelerometer magnitude over the motion window, in g². */\n accelVariance(): number | null {\n const cutoff = Date.now() - MOTION_WINDOW_MS;\n const window = this.magnitudes.filter(s => s.t >= cutoff);\n // Two samples is not a variance; it is a coincidence.\n if (window.length < 5) return null;\n const mean = window.reduce((a, s) => a + s.m, 0) / window.length;\n return window.reduce((a, s) => a + (s.m - mean) ** 2, 0) / window.length;\n }\n\n currentGravity(): { x: number; y: number; z: number } | null { return this.gravity; }\n\n /**\n * Steps per minute over the step window.\n *\n * Computed from the DIFFERENCE between the first and last cumulative counts,\n * so it does not matter whether the platform counts from subscription start\n * or from midnight.\n *\n * Returns null — not 0 — when the window is too short to divide by. Zero\n * steps per minute is a real claim (\"standing still\") and must only be made\n * once there is a window long enough to support it.\n */\n cadenceSpm(): number | null {\n const cutoff = Date.now() - STEP_WINDOW_MS;\n const window = this.steps.filter(s => s.t >= cutoff);\n if (window.length < 2) {\n // The pedometer is alive and has simply reported nothing recently, which\n // is what standing still looks like: no callbacks at all.\n if (this.pedometerAvailable && Date.now() - this.startedAt > STEP_WINDOW_MS) return 0;\n return null;\n }\n const first = window[0], last = window[window.length - 1];\n const minutes = (last.t - first.t) / 60_000;\n if (minutes <= 0) return null;\n return (last.steps - first.steps) / minutes;\n }\n\n /** Whether a pedometer exists and is permitted. Null before `start()`. */\n hasPedometer(): boolean | null { return this.pedometerAvailable; }\n\n /** When the newest sample arrived — what freshness is actually about. */\n observedAt(): number | null {\n const ts = [\n this.magnitudes[this.magnitudes.length - 1]?.t,\n this.steps[this.steps.length - 1]?.t,\n ].filter((t): t is number => typeof t === 'number');\n return ts.length ? Math.max(...ts) : null;\n }\n\n // ── transitions ──\n //\n // Tracked on a timer inside the engine rather than computed when someone\n // reads, because a transition is an EVENT: \"you started walking\" happens\n // between two reads, and a domain that only classifies on demand reports the\n // state and loses the moment it changed. `since` has the same problem.\n\n private current: HumanActivityValue = 'unknown';\n private since: number | null = null;\n private transitions: Array<{ name: string; timestamp: number }> = [];\n private ticker: ReturnType<typeof setInterval> | null = null;\n\n /** Feed the engine its own classification once a second. */\n private startTicking(classify: () => HumanActivityValue): void {\n this.ticker = setInterval(() => {\n const next = classify();\n if (next === this.current) return;\n this.current = next;\n this.since = Date.now();\n // The transition NAME reuses the frozen HumanActivity vocabulary — it is\n // the state being entered. The freeze leaves transition names as bare\n // strings with no controlled vocabulary, and inventing one (\"walk_start\")\n // would put a word in plugins that the freeze may later contradict.\n this.transitions = [...this.transitions, { name: next, timestamp: this.since }].slice(-10);\n }, 1000);\n }\n\n private stopTicking(): void {\n if (this.ticker) clearInterval(this.ticker);\n this.ticker = null;\n this.current = 'unknown';\n this.since = null;\n this.transitions = [];\n }\n\n /** When the current activity began, or null if it has not been established. */\n currentSince(): number | null { return this.since; }\n /** Newest last, bounded. Empty is a real answer: nothing has changed yet. */\n recentTransitions(): Array<{ name: string; timestamp: number }> {\n return [...this.transitions];\n }\n}\n\n/** One engine per app: the sensors are singleton hardware. */\nexport const activityEngine = new ActivityEngine();\n","/**\n * What the user is doing, and how the phone is being held — as a pure function.\n *\n * Separated from the sensor plumbing so the JUDGEMENTS are testable: every\n * threshold below is a claim about human beings, and a claim that decides\n * whether a guide says \"you have arrived\" to someone sitting in a moving car\n * deserves a test rather than a comment.\n *\n * WHY THIS DOMAIN EXISTS AT ALL WHEN SPATIAL ALREADY REPORTS MOVEMENT: GPS is\n * what fails indoors, and indoors is where a guide most needs to know whether\n * the user is walking. Steps come from the pedometer, which does not care about\n * a roof. Spatial answers \"are you moving through the world\"; Activity answers\n * \"are you walking\" — in a mall those are different questions with different\n * sensors, and only one of them still works.\n */\nimport type { Confidence } from '@ateam-ai-mobile/plugin-runtime';\n\nexport type HumanActivityValue =\n | 'stationary' | 'walking' | 'running' | 'cycling' | 'driving' | 'unknown';\nexport type PlacementValue = 'handheld' | 'pocket' | 'table' | 'mounted' | 'unknown';\nexport type PoseOrientation = 'portrait' | 'landscape' | 'flat' | 'unknown';\n\nexport interface ActivityInput {\n /** Steps per minute over the recent window. `null` = no pedometer reading. */\n cadenceSpm: number | null;\n /**\n * Variance of accelerometer magnitude (g²) over the recent window.\n * `null` = the accelerometer has not reported yet.\n */\n accelVariance: number | null;\n /** Low-pass gravity estimate in g. `null` = not sampled yet. */\n gravity: { x: number; y: number; z: number } | null;\n /**\n * Speed from Spatial, when a spatial session is streaming.\n *\n * Passed IN rather than read here, so this stays pure. It exists for exactly\n * one job: a phone in a moving car looks identical to a phone on a desk —\n * no steps, little vibration — and calling that `stationary` would tell a\n * guide the user has arrived while they are doing 50 km/h.\n */\n speedMps: number | null;\n}\n\nexport interface ActivityVerdict {\n human: { state: HumanActivityValue; confidence: Confidence };\n pose: { placement: PlacementValue; orientation: PoseOrientation; confidence: Confidence };\n}\n\n/**\n * Cadence bands, in steps per minute.\n *\n * Ordinary walking sits around 100–120 spm and running around 160–180. 140 is\n * placed in the gap rather than at either edge: misreading a fast walk as a run\n * is harmless, but the reverse would have a guide give walking directions to\n * someone covering ground twice as fast.\n *\n * 20 spm is the floor for \"walking at all\" — below that it is a few steps of\n * shuffling, which is not travel.\n */\nconst RUN_SPM = 140;\nconst WALK_SPM = 20;\n\n/**\n * Accelerometer magnitude variance, in g².\n *\n * A phone lying on a table reads a near-constant 1g, so its variance is\n * essentially sensor noise. A hand holding a phone never manages that.\n */\nconst STILL_VAR = 0.002;\n/** Above this the phone is being moved about, not merely held. */\nconst ACTIVE_VAR = 0.02;\n\n/**\n * Speed at which the user is certainly not walking, in m/s.\n *\n * 3 m/s is ~11 km/h — above a fast walk (~2 m/s) and below anything a vehicle\n * does in traffic.\n */\nconst VEHICLE_MPS = 3;\n\n/** Gravity component that has to dominate before an axis names the pose. */\nconst AXIS_DOMINANT = 0.75;\n\nexport function classifyActivity(input: ActivityInput): ActivityVerdict {\n return { human: classifyHuman(input), pose: classifyPose(input) };\n}\n\nfunction classifyHuman({ cadenceSpm, accelVariance, speedMps }: ActivityInput): ActivityVerdict['human'] {\n // Steps are the strongest evidence there is, and the only one that survives\n // indoors. Take them first when they exist.\n if (cadenceSpm !== null && cadenceSpm >= RUN_SPM) {\n return { state: 'running', confidence: { level: 'high' } };\n }\n if (cadenceSpm !== null && cadenceSpm >= WALK_SPM) {\n return { state: 'walking', confidence: { level: 'high' } };\n }\n\n // No meaningful stepping. Before calling it stationary, rule out a vehicle:\n // riding is not walking, and it is not standing still either.\n if (speedMps !== null && speedMps >= VEHICLE_MPS) {\n // Moving fast without stepping. Which vehicle it is cannot be told from an\n // accelerometer and a pedometer — separating cycling from driving needs\n // real activity recognition (CMMotionActivity), which is not available\n // here. `unknown` with the speed already published by Spatial is honest;\n // guessing `driving` because it is the commoner case is not.\n return { state: 'unknown', confidence: { level: 'low' } };\n }\n\n if (accelVariance !== null && accelVariance < STILL_VAR && (cadenceSpm === null || cadenceSpm < WALK_SPM)) {\n // Genuinely still. Medium rather than high: a phone resting in a\n // stationary car at a red light produces exactly this, and Spatial's speed\n // cannot separate them either while the car is stopped.\n return { state: 'stationary', confidence: { level: 'medium' } };\n }\n\n // Something is moving but nothing identifies it — a phone being handled\n // while its owner stands still, most often. Naming an activity here would be\n // a guess dressed as a reading.\n return { state: 'unknown', confidence: { level: 'unknown' } };\n}\n\nfunction classifyPose({ gravity, accelVariance }: ActivityInput): ActivityVerdict['pose'] {\n if (!gravity) {\n return { placement: 'unknown', orientation: 'unknown', confidence: { level: 'unknown' } };\n }\n\n const { x, y, z } = gravity;\n const orientation: PoseOrientation =\n Math.abs(z) >= AXIS_DOMINANT ? 'flat'\n : Math.abs(y) >= AXIS_DOMINANT ? 'portrait'\n : Math.abs(x) >= AXIS_DOMINANT ? 'landscape'\n : 'unknown';\n\n // Flat and utterly still is a surface. This is the one placement the sensors\n // can actually establish.\n if (orientation === 'flat' && accelVariance !== null && accelVariance < STILL_VAR) {\n return { placement: 'table', orientation, confidence: { level: 'high' } };\n }\n\n // Upright and moving a little is a hand. Medium, because a phone in a shirt\n // pocket looks much the same and this cannot tell them apart.\n if (orientation !== 'flat' && accelVariance !== null && accelVariance >= ACTIVE_VAR) {\n return { placement: 'handheld', orientation, confidence: { level: 'medium' } };\n }\n\n // `pocket` and `mounted` are NEVER returned. Distinguishing a pocket needs\n // the proximity or light sensor, and a car mount needs Bluetooth or CarPlay\n // context — none of which is wired. The orientation is still reported,\n // because it was actually measured.\n return { placement: 'unknown', orientation, confidence: { level: 'unknown' } };\n}\n","// ── activity ─────────────────────────────────────────────────────────────\n// THE INDOOR MOVEMENT SIGNAL (Spatial stops working indoors).\nimport type { DeviceState, ActivitySnapshot } from '@ateam-ai-mobile/plugin-runtime';\nimport { activityEngine } from '../activityEngine';\nimport { classifyActivity, type ActivityInput } from '../activityClassifier';\nimport type { SpeedRef } from '../collectors';\nimport { qualityFrom, qualityUnknown, unsupported } from '../quality';\n\n/**\n * What the user is doing, and how the phone is being held.\n *\n * THIS IS THE INDOOR MOVEMENT SIGNAL. Spatial answers \"are you moving through\n * the world\" and stops being able to indoors, which is exactly where a guide\n * most needs to know whether its user is walking. The pedometer does not care\n * about a roof.\n *\n * SESSION-SCOPED, unlike the passive domains. An accelerometer left running is\n * a background drain and a pedometer subscription is permission-visible on\n * iOS, so nothing samples until a plugin says it needs this. A `getSnapshot()`\n * with no session open returns an explicit unknown rather than switching the\n * sensors on — a read must not have that side effect, and one that did would\n * also return junk, since the windows would be empty at the moment it started.\n */\nexport function createActivityDomain(speedRef: SpeedRef): DeviceState['activity'] {\n /** A speed older than this says nothing about now. */\n const SPEED_MAX_AGE_MS = 15_000;\n\n const input = (): ActivityInput => ({\n cadenceSpm: activityEngine.cadenceSpm(),\n accelVariance: activityEngine.accelVariance(),\n gravity: activityEngine.currentGravity(),\n speedMps:\n speedRef.at && Date.now() - speedRef.at < SPEED_MAX_AGE_MS ? speedRef.mps : null,\n });\n\n return {\n async getSnapshot(): Promise<ActivitySnapshot> {\n if (!activityEngine.isRunning()) {\n // Nothing is sampling. Every field says so rather than describing a\n // motionless phone, which is what an unstarted engine would look like.\n return {\n human: { state: 'unknown', since: null, confidence: { level: 'unknown' } },\n devicePose: { placement: 'unknown', orientation: 'unknown', confidence: { level: 'unknown' } },\n recentTransitions: [],\n quality: qualityUnknown(),\n };\n }\n\n const verdict = classifyActivity(input());\n const observedAt = activityEngine.observedAt();\n const transitions = activityEngine.recentTransitions();\n\n return {\n human: {\n state: verdict.human.state,\n since: activityEngine.currentSince(),\n confidence: verdict.human.confidence,\n },\n devicePose: {\n placement: verdict.pose.placement,\n orientation: verdict.pose.orientation,\n confidence: verdict.pose.confidence,\n },\n transition: {\n current: verdict.human.state,\n // A hint, and we have no basis for one. The freeze calls `likelyNext`\n // advisory; advisory is not licence to invent it.\n likelyNext: null,\n },\n recentTransitions: transitions,\n quality: observedAt === null\n ? qualityUnknown()\n : qualityFrom(observedAt, verdict.human.confidence),\n };\n },\n\n /**\n * Start sampling. The intent is recorded but does not yet change strategy —\n * there is no native power-tuning layer for it to reach.\n */\n async start(options) {\n const s = await activityEngine.start(() => classifyActivity(input()).human.state);\n return { id: s.id, intent: options.intent, startedAt: s.startedAt } as any;\n },\n\n async stop() { activityEngine.stop(); },\n\n // No activity event vocabulary is frozen — the design notes there is no\n // \"Initial Activity Events\" list at all — so naming events here would put\n // words in plugins that the freeze may contradict.\n subscribe() { throw unsupported('activity.subscribe'); },\n };\n}\n","/**\n * Device Bridge v2 — the runtime behind `props.deviceState`.\n *\n * R5 split the former monolith into one module per domain (./domains/*), a\n * single quality module that owns freshness/confidence/unknown (./quality), and\n * this composition root, which wires the domains and injects their dependencies.\n * The behaviour — and every snapshot shape — is unchanged; only the file layout is.\n *\n * WHAT IS AND IS NOT HERE — stated up front, because a half-built state layer\n * that pretends to be whole is the exact failure this whole effort exists to\n * end:\n *\n * deviceContext power ✓ thermal ✗ memory/storage ✗\n * connectivity reachability ✓ transport ✓ quality/stability/metered ✗\n * capability permission ✓ availability ✗ restriction ✗\n * interaction app lifecycle ✓ screenOn ✗ audio ✗\n * spatial position ✓ heading ✓ turns ✓ path/events ✓ (while a session is open) speed ~derived\n * visual frames ✓ heading ✓ freshness ✓ blur/exposure ✗\n * activity walking/running/stationary ✓ pose ✓ cycling/driving ✗ events ✗\n * environment NOT IMPLEMENTED AT ALL\n *\n * Everything marked ✗ reports `unknown`, which is a real answer in this design\n * and the only honest one. It never reports a plausible default: an invented\n * 'nominal' thermal state is indistinguishable from a measured one, and that is\n * how 56 fabrications reached users from the layer below.\n *\n * A domain with no engine here is not simply missing: `gateDeviceState` (in\n * plugin-runtime/src/deviceStateHost.ts) fills the gap with a domain whose\n * reads answer explicit `unknown`s and whose ACTIONS throw. So a plugin can\n * call any of the eight without an existence check, and still cannot mistake\n * \"not built\" for \"nothing to report\". `environment` is deliberately absent\n * from the composition below for exactly that reason.\n */\nimport type { DeviceState } from '@ateam-ai-mobile/plugin-runtime';\nimport { DeviceStateCollectors, SpeedRef } from './collectors';\nimport { createDeviceContextDomain } from './domains/deviceContext';\nimport { createConnectivityDomain } from './domains/connectivity';\nimport { createCapabilityDomain } from './domains/capability';\nimport { createInteractionDomain } from './domains/interaction';\nimport { createSpatialDomain } from './domains/spatial';\nimport { createVisualDomain } from './domains/visual';\nimport { createActivityDomain } from './domains/activity';\n\nexport type { DeviceStateCollectors, SpeedRef } from './collectors';\n\n/**\n * Build the semantic surface.\n *\n * Only implemented domains appear. `environment` is deliberately absent: a\n * plugin touching it gets a TypeError naming it, which is a far better failure\n * than a snapshot of nulls that reads like a quiet device.\n */\nexport function createDeviceState(collectors: DeviceStateCollectors = {}): Partial<DeviceState> {\n // The ONE explicit cross-domain dependency: Activity needs Spatial's newest\n // speed to tell \"walking\" from \"sitting in a moving car\". Passed as a shared\n // reference, not a cross-domain import — see SpeedRef.\n const speedRef: SpeedRef = { mps: null, at: 0 };\n return {\n deviceContext: createDeviceContextDomain(),\n connectivity: createConnectivityDomain(collectors),\n capability: createCapabilityDomain(collectors),\n interaction: createInteractionDomain(),\n spatial: createSpatialDomain(collectors, speedRef),\n visual: createVisualDomain(),\n activity: createActivityDomain(speedRef),\n };\n}\n\n/** Which domains this build can actually answer. */\nexport const IMPLEMENTED_DOMAINS = [\n 'deviceContext',\n 'connectivity',\n 'capability',\n 'interaction',\n 'spatial',\n 'visual',\n 'activity',\n] as const;\n","/**\n * The mounted half of the camera tasks — scanning and recording.\n *\n * The app renders this ONCE, near the root. It draws nothing until a plugin\n * asks for a scan or a recording; then it takes over the screen with a live\n * preview and settles the plugin's promise when the work is done.\n *\n * WHY IT IS VISIBLE, unlike VisualEngineHost's 1×1 invisible camera. These two\n * tasks need the USER to aim: a barcode has to be framed, a clip has to point\n * at something. An invisible scanner is not a privacy nicety, it is unusable —\n * the user cannot tell what the camera sees or when it has read something.\n *\n * WHY IT CAN ALWAYS BE CANCELLED. The camera is on and pointed at whatever is\n * in front of the person holding it. A capture surface with no way out is a\n * trap; the cancel path settles the promise with a distinct CANCELLED code so\n * the caller can tell \"they chose not to\" from \"it broke\".\n */\nimport React, { useEffect, useRef, useState } from 'react';\nimport { Pressable, StyleSheet, Text, View } from 'react-native';\nimport { CameraView, useCameraPermissions, useMicrophonePermissions } from 'expo-camera';\n\nimport { cameraTasks, cameraError, type CameraTask } from './cameraTasks';\n\n/** How long a scan waits before giving up. */\nconst SCAN_TIMEOUT_MS = 30_000;\n/** Ceiling on a single clip, so a forgotten recording cannot fill the disk. */\nconst MAX_RECORD_MS = 60_000;\n\nexport function CameraTaskHost(): React.ReactElement | null {\n const [task, setTask] = useState<CameraTask | null>(cameraTasks.current());\n const [camPermission, requestCamPermission] = useCameraPermissions();\n const [micPermission, requestMicPermission] = useMicrophonePermissions();\n const cameraRef = useRef<CameraView | null>(null);\n /** Guards against a barcode firing twice before the task is cleared. */\n const settling = useRef(false);\n\n // Announce that a surface exists. Without this the broker rejects every\n // request immediately, which is the correct behaviour when no host is\n // mounted — a plugin must never await a promise nothing can settle.\n useEffect(() => {\n cameraTasks.setHostMounted(true);\n return () => cameraTasks.setHostMounted(false);\n }, []);\n\n useEffect(() => cameraTasks.subscribe(() => {\n settling.current = false;\n setTask(cameraTasks.current());\n }), []);\n\n // Ask for permission when a task arrives, not at app start: a camera prompt\n // with no visible reason attached is one users refuse.\n useEffect(() => {\n if (!task) return;\n let cancelled = false;\n void (async () => {\n const cam = camPermission?.granted ? camPermission : await requestCamPermission();\n if (cancelled) return;\n if (!cam?.granted) {\n cameraTasks.fail(cameraError(\n 'PERMISSION_DENIED',\n 'Camera permission was not granted. Enable it for this app in Settings > Privacy > Camera.',\n ));\n return;\n }\n if (task.kind === 'record' && task.opts.withAudio) {\n const mic = micPermission?.granted ? micPermission : await requestMicPermission();\n if (cancelled) return;\n if (!mic?.granted) {\n // Refuse rather than silently recording a silent video — a caller\n // that asked for audio and got none has no way to find out.\n cameraTasks.fail(cameraError(\n 'PERMISSION_DENIED',\n 'Microphone permission was not granted, so a video with sound cannot be recorded.',\n ));\n }\n }\n })();\n return () => { cancelled = true; };\n }, [task]);\n\n // Scan timeout. A scan that never finds a code must END, and say so: leaving\n // the camera up indefinitely is both a battery drain and a promise that\n // never settles.\n useEffect(() => {\n if (task?.kind !== 'scan') return;\n const ms = task.opts.timeoutMs ?? SCAN_TIMEOUT_MS;\n const t = setTimeout(() => {\n cameraTasks.fail(cameraError('TIMEOUT', `No barcode was found within ${Math.round(ms / 1000)}s.`));\n }, ms);\n return () => clearTimeout(t);\n }, [task]);\n\n // Recording runs for its duration and resolves with the file.\n useEffect(() => {\n if (task?.kind !== 'record' || !camPermission?.granted) return;\n let cancelled = false;\n const maxMs = Math.min(task.opts.maxDurationMs ?? 15_000, MAX_RECORD_MS);\n const startedAt = Date.now();\n\n void (async () => {\n // A frame late is better than a crash: the ref is attached on the render\n // after the task arrives.\n await new Promise(r => setTimeout(r, 50));\n if (cancelled || !cameraRef.current) return;\n try {\n const video = await cameraRef.current.recordAsync({ maxDuration: Math.ceil(maxMs / 1000) });\n if (cancelled) return;\n if (!video?.uri) {\n cameraTasks.fail(cameraError('INTERNAL', 'Recording finished but produced no file.'));\n return;\n }\n cameraTasks.complete({ uri: video.uri, durationMs: Date.now() - startedAt });\n } catch (err: any) {\n if (!cancelled) cameraTasks.fail(cameraError('INTERNAL', err?.message || 'Recording failed.'));\n }\n })();\n\n return () => {\n cancelled = true;\n // Stop the hardware if the surface goes away mid-take.\n try { cameraRef.current?.stopRecording(); } catch { /* already stopped */ }\n };\n }, [task, camPermission?.granted]);\n\n if (!task || !camPermission?.granted) return null;\n\n const isScan = task.kind === 'scan';\n\n return (\n <View style={styles.overlay}>\n <CameraView\n ref={cameraRef}\n style={StyleSheet.absoluteFill}\n facing=\"back\"\n mode={isScan ? 'picture' : 'video'}\n barcodeScannerSettings={\n isScan && task.opts.formats?.length\n ? { barcodeTypes: task.opts.formats as any }\n : undefined\n }\n onBarcodeScanned={\n isScan\n ? ({ data, type }) => {\n // onBarcodeScanned fires continuously while a code is in frame.\n // Without this guard the same scan resolves, then tries to\n // resolve an already-cleared task on the very next frame.\n if (settling.current) return;\n settling.current = true;\n cameraTasks.complete({ data, format: type });\n }\n : undefined\n }\n />\n\n <View style={styles.hud} pointerEvents=\"box-none\">\n <Text style={styles.hint}>\n {isScan ? 'Point the camera at the code' : 'Recording…'}\n </Text>\n <Pressable style={styles.cancel} onPress={() => cameraTasks.cancel()} accessibilityRole=\"button\">\n <Text style={styles.cancelText}>Cancel</Text>\n </Pressable>\n </View>\n </View>\n );\n}\n\nconst styles = StyleSheet.create({\n overlay: { ...StyleSheet.absoluteFillObject, backgroundColor: '#000', zIndex: 9999 },\n hud: { flex: 1, justifyContent: 'space-between', paddingVertical: 64, alignItems: 'center' },\n hint: { color: '#fff', fontSize: 16, textAlign: 'center', paddingHorizontal: 24 },\n cancel: { paddingHorizontal: 28, paddingVertical: 14, borderRadius: 24, backgroundColor: 'rgba(255,255,255,0.18)' },\n cancelText: { color: '#fff', fontSize: 16, fontWeight: '600' },\n});\n\nexport default CameraTaskHost;\n","/**\n * Replay — run guidance logic against a RECORDED WALK instead of a phone.\n *\n * WHY THIS EXISTS. Everything device-shaped is verifiable only on real\n * hardware: the simulator has no camera, no pedometer, no moving GPS. So the\n * loop was write → ask for a build → wait → ask someone to walk around the\n * block. We could unit-test that the window clamp picks the smaller number; we\n * could not test that THE GUIDE SAYS \"TURN RIGHT\" AT THE CORNER, which is the\n * only thing a user cares about.\n *\n * A fixture is one real walk, recorded once by the app (see\n * docs/SPEC_device_walk_recorder.md) and replayed here forever.\n *\n * IT REPLAYS AT THE SNAPSHOT LEVEL, not the sensor level, and that is a\n * deliberate boundary. The engines already have their own tests — the turn\n * detector, the activity classifier, the window clamp. What had no test was\n * everything ABOVE them. So this implements the same `DeviceState` surface a\n * plugin sees and serves recorded snapshots through it: the code under test is\n * the guidance logic, unchanged, unaware it is not on a phone.\n *\n * THE CLOCK IS VIRTUAL. A ten-minute walk runs in milliseconds, and a test\n * never waits. `t` in a fixture is milliseconds since the recording started,\n * never a wall clock, so a fixture does not rot when the date changes.\n *\n * WHAT IT REFUSES TO SMOOTH OVER: a fixture's `unknown`s, `null`s and refusals\n * are replayed exactly as recorded. They are the most valuable records in the\n * file — they are what a guide must handle correctly, and precisely what a\n * hand-written fixture would never contain.\n *\n * SPATIAL EVENTS REPLAY; THE OTHERS REFUSE. Spatial events are derived from\n * snapshots by the same detector the live engine runs, so feeding it the\n * recorded snapshots in time order gives the events the walk produced — at the\n * virtual time of the sample that completed each one. While a listener runs,\n * the clock stands AT that sample, so a listener that re-reads gets the\n * snapshot its event came from, as it would live. Two limits, inherited from\n * recording snapshots: two turns inside one recorded tick collapse to the later\n * one, because a snapshot carries only the last turn; and a hold completes at\n * the recording's tick rather than the live engine's one-second one.\n */\nimport type { DeviceState, SpatialStateEvent } from '@ateam-ai-mobile/plugin-runtime';\nimport { SpatialEventDetector, spatialEventProfile } from './spatialEvents';\nimport { listenerSet } from './events';\n\n/** One tick of a recorded walk. Domains are stored verbatim, never normalised. */\nexport interface WalkSample {\n /** Milliseconds since recording started. NOT a wall clock. */\n t: number;\n spatial?: any;\n activity?: any;\n visual?: any;\n deviceContext?: any;\n interaction?: any;\n connectivity?: any;\n capability?: any;\n environment?: any;\n}\n\n/**\n * A domain whose `start()` FAILED during recording.\n *\n * Recorded so a replayed walk fails the same way the real one did. Without it,\n * a fixture where the pedometer was denied is indistinguishable from a fixture\n * where activity simply never appeared — and only one of those is a bug.\n */\nexport interface WalkRefusal {\n domain: string;\n /** The error code the real call threw, e.g. RESTRICTED / UNSUPPORTED. */\n code: string;\n message?: string;\n}\n\nexport interface WalkFixture {\n /** Provenance. Without `sdkCommit`, a fixture that stops matching cannot be\n * told apart from a regression. */\n header: {\n recordedAt?: string;\n appVersion?: string;\n sdkCommit?: string;\n device?: string;\n os?: string;\n /** What the visual engine reported it was really holding. */\n retentionMs?: number | null;\n withFrames?: boolean;\n note?: string;\n };\n refusals?: WalkRefusal[];\n /** Ascending by `t`. */\n samples: WalkSample[];\n}\n\n/** A clock a test drives by hand. Nothing here ever waits. */\nexport interface ReplayClock {\n /** Current position in the walk, ms since start. */\n now(): number;\n /** Jump forward. Returns the new position. */\n advance(ms: number): number;\n /** Jump to an absolute offset. */\n seek(ms: number): number;\n /** Run to the end of the fixture. */\n end(): number;\n /** Total length of the recorded walk. */\n durationMs(): number;\n}\n\nexport interface ReplaySession {\n deviceState: DeviceState;\n clock: ReplayClock;\n /** Which domains had a session opened by the code under test. */\n started(): string[];\n}\n\nconst DOMAINS = [\n 'spatial', 'visual', 'activity', 'deviceContext',\n 'connectivity', 'capability', 'environment', 'interaction',\n] as const;\n\n/** The snapshot a domain returns when the fixture has nothing for it. */\nfunction unknownSnapshot(domain: string): any {\n const quality = { freshness: { state: 'unknown', observedAt: null, ageMs: null }, confidence: { level: 'unknown' } };\n switch (domain) {\n case 'spatial':\n return { ...{ quality }, position: null, movement: { speedMps: null, bearingDeg: null, distanceRecentM: null, state: 'unknown' }, orientation: { headingDeg: null }, recentPath: [] };\n case 'visual':\n return { quality, camera: { available: false, active: false, facing: 'unknown' }, latest: null, recentTimeline: { windowMs: 0, frames: [] } };\n case 'activity':\n return { quality, human: { state: 'unknown', since: null, confidence: { level: 'unknown' } }, devicePose: { placement: 'unknown', orientation: 'unknown', confidence: { level: 'unknown' } }, recentTransitions: [] };\n default:\n return { quality };\n }\n}\n\nfunction refusal(domain: string, code: string, message?: string): Error {\n const err: any = new Error(message || `deviceState.${domain}: refused during recording (${code}).`);\n err.code = code;\n err.domain = domain;\n return err;\n}\n\n/**\n * Build a `DeviceState` backed by a recorded walk.\n *\n * Hand the result to the code under test exactly as the host would hand it the\n * real one — it cannot tell the difference, which is the point.\n */\nexport function createReplayDeviceState(fixture: WalkFixture): ReplaySession {\n const samples = [...(fixture.samples || [])].sort((a, b) => a.t - b.t);\n const duration = samples.length ? samples[samples.length - 1].t : 0;\n const refusals = new Map((fixture.refusals || []).map(r => [r.domain, r]));\n const openSessions = new Set<string>();\n\n let cursor = 0;\n\n // Spatial events: the live engine's detector, fed recorded snapshots.\n const spatialDetector = new SpatialEventDetector();\n const spatialListeners = listenerSet<SpatialStateEvent>();\n\n /**\n * Move the cursor, feeding every spatial sample crossed on the way to the\n * detector, oldest first, with the sample's own `t` as \"now\". Only while a\n * spatial session is open — the live engine has no event feed without one.\n *\n * The cursor steps THROUGH each fed sample rather than jumping to the target\n * first: an event and a read taken inside its listener must describe the same\n * instant, or the event says \"you turned\" while the read still shows the\n * sample before the corner.\n *\n * Backwards is not a walk: the detector forgets and says nothing, and the\n * next step forward starts from the new position as a fresh session would.\n */\n const moveTo = (target: number): number => {\n const next = Math.min(duration, Math.max(0, target));\n if (!openSessions.has('spatial')) return (cursor = next);\n if (next < cursor) {\n cursor = next;\n spatialDetector.reset(undefined, turnOnRecordAt());\n return cursor;\n }\n const from = cursor;\n for (const s of samples) {\n if (s.t <= from || !s.spatial) continue;\n if (s.t > next) break;\n cursor = s.t;\n for (const event of spatialDetector.push(s.spatial, s.t)) spatialListeners.emit(event);\n }\n return (cursor = next);\n };\n\n const clock: ReplayClock = {\n now: () => cursor,\n advance: ms => moveTo(cursor + ms),\n seek: ms => moveTo(ms),\n end: () => moveTo(duration),\n durationMs: () => duration,\n };\n\n /**\n * The newest sample AT OR BEFORE the cursor.\n *\n * Never the nearest, and never interpolated. A guide reads what the device\n * had ALREADY produced at that instant; letting a test see a value from\n * slightly in the future would make it pass on information the real code\n * could not have had.\n */\n const sampleAt = (): WalkSample | null => {\n let found: WalkSample | null = null;\n for (const s of samples) {\n if (s.t > cursor) break;\n found = s;\n }\n return found;\n };\n\n /**\n * The turn the recording already holds at the cursor. Taken from the sample,\n * never compared with the cursor: a recording stamps turns in the phone's\n * epoch ms, while `t` is ms since the recording started.\n */\n const turnOnRecordAt = (): number | null => sampleAt()?.spatial?.changes?.timestamp ?? null;\n\n const domainFor = (name: string): any => ({\n async getSnapshot() {\n const s = sampleAt();\n const recorded = s ? (s as any)[name] : undefined;\n // Nothing recorded is an honest unknown, not an empty object — the same\n // answer the real gate gives for a domain it cannot serve.\n return recorded ?? unknownSnapshot(name);\n },\n async start(options?: { intent?: string }) {\n const r = refusals.get(name);\n // Replay the refusal the real walk hit. A fixture recorded with the\n // pedometer denied must fail here, or a test proves the guide works in\n // conditions the user never had.\n if (r) throw refusal(name, r.code, r.message);\n // A session opened at `t` hears samples recorded AFTER `t`: the reading\n // at `t` already existed before it asked, as a live fix would have.\n if (name === 'spatial' && !openSessions.has(name)) {\n spatialDetector.reset(spatialEventProfile(options?.intent), turnOnRecordAt());\n }\n openSessions.add(name);\n return { id: `replay_${name}`, intent: 'replay', startedAt: 0 } as any;\n },\n async stop() {\n openSessions.delete(name);\n if (name === 'spatial') spatialDetector.reset();\n },\n subscribe(listener: (event: SpatialStateEvent) => void) {\n // Spatial events are derived from snapshots, and the snapshots are what\n // this file replays — so they fire as the clock crosses each sample.\n if (name === 'spatial') return spatialListeners.add(listener);\n // The rest are deliberately unsupported: their events come from engine\n // callbacks a fixture does not record, and a subscription that silently\n // never fires reads as \"nothing has changed\" — the failure this whole\n // layer exists to prevent. Step the clock and read instead.\n throw refusal(name, 'UNSUPPORTED', `deviceState.${name}.subscribe is not replayable — advance the clock and read.`);\n },\n });\n\n const deviceState: any = {};\n for (const d of DOMAINS) deviceState[d] = domainFor(d);\n\n return { deviceState: deviceState as DeviceState, clock, started: () => [...openSessions] };\n}\n\n/** Parse a `.jsonl` recording into a fixture. First line is the header. */\nexport function parseWalkJsonl(text: string): WalkFixture {\n const lines = text.split('\\n').map(l => l.trim()).filter(Boolean);\n if (!lines.length) throw new Error('Empty recording — no header line.');\n\n const first = JSON.parse(lines[0]);\n const header = first.header ?? first;\n const refusals: WalkRefusal[] = first.refusals ?? [];\n const samples: WalkSample[] = [];\n\n for (const line of lines.slice(1)) {\n const rec = JSON.parse(line);\n // Refusals may arrive as their own records mid-file (a session that failed\n // partway through), not only in the header.\n if (rec.refusal) { refusals.push(rec.refusal); continue; }\n if (typeof rec.t === 'number') samples.push(rec);\n }\n\n // A truncated file is still usable — a walk that ended on a flat battery\n // should not cost us the part that was recorded.\n return { header, refusals, samples };\n}\n"],"mappings":";;;;;;;;;;;;AAQA,YAAY,oBAAoB;AAChC,YAAY,iBAAiB;AAC7B,YAAY,mBAAmB;AAC/B,OAAOA,mBAA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raPreview = (() => {
|
|
|
9
9
|
}
|
|
10
10
|
})();
|
|
11
11
|
var native = requireOptionalNativeModule("AdaVision");
|
|
12
|
+
function verb(name) {
|
|
13
|
+
if (!native) throw new Error("ada-vision native module not present in this build");
|
|
14
|
+
const fn = native[name];
|
|
15
|
+
if (typeof fn !== "function") {
|
|
16
|
+
throw Object.assign(new Error(`UNAVAILABLE: ada-vision ${String(name)} is not implemented on this platform (iOS only)`), { code: "UNAVAILABLE" });
|
|
17
|
+
}
|
|
18
|
+
return fn.bind(native);
|
|
19
|
+
}
|
|
12
20
|
function isSupported() {
|
|
13
21
|
try {
|
|
14
22
|
return native?.isSupported() ?? false;
|
|
@@ -20,9 +28,15 @@ async function assessQuality(uri) {
|
|
|
20
28
|
if (!native) throw new Error("ada-vision native module not present in this build");
|
|
21
29
|
return native.assessQuality(uri);
|
|
22
30
|
}
|
|
23
|
-
async function startVision(intervalMs = 1e3) {
|
|
24
|
-
if (
|
|
25
|
-
|
|
31
|
+
async function startVision(intervalMs = 1e3, facing = "back") {
|
|
32
|
+
if (facing !== "front" && facing !== "back") {
|
|
33
|
+
throw new Error(`Unknown camera "${String(facing)}" \u2014 use "front" or "back"`);
|
|
34
|
+
}
|
|
35
|
+
if (facing === "back") return verb("startVision")(intervalMs);
|
|
36
|
+
if (native && typeof native.startVisionCamera !== "function") {
|
|
37
|
+
throw new Error("the front camera needs a newer app build \u2014 this one can read the back camera only");
|
|
38
|
+
}
|
|
39
|
+
return verb("startVisionCamera")(intervalMs, facing);
|
|
26
40
|
}
|
|
27
41
|
function stopVision() {
|
|
28
42
|
native?.stopVision?.();
|
|
@@ -39,16 +53,13 @@ function qualityById(id) {
|
|
|
39
53
|
return native?.qualityById?.(id) ?? null;
|
|
40
54
|
}
|
|
41
55
|
async function readText(id, languages) {
|
|
42
|
-
|
|
43
|
-
return native.readText(id, languages ?? null);
|
|
56
|
+
return verb("readText")(id, languages ?? null);
|
|
44
57
|
}
|
|
45
58
|
async function describe(id, maxLabels = 5) {
|
|
46
|
-
|
|
47
|
-
return native.describe(id, maxLabels);
|
|
59
|
+
return verb("describe")(id, maxLabels);
|
|
48
60
|
}
|
|
49
61
|
async function detectObjects(id, maxItems = 10, minConfidence = 0.25) {
|
|
50
|
-
|
|
51
|
-
return native.detectObjects(id, maxItems, minConfidence);
|
|
62
|
+
return verb("detectObjects")(id, maxItems, minConfidence);
|
|
52
63
|
}
|
|
53
64
|
|
|
54
65
|
export {
|
|
@@ -64,4 +75,4 @@ export {
|
|
|
64
75
|
describe,
|
|
65
76
|
detectObjects
|
|
66
77
|
};
|
|
67
|
-
//# sourceMappingURL=chunk-
|
|
78
|
+
//# sourceMappingURL=chunk-6JX222YX.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/vision/index.ts"],"sourcesContent":["// ada-vision — on-device image understanding, our own native code (no camera lib).\n//\n// On-device, frames never leave the phone:\n// assessQuality(uri) — one-shot quality of a LOCAL frame file.\n// startVision()/onVisionFrame — the headless camera session that publishes a\n// timeline frame (id + quality, never pixels) at a\n// cadence; readText/describe/detectObjects resolve\n// a published id ON-DEVICE.\n//\n// The luma math mirrors ./analysis.js (which has the exhaustive unit tests).\n//\n// requireOptionalNativeModule: on a binary WITHOUT the native module, the import\n// is null, isSupported() is false, and the session calls refuse cleanly (no crash).\n\nimport type { ComponentType } from 'react';\nimport { requireOptionalNativeModule } from 'expo-modules-core';\nimport { requireNativeView } from 'expo';\n\n/** TEST-ONLY live preview bound to the same headless session (so a tester can\n * aim). The real Guide runs headless and never mounts this. Null on a binary\n * without the native view, so importing this module never crashes. */\nexport const CameraPreview: ComponentType<any> | null = (() => {\n try { return requireNativeView('AdaVision'); } catch { return null; }\n})();\n\nexport interface RawFrameQuality {\n blur: 'sharp' | 'slight' | 'severe' | 'unknown';\n exposure: 'good' | 'dark' | 'bright' | 'unknown';\n /** true = lens covered; false = clearly not; null = not assessed. */\n obstructed: boolean | null;\n}\n\ninterface Subscription { remove(): void }\n\n/** A published timeline frame from the vision session (metadata only — never\n * pixels; the buffer stays in the native ring, resolved by id). */\nexport interface VisionFrame {\n id: string;\n ts: number;\n expiresAt: number;\n blur: RawFrameQuality['blur'];\n exposure: RawFrameQuality['exposure'];\n obstructed: RawFrameQuality['obstructed'];\n}\n\n/** One OCR result on a frame — matches the SDK's VisionText. */\nexport interface VisionText {\n text: string;\n /** 0..1 recogniser confidence (never a 1.0 filler). */\n confidence: number | null;\n /** Normalised 0..1 box, origin TOP-left, in the UPRIGHT portrait frame (native\n * rotates frames at capture) — x runs from the user's left to their right. */\n box?: { x: number; y: number; width: number; height: number };\n}\n\n/** One classifier label — a vocabulary term, NOT prose. Matches VisionLabel. */\nexport interface VisionLabel {\n label: string;\n confidence: number | null;\n}\n\n/** One detected object — a located item with a box, from the bundled detector. */\nexport interface VisionObject {\n /** Open-Images class name (e.g. \"Car\", \"Traffic light\", \"Building\", \"Person\"). */\n label: string;\n /** 0..1 detector confidence. */\n confidence: number | null;\n /** Normalised 0..1 box, origin TOP-left, in the UPRIGHT portrait frame (native\n * rotates frames at capture) — x runs from the user's left to their right. */\n box?: { x: number; y: number; width: number; height: number };\n}\n\ninterface AdaVisionNative {\n isSupported(): boolean;\n assessQuality(uri: string): Promise<RawFrameQuality>;\n startVision(intervalMs: number): Promise<void>;\n /** Absent from binaries built before the front camera existed. */\n startVisionCamera?(intervalMs: number, facing: VisionFacing): Promise<void>;\n stopVision(): void;\n visionRetentionMs(): number;\n qualityById(id: string): RawFrameQuality | null;\n readText(id: string, languages: string[] | null): Promise<VisionText[]>;\n describe(id: string, maxLabels: number): Promise<VisionLabel[]>;\n detectObjects(id: string, maxItems: number, minConfidence: number): Promise<VisionObject[]>;\n addListener(event: 'visionFrame', cb: (f: VisionFrame) => void): Subscription;\n}\n\nconst native = requireOptionalNativeModule<AdaVisionNative>('AdaVision');\n\n/** The native function, or a refusal by name: Android's AdaVision has assessQuality only\n * (AI Models F4), so a verb it lacks says so instead of failing as \"not a function\". */\nfunction verb<K extends keyof AdaVisionNative>(name: K): AdaVisionNative[K] {\n if (!native) throw new Error('ada-vision native module not present in this build');\n const fn = native[name];\n if (typeof fn !== 'function') {\n throw Object.assign(new Error(`UNAVAILABLE: ada-vision ${String(name)} is not implemented on this platform (iOS only)`), { code: 'UNAVAILABLE' });\n }\n return (fn as Function).bind(native) as AdaVisionNative[K];\n}\n\n/** Is the native vision module present in this binary? */\nexport function isSupported(): boolean {\n try { return native?.isSupported() ?? false; } catch { return false; }\n}\n\n/** One-shot: measure a frame's quality from its LOCAL file. Throws if it cannot look. */\nexport async function assessQuality(uri: string): Promise<RawFrameQuality> {\n if (!native) throw new Error('ada-vision native module not present in this build');\n return native.assessQuality(uri);\n}\n\n// ── Vision session (feeds the Visual Engine) ──\n\n/** Which camera the vision session reads. */\nexport type VisionFacing = 'front' | 'back';\n\n/**\n * Start the vision session on `facing` — publishes a timeline frame every\n * `intervalMs`. The back camera works on every binary; the front camera needs a\n * binary built with it, and an older one REFUSES it rather than quietly reading\n * the back camera instead.\n */\nexport async function startVision(intervalMs = 1000, facing: VisionFacing = 'back'): Promise<void> {\n if (facing !== 'front' && facing !== 'back') {\n throw new Error(`Unknown camera \"${String(facing)}\" — use \"front\" or \"back\"`);\n }\n if (facing === 'back') return verb('startVision')(intervalMs);\n if (native && typeof native.startVisionCamera !== 'function') {\n throw new Error('the front camera needs a newer app build — this one can read the back camera only');\n }\n return verb('startVisionCamera')!(intervalMs, facing);\n}\n\n/** Stop the vision session and release the camera. */\nexport function stopVision(): void {\n native?.stopVision?.();\n}\n\n/** Subscribe to published timeline frames (id + quality + expiry; no pixels). */\nexport function onVisionFrame(cb: (f: VisionFrame) => void): Subscription {\n if (!native) return { remove() {} };\n return native.addListener('visionFrame', cb);\n}\n\n/** How far back the native ring can still resolve a frame id (ms). */\nexport function visionRetentionMs(): number {\n return native?.visionRetentionMs?.() ?? 0;\n}\n\n/** Resolve a published frame id → its measured quality (null if evicted). */\nexport function qualityById(id: string): RawFrameQuality | null {\n return native?.qualityById?.(id) ?? null;\n}\n\n/** On-device OCR on a published frame. [] = looked, no text; throws = couldn't look. */\nexport async function readText(id: string, languages?: string[]): Promise<VisionText[]> {\n return verb('readText')(id, languages ?? null);\n}\n\n/** On-device image classification → labels (not prose). [] = nothing recognised. */\nexport async function describe(id: string, maxLabels = 5): Promise<VisionLabel[]> {\n return verb('describe')(id, maxLabels);\n}\n\n/** On-device object detection (bundled YOLOv8n, Open-Images vocabulary) → located\n * items with boxes. [] = looked, found nothing above `minConfidence`; throws =\n * couldn't look (id aged out, or the model isn't in this build). */\nexport async function detectObjects(id: string, maxItems = 10, minConfidence = 0.25): Promise<VisionObject[]> {\n return verb('detectObjects')(id, maxItems, minConfidence);\n}\n"],"mappings":";AAeA,SAAS,mCAAmC;AAC5C,SAAS,yBAAyB;AAK3B,IAAM,iBAA4C,MAAM;AAC7D,MAAI;AAAE,WAAO,kBAAkB,WAAW;AAAA,EAAG,QAAQ;AAAE,WAAO;AAAA,EAAM;AACtE,GAAG;AAgEH,IAAM,SAAS,4BAA6C,WAAW;AAIvE,SAAS,KAAsC,MAA6B;AAC1E,MAAI,CAAC,OAAQ,OAAM,IAAI,MAAM,oDAAoD;AACjF,QAAM,KAAK,OAAO,IAAI;AACtB,MAAI,OAAO,OAAO,YAAY;AAC5B,UAAM,OAAO,OAAO,IAAI,MAAM,2BAA2B,OAAO,IAAI,CAAC,iDAAiD,GAAG,EAAE,MAAM,cAAc,CAAC;AAAA,EAClJ;AACA,SAAQ,GAAgB,KAAK,MAAM;AACrC;AAGO,SAAS,cAAuB;AACrC,MAAI;AAAE,WAAO,QAAQ,YAAY,KAAK;AAAA,EAAO,QAAQ;AAAE,WAAO;AAAA,EAAO;AACvE;AAGA,eAAsB,cAAc,KAAuC;AACzE,MAAI,CAAC,OAAQ,OAAM,IAAI,MAAM,oDAAoD;AACjF,SAAO,OAAO,cAAc,GAAG;AACjC;AAaA,eAAsB,YAAY,aAAa,KAAM,SAAuB,QAAuB;AACjG,MAAI,WAAW,WAAW,WAAW,QAAQ;AAC3C,UAAM,IAAI,MAAM,mBAAmB,OAAO,MAAM,CAAC,gCAA2B;AAAA,EAC9E;AACA,MAAI,WAAW,OAAQ,QAAO,KAAK,aAAa,EAAE,UAAU;AAC5D,MAAI,UAAU,OAAO,OAAO,sBAAsB,YAAY;AAC5D,UAAM,IAAI,MAAM,wFAAmF;AAAA,EACrG;AACA,SAAO,KAAK,mBAAmB,EAAG,YAAY,MAAM;AACtD;AAGO,SAAS,aAAmB;AACjC,UAAQ,aAAa;AACvB;AAGO,SAAS,cAAc,IAA4C;AACxE,MAAI,CAAC,OAAQ,QAAO,EAAE,SAAS;AAAA,EAAC,EAAE;AAClC,SAAO,OAAO,YAAY,eAAe,EAAE;AAC7C;AAGO,SAAS,oBAA4B;AAC1C,SAAO,QAAQ,oBAAoB,KAAK;AAC1C;AAGO,SAAS,YAAY,IAAoC;AAC9D,SAAO,QAAQ,cAAc,EAAE,KAAK;AACtC;AAGA,eAAsB,SAAS,IAAY,WAA6C;AACtF,SAAO,KAAK,UAAU,EAAE,IAAI,aAAa,IAAI;AAC/C;AAGA,eAAsB,SAAS,IAAY,YAAY,GAA2B;AAChF,SAAO,KAAK,UAAU,EAAE,IAAI,SAAS;AACvC;AAKA,eAAsB,cAAc,IAAY,WAAW,IAAI,gBAAgB,MAA+B;AAC5G,SAAO,KAAK,eAAe,EAAE,IAAI,UAAU,aAAa;AAC1D;","names":[]}
|
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
isSupported,
|
|
5
5
|
qualityById,
|
|
6
6
|
readText
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-6JX222YX.mjs";
|
|
8
8
|
import {
|
|
9
9
|
cameraTasks
|
|
10
10
|
} from "./chunk-6LXF3JVX.mjs";
|
|
@@ -644,4 +644,4 @@ export {
|
|
|
644
644
|
clearAllPluginData,
|
|
645
645
|
createDeviceProvider
|
|
646
646
|
};
|
|
647
|
-
//# sourceMappingURL=chunk-
|
|
647
|
+
//# sourceMappingURL=chunk-DLDG77DX.mjs.map
|