Skip to content

An agentic browser harness: an AI side panel & orb in your own logged-in Chrome, Edge, Arc, Brave or even Firefox run by the agent/subscription you already use (Claude Code, Codex, Antigravity). Free, open source, no API key. Also an MCP server.

feat(actions): hand page text back one group at a time, resumable by cursor

Shahriarc15925f6615b
diff --git a/lib/actions/page/chunks.ts b/lib/actions/page/chunks.ts
new file mode 100644
index 0000000..cafc114
--- /dev/null
+++ b/lib/actions/page/chunks.ts
@@ -0,0 +1,49 @@
+const FNV_OFFSET = 2166136261;
+const FNV_PRIME = 16777619;
+
+const BOUNDARIES = [
+ { pattern: /\n{2,}/g, floor: 0.5 },
+ { pattern: /(?<=[.!?…]["'’”)\]]*)\s+/g, floor: 0.25 },
+ { pattern: /[。!?]["’”」』))\]]*/g, floor: 0.25 },
+ { pattern: /\n/g, floor: 0.25 },
+ { pattern: /\s+/g, floor: 0 },
+];
+
+const CURSOR = /^(\d+)\.([0-9a-f]{8})$/;
+
+export function digestOf(text: string): string {
+ let hash = FNV_OFFSET;
+ for (let i = 0; i < text.length; i += 1) {
+ hash ^= text.charCodeAt(i);
+ hash = Math.imul(hash, FNV_PRIME);
+ }
+ return (hash >>> 0).toString(16).padStart(8, '0');
+}
+
+export function groupEnd(text: string, start: number, budget: number): number {
+ if (text.length - start <= budget) return text.length;
+ const window = text.slice(start, start + budget);
+ for (const { pattern, floor } of BOUNDARIES) {
+ const cut = lastBoundary(window, pattern, Math.floor(budget * floor));
+ if (cut > 0) return start + cut;
+ }
+ return start + budget;
+}
+
+export function writeCursor(offset: number, delivered: string): string {
+ return `${offset}.${digestOf(delivered)}`;
+}
+
+export function readCursor(cursor: string): { offset: number; digest: string } | null {
+ const parsed = CURSOR.exec(cursor.trim());
+ return parsed ? { offset: Number(parsed[1]), digest: parsed[2] } : null;
+}
+
+function lastBoundary(window: string, pattern: RegExp, floor: number): number {
+ pattern.lastIndex = 0;
+ let end = 0;
+ for (let match = pattern.exec(window); match; match = pattern.exec(window)) {
+ if (match.index >= floor) end = match.index + match[0].length;
+ }
+ return end;
+}
diff --git a/lib/actions/page/extract-text.ts b/lib/actions/page/extract-text.ts
index 2115bff..0730e04 100644
--- a/lib/actions/page/extract-text.ts
+++ b/lib/actions/page/extract-text.ts
@@ -1,12 +1,20 @@
import { z } from 'zod';
-import { defineAction } from '../core';
+import { ActionError, defineAction } from '../core';
+import { digestOf, groupEnd, readCursor, writeCursor } from './chunks';
import { cssPath, resolveTarget, targetSchema } from './dom';
const MAX_EXTRACT_CHARS = 200_000;
export const extractText = defineAction({
name: 'page.extractText',
- description: 'Read the rendered text or raw HTML of an element or the whole page.',
+ description:
+ 'Read the rendered text or raw HTML of an element or the whole page, one group at a time. ' +
+ 'A group is cut at the last paragraph or sentence boundary that fits in maxLength, never mid-sentence, ' +
+ 'and the same page always groups the same way. When more text remains the reply carries a cursor — pass it ' +
+ 'back for the next group, and keep going until no cursor comes back. ' +
+ 'The cursor is checked against the text it already handed you, so two groups never come from two different ' +
+ 'versions of the page: if the page rewrote what you had read, the reply is "stale": true and nothing else, ' +
+ 'and the read starts over with no cursor.',
input: z.object({
target: targetSchema.optional().describe('Element to read; defaults to the whole page'),
format: z
@@ -22,17 +30,41 @@ export const extractText = defineAction({
.positive()
.max(MAX_EXTRACT_CHARS)
.default(20000)
- .describe('Truncate the result to this many characters'),
+ .describe('Characters per group — the reply stops at the last boundary that fits, so it comes back a little shorter'),
+ cursor: z
+ .string()
+ .max(32)
+ .optional()
+ .describe('Continue a read: the cursor the previous reply returned. Omit to start from the top.'),
}),
- execute({ target, format, maxLength }) {
+ execute({ target, format, maxLength, cursor }) {
const element = target ? resolveTarget(target) : document.body;
const full =
format === 'text' ? element.innerText.replace(/\n{3,}/g, '\n\n').trim() : element.outerHTML;
+ const source = cssPath(element);
+ const budget = Math.min(maxLength, MAX_EXTRACT_CHARS);
+
+ const resume = cursor ? readCursor(cursor) : null;
+ if (cursor && !resume) {
+ throw new ActionError(
+ 'That is not a cursor page.extractText handed out — omit it to read from the top',
+ 'INVALID_INPUT',
+ );
+ }
+ if (resume && digestOf(full.slice(0, resume.offset)) !== resume.digest) {
+ return { stale: true, source, length: full.length };
+ }
+
+ const offset = resume?.offset ?? 0;
+ const end = groupEnd(full, offset, budget);
+ const more = end < full.length;
return {
- content: full.slice(0, Math.min(maxLength, MAX_EXTRACT_CHARS)),
- truncated: full.length > Math.min(maxLength, MAX_EXTRACT_CHARS),
+ content: full.slice(offset, end).trimEnd(),
+ source,
length: full.length,
- source: cssPath(element),
+ offset,
+ truncated: more,
+ ...(more ? { nextOffset: end, cursor: writeCursor(end, full.slice(0, end)) } : {}),
};
},
});