Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add weather tool to fetch weather data #1

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion src/lib/server/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import calculator from "./calculator";
import directlyAnswer from "./directlyAnswer";
import fetchUrl from "./web/url";
import websearch from "./web/search";
import weather from "./weather";
import { callSpace, getIpToken } from "./utils";
import { uploadFile } from "../files/uploadFile";
import type { MessageFile } from "$lib/types/Message";
Expand Down Expand Up @@ -127,7 +128,7 @@ export const configTools = z
}))
)
// add the extra hardcoded tools
.transform((val) => [...val, calculator, directlyAnswer, fetchUrl, websearch]);
.transform((val) => [...val, calculator, directlyAnswer, fetchUrl, websearch, weather]);

export function getCallMethod(tool: Omit<BaseTool, "call">): BackendCall {
return async function* (params, ctx, uuid) {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/server/tools/outputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export const ToolOutputPaths: Record<
type: "str",
path: "$",
},
weather: {
type: "str",
path: "$",
},
};

export const isValidOutputComponent = (
Expand Down
25 changes: 25 additions & 0 deletions src/lib/server/tools/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,28 @@ export async function extractJson(text: string): Promise<unknown[]> {
}
return calls.flat();
}

export async function fetchWeatherData(latitude: number, longitude: number): Promise<any> {
const response = await fetch(
`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&hourly=temperature_2m`
);
if (!response.ok) {
throw new Error("Failed to fetch weather data");
}
return response.json();
}

export async function fetchCoordinates(location: string): Promise<{ latitude: number; longitude: number }> {
const response = await fetch(
`https://geocoding-api.open-meteo.com/v1/search?name=${location}&count=1`
);
if (!response.ok) {
throw new Error("Failed to fetch coordinates");
}
const data = await response.json();
if (data.results.length === 0) {
throw new Error("Location not found");
}
const { latitude, longitude } = data.results[0];
return { latitude, longitude };
}
39 changes: 39 additions & 0 deletions src/lib/server/tools/weather.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { ConfigTool } from "$lib/types/Tool";
import { ObjectId } from "mongodb";
import { fetchWeatherData, fetchCoordinates } from "./utils";

const weather: ConfigTool = {
_id: new ObjectId("00000000000000000000000D"),
type: "config",
description: "Fetch the weather for a specified location",
color: "blue",
icon: "cloud",
displayName: "Weather",
name: "weather",
endpoint: null,
inputs: [
{
name: "location",
type: "str",
description: "The name of the location to fetch the weather for",
paramType: "required",
},
],
outputComponent: null,
outputComponentIdx: null,
showOutput: false,
async *call({ location }) {
try {
const coordinates = await fetchCoordinates(location);
const weatherData = await fetchWeatherData(coordinates.latitude, coordinates.longitude);

return {
outputs: [{ weather: weatherData }],
};
} catch (error) {
throw new Error("Failed to fetch weather data", { cause: error });
}
},
};

export default weather;