@peakflo/agent-harness 0.2.20-rc.1 → 0.2.20-rc.2
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 +78 -0
- package/dist/browser-broker/agent-actions.mjs +799 -0
- package/dist/browser-broker/browsers.mjs +1699 -0
- package/dist/browser-broker/credentials.mjs +211 -0
- package/dist/browser-broker/disk-guard.mjs +84 -0
- package/dist/browser-broker/egress.mjs +183 -0
- package/dist/browser-broker/input.mjs +270 -0
- package/dist/browser-broker/lease.mjs +547 -0
- package/dist/browser-broker/local-network.mjs +173 -0
- package/dist/browser-broker/local.mjs +158 -0
- package/dist/browser-broker/log.mjs +28 -0
- package/dist/browser-broker/network-policy.mjs +144 -0
- package/dist/browser-broker/os-users.mjs +493 -0
- package/dist/browser-broker/package.json +1 -0
- package/dist/browser-broker/protocol.mjs +321 -0
- package/dist/browser-broker/queue.mjs +37 -0
- package/dist/browser-broker/recorder-scripts.mjs +221 -0
- package/dist/browser-broker/recorder.mjs +438 -0
- package/dist/browser-broker/recording-store.mjs +493 -0
- package/dist/browser-broker/relay.mjs +399 -0
- package/dist/browser-broker/screencast.mjs +198 -0
- package/dist/browser-broker/state-store.mjs +112 -0
- package/dist/browser-broker/transfers.mjs +460 -0
- package/dist/control-plane.d.ts +6 -0
- package/dist/control-plane.js +10 -0
- package/dist/github-workspace.d.ts +2 -0
- package/dist/github-workspace.js +4 -0
- package/dist/harness.d.ts +1 -0
- package/dist/harness.js +24 -0
- package/dist/runtime/knowledge-tools.d.ts +1 -0
- package/dist/runtime/knowledge-tools.js +3 -0
- package/dist/runtime/local-browser.d.ts +33 -0
- package/dist/runtime/local-browser.js +135 -0
- package/package.json +5 -3
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A person's input, applied through CDP Input.* on the active page.
|
|
3
|
+
*
|
|
4
|
+
* Coordinates arrive NORMALISED to [0, 1] across the frame and are mapped to
|
|
5
|
+
* viewport CSS pixels here. The caller has already fenced the event against
|
|
6
|
+
* the applied user lease; this module validates shape and bounds only.
|
|
7
|
+
*
|
|
8
|
+
* NEVER LOG an event from here: key and text events are what a person types.
|
|
9
|
+
*/
|
|
10
|
+
import { BROWSER_MAX_INPUT_TEXT, BrokerError } from './protocol.mjs';
|
|
11
|
+
import { normaliseUrl } from './agent-actions.mjs';
|
|
12
|
+
|
|
13
|
+
const BUTTONS = new Set(['left', 'middle', 'right']);
|
|
14
|
+
const BUTTON_MASK = { left: 1, right: 2, middle: 4 };
|
|
15
|
+
|
|
16
|
+
export function modifierMask(modifiers) {
|
|
17
|
+
if (!modifiers || typeof modifiers !== 'object') return 0;
|
|
18
|
+
return (
|
|
19
|
+
(modifiers.alt ? 1 : 0) |
|
|
20
|
+
(modifiers.ctrl ? 2 : 0) |
|
|
21
|
+
(modifiers.meta ? 4 : 0) |
|
|
22
|
+
(modifiers.shift ? 8 : 0)
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function unit(value) {
|
|
27
|
+
return (
|
|
28
|
+
typeof value === 'number' &&
|
|
29
|
+
Number.isFinite(value) &&
|
|
30
|
+
value >= 0 &&
|
|
31
|
+
value <= 1
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function toViewport(x, y, viewport) {
|
|
36
|
+
return {
|
|
37
|
+
x:
|
|
38
|
+
Math.round(Math.min(1, Math.max(0, x)) * (viewport.width - 1) * 100) /
|
|
39
|
+
100,
|
|
40
|
+
y:
|
|
41
|
+
Math.round(Math.min(1, Math.max(0, y)) * (viewport.height - 1) * 100) /
|
|
42
|
+
100
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Windows virtual key codes for the non-printable keys a viewer sends. */
|
|
47
|
+
const VK = {
|
|
48
|
+
Backspace: 8,
|
|
49
|
+
Tab: 9,
|
|
50
|
+
Enter: 13,
|
|
51
|
+
Shift: 16,
|
|
52
|
+
Control: 17,
|
|
53
|
+
Alt: 18,
|
|
54
|
+
Pause: 19,
|
|
55
|
+
CapsLock: 20,
|
|
56
|
+
Escape: 27,
|
|
57
|
+
' ': 32,
|
|
58
|
+
PageUp: 33,
|
|
59
|
+
PageDown: 34,
|
|
60
|
+
End: 35,
|
|
61
|
+
Home: 36,
|
|
62
|
+
ArrowLeft: 37,
|
|
63
|
+
ArrowUp: 38,
|
|
64
|
+
ArrowRight: 39,
|
|
65
|
+
ArrowDown: 40,
|
|
66
|
+
Insert: 45,
|
|
67
|
+
Delete: 46,
|
|
68
|
+
Meta: 91,
|
|
69
|
+
ContextMenu: 93,
|
|
70
|
+
F1: 112,
|
|
71
|
+
F2: 113,
|
|
72
|
+
F3: 114,
|
|
73
|
+
F4: 115,
|
|
74
|
+
F5: 116,
|
|
75
|
+
F6: 117,
|
|
76
|
+
F7: 118,
|
|
77
|
+
F8: 119,
|
|
78
|
+
F9: 120,
|
|
79
|
+
F10: 121,
|
|
80
|
+
F11: 122,
|
|
81
|
+
F12: 123
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export function virtualKeyCode(key) {
|
|
85
|
+
if (key in VK) return VK[key];
|
|
86
|
+
if (key.length === 1) {
|
|
87
|
+
const upper = key.toUpperCase();
|
|
88
|
+
if (/[A-Z0-9]/.test(upper)) return upper.charCodeAt(0);
|
|
89
|
+
}
|
|
90
|
+
return 0;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Validate a BrowserInputEvent. Returns a message, or null when valid. */
|
|
94
|
+
export function invalidInput(event) {
|
|
95
|
+
if (!event || typeof event !== 'object') return 'event is required';
|
|
96
|
+
switch (event.type) {
|
|
97
|
+
case 'mouse':
|
|
98
|
+
if (!['down', 'up', 'move'].includes(event.action)) return 'mouse.action';
|
|
99
|
+
if (!unit(event.x) || !unit(event.y))
|
|
100
|
+
return 'mouse coordinates must be in [0, 1]';
|
|
101
|
+
if (event.button !== undefined && !BUTTONS.has(event.button))
|
|
102
|
+
return 'mouse.button';
|
|
103
|
+
if (
|
|
104
|
+
event.clickCount !== undefined &&
|
|
105
|
+
!(
|
|
106
|
+
Number.isInteger(event.clickCount) &&
|
|
107
|
+
event.clickCount >= 0 &&
|
|
108
|
+
event.clickCount <= 3
|
|
109
|
+
)
|
|
110
|
+
) {
|
|
111
|
+
return 'mouse.clickCount';
|
|
112
|
+
}
|
|
113
|
+
return null;
|
|
114
|
+
case 'wheel':
|
|
115
|
+
if (!unit(event.x) || !unit(event.y))
|
|
116
|
+
return 'wheel coordinates must be in [0, 1]';
|
|
117
|
+
if (
|
|
118
|
+
![event.dx, event.dy].every(
|
|
119
|
+
(d) =>
|
|
120
|
+
typeof d === 'number' && Number.isFinite(d) && Math.abs(d) <= 10000
|
|
121
|
+
)
|
|
122
|
+
) {
|
|
123
|
+
return 'wheel delta';
|
|
124
|
+
}
|
|
125
|
+
return null;
|
|
126
|
+
case 'key':
|
|
127
|
+
if (!['down', 'up'].includes(event.action)) return 'key.action';
|
|
128
|
+
if (typeof event.key !== 'string' || !event.key || event.key.length > 32)
|
|
129
|
+
return 'key.key';
|
|
130
|
+
if (
|
|
131
|
+
event.code !== undefined &&
|
|
132
|
+
(typeof event.code !== 'string' || event.code.length > 32)
|
|
133
|
+
)
|
|
134
|
+
return 'key.code';
|
|
135
|
+
if (
|
|
136
|
+
event.text !== undefined &&
|
|
137
|
+
(typeof event.text !== 'string' || event.text.length > 8)
|
|
138
|
+
)
|
|
139
|
+
return 'key.text';
|
|
140
|
+
return null;
|
|
141
|
+
case 'text':
|
|
142
|
+
if (typeof event.text !== 'string' || !event.text) return 'text.text';
|
|
143
|
+
if (event.text.length > BROWSER_MAX_INPUT_TEXT) return 'text too long';
|
|
144
|
+
return null;
|
|
145
|
+
case 'nav':
|
|
146
|
+
if (!['back', 'forward', 'reload', 'goto'].includes(event.action))
|
|
147
|
+
return 'nav.action';
|
|
148
|
+
if (
|
|
149
|
+
event.action === 'goto' &&
|
|
150
|
+
(typeof event.url !== 'string' || !event.url || event.url.length > 4096)
|
|
151
|
+
) {
|
|
152
|
+
return 'nav.url';
|
|
153
|
+
}
|
|
154
|
+
return null;
|
|
155
|
+
default:
|
|
156
|
+
return 'unknown event type';
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Token bucket: at most `perSecond` events, refilled continuously. */
|
|
161
|
+
export class RateLimiter {
|
|
162
|
+
constructor(perSecond, now = () => Date.now()) {
|
|
163
|
+
this.capacity = perSecond;
|
|
164
|
+
this.tokens = perSecond;
|
|
165
|
+
this.now = now;
|
|
166
|
+
this.last = now();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
take() {
|
|
170
|
+
const t = this.now();
|
|
171
|
+
this.tokens = Math.min(
|
|
172
|
+
this.capacity,
|
|
173
|
+
this.tokens + ((t - this.last) / 1000) * this.capacity
|
|
174
|
+
);
|
|
175
|
+
this.last = t;
|
|
176
|
+
if (this.tokens < 1) return false;
|
|
177
|
+
this.tokens -= 1;
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Apply one validated event. `buttons` tracks pressed mouse buttons so drags
|
|
184
|
+
* carry the right state.
|
|
185
|
+
*/
|
|
186
|
+
export async function applyInput(event, { cdp, page, viewport, state }) {
|
|
187
|
+
const problem = invalidInput(event);
|
|
188
|
+
if (problem) throw new BrokerError('invalid_args', problem);
|
|
189
|
+
const modifiers = modifierMask(event.modifiers);
|
|
190
|
+
switch (event.type) {
|
|
191
|
+
case 'mouse': {
|
|
192
|
+
const { x, y } = toViewport(event.x, event.y, viewport);
|
|
193
|
+
const button = event.button ?? 'left';
|
|
194
|
+
if (event.action === 'down') state.buttons |= BUTTON_MASK[button];
|
|
195
|
+
if (event.action === 'up') state.buttons &= ~BUTTON_MASK[button];
|
|
196
|
+
await cdp.send('Input.dispatchMouseEvent', {
|
|
197
|
+
type:
|
|
198
|
+
event.action === 'down'
|
|
199
|
+
? 'mousePressed'
|
|
200
|
+
: event.action === 'up'
|
|
201
|
+
? 'mouseReleased'
|
|
202
|
+
: 'mouseMoved',
|
|
203
|
+
x,
|
|
204
|
+
y,
|
|
205
|
+
modifiers,
|
|
206
|
+
button:
|
|
207
|
+
event.action === 'move' ? (state.buttons ? button : 'none') : button,
|
|
208
|
+
buttons: state.buttons,
|
|
209
|
+
clickCount: event.action === 'move' ? 0 : (event.clickCount ?? 1)
|
|
210
|
+
});
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
case 'wheel': {
|
|
214
|
+
const { x, y } = toViewport(event.x, event.y, viewport);
|
|
215
|
+
await cdp.send('Input.dispatchMouseEvent', {
|
|
216
|
+
type: 'mouseWheel',
|
|
217
|
+
x,
|
|
218
|
+
y,
|
|
219
|
+
deltaX: event.dx,
|
|
220
|
+
deltaY: event.dy,
|
|
221
|
+
modifiers
|
|
222
|
+
});
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
case 'key': {
|
|
226
|
+
const printable =
|
|
227
|
+
typeof event.text === 'string' &&
|
|
228
|
+
event.text.length > 0 &&
|
|
229
|
+
!(modifiers & (2 | 4));
|
|
230
|
+
const params = {
|
|
231
|
+
type:
|
|
232
|
+
event.action === 'up'
|
|
233
|
+
? 'keyUp'
|
|
234
|
+
: printable
|
|
235
|
+
? 'keyDown'
|
|
236
|
+
: 'rawKeyDown',
|
|
237
|
+
key: event.key,
|
|
238
|
+
code: event.code ?? '',
|
|
239
|
+
windowsVirtualKeyCode: virtualKeyCode(event.key),
|
|
240
|
+
modifiers
|
|
241
|
+
};
|
|
242
|
+
if (event.action === 'down' && printable) {
|
|
243
|
+
params.text = event.text;
|
|
244
|
+
params.unmodifiedText = event.text;
|
|
245
|
+
}
|
|
246
|
+
if (event.action === 'down' && event.key === 'Enter') {
|
|
247
|
+
params.text = '\r';
|
|
248
|
+
params.unmodifiedText = '\r';
|
|
249
|
+
params.type = 'keyDown';
|
|
250
|
+
}
|
|
251
|
+
await cdp.send('Input.dispatchKeyEvent', params);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
case 'text':
|
|
255
|
+
await cdp.send('Input.insertText', { text: event.text });
|
|
256
|
+
return;
|
|
257
|
+
case 'nav': {
|
|
258
|
+
const options = { timeout: 15000, waitUntil: 'commit' };
|
|
259
|
+
const run = async () => {
|
|
260
|
+
if (event.action === 'back') await page.goBack(options);
|
|
261
|
+
else if (event.action === 'forward') await page.goForward(options);
|
|
262
|
+
else if (event.action === 'reload') await page.reload(options);
|
|
263
|
+
else await page.goto(normaliseUrl(event.url), options);
|
|
264
|
+
};
|
|
265
|
+
// Do not hold the input path for a slow page.
|
|
266
|
+
run().catch(() => undefined);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|