import type { Cache } from "../pipeline/types.ts"; import { type RuleTool } from "./tools.ts"; /** * Reading a site the implementer chose, when the corpus cannot answer. * * NO SITE SHIPS WITH THIS PACKAGE. rulekit names none, recommends none, and * endorses none. Every host below arrives from the application that mounted the * agent, and that application's owner is responsible for the site's terms of use * and for how often they read it. * * THE CORPUS IS STILL THE SOURCE OF TRUTH. A page read here is a second source * and is labelled as one, in the prose the model writes and in the trace the * browser renders. The instruction block in `instructions/references.md` is what * makes the prose half hold, and it is added ONLY when sites are configured, * because without it a fetched page reads as though it were the rules. * * A model chooses the address, so treat every one as untrusted input. The ten * rules `fetchReference` enforces are listed on that function, and each is * tested. The one that is easiest to leave out is the redirect: a site that * answers a request with a redirect to an address inside the network hosting * this server turns a rules assistant into a way to read that network. * * THIS PACKAGE READS NO robots.txt. A site states its own rules there, and an * operator who accepts a site's terms needs somewhere to record them, so * `ReferenceSite.disallowPaths` is that place. */ export type ReferenceSite = { /** What an answer calls this site, e.g. "Riftbound FAQ". */ name: string; /** The host, with no scheme and no path. This host and its subdomains only. */ host: string; /** One sentence a model reads to choose the site: what it holds, and who publishes it. */ describes: string; /** * False by default, and false is the safe reading. * * A community site is one reader's careful reading of the rules. Marking one * official tells a reader it carries the publisher's authority, which is a * claim they cannot check and would not otherwise make. */ official?: boolean; /** * How a piece's name becomes a page, e.g. "/cards/{slug}". * * `{slug}` becomes the name in lower case, with runs of non-letters turned * into single hyphens. This is a hint for the model, not a guarantee: the site * decides its own addresses, and a built address can still be absent. */ cardPath?: string; /** * Address prefixes on this host that must never be read, e.g. `["/api/"]`. * * **This package reads no robots.txt.** A site states its own rules there, and * honouring them is the operator's job, so this is where you write them down. * A site that disallows `/api/` needs that prefix here, or a model that builds * such an address gets it fetched. * * A prefix matches the start of the path. Matching is case-insensitive. */ disallowPaths?: string[]; }; export type ReferenceOptions = { /** The sites this agent may read. An empty list adds no tools at all. */ sites: ReferenceSite[]; /** Defaults to the global fetch. A test passes a stub, so no test needs a network. */ fetchImpl?: typeof fetch; /** Turns a fetched page into plain text. Defaults to `stripHtml`. */ readPage?: (body: string, contentType: string) => string; /** Default 5000. A slow site must not hold a reader's question open. */ timeoutMs?: number; /** Default 200000 characters. Enforced while reading, not after. */ maxBytes?: number; /** Default 3. Past it, the model is told to answer from what it has. */ maxFetchesPerTurn?: number; /** Where fetched pages are held. Without one, every question refetches. */ cache?: Cache; /** Default one hour. */ cacheTtlSeconds?: number; /** * Sent as the User-Agent header. * * It names this package and leaves room for a contact address, so a site * operator can see who is reading and block it. A tool that reads somebody * else's site anonymously gives them no way to say no. */ userAgent?: string; }; /** What one successful read returns, and what the trace and the model both see. */ export type ReferencePage = { site: string; url: string; title: string | null; text: string; official: boolean; /** True when `maxBytes` cut the page short, so the model does not read an ending that is not there. */ truncated: boolean; }; /** A piece's name as a URL slug: lower case, single hyphens, no leading or trailing one. */ export declare function slugify(name: string): string; /** * A page's readable text, with no HTML parser and no new dependency. * * This is deliberately crude. It removes the four elements whose contents are * never the article, drops the remaining tags, and decodes the five entities * that matter. A frequently-asked-questions page reads well enough from that. * * A site whose text does not survive this needs a real parser, and an * implementer supplies one through `readPage`. Adding a parser here would put a * dependency in every install to serve the installs that do not need it. */ export declare function stripHtml(body: string, contentType?: string): string; /** The site an address belongs to, or null when no configured site owns it. */ export declare function siteFor(url: URL, sites: ReferenceSite[]): ReferenceSite | null; /** * A refused or failed read of a reference page. * * Not called `ReferenceError`: that is a JavaScript built-in about an undefined * variable, and a catch block written for one would silently swallow the other. */ export declare class ReferenceFetchError extends Error { } /** * Fetch one page from an allowlisted site. * * The ten rules, in the order they run: * * 1. The scheme must be https. * 2. The host must equal a configured host, or be one of its subdomains. * 3. The path must not start with a prefix the operator refused. * 4. Redirects are followed by hand, at most once, and only to an address that * passes rules 2 and 3. * 5. No credentials, so nothing this server holds is sent to somebody's site. * 6. A timeout, so one slow site cannot hold a reader's question open. * 7. The content type must be readable text. * 8. A byte cap, applied WHILE reading rather than after. * 9. A per-turn count, enforced by the caller. * 10. A cache by address, so a popular page is read once. */ export declare function fetchReference(raw: string, options: ReferenceOptions, seen?: Set): Promise; /** * The two tools an agent gets when an implementer configured a reference site. * * An empty site list returns no tools. A tool that can read nothing costs a step * and teaches the model that these tools return nothing, which is the same rule * that withholds a banned-list tool from a corpus with no banned list. * * CALL THIS ONCE PER TURN, not once per process. The fetch count lives in the * closure it returns, so a set of tools built once and reused would spend its * whole budget on the first question and refuse every question afterwards for * the life of the process. Building them is cheap: no store is read, and the * corpus tools that DO read one are still built once and cached. */ export declare function defineReferenceTools(options: ReferenceOptions): RuleTool[]; //# sourceMappingURL=references.d.ts.map