Nekochu commited on
Commit
54009a5
·
verified ·
1 Parent(s): 87e03d2

Page reading: stub detection, defuddle.md and Wayback tiers

Browse files
Files changed (2) hide show
  1. index.html +1 -1
  2. tools.js +19 -17
index.html CHANGED
@@ -42,7 +42,7 @@
42
  <p><b>Agent.</b> The prompt, the two tools (<code>search</code>, <code>webterminal</code>), the 150-call budget and the final-answer warning are from
43
  <a href="https://github.com/allenai/dr-tulu2" target="_blank" rel="noopener">allenai/dr-tulu2</a>. Its live mode uses paid Serper and Jina keys; here there is no server at all:
44
  <code>search</code> reads DuckDuckGo results (Brave as backup) and <code>browse</code> reads pages through <a href="https://jina.ai/reader" target="_blank" rel="noopener">Jina Reader</a>'s anonymous, browser-friendly endpoint
45
- (it renders JavaScript and gets past most bot walls), with Wikipedia and OpenAlex search and the Wayback Machine as fallbacks.
46
  The terminal is a small safe runner written in JavaScript for <code>head tail rg grep sed wc sort uniq cut tr nl</code>; there is no shell. Private and local addresses are refused, as are huggingface.co pages (as upstream blocks them). Each filter pipeline is limited to 5 seconds.</p>
47
  <p><b>One addition.</b> The system prompt is Ai2's, plus one line giving today's date (the model does not know it and otherwise searches for old news). Add <code>?date=0</code> to the address to remove it.</p>
48
  <p><b>Honest limits.</b> A 4-bit-class model on a free search stack is not the paper's setup: results can be thinner than the reported benchmarks, the free services rate-limit (about 20 page reads a minute, so steps can pause), and answers can be wrong. Check the sources it cites.
 
42
  <p><b>Agent.</b> The prompt, the two tools (<code>search</code>, <code>webterminal</code>), the 150-call budget and the final-answer warning are from
43
  <a href="https://github.com/allenai/dr-tulu2" target="_blank" rel="noopener">allenai/dr-tulu2</a>. Its live mode uses paid Serper and Jina keys; here there is no server at all:
44
  <code>search</code> reads DuckDuckGo results (Brave as backup) and <code>browse</code> reads pages through <a href="https://jina.ai/reader" target="_blank" rel="noopener">Jina Reader</a>'s anonymous, browser-friendly endpoint
45
+ (it renders JavaScript and gets past most bot walls); if a site refuses it, the page is retried through defuddle.md, a direct read and the newest Wayback Machine copy. Hard paywalls and bot-protected news sites (Reuters, NYT…) stay unreadable, and the model is told so. Search falls back to Wikipedia and OpenAlex.
46
  The terminal is a small safe runner written in JavaScript for <code>head tail rg grep sed wc sort uniq cut tr nl</code>; there is no shell. Private and local addresses are refused, as are huggingface.co pages (as upstream blocks them). Each filter pipeline is limited to 5 seconds.</p>
47
  <p><b>One addition.</b> The system prompt is Ai2's, plus one line giving today's date (the model does not know it and otherwise searches for old news). Add <code>?date=0</code> to the address to remove it.</p>
48
  <p><b>Honest limits.</b> A 4-bit-class model on a free search stack is not the paper's setup: results can be thinner than the reported benchmarks, the free services rate-limit (about 20 page reads a minute, so steps can pause), and answers can be wrong. Check the sources it cites.
tools.js CHANGED
@@ -48,28 +48,30 @@ export function checkUrl(u) {
48
  return p.href;
49
  }
50
  const pageCache = new Map();
 
 
51
  export async function fetchPage(url) {
52
  url = checkUrl(url);
53
  if (pageCache.has(url)) return pageCache.get(url);
54
  const tried = []; let text = '';
55
- try { // tier 1: Jina Reader (renders JS, strips boilerplate)
56
- const r = await getText(url, { reader: true, timeout: 45000 });
57
- tried.push(`reader HTTP ${r.status}`);
58
- if (r.status < 400 && r.text.trim()) text = r.text;
59
- if (BLOCK.test(text.slice(0, 1500)) && text.length < 3000) text = '';
60
- } catch (e) { tried.push(`reader ${e.name}`); }
61
- if (!text.trim()) try { // tier 2: the page itself, when its server allows cross-origin reads
62
- const r = await getText(url, { timeout: 15000 });
63
- tried.push(`direct HTTP ${r.status}`);
64
- if (r.status < 400) text = htmlToText(r.text);
65
- } catch (e) { tried.push(`direct ${e.name === 'AbortError' ? 'timeout' : 'blocked by CORS'}`); }
66
- if (!text.trim()) try { // tier 3: Wayback Machine snapshot
67
  const av = await getText(`https://archive.org/wayback/available?url=${encodeURIComponent(url)}`, { timeout: 15000 });
68
- const snap = JSON.parse(av.text)?.archived_snapshots?.closest?.url;
69
- if (snap) { const r = await getText(snap.replace(/^http:/, 'https:'), { reader: true, timeout: 45000 }); tried.push(`archive HTTP ${r.status}`); if (r.status < 400) text = r.text; }
70
- else tried.push('archive none');
71
- } catch (e) { tried.push(`archive ${e.name}`); }
72
- text = text.trim() || `BLOCKED/EMPTY: could not get readable content from ${url} (${tried.join(', ')}). Try another source.`;
 
 
 
73
  pageCache.set(url, text); if (pageCache.size > 100) pageCache.delete(pageCache.keys().next().value);
74
  return text;
75
  }
 
48
  return p.href;
49
  }
50
  const pageCache = new Map();
51
+ // Jina answers 200 even for blocked pages, with a header and a "Warning: Target URL returned error 401": that is a failure, not content.
52
+ const usable = (t) => { const body = (t.split('Markdown Content:')[1] ?? t).trim(); return body.length >= 400 && !(BLOCK.test(body.slice(0, 1500)) && body.length < 3000); };
53
  export async function fetchPage(url) {
54
  url = checkUrl(url);
55
  if (pageCache.has(url)) return pageCache.get(url);
56
  const tried = []; let text = '';
57
+ const attempt = async (name, fn) => { // run one tier; keep its text only if it is real content
58
+ if (text) return;
59
+ try { const r = await fn(); tried.push(`${name} HTTP ${r.status}`); if (r.status < 400 && usable(r.text)) text = r.text; }
60
+ catch (e) { if (e?.name === 'AbortError' && outer?.aborted) throw e; tried.push(`${name} ${e?.name === 'AbortError' ? 'timeout' : 'unreachable'}`); }
61
+ };
62
+ await attempt('reader', () => getText(url, { reader: true, timeout: 45000 })); // 1. Jina Reader: renders JavaScript, strips boilerplate
63
+ await attempt('defuddle', () => getText('https://defuddle.md/' + url, { timeout: 30000 })); // 2. defuddle.md: another free, CORS-enabled reader; fetches from other IPs than Jina (reads some sites Jina refuses)
64
+ await attempt('direct', async () => { const r = await getText(url, { timeout: 15000 }); return { ...r, text: r.status < 400 ? htmlToText(r.text) : '' }; }); // 3. the page itself, when its server allows cross-origin reads
65
+ if (!text) try { // 4. newest Wayback Machine snapshot (CORS-enabled API), raw copy first, then through defuddle
 
 
 
66
  const av = await getText(`https://archive.org/wayback/available?url=${encodeURIComponent(url)}`, { timeout: 15000 });
67
+ const snap = JSON.parse(av.text)?.archived_snapshots?.closest?.url?.replace(/^http:/, 'https:');
68
+ if (snap) {
69
+ const raw = snap.replace(/\/web\/(\d+)\//, '/web/$1id_/');
70
+ await attempt('archive', async () => { const r = await getText(raw, { timeout: 30000 }); return { ...r, text: r.status < 400 ? htmlToText(r.text) : '' }; });
71
+ await attempt('archive via defuddle', () => getText('https://defuddle.md/' + raw, { timeout: 45000 }));
72
+ } else tried.push('archive none');
73
+ } catch (e) { if (e?.name === 'AbortError' && outer?.aborted) throw e; tried.push('archive unreachable'); }
74
+ text = text.trim() || `BLOCKED/EMPTY: could not get readable content from ${url} (${tried.join(', ')}). The site blocks automated readers or needs a login; use a different source.`;
75
  pageCache.set(url, text); if (pageCache.size > 100) pageCache.delete(pageCache.keys().next().value);
76
  return text;
77
  }