@parcha/agentrun-serve 0.1.0-beta.55 → 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/CHANGELOG.md +261 -0
- package/README.md +228 -45
- package/dist/acp-package.d.ts +2 -0
- package/dist/adapters.d.ts +18 -5
- package/dist/adapters.js +23 -9
- package/dist/agent-package.d.ts +3 -3
- package/dist/agent-package.js +17 -8
- package/dist/answer-json.d.ts +6 -2
- package/dist/answer-json.js +22 -4
- package/dist/api-keys.d.ts +76 -0
- package/dist/api-keys.js +217 -0
- package/dist/api-only.d.ts +17 -5
- package/dist/api-only.js +36 -8
- package/dist/browser-hold.d.ts +23 -0
- package/dist/browser-hold.js +48 -0
- package/dist/browser-person.d.ts +3 -0
- package/dist/browser-person.js +4 -1
- package/dist/calendar/tools.js +3 -2
- package/dist/chatgpt-plan.d.ts +28 -3
- package/dist/chatgpt-plan.js +182 -21
- package/dist/claude-login.d.ts +101 -14
- package/dist/claude-login.js +232 -49
- package/dist/claude-sign-in.d.ts +46 -0
- package/dist/claude-sign-in.js +93 -0
- package/dist/cli.js +63 -6
- package/dist/composio-made.d.ts +4 -0
- package/dist/composio-made.js +4 -0
- package/dist/composio-preset.d.ts +43 -2
- package/dist/composio-preset.js +92 -4
- package/dist/computer-held-routes.d.ts +5 -4
- package/dist/computer-held-routes.js +29 -8
- package/dist/computer-held.d.ts +6 -1
- package/dist/computer-held.js +14 -0
- package/dist/computer-record.js +4 -2
- package/dist/computer-viewer.js +4 -1
- package/dist/console/assets/{backend-D5EmB4N3.js → backend-DVhHzJX2.js} +19 -19
- package/dist/console/assets/index-Bwn1oTem.css +1 -0
- package/dist/console/assets/index-okP-y8PK.js +747 -0
- package/dist/console/index.html +2 -2
- package/dist/decisions.d.ts +1 -1
- package/dist/decisions.js +1 -1
- package/dist/docker-host-addresses.d.ts +22 -11
- package/dist/docker-host-addresses.js +38 -26
- package/dist/env-switch.d.ts +10 -0
- package/dist/env-switch.js +33 -2
- package/dist/errors.d.ts +1 -0
- package/dist/errors.js +2 -0
- package/dist/fal-price.d.ts +58 -20
- package/dist/fal-price.js +341 -82
- package/dist/handed-values.d.ts +43 -1
- package/dist/handed-values.js +119 -2
- package/dist/harness-archive.d.ts +13 -10
- package/dist/harness-archive.js +60 -34
- package/dist/harness-check.d.ts +3 -1
- package/dist/harness-check.js +25 -46
- package/dist/harness-keys.d.ts +73 -27
- package/dist/harness-keys.js +194 -61
- package/dist/harness-recipe-entry.d.ts +8 -7
- package/dist/harness-recipe-entry.js +21 -32
- package/dist/harness-recipe.d.ts +38 -25
- package/dist/harness-recipe.js +49 -36
- package/dist/harness-sign-in.js +12 -23
- package/dist/harness-table.d.ts +10 -3
- package/dist/harness-table.js +26 -21
- package/dist/harness.d.ts +1 -0
- package/dist/harness.js +17 -5
- package/dist/host-check.d.ts +4 -0
- package/dist/host-check.js +4 -0
- package/dist/hosted-search.d.ts +1 -1
- package/dist/hosted-search.js +1 -2
- package/dist/import-worker.d.ts +4 -2
- package/dist/import-worker.js +9 -2
- package/dist/in-flight.d.ts +26 -0
- package/dist/in-flight.js +52 -0
- package/dist/key-refusal.d.ts +26 -5
- package/dist/key-refusal.js +313 -37
- package/dist/learn/secrets.d.ts +31 -11
- package/dist/learn/secrets.js +152 -191
- package/dist/learning-cost.d.ts +6 -3
- package/dist/learning-cost.js +4 -2
- package/dist/link-check.js +1 -1
- package/dist/llms.txt +2 -2
- package/dist/marketplace.d.ts +77 -0
- package/dist/marketplace.js +224 -0
- package/dist/mcp-api-only-origins.d.ts +30 -0
- package/dist/mcp-api-only-origins.js +57 -0
- package/dist/mcp-gateway.d.ts +7 -5
- package/dist/mcp-gateway.js +9 -12
- package/dist/mcp-secret-writes.js +5 -3
- package/dist/mcp-servers-store.d.ts +28 -6
- package/dist/mcp-servers-store.js +53 -4
- package/dist/media-duration.d.ts +3 -0
- package/dist/media-duration.js +90 -9
- package/dist/model-form.d.ts +5 -4
- package/dist/model-form.js +11 -7
- package/dist/model.d.ts +5 -2
- package/dist/model.js +9 -5
- package/dist/ollama-provider.d.ts +7 -1
- package/dist/ollama-provider.js +14 -2
- package/dist/openapi.d.ts +3 -3
- package/dist/openapi.js +245 -42
- package/dist/otel/genai.d.ts +65 -0
- package/dist/otel/genai.js +116 -0
- package/dist/otel/tracer.d.ts +55 -0
- package/dist/otel/tracer.js +308 -0
- package/dist/page.d.ts +2 -0
- package/dist/page.js +3 -1
- package/dist/paid-calls.d.ts +1 -32
- package/dist/paid-calls.js +4 -39
- package/dist/place-offers.d.ts +14 -6
- package/dist/place-offers.js +26 -13
- package/dist/provider.d.ts +9 -0
- package/dist/provider.js +50 -3
- package/dist/redact/match.d.ts +101 -0
- package/dist/redact/match.js +1152 -0
- package/dist/refusal-words.d.ts +11 -0
- package/dist/refusal-words.js +11 -0
- package/dist/run-census.d.ts +67 -0
- package/dist/run-census.js +240 -0
- package/dist/run-computer.d.ts +6 -3
- package/dist/run-computer.js +4 -1
- package/dist/run-cut.d.ts +15 -6
- package/dist/run-cut.js +30 -9
- package/dist/run-files.d.ts +3 -0
- package/dist/run-files.js +3 -0
- package/dist/run-launch.d.ts +12 -1
- package/dist/run-launch.js +9 -10
- package/dist/run-learning-served.d.ts +2 -0
- package/dist/run-model-key.d.ts +8 -0
- package/dist/run-model-key.js +31 -1
- package/dist/run-reads-over-store.d.ts +24 -6
- package/dist/run-reads-over-store.js +67 -23
- package/dist/run-recordings.d.ts +5 -1
- package/dist/run-recordings.js +39 -14
- package/dist/run-timeline-served.d.ts +3 -1
- package/dist/run-timeline-served.js +4 -4
- package/dist/run-usage.d.ts +1 -0
- package/dist/runnable.d.ts +10 -1
- package/dist/runnable.js +20 -8
- package/dist/runner/adapter-loop.d.ts +14 -0
- package/dist/runner/adapter-loop.js +56 -11
- package/dist/runner/adapter-run-spec.d.ts +45 -8
- package/dist/runner/adapter-run-spec.js +154 -29
- package/dist/runner/adapters/pi.js +25 -3
- package/dist/runner/beside.js +11 -6
- package/dist/runner/big-writes.js +1 -10
- package/dist/runner/claim-secrets.d.ts +18 -3
- package/dist/runner/claim-secrets.js +35 -6
- package/dist/runner/cli-run-spec.d.ts +6 -0
- package/dist/runner/cli-run-spec.js +85 -36
- package/dist/runner/computer-journal.d.ts +9 -1
- package/dist/runner/computer-journal.js +26 -0
- package/dist/runner/conformance/harness.js +1 -1
- package/dist/runner/feedback-round.js +3 -0
- package/dist/runner/harness-loops.d.ts +6 -5
- package/dist/runner/harness-loops.js +7 -7
- package/dist/runner/held.d.ts +11 -5
- package/dist/runner/journal-kinds.d.ts +5 -0
- package/dist/runner/journal-kinds.js +5 -0
- package/dist/runner/loop-contract.d.ts +4 -0
- package/dist/runner/main.js +15 -6
- package/dist/runner/mcp-claim.js +30 -3
- package/dist/runner/mcp-run.d.ts +12 -0
- package/dist/runner/mcp-run.js +12 -5
- package/dist/runner/model-route.d.ts +17 -0
- package/dist/runner/model-route.js +120 -2
- package/dist/runner/presence.d.ts +4 -3
- package/dist/runner/presence.js +4 -2
- package/dist/runner/run-child-host.d.ts +5 -0
- package/dist/runner/run-child-host.js +71 -30
- package/dist/runner/run-child.js +5 -2
- package/dist/runner/runner.d.ts +18 -3
- package/dist/runner/runner.js +162 -36
- package/dist/runner/store-port.d.ts +1 -1
- package/dist/runner/store-secrets.d.ts +6 -0
- package/dist/runner/store-secrets.js +49 -7
- package/dist/runner/subagent.js +4 -40
- package/dist/runner/workflow-first.d.ts +11 -4
- package/dist/runner/workflow-first.js +14 -8
- package/dist/runner/workflow-spec.js +3 -2
- package/dist/runs-over-store.d.ts +15 -6
- package/dist/runs-over-store.js +33 -21
- package/dist/scrub-command.d.ts +73 -0
- package/dist/scrub-command.js +307 -0
- package/dist/secret-cut.d.ts +73 -0
- package/dist/secret-cut.js +291 -4
- package/dist/secret-rules.d.ts +143 -0
- package/dist/secret-rules.js +2564 -0
- package/dist/secrets.d.ts +7 -0
- package/dist/secrets.js +20 -0
- package/dist/server-keys.d.ts +20 -0
- package/dist/server-keys.js +25 -6
- package/dist/server.d.ts +57 -13
- package/dist/server.js +371 -113
- package/dist/skills/agentrun-serve/SKILL.md +4 -0
- package/dist/store/dialect.d.ts +3 -0
- package/dist/store/dialect.js +1 -0
- package/dist/store/index.d.ts +1 -1
- package/dist/store/index.js +1 -1
- package/dist/store/journal/storage.js +1 -1
- package/dist/store/postgres/dialect.js +3 -0
- package/dist/store/postgres/migrations/0027_run_launch_census.sql +31 -0
- package/dist/store/schema.d.ts +6 -0
- package/dist/store/schema.js +11 -1
- package/dist/store/seal.d.ts +6 -1
- package/dist/store/seal.js +5 -3
- package/dist/store/sqlite/run-launch.d.ts +9 -1
- package/dist/store/sqlite/run-launch.js +24 -13
- package/dist/store/sqlite/run-patch.d.ts +1 -1
- package/dist/store/sqlite/run-patch.js +13 -4
- package/dist/store/sqlite/store.js +76 -16
- package/dist/store/store.d.ts +53 -3
- package/dist/store/store.js +27 -0
- package/dist/store-reads/learning.d.ts +6 -3
- package/dist/store-reads/learning.js +22 -7
- package/dist/store-view.d.ts +3 -0
- package/dist/store-view.js +4 -4
- package/dist/suggestions.js +3 -2
- package/dist/template-eval.d.ts +22 -11
- package/dist/template-eval.js +124 -41
- package/dist/template-import.d.ts +10 -0
- package/dist/template-import.js +16 -7
- package/dist/templates.d.ts +10 -2
- package/dist/templates.js +28 -8
- package/dist/timeline/index.d.ts +1 -1
- package/dist/timeline/index.js +1 -1
- package/dist/timeline/labels.d.ts +6 -4
- package/dist/timeline/labels.js +18 -6
- package/dist/timeline/timeline.d.ts +17 -7
- package/dist/timeline/timeline.js +28 -14
- package/dist/timeline/util.js +1 -1
- package/dist/timeline/web-calls.d.ts +26 -9
- package/dist/timeline/web-calls.js +124 -37
- package/dist/tool-gate.d.ts +42 -3
- package/dist/tool-gate.js +69 -6
- package/dist/tool-keys-store.d.ts +2 -4
- package/dist/tool-keys-store.js +5 -21
- package/dist/tool-keys.d.ts +2 -2
- package/dist/tool-keys.js +2 -2
- package/dist/tool-list.js +1 -1
- package/dist/tool-plane.d.ts +2 -2
- package/dist/tool-plane.js +35 -33
- package/dist/tools.js +13 -7
- package/dist/trigger-targets.d.ts +4 -0
- package/dist/trigger-targets.js +9 -2
- package/dist/triggers.d.ts +67 -4
- package/dist/triggers.js +171 -22
- package/dist/until.d.ts +3 -0
- package/dist/until.js +11 -0
- package/dist/validate.js +1 -1
- package/dist/web-fake.d.ts +1 -3
- package/dist/web-fake.js +7 -18
- package/dist/web-fetch.d.ts +6 -72
- package/dist/web-fetch.js +11 -265
- package/dist/web-run.d.ts +31 -0
- package/dist/web-run.js +90 -0
- package/dist/web-save.d.ts +6 -4
- package/dist/web-save.js +9 -12
- package/dist/web-search.d.ts +9 -127
- package/dist/web-search.js +23 -259
- package/dist/wires.d.ts +13 -0
- package/dist/wires.js +11 -0
- package/dist/workflow-run.d.ts +5 -4
- package/dist/workflow-run.js +5 -4
- package/dist/workspace/computer-deps.d.ts +3 -1
- package/dist/workspace/computer-deps.js +12 -4
- package/dist/workspace/computer-for-claim.d.ts +19 -2
- package/dist/workspace/computer-for-claim.js +101 -13
- package/dist/workspace/computer-janitor.d.ts +15 -7
- package/dist/workspace/computer-janitor.js +33 -8
- package/dist/workspace/computer-kept.d.ts +55 -4
- package/dist/workspace/computer-kept.js +129 -8
- package/dist/workspace/index.d.ts +1 -1
- package/dist/workspace/index.js +1 -1
- package/dist/workspace/kind-skills.d.ts +4 -0
- package/dist/workspace/kind-skills.js +9 -0
- package/dist/workspace/run-end-computers.d.ts +5 -1
- package/dist/workspace/run-end-computers.js +27 -1
- package/dist/workspace/synced-folder.js +3 -4
- package/dist/wrapped.d.ts +65 -0
- package/dist/wrapped.js +77 -0
- package/package.json +17 -11
- package/skills/agentrun-serve/SKILL.md +4 -0
- package/templates/README.md +38 -15
- package/dist/console/assets/index-DUYLaqiR.js +0 -746
- package/dist/console/assets/index-DYhU4adm.css +0 -1
- package/dist/exa-keyless.d.ts +0 -34
- package/dist/exa-keyless.js +0 -118
- package/dist/firecrawl-keyless.d.ts +0 -41
- package/dist/firecrawl-keyless.js +0 -152
- package/dist/keyless-mcp.d.ts +0 -36
- package/dist/keyless-mcp.js +0 -127
- package/dist/text-urls.d.ts +0 -38
- package/dist/text-urls.js +0 -170
- package/dist/web-fetch-direct.d.ts +0 -119
- package/dist/web-fetch-direct.js +0 -190
package/dist/web-fetch.js
CHANGED
|
@@ -1,277 +1,23 @@
|
|
|
1
1
|
import { defineTool } from "@earendil-works/pi-durable";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { askedUrl, HTML_READ_CHARS, MAX_CHARS_MIN, PAGE_CHARS, PAGE_READ_MAX_CHARS, partProblem, TEXT_CAP, webFetchText } from "@parcha/agentrun-web";
|
|
3
|
+
import { fetchOptions, URL_PARAMETERS, webOf } from "./web-run.js";
|
|
4
4
|
import { FAUX_ENV } from "./model.js";
|
|
5
|
-
import { PaidRefused, paidRequest, parallelFeeOf } from "./paid-calls.js";
|
|
6
|
-
import { logLine } from "./print.js";
|
|
7
5
|
import { forModel } from "./result.js";
|
|
8
6
|
import { isUnavailable, planeOf, whyUnavailable } from "./tool-plane.js";
|
|
9
7
|
export const WEB_FETCH_TOOL = "web_fetch";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const PORTS = new Set(["", "80", "443"]);
|
|
13
|
-
/** How many pages a run keeps what it read of (Pages): each at most PAGE_READ_MAX_CHARS characters of text. */
|
|
14
|
-
export const PAGES_HELD = 8;
|
|
15
|
-
/** Parallel's extract on `apiKey`: one URL a request, each a billed extract, its full content to the most of a page
|
|
16
|
-
* web_fetch reads as the page's text (markdown), its links read from it, status 200 (Parallel answers a page it could
|
|
17
|
-
* not read in `errors`, with no content). */
|
|
18
|
-
export function parallelExtract(apiKey, send = fetch) {
|
|
19
|
-
let refused = false;
|
|
20
|
-
// Each request is a paid one (paid-calls.ts): held by the run's cap and booked as the call's cost at its endpoint's
|
|
21
|
-
// price. One the cap refuses is not sent: it throws its refusal (PaidRefused).
|
|
22
|
-
const post = async (url, body, signal) => {
|
|
23
|
-
const sent = async () => {
|
|
24
|
-
const response = await send(url, {
|
|
25
|
-
method: "POST",
|
|
26
|
-
headers: { "x-api-key": apiKey, "content-type": "application/json" },
|
|
27
|
-
body: JSON.stringify(body),
|
|
28
|
-
signal: AbortSignal.any([AbortSignal.timeout(PARALLEL_TIMEOUT_MS), ...(signal ? [signal] : [])]),
|
|
29
|
-
});
|
|
30
|
-
if (response.status === 401 || response.status === 403)
|
|
31
|
-
refused = true;
|
|
32
|
-
return response;
|
|
33
|
-
};
|
|
34
|
-
const fee = parallelFeeOf(url, body);
|
|
35
|
-
if (!fee)
|
|
36
|
-
throw new PaidRefused(`Parallel's ${URL.parse(url)?.pathname ?? url} was not sent: it has no price here`);
|
|
37
|
-
return paidRequest(fee, sent);
|
|
38
|
-
};
|
|
39
|
-
return {
|
|
40
|
-
live: () => !refused,
|
|
41
|
-
post,
|
|
42
|
-
async read(url, signal) {
|
|
43
|
-
try {
|
|
44
|
-
// The most of a page a read reaches and one character more, so a page Parallel cut there shows as going on.
|
|
45
|
-
const asked = PAGE_READ_MAX_CHARS + 1;
|
|
46
|
-
const response = await post(PARALLEL_EXTRACT_URL, { urls: [url], advanced_settings: { full_content: { max_chars_per_result: asked } } }, signal);
|
|
47
|
-
if (!response.ok) {
|
|
48
|
-
await response.body?.cancel().catch(() => undefined);
|
|
49
|
-
return response.status === 401 || response.status === 403 ? `refused the key (${response.status})` : `answered ${response.status}`;
|
|
50
|
-
}
|
|
51
|
-
const { results, errors } = (await response.json());
|
|
52
|
-
const row = results?.find((r) => typeof r?.url === "string" && typeof r.full_content === "string" && r.full_content.trim() !== "");
|
|
53
|
-
if (!row)
|
|
54
|
-
return errors?.length ? "listed the URL in errors" : "answered no content";
|
|
55
|
-
const text = row.full_content;
|
|
56
|
-
return { ok: true, url: clip(row.url, URL_MAX), status: 200, title: clip(typeof row.title === "string" ? row.title : "", TITLE_CAP), ...textOf(text, text.length < asked), ...linksWithin(linksOf(text)) };
|
|
57
|
-
}
|
|
58
|
-
catch (error) {
|
|
59
|
-
if (error instanceof PaidRefused)
|
|
60
|
-
return error.message;
|
|
61
|
-
const name = error?.name;
|
|
62
|
-
return name === "TimeoutError" ? "timed out" : name === "AbortError" ? "was stopped" : `failed: ${String(error?.message ?? error).slice(0, 200)}`;
|
|
63
|
-
}
|
|
64
|
-
},
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
/** The server's log, where a web_fetch notes what the model is not told (browser-cost.ts' lines go there too). */
|
|
68
|
-
const serverLog = (line) => void process.stderr.write(`agentrun-serve: ${line}\n`);
|
|
69
|
-
/** Whether the public-host guard passes `url` within `ms` and the caller's signal: the guarded fetch's port check and its
|
|
70
|
-
* refusal of a user name or password in the URL (readDirect's), then publicFetch's own checks of the first hop (scheme,
|
|
71
|
-
* name, every address it resolves to), stopped before it dials. A
|
|
72
|
-
* check that runs out of time (a resolver that does not answer) does not pass; publicFetch ends a lookup at the abort. */
|
|
73
|
-
async function passes(url, options, ms) {
|
|
74
|
-
const target = URL.parse(url);
|
|
75
|
-
if (!target || !PORTS.has(target.port) || url.length > URL_MAX || target.username || target.password)
|
|
76
|
-
return false;
|
|
77
|
-
const { publicFetch } = await import("@parcha/pi-browser");
|
|
78
|
-
const checked = new Error("checked, not dialled");
|
|
79
|
-
const signal = AbortSignal.any([AbortSignal.timeout(ms), ...(options.signal ? [options.signal] : [])]);
|
|
80
|
-
return publicFetch(url, { ...options, signal, fetchImpl: async () => { throw checked; } }).then(() => false, (error) => error === checked);
|
|
81
|
-
}
|
|
82
|
-
/** The page at `url` read once: through `firecrawl` or `parallel` when it is given and live and the guard passes the URL
|
|
83
|
-
* within `checkMs` (default the guarded fetch's 30 s), else (or when it answers no page) through the guarded fetch. A
|
|
84
|
-
* page fetched directly where `parallel` was given says why Parallel gave none (`parallel`): a picked provider that
|
|
85
|
-
* fails is never a silent fallback. Keyless Firecrawl is a free service with the direct fetch behind it, by design. */
|
|
86
|
-
async function readOnce(url, parallel, firecrawl, checkMs, options, log) {
|
|
87
|
-
let firecrawlSaid;
|
|
88
|
-
if (firecrawl?.live() && typeof url === "string" && (await passes(url, options, checkMs))) {
|
|
89
|
-
const read = await firecrawl.read(url, options.signal);
|
|
90
|
-
if (typeof read !== "string")
|
|
91
|
-
return { ...read, via: "firecrawl-keyless" };
|
|
92
|
-
firecrawlSaid = read;
|
|
93
|
-
}
|
|
94
|
-
let why;
|
|
95
|
-
if (parallel && !parallel.live())
|
|
96
|
-
why = "not tried: refused earlier in this run";
|
|
97
|
-
else if (parallel && !(typeof url === "string" && (await passes(url, options, checkMs))))
|
|
98
|
-
why = "not tried: the URL did not pass the public-host guard";
|
|
99
|
-
else if (parallel) {
|
|
100
|
-
const read = await parallel.read(url, options.signal);
|
|
101
|
-
if (typeof read !== "string")
|
|
102
|
-
return { ...read, via: "parallel" };
|
|
103
|
-
why = read;
|
|
104
|
-
}
|
|
105
|
-
const direct = await readDirect(url, options);
|
|
106
|
-
// Why Firecrawl gave no page goes to the server's log, never to the model (the page is the direct fetch's); never the URL.
|
|
107
|
-
if (firecrawlSaid !== undefined)
|
|
108
|
-
log(logLine("web_fetch_firecrawl_failed", { said: firecrawlSaid.slice(0, 300), direct: direct.ok ? "read" : "failed" }));
|
|
109
|
-
return { ...direct, via: "direct", ...(why ? { parallel: why } : {}) };
|
|
110
|
-
}
|
|
111
|
-
/** The reads that have ended: only these leave `pages` past its bound (trim). */
|
|
112
|
-
const settled = new WeakSet();
|
|
113
|
-
/** `pages` down to PAGES_HELD reads that have ENDED, dropping the least recently used of those first. Only ended reads
|
|
114
|
-
* count toward the bound, and only they are dropped: a read under way stays until it ends (a caller may be about to
|
|
115
|
-
* share it, and dropping it would read and bill the page again), and it never makes room by pushing out a page that has
|
|
116
|
-
* ended. So the run holds at most PAGES_HELD ended reads plus the ones still under way, and trims again as each ends. */
|
|
117
|
-
function trim(pages) {
|
|
118
|
-
let ended = 0;
|
|
119
|
-
for (const read of pages.values())
|
|
120
|
-
if (settled.has(read))
|
|
121
|
-
ended++;
|
|
122
|
-
for (const [held, read] of pages) {
|
|
123
|
-
if (ended <= PAGES_HELD)
|
|
124
|
-
return;
|
|
125
|
-
if (settled.has(read))
|
|
126
|
-
(pages.delete(held), ended--);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
/** `reading`, or, as soon as `signal` stops this call, a failed page saying so (as a stopped read reads): the read
|
|
130
|
-
* itself goes on for the run's other callers of the URL, within its own time limits. */
|
|
131
|
-
function untilStopped(url, reading, signal) {
|
|
132
|
-
if (!signal)
|
|
133
|
-
return reading;
|
|
134
|
-
// No page came for this call, by any path: never "direct", which says the host was fetched.
|
|
135
|
-
const stopped = () => ({ ok: false, url: typeof url === "string" ? clip(url, URL_MAX) : "", status: 0, title: "", text: "", links: [], error: "the call was stopped before the page arrived", via: "stopped" });
|
|
136
|
-
if (signal.aborted)
|
|
137
|
-
return Promise.resolve(stopped());
|
|
138
|
-
return new Promise((resolve, reject) => {
|
|
139
|
-
const onStop = () => resolve(stopped());
|
|
140
|
-
signal.addEventListener("abort", onStop, { once: true });
|
|
141
|
-
reading.then((read) => (signal.removeEventListener("abort", onStop), resolve(read)), (error) => (signal.removeEventListener("abort", onStop), reject(error)));
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
/** The part (`offset`, `maxChars`) of the page at `url` as a WebPage: from `pages`, the run's, where it holds the URL
|
|
145
|
-
* (read, or being read), with no request of its own; else read once (readOnce), kept there from the read's start. A read
|
|
146
|
-
* that fails leaves `pages`, so a later call reads again. The read is the run's, not a caller's: no caller's signal
|
|
147
|
-
* reaches it (it keeps Parallel's and the fetch's own time limits), so one caller's stop never fails another's; each
|
|
148
|
-
* caller stops waiting at its own signal (untilStopped). */
|
|
149
|
-
export async function webFetch(url, { parallel, firecrawl, pages, checkMs = TIMEOUT_MS, offset, maxChars, signal, log = serverLog, ...options } = {}) {
|
|
150
|
-
const named = typeof url === "string" ? url : undefined;
|
|
151
|
-
let reading = named === undefined ? undefined : pages?.get(named);
|
|
152
|
-
if (!reading) {
|
|
153
|
-
const started = readOnce(url, parallel, firecrawl, checkMs, options, log);
|
|
154
|
-
if (pages && named !== undefined) {
|
|
155
|
-
// Ahead of any caller's await, so no call sees a failed read kept.
|
|
156
|
-
const drop = () => void (pages.get(named) === started && pages.delete(named));
|
|
157
|
-
void started.then((read) => { settled.add(started); if (!read.ok)
|
|
158
|
-
drop(); trim(pages); }, () => { settled.add(started); drop(); });
|
|
159
|
-
}
|
|
160
|
-
reading = started;
|
|
161
|
-
}
|
|
162
|
-
if (pages && named !== undefined) {
|
|
163
|
-
// The last used last: the oldest goes past PAGES_HELD.
|
|
164
|
-
pages.delete(named);
|
|
165
|
-
pages.set(named, reading);
|
|
166
|
-
trim(pages);
|
|
167
|
-
}
|
|
168
|
-
const read = await untilStopped(url, reading, signal);
|
|
169
|
-
if (!read.ok)
|
|
170
|
-
return read;
|
|
171
|
-
const { via, parallel: why, ...page } = read;
|
|
172
|
-
return { ...partOfRead(page, { offset, maxChars }), via, ...(why ? { parallel: why } : {}) };
|
|
173
|
-
}
|
|
174
|
-
/** Each run's Parallel extract, by the environment its calls are given: one object per run (main's run process hands
|
|
175
|
-
* process.env; the runner hands each claim its own, claim-secrets.ts), so one claim's key or client never serves another. */
|
|
176
|
-
const parallels = new WeakMap();
|
|
177
|
-
/** Each run's pages, by the environment its calls are given (one object per run: main's run process hands process.env,
|
|
178
|
-
* the runner hands each claim its own), so the pages one run read never serve another's. */
|
|
179
|
-
const pagesByRun = new WeakMap();
|
|
180
|
-
/** The pages the run whose calls are given `env` read (Pages): the agent's tool and a call node's web_fetch of that run
|
|
181
|
-
* share them, another run's never. */
|
|
182
|
-
export function pagesOf(env) {
|
|
183
|
-
let pages = pagesByRun.get(env);
|
|
184
|
-
if (!pages)
|
|
185
|
-
pagesByRun.set(env, (pages = new Map()));
|
|
186
|
-
return pages;
|
|
187
|
-
}
|
|
188
|
-
/** Each run's keyless Firecrawl scrape (and its breaker), by the environment its calls are given, as parallels. */
|
|
189
|
-
const firecrawls = new WeakMap();
|
|
190
|
-
/** The run's keyless Firecrawl scrape, made once for the environment its calls are given, so its breaker holds for the
|
|
191
|
-
* rest of the run (firecrawlFetch). Under --faux it asks the fake whose URL FAUX_FIRECRAWL_ENV names, and with none
|
|
192
|
-
* named it is null: a test never reaches the network. Its module loads at the first call (it reads web-search.ts,
|
|
193
|
-
* which reads this module as it loads). */
|
|
194
|
-
export function firecrawlOf(env) {
|
|
195
|
-
let made = firecrawls.get(env);
|
|
196
|
-
if (!made) {
|
|
197
|
-
made = import("./firecrawl-keyless.js").then(({ FAUX_FIRECRAWL_ENV, firecrawlFetch }) => {
|
|
198
|
-
const fake = env[FAUX_ENV] ? env[FAUX_FIRECRAWL_ENV] : undefined;
|
|
199
|
-
return env[FAUX_ENV] && !fake ? null : firecrawlFetch(fake ? { endpoint: fake } : {});
|
|
200
|
-
});
|
|
201
|
-
firecrawls.set(env, made);
|
|
202
|
-
}
|
|
203
|
-
return made;
|
|
204
|
-
}
|
|
205
|
-
/** The run's Parallel extract, made once for the environment its calls are given, so a refused key holds for the rest of
|
|
206
|
-
* the run: on the Parallel key that environment holds (parallelKeyOf: the per-run pipe, or the claim's own), null
|
|
207
|
-
* without one. */
|
|
208
|
-
export function parallelOf(env) {
|
|
209
|
-
const made = parallels.get(env);
|
|
210
|
-
if (made !== undefined)
|
|
211
|
-
return made;
|
|
212
|
-
const key = parallelKeyOf(env);
|
|
213
|
-
const extract = key === undefined ? null : parallelExtract(key, async (...args) => ((await fauxFetch(env, FAUX_PARALLEL_ENV)) ?? fetch)(...args));
|
|
214
|
-
parallels.set(env, extract);
|
|
215
|
-
return extract;
|
|
216
|
-
}
|
|
217
|
-
/** The part `options` names of the page at `url` by the provider the run's plane resolves web_fetch to: Parallel's
|
|
218
|
-
* extract (with the direct fetch behind it) where it resolves to parallel, keyless Firecrawl's scrape (the same) where
|
|
219
|
-
* it resolves to firecrawl-keyless, the guarded direct fetch otherwise, and a
|
|
220
|
-
* failed Page saying why where it is unavailable or the part is one a call may not ask for (partProblem), before
|
|
221
|
-
* anything is fetched. From `pages` (the run's, pagesOf) where they hold the URL. */
|
|
8
|
+
/** The part `options` names of the page at `url`, through the run's Web; a failed Page saying why where web_fetch does
|
|
9
|
+
* not run here. A part a call may not ask for is refused first. */
|
|
222
10
|
export async function fetchFor(env, url, options = {}) {
|
|
223
|
-
const problem = partProblem(options);
|
|
224
|
-
if (problem)
|
|
225
|
-
return { ok: false, url: typeof url === "string" ? clip(url, URL_MAX) : "", status: 0, title: "", text: "", links: [], error: problem };
|
|
226
11
|
const resolved = planeOf(env).web_fetch;
|
|
227
|
-
if (isUnavailable(resolved))
|
|
228
|
-
return { ok: false, url:
|
|
229
|
-
|
|
230
|
-
const page = await webFetch(url, { ...options, parallel: extract, firecrawl: resolved === "firecrawl-keyless" ? await firecrawlOf(env) : null });
|
|
231
|
-
return resolved === "parallel" && !extract ? { ...page, parallel: "not tried: this run holds no Parallel key" } : page;
|
|
232
|
-
}
|
|
233
|
-
/** What the part no part follows says of a page whose HTML readPage cut before it was read (html_chars). */
|
|
234
|
-
const CUT_LINE = `The page was cut at ${HTML_READ_CHARS.toLocaleString("en-US")} characters of HTML before reading; the rest wasn't read.`;
|
|
235
|
-
/** 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
|
|
236
|
-
* 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
|
|
237
|
-
* one that was not read. */
|
|
238
|
-
function partLine(page, name) {
|
|
239
|
-
if (!page.ok || page.chars === undefined)
|
|
240
|
-
return undefined;
|
|
241
|
-
const start = page.offset ?? 0;
|
|
242
|
-
const end = start + page.chars;
|
|
243
|
-
if (page.truncated)
|
|
244
|
-
return `Characters ${start}-${end} of ${page.original_chars ?? "a longer page"}: call ${name} again with offset=${page.next_offset ?? end} for the rest.`;
|
|
245
|
-
// No part follows, and the page was cut before it was read: its text ends at the cut, not at the page's end.
|
|
246
|
-
if (page.html_chars !== undefined)
|
|
247
|
-
return CUT_LINE;
|
|
248
|
-
if (page.beyond_read)
|
|
249
|
-
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.`;
|
|
250
|
-
if (page.original_chars === undefined)
|
|
251
|
-
return undefined;
|
|
252
|
-
if (page.chars === 0)
|
|
253
|
-
return `Nothing at offset=${start}: the page has ${page.original_chars} characters.`;
|
|
254
|
-
return `Characters ${start}-${end} of ${page.original_chars}: the end of the page.`;
|
|
255
|
-
}
|
|
256
|
-
/** A page as the tool `name` answers the model: its JSON, then a line saying why Parallel gave none where it was picked,
|
|
257
|
-
* and one saying where this part is in the page when it is not all of it. */
|
|
258
|
-
export function webFetchText(page, name = WEB_FETCH_TOOL) {
|
|
259
|
-
const lines = [JSON.stringify(page)];
|
|
260
|
-
if ("parallel" in page && page.parallel)
|
|
261
|
-
lines.push(`Parallel was picked for web_fetch and gave no page (${page.parallel}); this one was fetched directly.`);
|
|
262
|
-
const where = partLine(page, name);
|
|
263
|
-
if (where)
|
|
264
|
-
lines.push(where);
|
|
265
|
-
return lines.join("\n");
|
|
12
|
+
if (isUnavailable(resolved) && !partProblem(options))
|
|
13
|
+
return { ok: false, url: askedUrl(url), status: 0, title: "", text: "", links: [], error: `web_fetch does not run here: ${whyUnavailable(resolved)}` };
|
|
14
|
+
return (await webOf(env).fetch(url, options)).answer;
|
|
266
15
|
}
|
|
267
|
-
/** The agent's tool
|
|
268
|
-
* run
|
|
269
|
-
/** Typed here, so its declaration names the schema type by a package serve declares (pi-ai), whatever else the build loads. */
|
|
270
|
-
/** `fake`: what reads each page in the provider's place (a template eval's web fake, web-fake.ts), the tool otherwise
|
|
271
|
-
* the same. */
|
|
16
|
+
/** The agent's tool, named `name`. A run whose web_fetch resolves to Parallel reads its key as the tool is made, when the
|
|
17
|
+
* run opens. `fake`: what reads each page in the provider's place (a template eval's web fake, web-fake.ts). */
|
|
272
18
|
export function webFetchTool(env, name = WEB_FETCH_TOOL, fake) {
|
|
273
19
|
if (planeOf(env).web_fetch === "parallel" && !fake)
|
|
274
|
-
|
|
20
|
+
webOf(env);
|
|
275
21
|
return defineTool({
|
|
276
22
|
name,
|
|
277
23
|
description: `Read one web page from a public host, a part at a time. It has no cookies, sign-ins or session: it reads one page cold, so a page behind a sign-in, or one that depends on an earlier page, needs the browser tool, where this run has it. Answers JSON: ok, the final url, the HTTP status, the page's title, text (a part of its readable text as markdown: about max_chars characters from offset, ${PAGE_CHARS.toLocaleString("en-US")} by default, at least ${MAX_CHARS_MIN} and at most ${TEXT_CAP.toLocaleString("en-US")}) and chars (that part's length), the absolute links the page holds (its link targets and the URLs written out in its text, a JSON body's strings and an XML feed's links among them, where an HTML page fetched directly gives its link targets alone; with the part at offset 0; links_omitted counts those its caps left out), and via: parallel when Parallel's extract read it, firecrawl-keyless when Firecrawl's keyless service read it (it is sent the URL), direct when it was fetched from the host (with parallel saying why, where Parallel was picked and gave no page), stopped when the call was stopped before its page arrived. A part that is not the whole page says so: truncated is true while more follows, next_offset is where the rest begins, original_chars is the page's whole length where it is known, and a line after the JSON says which characters these are; call again with offset to read on. A page whose HTML is longer than ${HTML_READ_CHARS.toLocaleString("en-US")} characters (scripts and styles aside) is cut before it is read: html_chars gives its whole length on every part, its text is only the start of the page, and the last part's line says it was cut. converted is "text" when the text is the page's plain text, its conversion to markdown having run out of time. A page is read to its first ${PAGE_READ_MAX_CHARS.toLocaleString("en-US")} characters (beyond_read on the last part says it goes on). A page is fetched once a run, at its first call: its later parts come from that read, with no request (through Parallel, one billed extract a page). A private or local address, a port other than 80 or 443, or a scheme other than http(s) is refused, never read and never sent to a third party (ok false, with refused saying why); so is a page fetched directly that is not text.`,
|
|
@@ -280,7 +26,7 @@ export function webFetchTool(env, name = WEB_FETCH_TOOL, fake) {
|
|
|
280
26
|
execute: async (args, _api, context) => {
|
|
281
27
|
const { url, offset, max_chars } = args;
|
|
282
28
|
const signal = context?.abortSignal ? { signal: context.abortSignal } : {};
|
|
283
|
-
const page = fake ? await fake(url, { offset, maxChars: max_chars, ...signal }) : await fetchFor(env, url, { ...(await fetchOptions(env, FAUX_ENV)), offset, maxChars: max_chars,
|
|
29
|
+
const page = fake ? await fake(url, { offset, maxChars: max_chars, ...signal }) : await fetchFor(env, url, { ...(await fetchOptions(env, FAUX_ENV)), offset, maxChars: max_chars, ...signal });
|
|
284
30
|
return forModel(webFetchText(page, name), { details: page });
|
|
285
31
|
},
|
|
286
32
|
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import type { TSchema } from "typebox";
|
|
3
|
+
import type { PublicFetchOptions } from "@parcha/pi-browser";
|
|
4
|
+
import { type FetchProvider, type Guard, type SearchProvider, type Web } from "@parcha/agentrun-web";
|
|
5
|
+
import { type SearchModels } from "./hosted-search.js";
|
|
6
|
+
/** pi-browser's guard and reader, loaded at the first fetch, so a run that never fetches never loads pi-browser. */
|
|
7
|
+
export declare const GUARD: Guard;
|
|
8
|
+
export declare const RATE_LIMITED = "Free web search is rate-limited right now. Try again in a minute; add the free Parallel Search server in Settings \u2192 MCP servers and grant the agent its web_search; or add a Parallel key in Settings \u2192 API Keys to search with a key.";
|
|
9
|
+
/** Under `--faux` only: a module whose default export gives `{ lookup, fetchImpl }` for the fetch. */
|
|
10
|
+
export declare const FAUX_WEB_ENV = "AGENTRUN_SERVE_FAUX_WEB";
|
|
11
|
+
/** Under `--faux` only: a module whose default export is the fetch keyless Exa's MCP exchange goes through. */
|
|
12
|
+
export declare const FAUX_EXA_ENV = "AGENTRUN_SERVE_FAUX_EXA";
|
|
13
|
+
/** Under `--faux` only: the URL of a fake MCP server asked in place of keyless Firecrawl. With none named, Firecrawl is
|
|
14
|
+
* never asked. */
|
|
15
|
+
export declare const FAUX_FIRECRAWL_ENV = "AGENTRUN_SERVE_FAUX_FIRECRAWL";
|
|
16
|
+
/** The fetch's resolver and transport: the faux module's under `--faux` with one named, else none. */
|
|
17
|
+
export declare function fetchOptions(env: NodeJS.ProcessEnv, fauxEnv: string): Promise<Omit<PublicFetchOptions, "maxBytes" | "signal">>;
|
|
18
|
+
export declare const URL_PARAMETERS: TSchema;
|
|
19
|
+
/** The run's models a native search goes through, asked when a search runs: ToolHost.search, EffectRun.search. */
|
|
20
|
+
export type SearchOn = () => Promise<SearchModels | undefined>;
|
|
21
|
+
/** The models of the search in flight. */
|
|
22
|
+
export declare const searchingOn: AsyncLocalStorage<SearchOn | undefined>;
|
|
23
|
+
/** The providers each web tool asks where the run's plane resolves it. Under --faux, a keyless provider the server names
|
|
24
|
+
* no fake for is left out, so a test never reaches the network. */
|
|
25
|
+
export declare function ordersOf(env: NodeJS.ProcessEnv): {
|
|
26
|
+
search: SearchProvider[];
|
|
27
|
+
fetch: FetchProvider[];
|
|
28
|
+
};
|
|
29
|
+
/** The run's Web. Where either web tool resolves to Parallel, the key is read here, once. A search's deadlines are its
|
|
30
|
+
* call's (web-search.ts), never the Web's. */
|
|
31
|
+
export declare function webOf(env: NodeJS.ProcessEnv): Web;
|
package/dist/web-run.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// Serve's one Web per run (webOf) and the parts it injects: pi-browser's guard and reader, the run's ledger as Pay,
|
|
2
|
+
// serve's words for a free search's limit, the server's log, and the `--faux` switches that put a test's fakes in each
|
|
3
|
+
// provider's place.
|
|
4
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
5
|
+
import { pathToFileURL } from "node:url";
|
|
6
|
+
import { createWeb, MAX_CHARS_MIN, PAGE_CHARS, TEXT_CAP } from "@parcha/agentrun-web";
|
|
7
|
+
import { FAUX_PARALLEL_ENV, fauxFetch, parallelKeyOf } from "./decisions.js";
|
|
8
|
+
import { hostedSearched } from "./hosted-search.js";
|
|
9
|
+
import { FAUX_ENV } from "./model.js";
|
|
10
|
+
import { paidRequest } from "./paid-calls.js";
|
|
11
|
+
import { logLine } from "./print.js";
|
|
12
|
+
import { planeOf } from "./tool-plane.js";
|
|
13
|
+
/** pi-browser's guard and reader, loaded at the first fetch, so a run that never fetches never loads pi-browser. */
|
|
14
|
+
export const GUARD = {
|
|
15
|
+
fetch: async (url, options) => (await import("@parcha/pi-browser")).publicFetch(url, options),
|
|
16
|
+
check: async (url, options) => (await import("@parcha/pi-browser")).publicCheck(url, options),
|
|
17
|
+
html: async (html, url, options) => (await import("@parcha/pi-browser")).readPage(html, url, options),
|
|
18
|
+
};
|
|
19
|
+
export const RATE_LIMITED = "Free web search is rate-limited right now. Try again in a minute; add the free Parallel Search server in Settings → MCP servers and grant the agent its web_search; or add a Parallel key in Settings → API Keys to search with a key.";
|
|
20
|
+
/** Under `--faux` only: a module whose default export gives `{ lookup, fetchImpl }` for the fetch. */
|
|
21
|
+
export const FAUX_WEB_ENV = "AGENTRUN_SERVE_FAUX_WEB";
|
|
22
|
+
/** Under `--faux` only: a module whose default export is the fetch keyless Exa's MCP exchange goes through. */
|
|
23
|
+
export const FAUX_EXA_ENV = "AGENTRUN_SERVE_FAUX_EXA";
|
|
24
|
+
/** Under `--faux` only: the URL of a fake MCP server asked in place of keyless Firecrawl. With none named, Firecrawl is
|
|
25
|
+
* never asked. */
|
|
26
|
+
export const FAUX_FIRECRAWL_ENV = "AGENTRUN_SERVE_FAUX_FIRECRAWL";
|
|
27
|
+
/** The fetch's resolver and transport: the faux module's under `--faux` with one named, else none. */
|
|
28
|
+
export async function fetchOptions(env, fauxEnv) {
|
|
29
|
+
const faux = env[fauxEnv] ? env[FAUX_WEB_ENV] : undefined;
|
|
30
|
+
if (!faux)
|
|
31
|
+
return {};
|
|
32
|
+
return (await import(pathToFileURL(faux).href)).default();
|
|
33
|
+
}
|
|
34
|
+
export const URL_PARAMETERS = {
|
|
35
|
+
type: "object",
|
|
36
|
+
additionalProperties: false,
|
|
37
|
+
properties: {
|
|
38
|
+
url: { type: "string", minLength: 1, description: "An absolute http(s) URL." },
|
|
39
|
+
offset: { type: "integer", minimum: 0, description: "Where in the page's text to start, in characters (default 0). An answer that stops short of the page's end gives the offset that reads on." },
|
|
40
|
+
max_chars: { type: "integer", minimum: MAX_CHARS_MIN, maximum: TEXT_CAP, description: `About how many characters of the page's text to read (default ${PAGE_CHARS.toLocaleString("en-US")}, at least ${MAX_CHARS_MIN}, at most ${TEXT_CAP.toLocaleString("en-US")}).` },
|
|
41
|
+
},
|
|
42
|
+
required: ["url"],
|
|
43
|
+
};
|
|
44
|
+
/** The models of the search in flight. */
|
|
45
|
+
export const searchingOn = new AsyncLocalStorage();
|
|
46
|
+
const serverLog = (line) => void process.stderr.write(`agentrun-serve: ${line}\n`);
|
|
47
|
+
/** The providers each web tool asks where the run's plane resolves it. Under --faux, a keyless provider the server names
|
|
48
|
+
* no fake for is left out, so a test never reaches the network. */
|
|
49
|
+
export function ordersOf(env) {
|
|
50
|
+
const { web_search: search, web_fetch: fetch } = planeOf(env);
|
|
51
|
+
const faux = Boolean(env[FAUX_ENV]);
|
|
52
|
+
const firecrawl = !faux || Boolean(env[FAUX_FIRECRAWL_ENV]);
|
|
53
|
+
const exa = !faux || Boolean(env[FAUX_EXA_ENV]);
|
|
54
|
+
const searchOrder = search === "parallel" || search === "native" ? [search] : [];
|
|
55
|
+
if (search === "firecrawl-keyless" && firecrawl)
|
|
56
|
+
searchOrder.push("firecrawl-keyless");
|
|
57
|
+
if ((search === "firecrawl-keyless" || search === "exa-keyless") && exa)
|
|
58
|
+
searchOrder.push("exa-keyless");
|
|
59
|
+
const fetchOrder = fetch === "parallel" ? ["parallel", "direct"] : fetch === "firecrawl-keyless" && firecrawl ? ["firecrawl-keyless", "direct"] : ["direct"];
|
|
60
|
+
return { search: searchOrder, fetch: fetchOrder };
|
|
61
|
+
}
|
|
62
|
+
/** One Web for each environment a run's calls are given (main's run process hands process.env; the runner hands each
|
|
63
|
+
* claim its own), so one run's key, client, pages and breakers never serve another. */
|
|
64
|
+
const webs = new WeakMap();
|
|
65
|
+
/** The run's Web. Where either web tool resolves to Parallel, the key is read here, once. A search's deadlines are its
|
|
66
|
+
* call's (web-search.ts), never the Web's. */
|
|
67
|
+
export function webOf(env) {
|
|
68
|
+
let web = webs.get(env);
|
|
69
|
+
if (web)
|
|
70
|
+
return web;
|
|
71
|
+
const plane = planeOf(env);
|
|
72
|
+
const key = plane.web_search === "parallel" || plane.web_fetch === "parallel" ? parallelKeyOf(env) : undefined;
|
|
73
|
+
const fakeFirecrawl = env[FAUX_ENV] ? env[FAUX_FIRECRAWL_ENV] : undefined;
|
|
74
|
+
web = createWeb({
|
|
75
|
+
order: ordersOf(env),
|
|
76
|
+
...(key === undefined ? {} : { keys: { parallel: key } }),
|
|
77
|
+
native: async (query, n, signal) => hostedSearched(query, n, planeOf(env).model, signal, await searchingOn.getStore()?.()),
|
|
78
|
+
guard: GUARD,
|
|
79
|
+
pay: paidRequest,
|
|
80
|
+
...(fakeFirecrawl ? { endpoints: { "firecrawl-keyless": fakeFirecrawl } } : {}),
|
|
81
|
+
send: {
|
|
82
|
+
parallel: async (...args) => ((await fauxFetch(env, FAUX_PARALLEL_ENV)) ?? fetch)(...args),
|
|
83
|
+
...(env[FAUX_ENV] && env[FAUX_EXA_ENV] ? { "exa-keyless": async (...args) => (await fauxFetch(env, FAUX_EXA_ENV))(...args) } : {}),
|
|
84
|
+
},
|
|
85
|
+
words: { rateLimited: RATE_LIMITED },
|
|
86
|
+
log: (event, fields) => serverLog(logLine(event, fields)),
|
|
87
|
+
});
|
|
88
|
+
webs.set(env, web);
|
|
89
|
+
return web;
|
|
90
|
+
}
|
package/dist/web-save.d.ts
CHANGED
|
@@ -6,8 +6,8 @@ import { type Workspace } from "./workspace/index.js";
|
|
|
6
6
|
export declare const WEB_SAVE_TOOL = "web_save";
|
|
7
7
|
/** The most bytes a saved file may have. */
|
|
8
8
|
export declare const SAVE_MAX_BYTES: number;
|
|
9
|
-
/** One deadline for a whole download: a 200 MiB file at about 350 KB/s. A page's 30 s (web
|
|
10
|
-
* large file on any link under about 7 MB/s. */
|
|
9
|
+
/** One deadline for a whole download: a 200 MiB file at about 350 KB/s. A page's 30 s (@parcha/agentrun-web page.ts
|
|
10
|
+
* TIMEOUT_MS) would cut a large file on any link under about 7 MB/s. */
|
|
11
11
|
export declare const SAVE_TIMEOUT_MS = 600000;
|
|
12
12
|
/** The folder a bare name is saved in. */
|
|
13
13
|
export declare const MEDIA_DIR = "media";
|
|
@@ -16,8 +16,10 @@ export declare const MEDIA_DIR = "media";
|
|
|
16
16
|
export declare const PATH_MAX_CHARS = 1024;
|
|
17
17
|
export declare const TYPE_MAX_CHARS = 255;
|
|
18
18
|
export declare const WORDS_MAX_CHARS = 2000;
|
|
19
|
-
/**
|
|
20
|
-
|
|
19
|
+
/** The saved types whose length their bytes give (media-duration.ts), in the words every description of web_save uses. */
|
|
20
|
+
export declare const LENGTH_TYPES = "mp3, wav, mp4 or m4a";
|
|
21
|
+
/** `duration_s`: an audio or video file's length in seconds (to the millisecond), when its bytes give it (LENGTH_TYPES);
|
|
22
|
+
* absent for any other file, so an agent can time a cut or price a job billed by length. */
|
|
21
23
|
export type Saved = {
|
|
22
24
|
ok: true;
|
|
23
25
|
path: string;
|
package/dist/web-save.js
CHANGED
|
@@ -5,14 +5,14 @@ import { extname } from "node:path";
|
|
|
5
5
|
import { defineTool } from "@earendil-works/pi-durable";
|
|
6
6
|
import { extensionOf, typeOf } from "./file-types.js";
|
|
7
7
|
import { mediaSeconds } from "./media-duration.js";
|
|
8
|
-
import { clip, URL_MAX } from "
|
|
8
|
+
import { clip, URL_MAX } from "@parcha/agentrun-web";
|
|
9
9
|
import { forModel } from "./result.js";
|
|
10
10
|
import { isWorkspaceError, workspaceErrorWords } from "./workspace/index.js";
|
|
11
11
|
export const WEB_SAVE_TOOL = "web_save";
|
|
12
12
|
/** The most bytes a saved file may have. */
|
|
13
13
|
export const SAVE_MAX_BYTES = 200 * 1024 * 1024;
|
|
14
|
-
/** One deadline for a whole download: a 200 MiB file at about 350 KB/s. A page's 30 s (web
|
|
15
|
-
* large file on any link under about 7 MB/s. */
|
|
14
|
+
/** One deadline for a whole download: a 200 MiB file at about 350 KB/s. A page's 30 s (@parcha/agentrun-web page.ts
|
|
15
|
+
* TIMEOUT_MS) would cut a large file on any link under about 7 MB/s. */
|
|
16
16
|
export const SAVE_TIMEOUT_MS = 600_000;
|
|
17
17
|
/** The folder a bare name is saved in. */
|
|
18
18
|
export const MEDIA_DIR = "media";
|
|
@@ -21,6 +21,8 @@ export const MEDIA_DIR = "media";
|
|
|
21
21
|
export const PATH_MAX_CHARS = 1_024;
|
|
22
22
|
export const TYPE_MAX_CHARS = 255;
|
|
23
23
|
export const WORDS_MAX_CHARS = 2_000;
|
|
24
|
+
/** The saved types whose length their bytes give (media-duration.ts), in the words every description of web_save uses. */
|
|
25
|
+
export const LENGTH_TYPES = "mp3, wav, mp4 or m4a";
|
|
24
26
|
/** The most web_save downloads at once per runner, across its runs (SERVER_SLOTS); the rest wait under their own
|
|
25
27
|
* deadline. Each holds its file in memory until its one write: its size when its host states its length, about twice
|
|
26
28
|
* that at the peak when it does not (publicDownload). So a runner's downloads hold at most 800 MiB at 200 MiB a file,
|
|
@@ -212,17 +214,12 @@ export async function webSave(args, work, options = {}) {
|
|
|
212
214
|
}
|
|
213
215
|
}
|
|
214
216
|
/** An audio or video file's length in seconds, rounded to the millisecond; undefined for any other type, or when its bytes
|
|
215
|
-
* don't give it
|
|
217
|
+
* don't give it (mediaSeconds only reads its bytes, and never throws). */
|
|
216
218
|
function lengthOf(bytes, type) {
|
|
217
219
|
if (!/^(audio|video)\//.test(type))
|
|
218
220
|
return undefined;
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
return seconds === undefined ? undefined : Math.round(seconds * 1000) / 1000;
|
|
222
|
-
}
|
|
223
|
-
catch {
|
|
224
|
-
return undefined;
|
|
225
|
-
}
|
|
221
|
+
const seconds = mediaSeconds(bytes);
|
|
222
|
+
return seconds === undefined ? undefined : Math.round(seconds * 1000) / 1000;
|
|
226
223
|
}
|
|
227
224
|
/** The words the model reads: the answer's JSON. */
|
|
228
225
|
export const savedText = (answer) => JSON.stringify(answer);
|
|
@@ -231,7 +228,7 @@ export const savedText = (answer) => JSON.stringify(answer);
|
|
|
231
228
|
export function webSaveTool(work, guard, slots) {
|
|
232
229
|
return defineTool({
|
|
233
230
|
name: WEB_SAVE_TOOL,
|
|
234
|
-
description: `Download one file from a public URL into this run's folder, where it stays (the run's Files show and play it): use it for a generated image, audio or video a service answered with a link that expires. path is where to save it, relative to the run's folder; a bare name (cat.png) goes under ${MEDIA_DIR}/. A path that already holds a file is refused, never overwritten. The file must be a type its bytes show: an image (png, jpeg, gif, webp), audio (mp3, wav, ogg, m4a) or video (mp4, webm), and the path must have an extension of that type (cat.png, not cat or cat.bin); any other type (a page, a PDF, text) is saved only to a path whose extension names the type the host declares. At most ${SAVE_MAX_BYTES / (1024 * 1024)} MiB and ${SAVE_TIMEOUT_MS / 60_000} minutes. A private or local address, a port other than 80 or 443, or a scheme other than http(s) is refused, at every redirect too. Answers JSON: { ok: true, path, bytes, content_type, duration_s?, url } (url the final one, after redirects; duration_s an audio or video file's length in seconds, when its bytes give it:
|
|
231
|
+
description: `Download one file from a public URL into this run's folder, where it stays (the run's Files show and play it): use it for a generated image, audio or video a service answered with a link that expires. path is where to save it, relative to the run's folder; a bare name (cat.png) goes under ${MEDIA_DIR}/. A path that already holds a file is refused, never overwritten. The file must be a type its bytes show: an image (png, jpeg, gif, webp), audio (mp3, wav, ogg, m4a) or video (mp4, webm), and the path must have an extension of that type (cat.png, not cat or cat.bin); any other type (a page, a PDF, text) is saved only to a path whose extension names the type the host declares. At most ${SAVE_MAX_BYTES / (1024 * 1024)} MiB and ${SAVE_TIMEOUT_MS / 60_000} minutes. A private or local address, a port other than 80 or 443, or a scheme other than http(s) is refused, at every redirect too. Answers JSON: { ok: true, path, bytes, content_type, duration_s?, url } (url the final one, after redirects; duration_s an audio or video file's length in seconds, when its bytes give it: ${LENGTH_TYPES}), or { ok: false, url, refused } when it was refused, { ok: false, url, error } when it failed; nothing is left at the path when it does not save.`,
|
|
235
232
|
parameters: {
|
|
236
233
|
type: "object",
|
|
237
234
|
additionalProperties: false,
|