@air-jam/sdk 0.1.4 → 0.9.1

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 (357) 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 +481 -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 +14 -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 +544 -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 +29 -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 +1737 -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 +123 -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-32NYL7XI.js +539 -0
  70. package/dist/chunk-32NYL7XI.js.map +1 -0
  71. package/dist/chunk-3USU7GY2.js +54 -0
  72. package/dist/chunk-3USU7GY2.js.map +1 -0
  73. package/dist/chunk-7UXSWCZZ.js +32 -0
  74. package/dist/chunk-7UXSWCZZ.js.map +1 -0
  75. package/dist/chunk-BE6Z4SIT.js +65 -0
  76. package/dist/chunk-BE6Z4SIT.js.map +1 -0
  77. package/dist/chunk-CABEUV65.js +184 -0
  78. package/dist/chunk-CABEUV65.js.map +1 -0
  79. package/dist/chunk-CEJ5RFSG.js +288 -0
  80. package/dist/chunk-CEJ5RFSG.js.map +1 -0
  81. package/dist/chunk-EDRQIOCV.js +25 -0
  82. package/dist/chunk-EDRQIOCV.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-FAT2KJ3K.js +238 -0
  86. package/dist/chunk-FAT2KJ3K.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-JEHV5C2X.js +249 -0
  90. package/dist/chunk-JEHV5C2X.js.map +1 -0
  91. package/dist/chunk-JO2HR5PV.js +43 -0
  92. package/dist/chunk-JO2HR5PV.js.map +1 -0
  93. package/dist/chunk-JTF47B56.js +1 -0
  94. package/dist/chunk-JTF47B56.js.map +1 -0
  95. package/dist/chunk-KXAFI5OP.js +193 -0
  96. package/dist/chunk-KXAFI5OP.js.map +1 -0
  97. package/dist/chunk-LIXPF4P6.js +22 -0
  98. package/dist/chunk-LIXPF4P6.js.map +1 -0
  99. package/dist/chunk-MPDMJIX5.js +1 -0
  100. package/dist/chunk-MPDMJIX5.js.map +1 -0
  101. package/dist/chunk-NCL6WJCL.js +91 -0
  102. package/dist/chunk-NCL6WJCL.js.map +1 -0
  103. package/dist/chunk-NUJBMV3O.js +1435 -0
  104. package/dist/chunk-NUJBMV3O.js.map +1 -0
  105. package/dist/chunk-TGRTEWYV.js +195 -0
  106. package/dist/chunk-TGRTEWYV.js.map +1 -0
  107. package/dist/chunk-UWJOIVON.js +369 -0
  108. package/dist/chunk-UWJOIVON.js.map +1 -0
  109. package/dist/chunk-VKBMZHMQ.js +216 -0
  110. package/dist/chunk-VKBMZHMQ.js.map +1 -0
  111. package/dist/chunk-W2VK36RO.js +11 -0
  112. package/dist/chunk-W2VK36RO.js.map +1 -0
  113. package/dist/chunk-W3YA5BWR.js +126 -0
  114. package/dist/chunk-W3YA5BWR.js.map +1 -0
  115. package/dist/chunk-X24K52DG.js +12 -0
  116. package/dist/chunk-X24K52DG.js.map +1 -0
  117. package/dist/chunk-YXHH3MQT.js +230 -0
  118. package/dist/chunk-YXHH3MQT.js.map +1 -0
  119. package/dist/chunk-ZLPL2R3R.js +183 -0
  120. package/dist/chunk-ZLPL2R3R.js.map +1 -0
  121. package/dist/chunk-ZTQ5SP4H.js +9 -0
  122. package/dist/chunk-ZTQ5SP4H.js.map +1 -0
  123. package/dist/controller-Bod8YYr1.d.cts +172 -0
  124. package/dist/controller-DxVchfiA.d.ts +172 -0
  125. package/dist/dev-log-events-CVAM29hx.d.cts +123 -0
  126. package/dist/dev-log-events-CVAM29hx.d.ts +123 -0
  127. package/dist/handshake-CC_LrK5g.d.cts +31 -0
  128. package/dist/handshake-CC_LrK5g.d.ts +31 -0
  129. package/dist/host-8hmq_Sgc.d.cts +109 -0
  130. package/dist/host-Cw-N6e-5.d.ts +109 -0
  131. package/dist/index.cjs +8730 -3426
  132. package/dist/index.cjs.map +1 -1
  133. package/dist/index.d.cts +676 -1070
  134. package/dist/index.d.ts +676 -1070
  135. package/dist/index.js +4892 -3196
  136. package/dist/index.js.map +1 -1
  137. package/dist/metadata.cjs +114 -0
  138. package/dist/metadata.cjs.map +1 -0
  139. package/dist/metadata.d.cts +95 -0
  140. package/dist/metadata.d.ts +95 -0
  141. package/dist/metadata.js +83 -0
  142. package/dist/metadata.js.map +1 -0
  143. package/dist/notices-CnGaaS8B.d.ts +38 -0
  144. package/dist/notices-NyDM5Mtz.d.cts +38 -0
  145. package/dist/platform-machine.cjs +425 -0
  146. package/dist/platform-machine.cjs.map +1 -0
  147. package/dist/platform-machine.d.cts +1183 -0
  148. package/dist/platform-machine.d.ts +1183 -0
  149. package/dist/platform-machine.js +309 -0
  150. package/dist/platform-machine.js.map +1 -0
  151. package/dist/platform-settings-cuor3i11.d.cts +48 -0
  152. package/dist/platform-settings-cuor3i11.d.ts +48 -0
  153. package/dist/prefabs.cjs +36 -0
  154. package/dist/prefabs.cjs.map +1 -0
  155. package/dist/prefabs.d.cts +49 -0
  156. package/dist/prefabs.d.ts +49 -0
  157. package/dist/prefabs.js +8 -0
  158. package/dist/prefabs.js.map +1 -0
  159. package/dist/preview.cjs +2620 -0
  160. package/dist/preview.cjs.map +1 -0
  161. package/dist/preview.d.cts +155 -0
  162. package/dist/preview.d.ts +155 -0
  163. package/dist/preview.js +1671 -0
  164. package/dist/preview.js.map +1 -0
  165. package/dist/protocol.cjs +621 -54
  166. package/dist/protocol.cjs.map +1 -1
  167. package/dist/protocol.d.cts +76 -240
  168. package/dist/protocol.d.ts +76 -240
  169. package/dist/protocol.js +100 -2
  170. package/dist/protocol.js.map +1 -1
  171. package/dist/realtime-client-Cu8yVJFw.d.cts +15 -0
  172. package/dist/realtime-client-Cu8yVJFw.d.ts +15 -0
  173. package/dist/release.cjs +143 -0
  174. package/dist/release.cjs.map +1 -0
  175. package/dist/release.d.cts +64 -0
  176. package/dist/release.d.ts +64 -0
  177. package/dist/release.js +43 -0
  178. package/dist/release.js.map +1 -0
  179. package/dist/runtime-topology.cjs +405 -0
  180. package/dist/runtime-topology.cjs.map +1 -0
  181. package/dist/runtime-topology.d.cts +59 -0
  182. package/dist/runtime-topology.d.ts +59 -0
  183. package/dist/runtime-topology.js +31 -0
  184. package/dist/runtime-topology.js.map +1 -0
  185. package/dist/standard-match-phase-BucA5dYo.d.ts +44 -0
  186. package/dist/standard-match-phase-U2ra8J-0.d.cts +44 -0
  187. package/dist/sync-B6QsNrXz.d.cts +125 -0
  188. package/dist/sync-B6QsNrXz.d.ts +125 -0
  189. package/dist/ui.cjs +3061 -0
  190. package/dist/ui.cjs.map +1 -0
  191. package/dist/ui.d.cts +320 -0
  192. package/dist/ui.d.ts +320 -0
  193. package/dist/ui.js +1719 -0
  194. package/dist/ui.js.map +1 -0
  195. package/dist/url-policy-C89zxgw9.d.cts +9 -0
  196. package/dist/url-policy-C89zxgw9.d.ts +9 -0
  197. package/package.json +155 -10
  198. package/src/agent/agent-action.ts +304 -0
  199. package/src/agent/agent-contract.ts +180 -0
  200. package/src/agent-tooling.ts +20 -0
  201. package/src/arcade/bridge/controller.ts +15 -0
  202. package/src/arcade/bridge/host.ts +12 -0
  203. package/src/arcade/bridge/iframe.ts +61 -0
  204. package/src/arcade/bridge.ts +3 -0
  205. package/src/arcade/host.ts +1 -0
  206. package/src/arcade/surface.ts +4 -0
  207. package/src/arcade/url.ts +60 -0
  208. package/src/arcade.ts +4 -0
  209. package/src/audio/audio-manager.ts +162 -171
  210. package/src/audio/hooks.tsx +218 -97
  211. package/src/audio/index.ts +1 -1
  212. package/src/audio/music.tsx +139 -0
  213. package/src/capabilities/manifest.ts +71 -0
  214. package/src/capabilities.ts +12 -0
  215. package/src/components/connection-status-pill.tsx +55 -0
  216. package/src/components/controller-player-name-field.tsx +119 -0
  217. package/src/components/controller-primary-action.tsx +58 -0
  218. package/src/components/host-mute-button.tsx +49 -0
  219. package/src/components/join-qr-overlay.tsx +133 -0
  220. package/src/components/join-url-action-buttons.tsx +95 -0
  221. package/src/components/join-url-controls.tsx +58 -0
  222. package/src/components/join-url-field.tsx +52 -0
  223. package/src/components/lifecycle-action-group.tsx +129 -0
  224. package/src/components/player-avatar-strip.tsx +65 -0
  225. package/src/components/player-avatar.tsx +42 -13
  226. package/src/components/room-qr-code.tsx +125 -0
  227. package/src/components/runtime-shell-header.tsx +37 -0
  228. package/src/components/shell-classes.ts +19 -0
  229. package/src/components/surface-orientation-layout.ts +60 -0
  230. package/src/components/surface-viewport-layout.ts +268 -0
  231. package/src/components/surface-viewport.tsx +314 -0
  232. package/src/components/ui/button.tsx +4 -6
  233. package/src/components/ui/slider.tsx +1 -1
  234. package/src/components/volume-controls.tsx +65 -6
  235. package/src/context/air-jam-context.tsx +209 -161
  236. package/src/context/index.ts +2 -2
  237. package/src/context/session-providers.tsx +77 -0
  238. package/src/context/session-scope.ts +64 -0
  239. package/src/context/socket-manager.ts +6 -24
  240. package/src/contracts/v2/bridge.ts +261 -0
  241. package/src/contracts/v2/handshake.ts +40 -0
  242. package/src/contracts/v2/index.ts +2 -0
  243. package/src/dev/browser-log-sink.ts +912 -0
  244. package/src/diagnostics.ts +128 -0
  245. package/src/events.ts +7 -34
  246. package/src/hooks/internal/use-controller-runtime-api.ts +875 -0
  247. package/src/hooks/internal/use-host-runtime-api.ts +1051 -0
  248. package/src/hooks/internal/use-runtime-control-contract.ts +15 -0
  249. package/src/hooks/internal/use-runtime-inspection-contract.ts +16 -0
  250. package/src/hooks/internal/use-runtime-observability-contract.ts +24 -0
  251. package/src/hooks/use-air-jam-controller.ts +155 -475
  252. package/src/hooks/use-air-jam-host.ts +156 -485
  253. package/src/hooks/use-connection-status.ts +8 -0
  254. package/src/hooks/use-controller-lifecycle-intents.ts +27 -0
  255. package/src/hooks/use-controller-lifecycle-permissions.ts +36 -0
  256. package/src/hooks/use-controller-shell-status.ts +40 -0
  257. package/src/hooks/use-controller-tick.ts +65 -0
  258. package/src/hooks/use-controller-toasts.ts +116 -0
  259. package/src/hooks/use-get-input.ts +8 -5
  260. package/src/hooks/use-host-arcade-restore.ts +30 -0
  261. package/src/hooks/use-host-join-controls.ts +152 -0
  262. package/src/hooks/use-host-tick.ts +171 -0
  263. package/src/hooks/use-input-writer.ts +83 -0
  264. package/src/hooks/use-lifecycle-action-group-model.ts +98 -0
  265. package/src/hooks/use-players.ts +12 -0
  266. package/src/hooks/use-room.ts +22 -0
  267. package/src/hooks/use-send-signal.ts +6 -2
  268. package/src/index.ts +205 -31
  269. package/src/internal/input-manager.ts +195 -215
  270. package/src/lifecycle/index.ts +9 -0
  271. package/src/lifecycle/standard-match-phase.ts +32 -0
  272. package/src/metadata/manifest.ts +152 -0
  273. package/src/metadata.ts +12 -0
  274. package/src/platform-machine.ts +475 -0
  275. package/src/prefabs/types.ts +82 -0
  276. package/src/prefabs.ts +12 -0
  277. package/src/preview/host-workspace.tsx +59 -0
  278. package/src/preview/identity.ts +66 -0
  279. package/src/preview/layout.ts +223 -0
  280. package/src/preview/manager.ts +577 -0
  281. package/src/preview/messages.ts +26 -0
  282. package/src/preview/ownership.ts +81 -0
  283. package/src/preview/url.ts +117 -0
  284. package/src/preview/window.tsx +490 -0
  285. package/src/preview/workspace.tsx +678 -0
  286. package/src/preview.ts +42 -0
  287. package/src/protocol/arcade-platform-actions.ts +29 -0
  288. package/src/protocol/controller.ts +187 -0
  289. package/src/protocol/core.ts +23 -0
  290. package/src/protocol/dev-log-events.ts +222 -0
  291. package/src/protocol/errors.ts +21 -0
  292. package/src/protocol/host-grant.ts +185 -0
  293. package/src/protocol/host.ts +138 -0
  294. package/src/protocol/notices.ts +43 -0
  295. package/src/protocol/signals.ts +38 -0
  296. package/src/protocol/socket-events.ts +154 -0
  297. package/src/protocol/sync.ts +121 -0
  298. package/src/protocol/url-policy.ts +63 -0
  299. package/src/protocol.ts +12 -386
  300. package/src/qrcode.d.ts +16 -0
  301. package/src/release.ts +99 -0
  302. package/src/runtime/air-jam-config.ts +287 -0
  303. package/src/runtime/air-jam-error-boundary.tsx +672 -0
  304. package/src/runtime/arcade-bridge-request-surface.ts +15 -0
  305. package/src/runtime/arcade-runtime-url.ts +95 -0
  306. package/src/runtime/arcade-surface-identity.ts +16 -0
  307. package/src/runtime/contracts/control.ts +68 -0
  308. package/src/runtime/contracts/inspection.ts +165 -0
  309. package/src/runtime/contracts/observability.ts +94 -0
  310. package/src/runtime/controller-bridge.ts +451 -0
  311. package/src/runtime/controller-identity.ts +132 -0
  312. package/src/runtime/controller-presentation.ts +24 -0
  313. package/src/runtime/controller-realtime-client.ts +591 -0
  314. package/src/runtime/create-air-jam-app.tsx +260 -0
  315. package/src/runtime/dev-runtime-events.ts +36 -0
  316. package/src/runtime/embedded-runtime-adapters.ts +68 -0
  317. package/src/runtime/host-bridge.ts +388 -0
  318. package/src/runtime/host-realtime-client.ts +550 -0
  319. package/src/runtime/iframe-bridge.ts +135 -0
  320. package/src/runtime/realtime-client.ts +20 -0
  321. package/src/runtime/router-basename.ts +25 -0
  322. package/src/runtime/runtime-owner-contexts.tsx +4 -0
  323. package/src/runtime/runtime-session-params.ts +169 -0
  324. package/src/runtime/sdk-version.ts +6 -0
  325. package/src/runtime/session-runtimes.tsx +188 -0
  326. package/src/runtime/validate-arcade-bridge-attach.ts +14 -0
  327. package/src/runtime-control.ts +10 -0
  328. package/src/runtime-inspection.ts +14 -0
  329. package/src/runtime-observability.ts +9 -0
  330. package/src/runtime-topology.ts +569 -0
  331. package/src/settings/platform-settings-bridge.ts +336 -0
  332. package/src/settings/platform-settings-runtime.tsx +387 -0
  333. package/src/settings/platform-settings.ts +288 -0
  334. package/src/state/connection-store.ts +80 -12
  335. package/src/store/air-jam-store-domain-constants.ts +4 -0
  336. package/src/store/create-air-jam-store.ts +1094 -227
  337. package/src/styles.css +4 -38
  338. package/src/ui.ts +29 -0
  339. package/src/utils/is-rpc-serializable.ts +56 -0
  340. package/src/utils/player-avatar-url.ts +48 -0
  341. package/src/utils/url-builder.ts +7 -0
  342. package/dist/.tsbuildinfo +0 -1
  343. package/dist/chunk-B5DURUWT.js +0 -80
  344. package/dist/chunk-B5DURUWT.js.map +0 -1
  345. package/src/AirJamClient.ts +0 -224
  346. package/src/audio/volume-store.ts +0 -124
  347. package/src/components/controller-shell.tsx +0 -346
  348. package/src/components/host-shell.tsx +0 -520
  349. package/src/components/qr-scanner-dialog.tsx +0 -373
  350. package/src/components/ui/alert.tsx +0 -66
  351. package/src/components/ui/avatar.tsx +0 -46
  352. package/src/components/ui/badge.tsx +0 -47
  353. package/src/components/ui/card.tsx +0 -92
  354. package/src/components/ui/dialog.tsx +0 -122
  355. package/src/components/ui/tabs.tsx +0 -64
  356. package/src/hooks/use-air-jam-shell.ts +0 -379
  357. package/src/hooks/use-fullscreen.ts +0 -108
@@ -0,0 +1,912 @@
1
+ import {
2
+ isLocalDevControlSurfaceTopology,
3
+ type ProxyStrategy,
4
+ type ResolvedAirJamRuntimeTopology,
5
+ } from "../runtime-topology";
6
+ import {
7
+ onAirJamDiagnostic,
8
+ type AirJamDiagnostic,
9
+ type AirJamDiagnosticSeverity,
10
+ } from "../diagnostics";
11
+ import type {
12
+ AirJamDevBrowserConsoleCategory,
13
+ AirJamDevBrowserLogSource,
14
+ AirJamDevLogEventName,
15
+ } from "../protocol";
16
+ import {
17
+ AIRJAM_DEV_LOG_EVENTS,
18
+ resolveAirJamBrowserConsoleCategory,
19
+ } from "../protocol";
20
+ import {
21
+ AIRJAM_DEV_LOG_SINK_FAILURE,
22
+ AIRJAM_DEV_RUNTIME_EVENT,
23
+ type AirJamDevRuntimeEventDetail,
24
+ } from "../runtime/dev-runtime-events";
25
+
26
+ type BrowserLogLevel = "debug" | "info" | "warn" | "error";
27
+ type BrowserLogSource = AirJamDevBrowserLogSource;
28
+
29
+ interface BrowserLogEntry {
30
+ occurredAt: string;
31
+ level: BrowserLogLevel;
32
+ source: BrowserLogSource;
33
+ message: string;
34
+ sourceSeq?: number;
35
+ repeatCount?: number;
36
+ event?: AirJamDevLogEventName;
37
+ data?: unknown[];
38
+ stack?: string;
39
+ code?: string;
40
+ consoleCategory?: AirJamDevBrowserConsoleCategory;
41
+ role?: "host" | "controller";
42
+ traceId?: string;
43
+ roomId?: string;
44
+ controllerId?: string;
45
+ runtimeEpoch?: number;
46
+ runtimeKind?: string;
47
+ }
48
+
49
+ interface BrowserLogBatchPayload {
50
+ mode: "reset" | "append";
51
+ sessionId: string;
52
+ metadata: {
53
+ appId?: string;
54
+ traceId?: string;
55
+ roomId?: string;
56
+ controllerId?: string;
57
+ role?: "host" | "controller";
58
+ origin?: string;
59
+ pathname?: string;
60
+ href?: string;
61
+ title?: string;
62
+ userAgent?: string;
63
+ };
64
+ entries: BrowserLogEntry[];
65
+ }
66
+
67
+ interface BrowserLogUnloadPayload {
68
+ sessionId: string;
69
+ metadata: BrowserLogBatchPayload["metadata"];
70
+ entry: BrowserLogEntry;
71
+ }
72
+
73
+ type ConsoleMethodName = "debug" | "info" | "log" | "warn" | "error";
74
+
75
+ interface BrowserLogSinkOptions {
76
+ backendOrigin?: string;
77
+ serverUrl?: string;
78
+ appOrigin?: string;
79
+ proxyStrategy?: ProxyStrategy;
80
+ appId?: string;
81
+ topology?: Pick<ResolvedAirJamRuntimeTopology, "runtimeMode">;
82
+ }
83
+
84
+ interface BrowserLogSinkController {
85
+ update: (options: BrowserLogSinkOptions) => void;
86
+ dispose?: () => void;
87
+ }
88
+
89
+ interface BrowserLogSinkGlobal {
90
+ __airJamBrowserLogSink__?: BrowserLogSinkController;
91
+ __airJamHostTraceId__?: string;
92
+ __airJamDevLogContext__?: DevBrowserLogContext;
93
+ }
94
+
95
+ export interface DevBrowserLogContext {
96
+ traceId?: string;
97
+ roomId?: string;
98
+ controllerId?: string;
99
+ role?: "host" | "controller";
100
+ }
101
+
102
+ const FLUSH_INTERVAL_MS = 200;
103
+ const CONSOLE_REPEAT_COALESCE_WINDOW_MS = 1000;
104
+
105
+ const resolveNodeEnvMode = (): string | undefined => {
106
+ return (
107
+ globalThis as {
108
+ process?: { env?: Record<string, string | undefined> };
109
+ }
110
+ ).process?.env?.NODE_ENV;
111
+ };
112
+
113
+ const resolveImportMetaDev = (): boolean | undefined => {
114
+ try {
115
+ const meta = import.meta as ImportMeta & { env?: { DEV?: boolean } };
116
+ if (meta?.env && typeof meta.env.DEV === "boolean") {
117
+ return meta.env.DEV;
118
+ }
119
+ } catch {
120
+ return undefined;
121
+ }
122
+
123
+ return undefined;
124
+ };
125
+
126
+ const isDevelopmentRuntime = (): boolean => {
127
+ const importMetaDev = resolveImportMetaDev();
128
+ if (typeof importMetaDev === "boolean") {
129
+ return importMetaDev;
130
+ }
131
+
132
+ return resolveNodeEnvMode() !== "production";
133
+ };
134
+
135
+ const resolveEndpointBase = ({
136
+ backendOrigin,
137
+ serverUrl,
138
+ appOrigin,
139
+ proxyStrategy,
140
+ }: Pick<
141
+ BrowserLogSinkOptions,
142
+ "backendOrigin" | "serverUrl" | "appOrigin" | "proxyStrategy"
143
+ >): string | null => {
144
+ if (proxyStrategy && proxyStrategy !== "none") {
145
+ return (appOrigin ?? resolveDevProxyBaseUrl())?.replace(/\/$/, "") ?? null;
146
+ }
147
+
148
+ const explicitOrigin = backendOrigin ?? serverUrl;
149
+ if (!explicitOrigin) {
150
+ return resolveDevProxyBaseUrl();
151
+ }
152
+
153
+ if (explicitOrigin.startsWith("ws://")) {
154
+ return `http://${explicitOrigin.slice(5)}`.replace(/\/$/, "");
155
+ }
156
+ if (explicitOrigin.startsWith("wss://")) {
157
+ return `https://${explicitOrigin.slice(6)}`.replace(/\/$/, "");
158
+ }
159
+
160
+ return explicitOrigin.replace(/\/$/, "");
161
+ };
162
+
163
+ const resolveDevProxyBaseUrl = (): string | null => {
164
+ if (typeof window === "undefined") {
165
+ return null;
166
+ }
167
+
168
+ return window.location.origin.replace(/\/$/, "");
169
+ };
170
+
171
+ const resolveLogEndpoint = (options: BrowserLogSinkOptions): string | null => {
172
+ const baseUrl = resolveEndpointBase(options);
173
+ if (!baseUrl) {
174
+ return null;
175
+ }
176
+ return `${baseUrl}/__airjam/dev/browser-logs`;
177
+ };
178
+
179
+ const resolveUnloadEndpoint = (
180
+ options: BrowserLogSinkOptions,
181
+ ): string | null => {
182
+ const baseUrl = resolveEndpointBase(options);
183
+ if (!baseUrl) {
184
+ return null;
185
+ }
186
+ return `${baseUrl}/__airjam/dev/browser-unload`;
187
+ };
188
+
189
+ const resolveLogLevelFromDiagnostic = (
190
+ severity: AirJamDiagnosticSeverity,
191
+ ): BrowserLogLevel => {
192
+ return severity === "error" ? "error" : "warn";
193
+ };
194
+
195
+ const resolveConsoleLevel = (
196
+ methodName: ConsoleMethodName,
197
+ ): BrowserLogLevel => {
198
+ switch (methodName) {
199
+ case "debug":
200
+ return "debug";
201
+ case "info":
202
+ case "log":
203
+ return "info";
204
+ case "warn":
205
+ return "warn";
206
+ case "error":
207
+ return "error";
208
+ }
209
+ };
210
+
211
+ const describeUnknown = (value: unknown): string => {
212
+ if (typeof value === "string") {
213
+ return value;
214
+ }
215
+ if (value instanceof Error) {
216
+ return value.message;
217
+ }
218
+ if (value === undefined) {
219
+ return "undefined";
220
+ }
221
+ try {
222
+ return JSON.stringify(value);
223
+ } catch {
224
+ return String(value);
225
+ }
226
+ };
227
+
228
+ const toSerializable = (value: unknown): unknown => {
229
+ if (
230
+ value === null ||
231
+ typeof value === "string" ||
232
+ typeof value === "number" ||
233
+ typeof value === "boolean"
234
+ ) {
235
+ return value;
236
+ }
237
+
238
+ if (value instanceof Error) {
239
+ return {
240
+ name: value.name,
241
+ message: value.message,
242
+ stack: value.stack,
243
+ };
244
+ }
245
+
246
+ try {
247
+ return JSON.parse(JSON.stringify(value));
248
+ } catch {
249
+ return describeUnknown(value);
250
+ }
251
+ };
252
+
253
+ const buildConsoleMessage = (args: unknown[]): string => {
254
+ if (args.length === 0) {
255
+ return "(empty console call)";
256
+ }
257
+ return args.map((arg) => describeUnknown(arg)).join(" ");
258
+ };
259
+
260
+ export const resolveBrowserConsoleCategory = (
261
+ args: unknown[],
262
+ ): AirJamDevBrowserConsoleCategory => {
263
+ return resolveAirJamBrowserConsoleCategory(args);
264
+ };
265
+
266
+ const createSessionId = (): string => {
267
+ const cryptoLike = globalThis.crypto as
268
+ | { randomUUID?: () => string }
269
+ | undefined;
270
+ if (cryptoLike?.randomUUID) {
271
+ return cryptoLike.randomUUID();
272
+ }
273
+ return `airjam-browser-${Date.now().toString(36)}`;
274
+ };
275
+
276
+ const pruneUndefined = <T extends Record<string, unknown>>(value: T): T => {
277
+ return Object.fromEntries(
278
+ Object.entries(value).filter(([, entry]) => entry !== undefined),
279
+ ) as T;
280
+ };
281
+
282
+ const resolveDevBrowserLogContext = (): DevBrowserLogContext => {
283
+ const globalState = globalThis as BrowserLogSinkGlobal;
284
+ const explicitContext = globalState.__airJamDevLogContext__;
285
+ if (explicitContext) {
286
+ return explicitContext;
287
+ }
288
+
289
+ if (globalState.__airJamHostTraceId__) {
290
+ return {
291
+ traceId: globalState.__airJamHostTraceId__,
292
+ };
293
+ }
294
+
295
+ return {};
296
+ };
297
+
298
+ export const updateDevBrowserLogContext = (
299
+ patch: DevBrowserLogContext,
300
+ ): void => {
301
+ if (typeof window === "undefined") {
302
+ return;
303
+ }
304
+
305
+ const globalState = globalThis as BrowserLogSinkGlobal;
306
+ const nextValue = pruneUndefined({
307
+ ...resolveDevBrowserLogContext(),
308
+ ...patch,
309
+ });
310
+
311
+ if (Object.keys(nextValue).length === 0) {
312
+ delete globalState.__airJamDevLogContext__;
313
+ delete globalState.__airJamHostTraceId__;
314
+ return;
315
+ }
316
+
317
+ globalState.__airJamDevLogContext__ = nextValue;
318
+ if (nextValue.traceId) {
319
+ globalState.__airJamHostTraceId__ = nextValue.traceId;
320
+ return;
321
+ }
322
+ delete globalState.__airJamHostTraceId__;
323
+ };
324
+
325
+ class BrowserLogSinkRuntime {
326
+ private endpoint: string | null = null;
327
+ private unloadEndpoint: string | null = null;
328
+ private appId: string | undefined;
329
+ private readonly sessionId = createSessionId();
330
+ private readonly queue: BrowserLogEntry[] = [];
331
+ private nextSourceSeq = 1;
332
+ private flushTimer: ReturnType<typeof setTimeout> | null = null;
333
+ private hasReset = false;
334
+ private readonly originalConsole = new Map<
335
+ ConsoleMethodName,
336
+ Console[ConsoleMethodName]
337
+ >();
338
+ private readonly disposers: Array<() => void> = [];
339
+ private installed = false;
340
+
341
+ update(options: BrowserLogSinkOptions): void {
342
+ const previousEndpoint = this.endpoint;
343
+ this.endpoint = resolveLogEndpoint(options);
344
+ this.unloadEndpoint = resolveUnloadEndpoint(options);
345
+ this.appId = options.appId;
346
+
347
+ if (
348
+ this.endpoint &&
349
+ this.endpoint !== previousEndpoint &&
350
+ this.queue.length > 0 &&
351
+ !this.flushTimer
352
+ ) {
353
+ this.flushTimer = setTimeout(() => {
354
+ void this.flush();
355
+ }, 0);
356
+ }
357
+ }
358
+
359
+ private allocateSourceSeq(explicitSourceSeq?: number): number {
360
+ const sourceSeq = explicitSourceSeq ?? this.nextSourceSeq++;
361
+ if (sourceSeq >= this.nextSourceSeq) {
362
+ this.nextSourceSeq = sourceSeq + 1;
363
+ }
364
+ return sourceSeq;
365
+ }
366
+
367
+ private normalizeEntry(entry: BrowserLogEntry): BrowserLogEntry {
368
+ return {
369
+ ...entry,
370
+ sourceSeq: this.allocateSourceSeq(entry.sourceSeq),
371
+ };
372
+ }
373
+
374
+ private reportTransportFailure(
375
+ reason: string,
376
+ details?: Record<string, unknown>,
377
+ ): void {
378
+ const payload = {
379
+ reason,
380
+ endpoint: this.endpoint,
381
+ appId: this.appId,
382
+ origin:
383
+ typeof window !== "undefined" ? window.location.origin : undefined,
384
+ href: typeof window !== "undefined" ? window.location.href : undefined,
385
+ ...details,
386
+ };
387
+
388
+ const originalError =
389
+ this.originalConsole.get("error") ?? console.error.bind(console);
390
+ originalError("[AirJam][AJ_BROWSER_LOG_SINK_POST_FAILED]", payload);
391
+
392
+ if (
393
+ typeof window !== "undefined" &&
394
+ window.parent &&
395
+ window.parent !== window
396
+ ) {
397
+ try {
398
+ window.parent.postMessage(
399
+ {
400
+ type: AIRJAM_DEV_LOG_SINK_FAILURE,
401
+ payload,
402
+ },
403
+ "*",
404
+ );
405
+ } catch {
406
+ // Best effort only
407
+ }
408
+ }
409
+ }
410
+
411
+ install(): void {
412
+ if (typeof window === "undefined" || this.installed) {
413
+ return;
414
+ }
415
+ this.installed = true;
416
+
417
+ const consoleMethods: ConsoleMethodName[] = [
418
+ "debug",
419
+ "info",
420
+ "log",
421
+ "warn",
422
+ "error",
423
+ ];
424
+
425
+ for (const methodName of consoleMethods) {
426
+ this.originalConsole.set(
427
+ methodName,
428
+ window.console[methodName].bind(window.console),
429
+ );
430
+ const original = window.console[methodName].bind(window.console);
431
+ window.console[methodName] = ((...args: unknown[]) => {
432
+ original(...args);
433
+ this.enqueue({
434
+ occurredAt: new Date().toISOString(),
435
+ level: resolveConsoleLevel(methodName),
436
+ source: "console",
437
+ message: buildConsoleMessage(args),
438
+ consoleCategory: resolveBrowserConsoleCategory(args),
439
+ data: args.map((arg) => toSerializable(arg)),
440
+ });
441
+ }) as Console[ConsoleMethodName];
442
+ this.disposers.push(() => {
443
+ window.console[methodName] = original;
444
+ });
445
+ }
446
+
447
+ const errorHandler = (event: ErrorEvent) => {
448
+ this.enqueue({
449
+ occurredAt: new Date().toISOString(),
450
+ level: "error",
451
+ source: "window-error",
452
+ message: event.message || "Unhandled window error",
453
+ stack:
454
+ event.error instanceof Error
455
+ ? event.error.stack
456
+ : typeof event.filename === "string"
457
+ ? `${event.filename}:${event.lineno}:${event.colno}`
458
+ : undefined,
459
+ });
460
+ };
461
+ window.addEventListener("error", errorHandler);
462
+ this.disposers.push(() => {
463
+ window.removeEventListener("error", errorHandler);
464
+ });
465
+
466
+ const rejectionHandler = (event: PromiseRejectionEvent) => {
467
+ const reason =
468
+ event.reason instanceof Error
469
+ ? event.reason.message
470
+ : describeUnknown(event.reason);
471
+ this.enqueue({
472
+ occurredAt: new Date().toISOString(),
473
+ level: "error",
474
+ source: "unhandledrejection",
475
+ message: reason,
476
+ stack: event.reason instanceof Error ? event.reason.stack : undefined,
477
+ });
478
+ };
479
+ window.addEventListener("unhandledrejection", rejectionHandler);
480
+ this.disposers.push(() => {
481
+ window.removeEventListener("unhandledrejection", rejectionHandler);
482
+ });
483
+
484
+ const removeDiagnosticListener = onAirJamDiagnostic((diagnostic) => {
485
+ this.enqueue(this.fromDiagnostic(diagnostic));
486
+ });
487
+ this.disposers.push(removeDiagnosticListener);
488
+
489
+ const runtimeEventHandler = (event: Event) => {
490
+ const detail = (event as CustomEvent<AirJamDevRuntimeEventDetail>).detail;
491
+ if (!detail) {
492
+ return;
493
+ }
494
+
495
+ this.enqueue(this.fromRuntimeEvent(detail));
496
+ };
497
+ window.addEventListener(AIRJAM_DEV_RUNTIME_EVENT, runtimeEventHandler);
498
+ this.disposers.push(() => {
499
+ window.removeEventListener(AIRJAM_DEV_RUNTIME_EVENT, runtimeEventHandler);
500
+ });
501
+
502
+ const pageHideHandler = () => {
503
+ this.emitUnloadEvent(
504
+ {
505
+ occurredAt: new Date().toISOString(),
506
+ level: "info",
507
+ source: "runtime",
508
+ event: AIRJAM_DEV_LOG_EVENTS.runtime.windowPageHide,
509
+ message: "Window pagehide observed",
510
+ },
511
+ { trigger: "pagehide" },
512
+ );
513
+ void this.flush(true);
514
+ };
515
+ window.addEventListener("pagehide", pageHideHandler);
516
+ this.disposers.push(() => {
517
+ window.removeEventListener("pagehide", pageHideHandler);
518
+ });
519
+
520
+ const beforeUnloadHandler = () => {
521
+ this.emitUnloadEvent(
522
+ {
523
+ occurredAt: new Date().toISOString(),
524
+ level: "info",
525
+ source: "runtime",
526
+ event: AIRJAM_DEV_LOG_EVENTS.runtime.windowBeforeUnload,
527
+ message: "Window beforeunload observed",
528
+ },
529
+ { trigger: "beforeunload" },
530
+ );
531
+ void this.flush(true);
532
+ };
533
+ window.addEventListener("beforeunload", beforeUnloadHandler);
534
+ this.disposers.push(() => {
535
+ window.removeEventListener("beforeunload", beforeUnloadHandler);
536
+ });
537
+
538
+ const visibilityChangeHandler = () => {
539
+ if (document.visibilityState !== "hidden") {
540
+ return;
541
+ }
542
+
543
+ this.emitUnloadEvent(
544
+ {
545
+ occurredAt: new Date().toISOString(),
546
+ level: "info",
547
+ source: "runtime",
548
+ event: AIRJAM_DEV_LOG_EVENTS.runtime.windowPageHide,
549
+ message: "Document hidden before unload",
550
+ },
551
+ { trigger: "visibilitychange" },
552
+ );
553
+ void this.flush(true);
554
+ };
555
+ document.addEventListener("visibilitychange", visibilityChangeHandler);
556
+ this.disposers.push(() => {
557
+ document.removeEventListener("visibilitychange", visibilityChangeHandler);
558
+ });
559
+
560
+ this.enqueue({
561
+ occurredAt: new Date().toISOString(),
562
+ level: "info",
563
+ source: "console",
564
+ message: "Browser log sink started",
565
+ consoleCategory: "airjam",
566
+ data: [
567
+ {
568
+ href: window.location.href,
569
+ origin: window.location.origin,
570
+ pathname: window.location.pathname,
571
+ },
572
+ ],
573
+ });
574
+ }
575
+
576
+ dispose(): void {
577
+ if (this.flushTimer) {
578
+ clearTimeout(this.flushTimer);
579
+ this.flushTimer = null;
580
+ }
581
+
582
+ while (this.disposers.length > 0) {
583
+ const dispose = this.disposers.pop();
584
+ dispose?.();
585
+ }
586
+
587
+ this.originalConsole.clear();
588
+ this.installed = false;
589
+ }
590
+
591
+ private fromDiagnostic(diagnostic: AirJamDiagnostic): BrowserLogEntry {
592
+ return {
593
+ occurredAt: new Date(diagnostic.timestamp).toISOString(),
594
+ level: resolveLogLevelFromDiagnostic(diagnostic.severity),
595
+ source: "diagnostic",
596
+ message: diagnostic.message,
597
+ code: diagnostic.code,
598
+ data: diagnostic.details
599
+ ? [toSerializable(diagnostic.details)]
600
+ : undefined,
601
+ };
602
+ }
603
+
604
+ private fromRuntimeEvent(
605
+ detail: AirJamDevRuntimeEventDetail,
606
+ ): BrowserLogEntry {
607
+ return {
608
+ occurredAt: new Date().toISOString(),
609
+ level: detail.level ?? "info",
610
+ source: "runtime",
611
+ event: detail.event,
612
+ message: detail.message,
613
+ code: detail.code,
614
+ role: detail.role,
615
+ traceId: detail.traceId,
616
+ roomId: detail.roomId,
617
+ controllerId: detail.controllerId,
618
+ runtimeEpoch: detail.runtimeEpoch,
619
+ runtimeKind: detail.runtimeKind,
620
+ data: detail.data ? [toSerializable(detail.data)] : undefined,
621
+ };
622
+ }
623
+
624
+ private isSameConsolePayload(
625
+ left: BrowserLogEntry["data"],
626
+ right: BrowserLogEntry["data"],
627
+ ): boolean {
628
+ try {
629
+ return JSON.stringify(left ?? null) === JSON.stringify(right ?? null);
630
+ } catch {
631
+ return false;
632
+ }
633
+ }
634
+
635
+ private shouldCoalesceConsoleEntry(
636
+ previous: BrowserLogEntry | undefined,
637
+ next: BrowserLogEntry,
638
+ ): boolean {
639
+ if (
640
+ !previous ||
641
+ previous.source !== "console" ||
642
+ next.source !== "console"
643
+ ) {
644
+ return false;
645
+ }
646
+
647
+ if (next.level !== "warn" && next.level !== "error") {
648
+ return false;
649
+ }
650
+
651
+ const previousTime = Date.parse(previous.occurredAt);
652
+ const nextTime = Date.parse(next.occurredAt);
653
+ if (
654
+ Number.isFinite(previousTime) &&
655
+ Number.isFinite(nextTime) &&
656
+ nextTime - previousTime > CONSOLE_REPEAT_COALESCE_WINDOW_MS
657
+ ) {
658
+ return false;
659
+ }
660
+
661
+ return (
662
+ previous.level === next.level &&
663
+ previous.message === next.message &&
664
+ previous.consoleCategory === next.consoleCategory &&
665
+ previous.code === next.code &&
666
+ previous.role === next.role &&
667
+ previous.traceId === next.traceId &&
668
+ previous.roomId === next.roomId &&
669
+ previous.controllerId === next.controllerId &&
670
+ this.isSameConsolePayload(previous.data, next.data)
671
+ );
672
+ }
673
+
674
+ private appendOrCoalesce(entry: BrowserLogEntry): void {
675
+ const previous = this.queue.at(-1);
676
+ if (this.shouldCoalesceConsoleEntry(previous, entry) && previous) {
677
+ previous.repeatCount = (previous.repeatCount ?? 1) + 1;
678
+ return;
679
+ }
680
+
681
+ this.queue.push(entry);
682
+ }
683
+
684
+ private enqueue(entry: BrowserLogEntry): void {
685
+ this.appendOrCoalesce(this.normalizeEntry(entry));
686
+
687
+ if (!this.flushTimer) {
688
+ this.flushTimer = setTimeout(() => {
689
+ void this.flush();
690
+ }, FLUSH_INTERVAL_MS);
691
+ }
692
+ }
693
+
694
+ private buildPayload(entries: BrowserLogEntry[]): BrowserLogBatchPayload {
695
+ return {
696
+ mode: this.hasReset ? "append" : "reset",
697
+ sessionId: this.sessionId,
698
+ metadata: this.getMetadata(),
699
+ entries,
700
+ };
701
+ }
702
+
703
+ private serializePayload(payload: unknown): string | null {
704
+ try {
705
+ return JSON.stringify(payload);
706
+ } catch {
707
+ return null;
708
+ }
709
+ }
710
+
711
+ private async postUnloadPayload(body: string): Promise<void> {
712
+ if (!this.unloadEndpoint) {
713
+ return;
714
+ }
715
+
716
+ if (
717
+ typeof navigator !== "undefined" &&
718
+ typeof navigator.sendBeacon === "function"
719
+ ) {
720
+ try {
721
+ if (navigator.sendBeacon(this.unloadEndpoint, body)) {
722
+ return;
723
+ }
724
+ } catch {
725
+ // Fall through to fetch keepalive transport.
726
+ }
727
+ }
728
+
729
+ try {
730
+ const response = await fetch(this.unloadEndpoint, {
731
+ method: "POST",
732
+ headers: {
733
+ "content-type": "text/plain;charset=UTF-8",
734
+ },
735
+ body,
736
+ keepalive: true,
737
+ mode: "cors",
738
+ });
739
+ if (!response.ok) {
740
+ this.reportTransportFailure(
741
+ `Browser unload endpoint returned ${response.status}`,
742
+ { status: response.status, transport: "unload" },
743
+ );
744
+ }
745
+ } catch (error) {
746
+ const reason =
747
+ error instanceof Error
748
+ ? error.message
749
+ : "Unknown browser unload transport failure";
750
+ this.reportTransportFailure(reason, { transport: "unload" });
751
+ }
752
+ }
753
+
754
+ private emitUnloadEvent(
755
+ entry: BrowserLogEntry,
756
+ details?: Record<string, unknown>,
757
+ ): void {
758
+ if (!this.unloadEndpoint) {
759
+ return;
760
+ }
761
+
762
+ const payload: BrowserLogUnloadPayload = {
763
+ sessionId: this.sessionId,
764
+ metadata: this.getMetadata(),
765
+ entry: this.normalizeEntry({
766
+ ...entry,
767
+ data: details ? [toSerializable(details)] : entry.data,
768
+ }),
769
+ };
770
+ const body = this.serializePayload(payload);
771
+ if (!body) {
772
+ return;
773
+ }
774
+
775
+ void this.postUnloadPayload(body);
776
+ }
777
+
778
+ private getMetadata(): BrowserLogBatchPayload["metadata"] {
779
+ if (typeof window === "undefined") {
780
+ const devContext = resolveDevBrowserLogContext();
781
+ return {
782
+ appId: this.appId,
783
+ traceId: devContext.traceId,
784
+ roomId: devContext.roomId,
785
+ controllerId: devContext.controllerId,
786
+ role: devContext.role,
787
+ };
788
+ }
789
+
790
+ const devContext = resolveDevBrowserLogContext();
791
+ return {
792
+ appId: this.appId,
793
+ traceId: devContext.traceId,
794
+ roomId: devContext.roomId,
795
+ controllerId: devContext.controllerId,
796
+ role: devContext.role,
797
+ origin: window.location.origin,
798
+ pathname: window.location.pathname,
799
+ href: window.location.href,
800
+ title: document.title || undefined,
801
+ userAgent: navigator.userAgent,
802
+ };
803
+ }
804
+
805
+ private async flush(useBeacon = false): Promise<void> {
806
+ if (this.flushTimer) {
807
+ clearTimeout(this.flushTimer);
808
+ this.flushTimer = null;
809
+ }
810
+
811
+ if (!this.endpoint || this.queue.length === 0) {
812
+ return;
813
+ }
814
+
815
+ const entries = [...this.queue];
816
+ const payload = this.buildPayload(entries);
817
+ const body = this.serializePayload(payload);
818
+ if (!body) {
819
+ return;
820
+ }
821
+ this.queue.splice(0, this.queue.length);
822
+ this.hasReset = true;
823
+ await this.postPayload(body, useBeacon);
824
+ }
825
+
826
+ private async postPayload(body: string, useBeacon: boolean): Promise<void> {
827
+ if (!this.endpoint) {
828
+ return;
829
+ }
830
+
831
+ if (
832
+ useBeacon &&
833
+ typeof navigator !== "undefined" &&
834
+ typeof navigator.sendBeacon === "function"
835
+ ) {
836
+ try {
837
+ const requestBody =
838
+ typeof Blob === "function"
839
+ ? new Blob([body], { type: "application/json" })
840
+ : body;
841
+ if (navigator.sendBeacon(this.endpoint, requestBody)) {
842
+ return;
843
+ }
844
+ } catch {
845
+ // Fall through to fetch keepalive transport.
846
+ }
847
+ }
848
+
849
+ try {
850
+ const response = await fetch(this.endpoint, {
851
+ method: "POST",
852
+ headers: {
853
+ "content-type": "application/json",
854
+ },
855
+ body,
856
+ keepalive: useBeacon,
857
+ mode: "cors",
858
+ });
859
+ if (!response.ok) {
860
+ this.reportTransportFailure(
861
+ `Browser log sink endpoint returned ${response.status}`,
862
+ { status: response.status },
863
+ );
864
+ }
865
+ } catch (error) {
866
+ const reason =
867
+ error instanceof Error
868
+ ? error.message
869
+ : "Unknown browser log sink transport failure";
870
+ this.reportTransportFailure(reason);
871
+ }
872
+ }
873
+ }
874
+
875
+ export const ensureDevBrowserLogSink = (
876
+ options: BrowserLogSinkOptions,
877
+ ): void => {
878
+ if (typeof window === "undefined") {
879
+ return;
880
+ }
881
+
882
+ const globalState = globalThis as BrowserLogSinkGlobal;
883
+ const autoEnabled =
884
+ options.topology != null
885
+ ? isLocalDevControlSurfaceTopology(options.topology)
886
+ : isDevelopmentRuntime();
887
+
888
+ if (!autoEnabled) {
889
+ globalState.__airJamBrowserLogSink__?.dispose?.();
890
+ delete globalState.__airJamBrowserLogSink__;
891
+ return;
892
+ }
893
+
894
+ if (!globalState.__airJamBrowserLogSink__) {
895
+ const runtime = new BrowserLogSinkRuntime();
896
+ runtime.update(options);
897
+ runtime.install();
898
+ globalState.__airJamBrowserLogSink__ = {
899
+ update: (nextOptions) => runtime.update(nextOptions),
900
+ dispose: () => runtime.dispose(),
901
+ };
902
+ return;
903
+ }
904
+
905
+ globalState.__airJamBrowserLogSink__.update(options);
906
+ };
907
+
908
+ export const primeDevBrowserLogSink = (
909
+ options: BrowserLogSinkOptions = {},
910
+ ): void => {
911
+ ensureDevBrowserLogSink(options);
912
+ };