@ateam-ai-mobile/expo 0.1.4 → 0.1.7
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/android/build.gradle +16 -0
- package/android/src/main/java/expo/modules/adavision/AdaVisionModule.kt +75 -0
- package/dist/attachmentCache-DhQTqi2E.d.mts +18 -0
- package/dist/attachmentCache-DhQTqi2E.d.ts +18 -0
- package/dist/bootstrap.d.mts +95 -0
- package/dist/bootstrap.d.ts +95 -0
- package/dist/bootstrap.js +2473 -0
- package/dist/bootstrap.js.map +1 -0
- package/dist/bootstrap.mjs +64 -0
- package/dist/bootstrap.mjs.map +1 -0
- package/dist/chunk-4BMWVN25.mjs +91 -0
- package/dist/chunk-4BMWVN25.mjs.map +1 -0
- package/dist/chunk-4XYXJYN5.mjs +1513 -0
- package/dist/chunk-4XYXJYN5.mjs.map +1 -0
- package/dist/chunk-5HM6UA2K.mjs +571 -0
- package/dist/chunk-5HM6UA2K.mjs.map +1 -0
- package/dist/chunk-6LXF3JVX.mjs +96 -0
- package/dist/chunk-6LXF3JVX.mjs.map +1 -0
- package/dist/chunk-FF6ZZZXH.mjs +71 -0
- package/dist/chunk-FF6ZZZXH.mjs.map +1 -0
- package/dist/chunk-KOXF2FZM.mjs +92 -0
- package/dist/chunk-KOXF2FZM.mjs.map +1 -0
- package/dist/chunk-L4V7OCJ3.mjs +71 -0
- package/dist/chunk-L4V7OCJ3.mjs.map +1 -0
- package/dist/chunk-TZXWVYYR.mjs +67 -0
- package/dist/chunk-TZXWVYYR.mjs.map +1 -0
- package/dist/chunk-VQJPSLHP.mjs +1801 -0
- package/dist/chunk-VQJPSLHP.mjs.map +1 -0
- package/dist/chunk-WMJKH4XE.mjs +34 -0
- package/dist/chunk-WMJKH4XE.mjs.map +1 -0
- package/dist/chunk-XQSY6MHP.mjs +213 -0
- package/dist/chunk-XQSY6MHP.mjs.map +1 -0
- package/dist/createBridge.mjs +2 -0
- package/dist/createBridge.mjs.map +1 -1
- package/dist/device/createDeviceProvider.d.mts +23 -0
- package/dist/device/createDeviceProvider.d.ts +23 -0
- package/dist/device/createDeviceProvider.js +967 -0
- package/dist/device/createDeviceProvider.js.map +1 -0
- package/dist/device/createDeviceProvider.mjs +17 -0
- package/dist/device/createDeviceProvider.mjs.map +1 -0
- package/dist/device-bridge/collectors/appEngagement.d.mts +10 -0
- package/dist/device-bridge/collectors/appEngagement.d.ts +10 -0
- package/dist/device-bridge/collectors/appEngagement.js +128 -0
- package/dist/device-bridge/collectors/appEngagement.js.map +1 -0
- package/dist/device-bridge/collectors/appEngagement.mjs +12 -0
- package/dist/device-bridge/collectors/appEngagement.mjs.map +1 -0
- package/dist/device-bridge/collectors/health.d.mts +24 -0
- package/dist/device-bridge/collectors/health.d.ts +24 -0
- package/dist/device-bridge/collectors/health.js +238 -0
- package/dist/device-bridge/collectors/health.js.map +1 -0
- package/dist/device-bridge/collectors/health.mjs +18 -0
- package/dist/device-bridge/collectors/health.mjs.map +1 -0
- package/dist/device-bridge/cookies.d.mts +49 -0
- package/dist/device-bridge/cookies.d.ts +49 -0
- package/dist/device-bridge/cookies.js +90 -0
- package/dist/device-bridge/cookies.js.map +1 -0
- package/dist/device-bridge/cookies.mjs +14 -0
- package/dist/device-bridge/cookies.mjs.map +1 -0
- package/dist/device-bridge/index.d.mts +483 -0
- package/dist/device-bridge/index.d.ts +483 -0
- package/dist/device-bridge/index.js +2084 -0
- package/dist/device-bridge/index.js.map +1 -0
- package/dist/device-bridge/index.mjs +32 -0
- package/dist/device-bridge/index.mjs.map +1 -0
- package/dist/device-bridge/nfc.d.mts +30 -0
- package/dist/device-bridge/nfc.d.ts +30 -0
- package/dist/device-bridge/nfc.js +109 -0
- package/dist/device-bridge/nfc.js.map +1 -0
- package/dist/device-bridge/nfc.mjs +14 -0
- package/dist/device-bridge/nfc.mjs.map +1 -0
- package/dist/index.d.mts +493 -18
- package/dist/index.d.ts +493 -18
- package/dist/index.js +1409 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +23 -194
- package/dist/index.mjs.map +1 -1
- package/dist/oauth/expoOAuth.d.mts +18 -0
- package/dist/oauth/expoOAuth.d.ts +18 -0
- package/dist/oauth/expoOAuth.js +102 -0
- package/dist/oauth/expoOAuth.js.map +1 -0
- package/dist/oauth/expoOAuth.mjs +8 -0
- package/dist/oauth/expoOAuth.mjs.map +1 -0
- package/dist/types-BRanJYTn.d.mts +138 -0
- package/dist/types-BRanJYTn.d.ts +138 -0
- package/dist/vision/index.d.mts +81 -0
- package/dist/vision/index.d.ts +81 -0
- package/dist/vision/index.js +101 -0
- package/dist/vision/index.js.map +1 -0
- package/dist/vision/index.mjs +28 -0
- package/dist/vision/index.mjs.map +1 -0
- package/expo-module.config.json +9 -0
- package/ios/AdaVision.podspec +28 -0
- package/ios/AdaVisionModule.swift +432 -0
- package/ios/Resources/yolov8n-oiv7.mlmodelc/analytics/coremldata.bin +0 -0
- package/ios/Resources/yolov8n-oiv7.mlmodelc/coremldata.bin +0 -0
- package/ios/Resources/yolov8n-oiv7.mlmodelc/metadata.json +773 -0
- package/ios/Resources/yolov8n-oiv7.mlmodelc/model0/analytics/coremldata.bin +0 -0
- package/ios/Resources/yolov8n-oiv7.mlmodelc/model0/coremldata.bin +0 -0
- package/ios/Resources/yolov8n-oiv7.mlmodelc/model0/model.mil +720 -0
- package/ios/Resources/yolov8n-oiv7.mlmodelc/model0/weights/weight.bin +0 -0
- package/ios/Resources/yolov8n-oiv7.mlmodelc/model1/coremldata.bin +0 -0
- package/package.json +107 -7
- package/src/bootstrap.ts +159 -0
- package/src/camera/CameraTaskHost.tsx +175 -0
- package/src/camera/cameraTasks.ts +129 -0
- package/src/device/createDeviceProvider.ts +832 -0
- package/src/device-bridge/actions/calendar-actions.ts +217 -0
- package/src/device-bridge/actions/dnd.ts +74 -0
- package/src/device-bridge/actions/health-actions.ts +58 -0
- package/src/device-bridge/actions/index.ts +12 -0
- package/src/device-bridge/actions/messaging.ts +74 -0
- package/src/device-bridge/actions/navigation.ts +88 -0
- package/src/device-bridge/actions/queue-poller.ts +151 -0
- package/src/device-bridge/background.ts +137 -0
- package/src/device-bridge/biometrics.ts +64 -0
- package/src/device-bridge/capabilities.ts +218 -0
- package/src/device-bridge/collectors/appEngagement.ts +120 -0
- package/src/device-bridge/collectors/battery.ts +83 -0
- package/src/device-bridge/collectors/calendar.ts +162 -0
- package/src/device-bridge/collectors/camera.ts +27 -0
- package/src/device-bridge/collectors/connectivity.ts +60 -0
- package/src/device-bridge/collectors/contacts.ts +200 -0
- package/src/device-bridge/collectors/health.ts +299 -0
- package/src/device-bridge/collectors/index.ts +12 -0
- package/src/device-bridge/collectors/location.ts +224 -0
- package/src/device-bridge/collectors/mood.ts +141 -0
- package/src/device-bridge/collectors/notifications.ts +70 -0
- package/src/device-bridge/cookies.ts +89 -0
- package/src/device-bridge/dynamic-connector.ts +422 -0
- package/src/device-bridge/index.ts +467 -0
- package/src/device-bridge/nfc.ts +85 -0
- package/src/device-bridge/plugin.ts +71 -0
- package/src/device-bridge/relay-client.ts +237 -0
- package/src/device-bridge/scanner.ts +42 -0
- package/src/device-bridge/sharing.ts +35 -0
- package/src/device-bridge/types.ts +326 -0
- package/src/device-state/activityClassifier.ts +151 -0
- package/src/device-state/activityEngine.ts +252 -0
- package/src/device-state/collectors.ts +31 -0
- package/src/device-state/domains/activity.ts +93 -0
- package/src/device-state/domains/capability.ts +60 -0
- package/src/device-state/domains/connectivity.ts +60 -0
- package/src/device-state/domains/deviceContext.ts +55 -0
- package/src/device-state/domains/interaction.ts +59 -0
- package/src/device-state/domains/spatial.ts +283 -0
- package/src/device-state/domains/visual.ts +100 -0
- package/src/device-state/headingSource.ts +84 -0
- package/src/device-state/index.ts +77 -0
- package/src/device-state/quality.ts +60 -0
- package/src/device-state/replay.ts +220 -0
- package/src/device-state/turnDetector.ts +121 -0
- package/src/device-state/visualEngine.ts +341 -0
- package/src/haptics.ts +4 -0
- package/src/index.ts +37 -0
- package/src/oauth/expoOAuth.tsx +113 -0
- package/src/vision/analysis.js +80 -0
- package/src/vision/index.ts +144 -0
- package/src/vision/ring.js +35 -0
|
@@ -0,0 +1,1513 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cameraError,
|
|
3
|
+
cameraTasks
|
|
4
|
+
} from "./chunk-6LXF3JVX.mjs";
|
|
5
|
+
import {
|
|
6
|
+
__require
|
|
7
|
+
} from "./chunk-WMJKH4XE.mjs";
|
|
8
|
+
|
|
9
|
+
// src/index.ts
|
|
10
|
+
import * as ExpoWebBrowser from "expo-web-browser";
|
|
11
|
+
import * as ExpoLinking from "expo-linking";
|
|
12
|
+
import * as ExpoClipboard from "expo-clipboard";
|
|
13
|
+
import AsyncStorage3 from "@react-native-async-storage/async-storage";
|
|
14
|
+
import { setAcsCacheStorage } from "@ateam-ai-mobile/core";
|
|
15
|
+
import {
|
|
16
|
+
setHapticsProvider,
|
|
17
|
+
setBundleStorage,
|
|
18
|
+
registerBundleModules,
|
|
19
|
+
setPluginNativeProvider
|
|
20
|
+
} from "@ateam-ai-mobile/plugin-runtime";
|
|
21
|
+
import { setClipboardProvider } from "@ateam-ai-mobile/ui";
|
|
22
|
+
|
|
23
|
+
// src/haptics.ts
|
|
24
|
+
import * as Haptics from "expo-haptics";
|
|
25
|
+
var expoHaptics = {
|
|
26
|
+
impact(style) {
|
|
27
|
+
const map = {
|
|
28
|
+
light: Haptics.ImpactFeedbackStyle.Light,
|
|
29
|
+
medium: Haptics.ImpactFeedbackStyle.Medium,
|
|
30
|
+
heavy: Haptics.ImpactFeedbackStyle.Heavy
|
|
31
|
+
};
|
|
32
|
+
Haptics.impactAsync(map[style]).catch(() => {
|
|
33
|
+
});
|
|
34
|
+
},
|
|
35
|
+
notification(type) {
|
|
36
|
+
const map = {
|
|
37
|
+
success: Haptics.NotificationFeedbackType.Success,
|
|
38
|
+
warning: Haptics.NotificationFeedbackType.Warning,
|
|
39
|
+
error: Haptics.NotificationFeedbackType.Error
|
|
40
|
+
};
|
|
41
|
+
Haptics.notificationAsync(map[type]).catch(() => {
|
|
42
|
+
});
|
|
43
|
+
},
|
|
44
|
+
selection() {
|
|
45
|
+
Haptics.selectionAsync().catch(() => {
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
// Shorthand for notification('error') — the form plugins actually call.
|
|
49
|
+
error() {
|
|
50
|
+
Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error).catch(() => {
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// src/bundleStorage.ts
|
|
56
|
+
import * as FileSystem from "expo-file-system/legacy";
|
|
57
|
+
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
58
|
+
var CACHE_DIR = `${FileSystem.cacheDirectory}plugin-bundles/`;
|
|
59
|
+
var DEFAULT_CACHE_INDEX_KEY = "adas-plugin-bundles-index-v1";
|
|
60
|
+
var ExpoBundleStorage = class {
|
|
61
|
+
/** Pass `indexKey` when migrating from an app that already persisted a
|
|
62
|
+
* bundle index under a different key — keeps the existing cache warm. */
|
|
63
|
+
constructor(opts) {
|
|
64
|
+
this.dirReady = false;
|
|
65
|
+
this.indexKey = opts?.indexKey || DEFAULT_CACHE_INDEX_KEY;
|
|
66
|
+
}
|
|
67
|
+
async ensureDir() {
|
|
68
|
+
if (this.dirReady) return;
|
|
69
|
+
try {
|
|
70
|
+
const info = await FileSystem.getInfoAsync(CACHE_DIR);
|
|
71
|
+
if (!info.exists) {
|
|
72
|
+
await FileSystem.makeDirectoryAsync(CACHE_DIR, { intermediates: true });
|
|
73
|
+
}
|
|
74
|
+
} catch (err) {
|
|
75
|
+
console.warn("[ExpoBundleStorage] cache dir error:", err);
|
|
76
|
+
}
|
|
77
|
+
this.dirReady = true;
|
|
78
|
+
}
|
|
79
|
+
async loadIndex() {
|
|
80
|
+
try {
|
|
81
|
+
const raw = await AsyncStorage.getItem(this.indexKey);
|
|
82
|
+
return raw ? JSON.parse(raw) : {};
|
|
83
|
+
} catch {
|
|
84
|
+
return {};
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
async saveIndex(index) {
|
|
88
|
+
try {
|
|
89
|
+
await AsyncStorage.setItem(this.indexKey, JSON.stringify(index));
|
|
90
|
+
} catch (err) {
|
|
91
|
+
console.warn("[ExpoBundleStorage] index save error:", err);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async readBundle(localPath) {
|
|
95
|
+
try {
|
|
96
|
+
const info = await FileSystem.getInfoAsync(localPath);
|
|
97
|
+
if (!info.exists) return null;
|
|
98
|
+
return await FileSystem.readAsStringAsync(localPath);
|
|
99
|
+
} catch {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
async writeBundle(key, content) {
|
|
104
|
+
await this.ensureDir();
|
|
105
|
+
const localPath = `${CACHE_DIR}${key}`;
|
|
106
|
+
await FileSystem.writeAsStringAsync(localPath, content);
|
|
107
|
+
return localPath;
|
|
108
|
+
}
|
|
109
|
+
async deleteBundle(localPath) {
|
|
110
|
+
await FileSystem.deleteAsync(localPath, { idempotent: true }).catch(() => {
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
async clearAll() {
|
|
114
|
+
await FileSystem.deleteAsync(CACHE_DIR, { idempotent: true }).catch(() => {
|
|
115
|
+
});
|
|
116
|
+
await AsyncStorage.removeItem(this.indexKey).catch(() => {
|
|
117
|
+
});
|
|
118
|
+
this.dirReady = false;
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// src/attachmentCache.ts
|
|
123
|
+
import * as FileSystem2 from "expo-file-system/legacy";
|
|
124
|
+
import AsyncStorage2 from "@react-native-async-storage/async-storage";
|
|
125
|
+
var DEFAULT_INDEX_KEY = "adas-chat-attachment-index-v1";
|
|
126
|
+
var DIR = `${FileSystem2.documentDirectory}chat-attachments/`;
|
|
127
|
+
var MAX_JOBS = 100;
|
|
128
|
+
var ExpoAttachmentCache = class {
|
|
129
|
+
/** Pass `indexKey` when migrating from an app that already persisted an
|
|
130
|
+
* index under a different AsyncStorage key — otherwise existing users
|
|
131
|
+
* lose every cached preview on upgrade. */
|
|
132
|
+
constructor(opts) {
|
|
133
|
+
this.index = {};
|
|
134
|
+
const indexKey = opts?.indexKey || DEFAULT_INDEX_KEY;
|
|
135
|
+
this.indexKey = indexKey;
|
|
136
|
+
this.loaded = (async () => {
|
|
137
|
+
try {
|
|
138
|
+
const info = await FileSystem2.getInfoAsync(DIR);
|
|
139
|
+
if (!info.exists) await FileSystem2.makeDirectoryAsync(DIR, { intermediates: true });
|
|
140
|
+
const raw = await AsyncStorage2.getItem(indexKey);
|
|
141
|
+
if (raw) this.index = JSON.parse(raw);
|
|
142
|
+
} catch {
|
|
143
|
+
}
|
|
144
|
+
})();
|
|
145
|
+
}
|
|
146
|
+
async ready() {
|
|
147
|
+
await this.loaded;
|
|
148
|
+
}
|
|
149
|
+
async save(jobId, attachments) {
|
|
150
|
+
await this.loaded;
|
|
151
|
+
const entries = [];
|
|
152
|
+
for (let i = 0; i < attachments.length; i++) {
|
|
153
|
+
const att = attachments[i];
|
|
154
|
+
const ext = att.mimeType.startsWith("image/") ? att.mimeType.split("/")[1] || "jpg" : "bin";
|
|
155
|
+
const filePath = `${DIR}${jobId}_${i}.${ext}`;
|
|
156
|
+
try {
|
|
157
|
+
await FileSystem2.writeAsStringAsync(filePath, att.data, { encoding: FileSystem2.EncodingType.Base64 });
|
|
158
|
+
entries.push({ mimeType: att.mimeType, name: att.name, filePath });
|
|
159
|
+
} catch (err) {
|
|
160
|
+
console.warn("[ExpoAttachmentCache] write failed:", err);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (entries.length === 0) return;
|
|
164
|
+
this.index[jobId] = entries;
|
|
165
|
+
const keys = Object.keys(this.index);
|
|
166
|
+
if (keys.length > MAX_JOBS) {
|
|
167
|
+
for (const k of keys.slice(0, keys.length - MAX_JOBS)) {
|
|
168
|
+
const old = this.index[k];
|
|
169
|
+
if (old) for (const o of old) FileSystem2.deleteAsync(o.filePath, { idempotent: true }).catch(() => {
|
|
170
|
+
});
|
|
171
|
+
delete this.index[k];
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
try {
|
|
175
|
+
await AsyncStorage2.setItem(this.indexKey, JSON.stringify(this.index));
|
|
176
|
+
} catch {
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
get(jobId) {
|
|
180
|
+
const entries = this.index[jobId];
|
|
181
|
+
if (!entries || entries.length === 0) return void 0;
|
|
182
|
+
return entries.map((e) => ({
|
|
183
|
+
mimeType: e.mimeType,
|
|
184
|
+
name: e.name,
|
|
185
|
+
previewUri: e.filePath
|
|
186
|
+
}));
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
// src/device-state/domains/deviceContext.ts
|
|
191
|
+
import * as ExpoBattery from "expo-battery";
|
|
192
|
+
|
|
193
|
+
// src/device-state/quality.ts
|
|
194
|
+
import { neverObserved } from "@ateam-ai-mobile/plugin-runtime";
|
|
195
|
+
var FRESH_MS = 1e4;
|
|
196
|
+
var AGING_MS = 6e4;
|
|
197
|
+
function qualityFrom(observedAt, confidence) {
|
|
198
|
+
const ageMs = Date.now() - observedAt;
|
|
199
|
+
return {
|
|
200
|
+
freshness: {
|
|
201
|
+
state: ageMs < FRESH_MS ? "fresh" : ageMs < AGING_MS ? "aging" : "stale",
|
|
202
|
+
observedAt,
|
|
203
|
+
ageMs
|
|
204
|
+
},
|
|
205
|
+
...confidence ? { confidence } : {}
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
function qualityUnknown() {
|
|
209
|
+
return { freshness: neverObserved() };
|
|
210
|
+
}
|
|
211
|
+
function unsupported(what) {
|
|
212
|
+
const err = new Error(
|
|
213
|
+
`${what} is not implemented in this build \u2014 the native engine behind it does not exist yet.`
|
|
214
|
+
);
|
|
215
|
+
err.code = "UNSUPPORTED";
|
|
216
|
+
return err;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// src/device-state/domains/deviceContext.ts
|
|
220
|
+
function createDeviceContextDomain() {
|
|
221
|
+
return {
|
|
222
|
+
async getSnapshot() {
|
|
223
|
+
const observedAt = Date.now();
|
|
224
|
+
try {
|
|
225
|
+
const [level, state, lowPower] = await Promise.all([
|
|
226
|
+
ExpoBattery.getBatteryLevelAsync(),
|
|
227
|
+
ExpoBattery.getBatteryStateAsync(),
|
|
228
|
+
ExpoBattery.isLowPowerModeEnabledAsync()
|
|
229
|
+
]);
|
|
230
|
+
return {
|
|
231
|
+
power: {
|
|
232
|
+
// expo-battery returns -1 when it cannot tell. That is not a level.
|
|
233
|
+
batteryLevel: typeof level === "number" && level >= 0 ? level : null,
|
|
234
|
+
charging: state === ExpoBattery.BatteryState.CHARGING || state === ExpoBattery.BatteryState.FULL,
|
|
235
|
+
lowPowerMode: lowPower
|
|
236
|
+
},
|
|
237
|
+
// No Expo API for either. iOS needs ProcessInfo.thermalState, Android
|
|
238
|
+
// PowerManager.getThermalStatus — a native module that does not exist.
|
|
239
|
+
thermal: { state: "unknown" },
|
|
240
|
+
resources: { memoryPressure: "unknown", storagePressure: "unknown" },
|
|
241
|
+
quality: qualityFrom(observedAt)
|
|
242
|
+
};
|
|
243
|
+
} catch {
|
|
244
|
+
return {
|
|
245
|
+
power: { batteryLevel: null, charging: null, lowPowerMode: null },
|
|
246
|
+
thermal: { state: "unknown" },
|
|
247
|
+
resources: { memoryPressure: "unknown", storagePressure: "unknown" },
|
|
248
|
+
quality: qualityUnknown()
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
// Real events, not a placeholder: expo-battery exposes native listeners,
|
|
253
|
+
// so a plugin that subscribes genuinely hears about a change rather than
|
|
254
|
+
// sitting on a subscription that can never fire.
|
|
255
|
+
subscribe(listener) {
|
|
256
|
+
const subs = [
|
|
257
|
+
ExpoBattery.addBatteryLevelListener(() => listener({ type: "batteryLevelChanged" })),
|
|
258
|
+
ExpoBattery.addBatteryStateListener(() => listener({ type: "chargingChanged" }))
|
|
259
|
+
];
|
|
260
|
+
return { unsubscribe: () => subs.forEach((x) => x?.remove?.()) };
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// src/device-state/domains/connectivity.ts
|
|
266
|
+
function createConnectivityDomain(collectors) {
|
|
267
|
+
return {
|
|
268
|
+
async getSnapshot() {
|
|
269
|
+
const observedAt = Date.now();
|
|
270
|
+
try {
|
|
271
|
+
if (!collectors.connectivity) throw unsupported("connectivity collector");
|
|
272
|
+
const data = await collectors.connectivity.collectConnectivity();
|
|
273
|
+
const online = data?.is_connected === true;
|
|
274
|
+
const type = String(data?.type || "").toLowerCase();
|
|
275
|
+
return {
|
|
276
|
+
reachability: data?.is_connected == null ? "unknown" : online ? "online" : "offline",
|
|
277
|
+
transport: type === "wifi" ? "wifi" : type === "cellular" ? "cellular" : type === "ethernet" ? "ethernet" : online ? "other" : data?.is_connected === false ? "none" : "unknown",
|
|
278
|
+
// All four below need measurement nobody takes yet. `unknown` is the
|
|
279
|
+
// designed answer — "return unknown rather than stale precision".
|
|
280
|
+
qualityClass: "unknown",
|
|
281
|
+
stability: "unknown",
|
|
282
|
+
latencyMs: null,
|
|
283
|
+
bandwidthClass: "unknown",
|
|
284
|
+
metered: null,
|
|
285
|
+
constrained: null,
|
|
286
|
+
quality: qualityFrom(observedAt)
|
|
287
|
+
};
|
|
288
|
+
} catch {
|
|
289
|
+
return {
|
|
290
|
+
reachability: "unknown",
|
|
291
|
+
transport: "unknown",
|
|
292
|
+
qualityClass: "unknown",
|
|
293
|
+
stability: "unknown",
|
|
294
|
+
latencyMs: null,
|
|
295
|
+
bandwidthClass: "unknown",
|
|
296
|
+
metered: null,
|
|
297
|
+
constrained: null,
|
|
298
|
+
quality: qualityUnknown()
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
},
|
|
302
|
+
// Intent selects a native sampling strategy — latency probes, quality
|
|
303
|
+
// classification. Nothing here samples anything, so accepting an intent
|
|
304
|
+
// would be accepting an instruction we do not carry out.
|
|
305
|
+
async start() {
|
|
306
|
+
throw unsupported("connectivity.start");
|
|
307
|
+
},
|
|
308
|
+
async stop() {
|
|
309
|
+
throw unsupported("connectivity.stop");
|
|
310
|
+
},
|
|
311
|
+
// A subscription that can never fire is worse than none: the caller
|
|
312
|
+
// reasonably reads silence as "nothing changed".
|
|
313
|
+
subscribe() {
|
|
314
|
+
throw unsupported("connectivity.subscribe");
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// src/device-state/domains/capability.ts
|
|
320
|
+
function statusFrom(granted) {
|
|
321
|
+
return {
|
|
322
|
+
support: "unknown",
|
|
323
|
+
permission: granted === null ? "unknown" : granted ? "granted" : "denied",
|
|
324
|
+
availability: "unknown"
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
var UNKNOWN_STATUS = {
|
|
328
|
+
support: "unknown",
|
|
329
|
+
permission: "unknown",
|
|
330
|
+
availability: "unknown"
|
|
331
|
+
};
|
|
332
|
+
function createCapabilityDomain(collectors) {
|
|
333
|
+
return {
|
|
334
|
+
async getSnapshot() {
|
|
335
|
+
const observedAt = Date.now();
|
|
336
|
+
const locationGranted = collectors.location ? await collectors.location.hasPermission().catch(() => null) : null;
|
|
337
|
+
return {
|
|
338
|
+
// Only location has a real reader today. The rest are declared by the
|
|
339
|
+
// contract and answered honestly rather than omitted.
|
|
340
|
+
camera: UNKNOWN_STATUS,
|
|
341
|
+
microphone: UNKNOWN_STATUS,
|
|
342
|
+
location: {
|
|
343
|
+
...statusFrom(locationGranted),
|
|
344
|
+
// iOS 14+ distinguishes precise from approximate; nothing reads it.
|
|
345
|
+
precision: "unknown",
|
|
346
|
+
backgroundPermission: "unknown"
|
|
347
|
+
},
|
|
348
|
+
motion: UNKNOWN_STATUS,
|
|
349
|
+
bluetooth: UNKNOWN_STATUS,
|
|
350
|
+
uwb: UNKNOWN_STATUS,
|
|
351
|
+
nfc: UNKNOWN_STATUS,
|
|
352
|
+
quality: qualityFrom(observedAt)
|
|
353
|
+
};
|
|
354
|
+
},
|
|
355
|
+
// No OS permission-change stream is wired. Same reasoning as connectivity:
|
|
356
|
+
// silence would be indistinguishable from stability.
|
|
357
|
+
subscribe() {
|
|
358
|
+
throw unsupported("capability.subscribe");
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// src/device-state/domains/interaction.ts
|
|
364
|
+
import { AppState } from "react-native";
|
|
365
|
+
function createInteractionDomain() {
|
|
366
|
+
const mapState = (s) => s === "active" ? "foreground" : s === "background" ? "background" : s === "inactive" ? "inactive" : "unknown";
|
|
367
|
+
return {
|
|
368
|
+
async getSnapshot() {
|
|
369
|
+
const observedAt = Date.now();
|
|
370
|
+
return {
|
|
371
|
+
lifecycle: {
|
|
372
|
+
appState: mapState(AppState.currentState),
|
|
373
|
+
screenOn: null,
|
|
374
|
+
userActive: null,
|
|
375
|
+
idleDurationMs: null
|
|
376
|
+
},
|
|
377
|
+
audio: {
|
|
378
|
+
captureActive: null,
|
|
379
|
+
playbackActive: null,
|
|
380
|
+
route: "unknown",
|
|
381
|
+
interruption: "unknown"
|
|
382
|
+
},
|
|
383
|
+
// The voice block is OMITTED rather than filled with nulls: the design
|
|
384
|
+
// says a platform that cannot report voice session state leaves the
|
|
385
|
+
// group out instead of guessing `false` for each field. The app's own
|
|
386
|
+
// voice store knows this, but it is not reachable from the SDK.
|
|
387
|
+
quality: qualityFrom(observedAt)
|
|
388
|
+
};
|
|
389
|
+
},
|
|
390
|
+
// Real: AppState genuinely emits on every foreground/background change,
|
|
391
|
+
// which is the one part of this domain that works.
|
|
392
|
+
subscribe(listener) {
|
|
393
|
+
const sub2 = AppState.addEventListener("change", (next) => {
|
|
394
|
+
listener({ type: "appStateChanged", appState: mapState(next) });
|
|
395
|
+
});
|
|
396
|
+
return { unsubscribe: () => sub2.remove() };
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// src/device-state/visualEngine.ts
|
|
402
|
+
var PROFILES = {
|
|
403
|
+
navigation: { intervalMs: 1e3, windowMs: 5e3 },
|
|
404
|
+
accessibility: { intervalMs: 1e3, windowMs: 5e3 },
|
|
405
|
+
stream: { intervalMs: 500, windowMs: 5e3 },
|
|
406
|
+
inspection: { intervalMs: 2e3, windowMs: 8e3 },
|
|
407
|
+
shopping: { intervalMs: 2e3, windowMs: 5e3 },
|
|
408
|
+
document: { intervalMs: 3e3, windowMs: 3e3 },
|
|
409
|
+
ocr: { intervalMs: 3e3, windowMs: 3e3 },
|
|
410
|
+
highQuality: { intervalMs: 5e3, windowMs: 5e3 }
|
|
411
|
+
};
|
|
412
|
+
var VisualEngine = class {
|
|
413
|
+
constructor() {
|
|
414
|
+
this.frames = [];
|
|
415
|
+
this.session = null;
|
|
416
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
417
|
+
/** Set by the host component while a camera is actually mounted and ready. */
|
|
418
|
+
this.cameraReady = false;
|
|
419
|
+
/** Optional: supplies heading/position stamped onto each frame. */
|
|
420
|
+
this.context = null;
|
|
421
|
+
// The native ring's actual retention (ms). The engine's effective window is
|
|
422
|
+
// min(intent window, retention) — set by whoever owns the clamp semantics; this
|
|
423
|
+
// just stores the input so that logic has one source of truth to read.
|
|
424
|
+
this._retentionMs = null;
|
|
425
|
+
}
|
|
426
|
+
/** Host DI. Without it frames carry no orientation, and say so with nulls. */
|
|
427
|
+
setContextProvider(p) {
|
|
428
|
+
this.context = p;
|
|
429
|
+
}
|
|
430
|
+
// ── session ──
|
|
431
|
+
startSession(intent) {
|
|
432
|
+
if (this.session) return this.session;
|
|
433
|
+
this.session = {
|
|
434
|
+
id: `visual_${Date.now()}`,
|
|
435
|
+
intent: PROFILES[intent] ? intent : "navigation",
|
|
436
|
+
startedAt: Date.now(),
|
|
437
|
+
suspended: false
|
|
438
|
+
};
|
|
439
|
+
this.notify();
|
|
440
|
+
return this.session;
|
|
441
|
+
}
|
|
442
|
+
stopSession() {
|
|
443
|
+
this.session = null;
|
|
444
|
+
this.frames = [];
|
|
445
|
+
this.cameraReady = false;
|
|
446
|
+
this.notify();
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Background: suspend, do not end.
|
|
450
|
+
*
|
|
451
|
+
* iOS tears the camera down when the app leaves the foreground. The session
|
|
452
|
+
* is a STATE that survives that; what does not survive is the claim that the
|
|
453
|
+
* camera is live. While suspended the ring is kept but ages — a read gets
|
|
454
|
+
* old frames clearly marked old, rather than a fresh-looking lie.
|
|
455
|
+
*/
|
|
456
|
+
suspend() {
|
|
457
|
+
if (!this.session || this.session.suspended) return;
|
|
458
|
+
this.session.suspended = true;
|
|
459
|
+
this.cameraReady = false;
|
|
460
|
+
this.notify();
|
|
461
|
+
}
|
|
462
|
+
resume() {
|
|
463
|
+
if (!this.session?.suspended) return;
|
|
464
|
+
this.session.suspended = false;
|
|
465
|
+
this.notify();
|
|
466
|
+
}
|
|
467
|
+
getSession() {
|
|
468
|
+
return this.session;
|
|
469
|
+
}
|
|
470
|
+
/** Is a camera mounted and ready right now? Only the host knows. */
|
|
471
|
+
setCameraReady(ready) {
|
|
472
|
+
this.cameraReady = ready;
|
|
473
|
+
this.notify();
|
|
474
|
+
}
|
|
475
|
+
isCameraReady() {
|
|
476
|
+
return this.cameraReady && !this.session?.suspended;
|
|
477
|
+
}
|
|
478
|
+
// ── the ring ──
|
|
479
|
+
profile() {
|
|
480
|
+
const intent = this.session?.intent || "navigation";
|
|
481
|
+
return PROFILES[intent];
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Feed from the native vision session (VisionSession, iOS) — the FAST frame
|
|
485
|
+
* source that replaces the takePictureAsync loop. The frame is IN-MEMORY: the
|
|
486
|
+
* native ring holds the pixels keyed by `id`, and native.vision resolves that
|
|
487
|
+
* id inside the trusted layer. No `uri` crosses here; the quality is a REAL
|
|
488
|
+
* measurement (not `unknown`), and `expiresAt` is the ring's real eviction time.
|
|
489
|
+
* The public VisualFrameRef strip to id-only is owned by the device-state session.
|
|
490
|
+
*/
|
|
491
|
+
pushFrameMeta(meta) {
|
|
492
|
+
let heading = null;
|
|
493
|
+
try {
|
|
494
|
+
heading = this.context?.heading() ?? null;
|
|
495
|
+
} catch {
|
|
496
|
+
}
|
|
497
|
+
this.frames.push({
|
|
498
|
+
// In-memory: no file. native.vision resolves the id inside the native ring.
|
|
499
|
+
handle: { id: meta.id, mimeType: "image/none", expiresAt: meta.expiresAt ?? null },
|
|
500
|
+
timestamp: meta.timestamp,
|
|
501
|
+
orientation: { headingDeg: heading?.headingDeg ?? null, pitchDeg: null, rollDeg: null },
|
|
502
|
+
quality: meta.quality ?? { blur: "unknown", exposure: "unknown", obstructed: null }
|
|
503
|
+
});
|
|
504
|
+
this.evict();
|
|
505
|
+
this.notify();
|
|
506
|
+
}
|
|
507
|
+
setRetentionMs(ms) {
|
|
508
|
+
this._retentionMs = ms && ms > 0 ? ms : null;
|
|
509
|
+
}
|
|
510
|
+
retentionMs() {
|
|
511
|
+
return this._retentionMs;
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* How far back the timeline ACTUALLY reaches: `min(intent window, native
|
|
515
|
+
* retention)`.
|
|
516
|
+
*
|
|
517
|
+
* The intent asks for a window; native decides what it can afford to keep.
|
|
518
|
+
* Full-res luma at ~24fps is tens of megabytes for a few seconds, so the ring
|
|
519
|
+
* is memory-bounded and shrinks under pressure — `retentionMs` is native
|
|
520
|
+
* publishing what it is really holding.
|
|
521
|
+
*
|
|
522
|
+
* EVERY FRAME IN THE TIMELINE CAN BE ANALYSED. That is the one sentence a
|
|
523
|
+
* plugin author has to hold, and this clamp is what makes it true. The
|
|
524
|
+
* alternative — listing frames past retention marked "not analysable" — was
|
|
525
|
+
* considered and rejected: a caller would have to check a flag before every
|
|
526
|
+
* use, and forgetting throws mid-guidance on a frame the model can see
|
|
527
|
+
* listed. Present-but-unusable is the same trap as an empty rowset reading
|
|
528
|
+
* as "no rows matched".
|
|
529
|
+
*
|
|
530
|
+
* If per-frame history beyond retention is ever needed, the shape is an
|
|
531
|
+
* AGGREGATE on the snapshot ("the view has been unusable for N ms"), never a
|
|
532
|
+
* per-entry flag.
|
|
533
|
+
*/
|
|
534
|
+
effectiveWindowMs() {
|
|
535
|
+
const intent = this.profile().windowMs;
|
|
536
|
+
const retention = this._retentionMs;
|
|
537
|
+
return retention === null ? intent : Math.min(intent, retention);
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Time-based, not count-based: the design specifies a rolling WINDOW.
|
|
541
|
+
*
|
|
542
|
+
* Evicts on the EFFECTIVE window, so a frame whose pixels native has already
|
|
543
|
+
* dropped leaves the timeline at the same moment. Without this, `frameById`
|
|
544
|
+
* would start returning null for frames still being listed.
|
|
545
|
+
*/
|
|
546
|
+
evict() {
|
|
547
|
+
const cutoff = Date.now() - this.effectiveWindowMs();
|
|
548
|
+
this.frames = this.frames.filter((f) => f.timestamp >= cutoff);
|
|
549
|
+
}
|
|
550
|
+
timeline() {
|
|
551
|
+
this.evict();
|
|
552
|
+
return { windowMs: this.effectiveWindowMs(), frames: [...this.frames] };
|
|
553
|
+
}
|
|
554
|
+
latest() {
|
|
555
|
+
this.evict();
|
|
556
|
+
return this.frames.length ? this.frames[this.frames.length - 1] : null;
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Resolve a frame handle id (from a VisualFrameRef) back to the live frame so
|
|
560
|
+
* the mechanical layer — native.vision — can read its on-device file. The
|
|
561
|
+
* whole contract of that layer is that it takes an id, never pixels; this is
|
|
562
|
+
* where an id becomes a `handle.uri` it can open. Returns null when the id is
|
|
563
|
+
* unknown OR its frame has aged out of the rolling window (the cache file may
|
|
564
|
+
* also be gone by then) — a real "I can't look at that frame" answer, which
|
|
565
|
+
* the caller must treat as distinct from "I looked and found nothing".
|
|
566
|
+
*/
|
|
567
|
+
frameById(id) {
|
|
568
|
+
this.evict();
|
|
569
|
+
return this.frames.find((f) => f.handle.id === id) ?? null;
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* Record a MEASURED quality back onto a frame (native.vision.assessQuality).
|
|
573
|
+
* Frames are minted with `quality: unknown` because nothing looks at the
|
|
574
|
+
* pixels at capture time; this is how a real measurement replaces that so a
|
|
575
|
+
* later read of VisualFrameRef.quality reflects it. No-op if the frame has
|
|
576
|
+
* aged out — we never resurrect an evicted frame just to label it.
|
|
577
|
+
*/
|
|
578
|
+
setFrameQuality(id, quality) {
|
|
579
|
+
const f = this.frames.find((fr) => fr.handle.id === id);
|
|
580
|
+
if (!f) return;
|
|
581
|
+
f.quality = quality;
|
|
582
|
+
this.notify();
|
|
583
|
+
}
|
|
584
|
+
// ── change notification ──
|
|
585
|
+
subscribe(fn) {
|
|
586
|
+
this.listeners.add(fn);
|
|
587
|
+
return () => this.listeners.delete(fn);
|
|
588
|
+
}
|
|
589
|
+
notify() {
|
|
590
|
+
for (const fn of this.listeners) {
|
|
591
|
+
try {
|
|
592
|
+
fn();
|
|
593
|
+
} catch {
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
};
|
|
598
|
+
var visualEngine = new VisualEngine();
|
|
599
|
+
function frameQuality(frame) {
|
|
600
|
+
if (!frame) return { freshness: { state: "unknown", observedAt: null, ageMs: null } };
|
|
601
|
+
const ageMs = Date.now() - frame.timestamp;
|
|
602
|
+
return {
|
|
603
|
+
freshness: {
|
|
604
|
+
// Tighter than the other domains on purpose: a two-second-old frame of a
|
|
605
|
+
// moving scene is not "fresh" in any useful sense.
|
|
606
|
+
state: ageMs < 2e3 ? "fresh" : ageMs < 1e4 ? "aging" : "stale",
|
|
607
|
+
observedAt: frame.timestamp,
|
|
608
|
+
ageMs
|
|
609
|
+
},
|
|
610
|
+
confidence: { level: "unknown" }
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
// src/device-state/headingSource.ts
|
|
615
|
+
import * as ExpoLocation from "expo-location";
|
|
616
|
+
|
|
617
|
+
// src/device-state/turnDetector.ts
|
|
618
|
+
function signedDelta(a, b) {
|
|
619
|
+
return (b - a + 540) % 360 - 180;
|
|
620
|
+
}
|
|
621
|
+
var TurnDetector = class {
|
|
622
|
+
constructor(opts = {}) {
|
|
623
|
+
/** The heading currently treated as "straight ahead". */
|
|
624
|
+
this.refDeg = null;
|
|
625
|
+
this.stableSince = 0;
|
|
626
|
+
this.lastTurn = null;
|
|
627
|
+
this.minTurnDeg = opts.minTurnDeg ?? 35;
|
|
628
|
+
this.stableDeg = opts.stableDeg ?? 12;
|
|
629
|
+
this.stableMs = opts.stableMs ?? 2500;
|
|
630
|
+
}
|
|
631
|
+
/** Feed one compass reading. `null` degrees means the compass said nothing. */
|
|
632
|
+
push(deg, now) {
|
|
633
|
+
if (deg === null) return null;
|
|
634
|
+
if (this.refDeg === null) {
|
|
635
|
+
this.refDeg = deg;
|
|
636
|
+
this.stableSince = now;
|
|
637
|
+
return null;
|
|
638
|
+
}
|
|
639
|
+
const delta = signedDelta(this.refDeg, deg);
|
|
640
|
+
if (Math.abs(delta) >= this.minTurnDeg) {
|
|
641
|
+
this.lastTurn = { deg: Math.round(delta), timestamp: now };
|
|
642
|
+
this.refDeg = deg;
|
|
643
|
+
this.stableSince = now;
|
|
644
|
+
return this.lastTurn;
|
|
645
|
+
}
|
|
646
|
+
if (Math.abs(delta) > this.stableDeg) {
|
|
647
|
+
this.stableSince = now;
|
|
648
|
+
return null;
|
|
649
|
+
}
|
|
650
|
+
if (now - this.stableSince >= this.stableMs) {
|
|
651
|
+
this.refDeg = deg;
|
|
652
|
+
this.stableSince = now;
|
|
653
|
+
}
|
|
654
|
+
return null;
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* The most recent completed turn, or null if none this session.
|
|
658
|
+
*
|
|
659
|
+
* It does NOT expire on its own: the age is derivable from `timestamp`, and
|
|
660
|
+
* "recent enough to mention" is a product judgement this layer must not make
|
|
661
|
+
* on the caller's behalf.
|
|
662
|
+
*/
|
|
663
|
+
last() {
|
|
664
|
+
return this.lastTurn;
|
|
665
|
+
}
|
|
666
|
+
reset() {
|
|
667
|
+
this.refDeg = null;
|
|
668
|
+
this.stableSince = 0;
|
|
669
|
+
this.lastTurn = null;
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
|
|
673
|
+
// src/device-state/headingSource.ts
|
|
674
|
+
var latest = null;
|
|
675
|
+
var sub = null;
|
|
676
|
+
var turns = new TurnDetector();
|
|
677
|
+
async function startHeading() {
|
|
678
|
+
if (sub) return;
|
|
679
|
+
try {
|
|
680
|
+
sub = await ExpoLocation.watchHeadingAsync((h) => {
|
|
681
|
+
const raw = h.trueHeading >= 0 ? h.trueHeading : h.magHeading;
|
|
682
|
+
const deg = typeof raw === "number" && raw >= 0 ? raw : null;
|
|
683
|
+
const cal = typeof h.accuracy === "number" && h.accuracy >= 0 ? h.accuracy : null;
|
|
684
|
+
latest = { headingDeg: deg, calibration: cal, timestamp: Date.now() };
|
|
685
|
+
turns.push(deg, Date.now());
|
|
686
|
+
});
|
|
687
|
+
} catch {
|
|
688
|
+
sub = null;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
function stopHeading() {
|
|
692
|
+
sub?.remove();
|
|
693
|
+
sub = null;
|
|
694
|
+
latest = null;
|
|
695
|
+
turns.reset();
|
|
696
|
+
}
|
|
697
|
+
function currentHeading() {
|
|
698
|
+
return latest ? { headingDeg: latest.headingDeg } : null;
|
|
699
|
+
}
|
|
700
|
+
function currentHeadingDetail() {
|
|
701
|
+
return latest;
|
|
702
|
+
}
|
|
703
|
+
function lastMeaningfulTurn() {
|
|
704
|
+
return turns.last();
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
// src/device-state/domains/spatial.ts
|
|
708
|
+
function createSpatialDomain(collectors, speedRef) {
|
|
709
|
+
const RECENT_PATH_MAX = 60;
|
|
710
|
+
const STATIONARY_MPS = 0.4;
|
|
711
|
+
let recentPath = [];
|
|
712
|
+
let latest2 = null;
|
|
713
|
+
let latestSpeed = null;
|
|
714
|
+
let latestCourse = null;
|
|
715
|
+
let stopWatch = null;
|
|
716
|
+
let sessionId = null;
|
|
717
|
+
const nonNegative = (v) => typeof v === "number" && v >= 0 ? v : null;
|
|
718
|
+
const derivedSpeed = () => {
|
|
719
|
+
if (recentPath.length < 2) return null;
|
|
720
|
+
const b = recentPath[recentPath.length - 1];
|
|
721
|
+
const a = recentPath[recentPath.length - 2];
|
|
722
|
+
const dtS = (b.timestamp - a.timestamp) / 1e3;
|
|
723
|
+
if (dtS <= 0 || dtS > 10) return null;
|
|
724
|
+
return distanceOf([a, b]) / dtS;
|
|
725
|
+
};
|
|
726
|
+
const derivedCourse = () => {
|
|
727
|
+
if (recentPath.length < 2) return null;
|
|
728
|
+
const b = recentPath[recentPath.length - 1];
|
|
729
|
+
const a = recentPath[recentPath.length - 2];
|
|
730
|
+
if (b.timestamp - a.timestamp > 1e4) return null;
|
|
731
|
+
if (distanceOf([a, b]) < 3) return null;
|
|
732
|
+
return bearingBetween(a, b);
|
|
733
|
+
};
|
|
734
|
+
return {
|
|
735
|
+
async getSnapshot() {
|
|
736
|
+
let point = latest2;
|
|
737
|
+
let speedMps = latestSpeed;
|
|
738
|
+
let courseDeg = latestCourse;
|
|
739
|
+
let derived = false;
|
|
740
|
+
if (!point) {
|
|
741
|
+
try {
|
|
742
|
+
if (!collectors.location) throw unsupported("location collector");
|
|
743
|
+
const fix = await collectors.location.getPosition({ accuracy: "low" });
|
|
744
|
+
point = {
|
|
745
|
+
latitude: fix.latitude,
|
|
746
|
+
longitude: fix.longitude,
|
|
747
|
+
altitude: fix.altitude ?? null,
|
|
748
|
+
accuracyM: nonNegative(fix.accuracy),
|
|
749
|
+
timestamp: fix.timestamp ?? Date.now()
|
|
750
|
+
};
|
|
751
|
+
speedMps = nonNegative(fix.speed);
|
|
752
|
+
courseDeg = nonNegative(fix.course);
|
|
753
|
+
} catch {
|
|
754
|
+
return {
|
|
755
|
+
position: null,
|
|
756
|
+
movement: { speedMps: null, bearingDeg: null, distanceRecentM: null, state: "unknown" },
|
|
757
|
+
orientation: { headingDeg: null },
|
|
758
|
+
recentPath: [],
|
|
759
|
+
quality: qualityUnknown()
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
if (speedMps === null) {
|
|
764
|
+
const d = derivedSpeed();
|
|
765
|
+
if (d !== null) {
|
|
766
|
+
speedMps = d;
|
|
767
|
+
derived = true;
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
if (courseDeg === null) {
|
|
771
|
+
const d = derivedCourse();
|
|
772
|
+
if (d !== null) {
|
|
773
|
+
courseDeg = d;
|
|
774
|
+
derived = true;
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
const heading = currentHeadingDetail();
|
|
778
|
+
const turn = lastMeaningfulTurn();
|
|
779
|
+
return {
|
|
780
|
+
position: point,
|
|
781
|
+
movement: {
|
|
782
|
+
speedMps,
|
|
783
|
+
bearingDeg: courseDeg,
|
|
784
|
+
// Needs two points from the SAME session; without a stream there is
|
|
785
|
+
// nothing to measure against, and a distance of 0 would read as
|
|
786
|
+
// "the user has not moved".
|
|
787
|
+
distanceRecentM: recentPath.length >= 2 ? distanceOf(recentPath) : null,
|
|
788
|
+
// Only claim stationary/moving when a speed was actually obtained.
|
|
789
|
+
// With no speed the truthful answer is `unknown`, not `stationary` —
|
|
790
|
+
// "you have stopped" is a false statement, not a neutral default.
|
|
791
|
+
state: speedMps === null ? "unknown" : speedMps < STATIONARY_MPS ? "stationary" : "moving"
|
|
792
|
+
},
|
|
793
|
+
orientation: {
|
|
794
|
+
headingDeg: heading?.headingDeg ?? null,
|
|
795
|
+
// NOT the calibration rank. iOS reports 0–3 (none…high), which is not
|
|
796
|
+
// a number of degrees, and converting one to the other would be an
|
|
797
|
+
// invented error bar. The rank drives `confidence` below instead.
|
|
798
|
+
headingAccuracyDeg: null
|
|
799
|
+
},
|
|
800
|
+
changes: {
|
|
801
|
+
lastMeaningfulTurnDeg: turn ? turn.deg : null,
|
|
802
|
+
timestamp: turn ? turn.timestamp : null
|
|
803
|
+
},
|
|
804
|
+
recentPath: [...recentPath],
|
|
805
|
+
quality: qualityFrom(
|
|
806
|
+
point.timestamp,
|
|
807
|
+
spatialConfidence(point.accuracyM ?? null, heading?.calibration ?? null, derived)
|
|
808
|
+
)
|
|
809
|
+
};
|
|
810
|
+
},
|
|
811
|
+
/**
|
|
812
|
+
* Open the warm window: position stream + compass.
|
|
813
|
+
*
|
|
814
|
+
* The intent is recorded but does NOT yet change sampling — no native
|
|
815
|
+
* strategy layer exists — so it is accepted rather than refused only
|
|
816
|
+
* because the streams it opens are real and useful.
|
|
817
|
+
*/
|
|
818
|
+
async start(options) {
|
|
819
|
+
if (stopWatch) return { id: sessionId, intent: options.intent, startedAt: Date.now() };
|
|
820
|
+
sessionId = `spatial_${Date.now()}`;
|
|
821
|
+
if (!collectors.location) throw unsupported("location collector");
|
|
822
|
+
void startHeading();
|
|
823
|
+
stopWatch = collectors.location.watchPosition((pos) => {
|
|
824
|
+
const p = {
|
|
825
|
+
latitude: pos.latitude,
|
|
826
|
+
longitude: pos.longitude,
|
|
827
|
+
altitude: pos.altitude ?? null,
|
|
828
|
+
accuracyM: nonNegative(pos.accuracy),
|
|
829
|
+
timestamp: pos.timestamp ?? Date.now()
|
|
830
|
+
};
|
|
831
|
+
latest2 = p;
|
|
832
|
+
latestSpeed = nonNegative(pos.speed);
|
|
833
|
+
latestCourse = nonNegative(pos.course);
|
|
834
|
+
speedRef.mps = latestSpeed;
|
|
835
|
+
speedRef.at = p.timestamp;
|
|
836
|
+
recentPath = [...recentPath, p].slice(-RECENT_PATH_MAX);
|
|
837
|
+
});
|
|
838
|
+
return { id: sessionId, intent: options.intent, startedAt: Date.now() };
|
|
839
|
+
},
|
|
840
|
+
async stop() {
|
|
841
|
+
stopWatch?.();
|
|
842
|
+
stopWatch = null;
|
|
843
|
+
sessionId = null;
|
|
844
|
+
recentPath = [];
|
|
845
|
+
latest2 = null;
|
|
846
|
+
latestSpeed = null;
|
|
847
|
+
latestCourse = null;
|
|
848
|
+
speedRef.mps = null;
|
|
849
|
+
speedRef.at = 0;
|
|
850
|
+
if (!visualEngine.getSession()) stopHeading();
|
|
851
|
+
},
|
|
852
|
+
// No event vocabulary is frozen for spatial, and inventing names here would
|
|
853
|
+
// put them in plugins before the freeze can contradict them.
|
|
854
|
+
subscribe() {
|
|
855
|
+
throw unsupported("spatial.subscribe");
|
|
856
|
+
}
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
function spatialConfidence(accuracyM, calibration, derived) {
|
|
860
|
+
const rank = (v) => v === "high" ? 3 : v === "medium" ? 2 : v === "low" ? 1 : 0;
|
|
861
|
+
const fromFix = accuracyM === null ? "unknown" : accuracyM <= 10 ? "high" : accuracyM <= 50 ? "medium" : "low";
|
|
862
|
+
const fromCompass = calibration === null ? "unknown" : calibration >= 3 ? "high" : calibration >= 2 ? "medium" : "low";
|
|
863
|
+
const parts = [fromFix, fromCompass, ...derived ? ["low"] : []];
|
|
864
|
+
const known = parts.filter((p) => p !== "unknown");
|
|
865
|
+
if (!known.length) return { level: "unknown" };
|
|
866
|
+
const worst = known.reduce((a, b) => rank(a) <= rank(b) ? a : b);
|
|
867
|
+
return { level: worst };
|
|
868
|
+
}
|
|
869
|
+
function bearingBetween(a, b) {
|
|
870
|
+
const toRad = (d) => d * Math.PI / 180;
|
|
871
|
+
const la1 = toRad(a.latitude), la2 = toRad(b.latitude);
|
|
872
|
+
const dLon = toRad(b.longitude - a.longitude);
|
|
873
|
+
const y = Math.sin(dLon) * Math.cos(la2);
|
|
874
|
+
const x = Math.cos(la1) * Math.sin(la2) - Math.sin(la1) * Math.cos(la2) * Math.cos(dLon);
|
|
875
|
+
return (Math.atan2(y, x) * 180 / Math.PI + 360) % 360;
|
|
876
|
+
}
|
|
877
|
+
function distanceOf(path) {
|
|
878
|
+
const R = 6371e3;
|
|
879
|
+
let total = 0;
|
|
880
|
+
for (let i = 1; i < path.length; i++) {
|
|
881
|
+
const a = path[i - 1], b = path[i];
|
|
882
|
+
const dLat = (b.latitude - a.latitude) * Math.PI / 180;
|
|
883
|
+
const dLon = (b.longitude - a.longitude) * Math.PI / 180;
|
|
884
|
+
const la1 = a.latitude * Math.PI / 180, la2 = b.latitude * Math.PI / 180;
|
|
885
|
+
const h = Math.sin(dLat / 2) ** 2 + Math.cos(la1) * Math.cos(la2) * Math.sin(dLon / 2) ** 2;
|
|
886
|
+
total += 2 * R * Math.asin(Math.sqrt(h));
|
|
887
|
+
}
|
|
888
|
+
return Math.round(total);
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
// src/device-state/domains/visual.ts
|
|
892
|
+
function createVisualDomain() {
|
|
893
|
+
return {
|
|
894
|
+
async getSnapshot() {
|
|
895
|
+
const session = visualEngine.getSession();
|
|
896
|
+
const latest2 = visualEngine.latest();
|
|
897
|
+
const timeline = visualEngine.timeline();
|
|
898
|
+
return {
|
|
899
|
+
camera: {
|
|
900
|
+
// `available` = the app could use a camera at all. `active` = one is
|
|
901
|
+
// running RIGHT NOW. They differ exactly when it matters: a pocketed
|
|
902
|
+
// phone is available but not active, and answering `true` to both
|
|
903
|
+
// would let a guide speak about a scene nothing is watching.
|
|
904
|
+
available: !!session,
|
|
905
|
+
active: visualEngine.isCameraReady(),
|
|
906
|
+
facing: session ? "back" : "unknown"
|
|
907
|
+
},
|
|
908
|
+
// null, not a stale frame dressed as current. A caller with no usable
|
|
909
|
+
// frame must see nothing rather than something old.
|
|
910
|
+
latest: latest2,
|
|
911
|
+
recentTimeline: timeline,
|
|
912
|
+
quality: frameQuality(latest2)
|
|
913
|
+
};
|
|
914
|
+
},
|
|
915
|
+
async start(options) {
|
|
916
|
+
const s = visualEngine.startSession(options?.intent || "navigation");
|
|
917
|
+
visualEngine.setContextProvider({ heading: currentHeading });
|
|
918
|
+
void startHeading();
|
|
919
|
+
return { id: s.id, intent: s.intent, startedAt: s.startedAt };
|
|
920
|
+
},
|
|
921
|
+
async stop() {
|
|
922
|
+
visualEngine.stopSession();
|
|
923
|
+
visualEngine.setContextProvider(null);
|
|
924
|
+
stopHeading();
|
|
925
|
+
},
|
|
926
|
+
/**
|
|
927
|
+
* A deliberate one-shot. Distinct from getSnapshot because it is an
|
|
928
|
+
* OPERATION: it waits for a frame rather than serving the newest one.
|
|
929
|
+
*
|
|
930
|
+
* Throws when it cannot, rather than returning an empty result — the
|
|
931
|
+
* design is explicit that capture is an operation and reads are state.
|
|
932
|
+
*/
|
|
933
|
+
async capture() {
|
|
934
|
+
if (!visualEngine.getSession()) {
|
|
935
|
+
throw unsupported("visual.capture without an open session \u2014 call start({ intent }) first");
|
|
936
|
+
}
|
|
937
|
+
if (!visualEngine.isCameraReady()) {
|
|
938
|
+
const err2 = new Error(
|
|
939
|
+
"The camera is not running right now \u2014 the app may be in the background."
|
|
940
|
+
);
|
|
941
|
+
err2.code = "TEMPORARILY_UNAVAILABLE";
|
|
942
|
+
throw err2;
|
|
943
|
+
}
|
|
944
|
+
const before = visualEngine.latest()?.timestamp ?? 0;
|
|
945
|
+
const deadline = Date.now() + 4e3;
|
|
946
|
+
while (Date.now() < deadline) {
|
|
947
|
+
const f = visualEngine.latest();
|
|
948
|
+
if (f && f.timestamp > before) return { frame: f };
|
|
949
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
950
|
+
}
|
|
951
|
+
const err = new Error("No frame arrived within 4s.");
|
|
952
|
+
err.code = "TIMEOUT";
|
|
953
|
+
throw err;
|
|
954
|
+
},
|
|
955
|
+
subscribe(listener) {
|
|
956
|
+
const off = visualEngine.subscribe(() => {
|
|
957
|
+
listener({ type: "visualStateChanged" });
|
|
958
|
+
});
|
|
959
|
+
return { unsubscribe: off };
|
|
960
|
+
}
|
|
961
|
+
};
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
// src/device-state/activityEngine.ts
|
|
965
|
+
var sensors = null;
|
|
966
|
+
var sensorsMissing = false;
|
|
967
|
+
function loadSensors() {
|
|
968
|
+
if (sensors || sensorsMissing) return sensors;
|
|
969
|
+
try {
|
|
970
|
+
sensors = __require("expo-sensors");
|
|
971
|
+
} catch {
|
|
972
|
+
sensorsMissing = true;
|
|
973
|
+
sensors = null;
|
|
974
|
+
}
|
|
975
|
+
return sensors;
|
|
976
|
+
}
|
|
977
|
+
var ACCEL_INTERVAL_MS = 200;
|
|
978
|
+
var MOTION_WINDOW_MS = 3e3;
|
|
979
|
+
var STEP_WINDOW_MS = 2e4;
|
|
980
|
+
var GRAVITY_ALPHA = 0.1;
|
|
981
|
+
var ActivityEngine = class {
|
|
982
|
+
constructor() {
|
|
983
|
+
this.accelSub = null;
|
|
984
|
+
this.stepSub = null;
|
|
985
|
+
this.magnitudes = [];
|
|
986
|
+
this.gravity = null;
|
|
987
|
+
this.steps = [];
|
|
988
|
+
this.sessionId = null;
|
|
989
|
+
this.startedAt = 0;
|
|
990
|
+
/** When the CURRENT reading last changed — feeds `human.since`. */
|
|
991
|
+
this.pedometerAvailable = null;
|
|
992
|
+
// ── transitions ──
|
|
993
|
+
//
|
|
994
|
+
// Tracked on a timer inside the engine rather than computed when someone
|
|
995
|
+
// reads, because a transition is an EVENT: "you started walking" happens
|
|
996
|
+
// between two reads, and a domain that only classifies on demand reports the
|
|
997
|
+
// state and loses the moment it changed. `since` has the same problem.
|
|
998
|
+
this.current = "unknown";
|
|
999
|
+
this.since = null;
|
|
1000
|
+
this.transitions = [];
|
|
1001
|
+
this.ticker = null;
|
|
1002
|
+
}
|
|
1003
|
+
isRunning() {
|
|
1004
|
+
return this.sessionId !== null;
|
|
1005
|
+
}
|
|
1006
|
+
session() {
|
|
1007
|
+
return this.sessionId ? { id: this.sessionId, startedAt: this.startedAt } : null;
|
|
1008
|
+
}
|
|
1009
|
+
/**
|
|
1010
|
+
* @param classify how to name the current activity — injected so the engine
|
|
1011
|
+
* keeps no opinion of its own and the classifier stays pure and testable.
|
|
1012
|
+
*/
|
|
1013
|
+
async start(classify) {
|
|
1014
|
+
if (this.sessionId) return this.session();
|
|
1015
|
+
this.sessionId = `activity_${Date.now()}`;
|
|
1016
|
+
this.startedAt = Date.now();
|
|
1017
|
+
const s = loadSensors();
|
|
1018
|
+
if (!s) {
|
|
1019
|
+
this.pedometerAvailable = false;
|
|
1020
|
+
this.startTicking(classify);
|
|
1021
|
+
return this.session();
|
|
1022
|
+
}
|
|
1023
|
+
const { Accelerometer, Pedometer } = s;
|
|
1024
|
+
Accelerometer.setUpdateInterval(ACCEL_INTERVAL_MS);
|
|
1025
|
+
this.accelSub = Accelerometer.addListener(({ x, y, z }) => {
|
|
1026
|
+
const now = Date.now();
|
|
1027
|
+
this.magnitudes.push({ m: Math.sqrt(x * x + y * y + z * z), t: now });
|
|
1028
|
+
const cutoff = now - MOTION_WINDOW_MS;
|
|
1029
|
+
if (this.magnitudes.length > 8 && this.magnitudes[0].t < cutoff) {
|
|
1030
|
+
this.magnitudes = this.magnitudes.filter((s2) => s2.t >= cutoff);
|
|
1031
|
+
}
|
|
1032
|
+
this.gravity = this.gravity ? {
|
|
1033
|
+
x: this.gravity.x + GRAVITY_ALPHA * (x - this.gravity.x),
|
|
1034
|
+
y: this.gravity.y + GRAVITY_ALPHA * (y - this.gravity.y),
|
|
1035
|
+
z: this.gravity.z + GRAVITY_ALPHA * (z - this.gravity.z)
|
|
1036
|
+
} : { x, y, z };
|
|
1037
|
+
});
|
|
1038
|
+
try {
|
|
1039
|
+
this.pedometerAvailable = await Pedometer.isAvailableAsync();
|
|
1040
|
+
} catch {
|
|
1041
|
+
this.pedometerAvailable = false;
|
|
1042
|
+
}
|
|
1043
|
+
if (this.pedometerAvailable) {
|
|
1044
|
+
try {
|
|
1045
|
+
this.stepSub = Pedometer.watchStepCount(({ steps }) => {
|
|
1046
|
+
this.steps.push({ steps, t: Date.now() });
|
|
1047
|
+
const cutoff = Date.now() - STEP_WINDOW_MS;
|
|
1048
|
+
if (this.steps.length > 2 && this.steps[0].t < cutoff) {
|
|
1049
|
+
this.steps = this.steps.filter((s2) => s2.t >= cutoff);
|
|
1050
|
+
}
|
|
1051
|
+
});
|
|
1052
|
+
} catch {
|
|
1053
|
+
this.stepSub = null;
|
|
1054
|
+
this.pedometerAvailable = false;
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
this.startTicking(classify);
|
|
1058
|
+
return this.session();
|
|
1059
|
+
}
|
|
1060
|
+
stop() {
|
|
1061
|
+
this.accelSub?.remove();
|
|
1062
|
+
this.stepSub?.remove();
|
|
1063
|
+
this.accelSub = null;
|
|
1064
|
+
this.stepSub = null;
|
|
1065
|
+
this.sessionId = null;
|
|
1066
|
+
this.stopTicking();
|
|
1067
|
+
this.magnitudes = [];
|
|
1068
|
+
this.steps = [];
|
|
1069
|
+
this.gravity = null;
|
|
1070
|
+
}
|
|
1071
|
+
/** Variance of accelerometer magnitude over the motion window, in g². */
|
|
1072
|
+
accelVariance() {
|
|
1073
|
+
const cutoff = Date.now() - MOTION_WINDOW_MS;
|
|
1074
|
+
const window = this.magnitudes.filter((s) => s.t >= cutoff);
|
|
1075
|
+
if (window.length < 5) return null;
|
|
1076
|
+
const mean = window.reduce((a, s) => a + s.m, 0) / window.length;
|
|
1077
|
+
return window.reduce((a, s) => a + (s.m - mean) ** 2, 0) / window.length;
|
|
1078
|
+
}
|
|
1079
|
+
currentGravity() {
|
|
1080
|
+
return this.gravity;
|
|
1081
|
+
}
|
|
1082
|
+
/**
|
|
1083
|
+
* Steps per minute over the step window.
|
|
1084
|
+
*
|
|
1085
|
+
* Computed from the DIFFERENCE between the first and last cumulative counts,
|
|
1086
|
+
* so it does not matter whether the platform counts from subscription start
|
|
1087
|
+
* or from midnight.
|
|
1088
|
+
*
|
|
1089
|
+
* Returns null — not 0 — when the window is too short to divide by. Zero
|
|
1090
|
+
* steps per minute is a real claim ("standing still") and must only be made
|
|
1091
|
+
* once there is a window long enough to support it.
|
|
1092
|
+
*/
|
|
1093
|
+
cadenceSpm() {
|
|
1094
|
+
const cutoff = Date.now() - STEP_WINDOW_MS;
|
|
1095
|
+
const window = this.steps.filter((s) => s.t >= cutoff);
|
|
1096
|
+
if (window.length < 2) {
|
|
1097
|
+
if (this.pedometerAvailable && Date.now() - this.startedAt > STEP_WINDOW_MS) return 0;
|
|
1098
|
+
return null;
|
|
1099
|
+
}
|
|
1100
|
+
const first = window[0], last = window[window.length - 1];
|
|
1101
|
+
const minutes = (last.t - first.t) / 6e4;
|
|
1102
|
+
if (minutes <= 0) return null;
|
|
1103
|
+
return (last.steps - first.steps) / minutes;
|
|
1104
|
+
}
|
|
1105
|
+
/** Whether a pedometer exists and is permitted. Null before `start()`. */
|
|
1106
|
+
hasPedometer() {
|
|
1107
|
+
return this.pedometerAvailable;
|
|
1108
|
+
}
|
|
1109
|
+
/** When the newest sample arrived — what freshness is actually about. */
|
|
1110
|
+
observedAt() {
|
|
1111
|
+
const ts = [
|
|
1112
|
+
this.magnitudes[this.magnitudes.length - 1]?.t,
|
|
1113
|
+
this.steps[this.steps.length - 1]?.t
|
|
1114
|
+
].filter((t) => typeof t === "number");
|
|
1115
|
+
return ts.length ? Math.max(...ts) : null;
|
|
1116
|
+
}
|
|
1117
|
+
/** Feed the engine its own classification once a second. */
|
|
1118
|
+
startTicking(classify) {
|
|
1119
|
+
this.ticker = setInterval(() => {
|
|
1120
|
+
const next = classify();
|
|
1121
|
+
if (next === this.current) return;
|
|
1122
|
+
this.current = next;
|
|
1123
|
+
this.since = Date.now();
|
|
1124
|
+
this.transitions = [...this.transitions, { name: next, timestamp: this.since }].slice(-10);
|
|
1125
|
+
}, 1e3);
|
|
1126
|
+
}
|
|
1127
|
+
stopTicking() {
|
|
1128
|
+
if (this.ticker) clearInterval(this.ticker);
|
|
1129
|
+
this.ticker = null;
|
|
1130
|
+
this.current = "unknown";
|
|
1131
|
+
this.since = null;
|
|
1132
|
+
this.transitions = [];
|
|
1133
|
+
}
|
|
1134
|
+
/** When the current activity began, or null if it has not been established. */
|
|
1135
|
+
currentSince() {
|
|
1136
|
+
return this.since;
|
|
1137
|
+
}
|
|
1138
|
+
/** Newest last, bounded. Empty is a real answer: nothing has changed yet. */
|
|
1139
|
+
recentTransitions() {
|
|
1140
|
+
return [...this.transitions];
|
|
1141
|
+
}
|
|
1142
|
+
};
|
|
1143
|
+
var activityEngine = new ActivityEngine();
|
|
1144
|
+
|
|
1145
|
+
// src/device-state/activityClassifier.ts
|
|
1146
|
+
var RUN_SPM = 140;
|
|
1147
|
+
var WALK_SPM = 20;
|
|
1148
|
+
var STILL_VAR = 2e-3;
|
|
1149
|
+
var ACTIVE_VAR = 0.02;
|
|
1150
|
+
var VEHICLE_MPS = 3;
|
|
1151
|
+
var AXIS_DOMINANT = 0.75;
|
|
1152
|
+
function classifyActivity(input) {
|
|
1153
|
+
return { human: classifyHuman(input), pose: classifyPose(input) };
|
|
1154
|
+
}
|
|
1155
|
+
function classifyHuman({ cadenceSpm, accelVariance, speedMps }) {
|
|
1156
|
+
if (cadenceSpm !== null && cadenceSpm >= RUN_SPM) {
|
|
1157
|
+
return { state: "running", confidence: { level: "high" } };
|
|
1158
|
+
}
|
|
1159
|
+
if (cadenceSpm !== null && cadenceSpm >= WALK_SPM) {
|
|
1160
|
+
return { state: "walking", confidence: { level: "high" } };
|
|
1161
|
+
}
|
|
1162
|
+
if (speedMps !== null && speedMps >= VEHICLE_MPS) {
|
|
1163
|
+
return { state: "unknown", confidence: { level: "low" } };
|
|
1164
|
+
}
|
|
1165
|
+
if (accelVariance !== null && accelVariance < STILL_VAR && (cadenceSpm === null || cadenceSpm < WALK_SPM)) {
|
|
1166
|
+
return { state: "stationary", confidence: { level: "medium" } };
|
|
1167
|
+
}
|
|
1168
|
+
return { state: "unknown", confidence: { level: "unknown" } };
|
|
1169
|
+
}
|
|
1170
|
+
function classifyPose({ gravity, accelVariance }) {
|
|
1171
|
+
if (!gravity) {
|
|
1172
|
+
return { placement: "unknown", orientation: "unknown", confidence: { level: "unknown" } };
|
|
1173
|
+
}
|
|
1174
|
+
const { x, y, z } = gravity;
|
|
1175
|
+
const orientation = Math.abs(z) >= AXIS_DOMINANT ? "flat" : Math.abs(y) >= AXIS_DOMINANT ? "portrait" : Math.abs(x) >= AXIS_DOMINANT ? "landscape" : "unknown";
|
|
1176
|
+
if (orientation === "flat" && accelVariance !== null && accelVariance < STILL_VAR) {
|
|
1177
|
+
return { placement: "table", orientation, confidence: { level: "high" } };
|
|
1178
|
+
}
|
|
1179
|
+
if (orientation !== "flat" && accelVariance !== null && accelVariance >= ACTIVE_VAR) {
|
|
1180
|
+
return { placement: "handheld", orientation, confidence: { level: "medium" } };
|
|
1181
|
+
}
|
|
1182
|
+
return { placement: "unknown", orientation, confidence: { level: "unknown" } };
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
// src/device-state/domains/activity.ts
|
|
1186
|
+
function createActivityDomain(speedRef) {
|
|
1187
|
+
const SPEED_MAX_AGE_MS = 15e3;
|
|
1188
|
+
const input = () => ({
|
|
1189
|
+
cadenceSpm: activityEngine.cadenceSpm(),
|
|
1190
|
+
accelVariance: activityEngine.accelVariance(),
|
|
1191
|
+
gravity: activityEngine.currentGravity(),
|
|
1192
|
+
speedMps: speedRef.at && Date.now() - speedRef.at < SPEED_MAX_AGE_MS ? speedRef.mps : null
|
|
1193
|
+
});
|
|
1194
|
+
return {
|
|
1195
|
+
async getSnapshot() {
|
|
1196
|
+
if (!activityEngine.isRunning()) {
|
|
1197
|
+
return {
|
|
1198
|
+
human: { state: "unknown", since: null, confidence: { level: "unknown" } },
|
|
1199
|
+
devicePose: { placement: "unknown", orientation: "unknown", confidence: { level: "unknown" } },
|
|
1200
|
+
recentTransitions: [],
|
|
1201
|
+
quality: qualityUnknown()
|
|
1202
|
+
};
|
|
1203
|
+
}
|
|
1204
|
+
const verdict = classifyActivity(input());
|
|
1205
|
+
const observedAt = activityEngine.observedAt();
|
|
1206
|
+
const transitions = activityEngine.recentTransitions();
|
|
1207
|
+
return {
|
|
1208
|
+
human: {
|
|
1209
|
+
state: verdict.human.state,
|
|
1210
|
+
since: activityEngine.currentSince(),
|
|
1211
|
+
confidence: verdict.human.confidence
|
|
1212
|
+
},
|
|
1213
|
+
devicePose: {
|
|
1214
|
+
placement: verdict.pose.placement,
|
|
1215
|
+
orientation: verdict.pose.orientation,
|
|
1216
|
+
confidence: verdict.pose.confidence
|
|
1217
|
+
},
|
|
1218
|
+
transition: {
|
|
1219
|
+
current: verdict.human.state,
|
|
1220
|
+
// A hint, and we have no basis for one. The freeze calls `likelyNext`
|
|
1221
|
+
// advisory; advisory is not licence to invent it.
|
|
1222
|
+
likelyNext: null
|
|
1223
|
+
},
|
|
1224
|
+
recentTransitions: transitions,
|
|
1225
|
+
quality: observedAt === null ? qualityUnknown() : qualityFrom(observedAt, verdict.human.confidence)
|
|
1226
|
+
};
|
|
1227
|
+
},
|
|
1228
|
+
/**
|
|
1229
|
+
* Start sampling. The intent is recorded but does not yet change strategy —
|
|
1230
|
+
* there is no native power-tuning layer for it to reach.
|
|
1231
|
+
*/
|
|
1232
|
+
async start(options) {
|
|
1233
|
+
const s = await activityEngine.start(() => classifyActivity(input()).human.state);
|
|
1234
|
+
return { id: s.id, intent: options.intent, startedAt: s.startedAt };
|
|
1235
|
+
},
|
|
1236
|
+
async stop() {
|
|
1237
|
+
activityEngine.stop();
|
|
1238
|
+
},
|
|
1239
|
+
// No activity event vocabulary is frozen — the design notes there is no
|
|
1240
|
+
// "Initial Activity Events" list at all — so naming events here would put
|
|
1241
|
+
// words in plugins that the freeze may contradict.
|
|
1242
|
+
subscribe() {
|
|
1243
|
+
throw unsupported("activity.subscribe");
|
|
1244
|
+
}
|
|
1245
|
+
};
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
// src/device-state/index.ts
|
|
1249
|
+
function createDeviceState(collectors = {}) {
|
|
1250
|
+
const speedRef = { mps: null, at: 0 };
|
|
1251
|
+
return {
|
|
1252
|
+
deviceContext: createDeviceContextDomain(),
|
|
1253
|
+
connectivity: createConnectivityDomain(collectors),
|
|
1254
|
+
capability: createCapabilityDomain(collectors),
|
|
1255
|
+
interaction: createInteractionDomain(),
|
|
1256
|
+
spatial: createSpatialDomain(collectors, speedRef),
|
|
1257
|
+
visual: createVisualDomain(),
|
|
1258
|
+
activity: createActivityDomain(speedRef)
|
|
1259
|
+
};
|
|
1260
|
+
}
|
|
1261
|
+
var IMPLEMENTED_DOMAINS = [
|
|
1262
|
+
"deviceContext",
|
|
1263
|
+
"connectivity",
|
|
1264
|
+
"capability",
|
|
1265
|
+
"interaction",
|
|
1266
|
+
"spatial",
|
|
1267
|
+
"visual",
|
|
1268
|
+
"activity"
|
|
1269
|
+
];
|
|
1270
|
+
|
|
1271
|
+
// src/camera/CameraTaskHost.tsx
|
|
1272
|
+
import { useEffect, useRef, useState } from "react";
|
|
1273
|
+
import { Pressable, StyleSheet, Text, View } from "react-native";
|
|
1274
|
+
import { CameraView, useCameraPermissions, useMicrophonePermissions } from "expo-camera";
|
|
1275
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
1276
|
+
var SCAN_TIMEOUT_MS = 3e4;
|
|
1277
|
+
var MAX_RECORD_MS = 6e4;
|
|
1278
|
+
function CameraTaskHost() {
|
|
1279
|
+
const [task, setTask] = useState(cameraTasks.current());
|
|
1280
|
+
const [camPermission, requestCamPermission] = useCameraPermissions();
|
|
1281
|
+
const [micPermission, requestMicPermission] = useMicrophonePermissions();
|
|
1282
|
+
const cameraRef = useRef(null);
|
|
1283
|
+
const settling = useRef(false);
|
|
1284
|
+
useEffect(() => {
|
|
1285
|
+
cameraTasks.setHostMounted(true);
|
|
1286
|
+
return () => cameraTasks.setHostMounted(false);
|
|
1287
|
+
}, []);
|
|
1288
|
+
useEffect(() => cameraTasks.subscribe(() => {
|
|
1289
|
+
settling.current = false;
|
|
1290
|
+
setTask(cameraTasks.current());
|
|
1291
|
+
}), []);
|
|
1292
|
+
useEffect(() => {
|
|
1293
|
+
if (!task) return;
|
|
1294
|
+
let cancelled = false;
|
|
1295
|
+
void (async () => {
|
|
1296
|
+
const cam = camPermission?.granted ? camPermission : await requestCamPermission();
|
|
1297
|
+
if (cancelled) return;
|
|
1298
|
+
if (!cam?.granted) {
|
|
1299
|
+
cameraTasks.fail(cameraError(
|
|
1300
|
+
"PERMISSION_DENIED",
|
|
1301
|
+
"Camera permission was not granted. Enable it for this app in Settings > Privacy > Camera."
|
|
1302
|
+
));
|
|
1303
|
+
return;
|
|
1304
|
+
}
|
|
1305
|
+
if (task.kind === "record" && task.opts.withAudio) {
|
|
1306
|
+
const mic = micPermission?.granted ? micPermission : await requestMicPermission();
|
|
1307
|
+
if (cancelled) return;
|
|
1308
|
+
if (!mic?.granted) {
|
|
1309
|
+
cameraTasks.fail(cameraError(
|
|
1310
|
+
"PERMISSION_DENIED",
|
|
1311
|
+
"Microphone permission was not granted, so a video with sound cannot be recorded."
|
|
1312
|
+
));
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
})();
|
|
1316
|
+
return () => {
|
|
1317
|
+
cancelled = true;
|
|
1318
|
+
};
|
|
1319
|
+
}, [task]);
|
|
1320
|
+
useEffect(() => {
|
|
1321
|
+
if (task?.kind !== "scan") return;
|
|
1322
|
+
const ms = task.opts.timeoutMs ?? SCAN_TIMEOUT_MS;
|
|
1323
|
+
const t = setTimeout(() => {
|
|
1324
|
+
cameraTasks.fail(cameraError("TIMEOUT", `No barcode was found within ${Math.round(ms / 1e3)}s.`));
|
|
1325
|
+
}, ms);
|
|
1326
|
+
return () => clearTimeout(t);
|
|
1327
|
+
}, [task]);
|
|
1328
|
+
useEffect(() => {
|
|
1329
|
+
if (task?.kind !== "record" || !camPermission?.granted) return;
|
|
1330
|
+
let cancelled = false;
|
|
1331
|
+
const maxMs = Math.min(task.opts.maxDurationMs ?? 15e3, MAX_RECORD_MS);
|
|
1332
|
+
const startedAt = Date.now();
|
|
1333
|
+
void (async () => {
|
|
1334
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
1335
|
+
if (cancelled || !cameraRef.current) return;
|
|
1336
|
+
try {
|
|
1337
|
+
const video = await cameraRef.current.recordAsync({ maxDuration: Math.ceil(maxMs / 1e3) });
|
|
1338
|
+
if (cancelled) return;
|
|
1339
|
+
if (!video?.uri) {
|
|
1340
|
+
cameraTasks.fail(cameraError("INTERNAL", "Recording finished but produced no file."));
|
|
1341
|
+
return;
|
|
1342
|
+
}
|
|
1343
|
+
cameraTasks.complete({ uri: video.uri, durationMs: Date.now() - startedAt });
|
|
1344
|
+
} catch (err) {
|
|
1345
|
+
if (!cancelled) cameraTasks.fail(cameraError("INTERNAL", err?.message || "Recording failed."));
|
|
1346
|
+
}
|
|
1347
|
+
})();
|
|
1348
|
+
return () => {
|
|
1349
|
+
cancelled = true;
|
|
1350
|
+
try {
|
|
1351
|
+
cameraRef.current?.stopRecording();
|
|
1352
|
+
} catch {
|
|
1353
|
+
}
|
|
1354
|
+
};
|
|
1355
|
+
}, [task, camPermission?.granted]);
|
|
1356
|
+
if (!task || !camPermission?.granted) return null;
|
|
1357
|
+
const isScan = task.kind === "scan";
|
|
1358
|
+
return /* @__PURE__ */ jsxs(View, { style: styles.overlay, children: [
|
|
1359
|
+
/* @__PURE__ */ jsx(
|
|
1360
|
+
CameraView,
|
|
1361
|
+
{
|
|
1362
|
+
ref: cameraRef,
|
|
1363
|
+
style: StyleSheet.absoluteFill,
|
|
1364
|
+
facing: "back",
|
|
1365
|
+
mode: isScan ? "picture" : "video",
|
|
1366
|
+
barcodeScannerSettings: isScan && task.opts.formats?.length ? { barcodeTypes: task.opts.formats } : void 0,
|
|
1367
|
+
onBarcodeScanned: isScan ? ({ data, type }) => {
|
|
1368
|
+
if (settling.current) return;
|
|
1369
|
+
settling.current = true;
|
|
1370
|
+
cameraTasks.complete({ data, format: type });
|
|
1371
|
+
} : void 0
|
|
1372
|
+
}
|
|
1373
|
+
),
|
|
1374
|
+
/* @__PURE__ */ jsxs(View, { style: styles.hud, pointerEvents: "box-none", children: [
|
|
1375
|
+
/* @__PURE__ */ jsx(Text, { style: styles.hint, children: isScan ? "Point the camera at the code" : "Recording\u2026" }),
|
|
1376
|
+
/* @__PURE__ */ jsx(Pressable, { style: styles.cancel, onPress: () => cameraTasks.cancel(), accessibilityRole: "button", children: /* @__PURE__ */ jsx(Text, { style: styles.cancelText, children: "Cancel" }) })
|
|
1377
|
+
] })
|
|
1378
|
+
] });
|
|
1379
|
+
}
|
|
1380
|
+
var styles = StyleSheet.create({
|
|
1381
|
+
overlay: { ...StyleSheet.absoluteFillObject, backgroundColor: "#000", zIndex: 9999 },
|
|
1382
|
+
hud: { flex: 1, justifyContent: "space-between", paddingVertical: 64, alignItems: "center" },
|
|
1383
|
+
hint: { color: "#fff", fontSize: 16, textAlign: "center", paddingHorizontal: 24 },
|
|
1384
|
+
cancel: { paddingHorizontal: 28, paddingVertical: 14, borderRadius: 24, backgroundColor: "rgba(255,255,255,0.18)" },
|
|
1385
|
+
cancelText: { color: "#fff", fontSize: 16, fontWeight: "600" }
|
|
1386
|
+
});
|
|
1387
|
+
|
|
1388
|
+
// src/device-state/replay.ts
|
|
1389
|
+
var DOMAINS = [
|
|
1390
|
+
"spatial",
|
|
1391
|
+
"visual",
|
|
1392
|
+
"activity",
|
|
1393
|
+
"deviceContext",
|
|
1394
|
+
"connectivity",
|
|
1395
|
+
"capability",
|
|
1396
|
+
"environment",
|
|
1397
|
+
"interaction"
|
|
1398
|
+
];
|
|
1399
|
+
function unknownSnapshot(domain) {
|
|
1400
|
+
const quality = { freshness: { state: "unknown", observedAt: null, ageMs: null }, confidence: { level: "unknown" } };
|
|
1401
|
+
switch (domain) {
|
|
1402
|
+
case "spatial":
|
|
1403
|
+
return { ...{ quality }, position: null, movement: { speedMps: null, bearingDeg: null, distanceRecentM: null, state: "unknown" }, orientation: { headingDeg: null }, recentPath: [] };
|
|
1404
|
+
case "visual":
|
|
1405
|
+
return { quality, camera: { available: false, active: false, facing: "unknown" }, latest: null, recentTimeline: { windowMs: 0, frames: [] } };
|
|
1406
|
+
case "activity":
|
|
1407
|
+
return { quality, human: { state: "unknown", since: null, confidence: { level: "unknown" } }, devicePose: { placement: "unknown", orientation: "unknown", confidence: { level: "unknown" } }, recentTransitions: [] };
|
|
1408
|
+
default:
|
|
1409
|
+
return { quality };
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
function refusal(domain, code, message) {
|
|
1413
|
+
const err = new Error(message || `deviceState.${domain}: refused during recording (${code}).`);
|
|
1414
|
+
err.code = code;
|
|
1415
|
+
err.domain = domain;
|
|
1416
|
+
return err;
|
|
1417
|
+
}
|
|
1418
|
+
function createReplayDeviceState(fixture) {
|
|
1419
|
+
const samples = [...fixture.samples || []].sort((a, b) => a.t - b.t);
|
|
1420
|
+
const duration = samples.length ? samples[samples.length - 1].t : 0;
|
|
1421
|
+
const refusals = new Map((fixture.refusals || []).map((r) => [r.domain, r]));
|
|
1422
|
+
const openSessions = /* @__PURE__ */ new Set();
|
|
1423
|
+
let cursor = 0;
|
|
1424
|
+
const clock = {
|
|
1425
|
+
now: () => cursor,
|
|
1426
|
+
advance: (ms) => cursor = Math.min(duration, Math.max(0, cursor + ms)),
|
|
1427
|
+
seek: (ms) => cursor = Math.min(duration, Math.max(0, ms)),
|
|
1428
|
+
end: () => cursor = duration,
|
|
1429
|
+
durationMs: () => duration
|
|
1430
|
+
};
|
|
1431
|
+
const sampleAt = () => {
|
|
1432
|
+
let found = null;
|
|
1433
|
+
for (const s of samples) {
|
|
1434
|
+
if (s.t > cursor) break;
|
|
1435
|
+
found = s;
|
|
1436
|
+
}
|
|
1437
|
+
return found;
|
|
1438
|
+
};
|
|
1439
|
+
const domainFor = (name) => ({
|
|
1440
|
+
async getSnapshot() {
|
|
1441
|
+
const s = sampleAt();
|
|
1442
|
+
const recorded = s ? s[name] : void 0;
|
|
1443
|
+
return recorded ?? unknownSnapshot(name);
|
|
1444
|
+
},
|
|
1445
|
+
async start() {
|
|
1446
|
+
const r = refusals.get(name);
|
|
1447
|
+
if (r) throw refusal(name, r.code, r.message);
|
|
1448
|
+
openSessions.add(name);
|
|
1449
|
+
return { id: `replay_${name}`, intent: "replay", startedAt: 0 };
|
|
1450
|
+
},
|
|
1451
|
+
async stop() {
|
|
1452
|
+
openSessions.delete(name);
|
|
1453
|
+
},
|
|
1454
|
+
subscribe() {
|
|
1455
|
+
throw refusal(name, "UNSUPPORTED", `deviceState.${name}.subscribe is not replayable \u2014 advance the clock and read.`);
|
|
1456
|
+
}
|
|
1457
|
+
});
|
|
1458
|
+
const deviceState = {};
|
|
1459
|
+
for (const d of DOMAINS) deviceState[d] = domainFor(d);
|
|
1460
|
+
return { deviceState, clock, started: () => [...openSessions] };
|
|
1461
|
+
}
|
|
1462
|
+
function parseWalkJsonl(text) {
|
|
1463
|
+
const lines = text.split("\n").map((l) => l.trim()).filter(Boolean);
|
|
1464
|
+
if (!lines.length) throw new Error("Empty recording \u2014 no header line.");
|
|
1465
|
+
const first = JSON.parse(lines[0]);
|
|
1466
|
+
const header = first.header ?? first;
|
|
1467
|
+
const refusals = first.refusals ?? [];
|
|
1468
|
+
const samples = [];
|
|
1469
|
+
for (const line of lines.slice(1)) {
|
|
1470
|
+
const rec = JSON.parse(line);
|
|
1471
|
+
if (rec.refusal) {
|
|
1472
|
+
refusals.push(rec.refusal);
|
|
1473
|
+
continue;
|
|
1474
|
+
}
|
|
1475
|
+
if (typeof rec.t === "number") samples.push(rec);
|
|
1476
|
+
}
|
|
1477
|
+
return { header, refusals, samples };
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1480
|
+
// src/index.ts
|
|
1481
|
+
function setupExpoProviders(opts) {
|
|
1482
|
+
setHapticsProvider(expoHaptics);
|
|
1483
|
+
if (opts?.deviceProvider) setPluginNativeProvider(opts.deviceProvider);
|
|
1484
|
+
setBundleStorage(new ExpoBundleStorage({ indexKey: opts?.bundleIndexKey }));
|
|
1485
|
+
setClipboardProvider({
|
|
1486
|
+
setString: (text) => ExpoClipboard.setStringAsync(text).then(() => {
|
|
1487
|
+
})
|
|
1488
|
+
});
|
|
1489
|
+
registerBundleModules({
|
|
1490
|
+
"expo-web-browser": ExpoWebBrowser,
|
|
1491
|
+
"expo-linking": ExpoLinking,
|
|
1492
|
+
...opts?.bundleModules || {}
|
|
1493
|
+
});
|
|
1494
|
+
setAcsCacheStorage({
|
|
1495
|
+
getItem: (k) => AsyncStorage3.getItem(k),
|
|
1496
|
+
setItem: (k, v) => AsyncStorage3.setItem(k, v)
|
|
1497
|
+
});
|
|
1498
|
+
return { attachmentCache: new ExpoAttachmentCache({ indexKey: opts?.attachmentIndexKey }) };
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
export {
|
|
1502
|
+
expoHaptics,
|
|
1503
|
+
ExpoBundleStorage,
|
|
1504
|
+
ExpoAttachmentCache,
|
|
1505
|
+
visualEngine,
|
|
1506
|
+
createDeviceState,
|
|
1507
|
+
IMPLEMENTED_DOMAINS,
|
|
1508
|
+
CameraTaskHost,
|
|
1509
|
+
createReplayDeviceState,
|
|
1510
|
+
parseWalkJsonl,
|
|
1511
|
+
setupExpoProviders
|
|
1512
|
+
};
|
|
1513
|
+
//# sourceMappingURL=chunk-4XYXJYN5.mjs.map
|