Files
CatoandClaude 30c7137d60 feat(ask): research mode for analytical and forward-looking questions (#248)
The owner, 2026-10-25: asked about future demand for neon, the Ask read six
pages and answered with one US table. Analytical questions (outlooks,
forecasts, what-ifs, comparisons of impact, "when could X be commercial") are
now routed to a research mode:

- route.ts classifies `analysis`; loop.ts hands those to research.ts
- research-plan.ts: one model call writes 3-6 sub-questions with a query each,
  validated in code (listed corpus tools, their own arguments); a code plan by
  question kind when the call fails or times out (7 s)
- research-read.ts: every sub-question searched in parallel through lookUp
  (SSRF-safe readPage, PDFs), one 15 s deadline, passages kept round-robin,
  one URL once, two per site, primary/trade/analyst before farms, as [W#]
- research-shape.ts: bottom line first, then What is established, What drives
  it, Scenarios, What would change this, Confidence and gaps, Sources (from
  the citations); uncited forecasts dropped; unanswered sub-questions said by
  code and filed through gaps.ts
- scenario-calc.ts: the model names cited inputs and cases, code checks every
  input against its passage, computes low/base/high with the formula shown and
  draws a line chart through the chat_ui chart block
- default deny holds reasoning sections to the labelled-background rule
- api/chat: "Reading <site>…" now reaches the reader (onRead was dropped);
  analysis answers get more output room
- tests with fakes for the five owner questions; ask-evals rows; a script to
  ask them live after deploy (scripts/ux/ask-live.ts)


Claude-Session: https://claude.ai/code/session_018WZnBnaLP6RhwFkLxRTKgE

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-25 12:59:15 +01:00

119 lines
4.4 KiB
TypeScript

/**
* Ask the live site the research-mode questions and keep what came back.
*
* pnpm exec tsx scripts/ux/ask-live.ts # production, the five questions
* pnpm exec tsx scripts/ux/ask-live.ts --base http://localhost:3000
* pnpm exec tsx scripts/ux/ask-live.ts --only neon # questions containing "neon"
*
* Posts each question to /api/chat exactly as the Ask panel does (same
* origin, the free model chain, the reader's hourly and daily limits apply:
* five questions spend five of them), reads the event stream, and writes
* scripts/ux/reports/ask-live-<date>.md with, per question: the steps the
* reader saw (Planning…, Searching: …, Reading …, Writing…), seconds to the
* first word and to the end, model calls, the answer as the reader got it,
* and every web page read with its date. Nothing is graded here: read it.
*
* See docs/architecture/2026-10-25-research-mode.md.
*/
import { mkdirSync, writeFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { parseArgs } from 'node:util';
export const RESEARCH_QUESTIONS = [
'What do you think will be the demand for neon in the future? Give sources.',
'What is the demand outlook for helium over the next decade?',
'When could small modular reactors be commercial?',
'What happens to chip supply if Ukraine neon is cut again?',
'Compare gallium and germanium export controls impact',
];
interface Event {
type: string;
text?: string;
label?: string;
error?: string;
data?: {
answer: string;
web?: { title: string; url: string; published?: string }[];
timing?: { firstText?: number; total: number; calls?: number };
};
}
async function ask(base: string, question: string): Promise<string> {
const started = Date.now();
const res = await fetch(new URL('/api/chat', base), {
method: 'POST',
headers: { 'content-type': 'application/json', origin: new URL(base).origin },
body: JSON.stringify({ question, onPath: '/chat', model: 'auto', history: [] }),
});
if (!res.ok || !res.body) return `**HTTP ${res.status}**: ${await res.text()}`;
const steps: string[] = [];
let firstWord: number | undefined;
let done: Event['data'];
let error = '';
let buffer = '';
const decoder = new TextDecoder();
for await (const chunk of res.body) {
buffer += decoder.decode(chunk as Uint8Array, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop() ?? '';
for (const line of lines.filter(Boolean)) {
const e = JSON.parse(line) as Event;
if (e.type === 'status' && e.text) steps.push(`status: ${e.text}`);
if (e.type === 'tool' && e.label) steps.push(e.label);
if (e.type === 'delta' && firstWord === undefined) firstWord = Date.now() - started;
if (e.type === 'done') done = e.data;
if (e.type === 'error') error = e.error ?? 'error';
}
}
const secs = (ms?: number) => (ms === undefined ? '—' : `${(ms / 1000).toFixed(1)} s`);
return [
`First word ${secs(firstWord)} · done ${secs(Date.now() - started)} · model calls ${done?.timing?.calls ?? '—'}`,
'',
'<details><summary>Steps</summary>',
'',
...steps.map((s) => `- ${s}`),
'',
'</details>',
'',
error ? `**Error:** ${error}` : (done?.answer ?? '**No answer.**'),
'',
'**Web pages read**',
'',
...(done?.web ?? []).map(
(w, i) => `${i + 1}. [W${i + 1}] [${w.title}](${w.url}) — ${w.published ?? 'undated'}`,
),
].join('\n');
}
async function main() {
const { values } = parseArgs({
options: {
base: { type: 'string', default: 'https://substrata.orangecat.ch' },
only: { type: 'string' },
},
});
const base = values.base!;
const questions = RESEARCH_QUESTIONS.filter(
(q) => !values.only || q.toLowerCase().includes(values.only.toLowerCase()),
);
const sections: string[] = [];
for (const q of questions) {
process.stdout.write(`Asking: ${q}\n`);
const out = await ask(base, q).catch((e: unknown) => `**Failed:** ${String(e)}`);
sections.push(`## ${q}\n\n${out}`);
}
const date = new Date().toISOString().slice(0, 10);
const here = dirname(fileURLToPath(import.meta.url));
const file = join(here, 'reports', `ask-live-${date}.md`);
mkdirSync(dirname(file), { recursive: true });
writeFileSync(
file,
`# Ask, research mode, live — ${base}, ${date}\n\n${sections.join('\n\n')}\n`,
);
process.stdout.write(`Wrote ${file}\n`);
}
void main();