@air-jam/devtools 0.0.0-stage → 1.0.0-beta.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 (256) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +112 -2
  3. package/dist/chunk-2O67AXCU.js +470 -0
  4. package/dist/chunk-2O67AXCU.js.map +1 -0
  5. package/dist/chunk-AO3ZDXF6.js +142 -0
  6. package/dist/chunk-AO3ZDXF6.js.map +1 -0
  7. package/dist/chunk-ART5TEYT.js +472 -0
  8. package/dist/chunk-ART5TEYT.js.map +1 -0
  9. package/dist/chunk-ASC2CK2R.js +77 -0
  10. package/dist/chunk-ASC2CK2R.js.map +1 -0
  11. package/dist/chunk-E3UZZGAV.js +50 -0
  12. package/dist/chunk-E3UZZGAV.js.map +1 -0
  13. package/dist/chunk-EJTE7UKX.js +64 -0
  14. package/dist/chunk-EJTE7UKX.js.map +1 -0
  15. package/dist/chunk-ESFKNN75.js +110 -0
  16. package/dist/chunk-ESFKNN75.js.map +1 -0
  17. package/dist/chunk-EU7A6PXW.js +1339 -0
  18. package/dist/chunk-EU7A6PXW.js.map +1 -0
  19. package/dist/chunk-FN7E6EA7.js +549 -0
  20. package/dist/chunk-FN7E6EA7.js.map +1 -0
  21. package/dist/chunk-GDFAWHGA.js +190 -0
  22. package/dist/chunk-GDFAWHGA.js.map +1 -0
  23. package/dist/chunk-HZUGBH4Y.js +347 -0
  24. package/dist/chunk-HZUGBH4Y.js.map +1 -0
  25. package/dist/chunk-ITKAOEGX.js +93 -0
  26. package/dist/chunk-ITKAOEGX.js.map +1 -0
  27. package/dist/chunk-JBCUTWRI.js +897 -0
  28. package/dist/chunk-JBCUTWRI.js.map +1 -0
  29. package/dist/chunk-JSI5S4WP.js +1174 -0
  30. package/dist/chunk-JSI5S4WP.js.map +1 -0
  31. package/dist/chunk-L4D675BG.js +97 -0
  32. package/dist/chunk-L4D675BG.js.map +1 -0
  33. package/dist/chunk-M6CJ7GQC.js +133 -0
  34. package/dist/chunk-M6CJ7GQC.js.map +1 -0
  35. package/dist/chunk-MEIMOSZC.js +749 -0
  36. package/dist/chunk-MEIMOSZC.js.map +1 -0
  37. package/dist/chunk-O3XG3FNL.js +1014 -0
  38. package/dist/chunk-O3XG3FNL.js.map +1 -0
  39. package/dist/chunk-OM6SCJ7H.js +18 -0
  40. package/dist/chunk-OM6SCJ7H.js.map +1 -0
  41. package/dist/chunk-PKD4WVX5.js +63 -0
  42. package/dist/chunk-PKD4WVX5.js.map +1 -0
  43. package/dist/chunk-PMBLXGFM.js +3384 -0
  44. package/dist/chunk-PMBLXGFM.js.map +1 -0
  45. package/dist/chunk-PMNE5GIN.js +279 -0
  46. package/dist/chunk-PMNE5GIN.js.map +1 -0
  47. package/dist/chunk-QAWI64VR.js +148 -0
  48. package/dist/chunk-QAWI64VR.js.map +1 -0
  49. package/dist/chunk-QGADSPHZ.js +449 -0
  50. package/dist/chunk-QGADSPHZ.js.map +1 -0
  51. package/dist/chunk-SPYN66UV.js +338 -0
  52. package/dist/chunk-SPYN66UV.js.map +1 -0
  53. package/dist/context.d.ts +19 -0
  54. package/dist/context.js +10 -0
  55. package/dist/context.js.map +1 -0
  56. package/dist/controller.d.ts +67 -0
  57. package/dist/controller.js +40 -0
  58. package/dist/controller.js.map +1 -0
  59. package/dist/dependencies.d.ts +44 -0
  60. package/dist/dependencies.js +10 -0
  61. package/dist/dependencies.js.map +1 -0
  62. package/dist/dev.d.ts +21 -0
  63. package/dist/dev.js +19 -0
  64. package/dist/dev.js.map +1 -0
  65. package/dist/game-builds.d.ts +200 -0
  66. package/dist/game-builds.js +16 -0
  67. package/dist/game-builds.js.map +1 -0
  68. package/dist/game-run-watch-CEd_SpAT.d.ts +84 -0
  69. package/dist/game-runs.d.ts +1452 -0
  70. package/dist/game-runs.js +76 -0
  71. package/dist/game-runs.js.map +1 -0
  72. package/dist/game-session-broker-CUvORfMd.d.ts +439 -0
  73. package/dist/game-session-broker.d.ts +4 -0
  74. package/dist/game-session-broker.js +40 -0
  75. package/dist/game-session-broker.js.map +1 -0
  76. package/dist/game-session.d.ts +3934 -0
  77. package/dist/game-session.js +195 -0
  78. package/dist/game-session.js.map +1 -0
  79. package/dist/game-settings.d.ts +16 -0
  80. package/dist/game-settings.js +237 -0
  81. package/dist/game-settings.js.map +1 -0
  82. package/dist/games.d.ts +28 -0
  83. package/dist/games.js +16 -0
  84. package/dist/games.js.map +1 -0
  85. package/dist/harness/visual.d.ts +204 -0
  86. package/dist/harness/visual.js +46 -0
  87. package/dist/harness/visual.js.map +1 -0
  88. package/dist/logs.d.ts +15 -0
  89. package/dist/logs.js +57 -0
  90. package/dist/logs.js.map +1 -0
  91. package/dist/managed-process-scope-BicYi_t9.d.ts +3 -0
  92. package/dist/mcp-config.d.ts +63 -0
  93. package/dist/mcp-config.js +191 -0
  94. package/dist/mcp-config.js.map +1 -0
  95. package/dist/platform-auth.d.ts +9 -0
  96. package/dist/platform-auth.js +45 -0
  97. package/dist/platform-auth.js.map +1 -0
  98. package/dist/platform-game-analytics.d.ts +51 -0
  99. package/dist/platform-game-analytics.js +49 -0
  100. package/dist/platform-game-analytics.js.map +1 -0
  101. package/dist/platform-game-app-id.d.ts +46 -0
  102. package/dist/platform-game-app-id.js +93 -0
  103. package/dist/platform-game-app-id.js.map +1 -0
  104. package/dist/platform-game-media.d.ts +347 -0
  105. package/dist/platform-game-media.js +168 -0
  106. package/dist/platform-game-media.js.map +1 -0
  107. package/dist/platform-games.d.ts +87 -0
  108. package/dist/platform-games.js +183 -0
  109. package/dist/platform-games.js.map +1 -0
  110. package/dist/quality.d.ts +16 -0
  111. package/dist/quality.js +62 -0
  112. package/dist/quality.js.map +1 -0
  113. package/dist/release.d.ts +1304 -0
  114. package/dist/release.js +34 -0
  115. package/dist/release.js.map +1 -0
  116. package/dist/rooms-check.d.ts +90 -0
  117. package/dist/rooms-check.js +717 -0
  118. package/dist/rooms-check.js.map +1 -0
  119. package/dist/source-tree-contract.d.ts +16 -0
  120. package/dist/source-tree-contract.js +13 -0
  121. package/dist/source-tree-contract.js.map +1 -0
  122. package/dist/testing.d.ts +464 -0
  123. package/dist/testing.js +27 -0
  124. package/dist/testing.js.map +1 -0
  125. package/dist/tooling/describe-game.d.ts +2 -0
  126. package/dist/tooling/describe-game.js +54 -0
  127. package/dist/tooling/describe-game.js.map +1 -0
  128. package/dist/tooling/game-session-broker.d.ts +2 -0
  129. package/dist/tooling/game-session-broker.js +28 -0
  130. package/dist/tooling/game-session-broker.js.map +1 -0
  131. package/dist/tooling/hold-runtime-host.d.ts +2 -0
  132. package/dist/tooling/hold-runtime-host.js +931 -0
  133. package/dist/tooling/hold-runtime-host.js.map +1 -0
  134. package/dist/tooling/list-visual-scenarios.d.ts +2 -0
  135. package/dist/tooling/list-visual-scenarios.js +25 -0
  136. package/dist/tooling/list-visual-scenarios.js.map +1 -0
  137. package/dist/tooling/managed-dev-supervisor.d.ts +2 -0
  138. package/dist/tooling/managed-dev-supervisor.js +51 -0
  139. package/dist/tooling/managed-dev-supervisor.js.map +1 -0
  140. package/dist/tooling/process-scope-bootstrap.mjs +24 -0
  141. package/dist/tooling/process-scope-guard.d.ts +2 -0
  142. package/dist/tooling/process-scope-guard.js +388 -0
  143. package/dist/tooling/process-scope-guard.js.map +1 -0
  144. package/dist/tooling/process-scope-windows.ps1 +148 -0
  145. package/dist/tooling/rooms-check-device.d.ts +2 -0
  146. package/dist/tooling/rooms-check-device.js +498 -0
  147. package/dist/tooling/rooms-check-device.js.map +1 -0
  148. package/dist/tooling/run-headless-room.d.ts +2 -0
  149. package/dist/tooling/run-headless-room.js +287 -0
  150. package/dist/tooling/run-headless-room.js.map +1 -0
  151. package/dist/tooling/run-test-profile.d.ts +2 -0
  152. package/dist/tooling/run-test-profile.js +181 -0
  153. package/dist/tooling/run-test-profile.js.map +1 -0
  154. package/dist/tooling/run-visual-capture.d.ts +2 -0
  155. package/dist/tooling/run-visual-capture.js +203 -0
  156. package/dist/tooling/run-visual-capture.js.map +1 -0
  157. package/dist/tooling/seal-game-build.d.ts +2 -0
  158. package/dist/tooling/seal-game-build.js +120 -0
  159. package/dist/tooling/seal-game-build.js.map +1 -0
  160. package/dist/types-CdYrgrG_.d.ts +1036 -0
  161. package/dist/types-DyRD-ym1.d.ts +141 -0
  162. package/dist/visual.d.ts +21 -0
  163. package/dist/visual.js +219 -0
  164. package/dist/visual.js.map +1 -0
  165. package/package.json +150 -3
  166. package/src/bounded-response.ts +28 -0
  167. package/src/built-game-runtime.ts +152 -0
  168. package/src/commands.ts +40 -0
  169. package/src/context.ts +218 -0
  170. package/src/controller-protocol.ts +22 -0
  171. package/src/controller.ts +1526 -0
  172. package/src/current-room-host.ts +166 -0
  173. package/src/decision-provider.ts +102 -0
  174. package/src/dependencies.ts +176 -0
  175. package/src/dependency-install-contract.ts +12 -0
  176. package/src/dev.ts +995 -0
  177. package/src/fs-utils.ts +93 -0
  178. package/src/game-action-observation.ts +106 -0
  179. package/src/game-build-protocol.ts +72 -0
  180. package/src/game-build.ts +483 -0
  181. package/src/game-run-owner.ts +2358 -0
  182. package/src/game-run-protocol.ts +448 -0
  183. package/src/game-run-record.ts +92 -0
  184. package/src/game-run-replay.ts +95 -0
  185. package/src/game-run-watch.ts +155 -0
  186. package/src/game-session-broker.ts +793 -0
  187. package/src/game-session-operations.ts +61 -0
  188. package/src/game-session-protocol.ts +180 -0
  189. package/src/game-session-runtime.ts +861 -0
  190. package/src/game-session.ts +206 -0
  191. package/src/game-settings.ts +295 -0
  192. package/src/games.ts +262 -0
  193. package/src/harness/visual/browser.ts +100 -0
  194. package/src/harness/visual/controller-observation.ts +181 -0
  195. package/src/harness/visual/prefab-contract.ts +29 -0
  196. package/src/harness/visual/prefab-runner.ts +178 -0
  197. package/src/harness/visual/runner.ts +549 -0
  198. package/src/harness/visual/runtime-controller-observations.ts +151 -0
  199. package/src/harness/visual/scenario-contract.ts +8 -0
  200. package/src/harness/visual/scenario-helpers.ts +67 -0
  201. package/src/harness/visual/screencast.ts +155 -0
  202. package/src/harness/visual/session.ts +400 -0
  203. package/src/harness/visual/types.ts +184 -0
  204. package/src/harness/visual.ts +7 -0
  205. package/src/headless-room-protocol.ts +112 -0
  206. package/src/headless-room.ts +222 -0
  207. package/src/helper-channel.ts +229 -0
  208. package/src/helper-scripts.ts +69 -0
  209. package/src/installed-dependencies.ts +74 -0
  210. package/src/logs.ts +67 -0
  211. package/src/managed-process-scope.ts +481 -0
  212. package/src/mcp-config.ts +262 -0
  213. package/src/node-process-environment.ts +10 -0
  214. package/src/platform-auth.ts +594 -0
  215. package/src/platform-game-analytics.ts +50 -0
  216. package/src/platform-game-app-id.ts +99 -0
  217. package/src/platform-game-media.ts +209 -0
  218. package/src/platform-games.ts +217 -0
  219. package/src/policy-artifact-contract.ts +52 -0
  220. package/src/policy-artifacts.ts +134 -0
  221. package/src/process-scope-admission.ts +50 -0
  222. package/src/process-scope-protocol.ts +66 -0
  223. package/src/process-scope.ts +327 -0
  224. package/src/quality.ts +82 -0
  225. package/src/release.ts +1683 -0
  226. package/src/rooms-check-fleet.ts +194 -0
  227. package/src/rooms-check-link.ts +138 -0
  228. package/src/rooms-check-protocol.ts +244 -0
  229. package/src/rooms-check.ts +797 -0
  230. package/src/runtime-owner-protocol.ts +218 -0
  231. package/src/source-tree-contract.ts +70 -0
  232. package/src/source-tree.ts +84 -0
  233. package/src/test-profile-process.ts +153 -0
  234. package/src/test-profile-protocol.ts +116 -0
  235. package/src/testing.ts +430 -0
  236. package/src/tooling/declaration-hook.ts +52 -0
  237. package/src/tooling/describe-game.ts +49 -0
  238. package/src/tooling/game-session-broker.ts +7 -0
  239. package/src/tooling/hold-runtime-host.ts +441 -0
  240. package/src/tooling/list-visual-scenarios.ts +21 -0
  241. package/src/tooling/load-game.ts +79 -0
  242. package/src/tooling/managed-dev-supervisor.ts +71 -0
  243. package/src/tooling/process-scope-bootstrap.mjs +24 -0
  244. package/src/tooling/process-scope-guard.ts +213 -0
  245. package/src/tooling/process-scope-windows.ps1 +148 -0
  246. package/src/tooling/rooms-check-device.ts +493 -0
  247. package/src/tooling/run-headless-room.ts +326 -0
  248. package/src/tooling/run-test-profile.ts +190 -0
  249. package/src/tooling/run-visual-capture.ts +231 -0
  250. package/src/tooling/runtime-owner-actions.ts +65 -0
  251. package/src/tooling/seal-game-build.ts +118 -0
  252. package/src/tooling/visual-pack.ts +11 -0
  253. package/src/types.ts +928 -0
  254. package/src/visual.ts +296 -0
  255. package/src/windows-process-scopes.ts +189 -0
  256. package/src/workspace-contract.ts +32 -0
@@ -0,0 +1,206 @@
1
+ import type { z } from "zod";
2
+ import { inspectProject } from "./context.js";
3
+ import type { AirJamGameRunRecord } from "./game-run-owner.js";
4
+ import type {
5
+ CurrentRoomAttachment,
6
+ GameRunSteerOptions,
7
+ StartGameRunOptions,
8
+ } from "./game-run-protocol.js";
9
+ import {
10
+ gameRunAddressSchema,
11
+ gameRunCancellationSchema,
12
+ gameRunDiscoverySchema,
13
+ gameRunWaitSchema,
14
+ } from "./game-run-protocol.js";
15
+ import { readTerminalGameRun } from "./game-run-record.js";
16
+ import { gameRunWatchRequestSchema } from "./game-run-watch.js";
17
+ import {
18
+ ensureGameSessionBroker,
19
+ readGameSessionBrokerState,
20
+ requestGameSessionBroker,
21
+ watchGameSessionBroker,
22
+ } from "./game-session-broker.js";
23
+ import {
24
+ gameSessionBrokerRequestSchema,
25
+ gameSessionOperationSchemas,
26
+ } from "./game-session-protocol.js";
27
+ import type { createGameSessionRuntime } from "./game-session-runtime.js";
28
+ import type { AirJamTestProfileDescription } from "./testing.js";
29
+ import type {
30
+ AirJamGameSessionInspection,
31
+ AirJamGameSessionSummary,
32
+ AirJamGameSessionVisualCaptureResult,
33
+ CaptureGameSessionVisualsOptions,
34
+ CloseGameSessionOptions,
35
+ CloseGameSessionResult,
36
+ InvokeGameSessionActionOptions,
37
+ InvokeGameSessionActionResult,
38
+ OpenGameSessionOptions,
39
+ ReadGameSessionOptions,
40
+ SendGameSessionInputOptions,
41
+ SendGameSessionInputResult,
42
+ } from "./types.js";
43
+
44
+ export type {
45
+ AirJamGameSessionActionDescriptor,
46
+ AirJamGameSessionInspection,
47
+ AirJamGameSessionSummary,
48
+ AirJamGameSessionVisualCaptureResult,
49
+ CaptureGameSessionVisualsOptions,
50
+ CloseGameSessionOptions,
51
+ CloseGameSessionResult,
52
+ InvokeGameSessionActionOptions,
53
+ InvokeGameSessionActionResult,
54
+ OpenGameSessionOptions,
55
+ ReadGameSessionOptions,
56
+ SendGameSessionInputOptions,
57
+ SendGameSessionInputResult,
58
+ } from "./types.js";
59
+ export { gameSessionOperationSchemas };
60
+ export type {
61
+ AirJamGameRunRecord,
62
+ CurrentRoomAttachment,
63
+ GameRunSteerOptions,
64
+ StartGameRunOptions,
65
+ };
66
+ export type ControlGameSessionParticipantOptions = Parameters<
67
+ ReturnType<typeof createGameSessionRuntime>["controlGameSessionParticipant"]
68
+ >[0];
69
+ export type ObserveGameSessionOptions = Parameters<
70
+ ReturnType<typeof createGameSessionRuntime>["observeGameSession"]
71
+ >[0];
72
+
73
+ async function request<TResult>(
74
+ operation: string,
75
+ input: unknown,
76
+ cwd?: string,
77
+ attachment?: CurrentRoomAttachment,
78
+ ): Promise<TResult> {
79
+ const parsed = gameSessionBrokerRequestSchema.parse({
80
+ operation,
81
+ input,
82
+ ...(attachment ? { attachment } : {}),
83
+ });
84
+ const projectDir = (await inspectProject({ cwd })).context.rootDir;
85
+ if (parsed.operation === "run.read" || parsed.operation === "run.wait") {
86
+ const { runId } = parsed.input;
87
+ const record = await readTerminalGameRun(projectDir, runId);
88
+ if (record) return record as TResult;
89
+ }
90
+ const state =
91
+ operation === "open" ||
92
+ operation === "run.start" ||
93
+ operation === "run.cancel" ||
94
+ operation === "run.profiles"
95
+ ? await ensureGameSessionBroker(projectDir)
96
+ : await readGameSessionBrokerState(projectDir);
97
+ if (!state && operation === "run.stop-all") return [] as TResult;
98
+ if (!state)
99
+ throw new Error(
100
+ `owner_unavailable: No game session owner is running for ${projectDir}. Sessions are scoped to their project; pass cwd when addressing another project.`,
101
+ );
102
+ return requestGameSessionBroker<TResult>({ state, request: parsed });
103
+ }
104
+
105
+ export const openGameSession = (input: OpenGameSessionOptions = {}) =>
106
+ request<AirJamGameSessionSummary>("open", input, input.cwd);
107
+ export const readGameSession = (input: ReadGameSessionOptions) =>
108
+ request<AirJamGameSessionInspection>("read", input, input.cwd);
109
+ export const observeGameSession = (input: ObserveGameSessionOptions) =>
110
+ request<
111
+ Awaited<
112
+ ReturnType<
113
+ ReturnType<typeof createGameSessionRuntime>["observeGameSession"]
114
+ >
115
+ >
116
+ >("observe", input, input.cwd);
117
+ export const readGameSettings = (
118
+ input: z.infer<(typeof gameSessionOperationSchemas)["settings-read"]>,
119
+ ) =>
120
+ request<
121
+ Awaited<
122
+ ReturnType<
123
+ ReturnType<typeof createGameSessionRuntime>["readGameSettings"]
124
+ >
125
+ >
126
+ >("settings-read", input, input.cwd);
127
+ export const updateGameSettings = (
128
+ input: z.infer<(typeof gameSessionOperationSchemas)["settings-update"]>,
129
+ ) =>
130
+ request<
131
+ Awaited<
132
+ ReturnType<
133
+ ReturnType<typeof createGameSessionRuntime>["updateGameSettings"]
134
+ >
135
+ >
136
+ >("settings-update", input, input.cwd);
137
+ export const controlGameSessionParticipant = (
138
+ input: ControlGameSessionParticipantOptions,
139
+ ) =>
140
+ request<
141
+ Awaited<
142
+ ReturnType<
143
+ ReturnType<
144
+ typeof createGameSessionRuntime
145
+ >["controlGameSessionParticipant"]
146
+ >
147
+ >
148
+ >("control", input, input.cwd);
149
+ export const sendGameSessionInput = (input: SendGameSessionInputOptions) =>
150
+ request<SendGameSessionInputResult>("input", input, input.cwd);
151
+ export const invokeGameSessionAction = (
152
+ input: InvokeGameSessionActionOptions,
153
+ ) => request<InvokeGameSessionActionResult>("invoke", input, input.cwd);
154
+ export const captureGameSessionVisuals = (
155
+ input: CaptureGameSessionVisualsOptions,
156
+ ) => request<AirJamGameSessionVisualCaptureResult>("capture", input, input.cwd);
157
+ export const closeGameSession = (input: CloseGameSessionOptions) =>
158
+ request<CloseGameSessionResult>("close", input, input.cwd);
159
+
160
+ export const startGameRun = (
161
+ input: StartGameRunOptions,
162
+ attachment?: CurrentRoomAttachment,
163
+ ) => request<AirJamGameRunRecord>("run.start", input, input.cwd, attachment);
164
+ export const readGameRun = (input: z.infer<typeof gameRunAddressSchema>) =>
165
+ request<AirJamGameRunRecord>("run.read", input, input.cwd);
166
+ export const waitGameRun = (input: z.input<typeof gameRunWaitSchema>) =>
167
+ request<AirJamGameRunRecord>("run.wait", input, input.cwd);
168
+ export const stopGameRun = (input: z.infer<typeof gameRunAddressSchema>) =>
169
+ request<AirJamGameRunRecord>("run.stop", input, input.cwd);
170
+ export const steerGameRun = (input: GameRunSteerOptions) =>
171
+ request<AirJamGameRunRecord>("run.steer", input, input.cwd);
172
+ export const cancelGameRun = (input: z.infer<typeof gameRunAddressSchema>) =>
173
+ request<z.infer<typeof gameRunCancellationSchema>>(
174
+ "run.cancel",
175
+ input,
176
+ input.cwd,
177
+ );
178
+ export { replayGameRun } from "./game-run-replay.js";
179
+ export const listGameRuns = (input: { cwd?: string } = {}) =>
180
+ request<AirJamGameRunRecord[]>("run.list", input, input.cwd);
181
+ export const stopAllGameRuns = (input: { cwd?: string } = {}) =>
182
+ request<AirJamGameRunRecord[]>("run.stop-all", input, input.cwd);
183
+ export const listGameTestProfiles = (
184
+ input: z.infer<typeof gameRunDiscoverySchema> = {},
185
+ ) =>
186
+ request<{ gameId: string; profiles: AirJamTestProfileDescription[] }>(
187
+ "run.profiles",
188
+ input,
189
+ input.cwd,
190
+ );
191
+
192
+ export async function watchGameRun(
193
+ input: unknown,
194
+ onEvent: Parameters<typeof watchGameSessionBroker>[0]["onEvent"],
195
+ signal: AbortSignal,
196
+ ) {
197
+ const parsed = gameRunWatchRequestSchema.parse(input);
198
+ const projectDir = (await inspectProject({ cwd: parsed.cwd })).context
199
+ .rootDir;
200
+ const state = await readGameSessionBrokerState(projectDir);
201
+ if (!state)
202
+ throw new Error(
203
+ "owner_unavailable: No game session owner is running for this project.",
204
+ );
205
+ await watchGameSessionBroker({ state, input: parsed, onEvent, signal });
206
+ }
@@ -0,0 +1,295 @@
1
+ import { airJamSettingSchema, defineAirJamSettings } from "@air-jam/sdk/core";
2
+ import { randomUUID } from "node:crypto";
3
+ import { realpathSync } from "node:fs";
4
+ import {
5
+ readFile,
6
+ realpath,
7
+ rename,
8
+ rm,
9
+ stat,
10
+ writeFile,
11
+ } from "node:fs/promises";
12
+ import { createRequire } from "node:module";
13
+ import path from "node:path";
14
+ import type * as TS from "typescript";
15
+ import { inspectGame } from "./games.js";
16
+
17
+ export type SettingValue = number | boolean | string;
18
+ export type KeptGameSettings = {
19
+ file: string;
20
+ kept: { key: string; from: SettingValue; to: SettingValue }[];
21
+ };
22
+
23
+ function refuse(code: string, message: string): never {
24
+ throw Object.assign(new Error(`${code}: ${message}`), { code });
25
+ }
26
+
27
+ /** Changes only literal defaults in the declaration selected by the game's config. */
28
+ export async function keepGameSettings({
29
+ cwd = process.cwd(),
30
+ values,
31
+ }: {
32
+ cwd?: string;
33
+ values: Record<string, SettingValue>;
34
+ }): Promise<KeptGameSettings> {
35
+ cwd = await realpath(cwd);
36
+ let ts: typeof TS;
37
+ try {
38
+ ts = createRequire(path.join(cwd, "package.json"))(
39
+ "typescript",
40
+ ) as typeof TS;
41
+ } catch {
42
+ refuse(
43
+ "AJ_SETTINGS_TYPESCRIPT_MISSING",
44
+ "Install typescript in this game's dependencies before keeping settings.",
45
+ );
46
+ }
47
+ const fail: (message: string) => never = (message) =>
48
+ refuse("AJ_SETTINGS_DECLARATION", message);
49
+ // Bind only game source. SDK/package declarations and ambient libraries are
50
+ // irrelevant to resolving which local variable the game actually imports.
51
+ const sourceRoot = path.join(cwd, "src");
52
+ const localFile = (file: string) => {
53
+ try {
54
+ const resolved = realpathSync(file);
55
+ const relative = path.relative(cwd, resolved);
56
+ return relative !== ".." &&
57
+ !relative.startsWith(`..${path.sep}`) &&
58
+ !path.isAbsolute(relative) &&
59
+ !relative.split(path.sep).includes("node_modules")
60
+ ? resolved
61
+ : undefined;
62
+ } catch {
63
+ return undefined;
64
+ }
65
+ };
66
+ const options: TS.CompilerOptions = {
67
+ noLib: true,
68
+ types: [],
69
+ skipLibCheck: true,
70
+ target: ts.ScriptTarget.Latest,
71
+ module: ts.ModuleKind.ESNext,
72
+ moduleResolution: ts.ModuleResolutionKind.Bundler,
73
+ allowImportingTsExtensions: true,
74
+ };
75
+ const host = ts.createCompilerHost(options);
76
+ host.getCurrentDirectory = () => cwd;
77
+ host.fileExists = (file) => localFile(file) !== undefined;
78
+ host.readFile = (file) => {
79
+ const local = localFile(file);
80
+ return local ? ts.sys.readFile(local) : undefined;
81
+ };
82
+ host.getSourceFile = (file, languageVersion) => {
83
+ const local = localFile(file);
84
+ const text = local ? host.readFile(local) : undefined;
85
+ return local && text !== undefined
86
+ ? ts.createSourceFile(local, text, languageVersion, true)
87
+ : undefined;
88
+ };
89
+ host.resolveModuleNames = (names, containingFile) =>
90
+ names.map((name) =>
91
+ name.startsWith(".")
92
+ ? ts.resolveModuleName(name, containingFile, options, host)
93
+ .resolvedModule
94
+ : undefined,
95
+ );
96
+ const configPath = path.join(sourceRoot, "airjam.config.ts");
97
+ const program = ts.createProgram([configPath], options, host);
98
+ const config = program.getSourceFile(configPath);
99
+ if (!config) fail("The game's src/airjam.config.ts could not be read.");
100
+ if (program.getSyntacticDiagnostics().length)
101
+ fail("Fix the game's source syntax before keeping settings.");
102
+ const checker = program.getTypeChecker();
103
+ const resolveVariable = (initial: TS.Symbol | undefined) => {
104
+ let symbol = initial;
105
+ const aliases = new Set<TS.Symbol>();
106
+ while (symbol && symbol.flags & ts.SymbolFlags.Alias) {
107
+ if (aliases.has(symbol))
108
+ fail("The settings binding has a circular alias.");
109
+ aliases.add(symbol);
110
+ symbol = checker.getAliasedSymbol(symbol);
111
+ }
112
+ const declarations = symbol?.getDeclarations();
113
+ if (
114
+ declarations?.length !== 1 ||
115
+ !ts.isVariableDeclaration(declarations[0])
116
+ )
117
+ fail("Expected one unambiguous source variable declaration.");
118
+ const variable = declarations[0];
119
+ if (
120
+ !ts.isIdentifier(variable.name) ||
121
+ !ts.isVariableDeclarationList(variable.parent) ||
122
+ !(variable.parent.flags & ts.NodeFlags.Const) ||
123
+ !variable.initializer
124
+ )
125
+ fail("Settings must use one directly initialized const binding.");
126
+ return {
127
+ source: variable.getSourceFile(),
128
+ initializer: variable.initializer,
129
+ };
130
+ };
131
+ const name = (node: TS.PropertyName) => {
132
+ if (ts.isIdentifier(node) || ts.isStringLiteral(node)) return node.text;
133
+ return fail(
134
+ "Computed properties cannot be kept; use explicit property names.",
135
+ );
136
+ };
137
+ const properties = (node: TS.Expression | undefined) => {
138
+ if (!node || !ts.isObjectLiteralExpression(node))
139
+ return fail("Use one explicit object literal for the declaration.");
140
+ const result = new Map<string, TS.PropertyAssignment>();
141
+ for (const property of node.properties) {
142
+ if (!ts.isPropertyAssignment(property))
143
+ fail(
144
+ "Spread and shorthand properties cannot be kept; use explicit properties.",
145
+ );
146
+ const key = name(property.name);
147
+ if (result.has(key))
148
+ fail(`Duplicate property ${key} makes the declaration ambiguous.`);
149
+ result.set(key, property);
150
+ }
151
+ return result;
152
+ };
153
+ const isCall = (node: TS.Node, called: string): node is TS.CallExpression =>
154
+ ts.isCallExpression(node) &&
155
+ ts.isIdentifier(node.expression) &&
156
+ node.expression.text === called;
157
+ const games: TS.CallExpression[] = [];
158
+ const visit = (node: TS.Node) => {
159
+ if (isCall(node, "defineAirJamGame")) games.push(node);
160
+ ts.forEachChild(node, visit);
161
+ };
162
+ visit(config);
163
+ if (games.length !== 1)
164
+ fail("Expected exactly one defineAirJamGame declaration.");
165
+ const module = checker.getSymbolAtLocation(config);
166
+ const exportedGame =
167
+ module &&
168
+ checker.getExportsOfModule(module).find((entry) => entry.name === "airjam");
169
+ if (resolveVariable(exportedGame).initializer !== games[0])
170
+ fail(
171
+ "The defineAirJamGame declaration must be the game's exported airjam value.",
172
+ );
173
+ // A checker may still pick a symbol from invalid duplicate exports. Never
174
+ // treat that recovery choice as an authoritative source-edit target.
175
+ if (
176
+ program
177
+ .getSemanticDiagnostics()
178
+ .some(({ code }) => [2300, 2308, 2323, 2451, 2484, 2588].includes(code))
179
+ )
180
+ fail(
181
+ "Duplicate or ambiguous bindings must be resolved before keeping settings.",
182
+ );
183
+ const selected = properties(games[0].arguments[0]).get(
184
+ "settings",
185
+ )?.initializer;
186
+ if (!selected) fail("This game has no settings declaration.");
187
+ let source = config;
188
+ let declaration = selected;
189
+ if (ts.isIdentifier(selected)) {
190
+ const binding = resolveVariable(checker.getSymbolAtLocation(selected));
191
+ source = binding.source;
192
+ declaration = binding.initializer;
193
+ }
194
+ if (
195
+ !isCall(declaration, "defineAirJamSettings") ||
196
+ declaration.arguments.length !== 1
197
+ )
198
+ fail("Settings must be bound directly to defineAirJamSettings({...}).");
199
+ const settings = properties(declaration.arguments[0]);
200
+ const edits: { start: number; end: number; text: string }[] = [];
201
+ const kept: KeptGameSettings["kept"] = [];
202
+ for (const [key, to] of Object.entries(values)) {
203
+ const entry = settings.get(key);
204
+ if (!entry)
205
+ refuse("AJ_SETTING_UNKNOWN", `${key} is not declared by this game.`);
206
+ const call = entry.initializer;
207
+ if (
208
+ !ts.isCallExpression(call) ||
209
+ !ts.isPropertyAccessExpression(call.expression) ||
210
+ !ts.isIdentifier(call.expression.expression) ||
211
+ call.expression.expression.text !== "setting" ||
212
+ !["number", "boolean", "choice"].includes(call.expression.name.text) ||
213
+ call.arguments.length !== 1
214
+ )
215
+ fail(
216
+ `${key} must use setting.number, setting.boolean or setting.choice directly.`,
217
+ );
218
+ const literal = properties(call.arguments[0]).get("default")?.initializer;
219
+ if (!literal) fail(`${key} needs an explicit default literal.`);
220
+ let from: SettingValue;
221
+ if (ts.isNumericLiteral(literal)) from = Number(literal.text);
222
+ else if (
223
+ ts.isPrefixUnaryExpression(literal) &&
224
+ literal.operator === ts.SyntaxKind.MinusToken &&
225
+ ts.isNumericLiteral(literal.operand)
226
+ )
227
+ from = -Number(literal.operand.text);
228
+ else if (ts.isStringLiteral(literal)) from = literal.text;
229
+ else if (
230
+ literal.kind === ts.SyntaxKind.TrueKeyword ||
231
+ literal.kind === ts.SyntaxKind.FalseKeyword
232
+ )
233
+ from = literal.kind === ts.SyntaxKind.TrueKeyword;
234
+ else
235
+ fail(
236
+ `${key} needs a number, boolean or string default literal; shared or computed defaults cannot be kept.`,
237
+ );
238
+ kept.push({ key, from, to });
239
+ if (from !== to) {
240
+ let text = JSON.stringify(to);
241
+ if (
242
+ typeof to === "string" &&
243
+ source.text[literal.getStart(source)] === "'"
244
+ )
245
+ text = `'${to.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/\r/g, "\\r").replace(/\n/g, "\\n")}'`;
246
+ edits.push({ start: literal.getStart(source), end: literal.end, text });
247
+ }
248
+ }
249
+ // Use the same evaluated descriptors as inspection and the SDK's authoritative validation.
250
+ const inspected = await inspectGame({ cwd });
251
+ const descriptors = Object.fromEntries(
252
+ Object.entries(inspected.declaration.settings ?? {}).map(([key, value]) => [
253
+ key,
254
+ airJamSettingSchema.parse(value),
255
+ ]),
256
+ );
257
+ for (const [key, value] of Object.entries(values)) {
258
+ if (!Object.hasOwn(descriptors, key))
259
+ refuse("AJ_SETTING_UNKNOWN", `${key} is not declared by this game.`);
260
+ try {
261
+ defineAirJamSettings({
262
+ [key]: { ...descriptors[key], default: value },
263
+ } as Parameters<typeof defineAirJamSettings>[0]);
264
+ } catch {
265
+ refuse(
266
+ "AJ_SETTING_INVALID",
267
+ `${key} must match its declared type, range and step or options.`,
268
+ );
269
+ }
270
+ }
271
+ if (edits.length) {
272
+ let output = source.text;
273
+ for (const edit of edits.sort((a, b) => b.start - a.start))
274
+ output = output.slice(0, edit.start) + edit.text + output.slice(edit.end);
275
+ if ((await readFile(source.fileName, "utf8")) !== source.text)
276
+ refuse(
277
+ "AJ_SETTINGS_STALE",
278
+ "The game changed while keeping settings; read again.",
279
+ );
280
+ const temporary = `${source.fileName}.${randomUUID()}.tmp`;
281
+ try {
282
+ await writeFile(temporary, output, {
283
+ flag: "wx",
284
+ mode: (await stat(source.fileName)).mode,
285
+ });
286
+ await rename(temporary, source.fileName);
287
+ } finally {
288
+ await rm(temporary, { force: true });
289
+ }
290
+ }
291
+ return {
292
+ file: path.relative(cwd, source.fileName).split(path.sep).join("/"),
293
+ kept,
294
+ };
295
+ }