temp / shared /utils.py
CheeksTheGeek's picture
Initial commit: LLM Code Deployment System
c5292d8 unverified
"""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()