MCPcopy Create free account
hub / github.com/Stevenic/codepilot / addCreateFile

Function addCreateFile

src/functions/createFile.ts:32–57  ·  view source on GitHub ↗
(codepilot: Codepilot)

Source from the content-addressed store, hash-verified

30 * Adds the createFile function to the codepilot instance.
31 */
32export function addCreateFile(codepilot: Codepilot): void {
33 codepilot.addFunction(createFileFunction, async (args: any) => {
34 const { filePath, contents } = args;
35
36 // Check if the file already exists
37 if (await fs.access(path.join(process.cwd(), filePath)).then(() => true).catch(() => false)) {
38 return `A file already exists at that path.\nGive the user detailed instructions for how they should modify that file instead.`;
39 }
40
41 try {
42 // Create the directory path if it doesn't exist
43 const directoryPath = path.dirname(filePath);
44 await fs.mkdir(directoryPath, { recursive: true });
45
46 // Write the code to the file
47 await fs.writeFile(path.join(process.cwd(), filePath), contents);
48
49 // Add the file to the code index
50 await codepilot.index.upsertDocument(filePath);
51 console.log(Colorize.highlight(`Created a new file: ${filePath}`));
52 return `Successfully created file at ${filePath}`;
53 } catch (error) {
54 return `Failed to create file at ${filePath} due to the following error:\n${(error as Error).message}`;
55 }
56 });
57}

Callers 1

registerFunctionsFunction · 0.90

Calls 3

addFunctionMethod · 0.45
upsertDocumentMethod · 0.45
highlightMethod · 0.45

Tested by

no test coverage detected