import { Firecrawl } from 'firecrawl';
import OpenAI from 'openai';
import { z } from 'zod';
const firecrawl = new Firecrawl({ apiKey: process.env.FIRECRAWL_API_KEY });
// Nous Portal 兼容 OpenAI 接口
const nous = new OpenAI({
apiKey: process.env.NOUS_API_KEY,
baseURL: 'https://inference-api.nousresearch.com/v1',
});
const SearchArgs = z.object({
query: z.string().describe('The web search query'),
limit: z.number().int().min(1).max(10).default(5),
});
const ScrapeArgs = z.object({
url: z.string().describe('The URL to scrape'),
});
const tools = [
{
type: 'function' as const,
function: {
name: 'web_search',
description: 'Search the web with Firecrawl and return top results.',
parameters: z.toJSONSchema(SearchArgs),
},
},
{
type: 'function' as const,
function: {
name: 'scrape_website',
description: 'Scrape the markdown content of a URL.',
parameters: z.toJSONSchema(ScrapeArgs),
},
},
];
const response = await nous.chat.completions.create({
model: 'Hermes-4-405B',
tools,
messages: [
{
role: 'user',
content: 'Research Firecrawl\'s /agent endpoint and cite the docs.',
},
],
});
for (const call of response.choices[0]?.message.tool_calls ?? []) {
if (call.function.name === 'web_search') {
const { query, limit } = SearchArgs.parse(JSON.parse(call.function.arguments));
const results = await firecrawl.search(query, { limit });
console.log(results.web);
}
if (call.function.name === 'scrape_website') {
const { url } = ScrapeArgs.parse(JSON.parse(call.function.arguments));
const page = await firecrawl.scrape(url, { formats: ['markdown'] });
console.log(page.markdown);
}
}