@undo76/agent-dom 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +122 -0
- package/dist/index.cjs +603 -0
- package/dist/index.d.cts +128 -0
- package/dist/index.d.ts +128 -0
- package/dist/index.js +574 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# agent-dom
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@undo76/agent-dom)
|
|
4
|
+
[](https://github.com/undo76/agent-dom/actions/workflows/publish.yml)
|
|
5
|
+
|
|
6
|
+
`agent-dom` is a browser-native semantic DOM API for AI agents. It runs inside the page, derives roles and accessible names from DOM and ARIA, produces a compact observation, and maps short-lived refs back to real elements for actions.
|
|
7
|
+
|
|
8
|
+
It does not require Playwright, Puppeteer, CDP, or a remote browser.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm install @undo76/agent-dom
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Observe and act
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { createAgentPage } from "@undo76/agent-dom";
|
|
20
|
+
|
|
21
|
+
const page = createAgentPage(window);
|
|
22
|
+
const observation = page.observe();
|
|
23
|
+
|
|
24
|
+
console.log(observation.text);
|
|
25
|
+
// heading "Checkout" [ref=e1] [level=1]
|
|
26
|
+
// textbox "Email" [ref=e2]
|
|
27
|
+
// checkbox "Save card" [ref=e3] [checked=false]
|
|
28
|
+
// button "Pay €32.00" [ref=e4]
|
|
29
|
+
|
|
30
|
+
page.fill("e2", "foo@example.com");
|
|
31
|
+
|
|
32
|
+
// Observe again if the page changed before using another ref.
|
|
33
|
+
const next = page.observe();
|
|
34
|
+
page.click(next.findByRole("button", { name: "Pay" }).ref);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The `@` prefix is optional, so `e4` and `@e4` both work.
|
|
38
|
+
|
|
39
|
+
## Agent-shaped API
|
|
40
|
+
|
|
41
|
+
`act` accepts a discriminated union that can be exposed directly as an LLM tool:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
const observation = page.observe({ interactiveOnly: true });
|
|
45
|
+
|
|
46
|
+
page.act({
|
|
47
|
+
type: "fill",
|
|
48
|
+
ref: observation.findByLabel("Email").ref,
|
|
49
|
+
value: "foo@example.com",
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Supported actions are `click`, `fill`, `select`, `check`, `uncheck`, `focus`, `scroll`, and `press`.
|
|
54
|
+
|
|
55
|
+
## Structured observations
|
|
56
|
+
|
|
57
|
+
Every observation includes an immutable `elements` array:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
const observation = page.observe();
|
|
61
|
+
|
|
62
|
+
observation.elements;
|
|
63
|
+
// [
|
|
64
|
+
// {
|
|
65
|
+
// ref: "e1",
|
|
66
|
+
// role: "textbox",
|
|
67
|
+
// name: "Email",
|
|
68
|
+
// tag: "input",
|
|
69
|
+
// interactive: true,
|
|
70
|
+
// depth: 3,
|
|
71
|
+
// required: true,
|
|
72
|
+
// value: ""
|
|
73
|
+
// }
|
|
74
|
+
// ]
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Password and file-input values are never included in observations.
|
|
78
|
+
|
|
79
|
+
## Locators
|
|
80
|
+
|
|
81
|
+
Locators use the same accessible semantics as observations:
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
const observation = page.observe();
|
|
85
|
+
|
|
86
|
+
observation.findByRole("button", { name: /continue/i });
|
|
87
|
+
observation.findByLabel("Email address");
|
|
88
|
+
observation.findByText("Order summary");
|
|
89
|
+
|
|
90
|
+
page.getByRole("button", { name: "Continue" }); // returns a ref
|
|
91
|
+
page.getByLabel("Email"); // returns a ref
|
|
92
|
+
page.getByText("Order summary"); // returns a ref
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
A locator throws if it finds zero or multiple elements. This keeps agent actions deterministic.
|
|
96
|
+
|
|
97
|
+
## Ref lifetime
|
|
98
|
+
|
|
99
|
+
Refs belong to one observation. A `MutationObserver` tracks changes to the document, including attribute and text changes. If the DOM changes, an action using an older ref throws `StaleElementReferenceError` and the agent must observe again.
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
const first = page.observe();
|
|
103
|
+
const button = first.findByRole("button").ref;
|
|
104
|
+
|
|
105
|
+
document.body.append(document.createElement("div"));
|
|
106
|
+
|
|
107
|
+
page.click(button); // throws StaleElementReferenceError
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Browser boundaries
|
|
111
|
+
|
|
112
|
+
The library traverses the current document and open shadow roots. Normal page JavaScript cannot inspect closed shadow roots or cross-origin iframe documents. A browser extension can inject one `AgentPage` into each permitted frame and merge the results in a coordinator.
|
|
113
|
+
|
|
114
|
+
This library derives a useful semantic view from DOM and ARIA. It is not the browser's privileged accessibility tree, so unusual widgets can differ from Chromium's CDP accessibility output.
|
|
115
|
+
|
|
116
|
+
## Cleanup
|
|
117
|
+
|
|
118
|
+
Disconnect the internal mutation observer when the page object is no longer needed:
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
page.destroy();
|
|
122
|
+
```
|