Retries and replica takeovers ran tool calls again, and an interrupt recorded the same generic result whether a call had finished, was still running, or never started. chatd now derives a tool call ID from the chat, the committed assistant message, and the provider tool call ID, and sends it with execute starts, edit_files, and write_file. Every replica and retry computes the same ID, so the agent returns the first result instead of running the tool again, and a retried foreground execute waits only for the rest of its timeout. On interrupt, chatd cancels the unresolved calls of those tools on the agent before committing, within 30 seconds, and records what happened: "tool call was canceled before it ran; no changes were made", the output of a command canceled while running, or the real result. The UI shows a canceled command with the existing killed marker. If the cancel fails, including an old agent's 404, the result stays the generic interrupted text. Calls whose provider tool call ID is empty or repeats in the message get no ID and behave as before. Fixes CODAGT-757
96 lines
3.2 KiB
Go
96 lines
3.2 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" description:"Absolute path of the file to write. Plan files must use the chat-specific absolute plan path."`
|
|
Content string `json:"content" description:"Complete file contents. Replaces any existing contents."`
|
|
}
|
|
|
|
// WriteFileToolName is the registered name of the write_file tool.
|
|
const WriteFileToolName = "write_file"
|
|
|
|
func WriteFile(options WriteFileOptions) fantasy.AgentTool {
|
|
return fantasy.NewAgentTool(
|
|
WriteFileToolName,
|
|
"Create a file in the workspace or overwrite an existing one with the given content. "+
|
|
"Use edit_files for targeted changes to an existing file. "+
|
|
"During plan turns, only the chat-specific plan file path is writable.",
|
|
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
|
|
}
|
|
}
|
|
|
|
return writeFileResponse(conn.WriteFile(ctx, requestedPath, strings.NewReader(args.Content))), nil
|
|
}
|
|
|
|
func writeFileResponse(err error) fantasy.ToolResponse {
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(err.Error())
|
|
}
|
|
return toolResponse(map[string]any{"ok": true})
|
|
}
|