| 206 | 206 | } |
| 207 | 207 | }); |
| 208 | 208 | |
| 209 | +router.post('/tavily', jsonParser, async (request, response) => { |
| 210 | + try { |
| 211 | + const apiKey = readSecret(request.user.directories, SECRET_KEYS.TAVILY); |
| 212 | + |
| 213 | + if (!apiKey) { |
| 214 | + console.log('No Tavily key found'); |
| 215 | + return response.sendStatus(400); |
| 216 | + } |
| 217 | + |
| 218 | + const { query } = request.body; |
| 219 | + |
| 220 | + const body = { |
| 221 | + query: query, |
| 222 | + api_key: apiKey, |
| 223 | + search_depth: 'basic', |
| 224 | + topic: 'general', |
| 225 | + include_answer: true, |
| 226 | + include_raw_content: false, |
| 227 | + include_images: false, |
| 228 | + include_image_descriptions: false, |
| 229 | + include_domains: [], |
| 230 | + max_results: 10, |
| 231 | + }; |
| 232 | + |
| 233 | + const result = await fetch('https://api.tavily.com/search', { |
| 234 | + method: 'POST', |
| 235 | + headers: { |
| 236 | + 'Content-Type': 'application/json', |
| 237 | + }, |
| 238 | + body: JSON.stringify(body), |
| 239 | + }); |
| 240 | + |
| 241 | + console.log('Tavily query', query); |
| 242 | + |
| 243 | + if (!result.ok) { |
| 244 | + const text = await result.text(); |
| 245 | + console.log('Tavily request failed', result.statusText, text); |
| 246 | + return response.status(500).send(text); |
| 247 | + } |
| 248 | + |
| 249 | + const data = await result.json(); |
| 250 | + return response.json(data); |
| 251 | + } catch (error) { |
| 252 | + console.log(error); |
| 253 | + return response.sendStatus(500); |
| 254 | + } |
| 255 | +}); |
| 256 | + |
| 209 | 257 | router.post('/visit', jsonParser, async (request, response) => { |
| 210 | 258 | try { |
| 211 | 259 | const url = request.body.url; |