@air-jam/sdk 0.1.4 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (348) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +627 -57
  3. package/dist/agent-tooling-BSMGBix9.d.cts +123 -0
  4. package/dist/agent-tooling-BSMGBix9.d.ts +123 -0
  5. package/dist/agent-tooling.cjs +83 -0
  6. package/dist/agent-tooling.cjs.map +1 -0
  7. package/dist/agent-tooling.d.cts +2 -0
  8. package/dist/agent-tooling.d.ts +2 -0
  9. package/dist/agent-tooling.js +17 -0
  10. package/dist/agent-tooling.js.map +1 -0
  11. package/dist/arcade/bridge/controller.cjs +749 -0
  12. package/dist/arcade/bridge/controller.cjs.map +1 -0
  13. package/dist/arcade/bridge/controller.d.cts +85 -0
  14. package/dist/arcade/bridge/controller.d.ts +85 -0
  15. package/dist/arcade/bridge/controller.js +33 -0
  16. package/dist/arcade/bridge/controller.js.map +1 -0
  17. package/dist/arcade/bridge/host.cjs +421 -0
  18. package/dist/arcade/bridge/host.cjs.map +1 -0
  19. package/dist/arcade/bridge/host.d.cts +73 -0
  20. package/dist/arcade/bridge/host.d.ts +73 -0
  21. package/dist/arcade/bridge/host.js +25 -0
  22. package/dist/arcade/bridge/host.js.map +1 -0
  23. package/dist/arcade/bridge/iframe.cjs +453 -0
  24. package/dist/arcade/bridge/iframe.cjs.map +1 -0
  25. package/dist/arcade/bridge/iframe.d.cts +80 -0
  26. package/dist/arcade/bridge/iframe.d.ts +80 -0
  27. package/dist/arcade/bridge/iframe.js +28 -0
  28. package/dist/arcade/bridge/iframe.js.map +1 -0
  29. package/dist/arcade/bridge.cjs +1067 -0
  30. package/dist/arcade/bridge.cjs.map +1 -0
  31. package/dist/arcade/bridge.d.cts +11 -0
  32. package/dist/arcade/bridge.d.ts +11 -0
  33. package/dist/arcade/bridge.js +76 -0
  34. package/dist/arcade/bridge.js.map +1 -0
  35. package/dist/arcade/host.cjs +488 -0
  36. package/dist/arcade/host.cjs.map +1 -0
  37. package/dist/arcade/host.d.cts +19 -0
  38. package/dist/arcade/host.d.ts +19 -0
  39. package/dist/arcade/host.js +13 -0
  40. package/dist/arcade/host.js.map +1 -0
  41. package/dist/arcade/surface.cjs +113 -0
  42. package/dist/arcade/surface.cjs.map +1 -0
  43. package/dist/arcade/surface.d.cts +14 -0
  44. package/dist/arcade/surface.d.ts +14 -0
  45. package/dist/arcade/surface.js +27 -0
  46. package/dist/arcade/surface.js.map +1 -0
  47. package/dist/arcade/url.cjs +363 -0
  48. package/dist/arcade/url.cjs.map +1 -0
  49. package/dist/arcade/url.d.cts +68 -0
  50. package/dist/arcade/url.d.ts +68 -0
  51. package/dist/arcade/url.js +28 -0
  52. package/dist/arcade/url.js.map +1 -0
  53. package/dist/arcade-runtime-url-BW-MUAOY.d.cts +28 -0
  54. package/dist/arcade-runtime-url-l8qj01Pg.d.ts +28 -0
  55. package/dist/arcade-surface-identity-CQxDpn_w.d.cts +16 -0
  56. package/dist/arcade-surface-identity-CQxDpn_w.d.ts +16 -0
  57. package/dist/arcade.cjs +1565 -0
  58. package/dist/arcade.cjs.map +1 -0
  59. package/dist/arcade.d.cts +18 -0
  60. package/dist/arcade.d.ts +18 -0
  61. package/dist/arcade.js +122 -0
  62. package/dist/arcade.js.map +1 -0
  63. package/dist/capabilities.cjs +39 -0
  64. package/dist/capabilities.cjs.map +1 -0
  65. package/dist/capabilities.d.cts +31 -0
  66. package/dist/capabilities.d.ts +31 -0
  67. package/dist/capabilities.js +12 -0
  68. package/dist/capabilities.js.map +1 -0
  69. package/dist/chunk-2432OL7L.js +193 -0
  70. package/dist/chunk-2432OL7L.js.map +1 -0
  71. package/dist/chunk-2J65FXEP.js +41 -0
  72. package/dist/chunk-2J65FXEP.js.map +1 -0
  73. package/dist/chunk-32NYL7XI.js +539 -0
  74. package/dist/chunk-32NYL7XI.js.map +1 -0
  75. package/dist/chunk-3USU7GY2.js +54 -0
  76. package/dist/chunk-3USU7GY2.js.map +1 -0
  77. package/dist/chunk-BE6Z4SIT.js +65 -0
  78. package/dist/chunk-BE6Z4SIT.js.map +1 -0
  79. package/dist/chunk-CQUG24WX.js +249 -0
  80. package/dist/chunk-CQUG24WX.js.map +1 -0
  81. package/dist/chunk-D3X4MN4U.js +288 -0
  82. package/dist/chunk-D3X4MN4U.js.map +1 -0
  83. package/dist/chunk-EVZJA6RN.js +140 -0
  84. package/dist/chunk-EVZJA6RN.js.map +1 -0
  85. package/dist/chunk-HFWRZVZ3.js +1438 -0
  86. package/dist/chunk-HFWRZVZ3.js.map +1 -0
  87. package/dist/chunk-IKKDONQL.js +1 -0
  88. package/dist/chunk-IKKDONQL.js.map +1 -0
  89. package/dist/chunk-JTF47B56.js +1 -0
  90. package/dist/chunk-JTF47B56.js.map +1 -0
  91. package/dist/chunk-LIXPF4P6.js +22 -0
  92. package/dist/chunk-LIXPF4P6.js.map +1 -0
  93. package/dist/chunk-MKBASO6X.js +216 -0
  94. package/dist/chunk-MKBASO6X.js.map +1 -0
  95. package/dist/chunk-MPDMJIX5.js +1 -0
  96. package/dist/chunk-MPDMJIX5.js.map +1 -0
  97. package/dist/chunk-NCL6WJCL.js +91 -0
  98. package/dist/chunk-NCL6WJCL.js.map +1 -0
  99. package/dist/chunk-NU5W6VAV.js +126 -0
  100. package/dist/chunk-NU5W6VAV.js.map +1 -0
  101. package/dist/chunk-TGRTEWYV.js +195 -0
  102. package/dist/chunk-TGRTEWYV.js.map +1 -0
  103. package/dist/chunk-VDRNNGHX.js +184 -0
  104. package/dist/chunk-VDRNNGHX.js.map +1 -0
  105. package/dist/chunk-VN5LDWVN.js +32 -0
  106. package/dist/chunk-VN5LDWVN.js.map +1 -0
  107. package/dist/chunk-W2VK36RO.js +11 -0
  108. package/dist/chunk-W2VK36RO.js.map +1 -0
  109. package/dist/chunk-X24K52DG.js +12 -0
  110. package/dist/chunk-X24K52DG.js.map +1 -0
  111. package/dist/chunk-XLUWV45V.js +25 -0
  112. package/dist/chunk-XLUWV45V.js.map +1 -0
  113. package/dist/chunk-Y4RAPKKB.js +238 -0
  114. package/dist/chunk-Y4RAPKKB.js.map +1 -0
  115. package/dist/chunk-YXHH3MQT.js +230 -0
  116. package/dist/chunk-YXHH3MQT.js.map +1 -0
  117. package/dist/chunk-ZLPL2R3R.js +183 -0
  118. package/dist/chunk-ZLPL2R3R.js.map +1 -0
  119. package/dist/chunk-ZTQ5SP4H.js +9 -0
  120. package/dist/chunk-ZTQ5SP4H.js.map +1 -0
  121. package/dist/controller-Bod8YYr1.d.cts +172 -0
  122. package/dist/controller-DxVchfiA.d.ts +172 -0
  123. package/dist/dev-log-events-CVAM29hx.d.cts +123 -0
  124. package/dist/dev-log-events-CVAM29hx.d.ts +123 -0
  125. package/dist/handshake-CC_LrK5g.d.cts +31 -0
  126. package/dist/handshake-CC_LrK5g.d.ts +31 -0
  127. package/dist/host-8hmq_Sgc.d.cts +109 -0
  128. package/dist/host-Cw-N6e-5.d.ts +109 -0
  129. package/dist/index.cjs +8507 -3523
  130. package/dist/index.cjs.map +1 -1
  131. package/dist/index.d.cts +676 -1070
  132. package/dist/index.d.ts +676 -1070
  133. package/dist/index.js +4890 -3196
  134. package/dist/index.js.map +1 -1
  135. package/dist/metadata.cjs +114 -0
  136. package/dist/metadata.cjs.map +1 -0
  137. package/dist/metadata.d.cts +95 -0
  138. package/dist/metadata.d.ts +95 -0
  139. package/dist/metadata.js +83 -0
  140. package/dist/metadata.js.map +1 -0
  141. package/dist/notices-CnGaaS8B.d.ts +38 -0
  142. package/dist/notices-NyDM5Mtz.d.cts +38 -0
  143. package/dist/platform-machine.cjs +425 -0
  144. package/dist/platform-machine.cjs.map +1 -0
  145. package/dist/platform-machine.d.cts +1183 -0
  146. package/dist/platform-machine.d.ts +1183 -0
  147. package/dist/platform-machine.js +309 -0
  148. package/dist/platform-machine.js.map +1 -0
  149. package/dist/platform-settings-cuor3i11.d.cts +48 -0
  150. package/dist/platform-settings-cuor3i11.d.ts +48 -0
  151. package/dist/prefabs.cjs +36 -0
  152. package/dist/prefabs.cjs.map +1 -0
  153. package/dist/prefabs.d.cts +49 -0
  154. package/dist/prefabs.d.ts +49 -0
  155. package/dist/prefabs.js +8 -0
  156. package/dist/prefabs.js.map +1 -0
  157. package/dist/preview.cjs +2609 -0
  158. package/dist/preview.cjs.map +1 -0
  159. package/dist/preview.d.cts +155 -0
  160. package/dist/preview.d.ts +155 -0
  161. package/dist/preview.js +1671 -0
  162. package/dist/preview.js.map +1 -0
  163. package/dist/protocol.cjs +621 -54
  164. package/dist/protocol.cjs.map +1 -1
  165. package/dist/protocol.d.cts +76 -240
  166. package/dist/protocol.d.ts +76 -240
  167. package/dist/protocol.js +100 -2
  168. package/dist/protocol.js.map +1 -1
  169. package/dist/realtime-client-Cu8yVJFw.d.cts +15 -0
  170. package/dist/realtime-client-Cu8yVJFw.d.ts +15 -0
  171. package/dist/release.cjs +143 -0
  172. package/dist/release.cjs.map +1 -0
  173. package/dist/release.d.cts +64 -0
  174. package/dist/release.d.ts +64 -0
  175. package/dist/release.js +43 -0
  176. package/dist/release.js.map +1 -0
  177. package/dist/standard-match-phase-BucA5dYo.d.ts +44 -0
  178. package/dist/standard-match-phase-U2ra8J-0.d.cts +44 -0
  179. package/dist/sync-B6QsNrXz.d.cts +125 -0
  180. package/dist/sync-B6QsNrXz.d.ts +125 -0
  181. package/dist/ui.cjs +2827 -0
  182. package/dist/ui.cjs.map +1 -0
  183. package/dist/ui.d.cts +320 -0
  184. package/dist/ui.d.ts +320 -0
  185. package/dist/ui.js +1718 -0
  186. package/dist/ui.js.map +1 -0
  187. package/dist/url-policy-C89zxgw9.d.cts +9 -0
  188. package/dist/url-policy-C89zxgw9.d.ts +9 -0
  189. package/package.json +149 -11
  190. package/src/agent/agent-action.ts +304 -0
  191. package/src/agent/agent-contract.ts +180 -0
  192. package/src/agent-tooling.ts +20 -0
  193. package/src/arcade/bridge/controller.ts +15 -0
  194. package/src/arcade/bridge/host.ts +12 -0
  195. package/src/arcade/bridge/iframe.ts +61 -0
  196. package/src/arcade/bridge.ts +3 -0
  197. package/src/arcade/host.ts +1 -0
  198. package/src/arcade/surface.ts +4 -0
  199. package/src/arcade/url.ts +60 -0
  200. package/src/arcade.ts +4 -0
  201. package/src/audio/audio-manager.ts +162 -171
  202. package/src/audio/hooks.tsx +218 -97
  203. package/src/audio/index.ts +1 -1
  204. package/src/audio/music.tsx +139 -0
  205. package/src/capabilities/manifest.ts +71 -0
  206. package/src/capabilities.ts +12 -0
  207. package/src/components/connection-status-pill.tsx +55 -0
  208. package/src/components/controller-player-name-field.tsx +119 -0
  209. package/src/components/controller-primary-action.tsx +58 -0
  210. package/src/components/host-mute-button.tsx +49 -0
  211. package/src/components/join-qr-overlay.tsx +133 -0
  212. package/src/components/join-url-action-buttons.tsx +95 -0
  213. package/src/components/join-url-controls.tsx +58 -0
  214. package/src/components/join-url-field.tsx +52 -0
  215. package/src/components/lifecycle-action-group.tsx +129 -0
  216. package/src/components/player-avatar-strip.tsx +65 -0
  217. package/src/components/player-avatar.tsx +42 -13
  218. package/src/components/room-qr-code.tsx +125 -0
  219. package/src/components/runtime-shell-header.tsx +37 -0
  220. package/src/components/shell-classes.ts +19 -0
  221. package/src/components/surface-orientation-layout.ts +60 -0
  222. package/src/components/surface-viewport-layout.ts +268 -0
  223. package/src/components/surface-viewport.tsx +314 -0
  224. package/src/components/ui/button.tsx +4 -6
  225. package/src/components/ui/slider.tsx +1 -1
  226. package/src/components/volume-controls.tsx +65 -6
  227. package/src/context/air-jam-context.tsx +209 -161
  228. package/src/context/index.ts +2 -2
  229. package/src/context/session-providers.tsx +77 -0
  230. package/src/context/session-scope.ts +64 -0
  231. package/src/context/socket-manager.ts +6 -24
  232. package/src/contracts/v2/bridge.ts +261 -0
  233. package/src/contracts/v2/handshake.ts +40 -0
  234. package/src/contracts/v2/index.ts +2 -0
  235. package/src/dev/browser-log-sink.ts +912 -0
  236. package/src/diagnostics.ts +128 -0
  237. package/src/events.ts +7 -34
  238. package/src/hooks/internal/use-controller-runtime-api.ts +875 -0
  239. package/src/hooks/internal/use-host-runtime-api.ts +1051 -0
  240. package/src/hooks/internal/use-runtime-control-contract.ts +15 -0
  241. package/src/hooks/internal/use-runtime-inspection-contract.ts +16 -0
  242. package/src/hooks/internal/use-runtime-observability-contract.ts +24 -0
  243. package/src/hooks/use-air-jam-controller.ts +155 -475
  244. package/src/hooks/use-air-jam-host.ts +156 -485
  245. package/src/hooks/use-connection-status.ts +8 -0
  246. package/src/hooks/use-controller-lifecycle-intents.ts +27 -0
  247. package/src/hooks/use-controller-lifecycle-permissions.ts +36 -0
  248. package/src/hooks/use-controller-shell-status.ts +40 -0
  249. package/src/hooks/use-controller-tick.ts +65 -0
  250. package/src/hooks/use-controller-toasts.ts +116 -0
  251. package/src/hooks/use-get-input.ts +8 -5
  252. package/src/hooks/use-host-arcade-restore.ts +30 -0
  253. package/src/hooks/use-host-join-controls.ts +152 -0
  254. package/src/hooks/use-host-tick.ts +171 -0
  255. package/src/hooks/use-input-writer.ts +83 -0
  256. package/src/hooks/use-lifecycle-action-group-model.ts +98 -0
  257. package/src/hooks/use-players.ts +12 -0
  258. package/src/hooks/use-room.ts +22 -0
  259. package/src/hooks/use-send-signal.ts +6 -2
  260. package/src/index.ts +205 -31
  261. package/src/internal/input-manager.ts +195 -215
  262. package/src/lifecycle/index.ts +9 -0
  263. package/src/lifecycle/standard-match-phase.ts +32 -0
  264. package/src/metadata/manifest.ts +152 -0
  265. package/src/metadata.ts +12 -0
  266. package/src/platform-machine.ts +475 -0
  267. package/src/prefabs/types.ts +82 -0
  268. package/src/prefabs.ts +12 -0
  269. package/src/preview/host-workspace.tsx +59 -0
  270. package/src/preview/identity.ts +66 -0
  271. package/src/preview/layout.ts +223 -0
  272. package/src/preview/manager.ts +577 -0
  273. package/src/preview/messages.ts +26 -0
  274. package/src/preview/ownership.ts +81 -0
  275. package/src/preview/url.ts +117 -0
  276. package/src/preview/window.tsx +490 -0
  277. package/src/preview/workspace.tsx +678 -0
  278. package/src/preview.ts +42 -0
  279. package/src/protocol/arcade-platform-actions.ts +29 -0
  280. package/src/protocol/controller.ts +187 -0
  281. package/src/protocol/core.ts +23 -0
  282. package/src/protocol/dev-log-events.ts +222 -0
  283. package/src/protocol/errors.ts +21 -0
  284. package/src/protocol/host-grant.ts +185 -0
  285. package/src/protocol/host.ts +138 -0
  286. package/src/protocol/notices.ts +43 -0
  287. package/src/protocol/signals.ts +38 -0
  288. package/src/protocol/socket-events.ts +154 -0
  289. package/src/protocol/sync.ts +121 -0
  290. package/src/protocol/url-policy.ts +63 -0
  291. package/src/protocol.ts +12 -386
  292. package/src/qrcode.d.ts +16 -0
  293. package/src/release.ts +99 -0
  294. package/src/runtime/air-jam-config.ts +287 -0
  295. package/src/runtime/air-jam-error-boundary.tsx +672 -0
  296. package/src/runtime/arcade-bridge-request-surface.ts +15 -0
  297. package/src/runtime/arcade-runtime-url.ts +95 -0
  298. package/src/runtime/arcade-surface-identity.ts +16 -0
  299. package/src/runtime/contracts/control.ts +68 -0
  300. package/src/runtime/contracts/inspection.ts +165 -0
  301. package/src/runtime/contracts/observability.ts +94 -0
  302. package/src/runtime/controller-bridge.ts +451 -0
  303. package/src/runtime/controller-identity.ts +132 -0
  304. package/src/runtime/controller-presentation.ts +24 -0
  305. package/src/runtime/controller-realtime-client.ts +591 -0
  306. package/src/runtime/create-air-jam-app.tsx +260 -0
  307. package/src/runtime/dev-runtime-events.ts +36 -0
  308. package/src/runtime/embedded-runtime-adapters.ts +68 -0
  309. package/src/runtime/host-bridge.ts +388 -0
  310. package/src/runtime/host-realtime-client.ts +550 -0
  311. package/src/runtime/iframe-bridge.ts +135 -0
  312. package/src/runtime/realtime-client.ts +20 -0
  313. package/src/runtime/router-basename.ts +25 -0
  314. package/src/runtime/runtime-owner-contexts.tsx +4 -0
  315. package/src/runtime/runtime-session-params.ts +169 -0
  316. package/src/runtime/sdk-version.ts +6 -0
  317. package/src/runtime/session-runtimes.tsx +188 -0
  318. package/src/runtime/validate-arcade-bridge-attach.ts +14 -0
  319. package/src/runtime-control.ts +10 -0
  320. package/src/runtime-inspection.ts +14 -0
  321. package/src/runtime-observability.ts +9 -0
  322. package/src/settings/platform-settings-bridge.ts +336 -0
  323. package/src/settings/platform-settings-runtime.tsx +387 -0
  324. package/src/settings/platform-settings.ts +288 -0
  325. package/src/state/connection-store.ts +80 -12
  326. package/src/store/air-jam-store-domain-constants.ts +4 -0
  327. package/src/store/create-air-jam-store.ts +1094 -227
  328. package/src/styles.css +4 -38
  329. package/src/ui.ts +29 -0
  330. package/src/utils/is-rpc-serializable.ts +56 -0
  331. package/src/utils/player-avatar-url.ts +48 -0
  332. package/src/utils/url-builder.ts +7 -0
  333. package/dist/.tsbuildinfo +0 -1
  334. package/dist/chunk-B5DURUWT.js +0 -80
  335. package/dist/chunk-B5DURUWT.js.map +0 -1
  336. package/src/AirJamClient.ts +0 -224
  337. package/src/audio/volume-store.ts +0 -124
  338. package/src/components/controller-shell.tsx +0 -346
  339. package/src/components/host-shell.tsx +0 -520
  340. package/src/components/qr-scanner-dialog.tsx +0 -373
  341. package/src/components/ui/alert.tsx +0 -66
  342. package/src/components/ui/avatar.tsx +0 -46
  343. package/src/components/ui/badge.tsx +0 -47
  344. package/src/components/ui/card.tsx +0 -92
  345. package/src/components/ui/dialog.tsx +0 -122
  346. package/src/components/ui/tabs.tsx +0 -64
  347. package/src/hooks/use-air-jam-shell.ts +0 -379
  348. package/src/hooks/use-fullscreen.ts +0 -108
@@ -0,0 +1,336 @@
1
+ import {
2
+ AIRJAM_BRIDGE_INIT,
3
+ AIRJAM_SETTINGS_READY,
4
+ AIRJAM_SETTINGS_SYNC,
5
+ createBridgeHandshake,
6
+ isAirJamSettingsReadyMessage,
7
+ isAirJamSettingsSyncMessage,
8
+ parseAirJamBridgeInitMessage,
9
+ } from "../runtime/iframe-bridge";
10
+ import { AIR_JAM_SDK_VERSION } from "../runtime/sdk-version";
11
+ import type { PlatformSettingsSnapshot } from "./platform-settings";
12
+
13
+ const createSettingsBridgeInitMessage = () => ({
14
+ type: AIRJAM_BRIDGE_INIT,
15
+ payload: {
16
+ handshake: createBridgeHandshake({
17
+ sdkVersion: AIR_JAM_SDK_VERSION,
18
+ runtimeKind: "arcade-runtime",
19
+ capabilityFlags: {
20
+ settingsSync: true,
21
+ },
22
+ }),
23
+ },
24
+ });
25
+
26
+ const createSettingsSyncMessage = (settings: PlatformSettingsSnapshot) => ({
27
+ type: AIRJAM_SETTINGS_SYNC,
28
+ payload: {
29
+ settings,
30
+ },
31
+ });
32
+
33
+ export type PlatformSettingsBridgeState = "unbound" | "waiting_ready" | "ready";
34
+
35
+ export type PlatformSettingsBridgeTransport =
36
+ | "bridge_port"
37
+ | "window_postmessage";
38
+
39
+ export interface ParentPlatformSettingsBridgeEventMap {
40
+ bridgeAttached: { state: PlatformSettingsBridgeState };
41
+ settingsReady: { state: PlatformSettingsBridgeState };
42
+ snapshotFlushed: {
43
+ phase: "initial" | "update";
44
+ transports: PlatformSettingsBridgeTransport[];
45
+ settings: PlatformSettingsSnapshot;
46
+ };
47
+ }
48
+
49
+ export interface ParentPlatformSettingsBridgeOptions {
50
+ createMessageChannel?: () => MessageChannel;
51
+ onEvent?<T extends keyof ParentPlatformSettingsBridgeEventMap>(
52
+ event: T,
53
+ payload: ParentPlatformSettingsBridgeEventMap[T],
54
+ ): void;
55
+ }
56
+
57
+ export interface ParentPlatformSettingsBridgeController {
58
+ attach(targetWindow: Window, targetOrigin: string): void;
59
+ detach(): void;
60
+ updateSettings(settings: PlatformSettingsSnapshot): void;
61
+ handleMessage(
62
+ event: MessageEvent<unknown>,
63
+ targetWindow: Window | null,
64
+ targetOrigin: string | null,
65
+ ): boolean;
66
+ getState(): PlatformSettingsBridgeState;
67
+ }
68
+
69
+ const noop = () => {};
70
+
71
+ export const createParentPlatformSettingsBridge = (
72
+ options: ParentPlatformSettingsBridgeOptions = {},
73
+ ): ParentPlatformSettingsBridgeController => {
74
+ const createMessageChannel =
75
+ options.createMessageChannel ?? (() => new MessageChannel());
76
+ const emit = options.onEvent ?? noop;
77
+
78
+ let state: PlatformSettingsBridgeState = "unbound";
79
+ let latestSettings: PlatformSettingsSnapshot | null = null;
80
+ let bridgePort: MessagePort | null = null;
81
+ let targetWindow: Window | null = null;
82
+ let targetOrigin: string | null = null;
83
+ let initialSnapshotFlushed = false;
84
+ let childReadySeen = false;
85
+
86
+ const setState = (next: PlatformSettingsBridgeState) => {
87
+ state = next;
88
+ };
89
+
90
+ const closeBridgePort = () => {
91
+ if (!bridgePort) {
92
+ return;
93
+ }
94
+
95
+ try {
96
+ bridgePort.close();
97
+ } catch {
98
+ // Ignore close errors during teardown or iframe reload.
99
+ }
100
+
101
+ bridgePort = null;
102
+ };
103
+
104
+ const flushSnapshot = (phase: "initial" | "update") => {
105
+ if (
106
+ !latestSettings ||
107
+ !targetWindow ||
108
+ !targetOrigin ||
109
+ state !== "ready"
110
+ ) {
111
+ return;
112
+ }
113
+
114
+ const payload = createSettingsSyncMessage(latestSettings);
115
+ const transports: PlatformSettingsBridgeTransport[] = [];
116
+
117
+ if (bridgePort) {
118
+ bridgePort.postMessage(payload);
119
+ transports.push("bridge_port");
120
+ }
121
+
122
+ targetWindow.postMessage(payload, targetOrigin);
123
+ transports.push("window_postmessage");
124
+
125
+ emit("snapshotFlushed", {
126
+ phase,
127
+ transports,
128
+ settings: latestSettings,
129
+ });
130
+ initialSnapshotFlushed = true;
131
+ };
132
+
133
+ return {
134
+ attach(nextTargetWindow, nextTargetOrigin) {
135
+ closeBridgePort();
136
+
137
+ const channel = createMessageChannel();
138
+ bridgePort = channel.port1;
139
+ bridgePort.start?.();
140
+ targetWindow = nextTargetWindow;
141
+ targetOrigin = nextTargetOrigin;
142
+ initialSnapshotFlushed = false;
143
+ setState(childReadySeen ? "ready" : "waiting_ready");
144
+
145
+ nextTargetWindow.postMessage(
146
+ createSettingsBridgeInitMessage(),
147
+ nextTargetOrigin,
148
+ [channel.port2],
149
+ );
150
+
151
+ emit("bridgeAttached", {
152
+ state,
153
+ });
154
+
155
+ if (childReadySeen) {
156
+ flushSnapshot("initial");
157
+ }
158
+ },
159
+
160
+ detach() {
161
+ closeBridgePort();
162
+ targetWindow = null;
163
+ targetOrigin = null;
164
+ initialSnapshotFlushed = false;
165
+ childReadySeen = false;
166
+ setState("unbound");
167
+ },
168
+
169
+ updateSettings(settings) {
170
+ latestSettings = settings;
171
+ if (state === "ready") {
172
+ flushSnapshot(initialSnapshotFlushed ? "update" : "initial");
173
+ }
174
+ },
175
+
176
+ handleMessage(event, expectedTargetWindow, expectedTargetOrigin) {
177
+ if (!expectedTargetWindow || !expectedTargetOrigin) {
178
+ return false;
179
+ }
180
+
181
+ if (event.source !== expectedTargetWindow) {
182
+ return false;
183
+ }
184
+
185
+ if (event.origin !== expectedTargetOrigin) {
186
+ return false;
187
+ }
188
+
189
+ if (!isAirJamSettingsReadyMessage(event.data)) {
190
+ return false;
191
+ }
192
+
193
+ childReadySeen = true;
194
+ setState("ready");
195
+ emit("settingsReady", {
196
+ state,
197
+ });
198
+ flushSnapshot(initialSnapshotFlushed ? "update" : "initial");
199
+ return true;
200
+ },
201
+
202
+ getState() {
203
+ return state;
204
+ },
205
+ };
206
+ };
207
+
208
+ export interface InheritedPlatformSettingsBridgeOptions {
209
+ applySettings: (settings: PlatformSettingsSnapshot) => void;
210
+ onBridgeUnavailable?: () => void;
211
+ onBridgeAttached?: (origin: string) => void;
212
+ onSettingsReadyRequested?: (origin: string) => void;
213
+ onSnapshotApplied?: (
214
+ transport: PlatformSettingsBridgeTransport,
215
+ settings: PlatformSettingsSnapshot,
216
+ ) => void;
217
+ }
218
+
219
+ export const initializeInheritedPlatformSettingsBridge = ({
220
+ applySettings,
221
+ onBridgeUnavailable,
222
+ onBridgeAttached,
223
+ onSettingsReadyRequested,
224
+ onSnapshotApplied,
225
+ }: InheritedPlatformSettingsBridgeOptions): (() => void) => {
226
+ if (typeof window === "undefined" || window.parent === window) {
227
+ onBridgeUnavailable?.();
228
+ return () => {};
229
+ }
230
+
231
+ const trustedParentOrigin = (() => {
232
+ try {
233
+ return document.referrer ? new URL(document.referrer).origin : null;
234
+ } catch {
235
+ return null;
236
+ }
237
+ })();
238
+ if (!trustedParentOrigin) {
239
+ onBridgeUnavailable?.();
240
+ return () => {};
241
+ }
242
+
243
+ let bridgePortCleanup: (() => void) | null = null;
244
+
245
+ const applySnapshot = (
246
+ transport: PlatformSettingsBridgeTransport,
247
+ settings: PlatformSettingsSnapshot,
248
+ ) => {
249
+ onSnapshotApplied?.(transport, settings);
250
+ applySettings(settings);
251
+ };
252
+
253
+ const bindBridgePort = (port: MessagePort) => {
254
+ bridgePortCleanup?.();
255
+
256
+ const handlePortMessage = (event: MessageEvent<unknown>) => {
257
+ if (!isAirJamSettingsSyncMessage(event.data)) {
258
+ return;
259
+ }
260
+
261
+ applySnapshot("bridge_port", event.data.payload.settings);
262
+ };
263
+
264
+ port.addEventListener("message", handlePortMessage as EventListener);
265
+ port.start();
266
+
267
+ bridgePortCleanup = () => {
268
+ port.removeEventListener("message", handlePortMessage as EventListener);
269
+ try {
270
+ port.close();
271
+ } catch {
272
+ // Ignore close errors during iframe teardown.
273
+ }
274
+ bridgePortCleanup = null;
275
+ };
276
+ };
277
+
278
+ const handleInitMessage = (event: MessageEvent<unknown>) => {
279
+ if (event.source !== window.parent) {
280
+ return;
281
+ }
282
+
283
+ if (event.origin !== trustedParentOrigin) {
284
+ return;
285
+ }
286
+
287
+ const initMessage = parseAirJamBridgeInitMessage(event.data);
288
+ if (!initMessage) {
289
+ return;
290
+ }
291
+
292
+ const bridgePort = event.ports?.[0];
293
+ if (!bridgePort) {
294
+ return;
295
+ }
296
+
297
+ onBridgeAttached?.(event.origin);
298
+ bindBridgePort(bridgePort);
299
+ };
300
+
301
+ const handleFallbackMessage = (event: MessageEvent<unknown>) => {
302
+ if (event.source !== window.parent) {
303
+ return;
304
+ }
305
+
306
+ if (event.origin !== trustedParentOrigin) {
307
+ return;
308
+ }
309
+
310
+ if (!isAirJamSettingsSyncMessage(event.data)) {
311
+ return;
312
+ }
313
+
314
+ applySnapshot("window_postmessage", event.data.payload.settings);
315
+ };
316
+
317
+ window.addEventListener("message", handleInitMessage);
318
+ window.addEventListener("message", handleFallbackMessage);
319
+
320
+ if (typeof window.parent.postMessage === "function") {
321
+ window.parent.postMessage(
322
+ {
323
+ type: AIRJAM_SETTINGS_READY,
324
+ payload: { ready: true },
325
+ },
326
+ trustedParentOrigin,
327
+ );
328
+ onSettingsReadyRequested?.(trustedParentOrigin);
329
+ }
330
+
331
+ return () => {
332
+ window.removeEventListener("message", handleInitMessage);
333
+ window.removeEventListener("message", handleFallbackMessage);
334
+ bridgePortCleanup?.();
335
+ };
336
+ };
@@ -0,0 +1,387 @@
1
+ import {
2
+ createContext,
3
+ useCallback,
4
+ useContext,
5
+ useEffect,
6
+ useMemo,
7
+ useState,
8
+ type ReactNode,
9
+ } from "react";
10
+ import { AIRJAM_DEV_LOG_EVENTS } from "../protocol";
11
+ import { emitAirJamDevRuntimeEvent } from "../runtime/dev-runtime-events";
12
+ import {
13
+ DEFAULT_PLATFORM_SETTINGS,
14
+ getEffectiveAudioVolume,
15
+ LEGACY_AUDIO_SETTINGS_STORAGE_KEY,
16
+ mergePlatformSettings,
17
+ persistPlatformSettings,
18
+ PLATFORM_SETTINGS_STORAGE_KEY,
19
+ readPersistedPlatformSettings,
20
+ type PartialPlatformSettings,
21
+ type PlatformAudioSettings,
22
+ type PlatformSettingsPersistence,
23
+ type PlatformSettingsSnapshot,
24
+ } from "./platform-settings";
25
+ import { initializeInheritedPlatformSettingsBridge } from "./platform-settings-bridge";
26
+
27
+ type PlatformSettingsMode = "owner" | "inherited" | "default";
28
+ export type PlatformSettingsRuntimeStatus = "ready" | "waiting";
29
+
30
+ interface PlatformSettingsContextValue {
31
+ mode: PlatformSettingsMode;
32
+ status: PlatformSettingsRuntimeStatus;
33
+ settings: PlatformSettingsSnapshot;
34
+ setSettings: (
35
+ next:
36
+ | PlatformSettingsSnapshot
37
+ | ((current: PlatformSettingsSnapshot) => PlatformSettingsSnapshot),
38
+ ) => void;
39
+ updateSettings: (patch: PartialPlatformSettings) => void;
40
+ }
41
+
42
+ const platformSettingsContext =
43
+ createContext<PlatformSettingsContextValue | null>(null);
44
+
45
+ export interface PlatformSettingsRuntimeProps {
46
+ children: ReactNode;
47
+ persistence?: PlatformSettingsPersistence;
48
+ }
49
+
50
+ export interface PlatformSettingsBoundaryProps {
51
+ children: ReactNode;
52
+ persistence?: PlatformSettingsPersistence;
53
+ }
54
+
55
+ export interface PlatformSettingsOwnerApi {
56
+ settings: PlatformSettingsSnapshot;
57
+ setSettings: (
58
+ next:
59
+ | PlatformSettingsSnapshot
60
+ | ((current: PlatformSettingsSnapshot) => PlatformSettingsSnapshot),
61
+ ) => void;
62
+ updateSettings: (patch: PartialPlatformSettings) => void;
63
+ updateAudio: (patch: Partial<PlatformAudioSettings>) => void;
64
+ updateAccessibility: (
65
+ patch: Partial<PlatformSettingsSnapshot["accessibility"]>,
66
+ ) => void;
67
+ updateFeedback: (
68
+ patch: Partial<PlatformSettingsSnapshot["feedback"]>,
69
+ ) => void;
70
+ updatePreviewControllers: (
71
+ patch: Partial<PlatformSettingsSnapshot["previewControllers"]>,
72
+ ) => void;
73
+ }
74
+
75
+ export interface PlatformAudioSettingsApi extends PlatformAudioSettings {
76
+ getEffectiveVolume: (category: "music" | "sfx") => number;
77
+ isReadOnly: boolean;
78
+ setMasterVolume: (volume: number) => void;
79
+ setMusicVolume: (volume: number) => void;
80
+ setSfxVolume: (volume: number) => void;
81
+ }
82
+
83
+ const getInitialMode = (
84
+ persistence: PlatformSettingsPersistence,
85
+ ): PlatformSettingsMode => {
86
+ if (typeof window === "undefined") {
87
+ return persistence === "local" ? "owner" : "default";
88
+ }
89
+
90
+ if (window.parent !== window) {
91
+ return "inherited";
92
+ }
93
+
94
+ return persistence === "local" ? "owner" : "default";
95
+ };
96
+
97
+ const createDefaultContextValue = (): PlatformSettingsContextValue => ({
98
+ mode: "default",
99
+ status: "ready",
100
+ settings: DEFAULT_PLATFORM_SETTINGS,
101
+ setSettings: () => {
102
+ throw new Error(
103
+ 'Platform settings are read-only in this runtime. Mount <PlatformSettingsRuntime persistence="local"> in the platform shell to update shared settings.',
104
+ );
105
+ },
106
+ updateSettings: () => {
107
+ throw new Error(
108
+ 'Platform settings are read-only in this runtime. Mount <PlatformSettingsRuntime persistence="local"> in the platform shell to update shared settings.',
109
+ );
110
+ },
111
+ });
112
+
113
+ export function PlatformSettingsRuntime({
114
+ children,
115
+ persistence = "none",
116
+ }: PlatformSettingsRuntimeProps) {
117
+ const mode = getInitialMode(persistence);
118
+ const [settings, setSettingsState] = useState<PlatformSettingsSnapshot>(() =>
119
+ mode === "owner"
120
+ ? readPersistedPlatformSettings()
121
+ : DEFAULT_PLATFORM_SETTINGS,
122
+ );
123
+ const [status, setStatus] = useState<PlatformSettingsRuntimeStatus>(() =>
124
+ mode === "inherited" ? "waiting" : "ready",
125
+ );
126
+
127
+ useEffect(() => {
128
+ if (mode === "owner") {
129
+ setStatus("ready");
130
+ setSettingsState(readPersistedPlatformSettings());
131
+ return;
132
+ }
133
+
134
+ setSettingsState(DEFAULT_PLATFORM_SETTINGS);
135
+ setStatus(mode === "inherited" ? "waiting" : "ready");
136
+ }, [mode]);
137
+
138
+ useEffect(() => {
139
+ if (mode !== "owner") {
140
+ return;
141
+ }
142
+
143
+ persistPlatformSettings(settings);
144
+ }, [mode, settings]);
145
+
146
+ useEffect(() => {
147
+ if (mode !== "owner" || typeof window === "undefined") {
148
+ return;
149
+ }
150
+
151
+ const handleStorage = (event: StorageEvent) => {
152
+ if (event.storageArea !== window.localStorage) {
153
+ return;
154
+ }
155
+
156
+ if (
157
+ event.key !== null &&
158
+ event.key !== PLATFORM_SETTINGS_STORAGE_KEY &&
159
+ event.key !== LEGACY_AUDIO_SETTINGS_STORAGE_KEY
160
+ ) {
161
+ return;
162
+ }
163
+
164
+ setSettingsState(readPersistedPlatformSettings());
165
+ };
166
+
167
+ window.addEventListener("storage", handleStorage);
168
+ return () => {
169
+ window.removeEventListener("storage", handleStorage);
170
+ };
171
+ }, [mode]);
172
+
173
+ useEffect(() => {
174
+ if (mode !== "inherited") {
175
+ return;
176
+ }
177
+
178
+ return initializeInheritedPlatformSettingsBridge({
179
+ applySettings: (nextSettings) => {
180
+ setSettingsState(nextSettings);
181
+ setStatus("ready");
182
+ },
183
+ onBridgeUnavailable: () => {
184
+ setStatus("ready");
185
+ },
186
+ onBridgeAttached: (origin) => {
187
+ emitAirJamDevRuntimeEvent({
188
+ event: AIRJAM_DEV_LOG_EVENTS.browser.runtime,
189
+ message: "Embedded runtime bound platform settings bridge port",
190
+ level: "info",
191
+ data: {
192
+ origin,
193
+ state: "waiting_ready",
194
+ transport: "bridge_port",
195
+ },
196
+ });
197
+ },
198
+ onSettingsReadyRequested: (trustedParentOrigin) => {
199
+ emitAirJamDevRuntimeEvent({
200
+ event: AIRJAM_DEV_LOG_EVENTS.browser.runtime,
201
+ message:
202
+ "Embedded runtime requested initial platform settings from parent",
203
+ level: "info",
204
+ data: {
205
+ trustedParentOrigin,
206
+ state: "waiting_ready",
207
+ },
208
+ });
209
+ },
210
+ onSnapshotApplied: (transport, settings) => {
211
+ emitAirJamDevRuntimeEvent({
212
+ event: AIRJAM_DEV_LOG_EVENTS.browser.runtime,
213
+ message: "Embedded runtime applied platform settings snapshot",
214
+ level: "info",
215
+ data: {
216
+ transport,
217
+ state: "ready",
218
+ settings,
219
+ },
220
+ });
221
+ },
222
+ });
223
+ }, [mode]);
224
+
225
+ const setSettings = useCallback<PlatformSettingsContextValue["setSettings"]>(
226
+ (next) => {
227
+ if (mode !== "owner") {
228
+ throw new Error(
229
+ 'Platform settings are read-only in this runtime. Mount <PlatformSettingsRuntime persistence="local"> in the platform shell to update shared settings.',
230
+ );
231
+ }
232
+
233
+ setSettingsState((current) =>
234
+ typeof next === "function" ? next(current) : next,
235
+ );
236
+ },
237
+ [mode],
238
+ );
239
+
240
+ const updateSettings = useCallback<
241
+ PlatformSettingsContextValue["updateSettings"]
242
+ >(
243
+ (patch) => {
244
+ if (mode !== "owner") {
245
+ throw new Error(
246
+ 'Platform settings are read-only in this runtime. Mount <PlatformSettingsRuntime persistence="local"> in the platform shell to update shared settings.',
247
+ );
248
+ }
249
+
250
+ setSettingsState((current) => mergePlatformSettings(current, patch));
251
+ },
252
+ [mode],
253
+ );
254
+
255
+ const value = useMemo<PlatformSettingsContextValue>(
256
+ () => ({
257
+ mode,
258
+ status,
259
+ settings,
260
+ setSettings,
261
+ updateSettings,
262
+ }),
263
+ [mode, settings, setSettings, status, updateSettings],
264
+ );
265
+
266
+ return (
267
+ <platformSettingsContext.Provider value={value}>
268
+ {children}
269
+ </platformSettingsContext.Provider>
270
+ );
271
+ }
272
+
273
+ export function PlatformSettingsBoundary({
274
+ children,
275
+ persistence = "local",
276
+ }: PlatformSettingsBoundaryProps) {
277
+ const existingContext = useContext(platformSettingsContext);
278
+
279
+ if (existingContext) {
280
+ return children;
281
+ }
282
+
283
+ return (
284
+ <PlatformSettingsRuntime persistence={persistence}>
285
+ {children}
286
+ </PlatformSettingsRuntime>
287
+ );
288
+ }
289
+
290
+ export function usePlatformSettings(): PlatformSettingsOwnerApi {
291
+ const context = useContext(platformSettingsContext);
292
+
293
+ if (!context) {
294
+ throw new Error(
295
+ "usePlatformSettings must be used within a PlatformSettingsRuntime",
296
+ );
297
+ }
298
+
299
+ if (context.mode !== "owner") {
300
+ throw new Error(
301
+ 'usePlatformSettings can only be used in a platform-owned settings runtime. Mount <PlatformSettingsRuntime persistence="local"> in the platform shell.',
302
+ );
303
+ }
304
+
305
+ return {
306
+ settings: context.settings,
307
+ setSettings: context.setSettings,
308
+ updateSettings: context.updateSettings,
309
+ updateAudio: (patch) => context.updateSettings({ audio: patch }),
310
+ updateAccessibility: (patch) =>
311
+ context.updateSettings({ accessibility: patch }),
312
+ updateFeedback: (patch) => context.updateSettings({ feedback: patch }),
313
+ updatePreviewControllers: (patch) =>
314
+ context.updateSettings({ previewControllers: patch }),
315
+ };
316
+ }
317
+
318
+ export function useOptionalPlatformSettingsOwnerApi(): PlatformSettingsOwnerApi | null {
319
+ const context = useContext(platformSettingsContext);
320
+
321
+ if (!context || context.mode !== "owner") {
322
+ return null;
323
+ }
324
+
325
+ return {
326
+ settings: context.settings,
327
+ setSettings: context.setSettings,
328
+ updateSettings: context.updateSettings,
329
+ updateAudio: (patch) => context.updateSettings({ audio: patch }),
330
+ updateAccessibility: (patch) =>
331
+ context.updateSettings({ accessibility: patch }),
332
+ updateFeedback: (patch) => context.updateSettings({ feedback: patch }),
333
+ updatePreviewControllers: (patch) =>
334
+ context.updateSettings({ previewControllers: patch }),
335
+ };
336
+ }
337
+
338
+ export function useInheritedPlatformSettings(): Readonly<PlatformSettingsSnapshot> {
339
+ const context = useContext(platformSettingsContext);
340
+
341
+ if (!context) {
342
+ throw new Error(
343
+ "useInheritedPlatformSettings must be used within a PlatformSettingsRuntime",
344
+ );
345
+ }
346
+
347
+ return context.settings;
348
+ }
349
+
350
+ export function usePlatformSettingsRuntimeStatus(): PlatformSettingsRuntimeStatus {
351
+ return useContext(platformSettingsContext)?.status ?? "ready";
352
+ }
353
+
354
+ export function usePlatformAudioSettings(): PlatformAudioSettingsApi {
355
+ const context =
356
+ useContext(platformSettingsContext) ?? createDefaultContextValue();
357
+ const { audio } = context.settings;
358
+ const isReadOnly = context.mode !== "owner";
359
+
360
+ const setAudioSetting = useCallback(
361
+ (patch: Partial<PlatformAudioSettings>) => {
362
+ if (context.mode !== "owner") {
363
+ throw new Error(
364
+ 'Platform audio settings are read-only in this runtime. Mount <PlatformSettingsRuntime persistence="local"> in the platform shell to update shared settings.',
365
+ );
366
+ }
367
+
368
+ context.updateSettings({ audio: patch });
369
+ },
370
+ [context],
371
+ );
372
+
373
+ return {
374
+ ...audio,
375
+ isReadOnly,
376
+ getEffectiveVolume: (category) => getEffectiveAudioVolume(audio, category),
377
+ setMasterVolume: (volume) => setAudioSetting({ masterVolume: volume }),
378
+ setMusicVolume: (volume) => setAudioSetting({ musicVolume: volume }),
379
+ setSfxVolume: (volume) => setAudioSetting({ sfxVolume: volume }),
380
+ };
381
+ }
382
+
383
+ export function useResolvedPlatformSettingsSnapshot(): PlatformSettingsSnapshot {
384
+ return (
385
+ useContext(platformSettingsContext)?.settings ?? DEFAULT_PLATFORM_SETTINGS
386
+ );
387
+ }