File size: 2,152 Bytes
2e0f4f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// HuggingBot Module: Weather
(function(MS){
  MS.register("weather", {
    name: "Weather",
    description: "Get current weather and forecasts for any location worldwide using wttr.in (free).",
    tools: [{
      type: "function",
      function: {
        name: "get_weather",
        description: "Get current weather and forecast for any location. Supports city names, zip codes, airport codes.",
        parameters: {
          type: "object",
          properties: {
            location: { type: "string", description: "City name, zip code, or lat/lon" },
            forecast_days: { type: "number", description: "Forecast days (0-5)", default: 3 }
          },
          required: ["location"]
        }
      }
    }],
    handleToolCall: async function(tn, args) {
      if (tn !== "get_weather") return null;
      try {
        var r = await fetch("https://wttr.in/" + encodeURIComponent(args.location) + "?format=j1");
        if (!r.ok) throw Error("HTTP " + r.status);
        var d = await r.json();
        var c = d.nearest_area && d.nearest_area[0]
          ? d.nearest_area[0].areaName[0].value + ", " + d.nearest_area[0].country[0].value
          : args.location;
        var l = ["Location: " + c, ""];
        var cur = d.current_condition && d.current_condition[0];
        if (cur) {
          l.push("### Current Conditions");
          l.push((cur.weatherDesc && cur.weatherDesc[0] ? cur.weatherDesc[0].value : "?") +
            " -- " + cur.temp_C + "C (" + cur.temp_F + "F)");
          l.push("Humidity: " + cur.humidity + "% | Wind: " + cur.windspeedKmph + " km/h");
        }
        var days = Math.min(Math.max(parseInt(args.forecast_days) || 3, 0), 5);
        if (d.weather && d.weather.length > 1 && days > 0) {
          l.push("### " + days + "-Day Forecast");
          for (var i = 1; i <= Math.min(d.weather.length - 1, days); i++) {
            var w = d.weather[i];
            l.push(w.date + ": " + w.maxtempC + "C / " + w.mintempC + "C");
          }
        }
        return l.join("\n");
      } catch (e) {
        return "Weather unavailable: " + e.message;
      }
    }
  });
})(ModuleSystem);