function.go

Overview

The function.go file, part of the functiontool package, implements a generic Function Tool that wraps arbitrary Go functions as callable tools within an AI agent framework. It provides a mechanism to expose Go functions with typed inputs and outputs as tools that can be invoked by large language models (LLMs) or agents. The file emphasizes automatic JSON schema inference for function arguments and results, seamless serialization and deserialization, and integration with the tool lifecycle and LLM function calling mechanisms.

This file enables developers to easily create tools by wrapping Go functions without manual schema definition, facilitating robust, type-safe, and schema-validated function calls from agents or LLMs.


Key Components

Config struct

type Config struct {
    Name          string
    Description   string
    InputSchema   *jsonschema.Schema
    OutputSchema  *jsonschema.Schema
    IsLongRunning bool
}

Func Type

type Func[TArgs, TResults any] func(tool.Context, TArgs) (TResults, error)

New Function

func New[TArgs, TResults any](cfg Config, handler Func[TArgs, TResults]) (tool.Tool, error)

functionTool Struct

type functionTool[TArgs, TResults any] struct {
    cfg          Config
    inputSchema  *jsonschema.Resolved
    outputSchema *jsonschema.Resolved
    handler      Func[TArgs, TResults]
}

Methods Implementing tool.Tool Interface


Run Method

func (f *functionTool[TArgs, TResults]) Run(ctx tool.Context, args any) (map[string]any, error)

resolvedSchema Helper Function

func resolvedSchema[T any](override *jsonschema.Schema) (*jsonschema.Resolved, error)

Important Implementation Details


Interaction with Other System Components


Usage Example

type AddArgs struct {
    X int `json:"x"`
    Y int `json:"y"`
}

type AddResult struct {
    Sum int `json:"sum"`
}

addHandler := func(ctx tool.Context, args AddArgs) (AddResult, error) {
    return AddResult{Sum: args.X + args.Y}, nil
}

addTool, err := functiontool.New(functiontool.Config{
    Name:        "add",
    Description: "Adds two integers",
}, addHandler)

if err != nil {
    // handle error
}

This example creates a tool named "add" that sums two integers. The input and output JSON schemas are automatically generated from the AddArgs and AddResult types.


Visual Diagram

classDiagram
class functionTool {
+Name()
+Description()
+IsLongRunning()
+ProcessRequest()
+Declaration()
+Run()
}
functionTool --> Config
functionTool --> Func
functionTool --> jsonschema.Resolved
Config : +Name
Config : +Description
Config : +InputSchema
Config : +OutputSchema
Config : +IsLongRunning
Func : +func(tool.Context, TArgs) (TResults, error)

Workflow Summary

sequenceDiagram
participant Agent
participant LLM
participant FunctionTool
participant GoFunction
Agent->>LLM: Embed FunctionDeclaration with schemas
LLM->>Agent: Generate content (may include FunctionCall)
Agent->>FunctionTool: Run(args as map)
FunctionTool->>GoFunction: Deserialize args and execute
GoFunction-->>FunctionTool: Return result or error
FunctionTool-->>Agent: Serialize result map
Agent->>LLM: Provide function call result

This sequence shows how the function tool integrates with the agent and LLM:


References to Related Topics


This documentation covers the purpose, structure, and usage of the function.go file that implements the generic Function Tool within the functiontool package, providing an extensible and type-safe bridge between Go functions and AI agent tooling.