-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.js
More file actions
162 lines (150 loc) · 5.11 KB
/
Copy pathindex.js
File metadata and controls
162 lines (150 loc) · 5.11 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const GEO_URL = "https://geocoding-api.open-meteo.com/v1/search";
const WEATHER_URL = "https://api.open-meteo.com/v1/forecast";
const WEATHER_CODES = {
0: "ясно",
1: "преимущественно ясно",
2: "переменная облачность",
3: "пасмурно",
45: "туман",
48: "изморозь",
51: "лёгкая морось",
53: "морось",
55: "сильная морось",
61: "небольшой дождь",
63: "дождь",
65: "сильный дождь",
66: "ледяной дождь",
67: "сильный ледяной дождь",
71: "небольшой снег",
73: "снег",
75: "сильный снег",
77: "снежные зёрна",
80: "ливень",
81: "сильный ливень",
82: "очень сильный ливень",
85: "снегопад",
86: "сильный снегопад",
95: "гроза",
96: "гроза с градом",
99: "гроза с сильным градом",
};
async function geocode(city) {
const url = `${GEO_URL}?name=${encodeURIComponent(city)}&count=1&language=ru`;
const res = await fetch(url, { signal: AbortSignal.timeout(15000) });
if (!res.ok) return null;
const data = await res.json();
if (!data.results || data.results.length === 0) return null;
return data.results[0];
}
export const manifest = {
name: "weather",
version: "1.0.0",
sdkVersion: "^2.0.0",
description: "Current weather and 7-day forecast via Open-Meteo (no API key required).",
};
export const tools = (sdk) => [
{
name: "weather_current",
description:
"Get current weather for a city. Returns temperature, feels like, humidity, wind speed, and description in Russian. Supports any city name.",
category: "data-bearing",
scope: "always",
parameters: {
type: "object",
properties: {
city: {
type: "string",
description:
'City name (e.g. "Moscow", "Туапсе", "New York", "London")',
},
},
required: ["city"],
},
execute: async (params) => {
try {
const geo = await geocode(params.city);
if (!geo) {
return { success: false, error: `City "${params.city}" not found` };
}
const url = `${WEATHER_URL}?latitude=${geo.latitude}&longitude=${geo.longitude}¤t=temperature_2m,apparent_temperature,relative_humidity_2m,wind_speed_10m,surface_pressure,weather_code&timezone=auto`;
const res = await fetch(url, { signal: AbortSignal.timeout(15000) });
if (!res.ok) {
return { success: false, error: `Weather API error: ${res.status}` };
}
const data = await res.json();
const c = data.current;
return {
success: true,
data: {
city: geo.name,
country: geo.country,
temp: `${Math.round(c.temperature_2m)}°C`,
feels_like: `${Math.round(c.apparent_temperature)}°C`,
description: WEATHER_CODES[c.weather_code] || `code ${c.weather_code}`,
humidity: `${c.relative_humidity_2m}%`,
wind: `${c.wind_speed_10m} km/h`,
pressure: `${Math.round(c.surface_pressure)} hPa`,
},
};
} catch (err) {
sdk.log.error("weather_current:", err.message);
return {
success: false,
error: String(err.message || err).slice(0, 500),
};
}
},
},
{
name: "weather_forecast",
description:
"Get 7-day weather forecast for a city. Returns daily min/max temperature and conditions.",
category: "data-bearing",
scope: "always",
parameters: {
type: "object",
properties: {
city: {
type: "string",
description: "City name",
},
},
required: ["city"],
},
execute: async (params) => {
try {
const geo = await geocode(params.city);
if (!geo) {
return { success: false, error: `City "${params.city}" not found` };
}
const url = `${WEATHER_URL}?latitude=${geo.latitude}&longitude=${geo.longitude}&daily=temperature_2m_max,temperature_2m_min,weather_code,wind_speed_10m_max&timezone=auto`;
const res = await fetch(url, { signal: AbortSignal.timeout(15000) });
if (!res.ok) {
return { success: false, error: `Weather API error: ${res.status}` };
}
const data = await res.json();
const d = data.daily;
const forecast = d.time.map((date, i) => ({
date,
temp: `${Math.round(d.temperature_2m_min[i])}..${Math.round(d.temperature_2m_max[i])}°C`,
description: WEATHER_CODES[d.weather_code[i]] || `code ${d.weather_code[i]}`,
wind_max: `${d.wind_speed_10m_max[i]} km/h`,
}));
return {
success: true,
data: {
city: geo.name,
country: geo.country,
forecast,
},
};
} catch (err) {
sdk.log.error("weather_forecast:", err.message);
return {
success: false,
error: String(err.message || err).slice(0, 500),
};
}
},
},
];