|
| 1 | +import type { DuckDuckGoSearchParams, DuckDuckGoSearchResponse } from '@/tools/duckduckgo/types' |
| 2 | +import type { ToolConfig } from '@/tools/types' |
| 3 | + |
| 4 | +export const searchTool: ToolConfig<DuckDuckGoSearchParams, DuckDuckGoSearchResponse> = { |
| 5 | + id: 'duckduckgo_search', |
| 6 | + name: 'DuckDuckGo Search', |
| 7 | + description: |
| 8 | + 'Search the web using DuckDuckGo Instant Answers API. Returns instant answers, abstracts, and related topics for your query. Free to use without an API key.', |
| 9 | + version: '1.0.0', |
| 10 | + |
| 11 | + params: { |
| 12 | + query: { |
| 13 | + type: 'string', |
| 14 | + required: true, |
| 15 | + visibility: 'user-or-llm', |
| 16 | + description: 'The search query to execute', |
| 17 | + }, |
| 18 | + noHtml: { |
| 19 | + type: 'boolean', |
| 20 | + required: false, |
| 21 | + visibility: 'user-only', |
| 22 | + description: 'Remove HTML from text in results (default: true)', |
| 23 | + }, |
| 24 | + skipDisambig: { |
| 25 | + type: 'boolean', |
| 26 | + required: false, |
| 27 | + visibility: 'user-only', |
| 28 | + description: 'Skip disambiguation results (default: false)', |
| 29 | + }, |
| 30 | + }, |
| 31 | + |
| 32 | + request: { |
| 33 | + url: (params) => { |
| 34 | + const baseUrl = 'https://api.duckduckgo.com/' |
| 35 | + const searchParams = new URLSearchParams({ |
| 36 | + q: params.query, |
| 37 | + format: 'json', |
| 38 | + no_html: params.noHtml !== false ? '1' : '0', |
| 39 | + skip_disambig: params.skipDisambig ? '1' : '0', |
| 40 | + }) |
| 41 | + return `${baseUrl}?${searchParams.toString()}` |
| 42 | + }, |
| 43 | + method: 'GET', |
| 44 | + headers: () => ({ |
| 45 | + Accept: 'application/json', |
| 46 | + }), |
| 47 | + }, |
| 48 | + |
| 49 | + transformResponse: async (response: Response) => { |
| 50 | + const data = await response.json() |
| 51 | + |
| 52 | + // Map related topics |
| 53 | + const relatedTopics = (data.RelatedTopics || []).map((topic: any) => ({ |
| 54 | + FirstURL: topic.FirstURL, |
| 55 | + Text: topic.Text, |
| 56 | + Result: topic.Result, |
| 57 | + Icon: topic.Icon |
| 58 | + ? { |
| 59 | + URL: topic.Icon.URL, |
| 60 | + Height: topic.Icon.Height, |
| 61 | + Width: topic.Icon.Width, |
| 62 | + } |
| 63 | + : undefined, |
| 64 | + })) |
| 65 | + |
| 66 | + // Map results (external links) |
| 67 | + const results = (data.Results || []).map((result: any) => ({ |
| 68 | + FirstURL: result.FirstURL, |
| 69 | + Text: result.Text, |
| 70 | + Result: result.Result, |
| 71 | + Icon: result.Icon |
| 72 | + ? { |
| 73 | + URL: result.Icon.URL, |
| 74 | + Height: result.Icon.Height, |
| 75 | + Width: result.Icon.Width, |
| 76 | + } |
| 77 | + : undefined, |
| 78 | + })) |
| 79 | + |
| 80 | + return { |
| 81 | + success: true, |
| 82 | + output: { |
| 83 | + heading: data.Heading || '', |
| 84 | + abstract: data.Abstract || '', |
| 85 | + abstractText: data.AbstractText || '', |
| 86 | + abstractSource: data.AbstractSource || '', |
| 87 | + abstractURL: data.AbstractURL || '', |
| 88 | + image: data.Image || '', |
| 89 | + answer: data.Answer || '', |
| 90 | + answerType: data.AnswerType || '', |
| 91 | + type: data.Type || '', |
| 92 | + relatedTopics, |
| 93 | + results, |
| 94 | + }, |
| 95 | + } |
| 96 | + }, |
| 97 | + |
| 98 | + outputs: { |
| 99 | + heading: { |
| 100 | + type: 'string', |
| 101 | + description: 'The heading/title of the instant answer', |
| 102 | + }, |
| 103 | + abstract: { |
| 104 | + type: 'string', |
| 105 | + description: 'A short abstract summary of the topic', |
| 106 | + }, |
| 107 | + abstractText: { |
| 108 | + type: 'string', |
| 109 | + description: 'Plain text version of the abstract', |
| 110 | + }, |
| 111 | + abstractSource: { |
| 112 | + type: 'string', |
| 113 | + description: 'The source of the abstract (e.g., Wikipedia)', |
| 114 | + }, |
| 115 | + abstractURL: { |
| 116 | + type: 'string', |
| 117 | + description: 'URL to the source of the abstract', |
| 118 | + }, |
| 119 | + image: { |
| 120 | + type: 'string', |
| 121 | + description: 'URL to an image related to the topic', |
| 122 | + }, |
| 123 | + answer: { |
| 124 | + type: 'string', |
| 125 | + description: 'Direct answer if available (e.g., for calculations)', |
| 126 | + }, |
| 127 | + answerType: { |
| 128 | + type: 'string', |
| 129 | + description: 'Type of the answer (e.g., calc, ip, etc.)', |
| 130 | + }, |
| 131 | + type: { |
| 132 | + type: 'string', |
| 133 | + description: |
| 134 | + 'Response type: A (article), D (disambiguation), C (category), N (name), E (exclusive)', |
| 135 | + }, |
| 136 | + relatedTopics: { |
| 137 | + type: 'array', |
| 138 | + description: 'Array of related topics with URLs and descriptions', |
| 139 | + items: { |
| 140 | + type: 'object', |
| 141 | + properties: { |
| 142 | + FirstURL: { type: 'string', description: 'URL to the related topic' }, |
| 143 | + Text: { type: 'string', description: 'Description of the related topic' }, |
| 144 | + Result: { type: 'string', description: 'HTML result snippet' }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, |
| 148 | + results: { |
| 149 | + type: 'array', |
| 150 | + description: 'Array of external link results', |
| 151 | + items: { |
| 152 | + type: 'object', |
| 153 | + properties: { |
| 154 | + FirstURL: { type: 'string', description: 'URL of the result' }, |
| 155 | + Text: { type: 'string', description: 'Description of the result' }, |
| 156 | + Result: { type: 'string', description: 'HTML result snippet' }, |
| 157 | + }, |
| 158 | + }, |
| 159 | + }, |
| 160 | + }, |
| 161 | +} |
0 commit comments