smol-ai / developer

the first library to let you embed a developer agent in your own app!
https://twitter.com/SmolModels
MIT License
11.76k stars 1.03k forks source link

FIX for codeblock break out and filename #107

Open Wade-BuildOtto opened 1 year ago

Wade-BuildOtto commented 1 year ago

The codeblock is not broken out correctly this is the fix I added

` import re def write_file(filename, filecode, directory):

Output the filename in blue color

print("\033[94m" + filename + "\033[0m")
print(filecode)

regex = r"```(.*?)```"
matches = re.finditer(regex, filecode, re.DOTALL)
code = filecode

if matches:
    for match in matches:
        # Get the code
        code = match.group(1).split("\n")[1:]
        code = "\n".join(code)
        break

file_path = os.path.join(directory, filename)

# Check if the filename is actually a directory
if os.path.isdir(file_path):
    print(f"Error: {filename} is a directory, not a file.")
    return

# Open the file in write mode
with open(file_path, "w") as file:
    # Write content to the file
    file.write(code)

print("File written successfully.")

`

also lines 127 & 128 by adding that the list will be evaluated by ast it knows better how to deliver the list, "found on another issue" otherwise the list starts with "- " dash space only list the filepaths you would write, and return them as a python list of strings to be evaluated by the ast.literal_eval function. This is important: do not add any other explanation or notes, only return a python list of strings.

adding these 2 fixed make the code fox

Wade-BuildOtto commented 1 year ago

given that some projects need to create a folder structure

def write_file(filename, filecode, directory): filepath = os.path.dirname(filename) filename = os.path.basename(filename) directory = os.path.join(directory, filepath)

Output the filename in blue color

print("\033[94m" + filename + "\033[0m")
print("\033[94m" + directory + "\033[0m")
print(filecode)

regex = r"```(.*?)```"
matches = re.finditer(regex, filecode, re.DOTALL)
code = filecode

if matches:
    for match in matches:
        # Get the code
        code = match.group(1).split("\n")[1:]
        code = "\n".join(code)
        break

file_path = os.path.join(directory, filename)

# Create directory if it does not exist
if not os.path.exists(directory):
    os.makedirs(directory)

# Check if the filename is actually a directory
if os.path.isdir(file_path):
    print(f"Error: {filename} is a directory, not a file.")
    return

# Open the file in write mode
with open(file_path, "w") as file:
    # Write content to the file
    file.write(code)