@edlsh/browse-core 0.6.0 → 0.6.2
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/schema.js
CHANGED
|
@@ -54,11 +54,11 @@ const browserFields = {
|
|
|
54
54
|
return false;
|
|
55
55
|
const url = new URL(value);
|
|
56
56
|
return !url.username && !url.password && !url.hash;
|
|
57
|
-
}, "cdpUrl must be an http(s) or ws(s) endpoint without credentials or a fragment; send credentials in headers").optional().describe("Only for start with backend local. Attach to this explicitly supplied DevTools endpoint, on this machine or another; close disconnects without terminating the browser. Omit
|
|
57
|
+
}, "cdpUrl must be an http(s) or ws(s) endpoint without credentials or a fragment; send credentials in headers").optional().describe("Only for start with backend local. Attach to this explicitly supplied DevTools endpoint, on this machine or another; close disconnects without terminating the browser. Omit for the profile's local browser."),
|
|
58
58
|
headers: z.record(z.string().regex(/^[A-Za-z0-9-]{1,64}$/), z.string().min(1).max(4096)).optional().describe("Only with cdpUrl: HTTP headers an authenticated DevTools gateway needs, e.g. {\"Authorization\":\"Bearer <token>\"}. Sent only to cdpUrl's host; values never appear in results."),
|
|
59
59
|
extensionCdpUrl: z.url({ protocol: /^wss?$/ }).optional().describe("Only with cdpUrl for a browser on another machine or behind a gateway: the browser-level DevTools WebSocket URL the browser itself reaches without headers, e.g. ws://127.0.0.1:9222/devtools/browser/<id>. Default: the URL the browser reports."),
|
|
60
60
|
endpoint: z.string().regex(/^[A-Za-z0-9_-]{1,64}$/).optional().describe("Only for start with backend local: attach to the remote browser this profile configures under that name, which supplies its cdpUrl, headers, and extensionCdpUrl."),
|
|
61
|
-
headless: z.boolean().optional().describe("Only for
|
|
61
|
+
headless: z.boolean().optional().describe("Only for start with backend local: launch a browser of this tool's own, false for a visible window or true for none; cannot be used with cdpUrl or endpoint. Where the profile's local browser is the user's own, omit it to open tabs there: any value launches the separate browser instead, and its tabs never appear in the user's browser."),
|
|
62
62
|
tabId: z.string().min(1).optional().describe("Target tab ID; defaults to the selected tab. Required for tab_select/tab_close."),
|
|
63
63
|
// Other schemes are unsafe or unsupported: javascript: hangs navigation and file: reads the remote machine.
|
|
64
64
|
url: z.url({ protocol: /^https?$/, error: "url must be an http or https URL" }).optional().describe("An http or https URL. Required for open; optional for start/tab_new."),
|
package/dist/session.js
CHANGED
|
@@ -663,6 +663,10 @@ export class BrowseSession {
|
|
|
663
663
|
// Listing the cookie sources needs no browser.
|
|
664
664
|
if (p.action === "cookie_profiles")
|
|
665
665
|
return this.browse(p, context);
|
|
666
|
+
// The person's logins are already here; an import is for a browser this tool launches.
|
|
667
|
+
if (p.action === "cookie_import") {
|
|
668
|
+
throw new BrowseToolError("unsupported_backend", "cookie_import is not needed in the user's real browser: their logins are already there. Open the page, and call handoff if it asks to sign in. To import into a browser of this tool's own instead, close this one, then start with headless set or backend \"browserbase\".");
|
|
669
|
+
}
|
|
666
670
|
if (p.action === "ask" || p.target !== undefined)
|
|
667
671
|
return this.classifyInRealBrowser(real, p, context);
|
|
668
672
|
if (p.action === "har_stop") {
|
|
@@ -698,10 +702,8 @@ export class BrowseSession {
|
|
|
698
702
|
const view = { sessionId: null, backend: "local", origin: "connected", tabId: read.tabId, url: read.url, title: read.title };
|
|
699
703
|
const title = read.title ?? "";
|
|
700
704
|
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
705
|
try {
|
|
704
|
-
return { action: p.action, ...view, data: await askPage({ ...models, ...(image ? { images: [image] } : {}) }, { url: read.url, title, text }, p.fields, context.signal) };
|
|
706
|
+
return { action: p.action, ...view, data: await askPage({ ...models, ...(image ? { images: [image] } : {}) }, { url: read.url, title, text: read.text }, p.fields, context.signal) };
|
|
705
707
|
}
|
|
706
708
|
catch (error) {
|
|
707
709
|
if (context.signal.aborted)
|
package/package.json
CHANGED