effect-browser 0.3.0-beta.23 → 0.3.0-beta.25
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 +437 -142
- package/dist/Action.d.mts +1 -1
- package/dist/Action.mjs +61 -9
- package/dist/Action.mjs.map +1 -1
- package/dist/Browser.d.mts +2 -2
- package/dist/Browser.mjs +2 -2
- package/dist/BrowserError.d.mts +2 -2
- package/dist/BrowserError.mjs +194 -41
- package/dist/BrowserError.mjs.map +1 -1
- package/dist/BrowserEvent.d.mts +2 -2
- package/dist/BrowserEvent.mjs +185 -35
- package/dist/BrowserEvent.mjs.map +1 -1
- package/dist/Cdp.d.mts +15 -0
- package/dist/Cdp.mjs +22 -0
- package/dist/Cdp.mjs.map +1 -0
- package/dist/Change.d.mts +2 -2
- package/dist/Change.mjs +40 -7
- package/dist/Change.mjs.map +1 -1
- package/dist/Frame.d.mts +2 -2
- package/dist/Frame.mjs +132 -14
- package/dist/Frame.mjs.map +1 -1
- package/dist/InputStyle.d.mts +153 -0
- package/dist/InputStyle.mjs +2 -0
- package/dist/Layout.d.mts +2 -0
- package/dist/Layout.mjs +123 -0
- package/dist/Layout.mjs.map +1 -0
- package/dist/Overlay.d.mts +2 -0
- package/dist/Overlay.mjs +58 -0
- package/dist/Overlay.mjs.map +1 -0
- package/dist/Page.d.mts +2 -2
- package/dist/Page.mjs +2 -2
- package/dist/PageTime.d.mts +2 -0
- package/dist/PageTime.mjs +17 -0
- package/dist/PageTime.mjs.map +1 -0
- package/dist/Pose.d.mts +2 -0
- package/dist/Pose.mjs +65 -0
- package/dist/Pose.mjs.map +1 -0
- package/dist/Site.d.mts +984 -0
- package/dist/Site.mjs +110 -0
- package/dist/Site.mjs.map +1 -0
- package/dist/Snapshot.d.mts +2 -2
- package/dist/Snapshot.mjs +87 -6
- package/dist/Snapshot.mjs.map +1 -1
- package/dist/Text.d.mts +2 -0
- package/dist/Text.mjs +45 -0
- package/dist/Text.mjs.map +1 -0
- package/dist/Window.d.mts +2 -2
- package/dist/Window.mjs +31 -6
- package/dist/Window.mjs.map +1 -1
- package/dist/ai/BrowserTools.d.mts +4987 -0
- package/dist/ai/BrowserTools.mjs +3 -0
- package/dist/ai/PageControl.d.mts +286 -0
- package/dist/ai/PageControl.mjs +426 -0
- package/dist/ai/PageControl.mjs.map +1 -0
- package/dist/ai/PromptCache.d.mts +16 -0
- package/dist/ai/PromptCache.mjs +72 -0
- package/dist/ai/PromptCache.mjs.map +1 -0
- package/dist/ai/Tabs.d.mts +2 -0
- package/dist/ai/Tabs.mjs +72 -0
- package/dist/ai/Tabs.mjs.map +1 -0
- package/dist/ai/ToolError.d.mts +34 -0
- package/dist/ai/ToolError.mjs +2 -0
- package/dist/ai/index.d.mts +6 -0
- package/dist/ai/index.mjs +6 -0
- package/dist/index.d.mts +7 -15
- package/dist/index.mjs +9 -8
- package/dist/{Action-24sLOubi.d.mts → internal/Action-CkanNTUw.d.mts} +64 -4
- package/dist/{Browser-wZhwZSlG.d.mts → internal/Browser-DD9Vm1c-.d.mts} +80 -10
- package/dist/{Browser-BKPAbyys.mjs → internal/Browser-vzD7i2oj.mjs} +658 -495
- package/dist/internal/Browser-vzD7i2oj.mjs.map +1 -0
- package/dist/{BrowserError-BlcmLVzn.d.mts → internal/BrowserError-CsuRYcDX.d.mts} +187 -28
- package/dist/{BrowserEvent-lLqJ3GJS.d.mts → internal/BrowserEvent-tGPYYf6R.d.mts} +197 -18
- package/dist/internal/BrowserTools-Yi3lOX4h.mjs +497 -0
- package/dist/internal/BrowserTools-Yi3lOX4h.mjs.map +1 -0
- package/dist/{Style-AO4V7Czt.mjs → internal/InputStyle-CPzxoN4g.mjs} +94 -16
- package/dist/internal/InputStyle-CPzxoN4g.mjs.map +1 -0
- package/dist/{moment/Moment.mjs → internal/Moment-CstaX1mR.mjs} +49 -16
- package/dist/internal/Moment-CstaX1mR.mjs.map +1 -0
- package/dist/{moment/Moment.d.mts → internal/Moment-DOcldBdf.d.mts} +54 -21
- package/dist/internal/Page-BfGxn7oN.d.mts +1553 -0
- package/dist/{Page-BF5izr8R.mjs → internal/Page-DqF-tuTx.mjs} +62 -72
- package/dist/internal/Page-DqF-tuTx.mjs.map +1 -0
- package/dist/internal/Release-CFONkPDP.d.mts +39 -0
- package/dist/internal/Stage-D5ymSXDT.mjs +298 -0
- package/dist/internal/Stage-D5ymSXDT.mjs.map +1 -0
- package/dist/internal/Stillness-DCPeAN1a.mjs +1124 -0
- package/dist/internal/Stillness-DCPeAN1a.mjs.map +1 -0
- package/dist/internal/Supervisor-BcY_QNIY.d.mts +108 -0
- package/dist/internal/Supervisor-BcglY23c.mjs +531 -0
- package/dist/internal/Supervisor-BcglY23c.mjs.map +1 -0
- package/dist/internal/Tabs-dXMscIUQ.d.mts +52 -0
- package/dist/internal/TestPage-CYiYbh63.d.mts +159 -0
- package/dist/internal/ToolError-DArsRS2m.mjs +117 -0
- package/dist/internal/ToolError-DArsRS2m.mjs.map +1 -0
- package/dist/{plan/index.d.mts → internal/Walk-BNXcfZ2z.d.mts} +59 -35
- package/dist/{plan/index.mjs → internal/Walk-C5ky7uSf.mjs} +83 -51
- package/dist/internal/Walk-C5ky7uSf.mjs.map +1 -0
- package/dist/{ActionPort.mjs → internal/action-port-B5EkeGOR.mjs} +6 -12
- package/dist/internal/action-port-B5EkeGOR.mjs.map +1 -0
- package/dist/internal/cdp/workerd.d.mts +13 -0
- package/dist/internal/cdp/workerd.mjs +19 -0
- package/dist/internal/cdp/workerd.mjs.map +1 -0
- package/dist/{Clock.mjs → internal/clock-BKSAEf00.mjs} +13 -13
- package/dist/internal/clock-BKSAEf00.mjs.map +1 -0
- package/dist/{connection-2KHI4_KN.d.mts → internal/connection-BaB0vp1o.d.mts} +6 -6
- package/dist/{connection-CK83pvqP.mjs → internal/connection-C2X4GmXM.mjs} +19 -15
- package/dist/internal/connection-C2X4GmXM.mjs.map +1 -0
- package/dist/internal/events-DzCrww-y.mjs +72 -0
- package/dist/internal/events-DzCrww-y.mjs.map +1 -0
- package/dist/internal/fold.inpage-CyVZQHZL.d.mts +49 -0
- package/dist/internal/fold.inpage-uhrT-dOB.mjs +49 -0
- package/dist/internal/fold.inpage-uhrT-dOB.mjs.map +1 -0
- package/dist/internal/functions-jBOs6GKv.mjs +27 -0
- package/dist/internal/functions-jBOs6GKv.mjs.map +1 -0
- package/dist/{Geometry-DJx9qrYy.d.mts → internal/geometry-Dhj_ztJs.d.mts} +3 -3
- package/dist/{Geometry.mjs → internal/geometry-Lxyflb9J.mjs} +3 -12
- package/dist/internal/geometry-Lxyflb9J.mjs.map +1 -0
- package/dist/{Matching.mjs → internal/matching-Cs-UeOY0.mjs} +24 -19
- package/dist/internal/matching-Cs-UeOY0.mjs.map +1 -0
- package/dist/internal/page-DFQb-YOK.mjs +436 -0
- package/dist/internal/page-DFQb-YOK.mjs.map +1 -0
- package/dist/internal/pose.inpage-CBFeWcVa.mjs +1996 -0
- package/dist/internal/pose.inpage-CBFeWcVa.mjs.map +1 -0
- package/dist/{Url.mjs → internal/url-DB0pQYk6.mjs} +3 -12
- package/dist/internal/url-DB0pQYk6.mjs.map +1 -0
- package/dist/policy/Policy.d.mts +60 -6
- package/dist/policy/Policy.mjs +52 -9
- package/dist/policy/Policy.mjs.map +1 -1
- package/dist/policy/index.d.mts +2 -0
- package/dist/policy/index.mjs +2 -0
- package/dist/presentation/Film.d.mts +332 -0
- package/dist/presentation/Film.mjs +298 -0
- package/dist/presentation/Film.mjs.map +1 -0
- package/dist/presentation/Motion.d.mts +5 -1
- package/dist/presentation/Motion.mjs +16 -2
- package/dist/presentation/Motion.mjs.map +1 -1
- package/dist/presentation/Presentation.d.mts +48 -6
- package/dist/presentation/Presentation.mjs +28 -9
- package/dist/presentation/Presentation.mjs.map +1 -1
- package/dist/presentation/Stage.d.mts +40 -6
- package/dist/presentation/Stage.mjs +1 -262
- package/dist/presentation/index.d.mts +5 -0
- package/dist/presentation/index.mjs +5 -0
- package/dist/supervisor/Release.d.mts +2 -0
- package/dist/supervisor/Release.mjs +40 -0
- package/dist/supervisor/Release.mjs.map +1 -0
- package/dist/supervisor/Supervisor.d.mts +2 -104
- package/dist/supervisor/Supervisor.mjs +2 -515
- package/dist/supervisor/index.d.mts +3 -0
- package/dist/supervisor/index.mjs +3 -0
- package/dist/testing/HttpCassette.d.mts +98 -0
- package/dist/testing/HttpCassette.mjs +146 -0
- package/dist/testing/HttpCassette.mjs.map +1 -0
- package/dist/testing/Story.d.mts +31 -0
- package/dist/testing/Story.mjs +76 -0
- package/dist/testing/Story.mjs.map +1 -0
- package/dist/testing/TestBrowser.d.mts +191 -0
- package/dist/testing/TestBrowser.mjs +105 -0
- package/dist/testing/TestBrowser.mjs.map +1 -0
- package/dist/testing/TestModel.d.mts +133 -0
- package/dist/testing/TestModel.mjs +209 -0
- package/dist/testing/TestModel.mjs.map +1 -0
- package/dist/testing/TestPage.d.mts +2 -0
- package/dist/testing/TestPage.mjs +47 -0
- package/dist/testing/TestPage.mjs.map +1 -0
- package/dist/testing/index.d.mts +6 -337
- package/dist/testing/index.mjs +6 -515
- package/dist/timeline/Moment.d.mts +3 -0
- package/dist/timeline/Moment.mjs +3 -0
- package/dist/timeline/Stillness.d.mts +34 -0
- package/dist/timeline/Stillness.mjs +2 -0
- package/dist/timeline/index.d.mts +3 -0
- package/dist/timeline/index.mjs +3 -0
- package/dist/walk/Walk.d.mts +2 -0
- package/dist/walk/Walk.mjs +2 -0
- package/dist/walk/index.d.mts +2 -0
- package/dist/walk/index.mjs +2 -0
- package/package.json +54 -133
- package/src/Action.ts +73 -8
- package/src/Browser.ts +85 -14
- package/src/BrowserError.ts +258 -63
- package/src/BrowserEvent.ts +264 -48
- package/src/Cdp.ts +42 -0
- package/src/Change.ts +75 -4
- package/src/Frame.ts +247 -14
- package/src/{Style.ts → InputStyle.ts} +130 -18
- package/src/Layout.ts +187 -0
- package/src/Overlay.ts +76 -0
- package/src/Page.ts +144 -275
- package/src/PageTime.ts +72 -0
- package/src/Pose.ts +81 -0
- package/src/Site.ts +136 -0
- package/src/Snapshot.ts +179 -3
- package/src/Text.ts +60 -0
- package/src/Window.ts +53 -3
- package/src/ai/BrowserTools.ts +778 -0
- package/src/ai/PageControl.ts +702 -0
- package/src/ai/PromptCache.ts +87 -0
- package/src/ai/Tabs.ts +84 -0
- package/src/ai/ToolError.ts +50 -0
- package/src/ai/index.ts +36 -0
- package/src/ai/internal/crops.ts +38 -0
- package/src/ai/internal/points.ts +57 -0
- package/src/ai/internal/receipts.ts +107 -0
- package/src/index.ts +87 -6
- package/src/{ActionPort.ts → internal/action-port.ts} +3 -3
- package/src/{providers → internal/cdp}/connection.ts +19 -12
- package/src/internal/cdp/workerd.ts +36 -0
- package/src/{Clock.ts → internal/clock.ts} +20 -0
- package/src/internal/input/actions.ts +1 -1
- package/src/internal/input/dispatch.ts +2 -2
- package/src/internal/input/guard.inpage.ts +1 -1
- package/src/internal/input/guard.ts +1 -1
- package/src/internal/input/perform.ts +35 -7
- package/src/internal/input/pointer.ts +9 -6
- package/src/internal/input/targets.inpage.ts +3 -3
- package/src/internal/input/targets.ts +1 -1
- package/src/{Matching.ts → internal/matching.ts} +28 -19
- package/src/internal/page/bridge.ts +67 -35
- package/src/internal/page/clock.inpage.ts +306 -0
- package/src/internal/page/compose.inpage.ts +101 -0
- package/src/internal/page/context.ts +4 -2
- package/src/internal/page/functions.ts +84 -0
- package/src/internal/page/navigation.ts +1 -1
- package/src/internal/page/openings.ts +2 -2
- package/src/internal/page/overlay.inpage.ts +96 -0
- package/src/internal/page/overlay.ts +89 -0
- package/src/internal/page/page.ts +39 -29
- package/src/internal/page/port.ts +14 -8
- package/src/internal/page/pose.inpage.ts +200 -0
- package/src/internal/page/pose.ts +80 -0
- package/src/internal/page/replies.ts +1 -1
- package/src/internal/page/requests.ts +1 -1
- package/src/internal/page/scripts.ts +1 -1
- package/src/internal/page/time.inpage.ts +49 -0
- package/src/internal/page/time.ts +122 -0
- package/src/internal/page/viewport.ts +3 -2
- package/src/internal/pictures/capture.ts +1 -4
- package/src/internal/pictures/dimensions.ts +1 -1
- package/src/internal/pictures/frame.ts +1 -1
- package/src/internal/pictures/pictures.ts +144 -91
- package/src/internal/reading/context.inpage.ts +2 -2
- package/src/internal/reading/expect.inpage.ts +1 -1
- package/src/internal/reading/layout.inpage.ts +193 -0
- package/src/internal/reading/layout.ts +101 -0
- package/src/internal/reading/names.inpage.ts +15 -0
- package/src/internal/reading/outline.inpage.ts +5 -21
- package/src/internal/reading/reading.ts +33 -21
- package/src/internal/reading/ready.inpage.ts +21 -26
- package/src/internal/reading/ready.ts +26 -5
- package/src/internal/reading/subjects.inpage.ts +3 -3
- package/src/internal/reading/text.inpage.ts +3 -16
- package/src/internal/reading/walk.inpage.ts +1 -1
- package/src/internal/timeline/changes.inpage.ts +7 -15
- package/src/internal/timeline/changes.ts +17 -5
- package/src/internal/timeline/record.inpage.ts +15 -6
- package/src/internal/timeline/sight.inpage.ts +2 -2
- package/src/internal/timeline/window.ts +3 -4
- package/src/policy/Policy.ts +70 -16
- package/src/policy/index.ts +12 -0
- package/src/presentation/Film.ts +451 -0
- package/src/presentation/Motion.ts +45 -3
- package/src/presentation/Presentation.ts +60 -14
- package/src/presentation/Stage.ts +73 -35
- package/src/presentation/index.ts +30 -0
- package/src/presentation/{feed.ts → internal/feed.ts} +1 -1
- package/src/supervisor/Release.ts +45 -0
- package/src/supervisor/Supervisor.ts +80 -5
- package/src/supervisor/index.ts +18 -0
- package/src/supervisor/{contract.ts → internal/contract.ts} +14 -10
- package/src/supervisor/{lifecycle.ts → internal/lifecycle.ts} +7 -7
- package/src/supervisor/{runtime.ts → internal/runtime.ts} +7 -7
- package/src/testing/HttpCassette.ts +260 -0
- package/src/testing/Story.ts +103 -0
- package/src/testing/TestBrowser.ts +14 -4
- package/src/testing/TestModel.ts +297 -0
- package/src/testing/TestPage.ts +92 -4
- package/src/testing/index.ts +35 -1
- package/src/testing/{evidence.ts → internal/evidence.ts} +19 -11
- package/src/testing/{model.ts → internal/model.ts} +14 -8
- package/src/testing/{page.ts → internal/page.ts} +55 -51
- package/src/{moment → timeline}/Moment.ts +79 -29
- package/src/{stillness → timeline}/Stillness.ts +89 -46
- package/src/timeline/index.ts +18 -0
- package/src/{stillness/Pixels.ts → timeline/internal/pixels.ts} +27 -1
- package/src/{moment → timeline/internal}/words.ts +2 -2
- package/src/walk/Walk.ts +165 -0
- package/src/walk/index.ts +12 -0
- package/src/{plan → walk/internal}/choose.ts +23 -14
- package/src/{plan → walk/internal}/data.ts +37 -14
- package/src/{plan → walk/internal}/execute.ts +79 -36
- package/src/{plan → walk/internal}/record.ts +6 -6
- package/src/{plan → walk/internal}/targets.ts +35 -25
- package/dist/ActionPort.d.mts +0 -15
- package/dist/ActionPort.mjs.map +0 -1
- package/dist/Browser-BKPAbyys.mjs.map +0 -1
- package/dist/Change-Ba7INmay.d.mts +0 -69
- package/dist/Clock.d.mts +0 -57
- package/dist/Clock.mjs.map +0 -1
- package/dist/Frame-UEH2hO5T.d.mts +0 -124
- package/dist/Geometry.d.mts +0 -2
- package/dist/Geometry.mjs.map +0 -1
- package/dist/Matching.d.mts +0 -37
- package/dist/Matching.mjs.map +0 -1
- package/dist/Page-BF5izr8R.mjs.map +0 -1
- package/dist/Page-CvsOEorc.d.mts +0 -566
- package/dist/Released-tURbFmal.d.mts +0 -15
- package/dist/Snapshot-CtBVOtuu.d.mts +0 -73
- package/dist/Stillness-CNlvayIo.mjs +0 -136
- package/dist/Stillness-CNlvayIo.mjs.map +0 -1
- package/dist/Style-AO4V7Czt.mjs.map +0 -1
- package/dist/Style.d.mts +0 -72
- package/dist/Style.mjs +0 -2
- package/dist/Url.d.mts +0 -26
- package/dist/Url.mjs.map +0 -1
- package/dist/Window-CMddGMt1.d.mts +0 -34
- package/dist/connection-CK83pvqP.mjs.map +0 -1
- package/dist/moment/Moment.mjs.map +0 -1
- package/dist/plan/index.mjs.map +0 -1
- package/dist/presentation/Stage.mjs.map +0 -1
- package/dist/presentation/feed.d.mts +0 -20
- package/dist/presentation/feed.mjs +0 -18
- package/dist/presentation/feed.mjs.map +0 -1
- package/dist/providers/Cdp.d.mts +0 -9
- package/dist/providers/Cdp.mjs +0 -15
- package/dist/providers/Cdp.mjs.map +0 -1
- package/dist/providers/Chromium.d.mts +0 -32
- package/dist/providers/Chromium.mjs +0 -55
- package/dist/providers/Chromium.mjs.map +0 -1
- package/dist/providers/Released.d.mts +0 -2
- package/dist/providers/Released.mjs +0 -12
- package/dist/providers/Released.mjs.map +0 -1
- package/dist/stillness/Stillness.d.mts +0 -12
- package/dist/stillness/Stillness.mjs +0 -2
- package/dist/supervisor/Supervisor.mjs.map +0 -1
- package/dist/testing/index.mjs.map +0 -1
- package/dist/workerd/Cdp.d.mts +0 -8
- package/dist/workerd/Cdp.mjs +0 -13
- package/dist/workerd/Cdp.mjs.map +0 -1
- package/src/plan/index.ts +0 -36
- package/src/providers/Cdp.ts +0 -16
- package/src/providers/Chromium.ts +0 -99
- package/src/providers/Released.ts +0 -13
- package/src/workerd/Cdp.ts +0 -11
- /package/dist/{rolldown-runtime-D7D4PA-g.mjs → internal/rolldown-runtime-D7D4PA-g.mjs} +0 -0
- /package/src/{Geometry.ts → internal/geometry.ts} +0 -0
- /package/src/{Url.ts → internal/url.ts} +0 -0
- /package/src/{stillness → timeline/internal}/utilsBundle.d.ts +0 -0
|
@@ -0,0 +1,702 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* effect-browser pages as the controller `BrowserTools` serves: `actions`, which observe a page and
|
|
3
|
+
* act on the refs it observed, and `control`, which inspects, navigates, scrolls, waits, presses
|
|
4
|
+
* keys and selects tabs. Their observations and results are this module's schemas, and they fail
|
|
5
|
+
* with `ToolError`. Yielded Agent defined them as its `BrowserActions` and `BrowserControl` ports
|
|
6
|
+
* until 0.1.0-beta.174 replaced those with Code Mode over CDP; they keep the shapes they had there.
|
|
7
|
+
*
|
|
8
|
+
* Over a `page`, the controller is pinned to it: it lists no tabs, and a tab the page opens stays
|
|
9
|
+
* where it is. Over a `browser`, it acts on the tab the model last saw and lists the others as tabs
|
|
10
|
+
* it may select. A tab an action opens is shown at the next look as `follow` says: `"select"`, the
|
|
11
|
+
* default, makes it current without bringing it to front, so the page on air and an operator's
|
|
12
|
+
* view stay where they are; `"front"` also brings it to front; `"never"` keeps the current tab.
|
|
13
|
+
*
|
|
14
|
+
* An observation is the page's outline, with a ref on each control, and the controls as values. One
|
|
15
|
+
* after an action begins with what followed it: a dialog and how the browser answered it, a
|
|
16
|
+
* navigation, a tab it opened and what its input visibly changed. Every action goes through the
|
|
17
|
+
* page's own input policy, with `task` as the user's task its judge reads, and the browser answers
|
|
18
|
+
* dialogs itself. With `changes`, its outline holds only the lines that changed since the tab's last
|
|
19
|
+
* whole look, when those are fewer. An action that failed before its input reached the browser is `not-dispatched`,
|
|
20
|
+
* one that failed after is `possibly-dispatched`, and nothing is ever retried.
|
|
21
|
+
*
|
|
22
|
+
* @stability stable
|
|
23
|
+
* @since 0.3.0
|
|
24
|
+
*/
|
|
25
|
+
import { Duration, Effect, Option, Result, Schema } from "effect";
|
|
26
|
+
|
|
27
|
+
import type * as Browser from "../Browser.ts";
|
|
28
|
+
import type { BrowserError } from "../BrowserError.ts";
|
|
29
|
+
import * as Change from "../Change.ts";
|
|
30
|
+
import { millisOf } from "../internal/clock.ts";
|
|
31
|
+
import type * as Page from "../Page.ts";
|
|
32
|
+
import * as Policy from "../policy/Policy.ts";
|
|
33
|
+
import * as Snapshot from "../Snapshot.ts";
|
|
34
|
+
import { destination, dispatchOf, followed, told } from "./internal/receipts.ts";
|
|
35
|
+
import * as Tabs from "./Tabs.ts";
|
|
36
|
+
import { fromBrowserError, ToolError } from "./ToolError.ts";
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* A control's ref in an observation, such as `e12`.
|
|
40
|
+
*
|
|
41
|
+
* @stability stable
|
|
42
|
+
* @category schemas
|
|
43
|
+
* @since 0.3.0
|
|
44
|
+
*/
|
|
45
|
+
export const Ref = Schema.String.check(Schema.isPattern(/^[a-z][a-z0-9-]{0,50}$/));
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* An action on an observed control.
|
|
49
|
+
*
|
|
50
|
+
* @stability stable
|
|
51
|
+
* @category schemas
|
|
52
|
+
* @since 0.3.0
|
|
53
|
+
*/
|
|
54
|
+
export const Action = Schema.Union([
|
|
55
|
+
Schema.Struct({ kind: Schema.Literal("click"), ref: Ref }),
|
|
56
|
+
Schema.Struct({
|
|
57
|
+
kind: Schema.Literal("fill"),
|
|
58
|
+
ref: Ref,
|
|
59
|
+
value: Schema.String.check(Schema.isMaxLength(64 * 1024)),
|
|
60
|
+
}),
|
|
61
|
+
Schema.Struct({
|
|
62
|
+
kind: Schema.Literal("select"),
|
|
63
|
+
ref: Ref,
|
|
64
|
+
value: Schema.String.check(Schema.isMaxLength(4_096)),
|
|
65
|
+
}),
|
|
66
|
+
]);
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @stability stable
|
|
70
|
+
* @category models
|
|
71
|
+
* @since 0.3.0
|
|
72
|
+
*/
|
|
73
|
+
export type Action = typeof Action.Type;
|
|
74
|
+
|
|
75
|
+
/** A control as an observation lists it. */
|
|
76
|
+
const Control = Schema.Struct({
|
|
77
|
+
ref: Ref,
|
|
78
|
+
kind: Schema.String,
|
|
79
|
+
name: Schema.String,
|
|
80
|
+
value: Schema.String,
|
|
81
|
+
options: Schema.Array(Schema.String),
|
|
82
|
+
optionCount: Schema.optionalKey(Schema.Natural),
|
|
83
|
+
disabled: Schema.optionalKey(Schema.Boolean),
|
|
84
|
+
checked: Schema.optionalKey(Schema.Boolean),
|
|
85
|
+
editable: Schema.optionalKey(Schema.Boolean),
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The page's outline and its controls, where the page is scrolled, and a browser's tabs.
|
|
90
|
+
*
|
|
91
|
+
* @stability stable
|
|
92
|
+
* @category schemas
|
|
93
|
+
* @since 0.3.0
|
|
94
|
+
*/
|
|
95
|
+
export const Observation = Schema.Struct({
|
|
96
|
+
text: Schema.String,
|
|
97
|
+
controls: Schema.Array(Control),
|
|
98
|
+
page: Schema.Struct({
|
|
99
|
+
title: Schema.String,
|
|
100
|
+
scrollY: Schema.Finite,
|
|
101
|
+
viewportHeight: Schema.Finite,
|
|
102
|
+
documentHeight: Schema.Finite,
|
|
103
|
+
}),
|
|
104
|
+
truncated: Schema.Boolean,
|
|
105
|
+
tabs: Schema.optionalKey(
|
|
106
|
+
Schema.Array(Schema.Struct({ ref: Ref, url: Schema.String, active: Schema.Boolean })),
|
|
107
|
+
),
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* @stability stable
|
|
112
|
+
* @category models
|
|
113
|
+
* @since 0.3.0
|
|
114
|
+
*/
|
|
115
|
+
export type Observation = typeof Observation.Type;
|
|
116
|
+
|
|
117
|
+
const Dispatch = Schema.Literals(["not-dispatched", "acknowledged", "possibly-dispatched"]);
|
|
118
|
+
|
|
119
|
+
type Dispatch = typeof Dispatch.Type;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* What actions did: how many completed, the first one's failure, the page after them unless the
|
|
123
|
+
* caller asked not to look, and whether the input reached the browser. Never replay an input
|
|
124
|
+
* whose dispatch is not `not-dispatched`.
|
|
125
|
+
*
|
|
126
|
+
* @stability stable
|
|
127
|
+
* @category schemas
|
|
128
|
+
* @since 0.3.0
|
|
129
|
+
*/
|
|
130
|
+
export const ActionResult = Schema.Struct({
|
|
131
|
+
completed: Schema.Natural,
|
|
132
|
+
error: Schema.NullOr(Schema.String),
|
|
133
|
+
observation: Schema.NullOr(Observation),
|
|
134
|
+
dispatch: Dispatch,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* @stability stable
|
|
139
|
+
* @category models
|
|
140
|
+
* @since 0.3.0
|
|
141
|
+
*/
|
|
142
|
+
export type ActionResult = typeof ActionResult.Type;
|
|
143
|
+
|
|
144
|
+
/** What `control.wait` waits for, as the `wait` tool takes it. */
|
|
145
|
+
interface WaitRequest {
|
|
146
|
+
readonly selector: string;
|
|
147
|
+
readonly frame?: string | undefined;
|
|
148
|
+
readonly state: "visible" | "hidden" | "enabled" | "text";
|
|
149
|
+
readonly text?: string | undefined;
|
|
150
|
+
readonly timeoutMillis: number;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* What the controller does when a tab opens: see the module's comment.
|
|
155
|
+
*
|
|
156
|
+
* @stability stable
|
|
157
|
+
* @category models
|
|
158
|
+
* @since 0.3.0
|
|
159
|
+
*/
|
|
160
|
+
export type Follow = "select" | "front" | "never";
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* What the controller acts on: one page, or a browser's tabs.
|
|
164
|
+
*
|
|
165
|
+
* @stability stable
|
|
166
|
+
* @category models
|
|
167
|
+
* @since 0.3.0
|
|
168
|
+
*/
|
|
169
|
+
export type Target =
|
|
170
|
+
| { readonly page: Page.Page }
|
|
171
|
+
| { readonly browser: Browser.Service; readonly follow?: Follow | undefined };
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* @stability stable
|
|
175
|
+
* @category options
|
|
176
|
+
* @since 0.3.0
|
|
177
|
+
*/
|
|
178
|
+
export interface Options {
|
|
179
|
+
/** The user's task, which the input policy's judge reads as what authorizes a risky input. */
|
|
180
|
+
readonly task?: string | undefined;
|
|
181
|
+
/** Resume stable tab identities from a prior finished adapter state. */
|
|
182
|
+
readonly initialTabs?: Tabs.Tabs | undefined;
|
|
183
|
+
/** Bound on an observation's outline, in characters. Defaults to 8,000. */
|
|
184
|
+
readonly maxChars?: number | undefined;
|
|
185
|
+
/** Bound on a condition wait. Defaults to 5 seconds; an unmet wait returns the page as it is. */
|
|
186
|
+
readonly maxWait?: Duration.Input | undefined;
|
|
187
|
+
/** Give every control's box in the outline, as `WxH at (x,y)` in viewport CSS pixels. */
|
|
188
|
+
readonly boxes?: boolean | undefined;
|
|
189
|
+
/**
|
|
190
|
+
* After an action, give only the outline's lines that are gone and new since the tab's last whole
|
|
191
|
+
* look, when they are fewer than its lines. Defaults to false.
|
|
192
|
+
*/
|
|
193
|
+
readonly changes?: boolean | undefined;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
type Failing<A> = Effect.Effect<A, ToolError>;
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* The controller, and the tab it acts on, which `BrowserTools` shares.
|
|
200
|
+
*
|
|
201
|
+
* @stability stable
|
|
202
|
+
* @category models
|
|
203
|
+
* @since 0.3.0
|
|
204
|
+
*/
|
|
205
|
+
export interface Controller {
|
|
206
|
+
readonly actions: {
|
|
207
|
+
readonly observe: Failing<Observation>;
|
|
208
|
+
/** Act in order, stopping at the first failure; `observe: false` skips the look after. */
|
|
209
|
+
readonly act: (
|
|
210
|
+
actions: ReadonlyArray<Action>,
|
|
211
|
+
options?: { readonly observe?: boolean | undefined },
|
|
212
|
+
) => Failing<ActionResult>;
|
|
213
|
+
};
|
|
214
|
+
readonly control: {
|
|
215
|
+
/** The whole page's outline lines that hold `text`, ignoring case. */
|
|
216
|
+
readonly find: (request: { readonly text: string }) => Failing<Observation>;
|
|
217
|
+
readonly inspect: (request: {
|
|
218
|
+
readonly selector?: string | undefined;
|
|
219
|
+
readonly frame?: string | undefined;
|
|
220
|
+
readonly optionFilter?: string | undefined;
|
|
221
|
+
}) => Failing<Observation>;
|
|
222
|
+
readonly navigate: (request: { readonly url: string }) => Failing<ActionResult>;
|
|
223
|
+
readonly scroll: (request: {
|
|
224
|
+
readonly ref?: string | undefined;
|
|
225
|
+
readonly deltaX: number;
|
|
226
|
+
readonly deltaY: number;
|
|
227
|
+
}) => Failing<ActionResult>;
|
|
228
|
+
readonly wait: (request: WaitRequest) => Failing<Observation>;
|
|
229
|
+
readonly press: (request: {
|
|
230
|
+
readonly ref: string;
|
|
231
|
+
readonly key: string;
|
|
232
|
+
}) => Failing<ActionResult>;
|
|
233
|
+
readonly selectTab: (request: { readonly ref: string }) => Failing<Observation>;
|
|
234
|
+
};
|
|
235
|
+
/** The tab calls act on now: the pinned page, or the one the model last saw. */
|
|
236
|
+
readonly current: Effect.Effect<Page.Page, BrowserError>;
|
|
237
|
+
/** The current tab as a look finds it, following a tab an action opened. */
|
|
238
|
+
readonly look: Effect.Effect<Page.Page, BrowserError>;
|
|
239
|
+
/** Run `run` on the current tab with the task, and say what followed it there. */
|
|
240
|
+
readonly perform: <A>(
|
|
241
|
+
run: (page: Page.Page) => Effect.Effect<A, BrowserError>,
|
|
242
|
+
) => Effect.Effect<{ readonly value: A; readonly followed: ReadonlyArray<string> }, BrowserError>;
|
|
243
|
+
/** Give `effect` the run's task, for the input policy's judge. */
|
|
244
|
+
readonly withTask: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const invalid = (message: string) => Effect.fail(new ToolError({ code: "invalid", message }));
|
|
248
|
+
|
|
249
|
+
/** A snapshot as an observation: the outline, the controls and where the page is. */
|
|
250
|
+
const observation = (
|
|
251
|
+
snapshot: Snapshot.Snapshot,
|
|
252
|
+
extra: { readonly followed?: ReadonlyArray<string>; readonly tabs?: Observation["tabs"] },
|
|
253
|
+
): Observation => ({
|
|
254
|
+
text: [...(extra.followed ?? []), snapshot.rendered].join("\n"),
|
|
255
|
+
// An observation's controls leave out what a control does not have, rather than state it undefined.
|
|
256
|
+
controls: snapshot.controls.map((control) => ({
|
|
257
|
+
ref: control.ref,
|
|
258
|
+
kind: control.kind,
|
|
259
|
+
name: control.name,
|
|
260
|
+
value: control.value,
|
|
261
|
+
options: control.options ?? [],
|
|
262
|
+
...(control.optionCount === undefined ? {} : { optionCount: control.optionCount }),
|
|
263
|
+
...(control.disabled === undefined ? {} : { disabled: control.disabled }),
|
|
264
|
+
...(control.checked === undefined ? {} : { checked: control.checked }),
|
|
265
|
+
...(control.editable === undefined ? {} : { editable: control.editable }),
|
|
266
|
+
})),
|
|
267
|
+
page: {
|
|
268
|
+
title: snapshot.title,
|
|
269
|
+
scrollY: snapshot.scroll.y,
|
|
270
|
+
viewportHeight: snapshot.viewport.height,
|
|
271
|
+
documentHeight: snapshot.scroll.height,
|
|
272
|
+
},
|
|
273
|
+
truncated: snapshot.truncated,
|
|
274
|
+
...(extra.tabs === undefined ? {} : { tabs: extra.tabs }),
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
/** The tabs the controller follows in a browser: which one is current, and a stable ref for each. */
|
|
278
|
+
const following = (browser: Browser.Service, follow: Follow, initial?: Tabs.Tabs) =>
|
|
279
|
+
Effect.gen(function* () {
|
|
280
|
+
let state = initial ?? Tabs.make((yield* browser.pages).map((tab) => tab.id));
|
|
281
|
+
|
|
282
|
+
const become = (tab: Page.Page) =>
|
|
283
|
+
Effect.gen(function* () {
|
|
284
|
+
if (follow === "front" && state.shown !== undefined && tab.id !== state.shown)
|
|
285
|
+
yield* tab.bringToFront;
|
|
286
|
+
state = new Tabs.Tabs({ ...state, shown: tab.id });
|
|
287
|
+
|
|
288
|
+
return tab;
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
const look = Effect.gen(function* () {
|
|
292
|
+
const open = yield* browser.pages;
|
|
293
|
+
|
|
294
|
+
const next = Tabs.follow(
|
|
295
|
+
state,
|
|
296
|
+
open.map((tab) => tab.id),
|
|
297
|
+
follow,
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
const tab = open.find((tab) => tab.id === next.selected) ?? (yield* browser.firstPage);
|
|
301
|
+
|
|
302
|
+
yield* become(tab);
|
|
303
|
+
state = next.state;
|
|
304
|
+
|
|
305
|
+
return tab;
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
const tabs = Effect.gen(function* () {
|
|
309
|
+
const open = yield* browser.pages;
|
|
310
|
+
|
|
311
|
+
state = new Tabs.Tabs({
|
|
312
|
+
...state,
|
|
313
|
+
refs: Tabs.follow(
|
|
314
|
+
state,
|
|
315
|
+
open.map((tab) => tab.id),
|
|
316
|
+
"never",
|
|
317
|
+
).state.refs,
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
return yield* Effect.forEach(open, (tab) => {
|
|
321
|
+
const ref = state.refs[tab.id];
|
|
322
|
+
|
|
323
|
+
if (ref === undefined) return Effect.die("registered tab is missing its stable ref");
|
|
324
|
+
|
|
325
|
+
return Effect.map(tab.url, (url) => ({
|
|
326
|
+
ref,
|
|
327
|
+
url: url.startsWith("data:") ? "data:" : url,
|
|
328
|
+
active: tab.id === state.shown,
|
|
329
|
+
}));
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
const select = (ref: string) =>
|
|
334
|
+
Effect.gen(function* () {
|
|
335
|
+
const open = yield* browser.pages;
|
|
336
|
+
|
|
337
|
+
const id = Tabs.selected(
|
|
338
|
+
state,
|
|
339
|
+
open.map((tab) => tab.id),
|
|
340
|
+
ref,
|
|
341
|
+
);
|
|
342
|
+
|
|
343
|
+
const tab = open.find((tab) => tab.id === id);
|
|
344
|
+
|
|
345
|
+
if (tab === undefined) return Option.none<Page.Page>();
|
|
346
|
+
state = new Tabs.Tabs({ ...state, seen: [...new Set([...state.seen, tab.id])] });
|
|
347
|
+
|
|
348
|
+
return Option.some(yield* become(tab));
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
return {
|
|
352
|
+
current: Effect.suspend(() =>
|
|
353
|
+
state.shown === undefined
|
|
354
|
+
? look
|
|
355
|
+
: Effect.flatMap(
|
|
356
|
+
browser.page(state.shown),
|
|
357
|
+
Option.match({ onNone: () => look, onSome: Effect.succeed }),
|
|
358
|
+
),
|
|
359
|
+
),
|
|
360
|
+
look,
|
|
361
|
+
tabs,
|
|
362
|
+
select,
|
|
363
|
+
};
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* What of a page a read outlines: what is inside `within`, the lines of the whole page that hold
|
|
368
|
+
* `query`, or the viewport, which after an action may be only its `changes`.
|
|
369
|
+
*/
|
|
370
|
+
interface Scope {
|
|
371
|
+
readonly within?: string | undefined;
|
|
372
|
+
readonly query?: string | undefined;
|
|
373
|
+
readonly changes?: boolean | undefined;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** The outline's lines that are gone from `before` and new in `after`, as often as each repeats. */
|
|
377
|
+
const changed = (before: ReadonlyArray<string>, after: ReadonlyArray<string>) => {
|
|
378
|
+
const left = new Map<string, number>();
|
|
379
|
+
|
|
380
|
+
const take = (line: string) => {
|
|
381
|
+
const count = left.get(line) ?? 0;
|
|
382
|
+
|
|
383
|
+
left.set(line, count - 1);
|
|
384
|
+
|
|
385
|
+
return count > 0;
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
for (const line of before) left.set(line, (left.get(line) ?? 0) + 1);
|
|
389
|
+
const added = after.filter((line) => !take(line));
|
|
390
|
+
|
|
391
|
+
return { gone: before.filter(take), added };
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
/** A snapshot whose outline is only what changed since `before`, or itself when that is longer. */
|
|
395
|
+
const changesOf = (snapshot: Snapshot.Snapshot, before: string) => {
|
|
396
|
+
const lines = snapshot.text.split("\n");
|
|
397
|
+
const { gone, added } = changed(before.split("\n"), lines);
|
|
398
|
+
|
|
399
|
+
if (gone.length + added.length >= lines.length) return snapshot;
|
|
400
|
+
|
|
401
|
+
const text = [
|
|
402
|
+
"Only what changed in the outline since the last observation:",
|
|
403
|
+
...(gone.length + added.length === 0 ? ["(nothing)"] : []),
|
|
404
|
+
...(gone.length === 0 ? [] : ["No longer shown:", ...gone]),
|
|
405
|
+
...(added.length === 0 ? [] : ["Newly shown:", ...added]),
|
|
406
|
+
].join("\n");
|
|
407
|
+
|
|
408
|
+
return new Snapshot.Snapshot({
|
|
409
|
+
...snapshot,
|
|
410
|
+
text,
|
|
411
|
+
controls: snapshot.controls.filter((control) => text.includes(`[ref=${control.ref}]`)),
|
|
412
|
+
});
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
/** What the controller's parts share: the tab they act on, how they read it, and the run's task. */
|
|
416
|
+
interface Session {
|
|
417
|
+
readonly current: Effect.Effect<Page.Page, BrowserError>;
|
|
418
|
+
/** The current tab, following a tab an action opened; the pinned page is always the one. */
|
|
419
|
+
readonly look: Effect.Effect<Page.Page, BrowserError>;
|
|
420
|
+
readonly read: (
|
|
421
|
+
page: Page.Page,
|
|
422
|
+
scope?: Scope,
|
|
423
|
+
lines?: ReadonlyArray<string>,
|
|
424
|
+
) => Effect.Effect<Observation, BrowserError>;
|
|
425
|
+
readonly tabs: Effect.Success<ReturnType<typeof following>> | undefined;
|
|
426
|
+
readonly withTask: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
|
|
427
|
+
readonly maxWait: number;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const session = Effect.fnUntraced(function* (target: Target, options: Options) {
|
|
431
|
+
const maxChars = options.maxChars ?? 8000;
|
|
432
|
+
|
|
433
|
+
// A look follows a tab an action opened; the pinned page is always the one.
|
|
434
|
+
const { current, look, tabs } =
|
|
435
|
+
"page" in target
|
|
436
|
+
? { current: Effect.succeed(target.page), look: Effect.succeed(target.page), tabs: undefined }
|
|
437
|
+
: yield* Effect.map(
|
|
438
|
+
following(target.browser, target.follow ?? "select", options.initialTabs),
|
|
439
|
+
(followed) => ({
|
|
440
|
+
current: followed.current,
|
|
441
|
+
look: followed.look,
|
|
442
|
+
tabs: followed,
|
|
443
|
+
}),
|
|
444
|
+
);
|
|
445
|
+
|
|
446
|
+
// The pages whose changes are recorded, from their first look, so the receipt of the first
|
|
447
|
+
// action on one can say what it changed. A page whose record could not start says so, and the
|
|
448
|
+
// next look tries again.
|
|
449
|
+
const recording = new Set<string>();
|
|
450
|
+
|
|
451
|
+
const record = (page: Page.Page) =>
|
|
452
|
+
recording.has(page.id)
|
|
453
|
+
? Effect.succeed([])
|
|
454
|
+
: Change.read(page).pipe(
|
|
455
|
+
Effect.andThen(Effect.sync(() => recording.add(page.id))),
|
|
456
|
+
Effect.as([]),
|
|
457
|
+
Effect.catch((error) =>
|
|
458
|
+
Effect.succeed([`What changes on this page cannot be recorded yet: ${error.message}.`]),
|
|
459
|
+
),
|
|
460
|
+
);
|
|
461
|
+
|
|
462
|
+
// Each tab's last whole outline, with its address, which an action's changes are taken against.
|
|
463
|
+
const whole = new Map<string, { readonly url: string; readonly text: string }>();
|
|
464
|
+
|
|
465
|
+
const outlined = (page: Page.Page, scope: Scope) =>
|
|
466
|
+
Effect.map(
|
|
467
|
+
Snapshot.read(page, {
|
|
468
|
+
maxChars,
|
|
469
|
+
within: scope.within,
|
|
470
|
+
...(scope.query === undefined ? {} : { full: true, query: scope.query }),
|
|
471
|
+
boxes: options.boxes,
|
|
472
|
+
}),
|
|
473
|
+
(snapshot) => {
|
|
474
|
+
if (options.changes !== true || scope.within !== undefined || scope.query !== undefined)
|
|
475
|
+
return snapshot;
|
|
476
|
+
const before = whole.get(page.id);
|
|
477
|
+
|
|
478
|
+
whole.set(page.id, { url: snapshot.url, text: snapshot.text });
|
|
479
|
+
|
|
480
|
+
return scope.changes === true && before?.url === snapshot.url
|
|
481
|
+
? changesOf(snapshot, before.text)
|
|
482
|
+
: snapshot;
|
|
483
|
+
},
|
|
484
|
+
);
|
|
485
|
+
|
|
486
|
+
const read = (page: Page.Page, scope: Scope = {}, lines: ReadonlyArray<string> = []) =>
|
|
487
|
+
Effect.all([outlined(page, scope), tabs?.tabs ?? Effect.void, record(page)], {
|
|
488
|
+
concurrency: "unbounded",
|
|
489
|
+
}).pipe(
|
|
490
|
+
Effect.map(([snapshot, open, unrecorded]) =>
|
|
491
|
+
observation(snapshot, {
|
|
492
|
+
followed: [...lines, ...unrecorded],
|
|
493
|
+
tabs: open === undefined ? undefined : open,
|
|
494
|
+
}),
|
|
495
|
+
),
|
|
496
|
+
);
|
|
497
|
+
|
|
498
|
+
return {
|
|
499
|
+
current,
|
|
500
|
+
look,
|
|
501
|
+
read,
|
|
502
|
+
tabs,
|
|
503
|
+
withTask: <A, E, R>(effect: Effect.Effect<A, E, R>) =>
|
|
504
|
+
options.task === undefined
|
|
505
|
+
? effect
|
|
506
|
+
: Effect.provideService(effect, Policy.Task, options.task),
|
|
507
|
+
maxWait: millisOf(options.maxWait ?? Duration.seconds(5)),
|
|
508
|
+
} satisfies Session;
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Run actions in order on the current tab, stopping at the first failure, then look at the page,
|
|
513
|
+
* which follows a tab they opened. Acknowledged actions are counted even if the look fails.
|
|
514
|
+
*/
|
|
515
|
+
const acting =
|
|
516
|
+
({ current, look, read, withTask }: Session) =>
|
|
517
|
+
(
|
|
518
|
+
steps: ReadonlyArray<(page: Page.Page) => Effect.Effect<unknown, BrowserError>>,
|
|
519
|
+
observe: boolean,
|
|
520
|
+
): Effect.Effect<ActionResult, ToolError> =>
|
|
521
|
+
Effect.gen(function* () {
|
|
522
|
+
const page = yield* current;
|
|
523
|
+
const since = (yield* page.state).at;
|
|
524
|
+
let completed = 0;
|
|
525
|
+
let failure: BrowserError | undefined;
|
|
526
|
+
|
|
527
|
+
for (const step of steps) {
|
|
528
|
+
const done = yield* Effect.result(step(page));
|
|
529
|
+
|
|
530
|
+
if (Result.isFailure(done)) {
|
|
531
|
+
failure = done.failure;
|
|
532
|
+
break;
|
|
533
|
+
}
|
|
534
|
+
completed += 1;
|
|
535
|
+
}
|
|
536
|
+
const dispatch: Dispatch = failure === undefined ? "acknowledged" : dispatchOf(failure);
|
|
537
|
+
const lines = yield* followed(page, since);
|
|
538
|
+
const error = failure === undefined ? null : told(failure);
|
|
539
|
+
|
|
540
|
+
if (!observe) return { completed, error, observation: null, dispatch };
|
|
541
|
+
|
|
542
|
+
const seen = yield* Effect.result(
|
|
543
|
+
Effect.flatMap(look, (next) => read(next, { changes: true }, lines)),
|
|
544
|
+
);
|
|
545
|
+
|
|
546
|
+
return {
|
|
547
|
+
completed,
|
|
548
|
+
error:
|
|
549
|
+
error ??
|
|
550
|
+
(Result.isFailure(seen)
|
|
551
|
+
? `Done, but the page could not be read after it: ${told(seen.failure)}`
|
|
552
|
+
: null),
|
|
553
|
+
observation: Result.getOrNull(seen),
|
|
554
|
+
dispatch,
|
|
555
|
+
};
|
|
556
|
+
}).pipe(Effect.mapError(fromBrowserError), withTask);
|
|
557
|
+
|
|
558
|
+
const action = (step: Action) => (page: Page.Page) => {
|
|
559
|
+
switch (step.kind) {
|
|
560
|
+
case "click":
|
|
561
|
+
return page.perform({ _tag: "Click", target: step.ref });
|
|
562
|
+
case "fill":
|
|
563
|
+
return page.perform({ _tag: "Type", text: step.value, options: { into: step.ref } });
|
|
564
|
+
case "select":
|
|
565
|
+
return page.perform({ _tag: "Select", ref: step.ref, values: [step.value] });
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
const frameless = (operation: string) =>
|
|
570
|
+
invalid(`Frames are read inline here and none is listed: ${operation} without frame.`);
|
|
571
|
+
|
|
572
|
+
/** A condition wait, ending at the run's bound with the page as it is. */
|
|
573
|
+
const waiting =
|
|
574
|
+
({ current, read, withTask, maxWait }: Session) =>
|
|
575
|
+
(request: WaitRequest) => {
|
|
576
|
+
if (request.frame !== undefined) return frameless("wait");
|
|
577
|
+
if (request.state === "text" && request.text === undefined)
|
|
578
|
+
return invalid("A wait for text needs the text.");
|
|
579
|
+
const millis = Math.min(request.timeoutMillis, maxWait);
|
|
580
|
+
const state = request.state === "text" ? "visible" : request.state;
|
|
581
|
+
|
|
582
|
+
return Effect.gen(function* () {
|
|
583
|
+
const page = yield* current;
|
|
584
|
+
|
|
585
|
+
const { met } = yield* Snapshot.until(
|
|
586
|
+
page,
|
|
587
|
+
{
|
|
588
|
+
selector: request.selector,
|
|
589
|
+
text: request.text,
|
|
590
|
+
state: state === "enabled" ? "enabled" : "visible",
|
|
591
|
+
},
|
|
592
|
+
state === "hidden" ? "none" : "some",
|
|
593
|
+
millis,
|
|
594
|
+
);
|
|
595
|
+
|
|
596
|
+
return yield* read(page, {}, met ? [] : [`The wait ended after ${millis} ms unmet.`]);
|
|
597
|
+
}).pipe(Effect.mapError(fromBrowserError), withTask);
|
|
598
|
+
};
|
|
599
|
+
|
|
600
|
+
/** The controller's `control` over the session's current tab. */
|
|
601
|
+
const controlling = (context: Session): Controller["control"] => {
|
|
602
|
+
const { look, read, tabs, withTask } = context;
|
|
603
|
+
const act = acting(context);
|
|
604
|
+
|
|
605
|
+
return {
|
|
606
|
+
find: ({ text }) =>
|
|
607
|
+
Effect.flatMap(look, (page) => read(page, { query: text })).pipe(
|
|
608
|
+
Effect.mapError(fromBrowserError),
|
|
609
|
+
withTask,
|
|
610
|
+
),
|
|
611
|
+
inspect: ({ selector, frame, optionFilter }) =>
|
|
612
|
+
frame !== undefined
|
|
613
|
+
? frameless("inspect")
|
|
614
|
+
: Effect.flatMap(look, (page) => read(page, { within: selector })).pipe(
|
|
615
|
+
Effect.map((seen) =>
|
|
616
|
+
optionFilter === undefined ? seen : filterOptions(seen, optionFilter),
|
|
617
|
+
),
|
|
618
|
+
Effect.mapError(fromBrowserError),
|
|
619
|
+
withTask,
|
|
620
|
+
),
|
|
621
|
+
navigate: ({ url }) =>
|
|
622
|
+
act(
|
|
623
|
+
[(page) => Effect.flatMap(destination("navigate", url), (address) => page.goto(address))],
|
|
624
|
+
true,
|
|
625
|
+
),
|
|
626
|
+
scroll: ({ ref, deltaX, deltaY }) =>
|
|
627
|
+
act([(page) => page.scroll({ at: ref, dx: deltaX, dy: deltaY })], true),
|
|
628
|
+
wait: waiting(context),
|
|
629
|
+
press: ({ ref, key }) => act([(page) => page.press(key, { on: ref })], true),
|
|
630
|
+
selectTab: ({ ref }) =>
|
|
631
|
+
tabs === undefined
|
|
632
|
+
? invalid("These tools are pinned to one tab.")
|
|
633
|
+
: tabs.select(ref).pipe(
|
|
634
|
+
Effect.mapError(fromBrowserError),
|
|
635
|
+
Effect.flatMap(
|
|
636
|
+
Option.match({
|
|
637
|
+
onNone: () => invalid(`${ref} is not an open tab; observe for the current tabs.`),
|
|
638
|
+
onSome: (page) => read(page).pipe(Effect.mapError(fromBrowserError)),
|
|
639
|
+
}),
|
|
640
|
+
),
|
|
641
|
+
withTask,
|
|
642
|
+
),
|
|
643
|
+
};
|
|
644
|
+
};
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* The controller over `target`, and the tab it acts on.
|
|
648
|
+
*
|
|
649
|
+
* @stability stable
|
|
650
|
+
* @category constructors
|
|
651
|
+
* @since 0.3.0
|
|
652
|
+
*/
|
|
653
|
+
export const make = Effect.fnUntraced(function* (target: Target, options: Options = {}) {
|
|
654
|
+
const context = yield* session(target, options);
|
|
655
|
+
const { current, look, read, withTask } = context;
|
|
656
|
+
const act = acting(context);
|
|
657
|
+
|
|
658
|
+
const actions: Controller["actions"] = {
|
|
659
|
+
observe: Effect.flatMap(look, (page) => read(page)).pipe(
|
|
660
|
+
Effect.mapError(fromBrowserError),
|
|
661
|
+
withTask,
|
|
662
|
+
),
|
|
663
|
+
act: (steps, actOptions = {}) => act(steps.map(action), actOptions.observe ?? true),
|
|
664
|
+
};
|
|
665
|
+
|
|
666
|
+
const perform = <A>(run: (page: Page.Page) => Effect.Effect<A, BrowserError>) =>
|
|
667
|
+
Effect.gen(function* () {
|
|
668
|
+
const page = yield* current;
|
|
669
|
+
const since = (yield* page.state).at;
|
|
670
|
+
const value = yield* run(page);
|
|
671
|
+
|
|
672
|
+
return { value, followed: yield* followed(page, since) };
|
|
673
|
+
}).pipe(withTask);
|
|
674
|
+
|
|
675
|
+
return {
|
|
676
|
+
actions,
|
|
677
|
+
control: controlling(context),
|
|
678
|
+
current,
|
|
679
|
+
look,
|
|
680
|
+
perform,
|
|
681
|
+
withTask,
|
|
682
|
+
} satisfies Controller;
|
|
683
|
+
});
|
|
684
|
+
|
|
685
|
+
/** A select's options narrowed to those whose label holds `filter`, keeping its chosen value. */
|
|
686
|
+
const filterOptions = (seen: Observation, filter: string): Observation => {
|
|
687
|
+
const wanted = filter.toLowerCase();
|
|
688
|
+
|
|
689
|
+
return {
|
|
690
|
+
...seen,
|
|
691
|
+
controls: seen.controls.map((control) =>
|
|
692
|
+
control.options.length === 0
|
|
693
|
+
? control
|
|
694
|
+
: {
|
|
695
|
+
...control,
|
|
696
|
+
options: control.options.filter(
|
|
697
|
+
(option) => option.toLowerCase().includes(wanted) || option === control.value,
|
|
698
|
+
),
|
|
699
|
+
},
|
|
700
|
+
),
|
|
701
|
+
};
|
|
702
|
+
};
|