From ad589844be02cd8320e743aa1865219934b7ebad Mon Sep 17 00:00:00 2001 From: kim Date: Sat, 17 Oct 2026 11:50:20 +0900 Subject: [PATCH] =?UTF-8?q?web=5Fsearch:=20=EC=9D=BC=EB=B0=98=20=EA=B2=80?= =?UTF-8?q?=EC=83=89=EB=8F=84=20=EC=83=81=EC=9C=84=203=EA=B0=9C=20?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=A7=80=20=EB=B3=B8=EB=AC=B8=EC=9D=84=20?= =?UTF-8?q?=EB=B3=91=EB=A0=AC=EB=A1=9C=20=EC=9D=BD=EC=96=B4=20=EB=B6=99?= =?UTF-8?q?=EC=9D=B4=EA=B3=A0=20[n]=20=EA=B7=BC=EA=B1=B0=20=ED=91=9C?= =?UTF-8?q?=EA=B8=B0=20=EC=95=88=EB=82=B4=20(=EB=89=B4=EC=8A=A4=C2=B7?= =?UTF-8?q?=EB=B9=84=EA=B5=90=20=EC=A0=9C=EC=99=B8,=20SMALLCLAW=5FWEB=5FDE?= =?UTF-8?q?EPREAD=3D0=EC=9C=BC=EB=A1=9C=20=EB=81=94)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 --- src/tools/web.ts | 63 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/src/tools/web.ts b/src/tools/web.ts index c056af6..df4a7e9 100644 --- a/src/tools/web.ts +++ b/src/tools/web.ts @@ -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> { + 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(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 { - 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 {