tools

package
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToOpenAITools

func ToOpenAITools(tools []*Tool) []openai.ChatCompletionToolUnionParam

ToOpenAITools converts a slice of Tool pointers to a slice of OpenAI ChatCompletionToolUnionParam

Types

type Agent

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

Agent represents a simplified tools agent that hides OpenAI SDK details

func NewAgent

func NewAgent(
	ctx context.Context,
	agentConfig agents.Config,
	modelConfig models.Config,
	options ...any,
) (*Agent, error)

NewAgent creates a new simplified tools agent

func (*Agent) AddMessage

func (agent *Agent) AddMessage(role roles.Role, content string)

AddMessage adds a message to the conversation history

func (*Agent) AddMessages added in v1.0.4

func (agent *Agent) AddMessages(msgs []messages.Message)

AddMessages adds multiple messages to the conversation history

func (*Agent) DetectParallelToolCalls added in v0.0.4

func (agent *Agent) DetectParallelToolCalls(
	userMessages []messages.Message,
	toolCallback ...ToolCallback,
) (*ToolCallResult, error)

NOTE: IMPORTANT: Not all LLMs with tool support support parallel tool calls. DetectParallelToolCalls detects and executes multiple tool calls in parallel The toolCallback parameter is optional. If not provided, uses the callback set via WithExecuteFn option.

func (*Agent) DetectParallelToolCallsWithConfirmation added in v0.0.4

func (agent *Agent) DetectParallelToolCallsWithConfirmation(
	userMessages []messages.Message,
	callbacks ...any,
) (*ToolCallResult, error)

NOTE: IMPORTANT: Not all LLMs with tool support support parallel tool calls. DetectParallelToolCallsWithConfirmation detects and executes multiple tool calls with user confirmation Optional callbacks can be provided in order: toolCallback, confirmationCallback Usage:

  • DetectParallelToolCallsWithConfirmation(messages) → uses both from options
  • DetectParallelToolCallsWithConfirmation(messages, toolCallback) → toolCallback from param, confirmationCallback from option
  • DetectParallelToolCallsWithConfirmation(messages, toolCallback, confirmationCallback) → both from params

func (*Agent) DetectToolCallsLoop added in v0.0.4

func (agent *Agent) DetectToolCallsLoop(
	userMessages []messages.Message,
	toolCallback ...ToolCallback,
) (*ToolCallResult, error)

DetectToolCallsLoop sends messages and detects tool calls, executing them via callback The toolCallback parameter is optional. If not provided, uses the callback set via WithExecuteFn option. If neither is provided, returns an error.

func (*Agent) DetectToolCallsLoopStream added in v0.0.4

func (agent *Agent) DetectToolCallsLoopStream(
	userMessages []messages.Message,
	streamCallback StreamCallback,
	toolCallback ...ToolCallback,
) (*ToolCallResult, error)

DetectToolCallsLoopStream sends messages and detects tool calls with streaming The toolCallback parameter is optional. If not provided, uses the callback set via WithExecuteFn option. The streamCallback parameter is required (it's the purpose of this method).

func (*Agent) DetectToolCallsLoopWithConfirmation added in v0.0.4

func (agent *Agent) DetectToolCallsLoopWithConfirmation(
	userMessages []messages.Message,
	callbacks ...any,
) (*ToolCallResult, error)

DetectToolCallsLoopWithConfirmation sends messages and detects tool calls with user confirmation Optional callbacks can be provided in order: toolCallback, confirmationCallback If not provided, uses callbacks set via WithExecuteFn and WithConfirmationPromptFn options. Usage:

  • DetectToolCallsLoopWithConfirmation(messages) → uses both from options
  • DetectToolCallsLoopWithConfirmation(messages, toolCallback) → toolCallback from param, confirmationCallback from option
  • DetectToolCallsLoopWithConfirmation(messages, toolCallback, confirmationCallback) → both from params

func (*Agent) DetectToolCallsLoopWithConfirmationStream added in v0.0.4

func (agent *Agent) DetectToolCallsLoopWithConfirmationStream(
	userMessages []messages.Message,
	streamCallback StreamCallback,
	callbacks ...any,
) (*ToolCallResult, error)

DetectToolCallsLoopWithConfirmationStream sends messages and detects tool calls with confirmation and streaming The streamCallback parameter is required. Optional callbacks can be provided in order: toolCallback, confirmationCallback Usage:

  • DetectToolCallsLoopWithConfirmationStream(messages, streamCallback) → uses both callbacks from options
  • DetectToolCallsLoopWithConfirmationStream(messages, streamCallback, toolCallback) → toolCallback from param, confirmationCallback from option
  • DetectToolCallsLoopWithConfirmationStream(messages, streamCallback, toolCallback, confirmationCallback) → both from params

func (*Agent) ExportMessagesToJSON added in v1.1.2

func (agent *Agent) ExportMessagesToJSON() (string, error)

func (*Agent) GetConfig added in v1.0.8

func (agent *Agent) GetConfig() agents.Config

GetConfig returns the agent configuration

func (*Agent) GetContext added in v1.2.2

func (agent *Agent) GetContext() context.Context

GetContext returns the agent's context

func (*Agent) GetContextSize

func (agent *Agent) GetContextSize() int

GetContextSize returns the approximate size of the current context

func (*Agent) GetLastRequestRawJSON added in v1.1.2

func (agent *Agent) GetLastRequestRawJSON() string

func (*Agent) GetLastRequestSON added in v1.1.2

func (agent *Agent) GetLastRequestSON() (string, error)

func (*Agent) GetLastResponseJSON added in v1.0.2

func (agent *Agent) GetLastResponseJSON() (string, error)

func (*Agent) GetLastResponseRawJSON added in v1.1.2

func (agent *Agent) GetLastResponseRawJSON() string

func (*Agent) GetLastStateToolCalls added in v1.1.8

func (agent *Agent) GetLastStateToolCalls() LastToolCallsState

GetLastStatedToolCalls returns the last stated tool calls state

  • IMPORTANT: Allows checking the state of tool calls across multiple invocations
  • USEFUL: for maintaining continuity in tool call confirmations and executions

and when transfering context between different agent instances

func (*Agent) GetMessages

func (agent *Agent) GetMessages() []messages.Message

GetMessages returns all conversation messages

func (*Agent) GetModelConfig added in v1.0.8

func (agent *Agent) GetModelConfig() models.Config

GetModelConfig returns the model configuration

func (*Agent) GetModelID added in v0.0.7

func (agent *Agent) GetModelID() string

func (*Agent) GetName added in v0.0.3

func (agent *Agent) GetName() string

func (*Agent) GetTools added in v1.3.2

func (agent *Agent) GetTools() []openai.ChatCompletionToolUnionParam

GetTools returns the tools configured for this agent

func (*Agent) Kind

func (agent *Agent) Kind() agents.Kind

Kind returns the agent type

func (*Agent) ResetLastStateToolCalls added in v1.1.8

func (agent *Agent) ResetLastStateToolCalls()

func (*Agent) ResetMessages

func (agent *Agent) ResetMessages()

ResetMessages clears all messages except the system instruction

func (*Agent) SetConfig added in v1.0.8

func (agent *Agent) SetConfig(config agents.Config)

SetConfig updates the agent configuration

func (*Agent) SetContext added in v1.2.2

func (agent *Agent) SetContext(ctx context.Context)

SetContext updates the agent's context

func (*Agent) SetModelConfig added in v1.0.8

func (agent *Agent) SetModelConfig(config models.Config)

SetModelConfig updates the model configuration Note: This updates the stored config but doesn't regenerate the internal OpenAI params For most parameters to take effect, create a new agent with the new config

type AgentOption

type AgentOption func(*BaseAgent)

type BaseAgent

type BaseAgent struct {
	*base.Agent
	// contains filtered or unexported fields
}

BaseAgent wraps the shared base.Agent for tools-specific functionality

func NewBaseAgent

func NewBaseAgent(
	ctx context.Context,
	agentConfig agents.Config,
	modelConfig openai.ChatCompletionNewParams,
	options ...AgentOption,
) (toolsAgent *BaseAgent, err error)

NewBaseAgent creates a simplified Tools agent using the shared base agent

func (*BaseAgent) DetectParallelToolCalls added in v0.0.4

func (agent *BaseAgent) DetectParallelToolCalls(messages []openai.ChatCompletionMessageParamUnion, toolCallBack func(functionName string, arguments string) (string, error)) (string, []string, string, error)

NOTE: IMPORTANT: Not all LLMs with tool support support parallel tool calls.

func (*BaseAgent) DetectParallelToolCallsWitConfirmation added in v0.0.4

func (agent *BaseAgent) DetectParallelToolCallsWitConfirmation(
	messages []openai.ChatCompletionMessageParamUnion,
	toolCallBack func(functionName string, arguments string) (string, error),
	confirmationCallBack func(functionName string, arguments string) ConfirmationResponse) (string, []string, string, error)

TODO: -> Tools.Agent

func (*BaseAgent) DetectToolCallsLoop added in v0.0.4

func (agent *BaseAgent) DetectToolCallsLoop(messages []openai.ChatCompletionMessageParamUnion, toolCallBack func(functionName string, arguments string) (string, error)) (string, []string, string, error)

func (*BaseAgent) DetectToolCallsLoopStream added in v0.0.4

func (agent *BaseAgent) DetectToolCallsLoopStream(messages []openai.ChatCompletionMessageParamUnion, toolCallback func(functionName string, arguments string) (string, error), streamCallback func(content string) error) (string, []string, string, error)

func (*BaseAgent) DetectToolCallsLoopWithConfirmation added in v0.0.4

func (agent *BaseAgent) DetectToolCallsLoopWithConfirmation(
	messages []openai.ChatCompletionMessageParamUnion,
	toolCallBack func(functionName string, arguments string) (string, error),
	confirmationCallBack func(functionName string, arguments string) ConfirmationResponse) (string, []string, string, error)

func (*BaseAgent) DetectToolCallsLoopWithConfirmationStream added in v0.0.4

func (agent *BaseAgent) DetectToolCallsLoopWithConfirmationStream(
	messages []openai.ChatCompletionMessageParamUnion,
	toolCallback func(functionName string, arguments string) (string, error),
	confirmationCallBack func(functionName string, arguments string) ConfirmationResponse,
	streamCallback func(content string) error) (string, []string, string, error)

func (*BaseAgent) Kind

func (agent *BaseAgent) Kind() (kind agents.Kind)

type ConfirmationCallback

type ConfirmationCallback func(functionName string, arguments string) ConfirmationResponse

type ConfirmationResponse

type ConfirmationResponse int
const (
	Confirmed ConfirmationResponse = iota
	Denied
	Quit
)

type LastToolCallsState added in v1.1.8

type LastToolCallsState struct {
	// If a tool call is awaiting user confirmation
	// Possible values: `Confirmed`, `Denied`, `Quit`
	// Denied: do not execute the tool call, but continue the flow
	// Quit: stop the entire agent execution (exit loop)
	Confirmation    ConfirmationResponse
	ExecutionResult ToolExecutionResult
}

WIP: GOAL: be able to check state of tool calls across multiple invocations

type Parameter

type Parameter struct {
	Type        string `json:"type"`
	Description string `json:"description"`
}

Parameter represents a function parameter

type StreamCallback

type StreamCallback func(chunk string) error

StreamCallback is a function called for each chunk of streaming response

type Tool

type Tool struct {
	Name        string
	Description string
	Parameters  map[string]Parameter
	Required    []string
}

Tool represents a function tool with a fluent builder API

func NewTool

func NewTool(name string) *Tool

NewTool creates a new Tool with the given name

func (*Tool) AddParameter

func (t *Tool) AddParameter(name, paramType, description string, isRequired bool) *Tool

AddParameter adds a parameter to the tool paramType should be one of: "string", "number", "boolean", "object", "array" isRequired indicates whether the parameter is required

func (*Tool) GetDescription

func (t *Tool) GetDescription() string

GetDescription returns the description of the tool

func (*Tool) GetName

func (t *Tool) GetName() string

GetName returns the name of the tool

func (*Tool) GetParameters

func (t *Tool) GetParameters() map[string]Parameter

GetParameters returns the parameters of the tool

func (*Tool) GetRequired

func (t *Tool) GetRequired() []string

GetRequired returns the list of required parameter names

func (*Tool) SetDescription

func (t *Tool) SetDescription(description string) *Tool

SetDescription sets the description of the tool

func (*Tool) ToOpenAI

ToOpenAI converts the Tool to an OpenAI ChatCompletionToolUnionParam

type ToolAgentOption added in v0.0.2

type ToolAgentOption func(*openai.ChatCompletionNewParams)

ToolAgentOption is a functional option for configuring an Agent during creation

func WithMCPTools added in v0.0.2

func WithMCPTools(tools []mcp.Tool) ToolAgentOption

func WithOpenAITools added in v0.0.2

func WithOpenAITools(tools []openai.ChatCompletionToolUnionParam) ToolAgentOption

WithTools sets custom tools for the agent

func WithTools added in v0.0.2

func WithTools(tools []*Tool) ToolAgentOption

type ToolCallResult

type ToolCallResult struct {
	FinishReason         string
	Results              []string
	LastAssistantMessage string
}

ToolCallResult represents the result of tool call detection

type ToolCallback

type ToolCallback func(functionName string, arguments string) (string, error)

ToolCallback is a function called when a tool needs to be executed

type ToolExecutionResult added in v1.1.8

type ToolExecutionResult struct {
	Content    string
	ShouldStop bool
	// Possible values: "function_executed", "user_denied", "user_quit", "error", "exit_loop"
	ExecFinishReason string
}

ToolExecutionResult holds the result of a tool execution

type ToolsAgentOption added in v1.2.9

type ToolsAgentOption func(*Agent)

ToolsAgentOption is a functional option for configuring lifecycle hooks on the Agent

func AfterCompletion added in v1.2.9

func AfterCompletion(fn func(*Agent)) ToolsAgentOption

AfterCompletion sets a hook that is called after each tool call detection

func BeforeCompletion added in v1.2.9

func BeforeCompletion(fn func(*Agent)) ToolsAgentOption

BeforeCompletion sets a hook that is called before each tool call detection

func WithConfirmationPromptFn added in v1.3.2

func WithConfirmationPromptFn(fn ConfirmationCallback) ToolsAgentOption

WithConfirmationPromptFn sets the default confirmation callback for the agent This callback will be used by all confirmation methods if no callback is explicitly provided

func WithExecuteFn added in v1.3.2

func WithExecuteFn(fn ToolCallback) ToolsAgentOption

WithExecuteFn sets the default tool execution callback for the agent This callback will be used by all detection methods if no callback is explicitly provided

Jump to

Keyboard shortcuts

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