diff --git a/devchat/engine/recursive_prompter.py b/devchat/engine/recursive_prompter.py index 8bf3ff04..e5150a09 100644 --- a/devchat/engine/recursive_prompter.py +++ b/devchat/engine/recursive_prompter.py @@ -1,3 +1,5 @@ +import re +import os from .namespace import Namespace @@ -13,6 +15,23 @@ def run(self, name: str) -> str: file_path = self.namespace.get_file(ancestor_name, 'prompt.txt') if file_path: with open(file_path, 'r', encoding='utf-8') as file: - merged_content += file.read() + prompt_content = file.read() + # replace @file@ with the content of the file + prompt_content = self._replace_file_references(file_path, prompt_content) + merged_content += prompt_content merged_content += '\n' + return merged_content + + def _replace_file_references(self, prompt_file_path: str, content: str) -> str: + # prompt_file_path is the path to the file that contains the content + # @relative file path@: file is relative to the prompt_file_path + pattern = re.compile(r'@(.+?)@') + matches = pattern.findall(content) + for match in matches: + file_path = os.path.join(os.path.dirname(prompt_file_path), match) + if os.path.exists(file_path): + with open(file_path, 'r', encoding='utf-8') as file: + file_content = file.read() + content = content.replace(f'@{match}@', file_content) + return content diff --git a/devchat/engine/router.py b/devchat/engine/router.py index 1caa8358..786f7c49 100644 --- a/devchat/engine/router.py +++ b/devchat/engine/router.py @@ -12,7 +12,7 @@ def load_workflow_instruction(user_input: str): if user_input[:1] != '/': return None - workflows_dir = os.path.join(os.path.abspath('~/.chat'), 'workflows') + workflows_dir = os.path.join(os.path.expanduser('~/.chat'), 'workflows') if not os.path.exists(workflows_dir): return None if not os.path.isdir(workflows_dir): @@ -24,7 +24,7 @@ def load_workflow_instruction(user_input: str): command_name = user_input.split()[0][1:] command_prompts = prompter.run(command_name) - return command_prompts + return [command_prompts] def run_command(