vibedgames 0.5.1 → 0.6.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/dist/commands/playtest-run.d.ts +66 -0
- package/dist/commands/playtest-run.js +268 -0
- package/dist/commands/playtest.d.ts +37 -2
- package/dist/commands/playtest.js +17 -10
- package/dist/index.js +12 -2
- package/dist/lib/playtest/agent.d.ts +192 -0
- package/dist/lib/playtest/agent.js +652 -0
- package/dist/lib/playtest/browser.d.ts +109 -0
- package/dist/lib/playtest/browser.js +219 -0
- package/dist/lib/playtest/controls.d.ts +47 -0
- package/dist/lib/playtest/controls.js +218 -0
- package/dist/lib/playtest/errors.d.ts +9 -0
- package/dist/lib/playtest/errors.js +12 -0
- package/dist/lib/playtest/keys.d.ts +41 -0
- package/dist/lib/playtest/keys.js +157 -0
- package/dist/lib/playtest/run.d.ts +195 -0
- package/dist/lib/playtest/run.js +409 -0
- package/package.json +1 -1
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The in-page playtester.
|
|
3
|
+
*
|
|
4
|
+
* `vg playtest run` injects this function's SOURCE into the game's page and
|
|
5
|
+
* calls it there, so the whole decide → apply → measure loop runs inside the
|
|
6
|
+
* browser: no CLI round trip per decision, and a per-frame reflex layer that
|
|
7
|
+
* a subprocess could never keep up with. The CLI's only jobs are to mint the
|
|
8
|
+
* token, inject, poll `window.__PLAYTEST_AGENT__.done`, and read the
|
|
9
|
+
* records back.
|
|
10
|
+
*
|
|
11
|
+
* Rules that follow from being stringified: `inPageAgent` and `browserEnv`
|
|
12
|
+
* must be entirely self-contained — no imports, no module-scope references
|
|
13
|
+
* (the `declare`d globals below are erased and resolve to the page's own) —
|
|
14
|
+
* which is why every helper lives inside them. The agent talks to the page
|
|
15
|
+
* only through `env`, the real browser in production and a fake in tests.
|
|
16
|
+
*
|
|
17
|
+
* The decision is the hold: the previous inputs stay down while the model
|
|
18
|
+
* answers, so the game runs continuously and the loop decides at the
|
|
19
|
+
* model's latency, with `tickMs` as a floor. A move option in the game's
|
|
20
|
+
* `__GAME_PLAYTEST__` may carry a `reflex(game)` function; while that option
|
|
21
|
+
* is the current intent, the reflex runs every frame and its returned inputs
|
|
22
|
+
* are what is held — the model sets intent a few times a second, the reflex
|
|
23
|
+
* turns it into input at 60 fps.
|
|
24
|
+
*/
|
|
25
|
+
/** A parsed JSON document — the shape of everything that crosses the page boundary. */
|
|
26
|
+
type Json = string | number | boolean | null | Json[] | {
|
|
27
|
+
[key: string]: Json;
|
|
28
|
+
};
|
|
29
|
+
export interface AgentKeyInit {
|
|
30
|
+
code: string;
|
|
31
|
+
key: string;
|
|
32
|
+
keyCode: number;
|
|
33
|
+
which: number;
|
|
34
|
+
altKey: boolean;
|
|
35
|
+
ctrlKey: boolean;
|
|
36
|
+
shiftKey: boolean;
|
|
37
|
+
bubbles: true;
|
|
38
|
+
}
|
|
39
|
+
export interface AgentPointer {
|
|
40
|
+
x: number;
|
|
41
|
+
y: number;
|
|
42
|
+
down: boolean;
|
|
43
|
+
}
|
|
44
|
+
export interface AgentMove {
|
|
45
|
+
description: string;
|
|
46
|
+
inits: AgentKeyInit[];
|
|
47
|
+
pointer: AgentPointer | null;
|
|
48
|
+
}
|
|
49
|
+
export interface AgentAction {
|
|
50
|
+
description: string;
|
|
51
|
+
inits: AgentKeyInit[];
|
|
52
|
+
}
|
|
53
|
+
export interface AgentConfig {
|
|
54
|
+
decideUrl: string;
|
|
55
|
+
token: string;
|
|
56
|
+
model: string;
|
|
57
|
+
/** Hold this move for the whole run and never call the model — for tuning a reflex. */
|
|
58
|
+
pinMove: string | null;
|
|
59
|
+
goal: string;
|
|
60
|
+
move: Record<string, AgentMove>;
|
|
61
|
+
actions: Record<string, AgentAction>;
|
|
62
|
+
/** Every supported KeyboardEvent code, for inputs a reflex returns. */
|
|
63
|
+
keyTable: Record<string, AgentKeyInit>;
|
|
64
|
+
ticks: number;
|
|
65
|
+
tickMs: number;
|
|
66
|
+
finalHoldMs: number;
|
|
67
|
+
actionThreshold: number;
|
|
68
|
+
motionEpsilon: number;
|
|
69
|
+
stuckRun: number;
|
|
70
|
+
/** Decisions in a row of holding nothing, going nowhere, before the no-input option is rested. */
|
|
71
|
+
idleRun: number;
|
|
72
|
+
/** How many decisions a rested no-input option stays out of the question. */
|
|
73
|
+
idleRestTicks: number;
|
|
74
|
+
trailLength: number;
|
|
75
|
+
decisionRetries: number;
|
|
76
|
+
decisionTimeoutMs: number;
|
|
77
|
+
}
|
|
78
|
+
export interface AgentSample {
|
|
79
|
+
x: number;
|
|
80
|
+
y: number;
|
|
81
|
+
z: number;
|
|
82
|
+
frame: number;
|
|
83
|
+
score: number;
|
|
84
|
+
complete: boolean;
|
|
85
|
+
}
|
|
86
|
+
export interface AgentWindow {
|
|
87
|
+
path: number;
|
|
88
|
+
peak: number;
|
|
89
|
+
frameBefore: number;
|
|
90
|
+
frame: number;
|
|
91
|
+
scoreBefore: number;
|
|
92
|
+
score: number;
|
|
93
|
+
x: number;
|
|
94
|
+
y: number;
|
|
95
|
+
z: number;
|
|
96
|
+
complete: boolean;
|
|
97
|
+
}
|
|
98
|
+
export interface AgentRecord {
|
|
99
|
+
tick: number;
|
|
100
|
+
move: string;
|
|
101
|
+
actions: string[];
|
|
102
|
+
actionProbabilities: Record<string, number>;
|
|
103
|
+
confidence: number | null;
|
|
104
|
+
decisionMs: number;
|
|
105
|
+
inputTokens: number;
|
|
106
|
+
outputTokens: number;
|
|
107
|
+
/** Frames on which the option's reflex produced the held input. */
|
|
108
|
+
reflexFrames: number;
|
|
109
|
+
/**
|
|
110
|
+
* The model's own read of how close the player is to `goal`, 0–1, from the
|
|
111
|
+
* `progress` score question; null when it gave none.
|
|
112
|
+
*/
|
|
113
|
+
progress: number | null;
|
|
114
|
+
/** Whether the move asked the player to move at all, so a still window can count as stuck. */
|
|
115
|
+
askedToMove: boolean;
|
|
116
|
+
window: AgentWindow;
|
|
117
|
+
}
|
|
118
|
+
export interface AgentUsage {
|
|
119
|
+
calls: number;
|
|
120
|
+
inputTokens: number;
|
|
121
|
+
outputTokens: number;
|
|
122
|
+
totalDecisionMs: number;
|
|
123
|
+
maxDecisionMs: number;
|
|
124
|
+
}
|
|
125
|
+
export interface AgentResult {
|
|
126
|
+
done: boolean;
|
|
127
|
+
error: string | null;
|
|
128
|
+
records: AgentRecord[];
|
|
129
|
+
usage: AgentUsage;
|
|
130
|
+
wallMs: number;
|
|
131
|
+
completedAtTick: number | null;
|
|
132
|
+
stop: () => void;
|
|
133
|
+
}
|
|
134
|
+
/** The game's diagnostics, as far as the agent reads them. */
|
|
135
|
+
export interface Diagnostics {
|
|
136
|
+
frame?: number;
|
|
137
|
+
score?: number;
|
|
138
|
+
complete?: boolean;
|
|
139
|
+
player?: {
|
|
140
|
+
x?: number;
|
|
141
|
+
y?: number;
|
|
142
|
+
z?: number;
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
/** What a reflex may hand back: key codes to hold and/or a pointer position. */
|
|
146
|
+
export interface ReflexInputs {
|
|
147
|
+
keys?: string[];
|
|
148
|
+
pointer?: {
|
|
149
|
+
x: number;
|
|
150
|
+
y: number;
|
|
151
|
+
down?: boolean;
|
|
152
|
+
} | null;
|
|
153
|
+
/** The reflex is trying to move and getting nowhere; see `settle`. */
|
|
154
|
+
stuck?: boolean;
|
|
155
|
+
}
|
|
156
|
+
export type Reflex = (game: Diagnostics | null) => ReflexInputs | null | undefined;
|
|
157
|
+
export interface AgentResponse {
|
|
158
|
+
ok: boolean;
|
|
159
|
+
status: number;
|
|
160
|
+
text: () => Promise<string>;
|
|
161
|
+
}
|
|
162
|
+
export interface AgentRequest {
|
|
163
|
+
method: string;
|
|
164
|
+
headers: Record<string, string>;
|
|
165
|
+
body: string;
|
|
166
|
+
}
|
|
167
|
+
/** The page, as the agent sees it. */
|
|
168
|
+
export interface AgentEnv {
|
|
169
|
+
now: () => number;
|
|
170
|
+
fetch: (url: string, init: AgentRequest) => Promise<AgentResponse>;
|
|
171
|
+
raf: (frame: () => void) => number;
|
|
172
|
+
caf: (id: number) => void;
|
|
173
|
+
delay: (ms: number) => Promise<void>;
|
|
174
|
+
read: () => AgentSample;
|
|
175
|
+
/** A JSON-safe copy of the diagnostics, for the model. */
|
|
176
|
+
snapshot: () => Json;
|
|
177
|
+
/** The live diagnostics object, for a reflex. */
|
|
178
|
+
live: () => Diagnostics | null;
|
|
179
|
+
dispatchKey: (type: "keydown" | "keyup", init: AgentKeyInit) => void;
|
|
180
|
+
dispatchPointer: (pointer: AgentPointer, type: "pointermove" | "pointerdown" | "pointerup") => void;
|
|
181
|
+
reflexFor: (label: string) => Reflex | null;
|
|
182
|
+
publish: (result: AgentResult) => void;
|
|
183
|
+
}
|
|
184
|
+
/** The real page as an AgentEnv. Only ever runs in the browser; injected beside the agent. */
|
|
185
|
+
export declare const browserEnv: () => AgentEnv;
|
|
186
|
+
/**
|
|
187
|
+
* Run the playtest inside the page. Returns the live result object, which is
|
|
188
|
+
* also published as `window.__PLAYTEST_AGENT__`; `done` flips when the run
|
|
189
|
+
* ends, `error` says why if it ended early.
|
|
190
|
+
*/
|
|
191
|
+
export declare const inPageAgent: (config: AgentConfig, env: AgentEnv) => AgentResult;
|
|
192
|
+
export {};
|