tools

package
v2.10.2 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 10 Imported by: 0 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 tools holds the tools Scribe's agent can call. Each tool is a value implementing Tool; the agent loop looks them up in a Registry.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Arr

func Arr(desc string, items map[string]any) map[string]any

func Bool

func Bool(desc string) map[string]any

func Decode

func Decode(args json.RawMessage, v any) error

Decode unmarshals args into v, tolerating an empty payload.

func DeniedWithReason

func DeniedWithReason(reason string) string

DeniedWithReason is the tail a caller appends to a denial message: the reason the Host gave (the person's words, or why the phone side failed), otherwise nothing.

func Enum

func Enum(desc string, vals ...string) map[string]any

func Int

func Int(desc string) map[string]any

func IsReadOnlyCall

func IsReadOnlyCall(t Tool, args json.RawMessage) bool

IsReadOnlyCall reports whether this particular call of t only reads.

func NoteApproval

func NoteApproval(ctx context.Context, reason string)

NoteApproval records that the user approved a call and gave a note. It is a no-op for an empty reason or a context without a slot. Call it after an approval that was not a denial, with the reason the Host gave.

func Schema

func Schema(props map[string]any, required ...string) json.RawMessage

Schema is a helper to build a JSON Schema object for tool specs:

Schema(map[string]any{"path": Str("file path")}, "path")

func Service

func Service[T any](e *Env, key string) (T, bool)

Service returns Services[key] as T.

func SetApprovalReason

func SetApprovalReason(ctx context.Context, reason string)

SetApprovalReason records the reason a Host gives for its answer. It is a no-op when the context has no slot. Long text is cut to 500 bytes.

func Str

func Str(desc string) map[string]any

Str, Int, Bool, Arr and Enum build property schemas.

func WithCallNotes

func WithCallNotes(ctx context.Context) (context.Context, func() string)

WithCallNotes returns a context that collects approval notes for one tool call, and a function that returns them joined by newlines ("" when none).

func WithReasonSlot

func WithReasonSlot(ctx context.Context) (context.Context, func() string)

WithReasonSlot returns a context that carries a reason slot, and a function that returns the reason set on it ("" when none). Create it before calling Host.Approve, and read it after.

Types

type Approval

type Approval struct {
	Tool   string // tool name
	Kind   string // "shell", "write_outside", "destructive", "web", "mcp"
	Title  string // short line, e.g. "Run shell command"
	Detail string // the command / path / URL
	Danger bool   // destructive: render red, default to Deny
}

Approval is a question to the user before a risky action.

type CallReadOnly

type CallReadOnly interface {
	ReadOnlyCall(args json.RawMessage) bool
}

CallReadOnly is implemented by tools whose read-only-ness depends on the arguments (git status vs git commit). The agent's mode gate and its parallel scheduler prefer it over Tool.ReadOnly.

type ClaimSource

type ClaimSource interface {
	// OtherClaims maps each file another tab claimed to that tab's label.
	OtherClaims() map[string]string
}

ClaimSource lists live file claims held by other tabs. Paths are absolute.

type Decision

type Decision int

Decision is the user's answer to an Approval.

const (
	Deny Decision = iota
	AllowOnce
	AllowSession // allow this kind of action for the rest of the session
)

type Env

type Env struct {
	Workdir string // absolute project root
	CallID  string // the tool call id
	Host    Host
	Files   FileTracker
	// Services is a bag of optional subsystems keyed by name ("repomap",
	// "lsp", "memory", "skills", "plan", "tasks", "dispatch", "mcp"). Each
	// package that provides one documents its key and type.
	Services map[string]any
	// Mode is the agent mode ("default", "plan", "auto", ...). Tools that
	// self-approve in auto mode read it.
	Mode string
	// Scope identifies the conversation context the call belongs to: one
	// agent's history, renewed whenever that history is compacted, reset or
	// replaced. Tools that remember what the model has already seen (the read
	// tool's "unchanged" notices) key that memory by Scope.
	Scope string
	// Claims reports the files other tabs in this process have claimed. Nil
	// when the tab has no cross-tab registry (tests, headless runs).
	Claims ClaimSource
}

Env is the per-call environment. Optional services are nil when not available; tools must check.

type FileTracker

type FileTracker interface {
	// BeforeWrite snapshots abs before it is modified (or notes that it
	// did not exist). Idempotent within a checkpoint.
	BeforeWrite(abs string)
	// MarkRead records that the model has seen abs at its current mtime.
	MarkRead(abs string)
	// StaleSinceRead reports whether abs changed on disk since MarkRead
	// (false when it was never read).
	StaleSinceRead(abs string) bool
	// WasRead reports whether the model has read abs this session.
	WasRead(abs string) bool
}

FileTracker records file contents before the agent changes them, so a turn can be undone (checkpoints) and edits can be refused on files the model has not read or that changed since it read them.

type Host

type Host interface {
	Approve(ctx context.Context, a Approval) Decision
	Ask(ctx context.Context, q Question) (string, error)
	// Progress streams partial output of a long-running call (shell
	// output lines, subagent steps) to the UI. May be a no-op.
	Progress(callID, text string)
}

Host is what the agent's surroundings (TUI, headless runner, Lectern session) provide to tools. Every method must be safe to call from any goroutine. Implementations for headless use may answer without a user.

type Question

type Question struct {
	Question string   `json:"question"`
	Options  []string `json:"options,omitempty"`
	// AllowFreeText lets the user type an answer instead of picking.
	AllowFreeText bool `json:"allow_free_text,omitempty"`
	// Kind is "" for a plain question or "plan_review" when the agent is
	// asking the user to approve a plan (the UI shows the plan view).
	Kind string `json:"kind,omitempty"`
	// Body is extra content to show with the question (the plan markdown).
	Body string `json:"body,omitempty"`
	// NoSkip means the question must be answered: Esc does not skip it.
	NoSkip bool `json:"no_skip,omitempty"`
}

Question is an ask_user prompt.

type Registry

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

Registry is a name-indexed set of tools.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty registry.

func (*Registry) Get

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

Get returns the tool named name.

func (*Registry) Names

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

Names returns tool names in registration order.

func (*Registry) Register

func (r *Registry) Register(t Tool)

Register adds t, replacing any tool with the same name.

func (*Registry) SortedNames

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

SortedNames is Names sorted alphabetically.

func (*Registry) Specs

func (r *Registry) Specs(names map[string]bool) []llm.ToolSpec

Specs returns specs in registration order, restricted to names when names is non-nil.

func (*Registry) Unregister

func (r *Registry) Unregister(name string) bool

Unregister removes the tool called name. It reports whether one was registered. Specs and Names no longer include it, so the next model request does not offer it.

type Result

type Result struct {
	Output  string `json:"output"`
	IsError bool   `json:"is_error,omitempty"`
	// Tag is a short machine code for an error, so the UI and hooks can
	// branch without parsing Output. Empty when there is nothing to branch
	// on. Known values: "forbidden", "not found", "concurrent modification",
	// "stale_undo", "contested".
	Tag string `json:"tag,omitempty"`
	// Summary is a one-line human summary for tool rows, e.g. "42 lines",
	// "3 matches in 2 files", "+5 -2". Optional.
	Summary string `json:"summary,omitempty"`
	// Diff is a unified diff for edit/write tools, rendered by the UI.
	Diff string `json:"diff,omitempty"`
	// Files lists paths (relative to the workdir) the call created,
	// changed or deleted. Used for checkpoints, "files edited" and the
	// changed-files bar.
	Files []string `json:"files,omitempty"`
	// Meta is free-form structured data for the UI.
	Meta map[string]any `json:"meta,omitempty"`
	// Image is an image the model should see with the result (a screenshot).
	// ImageType is its media type. Adapters that cannot send images drop it.
	Image     []byte `json:"-"`
	ImageType string `json:"-"`
}

Result is what a tool returns to the model (Output) and to the UI (Display and Meta). Output is always plain text the model reads.

func Errorf

func Errorf(format string, a ...any) Result

Errorf builds an error Result.

type Tool

type Tool interface {
	Spec() llm.ToolSpec
	// ReadOnly reports whether the tool never changes files or runs
	// commands. Restricted modes (plan, architect, ...) only allow these.
	ReadOnly() bool
	Run(ctx context.Context, env *Env, args json.RawMessage) Result
}

Tool is one callable tool.

Directories

Path Synopsis
Package exec holds Scribe's execution tools: shell (foreground and background), project (detected test, lint, build and run commands) and git.
Package exec holds Scribe's execution tools: shell (foreground and background), project (detected test, lint, build and run commands) and git.
Package fs holds Scribe's file tools: read, write_file, edit_file, multi_edit, undo_edit, list_dir, glob and grep.
Package fs holds Scribe's file tools: read, write_file, edit_file, multi_edit, undo_edit, list_dir, glob and grep.
Package web holds Scribe's web tools: web_search (SearXNG or DuckDuckGo HTML) and fetch_page (a readable-text fetch with an SSRF guard).
Package web holds Scribe's web tools: web_search (SearXNG or DuckDuckGo HTML) and fetch_page (a readable-text fetch with an SSRF guard).

Jump to

Keyboard shortcuts

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