11 * Creates a tool for making HTTP requests to vals in a Val Town project
12 */
13export const makeFetchTool = (
14 { bearerToken, project, branch_id }: { bearerToken?: string; project?: any; branch_id?: string } = {},
15) =>
16 tool({
17 name: "fetch",
18 description: "Make an HTTP request to a Val Town val and return the response. Useful for testing HTTP vals.",
19 parameters: z.object({
68 return {
69 type: "error",
70 message: `Error fetching val at path '${valPath}': ${error.message}`,
71 };
72 }
83 return {
84 type: "error",
85 message: `The val at path '${valPath}' is not an HTTP val. Only HTTP vals can be called with fetch.`,
86 };
87 }
111 let response;
112 try {
113 response = await fetch(valEndpoint + urlPath, options);
114 } catch (error: any) {
115 // Return error information
49 files={project.data?.files}
50 branchId={branchId}
51 refetch={project.refetch}
52 />
53 </ProjectContext>
59 files,
60 branchId,
61 refetch,
62}: {
63 project: any;
64 files: any[];
65 branchId: string;
66 refetch: () => void;
67}) {
68 const [images, setImages] = useState<(string|null)[]>([]);
94 if (!messages?.length) return;
95 let last = messages.at(-1);
96 if (shouldRefetch(last)) {
97 refetch();
98 }
99 }, [messages]);
194}
195
196function shouldRefetch (message) {
197 for (let i = 0; i < message?.parts?.length; i++) {
198 let part = message.parts[i];
19
20 try {
21 const response = await fetch(
22 `https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lon}¤t=temperature_2m,weather_code,wind_speed_10m,wind_direction_10m,relative_humidity_2m,surface_pressure,visibility,uv_index&hourly=temperature_2m,weather_code,precipitation,wind_speed_10m,wind_direction_10m,relative_humidity_2m&daily=temperature_2m_max,temperature_2m_min,weather_code,precipitation_sum,wind_speed_10m_max,wind_direction_10m_dominant,uv_index_max,sunrise,sunset&timezone=Europe/London`
23 );
68 } catch (error) {
69 console.error("Weather API error:", error);
70 return c.json({ error: "Failed to fetch weather data" }, 500);
71 }
72});
95 const weatherPromises = ukCities.map(async (city) => {
96 try {
97 const response = await fetch(
98 `https://api.open-meteo.com/v1/forecast?latitude=${city.lat}&longitude=${city.lon}¤t=temperature_2m,weather_code&timezone=Europe/London`
99 );
113 };
114 } catch (error) {
115 console.error(`Failed to fetch weather for ${city.name}:`, error);
116 return null;
117 }
124 } catch (error) {
125 console.error("Map weather API error:", error);
126 return c.json({ error: "Failed to fetch map weather data" }, 500);
127 }
128});
137 try {
138 // Use Open-Meteo geocoding API with focus on UK
139 const response = await fetch(
140 `https://geocoding-api.open-meteo.com/v1/search?name=${encodeURIComponent(query)}&count=10&language=en&format=json`
141 );
189 // Inject initial UK weather data to avoid extra round-trips
190 try {
191 const ukWeatherResponse = await fetch(
192 "https://api.open-meteo.com/v1/forecast?latitude=54.5&longitude=-2¤t=temperature_2m,weather_code,wind_speed_10m,wind_direction_10m,relative_humidity_2m,surface_pressure,visibility,uv_index&hourly=temperature_2m,weather_code,precipitation,wind_speed_10m,wind_direction_10m,relative_humidity_2m&daily=temperature_2m_max,temperature_2m_min,weather_code,precipitation_sum,wind_speed_10m_max,wind_direction_10m_dominant,uv_index_max,sunrise,sunset&timezone=Europe/London"
193 );
210 html = html.replace("</head>", `${dataScript}</head>`);
211 } catch (error) {
212 console.error("Failed to fetch initial weather data:", error);
213 }
214
216});
217
218export default app.fetch;