Up until now, our program has been read-only... now it's getting really dangerous fun! We'll give our agent the ability to write and overwrite files, within strict limits.
def write_file(working_directory, file_path, content):
f'Error: Cannot write to "{file_path}" as it is outside the permitted working directory'
f'Successfully wrote to "{file_path}" ({len(content)} characters written)'
It's important to return a success string so that our LLM knows that the action it took actually worked. Feedback loops, feedback loops, feedback loops!
write_file("calculator", "lorem.txt", "wait, this isn't lorem ipsum")write_file("calculator", "pkg/morelorem.txt", "lorem ipsum dolor sit amet")write_file("calculator", "/tmp/temp.txt", "this should not be allowed")Run and submit the CLI tests.
os.path.exists: Check if a path existsos.makedirs: Create a directory, along with any necessary parent directoriesos.path.dirname: Return the directory nameExample of writing to a file:
with open(file_path, "w") as f:
f.write(content)