Files
ai-agents/agents/weather_agent.py
T

157 lines
5.6 KiB
Python

#!/usr/bin/env python3
"""
Weather Agent
Fetches weather for a configurable location and returns structured data + markdown.
Called by Daily Briefing agents with location config. Can also run standalone.
"""
import sys
from datetime import datetime
from shared import MT, api_request, log_run
AGENT_ID = "weather"
# Default location (Providence, UT)
DEFAULT_LOCATION = {
"name": "Providence",
"state": "Utah",
"country": "US",
"lat": 41.7064,
"lon": -111.8133,
}
WMO_CODES = {
0: "Clear sky", 1: "Mainly clear", 2: "Partly cloudy", 3: "Overcast",
45: "Foggy", 48: "Icy fog", 51: "Light drizzle", 53: "Drizzle",
55: "Heavy drizzle", 61: "Light rain", 63: "Rain", 65: "Heavy rain",
66: "Freezing rain", 67: "Heavy freezing rain",
71: "Light snow", 73: "Snow", 75: "Heavy snow", 77: "Snow grains",
80: "Light showers", 81: "Showers", 82: "Heavy showers",
85: "Light snow showers", 86: "Heavy snow showers",
95: "Thunderstorm", 96: "Thunderstorm w/ hail", 99: "Severe thunderstorm",
}
DAY_NAMES = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
def build_url(location):
"""Build the Open-Meteo API URL for a given location."""
lat = location["lat"]
lon = location["lon"]
return (
f"https://api.open-meteo.com/v1/forecast?"
f"latitude={lat}&longitude={lon}"
f"&current=temperature_2m,apparent_temperature,weather_code,wind_speed_10m,relative_humidity_2m"
f"&daily=weather_code,temperature_2m_max,temperature_2m_min,precipitation_sum,wind_speed_10m_max,sunrise,sunset"
f"&temperature_unit=fahrenheit&wind_speed_unit=mph&precipitation_unit=inch"
f"&timezone=America/Denver&forecast_days=7"
)
def location_label(location):
"""Human-readable location string."""
parts = [location.get("name", "")]
if location.get("state"):
parts.append(location["state"])
if location.get("country") and location["country"] != "US":
parts.append(location["country"])
return ", ".join(p for p in parts if p)
def fetch_weather(location=None):
"""Fetch weather data from Open-Meteo for a given location."""
loc = location or DEFAULT_LOCATION
url = build_url(loc)
return api_request(url)
def format_section(weather, location=None):
"""Format weather into a markdown section and a one-line summary."""
loc = location or DEFAULT_LOCATION
label = location_label(loc)
now = datetime.now(MT)
current = weather["current"]
daily = weather["daily"]
condition = WMO_CODES.get(current["weather_code"], "Unknown")
temp = round(current["temperature_2m"])
feels = round(current["apparent_temperature"])
wind = round(current["wind_speed_10m"])
humidity = current["relative_humidity_2m"]
md = f"## Weather — {label}\n\n"
md += "### Current Conditions\n\n"
md += "| | |\n|---|---|\n"
md += f"| **Condition** | {condition} |\n"
md += f"| **Temperature** | {temp}°F (feels like {feels}°F) |\n"
md += f"| **Wind** | {wind} mph |\n"
md += f"| **Humidity** | {humidity}% |\n\n"
md += "### 7-Day Forecast\n\n"
md += "| Day | Condition | High | Low | Precip | Wind |\n"
md += "|-----|-----------|------|-----|--------|------|\n"
for i in range(len(daily["time"])):
d = datetime.strptime(daily["time"][i], "%Y-%m-%d")
day_name = DAY_NAMES[d.weekday()]
if i == 0:
day_name = "**Today**"
elif i == 1:
day_name = "Tomorrow"
cond = WMO_CODES.get(daily["weather_code"][i], "?")
hi = round(daily["temperature_2m_max"][i])
lo = round(daily["temperature_2m_min"][i])
precip = daily["precipitation_sum"][i]
wind_max = round(daily["wind_speed_10m_max"][i])
precip_str = f'{precip}"' if precip > 0 else "-"
md += f"| {day_name} | {cond} | {hi}°F | {lo}°F | {precip_str} | {wind_max} mph |\n"
sunrise = daily["sunrise"][0].split("T")[1] if daily["sunrise"][0] else "?"
sunset = daily["sunset"][0].split("T")[1] if daily["sunset"][0] else "?"
md += f"\n**Sunrise:** {sunrise} | **Sunset:** {sunset}\n"
summary = f"{label}: {condition}, {temp}°F (feels like {feels}°F), wind {wind} mph"
return md, summary
def run(location=None):
"""Run the weather agent. Returns (markdown_section, summary) or raises."""
loc = location or DEFAULT_LOCATION
weather = fetch_weather(loc)
section, summary = format_section(weather, loc)
log_run(AGENT_ID, "success", output=summary, metadata={"location": location_label(loc)})
return section, summary
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Weather Agent")
parser.add_argument("--lat", type=float, help="Latitude")
parser.add_argument("--lon", type=float, help="Longitude")
parser.add_argument("--name", type=str, help="City/location name")
parser.add_argument("--state", type=str, help="State")
parser.add_argument("--country", type=str, default="US", help="Country code")
args = parser.parse_args()
loc = DEFAULT_LOCATION
if args.lat and args.lon:
loc = {
"name": args.name or "Custom",
"state": args.state or "",
"country": args.country,
"lat": args.lat,
"lon": args.lon,
}
try:
section, summary = run(loc)
print(section)
print(f"\nSummary: {summary}")
except Exception as e:
err_msg = f"{type(e).__name__}: {e}"
print(f"Error: {err_msg}", file=sys.stderr)
log_run(AGENT_ID, "failed", err=err_msg)
sys.exit(1)