@parcha/agentrun-web 0.1.0-beta.57 → 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/web.d.ts
CHANGED
|
@@ -1,25 +1,28 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type { WebDecider } from "./decide/decider.js";
|
|
2
|
+
import { type FetchAnswer } from "./decide/fetch.js";
|
|
3
|
+
import { type InjectionVerdict, type WallVerdict } from "./decide/page-guard.js";
|
|
4
|
+
import { type DecisionRecord, type Sift } from "./decide/record.js";
|
|
5
|
+
import { type DecidePolicy } from "./decide/search.js";
|
|
6
|
+
import { type WebLog } from "./fetch.js";
|
|
2
7
|
import { type Fee, type Pay } from "./fees.js";
|
|
3
8
|
import type { Guard, GuardOptions } from "./guard.js";
|
|
4
|
-
import { type
|
|
9
|
+
import { type Part } from "./page.js";
|
|
10
|
+
import { type NativeSearch, type WebClient } from "./provider.js";
|
|
5
11
|
import type { Words } from "./providers/mcp.js";
|
|
6
|
-
import { type
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export type NativeSearch = (query: string, n: number, signal?: AbortSignal) => Promise<Searched>;
|
|
11
|
-
/** What an answer took and cost. `out` is the answer as the model reads it; `cache` is "hit" when a fetch came from the
|
|
12
|
-
* run's pages. */
|
|
12
|
+
import { type FetchProvider, type KeyedProvider, type ProviderId, type ProviderSettings, type SearchProvider } from "./providers/registry.js";
|
|
13
|
+
import { type Breaker, type Holding, type Limit, type Search, type SearchBounds, type Try } from "./search.js";
|
|
14
|
+
/** What an answer took and cost. `asked`: a search's objective, a fetch's URL; `queries`, a search's keyword queries.
|
|
15
|
+
* `out` is the answer as the model reads it; `cache` is "hit" when a fetch came from the run's pages. */
|
|
13
16
|
export type Receipt = {
|
|
14
17
|
op: "search" | "fetch";
|
|
15
18
|
asked: string;
|
|
19
|
+
queries?: string[];
|
|
16
20
|
provider: ProviderId | null;
|
|
17
21
|
tries: Try[];
|
|
18
|
-
classifier: "off";
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
injection: "unjudged";
|
|
22
|
+
classifier: "off" | DecisionRecord;
|
|
23
|
+
/** The page guard's verdicts on a fetched page, when `pageGuard` is on; "unjudged" otherwise. */
|
|
24
|
+
wall: WallVerdict;
|
|
25
|
+
injection: InjectionVerdict;
|
|
23
26
|
out: {
|
|
24
27
|
bytes: number;
|
|
25
28
|
chars: number;
|
|
@@ -29,49 +32,125 @@ export type Receipt = {
|
|
|
29
32
|
lines: Fee[];
|
|
30
33
|
};
|
|
31
34
|
cache?: "hit";
|
|
35
|
+
/** With a classifier in the run: the text a web with none would have given the model for this call (a fetch's part
|
|
36
|
+
* as asked, without its objective), set beside what it read to see what the step saved. */
|
|
37
|
+
unsifted?: string;
|
|
32
38
|
};
|
|
33
|
-
/** `order
|
|
34
|
-
*
|
|
39
|
+
/** `order` names the providers each tool asks, in turn, by their registry ids (providers/registry.ts); `order.fetch`
|
|
40
|
+
* ends with "direct" where a page may be fetched directly. An id no provider has, or a provider for a tool it does not
|
|
41
|
+
* serve, throws here. No key for a keyed provider in an order (`keys`): it answers that the run holds none, unsent.
|
|
42
|
+
* `settings`: a provider's, by its id (ProviderSettings); one a provider does not take throws here, before anything is
|
|
43
|
+
* sent. No `guard`: nothing is fetched directly and no URL goes to a provider. No `pay`: unmetered. `endpoints` and
|
|
44
|
+
* `send`: where a provider's requests go, and through what, by its id. */
|
|
35
45
|
export type WebConfig = {
|
|
36
46
|
order: {
|
|
37
47
|
search: readonly SearchProvider[];
|
|
38
48
|
fetch: readonly FetchProvider[];
|
|
39
49
|
};
|
|
40
|
-
keys?:
|
|
41
|
-
|
|
42
|
-
};
|
|
50
|
+
keys?: Partial<Record<KeyedProvider, string>>;
|
|
51
|
+
settings?: ProviderSettings;
|
|
43
52
|
native?: NativeSearch;
|
|
44
53
|
guard?: Guard;
|
|
45
54
|
pay?: Pay;
|
|
46
|
-
endpoints?:
|
|
47
|
-
|
|
48
|
-
};
|
|
49
|
-
send?: Partial<Record<"parallel" | "firecrawl-keyless" | "exa-keyless", typeof fetch>>;
|
|
55
|
+
endpoints?: Partial<Record<ProviderId, string>>;
|
|
56
|
+
send?: Partial<Record<ProviderId, typeof fetch>>;
|
|
50
57
|
words?: Words;
|
|
51
58
|
now?: () => number;
|
|
52
59
|
bounds?: SearchBounds & {
|
|
53
60
|
checkMs?: number;
|
|
61
|
+
decideMs?: number;
|
|
54
62
|
};
|
|
55
63
|
log?: WebLog;
|
|
64
|
+
/** Holds known before the Web is made (Hold): each holds its breaker from the start, the Web's and a call's own alike;
|
|
65
|
+
* one already over holds nothing. */
|
|
66
|
+
holds?: readonly Hold[];
|
|
67
|
+
/** Given each limit a breaker reads that leaves it holding (Hold): the tool's, or its key's. */
|
|
68
|
+
onHold?: (hold: Hold) => void;
|
|
69
|
+
/** Sifts each search's results and judges each fetched part; none: nothing is sifted, as before the step. */
|
|
70
|
+
decider?: WebDecider;
|
|
71
|
+
/** How a search's results reach the model once the decider has scored them ("drop" by default); checked when the
|
|
72
|
+
* web is made. */
|
|
73
|
+
policy?: DecidePolicy;
|
|
74
|
+
/** The page guard: with a decider, each page a fetch reads is asked once a run whether it is a wall or holds
|
|
75
|
+
* instructions addressed to an AI reader. Off unless true; true without a decider throws. */
|
|
76
|
+
pageGuard?: boolean;
|
|
77
|
+
/** The agent calling the web tools, as far as the run knows it: told to a provider that takes it (Parallel's search). */
|
|
78
|
+
client?: WebClient;
|
|
56
79
|
};
|
|
57
|
-
/** `
|
|
58
|
-
*
|
|
80
|
+
/** `n`: how many results, where the search asked names none. `breakers`: the search breakers this call reads and trips
|
|
81
|
+
* (default the Web's own). `keyLimits`: where a quota of a provider's whole key goes (search.ts Failure.covers): the
|
|
82
|
+
* Web's key breakers, shared with web_fetch ("web", the default without `breakers`), or the call's own breakers
|
|
83
|
+
* ("call", the default with them). `task`: what the caller needs, which a decider judges against (a search without
|
|
84
|
+
* one is judged on its objective). `bounds`: this call's deadlines, each in place of the Web's (WebConfig.bounds)
|
|
85
|
+
* where given. `session`: this call's session, told to a provider that takes one in place of WebConfig.client's. */
|
|
59
86
|
export type SearchOptions = {
|
|
60
87
|
n?: number;
|
|
61
88
|
signal?: AbortSignal;
|
|
62
89
|
breakers?: (name: string) => Breaker;
|
|
90
|
+
keyLimits?: "web" | "call";
|
|
91
|
+
task?: string;
|
|
63
92
|
bounds?: SearchBounds;
|
|
93
|
+
session?: string;
|
|
94
|
+
};
|
|
95
|
+
/** `lookup` and `fetchImpl`: the resolver and transport this call's guard dials through. `objective`: what the caller
|
|
96
|
+
* wants from the page (the tool's objective); `lookingFor`, the same under its older name, read where no objective is
|
|
97
|
+
* given. With a decider, the answer is the chunks of the whole page most likely to hold it (findInPage), `offset` and
|
|
98
|
+
* `maxChars` aside; when the step fails, the part asked for, saying the objective was not applied; with no decider, the
|
|
99
|
+
* part asked for, as with none. Blank: none. `session`: this call's session, told to a provider that takes one in place
|
|
100
|
+
* of WebConfig.client's, as SearchOptions.session (a page the run already read is not read again). */
|
|
101
|
+
export type FetchOptions = Part & GuardOptions & {
|
|
102
|
+
task?: string;
|
|
103
|
+
objective?: string;
|
|
104
|
+
lookingFor?: string;
|
|
105
|
+
session?: string;
|
|
106
|
+
};
|
|
107
|
+
/** `sift`, only when a decider is given: what it did, for the model to read. */
|
|
108
|
+
/** A provider a Web's tool asks: its id, how it is reached (`keyed`, on the run's key; `unkeyed`, a keyed provider the
|
|
109
|
+
* run holds no key for, so never sent; `keyless`, a service's free use; `none`, neither: the model provider's own search
|
|
110
|
+
* or the guarded direct fetch), and what holds it now (the tool's breaker, else its key's), if anything. */
|
|
111
|
+
export type Destination = {
|
|
112
|
+
id: ProviderId;
|
|
113
|
+
access: "keyed" | "unkeyed" | "keyless" | "none";
|
|
114
|
+
held?: Holding;
|
|
64
115
|
};
|
|
65
|
-
/** `
|
|
66
|
-
|
|
116
|
+
/** A provider's breaker for `tool` ("key": its key's, across both tools) holding as `kind` until `until` (epoch ms;
|
|
117
|
+
* Infinity: for the Web's life). */
|
|
118
|
+
export type Hold = {
|
|
119
|
+
provider: ProviderId;
|
|
120
|
+
tool: "search" | "fetch" | "key";
|
|
121
|
+
kind: Limit["kind"];
|
|
122
|
+
until: number;
|
|
123
|
+
};
|
|
124
|
+
/** Each tool's providers, in the order it asks them. */
|
|
125
|
+
export type Destinations = {
|
|
126
|
+
search: Destination[];
|
|
127
|
+
fetch: Destination[];
|
|
128
|
+
};
|
|
129
|
+
/** What a search asks: `{objective, queries?, n?}`, or a query alone (a string, or the deprecated `{query}`), which is
|
|
130
|
+
* searched as the objective and the one query (searchArgs). */
|
|
131
|
+
export type SearchInput = string | {
|
|
132
|
+
objective?: string;
|
|
133
|
+
queries?: readonly string[];
|
|
134
|
+
query?: string;
|
|
135
|
+
n?: number;
|
|
136
|
+
};
|
|
137
|
+
/** `destinations`: where each tool goes now (Destinations), for a run's start line; never a key. */
|
|
67
138
|
export type Web = {
|
|
68
|
-
search(
|
|
69
|
-
answer: Search;
|
|
139
|
+
search(input: SearchInput, options?: SearchOptions): Promise<{
|
|
140
|
+
answer: Search & Sift;
|
|
70
141
|
receipt: Receipt;
|
|
71
142
|
}>;
|
|
72
143
|
fetch(url: unknown, options?: FetchOptions): Promise<{
|
|
73
|
-
answer:
|
|
144
|
+
answer: FetchAnswer;
|
|
74
145
|
receipt: Receipt;
|
|
75
146
|
}>;
|
|
147
|
+
destinations(): Destinations;
|
|
148
|
+
};
|
|
149
|
+
/** Each tool's destinations in words, for a log line's fields. */
|
|
150
|
+
export declare const destinationsWords: ({ search, fetch }: Destinations) => {
|
|
151
|
+
search: string;
|
|
152
|
+
fetch: string;
|
|
76
153
|
};
|
|
154
|
+
/** Both tools' destinations on one line: "search: …; fetch: …". */
|
|
155
|
+
export declare function destinationsLine(destinations: Destinations): string;
|
|
77
156
|
export declare function createWeb(config: WebConfig): Web;
|
package/dist/web.js
CHANGED
|
@@ -1,76 +1,177 @@
|
|
|
1
1
|
// One run's web search and fetch (createWeb). The providers asked are data (WebConfig.order); everything from outside
|
|
2
2
|
// comes in through the config. Every answer comes with a Receipt, which is never put in front of the model.
|
|
3
3
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
4
|
-
import {
|
|
4
|
+
import { decideFetch, judgePart } from "./decide/fetch.js";
|
|
5
|
+
import { guardPage } from "./decide/page-guard.js";
|
|
6
|
+
import { decisionPay, heldOnRefusal } from "./decide/record.js";
|
|
7
|
+
import { policyOf, siftSearch } from "./decide/search.js";
|
|
8
|
+
import { missesOf, webFetchText, webPart, webRead } from "./fetch.js";
|
|
5
9
|
import { unmetered } from "./fees.js";
|
|
6
10
|
import { askedUrl, partProblem } from "./page.js";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
+
import { holdingWords, readerOf } from "./provider.js";
|
|
12
|
+
import { providerOf, settingsProblemsOf } from "./providers/registry.js";
|
|
13
|
+
import { breakers, fitSearch, withKeyBreaker, searchArgs, searchInTurn, searchText } from "./search.js";
|
|
14
|
+
/** One tool's destinations in words: each id, how it is reached, and what holds it ("none" for an empty order). */
|
|
15
|
+
const toolWords = (order) => order.map(({ id, access, held }) => {
|
|
16
|
+
const notes = [...(access === "none" ? [] : [access === "unkeyed" ? "keyed, no key" : access]), ...(held ? [holdingWords(held)] : [])];
|
|
17
|
+
return notes.length ? `${id} (${notes.join(", ")})` : id;
|
|
18
|
+
}).join(" > ") || "none";
|
|
19
|
+
/** Each tool's destinations in words, for a log line's fields. */
|
|
20
|
+
export const destinationsWords = ({ search, fetch }) => ({ search: toolWords(search), fetch: toolWords(fetch) });
|
|
21
|
+
/** Both tools' destinations on one line: "search: …; fetch: …". */
|
|
22
|
+
export function destinationsLine(destinations) {
|
|
23
|
+
const { search, fetch } = destinationsWords(destinations);
|
|
24
|
+
return `search: ${search}; fetch: ${fetch}`;
|
|
25
|
+
}
|
|
11
26
|
/** The fees of the requests sent within one call. */
|
|
12
27
|
const feesOfCall = new AsyncLocalStorage();
|
|
13
28
|
const usd = (lines) => Math.round(lines.reduce((sum, fee) => sum + fee.usd, 0) * 1e6) / 1e6;
|
|
14
29
|
const outOf = (text) => ({ bytes: Buffer.byteLength(text), chars: text.length });
|
|
15
|
-
const
|
|
16
|
-
/**
|
|
17
|
-
|
|
30
|
+
const unjudged = { wall: { verdict: "unjudged" }, injection: "unjudged" };
|
|
31
|
+
/** `shared`, a guard check another call started, or as soon as `signal` stops this call, the page left unjudged as a
|
|
32
|
+
* stopped check leaves it (code "aborted"). The shared check is not stopped: another call may still need it. */
|
|
33
|
+
function guardUntilStopped(shared, signal) {
|
|
34
|
+
if (!signal)
|
|
35
|
+
return shared;
|
|
36
|
+
const stopped = () => ({ wall: { verdict: "unjudged", error: { code: "aborted", message: String(signal.reason?.message ?? signal.reason).slice(0, 500) } }, injection: "unjudged" });
|
|
37
|
+
if (signal.aborted)
|
|
38
|
+
return Promise.resolve(stopped());
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
const onStop = () => resolve(stopped());
|
|
41
|
+
signal.addEventListener("abort", onStop, { once: true });
|
|
42
|
+
shared.then((guard) => (signal.removeEventListener("abort", onStop), resolve(guard)), (error) => (signal.removeEventListener("abort", onStop), reject(error)));
|
|
43
|
+
});
|
|
44
|
+
}
|
|
18
45
|
export function createWeb(config) {
|
|
46
|
+
const problems = settingsProblemsOf(config.settings);
|
|
47
|
+
if (problems.length)
|
|
48
|
+
throw new Error(`web settings: ${problems.join("; ")}`);
|
|
49
|
+
for (const op of ["search", "fetch"])
|
|
50
|
+
for (const id of config.order[op])
|
|
51
|
+
if (!providerOf(id).ops.includes(op))
|
|
52
|
+
throw new Error(`web: ${id} gives no ${op}`);
|
|
19
53
|
const now = config.now ?? Date.now;
|
|
54
|
+
const policy = policyOf(config.policy);
|
|
55
|
+
if (config.pageGuard !== undefined && typeof config.pageGuard !== "boolean")
|
|
56
|
+
throw new TypeError(`pageGuard must be true or false (it was ${JSON.stringify(config.pageGuard)})`);
|
|
57
|
+
if (config.pageGuard && !config.decider)
|
|
58
|
+
throw new TypeError("pageGuard needs a decider: the page guard asks the run's decider about each page");
|
|
59
|
+
// The decider as this Web asks it: held for the Web's life once it refuses its key or its account.
|
|
60
|
+
const held = config.decider ? heldOnRefusal(config.decider) : undefined;
|
|
61
|
+
/** The page guard's verdict on each read the run's pages hold, kept with the read: it goes when the page is evicted,
|
|
62
|
+
* and a page read again is asked about again, as is one whose guard failed. */
|
|
63
|
+
const pageVerdicts = new WeakMap();
|
|
64
|
+
const guardOf = (decider, url, read, step) => {
|
|
65
|
+
const known = pageVerdicts.get(read);
|
|
66
|
+
// A later caller waits on the check another started only until its own signal stops it; the check goes on.
|
|
67
|
+
if (known)
|
|
68
|
+
return guardUntilStopped(known, step.signal);
|
|
69
|
+
const asked = guardPage(decider, url, read, step);
|
|
70
|
+
pageVerdicts.set(read, asked);
|
|
71
|
+
void asked.then((verdict) => verdict.wall.error && pageVerdicts.delete(read));
|
|
72
|
+
return asked;
|
|
73
|
+
};
|
|
74
|
+
/** A receipt's `unsifted`, only in a run with a classifier. */
|
|
75
|
+
const unsifted = (text) => (config.decider ? { unsifted: text() } : {});
|
|
20
76
|
const runBreakers = breakers(now);
|
|
77
|
+
const fetchBreakers = breakers(now);
|
|
78
|
+
// Each provider's own across both tools: a quota of the whole key holds web_search and web_fetch alike.
|
|
79
|
+
const keyBreakers = breakers(now);
|
|
21
80
|
const pages = new Map();
|
|
81
|
+
// Each breaker takes the holds known at the start once, at its first use: the Web's, and each a call brings.
|
|
82
|
+
const seeded = new WeakSet();
|
|
83
|
+
const watched = (tool, provider, breaker) => {
|
|
84
|
+
if (!seeded.has(breaker)) {
|
|
85
|
+
seeded.add(breaker);
|
|
86
|
+
for (const hold of config.holds ?? []) {
|
|
87
|
+
const at = now();
|
|
88
|
+
if (hold.provider !== provider || hold.tool !== tool || !(hold.until > at))
|
|
89
|
+
continue;
|
|
90
|
+
const limit = hold.until === Number.POSITIVE_INFINITY ? { kind: hold.kind } : { kind: hold.kind, retryAfter: (hold.until - at) / 1_000 };
|
|
91
|
+
breaker.read({ answer: { error: "held" }, limit });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (!config.onHold)
|
|
95
|
+
return breaker;
|
|
96
|
+
const onHold = config.onHold;
|
|
97
|
+
return {
|
|
98
|
+
...breaker,
|
|
99
|
+
read(searched) {
|
|
100
|
+
const answer = breaker.read(searched);
|
|
101
|
+
const holding = searched.limit ? breaker.holding() : undefined;
|
|
102
|
+
if (holding)
|
|
103
|
+
onHold({ provider, tool, ...holding });
|
|
104
|
+
return answer;
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
const toolBreaker = (tool, id, own, key) => withKeyBreaker(watched(tool, id, own), watched("key", id, key));
|
|
22
109
|
// A fee goes on the call's receipt once its request is sent, whatever the cap then books.
|
|
23
110
|
const pay = (fee, send) => (config.pay ?? unmetered)(fee, () => (feesOfCall.getStore()?.push(fee), send()));
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
return "error" in found ? failedWith(found.error, "limit" in found ? found.limit : undefined) : { answer: found };
|
|
49
|
-
} };
|
|
111
|
+
// Each provider an order names is made once per Web, at its first use, from the run's parts for it: one client a
|
|
112
|
+
// provider, so a key refused by one tool is refused by both.
|
|
113
|
+
const runs = new Map();
|
|
114
|
+
const runOf = (id) => {
|
|
115
|
+
let run = runs.get(id);
|
|
116
|
+
if (!run) {
|
|
117
|
+
const key = config.keys?.[id];
|
|
118
|
+
const endpoint = config.endpoints?.[id];
|
|
119
|
+
const send = config.send?.[id];
|
|
120
|
+
const settings = config.settings?.[id];
|
|
121
|
+
run = providerOf(id).make({
|
|
122
|
+
...(key === undefined ? {} : { key }),
|
|
123
|
+
...(endpoint ? { endpoint } : {}),
|
|
124
|
+
...(send ? { send } : {}),
|
|
125
|
+
...(settings === undefined ? {} : { settings }),
|
|
126
|
+
...(config.guard ? { guard: config.guard } : {}),
|
|
127
|
+
...(config.words ? { words: config.words } : {}),
|
|
128
|
+
...(config.native ? { native: config.native } : {}),
|
|
129
|
+
...(config.bounds ? { bounds: config.bounds } : {}),
|
|
130
|
+
...(config.client ? { client: config.client } : {}),
|
|
131
|
+
pay,
|
|
132
|
+
now,
|
|
133
|
+
});
|
|
134
|
+
runs.set(id, run);
|
|
50
135
|
}
|
|
51
|
-
return
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
136
|
+
return run;
|
|
137
|
+
};
|
|
138
|
+
let readers;
|
|
139
|
+
// A provider that ends the order is made by webRead for each read, never here.
|
|
140
|
+
const readersOf = () => (readers ??= Object.fromEntries(config.order.fetch.filter((id) => !providerOf(id).fetch?.ends).map((id) => [id, readerOf(runOf(id), toolBreaker("fetch", id, fetchBreakers(id), keyBreakers(id)))])));
|
|
141
|
+
const destinationOf = (id, tool, own) => {
|
|
142
|
+
const provider = providerOf(id);
|
|
143
|
+
const keyed = config.keys?.[id] !== undefined;
|
|
144
|
+
const access = provider.key ? (keyed ? "keyed" : "unkeyed") : provider.keyless ? "keyless" : "none";
|
|
145
|
+
const held = toolBreaker(tool, id, own(id), keyBreakers(id)).holding();
|
|
146
|
+
return { id, access, ...(held ? { held } : {}) };
|
|
55
147
|
};
|
|
56
148
|
return {
|
|
57
|
-
|
|
149
|
+
destinations: () => ({ search: config.order.search.map((id) => destinationOf(id, "search", runBreakers)), fetch: config.order.fetch.map((id) => destinationOf(id, "fetch", fetchBreakers)) }),
|
|
150
|
+
async search(input, { n, signal, breakers: given, keyLimits = given ? "call" : "web", task, bounds, session } = {}) {
|
|
151
|
+
const ownOf = given ?? runBreakers;
|
|
152
|
+
const breakerOf = (name) => toolBreaker("search", name, ownOf(name), keyLimits === "web" ? keyBreakers(name) : ownOf(`${name} key`));
|
|
58
153
|
const lines = [];
|
|
59
154
|
const tries = [];
|
|
60
|
-
const
|
|
155
|
+
const asked = typeof input === "string" ? { query: input } : { ...input };
|
|
156
|
+
const ask = searchArgs({ ...asked, n: asked.n ?? n });
|
|
61
157
|
const order = config.order.search;
|
|
62
158
|
const searchMs = bounds?.searchMs ?? config.bounds?.searchMs;
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const found = !order.includes("parallel") && !searchArgs(args).query ? { error: "web_search needs a query" }
|
|
159
|
+
// No provider is asked an empty objective.
|
|
160
|
+
const found = !ask.objective ? { error: "web_search needs an objective" }
|
|
66
161
|
: !order.length ? { error: "web_search has no provider to ask" }
|
|
67
|
-
: await feesOfCall.run(lines, () => searchInTurn(order.map((
|
|
68
|
-
|
|
69
|
-
const
|
|
162
|
+
: await feesOfCall.run(lines, () => searchInTurn(order.map((id) => ({ name: id, ask: (deadline) => runOf(id).search(ask, deadline, signal, bounds, session ? { session } : undefined) })), breakerOf, signal, searchMs, tries));
|
|
163
|
+
// Sifted before it is fitted, so the fit sees only the kept results; fitSearch keeps every other field.
|
|
164
|
+
const sifted = held ? await siftSearch(held, found, { task, signal, ms: config.bounds?.decideMs, pay: decisionPay(config.pay ?? unmetered, lines), policy }) : { answer: found, classifier: "off" };
|
|
165
|
+
const answer = fitSearch(sifted.answer);
|
|
166
|
+
const { classifier } = sifted;
|
|
167
|
+
const said = [asked.objective, asked.query].find((one) => typeof one === "string") ?? "";
|
|
168
|
+
// An empty search's receipt keeps the words it was given.
|
|
169
|
+
const receipt = { op: "search", asked: ask.objective || said, queries: ask.queries, provider: "error" in answer ? null : answer.provider, tries, classifier, ...unjudged, out: outOf(searchText(answer)), cost: { usd: usd(lines), lines }, ...unsifted(() => searchText(fitSearch(found))) };
|
|
70
170
|
return { answer, receipt };
|
|
71
171
|
},
|
|
72
|
-
// Only the call's part, resolver, transport and
|
|
73
|
-
|
|
172
|
+
// Only the call's part, resolver, transport, signal, task, objective and session are read: the guard, its bound, the order
|
|
173
|
+
// and the readers stay the Web's.
|
|
174
|
+
async fetch(url, { offset, maxChars, task, objective, lookingFor, lookup, fetchImpl, signal, session } = {}) {
|
|
74
175
|
const asked = typeof url === "string" ? url : "";
|
|
75
176
|
const lines = [];
|
|
76
177
|
const tries = [];
|
|
@@ -78,33 +179,34 @@ export function createWeb(config) {
|
|
|
78
179
|
const problem = partProblem({ offset, maxChars });
|
|
79
180
|
if (problem) {
|
|
80
181
|
const refused = { ok: false, url: askedUrl(asked), status: 0, title: "", text: "", links: [], error: problem };
|
|
81
|
-
|
|
182
|
+
const classifier = held ? (await judgePart(held, asked, refused, {})).classifier : "off";
|
|
183
|
+
return { answer: refused, receipt: { op: "fetch", asked, provider: null, tries, classifier, ...unjudged, out: outOf(webFetchText(refused)), cost: { usd: 0, lines }, ...unsifted(() => webFetchText(refused)) } };
|
|
82
184
|
}
|
|
83
185
|
const cached = typeof url === "string" && pages.has(url);
|
|
84
|
-
const
|
|
85
|
-
const extract = order.includes("parallel") ? parallelOf() : null;
|
|
86
|
-
const page = await feesOfCall.run(lines, () => webFetch(url, {
|
|
186
|
+
const whole = await feesOfCall.run(lines, () => webRead(url, {
|
|
87
187
|
...(lookup ? { lookup } : {}),
|
|
88
188
|
...(fetchImpl ? { fetchImpl } : {}),
|
|
89
189
|
...(signal ? { signal } : {}),
|
|
90
|
-
|
|
91
|
-
|
|
190
|
+
...(session ? { client: { session } } : {}),
|
|
191
|
+
readers: readersOf(),
|
|
92
192
|
pages,
|
|
93
|
-
order,
|
|
193
|
+
order: config.order.fetch,
|
|
94
194
|
...(config.guard ? { guard: config.guard } : {}),
|
|
95
195
|
...(config.bounds?.checkMs === undefined ? {} : { checkMs: config.bounds.checkMs }),
|
|
96
|
-
...(offset === undefined ? {} : { offset }),
|
|
97
|
-
...(maxChars === undefined ? {} : { maxChars }),
|
|
98
196
|
...(config.log ? { log: config.log } : {}),
|
|
99
|
-
|
|
100
|
-
tried: (one) => void tries.push(one),
|
|
101
|
-
...(order.includes("parallel") && !extract ? { noParallel: "not tried: this run holds no Parallel key" } : {}),
|
|
197
|
+
tries,
|
|
102
198
|
}));
|
|
103
|
-
const
|
|
104
|
-
const provider =
|
|
199
|
+
const part = webPart(whole, { ...(offset === undefined ? {} : { offset }), ...(maxChars === undefined ? {} : { maxChars }) });
|
|
200
|
+
const provider = part.ok && part.via !== "stopped" && part.via !== "none" ? part.via : null;
|
|
201
|
+
const decider = held;
|
|
202
|
+
const guard = config.pageGuard && decider && whole.ok ? (options) => guardOf(decider, asked, whole, options) : undefined;
|
|
203
|
+
const decided = await decideFetch(decider, asked, whole, part, { task, objective: typeof objective === "string" && objective.trim() ? objective : lookingFor, signal, ms: config.bounds?.decideMs, pay: config.pay ?? unmetered, lines, guard });
|
|
204
|
+
// What the objective found names each provider asked ahead that gave no page, as the part does.
|
|
205
|
+
const answer = "found" in decided.answer ? { ...decided.answer, ...missesOf(whole) } : decided.answer;
|
|
206
|
+
const { classifier, guard: verdicts } = decided;
|
|
105
207
|
// Copies: a read this call stopped waiting for goes on, and its later turns and fees are not on this receipt.
|
|
106
208
|
const spent = [...lines];
|
|
107
|
-
const receipt = { op: "fetch", asked, provider, tries: [...tries], ...
|
|
209
|
+
const receipt = { op: "fetch", asked, provider, tries: [...tries], classifier, ...(verdicts ? { wall: verdicts.wall, injection: verdicts.injection } : unjudged), out: outOf(webFetchText(answer)), cost: { usd: usd(spent), lines: spent }, ...(cached ? { cache: "hit" } : {}), ...unsifted(() => webFetchText(part)) };
|
|
108
210
|
return { answer, receipt };
|
|
109
211
|
},
|
|
110
212
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcha/agentrun-web",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.59",
|
|
4
4
|
"description": "Web search and web fetch for agents: providers asked in an order given as data, a breaker per provider, a per-run page cache read in parts, and a direct fetch through an injected public-host guard.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agentrun",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"undici": "^8.12.2"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@parcha/pi-browser": "0.1.0-beta.
|
|
49
|
+
"@parcha/pi-browser": "0.1.0-beta.59"
|
|
50
50
|
},
|
|
51
51
|
"repository": {
|
|
52
52
|
"type": "git",
|