Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove console logs from duckduckgo search tool #92

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions apps/shinkai-tool-duckduckgo-search/src/index.test.ts
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');
});
41 changes: 16 additions & 25 deletions apps/shinkai-tool-duckduckgo-search/src/index.ts
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 = {};
Expand All @@ -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;
};

Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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';
Expand Down
Loading