import fetch from 'node-fetch';
import { WEATHER_CODES } from '../types/openmeteo.js';
export async function getCurrentWeather(args: any) {
const { latitude, longitude, units = 'celsius' } = args;
if (typeof latitude !== 'number' || typeof longitude !== 'number') {
throw new Error('Latitude and longitude must be numbers');
}
if (latitude < -90 || latitude > 90) {
throw new Error('Latitude must be between -90 and 90');
}
if (longitude < -180 || longitude > 180) {
throw new Error('Longitude must be between -180 and 180');
}
const temperatureUnit = units === 'fahrenheit' ? 'fahrenheit' : 'celsius';
const currentParams = [
'temperature_2m',
'relative_humidity_2m',
'apparent_temperature',
'is_day',
'precipitation',
'rain',
'showers',
'snowfall',
'weather_code',
'cloud_cover',
'pressure_msl',
'surface_pressure',
'wind_speed_10m',
'wind_direction_10m',
'wind_gusts_10m'
].join(',');
const url = `https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}¤t=${currentParams}&temperature_unit=${temperatureUnit}&wind_speed_unit=kmh&precipitation_unit=mm`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Open Meteo API error: ${response.status} ${response.statusText}`);
}
const data = await response.json() as any;
if (!data.current) {
throw new Error('No current weather data available');
}
const current = data.current;
const weatherDescription = WEATHER_CODES[current.weather_code] || 'Unknown';
const tempUnit = units === 'fahrenheit' ? '°F' : '°C';
const result = {
location: {
latitude: data.latitude,
longitude: data.longitude,
elevation: data.elevation,
timezone: data.timezone,
},
current_weather: {
time: current.time,
temperature: `${current.temperature_2m}${tempUnit}`,
apparent_temperature: `${current.apparent_temperature}${tempUnit}`,
humidity: `${current.relative_humidity_2m}%`,
weather_description: weatherDescription,
weather_code: current.weather_code,
is_day: current.is_day === 1,
precipitation: `${current.precipitation} mm`,
rain: `${current.rain} mm`,
showers: `${current.showers} mm`,
snowfall: `${current.snowfall} mm`,
cloud_cover: `${current.cloud_cover}%`,
pressure_sea_level: `${current.pressure_msl} hPa`,
surface_pressure: `${current.surface_pressure} hPa`,
wind_speed: `${current.wind_speed_10m} km/h`,
wind_direction: `${current.wind_direction_10m}°`,
wind_gusts: `${current.wind_gusts_10m} km/h`,
},
};
return {
content: [
{
type: 'text',
text: `# Current Weather
**Location:** ${data.latitude.toFixed(4)}°N, ${data.longitude.toFixed(4)}°E
**Elevation:** ${data.elevation}m
**Timezone:** ${data.timezone}
**Time:** ${current.time}
## Weather Conditions
- **Description:** ${weatherDescription}
- **Temperature:** ${current.temperature_2m}${tempUnit}
- **Feels Like:** ${current.apparent_temperature}${tempUnit}
- **Humidity:** ${current.relative_humidity_2m}%
- **Cloud Cover:** ${current.cloud_cover}%
- **Day/Night:** ${current.is_day === 1 ? 'Day' : 'Night'}
## Precipitation
- **Current Precipitation:** ${current.precipitation} mm
- **Rain:** ${current.rain} mm
- **Showers:** ${current.showers} mm
- **Snowfall:** ${current.snowfall} mm
## Wind & Pressure
- **Wind Speed:** ${current.wind_speed_10m} km/h
- **Wind Direction:** ${current.wind_direction_10m}°
- **Wind Gusts:** ${current.wind_gusts_10m} km/h
- **Sea Level Pressure:** ${current.pressure_msl} hPa
- **Surface Pressure:** ${current.surface_pressure} hPa`,
},
{
type: 'text',
text: JSON.stringify(result, null, 2),
},
],
};
} catch (error) {
throw new Error(`Failed to fetch current weather: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}