import FirecrawlApp from '@mendable/firecrawl-js';
import { openai } from '@ai-sdk/openai';
import { generateText, tool } from 'ai';
import { z } from 'zod';
console.log('Initializing Firecrawl...');
const firecrawl = new FirecrawlApp({ apiKey: process.env.FIRECRAWL_API_KEY });
const scrapeWebsiteTool = tool({
description: 'Scrape content from any website URL',
inputSchema: z.object({
url: z.string().url().describe('The URL to scrape')
}),
execute: async ({ url }) => {
console.log('Scraping:', url);
const result = await firecrawl.scrape(url, {
formats: ['markdown']
});
console.log('Scraped content preview:', result.markdown?.slice(0, 200) + '...');
return { content: result.markdown };
}
});
console.log('Generating text with AI...');
const { text, toolCalls } = await generateText({
model: openai('gpt-5-nano'),
prompt: 'What is Firecrawl? Visit firecrawl.dev and tell me about it.',
tools: {
scrapeWebsite: scrapeWebsiteTool
},
});
console.log('Tool calls:', toolCalls);
// Continue the conversation OR use the text for something else