chenbhao commited on
Commit
64c10f6
·
1 Parent(s): fdc8b59

feat: WebSearch markdown add image link can show image

Browse files
src/tools/WebSearchTool/WebSearchTool.ts CHANGED
@@ -25,6 +25,7 @@ const searchResultSchema = lazySchema(() => {
25
  title: z.string(),
26
  url: z.string(),
27
  snippet: z.string().optional(),
 
28
  })
29
 
30
  return z.object({
@@ -53,7 +54,7 @@ import type { WebSearchProgress } from '../../types/tools.js'
53
  */
54
  async function searchSearXNG(
55
  query: string
56
- ): Promise<Array<{ title: string; url: string; snippet?: string }>> {
57
  try {
58
  const url = new URL('http://localhost:8080/search')
59
  url.searchParams.set('q', query)
@@ -85,6 +86,7 @@ async function searchSearXNG(
85
  title: r.title,
86
  url: r.url,
87
  snippet: r.content,
 
88
  }))
89
  } catch (error) {
90
  logError('SearXNG search failed', error)
@@ -99,7 +101,7 @@ async function searchSearXNG(
99
  */
100
  async function searchTavily(
101
  query: string
102
- ): Promise<Array<{ title: string; url: string; snippet?: string }>> {
103
  try {
104
  const apiKey = process.env.TAVILY_API_KEY
105
  if (!apiKey) {
@@ -117,6 +119,7 @@ async function searchTavily(
117
  title: r.title,
118
  url: r.url,
119
  snippet: r.content,
 
120
  }))
121
  } catch (error) {
122
  logError('Tavily search failed', error)
@@ -295,7 +298,8 @@ export const WebSearchTool = buildTool({
295
  text += r + '\n\n'
296
  } else {
297
  r.content.forEach((item: any, i: number) => {
298
- text += `${i + 1}. ${item.title}\n${item.url}\n${item.snippet || ''}\n\n`
 
299
  })
300
  }
301
  }
 
25
  title: z.string(),
26
  url: z.string(),
27
  snippet: z.string().optional(),
28
+ image: z.string().optional(),
29
  })
30
 
31
  return z.object({
 
54
  */
55
  async function searchSearXNG(
56
  query: string
57
+ ): Promise<Array<{ title: string; url: string; snippet?: string; image?: string }>> {
58
  try {
59
  const url = new URL('http://localhost:8080/search')
60
  url.searchParams.set('q', query)
 
86
  title: r.title,
87
  url: r.url,
88
  snippet: r.content,
89
+ image: r.thumbnail || undefined,
90
  }))
91
  } catch (error) {
92
  logError('SearXNG search failed', error)
 
101
  */
102
  async function searchTavily(
103
  query: string
104
+ ): Promise<Array<{ title: string; url: string; snippet?: string; image?: string }>> {
105
  try {
106
  const apiKey = process.env.TAVILY_API_KEY
107
  if (!apiKey) {
 
119
  title: r.title,
120
  url: r.url,
121
  snippet: r.content,
122
+ image: r.img || r.image || undefined,
123
  }))
124
  } catch (error) {
125
  logError('Tavily search failed', error)
 
298
  text += r + '\n\n'
299
  } else {
300
  r.content.forEach((item: any, i: number) => {
301
+ const imgLine = item.image ? `\n![${item.title}](${item.image})\n` : ''
302
+ text += `${i + 1}. ${item.title}\n${item.url}\n${item.snippet || ''}${imgLine}\n`
303
  })
304
  }
305
  }
src/tools/WebSearchTool/prompt.ts CHANGED
@@ -19,6 +19,12 @@ Search Strategy:
19
  - If results are weak or empty, try rephrasing the query
20
  - Prefer more specific queries when possible (add keywords, version numbers, or context)
21
 
 
 
 
 
 
 
22
  CRITICAL REQUIREMENT - You MUST follow this:
23
  - After answering the user's question, you MUST include a "Sources:" section at the end of your response
24
  - In the Sources section, list relevant URLs from the search results as markdown hyperlinks: [Title](URL)
 
19
  - If results are weak or empty, try rephrasing the query
20
  - Prefer more specific queries when possible (add keywords, version numbers, or context)
21
 
22
+ Search Result Format:
23
+ - Each search result includes: title, URL, snippet, and optionally an image URL (thumbnail)
24
+ - You can use images in your response via markdown syntax: ![alt text](image_url)
25
+ - When search results include images, incorporate them into your answer for a richer,图文混排 experience
26
+ - Images make responses much more readable — use them whenever available
27
+
28
  CRITICAL REQUIREMENT - You MUST follow this:
29
  - After answering the user's question, you MUST include a "Sources:" section at the end of your response
30
  - In the Sources section, list relevant URLs from the search results as markdown hyperlinks: [Title](URL)