import { WeatherClient } from '@deepagent/weather'import { tool } from 'ai'import { z } from 'zod'export const weatherTool = tool({ description: 'Get the weather in a location', inputSchema: z.object({ location: z.string().describe('The location to get the weather for'), }), execute: async function ({ location }) { try { const cleanedLocation = location.trim().toLowerCase() const weather = new WeatherClient() const res = await weather.getCurrentWeather(cleanedLocation) if (!res || !res.current || !res.location) { return { error: 'Sorry, we don’t have weather data for that location.' } } return res; } catch (err: any) { const status = err?.response?.status || err?.status if (status === 400) { return { error: `Sorry, we don’t have weather data for "${location}".`, } } return { error: `Something went wrong while fetching weather for "${location}". Please try again later.`, } } }});
import { generateText } from 'ai'import { openai } from '@ai-sdk/openai'const result = await generateText({ model: openai('gpt-4-turbo'), messages: [ { role: 'user', content: 'What\'s the weather like in Tokyo today?' } ], tools: { getWeather: tool({ description: 'Get the weather in a location', inputSchema: z.object({ location: z.string().describe('The location to get the weather for'), }), execute: async function ({ location }) { try { const cleanedLocation = location.trim().toLowerCase() const weather = new WeatherClient() const res = await weather.getCurrentWeather(cleanedLocation) if (!res || !res.current || !res.location) { return { error: 'Sorry, we don’t have weather data for that location.' } } return res; } catch (err: any) { const status = err?.response?.status || err?.status if (status === 400) { return { error: `Sorry, we don’t have weather data for "${location}".`, } } return { error: `Something went wrong while fetching weather for "${location}". Please try again later.`, } } }}), },})console.log(result.text)