Skip to content

naravid19/ai-project-rules-generator

generating-project-rules

Verified

Make sure to use this skill WHENEVER the user mentions creating, generating, or updating project rules files (.cursorrules, AGENTS.md, CLAUDE.md, GEMINI.md, or equivalent) for any software project. This is essential when a user wants to integrate AI skills into their development workflow, or when they need cross-platform AI tool configuration. Skip this for general code generation, refactoring, or tasks unrelated to AI agent configuration files.

scripts/config_runtime.py

View on GitHub
from __future__ import annotations

import json
import os
import re
from pathlib import Path
from typing import Any

from rules_config import load_rules_config

DEFAULT_MCP_REGISTRY_PATH = Path(__file__).resolve().parents[1] / "templates" / "mcp_registry.yaml"
MCP_CONFIG_CANDIDATES = (
    Path(".cursor") / "mcp.json",
    Path(".cursor") / "mcp.jsonc",
    Path(".claude") / "mcp.json",
    Path(".mcp.json"),
)
HOME_MCP_CONFIG_CANDIDATES = (
    Path.home() / "AppData" / "Roaming" / "Claude" / "claude_desktop_config.json",
    Path.home() / "Library" / "Application Support" / "Claude" / "claude_desktop_config.json",
    Path.home() / ".config" / "Claude" / "claude_desktop_config.json",
    Path.home() / ".claude.json",
)
YAML_LIST_ITEM_PATTERN = re.compile(r"^\s*-\s*(.+?)\s*$")
YAML_SECTION_PATTERN = re.compile(r"^(?P<key>[A-Za-z0-9_-]+)\s*:\s*$")


def load_mcp_registry(registry_path: Path | None = None) -> dict[str, list[str]]:
    path = registry_path or DEFAULT_MCP_REGISTRY_PATH
    if not path.exists():
        return {}

    lines = path.read_text(encoding="utf-8").splitlines()
    registry: dict[str, list[str]] = {}
    current_key = ""

    for raw_line in lines:
        if not raw_line.strip() or raw_line.lstrip().startswith("#"):
            continue

        section_match = YAML_SECTION_PATTERN.match(raw_line)
        if section_match:
            current_key = section_match.group("key")
            registry.setdefault(current_key, [])
            continue

        if not current_key:
            continue

        item_match = YAML_LIST_ITEM_PATTERN.match(raw_line)
        if item_match:
            registry[current_key].append(_strip_yaml_scalar(item_match.group(1)))

    return registry


def detect_native_mcp_servers(project_root: Path) -> list[str]:
    root = Path(project_root).resolve()
    detected: list[str] = []

    for candidate in list(MCP_CONFIG_CANDIDATES) + list(HOME_MCP_CONFIG_CANDIDATES):
        config_path = candidate if candidate.is_absolute() else root / candidate
        if not config_path.exists():
            continue

        payload = _load_jsonc_file(config_path)
        if not isinstance(payload, dict):
            continue

        servers = payload.get("mcpServers") or payload.get("servers") or {}
        if isinstance(servers, dict):
            for server_name in servers:
                if server_name not in detected:
                    detected.append(str(server_name))

    return detected


def route_mcp_servers(
    user_intent: str,
    tech_stack: list[str] | tuple[str, ...] | str,
    registry: dict[str, list[str]] | None = None,
) -> list[str]:
    effective_registry = registry or load_mcp_registry()
    haystack = " ".join(
        [user_intent.lower(), *(_normalize_sequence(tech_stack))]
    )
    matched: list[str] = []

    for intent_key, servers in effective_registry.items():
        if intent_key.lower().replace("-", " ") not in haystack:
            continue
        for server in servers:
            if server not in matched:
                matched.append(server)

    return matched


def load_runtime_defaults(config_path: Path) -> dict[str, Any]:
    payload = load_rules_config(config_path)
    if payload.get("confidence_threshold") is None:
        payload["confidence_threshold"] = 80
    if payload.get("skill_match_limit") is None:
        payload["skill_match_limit"] = 5
    if payload.get("agentic_match_limit") is None:
        payload["agentic_match_limit"] = 3
    return payload


def _load_jsonc_file(path: Path) -> Any:
    raw = path.read_text(encoding="utf-8", errors="ignore")
    sanitized = re.sub(r"//.*?$", "", raw, flags=re.MULTILINE)
    sanitized = re.sub(r"/\*.*?\*/", "", sanitized, flags=re.DOTALL)
    try:
        return json.loads(sanitized)
    except json.JSONDecodeError:
        return {}


def _normalize_sequence(value: list[str] | tuple[str, ...] | str) -> list[str]:
    if isinstance(value, str):
        return [value.lower()]
    return [str(item).lower() for item in value]


def _strip_yaml_scalar(value: str) -> str:
    return value.strip().strip('"').strip("'")
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: 469kUpdated 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: 321kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 321kUpdated 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: 321kUpdated Claude CodeCodexVerifiedInstall