| 21 | } |
| 22 | |
| 23 | func WriteFile(options WriteFileOptions) fantasy.AgentTool { |
| 24 | return fantasy.NewAgentTool( |
| 25 | "write_file", |
| 26 | "Write a file to the workspace.", |
| 27 | func(ctx context.Context, args WriteFileArgs, _ fantasy.ToolCall) (fantasy.ToolResponse, error) { |
| 28 | var planPath string |
| 29 | if options.IsPlanTurn { |
| 30 | args.Path = strings.TrimSpace(args.Path) |
| 31 | resolvedPlanPath, err := resolvePlanTurnPath(ctx, options.ResolvePlanPath) |
| 32 | if err != nil { |
| 33 | return fantasy.NewTextErrorResponse(err.Error()), nil |
| 34 | } |
| 35 | if args.Path != resolvedPlanPath { |
| 36 | return fantasy.NewTextErrorResponse("during plan turns, write_file is restricted to " + resolvedPlanPath), nil |
| 37 | } |
| 38 | planPath = resolvedPlanPath |
| 39 | } |
| 40 | if options.GetWorkspaceConn == nil { |
| 41 | return fantasy.NewTextErrorResponse("workspace connection resolver is not configured"), nil |
| 42 | } |
| 43 | conn, err := options.GetWorkspaceConn(ctx) |
| 44 | if err != nil { |
| 45 | return fantasy.NewTextErrorResponse(err.Error()), nil |
| 46 | } |
| 47 | if planPath != "" { |
| 48 | if err := ensurePlanPathResolvesToItself(ctx, conn, planPath); err != nil { |
| 49 | return fantasy.NewTextErrorResponse(err.Error()), nil |
| 50 | } |
| 51 | } |
| 52 | return executeWriteFileTool(ctx, conn, args, options.ResolvePlanPath) |
| 53 | }, |
| 54 | ) |
| 55 | } |
| 56 | |
| 57 | func executeWriteFileTool( |
| 58 | ctx context.Context, |