@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
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// The run's own model provider's hosted search, injected (WebConfig.native): web_search only, on the caller's signal and
|
|
2
|
+
// its own bounds. It is asked a search's first query, and its answer carries the search's objective and queries.
|
|
3
|
+
import { defineProvider } from "../provider.js";
|
|
4
|
+
export const native = defineProvider({ id: "native", label: "the model provider's own search", ops: ["search"] }, ({ native: search }) => ({
|
|
5
|
+
async search({ objective, queries, n }, _deadline, signal) {
|
|
6
|
+
if (!search)
|
|
7
|
+
return { answer: { error: "web_search: no native search was given" } };
|
|
8
|
+
const searched = await search(queries[0] ?? objective, n, signal);
|
|
9
|
+
return "error" in searched.answer ? searched : { ...searched, answer: { ...searched.answer, objective, queries } };
|
|
10
|
+
},
|
|
11
|
+
}));
|
|
@@ -1,20 +1,65 @@
|
|
|
1
|
-
import { type Pay } from "../fees.js";
|
|
1
|
+
import { type ParallelSearchMode, type Pay } from "../fees.js";
|
|
2
2
|
import { type Read } from "../page.js";
|
|
3
|
-
import { type Searched } from "../search.js";
|
|
3
|
+
import { type Failed, type SearchAsk, type Searched } from "../search.js";
|
|
4
|
+
import { type ProviderRun, type WebClient } from "../provider.js";
|
|
4
5
|
export declare const PARALLEL_SEARCH_URL = "https://api.parallel.ai/v1/search";
|
|
5
6
|
export declare const PARALLEL_EXTRACT_URL = "https://api.parallel.ai/v1/extract";
|
|
6
|
-
/** Parallel
|
|
7
|
-
*
|
|
7
|
+
/** How Parallel searches and reads, each setting optional (PARALLEL_DEFAULTS); with none, each request is the one the web
|
|
8
|
+
* tools have always sent:
|
|
9
|
+
* - `mode`: the search's depth and price (fees.ts PARALLEL_SEARCH_MODE_USD), default "fast";
|
|
10
|
+
* - `maxCharsTotal`: the most characters of excerpts across a search's results; unset, Parallel's own;
|
|
11
|
+
* - `includeDomains`, `excludeDomains` (at most 200 together): domains, domain/path prefixes or extensions (".gov") a
|
|
12
|
+
* search keeps to or leaves out; `afterDate`: the earliest publish date a search keeps, YYYY-MM-DD;
|
|
13
|
+
* - `maxAgeSeconds`: the oldest cached page a search or extract may answer from before Parallel fetches it live, 600 or
|
|
14
|
+
* more; unset, Parallel's own (its cache, no live fetch). */
|
|
15
|
+
export type ParallelSettings = {
|
|
16
|
+
mode?: ParallelSearchMode;
|
|
17
|
+
maxCharsTotal?: number;
|
|
18
|
+
includeDomains?: string[];
|
|
19
|
+
excludeDomains?: string[];
|
|
20
|
+
afterDate?: string;
|
|
21
|
+
maxAgeSeconds?: number;
|
|
22
|
+
};
|
|
23
|
+
export declare const PARALLEL_DEFAULTS: Readonly<{
|
|
24
|
+
readonly mode: "fast";
|
|
25
|
+
}>;
|
|
26
|
+
export declare function parallelSettingsProblems(settings: unknown): string[];
|
|
27
|
+
/** Parallel for one run, on one key: `extract`, its extract of a page (one billed request), the page it read or, for
|
|
28
|
+
* any failure, why it read none, typed; `read`, the same with why in Parallel's words alone; `post`, one request to another of its endpoints (web_search's, parallelSearched). Neither
|
|
8
29
|
* retries, and each stops at 60 s. `live` is false once a 401 or 403 to either said the key is not served. */
|
|
9
30
|
export type Extract = {
|
|
10
31
|
live(): boolean;
|
|
11
|
-
|
|
32
|
+
extract?(url: string, signal?: AbortSignal, client?: WebClient): Promise<Read | Failed>;
|
|
33
|
+
read(url: string, signal?: AbortSignal, client?: WebClient): Promise<Read | string>;
|
|
12
34
|
post(url: string, body: unknown, signal?: AbortSignal): Promise<Response>;
|
|
13
35
|
};
|
|
14
36
|
/** Parallel's extract on `apiKey`: one URL a request, each a billed extract, its full content to the most of a page
|
|
15
37
|
* web_fetch reads as the page's text (markdown), its links read from it, status 200 (Parallel answers a page it could
|
|
16
|
-
* not read in `errors`, with no content). */
|
|
17
|
-
export declare function parallelExtract(apiKey: string, send?: typeof fetch, pay?: Pay): Extract;
|
|
18
|
-
/**
|
|
19
|
-
*
|
|
20
|
-
|
|
38
|
+
* not read in `errors`, with no content), told the read's client (clientFields). */
|
|
39
|
+
export declare function parallelExtract(apiKey: string, send?: typeof fetch, pay?: Pay, settings?: ParallelSettings): Extract;
|
|
40
|
+
/** The /v1/search request for `ask` (searchArgs), under `settings`, as Parallel's guidance for agents asks: the
|
|
41
|
+
* objective, a goal in plain words, as its objective; the keyword queries as search_queries; and the model and
|
|
42
|
+
* session of `client` (cut at SESSION_MAX) where the run knows them. Excerpts are Parallel's own length
|
|
43
|
+
* (max_chars_total where set). */
|
|
44
|
+
export declare function parallelSearchBody({ objective, queries, n }: SearchAsk, settings?: ParallelSettings, client?: WebClient): Record<string, unknown>;
|
|
45
|
+
/** `{objective, queries?, n?}` (searchArgs) searched through `parallel` (null: the run holds no key) under `settings`,
|
|
46
|
+
* told `client`, and the limit Parallel answered for the run's key, if any. */
|
|
47
|
+
export declare function parallelSearched(args: Record<string, unknown>, parallel: Extract | null, signal?: AbortSignal, settings?: ParallelSettings, client?: WebClient): Promise<Searched>;
|
|
48
|
+
/** web_fetch's reads through `client` (null: the run holds no key), told `told` (the Web's client, each field in place
|
|
49
|
+
* of which the call's own goes where given, as a search's): a failure in Parallel's words as ours, and why a read is not
|
|
50
|
+
* asked now (no key, or the key refused earlier in the run). */
|
|
51
|
+
export declare function parallelReads(client: Extract | null, told?: WebClient): Required<Pick<ProviderRun, "read" | "unready">>;
|
|
52
|
+
/** Parallel on the run's key, both tools on one client (a key refused by either is refused by both). Its search keeps
|
|
53
|
+
* the caller's signal and its own bound; a fetch it gives no page is said on the page. */
|
|
54
|
+
export declare const parallel: {
|
|
55
|
+
readonly id: "parallel";
|
|
56
|
+
readonly label: "Parallel";
|
|
57
|
+
readonly key: {
|
|
58
|
+
readonly env: "PARALLEL_API_KEY";
|
|
59
|
+
};
|
|
60
|
+
readonly ops: readonly ["search", "fetch"];
|
|
61
|
+
readonly fetch: {
|
|
62
|
+
readonly miss: "said";
|
|
63
|
+
};
|
|
64
|
+
readonly settings: import("../provider.js").SettingsSpec<ParallelSettings>;
|
|
65
|
+
} & Pick<import("../provider.js").WebProvider<string>, "make">;
|
|
@@ -1,16 +1,44 @@
|
|
|
1
1
|
// Parallel on the run's key: Extract for web_fetch, Search for web_search. Every request is paid through the injected
|
|
2
2
|
// Pay at its endpoint's price, and a 401 or 403 stops both for the client's life. Nothing is retried; each ends at 60 s.
|
|
3
|
-
import { PaidRefused, parallelFeeOf, unmetered } from "../fees.js";
|
|
3
|
+
import { PARALLEL_SEARCH_MODE_USD, PaidRefused, parallelFeeOf, unmetered } from "../fees.js";
|
|
4
4
|
import { clip, markdownRead, PAGE_READ_MAX_CHARS, TITLE_CAP, withoutUserinfo } from "../page.js";
|
|
5
|
-
import {
|
|
5
|
+
import { failed, failure, keyWide, searchArgs, searchFailed, snippetOf } from "../search.js";
|
|
6
|
+
import { defineProvider, settingsOf } from "../provider.js";
|
|
7
|
+
import { domains, matching, oneOf, settingsProblems, wholeIn } from "./keyed.js";
|
|
6
8
|
export const PARALLEL_SEARCH_URL = "https://api.parallel.ai/v1/search";
|
|
7
9
|
export const PARALLEL_EXTRACT_URL = "https://api.parallel.ai/v1/extract";
|
|
8
10
|
const PARALLEL_TIMEOUT_MS = 60_000;
|
|
11
|
+
/** Parallel's cap on a search's session_id, in characters: a longer session is sent as its first SESSION_MAX. */
|
|
12
|
+
const SESSION_MAX = 1_000;
|
|
13
|
+
export const PARALLEL_DEFAULTS = Object.freeze({ mode: "fast" });
|
|
14
|
+
const PARALLEL_CHECKS = {
|
|
15
|
+
mode: oneOf(Object.keys(PARALLEL_SEARCH_MODE_USD)),
|
|
16
|
+
maxCharsTotal: wholeIn(1, 10_000_000),
|
|
17
|
+
includeDomains: domains(200),
|
|
18
|
+
excludeDomains: domains(200),
|
|
19
|
+
afterDate: matching(/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/, "a date, YYYY-MM-DD"),
|
|
20
|
+
maxAgeSeconds: wholeIn(600, 315_360_000),
|
|
21
|
+
};
|
|
22
|
+
export function parallelSettingsProblems(settings) {
|
|
23
|
+
const problems = settingsProblems("Parallel", settings, PARALLEL_CHECKS);
|
|
24
|
+
const asked = settings;
|
|
25
|
+
if (!problems.length && (asked.includeDomains?.length ?? 0) + (asked.excludeDomains?.length ?? 0) > 200)
|
|
26
|
+
problems.push("Parallel takes at most 200 includeDomains and excludeDomains together");
|
|
27
|
+
return problems;
|
|
28
|
+
}
|
|
29
|
+
/** The fetch policy `settings` ask, if any. */
|
|
30
|
+
const fetchPolicy = (settings) => (settings.maxAgeSeconds === undefined ? {} : { fetch_policy: { max_age_seconds: settings.maxAgeSeconds } });
|
|
9
31
|
const REFUSED = "Parallel refused this run's key (401 or 403): it is not asked again in this run";
|
|
32
|
+
/** What Parallel is told of `client` on a search or an extract, as its guidance asks (one session_id for a task's Search
|
|
33
|
+
* and Extract): its model as client_model, its session as session_id cut at SESSION_MAX, each only where known. */
|
|
34
|
+
const clientFields = (client) => ({
|
|
35
|
+
...(client.model ? { client_model: client.model } : {}),
|
|
36
|
+
...(client.session ? { session_id: client.session.slice(0, SESSION_MAX) } : {}),
|
|
37
|
+
});
|
|
10
38
|
/** Parallel's extract on `apiKey`: one URL a request, each a billed extract, its full content to the most of a page
|
|
11
39
|
* web_fetch reads as the page's text (markdown), its links read from it, status 200 (Parallel answers a page it could
|
|
12
|
-
* not read in `errors`, with no content). */
|
|
13
|
-
export function parallelExtract(apiKey, send = fetch, pay = unmetered) {
|
|
40
|
+
* not read in `errors`, with no content), told the read's client (clientFields). */
|
|
41
|
+
export function parallelExtract(apiKey, send = fetch, pay = unmetered, settings = {}) {
|
|
14
42
|
let refused = false;
|
|
15
43
|
// Each request is a paid one, sent through `pay` (fees.ts; serve's paid-calls.ts holds it to the run's cap and books it
|
|
16
44
|
// as the call's cost at its endpoint's price). One the cap refuses is not sent: it throws its refusal (PaidRefused).
|
|
@@ -20,6 +48,8 @@ export function parallelExtract(apiKey, send = fetch, pay = unmetered) {
|
|
|
20
48
|
method: "POST",
|
|
21
49
|
headers: { "x-api-key": apiKey, "content-type": "application/json" },
|
|
22
50
|
body: JSON.stringify(body),
|
|
51
|
+
// It carries the key: a redirect is an error, never followed to an address the key was not meant for.
|
|
52
|
+
redirect: "error",
|
|
23
53
|
signal: AbortSignal.any([AbortSignal.timeout(PARALLEL_TIMEOUT_MS), ...(signal ? [signal] : [])]),
|
|
24
54
|
});
|
|
25
55
|
if (response.status === 401 || response.status === 403)
|
|
@@ -31,31 +61,41 @@ export function parallelExtract(apiKey, send = fetch, pay = unmetered) {
|
|
|
31
61
|
throw new PaidRefused(`Parallel's ${URL.parse(url)?.pathname ?? url} was not sent: it has no price here`);
|
|
32
62
|
return pay(fee, sent);
|
|
33
63
|
};
|
|
64
|
+
const extract = async (url, signal, client = {}) => {
|
|
65
|
+
try {
|
|
66
|
+
// The most of a page a read reaches and one character more, so a page Parallel cut there shows as going on.
|
|
67
|
+
const asked = PAGE_READ_MAX_CHARS + 1;
|
|
68
|
+
const response = await post(PARALLEL_EXTRACT_URL, { urls: [url], advanced_settings: { full_content: { max_chars_per_result: asked }, ...fetchPolicy(settings) }, ...clientFields(client) }, signal);
|
|
69
|
+
if (!response.ok) {
|
|
70
|
+
await response.body?.cancel().catch(() => undefined);
|
|
71
|
+
if (response.status === 401 || response.status === 403)
|
|
72
|
+
return failed("refused", "KEY_REFUSED", `refused the key (${response.status})`);
|
|
73
|
+
// The key's credit spent holds both tools; a rate limit on an extract holds nothing.
|
|
74
|
+
if (response.status === 402)
|
|
75
|
+
return keyWide(failed("quota", "HTTP_402", "answered 402", { kind: "quota" }));
|
|
76
|
+
return failed(response.status === 429 ? "rate" : response.status >= 500 ? "infra" : "error", `HTTP_${response.status}`, `answered ${response.status}`);
|
|
77
|
+
}
|
|
78
|
+
const { results, errors } = (await response.json());
|
|
79
|
+
const row = results?.find((r) => typeof r?.url === "string" && typeof r.full_content === "string" && r.full_content.trim() !== "");
|
|
80
|
+
if (!row)
|
|
81
|
+
return errors?.length ? failed("error", "URL_IN_ERRORS", "listed the URL in errors") : failed("not_text", "NO_TEXT", "answered no content");
|
|
82
|
+
const text = row.full_content;
|
|
83
|
+
return markdownRead(row.url, typeof row.title === "string" ? row.title : "", text, text.length < asked);
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
if (error instanceof PaidRefused)
|
|
87
|
+
return failed("refused", "CAP_REFUSED", error.message);
|
|
88
|
+
const name = error?.name;
|
|
89
|
+
return name === "TimeoutError" ? failed("infra", "TIMEOUT", "timed out") : name === "AbortError" ? failed("infra", "STOPPED", "was stopped") : failed("infra", "CONNECT", `failed: ${withoutUserinfo(String(error?.message ?? error)).slice(0, 200)}`);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
34
92
|
return {
|
|
35
93
|
live: () => !refused,
|
|
36
94
|
post,
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const response = await post(PARALLEL_EXTRACT_URL, { urls: [url], advanced_settings: { full_content: { max_chars_per_result: asked } } }, signal);
|
|
42
|
-
if (!response.ok) {
|
|
43
|
-
await response.body?.cancel().catch(() => undefined);
|
|
44
|
-
return response.status === 401 || response.status === 403 ? `refused the key (${response.status})` : `answered ${response.status}`;
|
|
45
|
-
}
|
|
46
|
-
const { results, errors } = (await response.json());
|
|
47
|
-
const row = results?.find((r) => typeof r?.url === "string" && typeof r.full_content === "string" && r.full_content.trim() !== "");
|
|
48
|
-
if (!row)
|
|
49
|
-
return errors?.length ? "listed the URL in errors" : "answered no content";
|
|
50
|
-
const text = row.full_content;
|
|
51
|
-
return markdownRead(row.url, typeof row.title === "string" ? row.title : "", text, text.length < asked);
|
|
52
|
-
}
|
|
53
|
-
catch (error) {
|
|
54
|
-
if (error instanceof PaidRefused)
|
|
55
|
-
return error.message;
|
|
56
|
-
const name = error?.name;
|
|
57
|
-
return name === "TimeoutError" ? "timed out" : name === "AbortError" ? "was stopped" : `failed: ${withoutUserinfo(String(error?.message ?? error)).slice(0, 200)}`;
|
|
58
|
-
}
|
|
95
|
+
extract,
|
|
96
|
+
read: async (url, signal, client) => {
|
|
97
|
+
const read = await extract(url, signal, client);
|
|
98
|
+
return "ok" in read ? read : read.error;
|
|
59
99
|
},
|
|
60
100
|
};
|
|
61
101
|
}
|
|
@@ -67,23 +107,46 @@ const parallelLimit = (response) => {
|
|
|
67
107
|
const retryAfter = response.headers.get("retry-after");
|
|
68
108
|
return response.status === 429 ? { kind: "rate", ...(retryAfter ? { retryAfter } : {}) } : undefined;
|
|
69
109
|
};
|
|
70
|
-
/**
|
|
71
|
-
*
|
|
72
|
-
|
|
110
|
+
/** The /v1/search request for `ask` (searchArgs), under `settings`, as Parallel's guidance for agents asks: the
|
|
111
|
+
* objective, a goal in plain words, as its objective; the keyword queries as search_queries; and the model and
|
|
112
|
+
* session of `client` (cut at SESSION_MAX) where the run knows them. Excerpts are Parallel's own length
|
|
113
|
+
* (max_chars_total where set). */
|
|
114
|
+
export function parallelSearchBody({ objective, queries, n }, settings = {}, client = {}) {
|
|
115
|
+
const source = {
|
|
116
|
+
...(settings.includeDomains ? { include_domains: settings.includeDomains } : {}),
|
|
117
|
+
...(settings.excludeDomains ? { exclude_domains: settings.excludeDomains } : {}),
|
|
118
|
+
...(settings.afterDate ? { after_date: settings.afterDate } : {}),
|
|
119
|
+
};
|
|
120
|
+
return {
|
|
121
|
+
objective,
|
|
122
|
+
search_queries: queries,
|
|
123
|
+
mode: settings.mode ?? PARALLEL_DEFAULTS.mode,
|
|
124
|
+
...(settings.maxCharsTotal === undefined ? {} : { max_chars_total: settings.maxCharsTotal }),
|
|
125
|
+
advanced_settings: { max_results: n, ...(Object.keys(source).length ? { source_policy: source } : {}), ...fetchPolicy(settings) },
|
|
126
|
+
...clientFields(client),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/** `{objective, queries?, n?}` (searchArgs) searched through `parallel` (null: the run holds no key) under `settings`,
|
|
130
|
+
* told `client`, and the limit Parallel answered for the run's key, if any. */
|
|
131
|
+
export async function parallelSearched(args, parallel, signal, settings = {}, client = {}) {
|
|
73
132
|
const answer = (search) => ({ answer: search });
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
133
|
+
const ask = searchArgs(args);
|
|
134
|
+
const { objective, queries, n } = ask;
|
|
135
|
+
if (!objective)
|
|
136
|
+
return answer({ error: "web_search needs an objective" });
|
|
77
137
|
if (!parallel)
|
|
78
|
-
return
|
|
138
|
+
return searchFailed(failed("refused", "NO_KEY", "web_search searches through Parallel, and this run holds no Parallel key (PARALLEL_API_KEY)"));
|
|
79
139
|
if (!parallel.live())
|
|
80
|
-
return
|
|
140
|
+
return searchFailed(failed("refused", "KEY_REFUSED", REFUSED));
|
|
81
141
|
try {
|
|
82
|
-
const response = await parallel.post(PARALLEL_SEARCH_URL,
|
|
142
|
+
const response = await parallel.post(PARALLEL_SEARCH_URL, parallelSearchBody(ask, settings, client), signal);
|
|
83
143
|
if (!response.ok) {
|
|
84
144
|
await response.body?.cancel().catch(() => undefined);
|
|
85
145
|
const limit = parallelLimit(response);
|
|
86
|
-
|
|
146
|
+
if (!parallel.live())
|
|
147
|
+
return searchFailed(failed("refused", "KEY_REFUSED", REFUSED));
|
|
148
|
+
const said = failed(limit ? limit.kind : response.status >= 500 ? "infra" : "error", `HTTP_${response.status}`, `Parallel's search answered ${response.status}`, limit);
|
|
149
|
+
return searchFailed(response.status === 402 ? keyWide(said) : said);
|
|
87
150
|
}
|
|
88
151
|
const { results } = (await response.json());
|
|
89
152
|
const hits = (Array.isArray(results) ? results : []).flatMap((row) => {
|
|
@@ -92,11 +155,36 @@ export async function parallelSearched(args, parallel, signal) {
|
|
|
92
155
|
const excerpts = Array.isArray(row.excerpts) ? row.excerpts.filter((text) => typeof text === "string") : [];
|
|
93
156
|
return [{ url: row.url, title: typeof row.title === "string" ? clip(row.title, TITLE_CAP) : "", snippet: snippetOf(excerpts.join(" … ")), ...(typeof row.publish_date === "string" ? { published: row.publish_date } : {}) }];
|
|
94
157
|
});
|
|
95
|
-
return answer({ provider: "parallel",
|
|
158
|
+
return answer({ provider: "parallel", objective, queries, results: hits.slice(0, n) });
|
|
96
159
|
}
|
|
97
160
|
catch (error) {
|
|
98
161
|
if (error instanceof PaidRefused)
|
|
99
|
-
return
|
|
100
|
-
return
|
|
162
|
+
return searchFailed(failed("refused", "CAP_REFUSED", error.message));
|
|
163
|
+
return searchFailed(error?.name === "TimeoutError" ? failed("infra", "TIMEOUT", "Parallel's search did not answer within 60 s") : failed("infra", "CONNECT", `Parallel's search failed: ${error?.message ?? String(error)}`));
|
|
101
164
|
}
|
|
102
165
|
}
|
|
166
|
+
/** web_fetch's reads through `client` (null: the run holds no key), told `told` (the Web's client, each field in place
|
|
167
|
+
* of which the call's own goes where given, as a search's): a failure in Parallel's words as ours, and why a read is not
|
|
168
|
+
* asked now (no key, or the key refused earlier in the run). */
|
|
169
|
+
export function parallelReads(client, told = {}) {
|
|
170
|
+
return {
|
|
171
|
+
async read(url, { signal, client: call }) {
|
|
172
|
+
if (!client)
|
|
173
|
+
return failed("refused", "NO_KEY", "not tried: this run holds no Parallel key");
|
|
174
|
+
const asked = { ...told, ...call };
|
|
175
|
+
// A stand-in client (a test's) may give its read alone, why in words.
|
|
176
|
+
if (client.extract)
|
|
177
|
+
return client.extract(url, signal, asked);
|
|
178
|
+
const read = await client.read(url, signal, asked);
|
|
179
|
+
return typeof read === "string" ? { error: read } : read;
|
|
180
|
+
},
|
|
181
|
+
unready: () => (!client ? failure("refused", "NO_KEY", "not tried: this run holds no Parallel key") : client.live() ? undefined : failure("refused", "KEY_REFUSED", "not tried: refused earlier in this run")),
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
/** Parallel on the run's key, both tools on one client (a key refused by either is refused by both). Its search keeps
|
|
185
|
+
* the caller's signal and its own bound; a fetch it gives no page is said on the page. */
|
|
186
|
+
export const parallel = defineProvider({ id: "parallel", label: "Parallel", key: { env: "PARALLEL_API_KEY" }, ops: ["search", "fetch"], fetch: { miss: "said" }, settings: settingsOf(parallelSettingsProblems) }, ({ key, send, pay, settings, client: told }) => {
|
|
187
|
+
const asked = (settings ?? {});
|
|
188
|
+
const client = key === undefined ? null : parallelExtract(key, send ?? fetch, pay, asked);
|
|
189
|
+
return { search: (ask, _deadline, signal, _bounds, call) => parallelSearched(ask, client, signal, asked, { ...told, ...call }), ...parallelReads(client, told) };
|
|
190
|
+
});
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import type { SettingsSpec, WebProvider } from "../provider.js";
|
|
2
|
+
export declare const PROVIDERS: readonly [{
|
|
3
|
+
readonly id: "parallel";
|
|
4
|
+
readonly label: "Parallel";
|
|
5
|
+
readonly key: {
|
|
6
|
+
readonly env: "PARALLEL_API_KEY";
|
|
7
|
+
};
|
|
8
|
+
readonly ops: readonly ["search", "fetch"];
|
|
9
|
+
readonly fetch: {
|
|
10
|
+
readonly miss: "said";
|
|
11
|
+
};
|
|
12
|
+
readonly settings: SettingsSpec<import("./parallel.js").ParallelSettings>;
|
|
13
|
+
} & Pick<WebProvider<string>, "make">, {
|
|
14
|
+
readonly id: "exa";
|
|
15
|
+
readonly label: "Exa";
|
|
16
|
+
readonly key: {
|
|
17
|
+
readonly env: "EXA_API_KEY";
|
|
18
|
+
};
|
|
19
|
+
readonly ops: readonly ["search", "fetch"];
|
|
20
|
+
readonly fetch: {
|
|
21
|
+
readonly miss: {
|
|
22
|
+
readonly log: "web_fetch_provider_failed";
|
|
23
|
+
readonly when: "failed";
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
readonly settings: SettingsSpec<import("./exa.js").ExaSettings>;
|
|
27
|
+
} & Pick<WebProvider<string>, "make">, {
|
|
28
|
+
readonly id: "firecrawl";
|
|
29
|
+
readonly label: "Firecrawl";
|
|
30
|
+
readonly key: {
|
|
31
|
+
readonly env: "FIRECRAWL_API_KEY";
|
|
32
|
+
};
|
|
33
|
+
readonly ops: readonly ["search", "fetch"];
|
|
34
|
+
readonly fetch: {
|
|
35
|
+
readonly miss: {
|
|
36
|
+
readonly log: "web_fetch_provider_failed";
|
|
37
|
+
readonly when: "failed";
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
readonly settings: SettingsSpec<import("./firecrawl.js").FirecrawlSettings>;
|
|
41
|
+
} & Pick<WebProvider<string>, "make">, {
|
|
42
|
+
readonly id: "firecrawl-keyless";
|
|
43
|
+
readonly label: "Firecrawl";
|
|
44
|
+
readonly keyless: true;
|
|
45
|
+
readonly ops: readonly ["search", "fetch"];
|
|
46
|
+
readonly fetch: {
|
|
47
|
+
readonly miss: {
|
|
48
|
+
readonly log: "web_fetch_firecrawl_failed";
|
|
49
|
+
readonly when: "ends";
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
} & Pick<WebProvider<string>, "make">, {
|
|
53
|
+
readonly id: "exa-keyless";
|
|
54
|
+
readonly label: "Exa";
|
|
55
|
+
readonly keyless: true;
|
|
56
|
+
readonly ops: readonly ["search"];
|
|
57
|
+
} & Pick<WebProvider<string>, "make">, {
|
|
58
|
+
readonly id: "native";
|
|
59
|
+
readonly label: "the model provider's own search";
|
|
60
|
+
readonly ops: readonly ["search"];
|
|
61
|
+
} & Pick<WebProvider<string>, "make">, {
|
|
62
|
+
readonly id: "direct";
|
|
63
|
+
readonly label: "the guarded direct fetch";
|
|
64
|
+
readonly ops: readonly ["fetch"];
|
|
65
|
+
readonly fetch: {
|
|
66
|
+
readonly ends: true;
|
|
67
|
+
};
|
|
68
|
+
} & Pick<WebProvider<string>, "make">];
|
|
69
|
+
type Registered = (typeof PROVIDERS)[number];
|
|
70
|
+
export type ProviderId = Registered["id"];
|
|
71
|
+
type Serving<Op extends string, P = Registered> = P extends {
|
|
72
|
+
id: infer I extends string;
|
|
73
|
+
ops: readonly (infer O)[];
|
|
74
|
+
} ? (Op extends O ? I : never) : never;
|
|
75
|
+
/** A provider an order may name for web_search, and for web_fetch. */
|
|
76
|
+
export type SearchProvider = Serving<"search">;
|
|
77
|
+
export type FetchProvider = Serving<"fetch">;
|
|
78
|
+
/** A provider read on the run's key (WebConfig.keys), and one whose fetch says on the page why it gave none. */
|
|
79
|
+
export type KeyedProvider = Registered extends infer P ? (P extends {
|
|
80
|
+
id: infer I;
|
|
81
|
+
key: {
|
|
82
|
+
env: string;
|
|
83
|
+
};
|
|
84
|
+
} ? I : never) : never;
|
|
85
|
+
export type SaidProvider = Registered extends infer P ? (P extends {
|
|
86
|
+
id: infer I;
|
|
87
|
+
fetch: {
|
|
88
|
+
miss: "said";
|
|
89
|
+
};
|
|
90
|
+
} ? I : never) : never;
|
|
91
|
+
/** A provider web_fetch may ask ahead of the one that ends its order: the page names why it gave none under its id. */
|
|
92
|
+
export type AheadProvider = Registered extends infer P ? (P extends {
|
|
93
|
+
id: infer I;
|
|
94
|
+
fetch: {
|
|
95
|
+
miss: unknown;
|
|
96
|
+
};
|
|
97
|
+
} ? I : never) : never;
|
|
98
|
+
/** Each provider's settings, typed, by its id: those of a provider that takes any (each with its defaults, in its file). */
|
|
99
|
+
export type ProviderSettings = {
|
|
100
|
+
[P in Registered as P extends {
|
|
101
|
+
settings: SettingsSpec<unknown>;
|
|
102
|
+
} ? P["id"] : never]?: P extends {
|
|
103
|
+
settings: SettingsSpec<infer S>;
|
|
104
|
+
} ? S : never;
|
|
105
|
+
};
|
|
106
|
+
export declare const isProviderId: (id: unknown) => id is ProviderId;
|
|
107
|
+
/** The provider `id` names; a throw for an id no provider has (an order is checked when the Web is made). */
|
|
108
|
+
export declare function providerOf(id: string): WebProvider;
|
|
109
|
+
/** Whether `id` names a provider serving `op`. */
|
|
110
|
+
export declare const serves: (id: unknown, op: "search" | "fetch") => boolean;
|
|
111
|
+
/** The ids of the providers whose fetch says on the page a turn the guard refused, and records every turn (SaidProvider). */
|
|
112
|
+
export declare const SAID: readonly SaidProvider[];
|
|
113
|
+
/** The ids of the providers web_fetch may ask ahead of the one that ends its order (AheadProvider), in registry order. */
|
|
114
|
+
export declare const AHEAD: readonly AheadProvider[];
|
|
115
|
+
/** The provider `id` names, in our words: its label, after "keyless" where it is a service's free use. */
|
|
116
|
+
export declare const providerName: (id: string) => string;
|
|
117
|
+
/** What is wrong with `settings`, one line each: a provider they name that takes none, or what its own check refuses. */
|
|
118
|
+
export declare function settingsProblemsOf(settings: unknown): string[];
|
|
119
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { direct } from "./direct.js";
|
|
2
|
+
import { exa } from "./exa.js";
|
|
3
|
+
import { exaKeyless } from "./exa-keyless.js";
|
|
4
|
+
import { firecrawl } from "./firecrawl.js";
|
|
5
|
+
import { firecrawlKeyless } from "./firecrawl-keyless.js";
|
|
6
|
+
import { native } from "./native.js";
|
|
7
|
+
import { parallel } from "./parallel.js";
|
|
8
|
+
export const PROVIDERS = Object.freeze([parallel, exa, firecrawl, firecrawlKeyless, exaKeyless, native, direct]);
|
|
9
|
+
const BY_ID = new Map(PROVIDERS.map((provider) => [provider.id, provider]));
|
|
10
|
+
export const isProviderId = (id) => typeof id === "string" && BY_ID.has(id);
|
|
11
|
+
/** The provider `id` names; a throw for an id no provider has (an order is checked when the Web is made). */
|
|
12
|
+
export function providerOf(id) {
|
|
13
|
+
const provider = BY_ID.get(id);
|
|
14
|
+
if (!provider)
|
|
15
|
+
throw new Error(`web: no provider ${JSON.stringify(id)} (the providers are ${[...BY_ID.keys()].join(", ")})`);
|
|
16
|
+
return provider;
|
|
17
|
+
}
|
|
18
|
+
/** Whether `id` names a provider serving `op`. */
|
|
19
|
+
export const serves = (id, op) => isProviderId(id) && providerOf(id).ops.includes(op);
|
|
20
|
+
/** The ids of the providers whose fetch says on the page a turn the guard refused, and records every turn (SaidProvider). */
|
|
21
|
+
export const SAID = PROVIDERS.flatMap(({ id, fetch }) => (fetch && !fetch.ends && fetch.miss === "said" ? [id] : []));
|
|
22
|
+
/** The ids of the providers web_fetch may ask ahead of the one that ends its order (AheadProvider), in registry order. */
|
|
23
|
+
export const AHEAD = PROVIDERS.flatMap(({ id, fetch }) => (fetch && !fetch.ends ? [id] : []));
|
|
24
|
+
/** The provider `id` names, in our words: its label, after "keyless" where it is a service's free use. */
|
|
25
|
+
export const providerName = (id) => (providerOf(id).keyless ? `keyless ${providerOf(id).label}` : providerOf(id).label);
|
|
26
|
+
/** What is wrong with `settings`, one line each: a provider they name that takes none, or what its own check refuses. */
|
|
27
|
+
export function settingsProblemsOf(settings) {
|
|
28
|
+
if (settings === undefined)
|
|
29
|
+
return [];
|
|
30
|
+
if (typeof settings !== "object" || settings === null || Array.isArray(settings))
|
|
31
|
+
return ["settings must be an object of each provider's settings"];
|
|
32
|
+
const taking = PROVIDERS.filter((provider) => provider.settings).map(({ id }) => id);
|
|
33
|
+
const named = `${taking.slice(0, -1).join(", ")} and ${taking.at(-1)}`;
|
|
34
|
+
return Object.entries(settings).flatMap(([name, one]) => (one === undefined ? [] : BY_ID.get(name)?.settings?.problems(one) ?? [`settings name ${name}, which takes no settings here (${named} do)`]));
|
|
35
|
+
}
|