Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,7 @@ notebooks/generated_image.png
.auto-coder/
/actions/
/output.txt
/examples/
/examples/
.auto-coder/
/actions/
/output.txt
32 changes: 32 additions & 0 deletions src/autocoder/common/ac_style_command_parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

# 将查询字符串按命令分割
Expand Down Expand Up @@ -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]:
Expand Down
26 changes: 25 additions & 1 deletion src/autocoder/common/ac_style_command_parser/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -471,7 +495,7 @@ def test__command_with_path(self, parser):
}
}
}
assert result == expected
assert result == expected


# 参数化测试用例
Expand Down