@voicethere/agent 0.36.1 → 0.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -11
- package/dist/templates/game-sync/agent.js +256 -52
- package/dist/templates/registry.d.ts.map +1 -1
- package/dist/templates/registry.js +8 -3
- package/dist/templates/registry.js.map +1 -1
- package/dist/templates/world-sync/agent.js +22 -1
- package/dist/templates/world-sync-binary/agent.js +22 -1
- package/eject/llm/loop.ts +1 -1
- package/package.json +30 -1
- package/templates/README.md +11 -11
- package/templates/game-sync/README.md +13 -4
- package/templates/game-sync/agent.ts +175 -60
- package/templates/game-sync/protocol.ts +3 -0
- package/templates/game-sync/quantize.ts +53 -0
- package/templates/game-sync/redis.ts +51 -1
- package/templates/game-sync/sim.ts +6 -0
- package/templates/game-sync/world-layout.ts +191 -0
- package/templates/redis-sync/README.md +7 -5
- package/templates/redis-sync/agent.ts +77 -65
- package/templates/redis-sync/quantize.ts +53 -0
- package/templates/redis-sync/quantized-frames.vectors.json +249 -0
- package/templates/redis-sync/world-layout.ts +278 -116
- package/templates/world-sync/README.md +1 -1
- package/templates/world-sync/agent.ts +39 -4
- package/templates/world-sync-binary/README.md +1 -1
- package/templates/world-sync-binary/agent.ts +29 -2
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
{
|
|
2
|
+
"description": "Encoded sync frames and their expected decode. Platform imports this file from the bumped @voicethere/agent package (export ./templates/redis-sync/quantized-frames.vectors.json) and does not copy it; the agent and platform tests decode every frame to the listed slots. Regenerate only with the agent codec.",
|
|
3
|
+
"source": "agent/templates/redis-sync/quantized-frames.vectors.json",
|
|
4
|
+
"board": {
|
|
5
|
+
"min": -1024,
|
|
6
|
+
"max": 1024
|
|
7
|
+
},
|
|
8
|
+
"redisSync": {
|
|
9
|
+
"frameHeader": "u8 kind (1 keyframe, 2 delta) u8 version (1) u16le slotCount; slot = u8 clientIndex u8 active i16le x i16le y",
|
|
10
|
+
"frames": [
|
|
11
|
+
{
|
|
12
|
+
"name": "keyframe-empty",
|
|
13
|
+
"hex": "01010000",
|
|
14
|
+
"kind": "keyframe",
|
|
15
|
+
"version": 1,
|
|
16
|
+
"slots": []
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"name": "keyframe-three-slots",
|
|
20
|
+
"hex": "0101030000010000000003019001a0ff7f01ff7f0180",
|
|
21
|
+
"kind": "keyframe",
|
|
22
|
+
"version": 1,
|
|
23
|
+
"slots": [
|
|
24
|
+
{
|
|
25
|
+
"clientIndex": 0,
|
|
26
|
+
"active": 1,
|
|
27
|
+
"qx": 0,
|
|
28
|
+
"qy": 0
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"clientIndex": 3,
|
|
32
|
+
"active": 1,
|
|
33
|
+
"qx": 400,
|
|
34
|
+
"qy": -96
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
"clientIndex": 127,
|
|
38
|
+
"active": 1,
|
|
39
|
+
"qx": 32767,
|
|
40
|
+
"qy": -32767
|
|
41
|
+
}
|
|
42
|
+
]
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"name": "delta-all-new",
|
|
46
|
+
"hex": "0201030000010000000003019001a0ff7f01ff7f0180",
|
|
47
|
+
"kind": "delta",
|
|
48
|
+
"version": 1,
|
|
49
|
+
"slots": [
|
|
50
|
+
{
|
|
51
|
+
"clientIndex": 0,
|
|
52
|
+
"active": 1,
|
|
53
|
+
"qx": 0,
|
|
54
|
+
"qy": 0
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
"clientIndex": 3,
|
|
58
|
+
"active": 1,
|
|
59
|
+
"qx": 400,
|
|
60
|
+
"qy": -96
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
"clientIndex": 127,
|
|
64
|
+
"active": 1,
|
|
65
|
+
"qx": 32767,
|
|
66
|
+
"qy": -32767
|
|
67
|
+
}
|
|
68
|
+
]
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
"name": "delta-one-mover",
|
|
72
|
+
"hex": "020101000301880c7f57",
|
|
73
|
+
"kind": "delta",
|
|
74
|
+
"version": 1,
|
|
75
|
+
"slots": [
|
|
76
|
+
{
|
|
77
|
+
"clientIndex": 3,
|
|
78
|
+
"active": 1,
|
|
79
|
+
"qx": 3208,
|
|
80
|
+
"qy": 22399
|
|
81
|
+
}
|
|
82
|
+
]
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
"name": "delta-leave",
|
|
86
|
+
"hex": "02010100000000000000",
|
|
87
|
+
"kind": "delta",
|
|
88
|
+
"version": 1,
|
|
89
|
+
"slots": [
|
|
90
|
+
{
|
|
91
|
+
"clientIndex": 0,
|
|
92
|
+
"active": 0,
|
|
93
|
+
"qx": 0,
|
|
94
|
+
"qy": 0
|
|
95
|
+
}
|
|
96
|
+
]
|
|
97
|
+
}
|
|
98
|
+
]
|
|
99
|
+
},
|
|
100
|
+
"gameSync": {
|
|
101
|
+
"frameHeader": "u8 format (2 quantized) u8 version (1) u16le objectCount; record = u16le objectId, 8 x i16le [posX posY posZ posW dirX dirY dirZ dirW]; live objects only (18 B each)",
|
|
102
|
+
"bounds": {
|
|
103
|
+
"x": [0, 1280],
|
|
104
|
+
"y": [0, 720],
|
|
105
|
+
"velocity": [-1024, 1024],
|
|
106
|
+
"unit": [-1, 1]
|
|
107
|
+
},
|
|
108
|
+
"quantizer": "x, y, dirX, dirY: quantize(v, min, max) as in `quantizer`; posZ, posW, dirZ, dirW: round(clamp(v,-1,1)*32767)",
|
|
109
|
+
"frames": [
|
|
110
|
+
{
|
|
111
|
+
"name": "empty",
|
|
112
|
+
"hex": "02010000",
|
|
113
|
+
"input": [],
|
|
114
|
+
"objects": []
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
"name": "one-object",
|
|
118
|
+
"hex": "020101000100000000000000ff7f800cc0f900000000",
|
|
119
|
+
"input": [
|
|
120
|
+
{
|
|
121
|
+
"objectId": 1,
|
|
122
|
+
"slot": 0,
|
|
123
|
+
"pose": [640, 360, 0, 1, 100, -50, 0, 0]
|
|
124
|
+
}
|
|
125
|
+
],
|
|
126
|
+
"objects": [
|
|
127
|
+
{
|
|
128
|
+
"objectId": 1,
|
|
129
|
+
"q": [0, 0, 0, 32767, 3200, -1600, 0, 0]
|
|
130
|
+
}
|
|
131
|
+
]
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
"name": "two-objects-bounds",
|
|
135
|
+
"hex": "0201020002000180ff7f0000ff7fff7f0180000000001900ff7f01800000ff7f80e9801600000000",
|
|
136
|
+
"input": [
|
|
137
|
+
{
|
|
138
|
+
"objectId": 2,
|
|
139
|
+
"slot": 1,
|
|
140
|
+
"pose": [0, 720, 0, 1, 1024, -1024, 0, 0]
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
"objectId": 25,
|
|
144
|
+
"slot": 24,
|
|
145
|
+
"pose": [1280, 0, 0, 1, -180, 180, 0, 0]
|
|
146
|
+
}
|
|
147
|
+
],
|
|
148
|
+
"objects": [
|
|
149
|
+
{
|
|
150
|
+
"objectId": 2,
|
|
151
|
+
"q": [-32767, 32767, 0, 32767, 32767, -32767, 0, 0]
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
"objectId": 25,
|
|
155
|
+
"q": [32767, -32767, 0, 32767, -5760, 5760, 0, 0]
|
|
156
|
+
}
|
|
157
|
+
]
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
"name": "clamped-out-of-range",
|
|
161
|
+
"hex": "0201010001000180ff7f0000ff7fff7f018000000000",
|
|
162
|
+
"input": [
|
|
163
|
+
{
|
|
164
|
+
"objectId": 1,
|
|
165
|
+
"slot": 0,
|
|
166
|
+
"pose": [-5, 9999, 0, 1, 9999, -9999, 0, 0]
|
|
167
|
+
}
|
|
168
|
+
],
|
|
169
|
+
"objects": [
|
|
170
|
+
{
|
|
171
|
+
"objectId": 1,
|
|
172
|
+
"q": [-32767, 32767, 0, 32767, 32767, -32767, 0, 0]
|
|
173
|
+
}
|
|
174
|
+
]
|
|
175
|
+
}
|
|
176
|
+
]
|
|
177
|
+
},
|
|
178
|
+
"quantizer": {
|
|
179
|
+
"range": "quantize(v, min, max): clamp, round((v-min)/(max-min)*65534)-32767; unit: round(clamp(v,-1,1)*32767)",
|
|
180
|
+
"board": [
|
|
181
|
+
{
|
|
182
|
+
"value": -1024,
|
|
183
|
+
"q": -32767
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
"value": -512,
|
|
187
|
+
"q": -16383
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
"value": -0.015,
|
|
191
|
+
"q": 0
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
"value": 0,
|
|
195
|
+
"q": 0
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
"value": 0.015,
|
|
199
|
+
"q": 0
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
"value": 12.5,
|
|
203
|
+
"q": 400
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
"value": 700,
|
|
207
|
+
"q": 22399
|
|
208
|
+
},
|
|
209
|
+
{
|
|
210
|
+
"value": 1024,
|
|
211
|
+
"q": 32767
|
|
212
|
+
},
|
|
213
|
+
{
|
|
214
|
+
"value": 5000,
|
|
215
|
+
"q": 32767
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
"value": -5000,
|
|
219
|
+
"q": -32767
|
|
220
|
+
}
|
|
221
|
+
],
|
|
222
|
+
"unit": [
|
|
223
|
+
{
|
|
224
|
+
"value": -1,
|
|
225
|
+
"q": -32767
|
|
226
|
+
},
|
|
227
|
+
{
|
|
228
|
+
"value": -0.5,
|
|
229
|
+
"q": -16383
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
"value": 0,
|
|
233
|
+
"q": 0
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
"value": 0.25,
|
|
237
|
+
"q": 8192
|
|
238
|
+
},
|
|
239
|
+
{
|
|
240
|
+
"value": 1,
|
|
241
|
+
"q": 32767
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
"value": 2,
|
|
245
|
+
"q": 32767
|
|
246
|
+
}
|
|
247
|
+
]
|
|
248
|
+
}
|
|
249
|
+
}
|
|
@@ -1,54 +1,47 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Shared world
|
|
2
|
+
* Shared world layout and wire codec for the redis-sync template (agent + E2E helpers).
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* World blob (Redis value and the pod's in-memory world), 6 bytes per slot, slot index =
|
|
5
|
+
* clientIndex:
|
|
6
|
+
* u8 clientIndex · u8 active · i16le x · i16le y (x, y quantized, see BOARD_MIN/MAX)
|
|
7
|
+
* 128 slots = 768 B, so one position update is `SETRANGE key clientIndex*6 <6 bytes>`.
|
|
7
8
|
*
|
|
8
|
-
*
|
|
9
|
+
* Wire frame (agent -> client on voicethere-sync):
|
|
10
|
+
* u8 kind (1 keyframe, 2 delta) · u8 version (1) · u16le slotCount · slotCount x 6 B slots
|
|
11
|
+
* A keyframe carries every active slot and replaces the client's world. A delta carries only
|
|
12
|
+
* slots that changed since the pod's last broadcast; a slot that left goes out once with
|
|
13
|
+
* active = 0. Client -> agent poses stay float32le [clientIndex, x, y] (12 B).
|
|
14
|
+
*
|
|
15
|
+
* The matching test vectors are `quantized-frames.vectors.json` (copied into the platform tests).
|
|
9
16
|
*/
|
|
17
|
+
import { dequantize, isQuantizable, quantize } from "./quantize.js";
|
|
18
|
+
|
|
10
19
|
export const MAX_PEERS = 128;
|
|
11
|
-
export const
|
|
12
|
-
export const
|
|
13
|
-
export const WORLD_BYTE_LENGTH = WORLD_FLOAT_COUNT * 4;
|
|
14
|
-
export const PEER_SLOT_BYTE_LENGTH = PEER_STRIDE * 4;
|
|
20
|
+
export const SLOT_BYTES = 6;
|
|
21
|
+
export const WORLD_BYTE_LENGTH = MAX_PEERS * SLOT_BYTES;
|
|
15
22
|
|
|
16
|
-
|
|
17
|
-
export const
|
|
18
|
-
export const
|
|
19
|
-
export const PEER_FIELD_ACTIVE = 3;
|
|
23
|
+
/** Board extent: x and y are clamped to [BOARD_MIN, BOARD_MAX]. Step is 2048 / 65534 (about 0.031). */
|
|
24
|
+
export const BOARD_MIN = -1024;
|
|
25
|
+
export const BOARD_MAX = 1024;
|
|
20
26
|
|
|
21
|
-
|
|
27
|
+
export const FRAME_KIND_KEYFRAME = 1;
|
|
28
|
+
export const FRAME_KIND_DELTA = 2;
|
|
29
|
+
export const FRAME_VERSION = 1;
|
|
30
|
+
export const FRAME_HEADER_BYTES = 4;
|
|
31
|
+
export const MAX_FRAME_BYTES = FRAME_HEADER_BYTES + WORLD_BYTE_LENGTH;
|
|
32
|
+
|
|
33
|
+
/** Client -> agent position frame: float32le [clientIndex, x, y] (12 bytes). */
|
|
22
34
|
export const POSITION_FLOAT_COUNT = 3;
|
|
23
35
|
export const POSITION_BYTE_LENGTH = POSITION_FLOAT_COUNT * 4;
|
|
24
36
|
|
|
37
|
+
export const REDIS_WORLD_KEY = "e2e:redis-sync:world:v2";
|
|
38
|
+
|
|
25
39
|
export type PeerPosition = {
|
|
26
40
|
clientIndex: number;
|
|
27
41
|
x: number;
|
|
28
42
|
y: number;
|
|
29
43
|
};
|
|
30
44
|
|
|
31
|
-
export const REDIS_WORLD_KEY = "e2e:redis-sync:world";
|
|
32
|
-
|
|
33
|
-
/** Atomic splice of one peer slot (16 bytes) into the world blob. */
|
|
34
|
-
export const LUA_PATCH_PEER_SLOT = `
|
|
35
|
-
local key = KEYS[1]
|
|
36
|
-
local offset = tonumber(ARGV[1])
|
|
37
|
-
local slot = ARGV[2]
|
|
38
|
-
local size = tonumber(ARGV[3])
|
|
39
|
-
local world = redis.call('GET', key)
|
|
40
|
-
if not world then
|
|
41
|
-
world = string.rep(string.char(0), size)
|
|
42
|
-
elseif #world < size then
|
|
43
|
-
world = world .. string.rep(string.char(0), size - #world)
|
|
44
|
-
elseif #world > size then
|
|
45
|
-
world = string.sub(world, 1, size)
|
|
46
|
-
end
|
|
47
|
-
world = string.sub(world, 1, offset) .. slot .. string.sub(world, offset + #slot + 1)
|
|
48
|
-
redis.call('SET', key, world)
|
|
49
|
-
return size
|
|
50
|
-
`;
|
|
51
|
-
|
|
52
45
|
export type PeerSlot = {
|
|
53
46
|
clientIndex: number;
|
|
54
47
|
x: number;
|
|
@@ -56,32 +49,269 @@ export type PeerSlot = {
|
|
|
56
49
|
active: number;
|
|
57
50
|
};
|
|
58
51
|
|
|
59
|
-
export
|
|
60
|
-
|
|
52
|
+
export type FrameKind = "keyframe" | "delta";
|
|
53
|
+
|
|
54
|
+
export type DecodedSlot = {
|
|
55
|
+
clientIndex: number;
|
|
56
|
+
active: number;
|
|
57
|
+
/** Raw quantized components. */
|
|
58
|
+
qx: number;
|
|
59
|
+
qy: number;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export type DecodedFrame = {
|
|
63
|
+
kind: FrameKind;
|
|
64
|
+
version: number;
|
|
65
|
+
slots: DecodedSlot[];
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/** Thrown for a frame with an unknown kind or version byte, or a bad length. Never guessed. */
|
|
69
|
+
export class SyncFrameError extends Error {
|
|
70
|
+
constructor(message: string) {
|
|
71
|
+
super(message);
|
|
72
|
+
this.name = "SyncFrameError";
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function slotOffset(clientIndex: number): number {
|
|
77
|
+
return clientIndex * SLOT_BYTES;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function createEmptyWorldBuffer(): Uint8Array {
|
|
81
|
+
return new Uint8Array(WORLD_BYTE_LENGTH);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function isValidPosition(
|
|
85
|
+
clientIndex: number,
|
|
86
|
+
x: number,
|
|
87
|
+
y: number,
|
|
88
|
+
): boolean {
|
|
89
|
+
return (
|
|
90
|
+
Number.isInteger(clientIndex) &&
|
|
91
|
+
clientIndex >= 0 &&
|
|
92
|
+
clientIndex < MAX_PEERS &&
|
|
93
|
+
isQuantizable(x) &&
|
|
94
|
+
isQuantizable(y)
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Write one slot of raw quantized values at `offset` of `dest`. */
|
|
99
|
+
export function writeSlotBytes(
|
|
100
|
+
dest: Uint8Array,
|
|
101
|
+
offset: number,
|
|
102
|
+
clientIndex: number,
|
|
103
|
+
active: number,
|
|
104
|
+
qx: number,
|
|
105
|
+
qy: number,
|
|
106
|
+
): void {
|
|
107
|
+
dest[offset] = clientIndex;
|
|
108
|
+
dest[offset + 1] = active;
|
|
109
|
+
dest[offset + 2] = qx & 0xff;
|
|
110
|
+
dest[offset + 3] = (qx >> 8) & 0xff;
|
|
111
|
+
dest[offset + 4] = qy & 0xff;
|
|
112
|
+
dest[offset + 5] = (qy >> 8) & 0xff;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function readI16(src: Uint8Array, offset: number): number {
|
|
116
|
+
const v = src[offset]! | (src[offset + 1]! << 8);
|
|
117
|
+
return v >= 0x8000 ? v - 0x10000 : v;
|
|
61
118
|
}
|
|
62
119
|
|
|
63
|
-
|
|
64
|
-
|
|
120
|
+
/** Quantize a pose into the 6-byte slot at the start of `dest` (the SETRANGE payload). */
|
|
121
|
+
export function encodeSlotInto(
|
|
122
|
+
dest: Uint8Array,
|
|
123
|
+
clientIndex: number,
|
|
124
|
+
x: number,
|
|
125
|
+
y: number,
|
|
126
|
+
active: number,
|
|
127
|
+
): Uint8Array {
|
|
128
|
+
writeSlotBytes(
|
|
129
|
+
dest,
|
|
130
|
+
0,
|
|
131
|
+
clientIndex,
|
|
132
|
+
active,
|
|
133
|
+
active ? quantize(x, BOARD_MIN, BOARD_MAX) : 0,
|
|
134
|
+
active ? quantize(y, BOARD_MIN, BOARD_MAX) : 0,
|
|
135
|
+
);
|
|
136
|
+
return dest;
|
|
65
137
|
}
|
|
66
138
|
|
|
139
|
+
/** Write a quantized pose into a world blob in place (memory mode). */
|
|
67
140
|
export function writePeerSlot(
|
|
68
|
-
world:
|
|
141
|
+
world: Uint8Array,
|
|
69
142
|
clientIndex: number,
|
|
70
143
|
x: number,
|
|
71
144
|
y: number,
|
|
72
145
|
active: number,
|
|
73
146
|
): void {
|
|
74
|
-
const offset =
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
147
|
+
const offset = slotOffset(clientIndex);
|
|
148
|
+
writeSlotBytes(
|
|
149
|
+
world,
|
|
150
|
+
offset,
|
|
151
|
+
clientIndex,
|
|
152
|
+
active,
|
|
153
|
+
active ? quantize(x, BOARD_MIN, BOARD_MAX) : 0,
|
|
154
|
+
active ? quantize(y, BOARD_MIN, BOARD_MAX) : 0,
|
|
155
|
+
);
|
|
79
156
|
}
|
|
80
157
|
|
|
81
|
-
export function clearPeerSlot(world:
|
|
158
|
+
export function clearPeerSlot(world: Uint8Array, clientIndex: number): void {
|
|
82
159
|
writePeerSlot(world, clientIndex, 0, 0, 0);
|
|
83
160
|
}
|
|
84
161
|
|
|
162
|
+
export function readPeerSlot(
|
|
163
|
+
world: Uint8Array,
|
|
164
|
+
clientIndex: number,
|
|
165
|
+
): PeerSlot | null {
|
|
166
|
+
const offset = slotOffset(clientIndex);
|
|
167
|
+
if (world[offset + 1] !== 1) {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
clientIndex: world[offset]!,
|
|
172
|
+
x: dequantize(readI16(world, offset + 2), BOARD_MIN, BOARD_MAX),
|
|
173
|
+
y: dequantize(readI16(world, offset + 4), BOARD_MIN, BOARD_MAX),
|
|
174
|
+
active: 1,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Copy Redis bytes into `dest` in place, zero-padding a short blob and ignoring a long one. */
|
|
179
|
+
export function normalizeWorldBuffer(
|
|
180
|
+
raw: Uint8Array | null | undefined,
|
|
181
|
+
dest: Uint8Array,
|
|
182
|
+
): Uint8Array {
|
|
183
|
+
if (!raw || raw.byteLength === 0) {
|
|
184
|
+
dest.fill(0);
|
|
185
|
+
return dest;
|
|
186
|
+
}
|
|
187
|
+
const byteCount = Math.min(raw.byteLength, dest.byteLength);
|
|
188
|
+
dest.set(raw.subarray(0, byteCount));
|
|
189
|
+
if (byteCount < dest.byteLength) {
|
|
190
|
+
dest.fill(0, byteCount);
|
|
191
|
+
}
|
|
192
|
+
return dest;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function writeHeader(dest: Uint8Array, kind: number, slotCount: number): void {
|
|
196
|
+
dest[0] = kind;
|
|
197
|
+
dest[1] = FRAME_VERSION;
|
|
198
|
+
dest[2] = slotCount & 0xff;
|
|
199
|
+
dest[3] = (slotCount >> 8) & 0xff;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Keyframe of every active slot of `world` into `dest`; returns the frame length. */
|
|
203
|
+
export function encodeKeyframeInto(
|
|
204
|
+
dest: Uint8Array,
|
|
205
|
+
world: Uint8Array,
|
|
206
|
+
): number {
|
|
207
|
+
let count = 0;
|
|
208
|
+
let at = FRAME_HEADER_BYTES;
|
|
209
|
+
for (let i = 0; i < MAX_PEERS; i += 1) {
|
|
210
|
+
const o = i * SLOT_BYTES;
|
|
211
|
+
if (world[o + 1] !== 1) continue;
|
|
212
|
+
dest.set(world.subarray(o, o + SLOT_BYTES), at);
|
|
213
|
+
at += SLOT_BYTES;
|
|
214
|
+
count += 1;
|
|
215
|
+
}
|
|
216
|
+
writeHeader(dest, FRAME_KIND_KEYFRAME, count);
|
|
217
|
+
return at;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function slotChanged(world: Uint8Array, last: Uint8Array, o: number): boolean {
|
|
221
|
+
if (world[o + 1] === 0 && last[o + 1] === 0) {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
for (let b = 0; b < SLOT_BYTES; b += 1) {
|
|
225
|
+
if (world[o + b] !== last[o + b]) return true;
|
|
226
|
+
}
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Delta of slots that differ between `world` and `lastSent` into `dest`. Returns the frame
|
|
232
|
+
* length, or 0 when nothing changed (an empty delta is not sent). A slot that left is written
|
|
233
|
+
* as [clientIndex, 0, 0, 0, 0, 0].
|
|
234
|
+
*/
|
|
235
|
+
export function encodeDeltaInto(
|
|
236
|
+
dest: Uint8Array,
|
|
237
|
+
world: Uint8Array,
|
|
238
|
+
lastSent: Uint8Array,
|
|
239
|
+
): number {
|
|
240
|
+
let count = 0;
|
|
241
|
+
let at = FRAME_HEADER_BYTES;
|
|
242
|
+
for (let i = 0; i < MAX_PEERS; i += 1) {
|
|
243
|
+
const o = i * SLOT_BYTES;
|
|
244
|
+
if (!slotChanged(world, lastSent, o)) continue;
|
|
245
|
+
if (world[o + 1] === 1) {
|
|
246
|
+
dest.set(world.subarray(o, o + SLOT_BYTES), at);
|
|
247
|
+
} else {
|
|
248
|
+
writeSlotBytes(dest, at, i, 0, 0, 0);
|
|
249
|
+
}
|
|
250
|
+
at += SLOT_BYTES;
|
|
251
|
+
count += 1;
|
|
252
|
+
}
|
|
253
|
+
if (count === 0) return 0;
|
|
254
|
+
writeHeader(dest, FRAME_KIND_DELTA, count);
|
|
255
|
+
return at;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export function decodeFrame(data: Uint8Array): DecodedFrame {
|
|
259
|
+
if (data.byteLength < FRAME_HEADER_BYTES) {
|
|
260
|
+
throw new SyncFrameError(`frame too short: ${data.byteLength} bytes`);
|
|
261
|
+
}
|
|
262
|
+
const kindByte = data[0]!;
|
|
263
|
+
if (kindByte !== FRAME_KIND_KEYFRAME && kindByte !== FRAME_KIND_DELTA) {
|
|
264
|
+
throw new SyncFrameError(`unknown frame kind ${kindByte}`);
|
|
265
|
+
}
|
|
266
|
+
const version = data[1]!;
|
|
267
|
+
if (version !== FRAME_VERSION) {
|
|
268
|
+
throw new SyncFrameError(`unknown frame version ${version}`);
|
|
269
|
+
}
|
|
270
|
+
const slotCount = data[2]! | (data[3]! << 8);
|
|
271
|
+
if (data.byteLength !== FRAME_HEADER_BYTES + slotCount * SLOT_BYTES) {
|
|
272
|
+
throw new SyncFrameError(
|
|
273
|
+
`frame length ${data.byteLength} does not match slotCount ${slotCount}`,
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
const slots: DecodedSlot[] = [];
|
|
277
|
+
for (let i = 0; i < slotCount; i += 1) {
|
|
278
|
+
const o = FRAME_HEADER_BYTES + i * SLOT_BYTES;
|
|
279
|
+
slots.push({
|
|
280
|
+
clientIndex: data[o]!,
|
|
281
|
+
active: data[o + 1]!,
|
|
282
|
+
qx: readI16(data, o + 2),
|
|
283
|
+
qy: readI16(data, o + 4),
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
return {
|
|
287
|
+
kind: kindByte === FRAME_KIND_KEYFRAME ? "keyframe" : "delta",
|
|
288
|
+
version,
|
|
289
|
+
slots,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** Client side: apply a frame to a world blob (a keyframe replaces it, a delta patches it). */
|
|
294
|
+
export function applyFrameToWorld(
|
|
295
|
+
world: Uint8Array,
|
|
296
|
+
data: Uint8Array,
|
|
297
|
+
): DecodedFrame {
|
|
298
|
+
const frame = decodeFrame(data);
|
|
299
|
+
if (frame.kind === "keyframe") {
|
|
300
|
+
world.fill(0);
|
|
301
|
+
}
|
|
302
|
+
for (const slot of frame.slots) {
|
|
303
|
+
writeSlotBytes(
|
|
304
|
+
world,
|
|
305
|
+
slotOffset(slot.clientIndex),
|
|
306
|
+
slot.clientIndex,
|
|
307
|
+
slot.active,
|
|
308
|
+
slot.qx,
|
|
309
|
+
slot.qy,
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
return frame;
|
|
313
|
+
}
|
|
314
|
+
|
|
85
315
|
function asDataView(data: ArrayBufferLike | ArrayBufferView): DataView {
|
|
86
316
|
if (ArrayBuffer.isView(data)) {
|
|
87
317
|
return new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
@@ -89,7 +319,7 @@ function asDataView(data: ArrayBufferLike | ArrayBufferView): DataView {
|
|
|
89
319
|
return new DataView(data);
|
|
90
320
|
}
|
|
91
321
|
|
|
92
|
-
/** Decode a 12-byte inbound position frame. */
|
|
322
|
+
/** Decode a 12-byte inbound position frame; null for short, non-finite or out-of-range index. */
|
|
93
323
|
export function decodePositionBuffer(
|
|
94
324
|
data: ArrayBufferLike | ArrayBufferView | null | undefined,
|
|
95
325
|
): PeerPosition | null {
|
|
@@ -106,8 +336,7 @@ export function decodePositionBuffer(
|
|
|
106
336
|
if (
|
|
107
337
|
!Number.isFinite(clientIndex) ||
|
|
108
338
|
clientIndex < 0 ||
|
|
109
|
-
!
|
|
110
|
-
!Number.isFinite(y)
|
|
339
|
+
!isValidPosition(Math.trunc(clientIndex), x, y)
|
|
111
340
|
) {
|
|
112
341
|
return null;
|
|
113
342
|
}
|
|
@@ -126,70 +355,3 @@ export function encodePositionInto(
|
|
|
126
355
|
dest[2] = y;
|
|
127
356
|
return dest;
|
|
128
357
|
}
|
|
129
|
-
|
|
130
|
-
export function readPeerSlot(
|
|
131
|
-
world: Float32Array,
|
|
132
|
-
clientIndex: number,
|
|
133
|
-
): PeerSlot | null {
|
|
134
|
-
const offset = peerSlotOffset(clientIndex);
|
|
135
|
-
const active = world[offset + PEER_FIELD_ACTIVE];
|
|
136
|
-
if (active !== 1) {
|
|
137
|
-
return null;
|
|
138
|
-
}
|
|
139
|
-
return {
|
|
140
|
-
clientIndex: world[offset + PEER_FIELD_INDEX],
|
|
141
|
-
x: world[offset + PEER_FIELD_X],
|
|
142
|
-
y: world[offset + PEER_FIELD_Y],
|
|
143
|
-
active,
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Copy Redis / Node Buffer bytes into `dest` (default: a new world).
|
|
149
|
-
*
|
|
150
|
-
* Node Buffer pools can have `byteOffset` not divisible by 4, so we copy via
|
|
151
|
-
* a Uint8Array view onto `dest` instead of slicing a new ArrayBuffer.
|
|
152
|
-
*/
|
|
153
|
-
export function normalizeWorldBuffer(
|
|
154
|
-
raw: Uint8Array | null | undefined,
|
|
155
|
-
dest: Float32Array = createEmptyWorldBuffer(),
|
|
156
|
-
): Float32Array {
|
|
157
|
-
const destBytes = new Uint8Array(
|
|
158
|
-
dest.buffer,
|
|
159
|
-
dest.byteOffset,
|
|
160
|
-
dest.byteLength,
|
|
161
|
-
);
|
|
162
|
-
if (!raw || raw.byteLength === 0) {
|
|
163
|
-
destBytes.fill(0);
|
|
164
|
-
return dest;
|
|
165
|
-
}
|
|
166
|
-
const byteCount = Math.min(
|
|
167
|
-
Math.floor(raw.byteLength / 4) * 4,
|
|
168
|
-
destBytes.byteLength,
|
|
169
|
-
);
|
|
170
|
-
if (byteCount > 0) {
|
|
171
|
-
destBytes.set(raw.subarray(0, byteCount));
|
|
172
|
-
}
|
|
173
|
-
if (byteCount < destBytes.byteLength) {
|
|
174
|
-
destBytes.fill(0, byteCount);
|
|
175
|
-
}
|
|
176
|
-
return dest;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
export function decodeWorldBuffer(
|
|
180
|
-
data: ArrayBuffer,
|
|
181
|
-
dest: Float32Array = createEmptyWorldBuffer(),
|
|
182
|
-
): Float32Array {
|
|
183
|
-
return normalizeWorldBuffer(new Uint8Array(data), dest);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/** Same backing ArrayBuffer when the world is a standalone Float32Array. */
|
|
187
|
-
export function worldBufferToArrayBuffer(world: Float32Array): ArrayBuffer {
|
|
188
|
-
if (world.byteOffset === 0 && world.byteLength === world.buffer.byteLength) {
|
|
189
|
-
return world.buffer as ArrayBuffer;
|
|
190
|
-
}
|
|
191
|
-
return world.buffer.slice(
|
|
192
|
-
world.byteOffset,
|
|
193
|
-
world.byteOffset + world.byteLength,
|
|
194
|
-
) as ArrayBuffer;
|
|
195
|
-
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# world-sync
|
|
2
2
|
|
|
3
|
-
Super-simple positional sync for a **data-only** project. One in-memory agent (no Redis) stores the latest `{ x, y, z }` per session and broadcasts a JSON world snapshot.
|
|
3
|
+
Super-simple positional sync for a **data-only** project. One in-memory agent (no Redis) stores the latest `{ x, y, z }` per session and broadcasts a JSON world snapshot. A pose only marks the world dirty; a 30 Hz tick sends one shared snapshot when dirty, so a session receives at most 30 frames/s. Join and leave broadcast at once.
|
|
4
4
|
|
|
5
5
|
Use this when you want `onDataChannelMessage` plus `sendToClient` and nothing else.
|
|
6
6
|
|