@parcha/pi-browser 0.1.0-beta.73 → 0.1.0-beta.75
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/CHANGELOG.md +4 -0
- package/README.md +5 -5
- package/dist/core/public-fetch.d.ts +9 -3
- package/dist/core/public-fetch.js +33 -4
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 0.1.0-beta.75 (2026-11-30)
|
|
6
|
+
|
|
7
|
+
- A page web_fetch can't read whole now gives the agent its start and a way to the rest. A page whose body is over 5 MiB, fetched directly, used to answer `refused: the page is over 5 MiB` with no text at all; it now answers its first 5 MiB as the page's start, `cut_bytes` on every part, and the last part's line says the rest wasn't read. A page's text past the first 1,000,000 characters is still out of paging's reach, and every part that shows it now says so. An answer to an `objective` for such a page says its chunks are from the page's start alone. Of each, on a run granted `web_save`, the line tells the agent to save the page whole as a file with it and read that file. `publicFetch` takes `cut: true` for this; without it, and in `publicDownload` (web_save's), a body over the cap is refused as before. `webFetchText` takes the save tool's name as an optional third argument. <!-- changes/web-fetch-big-page.md -->
|
|
8
|
+
|
|
5
9
|
## 0.1.0-beta.73 (2026-11-29)
|
|
6
10
|
|
|
7
11
|
- The public-host guard (`publicFetch`, `publicDownload`, `publicCheck`) refuses a URL holding a user name or password itself, on the first hop and at every redirect, before that hop's name is resolved or anything is sent to it: `refused: the URL holds a user name or password`, or `refused: a redirect to a URL holding a user name or password`. The refusal never quotes the URL. Before, a redirect to such a URL was stopped only by undici's own `Request`, whose error quotes the URL whole, and a guard given another fetch followed it. Every hop now goes through the same checks, the first one's included: scheme, user name or password, then name, addresses and port. web_fetch's direct fetch, web_save's download and the server's fal file download answer such a redirect with the guard's refusal. <!-- changes/public-fetch-userinfo-every-hop.md -->
|
package/README.md
CHANGED
|
@@ -76,7 +76,7 @@ It prints the page's accessibility tree, the same text that `snapshot` gives a m
|
|
|
76
76
|
|
|
77
77
|
## Demos
|
|
78
78
|
|
|
79
|
-
Two runnable demos live in `examples/` in the repository. They are not in the npm package. Build once with `
|
|
79
|
+
Two runnable demos live in `examples/` in the repository. They are not in the npm package. Build once with `pnpm install --frozen-lockfile && pnpm run build`, then run one from the repository root. Each needs Chrome or Chromium (`CHROME_PATH` names it) except where it says otherwise.
|
|
80
80
|
|
|
81
81
|
| Demo | Run | What it shows |
|
|
82
82
|
|---|---|---|
|
|
@@ -268,7 +268,7 @@ The pi peers are `@earendil-works/pi-durable` and `chord` `1.5.2` exactly. CI te
|
|
|
268
268
|
- `@parcha/pi-browser`: the host-neutral core. The model contract (tool names, descriptions, JSON schemas, effect and
|
|
269
269
|
replay classes, the `browser` and `web` prompt sections), the typed failure envelope, the redactor, the screenshot
|
|
270
270
|
budget, the repeat guard, evidence and decision-hook types, usage and prices, `find` (chunking and ranking a long page),
|
|
271
|
-
`browser_downloads`, the opt-in request observer, and the provider interface. The public-host guard is here too: `publicFetch(url, { lookup?, fetchImpl?, signal?, maxBytes? })`, its bytes
|
|
271
|
+
`browser_downloads`, the opt-in request observer, and the provider interface. The public-host guard is here too: `publicFetch(url, { lookup?, fetchImpl?, signal?, maxBytes?, cut? })` (a text body over `maxBytes` refused, or with `cut` answered as its first `maxBytes`, `cut: true`, the rest never read), its bytes
|
|
272
272
|
variant `publicDownload(url, { lookup?, fetchImpl?, signal?, maxBytes? })` (one file of any type, `{ url, status,
|
|
273
273
|
contentType, bytes }`, `bytes` null for a status that is not 2xx; the same hops, checks and pinned dial as
|
|
274
274
|
`publicFetch`, a declared length over `maxBytes` refused before the body is read and a body over it refused as it is
|
|
@@ -417,9 +417,9 @@ Two things to know before you give a model a browser:
|
|
|
417
417
|
## Development
|
|
418
418
|
|
|
419
419
|
```sh
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
CHROME_PATH=/path/to/chrome
|
|
420
|
+
pnpm --filter @parcha/pi-browser run build
|
|
421
|
+
pnpm --filter @parcha/pi-browser test # everything but the real-Chrome suites
|
|
422
|
+
CHROME_PATH=/path/to/chrome pnpm --filter @parcha/pi-browser run test:chrome
|
|
423
423
|
```
|
|
424
424
|
|
|
425
425
|
The real-Chrome suites (`stagehand_real_chrome`, `cdp_local_chrome`, `effects-chrome`, `screenshot_clip_real_chrome`,
|
|
@@ -19,12 +19,14 @@ export type PublicFetchOptions = {
|
|
|
19
19
|
* dialled. Default 5. */
|
|
20
20
|
maxRedirects?: number;
|
|
21
21
|
};
|
|
22
|
-
/** The answer that ended the redirects: its URL, status and type, and its body when it is a 2xx text answer.
|
|
22
|
+
/** The answer that ended the redirects: its URL, status and type, and its body when it is a 2xx text answer. `cut` when
|
|
23
|
+
* that body is only its first `maxBytes` (publicFetch's `cut`): the rest was not read. */
|
|
23
24
|
export type PublicAnswer = {
|
|
24
25
|
url: string;
|
|
25
26
|
status: number;
|
|
26
27
|
contentType: string;
|
|
27
28
|
body: string | null;
|
|
29
|
+
cut?: true;
|
|
28
30
|
};
|
|
29
31
|
/** The answer that ended the redirects, as `publicDownload` gives it: its URL, status and type, and its bytes when it is a
|
|
30
32
|
* 2xx answer (an empty array for a 2xx with no body). */
|
|
@@ -48,10 +50,14 @@ export declare function publicCheck(url: string, options?: {
|
|
|
48
50
|
* body. Refused (`BrowserFailureError` `refused`), on the first hop or a redirect: a scheme other than http(s) (other
|
|
49
51
|
* than https, with `httpsOnly`); a URL holding a user name or password; a host that is local by name or resolves to any
|
|
50
52
|
* private address; a port other than 80
|
|
51
|
-
* or 443; too many redirects (any, with `maxRedirects` 0); a 2xx body that is not text or is over the cap. `
|
|
53
|
+
* or 443; too many redirects (any, with `maxRedirects` 0); a 2xx body that is not text or is over the cap. With `cut`, a
|
|
54
|
+
* body over the cap is not refused: its first `maxBytes` are read and decoded (a character the cut splits dropped), the
|
|
55
|
+
* rest is never read, and the answer says `cut`. `signal` ends the fetch at any point, a
|
|
52
56
|
* hop's name lookup included (it rejects with the signal's reason), and an aborted hop is never dialled.
|
|
53
57
|
*/
|
|
54
|
-
export declare function publicFetch(url: string, options?: PublicFetchOptions
|
|
58
|
+
export declare function publicFetch(url: string, options?: PublicFetchOptions & {
|
|
59
|
+
cut?: boolean;
|
|
60
|
+
}): Promise<PublicAnswer>;
|
|
55
61
|
/**
|
|
56
62
|
* Downloads `url` from a public host: publicFetch's hops and refusals (scheme, user name or password, local or private host, port, redirects,
|
|
57
63
|
* the signal), with any content type accepted and the body kept as bytes, up to `maxBytes` (default 5 MiB). A 2xx answer
|
|
@@ -128,11 +128,12 @@ function bomEncoding(bytes) {
|
|
|
128
128
|
}
|
|
129
129
|
/** A text page's `bytes` as text, decoded as a browser decodes it: by its byte order mark, else its Content-Type's
|
|
130
130
|
* `charset`, else (an HTML page) its <meta> charset in the first 1024 bytes, else UTF-8. A label no encoding goes by is
|
|
131
|
-
* passed over for the next. The mark itself is not kept. Bytes the charset cannot read become U+FFFD.
|
|
132
|
-
|
|
131
|
+
* passed over for the next. The mark itself is not kept. Bytes the charset cannot read become U+FFFD. Of a body `cut`
|
|
132
|
+
* short, a character the cut splits is left out, never decoded as U+FFFD: the decoder is not flushed at the end. */
|
|
133
|
+
function pageText(bytes, contentType, cut = false) {
|
|
133
134
|
const bom = bomEncoding(bytes);
|
|
134
135
|
const decoder = (bom ? new TextDecoder(bom) : undefined) ?? decoderFor(charsetParam(contentType), false) ?? (HTML_TYPE.test(contentType) ? metaDecoder(bytes) : undefined) ?? new TextDecoder("utf-8");
|
|
135
|
-
return decoder.decode(bytes);
|
|
136
|
+
return cut ? decoder.decode(bytes, { stream: true }) : decoder.decode(bytes);
|
|
136
137
|
}
|
|
137
138
|
const MIB = 1024 * 1024;
|
|
138
139
|
/** The options checked before any lookup: `maxBytes` (default 5 MiB) a whole number of bytes above 0, and
|
|
@@ -227,6 +228,7 @@ async function readCapped(body, maxBytes, over, signal) {
|
|
|
227
228
|
const { done, value } = await nextChunk(reader, signal);
|
|
228
229
|
if (done)
|
|
229
230
|
break;
|
|
231
|
+
// Refused as it passes the cap, before anything read is joined: a refused body is never copied.
|
|
230
232
|
if ((size += value.length) > maxBytes) {
|
|
231
233
|
await reader.cancel();
|
|
232
234
|
throw refusedError(over);
|
|
@@ -235,6 +237,25 @@ async function readCapped(body, maxBytes, over, signal) {
|
|
|
235
237
|
}
|
|
236
238
|
return Buffer.concat(chunks);
|
|
237
239
|
}
|
|
240
|
+
/** `body`'s first `maxBytes`, and whether it went on past them (`cut`): read until it ends or passes them, then the rest
|
|
241
|
+
* cancelled unread (publicFetch's `cut`; readCapped refuses instead). `signal` as readCapped's. */
|
|
242
|
+
async function readUpTo(body, maxBytes, signal) {
|
|
243
|
+
const chunks = [];
|
|
244
|
+
let size = 0;
|
|
245
|
+
for (const reader = body.getReader();;) {
|
|
246
|
+
const { done, value } = await nextChunk(reader, signal);
|
|
247
|
+
if (done)
|
|
248
|
+
break;
|
|
249
|
+
if (size + value.length > maxBytes) {
|
|
250
|
+
chunks.push(value.subarray(0, maxBytes - size));
|
|
251
|
+
await reader.cancel();
|
|
252
|
+
return { bytes: Buffer.concat(chunks), cut: true };
|
|
253
|
+
}
|
|
254
|
+
size += value.length;
|
|
255
|
+
chunks.push(value);
|
|
256
|
+
}
|
|
257
|
+
return { bytes: Buffer.concat(chunks), cut: false };
|
|
258
|
+
}
|
|
238
259
|
/** `body`, whose host stated it is `length` bytes, read into one buffer of that length, allocated before the first chunk:
|
|
239
260
|
* the file is held once, never joined into a second copy. A chunk that runs past `length` is refused as it comes, the
|
|
240
261
|
* body cancelled; a body that ends short of it is refused at its end. `signal` as readCapped's. */
|
|
@@ -275,7 +296,9 @@ function statedLength(headers) {
|
|
|
275
296
|
* body. Refused (`BrowserFailureError` `refused`), on the first hop or a redirect: a scheme other than http(s) (other
|
|
276
297
|
* than https, with `httpsOnly`); a URL holding a user name or password; a host that is local by name or resolves to any
|
|
277
298
|
* private address; a port other than 80
|
|
278
|
-
* or 443; too many redirects (any, with `maxRedirects` 0); a 2xx body that is not text or is over the cap. `
|
|
299
|
+
* or 443; too many redirects (any, with `maxRedirects` 0); a 2xx body that is not text or is over the cap. With `cut`, a
|
|
300
|
+
* body over the cap is not refused: its first `maxBytes` are read and decoded (a character the cut splits dropped), the
|
|
301
|
+
* rest is never read, and the answer says `cut`. `signal` ends the fetch at any point, a
|
|
279
302
|
* hop's name lookup included (it rejects with the signal's reason), and an aborted hop is never dialled.
|
|
280
303
|
*/
|
|
281
304
|
export async function publicFetch(url, options = {}) {
|
|
@@ -289,6 +312,12 @@ export async function publicFetch(url, options = {}) {
|
|
|
289
312
|
return { url: target.href, status: res.status, contentType, body: "" };
|
|
290
313
|
if (!TEXT_TYPE.test(contentType))
|
|
291
314
|
throw refusedError(`refused: ${contentType || "an unknown content type"} is not text`);
|
|
315
|
+
if (options.cut) {
|
|
316
|
+
const { bytes, cut } = await readUpTo(res.body, maxBytes);
|
|
317
|
+
if (!cut)
|
|
318
|
+
return { url: target.href, status: res.status, contentType, body: pageText(bytes, contentType) };
|
|
319
|
+
return { url: target.href, status: res.status, contentType, body: pageText(bytes, contentType, true), cut: true };
|
|
320
|
+
}
|
|
292
321
|
const body = await readCapped(res.body, maxBytes, `refused: the page is over ${maxBytes === PUBLIC_MAX_BYTES ? "5 MiB" : `${maxBytes} bytes`}`);
|
|
293
322
|
return { url: target.href, status: res.status, contentType, body: pageText(body, contentType) };
|
|
294
323
|
});
|