Files
gnome-jarvis/browser-use/reading.js
T
snxraven aa06c71fe1
Rolling release / release (push) Failing after 1m46s
obsidian
2026-09-14 11:24:10 -04:00

67 lines
3.1 KiB
JavaScript

/** Turn a browser snapshot into a reading the model can answer from. No Playwright. */
const PAGE_VISION_QUESTION = 'The attached images are slices of one open page, in order from the top to the bottom. page_text is the full readable page after it was scrolled. Answer from all of page_text, not only the first image. Do not only describe the pictures. Do not mention file paths.';
function pageSliceOffsets(scrollHeight, viewport, limit = 4) {
const view = Math.max(1, Number(viewport) || 800);
const height = Math.max(view, Number(scrollHeight) || view);
const maxY = Math.max(0, height - view);
const screens = Math.max(1, Math.ceil(height / view));
if (maxY < 80 || screens < 2) return [0];
const count = Math.min(Math.max(2, Number(limit) || 4), screens);
const out = [];
for (let i = 0; i < count; i++) out.push(Math.round((maxY * i) / (count - 1)));
return [...new Set(out)];
}
function lines(items, limit) {
return (Array.isArray(items) ? items : []).map((item) => String(item || '').replace(/\s+/g, ' ').trim()).filter(Boolean).slice(0, limit);
}
export function formatBrowserReading(shot) {
if (!shot || typeof shot !== 'object') return '';
const text = String(shot.text || shot.article || '').trim();
const headings = lines(shot.headings, 40);
const tables = (Array.isArray(shot.tables) ? shot.tables : []).map((table) => String(table || '').trim()).filter(Boolean).slice(0, 8);
const images = lines(shot.seen || shot.images, 40).filter((item) => !/^https?:/i.test(item));
const refs = (Array.isArray(shot.refs) ? shot.refs : []).slice(0, 48);
if (!text && !headings.length && !tables.length && !images.length && !refs.length && !shot.url) return '';
const out = [];
if (shot.url) out.push('url: ' + shot.url);
if (shot.title) out.push('title: ' + shot.title);
if (shot.coverage) out.push('coverage: ' + shot.coverage);
if (shot.challenge) out.push('challenge: true');
if (shot.next_action) out.push('next_action: ' + shot.next_action);
out.push('');
out.push('page_text:');
out.push(text || '(no readable text yet)');
if (headings.length) {
out.push('');
out.push('headings:');
for (const heading of headings) out.push('- ' + heading);
}
if (tables.length) {
out.push('');
out.push('tables:');
out.push(tables.join('\n\n'));
}
if (images.length) {
out.push('');
out.push('visible:');
for (const image of images) out.push('- ' + image);
}
if (refs.length) {
out.push('');
out.push('refs:');
for (const ref of refs) {
const name = String(ref.name || '').replace(/\s+/g, ' ').trim();
out.push(String(ref.ref) + ' ' + String(ref.role || '') + ' ' + name);
}
}
out.push('');
out.push('page_text is the full readable page after scrolling it. Read it from start to end, including the part past the first screen. The images are slices of that same page from top to bottom. If this is only a search or link list, open the best result and read that page before drafting. Do not answer from titles or the first image alone.');
return out.join('\n');
}
export { PAGE_VISION_QUESTION, pageSliceOffsets };