@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,321 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The broker's copy of the shared contract.
|
|
3
|
+
*
|
|
4
|
+
* SOURCE OF TRUTH: packages/core/src/services/coding-agents/browser-protocol.ts.
|
|
5
|
+
* The broker is plain Node in a root-owned image layer and cannot import the
|
|
6
|
+
* core package, so it carries a copy of the few constants it needs. A test in
|
|
7
|
+
* workflow-api reads this file and keeps every exported value here equal to
|
|
8
|
+
* core's. Change both together.
|
|
9
|
+
*
|
|
10
|
+
* Strings marked "20x" are copied verbatim from the 20x desktop app
|
|
11
|
+
* (src/main/browser-recording.ts, src/main/panel-browser-broker.ts) because
|
|
12
|
+
* agents and the recording format depend on the exact text.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* v3 (image 0.15.0): agent credentials. `fill_credential` command, the
|
|
17
|
+
* broker-initiated `cred_resolve` / `cred_hosts` / `cred_audit` requests
|
|
18
|
+
* answered by a `cred_result` push, the `cred_revoke` request, and `features`
|
|
19
|
+
* in the hello.
|
|
20
|
+
*/
|
|
21
|
+
export const BROWSER_BROKER_PROTOCOL_VERSION = 3;
|
|
22
|
+
/** v3 hello `features` (core BROWSER_BROKER_FEATURES). */
|
|
23
|
+
export const BROWSER_BROKER_FEATURES = Object.freeze([
|
|
24
|
+
'fill_credential',
|
|
25
|
+
'credential_proxy'
|
|
26
|
+
]);
|
|
27
|
+
/** Alias kept for the broker's own call sites. */
|
|
28
|
+
export const BROKER_FEATURES = BROWSER_BROKER_FEATURES;
|
|
29
|
+
export const BROWSER_BROKER_SOCKET_PATH = '/api/coding-agents/browser/broker';
|
|
30
|
+
export const BROWSER_PANEL_ID = 'chat-browser';
|
|
31
|
+
|
|
32
|
+
/** Largest text message either socket accepts. Frames are binary. */
|
|
33
|
+
export const BROWSER_MAX_TEXT_MESSAGE_BYTES = 256 * 1024;
|
|
34
|
+
/** Largest binary frame (header + JPEG) the API relays. */
|
|
35
|
+
export const BROWSER_MAX_FRAME_BYTES = 1024 * 1024;
|
|
36
|
+
/** Fixed page viewport for the first release; the UI scales the frame. */
|
|
37
|
+
export const BROWSER_VIEWPORT = Object.freeze({ width: 1280, height: 800 });
|
|
38
|
+
export const BROWSER_MAX_INPUT_TEXT = 2000;
|
|
39
|
+
export const BROWSER_MAX_INPUT_EVENTS_PER_SECOND = 200;
|
|
40
|
+
/** Default reconnect grace for a person who holds the lease (env PF_BROWSER_GRACE_MS). */
|
|
41
|
+
export const BROWSER_RECONNECT_GRACE_MS = 60_000;
|
|
42
|
+
|
|
43
|
+
/** 20x's cap; a recording that reaches it is interrupted with a gap. */
|
|
44
|
+
export const BROWSER_RECORDING_STEP_CAP = 10_000;
|
|
45
|
+
/** Per-recording byte cap across all files. */
|
|
46
|
+
export const BROWSER_RECORDING_BYTE_CAP = 64 * 1024 * 1024;
|
|
47
|
+
/** Per-chat cap on retained recording bytes. Start is refused above it. */
|
|
48
|
+
export const BROWSER_CHAT_RECORDING_BYTE_CAP = 512 * 1024 * 1024;
|
|
49
|
+
export const BROWSER_RECORDING_ID = /^[a-zA-Z0-9_-]{1,100}$/;
|
|
50
|
+
export const BROWSER_SNAPSHOT_ID = /^s[1-9][0-9]{0,5}$/;
|
|
51
|
+
|
|
52
|
+
/** Largest upload part per HTTP request (the API splits; the broker only counts parts). */
|
|
53
|
+
export const BROWSER_UPLOAD_PART_BYTES = 16 * 1024 * 1024;
|
|
54
|
+
/** The only sandbox paths the API will read or write through the Daytona toolbox. */
|
|
55
|
+
export const BROWSER_SANDBOX_TRANSFER_ROOTS = Object.freeze([
|
|
56
|
+
'/var/pf/browser-transfer/',
|
|
57
|
+
'/var/pf/browser-recordings/',
|
|
58
|
+
'/var/pf/browsers/'
|
|
59
|
+
]);
|
|
60
|
+
export const BROWSER_SANDBOX_ASLEEP_CODE = 'sandbox_asleep';
|
|
61
|
+
|
|
62
|
+
/** BrowserBrokerError codes (core `BrowserBrokerError['code']`). */
|
|
63
|
+
export const BROKER_ERROR_CODES = Object.freeze([
|
|
64
|
+
'stale_lease',
|
|
65
|
+
'stale_sequence',
|
|
66
|
+
'takeover_active',
|
|
67
|
+
'not_controller',
|
|
68
|
+
'no_browser',
|
|
69
|
+
'browser_limit',
|
|
70
|
+
'stale_ref',
|
|
71
|
+
'invalid_args',
|
|
72
|
+
'timeout',
|
|
73
|
+
'navigation_failed',
|
|
74
|
+
'recording_active',
|
|
75
|
+
'not_recording',
|
|
76
|
+
'not_found',
|
|
77
|
+
'too_large',
|
|
78
|
+
'storage_full',
|
|
79
|
+
// v3, fill_credential
|
|
80
|
+
'origin_not_allowed',
|
|
81
|
+
'origin_changed',
|
|
82
|
+
'element_not_allowed',
|
|
83
|
+
'credential_denied',
|
|
84
|
+
'internal'
|
|
85
|
+
]);
|
|
86
|
+
|
|
87
|
+
/** BrowserCommandName (core). */
|
|
88
|
+
export const BROWSER_COMMAND_NAMES = Object.freeze([
|
|
89
|
+
'preview_local',
|
|
90
|
+
'navigate',
|
|
91
|
+
'snapshot',
|
|
92
|
+
'click',
|
|
93
|
+
'type',
|
|
94
|
+
'fill_credential',
|
|
95
|
+
'press_key',
|
|
96
|
+
'scroll',
|
|
97
|
+
'get',
|
|
98
|
+
'wait',
|
|
99
|
+
'screenshot',
|
|
100
|
+
'back',
|
|
101
|
+
'forward',
|
|
102
|
+
'reload'
|
|
103
|
+
]);
|
|
104
|
+
|
|
105
|
+
/** BrowserControlOp (core). */
|
|
106
|
+
export const BROWSER_CONTROL_OPS = Object.freeze([
|
|
107
|
+
'connect',
|
|
108
|
+
'disconnect',
|
|
109
|
+
'take',
|
|
110
|
+
'hand_back',
|
|
111
|
+
'grant',
|
|
112
|
+
'deny',
|
|
113
|
+
'revoke'
|
|
114
|
+
]);
|
|
115
|
+
|
|
116
|
+
/** BrowserRecordingReadOp (core). */
|
|
117
|
+
export const BROWSER_RECORDING_READ_OPS = Object.freeze([
|
|
118
|
+
'list',
|
|
119
|
+
'get',
|
|
120
|
+
'steps',
|
|
121
|
+
'snapshot'
|
|
122
|
+
]);
|
|
123
|
+
|
|
124
|
+
/** BrowserBrokerRequest['t'] and BrowserBrokerPush['t'] (core). */
|
|
125
|
+
export const BROWSER_BROKER_REQUESTS = Object.freeze([
|
|
126
|
+
'open',
|
|
127
|
+
'close',
|
|
128
|
+
'state',
|
|
129
|
+
'control',
|
|
130
|
+
'command',
|
|
131
|
+
'recording_start',
|
|
132
|
+
'recording_stop',
|
|
133
|
+
'recording_read',
|
|
134
|
+
'recording_files',
|
|
135
|
+
'download_list',
|
|
136
|
+
'download_path',
|
|
137
|
+
'upload_begin',
|
|
138
|
+
'upload_commit',
|
|
139
|
+
'file_chooser_cancel',
|
|
140
|
+
'cred_revoke'
|
|
141
|
+
]);
|
|
142
|
+
export const BROWSER_BROKER_PUSHES = Object.freeze([
|
|
143
|
+
'input',
|
|
144
|
+
'screencast',
|
|
145
|
+
'ping',
|
|
146
|
+
'cred_result'
|
|
147
|
+
]);
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* `close` request fields besides t/id/sessionId (core). `keepRecordings`
|
|
151
|
+
* (optional, default false) only matters with `wipe`: archive/delete of a chat
|
|
152
|
+
* wipes the profile but keeps its recordings; "clear browser" deletes them.
|
|
153
|
+
*/
|
|
154
|
+
export const BROWSER_CLOSE_REQUEST_FIELDS = Object.freeze([
|
|
155
|
+
'wipe',
|
|
156
|
+
'keepRecordings'
|
|
157
|
+
]);
|
|
158
|
+
|
|
159
|
+
/** BrowserBrokerEventKind (core). */
|
|
160
|
+
export const BROWSER_BROKER_EVENT_KINDS = Object.freeze([
|
|
161
|
+
'state',
|
|
162
|
+
'closed',
|
|
163
|
+
'crashed',
|
|
164
|
+
'input_rejected',
|
|
165
|
+
'file_chooser',
|
|
166
|
+
'download',
|
|
167
|
+
'notice',
|
|
168
|
+
'recording_finished'
|
|
169
|
+
]);
|
|
170
|
+
|
|
171
|
+
/* -------------------------------------------------------------------------- */
|
|
172
|
+
/* Recording gap strings */
|
|
173
|
+
/* -------------------------------------------------------------------------- */
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* The two gaps every new recording starts with. The first is 20x verbatim.
|
|
177
|
+
* The second is 20x's with ONE deliberate change: 20x says snapshots contain
|
|
178
|
+
* "ordinary values"; here typed text is always recorded as [redacted].
|
|
179
|
+
*/
|
|
180
|
+
export const RECORDING_INITIAL_GAPS = Object.freeze([
|
|
181
|
+
'Only the main document is captured. Frames, native dialogs, popups, closed shadow roots and events before document capture starts are not captured.',
|
|
182
|
+
'Sensitive input values, page titles and URL query strings are omitted. Secret detection uses field metadata; unnamed sensitive fields may not be detected. Snapshots contain visible element structure, labels, bounded page text, checkbox state and selected options; typed text is recorded as [redacted]; no screenshots. Human snapshots use the page state before its event handler runs. Agent interaction snapshots follow the command; navigation intent snapshots precede navigation. The final snapshot shows the page state when Stop is selected.'
|
|
183
|
+
]);
|
|
184
|
+
|
|
185
|
+
export const GAP = Object.freeze({
|
|
186
|
+
/** 20x */
|
|
187
|
+
stepCap: 'Capture limit reached: 10000 steps.',
|
|
188
|
+
/** 20x */
|
|
189
|
+
appStopped: 'Application stopped before recording was saved.',
|
|
190
|
+
/** spec: per-recording or per-chat byte cap */
|
|
191
|
+
storageCap: 'Capture limit reached: storage cap.',
|
|
192
|
+
/** 20x */
|
|
193
|
+
flushUnconfirmed: 'Pending input flush could not be confirmed before Stop.',
|
|
194
|
+
/** 20x */
|
|
195
|
+
finalUnavailable:
|
|
196
|
+
'The final snapshot was unavailable because the browser was closed.',
|
|
197
|
+
/** 20x */
|
|
198
|
+
browserClosed:
|
|
199
|
+
'Browser closed before Stop. Pending snapshots may be missing.',
|
|
200
|
+
/** 20x */
|
|
201
|
+
writeFailed: 'Recording stopped because capture data could not be written.',
|
|
202
|
+
/** 20x */
|
|
203
|
+
captureStartFailed: 'Document event capture could not start.',
|
|
204
|
+
/** 20x */
|
|
205
|
+
snapshotTimeout: 'One or more snapshots timed out.',
|
|
206
|
+
/** 20x */
|
|
207
|
+
snapshotUnavailable: 'One or more snapshots were unavailable.',
|
|
208
|
+
/** 20x */
|
|
209
|
+
eventUnsaved: 'One or more page events could not be saved.'
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
/* -------------------------------------------------------------------------- */
|
|
213
|
+
/* 20x agent-tool error strings */
|
|
214
|
+
/* -------------------------------------------------------------------------- */
|
|
215
|
+
|
|
216
|
+
export const TOOL_ERRORS = Object.freeze({
|
|
217
|
+
staleTarget: (target) =>
|
|
218
|
+
`Stale or unknown target "${String(target).replace(/"/g, '')}" — run browser_snapshot again.`,
|
|
219
|
+
staleTargetShort: 'Stale or unknown target — run browser_snapshot again.',
|
|
220
|
+
notEditable: (target, role) =>
|
|
221
|
+
`Target "${String(target).replace(/"/g, '')}" is not an editable element (role: ${role}). Pick an input/textarea ref from browser_snapshot.`,
|
|
222
|
+
unsupportedKey: (key) =>
|
|
223
|
+
`Unsupported key ${JSON.stringify(key)} — use Enter, Tab, Escape, Backspace, Delete, Space, ArrowUp/Down/Left/Right, Home, End, PageUp, PageDown or a single character.`,
|
|
224
|
+
badGet: 'what must be "url", "title" or "text"',
|
|
225
|
+
waitTimeout: (ms, mode, value) =>
|
|
226
|
+
`Timed out after ${ms}ms waiting for ${mode}: ${
|
|
227
|
+
mode === 'url' ? value : String(value).replace(/[\\"]|<\/script/g, '')
|
|
228
|
+
}`,
|
|
229
|
+
evalTimeout:
|
|
230
|
+
'Timed out after 30s — the page did not answer (it may be navigating or throttled in the background).',
|
|
231
|
+
navigationFailed: (message) => `Navigation failed: ${message}`,
|
|
232
|
+
screenshotFailed: (message) => `Screenshot failed: ${message}`,
|
|
233
|
+
malformedSnapshot: 'Malformed snapshot payload',
|
|
234
|
+
alreadyRecording: 'This browser is already recording',
|
|
235
|
+
notRecording: 'This browser is not recording'
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
/* -------------------------------------------------------------------------- */
|
|
239
|
+
/* Helpers identical to core */
|
|
240
|
+
/* -------------------------------------------------------------------------- */
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Keep origin + path only; drop credentials, query and fragment (20x rule).
|
|
244
|
+
* Path segments that look like secrets (reset-password and magic-link
|
|
245
|
+
* tokens, JWTs, long hex or base64) become [redacted]: a long opaque run of
|
|
246
|
+
* [A-Za-z0-9_-+/=] (24+ chars, so long hex and base64 too) or a JWT-shaped
|
|
247
|
+
* a.b.c of base64url parts (24+ chars). Word slugs (lower-case words joined
|
|
248
|
+
* by - or _) are kept.
|
|
249
|
+
*/
|
|
250
|
+
export function recordingUrl(value) {
|
|
251
|
+
try {
|
|
252
|
+
const url = new URL(value);
|
|
253
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
|
|
254
|
+
return '[non-http page]';
|
|
255
|
+
}
|
|
256
|
+
const path = url.pathname
|
|
257
|
+
.split('/')
|
|
258
|
+
.map((segment) => {
|
|
259
|
+
let text = segment;
|
|
260
|
+
try {
|
|
261
|
+
text = decodeURIComponent(segment);
|
|
262
|
+
} catch {
|
|
263
|
+
/* keep the raw segment */
|
|
264
|
+
}
|
|
265
|
+
if (/^[a-z]+(?:[-_][a-z]+)+$/.test(text)) return segment;
|
|
266
|
+
if (/^[A-Za-z0-9+/=_-]{24,}$/.test(text)) return '[redacted]';
|
|
267
|
+
if (
|
|
268
|
+
text.length >= 24 &&
|
|
269
|
+
/^[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]{4,}$/.test(text)
|
|
270
|
+
) {
|
|
271
|
+
return '[redacted]';
|
|
272
|
+
}
|
|
273
|
+
return segment;
|
|
274
|
+
})
|
|
275
|
+
.join('/');
|
|
276
|
+
return url.origin + path;
|
|
277
|
+
} catch {
|
|
278
|
+
return '[unknown page]';
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** uint32 big-endian N | N bytes UTF-8 JSON header | JPEG bytes. */
|
|
283
|
+
export function encodeBrowserFrame(header, jpeg) {
|
|
284
|
+
const json = Buffer.from(JSON.stringify(header), 'utf8');
|
|
285
|
+
const out = Buffer.allocUnsafe(4 + json.length + jpeg.length);
|
|
286
|
+
out.writeUInt32BE(json.length, 0);
|
|
287
|
+
json.copy(out, 4);
|
|
288
|
+
Buffer.from(jpeg.buffer, jpeg.byteOffset, jpeg.byteLength).copy(
|
|
289
|
+
out,
|
|
290
|
+
4 + json.length
|
|
291
|
+
);
|
|
292
|
+
return out;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export function decodeBrowserFrame(data) {
|
|
296
|
+
if (data.length < 4) return null;
|
|
297
|
+
const length = data.readUInt32BE(0);
|
|
298
|
+
if (length <= 0 || length > 4096 || 4 + length > data.length) return null;
|
|
299
|
+
try {
|
|
300
|
+
const header = JSON.parse(data.subarray(4, 4 + length).toString('utf8'));
|
|
301
|
+
if (typeof header?.seq !== 'number') return null;
|
|
302
|
+
return { header, jpeg: data.subarray(4 + length) };
|
|
303
|
+
} catch {
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** A failure the API receives as `{ ok: false, error: { code, message } }`. */
|
|
309
|
+
export class BrokerError extends Error {
|
|
310
|
+
/** `state` (BrowserCoreState) rides along with lease errors. */
|
|
311
|
+
constructor(code, message, state) {
|
|
312
|
+
super(message);
|
|
313
|
+
this.code = BROKER_ERROR_CODES.includes(code) ? code : 'internal';
|
|
314
|
+
this.state = state ?? undefined;
|
|
315
|
+
}
|
|
316
|
+
toJSON() {
|
|
317
|
+
return this.state
|
|
318
|
+
? { code: this.code, message: this.message, state: this.state }
|
|
319
|
+
: { code: this.code, message: this.message };
|
|
320
|
+
}
|
|
321
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One serial queue per browser. Commands, lease changes, recording start/stop
|
|
3
|
+
* and file-chooser sets for a browser run here one at a time, in arrival
|
|
4
|
+
* order. A `lease` message therefore waits for the in-flight command: the
|
|
5
|
+
* takeover lands on a command boundary, never in the middle of a click.
|
|
6
|
+
*
|
|
7
|
+
* Person input does NOT go through this queue (it must not wait behind a 60 s
|
|
8
|
+
* agent `browser_wait`); it is fenced against the lease the queue last applied.
|
|
9
|
+
*/
|
|
10
|
+
export class SerialQueue {
|
|
11
|
+
#tail = Promise.resolve();
|
|
12
|
+
#pending = 0;
|
|
13
|
+
|
|
14
|
+
/** Run `job` after every earlier job settles. Resolves/rejects with it. */
|
|
15
|
+
run(job) {
|
|
16
|
+
this.#pending++;
|
|
17
|
+
const result = this.#tail.then(() => job());
|
|
18
|
+
this.#tail = result
|
|
19
|
+
.then(
|
|
20
|
+
() => undefined,
|
|
21
|
+
() => undefined
|
|
22
|
+
)
|
|
23
|
+
.finally(() => {
|
|
24
|
+
this.#pending--;
|
|
25
|
+
});
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
get pending() {
|
|
30
|
+
return this.#pending;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Resolves once everything queued so far has settled. */
|
|
34
|
+
idle() {
|
|
35
|
+
return this.#tail;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Page scripts for step recording. A port of 20x
|
|
3
|
+
* src/main/browser-recording-scripts.ts, kept as close to verbatim as the
|
|
4
|
+
* deliberate differences allow:
|
|
5
|
+
*
|
|
6
|
+
* 1. They run in a CDP ISOLATED WORLD named `pf-recorder` (installed with
|
|
7
|
+
* Page.addScriptToEvaluateOnNewDocument({ worldName, runImmediately }) and
|
|
8
|
+
* Page.createIsolatedWorld for the current document) and report through
|
|
9
|
+
* Runtime.addBinding('__pfRecord', executionContextName: 'pf-recorder')
|
|
10
|
+
* instead of console messages. The page's own scripts cannot see the
|
|
11
|
+
* binding or the listeners. The nonce check is kept.
|
|
12
|
+
* 2. TYPED TEXT IS ALWAYS REDACTED. The value of a text input, textarea or
|
|
13
|
+
* contenteditable is '[redacted]', whatever its metadata says. Checkbox
|
|
14
|
+
* and radio `checked` state and <select> values are kept (through
|
|
15
|
+
* safeText). Text typed into a contenteditable region is also cut out of
|
|
16
|
+
* the bounded page text.
|
|
17
|
+
* 3. Still no setAttribute anywhere: the recorder never touches the
|
|
18
|
+
* `data-bx-ref` attributes the agent's snapshot assigns.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export const RECORDING_WORLD_NAME = 'pf-recorder';
|
|
22
|
+
export const RECORDING_BINDING = '__pfRecord';
|
|
23
|
+
export const RECORDING_PREFIX = '__PF_RECORDING__';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Field metadata (type, name, id, autocomplete, aria-label, placeholder) that
|
|
27
|
+
* marks a secret. Shared by the recorder scripts, sanitizeRecordingTarget and
|
|
28
|
+
* the agent snapshot script (agent-actions.mjs). 20x's list plus card / bank
|
|
29
|
+
* fields: autocomplete cc-* (cc-number, cc-csc, cc-exp …), csc, iban, pwd,
|
|
30
|
+
* passcode, account number.
|
|
31
|
+
*
|
|
32
|
+
* Long, unambiguous words match anywhere (newpassword, csrftoken). Short
|
|
33
|
+
* words that hide inside ordinary ones (shipping / spinner -> pin, author ->
|
|
34
|
+
* auth, discard -> card, classname -> ssn, footprint -> otp) must stand alone
|
|
35
|
+
* as a word: no letter on either side, AFTER normalizeFieldText has split
|
|
36
|
+
* camelCase and snake_case (authToken -> "auth Token", pin_code -> "pin code").
|
|
37
|
+
* Use isSensitiveFieldText; the page scripts inline the same two steps.
|
|
38
|
+
*/
|
|
39
|
+
export const SENSITIVE_FIELD_RE =
|
|
40
|
+
/password|passwd|passphrase|passcode|secret|token|credit|cvv|cvc|one.?time|verification|iban|pwd|account.?number|(?<![a-z])(?:auth(?:entication|orization|code)?|pins?|pin.?code|cards?|card.?(?:num|number|no|holder)|cardnum(?:ber)?|otp(?:code)?|ssn|csc)(?![a-z])|(?<![a-z])cc-/i;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Split camelCase and snake_case into words so SENSITIVE_FIELD_RE's word
|
|
44
|
+
* boundaries see them. Self-contained: it is inlined into page scripts.
|
|
45
|
+
*/
|
|
46
|
+
export function normalizeFieldText(text) {
|
|
47
|
+
return String(text)
|
|
48
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
|
|
49
|
+
.replace(/_/g, ' ');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** True when field metadata / a selector names a secret. */
|
|
53
|
+
export function isSensitiveFieldText(text) {
|
|
54
|
+
return SENSITIVE_FIELD_RE.test(normalizeFieldText(text));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const helpers = `
|
|
58
|
+
const interactiveSelector = 'a,button,input,select,textarea,[role],summary,[contenteditable]';
|
|
59
|
+
const cleanUrl = () => location.origin + location.pathname;
|
|
60
|
+
const sensitive = el => ${SENSITIVE_FIELD_RE}.test((${normalizeFieldText})([el.type, el.name, el.id, el.autocomplete, el.getAttribute('aria-label'), el.getAttribute('placeholder')].join(' ')));
|
|
61
|
+
const safeText = (value, limit = 120) => String(value || '').replace(/(?:Bearer\\s+)?[a-zA-Z0-9_-]{32,}/g, '[redacted]').slice(0, limit);
|
|
62
|
+
const keptValueTypes = ['checkbox', 'radio', 'submit', 'button', 'reset', 'image'];
|
|
63
|
+
const keepsValue = el => el.tagName === 'SELECT' || (el.tagName === 'INPUT' && keptValueTypes.includes(el.type));
|
|
64
|
+
const visible = el => { const r = el.getBoundingClientRect(); const s = getComputedStyle(el); return r.width >= 2 && r.height >= 2 && s.visibility !== 'hidden' && s.display !== 'none' && Number(s.opacity) !== 0; };
|
|
65
|
+
const locator = el => {
|
|
66
|
+
const tag = el.tagName.toLowerCase();
|
|
67
|
+
if (sensitive(el)) return tag + (el.type ? '[type="' + CSS.escape(el.type) + '"]' : '');
|
|
68
|
+
if (el.id) return '#' + CSS.escape(el.id);
|
|
69
|
+
const testId = el.getAttribute('data-testid');
|
|
70
|
+
if (testId) return '[data-testid="' + CSS.escape(testId) + '"]';
|
|
71
|
+
if (el.name) return tag + '[name="' + CSS.escape(el.name) + '"]';
|
|
72
|
+
const sameTag = el.parentElement ? Array.from(el.parentElement.children).filter(node => node.tagName === el.tagName) : [];
|
|
73
|
+
return tag + (sameTag.length > 1 ? ':nth-of-type(' + (sameTag.indexOf(el) + 1) + ')' : '');
|
|
74
|
+
};
|
|
75
|
+
const describe = (el, knownIndex) => {
|
|
76
|
+
const editable = ['INPUT','TEXTAREA','SELECT'].includes(el.tagName) || el.isContentEditable;
|
|
77
|
+
const secret = sensitive(el);
|
|
78
|
+
const label = el.getAttribute('aria-label') || (el.labels && el.labels[0]?.textContent) || el.getAttribute('placeholder') || (!editable ? el.textContent : '') || '';
|
|
79
|
+
const index = knownIndex || Array.from(document.querySelectorAll(interactiveSelector)).filter(visible).indexOf(el) + 1;
|
|
80
|
+
return { tag: el.tagName.toLowerCase(), role: el.getAttribute('role') || '',
|
|
81
|
+
name: secret ? '[sensitive field]' : safeText(label), inputType: el.tagName === 'INPUT' ? el.type : undefined,
|
|
82
|
+
value: editable ? (secret || el.isContentEditable || !keepsValue(el) ? '[redacted]' : safeText(el.value)) : undefined,
|
|
83
|
+
checked: el.type === 'checkbox' || el.type === 'radio' ? !!el.checked : undefined,
|
|
84
|
+
index: index > 0 ? index : undefined, locator: safeText(locator(el), 160) };
|
|
85
|
+
};
|
|
86
|
+
const targetText = target => (target.index ? '#' + target.index + ' ' : '') + target.locator + ' | ' + [target.tag, target.role, target.name].filter(Boolean).join(':');
|
|
87
|
+
const snapshot = () => {
|
|
88
|
+
const elements = [];
|
|
89
|
+
const visibleElements = Array.from(document.querySelectorAll(interactiveSelector)).filter(visible);
|
|
90
|
+
for (const el of visibleElements.slice(0, 120)) {
|
|
91
|
+
elements.push(describe(el, elements.length + 1));
|
|
92
|
+
}
|
|
93
|
+
let text = (document.body?.innerText || '').slice(0, 10000);
|
|
94
|
+
for (const el of document.querySelectorAll('input,textarea,[contenteditable]')) {
|
|
95
|
+
const typed = el.isContentEditable ? (el.innerText || '').trim() : (keepsValue(el) ? '' : String(el.value || ''));
|
|
96
|
+
if (typed && (sensitive(el) || el.isContentEditable || typed.length >= 4)) text = text.split(typed).join('[redacted]');
|
|
97
|
+
}
|
|
98
|
+
return { url: cleanUrl(), elements, truncated: visibleElements.length > 120 || text.length > 4000, text: safeText(text, 4000) };
|
|
99
|
+
};`;
|
|
100
|
+
|
|
101
|
+
export function buildRecordingSnapshotScript() {
|
|
102
|
+
return `(() => { ${helpers} return snapshot(); })()`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Installs the document listeners. `globalThis` here is the isolated world's
|
|
107
|
+
* own global, so `__recordingCleanup` and the binding are invisible to the
|
|
108
|
+
* page.
|
|
109
|
+
*/
|
|
110
|
+
export function buildRecordingInstallScript(nonce) {
|
|
111
|
+
return `(() => {
|
|
112
|
+
if (globalThis.__recordingCleanup) globalThis.__recordingCleanup();
|
|
113
|
+
${helpers}
|
|
114
|
+
const binding = globalThis[${JSON.stringify(RECORDING_BINDING)}];
|
|
115
|
+
if (typeof binding !== 'function') return false;
|
|
116
|
+
const emit = message => binding(message);
|
|
117
|
+
let pendingInput = null;
|
|
118
|
+
let inputTimer;
|
|
119
|
+
let lastScroll = 0;
|
|
120
|
+
const send = payload => emit(${JSON.stringify(RECORDING_PREFIX + nonce + ':')} + JSON.stringify(payload));
|
|
121
|
+
const flush = () => { clearTimeout(inputTimer); if (pendingInput) { send(pendingInput); pendingInput = null; } };
|
|
122
|
+
const handler = event => {
|
|
123
|
+
if (!event.isTrusted) return;
|
|
124
|
+
if (event.type === 'keydown' && !['Enter','Tab','Escape','ArrowUp','ArrowDown','ArrowLeft','ArrowRight'].includes(event.key)) return;
|
|
125
|
+
const el = event.target instanceof Element ? event.target.closest('a,button,input,select,textarea,[role],summary,[contenteditable]') || event.target : null;
|
|
126
|
+
const target = el ? describe(el) : null;
|
|
127
|
+
const payload = { action: event.type === 'keydown' ? 'keydown:' + event.key : event.type,
|
|
128
|
+
target: target ? targetText(target) : '', snapshot: snapshot() };
|
|
129
|
+
if (event.type === 'scroll') { if (Date.now() - lastScroll < 250) return; lastScroll = Date.now(); payload.target = 'x=' + scrollX + ',y=' + scrollY; }
|
|
130
|
+
if (event.type === 'input') { pendingInput = payload; clearTimeout(inputTimer); inputTimer = setTimeout(flush, 200); return; }
|
|
131
|
+
flush();
|
|
132
|
+
send(payload);
|
|
133
|
+
};
|
|
134
|
+
const events = ['click', 'input', 'change', 'submit', 'keydown', 'scroll'];
|
|
135
|
+
for (const type of events) document.addEventListener(type, handler, true);
|
|
136
|
+
window.addEventListener('pagehide', flush, true);
|
|
137
|
+
globalThis.__recordingCleanup = () => { flush(); send({ action: 'recording-flush-complete' }); window.removeEventListener('pagehide', flush, true); for (const type of events) document.removeEventListener(type, handler, true); delete globalThis.__recordingCleanup; };
|
|
138
|
+
return true;
|
|
139
|
+
})()`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export const RECORDING_REMOVE_SCRIPT = `(() => {
|
|
143
|
+
if (typeof globalThis.__recordingCleanup !== 'function') return false;
|
|
144
|
+
globalThis.__recordingCleanup();
|
|
145
|
+
return true;
|
|
146
|
+
})()`;
|
|
147
|
+
|
|
148
|
+
export function buildRecordingTargetScript(selector) {
|
|
149
|
+
return `(() => { ${helpers} let el = null; try { el = document.querySelector(${JSON.stringify(selector)}); } catch {} if (!el) return '[unknown target]'; return targetText(describe(el)); })()`;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** 20x: the human actions the broker accepts from the page. */
|
|
153
|
+
export const HUMAN_ACTION =
|
|
154
|
+
/^(click|input|change|submit|scroll|keydown:(Enter|Tab|Escape|ArrowUp|ArrowDown|ArrowLeft|ArrowRight))$/;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Validate and bound one binding payload (20x receiveRecordingMessage).
|
|
158
|
+
* Returns { flush: true }, { step, snapshot } or null when invalid.
|
|
159
|
+
*/
|
|
160
|
+
export function parseRecordingMessage(message, nonce) {
|
|
161
|
+
const prefix = RECORDING_PREFIX + nonce + ':';
|
|
162
|
+
if (typeof message !== 'string' || !message.startsWith(prefix))
|
|
163
|
+
return { foreign: true };
|
|
164
|
+
if (message.length > 50000) return null;
|
|
165
|
+
let payload;
|
|
166
|
+
try {
|
|
167
|
+
payload = JSON.parse(message.slice(prefix.length));
|
|
168
|
+
} catch {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
if (payload?.action === 'recording-flush-complete') return { flush: true };
|
|
172
|
+
if (
|
|
173
|
+
!(
|
|
174
|
+
typeof payload?.action === 'string' && HUMAN_ACTION.test(payload.action)
|
|
175
|
+
) ||
|
|
176
|
+
!Array.isArray(payload.snapshot?.elements)
|
|
177
|
+
) {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
const snapshot = {
|
|
181
|
+
url: String(payload.snapshot.url || ''),
|
|
182
|
+
text: String(payload.snapshot.text || '').slice(0, 4000),
|
|
183
|
+
truncated: !!payload.snapshot.truncated,
|
|
184
|
+
elements: payload.snapshot.elements.slice(0, 120).map((e) => ({
|
|
185
|
+
tag: String(e?.tag || '').slice(0, 30),
|
|
186
|
+
role: String(e?.role || '').slice(0, 30),
|
|
187
|
+
name: String(e?.name || '').slice(0, 120),
|
|
188
|
+
index:
|
|
189
|
+
typeof e?.index === 'number' && Number.isInteger(e.index) && e.index > 0
|
|
190
|
+
? e.index
|
|
191
|
+
: undefined,
|
|
192
|
+
locator:
|
|
193
|
+
typeof e?.locator === 'string' ? e.locator.slice(0, 160) : undefined,
|
|
194
|
+
inputType:
|
|
195
|
+
typeof e?.inputType === 'string' ? e.inputType.slice(0, 30) : undefined,
|
|
196
|
+
value: typeof e?.value === 'string' ? e.value.slice(0, 120) : undefined,
|
|
197
|
+
checked: typeof e?.checked === 'boolean' ? e.checked : undefined
|
|
198
|
+
}))
|
|
199
|
+
};
|
|
200
|
+
return {
|
|
201
|
+
step: {
|
|
202
|
+
source: 'human',
|
|
203
|
+
action: payload.action,
|
|
204
|
+
target: String(payload.target || '').slice(0, 300),
|
|
205
|
+
url: snapshot.url,
|
|
206
|
+
snapshotPhase: 'before-action'
|
|
207
|
+
},
|
|
208
|
+
snapshot
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** 20x sanitizeRecordingTarget: agent command targets in steps. */
|
|
213
|
+
export function sanitizeRecordingTarget(target) {
|
|
214
|
+
const compact = String(target).trim().replace(/\s+/g, ' ');
|
|
215
|
+
if (isSensitiveFieldText(compact)) {
|
|
216
|
+
return '[sensitive selector]';
|
|
217
|
+
}
|
|
218
|
+
if (/(?:Bearer\s+)?[a-zA-Z0-9_-]{32,}/.test(compact))
|
|
219
|
+
return '[sensitive selector]';
|
|
220
|
+
return compact.slice(0, 160);
|
|
221
|
+
}
|