@parcha/agentrun-web 0.0.0-bootstrap.0 → 0.1.0-beta.57
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/LICENSE +202 -0
- package/NOTICE +4 -0
- package/README.md +71 -0
- package/dist/fees.d.ts +36 -0
- package/dist/fees.js +41 -0
- package/dist/fetch.d.ts +59 -0
- package/dist/fetch.js +183 -0
- package/dist/guard.d.ts +46 -0
- package/dist/guard.js +3 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +16 -0
- package/dist/page.d.ts +117 -0
- package/dist/page.js +143 -0
- package/dist/providers/direct.d.ts +9 -0
- package/dist/providers/direct.js +53 -0
- package/dist/providers/exa-keyless.d.ts +35 -0
- package/dist/providers/exa-keyless.js +109 -0
- package/dist/providers/firecrawl-keyless.d.ts +40 -0
- package/dist/providers/firecrawl-keyless.js +132 -0
- package/dist/providers/mcp.d.ts +39 -0
- package/dist/providers/mcp.js +107 -0
- package/dist/providers/parallel.d.ts +20 -0
- package/dist/providers/parallel.js +102 -0
- package/dist/search.d.ts +110 -0
- package/dist/search.js +153 -0
- package/dist/text-urls.d.ts +38 -0
- package/dist/text-urls.js +166 -0
- package/dist/web.d.ts +77 -0
- package/dist/web.js +111 -0
- package/package.json +60 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# @parcha/agentrun-web
|
|
2
|
+
|
|
3
|
+
Web search and web fetch for agents. Each provider is asked in an order given as data, behind a breaker of its own. A
|
|
4
|
+
run keeps a page cache it reads in parts. A direct fetch goes only through a public-host guard the caller injects.
|
|
5
|
+
AgentRun's server answers `web_search` and `web_fetch` through one `createWeb` per run.
|
|
6
|
+
|
|
7
|
+
It imports nothing from the server, a browser or a vendor SDK. Everything from outside comes in through `WebConfig`:
|
|
8
|
+
the provider order, the keys, the guard, the run's cap and the words a provider's refusal is told in.
|
|
9
|
+
|
|
10
|
+
## One run's web
|
|
11
|
+
|
|
12
|
+
```js
|
|
13
|
+
import { createWeb } from "@parcha/agentrun-web";
|
|
14
|
+
import { publicCheck, publicFetch, readPage } from "@parcha/pi-browser";
|
|
15
|
+
|
|
16
|
+
const web = createWeb({
|
|
17
|
+
order: { search: ["firecrawl-keyless", "exa-keyless"], fetch: ["firecrawl-keyless", "direct"] },
|
|
18
|
+
// The public-host guard: pi-browser's, the one AgentRun's server gives it.
|
|
19
|
+
guard: { fetch: publicFetch, check: publicCheck, html: readPage },
|
|
20
|
+
});
|
|
21
|
+
const { answer, receipt } = await web.search("agentrun durable workflows", { n: 5 });
|
|
22
|
+
const page = await web.fetch("https://example.org/", { offset: 0, maxChars: 20_000 });
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
- `order.search` holds the search providers in the order they are asked: `parallel`, `firecrawl-keyless`,
|
|
26
|
+
`exa-keyless`, `native`. The next is asked when one errs or its breaker holds it. `order.fetch` holds the fetch
|
|
27
|
+
providers, with `direct` for the guarded direct fetch. An order without `direct` never fetches a page itself.
|
|
28
|
+
- `guard` is the only way a page is fetched directly. Without one, nothing is fetched directly and no URL goes to a
|
|
29
|
+
provider: the read fails closed. A guard failure shaped `{ failure: { code: "refused" } }` is a refusal: the page is
|
|
30
|
+
not read, and the model is told why. `guard.check` decides which URLs go to Firecrawl and Parallel, so it must refuse
|
|
31
|
+
every URL `guard.fetch` would refuse: a check that passes everything sends private and internal URLs to a provider.
|
|
32
|
+
A guard is the Web's alone; one passed with a `fetch` call is ignored.
|
|
33
|
+
- `keys.parallel` is Parallel's key. `pay(fee, send)` is the run's cap: it sends the request when the fee fits, or
|
|
34
|
+
throws a `PaidRefused`. Without it, a request is unmetered.
|
|
35
|
+
- `words.rateLimited` is what a keyless provider's rate limit says to the model.
|
|
36
|
+
- `native` is the model's own hosted search, as a function. `send` and `endpoints` replace a provider's fetch or
|
|
37
|
+
address (a test's fakes). `now` is the breakers' clock. `bounds` sets the searches' deadlines. `log` receives what a
|
|
38
|
+
fetch notes that the model is not told.
|
|
39
|
+
- A search's answer is fitted to `SEARCH_MAX_CHARS`: past it, its least relevant results are left out and `omitted`
|
|
40
|
+
counts them. `search(query, { breakers })` reads and trips the given breakers instead of the Web's own (AgentRun's
|
|
41
|
+
server gives a call node fresh ones each call), and `search(query, { bounds })` holds that call to its own deadlines.
|
|
42
|
+
`fetch(url, { lookup, fetchImpl })` dials through the given resolver and transport.
|
|
43
|
+
|
|
44
|
+
Each call returns its `answer` and a `receipt`, `{ op, asked, provider, tries, out, cost, cache? }`. `tries` lists the
|
|
45
|
+
providers that answered before the call returned, in the order asked, with each outcome. A fetch also lists Parallel
|
|
46
|
+
where it was passed over (no key, refused earlier in the run, or a URL it may not be sent), with why; a part from the
|
|
47
|
+
run's pages lists none. `cost` holds the fees of the requests sent before the call returned. A fetch stopped at its
|
|
48
|
+
signal returns at once, but its read is the run's and goes on: a provider asked after the stop is in neither; one whose
|
|
49
|
+
request was already sent has its fee in `cost` but no turn in `tries`. `classifier` is `"off"`; `wall` and `injection`
|
|
50
|
+
are `"unjudged"`.
|
|
51
|
+
|
|
52
|
+
The pieces `createWeb` is built from are exported too: `searchInTurn` and `breakers` for search, `webFetch` and
|
|
53
|
+
`webFetchText` for fetch, `fetchPage` for one direct read, and `linksOf`, the links a page's text holds. So are the
|
|
54
|
+
providers (`exaSearch`, `firecrawlFetch`, `firecrawlScrape`, `firecrawlSearch`, `parallelExtract`, `parallelSearched`)
|
|
55
|
+
and Parallel's prices and fees (`PARALLEL_EXTRACT_USD`, `PARALLEL_SEARCH_USD`, `PARALLEL_SEARCH_INCLUDED`,
|
|
56
|
+
`PARALLEL_SEARCH_MODE_USD`, `PARALLEL_SEARCH_RESULT_USD`, `parallelExtractFee`, `parallelSearchFee`, `parallelFeeOf`,
|
|
57
|
+
`PaidRefused` and `unmetered`). The package's entry names each export; nothing else in it is API.
|
|
58
|
+
`@parcha/agentrun-web/text-urls` holds the link finder alone, for code that only finds the URLs in a text (the
|
|
59
|
+
console's timeline compiles it from source).
|
|
60
|
+
|
|
61
|
+
The rest of the entry is for servers: the bounds and words AgentRun's server builds its two tools from, and the helpers
|
|
62
|
+
that apply them. A caller of `createWeb` needs none of them. They are API like everything else the entry names, so a
|
|
63
|
+
change to one is a change to the package's API.
|
|
64
|
+
|
|
65
|
+
- search: `searchArgs`, `searchText`, `fitSearch`, `snippetOf`, `CITE`, `ANSWER_MAX`, `QUERY_MAX`, `RESULTS_DEFAULT`,
|
|
66
|
+
`RESULTS_MAX`, `SEARCH_MAX_CHARS` and `SNIPPET_MAX`;
|
|
67
|
+
- fetch: `clip`, `partEnd`, `partProblem`, `readDirect`, `FETCH_ORDER`, `PAGES_HELD`, `HTML_READ_CHARS`, `LINKS_CAP`,
|
|
68
|
+
`LINKS_CHARS`, `MAX_CHARS_MIN`, `PAGE_CHARS`, `PAGE_MAX_BYTES`, `PAGE_READ_MAX_CHARS`, `TEXT_CAP`, `TITLE_CAP` and
|
|
69
|
+
`URL_MAX`;
|
|
70
|
+
- providers: `PARALLEL_EXTRACT_URL` and `PARALLEL_SEARCH_URL`;
|
|
71
|
+
- links: `linksWritten`, `TRAILING`, `trimmed` and `wholeUrl`.
|
package/dist/fees.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** The modes Parallel's Search API is priced in here; a request in any other mode (or naming none) has no price. */
|
|
2
|
+
export type ParallelSearchMode = "turbo" | "fast" | "basic" | "advanced";
|
|
3
|
+
/** Parallel's published prices (https://docs.parallel.ai/resources/pricing, read 2026-11-20). Search, by the request's
|
|
4
|
+
* `mode`: `turbo` or `fast` $1 per 1,000 requests, `basic` or `advanced` $5 per 1,000, each with 10 results included,
|
|
5
|
+
* and each result past those 10 $1 per 1,000. Extract: $1 per 1,000 URLs. */
|
|
6
|
+
export declare const PARALLEL_SEARCH_MODE_USD: Readonly<Record<ParallelSearchMode, number>>;
|
|
7
|
+
/** A `fast` search's price: the mode the web tools search in (web-search.ts). */
|
|
8
|
+
export declare const PARALLEL_SEARCH_USD = 0.001;
|
|
9
|
+
export declare const PARALLEL_SEARCH_RESULT_USD = 0.001;
|
|
10
|
+
export declare const PARALLEL_SEARCH_INCLUDED = 10;
|
|
11
|
+
export declare const PARALLEL_EXTRACT_USD = 0.001;
|
|
12
|
+
/** One paid request: `name` in the cap's refusal, `tool` and `by` on its records, `usd` its price, `basis` why. */
|
|
13
|
+
export type Fee = {
|
|
14
|
+
name: string;
|
|
15
|
+
by: string;
|
|
16
|
+
usd: number;
|
|
17
|
+
basis: string;
|
|
18
|
+
};
|
|
19
|
+
/** A search in `mode` for `n` results: the mode's price (PARALLEL_SEARCH_MODE_USD), and $0.001 for each past the 10 it
|
|
20
|
+
* includes. The web tools search `fast`: $0.001. */
|
|
21
|
+
export declare const parallelSearchFee: (n: number, mode?: ParallelSearchMode) => Fee;
|
|
22
|
+
/** An extract of `urls` URLs (the web tools ask one at a time). */
|
|
23
|
+
export declare const parallelExtractFee: (urls?: number) => Fee;
|
|
24
|
+
/** The fee of a request to Parallel's `url` with `body`: its search's, by its `mode` and the results it asks for (10 by
|
|
25
|
+
* default), or its extract's, by the URLs; undefined for an endpoint the web tools do not ask, and for a search whose
|
|
26
|
+
* mode has no price here (one naming none, or one Parallel added since): never $0, never another mode's price. */
|
|
27
|
+
export declare function parallelFeeOf(url: string, body: unknown): Fee | undefined;
|
|
28
|
+
/** 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
|
|
29
|
+
* cap's words. */
|
|
30
|
+
export declare class PaidRefused extends Error {
|
|
31
|
+
readonly name = "PaidRefused";
|
|
32
|
+
}
|
|
33
|
+
/** How a paid request is sent: `send` once the run's cap admits `fee`, or a throw of PaidRefused when it does not. */
|
|
34
|
+
export type Pay = (fee: Fee, send: () => Promise<Response>) => Promise<Response>;
|
|
35
|
+
/** No cap: the request is sent as it is. */
|
|
36
|
+
export declare const unmetered: Pay;
|
package/dist/fees.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// Parallel's prices, the Fee a paid request carries, and Pay, the injected cap a paid request goes through. A request
|
|
2
|
+
// the cap refuses is never sent: Pay throws PaidRefused, whose message is the refusal.
|
|
3
|
+
/** Parallel's published prices (https://docs.parallel.ai/resources/pricing, read 2026-11-20). Search, by the request's
|
|
4
|
+
* `mode`: `turbo` or `fast` $1 per 1,000 requests, `basic` or `advanced` $5 per 1,000, each with 10 results included,
|
|
5
|
+
* and each result past those 10 $1 per 1,000. Extract: $1 per 1,000 URLs. */
|
|
6
|
+
export const PARALLEL_SEARCH_MODE_USD = Object.freeze({ turbo: 0.001, fast: 0.001, basic: 0.005, advanced: 0.005 });
|
|
7
|
+
/** A `fast` search's price: the mode the web tools search in (web-search.ts). */
|
|
8
|
+
export const PARALLEL_SEARCH_USD = 0.001;
|
|
9
|
+
export const PARALLEL_SEARCH_RESULT_USD = 0.001;
|
|
10
|
+
export const PARALLEL_SEARCH_INCLUDED = 10;
|
|
11
|
+
export const PARALLEL_EXTRACT_USD = 0.001;
|
|
12
|
+
/** In whole millionths of a dollar, so a sum of prices is the price. */
|
|
13
|
+
const usd = (dollars) => Math.round(dollars * 1e6) / 1e6;
|
|
14
|
+
const isSearchMode = (mode) => typeof mode === "string" && Object.hasOwn(PARALLEL_SEARCH_MODE_USD, mode);
|
|
15
|
+
/** A search in `mode` for `n` results: the mode's price (PARALLEL_SEARCH_MODE_USD), and $0.001 for each past the 10 it
|
|
16
|
+
* includes. The web tools search `fast`: $0.001. */
|
|
17
|
+
export const parallelSearchFee = (n, mode = "fast") => {
|
|
18
|
+
const more = Math.max(0, n - PARALLEL_SEARCH_INCLUDED);
|
|
19
|
+
return { name: "Parallel's search", by: "parallel", usd: usd(PARALLEL_SEARCH_MODE_USD[mode] + PARALLEL_SEARCH_RESULT_USD * more), basis: more ? `Parallel's list price for a ${mode} search of ${n} results` : `Parallel's list price for a ${mode} search` };
|
|
20
|
+
};
|
|
21
|
+
/** An extract of `urls` URLs (the web tools ask one at a time). */
|
|
22
|
+
export const parallelExtractFee = (urls = 1) => ({ name: "Parallel's extract", by: "parallel", usd: usd(PARALLEL_EXTRACT_USD * urls), basis: urls === 1 ? "Parallel's list price for one URL" : `Parallel's list price for ${urls} URLs` });
|
|
23
|
+
/** The fee of a request to Parallel's `url` with `body`: its search's, by its `mode` and the results it asks for (10 by
|
|
24
|
+
* default), or its extract's, by the URLs; undefined for an endpoint the web tools do not ask, and for a search whose
|
|
25
|
+
* mode has no price here (one naming none, or one Parallel added since): never $0, never another mode's price. */
|
|
26
|
+
export function parallelFeeOf(url, body) {
|
|
27
|
+
const path = URL.parse(url)?.pathname;
|
|
28
|
+
const asked = (body ?? {});
|
|
29
|
+
if (path === "/v1/search")
|
|
30
|
+
return isSearchMode(asked.mode) ? parallelSearchFee(typeof asked.advanced_settings?.max_results === "number" ? asked.advanced_settings.max_results : PARALLEL_SEARCH_INCLUDED, asked.mode) : undefined;
|
|
31
|
+
if (path === "/v1/extract")
|
|
32
|
+
return parallelExtractFee(Array.isArray(asked.urls) ? Math.max(1, asked.urls.length) : 1);
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
/** 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
|
+
* cap's words. */
|
|
37
|
+
export class PaidRefused extends Error {
|
|
38
|
+
name = "PaidRefused";
|
|
39
|
+
}
|
|
40
|
+
/** No cap: the request is sent as it is. */
|
|
41
|
+
export const unmetered = (_fee, send) => send();
|
package/dist/fetch.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Guard, GuardOptions } from "./guard.js";
|
|
2
|
+
import { type Page, type Part, type Read, type Unread } from "./page.js";
|
|
3
|
+
import type { KeylessFetch } from "./providers/firecrawl-keyless.js";
|
|
4
|
+
import type { Extract } from "./providers/parallel.js";
|
|
5
|
+
/** How many pages a run keeps what it read of (Pages): each at most PAGE_READ_MAX_CHARS characters of text. */
|
|
6
|
+
export declare const PAGES_HELD = 8;
|
|
7
|
+
/** `parallel`, where Parallel was asked for the page and gave none: why, on the page fetched directly after it, or on
|
|
8
|
+
* the failed page ("none") where nothing after it read one. `via` "none": the caller's order holds no direct fetch,
|
|
9
|
+
* and no provider in it read the page. */
|
|
10
|
+
export type WebPage = Page & {
|
|
11
|
+
via: "parallel" | "firecrawl-keyless" | "direct" | "stopped" | "none";
|
|
12
|
+
parallel?: string;
|
|
13
|
+
};
|
|
14
|
+
/** A page web_fetch read, and how (WebPage's `via` and `parallel`). */
|
|
15
|
+
export type WebRead = Read & Pick<WebPage, "via" | "parallel">;
|
|
16
|
+
/** A page web_fetch could not read, and how it tried. */
|
|
17
|
+
export type WebUnread = Unread & Pick<WebPage, "via" | "parallel">;
|
|
18
|
+
/** 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
|
+
* last: at most PAGES_HELD of them. */
|
|
20
|
+
export type Pages = Map<string, Promise<WebRead | WebUnread>>;
|
|
21
|
+
/** A fetch provider, as an order names it. */
|
|
22
|
+
export type FetchProvider = "firecrawl-keyless" | "parallel" | "direct";
|
|
23
|
+
/** The order a read takes when none is given: keyless Firecrawl, then Parallel, each where its client is given, then the
|
|
24
|
+
* guarded direct fetch. */
|
|
25
|
+
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). */
|
|
27
|
+
export type WebLog = (event: string, fields: Record<string, string>) => void;
|
|
28
|
+
/** One provider a read asked, as it answered, or Parallel passed over with why (its "not tried: ..."): a turn of a
|
|
29
|
+
* fetch's receipt. */
|
|
30
|
+
export type FetchTry = {
|
|
31
|
+
provider: FetchProvider;
|
|
32
|
+
outcome: "ok" | "error";
|
|
33
|
+
error?: string;
|
|
34
|
+
};
|
|
35
|
+
/** What webFetch is given: the guard's resolver and fetch (a test's) and the part, the provider clients (`parallel`,
|
|
36
|
+
* `firecrawl`) and their `order` (FETCH_ORDER by default), the run's `pages`, the `guard`, the guard check's bound
|
|
37
|
+
* (`checkMs`), the `log`, `tried`, told each provider the read asks as it answers (never on a page from `pages`), and
|
|
38
|
+
* `noParallel`, what a page fetched past an order's Parallel with no client says of it. */
|
|
39
|
+
export type WebFetchOptions = GuardOptions & Part & {
|
|
40
|
+
parallel?: Extract | null;
|
|
41
|
+
firecrawl?: KeylessFetch | null;
|
|
42
|
+
pages?: Pages;
|
|
43
|
+
order?: readonly FetchProvider[];
|
|
44
|
+
guard?: Guard;
|
|
45
|
+
checkMs?: number;
|
|
46
|
+
log?: WebLog;
|
|
47
|
+
tried?: (one: FetchTry) => void;
|
|
48
|
+
noParallel?: string;
|
|
49
|
+
};
|
|
50
|
+
/** The part (`offset`, `maxChars`) of the page at `url` as a WebPage: from `pages`, the run's, where it holds the URL
|
|
51
|
+
* (read, or being read), with no request of its own; else read once (readOnce), kept there from the read's start. A read
|
|
52
|
+
* that fails leaves `pages`, so a later call reads again. The read is the run's, not a caller's: no caller's signal
|
|
53
|
+
* reaches it (it keeps Parallel's and the fetch's own time limits), so one caller's stop never fails another's; each
|
|
54
|
+
* caller stops waiting at its own signal (untilStopped). */
|
|
55
|
+
export declare function webFetch(url: unknown, { parallel, firecrawl, pages, order, guard, checkMs, offset, maxChars, signal, log, tried, noParallel, ...options }?: WebFetchOptions): Promise<WebPage>;
|
|
56
|
+
/** A page as the tool `name` answers the model: its JSON, then a line saying why Parallel gave none where it was picked
|
|
57
|
+
* (and that the page was fetched directly, only where it was), and one saying where this part is in the page when it
|
|
58
|
+
* is not all of it. */
|
|
59
|
+
export declare function webFetchText(page: WebPage | Page, name?: string): string;
|
package/dist/fetch.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { askedUrl, HTML_READ_CHARS, PAGE_READ_MAX_CHARS, partOfRead, TIMEOUT_MS, URL_MAX, withoutCredentials, withoutUserinfo } from "./page.js";
|
|
2
|
+
import { readDirect } from "./providers/direct.js";
|
|
3
|
+
const PORTS = new Set(["", "80", "443"]);
|
|
4
|
+
/** How many pages a run keeps what it read of (Pages): each at most PAGE_READ_MAX_CHARS characters of text. */
|
|
5
|
+
export const PAGES_HELD = 8;
|
|
6
|
+
/** The order a read takes when none is given: keyless Firecrawl, then Parallel, each where its client is given, then the
|
|
7
|
+
* guarded direct fetch. */
|
|
8
|
+
export const FETCH_ORDER = ["firecrawl-keyless", "parallel", "direct"];
|
|
9
|
+
/** The URL a provider is sent for `url`, as parsed (its `href`), so the provider reads the host the guard checked; or
|
|
10
|
+
* `why` it may not go: readDirect's own refusals, in its words (not a URL, the href's length, the port, a user name or
|
|
11
|
+
* password), no guard, or the guard's check failing within `ms` and the caller's signal (a check out of time too). */
|
|
12
|
+
async function passes(url, guard, options, ms) {
|
|
13
|
+
const target = typeof url === "string" ? URL.parse(url) : null;
|
|
14
|
+
if (!target)
|
|
15
|
+
return { why: "url is not an absolute URL" };
|
|
16
|
+
if (target.href.length > URL_MAX)
|
|
17
|
+
return { why: `url is longer than ${URL_MAX.toLocaleString("en-US")} characters` };
|
|
18
|
+
if (!PORTS.has(target.port))
|
|
19
|
+
return { why: `port ${target.port} is not 80 or 443` };
|
|
20
|
+
if (target.username || target.password)
|
|
21
|
+
return { why: "the URL holds a user name or password" };
|
|
22
|
+
if (!guard)
|
|
23
|
+
return { why: "no public-host guard was given" };
|
|
24
|
+
const signal = AbortSignal.any([AbortSignal.timeout(ms), ...(options.signal ? [options.signal] : [])]);
|
|
25
|
+
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
|
+
}
|
|
27
|
+
/** The page at `url` read once, by the providers of `order` in turn: keyless Firecrawl or Parallel where its client is
|
|
28
|
+
* given and live and the guard passes the URL within `checkMs` (default the guarded fetch's 30 s), the guarded fetch
|
|
29
|
+
* at "direct", which answers whatever it reads. A page fetched directly where `parallel` was given says why Parallel
|
|
30
|
+
* gave none (`parallel`). */
|
|
31
|
+
async function readOnce(url, { parallel, firecrawl, order, guard, checkMs, options, log, tried, noParallel }) {
|
|
32
|
+
let firecrawlSaid;
|
|
33
|
+
let why;
|
|
34
|
+
// Each turn told where it ends, so every provider asked is told, whichever answers the page or none does. What a
|
|
35
|
+
// provider says reaches it with no URL's user name or password in it: scrubbed whole as it is read, before any cut
|
|
36
|
+
// (withoutUserinfo; readDirect scrubs its own).
|
|
37
|
+
const turn = (provider, read) => tried?.(typeof read === "string" ? { provider, outcome: "error", error: read } : read.ok ? { provider, outcome: "ok" } : { provider, outcome: "error", error: read.refused ?? read.error ?? "" });
|
|
38
|
+
for (const provider of order) {
|
|
39
|
+
if (provider === "firecrawl-keyless") {
|
|
40
|
+
const sent = firecrawl?.live() ? await passes(url, guard, options, checkMs) : null;
|
|
41
|
+
if (firecrawl && sent && "href" in sent) {
|
|
42
|
+
const read = await firecrawl.read(sent.href, options.signal);
|
|
43
|
+
if (typeof read !== "string") {
|
|
44
|
+
turn("firecrawl-keyless", read);
|
|
45
|
+
// A second scrub behind the client's own, kept on purpose: it covers a url the client did not clear and has not cut.
|
|
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" });
|
|
79
|
+
// A second scrub behind readDirect's own, kept on purpose: it covers a url readDirect did not clear and has not cut.
|
|
80
|
+
// No test reaches it: readDirect clears every url it names, the URL asked and the one its fetch ended at alike.
|
|
81
|
+
return { ...direct, url: withoutCredentials(direct.url), via: "direct", ...(why ? { parallel: why } : {}) };
|
|
82
|
+
}
|
|
83
|
+
}
|
|
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", ...(why ? { parallel: why } : {}) };
|
|
85
|
+
}
|
|
86
|
+
/** The reads that have ended: only these leave `pages` past its bound (trim). */
|
|
87
|
+
const settled = new WeakSet();
|
|
88
|
+
/** `pages` down to PAGES_HELD reads that have ENDED, dropping the least recently used of those first. Only ended reads
|
|
89
|
+
* count toward the bound, and only they are dropped: a read under way stays until it ends (a caller may be about to
|
|
90
|
+
* share it, and dropping it would read and bill the page again), and it never makes room by pushing out a page that has
|
|
91
|
+
* ended. So the run holds at most PAGES_HELD ended reads plus the ones still under way, and trims again as each ends. */
|
|
92
|
+
function trim(pages) {
|
|
93
|
+
let ended = 0;
|
|
94
|
+
for (const read of pages.values())
|
|
95
|
+
if (settled.has(read))
|
|
96
|
+
ended++;
|
|
97
|
+
for (const [held, read] of pages) {
|
|
98
|
+
if (ended <= PAGES_HELD)
|
|
99
|
+
return;
|
|
100
|
+
if (settled.has(read))
|
|
101
|
+
(pages.delete(held), ended--);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/** `reading`, or, as soon as `signal` stops this call, a failed page saying so (as a stopped read reads): the read
|
|
105
|
+
* itself goes on for the run's other callers of the URL, within its own time limits. */
|
|
106
|
+
function untilStopped(url, reading, signal) {
|
|
107
|
+
if (!signal)
|
|
108
|
+
return reading;
|
|
109
|
+
// No page came for this call, by any path: never "direct", which says the host was fetched.
|
|
110
|
+
const stopped = () => ({ ok: false, url: askedUrl(url), status: 0, title: "", text: "", links: [], error: "the call was stopped before the page arrived", via: "stopped" });
|
|
111
|
+
if (signal.aborted)
|
|
112
|
+
return Promise.resolve(stopped());
|
|
113
|
+
return new Promise((resolve, reject) => {
|
|
114
|
+
const onStop = () => resolve(stopped());
|
|
115
|
+
signal.addEventListener("abort", onStop, { once: true });
|
|
116
|
+
reading.then((read) => (signal.removeEventListener("abort", onStop), resolve(read)), (error) => (signal.removeEventListener("abort", onStop), reject(error)));
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/** The part (`offset`, `maxChars`) of the page at `url` as a WebPage: from `pages`, the run's, where it holds the URL
|
|
120
|
+
* (read, or being read), with no request of its own; else read once (readOnce), kept there from the read's start. A read
|
|
121
|
+
* that fails leaves `pages`, so a later call reads again. The read is the run's, not a caller's: no caller's signal
|
|
122
|
+
* reaches it (it keeps Parallel's and the fetch's own time limits), so one caller's stop never fails another's; each
|
|
123
|
+
* caller stops waiting at its own signal (untilStopped). */
|
|
124
|
+
export async function webFetch(url, { parallel, firecrawl, pages, order = FETCH_ORDER, guard, checkMs = TIMEOUT_MS, offset, maxChars, signal, log, tried, noParallel, ...options } = {}) {
|
|
125
|
+
const named = typeof url === "string" ? url : undefined;
|
|
126
|
+
let reading = named === undefined ? undefined : pages?.get(named);
|
|
127
|
+
if (!reading) {
|
|
128
|
+
const started = readOnce(url, { parallel, firecrawl, order, guard, checkMs, options, log, tried, noParallel });
|
|
129
|
+
if (pages && named !== undefined) {
|
|
130
|
+
// Ahead of any caller's await, so no call sees a failed read kept.
|
|
131
|
+
const drop = () => void (pages.get(named) === started && pages.delete(named));
|
|
132
|
+
void started.then((read) => { settled.add(started); if (!read.ok)
|
|
133
|
+
drop(); trim(pages); }, () => { settled.add(started); drop(); });
|
|
134
|
+
}
|
|
135
|
+
reading = started;
|
|
136
|
+
}
|
|
137
|
+
if (pages && named !== undefined) {
|
|
138
|
+
// The last used last: the oldest goes past PAGES_HELD.
|
|
139
|
+
pages.delete(named);
|
|
140
|
+
pages.set(named, reading);
|
|
141
|
+
trim(pages);
|
|
142
|
+
}
|
|
143
|
+
const read = await untilStopped(url, reading, signal);
|
|
144
|
+
if (!read.ok)
|
|
145
|
+
return read;
|
|
146
|
+
const { via, parallel: why, ...page } = read;
|
|
147
|
+
return { ...partOfRead(page, { offset, maxChars }), via, ...(why ? { parallel: why } : {}) };
|
|
148
|
+
}
|
|
149
|
+
/** What the part no part follows says of a page whose HTML readPage cut before it was read (html_chars). */
|
|
150
|
+
const CUT_LINE = `The page was cut at ${HTML_READ_CHARS.toLocaleString("en-US")} characters of HTML before reading; the rest wasn't read.`;
|
|
151
|
+
/** Where a part of a page is in it, for the model: which characters of how many, and the call that reads on; the end of
|
|
152
|
+
* the page, or where it was cut before it was read; or that the offset is past it. Nothing for a page read whole, or
|
|
153
|
+
* one that was not read. */
|
|
154
|
+
function partLine(page, name) {
|
|
155
|
+
if (!page.ok || page.chars === undefined)
|
|
156
|
+
return undefined;
|
|
157
|
+
const start = page.offset ?? 0;
|
|
158
|
+
const end = start + page.chars;
|
|
159
|
+
if (page.truncated)
|
|
160
|
+
return `Characters ${start}-${end} of ${page.original_chars ?? "a longer page"}: call ${name} again with offset=${page.next_offset ?? end} for the rest.`;
|
|
161
|
+
// No part follows, and the page was cut before it was read: its text ends at the cut, not at the page's end.
|
|
162
|
+
if (page.html_chars !== undefined)
|
|
163
|
+
return CUT_LINE;
|
|
164
|
+
if (page.beyond_read)
|
|
165
|
+
return `Characters ${start}-${end}${page.original_chars === undefined ? "" : ` of ${page.original_chars}`}: the page goes on, but web_fetch reads only a page's first ${PAGE_READ_MAX_CHARS.toLocaleString("en-US")} characters.`;
|
|
166
|
+
if (page.original_chars === undefined)
|
|
167
|
+
return undefined;
|
|
168
|
+
if (page.chars === 0)
|
|
169
|
+
return `Nothing at offset=${start}: the page has ${page.original_chars} characters.`;
|
|
170
|
+
return `Characters ${start}-${end} of ${page.original_chars}: the end of the page.`;
|
|
171
|
+
}
|
|
172
|
+
/** A page as the tool `name` answers the model: its JSON, then a line saying why Parallel gave none where it was picked
|
|
173
|
+
* (and that the page was fetched directly, only where it was), and one saying where this part is in the page when it
|
|
174
|
+
* is not all of it. */
|
|
175
|
+
export function webFetchText(page, name = "web_fetch") {
|
|
176
|
+
const lines = [JSON.stringify(page)];
|
|
177
|
+
if ("parallel" in page && page.parallel)
|
|
178
|
+
lines.push(`Parallel was picked for web_fetch and gave no page (${page.parallel})${page.via === "direct" ? "; this one was fetched directly" : ""}.`);
|
|
179
|
+
const where = partLine(page, name);
|
|
180
|
+
if (where)
|
|
181
|
+
lines.push(where);
|
|
182
|
+
return lines.join("\n");
|
|
183
|
+
}
|