From 3ba911a3aea94616ac63e8b3e89328ee33b3b313 Mon Sep 17 00:00:00 2001 From: huangzhiran Date: Sat, 4 Jan 2025 21:06:27 +0800 Subject: [PATCH 1/2] support zk prove --- src/tools/weatherapi.ts | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/tools/weatherapi.ts b/src/tools/weatherapi.ts index 5214492..3d64261 100644 --- a/src/tools/weatherapi.ts +++ b/src/tools/weatherapi.ts @@ -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 baseWsUrl: string; constructor(apiKey: string) { this.apiKey = apiKey; this.baseUrl = 'https://api.nubila.ai/api/v1/weather'; + this.baseWsUrl = 'https://dragonfruit-testnet.w3bstream.com/task/weather' } async execute(userInput: any): Promise { @@ -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 wsResp: string = `` + if ('temperature' in userInput) { + const url = `${this.baseWsUrl}?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` + } + wsResp = `, 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}${wsResp}`; } catch (error) { console.error("Error fetching weather data:", error); return "Could not retrieve weather information. Please check the API or your network connection."; From 68110afb4b3ee68f4ac668105559a040fbddef1c Mon Sep 17 00:00:00 2001 From: huangzhiran Date: Sat, 4 Jan 2025 21:13:36 +0800 Subject: [PATCH 2/2] rename --- src/tools/weatherapi.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/tools/weatherapi.ts b/src/tools/weatherapi.ts index 3d64261..971b8c9 100644 --- a/src/tools/weatherapi.ts +++ b/src/tools/weatherapi.ts @@ -28,12 +28,12 @@ export class WeatherTool implements Tool { 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 baseWsUrl: string; + private readonly wsBaseUrl: string; constructor(apiKey: string) { this.apiKey = apiKey; this.baseUrl = 'https://api.nubila.ai/api/v1/weather'; - this.baseWsUrl = 'https://dragonfruit-testnet.w3bstream.com/task/weather' + this.wsBaseUrl = 'https://dragonfruit-testnet.w3bstream.com/task/weather' } async execute(userInput: any): Promise { @@ -70,9 +70,9 @@ 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 wsResp: string = `` + let proofResp: string = `` if ('temperature' in userInput) { - const url = `${this.baseWsUrl}?temperature=${temperature}&expected_temperature=${userInput.temperature}`; + const url = `${this.wsBaseUrl}?temperature=${temperature}&expected_temperature=${userInput.temperature}`; try { const response = await fetch(url, {}); if (!response.ok) { @@ -88,14 +88,14 @@ export class WeatherTool implements Tool { if (!data.less) { state = `greater` } - wsResp = `, expected temperature ${userInput.temperature} is ${state} than real temperature, here is the proof ${data.proof}` + 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}${wsResp}`; + 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.";