effect-browser 0.3.0-beta.24 → 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 +333 -157
- 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 +1 -1
- package/dist/BrowserEvent.mjs +166 -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 +33 -6
- 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/{Style.d.mts → InputStyle.d.mts} +93 -25
- package/dist/{Style.mjs → InputStyle.mjs} +1 -1
- package/dist/Layout.d.mts +2 -2
- package/dist/Layout.mjs +58 -16
- package/dist/Layout.mjs.map +1 -1
- package/dist/Overlay.d.mts +2 -2
- package/dist/Overlay.mjs +33 -5
- package/dist/Overlay.mjs.map +1 -1
- package/dist/Page.d.mts +2 -2
- package/dist/Page.mjs +2 -2
- package/dist/PageTime.d.mts +2 -2
- package/dist/PageTime.mjs +13 -3
- package/dist/PageTime.mjs.map +1 -1
- package/dist/Pose.d.mts +2 -2
- package/dist/Pose.mjs +30 -9
- package/dist/Pose.mjs.map +1 -1
- package/dist/Site.d.mts +288 -18
- package/dist/Site.mjs +11 -3
- package/dist/Site.mjs.map +1 -1
- 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 -18
- package/dist/index.mjs +6 -9
- package/dist/{Action-24sLOubi.d.mts → internal/Action-CkanNTUw.d.mts} +64 -4
- package/dist/{Browser-DGR6ZEYo.d.mts → internal/Browser-DD9Vm1c-.d.mts} +75 -11
- package/dist/{Browser-CAQFhTkM.mjs → internal/Browser-vzD7i2oj.mjs} +174 -237
- 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-C2p-NwuU.d.mts → internal/BrowserEvent-tGPYYf6R.d.mts} +168 -13
- package/dist/internal/BrowserTools-Yi3lOX4h.mjs +497 -0
- package/dist/internal/BrowserTools-Yi3lOX4h.mjs.map +1 -0
- package/dist/{Style-T3ezDYLI.mjs → internal/InputStyle-CPzxoN4g.mjs} +75 -20
- 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} +53 -20
- package/dist/internal/Page-BfGxn7oN.d.mts +1553 -0
- package/dist/{Page-BaAOshxo.mjs → internal/Page-DqF-tuTx.mjs} +62 -74
- 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-Db-YtlbE.d.mts → internal/connection-BaB0vp1o.d.mts} +6 -6
- package/dist/{connection-C_jWYIwe.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-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} +3 -3
- 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/{pose.inpage-CLq7Mfdr.mjs → internal/pose.inpage-CBFeWcVa.mjs} +2 -48
- 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/{film → presentation}/Film.d.mts +83 -16
- package/dist/{film → presentation}/Film.mjs +82 -30
- 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 +42 -7
- package/dist/presentation/Presentation.mjs +26 -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 -311
- package/dist/testing/index.mjs +6 -521
- 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 -163
- package/src/Action.ts +73 -8
- package/src/Browser.ts +75 -9
- package/src/BrowserError.ts +258 -63
- package/src/BrowserEvent.ts +252 -55
- package/src/Cdp.ts +42 -0
- package/src/Change.ts +68 -3
- package/src/Frame.ts +247 -14
- package/src/{Style.ts → InputStyle.ts} +98 -22
- package/src/Layout.ts +95 -24
- package/src/Overlay.ts +42 -3
- package/src/Page.ts +144 -320
- package/src/PageTime.ts +29 -4
- package/src/Pose.ts +38 -7
- package/src/Site.ts +15 -3
- 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 +83 -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 +5 -5
- package/src/internal/input/pointer.ts +2 -2
- package/src/internal/input/targets.inpage.ts +3 -3
- package/src/{Matching.ts → internal/matching.ts} +6 -6
- package/src/internal/page/bridge.ts +2 -2
- package/src/internal/page/clock.inpage.ts +1 -1
- package/src/internal/page/compose.inpage.ts +2 -2
- package/src/internal/page/context.ts +2 -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/page.ts +33 -35
- package/src/internal/page/port.ts +14 -8
- 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.ts +1 -1
- 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 +53 -61
- 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 +3 -3
- package/src/internal/reading/layout.ts +5 -5
- package/src/internal/reading/outline.inpage.ts +3 -3
- package/src/internal/reading/reading.ts +11 -19
- package/src/internal/reading/ready.inpage.ts +2 -2
- package/src/internal/reading/ready.ts +3 -2
- package/src/internal/reading/subjects.inpage.ts +3 -3
- package/src/internal/reading/text.inpage.ts +2 -2
- package/src/internal/reading/walk.inpage.ts +1 -1
- package/src/internal/timeline/changes.inpage.ts +2 -2
- package/src/internal/timeline/changes.ts +3 -4
- 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/{film → presentation}/Film.ts +104 -37
- package/src/presentation/Motion.ts +45 -3
- package/src/presentation/Presentation.ts +47 -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 -56
- package/src/{moment → timeline}/Moment.ts +79 -29
- package/src/{stillness → timeline}/Stillness.ts +61 -43
- package/src/timeline/index.ts +18 -0
- package/src/{stillness/Pixels.ts → timeline/internal/pixels.ts} +2 -2
- 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-CAQFhTkM.mjs.map +0 -1
- package/dist/Change-CxT1mp2e.d.mts +0 -80
- package/dist/Clock-Du5nP-nK.d.mts +0 -57
- package/dist/Clock.d.mts +0 -2
- 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-MtcWZ_hA.d.mts +0 -266
- package/dist/Matching.d.mts +0 -2
- package/dist/Matching.mjs.map +0 -1
- package/dist/Overlay-CoGw5tL_.d.mts +0 -27
- package/dist/Page-BaAOshxo.mjs.map +0 -1
- package/dist/Page-CLzh5E9w.d.mts +0 -718
- package/dist/PageTime-68UPUZpZ.d.mts +0 -29
- package/dist/Pose-2qqGzPvb.d.mts +0 -32
- package/dist/Released-tURbFmal.d.mts +0 -15
- package/dist/Snapshot-CtBVOtuu.d.mts +0 -73
- package/dist/Stillness-BwP0RdI4.mjs +0 -157
- package/dist/Stillness-BwP0RdI4.mjs.map +0 -1
- package/dist/Style-T3ezDYLI.mjs.map +0 -1
- package/dist/Url-B7P-TWwL.d.mts +0 -26
- package/dist/Url.d.mts +0 -2
- package/dist/Url.mjs.map +0 -1
- package/dist/Window-BDa8MtNP.d.mts +0 -34
- package/dist/connection-C_jWYIwe.mjs.map +0 -1
- package/dist/film/Film.mjs.map +0 -1
- package/dist/moment/Moment.mjs.map +0 -1
- package/dist/plan/index.mjs.map +0 -1
- package/dist/pose.inpage-CLq7Mfdr.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/{fold.inpage-CyVZQHZL.d.mts → internal/fold.inpage-CyVZQHZL.d.mts} +0 -0
- /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,778 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser tools for an agent in any `effect/ai` loop, over `PageControl`'s controller: `observe`
|
|
3
|
+
* and `act` on the refs of an observed page; the `Control` tools, which inspect, navigate, scroll,
|
|
4
|
+
* wait, press keys and select tabs; and the `Pointer` tools for what has no ref, such as a canvas
|
|
5
|
+
* game or a chart: clicking, hovering and dragging at viewport points, typing and pressing keys
|
|
6
|
+
* into focus, zooming, waiting for the screen to go still, and going back. The ref and `Control`
|
|
7
|
+
* tools keep the names, parameters and descriptions Yielded Agent gave them before 0.1.0-beta.174
|
|
8
|
+
* replaced them with Code Mode over CDP, but that their optional parameters read null as left out.
|
|
9
|
+
* Every tool returns its failure to the model as a `ToolError`.
|
|
10
|
+
*
|
|
11
|
+
* With `vision`, the default, the model sees a screenshot of the current tab before each turn, and
|
|
12
|
+
* the crops `zoom` took since its last, as context the loop never keeps, so only the current picture
|
|
13
|
+
* is ever in a request. Its pixel coordinates are viewport coordinates, which the pointer tools
|
|
14
|
+
* take; with `pixels: "device"`, they are the screen's own, which the tools map back. The loop that
|
|
15
|
+
* runs the model adds `Pictures`' `next` to each request. With `vision: "ask"`, the screenshot comes
|
|
16
|
+
* only before a turn after the model called `look`; with `vision: "page"`, also after pointer input
|
|
17
|
+
* and while the viewport shows a canvas, a video or another site's frame. `Look` is that tool, for
|
|
18
|
+
* the agent's toolkit beside `make`'s.
|
|
19
|
+
*
|
|
20
|
+
* `make` gives the toolkit to put in an agent, and `layer`, which provides every tool's handler
|
|
21
|
+
* and the `Pictures` for one run, on a page or a browser's tabs, with the run's `Controller` for an
|
|
22
|
+
* application's own tools on the same tabs.
|
|
23
|
+
*
|
|
24
|
+
* @stability stable
|
|
25
|
+
* @since 0.3.0
|
|
26
|
+
*/
|
|
27
|
+
import {
|
|
28
|
+
Context,
|
|
29
|
+
Effect,
|
|
30
|
+
Layer,
|
|
31
|
+
Option,
|
|
32
|
+
Predicate,
|
|
33
|
+
Ref,
|
|
34
|
+
Schema,
|
|
35
|
+
SchemaTransformation,
|
|
36
|
+
} from "effect";
|
|
37
|
+
import { Prompt, Tool, Toolkit } from "effect/ai";
|
|
38
|
+
|
|
39
|
+
import { Action } from "../Action.ts";
|
|
40
|
+
import type { BrowserError } from "../BrowserError.ts";
|
|
41
|
+
import * as Frame from "../Frame.ts";
|
|
42
|
+
import type * as Page from "../Page.ts";
|
|
43
|
+
import * as Stillness from "../timeline/Stillness.ts";
|
|
44
|
+
import { cropped } from "./internal/crops.ts";
|
|
45
|
+
import * as Points from "./internal/points.ts";
|
|
46
|
+
import * as PageControl from "./PageControl.ts";
|
|
47
|
+
import { fromBrowserError, ToolError } from "./ToolError.ts";
|
|
48
|
+
|
|
49
|
+
// A parameter a model may leave out. Its JSON Schema allows null, as an optional one's does, and
|
|
50
|
+
// models send null for one they mean to leave out, so null reads as absent rather than refused.
|
|
51
|
+
const absent = <S extends Schema.Top>(
|
|
52
|
+
schema: S,
|
|
53
|
+
annotations: { readonly description?: string } = {},
|
|
54
|
+
) =>
|
|
55
|
+
Schema.optionalKey(Schema.NullOr(schema).annotate(annotations)).pipe(
|
|
56
|
+
Schema.decodeTo(
|
|
57
|
+
Schema.optional(Schema.toType(schema)),
|
|
58
|
+
SchemaTransformation.transformOptional<S["Type"] | undefined, S["Type"] | null>({
|
|
59
|
+
decode: (input) => Option.filter(input, Predicate.isNotNullish),
|
|
60
|
+
encode: (output) => Option.filter(output, Predicate.isNotUndefined),
|
|
61
|
+
}),
|
|
62
|
+
),
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const pixel = (axis: "x" | "y") =>
|
|
66
|
+
Schema.Finite.annotate({ description: `Viewport ${axis} in screenshot pixels` });
|
|
67
|
+
|
|
68
|
+
const ref = absent(Schema.String, {
|
|
69
|
+
description: "A ref from the latest observation, such as e12",
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* What a pointer tool did, and what followed on its page while it ran.
|
|
74
|
+
*
|
|
75
|
+
* @stability stable
|
|
76
|
+
* @category schemas
|
|
77
|
+
* @since 0.3.0
|
|
78
|
+
*/
|
|
79
|
+
export const Receipt = Schema.Struct({
|
|
80
|
+
did: Schema.String,
|
|
81
|
+
action: Schema.optional(Action),
|
|
82
|
+
followed: Schema.Array(Schema.String),
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const pointerTool = <const Name extends string, Parameters extends Schema.Struct.Fields>(
|
|
86
|
+
name: Name,
|
|
87
|
+
description: string,
|
|
88
|
+
parameters: Parameters,
|
|
89
|
+
) =>
|
|
90
|
+
Tool.make(name, {
|
|
91
|
+
description,
|
|
92
|
+
parameters: Schema.Struct(parameters),
|
|
93
|
+
success: Receipt,
|
|
94
|
+
failure: ToolError,
|
|
95
|
+
failureMode: "return",
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Tools for what has no ref, by viewport point and by keyboard focus.
|
|
100
|
+
*
|
|
101
|
+
* @stability stable
|
|
102
|
+
* @category toolkits
|
|
103
|
+
* @since 0.3.0
|
|
104
|
+
*/
|
|
105
|
+
export const Pointer = Toolkit.make(
|
|
106
|
+
pointerTool("click_at", "Click a point of the screenshot, such as on a canvas game or a chart.", {
|
|
107
|
+
x: pixel("x"),
|
|
108
|
+
y: pixel("y"),
|
|
109
|
+
double: absent(Schema.Boolean),
|
|
110
|
+
button: absent(Schema.Literals(["left", "right", "middle"])),
|
|
111
|
+
}),
|
|
112
|
+
pointerTool("hover", "Move the pointer over an observed ref, or to a point of the screenshot.", {
|
|
113
|
+
ref,
|
|
114
|
+
x: absent(pixel("x")),
|
|
115
|
+
y: absent(pixel("y")),
|
|
116
|
+
}),
|
|
117
|
+
pointerTool(
|
|
118
|
+
"drag",
|
|
119
|
+
"Drag from one ref or point to another, such as a slider or a chart's range.",
|
|
120
|
+
{
|
|
121
|
+
fromRef: ref,
|
|
122
|
+
fromX: absent(pixel("x")),
|
|
123
|
+
fromY: absent(pixel("y")),
|
|
124
|
+
toRef: ref,
|
|
125
|
+
toX: absent(pixel("x")),
|
|
126
|
+
toY: absent(pixel("y")),
|
|
127
|
+
},
|
|
128
|
+
),
|
|
129
|
+
pointerTool(
|
|
130
|
+
"type_text",
|
|
131
|
+
"Type text into whatever has focus, such as a field just clicked. To type into an observed field, act with fill.",
|
|
132
|
+
{
|
|
133
|
+
text: Schema.String,
|
|
134
|
+
submit: absent(Schema.Boolean, { description: "Press Enter afterwards" }),
|
|
135
|
+
},
|
|
136
|
+
),
|
|
137
|
+
pointerTool(
|
|
138
|
+
"press_keys",
|
|
139
|
+
"Press a key or chord on whatever has focus, such as ArrowLeft, Space or Control+A, as a game control might need.",
|
|
140
|
+
{
|
|
141
|
+
keys: Schema.String,
|
|
142
|
+
times: absent(Schema.Int, { description: "Press it this many times; defaults to 1" }),
|
|
143
|
+
holdMillis: absent(Schema.Finite, { description: "Hold the keys down this long" }),
|
|
144
|
+
},
|
|
145
|
+
),
|
|
146
|
+
pointerTool(
|
|
147
|
+
"zoom",
|
|
148
|
+
"Crop a small region of the viewport, shown on its own before your next turn at the viewport's own scale: a closer look at small text, not a magnification. Click coordinates stay in viewport space.",
|
|
149
|
+
Frame.Region.fields,
|
|
150
|
+
),
|
|
151
|
+
pointerTool(
|
|
152
|
+
"wait_still",
|
|
153
|
+
"Wait for the screen to stop moving, as reels coming to rest or a chart that loads; what was already moving before your last input, such as an idle animation, does not count.",
|
|
154
|
+
{ seconds: absent(Schema.Finite, { description: "At most this long; defaults to 15" }) },
|
|
155
|
+
),
|
|
156
|
+
// Without parameters, not with an empty struct: an empty struct's JSON Schema has no object root,
|
|
157
|
+
// which OpenAI's structured outputs reject for the whole request.
|
|
158
|
+
Tool.make("back", {
|
|
159
|
+
description: "Go back to the previous page in this tab.",
|
|
160
|
+
parameters: Tool.EmptyParams,
|
|
161
|
+
success: Receipt,
|
|
162
|
+
failure: ToolError,
|
|
163
|
+
failureMode: "return",
|
|
164
|
+
}),
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* The tool that asks for a screenshot before the next turn, for `vision: "ask"` and `"page"`, which
|
|
169
|
+
* otherwise show none on some turns. The layer serves it whether or not the agent has it.
|
|
170
|
+
*
|
|
171
|
+
* @stability stable
|
|
172
|
+
* @category toolkits
|
|
173
|
+
* @since 0.3.0
|
|
174
|
+
*/
|
|
175
|
+
export const Look = Toolkit.make(
|
|
176
|
+
Tool.make("look", {
|
|
177
|
+
description: "See a screenshot of the current tab before your next turn.",
|
|
178
|
+
parameters: Tool.EmptyParams,
|
|
179
|
+
success: Receipt,
|
|
180
|
+
failure: ToolError,
|
|
181
|
+
failureMode: "return",
|
|
182
|
+
}),
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
const Selector = Schema.NonEmptyString.check(Schema.isMaxLength(1_024));
|
|
186
|
+
|
|
187
|
+
const Delta = Schema.Int.check(Schema.isBetween({ minimum: -100_000, maximum: 100_000 }));
|
|
188
|
+
|
|
189
|
+
const pageTool = <
|
|
190
|
+
const Name extends string,
|
|
191
|
+
Parameters extends Schema.Struct.Fields,
|
|
192
|
+
Success extends Schema.Top,
|
|
193
|
+
>(
|
|
194
|
+
name: Name,
|
|
195
|
+
description: string,
|
|
196
|
+
parameters: Parameters,
|
|
197
|
+
success: Success,
|
|
198
|
+
) =>
|
|
199
|
+
Tool.make(name, {
|
|
200
|
+
description,
|
|
201
|
+
parameters: Schema.Struct(parameters),
|
|
202
|
+
success,
|
|
203
|
+
failure: ToolError,
|
|
204
|
+
failureMode: "return",
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Tools that inspect, navigate, scroll, wait, press keys on refs and select tabs.
|
|
209
|
+
*
|
|
210
|
+
* @stability stable
|
|
211
|
+
* @category toolkits
|
|
212
|
+
* @since 0.3.0
|
|
213
|
+
*/
|
|
214
|
+
export const Control = Toolkit.make(
|
|
215
|
+
pageTool(
|
|
216
|
+
"inspect",
|
|
217
|
+
"Inspect the page or a CSS scope within it, including open shadow roots. Frames are read inline in the observation, so leave frame unset. Narrow a truncated observation with selector or optionFilter before acting.",
|
|
218
|
+
{
|
|
219
|
+
selector: absent(Selector),
|
|
220
|
+
frame: absent(PageControl.Ref),
|
|
221
|
+
optionFilter: absent(Schema.NonEmptyString.check(Schema.isMaxLength(300))),
|
|
222
|
+
},
|
|
223
|
+
PageControl.Observation,
|
|
224
|
+
),
|
|
225
|
+
pageTool(
|
|
226
|
+
"navigate",
|
|
227
|
+
"Navigate the current browser to a host-authorized URL. Input receipt is independent of the returned observation; never replay uncertain navigation.",
|
|
228
|
+
{ url: Schema.String.check(Schema.isMaxLength(8_192)) },
|
|
229
|
+
PageControl.ActionResult,
|
|
230
|
+
),
|
|
231
|
+
pageTool(
|
|
232
|
+
"scroll",
|
|
233
|
+
"Scroll the viewport or an observed scroll container by CSS pixels and inspect the result.",
|
|
234
|
+
{ ref: absent(PageControl.Ref), deltaX: Delta, deltaY: Delta },
|
|
235
|
+
PageControl.ActionResult,
|
|
236
|
+
),
|
|
237
|
+
pageTool(
|
|
238
|
+
"wait",
|
|
239
|
+
"Wait for an asynchronous condition using standard CSS and observed DOM/attribute evidence in the current frame. Optional text qualifies every state, case-sensitively; hidden with text waits until no visible match contains that text. Control kinds are semantic roles, not necessarily HTML tags. Returns a fresh observation; do not wait for an already satisfied condition or use fixed sleeps.",
|
|
240
|
+
{
|
|
241
|
+
selector: Selector,
|
|
242
|
+
frame: absent(PageControl.Ref),
|
|
243
|
+
state: Schema.Literals(["visible", "hidden", "enabled", "text"]),
|
|
244
|
+
text: absent(Schema.String.check(Schema.isMaxLength(1_024))),
|
|
245
|
+
timeoutMillis: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 15_000 })),
|
|
246
|
+
},
|
|
247
|
+
PageControl.Observation,
|
|
248
|
+
),
|
|
249
|
+
pageTool(
|
|
250
|
+
"press",
|
|
251
|
+
"Press a bounded native keyboard key on an observed focusable control, including keyboard-only links with pointerEvents=none and tabindex=0. Revalidates identity, visible state and native focus; a pointer hit is not required. Never replay uncertain input.",
|
|
252
|
+
{
|
|
253
|
+
ref: PageControl.Ref,
|
|
254
|
+
key: Schema.Literals([
|
|
255
|
+
"Enter",
|
|
256
|
+
"Escape",
|
|
257
|
+
"Tab",
|
|
258
|
+
"ArrowDown",
|
|
259
|
+
"ArrowUp",
|
|
260
|
+
"ArrowLeft",
|
|
261
|
+
"ArrowRight",
|
|
262
|
+
"Home",
|
|
263
|
+
"End",
|
|
264
|
+
"Space",
|
|
265
|
+
"Backspace",
|
|
266
|
+
"Delete",
|
|
267
|
+
]),
|
|
268
|
+
},
|
|
269
|
+
PageControl.ActionResult,
|
|
270
|
+
),
|
|
271
|
+
pageTool(
|
|
272
|
+
"select_tab",
|
|
273
|
+
"Select an observed tab or popup in this browser context and inspect it. References from the previous page expire.",
|
|
274
|
+
{ ref: PageControl.Ref },
|
|
275
|
+
PageControl.Observation,
|
|
276
|
+
),
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* The tool that finds lines of the whole page's outline, in view or not. The layer serves it
|
|
281
|
+
* whether or not the agent has it.
|
|
282
|
+
*
|
|
283
|
+
* @stability stable
|
|
284
|
+
* @category toolkits
|
|
285
|
+
* @since 0.3.0
|
|
286
|
+
*/
|
|
287
|
+
export const Find = Toolkit.make(
|
|
288
|
+
pageTool(
|
|
289
|
+
"find",
|
|
290
|
+
"Find the lines of the whole page's outline, in view or not, that hold this text, ignoring case: controls with their refs, headings and text.",
|
|
291
|
+
{ text: Schema.NonEmptyString.check(Schema.isMaxLength(300)) },
|
|
292
|
+
PageControl.Observation,
|
|
293
|
+
),
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
const observe = Tool.make("observe", {
|
|
297
|
+
description: "Read the visible page and controls. Only observed controls may receive actions.",
|
|
298
|
+
parameters: Tool.EmptyParams,
|
|
299
|
+
success: PageControl.Observation,
|
|
300
|
+
failure: ToolError,
|
|
301
|
+
failureMode: "return",
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
// `act` takes one action, or up to eight in order.
|
|
305
|
+
const single = Toolkit.make(
|
|
306
|
+
observe,
|
|
307
|
+
pageTool(
|
|
308
|
+
"act",
|
|
309
|
+
"Perform one action on an observed ref and return the next observation. Never repeat an acknowledged action because its observation failed.",
|
|
310
|
+
{ action: PageControl.Action },
|
|
311
|
+
PageControl.ActionResult,
|
|
312
|
+
),
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
const batched = Toolkit.make(
|
|
316
|
+
observe,
|
|
317
|
+
pageTool(
|
|
318
|
+
"act",
|
|
319
|
+
"Perform 1–8 sequential actions on already observed controls. Stop at page/dialog transitions. completed counts acknowledged actions; never replay them.",
|
|
320
|
+
{
|
|
321
|
+
actions: Schema.Array(PageControl.Action).check(Schema.isMinLength(1), Schema.isMaxLength(8)),
|
|
322
|
+
},
|
|
323
|
+
PageControl.ActionResult,
|
|
324
|
+
),
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* The controller of the run `layer` serves, for an application's own tools beside these, such as a
|
|
329
|
+
* chart's: they act on the tab the model sees now, with the run's task, and say what followed.
|
|
330
|
+
*
|
|
331
|
+
* @stability stable
|
|
332
|
+
* @category services
|
|
333
|
+
* @since 0.3.0
|
|
334
|
+
*/
|
|
335
|
+
export class Controller extends Context.Service<Controller, PageControl.Controller>()(
|
|
336
|
+
"effect-browser/ai/BrowserTools/Controller",
|
|
337
|
+
) {}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* What the model sees before its next turn, for the loop that runs it: `next`, the current tab's
|
|
341
|
+
* screenshot when `vision` says so, and the crops `zoom` took since the last turn, to add to the
|
|
342
|
+
* coming request and never keep. A loop that shows the page its own way takes the crops with
|
|
343
|
+
* `crops` instead, as the model is shown them for the tab `page`. Either hands each crop over once.
|
|
344
|
+
*
|
|
345
|
+
* @stability stable
|
|
346
|
+
* @category services
|
|
347
|
+
* @since 0.3.0
|
|
348
|
+
*/
|
|
349
|
+
export class Pictures extends Context.Service<
|
|
350
|
+
Pictures,
|
|
351
|
+
{
|
|
352
|
+
readonly next: Effect.Effect<Prompt.Prompt>;
|
|
353
|
+
readonly crops: (page: string) => Effect.Effect<ReadonlyArray<Prompt.UserMessagePart>>;
|
|
354
|
+
}
|
|
355
|
+
>()("effect-browser/ai/BrowserTools/Pictures") {}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* What the model sees of the page; see `LayerOptions`.
|
|
359
|
+
*
|
|
360
|
+
* @stability stable
|
|
361
|
+
* @category models
|
|
362
|
+
* @since 0.3.0
|
|
363
|
+
*/
|
|
364
|
+
export type Vision = boolean | "ask" | "page";
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* @stability stable
|
|
368
|
+
* @category options
|
|
369
|
+
* @since 0.3.0
|
|
370
|
+
*/
|
|
371
|
+
export interface LayerOptions extends PageControl.Options {
|
|
372
|
+
readonly initialZooms?: ReadonlyArray<Frame.Zoom> | undefined;
|
|
373
|
+
/**
|
|
374
|
+
* What the model sees of the page: `true`, the default, a screenshot of the current tab before
|
|
375
|
+
* each turn; `"ask"`, one only before a turn after `look`; `"page"`, one also after pointer input
|
|
376
|
+
* and while the viewport shows a canvas, a video or another site's frame; `false`, no pictures.
|
|
377
|
+
*/
|
|
378
|
+
readonly vision?: Vision | undefined;
|
|
379
|
+
/**
|
|
380
|
+
* The pixels of the screenshots and crops the model sees, and of the points it gives back:
|
|
381
|
+
* `"css"`, the default, or `"device"`, the screen's own, as many to a CSS pixel as the browser's
|
|
382
|
+
* device scale factor, so smaller below 1 and sharper above.
|
|
383
|
+
*/
|
|
384
|
+
readonly pixels?: "css" | "device" | undefined;
|
|
385
|
+
/**
|
|
386
|
+
* How `click_at` and `drag` place a point: `"point"`, the default, at once; `"crop"`, first
|
|
387
|
+
* showing a crop of the viewport around each point, then placing once called at points inside
|
|
388
|
+
* crops already shown. A ref needs no crop.
|
|
389
|
+
*/
|
|
390
|
+
readonly placement?: "point" | "crop" | undefined;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const text = (value: string) => ({ type: "text" as const, text: value });
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* What the model sees before a turn: the current tab's screenshot if `screen` says so, and the crops
|
|
397
|
+
* since its last. A screenshot in device pixels sets `scale`, its pixels to a CSS pixel.
|
|
398
|
+
*/
|
|
399
|
+
const pictures = (
|
|
400
|
+
controller: PageControl.Controller,
|
|
401
|
+
zooms: Ref.Ref<Array<Frame.Zoom>>,
|
|
402
|
+
screen: (page: Page.Page) => Effect.Effect<boolean, BrowserError>,
|
|
403
|
+
pixels: "css" | "device",
|
|
404
|
+
scale: Ref.Ref<number>,
|
|
405
|
+
) =>
|
|
406
|
+
Effect.gen(function* () {
|
|
407
|
+
// A look before a turn shows a tab an action opened, as `follow` says.
|
|
408
|
+
const parts = yield* Effect.flatMap(controller.look, (page) =>
|
|
409
|
+
Effect.gen(function* () {
|
|
410
|
+
const zoomed = yield* Ref.getAndSet(zooms, []);
|
|
411
|
+
|
|
412
|
+
if (!(yield* screen(page))) return cropped(zoomed, page.id, yield* Ref.get(scale));
|
|
413
|
+
const { image: shot } = yield* Frame.read(page, { after: "input", scale: pixels });
|
|
414
|
+
|
|
415
|
+
if (pixels === "device") yield* Ref.set(scale, shot.width / (yield* page.viewport).width);
|
|
416
|
+
const crops = cropped(zoomed, page.id, yield* Ref.get(scale));
|
|
417
|
+
|
|
418
|
+
return [
|
|
419
|
+
text(
|
|
420
|
+
`The current tab's viewport, ${shot.width}x${shot.height}. Its pixel coordinates are viewport coordinates.`,
|
|
421
|
+
),
|
|
422
|
+
{ type: "file" as const, mediaType: shot.mediaType, data: shot.data },
|
|
423
|
+
...crops,
|
|
424
|
+
];
|
|
425
|
+
}),
|
|
426
|
+
).pipe(
|
|
427
|
+
Effect.catch((error) =>
|
|
428
|
+
Effect.succeed([text(`(no screenshot of the current tab: ${error.message})`)]),
|
|
429
|
+
),
|
|
430
|
+
controller.withTask,
|
|
431
|
+
);
|
|
432
|
+
|
|
433
|
+
return parts.length === 0 ? Prompt.empty : Prompt.make([{ role: "user", content: parts }]);
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
// Whether the viewport shows what an outline cannot: a canvas, a video, or a frame from another
|
|
437
|
+
// site or holding either, at least 64 pixels square. Page-side code: it stands alone.
|
|
438
|
+
const pictorial = () => {
|
|
439
|
+
const shown = (element: Element) => {
|
|
440
|
+
const box = element.getBoundingClientRect();
|
|
441
|
+
const width = Math.min(box.right, window.innerWidth) - Math.max(box.left, 0);
|
|
442
|
+
const height = Math.min(box.bottom, window.innerHeight) - Math.max(box.top, 0);
|
|
443
|
+
|
|
444
|
+
return width >= 64 && height >= 64;
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
return Array.from(document.querySelectorAll("canvas, video, iframe")).some(
|
|
448
|
+
(element) =>
|
|
449
|
+
shown(element) &&
|
|
450
|
+
(!(element instanceof HTMLIFrameElement) ||
|
|
451
|
+
element.contentDocument === null ||
|
|
452
|
+
element.contentDocument.querySelector("canvas, video, iframe") !== null),
|
|
453
|
+
);
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
const missing = (what: string) =>
|
|
457
|
+
Effect.fail(
|
|
458
|
+
new ToolError({
|
|
459
|
+
code: "invalid",
|
|
460
|
+
message: `Give ${what} a ref from the observation, or x and y from the screenshot.`,
|
|
461
|
+
}),
|
|
462
|
+
);
|
|
463
|
+
|
|
464
|
+
/** The `zoom` tool's handler: a crop of the current tab, shown before the next turn. */
|
|
465
|
+
const cropping =
|
|
466
|
+
(
|
|
467
|
+
controller: PageControl.Controller,
|
|
468
|
+
zooms: Ref.Ref<Array<Frame.Zoom>>,
|
|
469
|
+
vision: Vision,
|
|
470
|
+
scale: Ref.Ref<number>,
|
|
471
|
+
zoomed: { readonly scale: "device" } | undefined,
|
|
472
|
+
) =>
|
|
473
|
+
(region: ConstructorParameters<typeof Frame.Region>[0]) =>
|
|
474
|
+
vision !== false
|
|
475
|
+
? Effect.all([controller.current, Ref.get(scale)]).pipe(
|
|
476
|
+
Effect.flatMap(([page, scale]) =>
|
|
477
|
+
Frame.zoom(page, Points.viewportRegion(new Frame.Region(region), scale), zoomed),
|
|
478
|
+
),
|
|
479
|
+
Effect.tap((zoom) => Ref.update(zooms, (crops) => [...crops, zoom])),
|
|
480
|
+
Effect.map(() => ({
|
|
481
|
+
did: `Cropped (${region.x}, ${region.y}), ${region.width}x${region.height}: it is shown before your next turn if this tab is still current.`,
|
|
482
|
+
followed: [],
|
|
483
|
+
})),
|
|
484
|
+
Effect.mapError(fromBrowserError),
|
|
485
|
+
controller.withTask,
|
|
486
|
+
)
|
|
487
|
+
: Effect.fail(
|
|
488
|
+
new ToolError({
|
|
489
|
+
code: "invalid",
|
|
490
|
+
message: "These tools show no pictures, so a crop cannot be shown.",
|
|
491
|
+
}),
|
|
492
|
+
);
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* The pointer tools' handlers over the controller's current tab. Each sets `pointed` once its input
|
|
496
|
+
* reaches the page, but going back. Points and crops are in the pictures' pixels, `scale` to a
|
|
497
|
+
* CSS pixel. With placement by crop, `framed` holds the crops around the points last asked.
|
|
498
|
+
*/
|
|
499
|
+
const pointing = (
|
|
500
|
+
controller: PageControl.Controller,
|
|
501
|
+
zooms: Ref.Ref<Array<Frame.Zoom>>,
|
|
502
|
+
vision: Vision,
|
|
503
|
+
pointed: Ref.Ref<boolean>,
|
|
504
|
+
pixels: "css" | "device",
|
|
505
|
+
scale: Ref.Ref<number>,
|
|
506
|
+
framed: Ref.Ref<Array<Frame.Zoom>> | undefined,
|
|
507
|
+
) => {
|
|
508
|
+
const zoomed = pixels === "device" ? ({ scale: "device" } as const) : undefined;
|
|
509
|
+
|
|
510
|
+
const act = <A>(
|
|
511
|
+
run: (page: Page.Page) => Effect.Effect<A, BrowserError>,
|
|
512
|
+
did: (value: A) => string,
|
|
513
|
+
action?: Action,
|
|
514
|
+
) =>
|
|
515
|
+
controller.perform(run).pipe(
|
|
516
|
+
Effect.tap(() =>
|
|
517
|
+
action === undefined || action._tag === "Back" ? Effect.void : Ref.set(pointed, true),
|
|
518
|
+
),
|
|
519
|
+
// A program reads a receipt as JSON, which has no undefined, so absent fields are left out.
|
|
520
|
+
Effect.map(({ value, followed }) =>
|
|
521
|
+
action === undefined
|
|
522
|
+
? { did: did(value), followed }
|
|
523
|
+
: { did: did(value), followed, action },
|
|
524
|
+
),
|
|
525
|
+
Effect.mapError(fromBrowserError),
|
|
526
|
+
);
|
|
527
|
+
|
|
528
|
+
// Act once crops shown since the model asked hold every point; otherwise show crops around them.
|
|
529
|
+
const placed = <A>(
|
|
530
|
+
targets: ReadonlyArray<Page.Target>,
|
|
531
|
+
asked: string,
|
|
532
|
+
run: Effect.Effect<A, ToolError>,
|
|
533
|
+
) =>
|
|
534
|
+
Effect.gen(function* () {
|
|
535
|
+
const points = targets.filter((to) => typeof to !== "string");
|
|
536
|
+
|
|
537
|
+
if (framed === undefined || vision === false || points.length === 0) return yield* run;
|
|
538
|
+
const pending = yield* Ref.get(zooms);
|
|
539
|
+
const shown = (yield* Ref.get(framed)).filter((crop) => !pending.includes(crop));
|
|
540
|
+
|
|
541
|
+
if (points.every((point) => shown.some((crop) => Points.holds(crop, point)))) {
|
|
542
|
+
yield* Ref.set(framed, []);
|
|
543
|
+
|
|
544
|
+
return yield* run;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
const crops = yield* Effect.gen(function* () {
|
|
548
|
+
const page = yield* controller.current;
|
|
549
|
+
const viewport = yield* page.viewport;
|
|
550
|
+
|
|
551
|
+
return yield* Effect.forEach(points, (point) =>
|
|
552
|
+
Frame.zoom(page, Points.around(point, viewport), zoomed),
|
|
553
|
+
);
|
|
554
|
+
}).pipe(Effect.mapError(fromBrowserError), controller.withTask);
|
|
555
|
+
|
|
556
|
+
yield* Ref.set(framed, crops);
|
|
557
|
+
yield* Ref.update(zooms, (all) => [...all, ...crops]);
|
|
558
|
+
|
|
559
|
+
return {
|
|
560
|
+
did: `Not done yet: a crop around ${asked} is shown before your next turn. Call again at the points you mean, inside the crops, to act.`,
|
|
561
|
+
followed: [],
|
|
562
|
+
};
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
return Pointer.of({
|
|
566
|
+
click_at: ({ x, y, double, button }) =>
|
|
567
|
+
Effect.flatMap(Ref.get(scale), (scale) => {
|
|
568
|
+
const options = {
|
|
569
|
+
clickCount: double === true ? 2 : 1,
|
|
570
|
+
...(button === undefined ? {} : { button }),
|
|
571
|
+
};
|
|
572
|
+
|
|
573
|
+
const at = { x: x / scale, y: y / scale };
|
|
574
|
+
|
|
575
|
+
return placed(
|
|
576
|
+
[at],
|
|
577
|
+
`(${x}, ${y})`,
|
|
578
|
+
act(
|
|
579
|
+
(page) => page.click(at, options),
|
|
580
|
+
({ element }) => `Clicked ${element} at (${x}, ${y}).`,
|
|
581
|
+
{ _tag: "Click", target: at, options },
|
|
582
|
+
),
|
|
583
|
+
);
|
|
584
|
+
}),
|
|
585
|
+
hover: (place) =>
|
|
586
|
+
Option.match(Points.target(place), {
|
|
587
|
+
onNone: () => missing("hover"),
|
|
588
|
+
onSome: (to) =>
|
|
589
|
+
Effect.flatMap(Ref.get(scale), (scale) => {
|
|
590
|
+
const at = Points.viewportTarget(to, scale);
|
|
591
|
+
|
|
592
|
+
return act(
|
|
593
|
+
(page) => page.hover(at),
|
|
594
|
+
() => `Hovered over ${Points.named(to)}.`,
|
|
595
|
+
{ _tag: "Hover", target: at },
|
|
596
|
+
);
|
|
597
|
+
}),
|
|
598
|
+
}),
|
|
599
|
+
drag: (drag) => {
|
|
600
|
+
const from = Points.target({ ref: drag.fromRef, x: drag.fromX, y: drag.fromY });
|
|
601
|
+
const to = Points.target({ ref: drag.toRef, x: drag.toX, y: drag.toY });
|
|
602
|
+
|
|
603
|
+
return Option.isNone(from) || Option.isNone(to)
|
|
604
|
+
? missing("each end of a drag")
|
|
605
|
+
: Effect.flatMap(Ref.get(scale), (scale) => {
|
|
606
|
+
const start = Points.viewportTarget(from.value, scale);
|
|
607
|
+
const end = Points.viewportTarget(to.value, scale);
|
|
608
|
+
|
|
609
|
+
return placed(
|
|
610
|
+
[start, end],
|
|
611
|
+
`each end, ${Points.named(from.value)} and ${Points.named(to.value)},`,
|
|
612
|
+
act(
|
|
613
|
+
(page) => page.drag(start, end),
|
|
614
|
+
() => `Dragged from ${Points.named(from.value)} to ${Points.named(to.value)}.`,
|
|
615
|
+
{ _tag: "Drag", from: start, to: end },
|
|
616
|
+
),
|
|
617
|
+
);
|
|
618
|
+
});
|
|
619
|
+
},
|
|
620
|
+
type_text: ({ text: typed, submit }) =>
|
|
621
|
+
act(
|
|
622
|
+
(page) => page.type(typed, { submit }),
|
|
623
|
+
() => `Typed ${JSON.stringify(typed)}${submit === true ? " and pressed Enter" : ""}.`,
|
|
624
|
+
{ _tag: "Type", text: typed, ...(submit === undefined ? {} : { options: { submit } }) },
|
|
625
|
+
),
|
|
626
|
+
press_keys: ({ keys, times, holdMillis }) =>
|
|
627
|
+
act(
|
|
628
|
+
(page) => page.press(keys, { times, holdMillis }),
|
|
629
|
+
() => `Pressed ${keys}${times === undefined || times === 1 ? "" : ` ${times} times`}.`,
|
|
630
|
+
{
|
|
631
|
+
_tag: "Press",
|
|
632
|
+
keys,
|
|
633
|
+
options: {
|
|
634
|
+
...(times === undefined ? {} : { times }),
|
|
635
|
+
...(holdMillis === undefined ? {} : { holdMillis }),
|
|
636
|
+
},
|
|
637
|
+
},
|
|
638
|
+
),
|
|
639
|
+
zoom: cropping(controller, zooms, vision, scale, zoomed),
|
|
640
|
+
wait_still: ({ seconds }) =>
|
|
641
|
+
act(
|
|
642
|
+
(page) =>
|
|
643
|
+
// What was already moving before the input, such as a game's idle animation, is not
|
|
644
|
+
// what the model waits for.
|
|
645
|
+
Stillness.wait(page, {
|
|
646
|
+
still: "foreground",
|
|
647
|
+
quiet: "600 millis",
|
|
648
|
+
timeout: `${Math.min(Math.max(seconds ?? 15, 0), 30)} seconds`,
|
|
649
|
+
}),
|
|
650
|
+
() => "The screen is still.",
|
|
651
|
+
),
|
|
652
|
+
back: () =>
|
|
653
|
+
act(
|
|
654
|
+
(page) => page.back,
|
|
655
|
+
() => "Went back.",
|
|
656
|
+
{ _tag: "Back" },
|
|
657
|
+
),
|
|
658
|
+
});
|
|
659
|
+
};
|
|
660
|
+
|
|
661
|
+
/** One mode's tools and the layer that serves them for a run. */
|
|
662
|
+
const tooling = <Refs extends Record<string, Tool.Any>>(
|
|
663
|
+
refs: Toolkit.Toolkit<Refs>,
|
|
664
|
+
serve: (actions: PageControl.Controller["actions"]) => Layer.Layer<Tool.HandlersFor<Refs>>,
|
|
665
|
+
) => {
|
|
666
|
+
const toolkit = Toolkit.merge(refs, Control, Pointer);
|
|
667
|
+
|
|
668
|
+
/** Every tool's handler, and the `Pictures` and `Controller` of one run on `target`. */
|
|
669
|
+
const layer = (target: PageControl.Target, options: LayerOptions = {}) =>
|
|
670
|
+
Layer.unwrap(
|
|
671
|
+
Effect.gen(function* () {
|
|
672
|
+
const controller = yield* PageControl.make(target, options);
|
|
673
|
+
const zooms = yield* Ref.make<Array<Frame.Zoom>>([...(options.initialZooms ?? [])]);
|
|
674
|
+
const vision = options.vision ?? true;
|
|
675
|
+
const pixels = options.pixels ?? "css";
|
|
676
|
+
// The pictures' pixels to a CSS pixel, as the latest screenshot showed them.
|
|
677
|
+
const scale = yield* Ref.make(1);
|
|
678
|
+
const { control } = controller;
|
|
679
|
+
// Since the last turn: whether the model called `look`, and whether pointer input ran.
|
|
680
|
+
const asked = yield* Ref.make(false);
|
|
681
|
+
const pointed = yield* Ref.make(false);
|
|
682
|
+
|
|
683
|
+
const framed =
|
|
684
|
+
options.placement === "crop" ? yield* Ref.make<Array<Frame.Zoom>>([]) : undefined;
|
|
685
|
+
|
|
686
|
+
// Whether the screen is shown before a turn: always with `true`, after `look` with `"ask"`,
|
|
687
|
+
// and with `"page"` also after pointer input or while the viewport shows a picture.
|
|
688
|
+
const wanted = (page: Page.Page) =>
|
|
689
|
+
Effect.gen(function* () {
|
|
690
|
+
const looked = yield* Ref.getAndSet(asked, false);
|
|
691
|
+
const input = yield* Ref.getAndSet(pointed, false);
|
|
692
|
+
|
|
693
|
+
if (vision === true || looked) return true;
|
|
694
|
+
if (vision !== "page") return false;
|
|
695
|
+
|
|
696
|
+
return (
|
|
697
|
+
input ||
|
|
698
|
+
(yield* page.read(pictorial, { arg: undefined, result: Schema.Boolean })).value
|
|
699
|
+
);
|
|
700
|
+
});
|
|
701
|
+
|
|
702
|
+
const handlers = Layer.mergeAll(
|
|
703
|
+
serve(controller.actions),
|
|
704
|
+
Control.toLayer({
|
|
705
|
+
inspect: control.inspect,
|
|
706
|
+
navigate: control.navigate,
|
|
707
|
+
scroll: control.scroll,
|
|
708
|
+
wait: control.wait,
|
|
709
|
+
press: control.press,
|
|
710
|
+
select_tab: control.selectTab,
|
|
711
|
+
}),
|
|
712
|
+
Pointer.toLayer(pointing(controller, zooms, vision, pointed, pixels, scale, framed)),
|
|
713
|
+
Find.toLayer({ find: control.find }),
|
|
714
|
+
Look.toLayer({
|
|
715
|
+
look: () =>
|
|
716
|
+
vision === false
|
|
717
|
+
? Effect.fail(
|
|
718
|
+
new ToolError({ code: "invalid", message: "These tools show no pictures." }),
|
|
719
|
+
)
|
|
720
|
+
: Ref.set(asked, true).pipe(
|
|
721
|
+
Effect.as({
|
|
722
|
+
did:
|
|
723
|
+
vision === "ask" || vision === "page"
|
|
724
|
+
? "A screenshot of the current tab is shown before your next turn."
|
|
725
|
+
: "A screenshot is shown before every turn already.",
|
|
726
|
+
followed: [],
|
|
727
|
+
}),
|
|
728
|
+
),
|
|
729
|
+
}),
|
|
730
|
+
);
|
|
731
|
+
|
|
732
|
+
const shown = Pictures.of({
|
|
733
|
+
next:
|
|
734
|
+
vision === false
|
|
735
|
+
? Effect.succeed(Prompt.empty)
|
|
736
|
+
: pictures(controller, zooms, wanted, pixels, scale),
|
|
737
|
+
crops: (page) =>
|
|
738
|
+
Effect.zipWith(Ref.getAndSet(zooms, []), Ref.get(scale), (crops, scale) =>
|
|
739
|
+
cropped(crops, page, scale),
|
|
740
|
+
),
|
|
741
|
+
});
|
|
742
|
+
|
|
743
|
+
return Layer.mergeAll(
|
|
744
|
+
handlers,
|
|
745
|
+
Layer.succeed(Pictures, shown),
|
|
746
|
+
Layer.succeed(Controller, controller),
|
|
747
|
+
);
|
|
748
|
+
}),
|
|
749
|
+
);
|
|
750
|
+
|
|
751
|
+
return { toolkit, layer };
|
|
752
|
+
};
|
|
753
|
+
|
|
754
|
+
const singleTools = tooling(single, (actions) =>
|
|
755
|
+
single.toLayer({ observe: () => actions.observe, act: ({ action }) => actions.act([action]) }),
|
|
756
|
+
);
|
|
757
|
+
|
|
758
|
+
const batchedTools = tooling(batched, (actions) =>
|
|
759
|
+
batched.toLayer({
|
|
760
|
+
observe: () => actions.observe,
|
|
761
|
+
act: ({ actions: steps }) => actions.act(steps),
|
|
762
|
+
}),
|
|
763
|
+
);
|
|
764
|
+
|
|
765
|
+
/**
|
|
766
|
+
* The tools and their layer. `"batched"`, the default, lets `act` take up to eight actions; with
|
|
767
|
+
* `"single"`, it takes one.
|
|
768
|
+
*
|
|
769
|
+
* @stability stable
|
|
770
|
+
* @category constructors
|
|
771
|
+
* @since 0.3.0
|
|
772
|
+
*/
|
|
773
|
+
export function make(options?: { readonly mode?: "batched" }): typeof batchedTools;
|
|
774
|
+
export function make(options: { readonly mode: "single" }): typeof singleTools;
|
|
775
|
+
|
|
776
|
+
export function make(options: { readonly mode?: "single" | "batched" } = {}) {
|
|
777
|
+
return options.mode === "single" ? singleTools : batchedTools;
|
|
778
|
+
}
|