toolautocall

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 18 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package toolautocall provides the middleware that automatically invokes the function tools a model calls and feeds their results back for the next turn. Supporting providers add it by default and expose Config for customization.

Tool tracing is backend-neutral. Set Config.Tracer to supply a Tracer, or use WithTracer to supply one through the run context, including from agent middleware. An explicit Config.Tracer takes precedence over the context.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(cfg Config) agent.Middleware

New creates automatic tool-invocation middleware configured by cfg.

func WithTracer added in v0.3.0

func WithTracer(ctx context.Context, tracer Tracer) context.Context

WithTracer returns a context that supplies tracer to automatic tool invocations. A nil tracer disables context-provided tracing. Config.Tracer takes precedence over the context-provided tracer.

Types

type Config

type Config struct {
	// Logger receives diagnostic events about function invocation.
	// If nil, no events are logged.
	Logger *slog.Logger

	// LogSensitiveData controls whether sensitive function arguments and results
	// are included in logs. The default is false.
	LogSensitiveData bool

	// Tracer instruments tool invocations. It takes precedence over the tracer
	// supplied through [WithTracer]. If nil, the context-provided tracer is used
	// when available; otherwise tool invocations are not traced.
	Tracer Tracer

	// AdditionalTools are tools the middleware can invoke without adding them to
	// provider requests. Request tools supplied through agent options take
	// precedence; this collection is consulted afterward, which is useful when the
	// provider is already configured with tool declarations out of band.
	// Function invocation middleware registered before this middleware also wraps these tools.
	AdditionalTools []tool.Tool

	// IncludeDetailedErrors controls whether tool error details are included in
	// the function result sent back to the provider. When false, the provider sees
	// a generic error message while the raw error remains available on the
	// FunctionResultContent and is rethrown if the consecutive-error limit is
	// exceeded. The default is false.
	IncludeDetailedErrors bool

	// TerminateOnUnknownCalls controls whether a request to call an unknown tool
	// stops the function-calling loop. When false, the middleware returns a
	// generated "not found" function result to the provider. When true, the
	// original response is returned so the caller can handle the tool call. A known
	// schema tool that is not invocable always stops the loop. The default is false.
	TerminateOnUnknownCalls bool

	// AllowConcurrentInvocations controls whether multiple function calls from the
	// same provider response may execute in parallel. When false, function calls are
	// processed serially. The default is false.
	AllowConcurrentInvocations bool

	// MaximumConsecutiveErrorsPerRequest is the number of consecutive failing
	// function-invocation iterations allowed before the middleware returns the
	// aggregated invocation errors to the caller. A successful iteration resets the
	// count. When nil, the default of 3 is used. Set to new(0) to fail on the
	// first tool error.
	MaximumConsecutiveErrorsPerRequest *int

	// MaximumIterationsPerRequest is the maximum number of tool-invocation rounds
	// performed for a single run. When the limit is reached, the middleware makes a
	// final provider request without schema tools so no more local function calls
	// are requested. When nil, the default of 40 is used. Zero disables the
	// middleware as described by [Config]. Negative values are invalid.
	MaximumIterationsPerRequest *int

	// NewID generates synthetic IDs for generated tool-result messages and
	// approval-related fallback messages. If nil, a canonical dashed UUID is used.
	NewID func() string

	// DisableApprovalResponseBinding disables session-backed validation that
	// binds approval responses to approval requests surfaced by the framework.
	// Binding is enabled by default.
	DisableApprovalResponseBinding bool

	// DisableApprovalNotRequiredFunctionBypassing disables hiding and
	// automatically approving safe function calls returned alongside calls that
	// require approval. Bypassing is enabled by default.
	DisableApprovalNotRequiredFunctionBypassing bool
}

Config configures the automatic tool invocation middleware.

When a provider response contains function call content, the middleware looks up the matching tool, invokes it, sends the generated function result back to the provider, and repeats this loop until there are no more function calls or another stop condition is met. To disable the middleware, set MaximumIterationsPerRequest to new(0). A disabled middleware delegates once without changing the request options, messages, or response updates.

type Tracer added in v0.3.0

type Tracer interface {
	// StartToolCall begins tracing an invocation. The non-nil call and definition
	// are read-only. Calls awaiting approval and tools that cannot be invoked are
	// not traced.
	//
	// The returned context is passed to the tool. The returned completion
	// function is called exactly once with the invocation's final error, or nil
	// on success, after any tool panic has been recovered. Both return values
	// must be non-nil.
	StartToolCall(ctx context.Context, call *message.FunctionCallContent, definition tool.Tool) (context.Context, func(error))
}

Tracer instruments individual tool invocations without depending on a tracing backend. Implementations must be safe for concurrent calls.

Jump to

Keyboard shortcuts

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