Files
CatoandClaude Opus 5.5 8e85c7e7b4 feat(ask): search and read the web up front for open-world questions (#244)
* feat(ask): search and read the web up front for open-world questions

The owner read the recent chats: the Ask behaved like a search over the
corpus, not like a language model that researches. Asked for Kazatomprom's
2026 guidance it opened with "The Substrata corpus does not track..." and
never gave the figure; asked how competitors' share prices moved this month
it said Substrata "does not record" equity moves, with web leads unused.

- route.ts: a pure router decides per question whether the web is needed
  (news, prices, guidance, what someone said, competitors, people,
  explanations, and by default anything but questions about Substrata
  itself or off-topic chat). runPlan searches it in parallel with the
  corpus lookups; "this month"/"today" become dated queries.
- Pages are read, not snippets: top four in parallel, PDFs as text via
  unpdf behind readPage's SSRF checks, the matching passages verbatim with
  the page's date.
- Prompt: answer first and directly from corpus rows, [W#] passages and
  marked general knowledge; figures only from a row or a dated passage;
  never open with what the corpus lacks.
- Guards: a figure cited [W#] must be in that passage (sent back once,
  then dropped); marked background survives default deny without figures
  or link claims; corpus-gap openings move to the end and "Unchecked web
  results" headings go. Corpus table guards unchanged.
- UI: [W#] renders as a chatkit source chip linked to the page with its
  site and date; "Web pages read · not checked by Substrata" under it;
  "Reading <domain>…" steps while it works.
- Evals and tests for the router, page reading (fake fetch), the PDF
  path, the web-figure guard, answer-first, and the six new questions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018WZnBnaLP6RhwFkLxRTKgE

* docs(ask): record the open-web Ask policy and why

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018WZnBnaLP6RhwFkLxRTKgE

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-24 18:49:23 +02:00

53 lines
1.9 KiB
TypeScript

/**
* A web page cited in an answer, shown as a source chip linked to the page.
*
* The model cites a passage it read as `[W2]`. chatkit draws `[F#]`/`[D#]`
* handles as chips with a sources row under the answer (and a "Sources" sheet
* on a phone), and drops a handle it has no entry for. So, for display only,
* each `[W#]` the answer cites becomes a `[D#]` handle whose entry names the
* page's site, its title and date, and that Substrata has not checked it —
* the corpus-vs-web distinction stays visible claim by claim: corpus claims
* link to site pages, web claims to the page they came from. `[F#]` is never
* used: it means a corpus row someone accepted.
*/
import type { CitationMap } from '@bitbaum/chatkit/react';
import { domainOf } from '../chat-web-passages';
/** The fields of a web page read for an answer that a chip needs. */
interface WebPage {
title: string;
url: string;
published?: string;
}
export function webCitations(
content: string,
web: readonly WebPage[] | undefined,
): { content: string; citations?: CitationMap } {
if (!web?.length || !/\[W\d+/.test(content)) return { content };
const citations: CitationMap = {};
// "[W1]", "[W1, W3]" and "[W1][W2]" all become chatkit handles.
const text = content.replace(/\[(W\d+(?:,\s*W\d+)*)\]/g, (whole, inner: string) => {
const ids = inner
.split(/,\s*/)
.map((id) => Number(id.slice(1)))
.filter((n) => web[n - 1]);
if (!ids.length) return '';
for (const n of ids) {
const page = web[n - 1];
citations[`D${n}`] = {
label: domainOf(page.url),
detail: [
page.title,
page.published ? `dated ${page.published}` : 'undated',
'web page, not checked by Substrata',
].join(' · '),
href: page.url,
};
}
return `[${ids.map((n) => `D${n}`).join(', ')}]`;
});
return { content: text, citations };
}