From df07fa4617d0fbbc58d32f703058f53fceea8882 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Sat, 24 Feb 2024 11:44:08 +0800 Subject: [PATCH 1/2] fix: Improve path resolution - Use os.path.expanduser to correctly resolve user directory - Wrap command prompts return value in a list for consistency - Address issues with finding `prompt.txt` --- devchat/engine/router.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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( From 6bb77aa0c54f3eca3541307e7e819bed748dab92 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Sat, 24 Feb 2024 11:44:43 +0800 Subject: [PATCH 2/2] feat: Support @file@ syntax in prompt.txt parsing - Added import statements for re and os modules - Implemented logic to replace @file@ syntax with actual file content --- devchat/engine/recursive_prompter.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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