Skip to content

Dianel555/DSkills / skills/ace-tool

ace-tool

Verified

Semantic codebase search, code indexing, and prompt enhancement via standalone CLI. Use when: (1) Semantic code search with natural language queries, (2) Code indexing for remote codebase retrieval, (3) Prompt enhancement with codebase context, (4) Before grep/find/glob operations for better accuracy, (5) Complex requirements clarification, (6) Large codebase navigation. Triggers: "search context", "enhance prompt", "find code that", "index project", "clarify requirements". IMPORTANT: Always use ace-tool BEFORE grep/find/glob for semantic-level code location.

scripts/utils.py

View on GitHub
"""Utility functions for ACE-Tool CLI."""

import os
import re
import uuid
from pathlib import Path

_SESSION_ID: str | None = None

_VERSION_SUFFIX_RE = re.compile(r"/v\d[A-Za-z0-9_-]*$")
_VERSION_PREFIX_RE = re.compile(r"^/v\d[A-Za-z0-9_-]*(?=/|$)")


def _has_version_suffix(url: str) -> tuple[bool, int]:
    match = _VERSION_SUFFIX_RE.search(url.rstrip("/"))
    return (True, match.start()) if match else (False, -1)


def _strip_version_prefix(path: str) -> str:
    return _VERSION_PREFIX_RE.sub("", path, count=1)


def build_api_url(base_url: str, path: str) -> str:
    base_url = base_url.rstrip("/")
    if not path.startswith("/"):
        path = "/" + path
    has_ver, ver_idx = _has_version_suffix(base_url)
    if has_ver:
        stripped = _strip_version_prefix(path)
        return base_url + stripped
    return base_url + path


def load_env():
    """Load environment variables from the skill root .env file."""
    env_path = Path(__file__).resolve().parent.parent / ".env"
    if env_path.exists():
        with open(env_path) as f:
            for line in f:
                line = line.strip()
                if line and not line.startswith("#") and "=" in line:
                    key, value = line.split("=", 1)
                    value = value.strip()
                    # Strip inline comments (unquoted # preceded by whitespace)
                    if value and value[0] not in ('"', "'"):
                        value = re.split(r"\s+#", value, maxsplit=1)[0].strip()
                    else:
                        value = value.strip("\"'")
                    os.environ.setdefault(key.strip(), value)


def get_session_id() -> str:
    """Get or create persistent session ID."""
    global _SESSION_ID
    if _SESSION_ID is None:
        _SESSION_ID = str(uuid.uuid4())
    return _SESSION_ID


def is_chinese_text(text: str) -> bool:
    """Detect if text is primarily Chinese."""
    chinese_chars = re.findall(r"[\u4e00-\u9fa5]", text)
    if not chinese_chars:
        return False
    if len(chinese_chars) >= 3:
        return True
    non_ws = len([c for c in text if not c.isspace()])
    return non_ws > 0 and len(chinese_chars) / non_ws >= 0.1


def parse_chat_history(conversation_history: str) -> list[dict]:
    """Parse conversation history into ChatMessage format."""
    messages = []
    current_role = None
    current_lines = []

    user_prefixes = ["User:", "用户:"]
    assistant_prefixes = ["AI:", "Assistant:", "助手:"]

    for line in conversation_history.split("\n"):
        trimmed = line.strip()
        if not trimmed:
            if current_role:
                current_lines.append("")
            continue

        role_found = None
        content = None
        for prefix in user_prefixes:
            if trimmed.startswith(prefix):
                role_found = "user"
                content = trimmed[len(prefix) :].strip()
                break
        if not role_found:
            for prefix in assistant_prefixes:
                if trimmed.startswith(prefix):
                    role_found = "assistant"
                    content = trimmed[len(prefix) :].strip()
                    break

        if role_found:
            if current_role:
                messages.append({"role": current_role, "content": "\n".join(current_lines)})
            current_role = role_found
            current_lines = [content]
        elif current_role:
            current_lines.append(line)

    if current_role:
        messages.append({"role": current_role, "content": "\n".join(current_lines)})

    return messages


def decode_bytes(raw: bytes, encoding_chain: list[str]) -> str | None:
    """Decode already-read bytes. Returns None for binary/unreadable content."""
    if b"\x00" in raw[:8192]:
        return None
    for enc in encoding_chain:
        try:
            return raw.decode(enc)
        except (UnicodeDecodeError, LookupError):
            continue
    return None


def detect_and_read(file_path: Path, encoding_chain: list[str]) -> str | None:
    """Try multiple encodings to read a file. Returns None for binary/unreadable.

    Path-based convenience wrapper; callers that must not follow links should read
    through an :class:`secure_fs.AuthorizedRoot` handle and use ``decode_bytes``.
    """
    try:
        raw = file_path.read_bytes()
    except OSError:
        return None
    return decode_bytes(raw, encoding_chain)


def sanitize_content(content: str) -> str:
    """Normalize line endings and remove null bytes."""
    return content.replace("\r\n", "\n").replace("\r", "\n").replace("\x00", "")


def _validate_session_data(data: dict) -> tuple[str | None, str | None]:
    """Validate and extract session data fields.

    Returns (tenant_url, access_token) or (None, None) if invalid.
    """
    if not isinstance(data, dict):
        return None, None
    access_token = data.get("accessToken", "")
    tenant_url = data.get("tenantURL", "")
    if isinstance(access_token, str) and access_token.strip() and isinstance(tenant_url, str) and tenant_url.strip():
        return tenant_url.rstrip("/"), access_token
    return None, None


def load_session_auth() -> tuple[str | None, str | None, str]:
    """Load authentication from session.json, AUGMENT_SESSION_AUTH, or legacy env vars.

    Returns:
        (base_url, token, source) where source is one of:
        - "session.json"
        - "AUGMENT_SESSION_AUTH"
        - "ACE_API_TOKEN"
        - "none"
    """
    import json
    import logging

    log = logging.getLogger(__name__)

    # Try session.json first
    session_path = Path.home() / ".augment" / "session.json"
    if session_path.exists():
        try:
            content = session_path.read_text(encoding="utf-8-sig")
            data = json.loads(content)
            tenant_url, access_token = _validate_session_data(data)
            if tenant_url and access_token:
                log.debug("Auth source: session.json")
                return tenant_url, access_token, "session.json"
        except (OSError, json.JSONDecodeError, UnicodeDecodeError) as e:
            log.debug("Failed to load session.json: %s", e)

    # Try AUGMENT_SESSION_AUTH env var
    env_session = os.getenv("AUGMENT_SESSION_AUTH", "")
    if env_session:
        try:
            data = json.loads(env_session)
            tenant_url, access_token = _validate_session_data(data)
            if tenant_url and access_token:
                log.debug("Auth source: AUGMENT_SESSION_AUTH")
                return tenant_url, access_token, "AUGMENT_SESSION_AUTH"
        except (json.JSONDecodeError, TypeError) as e:
            log.debug("Failed to parse AUGMENT_SESSION_AUTH: %s", e)

    # Fallback to legacy env vars
    legacy_url = os.getenv("ACE_API_URL", "").rstrip("/")
    legacy_token = os.getenv("ACE_API_TOKEN", "")
    if legacy_url or legacy_token:
        log.debug("Auth source: ACE_API_TOKEN (legacy)")
        return legacy_url or None, legacy_token or None, "ACE_API_TOKEN"

    log.debug("Auth source: none")
    return None, None, "none"
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall