webhand 0.2.0 → 0.4.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.ja.md +104 -0
- package/README.md +70 -219
- package/assets/recorder.js +4 -0
- package/assets/snapshot.js +224 -4
- package/dist/agent.js +154 -13
- package/dist/auth.js +62 -0
- package/dist/bin.js +0 -0
- package/dist/browser.js +702 -23
- package/dist/cli.js +684 -86
- package/dist/collect.js +70 -0
- package/dist/compare.js +173 -0
- package/dist/daemon.js +125 -0
- package/dist/format.js +44 -0
- package/dist/knowledge.js +206 -0
- package/dist/learn.js +11 -5
- package/dist/metrics.js +64 -4
- package/dist/more.js +58 -0
- package/dist/policies/base.js +110 -14
- package/dist/policies/jev.js +245 -18
- package/dist/policies/scripted.js +2 -2
- package/dist/remote.js +165 -0
- package/dist/runbook.js +34 -19
- package/dist/session.js +261 -17
- package/dist/types.js +12 -0
- package/package.json +9 -6
- package/dist/py.js +0 -141
- package/dist/run.js +0 -80
- package/dist/tasks.js +0 -37
package/dist/policies/jev.js
CHANGED
|
@@ -4,19 +4,33 @@
|
|
|
4
4
|
* Adapted from jev-ultrafast (MIT); see THIRD_PARTY_LICENSE_jev-ultrafast.txt.
|
|
5
5
|
*/
|
|
6
6
|
import { Agent, fetch as undiciFetch } from "undici";
|
|
7
|
-
import {
|
|
7
|
+
import { MATCH, NO } from "../collect.js";
|
|
8
|
+
import { describeBest, KEEP, NONE } from "../compare.js";
|
|
9
|
+
import { sleep, toJson } from "../format.js";
|
|
8
10
|
import { Decision, modelCall, PolicyError, } from "../types.js";
|
|
9
|
-
import { actionSpace, modelState, NEXT_ACTION, NOT_VISIBLE, NOT_VISIBLE_HELP, OPERATION_HELP, planText, resolve, TARGET, TERMINAL_HELP, VALUE, valueChoices, } from "./base.js";
|
|
11
|
+
import { actionSpace, COLLECT_RULES, COMPARE_RULES, COMPARED_RULES, ESCALATE, ESCALATE_HELP, ESCALATE_RULES, INSTRUCTION_RULES, jevGoal, MORE_RULES, modelState, NEXT_ACTION, NEXT_ACTION_WHOLE, NOT_VISIBLE, NOT_VISIBLE_HELP, OPERATION_HELP, OPERATIONS, planText, READ_RULES, resolve, TAB_RULES, TARGET, TERMINAL_HELP, VALUE, valueChoices, } from "./base.js";
|
|
10
12
|
const URL = "https://api.typesafe.ai/v1/systemone";
|
|
11
13
|
const TIMEOUT_MS = 25_000;
|
|
14
|
+
/** The start of Jev's error body, for the run log. */
|
|
15
|
+
const ERROR_CHARS = 500;
|
|
16
|
+
async function errorBody(response) {
|
|
17
|
+
try {
|
|
18
|
+
const body = await response.json();
|
|
19
|
+
return (typeof body === "string" ? body : JSON.stringify(body)).slice(0, ERROR_CHARS);
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
12
25
|
async function postJson(fetchJson, key, body) {
|
|
26
|
+
const request = toJson(body);
|
|
13
27
|
for (let attempt = 0; attempt < 3; attempt++) {
|
|
14
28
|
let response;
|
|
15
29
|
try {
|
|
16
30
|
response = await fetchJson(URL, {
|
|
17
31
|
method: "POST",
|
|
18
32
|
headers: { Authorization: `Bearer ${key}`, "Content-Type": "application/json" },
|
|
19
|
-
body:
|
|
33
|
+
body: request,
|
|
20
34
|
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
21
35
|
});
|
|
22
36
|
}
|
|
@@ -27,8 +41,13 @@ async function postJson(fetchJson, key, body) {
|
|
|
27
41
|
await sleep(500 * 2 ** attempt);
|
|
28
42
|
continue;
|
|
29
43
|
}
|
|
30
|
-
if (response.status >= 400)
|
|
31
|
-
throw new PolicyError(`Jev returned HTTP ${response.status}; no action executed
|
|
44
|
+
if (response.status >= 400) {
|
|
45
|
+
throw new PolicyError(`Jev returned HTTP ${response.status}; no action executed.`, {
|
|
46
|
+
http_status: response.status,
|
|
47
|
+
request_chars: request.length,
|
|
48
|
+
error: await errorBody(response),
|
|
49
|
+
});
|
|
50
|
+
}
|
|
32
51
|
try {
|
|
33
52
|
return await response.json();
|
|
34
53
|
}
|
|
@@ -66,13 +85,91 @@ export function validateChoice(answer, ids) {
|
|
|
66
85
|
return answer;
|
|
67
86
|
}
|
|
68
87
|
const NO_VALUE = "NONE";
|
|
88
|
+
// Jev refuses a request over its token limit (HTTP 400 max_tokens_exceeded) and a question with more than 255
|
|
89
|
+
// choices (HTTP 422), found 2026-11-13. The estimate below read lower than Jev's count on real pages, by up to 1.5
|
|
90
|
+
// times, and the limit sits at about 24k estimated tokens: in round ff, all 320 requests that went through held
|
|
91
|
+
// 23,876 or fewer, and all 8 refused held 24,987 or more (rounds fc to ff showed that 40k, the first budget, was
|
|
92
|
+
// refused again and again, and the budget lowered by a refusal stayed lowered for the run). A refusal still lowers
|
|
93
|
+
// the budget and the request is fitted again; nothing runs in the browser in between.
|
|
94
|
+
const TOKEN_BUDGET = 24_000;
|
|
95
|
+
const MAX_CHOICES = 255;
|
|
96
|
+
const OVER_LIMIT = "max_tokens_exceeded";
|
|
97
|
+
const FIT_TRIES = 3;
|
|
98
|
+
const SHRINK = 0.6;
|
|
99
|
+
/** A rough token count: about 3 characters a token for ASCII, 1.5 tokens for each other character (such as CJK). */
|
|
100
|
+
export function estimateTokens(text) {
|
|
101
|
+
let ascii = 0;
|
|
102
|
+
for (let i = 0; i < text.length; i++)
|
|
103
|
+
if (text.charCodeAt(i) < 128)
|
|
104
|
+
ascii++;
|
|
105
|
+
return Math.ceil(ascii / 3 + (text.length - ascii) * 1.5);
|
|
106
|
+
}
|
|
107
|
+
/** The number of choices over all questions of a request. */
|
|
108
|
+
function choiceCount(questions) {
|
|
109
|
+
let total = 0;
|
|
110
|
+
for (const question of Object.values(questions)) {
|
|
111
|
+
const criteria = question.criteria;
|
|
112
|
+
total += criteria instanceof Map ? criteria.size : Object.keys(criteria).length;
|
|
113
|
+
}
|
|
114
|
+
return total;
|
|
115
|
+
}
|
|
116
|
+
/** What a request holds, as counts: for the run log, so a refusal can be set beside the requests that went through. */
|
|
117
|
+
function requestSize(fitted) {
|
|
118
|
+
const chars = toJson(fitted.body);
|
|
119
|
+
return {
|
|
120
|
+
estimated_tokens: estimateTokens(chars),
|
|
121
|
+
request_chars: chars.length,
|
|
122
|
+
choices: choiceCount(fitted.questions),
|
|
123
|
+
elements: fitted.targets ? [...fitted.targets.values()].reduce((n, m) => n + m.size, 0) : 0,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function fits(body, questions, budget) {
|
|
127
|
+
for (const question of Object.values(questions)) {
|
|
128
|
+
const criteria = question.criteria;
|
|
129
|
+
if ((criteria instanceof Map ? criteria.size : Object.keys(criteria).length) > MAX_CHOICES)
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
return estimateTokens(toJson(body)) <= budget;
|
|
133
|
+
}
|
|
134
|
+
const pixels = (action) => Number(/(\d+)px/.exec(String(action.at ?? ""))?.[1] ?? 0);
|
|
135
|
+
/**
|
|
136
|
+
* Asks about the page as observed when the request fits Jev's limits. Otherwise elements are left out until it
|
|
137
|
+
* fits: beyond the viewport the farthest first, then those in view from the end of the page. Controls (wait,
|
|
138
|
+
* scroll, reveal) stay. Jev can still answer NOT_VISIBLE, and nothing left out can be chosen.
|
|
139
|
+
*/
|
|
140
|
+
function fitChoose(page, build, budget) {
|
|
141
|
+
let built = build(page);
|
|
142
|
+
if (fits(built.body, built.questions, budget))
|
|
143
|
+
return { ...built, page, dropped: 0 };
|
|
144
|
+
const nodes = new Map();
|
|
145
|
+
for (const action of page.actions) {
|
|
146
|
+
if (!(action.kind in OPERATIONS) || nodes.has(action.node))
|
|
147
|
+
continue;
|
|
148
|
+
nodes.set(action.node, { offscreen: Boolean(action.offscreen), distance: pixels(action), order: nodes.size });
|
|
149
|
+
}
|
|
150
|
+
const order = [...nodes].sort(([, a], [, b]) => Number(b.offscreen) - Number(a.offscreen) || b.distance - a.distance || b.order - a.order);
|
|
151
|
+
const left = new Set();
|
|
152
|
+
let asked = page;
|
|
153
|
+
while (left.size < order.length) {
|
|
154
|
+
const step = Math.max(1, Math.ceil((order.length - left.size) / 10));
|
|
155
|
+
for (const [node] of order.slice(left.size, left.size + step))
|
|
156
|
+
left.add(node);
|
|
157
|
+
asked = { ...page, actions: page.actions.filter((a) => !(a.kind in OPERATIONS && left.has(a.node))) };
|
|
158
|
+
built = build(asked);
|
|
159
|
+
if (fits(built.body, built.questions, budget))
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
return { ...built, page: asked, dropped: left.size };
|
|
163
|
+
}
|
|
69
164
|
/** Claude's plan travels with every question, not only in the state. */
|
|
70
165
|
function taskBrief(plan) {
|
|
71
|
-
const brief = { goal: plan
|
|
166
|
+
const brief = { goal: jevGoal(plan) };
|
|
72
167
|
if (plan.steps?.length)
|
|
73
168
|
brief.plan = planText(plan);
|
|
74
169
|
if (plan.notes?.length)
|
|
75
170
|
brief.notes_from_earlier_attempts = plan.notes;
|
|
171
|
+
if (plan.instructions?.length)
|
|
172
|
+
brief.claude_instructions = plan.instructions;
|
|
76
173
|
return brief;
|
|
77
174
|
}
|
|
78
175
|
/** A dropdown option is only its label: the dropdown's current value is in the state's elements. */
|
|
@@ -83,21 +180,33 @@ function targetCriterion(operation, index, action) {
|
|
|
83
180
|
element: `[${index}] ${action.label}`,
|
|
84
181
|
current_value: "current_value" in action ? action.current_value : "value" in action ? action.value : "",
|
|
85
182
|
};
|
|
86
|
-
for (const key of ["role", "checked", "selected", "expanded"])
|
|
183
|
+
for (const key of ["role", "checked", "selected", "expanded", "at"])
|
|
87
184
|
if (key in action)
|
|
88
185
|
criterion[key] = action[key];
|
|
89
186
|
return criterion;
|
|
90
187
|
}
|
|
91
|
-
export function buildQuestions(plan, targets, controls, values) {
|
|
188
|
+
export function buildQuestions(plan, targets, controls, values, reading = false, tabbed = false) {
|
|
92
189
|
const brief = taskBrief(plan);
|
|
190
|
+
const next = plan.flow === "v2" ? NEXT_ACTION_WHOLE : NEXT_ACTION;
|
|
191
|
+
let rules = reading ? `${next}\n${READ_RULES}` : next;
|
|
192
|
+
if (tabbed)
|
|
193
|
+
rules = `${rules}\n${TAB_RULES}`;
|
|
194
|
+
if (plan.compared)
|
|
195
|
+
rules = `${rules}\n${COMPARED_RULES}`;
|
|
196
|
+
if (plan.instructions?.length)
|
|
197
|
+
rules = `${rules}\n${INSTRUCTION_RULES}`;
|
|
198
|
+
if (plan.flow === "v2")
|
|
199
|
+
rules = `${rules}\n${ESCALATE_RULES}`;
|
|
93
200
|
const operations = {};
|
|
94
201
|
for (const key of targets.keys())
|
|
95
202
|
operations[key] = OPERATION_HELP[key];
|
|
96
203
|
for (const [key, value] of controls)
|
|
97
204
|
operations[key] = value.label;
|
|
98
205
|
Object.assign(operations, TERMINAL_HELP);
|
|
206
|
+
if (plan.flow === "v2")
|
|
207
|
+
operations[ESCALATE] = ESCALATE_HELP;
|
|
99
208
|
const questions = {
|
|
100
|
-
operation: { type: "choice", criteria: operations, instructions: { ...brief, rules
|
|
209
|
+
operation: { type: "choice", criteria: operations, instructions: { ...brief, rules } },
|
|
101
210
|
};
|
|
102
211
|
for (const [operation, candidates] of targets) {
|
|
103
212
|
const criteria = new Map();
|
|
@@ -107,7 +216,7 @@ export function buildQuestions(plan, targets, controls, values) {
|
|
|
107
216
|
questions[`${operation.toLowerCase()}_target`] = {
|
|
108
217
|
type: "choice",
|
|
109
218
|
criteria,
|
|
110
|
-
instructions: { ...brief, operation, rules: [
|
|
219
|
+
instructions: { ...brief, operation, rules: [rules, TARGET] },
|
|
111
220
|
};
|
|
112
221
|
}
|
|
113
222
|
if (targets.has("TYPE_TEXT")) {
|
|
@@ -123,6 +232,58 @@ export function buildQuestions(plan, targets, controls, values) {
|
|
|
123
232
|
}
|
|
124
233
|
return [questions, operations];
|
|
125
234
|
}
|
|
235
|
+
/** The comparison question: items in view, and the best one so far or NONE. */
|
|
236
|
+
export function buildCompare(plan, criterion, best, candidates) {
|
|
237
|
+
const criteria = new Map();
|
|
238
|
+
for (const item of candidates) {
|
|
239
|
+
const context = item.context && item.context !== item.action.label ? ` — ${item.context}` : "";
|
|
240
|
+
criteria.set(item.key, `[${item.key}] ${item.action.label}${context}`);
|
|
241
|
+
}
|
|
242
|
+
if (best)
|
|
243
|
+
criteria.set(KEEP, `Keep the current best: ${describeBest(best)}`);
|
|
244
|
+
else
|
|
245
|
+
criteria.set(NONE, "No listed item fits the criterion");
|
|
246
|
+
return {
|
|
247
|
+
compare: {
|
|
248
|
+
type: "choice",
|
|
249
|
+
criteria,
|
|
250
|
+
instructions: { ...taskBrief(plan), criterion, current_best: describeBest(best), rules: COMPARE_RULES },
|
|
251
|
+
},
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
/** The question that finds Claude's control for more items among the elements in view, or NONE. */
|
|
255
|
+
export function buildMore(control, candidates) {
|
|
256
|
+
const criteria = new Map();
|
|
257
|
+
for (const item of candidates) {
|
|
258
|
+
const context = item.context && item.context !== item.action.label ? ` — ${item.context}` : "";
|
|
259
|
+
criteria.set(item.key, `[${item.key}] ${item.action.label}${context}`);
|
|
260
|
+
}
|
|
261
|
+
criteria.set(NONE, "Claude's control is not listed, or it is disabled");
|
|
262
|
+
return { more: { type: "choice", criteria, instructions: { control, rules: MORE_RULES } } };
|
|
263
|
+
}
|
|
264
|
+
/** The collection questions: one per item, MATCH or NO. The plan is in the state. */
|
|
265
|
+
export function buildCollect(criterion, rows) {
|
|
266
|
+
const questions = {};
|
|
267
|
+
for (const row of rows) {
|
|
268
|
+
questions[`item_${row.key}`] = {
|
|
269
|
+
type: "choice",
|
|
270
|
+
criteria: new Map([
|
|
271
|
+
[MATCH, `It meets the criterion: ${criterion}`],
|
|
272
|
+
[NO, "It does not, its text does not show it, or it is not an item of the list"],
|
|
273
|
+
]),
|
|
274
|
+
instructions: { criterion, item: row.text, rules: COLLECT_RULES },
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
return questions;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* The comparison's state: the plan only, not the page. With the page in the state, Jev favoured an item on
|
|
281
|
+
* screen over a better best that had scrolled away. Each item's own text is in the question.
|
|
282
|
+
*/
|
|
283
|
+
export function compareState(page, plan) {
|
|
284
|
+
const { page: _page, elements: _elements, action_history: _history, ...state } = modelState(page, [], plan);
|
|
285
|
+
return state;
|
|
286
|
+
}
|
|
126
287
|
export class JevPolicy {
|
|
127
288
|
name = "jev";
|
|
128
289
|
calls = [];
|
|
@@ -131,6 +292,8 @@ export class JevPolicy {
|
|
|
131
292
|
model;
|
|
132
293
|
fetchJson;
|
|
133
294
|
agent;
|
|
295
|
+
/** Lowered for the rest of the run when Jev refuses a request over its limit. */
|
|
296
|
+
budget = TOKEN_BUDGET;
|
|
134
297
|
constructor(options = {}) {
|
|
135
298
|
const key = process.env.TYPESAFE_API_KEY;
|
|
136
299
|
if (!key)
|
|
@@ -142,7 +305,7 @@ export class JevPolicy {
|
|
|
142
305
|
this.agent = null;
|
|
143
306
|
}
|
|
144
307
|
else {
|
|
145
|
-
// HTTP/2 when the server offers it
|
|
308
|
+
// HTTP/2 when the server offers it; HTTP/1.1 otherwise.
|
|
146
309
|
const agent = new Agent({ allowH2: true });
|
|
147
310
|
this.agent = agent;
|
|
148
311
|
this.fetchJson = (url, init) => undiciFetch(url, { ...init, dispatcher: agent });
|
|
@@ -154,19 +317,83 @@ export class JevPolicy {
|
|
|
154
317
|
async close() {
|
|
155
318
|
await this.agent?.close();
|
|
156
319
|
}
|
|
157
|
-
async
|
|
158
|
-
const
|
|
159
|
-
const
|
|
160
|
-
const
|
|
161
|
-
const
|
|
320
|
+
async compare(page, plan, criterion, best, candidates) {
|
|
321
|
+
const questions = buildCompare(plan, criterion, best, candidates);
|
|
322
|
+
const body = { model: this.model, state: compareState(page, plan), questions };
|
|
323
|
+
const result = await this.post(body, "compare");
|
|
324
|
+
const keys = [...candidates.map((item) => item.key), best ? KEEP : NONE];
|
|
325
|
+
return validateChoice(result?.answers?.compare ?? {}, keys).choice;
|
|
326
|
+
}
|
|
327
|
+
async more(page, plan, control, candidates) {
|
|
328
|
+
// The page is in the state here: with the plan only, Jev was unsure whether the control was there (about
|
|
329
|
+
// 0.5); with the page, it chose the control at 0.98 or more.
|
|
330
|
+
const body = { model: this.model, state: modelState(page, [], plan), questions: buildMore(control, candidates) };
|
|
331
|
+
const result = await this.post(body, "more");
|
|
332
|
+
const keys = [...candidates.map((item) => item.key), NONE];
|
|
333
|
+
return validateChoice(result?.answers?.more ?? {}, keys).choice;
|
|
334
|
+
}
|
|
335
|
+
async collect(page, plan, criterion, rows) {
|
|
336
|
+
const body = { model: this.model, state: compareState(page, plan), questions: buildCollect(criterion, rows) };
|
|
337
|
+
const answers = (await this.post(body, "collect")).answers ?? {};
|
|
338
|
+
const keys = new Set();
|
|
339
|
+
for (const row of rows) {
|
|
340
|
+
if (validateChoice(answers[`item_${row.key}`] ?? {}, [MATCH, NO]).choice === MATCH)
|
|
341
|
+
keys.add(row.key);
|
|
342
|
+
}
|
|
343
|
+
return keys;
|
|
344
|
+
}
|
|
345
|
+
async post(body, purpose, extra = {}) {
|
|
162
346
|
const started = performance.now();
|
|
163
347
|
const result = await postJson(this.fetchJson, this.key, body);
|
|
164
348
|
const isObject = result && typeof result === "object" && !Array.isArray(result);
|
|
165
349
|
const usage = (isObject && result.usage) || {};
|
|
166
350
|
this.servedModel = isObject && "model" in result ? result.model : this.model;
|
|
167
351
|
// The response carries token counts but no billed amount.
|
|
168
|
-
this.calls.push(modelCall("typesafe", this.servedModel,
|
|
169
|
-
|
|
352
|
+
this.calls.push(modelCall("typesafe", this.servedModel, purpose, Math.round(performance.now() - started), "input_tokens" in usage ? usage.input_tokens : (usage.prompt_tokens ?? null), "output_tokens" in usage ? usage.output_tokens : (usage.completion_tokens ?? null)));
|
|
353
|
+
Object.assign(this.calls[this.calls.length - 1], extra);
|
|
354
|
+
return isObject ? result : {};
|
|
355
|
+
}
|
|
356
|
+
async choose(whole, plan, history) {
|
|
357
|
+
const values = valueChoices(plan);
|
|
358
|
+
const build = (page) => {
|
|
359
|
+
const [, targets, controls] = actionSpace(page.actions);
|
|
360
|
+
const [questions, operations] = buildQuestions(plan, targets, controls, values, Boolean(page.read), Boolean(page.tabs));
|
|
361
|
+
return {
|
|
362
|
+
targets,
|
|
363
|
+
questions,
|
|
364
|
+
operations,
|
|
365
|
+
body: { model: this.model, state: modelState(page, history, plan), questions },
|
|
366
|
+
};
|
|
367
|
+
};
|
|
368
|
+
let fitted = fitChoose(whole, build, this.budget);
|
|
369
|
+
let result;
|
|
370
|
+
const refused = [];
|
|
371
|
+
for (let tries = 1;; tries++) {
|
|
372
|
+
const extra = fitted.dropped ? { dropped_elements: fitted.dropped } : {};
|
|
373
|
+
if (this.budget !== TOKEN_BUDGET)
|
|
374
|
+
extra.token_budget = this.budget;
|
|
375
|
+
if (refused.length)
|
|
376
|
+
extra.refused = refused;
|
|
377
|
+
extra.request = requestSize(fitted);
|
|
378
|
+
try {
|
|
379
|
+
result = await this.post(fitted.body, "choose", extra);
|
|
380
|
+
break;
|
|
381
|
+
}
|
|
382
|
+
catch (error) {
|
|
383
|
+
const over = error instanceof PolicyError && String(error.diagnostic?.error ?? "").includes(OVER_LIMIT);
|
|
384
|
+
if (!over)
|
|
385
|
+
throw error;
|
|
386
|
+
// What Jev refused is kept in the log (counts only, no page text): its limit is not known.
|
|
387
|
+
refused.push({ ...requestSize(fitted), budget: this.budget });
|
|
388
|
+
if (tries >= FIT_TRIES) {
|
|
389
|
+
throw new PolicyError(error.message, { ...error.diagnostic, refused });
|
|
390
|
+
}
|
|
391
|
+
this.budget = Math.floor(estimateTokens(toJson(fitted.body)) * SHRINK);
|
|
392
|
+
fitted = fitChoose(whole, build, this.budget);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
const { page, targets, operations } = fitted;
|
|
396
|
+
const answers = result.answers || {};
|
|
170
397
|
const operationAnswer = validateChoice(answers.operation ?? {}, Object.keys(operations));
|
|
171
398
|
const operation = operationAnswer.choice;
|
|
172
399
|
const confidences = [operationAnswer.confidence];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** Fixed steps from the task file. No API. It tests the harness, not an agent. */
|
|
2
|
-
import {
|
|
2
|
+
import { quote } from "../format.js";
|
|
3
3
|
import { PolicyError } from "../types.js";
|
|
4
4
|
import { actionSpace, OPERATIONS, resolve } from "./base.js";
|
|
5
5
|
/**
|
|
@@ -43,6 +43,6 @@ export class ScriptedPolicy {
|
|
|
43
43
|
}
|
|
44
44
|
if (controls.has("SCROLL_DOWN"))
|
|
45
45
|
return resolve(page, "SCROLL_DOWN", null);
|
|
46
|
-
throw new PolicyError(`Scripted step not found: ${
|
|
46
|
+
throw new PolicyError(`Scripted step not found: ${quote(step)}`);
|
|
47
47
|
}
|
|
48
48
|
}
|
package/dist/remote.js
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A browser that lives in another process. Lightpanda drops its pages when a CDP connection ends, so a daemon
|
|
3
|
+
* (daemon.ts) holds the connection and the page for the whole session, and each CLI command talks to it over a
|
|
4
|
+
* unix socket. RemoteBrowser has the shape of Browser for what the CLI uses; the daemon runs the real Browser,
|
|
5
|
+
* freshness check and all, so nothing is acted on without it.
|
|
6
|
+
*
|
|
7
|
+
* Wire format: one JSON object per line. Request {id, method, params, view}; reply {id, result} or
|
|
8
|
+
* {id, error: {name, message, changed}}.
|
|
9
|
+
*/
|
|
10
|
+
import { connect } from "node:net";
|
|
11
|
+
import { MutationUncertain, StalePage } from "./types.js";
|
|
12
|
+
/** The only methods the daemon runs for a client. */
|
|
13
|
+
export const METHODS = [
|
|
14
|
+
"version",
|
|
15
|
+
"observe",
|
|
16
|
+
"fresh",
|
|
17
|
+
"act",
|
|
18
|
+
"contexts",
|
|
19
|
+
"goto",
|
|
20
|
+
"rows",
|
|
21
|
+
"listEnd",
|
|
22
|
+
"check",
|
|
23
|
+
"unseen",
|
|
24
|
+
"ignoredWindows",
|
|
25
|
+
"screenshot",
|
|
26
|
+
"evaluate",
|
|
27
|
+
"storageState",
|
|
28
|
+
"restore",
|
|
29
|
+
"shutdown",
|
|
30
|
+
];
|
|
31
|
+
export function failure(error) {
|
|
32
|
+
if (error instanceof StalePage)
|
|
33
|
+
return { name: error.name, message: error.message, changed: error.changed };
|
|
34
|
+
if (error instanceof Error)
|
|
35
|
+
return { name: error.name, message: error.message };
|
|
36
|
+
return { name: "Error", message: String(error) };
|
|
37
|
+
}
|
|
38
|
+
/** The error the daemon saw, as the same class the CLI's callers already catch. */
|
|
39
|
+
function revive(error) {
|
|
40
|
+
if (error.name === "StalePage")
|
|
41
|
+
return new StalePage(error.message, error.changed ?? []);
|
|
42
|
+
if (error.name === "MutationUncertain")
|
|
43
|
+
return new MutationUncertain(error.message);
|
|
44
|
+
return new Error(error.message);
|
|
45
|
+
}
|
|
46
|
+
/** Splits a stream into lines; the last partial line waits for more. */
|
|
47
|
+
export function lines(onLine) {
|
|
48
|
+
let rest = "";
|
|
49
|
+
return (chunk) => {
|
|
50
|
+
rest += chunk.toString("utf8");
|
|
51
|
+
for (let at = rest.indexOf("\n"); at >= 0; at = rest.indexOf("\n")) {
|
|
52
|
+
const line = rest.slice(0, at);
|
|
53
|
+
rest = rest.slice(at + 1);
|
|
54
|
+
if (line)
|
|
55
|
+
onLine(line);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export class RemoteBrowser {
|
|
60
|
+
view;
|
|
61
|
+
version = "";
|
|
62
|
+
socket;
|
|
63
|
+
nextId = 1;
|
|
64
|
+
pending = new Map();
|
|
65
|
+
constructor(socket, view) {
|
|
66
|
+
this.socket = socket;
|
|
67
|
+
this.view = view;
|
|
68
|
+
socket.on("data", lines((line) => {
|
|
69
|
+
const reply = JSON.parse(line);
|
|
70
|
+
const waiting = this.pending.get(reply.id);
|
|
71
|
+
if (!waiting)
|
|
72
|
+
return;
|
|
73
|
+
this.pending.delete(reply.id);
|
|
74
|
+
if ("error" in reply)
|
|
75
|
+
waiting.reject(revive(reply.error));
|
|
76
|
+
else
|
|
77
|
+
waiting.resolve(reply.result);
|
|
78
|
+
}));
|
|
79
|
+
const lost = () => {
|
|
80
|
+
for (const waiting of this.pending.values())
|
|
81
|
+
waiting.reject(new Error("The browser daemon closed the connection"));
|
|
82
|
+
this.pending.clear();
|
|
83
|
+
};
|
|
84
|
+
socket.on("close", lost);
|
|
85
|
+
socket.on("error", lost);
|
|
86
|
+
}
|
|
87
|
+
/** Connects to the session's daemon. Nothing on the page changes. */
|
|
88
|
+
static async connect(path, view = null) {
|
|
89
|
+
const socket = await new Promise((resolve, reject) => {
|
|
90
|
+
const opened = connect(path);
|
|
91
|
+
opened.once("connect", () => resolve(opened));
|
|
92
|
+
opened.once("error", reject);
|
|
93
|
+
});
|
|
94
|
+
const remote = new RemoteBrowser(socket, view);
|
|
95
|
+
try {
|
|
96
|
+
remote.version = (await remote.call("version"));
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
await remote.close();
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
return remote;
|
|
103
|
+
}
|
|
104
|
+
// biome-ignore lint/suspicious/noExplicitAny: results are the browser method's own JSON.
|
|
105
|
+
call(method, ...params) {
|
|
106
|
+
return new Promise((resolve, reject) => {
|
|
107
|
+
const id = this.nextId++;
|
|
108
|
+
this.pending.set(id, { resolve, reject });
|
|
109
|
+
const request = { id, method, params, view: this.view };
|
|
110
|
+
this.socket.write(`${JSON.stringify(request)}\n`, (error) => {
|
|
111
|
+
if (error) {
|
|
112
|
+
this.pending.delete(id);
|
|
113
|
+
reject(error);
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
observe() {
|
|
119
|
+
return this.call("observe");
|
|
120
|
+
}
|
|
121
|
+
fresh(page, action = null) {
|
|
122
|
+
return this.call("fresh", page, action);
|
|
123
|
+
}
|
|
124
|
+
async act(action, page, text = null) {
|
|
125
|
+
await this.call("act", action, page, text);
|
|
126
|
+
}
|
|
127
|
+
contexts(nodes) {
|
|
128
|
+
return this.call("contexts", nodes);
|
|
129
|
+
}
|
|
130
|
+
async goto(url) {
|
|
131
|
+
await this.call("goto", url);
|
|
132
|
+
}
|
|
133
|
+
rows() {
|
|
134
|
+
return this.call("rows");
|
|
135
|
+
}
|
|
136
|
+
listEnd() {
|
|
137
|
+
return this.call("listEnd");
|
|
138
|
+
}
|
|
139
|
+
check() {
|
|
140
|
+
return this.call("check");
|
|
141
|
+
}
|
|
142
|
+
unseen() {
|
|
143
|
+
return this.call("unseen");
|
|
144
|
+
}
|
|
145
|
+
ignoredWindows() {
|
|
146
|
+
return this.call("ignoredWindows");
|
|
147
|
+
}
|
|
148
|
+
async screenshot() {
|
|
149
|
+
return Buffer.from((await this.call("screenshot")), "base64");
|
|
150
|
+
}
|
|
151
|
+
/** Read-only evaluation, for a research task's independent verification. */
|
|
152
|
+
evaluate(expression, options = {}) {
|
|
153
|
+
return this.call("evaluate", expression, options);
|
|
154
|
+
}
|
|
155
|
+
storageState() {
|
|
156
|
+
return this.call("storageState");
|
|
157
|
+
}
|
|
158
|
+
restore(state, url) {
|
|
159
|
+
return this.call("restore", state, url);
|
|
160
|
+
}
|
|
161
|
+
/** Ends this connection only. The daemon, its browser and the page stay until the session is stopped. */
|
|
162
|
+
async close() {
|
|
163
|
+
this.socket.destroy();
|
|
164
|
+
}
|
|
165
|
+
}
|