tools

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MPL-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package tools provides implementations for executing tools as part of MindTrial's function calling capabilities.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrToolNotAvailable is returned when a requested tool is not available.
	ErrToolNotAvailable = errors.New("tool not available")
	// ErrToolExecutionFailed is returned when a tool executes but fails with an error.
	ErrToolExecutionFailed = errors.New("tool execution failed")
	// ErrInvalidToolArguments is returned when tool arguments are invalid or don't match the expected schema.
	ErrInvalidToolArguments = errors.New("invalid tool arguments")
	// ErrToolInternal is returned for low-level internal errors during tool execution.
	ErrToolInternal = errors.New("tool internal error")
	// ErrUnsupportedToolType is returned when an unsupported tool type is encountered.
	ErrUnsupportedToolType = errors.New("unsupported tool type")
	// ErrToolMaxCallsExceeded is returned when a tool has exceeded its maximum call limit.
	ErrToolMaxCallsExceeded = errors.New("tool max calls exceeded")
	// ErrToolTimeout is returned when a tool execution times out.
	ErrToolTimeout = errors.New("tool execution timeout")
)

Functions

This section is empty.

Types

type DockerTool

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

func NewDockerTool

func NewDockerTool(cfg *config.ToolConfig, maxCalls *int, timeout *time.Duration, maxMemoryMB *int, cpuPercent *int) *DockerTool

NewDockerTool creates a new Docker tool.

type DockerToolExecutor

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

DockerToolExecutor executes tools within Docker containers.

func NewDockerToolExecutor

func NewDockerToolExecutor(ctx context.Context) (*DockerToolExecutor, error)

NewDockerToolExecutor creates a new Docker tool executor.

func (*DockerToolExecutor) Close

func (d *DockerToolExecutor) Close() error

Close closes the Docker client connection and cleans up shared directories.

func (*DockerToolExecutor) ExecuteTool

func (d *DockerToolExecutor) ExecuteTool(ctx context.Context, logger logging.Logger, toolName string, args json.RawMessage, data map[string][]byte, callCtx *ToolCallContext) (json.RawMessage, error)

ExecuteTool executes a tool by name with the given arguments and auxiliary data files. callCtx carries optional caller-supplied metadata (see ToolCallContext) to attach to the resulting ToolCallSummary; pass nil if there is none to provide.

func (*DockerToolExecutor) GetCallSummaries added in v0.20.0

func (d *DockerToolExecutor) GetCallSummaries() []ToolCallSummary

GetCallSummaries returns a log of every recorded invocation attempt across all tools, in the order calls completed, including attempts that never actually ran (e.g. due to invalid arguments or an infrastructure error during setup). This is tracked independently of GetUsageStats; a tool with no entry in GetUsageStats (because its container never once ran) can still appear here. Returns nil if the executor is nil.

func (*DockerToolExecutor) GetUsageStats

func (d *DockerToolExecutor) GetUsageStats() map[string]ToolUsage

GetUsageStats returns aggregate execution statistics for all tools.

func (*DockerToolExecutor) IsToolExhausted added in v0.15.0

func (d *DockerToolExecutor) IsToolExhausted(toolName string) bool

IsToolExhausted reports whether the named tool has exceeded its maximum call limit. Returns false if the executor is nil or the tool has not been used.

func (*DockerToolExecutor) RegisterTool

func (d *DockerToolExecutor) RegisterTool(tool *DockerTool)

RegisterTool registers a tool with the executor.

func (*DockerToolExecutor) ValidateTool added in v0.12.1

func (d *DockerToolExecutor) ValidateTool(ctx context.Context, cfg config.ToolConfig) error

ValidateTool ensures the Docker image referenced by the tool configuration is available locally.

type OutputCapture added in v0.20.0

type OutputCapture struct {
	// Bytes is the total size of the output stream, regardless of Truncated.
	Bytes int64
	// Preview is a truncated prefix of the output stream, or nil if not captured
	// (e.g. stdout on a successful call) or empty.
	Preview *string
	// Truncated indicates whether Preview was cut short of the full output.
	Truncated bool
}

OutputCapture holds a size-limited preview of a tool call's output stream.

type TextOrData added in v0.12.0

type TextOrData interface {
	~string | ~[]byte
}

TextOrData is a constraint for types that can be written to files.

type ToolCallContext added in v0.20.0

type ToolCallContext struct {
	// CallID is the provider's own identifier for this tool call, if the provider's API
	// assigns one (e.g. OpenAI/Anthropic/DeepSeek/Mistral AI/xAI's tool_call/tool_use ID).
	// Reusing the provider's own ID - rather than minting an unrelated one - means this same
	// ID can also be found in any API error message that references the call. If empty,
	// ExecuteTool generates one internally (a ULID) instead, so ToolCallSummary.CallID is
	// never empty; note this means CallID's shape/format varies depending on whether - and
	// how - the calling provider assigns its own IDs.
	CallID string
	// ConversationTurn is the 1-based conversation turn this call is being made during, or 0
	// if unknown/not applicable.
	ConversationTurn int
}

ToolCallContext carries optional caller-supplied metadata to attach to the ToolCallSummary recorded for a single ExecuteTool call. All fields are optional; a nil ToolCallContext (or a zero-value one) simply leaves the corresponding ToolCallSummary fields unset. New fields can be added here in the future without changing ExecuteTool's signature again.

type ToolCallSummary added in v0.20.0

type ToolCallSummary struct {
	// Tool is the name of the tool this call invoked.
	Tool string
	// CallID is a unique identifier (ULID) for this call. It is also included in the
	// prefix of every log line emitted while this call was in progress, so a specific
	// invocation can be correlated between this summary and the corresponding log lines.
	CallID string
	// ConversationTurn is the 1-based conversation turn this call was made during, or 0 if
	// unknown/not provided by the caller.
	ConversationTurn int
	// StartedAt is when this call began (start of setup, before the container ever runs).
	StartedAt time.Time
	// CompletedAt is when this call finished, successfully or not.
	CompletedAt time.Time
	// DurationNs is the wall-clock duration of the container's runtime (start through exit),
	// the same measurement window as the aggregate ToolUsage.TotalDurationNs. It does not
	// include setup (argument parsing, mounts, container creation) or teardown overhead, and
	// stays nil when no container process ever ran (e.g. an infrastructure_error during setup).
	DurationNs *int64
	// WallTimeNs is the wall-clock duration of the entire call attempt, from setup
	// through output retrieval - i.e. DurationNs plus setup/teardown overhead. Unlike
	// DurationNs, this is always set, even for calls whose container never ran.
	WallTimeNs int64
	// ExitCode is the container's exit code, or nil if no exit code is known (e.g. setup never
	// reached container start, or the run was aborted/cancelled before it could be observed).
	ExitCode *int64
	// TimedOut indicates the call was aborted due to exceeding its configured timeout.
	TimedOut bool
	// Status is one of: "success", "nonzero_exit", "empty_output", "timeout",
	// "invalid_arguments", "infrastructure_error". Statuses are chosen to be meaningful for
	// result analysis: "nonzero_exit", "empty_output", and "invalid_arguments" reflect the
	// tool being used incorrectly (a plausible model/tool-usage issue), while
	// "infrastructure_error" covers environment/tooling failures (container/filesystem setup,
	// Docker runtime errors including cancellation, and log retrieval failures) that the
	// model has no influence over and are not informative about the model or tool under test.
	Status string
	// Stdout is a size-limited capture of the call's standard output, or nil if no output was
	// ever captured (e.g. an infrastructure_error before or during log retrieval).
	Stdout *OutputCapture
	// Stderr is a size-limited capture of the call's standard error, or nil if no output was
	// ever captured (e.g. an infrastructure_error before or during log retrieval).
	Stderr *OutputCapture
	// ErrorMessage is a short explanation of the failure when Status is not "success".
	ErrorMessage string
}

ToolCallSummary records the outcome of a single tool invocation.

type ToolUsage

type ToolUsage struct {
	CallCount       int64
	TotalDurationNs int64
	Exhausted       int32
}

ToolUsage tracks aggregate execution statistics for a tool: CallCount and TotalDurationNs only reflect invocations whose container actually started running (regardless of exit code) - i.e. actual compute time spent, not every invocation attempt. An invocation that fails before the container ever starts (e.g. invalid arguments, or an infrastructure error during setup) does not affect these aggregates. See ToolCallSummary/GetCallSummaries for a complete per-invocation log that does include such attempts.

Jump to

Keyboard shortcuts

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