apitool

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 16 Imported by: ? modules, 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 apitool turns a service's OpenAPI document into ai.Tools that call the service's own http.Handler.

Every call is a synthetic *http.Request dispatched through the handler with the caller's bearer and account headers, so the ID middleware and every authorization wrapper run unchanged: the handlers are the policy and there is no tool-only path to a record. This is the natsapi rule applied to the agent.

Index

Constants

View Source
const (
	// HeaderTransport is set on every synthetic request so handlers and
	// logs can tell an agent call from a direct one.
	HeaderTransport = "X-Transport"
	// TransportAI is the value of HeaderTransport.
	TransportAI = "ai"
	// HeaderAccountID carries the caller's account to the ID middleware.
	HeaderAccountID = "X-Blackbox-AccountID"
	// HeaderUserID carries the caller's user id to the ID middleware.
	HeaderUserID = "X-Blackbox-UserID"

	// DefaultBaseURL is the host of the synthetic request when
	// Options.BaseURL is empty. The handler never dials it.
	DefaultBaseURL = "http://localhost"
	// DefaultMaxBodyBytes bounds a response body returned to the model.
	DefaultMaxBodyBytes = 1 << 20
)
View Source
const (
	// DefaultMaxResponseBytes is the response budget of Bounded. It stays
	// well below ai.DefaultMaxToolResultBytes (64 KiB) so every response
	// the model reads is complete JSON once the result envelope is added,
	// including the problem document that replaces an oversized body.
	DefaultMaxResponseBytes = 32 * 1024
	// DefaultMaxRepeatedFailures is how often one identical failed GET
	// reaches the handler within a run before Bounded answers for it.
	DefaultMaxRepeatedFailures = 2
	// DefaultGuidance is the advice in a problem document when the host
	// supplies no Guidance.
	DefaultGuidance = "Use a documented summary or paginated query. Report missing evidence if no bounded query is available."
)

Variables

This section is empty.

Functions

func Bounded

func Bounded(next http.Handler, o BoundedOptions) http.Handler

Bounded wraps the handler API tools dispatch through so the model only ever reads complete documents. A response larger than the budget is replaced by a 413 problem document that names a compact query instead of a truncated object list, and an identical GET that failed or overflowed MaxRepeatedFailures times in one run (see WithRunState) is answered with a 409 problem without reaching the handler again. A successful mutation clears the run's failure counts, because the data behind the failed reads may have changed. Streaming handlers must be excluded from the tool surface before they reach this wrapper.

func FromOpenAPI

func FromOpenAPI(spec []byte, h http.Handler, o Options) ([]ai.Tool, error)

FromOpenAPI builds one tool per operation in spec, an OpenAPI 3 document, executing each through h. Operations whose request body is not JSON (multipart uploads, for example) are skipped. Tool names are unique and match ai.ToolNamePattern.

func NewRequestTool

func NewRequestTool(h http.Handler, o Options) ai.Tool

NewRequestTool returns a tool named Prefix+"api_request" that calls a route of h, for routes the document does not list. It is never read-only, whatever the method and whatever Options.ReadOnly says: the approval policy decides whether a call runs. It only reaches paths under /api/, given in canonical form (no "." or ".." segments, no repeated slashes), and refuses any method and path Options.Include rejects, so a route excluded from the generated tools cannot be reached through it.

func WithRunState

func WithRunState(ctx context.Context) context.Context

WithRunState returns a context carrying a fresh failure guard for one run. A host installs it on the request that starts a run (ai/instance does so in Mount); tool calls inherit it through the run context, and state never leaks between runs. Without it Bounded only bounds the body.

Types

type BoundedOptions

type BoundedOptions struct {
	// MaxResponseBytes is the largest body a tool call may return. Zero
	// means DefaultMaxResponseBytes.
	MaxResponseBytes int
	// MaxRepeatedFailures is how many times one identical GET (method,
	// path and canonical query) may fail or overflow within a run before
	// the handler is no longer called for it. Zero means
	// DefaultMaxRepeatedFailures.
	MaxRepeatedFailures int
	// Guidance returns the advice placed in a problem document for a
	// request path: which compact or paginated query the model should use
	// instead. Nil means DefaultGuidance for every path. A product supplies
	// its own summary routes here; the platform names none.
	Guidance func(path string) string
}

BoundedOptions configures Bounded.

type Operation

type Operation struct {
	Method   string `json:"method"`
	Path     string `json:"path"`
	Name     string `json:"name"`
	ReadOnly bool   `json:"read_only"`
}

Operation describes one generated tool, for a status route that lists what the document produced.

func Operations

func Operations(spec []byte, o Options) ([]Operation, error)

Operations lists the operations FromOpenAPI would generate for spec without building the tools.

type Options

type Options struct {
	// Prefix is prepended to every tool name, for example "admin_".
	Prefix string
	// Include filters operations by method (upper case) and path template;
	// nil includes every operation. The generic request tool also calls it,
	// with the concrete request path instead of a template, and refuses a
	// call it rejects, so a filter that must hold for both matches a
	// template segment ("{id}") and a concrete one alike.
	Include func(method, path string) bool
	// ReadOnly overrides the read-only decision for one operation (method
	// upper case, path template). When it returns ok, readOnly is used as
	// is; otherwise the operation's x-ai-read-only extension decides when
	// present, and then the method (GET and HEAD are read-only). Use it, or
	// x-ai-read-only: false in the document, for a GET with side effects.
	ReadOnly func(method, path string) (readOnly bool, ok bool)
	// BaseURL is the scheme and host of the synthetic request. Default
	// DefaultBaseURL.
	BaseURL string
	// MaxBodyBytes truncates a response body returned to the model. Default
	// DefaultMaxBodyBytes.
	MaxBodyBytes int
}

Options configures the generated tools.

type Problem

type Problem struct {
	Type             string `json:"type"`
	Title            string `json:"title"`
	Status           int    `json:"status"`
	Detail           string `json:"detail"`
	RetrySameRequest bool   `json:"retry_same_request"`
}

Problem is the RFC 9457 document Bounded answers with. RetrySameRequest is always false: the same call will not succeed.

Jump to

Keyboard shortcuts

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