Documentation
¶
Overview ¶
Package workspacetool provides local filesystem and shell tools for ds4go tool loops.
The package is optional: callers choose the workspace root and explicitly opt into side-effecting capabilities such as file writes and shell commands. By default, paths are confined to the configured root, symlink traversal is rejected, writes are disabled, and shell execution is disabled. In-root file I/O is performed through an os.Root handle, so operations cannot escape the root even under concurrent symlink swaps.
Routine tool failures (a missing file, a wrong edit anchor, a denied Confirm) are returned to the model as "ERROR: ..." tool results so a tool loop can continue; only context cancellation and errors returned by the Confirm callback abort the loop. Register methods only advertise tools the configuration permits.
A typical read-only setup registers file reading, directory listing, and search:
w, err := workspacetool.New(workspacetool.Config{Root: "/abs/project"})
if err != nil {
return err
}
defer w.Close()
reg := ds4go.NewToolRegistry()
if err := w.RegisterReadOnly(reg); err != nil {
return err
}
Editing tools require AllowWrite:
w, err := workspacetool.New(workspacetool.Config{
Root: "/abs/project",
AllowWrite: true,
})
Shell tools require AllowShell and run with the workspace root as their working directory. On Unix-like platforms, stopping a shell job also stops child processes created by that shell by default:
w, err := workspacetool.New(workspacetool.Config{
Root: "/abs/project",
AllowShell: true,
})
Package workspacetool provides optional local workspace tools for ds4go tool loops.
Index ¶
- type Action
- type ActionKind
- type Config
- type Workspace
- func (w *Workspace) BashStatusTool() ds4.ToolHandler
- func (w *Workspace) BashStopTool() ds4.ToolHandler
- func (w *Workspace) BashTool() ds4.ToolHandler
- func (w *Workspace) Close() error
- func (w *Workspace) EditTool() ds4.ToolHandler
- func (w *Workspace) ListTool() ds4.ToolHandler
- func (w *Workspace) MoreTool() ds4.ToolHandler
- func (w *Workspace) ReadTool() ds4.ToolHandler
- func (w *Workspace) RegisterAll(reg *ds4.ToolRegistry) error
- func (w *Workspace) RegisterEditing(reg *ds4.ToolRegistry) error
- func (w *Workspace) RegisterReadOnly(reg *ds4.ToolRegistry) error
- func (w *Workspace) RegisterShell(reg *ds4.ToolRegistry) error
- func (w *Workspace) SearchTool() ds4.ToolHandler
- func (w *Workspace) ViewImageTool() ds4.ToolHandler
- func (w *Workspace) WriteTool() ds4.ToolHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Action ¶
type Action struct {
Kind ActionKind
Path string
Command string
}
Action describes a side-effecting operation before it is executed.
type ActionKind ¶
type ActionKind string
ActionKind identifies a class of side effect.
const ( ActionWrite ActionKind = "write" ActionEdit ActionKind = "edit" ActionShell ActionKind = "shell" )
type Config ¶
type Config struct {
// Root is the workspace root. Relative paths resolve under this directory.
// It defaults to the current working directory.
Root string
// AllowWrite enables write and edit tools.
AllowWrite bool
// AllowShell enables bash, bash_status, and bash_stop tools.
AllowShell bool
// AllowOutsideRoot permits paths outside Root.
AllowOutsideRoot bool
// FollowSymlinks allows paths that traverse symlink components.
FollowSymlinks bool
// LeaveChildProcesses makes shell stop/timeout/close target only the shell
// process by default. The default is to stop child processes too on
// platforms with process-group termination support.
LeaveChildProcesses bool
MaxReadBytes int64
MaxReadLines int
MaxSearchResults int
MaxSearchFileBytes int64
MaxOutputBytes int64
MaxListEntries int
// Confirm is called before side-effecting operations when non-nil.
Confirm func(context.Context, Action) (bool, error)
// VisionAvailable reports whether the engine can encode images. When nil
// or false, view_image returns a text observation asking for --vision.
VisionAvailable func() bool
// Env is the environment used for shell commands. It defaults to os.Environ.
Env []string
// Shell is the shell executable used by bash. It defaults to $SHELL or
// /bin/sh on Unix, and cmd.exe on Windows.
Shell string
}
Config controls workspace tool behavior and safety policy.
type Workspace ¶
type Workspace struct {
// contains filtered or unexported fields
}
Workspace owns tool state for one local workspace.
func (*Workspace) BashStatusTool ¶
func (w *Workspace) BashStatusTool() ds4.ToolHandler
BashStatusTool returns a tool that reports status and output for a shell job.
func (*Workspace) BashStopTool ¶
func (w *Workspace) BashStopTool() ds4.ToolHandler
BashStopTool returns a tool that terminates a shell job.
func (*Workspace) BashTool ¶
func (w *Workspace) BashTool() ds4.ToolHandler
BashTool returns a tool that runs a shell command.
func (*Workspace) EditTool ¶
func (w *Workspace) EditTool() ds4.ToolHandler
EditTool returns a tool that replaces exactly one old text match. The old text may contain one [upto] marker between unique head and tail anchors.
func (*Workspace) ListTool ¶
func (w *Workspace) ListTool() ds4.ToolHandler
ListTool returns a tool that lists one directory compactly.
func (*Workspace) MoreTool ¶
func (w *Workspace) MoreTool() ds4.ToolHandler
MoreTool returns a tool that continues the previous read-like output.
func (*Workspace) ReadTool ¶
func (w *Workspace) ReadTool() ds4.ToolHandler
ReadTool returns a tool that reads a text file or line range.
func (*Workspace) RegisterAll ¶
func (w *Workspace) RegisterAll(reg *ds4.ToolRegistry) error
RegisterAll registers the read-only tools plus every tool group the configuration permits: write and edit when AllowWrite is set, and the shell tools when AllowShell is set. Disabled groups are not advertised.
func (*Workspace) RegisterEditing ¶
func (w *Workspace) RegisterEditing(reg *ds4.ToolRegistry) error
RegisterEditing registers read-only tools plus write and edit. It fails unless AllowWrite is enabled, so registered tools are always usable.
func (*Workspace) RegisterReadOnly ¶
func (w *Workspace) RegisterReadOnly(reg *ds4.ToolRegistry) error
RegisterReadOnly registers read, more, list, and search.
func (*Workspace) RegisterShell ¶
func (w *Workspace) RegisterShell(reg *ds4.ToolRegistry) error
RegisterShell registers bash, bash_status, and bash_stop. It fails unless AllowShell is enabled, so registered tools are always usable.
func (*Workspace) SearchTool ¶
func (w *Workspace) SearchTool() ds4.ToolHandler
SearchTool returns a tool that searches files and returns compact matches.
func (*Workspace) ViewImageTool ¶ added in v0.7.0
func (w *Workspace) ViewImageTool() ds4.ToolHandler
ViewImageTool returns view_image, upstream ds4-agent's tool for opening a local PNG or JPEG as a visual observation. Paths are confined like read, and the observation carries the bytes read once through that confined handle: returning a path would have the tool loop re-read the file outside the os.Root guard on every round, unbounded and mutable between rounds. Files over maxImageBytes are refused. Without a vision encoder it returns a text observation rather than an error, so the model can carry on, matching upstream.
func (*Workspace) WriteTool ¶
func (w *Workspace) WriteTool() ds4.ToolHandler
WriteTool returns a tool that creates or overwrites a text file.