(input any, toolUseData *uctypes.UIMessageDataToolUse)
| 131 | } |
| 132 | |
| 133 | func writeTextFileCallback(input any, toolUseData *uctypes.UIMessageDataToolUse) (any, error) { |
| 134 | params, err := parseWriteTextFileInput(input) |
| 135 | if err != nil { |
| 136 | return nil, err |
| 137 | } |
| 138 | |
| 139 | expandedPath, err := wavebase.ExpandHomeDir(params.Filename) |
| 140 | if err != nil { |
| 141 | return nil, fmt.Errorf("failed to expand path: %w", err) |
| 142 | } |
| 143 | |
| 144 | if !filepath.IsAbs(expandedPath) { |
| 145 | return nil, fmt.Errorf("path must be absolute, got relative path: %s", params.Filename) |
| 146 | } |
| 147 | |
| 148 | contentsBytes := []byte(params.Contents) |
| 149 | if utilfn.HasBinaryData(contentsBytes) { |
| 150 | return nil, fmt.Errorf("contents appear to contain binary data") |
| 151 | } |
| 152 | |
| 153 | fileInfo, err := validateTextFile(expandedPath, "write to", false) |
| 154 | if err != nil { |
| 155 | return nil, err |
| 156 | } |
| 157 | |
| 158 | dirPath := filepath.Dir(expandedPath) |
| 159 | err = os.MkdirAll(dirPath, 0755) |
| 160 | if err != nil { |
| 161 | return nil, fmt.Errorf("failed to create directory: %w", err) |
| 162 | } |
| 163 | |
| 164 | if fileInfo != nil { |
| 165 | backupPath, err := filebackup.MakeFileBackup(expandedPath) |
| 166 | if err != nil { |
| 167 | return nil, fmt.Errorf("failed to create backup: %w", err) |
| 168 | } |
| 169 | toolUseData.WriteBackupFileName = backupPath |
| 170 | } |
| 171 | |
| 172 | err = os.WriteFile(expandedPath, contentsBytes, 0644) |
| 173 | if err != nil { |
| 174 | return nil, fmt.Errorf("failed to write file: %w", err) |
| 175 | } |
| 176 | |
| 177 | return map[string]any{ |
| 178 | "success": true, |
| 179 | "message": fmt.Sprintf("Successfully wrote %s (%d bytes)", params.Filename, len(contentsBytes)), |
| 180 | }, nil |
| 181 | } |
| 182 | |
| 183 | func GetWriteTextFileToolDefinition() uctypes.ToolDefinition { |
| 184 | return uctypes.ToolDefinition{ |
nothing calls this directly
no test coverage detected