@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
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Clients send `{ type: "pose", x, y, z }` (or `{ x, y, z }`) on the sync channel.
|
|
5
5
|
* The agent stores the latest pose per session and broadcasts
|
|
6
|
-
* `{ type: "world", poses }` to every connected peer.
|
|
6
|
+
* `{ type: "world", poses }` to every connected peer. A pose only marks the world
|
|
7
|
+
* dirty; a 30 Hz tick (BROADCAST_HZ) sends one shared frame when dirty, so the
|
|
8
|
+
* cost per session is at most 30 frames/s however many peers send poses. Join and
|
|
9
|
+
* leave broadcast at once. The tick runs while at least one peer is connected.
|
|
7
10
|
*
|
|
8
11
|
* Build:
|
|
9
12
|
* npx @voicethere/agent build --entry templates/world-sync/agent.ts --outfile dist/agent.js
|
|
@@ -12,7 +15,12 @@ import { defineAgent, sendToClient } from "@voicethere/agent";
|
|
|
12
15
|
|
|
13
16
|
export type Pose = { x: number; y: number; z: number };
|
|
14
17
|
|
|
18
|
+
export const BROADCAST_HZ = 30;
|
|
19
|
+
const BROADCAST_INTERVAL_MS = Math.floor(1000 / BROADCAST_HZ);
|
|
20
|
+
|
|
15
21
|
const poses = new Map<string, Pose>();
|
|
22
|
+
let dirty = false;
|
|
23
|
+
let broadcastTimer: ReturnType<typeof setInterval> | null = null;
|
|
16
24
|
|
|
17
25
|
export function isFiniteNumber(value: unknown): value is number {
|
|
18
26
|
return typeof value === "number" && Number.isFinite(value);
|
|
@@ -22,17 +30,27 @@ export function parsePoseMessage(message: unknown): Pose | null {
|
|
|
22
30
|
if (!message || typeof message !== "object") {
|
|
23
31
|
return null;
|
|
24
32
|
}
|
|
25
|
-
const record = message as {
|
|
33
|
+
const record = message as {
|
|
34
|
+
type?: unknown;
|
|
35
|
+
x?: unknown;
|
|
36
|
+
y?: unknown;
|
|
37
|
+
z?: unknown;
|
|
38
|
+
};
|
|
26
39
|
if (record.type !== undefined && record.type !== "pose") {
|
|
27
40
|
return null;
|
|
28
41
|
}
|
|
29
|
-
if (
|
|
42
|
+
if (
|
|
43
|
+
!isFiniteNumber(record.x) ||
|
|
44
|
+
!isFiniteNumber(record.y) ||
|
|
45
|
+
!isFiniteNumber(record.z)
|
|
46
|
+
) {
|
|
30
47
|
return null;
|
|
31
48
|
}
|
|
32
49
|
return { x: record.x, y: record.y, z: record.z };
|
|
33
50
|
}
|
|
34
51
|
|
|
35
52
|
function broadcastWorld(): void {
|
|
53
|
+
dirty = false;
|
|
36
54
|
const payload = {
|
|
37
55
|
type: "world",
|
|
38
56
|
poses: Object.fromEntries(poses),
|
|
@@ -42,21 +60,38 @@ function broadcastWorld(): void {
|
|
|
42
60
|
}
|
|
43
61
|
}
|
|
44
62
|
|
|
63
|
+
function startTickIfNeeded(): void {
|
|
64
|
+
if (broadcastTimer || poses.size === 0) return;
|
|
65
|
+
broadcastTimer = setInterval(() => {
|
|
66
|
+
if (dirty) broadcastWorld();
|
|
67
|
+
}, BROADCAST_INTERVAL_MS);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function stopTickIfIdle(): void {
|
|
71
|
+
if (poses.size > 0 || !broadcastTimer) return;
|
|
72
|
+
clearInterval(broadcastTimer);
|
|
73
|
+
broadcastTimer = null;
|
|
74
|
+
dirty = false;
|
|
75
|
+
}
|
|
76
|
+
|
|
45
77
|
export default defineAgent({
|
|
46
78
|
onClientJoin({ sessionId }) {
|
|
47
79
|
poses.set(sessionId, { x: 0, y: 0, z: 0 });
|
|
80
|
+
startTickIfNeeded();
|
|
48
81
|
broadcastWorld();
|
|
49
82
|
},
|
|
50
83
|
|
|
51
84
|
onClientLeave({ sessionId }) {
|
|
52
85
|
poses.delete(sessionId);
|
|
53
86
|
broadcastWorld();
|
|
87
|
+
stopTickIfIdle();
|
|
54
88
|
},
|
|
55
89
|
|
|
56
90
|
onDataChannelMessage({ sessionId, message }) {
|
|
57
91
|
const pose = parsePoseMessage(message);
|
|
58
92
|
if (!pose) return;
|
|
59
93
|
poses.set(sessionId, pose);
|
|
60
|
-
|
|
94
|
+
startTickIfNeeded();
|
|
95
|
+
dirty = true;
|
|
61
96
|
},
|
|
62
97
|
});
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Single-agent positional sync using **binary** DataChannel frames. No Redis.
|
|
4
4
|
|
|
5
|
-
Clients send an `ArrayBuffer` of three little-endian float32 values `[x, y, z]`. Reuse one `Float32Array(3)` on the client. The agent stores xyz in one `Float32Array`, rewrites a single `WorldSnapshotBuffer` in place, and fans the resulting view out with `broadCastBinaryToClients` — one `Buffer` wrap per tick, zero byte copies.
|
|
5
|
+
Clients send an `ArrayBuffer` of three little-endian float32 values `[x, y, z]`. Reuse one `Float32Array(3)` on the client. The agent stores xyz in one `Float32Array`, rewrites a single `WorldSnapshotBuffer` in place, and fans the resulting view out with `broadCastBinaryToClients` — one `Buffer` wrap per tick, zero byte copies. 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.
|
|
6
6
|
|
|
7
7
|
## Protocol
|
|
8
8
|
|
|
@@ -4,7 +4,11 @@
|
|
|
4
4
|
* Clients send an ArrayBuffer of 3 float32 values `[x, y, z]` on the sync
|
|
5
5
|
* DataChannel. Poses live in one Float32Array; the outbound snapshot is one
|
|
6
6
|
* growable buffer rewritten in place and fanned out as a single Buffer view
|
|
7
|
-
* (no per-tick or per-peer copies
|
|
7
|
+
* (no per-tick or per-peer copies; the IPC send copies the bytes synchronously,
|
|
8
|
+
* test/binary-send-copy.test.ts). A pose only marks the world dirty; a 30 Hz
|
|
9
|
+
* tick (BROADCAST_HZ) sends one shared frame when dirty, so each session gets at
|
|
10
|
+
* most 30 frames/s however many peers send poses. Join and leave broadcast at
|
|
11
|
+
* once. The tick runs while at least one peer is connected.
|
|
8
12
|
*
|
|
9
13
|
* Build:
|
|
10
14
|
* npx @voicethere/agent build --entry templates/world-sync-binary/agent.ts --outfile dist/agent.js
|
|
@@ -17,6 +21,11 @@ import {
|
|
|
17
21
|
WorldSnapshotBuffer,
|
|
18
22
|
} from "./protocol.js";
|
|
19
23
|
|
|
24
|
+
export const BROADCAST_HZ = 30;
|
|
25
|
+
const BROADCAST_INTERVAL_MS = Math.floor(1000 / BROADCAST_HZ);
|
|
26
|
+
|
|
27
|
+
let dirty = false;
|
|
28
|
+
let broadcastTimer: ReturnType<typeof setInterval> | null = null;
|
|
20
29
|
const snapshot = new WorldSnapshotBuffer();
|
|
21
30
|
const indexBySession = new Map<string, number>();
|
|
22
31
|
const sessionIds: string[] = [];
|
|
@@ -74,20 +83,37 @@ function removePeer(sessionId: string): void {
|
|
|
74
83
|
}
|
|
75
84
|
|
|
76
85
|
function broadcastWorld(): void {
|
|
86
|
+
dirty = false;
|
|
77
87
|
// sessionIds.length === peerCount, so one Buffer view is shared by every send.
|
|
78
88
|
const payload = snapshot.encodePacked(sessionIds, xyz, peerCount);
|
|
79
89
|
broadCastBinaryToClients(payload, sessionIds, "sync");
|
|
80
90
|
}
|
|
81
91
|
|
|
92
|
+
function startTickIfNeeded(): void {
|
|
93
|
+
if (broadcastTimer || peerCount === 0) return;
|
|
94
|
+
broadcastTimer = setInterval(() => {
|
|
95
|
+
if (dirty) broadcastWorld();
|
|
96
|
+
}, BROADCAST_INTERVAL_MS);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function stopTickIfIdle(): void {
|
|
100
|
+
if (peerCount > 0 || !broadcastTimer) return;
|
|
101
|
+
clearInterval(broadcastTimer);
|
|
102
|
+
broadcastTimer = null;
|
|
103
|
+
dirty = false;
|
|
104
|
+
}
|
|
105
|
+
|
|
82
106
|
export default defineAgent({
|
|
83
107
|
onClientJoin({ sessionId }) {
|
|
84
108
|
setPose(sessionId, 0, 0, 0);
|
|
109
|
+
startTickIfNeeded();
|
|
85
110
|
broadcastWorld();
|
|
86
111
|
},
|
|
87
112
|
|
|
88
113
|
onClientLeave({ sessionId }) {
|
|
89
114
|
removePeer(sessionId);
|
|
90
115
|
broadcastWorld();
|
|
116
|
+
stopTickIfIdle();
|
|
91
117
|
},
|
|
92
118
|
|
|
93
119
|
onDataChannelBinary({ sessionId, rawBinary }) {
|
|
@@ -102,6 +128,7 @@ export default defineAgent({
|
|
|
102
128
|
if (!decodePoseInto(rawBinary, xyz, index * POSE_FLOAT_COUNT)) {
|
|
103
129
|
return;
|
|
104
130
|
}
|
|
105
|
-
|
|
131
|
+
startTickIfNeeded();
|
|
132
|
+
dirty = true;
|
|
106
133
|
},
|
|
107
134
|
});
|