feat: add date-based weather forecasts and dynamic time context

This commit is contained in:
loveuer committed 2026-11-18 11:30:38 +08:00
1 parent a394f22d77
commit 975352c7d6
12 files changed
+607 -15

No files matched your search

+20 -6
View File
@@ -13,14 +13,20 @@ import (
"example.com/ueino/pkg/model"
)
// Weather 通过 wttr.in 查询当前天气。
// Weather 查询 wttr.in 当前天气或 Open-Meteo 日期预报。
type Weather struct {
client *http.Client
baseURL string
client *http.Client
baseURL string
geocodingURL string
forecastURL string
}
func NewWeather() *Weather {
return &Weather{client: &http.Client{Timeout: 10 * time.Second}, baseURL: "https://wttr.in"}
return &Weather{
client: &http.Client{Timeout: 10 * time.Second}, baseURL: "https://wttr.in",
geocodingURL: "https://geocoding-api.open-meteo.com/v1/search",
forecastURL: "https://api.open-meteo.com/v1/forecast",
}
}
func (w *Weather) Definition() model.ToolDefinition {
@@ -28,11 +34,13 @@ func (w *Weather) Definition() model.ToolDefinition {
Type: "function",
Function: model.ToolFunctionDefinition{
Name: "get_weather",
Description: "查询指定地点的当前天气,不提供未来预报。地点可从当前对话中获取;如果未提供地点,先询问用户,不要猜测。",
Description: "查询指定地点天气。不传日期查询当前天气;传 start_date 查询该日,end_date 可指定结束日期(含)。预报支持北京时间今天起共16天,不支持历史天气。日期必须是 YYYY-MM-DD,明天、本周末等需根据系统当前日期换算。地点可从对话获取,缺少地点时先询问,不要猜测。",
Parameters: map[string]any{
"type": "object",
"properties": map[string]any{
"location": map[string]string{"type": "string", "description": "需要查询的城市或地点,例如北京、上海"},
"location": map[string]string{"type": "string", "description": "需要查询的城市或地点,例如北京、上海"},
"start_date": map[string]string{"type": "string", "description": "预报开始日期,YYYY-MM-DD;不传则查当前天气"},
"end_date": map[string]string{"type": "string", "description": "预报结束日期,YYYY-MM-DD,含当天;不传则只查开始日期,不能单独提供"},
},
"required": []string{"location"},
},
@@ -49,6 +57,12 @@ func (w *Weather) Execute(ctx context.Context, arguments string) (string, error)
if location == "" {
return "", fmt.Errorf("请提供要查询的地点")
}
args.Location = location
args.StartDate = strings.TrimSpace(args.StartDate)
args.EndDate = strings.TrimSpace(args.EndDate)
if args.StartDate != "" || args.EndDate != "" {
return w.forecast(ctx, args)
}
query := url.Values{"format": {"3"}, "lang": {"zh"}, "m": {""}}
endpoint := w.baseURL + "/" + url.PathEscape(location) + "?" + query.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)