diff --git a/README.md b/README.md index 208e614..5219c27 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,7 @@ flowchart TD | ------------ | --------------------------------------------------------- | | `readFile` | Reads the contents of a file from disk | | `writeFile` | Writes or overwrites a file on disk | +| `appendFile` | Appends content to a file on disk. | | `deleteFile` | Deletes a file from disk | | `listDir` | Lists all files in a directory | | `fetchURL` | Fetches the content of a URL and returns it as plain text | diff --git a/src/agent.js b/src/agent.js index 2532969..d1f43dd 100644 --- a/src/agent.js +++ b/src/agent.js @@ -29,6 +29,7 @@ const SYSTEM_PROMPT = ` TOOL {"name":"listDir","args":{"path":"."}}-{"Reason":"user wants folder content."} TOOL {"name":"readFile","args":{"path":"package.json"}}-{"Reason":"user wants file conetnt."} TOOL {"name":"writeFile","args":{"path":"test.txt","content":"hello"}}-{"Reason":"user want to write in teh file."} + TOOL {"name":"appendFile","args":{"path":"test.txt","content":"hello"}}-{"Reason":"user wants to append content to the file."} TOOL {"name":"deleteFile","args":{"path":"test.txt"}}-{"Reason":"user wants to delete the file."} TOOL {"name":"fetchURL","args":{"url":"https://example.com"}}-{"Reason":"user asks about website content."} TOOL {"name":"runCommand","args":{"command":"git status"}}-{"Reason":"user wants to inspect the repository state."} diff --git a/src/functions.js b/src/functions.js index bba63ef..8b1fc64 100644 --- a/src/functions.js +++ b/src/functions.js @@ -47,6 +47,23 @@ function writeFile(path, content) { } } +function appendFile(path, content) { + try { + fs.appendFileSync(path, content, "utf8"); + return "Content appended successfully to the file"; + } catch (error) { + if (error.code === "EISDIR") { + return `"${path}" is a directory, not a file.`; + } + + if (error.code === "ENOENT") { + return `Content could not be appended to file "${path}" because its directory does not exist.`; + } + + throw error; + } +} + function deleteFile(path) { try { fs.unlinkSync(path); @@ -136,11 +153,4 @@ function runCommand(command) { } } -module.exports = { - readFile, - writeFile, - deleteFile, - listDir, - fetchURL, - runCommand, -}; +module.exports = { readFile, writeFile, appendFile, deleteFile, listDir, fetchURL, runCommand }; diff --git a/src/tools.js b/src/tools.js index 7c5b812..64bb54e 100644 --- a/src/tools.js +++ b/src/tools.js @@ -26,6 +26,20 @@ const tools = { }, func: funcs.writeFile, }, + appendFile: { + description: "Appends content to the end of an existing file on the disk.", + parameters: { + path: { + type: "STRING", + description: "The path of the file in the disk, e.g. ~/home/resume.txt", + }, + content: { + type: "STRING", + description: "The content to append to the file, e.g. Hello, World!", + }, + }, + func: funcs.appendFile, +}, deleteFile: { description: "Deletes a file from the disk.", parameters: {