@air-jam/cli 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.
- package/LICENSE +21 -0
- package/README.md +273 -2
- package/bin/airjam.mjs +66 -0
- package/dist/ai-pack.d.ts +44 -0
- package/dist/ai-pack.js +15 -0
- package/dist/ai-pack.js.map +1 -0
- package/dist/auth-profile.d.ts +5 -0
- package/dist/auth-profile.js +7 -0
- package/dist/auth-profile.js.map +1 -0
- package/dist/auth-sessions.d.ts +5 -0
- package/dist/auth-sessions.js +7 -0
- package/dist/auth-sessions.js.map +1 -0
- package/dist/chunk-7D4AYBAV.js +64 -0
- package/dist/chunk-7D4AYBAV.js.map +1 -0
- package/dist/chunk-J5MAU4QZ.js +102 -0
- package/dist/chunk-J5MAU4QZ.js.map +1 -0
- package/dist/chunk-LH7UO57C.js +23 -0
- package/dist/chunk-LH7UO57C.js.map +1 -0
- package/dist/chunk-N7AI7HZK.js +529 -0
- package/dist/chunk-N7AI7HZK.js.map +1 -0
- package/dist/chunk-PKTLVQXZ.js +82 -0
- package/dist/chunk-PKTLVQXZ.js.map +1 -0
- package/dist/chunk-SQE4NPZP.js +31 -0
- package/dist/chunk-SQE4NPZP.js.map +1 -0
- package/dist/chunk-U6V3HEBB.js +939 -0
- package/dist/chunk-U6V3HEBB.js.map +1 -0
- package/dist/chunk-VEHNFPEO.js +224 -0
- package/dist/chunk-VEHNFPEO.js.map +1 -0
- package/dist/development.d.ts +102 -0
- package/dist/development.js +33 -0
- package/dist/development.js.map +1 -0
- package/dist/documentation.d.ts +27 -0
- package/dist/documentation.js +7 -0
- package/dist/documentation.js.map +1 -0
- package/dist/game-analytics.d.ts +5 -0
- package/dist/game-analytics.js +7 -0
- package/dist/game-analytics.js.map +1 -0
- package/dist/game-app-id.d.ts +5 -0
- package/dist/game-app-id.js +7 -0
- package/dist/game-app-id.js.map +1 -0
- package/dist/game-media.d.ts +5 -0
- package/dist/game-media.js +7 -0
- package/dist/game-media.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2468 -0
- package/dist/index.js.map +1 -0
- package/dist/scaffold.d.ts +6 -0
- package/dist/scaffold.js +20 -0
- package/dist/scaffold.js.map +1 -0
- package/package.json +82 -3
- package/runtime/local-network.d.mts +6 -0
- package/runtime/local-network.mjs +9 -0
- package/runtime/public-assets.mjs +62 -0
- package/runtime/source-fence.mjs +182 -0
- package/runtime/vite-config.d.mts +43 -0
- package/runtime/vite-config.mjs +134 -0
- package/runtime/vite-dev.mjs +18 -0
- package/runtime/vite-https.d.mts +11 -0
- package/runtime/vite-https.mjs +40 -0
- package/template-assets/bootstrap/.claude/launch.json +12 -0
- package/template-assets/bootstrap/AGENTS.md +290 -0
- package/template-assets/bootstrap/CLAUDE.md +55 -0
- package/template-assets/bootstrap/_gitignore +6 -0
- package/template-assets/bootstrap/skills/airjam-docs-workflow/SKILL.md +49 -0
- package/template-assets/bootstrap/skills/airjam-mcp/SKILL.md +100 -0
- package/template-assets/bootstrap/skills/canvas-2d-gameplay/SKILL.md +47 -0
- package/template-assets/bootstrap/skills/controller-ui/SKILL.md +71 -0
- package/template-assets/bootstrap/skills/debug-and-test/SKILL.md +72 -0
- package/template-assets/bootstrap/skills/game-architecture/SKILL.md +102 -0
- package/template-assets/bootstrap/skills/game-state-and-rendering/SKILL.md +44 -0
- package/template-assets/bootstrap/skills/host-surface/SKILL.md +52 -0
- package/template-assets/bootstrap/skills/index.md +36 -0
- package/template-assets/bootstrap/skills/prefab-authoring/SKILL.md +58 -0
- package/template-assets/bootstrap/skills/r3f-3d-scene/SKILL.md +61 -0
- package/template-assets/documentation/for-agents/controller-ui/page.mdx +99 -0
- package/template-assets/documentation/for-agents/page.mdx +120 -0
- package/template-assets/documentation/for-agents/project-structure/page.mdx +170 -0
- package/template-assets/documentation/for-agents/state-and-rendering/page.mdx +77 -0
- package/template-assets/documentation/for-agents/supported-agents/page.mdx +18 -0
- package/template-assets/documentation/getting-started/debugging/page.mdx +164 -0
- package/template-assets/documentation/getting-started/dev-logs/page.mdx +169 -0
- package/template-assets/documentation/getting-started/game-ideas/page.mdx +114 -0
- package/template-assets/documentation/getting-started/introduction/page.mdx +200 -0
- package/template-assets/documentation/getting-started/migrating-legacy-games/page.mdx +673 -0
- package/template-assets/documentation/getting-started/quick-start/page.mdx +310 -0
- package/template-assets/documentation/how-it-works/architecture/page.mdx +230 -0
- package/template-assets/documentation/how-it-works/host-system/page.mdx +270 -0
- package/template-assets/documentation/manifest.json +500 -0
- package/template-assets/documentation/sdk/assets-and-theme/page.mdx +185 -0
- package/template-assets/documentation/sdk/hooks/page.mdx +435 -0
- package/template-assets/documentation/sdk/input-system/page.mdx +240 -0
- package/template-assets/documentation/sdk/networked-state/page.mdx +295 -0
- package/template-assets/documentation/sdk/settings/page.mdx +155 -0
- package/template-assets/documentation/sdk/ui-components/page.mdx +268 -0
- package/template-assets/managed/.airjam/ai-pack.json +209 -0
- package/template-assets/managed/docs/airjam/agent-gold-path.md +115 -0
- package/template-assets/managed/docs/airjam/agent-mcp.md +179 -0
- package/template-assets/managed/docs/airjam/canvas-2d-gameplay.md +82 -0
- package/template-assets/managed/docs/airjam/composition-shell-contract.md +87 -0
- package/template-assets/managed/docs/airjam/debug-and-testing.md +104 -0
- package/template-assets/managed/docs/airjam/development-loop.md +51 -0
- package/template-assets/managed/docs/airjam/docs-index.md +64 -0
- package/template-assets/managed/docs/airjam/generated/architecture.md +223 -0
- package/template-assets/managed/docs/airjam/generated/assets-and-theme.md +188 -0
- package/template-assets/managed/docs/airjam/generated/controller-ui.md +102 -0
- package/template-assets/managed/docs/airjam/generated/debugging-and-logs.md +165 -0
- package/template-assets/managed/docs/airjam/generated/for-agents.md +123 -0
- package/template-assets/managed/docs/airjam/generated/game-ideas.md +107 -0
- package/template-assets/managed/docs/airjam/generated/host-system.md +261 -0
- package/template-assets/managed/docs/airjam/generated/input-system.md +241 -0
- package/template-assets/managed/docs/airjam/generated/introduction.md +195 -0
- package/template-assets/managed/docs/airjam/generated/migrating-legacy-games.md +674 -0
- package/template-assets/managed/docs/airjam/generated/networked-state.md +298 -0
- package/template-assets/managed/docs/airjam/generated/project-structure.md +171 -0
- package/template-assets/managed/docs/airjam/generated/quick-start.md +311 -0
- package/template-assets/managed/docs/airjam/generated/sdk-hooks.md +430 -0
- package/template-assets/managed/docs/airjam/generated/settings.md +158 -0
- package/template-assets/managed/docs/airjam/generated/state-and-rendering.md +80 -0
- package/template-assets/managed/docs/airjam/generated/supported-agents.md +21 -0
- package/template-assets/managed/docs/airjam/generated/ui-components.md +265 -0
- package/template-assets/managed/docs/airjam/generated/unified-dev-logs.md +170 -0
- package/template-assets/managed/docs/airjam/iconography.md +55 -0
- package/template-assets/managed/docs/airjam/prefab-authoring.md +212 -0
- package/template-assets/managed/docs/airjam/preview-controllers.md +129 -0
- package/template-assets/managed/docs/airjam/r3f-3d-scene.md +163 -0
- package/template-assets/managed/docs/airjam/state-lanes-cookbook.md +101 -0
- package/template-assets/managed/docs/airjam/visual-system.md +103 -0
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
<!-- Generated from content/docs/sdk/hooks/page.mdx. Do not edit directly. -->
|
|
2
|
+
<!-- Canonical public doc: https://airjam.io/docs/sdk/hooks -->
|
|
3
|
+
|
|
4
|
+
# SDK Hooks
|
|
5
|
+
|
|
6
|
+
The Air Jam SDK provides React hooks for building multiplayer games. This page documents all available hooks and their usage.
|
|
7
|
+
|
|
8
|
+
### `createAirJamApp`
|
|
9
|
+
|
|
10
|
+
Declare the game once with `defineAirJamGame` from `@air-jam/sdk/core` in
|
|
11
|
+
`src/airjam.config.ts`. In `app.tsx`, `createAirJamApp(game, options)` binds that
|
|
12
|
+
declaration to the browser environment and returns `Host`, `Controller`,
|
|
13
|
+
`paths` and `session`. Its capacity comes from `game.metadata.maxPlayers`.
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { createAirJamApp, env } from "@air-jam/sdk";
|
|
17
|
+
import { airjam as game } from "./airjam.config";
|
|
18
|
+
|
|
19
|
+
export const airjam = createAirJamApp(game, {
|
|
20
|
+
runtime: env.vite(import.meta.env),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
<Routes>
|
|
24
|
+
<Route
|
|
25
|
+
path="/"
|
|
26
|
+
element={
|
|
27
|
+
<airjam.Host>
|
|
28
|
+
<HostView />
|
|
29
|
+
</airjam.Host>
|
|
30
|
+
}
|
|
31
|
+
/>
|
|
32
|
+
<Route
|
|
33
|
+
path={airjam.paths.controller}
|
|
34
|
+
element={
|
|
35
|
+
<airjam.Controller>
|
|
36
|
+
<ControllerView />
|
|
37
|
+
</airjam.Controller>
|
|
38
|
+
}
|
|
39
|
+
/>
|
|
40
|
+
</Routes>;
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
**Environment Variables:**
|
|
44
|
+
|
|
45
|
+
The provider automatically reads from these environment variables if props aren't provided:
|
|
46
|
+
|
|
47
|
+
- `VITE_AIR_JAM_SERVER_URL` / `NEXT_PUBLIC_AIR_JAM_SERVER_URL` - WebSocket server URL
|
|
48
|
+
- `VITE_AIR_JAM_APP_ID` / `NEXT_PUBLIC_AIR_JAM_APP_ID` - Public app ID
|
|
49
|
+
|
|
50
|
+
Session runtimes mount only in the browser: `airjam.Host`, `airjam.Controller`
|
|
51
|
+
and `AirJamHostRuntime` / `AirJamControllerRuntime` from
|
|
52
|
+
`@air-jam/sdk/arcade/runtime` render nothing on the server, so a server-rendered
|
|
53
|
+
page emits nothing inside them until it hydrates.
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
### `useAirJamHost`
|
|
58
|
+
|
|
59
|
+
Consumer hook for host runtime state and actions. Mount `airjam.Host` once at the boundary, then call `useAirJamHost()` anywhere below it.
|
|
60
|
+
|
|
61
|
+
Call `useAirJamHost(selector)` when a component only needs a narrow slice of
|
|
62
|
+
host session state and you want to avoid rerendering on unrelated runtime
|
|
63
|
+
fields.
|
|
64
|
+
Selectors receive state fields only; call `useAirJamHost()` when a component
|
|
65
|
+
also needs runtime controls such as `joinUrl` or `sendSignal`.
|
|
66
|
+
|
|
67
|
+
```tsx
|
|
68
|
+
import { useAirJamHost } from "@air-jam/sdk";
|
|
69
|
+
import { airjam } from "../app";
|
|
70
|
+
|
|
71
|
+
const HostShell = () => (
|
|
72
|
+
<airjam.Host
|
|
73
|
+
roomId="GAME"
|
|
74
|
+
onPlayerJoin={(player) => {
|
|
75
|
+
console.log(`${player.label} joined with color ${player.color}`);
|
|
76
|
+
}}
|
|
77
|
+
onPlayerLeave={(controllerId) => {
|
|
78
|
+
console.log(`Player ${controllerId} left`);
|
|
79
|
+
}}
|
|
80
|
+
>
|
|
81
|
+
<HostView />
|
|
82
|
+
</airjam.Host>
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const HostView = () => {
|
|
86
|
+
const host = useAirJamHost();
|
|
87
|
+
|
|
88
|
+
// Return values
|
|
89
|
+
const {
|
|
90
|
+
roomId, // "ABCD" - The room code
|
|
91
|
+
joinUrl, // Full URL for controllers to join
|
|
92
|
+
joinUrlStatus, // "loading" | "ready" | "unavailable"
|
|
93
|
+
connectionStatus, // "connected" | "connecting" | "reconnecting" | "disconnected" | "idle"
|
|
94
|
+
players, // Array of AirJamPlayer, including phones that are away
|
|
95
|
+
queuePosition, // Place in a busy hosted service's queue, or null
|
|
96
|
+
runtimeState, // "playing" | "paused"
|
|
97
|
+
lastError, // Error message if any
|
|
98
|
+
mode, // "standalone" | "platform"
|
|
99
|
+
|
|
100
|
+
// Functions
|
|
101
|
+
sendSignal, // Send haptics/toasts to controllers
|
|
102
|
+
pauseRuntime, // Set runtimeState to "paused"
|
|
103
|
+
resumeRuntime, // Set runtimeState to "playing"
|
|
104
|
+
setRuntimeState, // Explicitly set "playing" | "paused"
|
|
105
|
+
} = host;
|
|
106
|
+
|
|
107
|
+
return (
|
|
108
|
+
<div className="relative min-h-screen bg-black text-white">
|
|
109
|
+
<header className="absolute top-0 right-0 left-0 z-10 flex items-center justify-between border-b border-white/10 bg-black/70 px-4 py-2 backdrop-blur">
|
|
110
|
+
<span>Room {roomId}</span>
|
|
111
|
+
<button
|
|
112
|
+
onClick={runtimeState === "playing" ? pauseRuntime : resumeRuntime}
|
|
113
|
+
>
|
|
114
|
+
{runtimeState === "playing" ? "Pause" : "Resume"}
|
|
115
|
+
</button>
|
|
116
|
+
</header>
|
|
117
|
+
<GameCanvas />
|
|
118
|
+
</div>
|
|
119
|
+
);
|
|
120
|
+
};
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Each player is a seat the room assigned (`p1`, `p2`, …). A phone that drops
|
|
124
|
+
keeps its seat for two minutes and shows `connected: false` meanwhile;
|
|
125
|
+
reconnecting is automatic.
|
|
126
|
+
|
|
127
|
+
Players' controls reach the game only in its simulation steps, through
|
|
128
|
+
`input(playerId)`; see [Input System](./input-system.md).
|
|
129
|
+
|
|
130
|
+
Pause and orientation are the only presentation the host sends phones
|
|
131
|
+
directly. Authoritative multiplayer state lives in the networked stores; a
|
|
132
|
+
short message for the players is a toast signal or a store field.
|
|
133
|
+
|
|
134
|
+
A game whose `inputModalities` include `'joystick'` always gets a landscape controller; the SDK applies it, so do not set orientation; lay it out with `GamepadLayout` from `@air-jam/sdk/ui`: the stick under the left thumb, the actions under the right in a square the same size, and only short status between them.
|
|
135
|
+
|
|
136
|
+
**Sending Haptic Feedback:**
|
|
137
|
+
|
|
138
|
+
```tsx
|
|
139
|
+
// Vibrate a specific player's phone
|
|
140
|
+
host.sendSignal("HAPTIC", { pattern: "heavy" }, playerId);
|
|
141
|
+
|
|
142
|
+
// Available patterns: "light", "medium", "heavy", "success", "failure", "custom"
|
|
143
|
+
host.sendSignal(
|
|
144
|
+
"HAPTIC",
|
|
145
|
+
{
|
|
146
|
+
pattern: "custom",
|
|
147
|
+
sequence: [50, 100, 50], // Vibrate 50ms, pause 100ms, vibrate 50ms
|
|
148
|
+
},
|
|
149
|
+
playerId,
|
|
150
|
+
);
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
**Sending Toast Notifications:**
|
|
154
|
+
|
|
155
|
+
```tsx
|
|
156
|
+
// Show notification on a player's controller
|
|
157
|
+
host.sendSignal(
|
|
158
|
+
"TOAST",
|
|
159
|
+
{
|
|
160
|
+
message: "Achievement unlocked: First blood",
|
|
161
|
+
color: "#22c55e", // Optional accent color
|
|
162
|
+
duration: 2000, // Optional duration in ms
|
|
163
|
+
},
|
|
164
|
+
playerId,
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
// Broadcast to all players (omit targetId)
|
|
168
|
+
host.sendSignal("TOAST", {
|
|
169
|
+
message: "Round start! Get ready to fight.",
|
|
170
|
+
});
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
### `useSendSignal`
|
|
176
|
+
|
|
177
|
+
Lightweight hook for sending signals without triggering re-renders. Use in collision handlers.
|
|
178
|
+
|
|
179
|
+
```tsx
|
|
180
|
+
import { useSendSignal } from "@air-jam/sdk";
|
|
181
|
+
|
|
182
|
+
const Laser = ({ ownerId }: { ownerId: string }) => {
|
|
183
|
+
const sendSignal = useSendSignal();
|
|
184
|
+
|
|
185
|
+
const handleHit = (targetId: string) => {
|
|
186
|
+
// Vibrate the player who got hit
|
|
187
|
+
sendSignal("HAPTIC", { pattern: "heavy" }, targetId);
|
|
188
|
+
|
|
189
|
+
// Light feedback for the shooter
|
|
190
|
+
sendSignal("HAPTIC", { pattern: "light" }, ownerId);
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
// Collision detection...
|
|
194
|
+
};
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
---
|
|
198
|
+
|
|
199
|
+
### `useAirJamController`
|
|
200
|
+
|
|
201
|
+
Consumer hook for controller runtime state and actions. Mount `airjam.Controller` once at the boundary, then call `useAirJamController()` below it.
|
|
202
|
+
|
|
203
|
+
Call `useAirJamController(selector)` when a component only needs a narrow slice
|
|
204
|
+
of controller session state and should not rerender on unrelated runtime
|
|
205
|
+
updates.
|
|
206
|
+
Selectors receive state fields only; call `useAirJamController()` when a
|
|
207
|
+
component also needs controls such as `sendSystemCommand`.
|
|
208
|
+
|
|
209
|
+
A controller page keeps its seat's key for each room it joins, so a reload or a
|
|
210
|
+
dropped network returns the phone to the same seat.
|
|
211
|
+
|
|
212
|
+
```tsx
|
|
213
|
+
import { useAirJamController } from "@air-jam/sdk";
|
|
214
|
+
import { ControlButton, Stick } from "@air-jam/sdk/ui";
|
|
215
|
+
import { airjam } from "../app";
|
|
216
|
+
import { shipInput } from "../game/contracts/input";
|
|
217
|
+
|
|
218
|
+
const ControllerShell = () => (
|
|
219
|
+
<airjam.Controller roomId="ABCD" nickname="Player1">
|
|
220
|
+
<ControllerView />
|
|
221
|
+
</airjam.Controller>
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
const ControllerView = () => {
|
|
225
|
+
const controller = useAirJamController();
|
|
226
|
+
|
|
227
|
+
const {
|
|
228
|
+
roomId, // Room code (from URL or props)
|
|
229
|
+
controllerId, // This phone's seat ("p1", "p2", …)
|
|
230
|
+
connectionStatus, // Connection state
|
|
231
|
+
runtimeState, // "playing" | "paused"
|
|
232
|
+
controllerOrientation, // Declared session orientation; always landscape for joystick games
|
|
233
|
+
|
|
234
|
+
// Functions
|
|
235
|
+
sendSystemCommand, // "pause" | "resume"
|
|
236
|
+
setNickname, // Update nickname
|
|
237
|
+
setAvatarId, // Update avatar selection draft
|
|
238
|
+
updatePlayerProfile, // Patch live label/avatar when connected
|
|
239
|
+
selfPlayer, // This controller's player profile when known
|
|
240
|
+
leave, // Give up the seat
|
|
241
|
+
input, // This player's declared controls, which phone controls fill
|
|
242
|
+
} = controller;
|
|
243
|
+
|
|
244
|
+
if (connectionStatus === "connecting") {
|
|
245
|
+
return <div>Connecting to room {roomId}...</div>;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (connectionStatus === "reconnecting") {
|
|
249
|
+
return <div>Reconnecting…</div>;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return (
|
|
253
|
+
<div className="flex h-dvh flex-col bg-zinc-950 p-4 text-white">
|
|
254
|
+
<div className="mb-2 text-xs uppercase">Room {roomId}</div>
|
|
255
|
+
<div className="mb-2 text-xs uppercase">
|
|
256
|
+
Layout {controllerOrientation}
|
|
257
|
+
</div>
|
|
258
|
+
<Stick control={shipInput.move} className="size-48 rounded-full" />
|
|
259
|
+
<ControlButton control={shipInput.fire}>Fire</ControlButton>
|
|
260
|
+
</div>
|
|
261
|
+
);
|
|
262
|
+
};
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
Phone controls send the declared controls themselves; a controller runs no
|
|
266
|
+
send loop.
|
|
267
|
+
|
|
268
|
+
### Game simulation
|
|
269
|
+
|
|
270
|
+
A game's authoritative state that changes every step, such as positions,
|
|
271
|
+
timers and physics, lives in a declared simulation. The SDK steps it on the
|
|
272
|
+
host at a fixed rate with seeded randomness, so the same seed and inputs replay
|
|
273
|
+
the same game, in a browser or headless and faster than real time. It only
|
|
274
|
+
steps while the room is playing: pausing freezes the game and its game time.
|
|
275
|
+
|
|
276
|
+
```ts filename="src/game/engine/simulation.ts"
|
|
277
|
+
import { defineAirJamSimulation } from "@air-jam/sdk/core";
|
|
278
|
+
|
|
279
|
+
export const simulation = defineAirJamSimulation<Sim, GameInput, SimEvent>({
|
|
280
|
+
stepMs: 1000 / 60,
|
|
281
|
+
create: ({ random }) => createSim(random),
|
|
282
|
+
step: (sim, { input, players, random, emit }) => {
|
|
283
|
+
advance(sim, players, input);
|
|
284
|
+
},
|
|
285
|
+
// What agents and tests see of the game, as plain JSON.
|
|
286
|
+
observe: (sim) => ({ fighters: sim.fighters }),
|
|
287
|
+
});
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
Declare it with `defineAirJamGame({ simulation })`. When its code is heavy, such
|
|
291
|
+
as a physics engine, declare a loader instead,
|
|
292
|
+
`simulation: () => import("./game/engine/simulation").then((m) => m.simulation)`,
|
|
293
|
+
so only the host downloads it and controllers stay small. The host draws and
|
|
294
|
+
plays feedback from it, never advances it:
|
|
295
|
+
|
|
296
|
+
- `useSimulationFrame(({ sim, alpha }) => draw(sim, alpha))` runs after every
|
|
297
|
+
display frame; `alpha` interpolates between steps.
|
|
298
|
+
- `useSimulationEvents((event) => playSound(event))` receives what `step`
|
|
299
|
+
emitted, such as a hit.
|
|
300
|
+
- `useSimulationSelector((sim) => value, isEqual?)` reads a value for UI, such
|
|
301
|
+
as a health bar or an entity list, re-rendering only when it changes.
|
|
302
|
+
- `useSimulation()` returns the clock itself.
|
|
303
|
+
- `const captureFrame = useHostAgentCapture((target, options) => drawAgentCanvas(canvas, target, options))`
|
|
304
|
+
optionally gives agents a sharper host render surface. Supporting browsers
|
|
305
|
+
already capture the visible host stage, including HTML lobbies and nested canvases;
|
|
306
|
+
this callback takes priority when it returns true. Returning false (for example,
|
|
307
|
+
before a game canvas mounts) uses the native stage instead.
|
|
308
|
+
Call `captureFrame()` immediately after
|
|
309
|
+
drawing the game and its HUD. The SDK owns the persistent capture canvas and
|
|
310
|
+
records native video at 30 fps; still observations use the same drawing callback.
|
|
311
|
+
Still observations and frame-based test clips encode JPEGs asynchronously. The
|
|
312
|
+
clip sampler skips ticks while encoding is pending rather than queueing frames.
|
|
313
|
+
If you call `captureAgentCanvas()` directly, await its image result.
|
|
314
|
+
|
|
315
|
+
Step code reads stores with `gameStore.getState()` and dispatches with
|
|
316
|
+
`gameStore.actions`; it never reads `Date.now()` or `Math.random()`.
|
|
317
|
+
|
|
318
|
+
**Auto Room Join from URL:**
|
|
319
|
+
|
|
320
|
+
Controllers automatically join rooms from URL query parameters:
|
|
321
|
+
|
|
322
|
+
```
|
|
323
|
+
https://yourgame.com/controller?room=ABCD
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
This is how QR code scanning works—the host generates a URL with the room code embedded.
|
|
327
|
+
|
|
328
|
+
---
|
|
329
|
+
|
|
330
|
+
## Advanced (Non-default)
|
|
331
|
+
|
|
332
|
+
Use these only when integrating platform-specific runtime behavior.
|
|
333
|
+
|
|
334
|
+
### Room session access (escape hatch)
|
|
335
|
+
|
|
336
|
+
`host.room` and `controller.session` are the page's sessions in the room
|
|
337
|
+
(`@air-jam/sdk/room-session`). Networked stores and signals cover what games
|
|
338
|
+
need; reach for the session only for a channel of your own.
|
|
339
|
+
|
|
340
|
+
### Internal context hooks
|
|
341
|
+
|
|
342
|
+
`useAirJamContext`, `useAirJamConfig` and `useAirJamState` are intentionally internal and are not part of the root public SDK surface.
|
|
343
|
+
|
|
344
|
+
---
|
|
345
|
+
|
|
346
|
+
## Diagnostics
|
|
347
|
+
|
|
348
|
+
Air Jam emits structured diagnostics for common misuse paths in development builds.
|
|
349
|
+
|
|
350
|
+
```tsx
|
|
351
|
+
import { onAirJamDiagnostic, setAirJamDiagnosticsEnabled } from "@air-jam/sdk";
|
|
352
|
+
|
|
353
|
+
setAirJamDiagnosticsEnabled(true);
|
|
354
|
+
|
|
355
|
+
const unsubscribe = onAirJamDiagnostic((diagnostic) => {
|
|
356
|
+
console.log(diagnostic.code, diagnostic.message, diagnostic.details);
|
|
357
|
+
});
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
### Diagnostics Reference
|
|
361
|
+
|
|
362
|
+
| Code | Meaning | Expected Fix |
|
|
363
|
+
| ------------------------------------------ | --------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- |
|
|
364
|
+
| `AJ_SCOPE_MISMATCH` | A host/controller hook was used in the wrong scoped provider tree. | Use the correct scoped wrapper (`airjam.Host` / `airjam.Controller`) or the matching session provider. |
|
|
365
|
+
| `AJ_MISSING_SESSION_PROVIDER` | SDK context was read outside Air Jam session providers. | Wrap the route/root with `airjam.Host`, `airjam.Controller`, or the matching low-level session provider. |
|
|
366
|
+
| `AJ_CONFIG_MISSING_RUNTIME_TOPOLOGY` | Runtime config cannot resolve the complete topology contract. | Provide `topology` directly or enable environment resolution for a supported runtime mode. |
|
|
367
|
+
| `AJ_CONFIG_MISSING_APP_ID` | Production runtime started without canonical app ID config. | Set `VITE_AIR_JAM_APP_ID` / `NEXT_PUBLIC_AIR_JAM_APP_ID` or pass `appId` explicitly. |
|
|
368
|
+
| `AJ_STORE_ACTION_SESSION_NOT_READY` | Controller action dispatch attempted before role/room was ready. | Wait until room/session is established before dispatching store actions. |
|
|
369
|
+
| `AJ_STORE_ACTION_PAYLOAD_INVALID_SHAPE` | State action payload root was not omitted or a plain object. | Dispatch no payload or one plain object payload only. |
|
|
370
|
+
| `AJ_STORE_ACTION_PAYLOAD_NOT_SERIALIZABLE` | State action payload contained non-serializable values. | Use JSON-safe payload objects only (no functions/symbols/cycles). |
|
|
371
|
+
| `AJ_INPUT_INVALID` | Input from a controller or agent did not match the declared controls. | Send each declared control in its kind's shape; leave out controls not in use. |
|
|
372
|
+
| `AJ_INPUT_NOT_DECLARED` | A phone control rendered where the game declares no controls. | Declare them with `defineAirJamGame({ input })` and render controls inside `<airjam.Controller>`. |
|
|
373
|
+
|
|
374
|
+
---
|
|
375
|
+
|
|
376
|
+
### `AirJamPlayer`
|
|
377
|
+
|
|
378
|
+
```typescript
|
|
379
|
+
interface AirJamPlayer {
|
|
380
|
+
id: string; // The seat the room assigned ("p1", "p2", …)
|
|
381
|
+
label: string; // Display name (e.g., "Player 1")
|
|
382
|
+
color: string; // Assigned color (e.g., "#FF5733")
|
|
383
|
+
avatarId?: string; // Preset avatar id chosen by the player
|
|
384
|
+
kind: "phone" | "preview" | "agent";
|
|
385
|
+
connected: boolean; // false while the phone is away
|
|
386
|
+
}
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
### `ConnectionStatus`
|
|
390
|
+
|
|
391
|
+
```typescript
|
|
392
|
+
type ConnectionStatus =
|
|
393
|
+
| "idle" // Not yet connected
|
|
394
|
+
| "connecting" // Connection in progress
|
|
395
|
+
| "connected" // Successfully connected
|
|
396
|
+
| "disconnected" // Connection lost
|
|
397
|
+
| "reconnecting"; // Attempting to reconnect
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
### `RuntimeState`
|
|
401
|
+
|
|
402
|
+
```typescript
|
|
403
|
+
type RuntimeState = "playing" | "paused";
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
### `HapticSignalPayload`
|
|
407
|
+
|
|
408
|
+
```typescript
|
|
409
|
+
interface HapticSignalPayload {
|
|
410
|
+
pattern: "light" | "medium" | "heavy" | "success" | "failure" | "custom";
|
|
411
|
+
sequence?: number | number[]; // For "custom" pattern
|
|
412
|
+
}
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
### `ToastSignalPayload`
|
|
416
|
+
|
|
417
|
+
```typescript
|
|
418
|
+
interface ToastSignalPayload {
|
|
419
|
+
message: string;
|
|
420
|
+
color?: string; // Optional accent color (e.g., "#22c55e")
|
|
421
|
+
duration?: number; // Optional duration in ms
|
|
422
|
+
}
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
## useAirJamSettings
|
|
426
|
+
|
|
427
|
+
Host rendering reads declared settings with `useAirJamSettings(gameSettings)`.
|
|
428
|
+
The hook returns typed current values and subscribes to accepted host updates.
|
|
429
|
+
Controllers read display values copied to replicated store state. See
|
|
430
|
+
[Game Settings](./settings.md) for the full contract.
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
<!-- Generated from content/docs/sdk/settings/page.mdx. Do not edit directly. -->
|
|
2
|
+
<!-- Canonical public doc: https://airjam.io/docs/sdk/settings -->
|
|
3
|
+
|
|
4
|
+
# Game Settings
|
|
5
|
+
|
|
6
|
+
Declare the numbers, switches and choices a creator tunes while playtesting in
|
|
7
|
+
`airjam.config.ts`. The host owns their values and revision. A live update takes
|
|
8
|
+
effect between simulation steps and is recorded for exact replay. While a
|
|
9
|
+
simulation declared as a loader is still loading, updates are refused, so a
|
|
10
|
+
replay starts from the settings the simulation was created with.
|
|
11
|
+
|
|
12
|
+
## Declare settings in core
|
|
13
|
+
|
|
14
|
+
Keep the declaration in a leaf module so the simulation can import its type
|
|
15
|
+
without importing the game config back again.
|
|
16
|
+
|
|
17
|
+
```ts filename="src/game/contracts/settings.ts"
|
|
18
|
+
import {
|
|
19
|
+
defineAirJamSettings,
|
|
20
|
+
setting,
|
|
21
|
+
type AirJamSettingsValues,
|
|
22
|
+
} from "@air-jam/sdk/core";
|
|
23
|
+
|
|
24
|
+
export const pongSettings = defineAirJamSettings({
|
|
25
|
+
ballSpeed: setting.number({
|
|
26
|
+
default: 360,
|
|
27
|
+
min: 180,
|
|
28
|
+
max: 720,
|
|
29
|
+
step: 10,
|
|
30
|
+
live: true,
|
|
31
|
+
description: "Ball speed in pixels per second.",
|
|
32
|
+
}),
|
|
33
|
+
});
|
|
34
|
+
export type PongSettings = AirJamSettingsValues<typeof pongSettings>;
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Pass it as `defineAirJamGame({ settings: pongSettings, ... })`. Remove the
|
|
38
|
+
module constants it replaces. A lobby choice players make, such as
|
|
39
|
+
`pointsToWin`, remains replicated store state.
|
|
40
|
+
|
|
41
|
+
Keys are lower camel case. `setting.number` requires `default`, `min` and
|
|
42
|
+
`max`, with an optional positive `step`; use `step: 1` for whole numbers.
|
|
43
|
+
Steps are multiples of zero. `setting.boolean` requires a boolean `default`.
|
|
44
|
+
`setting.choice` takes a nonempty string tuple in `options` and a `default`
|
|
45
|
+
from that tuple; its value type is the union of those strings.
|
|
46
|
+
Every kind accepts `description` and `live`, which defaults to false.
|
|
47
|
+
`defineAirJamSettings` validates defaults immediately. The description returned
|
|
48
|
+
by `airjam.inspect_game` includes these descriptors in `declaration.settings`.
|
|
49
|
+
|
|
50
|
+
## Read the current values
|
|
51
|
+
|
|
52
|
+
Use the settings type as the fourth simulation type parameter:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
defineAirJamSimulation<State, Input, Event, PongSettings>({
|
|
56
|
+
stepMs: 1000 / 60,
|
|
57
|
+
create: ({ settings }) => createState(settings.ballSpeed),
|
|
58
|
+
step: (state, { settings, stepMs }) => {
|
|
59
|
+
state.x += (settings.ballSpeed * stepMs) / 1000;
|
|
60
|
+
},
|
|
61
|
+
observe: (state, { settings }) => ({
|
|
62
|
+
x: state.x,
|
|
63
|
+
ballSpeed: settings.ballSpeed,
|
|
64
|
+
}),
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Store actions read the same values through
|
|
69
|
+
`AirJamActionContext<PongSettings>.settings`. Host rendering calls
|
|
70
|
+
`useAirJamSettings(pongSettings)` from `@air-jam/sdk`; the hook subscribes to
|
|
71
|
+
the host runtime and returns the typed values. Controllers have no settings
|
|
72
|
+
hook. Copy any value a phone must display into replicated store state.
|
|
73
|
+
|
|
74
|
+
## Set initial values and tune a live room
|
|
75
|
+
|
|
76
|
+
`HostRuntimeOptions.settings` accepts a partial set of initial values and fills
|
|
77
|
+
the rest from defaults. `airjam.start_game_run` accepts the same `settings`
|
|
78
|
+
object when creating a new room, for browser and headless runs. Initial values
|
|
79
|
+
may include non-live settings. They are fixed for the host's lifetime.
|
|
80
|
+
|
|
81
|
+
Open a game session with host authority, then call:
|
|
82
|
+
|
|
83
|
+
```json
|
|
84
|
+
{
|
|
85
|
+
"tool": "airjam.read_game_settings",
|
|
86
|
+
"arguments": { "gameSessionId": "<session id>" }
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
The result contains `values` and `revision`, initially zero. Update using that
|
|
91
|
+
revision:
|
|
92
|
+
|
|
93
|
+
```json
|
|
94
|
+
{
|
|
95
|
+
"tool": "airjam.update_game_settings",
|
|
96
|
+
"arguments": {
|
|
97
|
+
"gameSessionId": "<session id>",
|
|
98
|
+
"patch": { "ballSpeed": 700 },
|
|
99
|
+
"expectedRevision": 0
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
A successful update returns the complete values and the next revision. The
|
|
105
|
+
whole patch is validated before any value changes. Both tools have the same
|
|
106
|
+
host authority requirements as semantic host actions. The runtime contract is
|
|
107
|
+
`settings-read` with `{ roomId, runtimeId }`, and `settings-update` with those
|
|
108
|
+
fields plus `patch` and `expectedRevision`.
|
|
109
|
+
|
|
110
|
+
The CLI uses the same operations:
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
pnpm exec airjam session settings-read <session-id>
|
|
114
|
+
pnpm exec airjam session settings-update <session-id> --patch '{"ballSpeed":700}' --expected-revision 0
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Both commands print JSON. Use `airjam run start --settings '{"ballSpeed":600}'` with the other required
|
|
118
|
+
run options to start a fresh room at a chosen speed. Attaching a run to an
|
|
119
|
+
existing room does not replace its initial settings.
|
|
120
|
+
|
|
121
|
+
## Errors and replay
|
|
122
|
+
|
|
123
|
+
`AJ_SETTING_UNKNOWN` names an undeclared key. `AJ_SETTING_INVALID` means a
|
|
124
|
+
value violates its type, range, step or choices. `AJ_SETTING_STALE` means
|
|
125
|
+
another update changed the revision; read again before applying a new patch.
|
|
126
|
+
`AJ_SETTING_NOT_LIVE` means the setting requires a new room with that initial
|
|
127
|
+
value. Rejected updates leave all values and the revision unchanged.
|
|
128
|
+
|
|
129
|
+
Input logs use `version: 3`, with resolved initial `settings` and ordered
|
|
130
|
+
`settings` events containing each accepted `patch` and `revision`.
|
|
131
|
+
They share the stream with inputs and actions. `replayAirJamInputLog` creates
|
|
132
|
+
the host with the initial values and applies each update in order before its
|
|
133
|
+
recorded step. Use the same seed and game code to reproduce the final state.
|
|
134
|
+
|
|
135
|
+
## Keeping a value
|
|
136
|
+
|
|
137
|
+
A live change lasts for the current room. To make a chosen value the default for
|
|
138
|
+
future rooms and test runs, keep it in the game's declaration:
|
|
139
|
+
|
|
140
|
+
```bash
|
|
141
|
+
pnpm exec airjam game settings keep --set ballSpeed=400 --json
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Use `--dir <game>` from another directory, and repeat `--set` for several values.
|
|
145
|
+
The command requires the game's own `typescript` dependency and rewrites only
|
|
146
|
+
literal `default:` values in the selected `defineAirJamSettings({...})`, even
|
|
147
|
+
when it is imported from another game module. Imported settings must resolve to
|
|
148
|
+
one `const` directly initialized by `defineAirJamSettings`; named and default
|
|
149
|
+
exports and explicit re-export aliases are resolved to that binding. Mutable or
|
|
150
|
+
ambiguous bindings are refused. The config must export the `defineAirJamGame`
|
|
151
|
+
binding as `airjam`, so an unused game declaration is never selected. Comments
|
|
152
|
+
and formatting stay in
|
|
153
|
+
place; the declaration remains the only source of defaults. Commit the change
|
|
154
|
+
as ordinary game source. Shared or computed defaults, ambiguous declarations,
|
|
155
|
+
unknown keys and values outside their declared type, range, step or options are
|
|
156
|
+
refused before writing. The JSON result names the file and each `{ key, from,
|
|
157
|
+
to }`; equal values write nothing. Studio's Keep operation uses this same
|
|
158
|
+
transform and captures the source change in history.
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
<!-- Generated from content/docs/for-agents/state-and-rendering/page.mdx. Do not edit directly. -->
|
|
2
|
+
<!-- Canonical public doc: https://airjam.io/docs/for-agents/state-and-rendering -->
|
|
3
|
+
|
|
4
|
+
# State and Rendering
|
|
5
|
+
|
|
6
|
+
Air Jam projects often combine React, Zustand, per-frame gameplay logic, and sometimes R3F or Three.
|
|
7
|
+
|
|
8
|
+
That stack works well only when state ownership is explicit.
|
|
9
|
+
|
|
10
|
+
## Core State Rules
|
|
11
|
+
|
|
12
|
+
1. keep authoritative gameplay state separate from local UI state
|
|
13
|
+
2. avoid one mega store for unrelated concerns
|
|
14
|
+
3. use narrow selectors when consuming Zustand state
|
|
15
|
+
4. keep store writes intentional and coarse
|
|
16
|
+
5. keep per-frame mutable values out of React state when React rendering is not needed
|
|
17
|
+
|
|
18
|
+
These are framework-level rules about ownership and performance.
|
|
19
|
+
They do not require one exact store or engine file layout.
|
|
20
|
+
|
|
21
|
+
## React Rules
|
|
22
|
+
|
|
23
|
+
1. do not run simulation through React rerenders
|
|
24
|
+
2. keep business logic out of presentational components
|
|
25
|
+
3. use refs for hot mutable runtime values
|
|
26
|
+
4. keep component boundaries small and purposeful
|
|
27
|
+
|
|
28
|
+
## Starter Template Reference
|
|
29
|
+
|
|
30
|
+
The starter Pong template demonstrates one good starter implementation of the state/rendering split:
|
|
31
|
+
|
|
32
|
+
1. `src/game/stores/pong-store.ts` owns Air Jam store wiring
|
|
33
|
+
2. `src/game/stores/pong-store-state.ts` owns pure state transitions
|
|
34
|
+
3. `src/game/engine/runtime-state.ts` owns per-frame mutable values
|
|
35
|
+
4. `src/game/engine/simulation.ts` owns update ordering and orchestration
|
|
36
|
+
5. `src/game/ui/` owns reusable game-facing presentation that host and controller surfaces can share
|
|
37
|
+
|
|
38
|
+
That split is a recommended starter pattern, not a mandatory framework contract.
|
|
39
|
+
If the game needs a different module layout, preserve the same ownership boundaries rather than copying the filenames literally.
|
|
40
|
+
|
|
41
|
+
## Starter Testing Pattern
|
|
42
|
+
|
|
43
|
+
The starter template also mirrors those boundaries in tests:
|
|
44
|
+
|
|
45
|
+
1. `tests/game/domain/` for pure gameplay rules
|
|
46
|
+
2. `tests/game/stores/` for pure state transitions
|
|
47
|
+
3. `tests/game/engine/` for focused runtime helpers
|
|
48
|
+
4. `tests/game/adapters/` for transport-facing mapping
|
|
49
|
+
5. `tests/game/ui/` for shared game-facing UI modules that do not require full host/controller shells
|
|
50
|
+
|
|
51
|
+
The default order is:
|
|
52
|
+
|
|
53
|
+
1. test pure logic first
|
|
54
|
+
2. test thin adapter boundaries second
|
|
55
|
+
3. add heavier runtime or browser-facing coverage only where the integration boundary actually matters
|
|
56
|
+
|
|
57
|
+
## R3F and Three Rules
|
|
58
|
+
|
|
59
|
+
1. avoid rerendering scene components on every frame
|
|
60
|
+
2. mutate refs in frame loops when visual updates do not require React state
|
|
61
|
+
3. keep rendering integration separate from pure game rules
|
|
62
|
+
4. use instancing when repeated objects justify it
|
|
63
|
+
|
|
64
|
+
## Air Jam Lane Rules
|
|
65
|
+
|
|
66
|
+
Keep the framework lanes separate:
|
|
67
|
+
|
|
68
|
+
1. input lane for high-frequency player control input
|
|
69
|
+
2. replicated state lane for replayable shared truth
|
|
70
|
+
3. signal lane for coarse UX and system events
|
|
71
|
+
|
|
72
|
+
Do not use replicated store actions as a substitute for per-frame input handling.
|
|
73
|
+
|
|
74
|
+
## Common Pitfalls
|
|
75
|
+
|
|
76
|
+
1. putting gameplay logic directly in render components
|
|
77
|
+
2. using React state as the simulation engine
|
|
78
|
+
3. selecting whole stores across many components
|
|
79
|
+
4. mixing debug state into hot runtime paths
|
|
80
|
+
5. solving every state problem with more component state
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<!-- Generated from content/docs/for-agents/supported-agents/page.mdx. Do not edit directly. -->
|
|
2
|
+
<!-- Canonical public doc: https://airjam.io/docs/for-agents/supported-agents -->
|
|
3
|
+
|
|
4
|
+
# Supported Agents
|
|
5
|
+
|
|
6
|
+
Any coding agent that can run terminal commands or start a local stdio MCP
|
|
7
|
+
server can build with Air Jam. Tested means the agent made a game through Air
|
|
8
|
+
Jam's CLI and MCP, played it with two phones and released it.
|
|
9
|
+
|
|
10
|
+
| Agent | Status | Notes |
|
|
11
|
+
| ---------------------------------- | ---------------------- | --------------------------------------- |
|
|
12
|
+
| Codex CLI | Tested | |
|
|
13
|
+
| Claude Code | Tested | |
|
|
14
|
+
| T3 Code | Tested | |
|
|
15
|
+
| Codex app, over SSH | Covered by Codex CLI | Runs the same `codex` program |
|
|
16
|
+
| Claude Desktop Code tab, over SSH | Covered by Claude Code | Runs the same `claude` program |
|
|
17
|
+
| Claude Desktop chat | Not for building | Cannot edit project files |
|
|
18
|
+
| Other terminal or stdio MCP agents | Not tested | Expected to work through the CLI or MCP |
|
|
19
|
+
|
|
20
|
+
Air Jam supports its own CLI and MCP, not the agent's interface. To connect an
|
|
21
|
+
agent, see [For Agents](./for-agents.md).
|