(args, extras)
| 7 | import { ContinueError, ContinueErrorReason } from "../../util/errors"; |
| 8 | |
| 9 | export const createNewFileImpl: ToolImpl = async (args, extras) => { |
| 10 | const filepath = getStringArg(args, "filepath"); |
| 11 | const contents = getStringArg(args, "contents", true); |
| 12 | |
| 13 | const resolvedFileUri = await inferResolvedUriFromRelativePath( |
| 14 | filepath, |
| 15 | extras.ide, |
| 16 | ); |
| 17 | if (resolvedFileUri) { |
| 18 | throwIfFileIsSecurityConcern(getCleanUriPath(resolvedFileUri)); |
| 19 | const exists = await extras.ide.fileExists(resolvedFileUri); |
| 20 | if (exists) { |
| 21 | throw new ContinueError( |
| 22 | ContinueErrorReason.FileAlreadyExists, |
| 23 | `File ${filepath} already exists. Use the edit tool to edit this file`, |
| 24 | ); |
| 25 | } |
| 26 | await extras.ide.writeFile(resolvedFileUri, contents); |
| 27 | await extras.ide.openFile(resolvedFileUri); |
| 28 | await extras.ide.saveFile(resolvedFileUri); |
| 29 | if (extras.codeBaseIndexer) { |
| 30 | void extras.codeBaseIndexer?.refreshCodebaseIndexFiles([resolvedFileUri]); |
| 31 | } |
| 32 | return [ |
| 33 | { |
| 34 | name: getUriPathBasename(resolvedFileUri), |
| 35 | description: getCleanUriPath(resolvedFileUri), |
| 36 | content: "File created successfuly", |
| 37 | uri: { |
| 38 | type: "file", |
| 39 | value: resolvedFileUri, |
| 40 | }, |
| 41 | }, |
| 42 | ]; |
| 43 | } else { |
| 44 | throw new ContinueError( |
| 45 | ContinueErrorReason.PathResolutionFailed, |
| 46 | "Failed to resolve path", |
| 47 | ); |
| 48 | } |
| 49 | }; |
no test coverage detected