tools

package
v0.0.20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2025 License: Apache-2.0 Imports: 21 Imported by: 2

Documentation

Overview

Package tools implements the kubectl-ai tool system.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertToolToGollm added in v0.0.12

func ConvertToolToGollm(mcpTool *mcp.Tool) (*gollm.FunctionDefinition, error)

ConvertToolToGollm converts an MCP tool to gollm.FunctionDefinition with a simple schema

func IsInteractiveCommand added in v0.0.11

func IsInteractiveCommand(command string) (bool, error)

func LoadAndRegisterCustomTools added in v0.0.9

func LoadAndRegisterCustomTools(configPath string) error

LoadAndRegisterCustomTools loads tool configurations from a YAML file and registers them.

func RegisterTool

func RegisterTool(tool Tool)

RegisterTool makes a tool available to the LLM.

func ToolResultToMap

func ToolResultToMap(result any) (map[string]any, error)

ToolResultToMap converts an arbitrary result to a map[string]any

Types

type BashTool

type BashTool struct{}

func (*BashTool) CheckModifiesResource added in v0.0.13

func (t *BashTool) CheckModifiesResource(args map[string]any) string

CheckModifiesResource determines if the command modifies kubernetes resources This is used for permission checks before command execution Returns "yes", "no", or "unknown"

func (*BashTool) Description

func (t *BashTool) Description() string

func (*BashTool) FunctionDefinition

func (t *BashTool) FunctionDefinition() *gollm.FunctionDefinition

func (*BashTool) IsInteractive added in v0.0.11

func (t *BashTool) IsInteractive(args map[string]any) (bool, error)

func (*BashTool) Name

func (t *BashTool) Name() string

func (*BashTool) Run

func (t *BashTool) Run(ctx context.Context, args map[string]any) (any, error)

type ContextKey added in v0.0.9

type ContextKey string
const (
	KubeconfigKey ContextKey = "kubeconfig"
	WorkDirKey    ContextKey = "work_dir"
)

type CustomTool added in v0.0.9

type CustomTool struct {
	// contains filtered or unexported fields
}

CustomTool implements the Tool interface for external commands.

func NewCustomTool added in v0.0.9

func NewCustomTool(config CustomToolConfig) (*CustomTool, error)

NewCustomTool creates a new CustomTool instance.

func (*CustomTool) CheckModifiesResource added in v0.0.13

func (t *CustomTool) CheckModifiesResource(args map[string]any) string

CheckModifiesResource determines if the command modifies resources For custom tools, we'll conservatively assume they might modify resources unless we have specific knowledge otherwise Returns "yes", "no", or "unknown"

func (*CustomTool) Description added in v0.0.9

func (t *CustomTool) Description() string

Description returns the tool's description from its function definition.

func (*CustomTool) FunctionDefinition added in v0.0.9

func (t *CustomTool) FunctionDefinition() *gollm.FunctionDefinition

FunctionDefinition returns the tool's function definition.

func (*CustomTool) IsInteractive added in v0.0.11

func (t *CustomTool) IsInteractive(args map[string]any) (bool, error)

For CustomTool

func (*CustomTool) Name added in v0.0.9

func (t *CustomTool) Name() string

Name returns the tool's name.

func (*CustomTool) Run added in v0.0.9

func (t *CustomTool) Run(ctx context.Context, args map[string]any) (any, error)

Run executes the external command defined for the custom tool.

type CustomToolConfig added in v0.0.9

type CustomToolConfig struct {
	Name          string `yaml:"name"`
	Description   string `yaml:"description"`
	Command       string `yaml:"command"`
	CommandDesc   string `yaml:"command_desc"`
	IsInteractive bool   `yaml:"is_interactive"`
}

CustomToolConfig defines the structure for configuring a custom tool.

type ExecResult

type ExecResult struct {
	Command    string `json:"command,omitempty"`
	Error      string `json:"error,omitempty"`
	Stdout     string `json:"stdout,omitempty"`
	Stderr     string `json:"stderr,omitempty"`
	ExitCode   int    `json:"exit_code,omitempty"`
	StreamType string `json:"stream_type,omitempty"`
}

func (*ExecResult) String added in v0.0.12

func (e *ExecResult) String() string

type InvokeToolOptions

type InvokeToolOptions struct {
	WorkDir string

	// Kubeconfig is the path to the kubeconfig file.
	Kubeconfig string
}

type Kubectl

type Kubectl struct{}

func (*Kubectl) CheckModifiesResource added in v0.0.13

func (t *Kubectl) CheckModifiesResource(args map[string]any) string

CheckModifiesResource determines if the command modifies kubernetes resources This is used for permission checks before command execution Returns "yes", "no", or "unknown"

func (*Kubectl) Description

func (t *Kubectl) Description() string

func (*Kubectl) FunctionDefinition

func (t *Kubectl) FunctionDefinition() *gollm.FunctionDefinition

func (*Kubectl) IsInteractive added in v0.0.11

func (t *Kubectl) IsInteractive(args map[string]any) (bool, error)

func (*Kubectl) Name

func (t *Kubectl) Name() string

func (*Kubectl) Run

func (t *Kubectl) Run(ctx context.Context, args map[string]any) (any, error)

type MCPTool added in v0.0.12

type MCPTool struct {
	// contains filtered or unexported fields
}

MCPTool wraps an MCP server tool to implement the Tool interface. It serves as an adapter between MCP-based tools and kubectl-ai's tool system.

func NewMCPTool added in v0.0.12

func NewMCPTool(serverName, toolName, description string, schema *gollm.FunctionDefinition, manager *mcp.Manager) *MCPTool

NewMCPTool creates a new MCP tool wrapper.

func (*MCPTool) CheckModifiesResource added in v0.0.13

func (t *MCPTool) CheckModifiesResource(args map[string]any) string

CheckModifiesResource determines if the command modifies kubernetes resources For MCP tools, we'll conservatively assume they might modify resources since we can't easily determine this for arbitrary external tools Returns "yes", "no", or "unknown"

func (*MCPTool) Description added in v0.0.12

func (t *MCPTool) Description() string

Description returns the tool description.

func (*MCPTool) FunctionDefinition added in v0.0.12

func (t *MCPTool) FunctionDefinition() *gollm.FunctionDefinition

FunctionDefinition returns the tool's function definition.

func (*MCPTool) IsInteractive added in v0.0.12

func (t *MCPTool) IsInteractive(args map[string]any) (bool, error)

TODO(tuannvm): This is a placeholder implementation. Need to implement detection of interactive MCP tools. IsInteractive checks if the tool requires interactive input.

func (*MCPTool) Name added in v0.0.12

func (t *MCPTool) Name() string

Name returns the tool name.

func (*MCPTool) Run added in v0.0.12

func (t *MCPTool) Run(ctx context.Context, args map[string]any) (any, error)

Run executes the MCP tool by calling the appropriate MCP server.

func (*MCPTool) ServerName added in v0.0.12

func (t *MCPTool) ServerName() string

ServerName returns the MCP server name.

type Tool

type Tool interface {
	// Name is the identifier for the tool; we pass this to the LLM.
	// The LLM uses this name when it wants to invoke the tool.
	// It should be meaningful and (we think) camel_case as (we think) that works better with most LLMs.
	Name() string

	// Description is an additional description that gives the LLM instructions on when to use the tool.
	Description() string

	// FunctionDefinition provides the full schema for the parameters to be used when invoking the tool.
	// The Description fields provides hints that the LLM may use to use the tool more effectively/correctly.
	FunctionDefinition() *gollm.FunctionDefinition

	// Run invokes the tool, the agent calls this when the LLM requests tool invocation.
	Run(ctx context.Context, args map[string]any) (any, error)

	// IsInteractive checks if a command is interactive
	// If the command is interactive, we need to handle it differently in the agent
	// Returns true if interactive, with an error explaining why it's interactive
	IsInteractive(args map[string]any) (bool, error)

	// CheckModifiesResource determines if the command modifies resources
	// This is used for permission checks before command execution
	// Returns "yes", "no", or "unknown"
	CheckModifiesResource(args map[string]any) string
}

func Lookup

func Lookup(name string) Tool

type ToolCall

type ToolCall struct {
	// contains filtered or unexported fields
}

func (*ToolCall) Description added in v0.0.15

func (t *ToolCall) Description() string

Description returns a description of the tool call. This is used to display the tool call in the UI. It should be human-readable, and should be concise enough that the user can read it quickly, but precise enough that the user can decide whether to invoke the tool.

func (*ToolCall) GetTool added in v0.0.11

func (t *ToolCall) GetTool() Tool

Add a method to access the tool

func (*ToolCall) InvokeTool

func (t *ToolCall) InvokeTool(ctx context.Context, opt InvokeToolOptions) (any, error)

InvokeTool handles the execution of a single action

type ToolRequestEvent

type ToolRequestEvent struct {
	CallID    string         `json:"id,omitempty"`
	Name      string         `json:"name,omitempty"`
	Arguments map[string]any `json:"arguments,omitempty"`
}

type ToolResponseEvent

type ToolResponseEvent struct {
	CallID   string `json:"id,omitempty"`
	Response any    `json:"response,omitempty"`
	Error    string `json:"error,omitempty"`
}

type Tools

type Tools struct {
	// contains filtered or unexported fields
}

func Default

func Default() Tools

func (*Tools) AllTools

func (t *Tools) AllTools() []Tool

func (*Tools) Lookup

func (t *Tools) Lookup(name string) Tool

func (*Tools) Names

func (t *Tools) Names() []string

func (*Tools) ParseToolInvocation

func (t *Tools) ParseToolInvocation(ctx context.Context, name string, arguments map[string]any) (*ToolCall, error)

ParseToolInvocation parses a request from the LLM into a tool call.

func (*Tools) RegisterTool

func (t *Tools) RegisterTool(tool Tool)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL