webhelm 1.0.54 → 1.0.56
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 +9 -0
- package/package.json +2 -1
- package/runtime/cli-help.mjs +31 -4
- package/runtime/cli.mjs +32 -1
- package/runtime/demo-capture.mjs +344 -0
- package/runtime/demo-engine.mjs +298 -0
- package/runtime/extension/manifest.json +1 -1
- package/runtime/node-daemon.mjs +273 -36
- package/runtime/user-helpers.mjs +72 -0
- package/runtime/network-observer.mjs +0 -55
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
export const DEMO_PROTOCOL = 'webhelm.demo/1';
|
|
2
|
+
const TEXT_LIMIT = 1500;
|
|
3
|
+
const WAIT_LIMIT = 30000;
|
|
4
|
+
const EVENT_LIMIT = 5000;
|
|
5
|
+
const HIGHLIGHT_MS = 650;
|
|
6
|
+
const CURSOR_MS = 480;
|
|
7
|
+
const MAX_RECORDING_MS = 300000;
|
|
8
|
+
|
|
9
|
+
const DEMO_COMMANDS = new Set([
|
|
10
|
+
'step', 'spotlight', 'annotate', 'caption', 'say', 'cursor', 'highlight',
|
|
11
|
+
'clear', 'pause', 'resume', 'wait', 'start_recording', 'stop_recording',
|
|
12
|
+
'status',
|
|
13
|
+
]);
|
|
14
|
+
const TARGET_COMMANDS = new Set(['spotlight', 'annotate', 'cursor', 'highlight']);
|
|
15
|
+
const TEXT_COMMANDS = new Set(['caption', 'say', 'annotate']);
|
|
16
|
+
const PAUSE_ALLOWED = new Set(['resume', 'clear', 'stop_recording']);
|
|
17
|
+
|
|
18
|
+
function text(value) {
|
|
19
|
+
return typeof value === 'string' ? value : '';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function integer(value) {
|
|
23
|
+
return typeof value === 'number' && Number.isSafeInteger(value) ? value : null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function jsString(value) {
|
|
27
|
+
return JSON.stringify(String(value));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function commandError(message) {
|
|
31
|
+
const error = new Error(message);
|
|
32
|
+
error.demoCommand = true;
|
|
33
|
+
return error;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function viewportValue(width, height) {
|
|
37
|
+
return { width: integer(width), height: integer(height) };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class DemoEngine {
|
|
41
|
+
constructor(session, transport) {
|
|
42
|
+
this.sessionId = session.session_id;
|
|
43
|
+
this.transport = transport;
|
|
44
|
+
this.targetId = session.targetId;
|
|
45
|
+
this.state = null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
requireActive() {
|
|
49
|
+
if (!this.sessionId) throw commandError('Demo requires an active WebHelm Session');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
ensureOverlay() {
|
|
53
|
+
if (!this.state) {
|
|
54
|
+
this.state = {
|
|
55
|
+
sessionId: this.sessionId,
|
|
56
|
+
targetId: this.targetId,
|
|
57
|
+
recording: false,
|
|
58
|
+
paused: false,
|
|
59
|
+
startedAt: 0,
|
|
60
|
+
stepNumber: 0,
|
|
61
|
+
events: [],
|
|
62
|
+
completed: null,
|
|
63
|
+
overlay: {
|
|
64
|
+
screen_width: this.transport.viewport.width,
|
|
65
|
+
screen_height: this.transport.viewport.height,
|
|
66
|
+
},
|
|
67
|
+
viewport: { ...this.transport.viewport },
|
|
68
|
+
};
|
|
69
|
+
} else if (!this.state.overlay?.screen_width) {
|
|
70
|
+
this.state.overlay = {
|
|
71
|
+
screen_width: this.state.viewport.width || this.transport.viewport.width,
|
|
72
|
+
screen_height: this.state.viewport.height || this.transport.viewport.height,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
return this.state;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
requireRecording() {
|
|
79
|
+
if (!this.state?.recording) throw commandError('Demo recording not started');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
newEvent(command, extra = {}) {
|
|
83
|
+
const at = Date.now();
|
|
84
|
+
const relative = this.state?.startedMono ? Math.round(performance.now() - this.state.startedMono) : 0;
|
|
85
|
+
return { command, at_unix_ms: at, relative_ms: relative, ...extra };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
result(event) {
|
|
89
|
+
return {
|
|
90
|
+
command: event.command,
|
|
91
|
+
event_count: this.state?.events.length || 0,
|
|
92
|
+
at_unix_ms: event.at_unix_ms,
|
|
93
|
+
relative_ms: event.relative_ms,
|
|
94
|
+
overlay_mode: 'webhelm_capture_only',
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
status() {
|
|
99
|
+
return {
|
|
100
|
+
protocol: DEMO_PROTOCOL,
|
|
101
|
+
recording: !!this.state?.recording,
|
|
102
|
+
paused: !!this.state?.paused,
|
|
103
|
+
overlay_active: !!(this.state && Object.keys(this.state.overlay).length > 2),
|
|
104
|
+
recording_started_at_unix_ms: this.state?.startedAt || 0,
|
|
105
|
+
screen_points: { ...(this.state?.viewport || this.transport.viewport || { width: 0, height: 0 }) },
|
|
106
|
+
events: this.state?.events || [],
|
|
107
|
+
completed_recording: this.state?.completed || null,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
startRecording() {
|
|
112
|
+
this.requireActive();
|
|
113
|
+
if (this.state?.recording) throw commandError('Demo recording already active; stop it before starting again');
|
|
114
|
+
const startedAt = Date.now();
|
|
115
|
+
this.state = {
|
|
116
|
+
sessionId: this.sessionId, targetId: this.targetId, recording: true,
|
|
117
|
+
paused: false, startedAt, startedMono: performance.now(), stepNumber: 0, events: [], overlay: {
|
|
118
|
+
screen_width: this.transport.viewport.width,
|
|
119
|
+
screen_height: this.transport.viewport.height,
|
|
120
|
+
}, viewport: { ...this.transport.viewport },
|
|
121
|
+
capture_deadline_mono: performance.now() + MAX_RECORDING_MS,
|
|
122
|
+
};
|
|
123
|
+
return {
|
|
124
|
+
protocol: DEMO_PROTOCOL,
|
|
125
|
+
recording_started_at_unix_ms: startedAt,
|
|
126
|
+
video_recorded: false,
|
|
127
|
+
events_recorded: true,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
stopRecording() {
|
|
132
|
+
this.requireRecording();
|
|
133
|
+
const endedAt = Date.now();
|
|
134
|
+
const completed = {
|
|
135
|
+
protocol: DEMO_PROTOCOL,
|
|
136
|
+
recording_started_at_unix_ms: this.state.startedAt,
|
|
137
|
+
recording_ended_at_unix_ms: endedAt,
|
|
138
|
+
video_recorded: false,
|
|
139
|
+
events: this.state.events,
|
|
140
|
+
screen_points: { ...this.state.viewport },
|
|
141
|
+
};
|
|
142
|
+
this.state.completed = completed;
|
|
143
|
+
this.state.recording = false;
|
|
144
|
+
this.state.paused = false;
|
|
145
|
+
this.state.events = [];
|
|
146
|
+
return completed;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async resolveTarget(command, args) {
|
|
150
|
+
const ref = text(args.ref);
|
|
151
|
+
const label = text(args.label);
|
|
152
|
+
if (!ref && !label) throw commandError('Demo target requires ref or accessible label');
|
|
153
|
+
if (ref && label) throw commandError('Supply ref or accessible label, not both');
|
|
154
|
+
if (ref.length > 200) throw commandError('Demo ref exceeds 200 characters');
|
|
155
|
+
if (label.length > TEXT_LIMIT) throw commandError('Demo accessible label exceeds 1500 characters');
|
|
156
|
+
if (label && !label.trim()) throw commandError('Demo accessible label must be nonempty');
|
|
157
|
+
if (ref && (!this.transport.snapshotGeneration || this.transport.snapshotTargetId !== this.targetId ||
|
|
158
|
+
!ref.startsWith(`d${this.sessionId}-${this.transport.snapshotGeneration}-`))) {
|
|
159
|
+
throw commandError('Invalid or stale element ref: take a fresh snapshot in the active tab');
|
|
160
|
+
}
|
|
161
|
+
const script = `(() => {
|
|
162
|
+
const wanted=${jsString(ref)};
|
|
163
|
+
const label=${jsString(label)};
|
|
164
|
+
let roots=[];
|
|
165
|
+
const visible=node=>{const r=node.getBoundingClientRect();if(r.width<=0||r.height<=0||r.right<=0||r.bottom<=0||r.left>=innerWidth||r.top>=innerHeight)return false;for(let p=node;p;p=p.parentElement){const s=getComputedStyle(p);if(s.visibility==='hidden'||s.visibility==='collapse'||s.display==='none'||s.opacity==='0'||p.hasAttribute('hidden')||p.hasAttribute('inert')||p.getAttribute('aria-hidden')==='true')return false}return true};
|
|
166
|
+
if(wanted)roots=Array.from(document.querySelectorAll('[data-ref]')).filter(node=>node.getAttribute('data-ref')===wanted&&visible(node));
|
|
167
|
+
else {
|
|
168
|
+
const matches=Array.from(document.querySelectorAll('button,a,input,textarea,select,[role],[tabindex]')).filter(node=>{
|
|
169
|
+
const labelledBy=node.getAttribute('aria-labelledby');
|
|
170
|
+
const labelled=labelledBy?labelledBy.trim().split(/\\s+/).map(id=>document.getElementById(id)?.innerText||'').join(' '):'';
|
|
171
|
+
const accessible=node.getAttribute('aria-label')||labelled||node.getAttribute('title')||node.getAttribute('alt')||node.value||node.labels?.[0]?.innerText||node.innerText||node.textContent||'';
|
|
172
|
+
return visible(node)&&accessible.trim().toLowerCase().includes(label.trim().toLowerCase());
|
|
173
|
+
});
|
|
174
|
+
roots=matches;
|
|
175
|
+
}
|
|
176
|
+
if(roots.length!==1)return {error:roots.length?'ambiguous':'missing'};
|
|
177
|
+
const node=roots[0];
|
|
178
|
+
const rect=node.getBoundingClientRect();
|
|
179
|
+
const style=getComputedStyle(node);
|
|
180
|
+
if(!visible(node))return {error:'invisible'};
|
|
181
|
+
return {frame:{x:rect.x,y:rect.y,width:rect.width,height:rect.height},label:node.getAttribute('aria-label')||node.textContent||''};
|
|
182
|
+
})()`;
|
|
183
|
+
const found = await this.transport.evaluate(script, this.targetId);
|
|
184
|
+
if (found?.error === 'missing') {
|
|
185
|
+
throw commandError(ref ? 'Invalid or stale element ref: take a fresh pilot snapshot' : 'Demo target not found; supply a current ref or accessible label');
|
|
186
|
+
}
|
|
187
|
+
if (found?.error === 'ambiguous') throw commandError('Ambiguous accessible label');
|
|
188
|
+
if (!found?.frame || !(found.frame.width > 0) || !(found.frame.height > 0)) {
|
|
189
|
+
throw commandError('Demo target is invisible or has zero area');
|
|
190
|
+
}
|
|
191
|
+
return found.frame;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async execute(command, args = {}) {
|
|
195
|
+
this.requireActive();
|
|
196
|
+
if (!DEMO_COMMANDS.has(command)) throw commandError('Invalid agent.demo command');
|
|
197
|
+
const state = this.ensureOverlay();
|
|
198
|
+
if (command === 'status') return this.status();
|
|
199
|
+
if (command === 'start_recording') return this.startRecording();
|
|
200
|
+
if (command === 'stop_recording') return this.stopRecording();
|
|
201
|
+
if (this.state?.paused && !PAUSE_ALLOWED.has(command)) {
|
|
202
|
+
throw commandError('Demo paused: call resume before adding events');
|
|
203
|
+
}
|
|
204
|
+
if (this.state?.recording && performance.now() >= this.state.capture_deadline_mono) {
|
|
205
|
+
throw commandError(`Demo recording reached ${MAX_RECORDING_MS / 1000} seconds; stop it before starting a new recording`);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const allowed = {
|
|
209
|
+
step:['title'], spotlight:['ref','label','text'], annotate:['ref','label','text'],
|
|
210
|
+
caption:['text'], say:['text'], cursor:['ref','label'], highlight:['ref','label'],
|
|
211
|
+
clear:[], pause:[], resume:[], wait:['ms'], status:[], start_recording:[], stop_recording:[],
|
|
212
|
+
}[command];
|
|
213
|
+
const unexpected=Object.keys(args).filter(key=>args[key]!==undefined&&!allowed.includes(key));
|
|
214
|
+
if(unexpected.length)throw commandError(`${command} does not accept argument: ${unexpected[0]}`);
|
|
215
|
+
if(TARGET_COMMANDS.has(command)){
|
|
216
|
+
for(const key of ['ref','label'])if(args[key]!==undefined&&typeof args[key]!=='string')throw commandError(`Demo ${key} must be a string`);
|
|
217
|
+
}
|
|
218
|
+
if(this.state.recording&&this.state.events.length>=EVENT_LIMIT)throw commandError(`Demo event limit reached (${EVENT_LIMIT})`);
|
|
219
|
+
|
|
220
|
+
const event = this.newEvent(command);
|
|
221
|
+
if (TARGET_COMMANDS.has(command)) {
|
|
222
|
+
if (text(args.ref)) event.ref = text(args.ref);
|
|
223
|
+
if (text(args.label)) event.label = text(args.label);
|
|
224
|
+
}
|
|
225
|
+
if (command === 'step') {
|
|
226
|
+
const title = text(args.title);
|
|
227
|
+
if (!title || title.length > TEXT_LIMIT) throw commandError('step requires title (1..1500 characters)');
|
|
228
|
+
event.title = title;
|
|
229
|
+
event.number = ++this.state.stepNumber;
|
|
230
|
+
}
|
|
231
|
+
if (TEXT_COMMANDS.has(command)) {
|
|
232
|
+
const value = text(args.text);
|
|
233
|
+
if (!value || value.length > TEXT_LIMIT) throw commandError('Demo command requires nonempty text (1..1500 characters)');
|
|
234
|
+
event.text = value;
|
|
235
|
+
}
|
|
236
|
+
if (command==='spotlight'&&args.text!==undefined&&typeof args.text!=='string') {
|
|
237
|
+
throw commandError('spotlight text must be a string');
|
|
238
|
+
}
|
|
239
|
+
if (text(args.text).length > TEXT_LIMIT) {
|
|
240
|
+
throw commandError('Demo command requires text at most 1500 characters');
|
|
241
|
+
}
|
|
242
|
+
if (command !== 'annotate' && text(args.text)) event.text = text(args.text);
|
|
243
|
+
if (TARGET_COMMANDS.has(command)) event.frame = await this.resolveTarget(command, args);
|
|
244
|
+
if (command === 'wait') {
|
|
245
|
+
const ms = integer(args.ms);
|
|
246
|
+
if (ms === null || ms < 0 || ms > WAIT_LIMIT) throw commandError('wait requires ms 0..30000');
|
|
247
|
+
event.ms = ms;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const overlay = state.overlay;
|
|
251
|
+
if (command === 'step') {
|
|
252
|
+
overlay.step = event.title;
|
|
253
|
+
overlay.step_number = event.number;
|
|
254
|
+
} else if (command === 'caption' || command === 'say') {
|
|
255
|
+
overlay.caption = event.text;
|
|
256
|
+
} else if (['spotlight', 'annotate', 'highlight'].includes(command)) {
|
|
257
|
+
overlay.frame = event.frame;
|
|
258
|
+
overlay.spotlight = command === 'spotlight';
|
|
259
|
+
overlay.tooltip = command === 'annotate' ? event.text : undefined;
|
|
260
|
+
overlay.expires_at_ms = command === 'highlight' ? event.at_unix_ms + HIGHLIGHT_MS : undefined;
|
|
261
|
+
} else if (command === 'cursor') {
|
|
262
|
+
const previous = overlay.cursor || {
|
|
263
|
+
x: this.state.viewport.width / 2,
|
|
264
|
+
y: this.state.viewport.height * 0.82,
|
|
265
|
+
};
|
|
266
|
+
overlay.cursor_from = previous;
|
|
267
|
+
overlay.cursor = {
|
|
268
|
+
x: event.frame.x + event.frame.width / 2,
|
|
269
|
+
y: event.frame.y + event.frame.height / 2,
|
|
270
|
+
};
|
|
271
|
+
overlay.cursor_started_at_ms = event.at_unix_ms;
|
|
272
|
+
overlay.cursor_duration_ms = CURSOR_MS;
|
|
273
|
+
overlay.expires_at_ms = undefined;
|
|
274
|
+
} else if (command === 'clear') {
|
|
275
|
+
state.overlay = {
|
|
276
|
+
screen_width: state.viewport.width,
|
|
277
|
+
screen_height: state.viewport.height,
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
if (command === 'pause') this.state.paused = true;
|
|
281
|
+
if (command === 'resume' || command === 'clear') this.state.paused = false;
|
|
282
|
+
if (state.recording) state.events.push(event);
|
|
283
|
+
if (command === 'wait' && event.ms > 0) {
|
|
284
|
+
await new Promise(resolve => setTimeout(resolve, event.ms));
|
|
285
|
+
event.wait_completed_at_unix_ms = Date.now();
|
|
286
|
+
event.wait_completed_relative_ms = Math.round(performance.now() - this.state.startedMono);
|
|
287
|
+
}
|
|
288
|
+
return this.result(event);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
dispose() {
|
|
292
|
+
try {
|
|
293
|
+
this.transport?.close?.();
|
|
294
|
+
} finally {
|
|
295
|
+
this.state = null;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|