|
| 1 | +package functions |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "google.golang.org/genai" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/honeycombio/beeline-go" |
| 11 | + "github.com/pebble-dev/bobby-assistant/service/assistant/quota" |
| 12 | +) |
| 13 | + |
| 14 | +type TimeResponse struct { |
| 15 | + Time string `json:"time"` |
| 16 | +} |
| 17 | + |
| 18 | +type GetTimeInput struct { |
| 19 | + // The timezone, e.g. 'America/Los_Angeles'. |
| 20 | + Timezone string `json:"timezone" jsonschema:"required"` |
| 21 | + // The number of seconds to add to the current time. |
| 22 | + Offset float64 `json:"offset"` |
| 23 | +} |
| 24 | + |
| 25 | +func init() { |
| 26 | + registerFunction(Registration{ |
| 27 | + Definition: genai.FunctionDeclaration{ |
| 28 | + Name: "get_time_elsewhere", |
| 29 | + Description: "Get the current time in a given valid tzdb timezone. Not all cities have a tzdb entry - be sure to use one that exists. Call multiple times to find the time in multiple timezones.", |
| 30 | + Parameters: &genai.Schema{ |
| 31 | + Type: genai.TypeObject, |
| 32 | + Nullable: false, |
| 33 | + Properties: map[string]*genai.Schema{ |
| 34 | + "timezone": { |
| 35 | + Type: genai.TypeString, |
| 36 | + Description: "The timezone, e.g. 'America/Los_Angeles'.", |
| 37 | + Nullable: false, |
| 38 | + }, |
| 39 | + "offset": { |
| 40 | + Type: genai.TypeNumber, |
| 41 | + Description: "The number of seconds to add to the current time, if checking a different time. Omit or set to zero for current time.", |
| 42 | + Format: "double", |
| 43 | + }, |
| 44 | + }, |
| 45 | + Required: []string{"timezone"}, |
| 46 | + }, |
| 47 | + }, |
| 48 | + Fn: getTimeElsewhere, |
| 49 | + Thought: getTimeThought, |
| 50 | + InputType: GetTimeInput{}, |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +func getTimeThought(args interface{}) string { |
| 55 | + arg := args.(*GetTimeInput) |
| 56 | + if arg.Timezone != "" { |
| 57 | + s := strings.Split(arg.Timezone, "/") |
| 58 | + place := strings.Replace(s[len(s)-1], "_", " ", -1) |
| 59 | + return "Checking the time in " + place |
| 60 | + } |
| 61 | + return "Checking the time" |
| 62 | +} |
| 63 | + |
| 64 | +func getTimeElsewhere(ctx context.Context, quotaTracker *quota.Tracker, args interface{}) interface{} { |
| 65 | + ctx, span := beeline.StartSpan(ctx, "get_time_elsewhere") |
| 66 | + defer span.Send() |
| 67 | + arg := args.(*GetTimeInput) |
| 68 | + utc := time.Now().UTC().Add(time.Duration(arg.Offset) * time.Second) |
| 69 | + loc, err := time.LoadLocation(arg.Timezone) |
| 70 | + if err != nil { |
| 71 | + return Error{fmt.Sprintf("The timezone %q is not valid", arg.Timezone)} |
| 72 | + } |
| 73 | + utc.In(loc) |
| 74 | + return TimeResponse{utc.In(loc).Format(time.RFC1123)} |
| 75 | +} |
0 commit comments