@framers/agentos-ext-public-sources

中文文档
0.2.3发布于 2026/11/12Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 3万

Public sources for AgentOS citation checks: English Wikipedia's search and page HTML as attributed passages

原始使用文档
文档目录11 节
  1. Installation
  2. Example
  3. Reading any page
  4. Outcomes
  5. Wikimedia's limits and the User-Agent
  6. Sizes and the deadline
  7. Attribution
  8. The page path
  9. A document's fields
  10. API
  11. License

@framers/agentos-ext-public-sources

Public sources for AgentOS citation checks. AgentOS's CitationVerifier grades a claim against the passages it is given; this library fetches those passages from a public source and keeps their attribution. It ships English Wikipedia's provider behind a PublicSourceProvider interface: a search by a short topic phrase, a read of a hit's page HTML by its key, and the article as blocks of plain text with the page's own title, the reference markers, maintenance tags, tables, figures, quotations, code, mathematics and navigation removed.

citationSources(document, cut) turns a fetched article into passages, one for each sentence your cut finds in each block. A passage is a VerificationSource (content, title, url) that also carries the site, the article's key, its revision, the time it was read, its licence, and the sentence's block and offsets. The library holds no sentence rule of its own: pass the one the rest of your pipeline uses, so the claims and the passages are cut the same way.

Installation

npm install @framers/agentos-ext-public-sources

It needs Node.js 22 or later, as @framers/agentos does. Its one runtime dependency is htmlparser2; @framers/agentos is a peer.

The example below takes its sentence rule, sentenceSpans, from the values entry of the grounding guard, a package of its own. Install it beside this one to run the example as written, or pass any function that answers a block's sentences as offsets:

npm install @framers/agentos-ext-grounding-guard

Example

import { citationSources, SourceLimiter, WikipediaSourceProvider } from '@framers/agentos-ext-public-sources';
import { sentenceSpans } from '@framers/agentos-ext-grounding-guard/values';

const wikipedia = new WikipediaSourceProvider({
  client: { name: 'MyApp', version: '1.2.0', contact: 'https://example.org/contact' },
  limiter: new SourceLimiter(), // one for every caller on this host: Wikimedia's limits apply per client
});
const found = await wikipedia.search('Treaty of Versailles', { limit: 2 });
if (found.kind === 'hits' && found.hits[0]) {
  const page = await wikipedia.read(found.hits[0]);
  if (page.kind === 'document') {
    const passages = citationSources(page.document, sentenceSpans); // VerificationSource[] with attribution
  }
}

Reading any page

htmlToBlocks(html, { mode: 'page' }) reads a page that is not an encyclopedia article, such as a page a citation names. It keeps the tables, figures, quotations, code and notes the page shows, and makes each table cell, caption, quotation and code block a block of its own, so two cells never read as one sentence. It still drops the page's own title, scripts, styles, templates, inline SVG, mathematics, elements with the hidden attribute, and Wikipedia's reference markers, maintenance tags, reference lists and navigation boxes. The article mode, the default, is the one the Wikipedia provider reads with.

import { htmlToBlocks } from '@framers/agentos-ext-public-sources';

const html = '<table><tr><th>Plan</th><td>$20 per month</td></tr></table>';
htmlToBlocks(html, { mode: 'page' }); // ['Plan', '$20 per month']
htmlToBlocks(html); // []: the article mode drops tables

Outcomes

search answers one of:

  • { kind: 'hits', hits }: at most limit hits (2 by default), each with a key and a title;
  • { kind: 'limited' }: the limiter refused, or your onSend did, and nothing was sent;
  • { kind: 'failed', status }: the HTTP status when an answer came, null for a transport failure, the deadline, or a phrase no address can carry (one holding half of a surrogate pair), for which nothing is sent.

read answers { kind: 'document', document }, { kind: 'limited' }, or { kind: 'skipped', status } for an answer that is not 200 HTML, one over the size caps, a transport failure, the deadline, or a key no address can carry. Neither method throws for a refusal or a failure; aborting your own signal throws its reason.

onSend runs once a search has its slot and before it is sent. Throw new LimiterRefused('caller') from it to hold searches to a quota of your own; the search then answers limited with nothing sent and none of the limiter's starts spent. The limiter stamps a search's start when the search is sent, after onSend has settled, so an onSend that takes a while never brings two requests closer than the spacing. A search can still answer limited after onSend has run, with nothing sent: when a 429 or a 503 blocked the limiter meanwhile, or when the spacing would take its wait past the 2 seconds (waitMs); the time onSend takes is not counted as waiting.

Wikimedia's limits and the User-Agent

Every request runs through a SourceLimiter: at most 3 at once, starts at least 250 ms apart (under 5 a second), at most 200 starts in any 60 seconds, a wait of at most 2 seconds for a slot or the spacing, and, after a 429 or a 503, a block until Retry-After has passed (5 seconds when the header is missing, an hour at most). The 3 at once, the 5 a second, the 200 a minute and the 5 seconds are what Wikimedia's rate limits and robot policy give an unauthenticated client with a compliant User-Agent; the 2-second wait and the hour's ceiling are this library's own.

The limits apply per client, and a limiter counts only the requests of its own process. A provider makes a limiter of its own when none is passed, so give every provider in a process the same one, and when several processes on one host read Wikipedia, send their requests through one of them.

The provider sends the User-Agent <name>/<version> (<contact>), the form of Wikimedia's User-Agent policy with its library part left out, as the policy allows. Give your client's own name and version and a contact the policy accepts: a website, an email address or a wiki user. Every request also sends Accept-Encoding: gzip, as the robot policy asks, follows no redirect and sends no cookie.

Sizes and the deadline

OptionDefaultWhat it bounds
timeoutMs3,000each request, from sending it to reading its body
searchMaxBytes65,536a search answer's body
pageMaxSentBytes1,500,000the content-length a page answer declares
pageMaxBytes6,000,000a page's body once unzipped
textBudget300,000the characters of text kept from a page

A page answer may declare no length (a chunked one never does), so pageMaxBytes is the cap that bounds every read.

Attribution

Wikipedia's text is under CC BY-SA 4.0, and every document and passage carries it as licence (WIKIPEDIA_LICENCE). Wherever you show a passage, show with it:

  • the sentence, as content holds it;
  • a link to the article (url), named by its title;
  • "CC BY-SA 4.0", linked to licence.url;
  • that the text was modified: the library removes reference markers and maintenance tags and collapses white space, and CC BY-SA 4.0 asks a reuser to "indicate if You modified the Licensed Material" (section 3(a)(1)(B) of its legal code).

Wikipedia's page on reusing its content describes the attribution in full. revision and readAt name the exact text a passage came from.

The page path

The provider reads page HTML from /api/rest_v1/page/html/{key} (PAGE_PATH), the cached interface the robot policy names. RESTBase, the service behind it, is being deprecated: pass pagePath: '/w/rest.php/v1/page/{key}/html' to read the same HTML from MediaWiki's own REST API. A read uses the search hit's key, so it follows no redirect; a title that redirects answers 307 at both paths, and the read answers skipped.

A document's fields

A SourceDocument is what a provider's read answers: site (the source's name as a reader sees it), key, title, url (the address an attribution links to), revision (null when the source names none), readAt (when it was fetched, in milliseconds), licence (name and url) and blocks. Two fields are optional, for a source that reads them from the page: publisher, the site's name as the page states it, and pageDate, the page's own date in milliseconds. Wikipedia's provider sets neither; @framers/agentos-ext-citation-check's web page source sets each where a page states it.

API

ExportWhat it is
PublicSourceProviderThe interface a public source implements: site, search(phrase, options), read(hit, options)
WikipediaSourceProviderEnglish Wikipedia's provider, or another wiki's through origin
SourceLimiter, LimiterRefused, retryAfterMsThe limiter, its refusal and the Retry-After reading
htmlToBlocks(html, { budget, mode })HTML as blocks of plain text, white space collapsed, in NFC: an article's prose (article, the default) or everything a page shows (page)
citationSources(document, cut)A fetched article as passages with their attribution and place
wikipediaUserAgent, articleUrl, WIKIPEDIA_LICENCE, SEARCH_PATH, PAGE_PATHThe User-Agent, the article's address, the licence and the paths the provider uses
SourceHit, SourceDocument, CitationSource, SourceLicence, SearchOutcome, ReadOutcome, CallOptions, WikipediaOptions, WikipediaClient, LimiterOptions, BlockMode, HtmlToBlocksOptionsThe types of the hits, documents, passages, outcomes and options

License

Apache-2.0

版本与发布记录

@framers/agentos-ext-public-sources 共发布 6 个版本,首次发布于 2026/10/10;过去 12 个月发布了 6 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.3latest2026/11/124 天前
0.2.22026/11/0115 天前
0.2.12026/10/2125 天前
0.2.02026/10/161 个月前
0.1.02026/10/101 个月前
0.0.0-stage2026/10/101 个月前

关于 @framers/agentos-ext-public-sources 的常见问题

@framers/agentos-ext-public-sources 是什么?

Public sources for AgentOS citation checks: English Wikipedia's search and page HTML as attributed passages

如何安装 @framers/agentos-ext-public-sources?

在项目目录运行 npm install @framers/agentos-ext-public-sources,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@framers/agentos(>=0.10.40)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@framers/agentos-ext-public-sources 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@framers/agentos-ext-public-sources 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@framers/agentos-ext-public-sources 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@framers/agentos-ext-public-sources 可以商用吗?

@framers/agentos-ext-public-sources 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@framers/agentos-ext-public-sources 的下载量有多少?

根据 npm 官方下载统计,@framers/agentos-ext-public-sources 在 2026/10/16 – 2026/11/14 这 30 天内被下载 30,215 次,日均约 1,007 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@framers/agentos-ext-public-sources 还在维护吗?

最新版本 0.2.3 发布于 2026/11/12(4 天前);至今共发布 6 个版本,其中过去 12 个月发布了 6 个;首次发布于 2026/10/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。