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
36 changes: 34 additions & 2 deletions src/tools/weatherapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@ interface NubilaWeatherResponse {
// ... other top-level properties if any
}

interface W3bstreamResponse {
less: boolean;
ok: boolean;
proof: string;
}

export class WeatherTool implements Tool {
name: string = "WeatherAPI";
description: string = "Gets the current weather from Nubila API. Input is json with latitude and longitude to retrieve weather data.";
description: string = "Gets the current weather from Nubila API. Input is json with latitude and longitude and temperature to retrieve weather data.";
private readonly apiKey: string;
private readonly baseUrl: string;
private readonly wsBaseUrl: string;

constructor(apiKey: string) {
this.apiKey = apiKey;
this.baseUrl = 'https://api.nubila.ai/api/v1/weather';
this.wsBaseUrl = 'https://dragonfruit-testnet.w3bstream.com/task/weather'
}

async execute(userInput: any): Promise<string> {
Expand Down Expand Up @@ -62,8 +70,32 @@ export class WeatherTool implements Tool {
const windSpeed = weatherData.wind_speed ? ` Wind Speed: ${weatherData.wind_speed} m/s` : "";
const windDirection = weatherData.wind_direction ? ` Wind Direction: ${weatherData.wind_direction}°` : "";

let proofResp: string = ``
if ('temperature' in userInput) {
const url = `${this.wsBaseUrl}?temperature=${temperature}&expected_temperature=${userInput.temperature}`;
try {
const response = await fetch(url, {});
if (!response.ok) {
const errorData = await response.json();
const errorMessage = errorData?.message || `ws API request failed with status: ${response.status} ${response.statusText}`;
return `W3bstream API Error: ${errorMessage}`;
}

const data: W3bstreamResponse = await response.json();
console.log("W3bstream API Response:", data);

let state = `less`
if (!data.less) {
state = `greater`
}
proofResp = `, expected temperature ${userInput.temperature} is ${state} than real temperature, here is the proof ${data.proof}`
} catch (error) {
console.error("Error fetching w3bstream data:", error);
return "Could not retrieve w3bstream information. Please check the API or your network connection.";
}
}

return `The current weather in ${userInput.latitude}, ${userInput.longitude} is ${weatherDescription} with a temperature of ${temperature}°C${feelsLike}.${humidity}${pressure}${windSpeed}${windDirection}`;
return `The current weather in ${userInput.latitude}, ${userInput.longitude} is ${weatherDescription} with a temperature of ${temperature}°C${feelsLike}.${humidity}${pressure}${windSpeed}${windDirection}${proofResp}`;
} catch (error) {
console.error("Error fetching weather data:", error);
return "Could not retrieve weather information. Please check the API or your network connection.";
Expand Down