mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-07-02 21:58:11 +00:00
fix: remove unused _DUPLICATE_SEPARATOR_PATTERN, cache compiled regex in slugify
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
# https://github.com/un33k/python-slugify
|
||||
# MIT License
|
||||
|
||||
import functools
|
||||
import hashlib
|
||||
import re
|
||||
from typing import Any, Final
|
||||
@@ -13,11 +14,15 @@ _QUOTE_PATTERN: Final[re.Pattern[str]] = re.compile(r"[\'\"]+")
|
||||
_CAMEL_LOWER_UPPER: Final[re.Pattern[str]] = re.compile(r"([a-z])([A-Z])")
|
||||
_CAMEL_UPPER_LOWER: Final[re.Pattern[str]] = re.compile(r"([A-Z]+)([A-Z][a-z])")
|
||||
_DISALLOWED_CHARS_PATTERN: Final[re.Pattern[str]] = re.compile(r"[^a-zA-Z0-9]+")
|
||||
_DUPLICATE_SEPARATOR_PATTERN: Final[re.Pattern[str]] = re.compile(r"[-_]{2,}")
|
||||
_DUPLICATE_UNDERSCORE_PATTERN: Final[re.Pattern[str]] = re.compile(r"_+")
|
||||
_MAX_TOOL_NAME_LENGTH: Final[int] = 64
|
||||
|
||||
|
||||
@functools.lru_cache(maxsize=8)
|
||||
def _duplicate_separator_pattern(separator: str) -> re.Pattern[str]:
|
||||
return re.compile(f"(?:{re.escape(separator)}){{2,}}")
|
||||
|
||||
|
||||
def sanitize_tool_name(name: str, max_length: int = _MAX_TOOL_NAME_LENGTH) -> str:
|
||||
"""Sanitize tool name for LLM provider compatibility.
|
||||
|
||||
@@ -67,8 +72,7 @@ def slugify(text: str, separator: str = "_") -> str:
|
||||
text = text.lower()
|
||||
text = _QUOTE_PATTERN.sub("", text)
|
||||
text = _DISALLOWED_CHARS_PATTERN.sub(separator, text)
|
||||
escaped_sep = re.escape(separator)
|
||||
text = re.sub(f"(?:{escaped_sep}){{2,}}", separator, text)
|
||||
text = _duplicate_separator_pattern(separator).sub(separator, text)
|
||||
return text.strip(separator)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user