import * as z from "zod" import { tool } from "langchain" /** * 获取城市经纬度工具 * @param {string} city - 城市名称 * @returns {Object|null} - 返回经纬度对象 {lat, lng},如果未找到则返回null */ export const getLatLngTool = tool( async (input) => { const { city } = input try { // 使用open-meteo地理编码API获取经纬度 console.log('获取城市经纬度:', city) const url = `https://geocoding-api.open-meteo.com/v1/search?name=${encodeURIComponent(city)}&language=zh&count=1` const res = await fetch(url) const data = await res.json() if (data.results?.length) { return { lat: data.results[0].latitude, lng: data.results[0].longitude } } // 如果没有找到结果,返回null return null } catch (error) { console.error('地理编码API调用失败:', error) return null } }, { name: "get_lat_lng", description: `根据城市名称获取经纬度信息`, schema: z.object({ city: z.string().describe("城市名称,例如:北京市海淀区") }) } )