@parcha/agentrun-serve 0.1.0-beta.56 → 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 +229 -0
- package/README.md +201 -52
- package/dist/acp-package.d.ts +2 -0
- package/dist/adapters.d.ts +15 -3
- package/dist/adapters.js +17 -6
- package/dist/agent-package.d.ts +3 -3
- package/dist/agent-package.js +17 -8
- 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 +34 -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/chatgpt-plan.d.ts +18 -8
- package/dist/chatgpt-plan.js +91 -34
- package/dist/claude-login.d.ts +74 -16
- package/dist/claude-login.js +215 -56
- package/dist/claude-sign-in.d.ts +17 -6
- package/dist/claude-sign-in.js +42 -39
- 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-Boh7tC4i.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/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 +9 -4
- package/dist/key-refusal.js +107 -25
- package/dist/learn/secrets.d.ts +13 -3
- package/dist/learn/secrets.js +118 -17
- 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 +1 -1
- 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.js +178 -27
- 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/provider.d.ts +9 -0
- package/dist/provider.js +50 -3
- package/dist/redact/match.d.ts +30 -3
- package/dist/redact/match.js +422 -41
- 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 +2 -0
- package/dist/run-timeline-served.js +2 -2
- 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 +11 -0
- package/dist/runner/adapter-loop.js +55 -10
- package/dist/runner/adapter-run-spec.d.ts +45 -8
- package/dist/runner/adapter-run-spec.js +150 -28
- package/dist/runner/adapters/pi.js +25 -3
- package/dist/runner/beside.js +4 -3
- 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 +82 -35
- 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/main.js +7 -2
- 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/run-child-host.d.ts +3 -0
- package/dist/runner/run-child-host.js +69 -28
- package/dist/runner/run-child.js +5 -2
- package/dist/runner/runner.d.ts +14 -3
- package/dist/runner/runner.js +129 -33
- 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 +52 -11
- package/dist/server.js +345 -118
- package/dist/skills/agentrun-serve/SKILL.md +4 -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/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 +34 -4
- package/dist/store/store.d.ts +43 -2
- package/dist/store/store.js +20 -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 +3 -3
- package/dist/suggestions.js +3 -2
- package/dist/template-eval.d.ts +20 -9
- package/dist/template-eval.js +115 -33
- 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/labels.d.ts +6 -4
- package/dist/timeline/labels.js +18 -6
- package/dist/timeline/timeline.d.ts +5 -0
- package/dist/timeline/timeline.js +14 -8
- 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 +66 -5
- 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/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 +2 -1
- package/dist/workspace/computer-deps.js +9 -2
- package/dist/workspace/computer-for-claim.d.ts +12 -1
- package/dist/workspace/computer-for-claim.js +77 -8
- package/dist/workspace/computer-kept.d.ts +3 -0
- package/dist/workspace/computer-kept.js +11 -2
- 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/synced-folder.js +3 -4
- package/dist/wrapped.d.ts +13 -5
- package/dist/wrapped.js +52 -26
- package/package.json +17 -11
- package/skills/agentrun-serve/SKILL.md +4 -0
- package/templates/README.md +30 -8
- package/dist/console/assets/index-CQ3cJRzI.js +0 -746
- package/dist/console/assets/index-DKkaS3a0.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/keyless-mcp.js
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
// The MCP client the keyless web backends share (firecrawl-keyless.ts, exa-keyless.ts): one tool call is one exchange
|
|
2
|
-
// over MCP's streamable HTTP transport. `initialize` (its session id, when the server names one), then
|
|
3
|
-
// `notifications/initialized`, then `tools/call`, each request carrying the session id only where one came back. An
|
|
4
|
-
// answer is JSON or an event stream's `data:` lines, and no more than 1 MiB of one is read, but for a call's answer given
|
|
5
|
-
// its own bound: one past that bound is `over`, never parsed cut. No key and no header beyond
|
|
6
|
-
// the transport's own is sent. What a refusal means (a free limit, a connection refused) is each backend's to read.
|
|
7
|
-
/** The MCP revision this client speaks; the server answers the one it takes. */
|
|
8
|
-
export const PROTOCOL = "2025-06-18";
|
|
9
|
-
const ACCEPT = "application/json, text/event-stream";
|
|
10
|
-
/** The most of an answer read: a search's text is a few kilobytes, a page's markdown more; past this the rest is never
|
|
11
|
-
* read. */
|
|
12
|
-
export const MAX_BYTES = 1 << 20;
|
|
13
|
-
/** What a free web search answers when its free limit holds, in our words: a backend's own (a link to sign up, a key in
|
|
14
|
-
* its MCP URL) mean nothing to a person here, so they are never passed on. */
|
|
15
|
-
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.";
|
|
16
|
-
/** The seconds a Retry-After names, as seconds or a date; undefined when it names none. */
|
|
17
|
-
export function retryAfterOf(response) {
|
|
18
|
-
const said = response.headers.get("retry-after")?.trim();
|
|
19
|
-
if (!said)
|
|
20
|
-
return undefined;
|
|
21
|
-
if (/^\d+$/.test(said))
|
|
22
|
-
return Number(said);
|
|
23
|
-
const at = Date.parse(said);
|
|
24
|
-
return Number.isNaN(at) ? undefined : Math.max(0, Math.ceil((at - Date.now()) / 1000));
|
|
25
|
-
}
|
|
26
|
-
/** `ms` of waiting that `signal` ends, rejecting with its reason. */
|
|
27
|
-
export const sleep = (ms, signal) => new Promise((resolve, reject) => {
|
|
28
|
-
if (signal.aborted)
|
|
29
|
-
return reject(signal.reason);
|
|
30
|
-
const stop = () => {
|
|
31
|
-
clearTimeout(timer);
|
|
32
|
-
reject(signal.reason);
|
|
33
|
-
};
|
|
34
|
-
const timer = setTimeout(() => {
|
|
35
|
-
signal.removeEventListener("abort", stop);
|
|
36
|
-
resolve();
|
|
37
|
-
}, ms);
|
|
38
|
-
signal.addEventListener("abort", stop, { once: true });
|
|
39
|
-
});
|
|
40
|
-
/** At most MAX_BYTES of a body, as text; the rest is not read. */
|
|
41
|
-
async function boundedText(response) {
|
|
42
|
-
const reader = response.body?.getReader();
|
|
43
|
-
const chunks = [];
|
|
44
|
-
for (let size = 0; reader && size < MAX_BYTES;) {
|
|
45
|
-
const { done, value } = await reader.read();
|
|
46
|
-
if (done)
|
|
47
|
-
break;
|
|
48
|
-
chunks.push(value);
|
|
49
|
-
size += value.byteLength;
|
|
50
|
-
}
|
|
51
|
-
await reader?.cancel().catch(() => undefined);
|
|
52
|
-
return Buffer.concat(chunks).subarray(0, MAX_BYTES).toString("utf8");
|
|
53
|
-
}
|
|
54
|
-
/** A body of at most `max` bytes, as text; undefined where it runs past them (the rest is not read). */
|
|
55
|
-
async function cappedText(response, max) {
|
|
56
|
-
const reader = response.body?.getReader();
|
|
57
|
-
const chunks = [];
|
|
58
|
-
for (let size = 0; reader;) {
|
|
59
|
-
const { done, value } = await reader.read();
|
|
60
|
-
if (done)
|
|
61
|
-
break;
|
|
62
|
-
chunks.push(value);
|
|
63
|
-
size += value.byteLength;
|
|
64
|
-
if (size > max) {
|
|
65
|
-
await reader.cancel().catch(() => undefined);
|
|
66
|
-
return undefined;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return Buffer.concat(chunks).toString("utf8");
|
|
70
|
-
}
|
|
71
|
-
/** A JSON-RPC answer's body: JSON, or the `data:` lines of an event stream, the one answering `id`. With `max`, a body
|
|
72
|
-
* past it is "over"; without, its first MAX_BYTES are read. */
|
|
73
|
-
async function answerOf(response, id, max) {
|
|
74
|
-
const text = max === undefined ? await boundedText(response) : await cappedText(response, max);
|
|
75
|
-
if (text === undefined)
|
|
76
|
-
return "over";
|
|
77
|
-
const messages = /event-stream/i.test(response.headers.get("content-type") ?? "")
|
|
78
|
-
? text.split("\n").filter((line) => line.startsWith("data:")).map((line) => line.slice(5).trim())
|
|
79
|
-
: [text];
|
|
80
|
-
for (const message of messages) {
|
|
81
|
-
try {
|
|
82
|
-
const parsed = JSON.parse(message);
|
|
83
|
-
if (parsed.id === id)
|
|
84
|
-
return parsed;
|
|
85
|
-
}
|
|
86
|
-
catch { }
|
|
87
|
-
}
|
|
88
|
-
return undefined;
|
|
89
|
-
}
|
|
90
|
-
/** A tool result's text blocks, joined by lines; empty when it has none. */
|
|
91
|
-
export const textBlocks = (result) => (Array.isArray(result?.content) ? result.content : []).flatMap((part) => (part.type === "text" && typeof part.text === "string" ? [part.text] : [])).join("\n");
|
|
92
|
-
export async function callTool(endpoint, dial, deadline, name, args, maxBytes) {
|
|
93
|
-
let session;
|
|
94
|
-
const post = (body) => dial(endpoint, {
|
|
95
|
-
method: "POST",
|
|
96
|
-
headers: { "content-type": "application/json", accept: ACCEPT, "mcp-protocol-version": PROTOCOL, ...(session ? { "mcp-session-id": session } : {}) },
|
|
97
|
-
body: JSON.stringify({ jsonrpc: "2.0", ...body }),
|
|
98
|
-
signal: deadline,
|
|
99
|
-
});
|
|
100
|
-
const refused = async (response, step) => {
|
|
101
|
-
if (response.ok)
|
|
102
|
-
return undefined;
|
|
103
|
-
await response.body?.cancel().catch(() => undefined);
|
|
104
|
-
const retryAfterS = retryAfterOf(response);
|
|
105
|
-
return { status: response.status, step, ...(retryAfterS === undefined ? {} : { retryAfterS }) };
|
|
106
|
-
};
|
|
107
|
-
const opened = await post({ id: 1, method: "initialize", params: { protocolVersion: PROTOCOL, capabilities: {}, clientInfo: { name: "agentrun-serve", version: "1" } } });
|
|
108
|
-
const failed = await refused(opened, "initialize");
|
|
109
|
-
if (failed)
|
|
110
|
-
return failed;
|
|
111
|
-
session = opened.headers.get("mcp-session-id") ?? undefined;
|
|
112
|
-
const hello = await answerOf(opened, 1);
|
|
113
|
-
if (hello === "over" || !hello?.result)
|
|
114
|
-
return { error: "", rpc: false };
|
|
115
|
-
const ready = await post({ method: "notifications/initialized" });
|
|
116
|
-
await ready.body?.cancel().catch(() => undefined);
|
|
117
|
-
const called = await post({ id: 2, method: "tools/call", params: { name, arguments: args } });
|
|
118
|
-
const late = await refused(called, "the call");
|
|
119
|
-
if (late)
|
|
120
|
-
return late;
|
|
121
|
-
const answer = await answerOf(called, 2, maxBytes);
|
|
122
|
-
if (answer === "over")
|
|
123
|
-
return { over: maxBytes };
|
|
124
|
-
if (answer?.error)
|
|
125
|
-
return { error: typeof answer.error.message === "string" ? answer.error.message : "", rpc: true };
|
|
126
|
-
return { result: answer?.result };
|
|
127
|
-
}
|
package/dist/text-urls.d.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
/** A URL written in text, as a link is read (a page's links, an answer's cited links): it also ends at "|", so a Slack
|
|
2
|
-
* mrkdwn link `<https://a/b|text>` is `https://a/b`. */
|
|
3
|
-
export declare const TEXT_URL: RegExp;
|
|
4
|
-
/** A URL written in text, as the timeline scrubs one (scrubUrls): it runs on through a "|", so a query value holding one
|
|
5
|
-
* (`?ids=1|2`) is elided whole, never shown past the "|". The scrub's only difference from TEXT_URL. */
|
|
6
|
-
export declare const SCRUBBED_URL: RegExp;
|
|
7
|
-
/** What a URL found in text may end with that is not its own: a sentence's punctuation, a closing quote, an emphasis's
|
|
8
|
-
* "*", a closing bracket the URL did not open (trimmed). */
|
|
9
|
-
export declare const TRAILING = ".,;:!?*)]}'";
|
|
10
|
-
/** A URL found in text less its tail (TRAILING): a closing bracket stays where the URL opened as many as it closes (a wiki
|
|
11
|
-
* title's `Foo_(bar)`). One count of the brackets, then one walk back, so a long run of them is linear. */
|
|
12
|
-
export declare function trimmed(raw: string): string;
|
|
13
|
-
/** Where a markdown link's target (`text` from `from`, the character after its "](") ends: at the ")" that closes the link,
|
|
14
|
-
* so parentheses it holds in pairs (a wiki title's) stay, else at whitespace or `to`. `text[end] === ")"` says it closed. */
|
|
15
|
-
export declare function targetEnd(text: string, from?: number, to?: number): number;
|
|
16
|
-
/** A string that is one URL whole (a JSON field holding a link means all of it, its last character too), trimmed of the
|
|
17
|
-
* whitespace around it; else undefined. */
|
|
18
|
-
export declare function wholeUrl(text: string): string | undefined;
|
|
19
|
-
/** A link as `text` writes it, and whether that is all of it (`exact`): a markdown link's target that its ")" closed is;
|
|
20
|
-
* a URL written out, or a target with no ")", may end in a tail that is not its own (TRAILING). */
|
|
21
|
-
export type WrittenLink = {
|
|
22
|
-
raw: string;
|
|
23
|
-
exact: boolean;
|
|
24
|
-
};
|
|
25
|
-
/** Every link `text` holds, in order, as written (WrittenLink): a markdown link's target (`[text](url)`, targetEnd), and a
|
|
26
|
-
* URL written out (TEXT_URL). A URL written out that runs into a "](" is a link written as its own text
|
|
27
|
-
* (`[https://a](https://a)`): what follows each "](" in it is a target. A target ends where targetEnd says, never where
|
|
28
|
-
* TEXT_URL stopped, so one holding a "|" (`](https://a/?m=1|2)`) is whole. Each link is looked for from where the last
|
|
29
|
-
* one ended, a target read past the URL running into it scanned once more at most: linear, however many unclosed "](" a
|
|
30
|
-
* hostile page holds. A page's links (hrefsIn) and an answer's cited links (link-check.ts) both read it. */
|
|
31
|
-
export declare function linksWritten(text: string): Generator<WrittenLink>;
|
|
32
|
-
/** Every link `text` holds, in order (linksWritten): a target its ")" closed as it is, any other less its tail (trimmed). */
|
|
33
|
-
export declare function hrefsIn(text: string): Generator<string>;
|
|
34
|
-
/** The absolute http(s) URLs a text holds, each once and without its fragment, in order: for JSON, those of its string
|
|
35
|
-
* values (jsonHrefs); for any other text (markdown, plain text, XML), its markdown link targets and the URLs it writes
|
|
36
|
-
* out (hrefsIn), read with its entities undone (readable). A fetched page's links (linksWithin keeps those within the
|
|
37
|
-
* caps and counts the rest), and the urls the link check takes from a call that is not a web call. */
|
|
38
|
-
export declare function linksOf(text: string): Generator<string>;
|
package/dist/text-urls.js
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
// The one reading of an http(s) URL written in text: where it ends, what tail is not its own, where a markdown link's
|
|
2
|
-
// target ends, and the links a page's text holds. The timeline's scrub (timeline/util.ts scrubUrls), the eval link check's
|
|
3
|
-
// cited links (link-check.ts) and a fetched page's links (web-fetch-direct.ts, web-fetch.ts) all read through it, so a
|
|
4
|
-
// link a page holds and the same link an answer cites come out in the same shape.
|
|
5
|
-
//
|
|
6
|
-
// Every reading here is linear in its text: a page is hostile and up to a million characters.
|
|
7
|
-
/** What ends a URL written in text, for every reader: whitespace, a quote, an angle bracket, a backtick or a backslash. */
|
|
8
|
-
const URL_ENDS = "\\s\"`<>\\\\";
|
|
9
|
-
/** A URL written in text, as a link is read (a page's links, an answer's cited links): it also ends at "|", so a Slack
|
|
10
|
-
* mrkdwn link `<https://a/b|text>` is `https://a/b`. */
|
|
11
|
-
export const TEXT_URL = new RegExp(`https?://[^${URL_ENDS}|]+`, "gi");
|
|
12
|
-
/** A URL written in text, as the timeline scrubs one (scrubUrls): it runs on through a "|", so a query value holding one
|
|
13
|
-
* (`?ids=1|2`) is elided whole, never shown past the "|". The scrub's only difference from TEXT_URL. */
|
|
14
|
-
export const SCRUBBED_URL = new RegExp(`https?://[^${URL_ENDS}]+`, "gi");
|
|
15
|
-
/** What a URL found in text may end with that is not its own: a sentence's punctuation, a closing quote, an emphasis's
|
|
16
|
-
* "*", a closing bracket the URL did not open (trimmed). */
|
|
17
|
-
export const TRAILING = ".,;:!?*)]}'";
|
|
18
|
-
const OPENER = { ")": "(", "]": "[", "}": "{" };
|
|
19
|
-
/** A URL found in text less its tail (TRAILING): a closing bracket stays where the URL opened as many as it closes (a wiki
|
|
20
|
-
* title's `Foo_(bar)`). One count of the brackets, then one walk back, so a long run of them is linear. */
|
|
21
|
-
export function trimmed(raw) {
|
|
22
|
-
const count = { "(": 0, ")": 0, "[": 0, "]": 0, "{": 0, "}": 0 };
|
|
23
|
-
for (const char of raw)
|
|
24
|
-
if (char in count)
|
|
25
|
-
count[char] += 1;
|
|
26
|
-
let end = raw.length;
|
|
27
|
-
for (; end > 0 && TRAILING.includes(raw[end - 1]); end--) {
|
|
28
|
-
const char = raw[end - 1];
|
|
29
|
-
const opener = OPENER[char];
|
|
30
|
-
if (opener && count[opener] >= count[char])
|
|
31
|
-
break;
|
|
32
|
-
if (opener)
|
|
33
|
-
count[char] -= 1;
|
|
34
|
-
}
|
|
35
|
-
return raw.slice(0, end);
|
|
36
|
-
}
|
|
37
|
-
/** Where a markdown link's target (`text` from `from`, the character after its "](") ends: at the ")" that closes the link,
|
|
38
|
-
* so parentheses it holds in pairs (a wiki title's) stay, else at whitespace or `to`. `text[end] === ")"` says it closed. */
|
|
39
|
-
export function targetEnd(text, from = 0, to = text.length) {
|
|
40
|
-
let end = from;
|
|
41
|
-
for (let depth = 0; end < to && !/\s/.test(text[end]); end++) {
|
|
42
|
-
if (text[end] === "(")
|
|
43
|
-
depth++;
|
|
44
|
-
else if (text[end] === ")" && depth-- === 0)
|
|
45
|
-
break;
|
|
46
|
-
}
|
|
47
|
-
return end;
|
|
48
|
-
}
|
|
49
|
-
/** Where a markdown target in angle brackets (`text` from `from`, the character after its "<") ends: the index of its ">",
|
|
50
|
-
* or -1 where whitespace, another "<" or `to` comes first (no such target: read it as targetEnd does). */
|
|
51
|
-
function angledEnd(text, from, to) {
|
|
52
|
-
for (let end = from; end < to && !/[\s<]/.test(text[end]); end++)
|
|
53
|
-
if (text[end] === ">")
|
|
54
|
-
return end;
|
|
55
|
-
return -1;
|
|
56
|
-
}
|
|
57
|
-
/** A string that is one URL whole (a JSON field holding a link means all of it, its last character too), trimmed of the
|
|
58
|
-
* whitespace around it; else undefined. */
|
|
59
|
-
export function wholeUrl(text) {
|
|
60
|
-
const whole = text.trim();
|
|
61
|
-
return /^https?:\/\//i.test(whole) && !/\s/.test(whole) ? whole : undefined;
|
|
62
|
-
}
|
|
63
|
-
/** Where a link starts: a markdown link's target (after its "]("), or a URL written out (TEXT_URL). */
|
|
64
|
-
const LINK_AT = new RegExp(`\\]\\(|${TEXT_URL.source}`, "gi");
|
|
65
|
-
/** Every link `text` holds, in order, as written (WrittenLink): a markdown link's target (`[text](url)`, targetEnd), and a
|
|
66
|
-
* URL written out (TEXT_URL). A URL written out that runs into a "](" is a link written as its own text
|
|
67
|
-
* (`[https://a](https://a)`): what follows each "](" in it is a target. A target ends where targetEnd says, never where
|
|
68
|
-
* TEXT_URL stopped, so one holding a "|" (`](https://a/?m=1|2)`) is whole. Each link is looked for from where the last
|
|
69
|
-
* one ended, a target read past the URL running into it scanned once more at most: linear, however many unclosed "](" a
|
|
70
|
-
* hostile page holds. A page's links (hrefsIn) and an answer's cited links (link-check.ts) both read it. */
|
|
71
|
-
export function* linksWritten(text) {
|
|
72
|
-
const at = new RegExp(LINK_AT.source, "gi");
|
|
73
|
-
for (let found = at.exec(text); found; found = at.exec(text)) {
|
|
74
|
-
// A "](" alone is ["", ""]: one target, after it.
|
|
75
|
-
const pieces = found[0].split("](");
|
|
76
|
-
if (pieces[0])
|
|
77
|
-
yield { raw: pieces[0], exact: false };
|
|
78
|
-
let from = found.index + pieces[0].length + 2;
|
|
79
|
-
for (let i = 1; i < pieces.length; i++) {
|
|
80
|
-
// Each target but the last ends by the next "](" in the URL; the last may run on past where TEXT_URL stopped.
|
|
81
|
-
const to = i < pieces.length - 1 ? from + pieces[i].length : text.length;
|
|
82
|
-
// A target in angle brackets (CommonMark's `](<https://a/b>)`) is what they hold, its ">" its end.
|
|
83
|
-
const angled = text[from] === "<" ? angledEnd(text, from + 1, to) : -1;
|
|
84
|
-
const end = angled >= 0 ? angled : targetEnd(text, from, to);
|
|
85
|
-
yield angled >= 0 ? { raw: text.slice(from + 1, angled), exact: true } : { raw: text.slice(from, end), exact: text[end] === ")" };
|
|
86
|
-
at.lastIndex = Math.max(at.lastIndex, end);
|
|
87
|
-
from += pieces[i].length + 2;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
/** Every link `text` holds, in order (linksWritten): a target its ")" closed as it is, any other less its tail (trimmed). */
|
|
92
|
-
export function* hrefsIn(text) {
|
|
93
|
-
for (const { raw, exact } of linksWritten(text))
|
|
94
|
-
yield exact ? raw : trimmed(raw);
|
|
95
|
-
}
|
|
96
|
-
/** The entities XML names (`'` HTML's `'` too, a numeric reference), undone once in a text that is not JSON. */
|
|
97
|
-
const ENTITY = /&(?:(quot|amp|lt|gt|apos)|#(\d{1,7})|#x([0-9a-f]{1,6}));/gi;
|
|
98
|
-
const NAMED = { quot: '"', amp: "&", lt: "<", gt: ">", apos: "'" };
|
|
99
|
-
/** An XML attribute: its name, and its value in double quotes (kept as it is, so an "='" in its query is never taken for
|
|
100
|
-
* an attribute) or in single quotes (the fourth group). */
|
|
101
|
-
const ATTRIBUTE = /(\s)([^\s=<>"'/]+)(\s*=\s*)(?:"[^"]*"|'([^'<]*)')/g;
|
|
102
|
-
/** `text` read for its links, where it is not JSON: its entities undone once, so an escaped `<a href="…">`
|
|
103
|
-
* (an Atom feed's content) ends its URL at the quote, never running on to the next space, and an attribute's
|
|
104
|
-
* `href="https://a/?x=1&y=2"` is the URL `https://a/?x=1&y=2`. One pass, so `&lt;` is `<`. Where it is XML,
|
|
105
|
-
* first: a single-quoted attribute (`href='https://a/b'`) is requoted to end at its quote as a double-quoted one does (its
|
|
106
|
-
* value holds no "'" or "<" of its own), and a namespace's name (`xmlns="http://www.w3.org/2005/Atom"`), an identifier,
|
|
107
|
-
* never a link, is dropped. Attributes are read in order, each whole. */
|
|
108
|
-
function readable(text) {
|
|
109
|
-
const xml = /^\s*<[?!A-Za-z]/.test(text)
|
|
110
|
-
? text.replace(ATTRIBUTE, (attribute, space, name, equals, single) => {
|
|
111
|
-
if (/^xmlns(:|$)/i.test(name))
|
|
112
|
-
return space;
|
|
113
|
-
return single === undefined ? attribute : `${space}${name}${equals}"${single}"`;
|
|
114
|
-
})
|
|
115
|
-
: text;
|
|
116
|
-
return xml.replace(ENTITY, (entity, name, decimal, hex) => {
|
|
117
|
-
if (name)
|
|
118
|
-
return NAMED[name.toLowerCase()];
|
|
119
|
-
const code = Number.parseInt(decimal ?? hex, decimal ? 10 : 16);
|
|
120
|
-
return code <= 0x10ffff ? String.fromCodePoint(code) : entity;
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
/** A JSON body's links as written, in the body's order: each string value's (hrefsIn), one that is a URL whole taken as
|
|
124
|
-
* it is (wholeUrl); undefined for a text that is not JSON. Read from the parsed body, so an escape (`\/`) never hides or
|
|
125
|
-
* cuts one. */
|
|
126
|
-
function jsonHrefs(text) {
|
|
127
|
-
// An object, an array or one string: the bodies that can hold a URL (a string body is one, or holds some).
|
|
128
|
-
if (!/^\s*[[{"]/.test(text))
|
|
129
|
-
return undefined;
|
|
130
|
-
let body;
|
|
131
|
-
try {
|
|
132
|
-
body = JSON.parse(text);
|
|
133
|
-
}
|
|
134
|
-
catch {
|
|
135
|
-
return undefined;
|
|
136
|
-
}
|
|
137
|
-
return (function* () {
|
|
138
|
-
// A stack, not recursion: a deeply nested body never overflows it.
|
|
139
|
-
const stack = [body];
|
|
140
|
-
while (stack.length) {
|
|
141
|
-
const next = stack.pop();
|
|
142
|
-
if (typeof next === "string") {
|
|
143
|
-
const whole = wholeUrl(next);
|
|
144
|
-
yield* whole === undefined ? hrefsIn(next) : [whole];
|
|
145
|
-
}
|
|
146
|
-
else if (typeof next === "object" && next !== null) {
|
|
147
|
-
const values = Object.values(next);
|
|
148
|
-
for (let i = values.length - 1; i >= 0; i--)
|
|
149
|
-
stack.push(values[i]);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
})();
|
|
153
|
-
}
|
|
154
|
-
/** The absolute http(s) URLs a text holds, each once and without its fragment, in order: for JSON, those of its string
|
|
155
|
-
* values (jsonHrefs); for any other text (markdown, plain text, XML), its markdown link targets and the URLs it writes
|
|
156
|
-
* out (hrefsIn), read with its entities undone (readable). A fetched page's links (linksWithin keeps those within the
|
|
157
|
-
* caps and counts the rest), and the urls the link check takes from a call that is not a web call. */
|
|
158
|
-
export function* linksOf(text) {
|
|
159
|
-
const seen = new Set();
|
|
160
|
-
for (const href of jsonHrefs(text) ?? hrefsIn(readable(text))) {
|
|
161
|
-
const link = /^https?:\/\//i.test(href) ? URL.parse(href) : null;
|
|
162
|
-
if (!link)
|
|
163
|
-
continue;
|
|
164
|
-
link.hash = "";
|
|
165
|
-
if (seen.has(link.href))
|
|
166
|
-
continue;
|
|
167
|
-
seen.add(link.href);
|
|
168
|
-
yield link.href;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import type { TSchema } from "typebox";
|
|
2
|
-
import type { PublicFetchOptions } from "@parcha/pi-browser";
|
|
3
|
-
import { linksOf } from "./text-urls.js";
|
|
4
|
-
/** A page's text read for its links (text-urls.ts): web_fetch's Parallel read takes it from here. */
|
|
5
|
-
export { linksOf };
|
|
6
|
-
/** On a page that was read, `chars`: the length of `text`, the part of the page's text this answer holds. `truncated`
|
|
7
|
-
* while more of it follows this part, and `next_offset` where that begins; `offset` where this part begins, when not 0;
|
|
8
|
-
* `original_chars` the whole text's length in characters, when it is known and this part is not all of it;
|
|
9
|
-
* `beyond_read` on the last part a read reaches of a page that goes on past PAGE_READ_MAX_CHARS; `links_omitted`, with the
|
|
10
|
-
* first part, how many of the page's links its caps left out. `html_chars`, on every part of a page whose HTML readPage
|
|
11
|
-
* cut before it was read: the page's whole length in characters. Its text, all its parts together, is then the start of
|
|
12
|
-
* the page's, so no part gives original_chars, and the last part (no truncated: no part follows) still says the page was
|
|
13
|
-
* cut. `converted: "text"` when the text is the page's plain text, its conversion to markdown having run out of time. */
|
|
14
|
-
export type Page = {
|
|
15
|
-
ok: boolean;
|
|
16
|
-
url: string;
|
|
17
|
-
status: number;
|
|
18
|
-
title: string;
|
|
19
|
-
text: string;
|
|
20
|
-
chars?: number;
|
|
21
|
-
truncated?: true;
|
|
22
|
-
original_chars?: number;
|
|
23
|
-
beyond_read?: true;
|
|
24
|
-
offset?: number;
|
|
25
|
-
next_offset?: number;
|
|
26
|
-
html_chars?: number;
|
|
27
|
-
converted?: "text";
|
|
28
|
-
links: string[];
|
|
29
|
-
links_omitted?: number;
|
|
30
|
-
refused?: string;
|
|
31
|
-
error?: string;
|
|
32
|
-
};
|
|
33
|
-
/** Under `--faux` only: a module whose default export gives `{ lookup, fetchImpl }` for the fetch (the tests' fake
|
|
34
|
-
* network), in place of the system's resolver and undici. */
|
|
35
|
-
export declare const FAUX_WEB_ENV = "AGENTRUN_SERVE_FAUX_WEB";
|
|
36
|
-
/** The most of a page's text one answer holds (max_chars at most), what it holds when max_chars is not given, and the
|
|
37
|
-
* least max_chars a tool's call may ask for (a smaller one is refused, saying so). */
|
|
38
|
-
export declare const TEXT_CAP = 50000;
|
|
39
|
-
export declare const PAGE_CHARS = 20000;
|
|
40
|
-
export declare const MAX_CHARS_MIN = 200;
|
|
41
|
-
/** The most of a page's TEXT web_fetch reads, from either provider: its first million characters (not HTML_READ_CHARS,
|
|
42
|
-
* readPage's cut of the HTML before any text is read). A part reads within it, Parallel is never asked for more of a
|
|
43
|
-
* page than that and one character (web-fetch.ts), and an offset at or past it is refused. */
|
|
44
|
-
export declare const PAGE_READ_MAX_CHARS = 1000000;
|
|
45
|
-
/** A page's links, with its first part only: at most LINKS_CAP of them, in at most LINKS_CHARS characters of the answer. */
|
|
46
|
-
export declare const LINKS_CAP = 200;
|
|
47
|
-
export declare const LINKS_CHARS = 20000;
|
|
48
|
-
export declare const TITLE_CAP = 500;
|
|
49
|
-
/** The longest URL fetched: a longer one is refused before anything is sent. */
|
|
50
|
-
export declare const URL_MAX = 4096;
|
|
51
|
-
export declare const TIMEOUT_MS = 30000;
|
|
52
|
-
/** Where readPage cuts a page's HTML before it reads it, scripts and styles aside (not PAGE_READ_MAX_CHARS, the most of
|
|
53
|
-
* the text read): pi-browser's READ_MAX_CHARS, held here so the tool's words name it without loading pi-browser where
|
|
54
|
-
* nothing is fetched (web-fetch-direct-cut.test holds the two equal). */
|
|
55
|
-
export declare const HTML_READ_CHARS = 1000000;
|
|
56
|
-
export declare const clip: (text: string, max: number) => string;
|
|
57
|
-
/** Which part of a page's text an answer holds: from `offset` (0 by default), about `maxChars` characters (PAGE_CHARS by
|
|
58
|
-
* default, at most TEXT_CAP). */
|
|
59
|
-
export type Part = {
|
|
60
|
-
offset?: number;
|
|
61
|
-
maxChars?: number;
|
|
62
|
-
};
|
|
63
|
-
/** Why a tool's call may not read `part`, or undefined: an offset that is not a whole number of 0 or more, a maxChars that
|
|
64
|
-
* is not a number of MAX_CHARS_MIN or more (one past TEXT_CAP reads TEXT_CAP). */
|
|
65
|
-
export declare function partProblem({ offset, maxChars }: Part): string | undefined;
|
|
66
|
-
/** `part` in range: a whole offset of 0 or more, a whole maxChars of 1 to TEXT_CAP. */
|
|
67
|
-
export declare const partOf: (part?: Part) => Required<Part>;
|
|
68
|
-
/** Where the longest run of `text` from `start` ends whose JSON string takes at most `budget` characters (less its
|
|
69
|
-
* quotes): a character takes one, a quote, a backslash or a control character JSON writes short (\n, \t) two, any other
|
|
70
|
-
* control character or a lone surrogate six. A surrogate pair is never split. While a character is left the run holds
|
|
71
|
-
* one at least, whatever it takes, so a part always advances (only a budget under six can be passed, by that one). */
|
|
72
|
-
export declare function partEnd(text: string, start: number, budget: number): number;
|
|
73
|
-
/** A page's text as it is read once: its first PAGE_READ_MAX_CHARS characters (`text`), whether the page's text goes on
|
|
74
|
-
* past them (`beyond`), and its whole length where it is known (`length`). */
|
|
75
|
-
export type Text = {
|
|
76
|
-
text: string;
|
|
77
|
-
beyond: boolean;
|
|
78
|
-
length?: number;
|
|
79
|
-
};
|
|
80
|
-
/** `text` as read: `whole` says whether it is all of the page's text, so its length is the page's (a provider that cut
|
|
81
|
-
* it, or readPage's cut of its HTML, gives none). The cut at PAGE_READ_MAX_CHARS never splits a surrogate pair: where
|
|
82
|
-
* it would, it is one unit short. */
|
|
83
|
-
export declare function textOf(text: string, whole: boolean): Text;
|
|
84
|
-
/** The part of a page's text an answer holds, and what it says of the rest. */
|
|
85
|
-
export declare function pageText({ text, beyond, length }: Text, part?: Part): Pick<Page, "text" | "chars" | "truncated" | "original_chars" | "beyond_read" | "offset" | "next_offset">;
|
|
86
|
-
/** A page's links as its first part holds them: in page order, at most LINKS_CAP of them, each one that fits in what is
|
|
87
|
-
* left of LINKS_CHARS (as JSON, with its comma); and how many of them that left out, when any. */
|
|
88
|
-
export declare function linksWithin(links: Iterable<string>): Pick<Page, "links" | "links_omitted">;
|
|
89
|
-
/** A page read once, before any part of it is taken: its final url, status and title, its Text, the links its first part
|
|
90
|
-
* holds (linksWithin), and readPage's `html_chars` and `converted` where it gave them. */
|
|
91
|
-
export type Read = {
|
|
92
|
-
ok: true;
|
|
93
|
-
url: string;
|
|
94
|
-
status: number;
|
|
95
|
-
title: string;
|
|
96
|
-
links: string[];
|
|
97
|
-
links_omitted?: number;
|
|
98
|
-
html_chars?: number;
|
|
99
|
-
converted?: "text";
|
|
100
|
-
} & Text;
|
|
101
|
-
/** A Page that was not read, saying why. */
|
|
102
|
-
export type Unread = Page & {
|
|
103
|
-
ok: false;
|
|
104
|
-
};
|
|
105
|
-
/** The part `part` names of a page read once, as a Page: the links with its first part only. */
|
|
106
|
-
export declare function partOfRead({ text, beyond, length, links, links_omitted, html_chars, converted, ...page }: Read, part?: Part): Page;
|
|
107
|
-
/** A page a provider read for us as markdown (Parallel's extract, keyless Firecrawl's scrape, keyless Exa's fetch), as a
|
|
108
|
-
* Read: its url, title and status, the text (`whole`: all of the page's, so its length is the page's) and the links it
|
|
109
|
-
* holds. */
|
|
110
|
-
export declare const markdownRead: (url: string, title: string, text: string, whole: boolean, status?: number) => Read;
|
|
111
|
-
/** The page at `url`, read once through the guarded direct fetch, or why it was not. The rest of `options` are the
|
|
112
|
-
* guard's resolver and fetch, for a test; a signal ends it early. */
|
|
113
|
-
export declare function readDirect(url: unknown, options?: Omit<PublicFetchOptions, "maxBytes">): Promise<Read | Unread>;
|
|
114
|
-
/** The part `options` names of the page at `url`, as a Page, read through the guarded direct fetch. */
|
|
115
|
-
export declare function fetchPage(url: unknown, { offset, maxChars, ...options }?: Omit<PublicFetchOptions, "maxBytes"> & Part): Promise<Page>;
|
|
116
|
-
/** The fetch's resolver and fetch in the run's process: the faux module's under `--faux` with one named, else none. */
|
|
117
|
-
export declare function fetchOptions(env: NodeJS.ProcessEnv, fauxEnv: string): Promise<Omit<PublicFetchOptions, "maxBytes" | "signal">>;
|
|
118
|
-
/** A page tool's arguments: `url`, and the part of its text to read (`offset`, `max_chars`). */
|
|
119
|
-
export declare const URL_PARAMETERS: TSchema;
|
package/dist/web-fetch-direct.js
DELETED
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
// The guarded direct fetch: one web page from a public host, as a Page: { ok, url, status, title, text, chars, links,
|
|
2
|
-
// refused?, error? }. web_fetch reads a page through it (web-fetch.ts). The
|
|
3
|
-
// fetch is pi-browser's public-host guard (publicFetch): every redirect hop resolved, checked and dialled at the address
|
|
4
|
-
// checked, http(s) only, a byte cap and a timeout. Before it, a URL on a port other than 80 or 443, or longer than
|
|
5
|
-
// URL_MAX, is refused here as well. A refusal or a failure is a Page with ok false, never a throw, so an agent reads why
|
|
6
|
-
// and a workflow can branch on it.
|
|
7
|
-
//
|
|
8
|
-
// A page is read once (a Read: its first PAGE_READ_MAX_CHARS characters of text, and what is known of the rest), and its
|
|
9
|
-
// text comes in parts of that: about max_chars characters from offset (a Part), counted as the answer holds them, so a
|
|
10
|
-
// part is within its bound whatever the page holds (web_fetch keeps a run's reads, web-fetch.ts). A part that is not the whole page says so: truncated while more
|
|
11
|
-
// follows, with the offset that reads on (next_offset), and the page's whole length (original_chars) where it is known.
|
|
12
|
-
// Every field of a Page is held to a cap (its text, its title, its URL, its links), so one answer is too: serve's
|
|
13
|
-
// host-bounds test holds it to the host's result bound.
|
|
14
|
-
import { pathToFileURL } from "node:url";
|
|
15
|
-
import { linksOf } from "./text-urls.js";
|
|
16
|
-
/** A page's text read for its links (text-urls.ts): web_fetch's Parallel read takes it from here. */
|
|
17
|
-
export { linksOf };
|
|
18
|
-
/** Under `--faux` only: a module whose default export gives `{ lookup, fetchImpl }` for the fetch (the tests' fake
|
|
19
|
-
* network), in place of the system's resolver and undici. */
|
|
20
|
-
export const FAUX_WEB_ENV = "AGENTRUN_SERVE_FAUX_WEB";
|
|
21
|
-
/** The most of a page's text one answer holds (max_chars at most), what it holds when max_chars is not given, and the
|
|
22
|
-
* least max_chars a tool's call may ask for (a smaller one is refused, saying so). */
|
|
23
|
-
export const TEXT_CAP = 50_000;
|
|
24
|
-
export const PAGE_CHARS = 20_000;
|
|
25
|
-
export const MAX_CHARS_MIN = 200;
|
|
26
|
-
/** The most of a page's TEXT web_fetch reads, from either provider: its first million characters (not HTML_READ_CHARS,
|
|
27
|
-
* readPage's cut of the HTML before any text is read). A part reads within it, Parallel is never asked for more of a
|
|
28
|
-
* page than that and one character (web-fetch.ts), and an offset at or past it is refused. */
|
|
29
|
-
export const PAGE_READ_MAX_CHARS = 1_000_000;
|
|
30
|
-
/** A page's links, with its first part only: at most LINKS_CAP of them, in at most LINKS_CHARS characters of the answer. */
|
|
31
|
-
export const LINKS_CAP = 200;
|
|
32
|
-
export const LINKS_CHARS = 20_000;
|
|
33
|
-
export const TITLE_CAP = 500;
|
|
34
|
-
/** The longest URL fetched: a longer one is refused before anything is sent. */
|
|
35
|
-
export const URL_MAX = 4_096;
|
|
36
|
-
export const TIMEOUT_MS = 30_000;
|
|
37
|
-
/** Where readPage cuts a page's HTML before it reads it, scripts and styles aside (not PAGE_READ_MAX_CHARS, the most of
|
|
38
|
-
* the text read): pi-browser's READ_MAX_CHARS, held here so the tool's words name it without loading pi-browser where
|
|
39
|
-
* nothing is fetched (web-fetch-direct-cut.test holds the two equal). */
|
|
40
|
-
export const HTML_READ_CHARS = 1_000_000;
|
|
41
|
-
export const clip = (text, max) => (text.length > max ? `${text.slice(0, max - 1)}…` : text);
|
|
42
|
-
/** Why a tool's call may not read `part`, or undefined: an offset that is not a whole number of 0 or more, a maxChars that
|
|
43
|
-
* is not a number of MAX_CHARS_MIN or more (one past TEXT_CAP reads TEXT_CAP). */
|
|
44
|
-
export function partProblem({ offset, maxChars }) {
|
|
45
|
-
const shown = (value) => (typeof value === "number" ? String(value) : String(JSON.stringify(value)).slice(0, 40));
|
|
46
|
-
if (offset !== undefined && !(Number.isInteger(offset) && offset >= 0))
|
|
47
|
-
return `offset must be a whole number, 0 or more (it was ${shown(offset)})`;
|
|
48
|
-
if (offset !== undefined && offset >= PAGE_READ_MAX_CHARS)
|
|
49
|
-
return `offset must be under ${PAGE_READ_MAX_CHARS.toLocaleString("en-US")}: web_fetch reads a page's first ${PAGE_READ_MAX_CHARS.toLocaleString("en-US")} characters (it was ${shown(offset)})`;
|
|
50
|
-
if (maxChars !== undefined && !(typeof maxChars === "number" && maxChars >= MAX_CHARS_MIN))
|
|
51
|
-
return `max_chars must be ${typeof maxChars === "number" ? "" : "a number, "}at least ${MAX_CHARS_MIN} (it was ${shown(maxChars)})`;
|
|
52
|
-
return undefined;
|
|
53
|
-
}
|
|
54
|
-
/** `part` in range: a whole offset of 0 or more, a whole maxChars of 1 to TEXT_CAP. */
|
|
55
|
-
export const partOf = (part = {}) => ({
|
|
56
|
-
offset: Math.max(0, Math.floor(Number(part.offset) || 0)),
|
|
57
|
-
maxChars: Math.max(1, Math.min(TEXT_CAP, Math.floor(Number(part.maxChars) || PAGE_CHARS))),
|
|
58
|
-
});
|
|
59
|
-
/** Where the longest run of `text` from `start` ends whose JSON string takes at most `budget` characters (less its
|
|
60
|
-
* quotes): a character takes one, a quote, a backslash or a control character JSON writes short (\n, \t) two, any other
|
|
61
|
-
* control character or a lone surrogate six. A surrogate pair is never split. While a character is left the run holds
|
|
62
|
-
* one at least, whatever it takes, so a part always advances (only a budget under six can be passed, by that one). */
|
|
63
|
-
export function partEnd(text, start, budget) {
|
|
64
|
-
let at = start;
|
|
65
|
-
for (let used = 0; at < text.length;) {
|
|
66
|
-
const unit = text.charCodeAt(at);
|
|
67
|
-
let width = 1;
|
|
68
|
-
let cost = 1;
|
|
69
|
-
if (unit >= 0xd800 && unit <= 0xdbff && at + 1 < text.length && (text.charCodeAt(at + 1) & 0xfc00) === 0xdc00)
|
|
70
|
-
[width, cost] = [2, 2];
|
|
71
|
-
else if ((unit & 0xf800) === 0xd800)
|
|
72
|
-
cost = 6;
|
|
73
|
-
else if (unit === 0x22 || unit === 0x5c || unit === 0x08 || unit === 0x09 || unit === 0x0a || unit === 0x0c || unit === 0x0d)
|
|
74
|
-
cost = 2;
|
|
75
|
-
else if (unit < 0x20)
|
|
76
|
-
cost = 6;
|
|
77
|
-
if (used + cost > budget && at > start)
|
|
78
|
-
break;
|
|
79
|
-
used += cost;
|
|
80
|
-
at += width;
|
|
81
|
-
}
|
|
82
|
-
return at;
|
|
83
|
-
}
|
|
84
|
-
/** `text` as read: `whole` says whether it is all of the page's text, so its length is the page's (a provider that cut
|
|
85
|
-
* it, or readPage's cut of its HTML, gives none). The cut at PAGE_READ_MAX_CHARS never splits a surrogate pair: where
|
|
86
|
-
* it would, it is one unit short. */
|
|
87
|
-
export function textOf(text, whole) {
|
|
88
|
-
const beyond = text.length > PAGE_READ_MAX_CHARS;
|
|
89
|
-
const split = (text.charCodeAt(PAGE_READ_MAX_CHARS - 1) & 0xfc00) === 0xd800 && (text.charCodeAt(PAGE_READ_MAX_CHARS) & 0xfc00) === 0xdc00;
|
|
90
|
-
return { text: beyond ? text.slice(0, PAGE_READ_MAX_CHARS - (split ? 1 : 0)) : text, beyond, ...(whole ? { length: text.length } : {}) };
|
|
91
|
-
}
|
|
92
|
-
/** The part of a page's text an answer holds, and what it says of the rest. */
|
|
93
|
-
export function pageText({ text, beyond, length }, part = {}) {
|
|
94
|
-
const { offset, maxChars } = partOf(part);
|
|
95
|
-
const start = Math.min(offset, text.length);
|
|
96
|
-
const end = partEnd(text, start, maxChars);
|
|
97
|
-
const held = text.slice(start, end);
|
|
98
|
-
const more = end < text.length;
|
|
99
|
-
// The last part a read reaches of a page that goes on past it.
|
|
100
|
-
const last = !more && beyond;
|
|
101
|
-
return {
|
|
102
|
-
text: held,
|
|
103
|
-
chars: held.length,
|
|
104
|
-
...(more ? { truncated: true } : {}),
|
|
105
|
-
...(length !== undefined && (offset > 0 || more || last) ? { original_chars: length } : {}),
|
|
106
|
-
...(last ? { beyond_read: true } : {}),
|
|
107
|
-
...(offset > 0 ? { offset } : {}),
|
|
108
|
-
...(more ? { next_offset: end } : {}),
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
/** A page's links as its first part holds them: in page order, at most LINKS_CAP of them, each one that fits in what is
|
|
112
|
-
* left of LINKS_CHARS (as JSON, with its comma); and how many of them that left out, when any. */
|
|
113
|
-
export function linksWithin(links) {
|
|
114
|
-
const kept = [];
|
|
115
|
-
let used = 0;
|
|
116
|
-
let omitted = 0;
|
|
117
|
-
for (const link of links) {
|
|
118
|
-
const cost = JSON.stringify(link).length + 1;
|
|
119
|
-
if (kept.length === LINKS_CAP || used + cost > LINKS_CHARS) {
|
|
120
|
-
omitted += 1;
|
|
121
|
-
continue;
|
|
122
|
-
}
|
|
123
|
-
kept.push(link);
|
|
124
|
-
used += cost;
|
|
125
|
-
}
|
|
126
|
-
return { links: kept, ...(omitted ? { links_omitted: omitted } : {}) };
|
|
127
|
-
}
|
|
128
|
-
/** The part `part` names of a page read once, as a Page: the links with its first part only. */
|
|
129
|
-
export function partOfRead({ text, beyond, length, links, links_omitted, html_chars, converted, ...page }, part = {}) {
|
|
130
|
-
const first = partOf(part).offset === 0;
|
|
131
|
-
return { ...page, ...pageText({ text, beyond, length }, part), ...(html_chars === undefined ? {} : { html_chars }), ...(converted ? { converted } : {}), links: first ? links : [], ...(first && links_omitted ? { links_omitted } : {}) };
|
|
132
|
-
}
|
|
133
|
-
/** A page a provider read for us as markdown (Parallel's extract, keyless Firecrawl's scrape, keyless Exa's fetch), as a
|
|
134
|
-
* Read: its url, title and status, the text (`whole`: all of the page's, so its length is the page's) and the links it
|
|
135
|
-
* holds. */
|
|
136
|
-
export const markdownRead = (url, title, text, whole, status = 200) => ({ ok: true, url: clip(url, URL_MAX), status, title: clip(title, TITLE_CAP), ...textOf(text, whole), ...linksWithin(linksOf(text)) });
|
|
137
|
-
/** The page at `url`, read once through the guarded direct fetch, or why it was not. The rest of `options` are the
|
|
138
|
-
* guard's resolver and fetch, for a test; a signal ends it early. */
|
|
139
|
-
export async function readDirect(url, options = {}) {
|
|
140
|
-
const failed = (fields) => ({ ok: false, url: typeof url === "string" ? clip(url, URL_MAX) : "", status: 0, title: "", text: "", links: [], ...fields });
|
|
141
|
-
if (typeof url !== "string" || !URL.canParse(url))
|
|
142
|
-
return failed({ error: "url is not an absolute URL" });
|
|
143
|
-
if (url.length > URL_MAX)
|
|
144
|
-
return failed({ error: `url is longer than ${URL_MAX.toLocaleString("en-US")} characters` });
|
|
145
|
-
const { port, username, password } = new URL(url);
|
|
146
|
-
if (port && port !== "80" && port !== "443")
|
|
147
|
-
return failed({ refused: `refused: port ${port} is not 80 or 443` });
|
|
148
|
-
// A URL's user name and password are never sent anywhere: a fetch refuses them, never a third party's read of them.
|
|
149
|
-
if (username || password)
|
|
150
|
-
return failed({ refused: "refused: the URL holds a user name or password" });
|
|
151
|
-
const { BrowserFailureError, publicFetch, readPage } = await import("@parcha/pi-browser");
|
|
152
|
-
try {
|
|
153
|
-
const signal = AbortSignal.any([AbortSignal.timeout(TIMEOUT_MS), ...(options.signal ? [options.signal] : [])]);
|
|
154
|
-
const answer = await publicFetch(url, { ...options, signal });
|
|
155
|
-
if (answer.body === null)
|
|
156
|
-
return failed({ url: clip(answer.url, URL_MAX), status: answer.status, error: `the page answered ${answer.status}` });
|
|
157
|
-
// The caller's stop reaches the read too; the timeout is the network's, and readPage keeps its own deadline.
|
|
158
|
-
const read = /html/i.test(answer.contentType) ? await readPage(answer.body, answer.url, options.signal ? { signal: options.signal } : {}) : null;
|
|
159
|
-
const page = read ?? { title: "", text: answer.body, links: linksOf(answer.body) };
|
|
160
|
-
return { ok: true, url: clip(answer.url, URL_MAX), status: answer.status, title: clip(page.title, TITLE_CAP), ...textOf(page.text, !read?.truncated), ...(read?.truncated ? { html_chars: read.html_chars } : {}), ...(read?.converted === "text" ? { converted: "text" } : {}), ...linksWithin(page.links) };
|
|
161
|
-
}
|
|
162
|
-
catch (error) {
|
|
163
|
-
if (error instanceof BrowserFailureError && error.failure.code === "refused")
|
|
164
|
-
return failed({ refused: error.message });
|
|
165
|
-
return failed({ error: error?.message ?? String(error) });
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
/** The part `options` names of the page at `url`, as a Page, read through the guarded direct fetch. */
|
|
169
|
-
export async function fetchPage(url, { offset, maxChars, ...options } = {}) {
|
|
170
|
-
const read = await readDirect(url, options);
|
|
171
|
-
return read.ok ? partOfRead(read, { offset, maxChars }) : read;
|
|
172
|
-
}
|
|
173
|
-
/** The fetch's resolver and fetch in the run's process: the faux module's under `--faux` with one named, else none. */
|
|
174
|
-
export async function fetchOptions(env, fauxEnv) {
|
|
175
|
-
const faux = env[fauxEnv] ? env[FAUX_WEB_ENV] : undefined;
|
|
176
|
-
if (!faux)
|
|
177
|
-
return {};
|
|
178
|
-
return (await import(pathToFileURL(faux).href)).default();
|
|
179
|
-
}
|
|
180
|
-
/** A page tool's arguments: `url`, and the part of its text to read (`offset`, `max_chars`). */
|
|
181
|
-
export const URL_PARAMETERS = {
|
|
182
|
-
type: "object",
|
|
183
|
-
additionalProperties: false,
|
|
184
|
-
properties: {
|
|
185
|
-
url: { type: "string", minLength: 1, description: "An absolute http(s) URL." },
|
|
186
|
-
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." },
|
|
187
|
-
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")}).` },
|
|
188
|
-
},
|
|
189
|
-
required: ["url"],
|
|
190
|
-
};
|