tools

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package tools implements the kubectl-qyai tool system.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertToolToGollm

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

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

func DetectKubectlStreaming

func DetectKubectlStreaming(command string) (bool, string)

DetectKubectlStreaming checks if a kubectl command is a streaming command

func ExecuteWithStreamingHandling

func ExecuteWithStreamingHandling(ctx context.Context, executor sandbox.Executor, command string, workDir string, env []string, detector StreamDetector) (*sandbox.ExecResult, error)

ExecuteWithStreamingHandling executes a command using the provided executor, handling streaming commands (watch, logs -f, attach) by applying a timeout and capturing partial output.

func ExpandShellVar

func ExpandShellVar(value string) (string, error)

ExpandShellVar expands shell variables and syntax using bash

func IsInteractiveCommand

func IsInteractiveCommand(command string) (bool, error)

func KubectlModifiesResource added in v1.0.5

func KubectlModifiesResource(command string) string

KubectlModifiesResource analyzes a kubectl command to determine if it modifies resources. It returns: - "yes" if the command modifies resources - "no" if the command is read-only - "unknown" if the command could not be safely classified

func LoadAndRegisterCustomTools

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 {
	// contains filtered or unexported fields
}

func NewBashTool

func NewBashTool(executor sandbox.Executor) *BashTool

func (*BashTool) CheckModifiesResource

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

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

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

type CustomTool

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

CustomTool implements the Tool interface for external commands.

func NewCustomTool

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

NewCustomTool creates a new CustomTool instance.

func (*CustomTool) CheckModifiesResource

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) CloneWithExecutor

func (t *CustomTool) CloneWithExecutor(executor sandbox.Executor) *CustomTool

CloneWithExecutor creates a copy of the CustomTool with the given executor. This is used to create a session-specific instance of the tool.

func (*CustomTool) Description

func (t *CustomTool) Description() string

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

func (*CustomTool) FunctionDefinition

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

FunctionDefinition returns the tool's function definition.

func (*CustomTool) IsInteractive

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

For CustomTool

func (*CustomTool) Name

func (t *CustomTool) Name() string

Name returns the tool's name.

func (*CustomTool) Run

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

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 InvokeToolOptions

type InvokeToolOptions struct {
	WorkDir string

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

	// Executor is the executor for tool execution
	Executor sandbox.Executor
}

type Kubectl

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

func NewKubectlTool

func NewKubectlTool(executor sandbox.Executor) *Kubectl

func (*Kubectl) CheckModifiesResource

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

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

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-qyai's tool system.

func NewMCPTool

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

NewMCPTool creates a new MCP tool wrapper.

func (*MCPTool) CheckModifiesResource

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

func (t *MCPTool) Description() string

Description returns the tool description.

func (*MCPTool) FunctionDefinition

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

FunctionDefinition returns the tool's function definition.

func (*MCPTool) IsInteractive

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

func (t *MCPTool) Name() string

Name returns the tool name.

func (*MCPTool) Run

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

func (t *MCPTool) ServerName() string

ServerName returns the MCP server name.

func (*MCPTool) UniqueToolName

func (t *MCPTool) UniqueToolName() string

type StreamDetector

type StreamDetector func(command string) (isStreaming bool, streamType string)

StreamDetector determines if a command is a streaming command and returns the stream type. It returns (true, streamType) if it is a streaming command, and (false, "") otherwise.

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

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

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) CloneWithExecutor

func (t *Tools) CloneWithExecutor(executor sandbox.Executor) Tools

CloneWithExecutor creates a shallow copy of the Tools collection, but clones any tools that need a session-specific executor (like CustomTool).

func (*Tools) Init

func (t *Tools) Init()

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)

Source Files

  • bash_tool.go
  • custom_tool.go
  • interfaces.go
  • kubectl_filter.go
  • kubectl_tool.go
  • mcp_tool.go
  • streaming.go
  • tools.go

Jump to

Keyboard shortcuts

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