Files
coder/coderd/x/chatd/chattool/writefile.go
T
Michael Suchacz 1cf0354f72 feat: add plan mode with restricted tool boundary (#24236)
> This PR was authored by Mux on behalf of Mike.

## Summary
- add persistent plan mode for chats and the chat-specific plan file
flow
- add structured planning tools such as `ask_user_question` and
`propose_plan`
- keep `write_file` and `edit_files` constrained to the chat-specific
plan file during plan turns
- allow shell exploration in plan mode, including subagents, via
`execute` and `process_output`
- block implementation-oriented, provider-native, MCP, dynamic, and
computer-use tools during plan turns
- update the chat UI, tests, and docs for the new planning flow
2026-04-16 11:12:01 +02:00

87 lines
2.6 KiB
Go

package chattool
import (
"context"
"strings"
"charm.land/fantasy"
"github.com/coder/coder/v2/codersdk/workspacesdk"
)
type WriteFileOptions struct {
GetWorkspaceConn func(context.Context) (workspacesdk.AgentConn, error)
ResolvePlanPath func(context.Context) (chatPath string, home string, err error)
IsPlanTurn bool
}
type WriteFileArgs struct {
Path string `json:"path"`
Content string `json:"content"`
}
func WriteFile(options WriteFileOptions) fantasy.AgentTool {
return fantasy.NewAgentTool(
"write_file",
"Write a file to the workspace.",
func(ctx context.Context, args WriteFileArgs, _ fantasy.ToolCall) (fantasy.ToolResponse, error) {
var planPath string
if options.IsPlanTurn {
args.Path = strings.TrimSpace(args.Path)
resolvedPlanPath, err := resolvePlanTurnPath(ctx, options.ResolvePlanPath)
if err != nil {
return fantasy.NewTextErrorResponse(err.Error()), nil
}
if args.Path != resolvedPlanPath {
return fantasy.NewTextErrorResponse("during plan turns, write_file is restricted to " + resolvedPlanPath), nil
}
planPath = resolvedPlanPath
}
if options.GetWorkspaceConn == nil {
return fantasy.NewTextErrorResponse("workspace connection resolver is not configured"), nil
}
conn, err := options.GetWorkspaceConn(ctx)
if err != nil {
return fantasy.NewTextErrorResponse(err.Error()), nil
}
if planPath != "" {
if err := ensurePlanPathResolvesToItself(ctx, conn, planPath); err != nil {
return fantasy.NewTextErrorResponse(err.Error()), nil
}
}
return executeWriteFileTool(ctx, conn, args, options.ResolvePlanPath)
},
)
}
func executeWriteFileTool(
ctx context.Context,
conn workspacesdk.AgentConn,
args WriteFileArgs,
resolvePlanPath func(context.Context) (chatPath string, home string, err error),
) (fantasy.ToolResponse, error) {
requestedPath := strings.TrimSpace(args.Path)
if requestedPath == "" {
return fantasy.NewTextErrorResponse("path is required"), nil
}
hasPlanFileName := looksLikePlanFileName(requestedPath)
if hasPlanFileName && !isAbsolutePath(requestedPath) {
return fantasy.NewTextErrorResponse(
"plan files must use absolute paths; use the chat-specific absolute plan path",
), nil
}
if resolvePlanPath != nil && hasPlanFileName {
chatPath, home, err := resolvePlanPath(ctx)
if resp, rejected := rejectSharedPlanPath(requestedPath, home, chatPath, err); rejected {
return resp, nil
}
}
if err := conn.WriteFile(ctx, requestedPath, strings.NewReader(args.Content)); err != nil {
return fantasy.NewTextErrorResponse(err.Error()), nil
}
return toolResponse(map[string]any{"ok": true}), nil
}