Spaces:
Sleeping
Sleeping
File size: 1,778 Bytes
c5292d8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
"""Utility functions for LLM Code Deployment system."""
import base64
import hashlib
import re
from datetime import datetime
from typing import Any
from uuid_utils import uuid7
def generate_nonce() -> str:
"""Generate a unique nonce using UUID7."""
return str(uuid7())
def generate_task_id(template_id: str, brief: str, attachments: list[dict[str, Any]]) -> str:
"""Generate a unique task ID based on template and content."""
content = f"{brief}{str(attachments)}"
hash_value = hashlib.sha256(content.encode()).hexdigest()[:5]
return f"{template_id}-{hash_value}"
def decode_data_uri(data_uri: str) -> tuple[str, bytes]:
"""Decode a data URI into mime type and content."""
# Format: data:mime/type;base64,content
match = re.match(r"data:([^;]+);base64,(.+)", data_uri)
if not match:
raise ValueError(f"Invalid data URI format: {data_uri[:50]}...")
mime_type = match.group(1)
encoded_content = match.group(2)
content = base64.b64decode(encoded_content)
return mime_type, content
def encode_data_uri(mime_type: str, content: bytes) -> str:
"""Encode content as a data URI."""
encoded = base64.b64encode(content).decode("utf-8")
return f"data:{mime_type};base64,{encoded}"
def sanitize_repo_name(name: str) -> str:
"""Sanitize a string for use as a GitHub repository name."""
# Replace spaces and special chars with hyphens
sanitized = re.sub(r"[^a-zA-Z0-9-_.]", "-", name)
# Remove leading/trailing hyphens
sanitized = sanitized.strip("-")
# Collapse multiple hyphens
sanitized = re.sub(r"-+", "-", sanitized)
# Ensure lowercase
return sanitized.lower()
def get_timestamp() -> datetime:
"""Get current UTC timestamp."""
return datetime.utcnow()
|