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