gpt-engineer / chat_to_files.py
jensinjames's picture
Upload 9 files
66214f3
import re
def parse_chat(chat): # -> List[Tuple[str, str]]:
# Get all ``` blocks and preceding filenames
regex = r"(\S+)\n\s*```[^\n]*\n(.+?)```"
matches = re.finditer(regex, chat, re.DOTALL)
files = []
for match in matches:
# Strip the filename of any non-allowed characters and convert / to \
path = re.sub(r'[<>"|?*]', "", match.group(1))
# Remove leading and trailing brackets
path = re.sub(r"^\[(.*)\]$", r"\1", path)
# Remove leading and trailing backticks
path = re.sub(r"^`(.*)`$", r"\1", path)
# Remove trailing ]
path = re.sub(r"\]$", "", path)
# Get the code
code = match.group(2)
# Add the file to the list
files.append((path, code))
# Get all the text before the first ``` block
readme = chat.split("```")[0]
files.append(("README.md", readme))
# Return the files
return files
def to_files(chat, workspace):
workspace["all_output.txt"] = chat
files = parse_chat(chat)
for file_name, file_content in files:
workspace[file_name] = file_content