Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions app/(chat)/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,21 @@ export async function POST(request: Request) {
getWeather: {
description: "Get the current weather at a location",
parameters: z.object({
latitude: z.number().describe("Latitude coordinate"),
longitude: z.number().describe("Longitude coordinate"),
city: z.string().describe("Name of the city"),
}),
execute: async ({ latitude, longitude }) => {
execute: async ({ city }) => {
const geoResponse = await fetch(
`https://nominatim.openstreetmap.org/search?city=${encodeURIComponent(city)}&format=json&limit=1`
);
const geoData = await geoResponse.json();
if (!geoData.length) {
throw new Error("City not found");
}

const { lat, lon } = geoData[0]; // Extract latitude and longitude

const response = await fetch(
`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&current=temperature_2m&hourly=temperature_2m&daily=sunrise,sunset&timezone=auto`,
`https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lon}&current=temperature_2m&hourly=temperature_2m&daily=sunrise,sunset&timezone=auto`,
);

const weatherData = await response.json();
Expand Down