|
| 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 tools |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + "os/exec" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/GoogleCloudPlatform/kubectl-ai/gollm" |
| 25 | +) |
| 26 | + |
| 27 | +// CustomToolConfig defines the structure for configuring a custom tool. |
| 28 | +type CustomToolConfig struct { |
| 29 | + Name string `yaml:"name"` |
| 30 | + Description string `yaml:"description"` |
| 31 | + Command string `yaml:"command"` |
| 32 | + CommandDesc string `yaml:"command_desc"` |
| 33 | +} |
| 34 | + |
| 35 | +// CustomTool implements the Tool interface for external commands. |
| 36 | +type CustomTool struct { |
| 37 | + config CustomToolConfig |
| 38 | +} |
| 39 | + |
| 40 | +// NewCustomTool creates a new CustomTool instance. |
| 41 | +func NewCustomTool(config CustomToolConfig) (*CustomTool, error) { |
| 42 | + if config.Name == "" { |
| 43 | + return nil, fmt.Errorf("custom tool name cannot be empty") |
| 44 | + } |
| 45 | + if len(config.Command) == 0 { |
| 46 | + return nil, fmt.Errorf("custom tool command cannot be empty for tool %q", config.Name) |
| 47 | + } |
| 48 | + |
| 49 | + return &CustomTool{config: config}, nil |
| 50 | +} |
| 51 | + |
| 52 | +// Name returns the tool's name. |
| 53 | +func (t *CustomTool) Name() string { |
| 54 | + return t.config.Name |
| 55 | +} |
| 56 | + |
| 57 | +// Description returns the tool's description from its function definition. |
| 58 | +func (t *CustomTool) Description() string { |
| 59 | + return t.config.Description |
| 60 | +} |
| 61 | + |
| 62 | +// FunctionDefinition returns the tool's function definition. |
| 63 | +func (t *CustomTool) FunctionDefinition() *gollm.FunctionDefinition { |
| 64 | + return &gollm.FunctionDefinition{ |
| 65 | + Name: t.Name(), |
| 66 | + Description: t.Description(), |
| 67 | + Parameters: &gollm.Schema{ |
| 68 | + Type: gollm.TypeObject, |
| 69 | + Properties: map[string]*gollm.Schema{ |
| 70 | + "command": { |
| 71 | + Type: gollm.TypeString, |
| 72 | + Description: t.config.CommandDesc, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// Run executes the external command defined for the custom tool. |
| 80 | +func (t *CustomTool) Run(ctx context.Context, args map[string]any) (any, error) { |
| 81 | + command := strings.Fields(t.config.Command) |
| 82 | + if len(command) == 0 { |
| 83 | + return nil, fmt.Errorf("empty command") |
| 84 | + } |
| 85 | + cmdArgs := []string{} |
| 86 | + if len(command) > 1 { |
| 87 | + cmdArgs = command[1:] |
| 88 | + } |
| 89 | + workDir := ctx.Value(WorkDirKey).(string) |
| 90 | + |
| 91 | + cmd := exec.CommandContext(ctx, command[0], cmdArgs...) |
| 92 | + cmd.Dir = workDir |
| 93 | + cmd.Env = os.Environ() |
| 94 | + |
| 95 | + return executeCommand(cmd) |
| 96 | +} |
0 commit comments