tool

package
v0.4.5 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// OutputToolName is the well-known name for the structured output tool.
	OutputToolName = "submit_output"

	// OutputToolInstruction is appended to the system prompt when the
	// output tool is active, directing the model to call it with the final answer.
	OutputToolInstruction = "Use available tools to gather information. When ready, only call " + OutputToolName + " with your final answer, do not output any other text."
)

Variables

This section is empty.

Functions

func WithTool

func WithTool(t ...llm.Tool) opt.Opt

WithTool adds one or more tools to the generation options. Individual tools are appended under opt.ToolKey and merged with toolkit tools by each provider.

Types

type DefaultTool

type DefaultTool struct{}

DefaultTool provides no-op default implementations of the optional Tool interface methods OutputSchema and Meta. Embed it in concrete tool types so they satisfy the full Tool interface without boilerplate.

func (DefaultTool) Meta

func (DefaultTool) Meta() llm.ToolMeta

func (DefaultTool) OutputSchema

func (DefaultTool) OutputSchema() *jsonschema.Schema

type JSONResource

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

JSONResource wraps raw JSON bytes as an llm.Resource with MIME type application/json. Use this to return JSON output from tool Run methods.

func NewJSONResource

func NewJSONResource(data []byte) *JSONResource

NewJSONResource creates a Resource wrapping the given JSON bytes.

func (*JSONResource) Description

func (r *JSONResource) Description() string

func (*JSONResource) Name

func (r *JSONResource) Name() string

func (*JSONResource) Read

func (r *JSONResource) Read(_ context.Context) ([]byte, error)

func (*JSONResource) Type

func (r *JSONResource) Type() string

func (*JSONResource) URI

func (r *JSONResource) URI() string

type OutputTool

type OutputTool struct {
	DefaultTool
	// contains filtered or unexported fields
}

OutputTool wraps a JSON schema as a tool, allowing the model to produce structured output by "calling" this tool with the desired data. This avoids the conflict in providers like Gemini that don't support function calling combined with a response JSON schema.

func NewOutputTool

func NewOutputTool(s *jsonschema.Schema) *OutputTool

NewOutputTool creates a tool whose parameter schema is the given JSON schema. When the model calls this tool, its arguments ARE the structured output.

func (*OutputTool) Description

func (t *OutputTool) Description() string

func (*OutputTool) InputSchema

func (t *OutputTool) InputSchema() *jsonschema.Schema

func (*OutputTool) Name

func (t *OutputTool) Name() string

func (*OutputTool) Run

func (t *OutputTool) Run(_ context.Context, input json.RawMessage) (any, error)

func (*OutputTool) Validate

func (t *OutputTool) Validate(data json.RawMessage) error

Validate checks that the given JSON conforms to the output schema. Returns nil if the data is valid or no schema was provided.

type Toolkit

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

Toolkit is a collection of tools with unique names

func NewToolkit

func NewToolkit(opts ...ToolkitOpt) (*Toolkit, error)

NewToolkit creates a new toolkit with the given options.

func (*Toolkit) AddBuiltin

func (tk *Toolkit) AddBuiltin(tools ...llm.Tool) error

AddBuiltin adds one or more locally-implemented tools to the toolkit. Returns an error if any tool has an invalid or duplicate name, or if the name is reserved (e.g. "submit_output").

func (*Toolkit) AddConnector

func (tk *Toolkit) AddConnector(url string, c llm.Connector) error

AddConnector registers a remote connector under the given URL and starts its Run loop in a background goroutine. The goroutine's context is cancelled by RemoveConnector or Close. Returns an error if a connector with the same URL is already registered.

func (*Toolkit) Close

func (tk *Toolkit) Close() error

Close cancels all active connector goroutines, waits for them to finish, and releases resources.

func (*Toolkit) Feedback

func (tk *Toolkit) Feedback(call schema.ToolCall) string

Feedback returns a human-readable description of a tool call, including the tool name and its description when available.

func (*Toolkit) ListTools

func (tk *Toolkit) ListTools(req schema.ToolListRequest) []llm.Tool

ListTools returns tools matching the given request filters. An empty request returns all tools across all namespaces (builtins + connectors).

func (*Toolkit) Lookup

func (tk *Toolkit) Lookup(name string) llm.Tool

Lookup returns a tool by name, searching builtins first then connector tools. Returns nil if not found.

func (*Toolkit) RemoveConnector

func (tk *Toolkit) RemoveConnector(url string)

RemoveConnector cancels the named connector's goroutine, waits for it to finish, then removes it from the registry. No-op if the URL is not registered.

func (*Toolkit) Run

func (tk *Toolkit) Run(ctx context.Context, name string, input json.RawMessage) (any, error)

Run executes a tool by name with the given JSON input. Returns an error if the tool is not found, the input does not match the schema, or the tool execution fails.

func (*Toolkit) String

func (tk *Toolkit) String() string

type ToolkitOpt

type ToolkitOpt func(*Toolkit) error

ToolkitOpt is a functional option for configuring a Toolkit at construction time.

func WithBuiltin

func WithBuiltin(tools ...llm.Tool) ToolkitOpt

WithBuiltin adds one or more locally-implemented tools to the toolkit at construction time.

func WithLogHandler

func WithLogHandler(fn func(url string, level slog.Level, msg string, args ...any)) ToolkitOpt

WithLogHandler sets a callback that receives log messages forwarded from a connector's MCP session. url identifies the originating connector.

func WithStateHandler

func WithStateHandler(fn func(url string, state schema.ConnectorState)) ToolkitOpt

WithStateHandler sets a callback that is invoked when a connector successfully connects or reconnects. state contains the server-reported name, version, and capabilities for that session.

func WithToolsHandler

func WithToolsHandler(fn func(url string, tools []llm.Tool)) ToolkitOpt

WithToolsHandler sets a callback that is invoked when a connector's tool list changes, or when a connector is removed or disconnects. tools is nil when the connector has gone away.

Jump to

Keyboard shortcuts

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