Changeset 0.11.0 (#62)
This commit is contained in:
64
server/tools/datetime.go
Normal file
64
server/tools/datetime.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
Register(&DateTimeTool{})
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════
|
||||
// datetime
|
||||
// ═══════════════════════════════════════════
|
||||
|
||||
type DateTimeTool struct{}
|
||||
|
||||
func (t *DateTimeTool) Definition() ToolDef {
|
||||
return ToolDef{
|
||||
Name: "datetime",
|
||||
Description: "Get the current date, time, timezone, and day of week. Use this whenever you need to know the current date or time, calculate days between dates, or answer questions about what day it is. Do NOT guess dates — always call this tool.",
|
||||
Parameters: JSONSchema(map[string]interface{}{
|
||||
"timezone": Prop("string", "IANA timezone name, e.g. 'America/New_York', 'UTC', 'Europe/London'. Defaults to UTC."),
|
||||
}, nil),
|
||||
}
|
||||
}
|
||||
|
||||
func (t *DateTimeTool) Execute(ctx context.Context, execCtx ExecutionContext, argsJSON string) (string, error) {
|
||||
var args struct {
|
||||
Timezone string `json:"timezone"`
|
||||
}
|
||||
if argsJSON != "" {
|
||||
_ = json.Unmarshal([]byte(argsJSON), &args)
|
||||
}
|
||||
|
||||
loc := time.UTC
|
||||
if args.Timezone != "" {
|
||||
parsed, err := time.LoadLocation(args.Timezone)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid timezone %q: %w", args.Timezone, err)
|
||||
}
|
||||
loc = parsed
|
||||
}
|
||||
|
||||
now := time.Now().In(loc)
|
||||
|
||||
year, week := now.ISOWeek()
|
||||
|
||||
result := map[string]interface{}{
|
||||
"datetime": now.Format(time.RFC3339),
|
||||
"date": now.Format("2006-01-02"),
|
||||
"time": now.Format("15:04:05"),
|
||||
"day": now.Weekday().String(),
|
||||
"timezone": loc.String(),
|
||||
"unix": now.Unix(),
|
||||
"iso_week": fmt.Sprintf("%d-W%02d", year, week),
|
||||
"day_of_year": now.YearDay(),
|
||||
}
|
||||
|
||||
b, _ := json.Marshal(result)
|
||||
return string(b), nil
|
||||
}
|
||||
Reference in New Issue
Block a user