|
| 1 | +/* |
| 2 | +Copyright © 2025 Vapi, Inc. |
| 3 | +
|
| 4 | +Licensed under the MIT License (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +
|
| 7 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 8 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 9 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 10 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 11 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 12 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 13 | +THE SOFTWARE. |
| 14 | +
|
| 15 | +Authors: |
| 16 | +
|
| 17 | + |
| 18 | +*/ |
| 19 | +package cmd |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/AlecAivazis/survey/v2" |
| 27 | + "github.com/spf13/cobra" |
| 28 | + |
| 29 | + "github.com/VapiAI/cli/pkg/output" |
| 30 | +) |
| 31 | + |
| 32 | +// Manage custom tools and functions that connect voice agents to external APIs |
| 33 | +var toolCmd = &cobra.Command{ |
| 34 | + Use: "tool", |
| 35 | + Short: "Manage Vapi tools and functions", |
| 36 | + Long: `Manage custom tools and functions that connect your voice agents to external APIs and databases. |
| 37 | +
|
| 38 | +Tools enable your assistants to: |
| 39 | +- Call external APIs and web services |
| 40 | +- Access databases and business systems |
| 41 | +- Perform custom actions during conversations |
| 42 | +- Integrate with third-party platforms |
| 43 | +- Execute business logic and workflows`, |
| 44 | +} |
| 45 | + |
| 46 | +var listToolCmd = &cobra.Command{ |
| 47 | + Use: "list", |
| 48 | + Short: "List all tools", |
| 49 | + Long: `Display all custom tools and functions in your account with their configurations.`, |
| 50 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 51 | + fmt.Println("🔧 Listing tools...") |
| 52 | + |
| 53 | + ctx := context.Background() |
| 54 | + |
| 55 | + // Fetch tools from the API |
| 56 | + // Note: The exact API endpoint may vary, this follows the pattern of other list commands |
| 57 | + tools, err := vapiClient.GetClient().Tools.List(ctx, nil) |
| 58 | + if err != nil { |
| 59 | + // Check if this is a deserialization error related to new features |
| 60 | + if strings.Contains(err.Error(), "cannot be deserialized") { |
| 61 | + fmt.Println("⚠️ Warning: The Vapi API returned data in a format not yet supported by this CLI version.") |
| 62 | + fmt.Println(" This usually happens when new features are added to Vapi.") |
| 63 | + fmt.Println(" Please check for CLI updates: https://github.com/VapiAI/cli/releases") |
| 64 | + fmt.Println() |
| 65 | + fmt.Printf(" Technical details: %v\n", err) |
| 66 | + return fmt.Errorf("incompatible API response format") |
| 67 | + } |
| 68 | + return fmt.Errorf("failed to list tools: %w", err) |
| 69 | + } |
| 70 | + |
| 71 | + // Display as formatted JSON for complete details |
| 72 | + if err := output.PrintJSON(tools); err != nil { |
| 73 | + return fmt.Errorf("failed to display tools: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + return nil |
| 77 | + }, |
| 78 | +} |
| 79 | + |
| 80 | +var getToolCmd = &cobra.Command{ |
| 81 | + Use: "get [tool-id]", |
| 82 | + Short: "Get details of a specific tool", |
| 83 | + Long: `Retrieve the complete configuration of a tool including function definition, parameters, and settings.`, |
| 84 | + Args: cobra.ExactArgs(1), |
| 85 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 86 | + ctx := context.Background() |
| 87 | + toolID := args[0] |
| 88 | + |
| 89 | + fmt.Printf("🔍 Getting tool details for ID: %s\n", toolID) |
| 90 | + |
| 91 | + // Fetch the tool configuration |
| 92 | + tool, err := vapiClient.GetClient().Tools.Get(ctx, toolID) |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("failed to get tool: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + // Display as formatted JSON for easy reading |
| 98 | + if err := output.PrintJSON(tool); err != nil { |
| 99 | + return fmt.Errorf("failed to display tool: %w", err) |
| 100 | + } |
| 101 | + |
| 102 | + return nil |
| 103 | + }, |
| 104 | +} |
| 105 | + |
| 106 | +var createToolCmd = &cobra.Command{ |
| 107 | + Use: "create", |
| 108 | + Short: "Create a new custom tool", |
| 109 | + Long: `Create a new custom tool or function for your voice agents. |
| 110 | + |
| 111 | +Tool creation involves defining: |
| 112 | +- Function signatures and parameters |
| 113 | +- API endpoints and authentication |
| 114 | +- Response handling and data mapping |
| 115 | +- Error handling and fallback behavior |
| 116 | +
|
| 117 | +This is best done through the Vapi dashboard for visual tool configuration.`, |
| 118 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 119 | + fmt.Println("🔧 Creating a new tool...") |
| 120 | + fmt.Println() |
| 121 | + fmt.Println("Tool creation requires defining:") |
| 122 | + fmt.Println("- Function name and description") |
| 123 | + fmt.Println("- Input parameters and types") |
| 124 | + fmt.Println("- API endpoint configuration") |
| 125 | + fmt.Println("- Authentication and headers") |
| 126 | + fmt.Println("- Response data mapping") |
| 127 | + fmt.Println("- Error handling logic") |
| 128 | + fmt.Println() |
| 129 | + fmt.Println("Create tools through the Vapi dashboard:") |
| 130 | + fmt.Println("https://dashboard.vapi.ai/tools") |
| 131 | + fmt.Println() |
| 132 | + fmt.Println("For programmatic tool creation, use the Vapi API:") |
| 133 | + fmt.Println("POST /tools with function definition") |
| 134 | + fmt.Println() |
| 135 | + fmt.Println("Built-in tool types available:") |
| 136 | + fmt.Println("- Function tools (custom API calls)") |
| 137 | + fmt.Println("- End call tools") |
| 138 | + fmt.Println("- Transfer call tools") |
| 139 | + fmt.Println("- DTMF (keypad) tools") |
| 140 | + fmt.Println("- Integration tools (Google Sheets, etc.)") |
| 141 | + |
| 142 | + return nil |
| 143 | + }, |
| 144 | +} |
| 145 | + |
| 146 | +var updateToolCmd = &cobra.Command{ |
| 147 | + Use: "update [tool-id]", |
| 148 | + Short: "Update an existing tool", |
| 149 | + Long: `Update the configuration of an existing tool. |
| 150 | + |
| 151 | +This includes modifying function parameters, API endpoints, |
| 152 | +authentication, and response handling logic.`, |
| 153 | + Args: cobra.ExactArgs(1), |
| 154 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 155 | + toolID := args[0] |
| 156 | + |
| 157 | + fmt.Printf("📝 Update tool: %s\n", toolID) |
| 158 | + fmt.Println() |
| 159 | + fmt.Println("Tool updates can include:") |
| 160 | + fmt.Println("- Function signature changes") |
| 161 | + fmt.Println("- API endpoint modifications") |
| 162 | + fmt.Println("- Authentication updates") |
| 163 | + fmt.Println("- Parameter validation rules") |
| 164 | + fmt.Println("- Response data mapping") |
| 165 | + fmt.Println("- Error handling improvements") |
| 166 | + fmt.Println() |
| 167 | + fmt.Println("Update via the Vapi dashboard:") |
| 168 | + fmt.Printf("https://dashboard.vapi.ai/tools/%s\n", toolID) |
| 169 | + fmt.Println() |
| 170 | + fmt.Println("Or use the Vapi API: PATCH /tools/{id}") |
| 171 | + |
| 172 | + return nil |
| 173 | + }, |
| 174 | +} |
| 175 | + |
| 176 | +var deleteToolCmd = &cobra.Command{ |
| 177 | + Use: "delete [tool-id]", |
| 178 | + Short: "Delete a custom tool", |
| 179 | + Long: `Permanently delete a custom tool. This will remove it from all assistants using it.`, |
| 180 | + Args: cobra.ExactArgs(1), |
| 181 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 182 | + ctx := context.Background() |
| 183 | + toolID := args[0] |
| 184 | + |
| 185 | + // Require explicit confirmation for destructive actions |
| 186 | + var confirmDelete bool |
| 187 | + prompt := &survey.Confirm{ |
| 188 | + Message: fmt.Sprintf("Are you sure you want to delete tool %s? This will remove it from all assistants.", toolID), |
| 189 | + Default: false, |
| 190 | + } |
| 191 | + |
| 192 | + if err := survey.AskOne(prompt, &confirmDelete); err != nil { |
| 193 | + return fmt.Errorf("deletion canceled: %w", err) |
| 194 | + } |
| 195 | + |
| 196 | + if !confirmDelete { |
| 197 | + fmt.Println("Deletion canceled.") |
| 198 | + return nil |
| 199 | + } |
| 200 | + |
| 201 | + fmt.Printf("🗑️ Deleting tool with ID: %s\n", toolID) |
| 202 | + |
| 203 | + // Execute deletion via API |
| 204 | + _, err := vapiClient.GetClient().Tools.Delete(ctx, toolID) |
| 205 | + if err != nil { |
| 206 | + return fmt.Errorf("failed to delete tool: %w", err) |
| 207 | + } |
| 208 | + |
| 209 | + fmt.Println("✅ Tool deleted successfully") |
| 210 | + fmt.Println("Note: Assistants using this tool may need to be reconfigured") |
| 211 | + return nil |
| 212 | + }, |
| 213 | +} |
| 214 | + |
| 215 | +var testToolCmd = &cobra.Command{ |
| 216 | + Use: "test [tool-id]", |
| 217 | + Short: "Test a tool with sample input", |
| 218 | + Long: `Test a tool by calling it with sample input parameters to verify it works correctly. |
| 219 | + |
| 220 | +This helps debug tool configurations and API integrations before using them in live conversations.`, |
| 221 | + Args: cobra.ExactArgs(1), |
| 222 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 223 | + toolID := args[0] |
| 224 | + |
| 225 | + fmt.Printf("🧪 Testing tool: %s\n", toolID) |
| 226 | + fmt.Println() |
| 227 | + fmt.Println("Tool testing helps verify:") |
| 228 | + fmt.Println("- Function execution and responses") |
| 229 | + fmt.Println("- API connectivity and authentication") |
| 230 | + fmt.Println("- Parameter validation and types") |
| 231 | + fmt.Println("- Error handling and edge cases") |
| 232 | + fmt.Println("- Response data structure") |
| 233 | + fmt.Println() |
| 234 | + fmt.Println("Test tools via:") |
| 235 | + fmt.Println("- Vapi Dashboard tool testing interface") |
| 236 | + fmt.Println("- API: POST /tools/{id}/test") |
| 237 | + fmt.Println("- Assistant conversations with debug mode") |
| 238 | + fmt.Println() |
| 239 | + fmt.Printf("Direct link: https://dashboard.vapi.ai/tools/%s?tab=test\n", toolID) |
| 240 | + fmt.Println() |
| 241 | + fmt.Println("For automated testing, use the webhook 'function-call'") |
| 242 | + fmt.Println("events with 'vapi listen' to see real-time function calls.") |
| 243 | + |
| 244 | + return nil |
| 245 | + }, |
| 246 | +} |
| 247 | + |
| 248 | +var listToolTypesCmd = &cobra.Command{ |
| 249 | + Use: "types", |
| 250 | + Short: "List available tool types", |
| 251 | + Long: `Display all available tool types and their capabilities for creating new tools.`, |
| 252 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 253 | + fmt.Println("🛠️ Available tool types...") |
| 254 | + fmt.Println() |
| 255 | + |
| 256 | + fmt.Println("📞 Call Control Tools:") |
| 257 | + fmt.Println(" • End Call Tool - Terminate calls with custom messages") |
| 258 | + fmt.Println(" • Transfer Call Tool - Transfer to phone numbers or assistants") |
| 259 | + fmt.Println(" • DTMF Tool - Handle keypad input during calls") |
| 260 | + fmt.Println() |
| 261 | + |
| 262 | + fmt.Println("🔧 Custom Function Tools:") |
| 263 | + fmt.Println(" • HTTP Function Tool - Call external APIs and web services") |
| 264 | + fmt.Println(" • Database Query Tool - Execute database queries") |
| 265 | + fmt.Println(" • Custom Logic Tool - Run business logic functions") |
| 266 | + fmt.Println() |
| 267 | + |
| 268 | + fmt.Println("🔌 Integration Tools:") |
| 269 | + fmt.Println(" • Google Sheets Tool - Read/write spreadsheet data") |
| 270 | + fmt.Println(" • Calendar Tool - Schedule and manage appointments") |
| 271 | + fmt.Println(" • CRM Integration Tool - Access customer data") |
| 272 | + fmt.Println(" • Payment Processing Tool - Handle transactions") |
| 273 | + fmt.Println() |
| 274 | + |
| 275 | + fmt.Println("📊 Data Tools:") |
| 276 | + fmt.Println(" • Analytics Tool - Track conversation metrics") |
| 277 | + fmt.Println(" • Logging Tool - Custom event logging") |
| 278 | + fmt.Println(" • Validation Tool - Data validation and formatting") |
| 279 | + fmt.Println() |
| 280 | + |
| 281 | + fmt.Println("Create tools at: https://dashboard.vapi.ai/tools") |
| 282 | + fmt.Println("API Documentation: https://docs.vapi.ai/tools") |
| 283 | + |
| 284 | + return nil |
| 285 | + }, |
| 286 | +} |
| 287 | + |
| 288 | +func init() { |
| 289 | + rootCmd.AddCommand(toolCmd) |
| 290 | + toolCmd.AddCommand(listToolCmd) |
| 291 | + toolCmd.AddCommand(getToolCmd) |
| 292 | + toolCmd.AddCommand(createToolCmd) |
| 293 | + toolCmd.AddCommand(updateToolCmd) |
| 294 | + toolCmd.AddCommand(deleteToolCmd) |
| 295 | + toolCmd.AddCommand(testToolCmd) |
| 296 | + toolCmd.AddCommand(listToolTypesCmd) |
| 297 | +} |
0 commit comments