import { Agent, openai, createTool } from '@runflow-ai/sdk';
import { httpGet } from '@runflow-ai/sdk/http';
import { z } from 'zod';
const weatherTool = createTool({
id: 'get-weather',
description: 'Get current weather for any city',
inputSchema: z.object({
city: z.string().describe('City name (e.g., "São Paulo", "New York")'),
}),
execute: async ({ context }) => {
try {
const apiKey = process.env.OPENWEATHER_API_KEY;
const data = await httpGet('https://api.openweathermap.org/data/2.5/weather', {
params: {
q: context.city,
appid: apiKey,
units: 'metric',
lang: 'pt_br',
},
timeout: 5000,
});
return {
city: data.name,
temperature: data.main.temp,
feelsLike: data.main.feels_like,
condition: data.weather[0].description,
humidity: data.main.humidity,
windSpeed: data.wind.speed,
};
} catch (error: any) {
if (error.message.includes('404')) {
return { error: `City "${context.city}" not found` };
}
throw new Error(`Weather API error: ${error.message}`);
}
},
});
const agent = new Agent({
name: 'Weather Assistant',
instructions: 'You help users check the weather. Use the weather tool when users ask about weather conditions.',
model: openai('gpt-4o'),
tools: {
weather: weatherTool,
},
});
// Use the agent
const result = await agent.process({
message: 'What is the weather like in São Paulo?',
});