UNPKG

@xynehq/jaf

Version:

Juspay Agent Framework - A purely functional agent framework with immutable state and composable tools

139 lines 5.07 kB
/** * JAF ADK - Production Weather Tool * * Real weather data fetching using OpenWeatherMap API */ import { createFunctionTool } from '../tools'; import { ToolParameterType } from '../types'; /** * Create a production weather tool using OpenWeatherMap API * Requires OPENWEATHER_API_KEY environment variable */ export const createWeatherTool = (apiKey) => { const key = apiKey || process.env.OPENWEATHER_API_KEY; return createFunctionTool({ name: 'get_weather', description: 'Get current weather information for a location', execute: async (params) => { const { location, units = 'metric' } = params; if (!key) { // Fallback to mock data if no API key return getMockWeatherData(location, units); } try { // Try to fetch from OpenWeatherMap API const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${encodeURIComponent(location)}&units=${units}&appid=${key}`); if (!response.ok) { if (response.status === 404) { throw new Error(`Location "${location}" not found`); } throw new Error(`Weather API error: ${response.status}`); } const data = await response.json(); return { location: data.name, country: data.sys.country, temperature: Math.round(data.main.temp), feels_like: Math.round(data.main.feels_like), condition: data.weather[0].main, description: data.weather[0].description, humidity: data.main.humidity, pressure: data.main.pressure, wind_speed: data.wind.speed, wind_direction: data.wind.deg, clouds: data.clouds.all, visibility: data.visibility, timezone: data.timezone, sunrise: new Date(data.sys.sunrise * 1000).toISOString(), sunset: new Date(data.sys.sunset * 1000).toISOString(), units: units === 'metric' ? 'celsius' : 'fahrenheit' }; } catch (error) { // Fallback to mock data on error console.warn('Weather API error, using mock data:', error); return getMockWeatherData(location, units); } }, parameters: [ { name: 'location', type: ToolParameterType.STRING, description: 'City name, or city and country code (e.g., "London,UK")', required: true }, { name: 'units', type: ToolParameterType.STRING, description: 'Temperature units', required: false, enum: ['metric', 'imperial'], default: 'metric' } ] }); }; /** * Fallback mock weather data for development/testing */ const getMockWeatherData = (location, units = 'metric') => { const mockData = { 'new york': { temp: units === 'metric' ? 15 : 59, condition: 'Clouds', description: 'overcast clouds', humidity: 78, wind_speed: 5.2 }, 'london': { temp: units === 'metric' ? 8 : 46, condition: 'Rain', description: 'light rain', humidity: 85, wind_speed: 3.8 }, 'tokyo': { temp: units === 'metric' ? 22 : 72, condition: 'Clear', description: 'clear sky', humidity: 60, wind_speed: 2.1 }, 'sydney': { temp: units === 'metric' ? 25 : 77, condition: 'Clouds', description: 'partly cloudy', humidity: 65, wind_speed: 4.5 } }; const location_key = location.toLowerCase(); const weather = mockData[location_key] || { temp: units === 'metric' ? 20 : 68, condition: 'Clear', description: 'clear sky', humidity: 50, wind_speed: 3.0 }; return { location: location.charAt(0).toUpperCase() + location.slice(1), country: 'XX', temperature: weather.temp, feels_like: weather.temp, condition: weather.condition, description: weather.description, humidity: weather.humidity, pressure: 1013, wind_speed: weather.wind_speed, wind_direction: 180, clouds: 25, visibility: 10000, timezone: 0, sunrise: new Date().toISOString(), sunset: new Date(Date.now() + 12 * 3600 * 1000).toISOString(), units: units === 'metric' ? 'celsius' : 'fahrenheit', mock: true }; }; export default createWeatherTool; //# sourceMappingURL=weather.js.map