-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathweather-tool.ts
More file actions
249 lines (225 loc) · 7.5 KB
/
weather-tool.ts
File metadata and controls
249 lines (225 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { Tool } from "@mastra/core/tools";
import { z } from "zod";
const weatherInputSchema = z.object({
location: z.string().describe("The city and country to get weather for"),
days: z.number().min(1).max(7).default(1).describe(
"Number of days to forecast",
),
includeHourly: z.boolean().default(true).describe(
"Include hourly forecast data",
),
});
export const weatherTool = new Tool({
id: "weather-tool",
description:
"Get detailed weather forecast including hourly data for temperature, precipitation, and conditions",
inputSchema: weatherInputSchema,
outputSchema: z.object({
location: z.string(),
current: z.object({
temperature: z.number(),
feelsLike: z.number(),
windSpeed: z.number(),
conditions: z.string(),
uvIndex: z.number(),
rainChance: z.number().optional(),
}),
today: z.object({
maxTemp: z.number(),
minTemp: z.number(),
conditions: z.string(),
rainChance: z.number(),
maxWindSpeed: z.number(),
maxUvIndex: z.number(),
}),
hourlyHighlights: z.array(z.object({
time: z.string(),
temperature: z.number(),
conditions: z.string(),
rainChance: z.number(),
})).optional(),
}),
execute: async ({ context }) => {
const { location, days, includeHourly } = context;
try {
// Try original location first, then fallback to city name only
const attempts = [
location,
location.split(",")[0].trim(), // Just the city name
];
let geoData = null;
for (const attempt of attempts) {
console.log(`Geocoding: "${attempt}"`);
const geoResponse = await fetch(
`https://geocoding-api.open-meteo.com/v1/search?name=${
encodeURIComponent(attempt)
}&count=1&language=en&format=json`,
);
if (!geoResponse.ok) {
console.warn(
`Geocoding API error for "${attempt}": ${geoResponse.status}`,
);
continue;
}
const tempGeoData = await geoResponse.json();
if (tempGeoData.results && tempGeoData.results.length > 0) {
geoData = tempGeoData;
console.log(
`Found location: ${geoData.results[0].name}, ${
geoData.results[0].country
}`,
);
break;
}
}
if (!geoData || !geoData.results || geoData.results.length === 0) {
throw new Error(
`Location "${location}" not found. Please try a different city name.`,
);
}
const { latitude, longitude, name, country, timezone } =
geoData.results[0];
const fullLocation = `${name}, ${country}`;
console.log(
`Successfully geocoded "${location}" to ${fullLocation} (${latitude}, ${longitude})`,
);
// Build weather API URL with detailed parameters
const weatherParams = new URLSearchParams({
latitude: latitude.toString(),
longitude: longitude.toString(),
current: [
"temperature_2m",
"apparent_temperature",
"relative_humidity_2m",
"weather_code",
"wind_speed_10m",
"wind_gusts_10m",
"visibility",
"uv_index",
].join(","),
daily: [
"temperature_2m_max",
"temperature_2m_min",
"weather_code",
"precipitation_sum",
"precipitation_probability_max",
"wind_speed_10m_max",
"wind_gusts_10m_max",
"uv_index_max",
].join(","),
timezone: "auto",
forecast_days: days.toString(),
});
// Add hourly data if requested
if (includeHourly) {
weatherParams.append(
"hourly",
[
"temperature_2m",
"apparent_temperature",
"relative_humidity_2m",
"weather_code",
"wind_speed_10m",
"wind_gusts_10m",
"precipitation",
"precipitation_probability",
"uv_index",
"visibility",
].join(","),
);
}
const weatherResponse = await fetch(
`https://api.open-meteo.com/v1/forecast?${weatherParams}`,
);
if (!weatherResponse.ok) {
throw new Error(
`Weather API error: ${weatherResponse.status} - ${weatherResponse.statusText}`,
);
}
const weatherData = await weatherResponse.json();
// Check if we got valid weather data
if (!weatherData.current) {
throw new Error("Invalid weather data received from API");
}
// Weather code mapping
const getWeatherDescription = (code: number): string => {
const weatherCodes: { [key: number]: string } = {
0: "Clear sky",
1: "Mainly clear",
2: "Partly cloudy",
3: "Overcast",
45: "Fog",
48: "Depositing rime fog",
51: "Light drizzle",
53: "Moderate drizzle",
55: "Dense drizzle",
56: "Light freezing drizzle",
57: "Dense freezing drizzle",
61: "Slight rain",
63: "Moderate rain",
65: "Heavy rain",
66: "Light freezing rain",
67: "Heavy freezing rain",
71: "Slight snow fall",
73: "Moderate snow fall",
75: "Heavy snow fall",
77: "Snow grains",
80: "Slight rain showers",
81: "Moderate rain showers",
82: "Violent rain showers",
85: "Slight snow showers",
86: "Heavy snow showers",
95: "Slight thunderstorm",
96: "Thunderstorm with slight hail",
99: "Thunderstorm with heavy hail",
};
return weatherCodes[code] || "Unknown";
};
// Process current weather - simplified for clothing recommendations
const current = {
temperature: weatherData.current.temperature_2m,
feelsLike: weatherData.current.apparent_temperature,
windSpeed: weatherData.current.wind_speed_10m,
conditions: getWeatherDescription(weatherData.current.weather_code),
uvIndex: weatherData.current.uv_index,
};
// Process today's forecast - just the essentials
const today = {
maxTemp: weatherData.daily.temperature_2m_max[0],
minTemp: weatherData.daily.temperature_2m_min[0],
conditions: getWeatherDescription(weatherData.daily.weather_code[0]),
rainChance: weatherData.daily.precipitation_probability_max[0] || 0,
maxWindSpeed: weatherData.daily.wind_speed_10m_max[0],
maxUvIndex: weatherData.daily.uv_index_max[0],
};
// Optional: key hourly highlights for next 8 hours
const hourlyHighlights = includeHourly && weatherData.hourly
? weatherData.hourly.time.slice(0, 8).map((
time: string,
index: number,
) => ({
time,
temperature: weatherData.hourly.temperature_2m[index],
conditions: getWeatherDescription(
weatherData.hourly.weather_code[index],
),
rainChance: weatherData.hourly.precipitation_probability[index] || 0,
}))
: undefined;
console.log(`Weather data successfully retrieved for ${fullLocation}`);
return {
location: fullLocation,
current,
today,
hourlyHighlights,
};
} catch (error) {
console.error("Weather API call failed:", error);
throw new Error(
`Weather API call failed: ${
error instanceof Error ? error.message : "Unknown error"
}`,
);
}
},
});