webhand 0.2.0 → 0.3.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 +89 -0
- package/README.md +59 -223
- package/assets/snapshot.js +6 -1
- package/dist/agent.js +18 -4
- package/dist/bin.js +0 -0
- package/dist/browser.js +1 -1
- package/dist/cli.js +46 -54
- package/dist/format.js +44 -0
- package/dist/learn.js +1 -2
- package/dist/metrics.js +45 -2
- package/dist/policies/base.js +5 -5
- package/dist/policies/jev.js +4 -4
- package/dist/policies/scripted.js +2 -2
- package/dist/runbook.js +28 -18
- package/dist/session.js +5 -5
- 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/cli.js
CHANGED
|
@@ -9,14 +9,13 @@ import { join } from "node:path";
|
|
|
9
9
|
import * as agent from "./agent.js";
|
|
10
10
|
import { Budget, execute, runAgent } from "./agent.js";
|
|
11
11
|
import { Browser } from "./browser.js";
|
|
12
|
+
import { describeError, quote, show, stamp, toJson } from "./format.js";
|
|
12
13
|
import * as learn from "./learn.js";
|
|
13
|
-
import { gitState, logOnce, Recorder, summarize } from "./metrics.js";
|
|
14
|
+
import { gitState, logOnce, pageEvent, Recorder, summarize } from "./metrics.js";
|
|
14
15
|
import { actionSpace, offscreen, planText, resolve } from "./policies/base.js";
|
|
15
16
|
import { JevPolicy } from "./policies/jev.js";
|
|
16
|
-
import { describeError, pyJson, pyLen, pyRepr, pyRound, pyStr, stamp, utcIso } from "./py.js";
|
|
17
17
|
import * as runbook from "./runbook.js";
|
|
18
18
|
import * as session from "./session.js";
|
|
19
|
-
import { loadTask, TaskError } from "./tasks.js";
|
|
20
19
|
import { MutationUncertain, PolicyError, StalePage, } from "./types.js";
|
|
21
20
|
import * as workspace from "./workspace.js";
|
|
22
21
|
export const OK = 0;
|
|
@@ -27,7 +26,7 @@ export const BUDGET = 5;
|
|
|
27
26
|
const DEFAULT_ATTEMPTS = 3;
|
|
28
27
|
const PERSON = "[person]";
|
|
29
28
|
const RECENT = 8; // Actions shown to Claude when an attempt stops.
|
|
30
|
-
/** What a command needs to stop with a message,
|
|
29
|
+
/** What a command needs to stop with a message, on stderr with exit code 1. */
|
|
31
30
|
export class CliExit extends Error {
|
|
32
31
|
name = "CliExit";
|
|
33
32
|
}
|
|
@@ -40,14 +39,14 @@ export const deps = {
|
|
|
40
39
|
policy: () => new JevPolicy(),
|
|
41
40
|
startChrome: session.startChrome,
|
|
42
41
|
};
|
|
43
|
-
let context = { out: [], err: () => { }, log: null };
|
|
42
|
+
let context = { out: [], err: () => { }, log: null, hooks: {} };
|
|
44
43
|
function print(text = "") {
|
|
45
44
|
context.out.push(`${text}\n`);
|
|
46
45
|
}
|
|
47
46
|
/** Runs fn and returns what it printed. For tests of the output Claude reads. */
|
|
48
47
|
export function captured(fn) {
|
|
49
48
|
const saved = context;
|
|
50
|
-
context = { out: [], err: saved.err, log: null };
|
|
49
|
+
context = { out: [], err: saved.err, log: null, hooks: {} };
|
|
51
50
|
try {
|
|
52
51
|
const result = fn();
|
|
53
52
|
return { result, text: context.out.join("") };
|
|
@@ -62,13 +61,13 @@ export function render(page) {
|
|
|
62
61
|
const [elements, , controls] = actionSpace(page.actions);
|
|
63
62
|
for (const element of elements) {
|
|
64
63
|
// Claude sees every option with its own line.
|
|
65
|
-
const details = element.value ? [`value=${
|
|
64
|
+
const details = element.value ? [`value=${quote(element.value)}`] : [];
|
|
66
65
|
for (const key of ["checked", "selected", "expanded"]) {
|
|
67
66
|
if (key in element)
|
|
68
|
-
details.push(`${key}=${
|
|
67
|
+
details.push(`${key}=${show(element[key])}`);
|
|
69
68
|
}
|
|
70
69
|
const operations = element.operations.join(",");
|
|
71
|
-
lines.push(`[${element.index}] ${
|
|
70
|
+
lines.push(`[${element.index}] ${show(element.role)} ${quote(element.label)} ${details.join(" ")} (${operations})`);
|
|
72
71
|
for (const option of element.options ?? []) {
|
|
73
72
|
lines.push(` [${option.index}] ${String(option.label).split(" → ").at(-1)}`);
|
|
74
73
|
}
|
|
@@ -110,8 +109,8 @@ export function describePlan(plan, view = null) {
|
|
|
110
109
|
// biome-ignore lint/suspicious/noExplicitAny: the session state.
|
|
111
110
|
export function recentActions(state) {
|
|
112
111
|
const rows = state.history.filter((h) => h.attempt === state.plan.attempt).slice(-RECENT);
|
|
113
|
-
const lines = rows.map((h) => ` ${h.step}. ${h.kind !== "human" ? h.operation : "HUMAN"} ${
|
|
114
|
-
(h.text ? ` text=${
|
|
112
|
+
const lines = rows.map((h) => ` ${h.step}. ${h.kind !== "human" ? h.operation : "HUMAN"} ${quote(h.action)}` +
|
|
113
|
+
(h.text ? ` text=${quote(h.text)}` : "") +
|
|
115
114
|
(h.page_changed ? "" : " (page unchanged)"));
|
|
116
115
|
return `ACTIONS THIS ATTEMPT:\n${lines.length ? lines.join("\n") : " (none)"}`;
|
|
117
116
|
}
|
|
@@ -121,7 +120,7 @@ function parseValues(pairs) {
|
|
|
121
120
|
const at = pair.indexOf("=");
|
|
122
121
|
const label = at < 0 ? "" : pair.slice(0, at).trim();
|
|
123
122
|
if (at < 0 || !label)
|
|
124
|
-
throw new CliExit(`Expected --value LABEL=VALUE, got ${
|
|
123
|
+
throw new CliExit(`Expected --value LABEL=VALUE, got ${quote(pair)}`);
|
|
125
124
|
return { label, value: pair.slice(at + 1) };
|
|
126
125
|
});
|
|
127
126
|
}
|
|
@@ -200,8 +199,8 @@ async function commandCatalog(args) {
|
|
|
200
199
|
print(" (no service.toml)");
|
|
201
200
|
}
|
|
202
201
|
for (const procedure of entry.procedures) {
|
|
203
|
-
print(` PROCEDURE ${procedure.name} [${
|
|
204
|
-
print(` start_url: ${
|
|
202
|
+
print(` PROCEDURE ${procedure.name} [${show(procedure.status)}] (${procedure.scope}): ${show(procedure.goal)}`);
|
|
203
|
+
print(` start_url: ${show(procedure.start_url)}`);
|
|
205
204
|
for (const [key, description] of Object.entries(procedure.params))
|
|
206
205
|
print(` param ${key}: ${description}`);
|
|
207
206
|
if (procedure.human_steps)
|
|
@@ -231,19 +230,7 @@ function chooseChrome(choice) {
|
|
|
231
230
|
async function commandStart(args) {
|
|
232
231
|
if (session.load())
|
|
233
232
|
throw new CliExit("A session is active. Run: webhand finish --status abandoned");
|
|
234
|
-
|
|
235
|
-
if (args.task) {
|
|
236
|
-
try {
|
|
237
|
-
task = await loadTask(args.task);
|
|
238
|
-
}
|
|
239
|
-
catch (error) {
|
|
240
|
-
if (error instanceof TaskError)
|
|
241
|
-
throw new CliExit(error.message);
|
|
242
|
-
throw error;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
if (task && args.goal)
|
|
246
|
-
throw new CliExit("A task supplies its own goal. Drop --goal");
|
|
233
|
+
const task = context.hooks.task ?? null;
|
|
247
234
|
let book = null;
|
|
248
235
|
let service = null;
|
|
249
236
|
let serviceName = null;
|
|
@@ -268,10 +255,10 @@ async function commandStart(args) {
|
|
|
268
255
|
}
|
|
269
256
|
throw error;
|
|
270
257
|
}
|
|
271
|
-
const goal =
|
|
272
|
-
const url =
|
|
258
|
+
const goal = args.goal || (book ? book.goal : null);
|
|
259
|
+
const url = args.url || (book ? book.start_url : null);
|
|
273
260
|
if (!(goal && url))
|
|
274
|
-
throw new CliExit("Give --
|
|
261
|
+
throw new CliExit("Give --runbook, or both --goal and --url");
|
|
275
262
|
let steps = [];
|
|
276
263
|
if (args.step.length)
|
|
277
264
|
steps = parseSteps(args.step);
|
|
@@ -313,10 +300,11 @@ async function commandStart(args) {
|
|
|
313
300
|
view_learned: learned,
|
|
314
301
|
view_claude: claudeView,
|
|
315
302
|
view: runbook.view(learned, claudeView),
|
|
316
|
-
task
|
|
303
|
+
task,
|
|
317
304
|
start_url: session.browserUrl(url, chrome),
|
|
318
305
|
plan,
|
|
319
306
|
runbook: book ? { name: book.name, status: book.status, digest: book.digest, scope: book.scope } : null,
|
|
307
|
+
service: serviceName,
|
|
320
308
|
history: [],
|
|
321
309
|
decisions: 0,
|
|
322
310
|
attempt_decisions: 0,
|
|
@@ -326,7 +314,7 @@ async function commandStart(args) {
|
|
|
326
314
|
max_actions: args.max_actions,
|
|
327
315
|
max_decisions: args.max_decisions,
|
|
328
316
|
started: Date.now() / 1000,
|
|
329
|
-
log: defaultLog(
|
|
317
|
+
log: defaultLog(task || "adhoc"),
|
|
330
318
|
};
|
|
331
319
|
context.log = state.log;
|
|
332
320
|
const recorder = new Recorder(state.log);
|
|
@@ -335,7 +323,7 @@ async function commandStart(args) {
|
|
|
335
323
|
browser = await Browser.open(state.start_url, { port: chromeState.port, view: state.view });
|
|
336
324
|
state.page = await browser.observe();
|
|
337
325
|
recorder.event("run_start", {
|
|
338
|
-
task
|
|
326
|
+
task,
|
|
339
327
|
goal,
|
|
340
328
|
chrome: browser.version,
|
|
341
329
|
chrome_host: chrome,
|
|
@@ -345,7 +333,7 @@ async function commandStart(args) {
|
|
|
345
333
|
service: serviceName,
|
|
346
334
|
view: state.view,
|
|
347
335
|
max_attempts: args.max_attempts,
|
|
348
|
-
started_at:
|
|
336
|
+
started_at: new Date().toISOString(),
|
|
349
337
|
});
|
|
350
338
|
recorder.event("plan", {
|
|
351
339
|
attempt: 1,
|
|
@@ -354,6 +342,7 @@ async function commandStart(args) {
|
|
|
354
342
|
view: state.view,
|
|
355
343
|
source: book && !args.step.length ? "runbook" : "claude",
|
|
356
344
|
});
|
|
345
|
+
recorder.event("page", pageEvent(state.page, 1));
|
|
357
346
|
}
|
|
358
347
|
catch (error) {
|
|
359
348
|
await session.stopChrome(chromeState);
|
|
@@ -373,7 +362,7 @@ async function commandStart(args) {
|
|
|
373
362
|
/** What Claude needs for this outcome, and nothing more. */
|
|
374
363
|
export function report(state, result) {
|
|
375
364
|
const outcome = result.outcome;
|
|
376
|
-
print(`OUTCOME ${outcome} (${attemptLabel(state)}): ${
|
|
365
|
+
print(`OUTCOME ${outcome} (${attemptLabel(state)}): ${show(result.detail)}`);
|
|
377
366
|
if (outcome === agent.UNCERTAIN) {
|
|
378
367
|
print("Do not repeat or replan. Run: webhand finish --status error");
|
|
379
368
|
return UNCERTAIN;
|
|
@@ -383,7 +372,7 @@ export function report(state, result) {
|
|
|
383
372
|
return OK;
|
|
384
373
|
}
|
|
385
374
|
if (outcome === agent.NEEDS_VALUE) {
|
|
386
|
-
print(`FIELD: ${
|
|
375
|
+
print(`FIELD: ${toJson(result.field)}`);
|
|
387
376
|
print("Give the value with: run --value 'LABEL=VALUE'. Never invent personal data; ask the person");
|
|
388
377
|
return OK;
|
|
389
378
|
}
|
|
@@ -578,6 +567,7 @@ async function commandAct(args) {
|
|
|
578
567
|
await execute(browser, state.page, decision, state.history, recorder, {
|
|
579
568
|
decidedBy: "claude",
|
|
580
569
|
attempt: state.plan.attempt,
|
|
570
|
+
values: state.plan.values,
|
|
581
571
|
onChange: (page) => {
|
|
582
572
|
state.page = page;
|
|
583
573
|
session.save(state);
|
|
@@ -643,17 +633,18 @@ function runTotals(log) {
|
|
|
643
633
|
not_visible: notVisible,
|
|
644
634
|
};
|
|
645
635
|
}
|
|
646
|
-
/** Verifies the page independently. The status is what the agent claims, nothing more. */
|
|
636
|
+
/** Verifies the page independently when a verifier was given. The status is what the agent claims, nothing more. */
|
|
647
637
|
async function commandFinish(args) {
|
|
648
638
|
const state = session.load();
|
|
649
639
|
if (state === null)
|
|
650
640
|
throw new CliExit("No active session. Run: webhand start");
|
|
651
641
|
let verdict = { success: null, evidence: "no verifier" };
|
|
652
642
|
try {
|
|
653
|
-
|
|
643
|
+
const verify = context.hooks.verify;
|
|
644
|
+
if (verify) {
|
|
654
645
|
const browser = await Browser.open(null, { port: state.port });
|
|
655
646
|
try {
|
|
656
|
-
verdict = await (
|
|
647
|
+
verdict = await verify(browser, state);
|
|
657
648
|
}
|
|
658
649
|
finally {
|
|
659
650
|
await browser.close();
|
|
@@ -673,18 +664,20 @@ async function commandFinish(args) {
|
|
|
673
664
|
logOnce(state.log, "run_end", {
|
|
674
665
|
status: args.status,
|
|
675
666
|
reason: args.reason,
|
|
667
|
+
service: state.service ?? null,
|
|
668
|
+
runbook: state.runbook ?? null,
|
|
676
669
|
goal: plan.goal,
|
|
677
670
|
plan: planText(plan),
|
|
678
671
|
attempts: plan.attempt,
|
|
679
672
|
attempt_outcomes: outcomes,
|
|
680
673
|
actions: state.history.length,
|
|
681
674
|
decisions: state.decisions,
|
|
682
|
-
elapsed_ms:
|
|
675
|
+
elapsed_ms: Math.round((Date.now() / 1000 - state.started) * 1000),
|
|
683
676
|
verified: verdict.success,
|
|
684
677
|
evidence: verdict.evidence,
|
|
685
678
|
...totals,
|
|
686
679
|
});
|
|
687
|
-
print(`FINISHED status=${args.status} verified=${
|
|
680
|
+
print(`FINISHED status=${args.status} verified=${show(verdict.success)} attempts=${plan.attempt} log=${state.log}`);
|
|
688
681
|
return OK;
|
|
689
682
|
}
|
|
690
683
|
function parentAlive(pid) {
|
|
@@ -764,7 +757,7 @@ async function commandLearn(args) {
|
|
|
764
757
|
chrome_host: chrome,
|
|
765
758
|
session: profile ? "profile" : "new",
|
|
766
759
|
git: gitState(),
|
|
767
|
-
started_at:
|
|
760
|
+
started_at: new Date().toISOString(),
|
|
768
761
|
});
|
|
769
762
|
counts = await learn.record(browser, session.browserUrl(args.url, chrome), recorder, () => signals.stopped() || performance.now() >= deadline);
|
|
770
763
|
recorder.event("learn_end", { counts });
|
|
@@ -777,7 +770,7 @@ async function commandLearn(args) {
|
|
|
777
770
|
}
|
|
778
771
|
if (note)
|
|
779
772
|
print(`NOTE: ${note}`);
|
|
780
|
-
print(`RECORDED ${
|
|
773
|
+
print(`RECORDED ${quote(counts)} log=${log}`);
|
|
781
774
|
print(`WRITE THE RUNBOOK TO ${runbookFile}`);
|
|
782
775
|
if (serviceFile)
|
|
783
776
|
print(`WRITE THE SERVICE INFORMATION TO ${serviceFile}`);
|
|
@@ -885,7 +878,6 @@ function commands() {
|
|
|
885
878
|
{ name: "param", type: "string", multiple: true, metavar: "KEY=VALUE", help: "A runbook parameter" },
|
|
886
879
|
{ name: "allow-draft", type: "boolean", help: "Trial a runbook no person has approved yet" },
|
|
887
880
|
...viewOptions,
|
|
888
|
-
{ name: "task", type: "string", help: "A task in tasks/ with an independent verify() (research repository)" },
|
|
889
881
|
{ name: "session", type: "string", default: "new", help: "new (temporary) or profile:NAME (saved sign-in)" },
|
|
890
882
|
chromeOption,
|
|
891
883
|
{ name: "headed", type: "boolean" },
|
|
@@ -1011,7 +1003,7 @@ function mainHelp(specs) {
|
|
|
1011
1003
|
lines.push("", " --version Print the version", " --help Print this help; COMMAND --help prints a command's options");
|
|
1012
1004
|
return `${lines.join("\n")}\n`;
|
|
1013
1005
|
}
|
|
1014
|
-
/** Parses argv
|
|
1006
|
+
/** Parses argv, with argparse-style messages. Throws UsageError, or returns help text to print. */
|
|
1015
1007
|
export function parse(argv) {
|
|
1016
1008
|
const specs = commands();
|
|
1017
1009
|
const [name, ...rest] = argv;
|
|
@@ -1023,7 +1015,7 @@ export function parse(argv) {
|
|
|
1023
1015
|
return { help: `${version()}\n` };
|
|
1024
1016
|
const spec = specs[name];
|
|
1025
1017
|
if (!spec)
|
|
1026
|
-
throw new UsageError(`argument command: invalid choice: ${
|
|
1018
|
+
throw new UsageError(`argument command: invalid choice: ${quote(name)}`);
|
|
1027
1019
|
const args = {};
|
|
1028
1020
|
const positionals = [];
|
|
1029
1021
|
const byName = new Map(spec.options.map((option) => [option.name, option]));
|
|
@@ -1054,7 +1046,7 @@ export function parse(argv) {
|
|
|
1054
1046
|
seen.add(option.name);
|
|
1055
1047
|
if (option.type === "boolean") {
|
|
1056
1048
|
if (at >= 0)
|
|
1057
|
-
throw fail(`argument --${option.name}: ignored explicit argument ${
|
|
1049
|
+
throw fail(`argument --${option.name}: ignored explicit argument ${quote(token.slice(at + 1))}`);
|
|
1058
1050
|
args[key] = true;
|
|
1059
1051
|
continue;
|
|
1060
1052
|
}
|
|
@@ -1072,18 +1064,18 @@ export function parse(argv) {
|
|
|
1072
1064
|
let value = raw;
|
|
1073
1065
|
if (option.type === "int") {
|
|
1074
1066
|
if (!/^\s*[-+]?\d+\s*$/.test(raw))
|
|
1075
|
-
throw fail(`argument --${option.name}: invalid int value: ${
|
|
1067
|
+
throw fail(`argument --${option.name}: invalid int value: ${quote(raw)}`);
|
|
1076
1068
|
value = Number.parseInt(raw, 10);
|
|
1077
1069
|
}
|
|
1078
1070
|
else if (option.type === "float") {
|
|
1079
1071
|
const number = Number(raw);
|
|
1080
1072
|
if (!raw.trim() || Number.isNaN(number)) {
|
|
1081
|
-
throw fail(`argument --${option.name}: invalid float value: ${
|
|
1073
|
+
throw fail(`argument --${option.name}: invalid float value: ${quote(raw)}`);
|
|
1082
1074
|
}
|
|
1083
1075
|
value = number;
|
|
1084
1076
|
}
|
|
1085
1077
|
if (option.choices && !option.choices.includes(String(value))) {
|
|
1086
|
-
throw fail(`argument --${option.name}: invalid choice: ${
|
|
1078
|
+
throw fail(`argument --${option.name}: invalid choice: ${quote(value)} (choose from ${option.choices.map(quote).join(", ")})`);
|
|
1087
1079
|
}
|
|
1088
1080
|
if (option.multiple)
|
|
1089
1081
|
args[key].push(value);
|
|
@@ -1101,7 +1093,7 @@ export function parse(argv) {
|
|
|
1101
1093
|
declared.forEach((positional, i) => {
|
|
1102
1094
|
const value = positionals[i] ?? null;
|
|
1103
1095
|
if (value !== null && positional.choices && !positional.choices.includes(value)) {
|
|
1104
|
-
throw fail(`argument ${positional.name}: invalid choice: ${
|
|
1096
|
+
throw fail(`argument ${positional.name}: invalid choice: ${quote(value)} (choose from ${positional.choices.map(quote).join(", ")})`);
|
|
1105
1097
|
}
|
|
1106
1098
|
args[positional.name] = value;
|
|
1107
1099
|
});
|
|
@@ -1123,13 +1115,13 @@ const processIO = {
|
|
|
1123
1115
|
* Runs one command and returns its exit code. A CliExit or UsageError is thrown after the output is written,
|
|
1124
1116
|
* so the caller prints its message and exits with 1 or 2.
|
|
1125
1117
|
*/
|
|
1126
|
-
export async function main(argv, io = processIO) {
|
|
1118
|
+
export async function main(argv, io = processIO, hooks = {}) {
|
|
1127
1119
|
const parsed = parse(argv);
|
|
1128
1120
|
if ("help" in parsed) {
|
|
1129
1121
|
io.out(parsed.help);
|
|
1130
1122
|
return OK;
|
|
1131
1123
|
}
|
|
1132
|
-
context = { out: [], err: io.err, log: null };
|
|
1124
|
+
context = { out: [], err: io.err, log: null, hooks };
|
|
1133
1125
|
const current = context;
|
|
1134
1126
|
// What Claude must read is a real cost of this design, so every handoff is sized and logged.
|
|
1135
1127
|
try {
|
|
@@ -1139,6 +1131,6 @@ export async function main(argv, io = processIO) {
|
|
|
1139
1131
|
const text = current.out.join("");
|
|
1140
1132
|
io.out(text);
|
|
1141
1133
|
if (current.log)
|
|
1142
|
-
logOnce(current.log, "handoff", { command: parsed.command, chars:
|
|
1134
|
+
logOnce(current.log, "handoff", { command: parsed.command, chars: [...text].length });
|
|
1143
1135
|
}
|
|
1144
1136
|
}
|
package/dist/format.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/** How values are written into what Claude reads, what Jev receives, and the logs. */
|
|
2
|
+
/** A value as JSON, the way the CLI quotes a value inside a message. */
|
|
3
|
+
export function quote(value) {
|
|
4
|
+
return toJson(value);
|
|
5
|
+
}
|
|
6
|
+
/** Strings as they are, anything else as JSON. */
|
|
7
|
+
export function show(value) {
|
|
8
|
+
return typeof value === "string" ? value : quote(value);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Compact JSON. Maps keep their insertion order, which a plain object would not keep for keys like
|
|
12
|
+
* "2" and "1". Undefined object entries are dropped; undefined elsewhere is null.
|
|
13
|
+
*/
|
|
14
|
+
export function toJson(value) {
|
|
15
|
+
const encode = (item) => {
|
|
16
|
+
if (item === null || item === undefined)
|
|
17
|
+
return "null";
|
|
18
|
+
if (typeof item === "number")
|
|
19
|
+
return Number.isFinite(item) ? JSON.stringify(item) : "null";
|
|
20
|
+
if (typeof item === "string" || typeof item === "boolean")
|
|
21
|
+
return JSON.stringify(item);
|
|
22
|
+
if (Array.isArray(item))
|
|
23
|
+
return `[${item.map(encode).join(",")}]`;
|
|
24
|
+
const entries = item instanceof Map ? [...item] : Object.entries(item);
|
|
25
|
+
return `{${entries
|
|
26
|
+
.filter(([, v]) => v !== undefined)
|
|
27
|
+
.map(([k, v]) => `${JSON.stringify(String(k))}:${encode(v)}`)
|
|
28
|
+
.join(",")}}`;
|
|
29
|
+
};
|
|
30
|
+
return encode(value);
|
|
31
|
+
}
|
|
32
|
+
/** A run folder name: YYYYMMDDTHHMMSS in local time. */
|
|
33
|
+
export function stamp(date = new Date()) {
|
|
34
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
35
|
+
return (`${date.getFullYear()}${pad(date.getMonth() + 1)}${pad(date.getDate())}` +
|
|
36
|
+
`T${pad(date.getHours())}${pad(date.getMinutes())}${pad(date.getSeconds())}`);
|
|
37
|
+
}
|
|
38
|
+
/** An error for a log line: its name and message. */
|
|
39
|
+
export function describeError(error) {
|
|
40
|
+
if (error instanceof Error)
|
|
41
|
+
return `${error.name}: ${error.message}`;
|
|
42
|
+
return `Error: ${String(error)}`;
|
|
43
|
+
}
|
|
44
|
+
export const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
package/dist/learn.js
CHANGED
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
* The record is untrusted: pages can forge binding calls, so it only ever becomes a draft for review.
|
|
5
5
|
*/
|
|
6
6
|
import { asset, READ_STATE } from "./browser.js";
|
|
7
|
-
import { pyRound } from "./py.js";
|
|
8
7
|
// A function replacer: the snapshot holds "$" sequences that a string replacement would expand.
|
|
9
8
|
const RECORDER = asset("recorder.js").replaceAll("__SNAPSHOT__", () => READ_STATE.trim());
|
|
10
9
|
const FIELDS = ["operation", "role", "label", "value", "checked", "reason", "url", "title", "text", "region"];
|
|
@@ -46,7 +45,7 @@ export async function record(browser, url, recorder, until) {
|
|
|
46
45
|
const started = performance.now();
|
|
47
46
|
const write = (kind, fields) => {
|
|
48
47
|
counts[kind] = (counts[kind] ?? 0) + 1;
|
|
49
|
-
recorder.event(kind, { at_ms:
|
|
48
|
+
recorder.event(kind, { at_ms: Math.round(performance.now() - started), ...fields });
|
|
50
49
|
};
|
|
51
50
|
const receive = (_source, event) => {
|
|
52
51
|
if (event === null || typeof event !== "object" || Array.isArray(event))
|
package/dist/metrics.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { execFileSync } from "node:child_process";
|
|
3
3
|
import { closeSync, mkdirSync, openSync, writeSync } from "node:fs";
|
|
4
4
|
import { dirname } from "node:path";
|
|
5
|
-
import {
|
|
5
|
+
import { toJson } from "./format.js";
|
|
6
6
|
/** Token totals per provider. */
|
|
7
7
|
export function summarize(calls) {
|
|
8
8
|
const providers = {};
|
|
@@ -40,7 +40,7 @@ export class Recorder {
|
|
|
40
40
|
event(type, fields = {}) {
|
|
41
41
|
if (this.fd === null)
|
|
42
42
|
throw new Error(`Recorder for ${this.path} is closed`);
|
|
43
|
-
writeSync(this.fd, `${
|
|
43
|
+
writeSync(this.fd, `${toJson({ type, ...fields })}\n`);
|
|
44
44
|
}
|
|
45
45
|
call(call) {
|
|
46
46
|
this.calls.push(call);
|
|
@@ -62,3 +62,46 @@ export function logOnce(path, type, fields = {}) {
|
|
|
62
62
|
recorder.close();
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
|
+
/** A URL for the run log: query values and a hash that holds values are hidden, as in a learn record. */
|
|
66
|
+
export function safeUrl(url) {
|
|
67
|
+
if (typeof url !== "string")
|
|
68
|
+
return null;
|
|
69
|
+
try {
|
|
70
|
+
const parsed = new URL(url);
|
|
71
|
+
for (const key of [...parsed.searchParams.keys()])
|
|
72
|
+
parsed.searchParams.set(key, "<redacted>");
|
|
73
|
+
if (parsed.hash.includes("="))
|
|
74
|
+
parsed.hash = "<redacted>";
|
|
75
|
+
return parsed.href;
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const OUTLINE_FIELDS = [
|
|
82
|
+
"key",
|
|
83
|
+
"position",
|
|
84
|
+
"text_chars",
|
|
85
|
+
"shown_chars",
|
|
86
|
+
"actions",
|
|
87
|
+
"shown_actions",
|
|
88
|
+
"offscreen_actions",
|
|
89
|
+
"view",
|
|
90
|
+
];
|
|
91
|
+
/** An observed page for the run log: where it was and its region outline. No page text. */
|
|
92
|
+
// biome-ignore lint/suspicious/noExplicitAny: pages come from the page script as untyped JSON.
|
|
93
|
+
export function pageEvent(page, attempt) {
|
|
94
|
+
const regions = Array.isArray(page.regions) ? page.regions : [];
|
|
95
|
+
return {
|
|
96
|
+
attempt,
|
|
97
|
+
url: safeUrl(page.url),
|
|
98
|
+
regions: regions.map((region) => {
|
|
99
|
+
const kept = {};
|
|
100
|
+
for (const key of OUTLINE_FIELDS)
|
|
101
|
+
if (key in region)
|
|
102
|
+
kept[key] = region[key];
|
|
103
|
+
return kept;
|
|
104
|
+
}),
|
|
105
|
+
omitted_regions: page.omitted_regions ?? 0,
|
|
106
|
+
};
|
|
107
|
+
}
|
package/dist/policies/base.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Adapted from jev-ultrafast (MIT); see THIRD_PARTY_LICENSE_jev-ultrafast.txt.
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
6
|
+
import { quote } from "../format.js";
|
|
7
7
|
import { Decision, PolicyError } from "../types.js";
|
|
8
8
|
export const NEXT_ACTION = `Advance the user's entire goal from the CURRENT page using one operation.
|
|
9
9
|
Work through the plan's steps in order; the action history shows what was already done in this run.
|
|
@@ -114,8 +114,8 @@ export function offscreen(page) {
|
|
|
114
114
|
const height = page.h || 0;
|
|
115
115
|
const y = "y" in scroll ? scroll.y : 0;
|
|
116
116
|
const total = "height" in scroll ? scroll.height : 0;
|
|
117
|
-
const above =
|
|
118
|
-
const below = height ? Math.max(0,
|
|
117
|
+
const above = Math.round(y);
|
|
118
|
+
const below = height ? Math.max(0, Math.round(total - y - height)) : 0;
|
|
119
119
|
return { above_px: above, below_px: below };
|
|
120
120
|
}
|
|
121
121
|
/** A dropdown's options once each, as index -> option label. The element's label is not repeated. */
|
|
@@ -170,9 +170,9 @@ export function resolve(page, operation, target) {
|
|
|
170
170
|
return new Decision(operation, control);
|
|
171
171
|
const choices = targets.get(operation);
|
|
172
172
|
if (!choices)
|
|
173
|
-
throw new PolicyError(`Operation ${
|
|
173
|
+
throw new PolicyError(`Operation ${quote(operation)} is unavailable; no action executed.`);
|
|
174
174
|
const action = target === null || target === undefined ? undefined : choices.get(target);
|
|
175
175
|
if (!action)
|
|
176
|
-
throw new PolicyError(`Target ${
|
|
176
|
+
throw new PolicyError(`Target ${quote(target ?? null)} is invalid for ${operation}; no action executed.`);
|
|
177
177
|
return new Decision(operation, action);
|
|
178
178
|
}
|
package/dist/policies/jev.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
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 { sleep, toJson } from "../format.js";
|
|
8
8
|
import { Decision, modelCall, PolicyError, } from "../types.js";
|
|
9
9
|
import { actionSpace, modelState, NEXT_ACTION, NOT_VISIBLE, NOT_VISIBLE_HELP, OPERATION_HELP, planText, resolve, TARGET, TERMINAL_HELP, VALUE, valueChoices, } from "./base.js";
|
|
10
10
|
const URL = "https://api.typesafe.ai/v1/systemone";
|
|
@@ -16,7 +16,7 @@ async function postJson(fetchJson, key, body) {
|
|
|
16
16
|
response = await fetchJson(URL, {
|
|
17
17
|
method: "POST",
|
|
18
18
|
headers: { Authorization: `Bearer ${key}`, "Content-Type": "application/json" },
|
|
19
|
-
body:
|
|
19
|
+
body: toJson(body),
|
|
20
20
|
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
21
21
|
});
|
|
22
22
|
}
|
|
@@ -142,7 +142,7 @@ export class JevPolicy {
|
|
|
142
142
|
this.agent = null;
|
|
143
143
|
}
|
|
144
144
|
else {
|
|
145
|
-
// HTTP/2 when the server offers it
|
|
145
|
+
// HTTP/2 when the server offers it; HTTP/1.1 otherwise.
|
|
146
146
|
const agent = new Agent({ allowH2: true });
|
|
147
147
|
this.agent = agent;
|
|
148
148
|
this.fetchJson = (url, init) => undiciFetch(url, { ...init, dispatcher: agent });
|
|
@@ -165,7 +165,7 @@ export class JevPolicy {
|
|
|
165
165
|
const usage = (isObject && result.usage) || {};
|
|
166
166
|
this.servedModel = isObject && "model" in result ? result.model : this.model;
|
|
167
167
|
// The response carries token counts but no billed amount.
|
|
168
|
-
this.calls.push(modelCall("typesafe", this.servedModel, "choose",
|
|
168
|
+
this.calls.push(modelCall("typesafe", this.servedModel, "choose", 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)));
|
|
169
169
|
const answers = (isObject && result.answers) || {};
|
|
170
170
|
const operationAnswer = validateChoice(answers.operation ?? {}, Object.keys(operations));
|
|
171
171
|
const operation = operationAnswer.choice;
|
|
@@ -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
|
}
|