effect-browser 0.3.0-beta.21 → 0.3.0-beta.23
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 +130 -159
- package/dist/Action-24sLOubi.d.mts +110 -0
- package/dist/Action.d.mts +2 -0
- package/dist/Action.mjs +141 -0
- package/dist/Action.mjs.map +1 -0
- package/dist/ActionPort.d.mts +15 -0
- package/dist/ActionPort.mjs +101 -0
- package/dist/ActionPort.mjs.map +1 -0
- package/dist/Browser-BKPAbyys.mjs +4076 -0
- package/dist/Browser-BKPAbyys.mjs.map +1 -0
- package/dist/{Browser-CiXHT1yO.d.mts → Browser-wZhwZSlG.d.mts} +51 -13
- package/dist/Browser.d.mts +2 -2
- package/dist/Browser.mjs +1 -1
- package/dist/{BrowserError-Coy7EW1N.d.mts → BrowserError-BlcmLVzn.d.mts} +22 -7
- package/dist/BrowserError.d.mts +2 -2
- package/dist/BrowserError.mjs +46 -9
- package/dist/BrowserError.mjs.map +1 -1
- package/dist/{BrowserEvent-dq6FRT8w.d.mts → BrowserEvent-lLqJ3GJS.d.mts} +93 -37
- package/dist/BrowserEvent.d.mts +2 -2
- package/dist/BrowserEvent.mjs +29 -24
- package/dist/BrowserEvent.mjs.map +1 -1
- package/dist/{Change-DqING3tr.d.mts → Change-Ba7INmay.d.mts} +4 -6
- package/dist/Change.d.mts +2 -2
- package/dist/Change.mjs +97 -2
- package/dist/Change.mjs.map +1 -0
- package/dist/Clock.d.mts +57 -0
- package/dist/Clock.mjs +80 -0
- package/dist/Clock.mjs.map +1 -0
- package/dist/{Frame-DkNdjjjr.d.mts → Frame-UEH2hO5T.d.mts} +10 -3
- package/dist/Frame.d.mts +2 -2
- package/dist/Frame.mjs +10 -2
- package/dist/Frame.mjs.map +1 -1
- package/dist/{Motion-DND6I-64.d.mts → Geometry-DJx9qrYy.d.mts} +7 -10
- package/dist/Geometry.d.mts +2 -0
- package/dist/Geometry.mjs +32 -0
- package/dist/Geometry.mjs.map +1 -0
- package/dist/Matching.d.mts +37 -0
- package/dist/Matching.mjs +72 -0
- package/dist/Matching.mjs.map +1 -0
- package/dist/Page-BF5izr8R.mjs +795 -0
- package/dist/Page-BF5izr8R.mjs.map +1 -0
- package/dist/{Page-ku6j5t-v.d.mts → Page-CvsOEorc.d.mts} +158 -185
- package/dist/Page.d.mts +2 -2
- package/dist/Page.mjs +1 -1
- package/dist/Released-tURbFmal.d.mts +15 -0
- package/dist/{Snapshot-B34hZ4Jl.d.mts → Snapshot-CtBVOtuu.d.mts} +6 -1
- package/dist/Snapshot.d.mts +1 -1
- package/dist/Snapshot.mjs.map +1 -1
- package/dist/Stillness-CNlvayIo.mjs +136 -0
- package/dist/Stillness-CNlvayIo.mjs.map +1 -0
- package/dist/Style-AO4V7Czt.mjs +357 -0
- package/dist/Style-AO4V7Czt.mjs.map +1 -0
- package/dist/Style.d.mts +72 -0
- package/dist/Style.mjs +2 -0
- package/dist/Url.d.mts +26 -0
- package/dist/Url.mjs +74 -0
- package/dist/Url.mjs.map +1 -0
- package/dist/Window-CMddGMt1.d.mts +34 -0
- package/dist/Window.d.mts +2 -0
- package/dist/Window.mjs +35 -0
- package/dist/Window.mjs.map +1 -0
- package/dist/{cdp-DL7XfEj5.d.mts → connection-2KHI4_KN.d.mts} +4 -4
- package/dist/{cdp-BMpYMWYe.mjs → connection-CK83pvqP.mjs} +5 -6
- package/dist/connection-CK83pvqP.mjs.map +1 -0
- package/dist/index.d.mts +15 -18
- package/dist/index.mjs +11 -15
- package/dist/moment/Moment.d.mts +86 -0
- package/dist/{Moment.mjs → moment/Moment.mjs} +44 -38
- package/dist/moment/Moment.mjs.map +1 -0
- package/dist/{Plan-DJ4hEEna.d.mts → plan/index.d.mts} +169 -64
- package/dist/{Plan-BG1WifW2.mjs → plan/index.mjs} +122 -199
- package/dist/plan/index.mjs.map +1 -0
- package/dist/{Policy-BGWjbqCl.d.mts → policy/Policy.d.mts} +16 -20
- package/dist/{Policy.mjs → policy/Policy.mjs} +11 -21
- package/dist/policy/Policy.mjs.map +1 -0
- package/dist/presentation/Motion.d.mts +12 -0
- package/dist/{Motion.mjs → presentation/Motion.mjs} +4 -27
- package/dist/presentation/Motion.mjs.map +1 -0
- package/dist/presentation/Presentation.d.mts +30 -0
- package/dist/presentation/Presentation.mjs +105 -0
- package/dist/presentation/Presentation.mjs.map +1 -0
- package/dist/{Stage-NHp8N81k.d.mts → presentation/Stage.d.mts} +12 -15
- package/dist/{Stage.mjs → presentation/Stage.mjs} +68 -76
- package/dist/presentation/Stage.mjs.map +1 -0
- package/dist/presentation/feed.d.mts +20 -0
- package/dist/presentation/feed.mjs +18 -0
- package/dist/presentation/feed.mjs.map +1 -0
- package/dist/providers/Cdp.d.mts +9 -0
- package/dist/{Cdp.mjs → providers/Cdp.mjs} +3 -8
- package/dist/providers/Cdp.mjs.map +1 -0
- package/dist/{Chromium.d.mts → providers/Chromium.d.mts} +6 -7
- package/dist/{Chromium.mjs → providers/Chromium.mjs} +6 -15
- package/dist/providers/Chromium.mjs.map +1 -0
- package/dist/providers/Released.d.mts +2 -0
- package/dist/providers/Released.mjs +12 -0
- package/dist/providers/Released.mjs.map +1 -0
- package/dist/stillness/Stillness.d.mts +12 -0
- package/dist/stillness/Stillness.mjs +2 -0
- package/dist/supervisor/Supervisor.d.mts +104 -0
- package/dist/supervisor/Supervisor.mjs +515 -0
- package/dist/supervisor/Supervisor.mjs.map +1 -0
- package/dist/testing/index.d.mts +337 -0
- package/dist/testing/index.mjs +515 -0
- package/dist/testing/index.mjs.map +1 -0
- package/dist/workerd/Cdp.d.mts +8 -3
- package/dist/workerd/Cdp.mjs +2 -7
- package/dist/workerd/Cdp.mjs.map +1 -1
- package/package.json +89 -39
- package/src/Action.ts +127 -0
- package/src/ActionPort.ts +128 -0
- package/src/Browser.ts +352 -201
- package/src/BrowserError.ts +63 -6
- package/src/BrowserEvent.ts +26 -25
- package/src/Change.ts +0 -2
- package/src/{internal/pictures/clock.ts → Clock.ts} +17 -6
- package/src/Frame.ts +14 -0
- package/src/Geometry.ts +48 -0
- package/src/{internal/reading/match.inpage.ts → Matching.ts} +34 -4
- package/src/Page.ts +111 -92
- package/src/Snapshot.ts +5 -0
- package/src/{internal/input/style.ts → Style.ts} +121 -14
- package/src/Window.ts +30 -0
- package/src/index.ts +15 -6
- package/src/internal/input/actions.ts +61 -119
- package/src/internal/input/dispatch.ts +3 -3
- package/src/internal/input/gate.ts +21 -18
- package/src/internal/input/guard.inpage.ts +25 -58
- package/src/internal/input/guard.ts +11 -20
- package/src/internal/input/keyboard.ts +12 -3
- package/src/internal/input/perform.ts +78 -60
- package/src/internal/input/pointer.ts +4 -2
- package/src/internal/input/settle.ts +7 -5
- package/src/internal/input/targets.inpage.ts +1 -1
- package/src/internal/input/targets.ts +4 -9
- package/src/internal/page/bridge.ts +209 -113
- package/src/internal/page/context.ts +12 -59
- package/src/internal/page/dialogs.ts +58 -0
- package/src/internal/page/identity.ts +6 -5
- package/src/internal/page/lane.ts +14 -15
- package/src/internal/page/navigation.ts +50 -42
- package/src/internal/page/openings.ts +11 -5
- package/src/internal/page/page.ts +44 -81
- package/src/internal/page/port.ts +125 -0
- package/src/internal/{input → page}/replies.ts +1 -1
- package/src/internal/page/request-owner.ts +269 -0
- package/src/internal/page/requests.ts +357 -0
- package/src/internal/page/scripts.ts +124 -0
- package/src/internal/pictures/capture.ts +255 -332
- package/src/internal/pictures/dimensions.ts +162 -0
- package/src/internal/pictures/frame.ts +68 -0
- package/src/internal/pictures/pictures.ts +57 -23
- package/src/internal/pictures/quiescence.ts +1 -1
- package/src/internal/pictures/state.ts +26 -0
- package/src/internal/pictures/transport.ts +21 -4
- package/src/internal/reading/controls.inpage.ts +5 -70
- package/src/internal/reading/expect.inpage.ts +13 -17
- package/src/internal/reading/names.inpage.ts +89 -45
- package/src/internal/reading/outline.inpage.ts +6 -7
- package/src/internal/reading/reading.ts +144 -176
- package/src/internal/reading/ready.inpage.ts +6 -17
- package/src/internal/reading/ready.ts +13 -135
- package/src/internal/reading/subjects.inpage.ts +83 -43
- package/src/internal/reading/walk.inpage.ts +33 -13
- package/src/internal/timeline/changes.ts +55 -6
- package/src/internal/timeline/events.ts +15 -5
- package/src/internal/timeline/history.inpage.ts +1 -1
- package/src/internal/timeline/record.inpage.ts +4 -1
- package/src/internal/timeline/window.ts +2 -1
- package/src/{Moment.ts → moment/Moment.ts} +13 -31
- package/src/{internal/timeline → moment}/words.ts +2 -2
- package/src/{internal/reading → plan}/choose.ts +3 -3
- package/src/plan/data.ts +117 -0
- package/src/plan/execute.ts +411 -0
- package/src/plan/index.ts +36 -0
- package/src/plan/record.ts +71 -0
- package/src/plan/targets.ts +71 -0
- package/src/{Policy.ts → policy/Policy.ts} +13 -10
- package/src/{Motion.ts → presentation/Motion.ts} +12 -46
- package/src/presentation/Presentation.ts +176 -0
- package/src/{Stage.ts → presentation/Stage.ts} +147 -112
- package/src/presentation/feed.ts +21 -0
- package/src/{Cdp.ts → providers/Cdp.ts} +2 -2
- package/src/{Chromium.ts → providers/Chromium.ts} +7 -10
- package/src/providers/Released.ts +13 -0
- package/src/{internal/cdp.ts → providers/connection.ts} +2 -2
- package/src/{internal/pictures/motion.ts → stillness/Pixels.ts} +1 -1
- package/src/stillness/Stillness.ts +166 -0
- package/src/supervisor/Supervisor.ts +170 -0
- package/src/supervisor/contract.ts +105 -0
- package/src/supervisor/lifecycle.ts +272 -0
- package/src/supervisor/runtime.ts +238 -0
- package/src/testing/TestBrowser.ts +115 -0
- package/src/testing/TestPage.ts +10 -0
- package/src/testing/evidence.ts +154 -0
- package/src/testing/index.ts +3 -0
- package/src/testing/model.ts +222 -0
- package/src/testing/page.ts +360 -0
- package/src/workerd/Cdp.ts +2 -2
- package/dist/Browser-CxXUjxOp.mjs +0 -619
- package/dist/Browser-CxXUjxOp.mjs.map +0 -1
- package/dist/Cdp-CzCRnMlM.d.mts +0 -10
- package/dist/Cdp.d.mts +0 -11
- package/dist/Cdp.mjs.map +0 -1
- package/dist/Change-DNvztNLu.mjs +0 -138
- package/dist/Change-DNvztNLu.mjs.map +0 -1
- package/dist/Chromium.mjs.map +0 -1
- package/dist/Expect-u3P_ENLX.d.mts +0 -62
- package/dist/Expect.d.mts +0 -2
- package/dist/Expect.mjs +0 -114
- package/dist/Expect.mjs.map +0 -1
- package/dist/Moment.d.mts +0 -2
- package/dist/Moment.mjs.map +0 -1
- package/dist/Motion.d.mts +0 -2
- package/dist/Motion.mjs.map +0 -1
- package/dist/Page-BP02bmNv.mjs +0 -264
- package/dist/Page-BP02bmNv.mjs.map +0 -1
- package/dist/Plan-BG1WifW2.mjs.map +0 -1
- package/dist/Plan.d.mts +0 -2
- package/dist/Plan.mjs +0 -2
- package/dist/Policy.d.mts +0 -2
- package/dist/Policy.mjs.map +0 -1
- package/dist/Presentation-DxVikn9_.d.mts +0 -48
- package/dist/Presentation.d.mts +0 -2
- package/dist/Presentation.mjs +0 -118
- package/dist/Presentation.mjs.map +0 -1
- package/dist/Stage.d.mts +0 -2
- package/dist/Stage.mjs.map +0 -1
- package/dist/Supervisor-2mPjIYYs.mjs +0 -676
- package/dist/Supervisor-2mPjIYYs.mjs.map +0 -1
- package/dist/Supervisor-CfRxbaso.d.mts +0 -317
- package/dist/Supervisor.d.mts +0 -2
- package/dist/Supervisor.mjs +0 -2
- package/dist/cdp-BMpYMWYe.mjs.map +0 -1
- package/dist/context-EGhOqjNN.mjs +0 -335
- package/dist/context-EGhOqjNN.mjs.map +0 -1
- package/dist/page-PTM8m5wC.mjs +0 -4057
- package/dist/page-PTM8m5wC.mjs.map +0 -1
- package/dist/url-Bsp2y55M.mjs +0 -264
- package/dist/url-Bsp2y55M.mjs.map +0 -1
- package/dist/workerd/index.d.mts +0 -16
- package/dist/workerd/index.mjs +0 -17
- package/src/Expect.ts +0 -180
- package/src/Plan.ts +0 -674
- package/src/Presentation.ts +0 -196
- package/src/Supervisor.ts +0 -632
- package/src/internal/supervisor/lifecycle.ts +0 -484
- package/src/workerd/index.ts +0 -17
- /package/src/{internal/page/url.ts → Url.ts} +0 -0
- /package/src/{internal/pictures → stillness}/utilsBundle.d.ts +0 -0
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
/** Replay and restore with durable read-back before repeating uncertain consequential input. */
|
|
2
|
+
import { Clock, Duration, Effect, Exit, Option, Schedule, Schema } from "effect";
|
|
3
|
+
|
|
4
|
+
import { type Action } from "../Action.ts";
|
|
5
|
+
import { type BrowserError, InvalidRequest, undispatched } from "../BrowserError.ts";
|
|
6
|
+
import type { Page, ReadyOptions, Target } from "../Page.ts";
|
|
7
|
+
import * as Stillness from "../stillness/Stillness.ts";
|
|
8
|
+
import { type Ambiguous, Drifted, type Missing } from "./choose.ts";
|
|
9
|
+
import { type Plan, Step, Receipt, Stopped, Uncertain, ReplayError } from "./data.ts";
|
|
10
|
+
import { aim, locate } from "./targets.ts";
|
|
11
|
+
|
|
12
|
+
const invalid = (detail: string) => undispatched("replay", new InvalidRequest({ detail }));
|
|
13
|
+
|
|
14
|
+
const dispatch = (
|
|
15
|
+
page: Page,
|
|
16
|
+
step: Step,
|
|
17
|
+
text: string,
|
|
18
|
+
before: Effect.Effect<void, BrowserError>,
|
|
19
|
+
): Effect.Effect<void, Missing | Ambiguous | Drifted | BrowserError> => {
|
|
20
|
+
const action = step.action;
|
|
21
|
+
const point = (target: Target) => (typeof target === "object" ? target : undefined);
|
|
22
|
+
|
|
23
|
+
const resolved: Effect.Effect<Action, Missing | Ambiguous | Drifted | BrowserError> = Effect.gen(
|
|
24
|
+
function* () {
|
|
25
|
+
switch (action._tag) {
|
|
26
|
+
case "Goto":
|
|
27
|
+
return { ...action, url: step.input === undefined ? action.url : text };
|
|
28
|
+
case "Back":
|
|
29
|
+
case "Reload":
|
|
30
|
+
return action;
|
|
31
|
+
case "Press":
|
|
32
|
+
return {
|
|
33
|
+
...action,
|
|
34
|
+
options: {
|
|
35
|
+
...action.options,
|
|
36
|
+
on: step.subject === undefined ? undefined : (yield* locate(page, step.subject)).ref,
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
case "Click":
|
|
40
|
+
case "Hover":
|
|
41
|
+
return {
|
|
42
|
+
...action,
|
|
43
|
+
target: yield* aim(page, step.subject, point(action.target), step.box),
|
|
44
|
+
};
|
|
45
|
+
case "Drag":
|
|
46
|
+
return {
|
|
47
|
+
...action,
|
|
48
|
+
from: yield* aim(page, step.subject, point(action.from)),
|
|
49
|
+
to: yield* aim(page, step.to, point(action.to)),
|
|
50
|
+
};
|
|
51
|
+
case "Select":
|
|
52
|
+
return {
|
|
53
|
+
...action,
|
|
54
|
+
ref: (yield* locate(
|
|
55
|
+
page,
|
|
56
|
+
step.subject ?? (yield* invalid("a selection needs a subject")),
|
|
57
|
+
)).ref,
|
|
58
|
+
};
|
|
59
|
+
case "Scroll":
|
|
60
|
+
return step.subject === undefined
|
|
61
|
+
? action
|
|
62
|
+
: {
|
|
63
|
+
...action,
|
|
64
|
+
options: {
|
|
65
|
+
...action.options,
|
|
66
|
+
at: yield* aim(
|
|
67
|
+
page,
|
|
68
|
+
step.subject,
|
|
69
|
+
action.options?.at === undefined ? undefined : point(action.options.at),
|
|
70
|
+
step.box,
|
|
71
|
+
),
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
case "Type":
|
|
75
|
+
return {
|
|
76
|
+
...action,
|
|
77
|
+
text: step.input === undefined ? action.text : text,
|
|
78
|
+
options: {
|
|
79
|
+
...action.options,
|
|
80
|
+
into:
|
|
81
|
+
step.subject === undefined ? undefined : (yield* locate(page, step.subject)).ref,
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
return resolved.pipe(
|
|
89
|
+
Effect.tap(() => before),
|
|
90
|
+
Effect.flatMap(page.perform),
|
|
91
|
+
Effect.asVoid,
|
|
92
|
+
);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export interface TakeOptions {
|
|
96
|
+
/** How each step waits first, except an opening navigation, or false. No final-step wait. */
|
|
97
|
+
readonly settle?: ReadyOptions | false | undefined;
|
|
98
|
+
readonly inputs?: Readonly<Record<string, string>> | undefined;
|
|
99
|
+
/**
|
|
100
|
+
* Bound for recorded arrival and expected effect, each. Defaults to 5 seconds. Zero makes one
|
|
101
|
+
* read without polling; that read retains the page operation's timeout.
|
|
102
|
+
*/
|
|
103
|
+
readonly arrival?: Duration.Input | undefined;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface ReplayOptions extends TakeOptions {
|
|
107
|
+
readonly from?: number | Stopped | undefined;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Input that can be repeated without repeating a commitment. `take` also validates a Checked
|
|
112
|
+
* target's boolean state before input. Back/reload can resubmit a form.
|
|
113
|
+
*/
|
|
114
|
+
export const repeatable = (step: Step): boolean =>
|
|
115
|
+
["Goto", "Hover", "Scroll", "Select"].includes(step.action._tag) ||
|
|
116
|
+
(step.action._tag === "Type" &&
|
|
117
|
+
step.subject !== undefined &&
|
|
118
|
+
step.action.options?.replace !== false &&
|
|
119
|
+
step.action.options?.submit !== true) ||
|
|
120
|
+
(step.action._tag === "Click" && step.expect?._tag === "Checked");
|
|
121
|
+
|
|
122
|
+
type StepError = Missing | Ambiguous | Drifted | BrowserError | Uncertain;
|
|
123
|
+
|
|
124
|
+
// Zero waits make one read; positive waits bound all reads and never repeat input.
|
|
125
|
+
const within = (check: Effect.Effect<boolean, StepError>, millis: number) =>
|
|
126
|
+
millis === 0
|
|
127
|
+
? check
|
|
128
|
+
: check.pipe(
|
|
129
|
+
Effect.repeat({ until: (held) => held, schedule: Schedule.spaced(Duration.millis(25)) }),
|
|
130
|
+
Effect.timeoutOrElse({
|
|
131
|
+
duration: Duration.millis(millis),
|
|
132
|
+
orElse: () => Effect.succeed(false),
|
|
133
|
+
}),
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
const sameSite = (url: string, recorded: string) => {
|
|
137
|
+
const origin = URL.parse(recorded)?.origin;
|
|
138
|
+
|
|
139
|
+
return (
|
|
140
|
+
recorded === "" ||
|
|
141
|
+
(origin === undefined || origin === "null"
|
|
142
|
+
? url === recorded
|
|
143
|
+
: URL.parse(url)?.origin === origin)
|
|
144
|
+
);
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const arrived = (page: Page, urls: ReadonlyArray<string>, millis: number) =>
|
|
148
|
+
within(
|
|
149
|
+
Effect.map(page.url, (url) => urls.some((recorded) => sameSite(url, recorded))),
|
|
150
|
+
millis,
|
|
151
|
+
).pipe(
|
|
152
|
+
Effect.filterOrFail(
|
|
153
|
+
(held) => held,
|
|
154
|
+
() =>
|
|
155
|
+
new Drifted({ detail: "the page did not reach the recorded site within the arrival wait" }),
|
|
156
|
+
),
|
|
157
|
+
Effect.asVoid,
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
const checkEffect = (
|
|
161
|
+
page: Page,
|
|
162
|
+
step: Step,
|
|
163
|
+
first?: { readonly url: string; readonly document: number; readonly connection: string },
|
|
164
|
+
millis = 0,
|
|
165
|
+
) => {
|
|
166
|
+
const expected = step.expect;
|
|
167
|
+
|
|
168
|
+
if (expected === undefined) return Effect.succeed(false);
|
|
169
|
+
switch (expected._tag) {
|
|
170
|
+
case "Checked":
|
|
171
|
+
return step.subject === undefined
|
|
172
|
+
? Effect.fail(invalid("a checked effect needs a subject"))
|
|
173
|
+
: Effect.flatMap(locate(page, step.subject), (found) =>
|
|
174
|
+
found.state.checked === undefined
|
|
175
|
+
? Effect.fail(invalid("a checked effect needs a checkable subject"))
|
|
176
|
+
: Effect.succeed(found.state.checked === expected.checked),
|
|
177
|
+
);
|
|
178
|
+
case "Navigated":
|
|
179
|
+
if (expected.url === undefined && first === undefined) return Effect.succeed(false);
|
|
180
|
+
|
|
181
|
+
return Effect.map(page.state, (now) =>
|
|
182
|
+
expected.url === undefined
|
|
183
|
+
? now.url !== first?.url ||
|
|
184
|
+
(now.connection === first?.connection && now.document !== first?.document)
|
|
185
|
+
: now.url === expected.url,
|
|
186
|
+
);
|
|
187
|
+
case "Appeared":
|
|
188
|
+
case "Gone":
|
|
189
|
+
return Effect.map(
|
|
190
|
+
page.until(
|
|
191
|
+
{ ...expected.query, scope: "document" },
|
|
192
|
+
expected._tag === "Appeared" ? "some" : "none",
|
|
193
|
+
millis,
|
|
194
|
+
),
|
|
195
|
+
({ met }) => met,
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Take one step. Unknown non-repeatable input is never resent: only positive durable read-back
|
|
202
|
+
* can skip it. An interrupted call remains interrupted; persist begun-before-call in the consumer
|
|
203
|
+
* and treat a missing receipt as Stopped{input: "unknown"}, including after process restart.
|
|
204
|
+
*/
|
|
205
|
+
export const take = Effect.fn("Plan.take")(function* (
|
|
206
|
+
page: Page,
|
|
207
|
+
plan: Plan,
|
|
208
|
+
at: number | Stopped,
|
|
209
|
+
options: TakeOptions = {},
|
|
210
|
+
): Effect.fn.Return<Receipt, ReplayError> {
|
|
211
|
+
const index = typeof at === "number" ? at : at.step;
|
|
212
|
+
let input: Stopped["input"] = typeof at === "number" ? "unsent" : at.input;
|
|
213
|
+
|
|
214
|
+
const work = Effect.gen(function* (): Effect.fn.Return<Receipt, StepError> {
|
|
215
|
+
const step = plan.steps[index];
|
|
216
|
+
|
|
217
|
+
if (!Number.isInteger(index) || index < 0 || step === undefined)
|
|
218
|
+
return yield* invalid("the step index is outside the plan");
|
|
219
|
+
if (!Schema.is(Step.fields.action)(step.action))
|
|
220
|
+
return yield* invalid("the step action cannot be replayed");
|
|
221
|
+
const duration = Duration.fromInput(options.arrival ?? Duration.seconds(5));
|
|
222
|
+
|
|
223
|
+
if (
|
|
224
|
+
Option.isNone(duration) ||
|
|
225
|
+
Duration.isNegative(duration.value) ||
|
|
226
|
+
!Duration.isFinite(duration.value)
|
|
227
|
+
)
|
|
228
|
+
return yield* invalid("arrival must be a finite nonnegative duration");
|
|
229
|
+
const millis = Duration.toMillis(duration.value);
|
|
230
|
+
const first = yield* page.state;
|
|
231
|
+
|
|
232
|
+
const uncertain = new Uncertain({
|
|
233
|
+
detail: "the step may have been sent; reconcile its durable effect before continuing",
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
const unresolved = input === "unknown" && !repeatable(step);
|
|
237
|
+
const expected = step.expect;
|
|
238
|
+
|
|
239
|
+
const durable =
|
|
240
|
+
expected !== undefined &&
|
|
241
|
+
(expected._tag === "Appeared" ||
|
|
242
|
+
(expected._tag === "Navigated" && typeof expected.url === "string")) &&
|
|
243
|
+
expected.durable === true;
|
|
244
|
+
|
|
245
|
+
if (
|
|
246
|
+
!durable &&
|
|
247
|
+
(unresolved || (expected !== undefined && "durable" in expected && expected.durable === true))
|
|
248
|
+
)
|
|
249
|
+
return yield* uncertain;
|
|
250
|
+
if (
|
|
251
|
+
options.settle !== false &&
|
|
252
|
+
(index > 0 || !["Goto", "Back", "Reload"].includes(step.action._tag))
|
|
253
|
+
)
|
|
254
|
+
yield* options.settle?.quietMillis === undefined
|
|
255
|
+
? page.ready(options.settle)
|
|
256
|
+
: Stillness.wait(page, {
|
|
257
|
+
...options.settle,
|
|
258
|
+
still: "content",
|
|
259
|
+
quietMillis: options.settle.quietMillis,
|
|
260
|
+
});
|
|
261
|
+
const before = plan.steps[index - 1]?.url;
|
|
262
|
+
|
|
263
|
+
if (before !== undefined)
|
|
264
|
+
yield* arrived(page, unresolved ? [before, step.url] : [before], millis);
|
|
265
|
+
const check = checkEffect(page, step);
|
|
266
|
+
|
|
267
|
+
const read =
|
|
268
|
+
millis === 0
|
|
269
|
+
? check
|
|
270
|
+
: check.pipe(
|
|
271
|
+
Effect.timeoutOrElse({
|
|
272
|
+
duration: Duration.millis(millis),
|
|
273
|
+
orElse: () =>
|
|
274
|
+
Effect.fail(new Drifted({ detail: "the expected-effect read timed out" })),
|
|
275
|
+
}),
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
const held = yield* read.pipe(Effect.mapError((error) => (unresolved ? uncertain : error)));
|
|
279
|
+
|
|
280
|
+
const receipt = (outcome: Receipt["outcome"]) =>
|
|
281
|
+
Effect.gen(function* () {
|
|
282
|
+
yield* arrived(page, [step.url], millis);
|
|
283
|
+
const now = yield* page.state;
|
|
284
|
+
|
|
285
|
+
return new Receipt({ step: index, outcome, startedAt: first.at, at: now.at });
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
if (held) return yield* receipt("skipped");
|
|
289
|
+
if (unresolved) return yield* uncertain;
|
|
290
|
+
const text = step.input === undefined ? "" : options.inputs?.[step.input];
|
|
291
|
+
|
|
292
|
+
if (text === undefined) return yield* invalid(`no text for the input "${step.input ?? ""}"`);
|
|
293
|
+
let baseline: Effect.Success<Page["state"]> | undefined;
|
|
294
|
+
|
|
295
|
+
const beforeDispatch = Effect.tap(page.state, (state) =>
|
|
296
|
+
Effect.sync(() => {
|
|
297
|
+
baseline = state;
|
|
298
|
+
}),
|
|
299
|
+
).pipe(Effect.asVoid);
|
|
300
|
+
|
|
301
|
+
const verify = Effect.suspend(() => within(checkEffect(page, step, baseline, millis), millis));
|
|
302
|
+
|
|
303
|
+
const acted = yield* Effect.exit(dispatch(page, step, text, beforeDispatch));
|
|
304
|
+
|
|
305
|
+
if (Exit.isFailure(acted)) {
|
|
306
|
+
const error = Exit.findErrorOption(acted);
|
|
307
|
+
|
|
308
|
+
if (Option.isSome(error) && error.value._tag === "BrowserError" && error.value.dispatched) {
|
|
309
|
+
input = "unknown";
|
|
310
|
+
if (expected !== undefined && (repeatable(step) || durable)) {
|
|
311
|
+
const observed = yield* Effect.exit(verify);
|
|
312
|
+
|
|
313
|
+
if (Exit.isSuccess(observed) && observed.value) return yield* receipt("verified");
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return yield* Effect.failCause(acted.cause);
|
|
318
|
+
}
|
|
319
|
+
input = "unknown";
|
|
320
|
+
if (expected !== undefined && !(yield* verify))
|
|
321
|
+
return yield* new Drifted({ detail: "the step's expected effect did not arrive" });
|
|
322
|
+
|
|
323
|
+
return yield* receipt(expected === undefined ? "taken" : "verified");
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
return yield* work.pipe(
|
|
327
|
+
Effect.provideService(Clock.Clock, page.clock),
|
|
328
|
+
Effect.mapError(
|
|
329
|
+
(reason) =>
|
|
330
|
+
new ReplayError({
|
|
331
|
+
step: index,
|
|
332
|
+
reason,
|
|
333
|
+
input,
|
|
334
|
+
receipts: [],
|
|
335
|
+
}),
|
|
336
|
+
),
|
|
337
|
+
);
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
const run = Effect.fnUntraced(function* (
|
|
341
|
+
page: Page,
|
|
342
|
+
plan: Plan,
|
|
343
|
+
options: TakeOptions,
|
|
344
|
+
from: number | Stopped,
|
|
345
|
+
until: number,
|
|
346
|
+
restoring: boolean,
|
|
347
|
+
) {
|
|
348
|
+
const receipts: Array<Receipt> = [];
|
|
349
|
+
const start = typeof from === "number" ? from : from.step;
|
|
350
|
+
|
|
351
|
+
if (
|
|
352
|
+
!Number.isInteger(start) ||
|
|
353
|
+
start < 0 ||
|
|
354
|
+
start > plan.steps.length ||
|
|
355
|
+
!Number.isInteger(until) ||
|
|
356
|
+
until < start ||
|
|
357
|
+
until > plan.steps.length
|
|
358
|
+
)
|
|
359
|
+
return yield* new ReplayError({
|
|
360
|
+
step: start,
|
|
361
|
+
input: "unsent",
|
|
362
|
+
receipts,
|
|
363
|
+
reason: invalid("the run bounds are outside the plan"),
|
|
364
|
+
});
|
|
365
|
+
// Fresh runs still ask for all input slots before changing the page. Restoration checks first.
|
|
366
|
+
if (!restoring && typeof from === "number") {
|
|
367
|
+
const unfilled = plan.steps.findIndex(
|
|
368
|
+
(step, index) =>
|
|
369
|
+
index >= start && step.input !== undefined && options.inputs?.[step.input] === undefined,
|
|
370
|
+
);
|
|
371
|
+
|
|
372
|
+
if (unfilled !== -1)
|
|
373
|
+
return yield* new ReplayError({
|
|
374
|
+
step: start,
|
|
375
|
+
input: "unsent",
|
|
376
|
+
receipts,
|
|
377
|
+
reason: invalid(`no text for the input "${plan.steps[unfilled]?.input ?? ""}"`),
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
for (let index = start; index < until; index++) {
|
|
381
|
+
const at = restoring
|
|
382
|
+
? new Stopped({ step: index, input: "unknown" })
|
|
383
|
+
: index === start
|
|
384
|
+
? from
|
|
385
|
+
: index;
|
|
386
|
+
|
|
387
|
+
const receipt = yield* take(page, plan, at, options).pipe(
|
|
388
|
+
Effect.mapError((error) => new ReplayError({ ...error, receipts: [...receipts] })),
|
|
389
|
+
);
|
|
390
|
+
|
|
391
|
+
receipts.push(receipt);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return receipts;
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
/** Take a plan's steps from a fresh index or durable stopping point, returning completed receipts. */
|
|
398
|
+
export const replay = (page: Page, plan: Plan, options: ReplayOptions = {}) =>
|
|
399
|
+
run(page, plan, options, options.from ?? 0, plan.steps.length, false).pipe(
|
|
400
|
+
Effect.withSpan("Plan.replay"),
|
|
401
|
+
);
|
|
402
|
+
|
|
403
|
+
/** Restore prior steps on a fresh page; unresolved commitments require positive durable evidence. */
|
|
404
|
+
export const restore = (
|
|
405
|
+
page: Page,
|
|
406
|
+
plan: Plan,
|
|
407
|
+
options: TakeOptions & { readonly upTo?: number | undefined } = {},
|
|
408
|
+
) =>
|
|
409
|
+
run(page, plan, options, 0, options.upTo ?? plan.steps.length, true).pipe(
|
|
410
|
+
Effect.withSpan("Plan.restore"),
|
|
411
|
+
);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A walk recorded once from a page's events and replayed later, on a fresh page, with no model
|
|
3
|
+
* call.
|
|
4
|
+
*
|
|
5
|
+
* `fromEvents` keeps one page's completed actions with their subjects and the options they were
|
|
6
|
+
* given, its navigations with both the address asked for and the one reached, and what was typed
|
|
7
|
+
* as a named input slot, never the text itself. A navigation to an address whose credentials the
|
|
8
|
+
* events withheld is an input slot too, so replay goes where the caller says, or nowhere, and
|
|
9
|
+
* never to the address without them. `replay` takes the steps in turn. Before a step
|
|
10
|
+
* except an opening navigate, back or reload, it waits for `Page.ready`; then it finds the element its
|
|
11
|
+
* subject names, by role and name among those that repeat all its recorded context, and acts on
|
|
12
|
+
* it. Text typed into focus was typed into the focused field, which replay finds the same way, and
|
|
13
|
+
* text typed into a secret field goes into nothing else. An element step never falls back to
|
|
14
|
+
* coordinates, and a step recorded at a point acts there only while the element found for it still
|
|
15
|
+
* covers that point. `take` and `replay` return receipts; failure preserves a stopping point and
|
|
16
|
+
* earlier receipts. Unknown consequential input is never resent. Only a positive durable read-back
|
|
17
|
+
* can reconcile it; restoration follows the same rule. The consumer journals begun steps before
|
|
18
|
+
* calling, treating interruption or a missing receipt as unknown. Version 3 carries canonical typed
|
|
19
|
+
* Actions. Prior string-action plans and unknown versions do not decode.
|
|
20
|
+
*
|
|
21
|
+
* @since 0.3.0
|
|
22
|
+
*/
|
|
23
|
+
export { Expected, Step, Plan, Receipt, Stopped, Uncertain, ReplayError } from "./data.ts";
|
|
24
|
+
export { fromEvents } from "./record.ts";
|
|
25
|
+
export { locate } from "./targets.ts";
|
|
26
|
+
|
|
27
|
+
export {
|
|
28
|
+
replay,
|
|
29
|
+
restore,
|
|
30
|
+
take,
|
|
31
|
+
repeatable,
|
|
32
|
+
type TakeOptions,
|
|
33
|
+
type ReplayOptions,
|
|
34
|
+
} from "./execute.ts";
|
|
35
|
+
|
|
36
|
+
export { Ambiguous, Drifted, Missing } from "./choose.ts";
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/** Derive replay steps from typed action events without parsing rendered descriptions. */
|
|
2
|
+
import { targetOf } from "../Action.ts";
|
|
3
|
+
import { type Action, type BrowserEvent, type Navigated } from "../BrowserEvent.ts";
|
|
4
|
+
import { redacted } from "../Page.ts";
|
|
5
|
+
import { Plan, Step } from "./data.ts";
|
|
6
|
+
|
|
7
|
+
const isStep = (
|
|
8
|
+
event: BrowserEvent,
|
|
9
|
+
): event is Action & {
|
|
10
|
+
readonly value: Exclude<Action["value"], { readonly _tag: "Act" }>;
|
|
11
|
+
} => event._tag === "Action" && event.ok && event.value._tag !== "Act";
|
|
12
|
+
|
|
13
|
+
/** A slot named for the field it fills, such as `email`, unique within the plan. */
|
|
14
|
+
const slotFor = (taken: Set<string>, field: string | undefined) => {
|
|
15
|
+
const base =
|
|
16
|
+
field
|
|
17
|
+
?.toLowerCase()
|
|
18
|
+
.match(/[\p{L}\p{N}]+/gu)
|
|
19
|
+
?.join("-") ?? "input";
|
|
20
|
+
|
|
21
|
+
let slot = base;
|
|
22
|
+
|
|
23
|
+
for (let count = 2; taken.has(slot); count++) slot = `${base}-${count}`;
|
|
24
|
+
taken.add(slot);
|
|
25
|
+
|
|
26
|
+
return slot;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The walk one page's events record: the completed actions of the page that acted first, in
|
|
31
|
+
* order. `page.recentEvents` gives them, or `browser.events()` read until the walk ends.
|
|
32
|
+
*/
|
|
33
|
+
export const fromEvents = (events: Iterable<BrowserEvent>): Plan => {
|
|
34
|
+
const all = Array.from(events);
|
|
35
|
+
const page = all.find((event) => event._tag === "Action")?.page;
|
|
36
|
+
const mine = all.filter((event) => "page" in event && event.page === page);
|
|
37
|
+
const steps = mine.filter(isStep);
|
|
38
|
+
const opened = mine.find((event) => event._tag === "PageOpened")?.url ?? "";
|
|
39
|
+
const moves = mine.filter((event): event is Navigated => event._tag === "Navigated");
|
|
40
|
+
const taken = new Set<string>();
|
|
41
|
+
|
|
42
|
+
// Where the page was just before a time: the last address it reached by then.
|
|
43
|
+
const before = (time: number) => moves.findLast((move) => move.at <= time)?.url ?? opened;
|
|
44
|
+
|
|
45
|
+
return new Plan({
|
|
46
|
+
version: 3,
|
|
47
|
+
steps: steps.map((action, index) => {
|
|
48
|
+
const value = action.value;
|
|
49
|
+
|
|
50
|
+
const target = targetOf(value);
|
|
51
|
+
const point = typeof target === "object" ? target : undefined;
|
|
52
|
+
const until = steps[index + 1]?.startedAt ?? Number.POSITIVE_INFINITY;
|
|
53
|
+
const arrival = moves.findLast((move) => move.at > action.at && move.at <= until);
|
|
54
|
+
|
|
55
|
+
// Refs are rebound from subjects at replay; typed text is replaced by a caller input slot.
|
|
56
|
+
return new Step({
|
|
57
|
+
action: value._tag === "Type" ? { ...value, text: "" } : value,
|
|
58
|
+
url: arrival?.url ?? action.url ?? before(until),
|
|
59
|
+
subject: action.subject,
|
|
60
|
+
to: action.to,
|
|
61
|
+
box: point === undefined ? undefined : action.box,
|
|
62
|
+
input:
|
|
63
|
+
value._tag === "Type"
|
|
64
|
+
? slotFor(taken, action.subject?.name)
|
|
65
|
+
: value._tag === "Goto" && value.url.includes(redacted)
|
|
66
|
+
? slotFor(taken, "address")
|
|
67
|
+
: undefined,
|
|
68
|
+
});
|
|
69
|
+
}),
|
|
70
|
+
});
|
|
71
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/** Find a recorded subject and verify where its point still lands before replay input. */
|
|
2
|
+
import { Clock, Duration, Effect, Schedule } from "effect";
|
|
3
|
+
|
|
4
|
+
import { InvalidRequest, undispatched } from "../BrowserError.ts";
|
|
5
|
+
import { type Box, type Subject } from "../BrowserEvent.ts";
|
|
6
|
+
import type { Page, Found, Point, Target } from "../Page.ts";
|
|
7
|
+
import { choose, Drifted } from "./choose.ts";
|
|
8
|
+
|
|
9
|
+
const invalid = (detail: string) => undispatched("replay", new InvalidRequest({ detail }));
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The one element on the page that a subject names, by role, name and context (see `Plan`),
|
|
13
|
+
* looking again for two seconds while the page may still be drawing it.
|
|
14
|
+
*/
|
|
15
|
+
export const locate = (page: Page, subject: Subject) =>
|
|
16
|
+
page
|
|
17
|
+
.find({
|
|
18
|
+
name: subject.name,
|
|
19
|
+
scope: "document",
|
|
20
|
+
...(subject.role === null ? {} : { role: subject.role }),
|
|
21
|
+
})
|
|
22
|
+
.pipe(
|
|
23
|
+
Effect.flatMap((found) => choose(subject, found)),
|
|
24
|
+
Effect.retry({
|
|
25
|
+
schedule: Schedule.spaced(Duration.millis(250)),
|
|
26
|
+
times: 8,
|
|
27
|
+
while: (error) => error._tag !== "BrowserError",
|
|
28
|
+
}),
|
|
29
|
+
Effect.provideService(Clock.Clock, page.clock),
|
|
30
|
+
Effect.withSpan("Plan.locate", { attributes: { role: subject.role ?? "" } }),
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Where the step acts: the element its subject names, or, for a step recorded at a point, the same
|
|
35
|
+
* place within that element, as long as a press there reaches that element and not one over it.
|
|
36
|
+
* An element out of view is first brought into it, by hovering it, as a press by ref would be.
|
|
37
|
+
*/
|
|
38
|
+
export const aim = (page: Page, subject: Subject | undefined, point?: Point, recorded?: Box) => {
|
|
39
|
+
if (subject === undefined) return Effect.fail(invalid("the step names no element"));
|
|
40
|
+
if (point === undefined) return Effect.map(locate(page, subject), ({ ref }): Target => ref);
|
|
41
|
+
|
|
42
|
+
const reach = ({ ref, box }: Found) => {
|
|
43
|
+
const at =
|
|
44
|
+
recorded === undefined || recorded.width <= 0 || recorded.height <= 0
|
|
45
|
+
? point
|
|
46
|
+
: {
|
|
47
|
+
x: box.x + ((point.x - recorded.x) / recorded.width) * box.width,
|
|
48
|
+
y: box.y + ((point.y - recorded.y) / recorded.height) * box.height,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
return Effect.map(page.find({ at }), ([reached]) => (reached?.ref === ref ? at : undefined));
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const missed = new Drifted({ detail: "a press at the subject's point reaches another element" });
|
|
55
|
+
|
|
56
|
+
return locate(page, subject).pipe(
|
|
57
|
+
Effect.flatMap((found) =>
|
|
58
|
+
Effect.flatMap(reach(found), (at) =>
|
|
59
|
+
at !== undefined
|
|
60
|
+
? Effect.succeed<Target>(at)
|
|
61
|
+
: page.hover(found.ref).pipe(
|
|
62
|
+
Effect.andThen(locate(page, subject)),
|
|
63
|
+
Effect.flatMap(reach),
|
|
64
|
+
Effect.flatMap((again) =>
|
|
65
|
+
again === undefined ? Effect.fail(missed) : Effect.succeed<Target>(again),
|
|
66
|
+
),
|
|
67
|
+
),
|
|
68
|
+
),
|
|
69
|
+
),
|
|
70
|
+
);
|
|
71
|
+
};
|
|
@@ -12,15 +12,15 @@
|
|
|
12
12
|
* only as evidence, set apart. It never sees the agent's own words, so an agent cannot argue it
|
|
13
13
|
* round, and a judgement only adds to what structure establishes: a `secret` fact is a secret
|
|
14
14
|
* whatever the judge reads. A judge that fails or runs out of time leaves the input unjudged,
|
|
15
|
-
* which `make` refuses
|
|
15
|
+
* which `make` refuses.
|
|
16
16
|
*
|
|
17
17
|
* @since 0.3.0
|
|
18
18
|
*/
|
|
19
19
|
import { Cause, Context, Duration, Effect, Record, Result, Schema } from "effect";
|
|
20
20
|
import { type AiError, Decision, DecisionModel, LanguageModel, Prompt } from "effect/ai";
|
|
21
21
|
|
|
22
|
-
import { PolicyDenied } from "
|
|
23
|
-
import { Fact, type InputGuard, InputRequest } from "
|
|
22
|
+
import { PolicyDenied } from "../BrowserError.ts";
|
|
23
|
+
import { Fact, type InputGuard, InputRequest } from "../Page.ts";
|
|
24
24
|
|
|
25
25
|
/** What an input means. Only a judge reading the evidence can recognise these. */
|
|
26
26
|
export const Risk = Schema.Literals([
|
|
@@ -66,7 +66,7 @@ export class Judgement extends Schema.Class<Judgement>("effect-browser/Judgement
|
|
|
66
66
|
}) {}
|
|
67
67
|
|
|
68
68
|
/**
|
|
69
|
-
* The user's task: what authorizes a risky input. `
|
|
69
|
+
* The user's task: what authorizes a risky input. `PageControl` provides its task to every input it
|
|
70
70
|
* makes; provide one around other calls with `Effect.provideService`.
|
|
71
71
|
*/
|
|
72
72
|
export const Task = Context.Reference<string | undefined>("effect-browser/Policy/Task", {
|
|
@@ -79,7 +79,7 @@ export type Judge = (request: InputRequest) => Effect.Effect<Judgement, AiError.
|
|
|
79
79
|
/** What a judge is shown. Everything under `evidence` is the page's to write. */
|
|
80
80
|
const Review = Schema.Struct({
|
|
81
81
|
task: Schema.NullOr(Schema.String),
|
|
82
|
-
action:
|
|
82
|
+
action: InputRequest.fields.action,
|
|
83
83
|
text: Schema.optional(Schema.String),
|
|
84
84
|
facts: Schema.Array(Fact),
|
|
85
85
|
evidence: Schema.Struct({
|
|
@@ -241,7 +241,7 @@ export interface Options {
|
|
|
241
241
|
readonly deny?: ReadonlyArray<Fact> | undefined;
|
|
242
242
|
/** Origins input may act on and go to, such as `"https://shop.example"`. Defaults to any. */
|
|
243
243
|
readonly origins?: ReadonlyArray<string> | undefined;
|
|
244
|
-
/** The probability from which a judge's reading counts. Defaults to 0.5. */
|
|
244
|
+
/** The probability from which a judge's reading counts: finite, greater than 0, at most 1. Defaults to 0.5. */
|
|
245
245
|
readonly threshold?: number | undefined;
|
|
246
246
|
/** How long the judge may take before the input counts as unjudged. Defaults to 30 seconds. */
|
|
247
247
|
readonly timeout?: Duration.Input | undefined;
|
|
@@ -259,18 +259,22 @@ const originOf = (url: string) => {
|
|
|
259
259
|
* A guard for a browser with nobody to ask. It allows hovering and scrolling. Otherwise it denies
|
|
260
260
|
* input on a page or towards a destination outside `origins`, input with a fact in `deny`, and,
|
|
261
261
|
* when a judge finds a risk, input the task does not ask for. An input the judge could not judge
|
|
262
|
-
* in time is denied
|
|
263
|
-
* allowed when structure shows nothing.
|
|
262
|
+
* in time is denied with the judge's failure as the denial's cause.
|
|
264
263
|
*/
|
|
265
264
|
export const make = (options: Options = {}): InputGuard => {
|
|
266
265
|
const threshold = options.threshold ?? 0.5;
|
|
266
|
+
const validThreshold = Schema.is(Probability.check(Schema.isGreaterThan(0)))(threshold);
|
|
267
267
|
const watched = options.risks ?? Risk.literals;
|
|
268
268
|
const timeout = options.timeout ?? Duration.seconds(30);
|
|
269
269
|
const origins = options.origins?.map(originOf);
|
|
270
270
|
|
|
271
271
|
return (request) =>
|
|
272
272
|
Effect.gen(function* () {
|
|
273
|
-
if (request.action === "
|
|
273
|
+
if (request.action._tag === "Hover" || request.action._tag === "Scroll") return;
|
|
274
|
+
if (!validThreshold)
|
|
275
|
+
return yield* new PolicyDenied({
|
|
276
|
+
detail: "policy threshold must be finite, greater than 0, and at most 1",
|
|
277
|
+
});
|
|
274
278
|
|
|
275
279
|
for (const url of [request.url, request.destination]) {
|
|
276
280
|
// A blank or inline page has no origin to keep to; where it sends input still does.
|
|
@@ -288,7 +292,6 @@ export const make = (options: Options = {}): InputGuard => {
|
|
|
288
292
|
const judged = yield* options.judge(request).pipe(Effect.timeout(timeout), Effect.result);
|
|
289
293
|
|
|
290
294
|
if (Result.isFailure(judged)) {
|
|
291
|
-
if (request.facts.length === 0) return;
|
|
292
295
|
const failure = judged.failure;
|
|
293
296
|
|
|
294
297
|
return yield* new PolicyDenied({
|