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
- func AgentAPIErrorMessage(err error) string
- func ExecuteDescription(names ToolNames) string
- func ExecuteRunInBackgroundDescription(names ToolNames) string
- func ProcessOutputDescription(names ToolNames) string
- func ProcessSignalDescription(names ToolNames) string
- func ValidateFileEdits(files []workspacesdk.FileEdits) error
- func ValidateSignal(signal string) error
- type EditFilesResult
- type ExecuteRequest
- type ExecuteResult
- func Execute(ctx context.Context, conn workspacesdk.AgentConn, req ExecuteRequest) (ExecuteResult, error)
- func ExitedResult(resp workspacesdk.ProcessOutputResponse) ExecuteResult
- func ProcessOutput(ctx context.Context, conn workspacesdk.AgentConn, req ProcessOutputRequest) (ExecuteResult, error)
- func StartErrorResult(action string, err error) ExecuteResult
- type OKResult
- type ProcessOutputRequest
- type ReadFileResult
- type SignalResult
- type ToolNames
Constants ¶
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." )
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 ¶
AgentAPIErrorMessage preserves the agent's actionable message while dropping the transport metadata (HTTP method, URL, status code) that codersdk.Error.Error() prefixes.
func ExecuteDescription ¶
ExecuteDescription describes the execute tool.
func ExecuteRunInBackgroundDescription ¶
ExecuteRunInBackgroundDescription describes the execute tool's run_in_background argument.
func ProcessOutputDescription ¶
ProcessOutputDescription describes the process output tool.
func ProcessSignalDescription ¶
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 ¶
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 ¶
func EditFiles(ctx context.Context, conn workspacesdk.AgentConn, files []workspacesdk.FileEdits) (EditFilesResult, error)
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 ¶
func Execute(ctx context.Context, conn workspacesdk.AgentConn, req ExecuteRequest) (ExecuteResult, error)
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 ¶
func ExitedResult(resp workspacesdk.ProcessOutputResponse) ExecuteResult
ExitedResult converts the output of a finished process to a result.
func ProcessOutput ¶
func ProcessOutput(ctx context.Context, conn workspacesdk.AgentConn, req ProcessOutputRequest) (ExecuteResult, error)
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 ¶
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.