|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package functions |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "github.com/pebble-dev/bobby-assistant/service/assistant/quota" |
| 20 | + "google.golang.org/genai" |
| 21 | + "strings" |
| 22 | +) |
| 23 | + |
| 24 | +type UpdateSettingsInput struct { |
| 25 | + UnitSystem string `json:"unit_system"` |
| 26 | + ResponseLanguage string `json:"response_language"` |
| 27 | + AlarmVibrationPattern string `json:"alarm_vibration_pattern"` |
| 28 | + TimerVibrationPattern string `json:"timer_vibration_pattern"` |
| 29 | + QuickLaunchBehaviour string `json:"quick_launch_behaviour"` |
| 30 | + ConfirmPrompts *bool `json:"confirm_prompts"` |
| 31 | +} |
| 32 | + |
| 33 | +func init() { |
| 34 | + t := true |
| 35 | + registerFunction(Registration{ |
| 36 | + Definition: genai.FunctionDeclaration{ |
| 37 | + Name: "update_settings", |
| 38 | + Description: "Update the user's settings, e.g. their preferred unit system. Call if and only if the user asks you to change something. Properties not specified won't be changed. No property is required. For security reasons, changing the location permission can't be done with this method - the user must go to the settings page.", |
| 39 | + Parameters: &genai.Schema{ |
| 40 | + Type: genai.TypeObject, |
| 41 | + Properties: map[string]*genai.Schema{ |
| 42 | + "unit_system": { |
| 43 | + Type: genai.TypeString, |
| 44 | + Description: "Whether the user prefers metric, imperial, 'UK hybrid' (temperature in celsius, distance in miles), or both metric and imperial. Or, 'auto' to figure it out based on the user's location.", |
| 45 | + Enum: []string{"auto", "imperial", "metric", "uk hybrid", "both"}, |
| 46 | + }, |
| 47 | + "response_language": { |
| 48 | + Type: genai.TypeString, |
| 49 | + Description: "The user's preferred response language. This is the language in which the assistant will respond to the user, or 'automatic' to use the language of the user's last message.", |
| 50 | + Enum: []string{"auto", "af_ZA", "id_ID", "ms_MY", "cs_CZ", "da_DK", "de_DE", |
| 51 | + "en_US", "es_ES", "fil_PH", "fr_FR", "gl_ES", "hr_HR", "is_IS", "it_IT", "sw_TZ", "lv_LV", |
| 52 | + "lt_LT", "hu_HU", "nl_NL", "no_NO", "pl_PL", "pt_PT", "ro_RO", "ru_RU", "sk_SK", "sl_SI", |
| 53 | + "fi_FI", "sv_SE", "tr_TR", "zu_ZA"}, |
| 54 | + }, |
| 55 | + "alarm_vibration_pattern": { |
| 56 | + Type: genai.TypeString, |
| 57 | + Description: "The user's preferred alarm vibration pattern, used when alarms go off.", |
| 58 | + Enum: []string{"Reveille", "Mario", "Nudge Nudge", "Jackhammer", "Standard"}, |
| 59 | + }, |
| 60 | + "timer_vibration_pattern": { |
| 61 | + Type: genai.TypeString, |
| 62 | + Description: "The user's preferred timer vibration pattern, used when timers go off.", |
| 63 | + Enum: []string{"Reveille", "Mario", "Nudge Nudge", "Jackhammer", "Standard"}, |
| 64 | + }, |
| 65 | + "quick_launch_behaviour": { |
| 66 | + Type: genai.TypeString, |
| 67 | + Description: "The user's preferred quick launch behaviour. The app can open the home screen (same as a non-quick launch), open the conversation but time out and quit after a minute, or open the conversation and stick around.", |
| 68 | + Enum: []string{"open home screen", "start conversation and time out", "start conversation and stay open"}, |
| 69 | + }, |
| 70 | + "confirm_prompts": { |
| 71 | + Type: genai.TypeBoolean, |
| 72 | + Description: "Whether the user wants to be asked to confirm their all of their queries before acting on them", |
| 73 | + Nullable: &t, |
| 74 | + }, |
| 75 | + }, |
| 76 | + Required: []string{}, |
| 77 | + }, |
| 78 | + }, |
| 79 | + Cb: updateSettingsImpl, |
| 80 | + Thought: updateSettingsThought, |
| 81 | + InputType: UpdateSettingsInput{}, |
| 82 | + Capability: "update_settings", |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +func updateSettingsImpl(ctx context.Context, quotaTracker *quota.Tracker, i any, requestChan chan<- map[string]any, responseChan <-chan map[string]any) any { |
| 87 | + arg := i.(*UpdateSettingsInput) |
| 88 | + request := map[string]any{ |
| 89 | + "action": "update_settings", |
| 90 | + } |
| 91 | + if arg.UnitSystem != "" { |
| 92 | + request["unitSystem"] = arg.UnitSystem |
| 93 | + } |
| 94 | + if arg.ResponseLanguage != "" { |
| 95 | + request["responseLanguage"] = arg.ResponseLanguage |
| 96 | + } |
| 97 | + if arg.AlarmVibrationPattern != "" { |
| 98 | + request["alarmVibrationPattern"] = arg.AlarmVibrationPattern |
| 99 | + } |
| 100 | + if arg.TimerVibrationPattern != "" { |
| 101 | + request["timerVibrationPattern"] = arg.TimerVibrationPattern |
| 102 | + } |
| 103 | + if arg.QuickLaunchBehaviour != "" { |
| 104 | + request["quickLaunchBehaviour"] = arg.QuickLaunchBehaviour |
| 105 | + } |
| 106 | + if arg.ConfirmPrompts != nil { |
| 107 | + request["confirmPrompts"] = *arg.ConfirmPrompts |
| 108 | + } |
| 109 | + requestChan <- request |
| 110 | + response := <-responseChan |
| 111 | + return response |
| 112 | +} |
| 113 | + |
| 114 | +func updateSettingsThought(args any) string { |
| 115 | + i := args.(*UpdateSettingsInput) |
| 116 | + var settingTypes []string |
| 117 | + if i.UnitSystem != "" { |
| 118 | + settingTypes = append(settingTypes, "unit system") |
| 119 | + } |
| 120 | + if i.ResponseLanguage != "" { |
| 121 | + settingTypes = append(settingTypes, "response language") |
| 122 | + } |
| 123 | + if i.AlarmVibrationPattern != "" { |
| 124 | + settingTypes = append(settingTypes, "alarm vibration") |
| 125 | + } |
| 126 | + if i.TimerVibrationPattern != "" { |
| 127 | + settingTypes = append(settingTypes, "timer vibration") |
| 128 | + } |
| 129 | + if i.QuickLaunchBehaviour != "" { |
| 130 | + settingTypes = append(settingTypes, "quick launch behaviour") |
| 131 | + } |
| 132 | + if i.ConfirmPrompts != nil { |
| 133 | + settingTypes = append(settingTypes, "prompt confirmation") |
| 134 | + } |
| 135 | + if len(settingTypes) == 0 { |
| 136 | + return "Updating no settings..." |
| 137 | + } |
| 138 | + var settingString string |
| 139 | + if len(settingTypes) == 1 { |
| 140 | + settingString = settingTypes[0] |
| 141 | + } else { |
| 142 | + lastEntry := settingTypes[len(settingTypes)-1] |
| 143 | + settingString = strings.Join(settingTypes[0:len(settingTypes)-1], ", ") + " and " + lastEntry |
| 144 | + } |
| 145 | + return "Updating " + settingString + " settings..." |
| 146 | +} |
0 commit comments