workspacetools

package
v2.39.1-notes-metadata-b Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: AGPL-3.0 Imports: 8 Imported by: 1 module, 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 workspacetools implements the workspace tool behavior shared by Coder Agents (coderd/x/chatd/chattool) and the Coder MCP server (codersdk/toolsdk). It talks to the workspace agent through workspacesdk.AgentConn and has no dependency on either tool framework, so both surfaces run the same commands and return the same result formats.

Index

Constants

View Source
const (
	// ExecuteCommandDescription describes the execute tool's command.
	ExecuteCommandDescription = `The shell command to execute. Runs under "sh -c" (POSIX).`
	// ExecuteTimeoutDescription describes the execute tool's timeout.
	ExecuteTimeoutDescription = "" /* 196-byte string literal not displayed */
	// ExecuteWorkDirDescription describes the execute tool's workdir.
	ExecuteWorkDirDescription = "Working directory for the command."
	// ProcessOutputWaitTimeoutDescription describes the process output
	// tool's wait_timeout.
	ProcessOutputWaitTimeoutDescription = "" /* 163-byte string literal not displayed */
	// EditFilesFilesDescription describes the edit files tool's files.
	EditFilesFilesDescription = "Files to edit. Every entry must include path and at least one edit."
	// WriteFileContentDescription describes the write file tool's content.
	WriteFileContentDescription = "Complete file contents. Replaces any existing contents."
	// ProcessListDescription describes the process list tool.
	ProcessListDescription = "List all tracked processes in the workspace. " +
		"Returns process IDs, commands, status (running or " +
		"exited), and exit codes. Use this to discover " +
		"processes or check which are still running."
	// ReadFileDescription describes the read file tool.
	ReadFileDescription = "Read a file from the workspace. Returns line-numbered content. " +
		"The offset parameter is a 1-based line number (default: 1). " +
		"The limit parameter is the maximum number of lines to return (default and maximum: 2000). " +
		"One response holds at most 32 KB of content, so it can stop before the limit. " +
		"If a response stops before the end of the file, its note gives the offset to read next. " +
		"To find specific content in a large file, search with grep first. " +
		"Files larger than 1 MB cannot be read with this tool."
	// EditFilesDescription describes the edit files tool.
	EditFilesDescription = "Perform edits on one or more files by replacing old_text with" +
		" new_text. Each entry in files must include the absolute path" +
		" of the file to edit and at least one edit. Matching is fuzzy" +
		" (tolerates whitespace and indentation differences) and preserves" +
		" the file's existing indentation and line endings. Errors if" +
		" old_text matches zero locations, or more than one unless" +
		" replace_all is set. All edits in a batch are validated before" +
		" any file is written."
)
View Source
const (
	// DefaultExecuteTimeout is how long a foreground command runs before
	// the caller gets a background process ID instead of the final result.
	DefaultExecuteTimeout = 10 * time.Second

	// DefaultProcessOutputWait is how long a process output request
	// blocks for the process to exit before returning output so far.
	DefaultProcessOutputWait = 10 * time.Second

	// MaxOutputBytes is the maximum command output returned to a model.
	MaxOutputBytes = 32 << 10 // 32KB

)

Variables

This section is empty.

Functions

func AgentAPIErrorMessage

func AgentAPIErrorMessage(err error) string

AgentAPIErrorMessage preserves the agent's actionable message while dropping the transport metadata (HTTP method, URL, status code) that codersdk.Error.Error() prefixes.

func ExecuteDescription

func ExecuteDescription(names ToolNames) string

ExecuteDescription describes the execute tool.

func ExecuteRunInBackgroundDescription

func ExecuteRunInBackgroundDescription(names ToolNames) string

ExecuteRunInBackgroundDescription describes the execute tool's run_in_background argument.

func ProcessOutputDescription

func ProcessOutputDescription(names ToolNames) string

ProcessOutputDescription describes the process output tool.

func ProcessSignalDescription

func ProcessSignalDescription(names ToolNames) string

ProcessSignalDescription describes the process signal tool.

func ValidateFileEdits

func ValidateFileEdits(files []workspacesdk.FileEdits) error

ValidateFileEdits trims each path in place and rejects batches the agent would otherwise apply partially or not at all.

func ValidateSignal

func ValidateSignal(signal string) error

ValidateSignal reports whether signal is one the process tools accept.

Types

type EditFilesResult

type EditFilesResult struct {
	Files []workspacesdk.FileEditResult `json:"files"`
	OK    bool                          `json:"ok"`
}

EditFilesResult is the structured success response from the edit files tool. Keep fields in alphabetical order: Coder Agents tool responses use sorted JSON keys.

func EditFiles

EditFiles applies validated edits and returns a per-file diff.

type ExecuteRequest

type ExecuteRequest struct {
	Command string
	// Timeout is the caller-supplied wait duration, such as "30s".
	// Nil uses DefaultTimeout.
	Timeout         *string
	WorkDir         string
	RunInBackground bool
	// DefaultTimeout overrides DefaultExecuteTimeout when positive.
	DefaultTimeout time.Duration
	// MaxTimeout, when positive, rejects longer foreground timeouts.
	MaxTimeout time.Duration
	// Env holds extra environment variables. The non-interactive
	// defaults take precedence over it.
	Env map[string]string
	// Names supplies the tool names used in follow-up hints.
	Names ToolNames
}

ExecuteRequest describes a command to run in the workspace.

type ExecuteResult

type ExecuteResult struct {
	Success             bool                            `json:"success"`
	Output              string                          `json:"output,omitempty"`
	ExitCode            int                             `json:"exit_code"`
	WallDurationMs      int64                           `json:"wall_duration_ms,omitempty"`
	Error               string                          `json:"error,omitempty"`
	Truncated           *workspacesdk.ProcessTruncation `json:"truncated,omitempty"`
	Note                string                          `json:"note,omitempty"`
	BackgroundProcessID string                          `json:"background_process_id,omitempty"`
	Command             string                          `json:"command,omitempty"`
	Running             bool                            `json:"running,omitempty"`
	Backgrounded        bool                            `json:"backgrounded,omitempty"`
	Canceled            bool                            `json:"canceled,omitempty"` // an interrupt canceled the command while it ran
}

ExecuteResult is the structured response from the execute and process output tools.

func Execute

Execute runs a command in the workspace through the agent's process API. A returned error means the request itself is invalid and nothing ran; every other outcome, including start failures and timeouts, is reported in the ExecuteResult.

func ExitedResult

ExitedResult converts the output of a finished process to a result.

func ProcessOutput

ProcessOutput retrieves a tracked process's output. A returned error means the request is invalid.

func StartErrorResult

func StartErrorResult(action string, err error) ExecuteResult

StartErrorResult reports a failed process start, adding remediation guidance when the workspace has no POSIX shell.

type OKResult

type OKResult struct {
	OK bool `json:"ok"`
}

OKResult is the structured success response from the write file tool.

type ProcessOutputRequest

type ProcessOutputRequest struct {
	ProcessID string
	// WaitTimeout is the caller-supplied wait duration, such as "30s".
	// Nil blocks for up to DefaultProcessOutputWait; "0s" returns a
	// snapshot without waiting.
	WaitTimeout *string
	// MaxWait, when positive, rejects longer wait timeouts.
	MaxWait time.Duration
}

ProcessOutputRequest describes a process output lookup.

type ReadFileResult

type ReadFileResult struct {
	Content    string `json:"content"`
	FileSize   int64  `json:"file_size"`
	LinesRead  int    `json:"lines_read"`
	Note       string `json:"note,omitempty"`
	TotalLines int    `json:"total_lines"`
}

ReadFileResult is the structured response from the read file tool. Keep fields in alphabetical order: Coder Agents tool responses use sorted JSON keys.

func ReadFile

func ReadFile(ctx context.Context, conn workspacesdk.AgentConn, path string, offset, limit *int64) (ReadFileResult, error)

ReadFile reads line-numbered content from a workspace file. offset is a 1-based line number (nil means 1) and limit is a line count (nil uses the agent default).

type SignalResult

type SignalResult struct {
	Message string `json:"message"`
	Success bool   `json:"success"`
}

SignalResult is the structured success response from the process signal tool. Keep fields in alphabetical order: Coder Agents tool responses use sorted JSON keys.

func SignalSent

func SignalSent(processID, signal string) SignalResult

SignalSent reports that signal was delivered to processID.

type ToolNames

type ToolNames struct {
	Execute       string
	ProcessOutput string
	ProcessList   string
	ReadFile      string
}

ToolNames are the names under which a tool surface registers the workspace tools. Descriptions and follow-up hints reference them so a model is always pointed at a tool that exists on its surface.

Jump to

Keyboard shortcuts

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