tools

package
v0.0.0-...-187d9d3 Latest Latest
Warning

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

Go to latest
Published: May 30, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package tools defines the Tool interface and an in-memory registry.

Concrete tools (apply_patch, shell, read) live in sub-packages and register themselves with a Registry held by the main loop.

Schema-driven input validation for tool args. Runs in the loop BEFORE each tool's own Run, so a model that emits valid JSON but the wrong key names sees a concrete corrected example instead of a generic "missing X" from inside the tool.

Why centralized: every tool hand-rolls required-key checks today and emits a plain-text "missing or empty 'foo' field" message. Small local models (qwen3-a3b, gemma3) need a literal example envelope to lock onto. This validator returns one, derived from the tool's own JSON Schema.

Index

Constants

View Source
const MaxOutputTokens = 50_000

MaxOutputTokens is the default cap for a tool's text result, in token estimates (using the chars/4 heuristic that the rest of bee uses).

View Source
const MaxWebfetchTokens = 10_000

MaxWebfetchTokens is the aggressive cap for fetch-style tools that return arbitrary remote content.

Variables

This section is empty.

Functions

func IntArg

func IntArg(in map[string]any, key string, def int) int

IntArg returns the int value at key, accepting JSON number variants and stringified ints (provider quirk). Returns def when missing or unparseable.

func RelTo

func RelTo(base, p string) string

RelTo returns p relative to base. Falls back to absolute path on cross-tree or error so callers never get an empty/wrong path.

func Truncate

func Truncate(toolName, content string) (string, bool)

Truncate caps content to limitFor(toolName) tokens (chars/4 heuristic). When the content exceeds the cap, the head is kept and a trailer is appended. Returns the (possibly modified) content and a bool indicating whether truncation occurred.

func TruncateForTool

func TruncateForTool(toolName, content string) string

TruncateForTool applies the per-tool default cap and returns only the (possibly modified) content. Convenience wrapper for callers that don't care about the truncated bool.

func TruncateHeadTail

func TruncateHeadTail(toolName, content string, tailChars int) (string, bool)

TruncateHeadTail is like Truncate but preserves both head and tail with a spacer in the middle, giving the model context from the end of large outputs. tailChars defaults to 4 KB (4096) if < 0. Returns modified content + true.

func TruncateHeadTailWithLimit

func TruncateHeadTailWithLimit(toolName, content string, limitTokens int, tailChars int) (string, bool)

TruncateHeadTailWithLimit is TruncateHeadTail with an explicit profile cap.

func TruncateWithLimit

func TruncateWithLimit(toolName, content string, limitTokens int) (string, bool)

TruncateWithLimit is Truncate with an explicit profile-provided cap in tokens (chars/4 heuristic). limitTokens<=0 → fall back to the per-tool default. The smaller of (limitTokens, per-tool default) wins so a profile override cannot accidentally raise a webfetch-style tight ceiling.

When truncation occurs and a spill directory can be resolved (via $BEE_HOME/spill or os.UserHomeDir()+/.bee/spill), the full untruncated body is written there and the trailer points the model at it. Spill failures degrade gracefully to the original truncate-and-discard.

func TruncateWithLimitSpill

func TruncateWithLimitSpill(toolName, content string, limitTokens int, spillDir string) (string, bool)

TruncateWithLimitSpill is the testable variant: caller supplies the spill directory. Empty spillDir disables spillover (truncate-and-discard).

Per-tool truncateMode controls shape: head-only by default; head-tail for bash so the panic/error at the end of a long test log survives the cut.

func ValidateInput

func ValidateInput(spec llm.ToolSpec, input map[string]any) error

ValidateInput checks that input contains all required keys from spec.Schema and that present values match the declared JSON Schema type. Returns nil on success. On failure returns a diagnostic string suitable for sending back to the model: lists what is wrong, lists the accepted keys with types, and finally a one-shot XML envelope example so 3B-active MoEs can copy it.

Type checks are intentionally lenient: JSON numbers decode as float64, so "integer" accepts float64 with an integral value. Strings, bools, arrays and objects map straight through. Unknown schema types skip checking.

Types

type Registry

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

Registry maps tool name -> Tool. Safe for concurrent reads after build.

func NewRegistry

func NewRegistry() *Registry

func (*Registry) Get

func (r *Registry) Get(name string) (Tool, bool)

func (*Registry) Names

func (r *Registry) Names() []string

Names returns the registered tool names sorted alphabetically. Used by diagnostics (e.g. unknown-tool error feedback) so the model can recover without having to re-read the system prompt.

func (*Registry) Register

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

func (*Registry) Specs

func (r *Registry) Specs() []llm.ToolSpec

Specs returns every registered tool's spec, sorted alphabetically by name. The sort guarantees a stable order across calls and process runs — critical for KV-cache prefix hits on the system prompt's tool manifest, which would otherwise reshuffle on every turn (Go map iteration is randomized).

type Result

type Result struct {
	Content string
	IsError bool
}

Result is the tool output handed back to the model.

type Tool

type Tool interface {
	Spec() llm.ToolSpec
	Run(ctx context.Context, input map[string]any) (Result, error)
}

Tool is the contract every executable tool must satisfy.

Directories

Path Synopsis
Package apply_patch implements the unified-diff mutation tool.
Package apply_patch implements the unified-diff mutation tool.
Package ask_user implements the ask_user tool: the model asks the user a multiple-choice question and blocks on the answer.
Package ask_user implements the ask_user tool: the model asks the user a multiple-choice question and blocks on the answer.
Package codegraph wraps the external `codegraph` CLI as a bee tool.
Package codegraph wraps the external `codegraph` CLI as a bee tool.
Package edit_diff implements the edit_diff tool: literal find/replace.
Package edit_diff implements the edit_diff tool: literal find/replace.
Package escalate implements the escalate tool: the model's explicit exit door when it's stuck and a human should take over.
Package escalate implements the escalate tool: the model's explicit exit door when it's stuck and a human should take over.
Package find implements the find tool: recursive name-glob file search.
Package find implements the find tool: recursive name-glob file search.
Package godoc implements the godoc tool: query the local Go documentation for a package or symbol via `go doc -short`.
Package godoc implements the godoc tool: query the local Go documentation for a package or symbol via `go doc -short`.
Package grep implements the grep tool: recursive regex search.
Package grep implements the grep tool: recursive regex search.
Package hashline_edit implements LINE#ID-anchored file edits.
Package hashline_edit implements LINE#ID-anchored file edits.
Package knowledge_search implements the knowledge_search tool: on-demand lookup of the bee knowledge store from inside an agent turn.
Package knowledge_search implements the knowledge_search tool: on-demand lookup of the bee knowledge store from inside an agent turn.
Package knowledge_write implements the knowledge_write tool: store a record in bee's on-disk knowledge store.
Package knowledge_write implements the knowledge_write tool: store a record in bee's on-disk knowledge store.
Package ls implements the ls tool: list a single directory (no recursion).
Package ls implements the ls tool: list a single directory (no recursion).
Package read implements the read tool: read file or list directory.
Package read implements the read tool: read file or list directory.
Package shell implements the shell tool: bash -c execution with timeout and output truncation.
Package shell implements the shell tool: bash -c execution with timeout and output truncation.
Package tool_lookup implements the tool_lookup tool: returns the full schema, description, and prompt snippet of any registered tool by name.
Package tool_lookup implements the tool_lookup tool: returns the full schema, description, and prompt snippet of any registered tool by name.
Package usertool registers user-defined shell-alias tools loaded from [[user_tools]] entries in ~/.bee/config.toml.
Package usertool registers user-defined shell-alias tools loaded from [[user_tools]] entries in ~/.bee/config.toml.
Package write implements the write tool: overwrite a file inside the workspace root.
Package write implements the write tool: overwrite a file inside the workspace root.

Jump to

Keyboard shortcuts

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