|
| 1 | +# write_to_file |
| 2 | + |
| 3 | +The `write_to_file` tool creates new files or completely replaces existing file content with an interactive approval process. It provides a diff view for reviewing changes before they're applied. |
| 4 | + |
| 5 | +## Parameters |
| 6 | + |
| 7 | +The tool accepts these parameters: |
| 8 | + |
| 9 | +- `path` (required): The path of the file to write to, relative to the current working directory |
| 10 | +- `content` (required): The complete content to write to the file |
| 11 | +- `line_count` (required): The number of lines in the file, including empty lines |
| 12 | + |
| 13 | +## What It Does |
| 14 | + |
| 15 | +This tool writes content to a specified file, either creating a new file if it doesn't exist or completely overwriting an existing file. All changes require explicit user approval through a diff view interface, where users can review and even edit the proposed changes before they're applied. |
| 16 | + |
| 17 | +## When is it used? |
| 18 | + |
| 19 | +- When Roo needs to create a new file from scratch |
| 20 | +- When Roo needs to completely rewrite an existing file |
| 21 | +- When creating multiple files for a new project |
| 22 | +- When generating configuration files, documentation, or source code |
| 23 | +- When you need to review changes before they're applied |
| 24 | + |
| 25 | +## Key Features |
| 26 | + |
| 27 | +- Interactive Approval: Shows changes in a diff view requiring explicit approval before applying |
| 28 | +- User Edit Support: Allows editing the proposed content before final approval |
| 29 | +- Safety Measures: Detects code omission, validates paths, and prevents truncated content |
| 30 | +- Editor Integration: Opens a diff view that scrolls to the first difference automatically |
| 31 | +- Content Preprocessing: Handles artifacts from different AI models to ensure clean content |
| 32 | +- Access Control: Validates against `.rooignore` restrictions before making changes |
| 33 | +- Parent Directories: May handle directory creation through system dependencies |
| 34 | +- Complete Replacement: Provides a fully transformed file in a single operation |
| 35 | + |
| 36 | +## Limitations |
| 37 | + |
| 38 | +- Not suitable for existing files: Much slower and less efficient than `apply_diff` for modifying existing files |
| 39 | +- Performance with large files: Operation becomes significantly slower with larger files |
| 40 | +- Complete overwrite: Replaces entire file content, cannot preserve original content |
| 41 | +- Line count required: Needs accurate line count to detect potential content truncation |
| 42 | +- Review overhead: The approval process adds extra steps compared to direct edits |
| 43 | +- Interactive only: Cannot be used in automated workflows that require non-interactive execution |
| 44 | + |
| 45 | +## How It Works |
| 46 | + |
| 47 | +When the `write_to_file` tool is invoked, it follows this process: |
| 48 | + |
| 49 | +1. **Parameter Validation**: Validates the required parameters and permissions |
| 50 | + - Checks that `path`, `content`, and `line_count` are provided |
| 51 | + - Validates the file is allowed (not restricted by `.rooignore`) |
| 52 | + - Ensures the path is within the workspace boundaries |
| 53 | + - Tracks consecutive mistake counts for missing parameters |
| 54 | + - Shows specific error messages for each validation failure |
| 55 | + |
| 56 | +2. **Content Preprocessing**: |
| 57 | + - Removes code block markers that might be added by AI models |
| 58 | + - Handles escaped HTML entities (specifically for non-Claude models) |
| 59 | + - Strips line numbers if accidentally included in content |
| 60 | + - Performs model-specific processing for different AI providers |
| 61 | + |
| 62 | +3. **Diff View Generation**: |
| 63 | + - Opens a diff view in the editor showing the proposed changes |
| 64 | + - Adds a 300ms delay to ensure UI responsiveness |
| 65 | + - Scrolls automatically to the first difference |
| 66 | + - Highlights changes for easy review |
| 67 | + |
| 68 | +4. **User Approval Process**: |
| 69 | + - Waits for explicit user approval to proceed |
| 70 | + - Allows users to edit the content in the diff view |
| 71 | + - Captures any user edits for the final content |
| 72 | + - Provides option to reject changes entirely |
| 73 | + - Detects and incorporates user modifications into the final result |
| 74 | + |
| 75 | +5. **Safety Validation**: |
| 76 | + - Detects potential content truncation by comparing with provided line count |
| 77 | + - Shows warnings if content appears incomplete |
| 78 | + - Validates file path and access permissions |
| 79 | + - Specifically checks if files are outside the workspace with `isOutsideWorkspace` flag |
| 80 | + |
| 81 | +6. **File Writing**: |
| 82 | + - Writes the approved content (with any user edits) to the file |
| 83 | + - Provides confirmation of successful write |
| 84 | + - Resets the consecutive mistakes counter on success |
| 85 | + |
| 86 | +## Examples When Used |
| 87 | + |
| 88 | +- When creating a new project, Roo generates multiple files but lets you review each before committing changes. |
| 89 | +- When setting up configuration files, Roo shows the proposed configuration in a diff view for approval. |
| 90 | +- When generating documentation, Roo creates markdown files but lets you make final adjustments in the diff view. |
| 91 | +- When developing a prototype, Roo shows complete source files in a diff view where you can fine-tune before saving. |
| 92 | + |
| 93 | +## Usage Examples |
| 94 | + |
| 95 | +Creating a new JSON configuration file: |
| 96 | +``` |
| 97 | +<write_to_file> |
| 98 | +<path>config/settings.json</path> |
| 99 | +<content> |
| 100 | +{ |
| 101 | + "apiEndpoint": "https://api.example.com", |
| 102 | + "theme": { |
| 103 | + "primaryColor": "#007bff", |
| 104 | + "secondaryColor": "#6c757d", |
| 105 | + "fontFamily": "Arial, sans-serif" |
| 106 | + }, |
| 107 | + "features": { |
| 108 | + "darkMode": true, |
| 109 | + "notifications": true, |
| 110 | + "analytics": false |
| 111 | + }, |
| 112 | + "version": "1.0.0" |
| 113 | +} |
| 114 | +</content> |
| 115 | +<line_count>14</line_count> |
| 116 | +</write_to_file> |
| 117 | +``` |
| 118 | + |
| 119 | +Creating a simple HTML file: |
| 120 | +``` |
| 121 | +<write_to_file> |
| 122 | +<path>src/index.html</path> |
| 123 | +<content> |
| 124 | +<!DOCTYPE html> |
| 125 | +<html lang="en"> |
| 126 | +<head> |
| 127 | + <meta charset="UTF-8"> |
| 128 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 129 | + <title>My Application</title> |
| 130 | + <link rel="stylesheet" href="styles.css"> |
| 131 | +</head> |
| 132 | +<body> |
| 133 | + <div id="app"></div> |
| 134 | + <script src="app.js"></script> |
| 135 | +</body> |
| 136 | +</html> |
| 137 | +</content> |
| 138 | +<line_count>13</line_count> |
| 139 | +</write_to_file> |
| 140 | +``` |
| 141 | + |
| 142 | +Creating a JavaScript module: |
| 143 | +``` |
| 144 | +<write_to_file> |
| 145 | +<path>src/utils/helpers.js</path> |
| 146 | +<content> |
| 147 | +/** |
| 148 | + * Utility functions for the application |
| 149 | + */ |
| 150 | +
|
| 151 | +export function formatDate(date) { |
| 152 | + return new Date(date).toLocaleDateString(); |
| 153 | +} |
| 154 | +
|
| 155 | +export function calculateTotal(items) { |
| 156 | + return items.reduce((sum, item) => sum + item.price, 0); |
| 157 | +} |
| 158 | +
|
| 159 | +export function debounce(func, delay) { |
| 160 | + let timeout; |
| 161 | + return function(...args) { |
| 162 | + clearTimeout(timeout); |
| 163 | + timeout = setTimeout(() => func.apply(this, args), delay); |
| 164 | + }; |
| 165 | +} |
| 166 | +</content> |
| 167 | +<line_count>18</line_count> |
| 168 | +</write_to_file> |
| 169 | +``` |
0 commit comments