| """ | |
| 文本清理核心逻辑 - 供 mcp.py 和 api.py 共用 | |
| """ | |
| import re | |
| async def do_clean(text: str, operations: list) -> dict: | |
| """执行文本清理操作。 | |
| Args: | |
| text: 待清理的文本 | |
| operations: 清理操作列表(trim、remove_empty_lines、normalize_spaces等) | |
| Returns: | |
| 包含清理结果的字典 | |
| """ | |
| result = text | |
| for op in operations: | |
| if op == "trim": | |
| result = result.strip() | |
| elif op == "remove_empty_lines": | |
| result = re.sub(r'\n\s*\n', '\n', result) | |
| elif op == "normalize_spaces": | |
| result = re.sub(r'[ \t]+', ' ', result) | |
| elif op == "remove_extra_newlines": | |
| result = re.sub(r'\n+', '\n', result) | |
| return { | |
| "success": True, | |
| "output": result, | |
| "message": f"已执行 {len(operations)} 个清理操作" | |
| } |