D2Snap is a first-of-its-kind DOM downsampling algorithm, designed for use with LLM-based web agents.
D2Snap.d2Snap(
dom: DOM,
rE: number, rA: number, rT: number,
options?: Options
): Promise<{
html: string;
meta: {};
}>
D2Snap.adaptiveD2Snap(
dom: DOM,
maxTokens: number = 4096,
maxIterations: number = 5,
options?: Options
): Promise<{
html: string;
meta: {};
parameters: {};
adaptiveIterations: number;
}>type DOM = Document | Element | string;
type Options = {
debug?: boolean; // false
uiFeatureHeuristics?: object; // compare src/types.ts:UIFeatureHeuristicsJSON
uiFeatureHeuristicsReplaceDefault?: boolean; // false
skipMarkdown?: boolean; // false
skipTextRank?: boolean; // false
uniqueIDs?: boolean; // false
};The downsampling UI feature heuristics can be overridden via
options.uiFeatureHeuristics(full replacement viauiFeatureHeuristicsReplaceDefault: true). Wildcards forariaanddataattributes are supported ({aria-|data-}*).
<script src="https://cdn.jsdelivr.net/gh/webfuse-com/D2Snap@main/dist.browser/D2Snap.js"></script>npm install webfuse-com/D2SnapInstall jsdom to use the library with Node.js:
npm install jsdom
import * as D2Snap from "@webfuse-com/d2snap";<section class="container" tabindex="3" required="true" type="example">
<div class="mx-auto" data-topic="products" required="false">
<h1>Our Pizza</h1>
<div>
<div class="shadow-lg">
<h2>Margherita</h2>
<p>
A simple classic: mozzarella, tomatoes and basil.
An everyday choice!
</p>
<button type="button">Add</button>
</div>
<div class="shadow-lg">
<h2>Capricciosa</h2>
<p>
A rich taste: mozzarella, ham, mushrooms, artichokes and olives.
A true favourite!
</p>
<button type="button">Add</button>
</div>
</div>
</div>
</section>↓ D2Snap ↓
<section class="container" required="true" type="example">
<div class="mx-auto" required="false">
# Our Pizza
<div>
## Margherita
A simple classic: mozzarella, tomatoes and basil.
An everyday choice!
<button type="button">Add</button>
## Capricciosa
A rich taste: mozzarella, ham, mushrooms, artichokes and olives.
A true favourite!
<button type="button">Add</button>
</div>
</div>
</section>↓ D2Snap ↓
# Our Pizza
## Margherita
A simple classic: mozzarella, tomatoes and basil.
An everyday choice!
<button>Add</button>
## Capricciosa
A rich taste: mozzarella, ham, mushrooms, artichokes and olives.
A true favourite!
<button>Add</button>npm install
npm install jsdomnpm run buildnpm run testProvide LLM API provider key(s) to .env (compare example).
cs eval && pip install -r requirements.txtnpm run eval:success:<subject>
npm run eval:ratio
<subject>∈ {gui,dom,bu,D2Snap}
npm run eval:success:D2Snap -- --verbose --split 10,20 --provider openai --model gpt-4onpm run snapshots:create
Beyond Pixels: Exploring DOM Downsampling for LLM-Based Web Agents
Thassilo M. Schiepanski
Nicholas Piël
Surfly BV
