Template
53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import { tool } from 'ai';
|
|
import { z } from 'zod';
|
|
|
|
export const firecrawlWebSearch = tool({
|
|
description:
|
|
'Search the web and return results as markdown. Use this tool when the user asks to search for information on the web.',
|
|
inputSchema: z.object({
|
|
query: z.string().describe('The search query'),
|
|
limit: z
|
|
.number()
|
|
.min(1)
|
|
.max(10)
|
|
.default(3)
|
|
.describe('Number of results to return (max 10)'),
|
|
}),
|
|
execute: async ({ query, limit }) => {
|
|
try {
|
|
const response = await fetch('https://api.firecrawl.dev/v1/search', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Authorization: `Bearer ${process.env.FIRECRAWL_API_KEY!}`,
|
|
},
|
|
body: JSON.stringify({
|
|
query,
|
|
limit,
|
|
scrapeOptions: { formats: ['markdown'] },
|
|
}),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorText = await response.text();
|
|
return { success: false, error: `Firecrawl API error: ${errorText}` };
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
if (!data?.success || !data?.data) {
|
|
return { success: false, error: 'No results found for this query' };
|
|
}
|
|
|
|
return { success: true, results: data.data };
|
|
} catch (error) {
|
|
console.error('Error searching web:', error);
|
|
return {
|
|
success: false,
|
|
error:
|
|
error instanceof Error ? error.message : 'Failed to search the web',
|
|
};
|
|
}
|
|
},
|
|
});
|