From d8b9635ef2674e2ed785bc8773f6123eb41eb0c8 Mon Sep 17 00:00:00 2001 From: WilliamZhu Date: Tue, 24 Jun 2025 17:50:47 +0800 Subject: [PATCH] =?UTF-8?q?/new=20=E5=9C=A8=20.auto-coder/targets=20?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E4=B8=8B=E7=94=9F=E6=88=90=E5=A6=82=E4=B8=8B?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E4=BB=A3=E7=A0=81=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ```python from autocoder.common.ac_style_command_parser.parser import CommandParser p = CommandParser() result = p.parse("""/command "tdd/hello.md" name='''威廉'''""") print(result) ``` 然后运行该代码,我们期待的运行结果是这样的: {'command': {'args': ['tdd/hello.md'], 'kwargs': {'name':'威廉'}}} 也就是我们希望能够支持解析 '''''' 引用起来的文本。 运行上面的python代码,直到运行结果符合我们预期。 auto_coder_000000000001_chat_action.yml --- .gitignore | 5 ++- .../common/ac_style_command_parser/parser.py | 32 +++++++++++++++++++ .../ac_style_command_parser/test_parser.py | 26 ++++++++++++++- 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index f61a6796..526b5359 100644 --- a/.gitignore +++ b/.gitignore @@ -74,4 +74,7 @@ notebooks/generated_image.png .auto-coder/ /actions/ /output.txt -/examples/ \ No newline at end of file +/examples/ +.auto-coder/ +/actions/ +/output.txt \ No newline at end of file diff --git a/src/autocoder/common/ac_style_command_parser/parser.py b/src/autocoder/common/ac_style_command_parser/parser.py index f3bc731f..af303698 100644 --- a/src/autocoder/common/ac_style_command_parser/parser.py +++ b/src/autocoder/common/ac_style_command_parser/parser.py @@ -63,7 +63,22 @@ def parse(self, query: str) -> Dict[str, Any]: # 找出所有命令 commands = re.findall(self.command_pattern, processed_query) + + # 如果没有找到命令,但有内容,则将整个字符串作为参数处理,使用空字符串作为命令名 if not commands: + # 恢复路径参数的原始值 + for placeholder, path in placeholders.items(): + query = query.replace(placeholder, path) + + # 解析参数 + args, kwargs = self._parse_params(query.strip()) + if args or kwargs: # 只有当有参数时才返回结果 + return { + "": { + 'args': args, + 'kwargs': kwargs + } + } return {} # 将查询字符串按命令分割 @@ -180,6 +195,23 @@ def parse_command(self, query: str, command: str) -> Optional[Dict[str, Any]]: """ commands = self.parse(query) return commands.get(command) + + def parse_params_only(self, params_str: str) -> Dict[str, Any]: + """ + 直接解析参数字符串,不需要命令前缀。 + 用于解析类似 '"tdd/hello.md" name="威廉"' 这样的参数字符串。 + + 参数: + params_str: 参数字符串 + + 返回: + Dict[str, Any]: 包含args和kwargs的字典 + """ + if not params_str or not params_str.strip(): + return {'args': [], 'kwargs': {}} + + args, kwargs = self._parse_params(params_str.strip()) + return {'args': args, 'kwargs': kwargs} def parse_query(query: str) -> Dict[str, Any]: diff --git a/src/autocoder/common/ac_style_command_parser/test_parser.py b/src/autocoder/common/ac_style_command_parser/test_parser.py index f9b103a8..3aab1b51 100644 --- a/src/autocoder/common/ac_style_command_parser/test_parser.py +++ b/src/autocoder/common/ac_style_command_parser/test_parser.py @@ -243,6 +243,30 @@ def test_command_at_end_of_string(self, parser): } } assert result == expected + + def test_parse_params_without_command(self, parser): + """测试解析不带命令前缀的参数""" + result = parser.parse('"tdd/hello.md" name="威廉"') + expected = { + "": { + "args": ["tdd/hello.md"], + "kwargs": { + "name": "威廉" + } + } + } + assert result == expected + + def test_parse_params_only_method(self, parser): + """测试parse_params_only方法""" + result = parser.parse_params_only('"tdd/hello.md" name="威廉"') + expected = { + "args": ["tdd/hello.md"], + "kwargs": { + "name": "威廉" + } + } + assert result == expected class TestConvenienceFunctions: @@ -471,7 +495,7 @@ def test__command_with_path(self, parser): } } } - assert result == expected + assert result == expected # 参数化测试用例