@edlsh/browse-core 0.6.0 → 0.6.1
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/dist/real-browser-ai.js
CHANGED
|
@@ -48,7 +48,5 @@ export async function observePage(generate, page, instruction, single) {
|
|
|
48
48
|
}
|
|
49
49
|
/** What the page says in answer to an instruction, as JSON matching `schema`; without one, as `{ extraction }`. */
|
|
50
50
|
export async function extractPage(generate, page, instruction, schema) {
|
|
51
|
-
|
|
52
|
-
const controls = page.outline.replace(/ \[ref=e\d+\]/g, "").replace(/\[ref=e\d+ /g, "[");
|
|
53
|
-
return generateJson(generate, "You extract information from a web page for a browser agent. Use only what the page says, and null for what it does not say. Answer with JSON only.", `${describe(page, instruction)}\n\nPage text:\n${page.text.slice(0, MAX_TEXT)}\n\nControls:\n${controls}`, schema ?? { type: "object", additionalProperties: false, required: ["extraction"], properties: { extraction: { type: "string" } } });
|
|
51
|
+
return generateJson(generate, "You extract information from a web page for a browser agent. Use only what the page says, and null for what it does not say. Answer with JSON only.", `${describe(page, instruction)}\n\nPage text:\n${page.text.slice(0, MAX_TEXT)}`, schema ?? { type: "object", additionalProperties: false, required: ["extraction"], properties: { extraction: { type: "string" } } });
|
|
54
52
|
}
|
|
@@ -5,7 +5,9 @@ export interface RealBrowserPage {
|
|
|
5
5
|
tabId: string;
|
|
6
6
|
url: string;
|
|
7
7
|
title: string | null;
|
|
8
|
+
/** OpenCode Browse's outline, with a ref on every element that can be acted on. */
|
|
8
9
|
outline: string;
|
|
10
|
+
/** What a model reads to answer about the page: its visible text, what its fields hold, and its controls with their states. */
|
|
9
11
|
text: string;
|
|
10
12
|
image?: {
|
|
11
13
|
data: string;
|
|
@@ -32,7 +34,7 @@ export declare class RealBrowserSession {
|
|
|
32
34
|
}, owner: string, group: string, allowedOrigins?: string[]): RealBrowserSession;
|
|
33
35
|
private constructor();
|
|
34
36
|
run(p: BrowserParams, signal: AbortSignal, timeoutMs: number): Promise<BrowserOutput>;
|
|
35
|
-
/** Reads a tab for ask and
|
|
37
|
+
/** Reads a tab for ask, JEV, and browse_ai in one script, so the outline, the text, and the viewport are of the same page. */
|
|
36
38
|
read(tabId: string | undefined, image: boolean, signal: AbortSignal, timeoutMs: number): Promise<RealBrowserPage>;
|
|
37
39
|
/** Runs one script in a tab and returns what it handed back, with the image when it took a screenshot. */
|
|
38
40
|
private script;
|
|
@@ -45,8 +45,25 @@ else await page.waitForLoadState(p.loadState, { timeout });`,
|
|
|
45
45
|
handoff: "data = { handoff: (await handoff(p.message)) ?? null };",
|
|
46
46
|
har_start: 'await browse.network({ action: "start" });',
|
|
47
47
|
};
|
|
48
|
-
/**
|
|
49
|
-
|
|
48
|
+
/**
|
|
49
|
+
* What a page shows and what its form fields hold, read in the page. The outline leaves out what is typed into a
|
|
50
|
+
* field and innerText has no field values at all, so they are listed by label here. Passwords, one-time codes, and
|
|
51
|
+
* card fields are never read; checkboxes and radios are in the outline with their state.
|
|
52
|
+
*/
|
|
53
|
+
const PAGE_TEXT = `(() => {
|
|
54
|
+
const skipped = { password: 1, hidden: 1, file: 1, checkbox: 1, radio: 1, submit: 1, button: 1, reset: 1, image: 1 };
|
|
55
|
+
const values = [];
|
|
56
|
+
for (const el of document.querySelectorAll("input, textarea, select")) {
|
|
57
|
+
if (values.length >= 100) break;
|
|
58
|
+
if (skipped[(el.getAttribute("type") || "").toLowerCase()] || /password|one-time-code|cc-/.test(el.getAttribute("autocomplete") || "")) continue;
|
|
59
|
+
const value = el.tagName === "SELECT" ? Array.from(el.selectedOptions, option => option.label).join(", ") : el.value;
|
|
60
|
+
const label = ((el.labels && el.labels[0] && el.labels[0].innerText) || el.getAttribute("aria-label") || el.getAttribute("placeholder") || el.name || el.id || "").trim().split("\\n")[0];
|
|
61
|
+
if (value && label) values.push(label.slice(0, 80) + ": " + String(value).replace(/\\s+/g, " ").slice(0, 500));
|
|
62
|
+
}
|
|
63
|
+
return { text: document.body ? document.body.innerText : "", values: values.join("\\n") };
|
|
64
|
+
})()`;
|
|
65
|
+
/** A page as ask, JEV, and browse_ai read it: the outline with its refs, the text and field values, and the viewport when asked for. */
|
|
66
|
+
const READ = `data = { outline: String(await snapshot({ maxItems: 200 })), ...await page.evaluate(${JSON.stringify(PAGE_TEXT)}) };
|
|
50
67
|
if (p.image) shot = await page.screenshot({ fullPage: false });`;
|
|
51
68
|
export class RealBrowserSession {
|
|
52
69
|
client;
|
|
@@ -141,11 +158,13 @@ export class RealBrowserSession {
|
|
|
141
158
|
...(image ? { image: new Uint8Array(Buffer.from(image.data, "base64")) } : {}),
|
|
142
159
|
};
|
|
143
160
|
}
|
|
144
|
-
/** Reads a tab for ask and
|
|
161
|
+
/** Reads a tab for ask, JEV, and browse_ai in one script, so the outline, the text, and the viewport are of the same page. */
|
|
145
162
|
async read(tabId, image, signal, timeoutMs) {
|
|
146
163
|
const { tab, view, image: shot } = await this.script("read", tabId, { image }, READ, signal, timeoutMs);
|
|
147
|
-
const { outline, text } = view.data;
|
|
148
|
-
|
|
164
|
+
const { outline, text, values } = view.data;
|
|
165
|
+
const controls = outline.replace(/ \[ref=e\d+\]/g, "").replace(/\[ref=e\d+ /g, "[");
|
|
166
|
+
const readable = `${text}${values ? `\n\nField values:\n${values}` : ""}\n\nControls:\n${controls}`;
|
|
167
|
+
return { tabId: String(tab), url: view.url, title: view.title, outline, text: readable, ...(shot ? { image: shot } : {}) };
|
|
149
168
|
}
|
|
150
169
|
/** Runs one script in a tab and returns what it handed back, with the image when it took a screenshot. */
|
|
151
170
|
async script(action, tabId, args, body, signal, timeoutMs) {
|
package/dist/session.js
CHANGED
|
@@ -698,10 +698,8 @@ export class BrowseSession {
|
|
|
698
698
|
const view = { sessionId: null, backend: "local", origin: "connected", tabId: read.tabId, url: read.url, title: read.title };
|
|
699
699
|
const title = read.title ?? "";
|
|
700
700
|
if (models) {
|
|
701
|
-
// The text says what the page shows; the outline adds what controls hold and whether they are checked.
|
|
702
|
-
const text = `${read.text}\n\nControls:\n${read.outline.replace(/ \[ref=e\d+\]/g, "").replace(/\[ref=e\d+ /g, "[")}`;
|
|
703
701
|
try {
|
|
704
|
-
return { action: p.action, ...view, data: await askPage({ ...models, ...(image ? { images: [image] } : {}) }, { url: read.url, title, text }, p.fields, context.signal) };
|
|
702
|
+
return { action: p.action, ...view, data: await askPage({ ...models, ...(image ? { images: [image] } : {}) }, { url: read.url, title, text: read.text }, p.fields, context.signal) };
|
|
705
703
|
}
|
|
706
704
|
catch (error) {
|
|
707
705
|
if (context.signal.aborted)
|
package/package.json
CHANGED