refactor: move is_uuid and is_creator_slug helpers to shared utils

This commit is contained in:
Zamil Majdy
2026-03-17 13:52:12 +07:00
parent 0d21179a25
commit 9190a28f24
3 changed files with 20 additions and 15 deletions

View File

@@ -3,7 +3,6 @@
from __future__ import annotations
import logging
import re
from typing import TYPE_CHECKING, Any, Literal
if TYPE_CHECKING:
@@ -19,15 +18,12 @@ from .models import (
NoResultsResponse,
ToolResponseBase,
)
from .utils import UUID_V4_PATTERN
from .utils import is_creator_slug, is_uuid
logger = logging.getLogger(__name__)
SearchSource = Literal["marketplace", "library"]
# Matches "creator/slug" identifiers used in the marketplace
_CREATOR_SLUG_PATTERN = re.compile(r"^[\w-]+/[\w-]+$")
# Keywords that should be treated as "list all" rather than a literal search
_LIST_ALL_KEYWORDS = frozenset({"all", "*", "everything", "any", ""})
@@ -55,7 +51,7 @@ async def _search_marketplace(query: str, session_id: str | None) -> ToolRespons
agents: list[AgentInfo] = []
try:
# Direct lookup if query matches "creator/slug" pattern
if _CREATOR_SLUG_PATTERN.match(query):
if is_creator_slug(query):
logger.info(f"Query looks like creator/slug, trying direct lookup: {query}")
creator, slug = query.split("/", 1)
agent_info = await _get_marketplace_agent_by_slug(creator, slug)
@@ -122,7 +118,7 @@ async def _search_library(
agents: list[AgentInfo] = []
try:
if _is_uuid(query):
if is_uuid(query):
logger.info(f"Query looks like UUID, trying direct lookup: {query}")
agent = await _get_library_agent_by_id(user_id, query)
if agent:
@@ -197,11 +193,6 @@ async def _search_library(
)
def _is_uuid(text: str) -> bool:
"""Check if text is a valid UUID v4."""
return bool(UUID_V4_PATTERN.match(text.strip()))
def _marketplace_agent_to_info(agent: Any) -> AgentInfo:
"""Convert a marketplace agent (StoreAgent or StoreAgentDetails) to an AgentInfo."""
return AgentInfo(

View File

@@ -15,7 +15,7 @@ from .models import (
ErrorResponse,
NoResultsResponse,
)
from .utils import UUID_V4_PATTERN
from .utils import is_uuid
logger = logging.getLogger(__name__)
@@ -122,7 +122,7 @@ class FindBlockTool(BaseTool):
try:
# Direct ID lookup if query looks like a UUID
if UUID_V4_PATTERN.match(query):
if is_uuid(query):
block = get_block(query.lower())
if block:
if block.disabled:

View File

@@ -21,12 +21,26 @@ from backend.util.exceptions import NotFoundError
logger = logging.getLogger(__name__)
# Shared UUID v4 pattern used by multiple tools for direct ID lookups.
UUID_V4_PATTERN = re.compile(
_UUID_V4_PATTERN = re.compile(
r"^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$",
re.IGNORECASE,
)
def is_uuid(text: str) -> bool:
"""Check if text is a valid UUID v4."""
return bool(_UUID_V4_PATTERN.match(text.strip()))
# Matches "creator/slug" identifiers used in the marketplace
_CREATOR_SLUG_PATTERN = re.compile(r"^[\w-]+/[\w-]+$")
def is_creator_slug(text: str) -> bool:
"""Check if text matches a 'creator/slug' marketplace identifier."""
return bool(_CREATOR_SLUG_PATTERN.match(text.strip()))
async def fetch_graph_from_store_slug(
username: str,
agent_name: str,