web_search: 일반 검색도 상위 3개 페이지 본문을 병렬로 읽어 붙이고 [n] 근거 표기 안내 (뉴스·비교 제외, SMALLCLAW_WEB_DEEPREAD=0으로 끔)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
aee2e3825b
commit
ad589844be
1 file changed
+61
-2
+61
-2
@@ -1363,11 +1363,70 @@ async function runComparisonSearch(
|
||||
};
|
||||
}
|
||||
|
||||
/** URLs worth opening: public http(s) pages, not media/video hosts and not LAN/loopback addresses. */
|
||||
function isDeepReadUrl(url: string): boolean {
|
||||
try {
|
||||
const u = new URL(url);
|
||||
if (!/^https?:$/.test(u.protocol)) return false;
|
||||
const h = u.hostname.toLowerCase();
|
||||
if (h === 'localhost' || h.endsWith('.local') || h.endsWith('.internal')) return false;
|
||||
if (/^(10\.|127\.|192\.168\.|169\.254\.|172\.(1[6-9]|2\d|3[01])\.)/.test(h) || h === '::1' || h.startsWith('[')) return false;
|
||||
if (/(^|\.)(youtube\.com|youtu\.be|instagram\.com|facebook\.com|tiktok\.com|x\.com|twitter\.com)$/.test(h)) return false;
|
||||
if (/\.(pdf|zip|jpe?g|png|gif|mp4|mp3)(\?|$)/i.test(u.pathname)) return false;
|
||||
return true;
|
||||
} catch { return false; }
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the top-ranked results in parallel and returns their body text keyed by result number.
|
||||
*
|
||||
* web_search hands the model titles and ~400-char snippets; the facts a real answer needs sit in the
|
||||
* page body, and the model does not call web_fetch on its own ([[feedback_local_model_needs_code_backstop]]).
|
||||
* Reading the pages in code is what lets the answer rest on the source instead of on a snippet.
|
||||
* Every page is best-effort: a walled, slow or JS-only page is dropped silently, never an error.
|
||||
*/
|
||||
async function deepReadResults(results: any[], maxPages = 3, perPageChars = 2200, budgetMs = 7000): Promise<Array<{ n: number; url: string; text: string }>> {
|
||||
const picks = (Array.isArray(results) ? results : [])
|
||||
.map((r, i) => ({ n: i + 1, url: String(r?.url || '').trim() }))
|
||||
.filter(x => isDeepReadUrl(x.url))
|
||||
.slice(0, maxPages);
|
||||
const timeout = new Promise<null>(resolve => setTimeout(() => resolve(null), budgetMs));
|
||||
const jobs = picks.map(async (x) => {
|
||||
try {
|
||||
const r = await Promise.race([executeWebFetch({ url: x.url, max_chars: perPageChars }), timeout]);
|
||||
if (!r || !r.success) return null;
|
||||
const text = String(r.stdout || '').replace(/\n{3,}/g, '\n\n').trim();
|
||||
// Short bodies are cookie walls, "enable JavaScript" stubs or redirects — not evidence.
|
||||
if (text.length < 400 || /enable javascript|자바스크립트를 (활성|사용)|access denied|captcha/i.test(text.slice(0, 600))) return null;
|
||||
return { n: x.n, url: x.url, text };
|
||||
} catch { return null; }
|
||||
});
|
||||
return (await Promise.all(jobs)).filter(Boolean) as Array<{ n: number; url: string; text: string }>;
|
||||
}
|
||||
|
||||
export async function executeWebSearchWithExtraction(args: { query: string; max_results?: number }): Promise<ToolResult> {
|
||||
const res = await executeWebSearch(args);
|
||||
let res = await executeWebSearch(args);
|
||||
if (!res.success || !res.stdout) return res;
|
||||
|
||||
const extracted = await extractAnswerFromResults(args.query, res.stdout);
|
||||
// Generic deep read: snippets alone were what every non-spec answer was built on. News and
|
||||
// comparison searches already have their own pipelines, so leave those alone.
|
||||
if (process.env.SMALLCLAW_WEB_DEEPREAD !== '0'
|
||||
&& !String(res.stdout).includes('[본문')
|
||||
&& !isNewsSeekingQuery(args.query)) {
|
||||
const started = Date.now();
|
||||
const pages = await deepReadResults((res.data as any)?.results || []);
|
||||
if (pages.length) {
|
||||
console.log(`[v2] web_search deep-read: ${pages.length} page(s) in ${Date.now() - started}ms`);
|
||||
const bodies = pages.map(p => `[본문 ${p.n}] ${p.url}\n${p.text}`).join('\n\n');
|
||||
res = {
|
||||
...res,
|
||||
data: { ...(res.data as any || {}), deep_read_pages: pages.length },
|
||||
stdout: `${res.stdout}\n\n--- 상위 결과 본문 (번호는 위 [n]과 같음. 답에 근거 번호를 [n]으로 표기하세요) ---\n${bodies}`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const extracted = await extractAnswerFromResults(args.query, String(res.stdout));
|
||||
if (!extracted) return res;
|
||||
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user