tools

package
v0.30.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package tools defines local tool registration, permission metadata, and authority-gated execution.

Registry is safe for concurrent definition reads and tool execution. Registration validates tool names, effects, permission modes, and reserved control names. Strictly safe read-only tools may omit Permission.Mode and are exposed as allowed tools. Mutating, destructive, open-world, shell, and network tools must declare explicit permission behavior. Host authorization is supplied through runtime.EffectAuthorizationGate; this package does not expose a standalone approval callback. PermissionDeny tools remain callable only as explicit host-side disabled tools and are not exposed to providers. Ordinary calls returned in one model batch execute concurrently; the model expresses dependencies by emitting dependent calls in later turns.

Index

Constants

View Source
const (
	DefaultToolVisibleMaxBytes = 64 * 1024
	DefaultToolVisibleMaxLines = 0
	DefaultToolOutputStrategy  = OutputTail
	DefaultPreserveFull        = true
	DefaultArtifactKind        = "tool_output"
	DefaultArtifactMIME        = "text/plain; charset=utf-8"
)
View Source
const (
	ControlAskUser      = "ask_user"
	ControlTaskComplete = "task_complete"
)
View Source
const (
	AnnotationRepeatPolicy                   = "repeat_policy"
	AnnotationRepeatIdentityIgnoredArguments = "repeat_identity_ignored_arguments"
	RepeatPolicyPolling                      = "polling"
	ResultMetadataProgressToken              = "progress_token"
)

Variables

View Source
var ErrDuplicate = errors.New("duplicate tool name")
View Source
var ErrEffectDispatcherRequired = errors.New("tool effect authority dispatcher is required")
View Source
var ErrInvalid = errors.New("invalid tool")
View Source
var ErrRejected = errors.New("tool call rejected")
View Source
var ErrSchema = errors.New("schema validation failed")

Functions

func Array

func Array(items map[string]any, description string) map[string]any

func Boolean

func Boolean(description string) map[string]any

func Enum

func Enum(values ...string) map[string]any

func Integer

func Integer(description string) map[string]any

func InvalidArgumentsText

func InvalidArgumentsText(name string, err error) string

func IsReservedName

func IsReservedName(name string) bool

func NormalizeInputSchema

func NormalizeInputSchema(schema map[string]any) (map[string]any, error)

func Nullable

func Nullable(schema map[string]any) map[string]any

func Number

func Number(description string) map[string]any

func PendingToolResultMetadata added in v0.3.10

func PendingToolResultMetadata(p PendingToolResult) map[string]any

func PendingToolResultText added in v0.3.10

func PendingToolResultText(p PendingToolResult) string

func StrictObject

func StrictObject(properties map[string]any, required []string) map[string]any

func String

func String(description string) map[string]any

func Validate

func Validate(schema map[string]any, raw []byte) (map[string]any, error)

func ValidateStructured

func ValidateStructured(schema map[string]any, value any) error

Types

type ActivityUpdate added in v0.3.68

type ActivityUpdate struct {
	Activity *observation.ActivityPresentation
	Metadata map[string]any
}

type ArtifactRef added in v0.3.0

type ArtifactRef struct {
	ID        string `json:"id,omitempty"`
	SafeLabel string `json:"safe_label,omitempty"`
	Kind      string `json:"kind,omitempty"`
	MIME      string `json:"mime,omitempty"`
	SizeBytes int64  `json:"size_bytes,omitempty"`
	SHA256    string `json:"sha256,omitempty"`
}

type Definition

type Definition struct {
	Name         string
	Title        string
	Description  string
	InputSchema  map[string]any
	OutputSchema map[string]any
	Activity     func(Invocation[any]) (*observation.ActivityPresentation, error)

	Effects     []Effect
	ReadOnly    bool
	Destructive bool
	OpenWorld   bool

	Permission    PermissionSpec
	PermissionFor PermissionResolver
	OutputPolicy  OutputPolicy
	Annotations   map[string]any
}

func ValidateDefinition

func ValidateDefinition(def Definition) (Definition, error)

type DispatchOptions added in v0.18.0

type DispatchOptions struct {
	RunID         string
	ThreadID      string
	TurnID        string
	PromptScopeID string
	Step          int
	BatchIndex    int
	BatchSize     int
	Labels        map[string]string
	HostContext   map[string]string

	DispatchStarted      func(DispatchStart)
	ActivityUpdated      func(ToolActivityUpdate)
	EffectBatchPreflight EffectBatchPreflight
	EffectDispatcher     EffectDispatcher
}

type DispatchStart added in v0.3.63

type DispatchStart struct {
	CallID        string
	Name          string
	RawArgs       string
	RunID         string
	ThreadID      string
	TurnID        string
	PromptScopeID string
	Step          int
	Labels        map[string]string
	HostContext   map[string]string
}

type Effect

type Effect string
const (
	EffectRead    Effect = "read"
	EffectWrite   Effect = "write"
	EffectShell   Effect = "shell"
	EffectNetwork Effect = "network"
)

type EffectBatchPreflight added in v0.20.0

type EffectBatchPreflight func(context.Context, []EffectDispatchRequest) error

type EffectDispatchRequest added in v0.18.0

type EffectDispatchRequest struct {
	CallID        string
	Name          string
	RawArgs       string
	RunID         string
	ThreadID      string
	TurnID        string
	PromptScopeID string
	Step          int
	BatchIndex    int
	BatchSize     int
	Labels        map[string]string
	HostContext   map[string]string
	Resources     []ResourceRef
	Effects       []Effect
	Permission    PermissionSpec
	ReadOnly      bool
	Destructive   bool
	OpenWorld     bool
}

type EffectDispatcher added in v0.18.0

type EffectDispatcher func(context.Context, EffectDispatchRequest, func(context.Context) Result) Result

EffectDispatcher authorizes one prepared effect and chooses the execution context used by the tool handler. A lifecycle-owning dispatcher is responsible for bounding that selected context by its caller's execution lifetime.

type FullOutputPlan added in v0.18.0

type FullOutputPlan struct {
	Text string
	Kind string
	MIME string
}

FullOutputPlan is a side-effect-free request to admit the complete tool output together with its canonical result. It is not a durable artifact ref.

type Invocation

type Invocation[T any] struct {
	CallID          string
	Name            string
	RawArgs         string
	Args            T
	RunID           string
	ThreadID        string
	TurnID          string
	PromptScopeID   string
	Step            int
	Labels          map[string]string
	HostContext     map[string]string
	ActivityUpdater func(ActivityUpdate)
}

func (Invocation[T]) UpdateActivity added in v0.3.68

func (i Invocation[T]) UpdateActivity(update ActivityUpdate)

type OutputPolicy

type OutputPolicy struct {
	VisibleMaxBytes int
	VisibleMaxLines int
	Strategy        OutputStrategy
	PreserveFull    bool
	PreserveFullSet bool
	ArtifactKind    string
	ArtifactMIME    string
}

func DefaultOutputPolicy

func DefaultOutputPolicy() OutputPolicy

func MergeOutputPolicy

func MergeOutputPolicy(base OutputPolicy, override *OutputPolicy) OutputPolicy

func NormalizeOutputPolicy

func NormalizeOutputPolicy(policy OutputPolicy) OutputPolicy

type OutputProjection

type OutputProjection struct {
	VisibleText    string
	Truncated      bool
	OriginalBytes  int
	VisibleBytes   int
	OriginalLines  int
	VisibleLines   int
	Strategy       OutputStrategy
	ContentSHA256  string
	FullOutput     *ArtifactRef
	FullOutputPlan *FullOutputPlan
}

func BuildOutputProjection

func BuildOutputProjection(result Result, policy OutputPolicy) OutputProjection

type OutputStrategy

type OutputStrategy string
const (
	OutputHead OutputStrategy = "head"
	OutputTail OutputStrategy = "tail"
)

type PendingToolResult added in v0.3.10

type PendingToolResult struct {
	// Handle is the provider-visible continuation token. Hosts should put the
	// exact token here that the model should reuse for later tool calls.
	Handle string
	State  PendingToolResultState
	// Summary and Instruction are provider-visible text.
	Summary     string
	Instruction string
	// Metadata is observation-only pending state. It is not rendered into the
	// provider-visible pending result text.
	Metadata map[string]string
}

PendingToolResult is returned by a tool handler after the host has started work whose lifecycle remains owned by the host application.

func (PendingToolResult) Validate added in v0.3.10

func (p PendingToolResult) Validate() error

type PendingToolResultState added in v0.3.10

type PendingToolResultState string
const (
	// PendingToolResultRunning marks host-owned work that is still active outside Floret.
	PendingToolResultRunning PendingToolResultState = "running"
)

type PermissionMode

type PermissionMode string
const (
	PermissionAllow PermissionMode = "allow"
	PermissionAsk   PermissionMode = "ask"
	PermissionDeny  PermissionMode = "deny"
)

type PermissionRequest added in v0.3.12

type PermissionRequest struct {
	CallID        string
	Name          string
	RawArgs       string
	Args          any
	RunID         string
	ThreadID      string
	TurnID        string
	PromptScopeID string
	Step          int
	Labels        map[string]string
	HostContext   map[string]string
}

type PermissionResolver added in v0.3.12

type PermissionResolver func(PermissionRequest) (PermissionSpec, error)

type PermissionSpec

type PermissionSpec struct {
	Mode          PermissionMode
	ResourceKinds []string
}

type Registry

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

func NewRegistry

func NewRegistry(items ...Tool) *Registry

func NewRegistryE

func NewRegistryE(items ...Tool) (*Registry, error)

func (*Registry) ActivityForCall added in v0.3.7

func (r *Registry) ActivityForCall(call ToolCall, opts DispatchOptions) (*observation.ActivityPresentation, error)

func (*Registry) Definition

func (r *Registry) Definition(name string) (Definition, bool)

func (*Registry) Definitions

func (r *Registry) Definitions() []ToolDefinition

func (*Registry) Dispatch added in v0.18.0

func (r *Registry) Dispatch(ctx context.Context, call ToolCall, opts DispatchOptions) Result

func (*Registry) DispatchBatch added in v0.20.0

func (r *Registry) DispatchBatch(ctx context.Context, calls []ToolCall, opts DispatchOptions) []Result

func (*Registry) ExposedDefinitions

func (r *Registry) ExposedDefinitions() []ToolDefinition

func (*Registry) OutputPolicyFor

func (r *Registry) OutputPolicyFor(name string) OutputPolicy

func (*Registry) Register

func (r *Registry) Register(t Tool) error

func (*Registry) Seal added in v0.28.0

func (r *Registry) Seal()

Seal prevents further registration while preserving definition reads and dispatch for the existing immutable tool snapshot. It is idempotent.

type ResourceRef

type ResourceRef struct {
	Kind  string
	Value string
}

type Result

type Result struct {
	CallID       string
	Name         string
	Title        string
	Text         string
	Structured   map[string]any
	Metadata     map[string]any
	Activity     *observation.ActivityPresentation
	Artifacts    []ArtifactRef
	OutputPolicy *OutputPolicy
	Pending      *PendingToolResult
	IsError      bool
	DispatchErr  error
	// contains filtered or unexported fields
}

func ErrorResult

func ErrorResult(callID, name, text string) Result

func (Result) RequiresEffectFinalization added in v0.18.0

func (r Result) RequiresEffectFinalization() bool

RequiresEffectFinalization reports whether the result crossed the effect authority dispatcher and therefore requires its atomic result finalizer.

type Tool

type Tool struct {
	Definition Definition
	// contains filtered or unexported fields
}

func Define

func Define[T any](
	def Definition,
	decode func([]byte) (T, error),
	resources func(Invocation[T]) ([]ResourceRef, error),
	handler func(context.Context, Invocation[T]) (Result, error),
) Tool

type ToolActivityUpdate added in v0.3.68

type ToolActivityUpdate struct {
	CallID        string
	Name          string
	RawArgs       string
	RunID         string
	ThreadID      string
	TurnID        string
	PromptScopeID string
	Step          int
	Labels        map[string]string
	HostContext   map[string]string
	Activity      *observation.ActivityPresentation
	Metadata      map[string]any
}

type ToolCall added in v0.3.0

type ToolCall struct {
	ID        string
	Name      string
	Args      string
	Reasoning string
}

type ToolDefinition added in v0.3.0

type ToolDefinition struct {
	Name         string
	Title        string
	Description  string
	InputSchema  map[string]any
	OutputSchema map[string]any
	Strict       bool
	Annotations  map[string]any
}

Jump to

Keyboard shortcuts

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