agents
Version:
A home for your AI agents
93 lines (92 loc) • 2.84 kB
JavaScript
import { a as WebFetchError, d as MAX_WEB_FETCH_URL_LENGTH, t as createWebFetchToolCore } from "../../../tool-CxKsvxjm.js";
import { int, obj, opt, str } from "@earendil-works/pi-schema";
import { StringEnum } from "@earendil-works/pi-ai";
//#region src/web/fetch/tools/pi.ts
/**
* `agents/webfetch/pi` — the `web_fetch` tool for the pi harness.
*
* @beta
*/
/** The model's input schema. pi already coerces `"20000"` to `20000`. */
function webFetchParameters() {
return obj({
url: str({
minLength: 1,
maxLength: MAX_WEB_FETCH_URL_LENGTH,
description: "The http(s) URL to read."
}),
offset: opt(int({
minimum: 0,
description: "Character offset into the content, to continue a long page from the previous result's offset. Default 0."
})),
format: opt(StringEnum(["auto", "raw"], { description: "\"auto\" (default) converts to the most readable form; \"raw\" returns the body as served." }))
}, { open: true });
}
/**
* Create a pi tool that reads a URL as Markdown, JSON, or text. Install it
* on a registry like any other pi tool.
*
* The model chooses the URL, the window (`offset`), and the format; the
* host chooses the URL policy, size limits, and window size. The tool is
* `replay: "safe"`: a fetch interrupted mid-call (say, by an eviction) runs
* again when the session recovers. Completed results are stored and not
* fetched again.
*
* @example
* ```ts
* import { createRegistry, Harness } from "@earendil-works/pi-durable";
* import { webFetchTool } from "agents/webfetch/pi";
*
* // In PiHarness's factory, so each object gets its own registry.
* harness: ({ storage, context, onWake }) => {
* const registry = createRegistry();
* registry.install({
* name: "tools",
* tools: [webFetchTool({ binding: this.env.AI })]
* });
* return Harness.open(storage, { models, registry, onWake }, context);
* }
* ```
*/
function webFetchTool(options) {
const core = createWebFetchToolCore(options);
return {
name: core.name,
description: core.description,
parameters: webFetchParameters(),
replay: "safe",
async execute(input, _api, context) {
const run = await core.run(input, { signal: context.abortSignal });
if (run.ok) return {
output: [{
type: "text",
text: run.text
}],
details: {
ok: true,
output: run.output
}
};
const details = {
ok: false,
message: run.error.message,
status: run.error.status,
code: run.error.code,
retryable: run.error.retryable
};
if (run.error.url !== void 0) details.url = run.error.url;
if (run.error.contentType !== void 0) details.contentType = run.error.contentType;
return {
output: [{
type: "text",
text: run.text
}],
isError: true,
details
};
}
};
}
//#endregion
export { WebFetchError, webFetchTool };
//# sourceMappingURL=pi.js.map