-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: remove console logs from duckduckgo search tool (#92)
* chore: remove console logs from duckduckgo search tool Co-Authored-By: Nicolas Arqueros <[email protected]> * fix: remove unused urlString variable Co-Authored-By: Nicolas Arqueros <[email protected]> * fix: update imports and test assertions for duckduckgo search tool Co-Authored-By: Nicolas Arqueros <[email protected]> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Nicolas Arqueros <[email protected]>
- Loading branch information
1 parent
fca8185
commit decdf3a
Showing
2 changed files
with
23 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
import { expect } from 'jsr:@std/expect/expect'; | ||
import { run } from './index.ts'; | ||
import { assertEquals, assertExists } from "https://deno.land/[email protected]/assert/mod.ts"; | ||
|
||
Deno.test('searches DuckDuckGo and gets a response', async () => { | ||
const result = await run({}, { message: 'best movie of all time' }); | ||
const message = result.message; | ||
const searchResults = JSON.parse(message.replace(/^searching: /, '')); | ||
|
||
expect(Array.isArray(searchResults)).toBe(true); | ||
expect(searchResults.length).toBeGreaterThan(0); | ||
expect(searchResults[0]).toHaveProperty('title'); | ||
expect(searchResults[0]).toHaveProperty('url'); // Updated from 'href' to 'url' | ||
expect(searchResults[0]).toHaveProperty('description'); // Updated from 'body' to 'description' | ||
assertExists(searchResults, 'Search results should exist'); | ||
assertEquals(Array.isArray(searchResults), true, 'Results should be an array'); | ||
assertEquals(searchResults.length > 0, true, 'Results should not be empty'); | ||
assertExists(searchResults[0].title, 'Result should have title'); | ||
assertExists(searchResults[0].url, 'Result should have url'); | ||
assertExists(searchResults[0].description, 'Result should have description'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import { URL } from 'npm:[email protected]'; | ||
import axios from 'npm:[email protected]'; | ||
import process from 'node:process'; | ||
|
||
type Configurations = {}; | ||
|
@@ -26,22 +24,26 @@ function buildQueryString(params: Record<string, string>): string { | |
const getVQD = async (keywords: string): Promise<string> => { | ||
const body = buildQueryString({ q: keywords }); | ||
await process.nextTick(() => {}); | ||
const response = await axios.post('https://duckduckgo.com', body, { | ||
const response = await fetch('https://duckduckgo.com', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
body, | ||
}); | ||
const text = response.data; | ||
// console.log('DuckDuckGo response HTML:', text); | ||
|
||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
const text = await response.text(); | ||
|
||
// Extract vqd token using a regular expression | ||
const vqdMatch = text.match(/vqd=\\?"([^\\"]+)\\?"/); | ||
// console.log('vqdMatch: ', vqdMatch); | ||
if (!vqdMatch || vqdMatch.length < 2) { | ||
throw new Error('Failed to retrieve vqd token'); | ||
} | ||
const vqd = vqdMatch[1]; | ||
// console.log('vqd: ', vqd); | ||
return vqd; | ||
}; | ||
|
||
|
@@ -72,17 +74,12 @@ const parseDuckDuckGoResponse = (response: string): SearchResult[] => { | |
result.title && result.description && result.url, | ||
); | ||
|
||
// console.log('results: ', results); | ||
// Convert to JSON string | ||
return results; | ||
}; | ||
|
||
const textSearch = async (keywords: string): Promise<any[]> => { | ||
console.log('textSearch: ', keywords); | ||
const vqd = await getVQD(keywords); | ||
console.log('vqd: ', vqd); | ||
const url = new URL('https://links.duckduckgo.com/d.js'); | ||
console.log('before url.searchParams.append'); | ||
url.searchParams.append('q', keywords); | ||
url.searchParams.append('vqd', vqd); | ||
url.searchParams.append('kl', 'wt-wt'); | ||
|
@@ -92,21 +89,19 @@ const textSearch = async (keywords: string): Promise<any[]> => { | |
url.searchParams.append('df', ''); | ||
url.searchParams.append('ex', '-1'); | ||
|
||
console.log('before urlString'); | ||
const urlString = url.toString(); | ||
console.log('urlString: ', urlString); | ||
|
||
await process.nextTick(() => {}); | ||
const response = await axios.get(url.toString(), { | ||
const response = await fetch(url.toString(), { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', | ||
}, | ||
}); | ||
console.log('response: ', response); | ||
const text = response.data; | ||
console.log('DuckDuckGo search response:', text); | ||
|
||
// Parse the response using the custom parser | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
const text = await response.text(); | ||
const results = parseDuckDuckGoResponse(text); | ||
if (results.length === 0) { | ||
throw new Error('Failed to extract search results'); | ||
|
@@ -119,12 +114,8 @@ export const run: Run<Configurations, Parameters, Result> = async ( | |
_configurations: Configurations, | ||
params: Parameters, | ||
): Promise<Result> => { | ||
console.log('run duckduckgo search from js', 4); | ||
console.log('second message', 4); | ||
console.log('params: ', params); | ||
try { | ||
const results = await textSearch(params.message); | ||
console.log('results: ', results); | ||
return { message: JSON.stringify(results) }; | ||
} catch (error) { | ||
let errorMessage = 'An unknown error occurred'; | ||
|