toolbase

package
v0.2.0 Latest Latest
Warning

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

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

Documentation

Overview

Package toolbase provides domain-neutral helpers shared by tool packages.

Package toolbase contains domain-neutral helpers shared by tool packages.

Package toolbase provides the small set of schema "semantic" wrappers used by FileEditTool, FileReadTool, etc. They mirror the TS zod helper style (semanticBoolean, semanticNumber) so the JSON schema published to the model carries human-readable descriptions and safe defaults.

Index

Constants

View Source
const ModelImageMaxBytes = 5 * 1024 * 1024

ModelImageMaxBytes is the maximum decoded size accepted for one tool image.

Variables

This section is empty.

Functions

func CanonicalPath

func CanonicalPath(path string) string

CanonicalPath normalizes a filesystem path for containment comparisons. On Darwin it also removes the kernel's /private alias for public system directories so the same path has one comparison form.

func DisplayPath

func DisplayPath(path string) string

DisplayPath returns the stable user-facing representation of a path.

func MatchGlob

func MatchGlob(pattern string, path string) (bool, error)

MatchGlob matches one path with the current doublestar glob contract.

func MatchGlobRelativeTo

func MatchGlobRelativeTo(pattern string, path string, root string) (bool, error)

MatchGlobRelativeTo behaves like MatchGlob but resolves path against root when path is absolute, so patterns like "src/**/*.go" continue to match "<root>/src/foo/bar.go". Patterns without a path separator match against the basename only.

func NewBase64ImageBlock

func NewBase64ImageBlock(data, mediaType string) types.ContentBlock

NewBase64ImageBlock builds an image content block from already encoded data.

func ParseInput

func ParseInput[T any](input map[string]any) (T, error)

ParseInput unmarshals a map input into T using the standard JSON contract.

func ParseInputOrError

func ParseInputOrError[T any](input map[string]any) (T, *types.ToolResult)

ParseInputOrError parses input and projects decoding failures as a failed tool result suitable for returning from Execute.

func ParseStrictInput

func ParseStrictInput[T any](input map[string]any) (T, error)

ParseStrictInput decodes T and rejects fields not declared by its JSON contract.

func ParseStrictInputOrError

func ParseStrictInputOrError[T any](input map[string]any) (T, *types.ToolResult)

ParseStrictInputOrError parses strict input and projects decoding failures as a failed tool result suitable for returning from Execute.

func PathContains

func PathContains(root, candidate string) bool

PathContains reports whether candidate is root itself or a descendant.

func PathWithinAllowedDirs

func PathWithinAllowedDirs(candidate string, allowedDirs []string) bool

PathWithinAllowedDirs checks containment against canonical absolute roots. Symlinks in the longest existing prefix are resolved for both candidate and roots; a not-yet-created suffix is then reattached to the resolved prefix.

func ResizeImageBytes

func ResizeImageBytes(raw []byte, mediaType string, targetBytes int) ([]byte, string, error)

ResizeImageBytes downsamples an encoded image (PNG/JPEG) until its byte size drops below `targetBytes`. We approximate matplotlib DPI=300 → DPI=96 by halving width/height repeatedly; the JPEG quality ladder (90 → 75 → 60) handles the long tail. Returns the encoded bytes and the media type (which switches to image/jpeg when JPEG is the smaller form).

func ResizeImageToolResult

func ResizeImageToolResult(res types.ToolResult) types.ToolResult

ResizeImageToolResult mutates res in-place to apply the disk- fallback + DPI downsampling pipeline TS uses to fit Anthropic's 5MB image cap. When the image already fits, it is returned unchanged.

The caller is expected to have populated res.ContentBlocks with an ImageBlock built from the stdout data URI; this routine is a no-op for non-image results.

func SemanticBoolean

func SemanticBoolean(description string, defaultValue bool) map[string]any

SemanticBoolean returns a JSON Schema object describing a boolean property with a human-friendly description and a default value. The `_semantic` marker distinguishes schemas built via this helper from raw boolean schemas, letting downstream introspection tell the two apart.

func SemanticNumber

func SemanticNumber(description string, minimum int, integer bool) map[string]any

SemanticNumber returns a JSON Schema object describing a numeric property with bounds and a description. `minimum`/`maximum` are emitted only when non-zero (use math.Inf-style sentinels by passing 0 to skip). `integer` is set when the value must be a whole number.

func SuggestNearbyPath

func SuggestNearbyPath(filePath, cwd string) string

SuggestNearbyPath returns the closest sibling path for a misspelled input.

Types

This section is empty.

Jump to

Keyboard shortcuts

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