feat: web_search新增fetch_top参数,搜索后自动抓取前N条完整内容,无需AI手动调web_fetch
This commit is contained in:
+1381
-1365
File diff suppressed because it is too large
Load Diff
@@ -218,20 +218,20 @@ function formatToolResultForModel(toolName: string, result: ToolResult): string
|
|||||||
case 'web_search': {
|
case 'web_search': {
|
||||||
const raw = result.results as Array<{ title: string; url: string; snippet: string }> | undefined;
|
const raw = result.results as Array<{ title: string; url: string; snippet: string }> | undefined;
|
||||||
if (!raw?.length) return JSON.stringify({ success: true, message: '未找到结果' });
|
if (!raw?.length) return JSON.stringify({ success: true, message: '未找到结果' });
|
||||||
// 只保留 top 10,让模型有更多 URL 可选
|
|
||||||
const visible = Math.min(10, raw.length);
|
const visible = Math.min(10, raw.length);
|
||||||
const top = raw.slice(0, visible).map((r, i) =>
|
const top = raw.slice(0, visible).map((r, i) =>
|
||||||
`[${i + 1}] ${r.title}\n URL: ${r.url}\n ${r.snippet}`
|
`[${i + 1}] ${r.title}\n URL: ${r.url}\n ${r.snippet}`
|
||||||
).join('\n\n');
|
).join('\n\n');
|
||||||
return `⚠️ 以下仅搜索摘要,缺乏完整内容无法准确回答。你必须从以上结果中选取最相关的 3-5 个 URL,逐一调用 web_fetch 获取完整详情后再综合回答。不要跳过抓取步骤!
|
const fetched = (result as any)._fetched as Array<{ url: string; title: string; content: string }> | undefined;
|
||||||
|
const body = JSON.stringify({
|
||||||
` + JSON.stringify({
|
success: true, query: result.query, total: result.total, shown: visible, results: top,
|
||||||
success: true,
|
|
||||||
query: result.query,
|
|
||||||
total: result.total,
|
|
||||||
shown: visible,
|
|
||||||
results: top,
|
|
||||||
});
|
});
|
||||||
|
if (fetched && fetched.length > 0) {
|
||||||
|
return body + '\n\n' + fetched.map((f, i) =>
|
||||||
|
`\n=== 📄 已抓取 ${i + 1}/${fetched.length}: ${f.title} ===\n${f.content}\n`
|
||||||
|
).join('\n---\n');
|
||||||
|
}
|
||||||
|
return body;
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'web_fetch': {
|
case 'web_fetch': {
|
||||||
|
|||||||
@@ -336,7 +336,7 @@ export const TOOL_DEFINITIONS: ToolDefinition[] = [
|
|||||||
type: 'function',
|
type: 'function',
|
||||||
function: {
|
function: {
|
||||||
name: 'web_search',
|
name: 'web_search',
|
||||||
description: 'Search the web. Returns title, URL, and short snippet for each result. Snippets are summaries only — to get full page content, you MUST call web_fetch on the relevant result URLs. After searching, pick 3-5 most relevant results and call web_fetch on each before composing your answer. Supports time range filtering (day/week/month/year).',
|
description: 'Search the web and optionally auto-fetch full page content. Set fetch_top to automatically fetch complete content from the top N results — this saves you the extra step of calling web_fetch yourself. Use fetch_top=3 for most cases. Returns snippets + full fetched content. Supports time range filtering.',
|
||||||
parameters: {
|
parameters: {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
required: ['query'],
|
required: ['query'],
|
||||||
@@ -344,7 +344,8 @@ export const TOOL_DEFINITIONS: ToolDefinition[] = [
|
|||||||
query: { type: 'string', description: 'The search query. Be specific and concise for best results.' },
|
query: { type: 'string', description: 'The search query. Be specific and concise for best results.' },
|
||||||
max_results: { type: 'integer', description: 'Maximum number of results to return. Default: 30, max: 30.' },
|
max_results: { type: 'integer', description: 'Maximum number of results to return. Default: 30, max: 30.' },
|
||||||
time_range: { type: 'string', enum: ['day', 'week', 'month', 'year'], description: 'Filter results by time. Supported by Bing and Google. Use for recent news, latest version, etc.' },
|
time_range: { type: 'string', enum: ['day', 'week', 'month', 'year'], description: 'Filter results by time. Supported by Bing and Google. Use for recent news, latest version, etc.' },
|
||||||
enhance_snippets: { type: 'boolean', description: 'Auto-fetch detailed snippet for results with too-short descriptions. Default: true.' }
|
enhance_snippets: { type: 'boolean', description: 'Auto-fetch detailed snippet for results with too-short descriptions. Default: true.' },
|
||||||
|
fetch_top: { type: 'integer', description: 'Automatically fetch full content from the top N search results. Default: 0 (no auto-fetch). Recommended: 3-5.' }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user