@air-jam/sdk 0.1.3 → 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 +8512 -3518
  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 +4892 -3188
  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 +154 -13
  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 -14
  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 -141
  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 -25
  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 -521
  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
@@ -1,51 +1,28 @@
1
1
  /**
2
2
  * @module InputManager
3
- * @description Internal class for managing controller input with validation and latching.
3
+ * @description Internal input runtime used by host hooks.
4
4
  *
5
- * The InputManager is created internally by the AirJamProvider when input configuration
6
- * is provided. It handles:
7
- *
8
- * 1. **Input Buffering** - Stores the latest input for each controller
9
- * 2. **Validation** - Validates input against a Zod schema (optional)
10
- * 3. **Latching** - Ensures rapid button presses and stick flicks are never missed
11
- *
12
- * ## What is Latching?
13
- *
14
- * Game loops typically run at 60fps, but network events may arrive between frames.
15
- * Without latching, a quick button tap might be missed if it starts and ends between
16
- * two consecutive `getInput()` calls.
17
- *
18
- * **Latching solves this by:**
19
- * - Boolean fields: Keeping `true` values until consumed
20
- * - Vector fields: Keeping non-zero values for one frame after release
21
- *
22
- * ## Example: Without Latching (Bug)
23
- * ```
24
- * Frame 1: getInput() → action: false
25
- * [Network: action: true arrives, then action: false arrives]
26
- * Frame 2: getInput() → action: false ← Tap missed!
27
- * ```
28
- *
29
- * ## Example: With Latching (Fixed)
30
- * ```
31
- * Frame 1: getInput() → action: false
32
- * [Network: action: true arrives, then action: false arrives]
33
- * Frame 2: getInput() → action: true ← Tap captured!
34
- * Frame 3: getInput() → action: false ← Consumed, resets
35
- * ```
5
+ * Public API terms:
6
+ * - `pulse`: consume-on-read, tap-safe behavior (default for booleans)
7
+ * - `hold`: keep last active vector until a new active value arrives
8
+ * - `latest`: read current value as-is (default for vectors and other fields)
36
9
  *
37
10
  * @internal This class is not exported publicly. Use hooks to access input.
38
11
  */
39
12
  import type { z } from "zod";
40
13
  import type { ControllerInputEvent } from "../protocol";
41
14
 
42
- // Throttling for debug logging
43
- let lastInputManagerLogTime = 0;
15
+ type InputFieldBehavior = "pulse" | "hold" | "latest";
16
+
17
+ interface VectorValue {
18
+ x: number;
19
+ y: number;
20
+ }
44
21
 
45
22
  /**
46
- * Configuration for input handling with optional validation and latching.
23
+ * Configuration for input handling with optional validation and per-field behavior.
47
24
  *
48
- * Provided to `AirJamProvider` to enable typed, validated, and latched input.
25
+ * Provided to `HostSessionProvider` to enable typed and behavior-aware input reads.
49
26
  *
50
27
  * @template TSchema - Zod schema type for input validation
51
28
  *
@@ -59,13 +36,14 @@ let lastInputManagerLogTime = 0;
59
36
  * };
60
37
  * ```
61
38
  *
62
- * @example With latching for button and stick
39
+ * @example With custom behavior overrides
63
40
  * ```ts
64
41
  * const config: InputConfig = {
65
42
  * schema: gameInputSchema,
66
- * latch: {
67
- * booleanFields: ["action", "ability", "jump"],
68
- * vectorFields: ["vector", "aim"],
43
+ * behavior: {
44
+ * pulse: ["action", "ability"], // consume-on-read
45
+ * latest: ["aim"], // read latest value
46
+ * hold: ["menuVector"], // keep last non-zero vector
69
47
  * },
70
48
  * };
71
49
  * ```
@@ -90,57 +68,45 @@ export interface InputConfig<TSchema extends z.ZodSchema = z.ZodSchema> {
90
68
  * ```
91
69
  */
92
70
  schema?: TSchema;
93
- /**
94
- * Latching configuration to ensure rapid taps and flicks are never missed.
95
- *
96
- * Latching "holds" boolean true values and non-zero vectors until the next
97
- * `getInput()` call, preventing missed inputs between game frames.
98
- */
99
- latch?: {
71
+ behavior?: {
100
72
  /**
101
- * Boolean field names that should be latched.
102
- *
103
- * When a boolean field is latched:
104
- * - If it becomes `true`, it stays `true` until consumed by `getInput()`
105
- * - After consumption, it resets to the actual current value
106
- *
107
- * Use for: buttons, triggers, action inputs
73
+ * Consume-on-read behavior. Useful for tap-safe buttons and one-shot vectors.
108
74
  *
109
- * @example ["action", "ability", "jump", "fire"]
75
+ * Defaults: booleans use `pulse` even without explicit config.
110
76
  */
111
- booleanFields?: string[];
77
+ pulse?: string[];
112
78
  /**
113
- * Vector field names that should be latched.
114
- *
115
- * When a vector field is latched:
116
- * - Non-zero vectors are kept for one frame after returning to zero
117
- * - Ensures quick stick flicks register in the game loop
118
- *
119
- * Use for: joysticks, d-pads, directional inputs
79
+ * Sticky behavior. For vectors, keeps the last non-zero direction until
80
+ * another non-zero direction arrives.
81
+ */
82
+ hold?: string[];
83
+ /**
84
+ * Stateless behavior. Returns the latest raw value.
120
85
  *
121
- * @example ["vector", "movement", "aim"]
86
+ * Defaults: vectors and non-boolean fields use `latest`.
122
87
  */
123
- vectorFields?: string[];
88
+ latest?: string[];
124
89
  };
125
90
  }
126
91
 
127
92
  /**
128
- * Internal state for tracking latch values per controller.
93
+ * Internal behavior state per controller.
129
94
  * @internal
130
95
  */
131
- interface LatchState<TInput = Record<string, unknown>> {
132
- /** The most recent raw input from the controller */
133
- raw: TInput;
134
- /** The latched input (with consumed values held) */
135
- latched: TInput;
136
- /** Hash of raw input to detect changes */
137
- lastRawHash?: string;
96
+ interface ControllerBehaviorState {
97
+ pulseBooleans: Set<string>;
98
+ pulseVectors: Map<string, VectorValue>;
99
+ holdVectors: Map<string, VectorValue>;
138
100
  }
139
101
 
140
102
  /**
141
- * Internal class that manages input buffering, validation, and latching.
103
+ * Internal class that manages input buffering, validation, and behavior semantics.
104
+ *
105
+ * **Single input lane:** one `InputManager` per `HostSessionProvider`, fed only by `server:input`.
106
+ * Multiple `createAirJamStore` domains in the same room (e.g. `arcade.surface` + embedded game)
107
+ * do not duplicate or fork input — gameplay still reads controller input through this manager only.
142
108
  *
143
- * Created automatically by `AirJamProvider` when input configuration is provided.
109
+ * Created automatically by `HostSessionProvider` when input configuration is provided.
144
110
  * Access input via `useGetInput()` or `useAirJamHost().getInput()`.
145
111
  *
146
112
  * @template TSchema - Zod schema type for input validation
@@ -149,10 +115,10 @@ interface LatchState<TInput = Record<string, unknown>> {
149
115
  *
150
116
  * @example How it's used internally
151
117
  * ```ts
152
- * // In AirJamProvider
118
+ * // In HostSessionProvider
153
119
  * const inputManager = new InputManager({
154
120
  * schema: gameInputSchema,
155
- * latch: { booleanFields: ["action"] },
121
+ * behavior: { pulse: ["action"] },
156
122
  * });
157
123
  *
158
124
  * // In useAirJamHost (socket handler)
@@ -166,11 +132,13 @@ interface LatchState<TInput = Record<string, unknown>> {
166
132
  */
167
133
  export class InputManager<TSchema extends z.ZodSchema = z.ZodSchema> {
168
134
  private inputBuffer = new Map<string, Record<string, unknown>>();
169
- private latchState = new Map<string, LatchState>();
135
+ private behaviorState = new Map<string, ControllerBehaviorState>();
136
+ private behaviorOverrides = new Map<string, InputFieldBehavior>();
170
137
  private config: InputConfig<TSchema>;
171
138
 
172
139
  constructor(config: InputConfig<TSchema> = {}) {
173
140
  this.config = config;
141
+ this.behaviorOverrides = this.createBehaviorOverrides(config.behavior);
174
142
  }
175
143
 
176
144
  /**
@@ -178,39 +146,15 @@ export class InputManager<TSchema extends z.ZodSchema = z.ZodSchema> {
178
146
  * Should be called when 'server:input' event is received.
179
147
  */
180
148
  handleInput(payload: ControllerInputEvent): void {
181
- // Note: We don't filter by roomId here because:
182
- // 1. The server already routes input correctly to the right host
183
- // 2. Multiple host instances might share the same InputManager instance
184
- // 3. The roomId check was causing false rejections when hosts re-register
185
-
186
- // Store raw input as-is (arbitrary structure)
149
+ // The server already routes by room; keep host runtime hot path minimal.
187
150
  this.inputBuffer.set(payload.controllerId, payload.input);
188
-
189
- // Throttled logging - only log active input, once per second max
190
- const now = Date.now();
191
- const input = payload?.input;
192
- const hasActiveInput =
193
- input &&
194
- (input.action === true ||
195
- (typeof input.vector === "object" &&
196
- input.vector !== null &&
197
- (Math.abs((input.vector as { x?: number; y?: number }).x ?? 0) >
198
- 0.01 ||
199
- Math.abs((input.vector as { x?: number; y?: number }).y ?? 0) >
200
- 0.01)));
201
-
202
- if (
203
- hasActiveInput &&
204
- (!lastInputManagerLogTime || now - lastInputManagerLogTime > 1000)
205
- ) {
206
- lastInputManagerLogTime = now;
207
- }
151
+ this.captureTransientBehaviors(payload.controllerId, payload.input);
208
152
  }
209
153
 
210
154
  /**
211
155
  * Gets input for a specific controller.
212
156
  * Returns validated and typed input if schema is provided, otherwise raw Record.
213
- * If latching is configured, applies latching logic.
157
+ * Applies configured input behavior semantics.
214
158
  */
215
159
  getInput(controllerId: string): z.infer<TSchema> | undefined {
216
160
  const rawInput = this.inputBuffer.get(controllerId);
@@ -234,15 +178,10 @@ export class InputManager<TSchema extends z.ZodSchema = z.ZodSchema> {
234
178
  validatedInput = result.data as Record<string, unknown>;
235
179
  }
236
180
 
237
- // Apply latching if configured
238
- if (this.config.latch) {
239
- return this.getLatched(
240
- controllerId,
241
- validatedInput as Record<string, unknown>,
242
- ) as z.infer<TSchema>;
243
- }
244
-
245
- return validatedInput as z.infer<TSchema>;
181
+ return this.applyBehaviors(
182
+ controllerId,
183
+ validatedInput as Record<string, unknown>,
184
+ ) as z.infer<TSchema>;
246
185
  }
247
186
 
248
187
  /**
@@ -250,127 +189,168 @@ export class InputManager<TSchema extends z.ZodSchema = z.ZodSchema> {
250
189
  */
251
190
  clearInput(controllerId: string): void {
252
191
  this.inputBuffer.delete(controllerId);
253
- this.latchState.delete(controllerId);
192
+ this.behaviorState.delete(controllerId);
254
193
  }
255
194
 
256
- /**
257
- * Internal method to apply latching logic.
258
- */
259
- private getLatched<TInput extends Record<string, unknown>>(
260
- controllerId: string,
261
- rawInput: TInput,
262
- ): TInput | undefined {
263
- const { booleanFields = [], vectorFields = [] } = this.config.latch!;
264
- const state = this.latchState.get(controllerId);
265
-
266
- // Simple hash to detect if raw input changed
267
- const rawHash = JSON.stringify(rawInput);
268
- const rawChanged = rawHash !== state?.lastRawHash;
269
-
270
- // If raw hasn't changed and we have previous state, we already consumed
271
- // Return raw (reset behavior - like old popInput did)
272
- if (!rawChanged && state) {
273
- // Reset latched to raw for next frame (consume behavior)
274
- const resetLatched = { ...rawInput } as TInput;
275
- const resetLatchedRecord = resetLatched as Record<string, unknown>;
276
- const actualRawRecord = rawInput as Record<string, unknown>;
277
-
278
- // Reset boolean fields to raw
279
- for (const field of booleanFields) {
280
- const rawValue = actualRawRecord[field];
281
- resetLatchedRecord[field] = rawValue ?? false;
282
- }
195
+ private getBehaviorState(controllerId: string): ControllerBehaviorState {
196
+ const existing = this.behaviorState.get(controllerId);
197
+ if (existing) {
198
+ return existing;
199
+ }
283
200
 
284
- // Reset vector fields to raw
285
- for (const field of vectorFields) {
286
- const rawValue = actualRawRecord[field];
287
- if (
288
- rawValue &&
289
- typeof rawValue === "object" &&
290
- !Array.isArray(rawValue)
291
- ) {
292
- const rawVec = rawValue as { x?: unknown; y?: unknown };
293
- if (typeof rawVec.x === "number" && typeof rawVec.y === "number") {
294
- resetLatchedRecord[field] = rawVec;
295
- } else {
296
- resetLatchedRecord[field] = { x: 0, y: 0 };
297
- }
298
- } else {
299
- resetLatchedRecord[field] = { x: 0, y: 0 };
201
+ const created: ControllerBehaviorState = {
202
+ pulseBooleans: new Set<string>(),
203
+ pulseVectors: new Map<string, VectorValue>(),
204
+ holdVectors: new Map<string, VectorValue>(),
205
+ };
206
+ this.behaviorState.set(controllerId, created);
207
+ return created;
208
+ }
209
+
210
+ private createBehaviorOverrides(
211
+ behavior: InputConfig<TSchema>["behavior"],
212
+ ): Map<string, InputFieldBehavior> {
213
+ const overrides = new Map<string, InputFieldBehavior>();
214
+
215
+ const assign = (fields: string[] | undefined, mode: InputFieldBehavior) => {
216
+ for (const field of fields ?? []) {
217
+ const existing = overrides.get(field);
218
+ if (existing && existing !== mode) {
219
+ throw new Error(
220
+ `[InputManager] Field "${field}" is configured for both "${existing}" and "${mode}".`,
221
+ );
300
222
  }
223
+ overrides.set(field, mode);
301
224
  }
225
+ };
302
226
 
303
- // Update state with reset values
304
- this.latchState.set(controllerId, {
305
- raw: rawInput,
306
- latched: resetLatched,
307
- lastRawHash: rawHash,
308
- });
227
+ assign(behavior?.pulse, "pulse");
228
+ assign(behavior?.hold, "hold");
229
+ assign(behavior?.latest, "latest");
309
230
 
310
- return resetLatched; // Return raw (consumed)
231
+ return overrides;
232
+ }
233
+
234
+ private resolveFieldBehavior(
235
+ field: string,
236
+ value: unknown,
237
+ ): InputFieldBehavior {
238
+ const override = this.behaviorOverrides.get(field);
239
+ if (override) {
240
+ return override;
241
+ }
242
+ if (typeof value === "boolean") {
243
+ return "pulse";
311
244
  }
245
+ if (this.isVectorValue(value)) {
246
+ return "latest";
247
+ }
248
+ return "latest";
249
+ }
312
250
 
313
- // New input or first call - calculate latched
314
- const prevLatched = state?.latched ?? ({} as TInput);
251
+ private isVectorValue(value: unknown): value is VectorValue {
252
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
253
+ return false;
254
+ }
255
+ const vector = value as { x?: unknown; y?: unknown };
256
+ return typeof vector.x === "number" && typeof vector.y === "number";
257
+ }
315
258
 
316
- const latched = { ...rawInput } as TInput;
317
- const latchedRecord = latched as Record<string, unknown>;
318
- const actualRawRecord = rawInput as Record<string, unknown>;
319
- const prevLatchedRecord = prevLatched as Record<string, unknown>;
259
+ private isActiveVector(vector: VectorValue): boolean {
260
+ return vector.x !== 0 || vector.y !== 0;
261
+ }
262
+
263
+ private captureTransientBehaviors(
264
+ controllerId: string,
265
+ rawInput: Record<string, unknown>,
266
+ ): void {
267
+ const state = this.getBehaviorState(controllerId);
320
268
 
321
- // Latch boolean fields
322
- for (const field of booleanFields) {
323
- const rawValue = actualRawRecord[field];
324
- const prevValue = prevLatchedRecord[field];
269
+ for (const [field, value] of Object.entries(rawInput)) {
270
+ const behavior = this.resolveFieldBehavior(field, value);
325
271
 
326
- if (typeof rawValue === "boolean") {
327
- // Latch: true if currently true OR was true last frame
328
- latchedRecord[field] = rawValue || prevValue === true;
272
+ if (behavior === "pulse") {
273
+ if (typeof value === "boolean") {
274
+ if (value) {
275
+ state.pulseBooleans.add(field);
276
+ }
277
+ continue;
278
+ }
279
+ if (this.isVectorValue(value) && this.isActiveVector(value)) {
280
+ state.pulseVectors.set(field, value);
281
+ }
282
+ continue;
283
+ }
284
+
285
+ if (behavior === "hold" && this.isVectorValue(value)) {
286
+ if (this.isActiveVector(value)) {
287
+ state.holdVectors.set(field, value);
288
+ }
329
289
  }
330
290
  }
291
+ }
292
+
293
+ private applyBehaviors<TInput extends Record<string, unknown>>(
294
+ controllerId: string,
295
+ validatedInput: TInput,
296
+ ): TInput {
297
+ const state = this.getBehaviorState(controllerId);
298
+ const output = { ...validatedInput } as Record<string, unknown>;
299
+ const processedFields = new Set<string>();
300
+
301
+ for (const [field, value] of Object.entries(validatedInput)) {
302
+ processedFields.add(field);
303
+ const behavior = this.resolveFieldBehavior(field, value);
304
+
305
+ if (behavior === "latest") {
306
+ continue;
307
+ }
308
+
309
+ if (behavior === "pulse") {
310
+ if (typeof value === "boolean") {
311
+ const pending = state.pulseBooleans.has(field);
312
+ output[field] = value === true || pending;
313
+ state.pulseBooleans.delete(field);
314
+ continue;
315
+ }
331
316
 
332
- // Latch vector fields (with zero detection)
333
- for (const field of vectorFields) {
334
- const rawValue = actualRawRecord[field];
335
- const prevValue = prevLatchedRecord[field];
336
-
337
- if (
338
- rawValue &&
339
- typeof rawValue === "object" &&
340
- !Array.isArray(rawValue)
341
- ) {
342
- const rawVec = rawValue as { x?: unknown; y?: unknown };
343
- const prevVec = prevValue as { x?: unknown; y?: unknown } | undefined;
344
-
345
- if (typeof rawVec.x === "number" && typeof rawVec.y === "number") {
346
- const isRawActive = rawVec.x !== 0 || rawVec.y !== 0;
347
- const isPrevActive =
348
- prevVec &&
349
- typeof prevVec.x === "number" &&
350
- typeof prevVec.y === "number" &&
351
- (prevVec.x !== 0 || prevVec.y !== 0);
352
-
353
- if (isRawActive) {
354
- // Currently active - use current value
355
- latchedRecord[field] = rawVec;
356
- } else if (isPrevActive) {
357
- // Just released but was active - keep previous value (flick)
358
- latchedRecord[field] = prevVec;
359
- } else {
360
- // Not active - use zero
361
- latchedRecord[field] = { x: 0, y: 0 };
317
+ if (this.isVectorValue(value)) {
318
+ const pending = state.pulseVectors.get(field);
319
+ if (pending) {
320
+ output[field] = this.isActiveVector(value) ? value : pending;
321
+ state.pulseVectors.delete(field);
362
322
  }
323
+ continue;
324
+ }
325
+
326
+ continue;
327
+ }
328
+
329
+ // hold
330
+ if (this.isVectorValue(value)) {
331
+ if (this.isActiveVector(value)) {
332
+ state.holdVectors.set(field, value);
333
+ output[field] = value;
334
+ } else {
335
+ output[field] = state.holdVectors.get(field) ?? { x: 0, y: 0 };
363
336
  }
364
337
  }
365
338
  }
366
339
 
367
- // Store state for next frame
368
- this.latchState.set(controllerId, {
369
- raw: rawInput,
370
- latched,
371
- lastRawHash: rawHash,
372
- });
340
+ // Include pending pulse values for optional fields not present in current payload.
341
+ for (const field of Array.from(state.pulseBooleans)) {
342
+ if (!processedFields.has(field)) {
343
+ output[field] = true;
344
+ state.pulseBooleans.delete(field);
345
+ }
346
+ }
347
+ for (const [field, vector] of Array.from(state.pulseVectors.entries())) {
348
+ if (!processedFields.has(field)) {
349
+ output[field] = vector;
350
+ state.pulseVectors.delete(field);
351
+ }
352
+ }
373
353
 
374
- return latched;
354
+ return output as TInput;
375
355
  }
376
356
  }
@@ -0,0 +1,9 @@
1
+ export {
2
+ isActiveMatchPhase,
3
+ isEndedMatchPhase,
4
+ isStandardMatchPhase,
5
+ standardMatchPhases,
6
+ toShellMatchPhase,
7
+ type ShellMatchPhase,
8
+ type StandardMatchPhase,
9
+ } from "./standard-match-phase";
@@ -0,0 +1,32 @@
1
+ export const standardMatchPhases = [
2
+ "lobby",
3
+ "countdown",
4
+ "playing",
5
+ "ended",
6
+ ] as const;
7
+
8
+ export type StandardMatchPhase = (typeof standardMatchPhases)[number];
9
+ export type ShellMatchPhase = Exclude<StandardMatchPhase, "countdown">;
10
+
11
+ export const isStandardMatchPhase = (
12
+ value: string,
13
+ ): value is StandardMatchPhase => {
14
+ return (standardMatchPhases as readonly string[]).includes(value);
15
+ };
16
+
17
+ export const isActiveMatchPhase = (phase: StandardMatchPhase): boolean => {
18
+ return phase === "countdown" || phase === "playing";
19
+ };
20
+
21
+ export const isEndedMatchPhase = (phase: StandardMatchPhase): boolean => {
22
+ return phase === "ended";
23
+ };
24
+
25
+ export const toShellMatchPhase = (
26
+ phase: StandardMatchPhase,
27
+ ): ShellMatchPhase => {
28
+ if (phase === "countdown") {
29
+ return "playing";
30
+ }
31
+ return phase;
32
+ };