@parcha/agentrun-web 0.1.0-beta.58 → 0.1.0-beta.59
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/README.md +126 -11
- package/dist/decide/decider.d.ts +183 -0
- package/dist/decide/decider.js +248 -0
- package/dist/decide/failure.d.ts +6 -0
- package/dist/decide/failure.js +22 -0
- package/dist/decide/fetch.d.ts +53 -0
- package/dist/decide/fetch.js +95 -0
- package/dist/decide/page-guard.d.ts +36 -0
- package/dist/decide/page-guard.js +26 -0
- package/dist/decide/passages.d.ts +6 -0
- package/dist/decide/passages.js +54 -0
- package/dist/decide/record.d.ts +102 -0
- package/dist/decide/record.js +125 -0
- package/dist/decide/search.d.ts +18 -0
- package/dist/decide/search.js +43 -0
- package/dist/fees.d.ts +35 -2
- package/dist/fees.js +62 -2
- package/dist/fetch.d.ts +48 -41
- package/dist/fetch.js +112 -72
- package/dist/index.d.ts +17 -6
- package/dist/index.js +19 -7
- package/dist/provider.d.ts +113 -0
- package/dist/provider.js +34 -0
- package/dist/providers/direct.d.ts +15 -0
- package/dist/providers/direct.js +24 -8
- package/dist/providers/exa-keyless.d.ts +13 -2
- package/dist/providers/exa-keyless.js +33 -14
- package/dist/providers/exa.d.ts +68 -0
- package/dist/providers/exa.js +111 -0
- package/dist/providers/firecrawl-keyless.d.ts +25 -14
- package/dist/providers/firecrawl-keyless.js +91 -46
- package/dist/providers/firecrawl.d.ts +77 -0
- package/dist/providers/firecrawl.js +145 -0
- package/dist/providers/keyed.d.ts +44 -0
- package/dist/providers/keyed.js +136 -0
- package/dist/providers/native.d.ts +5 -0
- package/dist/providers/native.js +11 -0
- package/dist/providers/parallel.d.ts +55 -10
- package/dist/providers/parallel.js +127 -39
- package/dist/providers/registry.d.ts +119 -0
- package/dist/providers/registry.js +35 -0
- package/dist/search.d.ts +86 -17
- package/dist/search.js +101 -34
- package/dist/tool-specs.d.ts +65 -0
- package/dist/tool-specs.js +36 -0
- package/dist/web.d.ts +109 -30
- package/dist/web.js +165 -63
- package/package.json +2 -2
package/dist/fees.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
// Parallel's prices, the Fee a paid request carries, and Pay, the injected cap a paid request goes
|
|
2
|
-
// the cap refuses is never sent: Pay throws PaidRefused, whose message is the refusal.
|
|
1
|
+
// Parallel's, Exa's and Firecrawl's prices, the Fee a paid request carries, and Pay, the injected cap a paid request goes
|
|
2
|
+
// through. A request the cap refuses is never sent: Pay throws PaidRefused, whose message is the refusal.
|
|
3
3
|
/** Parallel's published prices (https://docs.parallel.ai/resources/pricing, read 2026-11-20). Search, by the request's
|
|
4
4
|
* `mode`: `turbo` or `fast` $1 per 1,000 requests, `basic` or `advanced` $5 per 1,000, each with 10 results included,
|
|
5
5
|
* and each result past those 10 $1 per 1,000. Extract: $1 per 1,000 URLs. */
|
|
@@ -32,6 +32,66 @@ export function parallelFeeOf(url, body) {
|
|
|
32
32
|
return parallelExtractFee(Array.isArray(asked.urls) ? Math.max(1, asked.urls.length) : 1);
|
|
33
33
|
return undefined;
|
|
34
34
|
}
|
|
35
|
+
export const EXA_SEARCH_TYPE_USD = Object.freeze({ lite: 0.004, instant: 0.004, auto: 0.007, fast: 0.007, "deep-lite": 0.012, deep: 0.012, "deep-reasoning": 0.015 });
|
|
36
|
+
/** Each search result past the 10 a type includes: $1 per 1,000. */
|
|
37
|
+
export const EXA_SEARCH_RESULT_USD = 0.001;
|
|
38
|
+
export const EXA_SEARCH_INCLUDED = 10;
|
|
39
|
+
/** Each content asked of a page in a contents request (text, highlights, summary): $1 per 1,000 pages, so a page with
|
|
40
|
+
* text and highlights is two. A search's text and highlights are in its type's price (Exa's own cost report,
|
|
41
|
+
* costDollars, 2026-11-24); its AI page summaries are $1 per 1,000 pages. */
|
|
42
|
+
export const EXA_CONTENT_USD = 0.001;
|
|
43
|
+
export const EXA_SUMMARY_USD = 0.001;
|
|
44
|
+
const EXA_CONTENTS = ["text", "highlights", "summary"];
|
|
45
|
+
const isExaType = (type) => typeof type === "string" && Object.hasOwn(EXA_SEARCH_TYPE_USD, type);
|
|
46
|
+
/** How many of Exa's priced contents `contents` asks for: each one given and not false. */
|
|
47
|
+
const exaContentsAsked = (contents) => (typeof contents === "object" && contents !== null ? EXA_CONTENTS.filter((name) => { const asked = contents[name]; return asked !== undefined && asked !== null && asked !== false; }).length : 0);
|
|
48
|
+
/** The fee of a request to Exa's `path` with `body`: a search's by its type (auto when it names none, as Exa reads it), its
|
|
49
|
+
* results past 10 and a summary of each where it asks one; a contents request's by its URLs and the contents asked of each (one
|
|
50
|
+
* at least: Exa answers text when none is asked). Undefined for an endpoint the web tools do not ask, and for a search
|
|
51
|
+
* type with no price here. */
|
|
52
|
+
export function exaFeeOf(path, body) {
|
|
53
|
+
const asked = (body ?? {});
|
|
54
|
+
if (path === "/search") {
|
|
55
|
+
const type = asked.type ?? "auto";
|
|
56
|
+
if (!isExaType(type))
|
|
57
|
+
return undefined;
|
|
58
|
+
const n = typeof asked.numResults === "number" && asked.numResults > 0 ? Math.floor(asked.numResults) : EXA_SEARCH_INCLUDED;
|
|
59
|
+
const summary = asked.contents?.summary;
|
|
60
|
+
const summaries = summary !== undefined && summary !== null && summary !== false ? n : 0;
|
|
61
|
+
const more = Math.max(0, n - EXA_SEARCH_INCLUDED);
|
|
62
|
+
return { name: "Exa's search", by: "exa", usd: usd(EXA_SEARCH_TYPE_USD[type] + EXA_SEARCH_RESULT_USD * more + EXA_SUMMARY_USD * summaries), basis: `Exa's list price for a ${type} search of ${n} results${summaries ? ", each summarized" : ""}` };
|
|
63
|
+
}
|
|
64
|
+
if (path === "/contents") {
|
|
65
|
+
const urls = Array.isArray(asked.urls) ? Math.max(1, asked.urls.length) : 1;
|
|
66
|
+
const contents = Math.max(1, exaContentsAsked(asked));
|
|
67
|
+
return { name: "Exa's contents", by: "exa", usd: usd(EXA_CONTENT_USD * contents * urls), basis: urls === 1 && contents === 1 ? "Exa's list price for one page's text" : `Exa's list price for ${contents} contents of ${urls} pages` };
|
|
68
|
+
}
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
/** Firecrawl's price of a credit at the key's plan: Standard, $99 a month for 100,000 credits
|
|
72
|
+
* (https://www.firecrawl.dev/pricing, read 2026-11-24; the plan's credits read from /v2/team/credit-usage that day).
|
|
73
|
+
* Credits past the plan cost $5 per 2,000 ($0.0025). A search is 2 credits per 10 results, and a credit for each result
|
|
74
|
+
* it scrapes; a scrape is 1 credit, and 4 more for the JSON format. */
|
|
75
|
+
export const FIRECRAWL_CREDIT_USD = 0.00099;
|
|
76
|
+
export const FIRECRAWL_SEARCH_CREDITS = 2;
|
|
77
|
+
export const FIRECRAWL_SCRAPE_CREDITS = 1;
|
|
78
|
+
export const FIRECRAWL_JSON_CREDITS = 4;
|
|
79
|
+
/** Whether a scrape's `formats` asks for JSON (a string or an object of that type). */
|
|
80
|
+
const asksJson = (formats) => Array.isArray(formats) && formats.some((format) => format === "json" || (typeof format === "object" && format !== null && format.type === "json"));
|
|
81
|
+
/** The fee of a request to Firecrawl's `path` with `body`: a search's by its limit (10 by default) and whether it scrapes
|
|
82
|
+
* its results, a scrape's by whether it asks for JSON. Undefined for an endpoint the web tools do not ask. */
|
|
83
|
+
export function firecrawlFeeOf(path, body) {
|
|
84
|
+
const asked = (body ?? {});
|
|
85
|
+
const fee = (name, credits, basis) => ({ name, by: "firecrawl", usd: usd(FIRECRAWL_CREDIT_USD * credits), basis: `${basis}: ${credits} credit${credits === 1 ? "" : "s"} at the key's plan price` });
|
|
86
|
+
if (path === "/v2/search") {
|
|
87
|
+
const n = typeof asked.limit === "number" && asked.limit > 0 ? Math.floor(asked.limit) : 10;
|
|
88
|
+
const scraped = asked.scrapeOptions ? n * (FIRECRAWL_SCRAPE_CREDITS + (asksJson(asked.scrapeOptions.formats) ? FIRECRAWL_JSON_CREDITS : 0)) : 0;
|
|
89
|
+
return fee("Firecrawl's search", FIRECRAWL_SEARCH_CREDITS * Math.ceil(n / 10) + scraped, `Firecrawl's list price for a search of ${n} results${scraped ? ", each scraped" : ""}`);
|
|
90
|
+
}
|
|
91
|
+
if (path === "/v2/scrape")
|
|
92
|
+
return fee("Firecrawl's scrape", FIRECRAWL_SCRAPE_CREDITS + (asksJson(asked.formats) ? FIRECRAWL_JSON_CREDITS : 0), "Firecrawl's list price for a scrape");
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
35
95
|
/** A paid request that was not sent: the run's cap refused it, or it has no price here. Its message says why, in the
|
|
36
96
|
* cap's words. */
|
|
37
97
|
export class PaidRefused extends Error {
|
package/dist/fetch.d.ts
CHANGED
|
@@ -1,59 +1,66 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { FoundPage } from "./decide/fetch.js";
|
|
2
|
+
import type { Guard } from "./guard.js";
|
|
2
3
|
import { type Page, type Part, type Read, type Unread } from "./page.js";
|
|
3
|
-
import type
|
|
4
|
-
import type
|
|
4
|
+
import { type Reader, type ReadOptions } from "./provider.js";
|
|
5
|
+
import { type AheadProvider, type FetchProvider } from "./providers/registry.js";
|
|
6
|
+
import { type Try } from "./search.js";
|
|
5
7
|
/** How many pages a run keeps what it read of (Pages): each at most PAGE_READ_MAX_CHARS characters of text. */
|
|
6
8
|
export declare const PAGES_HELD = 8;
|
|
7
|
-
/**
|
|
8
|
-
* the
|
|
9
|
-
*
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
/** How a page came: `via`, the provider that gave it ("none": the caller's order holds no provider that ends it, and no
|
|
10
|
+
* provider in it read the page; "stopped": the call was stopped first); under the id of each provider asked ahead of it
|
|
11
|
+
* (AheadProvider, provider.ts FetchShape) that gave none, why, where the guarded direct fetch answered or none did
|
|
12
|
+
* (never on a page another provider read). */
|
|
13
|
+
export type Via = {
|
|
14
|
+
via: FetchProvider | "stopped" | "none";
|
|
15
|
+
} & Partial<Record<AheadProvider, string>>;
|
|
16
|
+
export type WebPage = Page & Via;
|
|
17
|
+
/** A page web_fetch read, and how. */
|
|
18
|
+
export type WebRead = Read & Via;
|
|
16
19
|
/** A page web_fetch could not read, and how it tried. */
|
|
17
|
-
export type WebUnread = Unread &
|
|
20
|
+
export type WebUnread = Unread & Via;
|
|
18
21
|
/** The pages a run reads, by the URL asked for, each from its read's start (so calls at once share it), the last used
|
|
19
22
|
* last: at most PAGES_HELD of them. */
|
|
20
23
|
export type Pages = Map<string, Promise<WebRead | WebUnread>>;
|
|
21
|
-
/**
|
|
22
|
-
export type
|
|
23
|
-
/** The order a read takes when none is given: keyless Firecrawl, then Parallel, each where its
|
|
24
|
+
/** The readers a read asks, by provider: each held or not, each read one page (provider.ts Reader). */
|
|
25
|
+
export type Readers = Partial<Record<FetchProvider, Reader>>;
|
|
26
|
+
/** The order a read takes when none is given: keyless Firecrawl, then Parallel, each where its reader is given, then the
|
|
24
27
|
* guarded direct fetch. */
|
|
25
28
|
export declare const FETCH_ORDER: readonly FetchProvider[];
|
|
26
|
-
/** Where a read notes what the model is not told: an event and its fields (serve writes them as a log line).
|
|
29
|
+
/** Where a read notes what the model is not told: an event and its fields (serve writes them as a log line). A provider
|
|
30
|
+
* whose misses are logged (provider.ts FetchShape) notes its event with what it `said` (in our words, at most 300
|
|
31
|
+
* characters) and, logged at once, its `provider` and the `limit` it met, as JSON, if any; logged once a provider that
|
|
32
|
+
* ends the order answered, whether that one `direct`ly "read" the page or "failed". */
|
|
27
33
|
export type WebLog = (event: string, fields: Record<string, string>) => void;
|
|
28
|
-
/** One provider a read asked, as it answered, or
|
|
29
|
-
* fetch's receipt. */
|
|
30
|
-
export type FetchTry = {
|
|
34
|
+
/** One provider a read asked, as it answered, or one passed over with why (its "not tried: ..."), or held: a turn of a
|
|
35
|
+
* fetch's receipt (search.ts Try). */
|
|
36
|
+
export type FetchTry = Try & {
|
|
31
37
|
provider: FetchProvider;
|
|
32
|
-
outcome: "ok" | "error";
|
|
33
|
-
error?: string;
|
|
34
38
|
};
|
|
35
|
-
/** What webFetch is given: the guard's resolver and fetch (a test's) and the part, the
|
|
36
|
-
*
|
|
37
|
-
* (`checkMs`), the `log`, `
|
|
38
|
-
*
|
|
39
|
-
export type WebFetchOptions =
|
|
40
|
-
|
|
41
|
-
firecrawl?: KeylessFetch | null;
|
|
39
|
+
/** What webFetch is given: the guard's resolver and fetch (a test's) and the part, the `readers` and their `order`
|
|
40
|
+
* (FETCH_ORDER by default; a provider that ends it is made here where no reader is given for it), the run's `pages`,
|
|
41
|
+
* the `guard`, the guard check's bound (`checkMs`), the `log`, and `tries`, where a read this call starts records its
|
|
42
|
+
* turns (a call answered from `pages` records none). */
|
|
43
|
+
export type WebFetchOptions = ReadOptions & Part & {
|
|
44
|
+
readers?: Readers;
|
|
42
45
|
pages?: Pages;
|
|
43
46
|
order?: readonly FetchProvider[];
|
|
44
47
|
guard?: Guard;
|
|
45
48
|
checkMs?: number;
|
|
46
49
|
log?: WebLog;
|
|
47
|
-
|
|
48
|
-
noParallel?: string;
|
|
50
|
+
tries?: Try[];
|
|
49
51
|
};
|
|
50
|
-
/** The
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
export declare function
|
|
56
|
-
/**
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
export declare function
|
|
52
|
+
/** The page at `url` as read once a run: from `pages`, the run's, where it holds the URL (read, or being read), with no
|
|
53
|
+
* request of its own; else read once (readOnce), kept there from the read's start. A read that fails leaves `pages`, so
|
|
54
|
+
* a later call reads again. The read is the run's, not a caller's: no caller's signal reaches it (it keeps Parallel's
|
|
55
|
+
* and the fetch's own time limits), so one caller's stop never fails another's; each caller stops waiting at its own
|
|
56
|
+
* signal (untilStopped). */
|
|
57
|
+
export declare function webRead(url: unknown, { readers, pages, order, guard, checkMs, signal, log, tries, ...options }?: Omit<WebFetchOptions, keyof Part>): Promise<WebRead | WebUnread>;
|
|
58
|
+
/** The part `part` names of a page read once (webRead), as a WebPage, how it came after it; a read that failed as it is. */
|
|
59
|
+
export declare function webPart(read: WebRead | WebUnread, part?: Part): WebPage;
|
|
60
|
+
/** Why each provider asked ahead of the one that gave `page` gave none, by its id (Via), in the order they were asked. */
|
|
61
|
+
export declare function missesOf(page: object): Partial<Record<AheadProvider, string>>;
|
|
62
|
+
/** The part (`offset`, `maxChars`) of the page at `url` as a WebPage: webPart of webRead. */
|
|
63
|
+
export declare function webFetch(url: unknown, { offset, maxChars, ...options }?: WebFetchOptions): Promise<WebPage>;
|
|
64
|
+
/** A page as the tool `name` answers the model: its JSON, then a line for each provider ahead that gave none saying why
|
|
65
|
+
* and what gave the page instead, and one saying where this part is in the page when it is not all of it. */
|
|
66
|
+
export declare function webFetchText(page: WebPage | Page | FoundPage, name?: string): string;
|
package/dist/fetch.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
import { unmetered } from "./fees.js";
|
|
1
2
|
import { askedUrl, HTML_READ_CHARS, PAGE_READ_MAX_CHARS, partOfRead, TIMEOUT_MS, URL_MAX, withoutCredentials, withoutUserinfo } from "./page.js";
|
|
2
|
-
import {
|
|
3
|
+
import { HELD_CODE, holdingWords } from "./provider.js";
|
|
4
|
+
import { AHEAD, providerName, providerOf } from "./providers/registry.js";
|
|
5
|
+
import { failure } from "./search.js";
|
|
3
6
|
const PORTS = new Set(["", "80", "443"]);
|
|
4
7
|
/** How many pages a run keeps what it read of (Pages): each at most PAGE_READ_MAX_CHARS characters of text. */
|
|
5
8
|
export const PAGES_HELD = 8;
|
|
6
|
-
/** The order a read takes when none is given: keyless Firecrawl, then Parallel, each where its
|
|
9
|
+
/** The order a read takes when none is given: keyless Firecrawl, then Parallel, each where its reader is given, then the
|
|
7
10
|
* guarded direct fetch. */
|
|
8
11
|
export const FETCH_ORDER = ["firecrawl-keyless", "parallel", "direct"];
|
|
9
12
|
/** The URL a provider is sent for `url`, as parsed (its `href`), so the provider reads the host the guard checked; or
|
|
@@ -24,64 +27,74 @@ async function passes(url, guard, options, ms) {
|
|
|
24
27
|
const signal = AbortSignal.any([AbortSignal.timeout(ms), ...(options.signal ? [options.signal] : [])]);
|
|
25
28
|
return guard.check(target.href, { ...(options.lookup ? { lookup: options.lookup } : {}), signal }).then(() => ({ href: target.href }), () => ({ why: "the URL did not pass the public-host guard" }));
|
|
26
29
|
}
|
|
27
|
-
/** The page at `url` read once, by the providers of `order` in turn
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* gave
|
|
31
|
-
async function readOnce(url, {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
return { ...read, url: withoutCredentials(read.url), via: "firecrawl-keyless" };
|
|
47
|
-
}
|
|
48
|
-
firecrawlSaid = withoutUserinfo(read);
|
|
49
|
-
turn("firecrawl-keyless", firecrawlSaid.slice(0, 300));
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
else if (provider === "parallel") {
|
|
53
|
-
const sent = parallel?.live() ? await passes(url, guard, options, checkMs) : null;
|
|
54
|
-
if (!parallel)
|
|
55
|
-
why = noParallel;
|
|
56
|
-
// Asked again after the check: a refusal while it ran turns Parallel off for this read too.
|
|
57
|
-
else if (!parallel.live() || !sent)
|
|
58
|
-
why = "not tried: refused earlier in this run";
|
|
59
|
-
else if ("why" in sent)
|
|
60
|
-
why = `not tried: ${sent.why}`;
|
|
61
|
-
else {
|
|
62
|
-
const read = await parallel.read(sent.href, options.signal);
|
|
63
|
-
if (typeof read !== "string") {
|
|
64
|
-
turn("parallel", read);
|
|
65
|
-
// A second scrub behind the client's own, kept on purpose: it covers a url the client did not clear and has not cut.
|
|
66
|
-
return { ...read, url: withoutCredentials(read.url), via: "parallel" };
|
|
67
|
-
}
|
|
68
|
-
why = withoutUserinfo(read);
|
|
69
|
-
}
|
|
70
|
-
if (why !== undefined)
|
|
71
|
-
turn("parallel", why);
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
const direct = await readDirect(url, guard, options);
|
|
75
|
-
turn("direct", direct);
|
|
76
|
-
// Why Firecrawl gave no page goes to the log, never to the model (the page is the direct fetch's); never the URL.
|
|
77
|
-
if (firecrawlSaid !== undefined)
|
|
78
|
-
log?.("web_fetch_firecrawl_failed", { said: firecrawlSaid.slice(0, 300), direct: direct.ok ? "read" : "failed" });
|
|
30
|
+
/** The page at `url` read once, by the providers of `order` in turn, each turn recorded in `tries` as it is taken. A
|
|
31
|
+
* provider that ends the order (the guarded direct fetch) answers whatever it reads. Any other is asked where its reader
|
|
32
|
+
* is given and not held and the guard passes the URL within `checkMs`, and is sent the URL as the guard checked it; why
|
|
33
|
+
* a read gave no page, or why a turn was not asked, is said on the page, and logged or recorded as its FetchShape says. */
|
|
34
|
+
async function readOnce(url, { readers, order, guard, checkMs, options, log, tries }) {
|
|
35
|
+
const said = {};
|
|
36
|
+
const logged = [];
|
|
37
|
+
for (const id of order) {
|
|
38
|
+
const shape = providerOf(id).fetch;
|
|
39
|
+
const reader = readers[id];
|
|
40
|
+
if (!shape || !reader)
|
|
41
|
+
continue;
|
|
42
|
+
if (shape.ends) {
|
|
43
|
+
// Its failure, typed, is the receipt's, never the page's.
|
|
44
|
+
const { failure, ...read } = (await reader.read(url, options));
|
|
45
|
+
// Why a provider ahead gave no page is logged as well as said, never with the URL.
|
|
46
|
+
for (const miss of logged)
|
|
47
|
+
log?.(miss.event, { said: miss.said, direct: read.ok ? "read" : "failed" });
|
|
48
|
+
tries.push({ provider: id, outcome: read.ok ? "ok" : "error", ...(read.ok ? {} : { error: read.refused ?? read.error ?? "" }), ...(failure ? { failure } : {}) });
|
|
79
49
|
// A second scrub behind readDirect's own, kept on purpose: it covers a url readDirect did not clear and has not cut.
|
|
80
50
|
// No test reaches it: readDirect clears every url it names, the URL asked and the one its fetch ended at alike.
|
|
81
|
-
return { ...
|
|
51
|
+
return { ...read, url: withoutCredentials(read.url), via: id, ...said };
|
|
52
|
+
}
|
|
53
|
+
// A provider is sent the URL the guard checked, as parsed (passes), or not asked, saying why in readDirect's words.
|
|
54
|
+
const before = reader.held();
|
|
55
|
+
const checked = before ? undefined : await passes(url, guard, options, checkMs);
|
|
56
|
+
// Asked again after the check: a refusal while it ran (its key's, by the other tool) holds it for this read too.
|
|
57
|
+
const held = before ?? reader.held();
|
|
58
|
+
const why = held ?? (checked && "why" in checked ? failure("refused", "GUARD", `not tried: ${checked.why}`) : undefined);
|
|
59
|
+
if (why !== undefined) {
|
|
60
|
+
if (shape.miss === "said" || held)
|
|
61
|
+
said[id] = why.message;
|
|
62
|
+
// Every turn not asked is on the receipt, as search's are: one its breaker holds as held, any other (its key refused
|
|
63
|
+
// earlier, the guard's refusal) as a failed one.
|
|
64
|
+
if (held?.code === HELD_CODE)
|
|
65
|
+
tries.push({ provider: id, outcome: "held", failure: held, ms: 0 });
|
|
66
|
+
else
|
|
67
|
+
tries.push({ provider: id, outcome: "error", error: why.message, failure: why });
|
|
68
|
+
continue;
|
|
82
69
|
}
|
|
70
|
+
const read = await reader.read(checked.href, options);
|
|
71
|
+
if ("ok" in read && read.ok) {
|
|
72
|
+
tries.push({ provider: id, outcome: "ok" });
|
|
73
|
+
// A reason is said only beside the direct fetch's answer or none's: webFetchText's line says it was fetched directly.
|
|
74
|
+
// A second scrub behind the client's own, kept on purpose: it covers a url the client did not clear and has not cut.
|
|
75
|
+
return { ...read, url: withoutCredentials(read.url), via: id };
|
|
76
|
+
}
|
|
77
|
+
// What a provider says reaches the page, the receipt and the log with no URL's user name or password in it: scrubbed
|
|
78
|
+
// whole as it is read, before any cut (a second scrub behind the client's own, kept on purpose).
|
|
79
|
+
const error = withoutUserinfo(read.error ?? "");
|
|
80
|
+
const limit = "limit" in read ? read.limit : undefined;
|
|
81
|
+
const typed = "failure" in read && read.failure ? { failure: { ...read.failure, message: withoutUserinfo(read.failure.message) } } : {};
|
|
82
|
+
// A read that met a limit is said by what holds the provider now, never by the provider's words for it.
|
|
83
|
+
const holding = limit ? reader.holding?.() : undefined;
|
|
84
|
+
const words = holding ? holdingWords(holding) : error;
|
|
85
|
+
if (shape.miss === "said") {
|
|
86
|
+
said[id] = words;
|
|
87
|
+
tries.push({ provider: id, outcome: "error", error, ...typed });
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
said[id] = words.slice(0, 300);
|
|
91
|
+
tries.push({ provider: id, outcome: "error", ...(error ? { error: error.slice(0, 300) } : {}), ...typed, ...(limit ? { limit } : {}) });
|
|
92
|
+
if (shape.miss.when === "failed")
|
|
93
|
+
log?.(shape.miss.log, { provider: id, said: error.slice(0, 300), ...(limit ? { limit: JSON.stringify(limit) } : {}) });
|
|
94
|
+
else
|
|
95
|
+
logged.push({ event: shape.miss.log, said: error.slice(0, 300) });
|
|
83
96
|
}
|
|
84
|
-
return { ok: false, url: askedUrl(url), status: 0, title: "", text: "", links: [], error: "no provider in the order read the page, and it holds no direct fetch", via: "none", ...
|
|
97
|
+
return { ok: false, url: askedUrl(url), status: 0, title: "", text: "", links: [], error: "no provider in the order read the page, and it holds no direct fetch", via: "none", ...said };
|
|
85
98
|
}
|
|
86
99
|
/** The reads that have ended: only these leave `pages` past its bound (trim). */
|
|
87
100
|
const settled = new WeakSet();
|
|
@@ -116,16 +129,17 @@ function untilStopped(url, reading, signal) {
|
|
|
116
129
|
reading.then((read) => (signal.removeEventListener("abort", onStop), resolve(read)), (error) => (signal.removeEventListener("abort", onStop), reject(error)));
|
|
117
130
|
});
|
|
118
131
|
}
|
|
119
|
-
/** The
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
export async function
|
|
132
|
+
/** The page at `url` as read once a run: from `pages`, the run's, where it holds the URL (read, or being read), with no
|
|
133
|
+
* request of its own; else read once (readOnce), kept there from the read's start. A read that fails leaves `pages`, so
|
|
134
|
+
* a later call reads again. The read is the run's, not a caller's: no caller's signal reaches it (it keeps Parallel's
|
|
135
|
+
* and the fetch's own time limits), so one caller's stop never fails another's; each caller stops waiting at its own
|
|
136
|
+
* signal (untilStopped). */
|
|
137
|
+
export async function webRead(url, { readers = {}, pages, order = FETCH_ORDER, guard, checkMs = TIMEOUT_MS, signal, log, tries = [], ...options } = {}) {
|
|
125
138
|
const named = typeof url === "string" ? url : undefined;
|
|
126
139
|
let reading = named === undefined ? undefined : pages?.get(named);
|
|
127
140
|
if (!reading) {
|
|
128
|
-
const
|
|
141
|
+
const ending = order.filter((id) => !readers[id] && providerOf(id).fetch?.ends).map((id) => [id, endingReader(id, guard)]);
|
|
142
|
+
const started = readOnce(url, { readers: { ...readers, ...Object.fromEntries(ending) }, order, guard, checkMs, options, log, tries });
|
|
129
143
|
if (pages && named !== undefined) {
|
|
130
144
|
// Ahead of any caller's await, so no call sees a failed read kept.
|
|
131
145
|
const drop = () => void (pages.get(named) === started && pages.delete(named));
|
|
@@ -140,11 +154,31 @@ export async function webFetch(url, { parallel, firecrawl, pages, order = FETCH_
|
|
|
140
154
|
pages.set(named, reading);
|
|
141
155
|
trim(pages);
|
|
142
156
|
}
|
|
143
|
-
|
|
157
|
+
return untilStopped(url, reading, signal);
|
|
158
|
+
}
|
|
159
|
+
/** A provider that ends the order, made for one read through `guard`: never held. */
|
|
160
|
+
function endingReader(id, guard) {
|
|
161
|
+
const run = providerOf(id).make({ ...(guard ? { guard } : {}), pay: unmetered, now: Date.now });
|
|
162
|
+
return { held: () => undefined, read: (url, options) => run.read(url, options) };
|
|
163
|
+
}
|
|
164
|
+
/** The part `part` names of a page read once (webRead), as a WebPage, how it came after it; a read that failed as it is. */
|
|
165
|
+
export function webPart(read, part = {}) {
|
|
144
166
|
if (!read.ok)
|
|
145
167
|
return read;
|
|
146
|
-
const { via,
|
|
147
|
-
|
|
168
|
+
const { via, ...page } = read;
|
|
169
|
+
const said = missesOf(read);
|
|
170
|
+
for (const id of AHEAD)
|
|
171
|
+
delete page[id];
|
|
172
|
+
return { ...partOfRead(page, part), via, ...said };
|
|
173
|
+
}
|
|
174
|
+
const AHEAD_IDS = new Set(AHEAD);
|
|
175
|
+
/** Why each provider asked ahead of the one that gave `page` gave none, by its id (Via), in the order they were asked. */
|
|
176
|
+
export function missesOf(page) {
|
|
177
|
+
return Object.fromEntries(Object.entries(page).filter(([id, why]) => AHEAD_IDS.has(id) && typeof why === "string"));
|
|
178
|
+
}
|
|
179
|
+
/** The part (`offset`, `maxChars`) of the page at `url` as a WebPage: webPart of webRead. */
|
|
180
|
+
export async function webFetch(url, { offset, maxChars, ...options } = {}) {
|
|
181
|
+
return webPart(await webRead(url, options), { offset, maxChars });
|
|
148
182
|
}
|
|
149
183
|
/** What the part no part follows says of a page whose HTML readPage cut before it was read (html_chars). */
|
|
150
184
|
const CUT_LINE = `The page was cut at ${HTML_READ_CHARS.toLocaleString("en-US")} characters of HTML before reading; the rest wasn't read.`;
|
|
@@ -169,14 +203,20 @@ function partLine(page, name) {
|
|
|
169
203
|
return `Nothing at offset=${start}: the page has ${page.original_chars} characters.`;
|
|
170
204
|
return `Characters ${start}-${end} of ${page.original_chars}: the end of the page.`;
|
|
171
205
|
}
|
|
172
|
-
/**
|
|
173
|
-
|
|
174
|
-
|
|
206
|
+
/** What answered after the providers ahead gave no page: the direct fetch, or nothing (Via: reasons are said only there). */
|
|
207
|
+
function cameBy(via) {
|
|
208
|
+
return via === "none" ? "no provider after it read the page" : "this one was fetched directly";
|
|
209
|
+
}
|
|
210
|
+
/** A page as the tool `name` answers the model: its JSON, then a line for each provider ahead that gave none saying why
|
|
211
|
+
* and what gave the page instead, and one saying where this part is in the page when it is not all of it. */
|
|
175
212
|
export function webFetchText(page, name = "web_fetch") {
|
|
176
213
|
const lines = [JSON.stringify(page)];
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
214
|
+
const came = cameBy(page.via);
|
|
215
|
+
for (const [id, why] of Object.entries(missesOf(page))) {
|
|
216
|
+
if (why)
|
|
217
|
+
lines.push(`${providerName(id)} gave no page (${why}); ${came}.`);
|
|
218
|
+
}
|
|
219
|
+
const where = "found" in page ? undefined : partLine(page, name);
|
|
180
220
|
if (where)
|
|
181
221
|
lines.push(where);
|
|
182
222
|
return lines.join("\n");
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,23 @@
|
|
|
1
|
-
export { type
|
|
1
|
+
export { type Destination, type Destinations, type FetchOptions, type Hold, type Receipt, type SearchInput, type SearchOptions, type Web, type WebConfig, createWeb, destinationsLine, destinationsWords, } from "./web.js";
|
|
2
2
|
export { type Guard, type GuardOptions, type HtmlRead, type HtmlReader, type PublicAnswer, type PublicCheck, type PublicFetch } from "./guard.js";
|
|
3
|
-
export { type
|
|
4
|
-
export { type
|
|
5
|
-
export { type
|
|
3
|
+
export { type FetchShape, type NativeSearch, type ProviderParts, type ProviderRun, type ReadFailed, type Reader, type SearchAsk, type SettingsSpec, type WebClient, type WebProvider, defineProvider, holdingWords, readerOf, } from "./provider.js";
|
|
4
|
+
export { type AheadProvider, type FetchProvider, type KeyedProvider, type ProviderId, type ProviderSettings, type SaidProvider, type SearchProvider, AHEAD, isProviderId, PROVIDERS, providerName, providerOf, SAID, serves, settingsProblemsOf, } from "./providers/registry.js";
|
|
5
|
+
export { type ExaSearchType, type Fee, type ParallelSearchMode, type Pay, EXA_CONTENT_USD, EXA_SEARCH_INCLUDED, EXA_SEARCH_RESULT_USD, EXA_SEARCH_TYPE_USD, EXA_SUMMARY_USD, exaFeeOf, FIRECRAWL_CREDIT_USD, FIRECRAWL_JSON_CREDITS, FIRECRAWL_SCRAPE_CREDITS, FIRECRAWL_SEARCH_CREDITS, firecrawlFeeOf, PARALLEL_EXTRACT_USD, PARALLEL_SEARCH_INCLUDED, PARALLEL_SEARCH_MODE_USD, PARALLEL_SEARCH_RESULT_USD, PARALLEL_SEARCH_USD, PaidRefused, parallelExtractFee, parallelFeeOf, parallelSearchFee, unmetered, } from "./fees.js";
|
|
6
|
+
export { type Backend, type Breaker, type Failure, type FailureClass, type Hit, type Holding, type Limit, type Search, type SearchBounds, type Searched, type Try, ANSWER_MAX, breakers, CITE, fitSearch, OBJECTIVE_MAX, QUERIES_MAX, QUERY_MAX, RESULTS_DEFAULT, RESULTS_MAX, SEARCH_MAX_CHARS, SNIPPET_MAX, searchArgs, searchBreaker, searchInTurn, searchText, snippetOf, } from "./search.js";
|
|
7
|
+
export { type FetchTry, type Pages, type Readers, type Via, type WebFetchOptions, type WebLog, type WebPage, type WebRead, type WebUnread, FETCH_ORDER, PAGES_HELD, webFetch, webFetchText, } from "./fetch.js";
|
|
6
8
|
export { type Page, type Part, type Read, type Unread, askedUrl, clip, HTML_READ_CHARS, LINKS_CAP, LINKS_CHARS, MAX_CHARS_MIN, PAGE_CHARS, PAGE_MAX_BYTES, PAGE_READ_MAX_CHARS, partEnd, partProblem, TEXT_CAP, TITLE_CAP, URL_MAX, } from "./page.js";
|
|
9
|
+
export { WEB_FETCH_DESCRIPTION, WEB_FETCH_PARAMETERS, WEB_SEARCH_DESCRIPTION, WEB_SEARCH_PARAMETERS } from "./tool-specs.js";
|
|
7
10
|
export { fetchPage, readDirect } from "./providers/direct.js";
|
|
11
|
+
export { type ExaSettings, EXA_DEFAULTS } from "./providers/exa.js";
|
|
8
12
|
export { type ExaLimited, exaSearch } from "./providers/exa-keyless.js";
|
|
9
|
-
export { type
|
|
13
|
+
export { type FirecrawlSettings, FIRECRAWL_DEFAULTS } from "./providers/firecrawl.js";
|
|
14
|
+
export { type FirecrawlFailed, type FirecrawlOptions, firecrawlFetch, firecrawlScrape, firecrawlSearch } from "./providers/firecrawl-keyless.js";
|
|
10
15
|
export type { Words } from "./providers/mcp.js";
|
|
11
|
-
export { type Extract, PARALLEL_EXTRACT_URL, PARALLEL_SEARCH_URL, parallelExtract, parallelSearched } from "./providers/parallel.js";
|
|
16
|
+
export { type Extract, type ParallelSettings, PARALLEL_DEFAULTS, PARALLEL_EXTRACT_URL, PARALLEL_SEARCH_URL, parallelExtract, parallelReads, parallelSearched, } from "./providers/parallel.js";
|
|
12
17
|
export { type WrittenLink, linksOf, linksWritten, TRAILING, trimmed, wholeUrl } from "./text-urls.js";
|
|
18
|
+
export { type ChoiceQuestion, type Cost, type DecideContext, type DeciderOptions, type DecisionAsk, type DecisionAsker, type DecisionPrice, type NoulQuestion, type PageAnswers, type Tokens, type Verdict, type Verdicts, type Wall, type WebDecider, deciderFrom, GUARD_CHARS, holdOf, JEV_MODEL, type JevWeb, jevWebDecider, judgeRequest, pageGuardRequest, QUESTIONS_PER_REQUEST, REQUESTS_AT_ONCE, siftRequest, STATE_BUDGET_BYTES, WALLS, } from "./decide/decider.js";
|
|
19
|
+
export { type FetchAnswer, type FoundChunk, type FoundPage, FIND_CHUNKS_MAX, FOUND_CHUNKS, judgePart, LEFT_OUT } from "./decide/fetch.js";
|
|
20
|
+
export { type InjectionVerdict, type WallVerdict, INJECTION_THRESHOLD, WALL_THRESHOLD } from "./decide/page-guard.js";
|
|
21
|
+
export { PASSAGES_MAX, passagesOf } from "./decide/passages.js";
|
|
22
|
+
export { DECIDE_MS, decisionPay, type DecisionRecord, type Scored, type Sift, type StepOptions, THRESHOLD } from "./decide/record.js";
|
|
23
|
+
export { type DecidePolicy, LOW_RELEVANCE, siftSearch } from "./decide/search.js";
|
package/dist/index.js
CHANGED
|
@@ -2,15 +2,27 @@
|
|
|
2
2
|
// vendor SDK: the public-host guard, the HTML reader, the run's cap, a native search and a product's words come in
|
|
3
3
|
// through WebConfig (createWeb) or as arguments, as data or injected functions.
|
|
4
4
|
//
|
|
5
|
-
// The package's whole API, named one by one: createWeb and its types, the
|
|
6
|
-
// bounds and words AgentRun's server reads. Anything else in src is
|
|
7
|
-
|
|
8
|
-
export {
|
|
9
|
-
export {
|
|
10
|
-
export {
|
|
5
|
+
// The package's whole API, named one by one: createWeb and its types, the provider contract and registry, the classifier
|
|
6
|
+
// step, the pieces it is built from (README), and the bounds and words AgentRun's server reads. Anything else in src is
|
|
7
|
+
// internal and may change in any release.
|
|
8
|
+
export { createWeb, destinationsLine, destinationsWords, } from "./web.js";
|
|
9
|
+
export { defineProvider, holdingWords, readerOf, } from "./provider.js";
|
|
10
|
+
export { AHEAD, isProviderId, PROVIDERS, providerName, providerOf, SAID, serves, settingsProblemsOf, } from "./providers/registry.js";
|
|
11
|
+
export { EXA_CONTENT_USD, EXA_SEARCH_INCLUDED, EXA_SEARCH_RESULT_USD, EXA_SEARCH_TYPE_USD, EXA_SUMMARY_USD, exaFeeOf, FIRECRAWL_CREDIT_USD, FIRECRAWL_JSON_CREDITS, FIRECRAWL_SCRAPE_CREDITS, FIRECRAWL_SEARCH_CREDITS, firecrawlFeeOf, PARALLEL_EXTRACT_USD, PARALLEL_SEARCH_INCLUDED, PARALLEL_SEARCH_MODE_USD, PARALLEL_SEARCH_RESULT_USD, PARALLEL_SEARCH_USD, PaidRefused, parallelExtractFee, parallelFeeOf, parallelSearchFee, unmetered, } from "./fees.js";
|
|
12
|
+
export { ANSWER_MAX, breakers, CITE, fitSearch, OBJECTIVE_MAX, QUERIES_MAX, QUERY_MAX, RESULTS_DEFAULT, RESULTS_MAX, SEARCH_MAX_CHARS, SNIPPET_MAX, searchArgs, searchBreaker, searchInTurn, searchText, snippetOf, } from "./search.js";
|
|
13
|
+
export { FETCH_ORDER, PAGES_HELD, webFetch, webFetchText, } from "./fetch.js";
|
|
11
14
|
export { askedUrl, clip, HTML_READ_CHARS, LINKS_CAP, LINKS_CHARS, MAX_CHARS_MIN, PAGE_CHARS, PAGE_MAX_BYTES, PAGE_READ_MAX_CHARS, partEnd, partProblem, TEXT_CAP, TITLE_CAP, URL_MAX, } from "./page.js";
|
|
15
|
+
export { WEB_FETCH_DESCRIPTION, WEB_FETCH_PARAMETERS, WEB_SEARCH_DESCRIPTION, WEB_SEARCH_PARAMETERS } from "./tool-specs.js";
|
|
12
16
|
export { fetchPage, readDirect } from "./providers/direct.js";
|
|
17
|
+
export { EXA_DEFAULTS } from "./providers/exa.js";
|
|
13
18
|
export { exaSearch } from "./providers/exa-keyless.js";
|
|
19
|
+
export { FIRECRAWL_DEFAULTS } from "./providers/firecrawl.js";
|
|
14
20
|
export { firecrawlFetch, firecrawlScrape, firecrawlSearch } from "./providers/firecrawl-keyless.js";
|
|
15
|
-
export { PARALLEL_EXTRACT_URL, PARALLEL_SEARCH_URL, parallelExtract, parallelSearched } from "./providers/parallel.js";
|
|
21
|
+
export { PARALLEL_DEFAULTS, PARALLEL_EXTRACT_URL, PARALLEL_SEARCH_URL, parallelExtract, parallelReads, parallelSearched, } from "./providers/parallel.js";
|
|
16
22
|
export { linksOf, linksWritten, TRAILING, trimmed, wholeUrl } from "./text-urls.js";
|
|
23
|
+
export { deciderFrom, GUARD_CHARS, holdOf, JEV_MODEL, jevWebDecider, judgeRequest, pageGuardRequest, QUESTIONS_PER_REQUEST, REQUESTS_AT_ONCE, siftRequest, STATE_BUDGET_BYTES, WALLS, } from "./decide/decider.js";
|
|
24
|
+
export { FIND_CHUNKS_MAX, FOUND_CHUNKS, judgePart, LEFT_OUT } from "./decide/fetch.js";
|
|
25
|
+
export { INJECTION_THRESHOLD, WALL_THRESHOLD } from "./decide/page-guard.js";
|
|
26
|
+
export { PASSAGES_MAX, passagesOf } from "./decide/passages.js";
|
|
27
|
+
export { DECIDE_MS, decisionPay, THRESHOLD } from "./decide/record.js";
|
|
28
|
+
export { LOW_RELEVANCE, siftSearch } from "./decide/search.js";
|