Documentation
¶
Overview ¶
Package functiontool provides a convenient way to create tools from typed Go functions. This follows the ADK-Go pattern for FunctionTool, providing compile-time type safety and automatic schema generation from struct tags.
FunctionTool is syntactic sugar over the CallableTool interface - it generates a CallableTool implementation from a typed function, reducing boilerplate and improving type safety.
Basic Usage ¶
type GetWeatherArgs struct {
City string `json:"city" jsonschema:"required,description=City name"`
Units string `json:"units,omitempty" jsonschema:"description=Temperature units,default=celsius,enum=celsius|fahrenheit"`
}
weatherTool, err := functiontool.New(
functiontool.Config{
Name: "get_weather",
Description: "Get current weather for a city",
},
func(ctx tool.Context, args GetWeatherArgs) (map[string]any, error) {
// Implementation
return map[string]any{"temp": 22, "condition": "sunny"}, nil
},
)
When to Use FunctionTool ¶
Use FunctionTool for simple, stateless tools with:
- ≤ 5 parameters
- No internal state
- No streaming output
- Static schema
- Straightforward error handling
For complex tools (streaming, dynamic schema, stateful), implement CallableTool directly.
ADK-Go Alignment ¶
This implementation follows ADK-Go patterns:
- Go (explicit): functiontool.New(cfg, func)
- Python (implicit): tools=[func] (auto-wrapped)
- Java (explicit): FunctionTool.create(class, method)
Go uses struct tags for schema generation, similar to Java's @Schema annotation.
Example (Basic) ¶
Example_basic demonstrates basic function tool usage
package main
import (
"fmt"
"github.com/verikod/hector/pkg/tool"
"github.com/verikod/hector/pkg/tool/functiontool"
)
func main() {
type GetWeatherArgs struct {
City string `json:"city" jsonschema:"required,description=City name"`
Units string `json:"units,omitempty" jsonschema:"description=Temperature units,default=celsius,enum=celsius|fahrenheit"`
}
weatherTool, err := functiontool.New(
functiontool.Config{
Name: "get_weather",
Description: "Get current weather for a city",
},
func(ctx tool.Context, args GetWeatherArgs) (map[string]any, error) {
// Simulate weather API call
return map[string]any{
"city": args.City,
"temp": 22,
"condition": "sunny",
"units": args.Units,
}, nil
},
)
if err != nil {
panic(err)
}
fmt.Printf("Tool Name: %s\n", weatherTool.Name())
fmt.Printf("Is Long Running: %v\n", weatherTool.IsLongRunning())
}
Output: Tool Name: get_weather Is Long Running: false
Example (ComplexTypes) ¶
Example_complexTypes demonstrates complex parameter types
package main
import (
"fmt"
"github.com/verikod/hector/pkg/tool"
"github.com/verikod/hector/pkg/tool/functiontool"
)
func main() {
type SearchArgs struct {
Query string `json:"query" jsonschema:"required,description=Search query"`
Languages []string `json:"languages,omitempty" jsonschema:"description=Language filters"`
MaxCount int `json:"max_count,omitempty" jsonschema:"description=Max results,default=10,minimum=1,maximum=100"`
Type string `json:"type,omitempty" jsonschema:"description=Search type,enum=semantic|keyword"`
}
searchTool, err := functiontool.New(
functiontool.Config{
Name: "search",
Description: "Search documents with filters",
},
func(ctx tool.Context, args SearchArgs) (map[string]any, error) {
return map[string]any{
"query": args.Query,
"results": []string{"doc1", "doc2"},
"count": 2,
}, nil
},
)
if err != nil {
panic(err)
}
schema := searchTool.Schema()
fmt.Printf("Schema type: %s\n", schema["type"])
}
Output: Schema type: object
Example (WithValidation) ¶
Example_withValidation demonstrates custom validation
package main
import (
"fmt"
"github.com/verikod/hector/pkg/tool"
"github.com/verikod/hector/pkg/tool/functiontool"
)
func main() {
type CreateFileArgs struct {
Path string `json:"path" jsonschema:"required,description=File path"`
Content string `json:"content" jsonschema:"required,description=File content"`
}
createFileTool, err := functiontool.NewWithValidation(
functiontool.Config{
Name: "create_file",
Description: "Create a new file",
},
func(ctx tool.Context, args CreateFileArgs) (map[string]any, error) {
// Simulate file creation
return map[string]any{
"path": args.Path,
"bytes": len(args.Content),
}, nil
},
func(args CreateFileArgs) error {
// Custom validation
if len(args.Content) > 1000000 {
return fmt.Errorf("content too large: %d bytes", len(args.Content))
}
return nil
},
)
if err != nil {
panic(err)
}
fmt.Printf("Tool: %s\n", createFileTool.Name())
}
Output: Tool: create_file
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
func New[Args any](cfg Config, fn func(tool.Context, Args) (map[string]any, error)) (tool.CallableTool, error)
New creates a CallableTool from a typed function. This is the primary way to create function tools in Hector v2.
The function signature must be:
func(tool.Context, Args) (map[string]any, error)
Where Args is a struct with json and jsonschema tags defining the parameters.
Example:
type SearchArgs struct {
Query string `json:"query" jsonschema:"required,description=Search query"`
Limit int `json:"limit,omitempty" jsonschema:"description=Max results,default=10"`
}
searchTool, err := functiontool.New(
functiontool.Config{Name: "search", Description: "Search documents"},
func(ctx tool.Context, args SearchArgs) (map[string]any, error) {
// Implementation
},
)
func NewWithValidation ¶
func NewWithValidation[Args any]( cfg Config, fn func(tool.Context, Args) (map[string]any, error), validate func(Args) error, ) (tool.CallableTool, error)
NewWithValidation creates a CallableTool with custom argument validation. The validation function is called before the main function, allowing you to implement complex validation logic beyond what struct tags can express.
Example:
functiontool.NewWithValidation(
cfg,
myFunction,
func(args MyArgs) error {
if strings.Contains(args.Path, "..") {
return fmt.Errorf("path traversal not allowed")
}
return nil
},
)
Types ¶
type Config ¶
type Config struct {
// Name is the unique identifier for this tool (required).
Name string
// Description explains what the tool does (required).
// This is shown to the LLM to help it decide when to use the tool.
Description string
}
Config defines the configuration for a function tool.