File size: 3,201 Bytes
7ef32e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**

 * The OEM host's engine bootstrap — the complete front-door contract an OEM

 * app implements: hand engine the auth/config/analytics/UI seams via

 * `engine.configure`, then `engine.start()` (which resolves only after the

 * device-identity stores hydrate, so the facade reads below are trustworthy).

 *

 * This module doubles as the compile-time contract test for the public

 * `@mentra/engine` entry: the "OEM Host Boundary Gate" CI workflow typechecks

 * this app against engine's BUILT types, with no `/internal` access.

 */
import {engine, type IslandConfigureOptions} from "@mentra/engine"

export type EngineLogger = (line: string) => void

/** Configure + start the engine runtime; returns an unsubscribe for the demo listeners. */
export async function startEngineRuntime(log: EngineLogger): Promise<() => void> {
  const options: IslandConfigureOptions = {
    auth: {
      // A real OEM host returns its logged-in user's backend token here.
      getSubjectToken: async () => ({token: "example-subject-token", type: "supabase"}),
      onStateChange: (callback) => {
        void callback // a real host forwards its auth-session events
      },
    },
    config: {audioFrameSizeBytes: 20},
    analytics: (event, props) => log(`analytics: ${event}${props ? ` ${JSON.stringify(props)}` : ""}`),
    ui: {
      // Engine dispatches miniapp wifi-setup requests here; the OEM owns the screen.
      requestWifiSetup: (reason) => log(`wifi setup requested (${reason ?? "no reason"})`),
      scanQr: async (options) => {
        log(`scanQr requested (${options?.title ?? "no title"})`)
        return {cancelled: true}
      },
    },
  }
  engine.configure(options)
  await engine.start()
  log("engine runtime started (device identity hydrated)")

  // Representative read-model subscriptions an OEM home screen renders from.
  // Engine decides; this host only renders.
  const unsubs = [
    engine.glasses.onStatus((status) => log(`glasses: ${status.state}${status.fullyBooted ? " (booted)" : ""}`)),
    engine.pairing.onReadiness((readiness) => log(`pairing readiness: ${JSON.stringify(readiness)}`)),
    engine.notifications.onNotification((notification) =>
      log(`engine notification: ${notification.kind} — ${notification.reason}`),
    ),
    engine.gallery.onNotice((notice) => log(`gallery notice: ${notice.code}`)),
    engine.ota.installSession.onSnapshot((snapshot) => log(`ota install: ${snapshot.displayState}`)),
  ]

  // From here on we own live subscriptions and a started runtime — if a later
  // bootstrap read fails, release the listeners AND stop the engine before
  // surfacing the error, so "start failed" is actually true and nothing keeps
  // running behind a failed bootstrap.
  try {
    const hasDefault = await engine.glasses.hasDefaultDevice()
    log(`paired device on record: ${hasDefault ? "yes" : "no"}`)
    log(`ota update available: ${engine.ota.updateAvailable()?.versionName ?? "none"}`)
  } catch (error) {
    unsubs.forEach((unsub) => unsub())
    await engine.stop().catch(() => {})
    throw error
  }

  return () => unsubs.forEach((unsub) => unsub())
}