daggerai
Version:
A simple and powerful Typescript based agent framework to help businesses thrive in the AI Agent revolution.
59 lines • 2.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SerperTool = void 0;
const zod_1 = require("zod");
class SerperTool {
code = 'serper';
name = 'Serper Search';
description = 'Use when you need to look up something on the internet.';
schema = zod_1.z.object({
query: zod_1.z.string(),
});
constructor() {
if (!process.env.SERPER_API_KEY) {
throw new Error('SERPER_API_KEY environment variable is required');
}
}
async execute(input) {
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-KEY': process.env.SERPER_API_KEY,
},
redirect: 'follow',
body: JSON.stringify({
q: `${input.query}`,
gl: 'br',
hl: 'pt-br',
}),
};
const url = `https://google.serper.dev/search`;
const response = await fetch(url, options);
if (response.status !== 200) {
throw new Error(`Error calling Serper API: ${response.statusText}`);
}
const json = (await response.json());
if (json.answerBox?.answer) {
return { text: json.answerBox.answer };
}
if (json.answerBox?.snippet) {
return { text: json.answerBox.snippet };
}
if (json.answerBox?.snippet_highlighted_words) {
return { text: json.answerBox.snippet_highlighted_words[0] };
}
if (json.sportsResults?.game_spotlight) {
return { text: json.sportsResults.game_spotlight };
}
if (json.knowledgeGraph?.description) {
return { text: json.knowledgeGraph.description };
}
if (json.organic?.[0]?.snippet) {
return { text: json.organic[0].snippet };
}
return { text: 'No good search result found' };
}
}
exports.SerperTool = SerperTool;
//# sourceMappingURL=serper.js.map