mirror of
https://github.com/Graphify-Labs/graphify.git
synced 2026-09-14 19:34:09 +08:00
870 lines
35 KiB
Python
870 lines
35 KiB
Python
"""graphify CLI - `graphify install` sets up the Claude Code skill."""
|
|
from __future__ import annotations
|
|
import json
|
|
import platform
|
|
import re
|
|
import shutil
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
try:
|
|
from importlib.metadata import version as _pkg_version
|
|
__version__ = _pkg_version("graphifyy")
|
|
except Exception:
|
|
__version__ = "unknown"
|
|
|
|
|
|
def _check_skill_version(skill_dst: Path) -> None:
|
|
"""Warn if the installed skill is from an older graphify version."""
|
|
version_file = skill_dst.parent / ".graphify_version"
|
|
if not version_file.exists():
|
|
return
|
|
installed = version_file.read_text(encoding="utf-8").strip()
|
|
if installed != __version__:
|
|
print(f" warning: skill is from graphify {installed}, package is {__version__}. Run 'graphify install' to update.")
|
|
|
|
_SETTINGS_HOOK = {
|
|
"matcher": "Glob|Grep",
|
|
"hooks": [
|
|
{
|
|
"type": "command",
|
|
"command": (
|
|
"[ -f graphify-out/graph.json ] && "
|
|
r"""echo '{"hookSpecificOutput":{"hookEventName":"PreToolUse","additionalContext":"graphify: Knowledge graph exists. Read graphify-out/GRAPH_REPORT.md for god nodes and community structure before searching raw files."}}' """
|
|
"|| true"
|
|
),
|
|
}
|
|
],
|
|
}
|
|
|
|
_SKILL_REGISTRATION = (
|
|
"\n# graphify\n"
|
|
"- **graphify** (`~/.claude/skills/graphify/SKILL.md`) "
|
|
"- any input to knowledge graph. Trigger: `/graphify`\n"
|
|
"When the user types `/graphify`, invoke the Skill tool "
|
|
"with `skill: \"graphify\"` before doing anything else.\n"
|
|
)
|
|
|
|
|
|
_PLATFORM_CONFIG: dict[str, dict] = {
|
|
"claude": {
|
|
"skill_file": "skill.md",
|
|
"skill_dst": Path(".claude") / "skills" / "graphify" / "SKILL.md",
|
|
"claude_md": True,
|
|
},
|
|
"codex": {
|
|
"skill_file": "skill-codex.md",
|
|
"skill_dst": Path(".agents") / "skills" / "graphify" / "SKILL.md",
|
|
"claude_md": False,
|
|
},
|
|
"opencode": {
|
|
"skill_file": "skill-opencode.md",
|
|
"skill_dst": Path(".config") / "opencode" / "skills" / "graphify" / "SKILL.md",
|
|
"claude_md": False,
|
|
},
|
|
"aider": {
|
|
"skill_file": "skill-aider.md",
|
|
"skill_dst": Path(".aider") / "graphify" / "SKILL.md",
|
|
"claude_md": False,
|
|
},
|
|
"copilot": {
|
|
"skill_file": "skill-copilot.md",
|
|
"skill_dst": Path(".copilot") / "skills" / "graphify" / "SKILL.md",
|
|
"claude_md": False,
|
|
},
|
|
"claw": {
|
|
"skill_file": "skill-claw.md",
|
|
"skill_dst": Path(".claw") / "skills" / "graphify" / "SKILL.md",
|
|
"claude_md": False,
|
|
},
|
|
"droid": {
|
|
"skill_file": "skill-droid.md",
|
|
"skill_dst": Path(".factory") / "skills" / "graphify" / "SKILL.md",
|
|
"claude_md": False,
|
|
},
|
|
"trae": {
|
|
"skill_file": "skill-trae.md",
|
|
"skill_dst": Path(".trae") / "skills" / "graphify" / "SKILL.md",
|
|
"claude_md": False,
|
|
},
|
|
"trae-cn": {
|
|
"skill_file": "skill-trae.md",
|
|
"skill_dst": Path(".trae-cn") / "skills" / "graphify" / "SKILL.md",
|
|
"claude_md": False,
|
|
},
|
|
"windows": {
|
|
"skill_file": "skill-windows.md",
|
|
"skill_dst": Path(".claude") / "skills" / "graphify" / "SKILL.md",
|
|
"claude_md": True,
|
|
},
|
|
}
|
|
|
|
|
|
def install(platform: str = "claude") -> None:
|
|
if platform == "gemini":
|
|
gemini_install()
|
|
return
|
|
if platform == "cursor":
|
|
_cursor_install()
|
|
return
|
|
if platform not in _PLATFORM_CONFIG:
|
|
print(
|
|
f"error: unknown platform '{platform}'. Choose from: {', '.join(_PLATFORM_CONFIG)}, gemini, cursor",
|
|
file=sys.stderr,
|
|
)
|
|
sys.exit(1)
|
|
|
|
cfg = _PLATFORM_CONFIG[platform]
|
|
skill_src = Path(__file__).parent / cfg["skill_file"]
|
|
if not skill_src.exists():
|
|
print(f"error: {cfg['skill_file']} not found in package - reinstall graphify", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
skill_dst = Path.home() / cfg["skill_dst"]
|
|
skill_dst.parent.mkdir(parents=True, exist_ok=True)
|
|
shutil.copy(skill_src, skill_dst)
|
|
(skill_dst.parent / ".graphify_version").write_text(__version__, encoding="utf-8")
|
|
print(f" skill installed -> {skill_dst}")
|
|
|
|
if cfg["claude_md"]:
|
|
# Register in ~/.claude/CLAUDE.md (Claude Code only)
|
|
claude_md = Path.home() / ".claude" / "CLAUDE.md"
|
|
if claude_md.exists():
|
|
content = claude_md.read_text(encoding="utf-8")
|
|
if "graphify" in content:
|
|
print(f" CLAUDE.md -> already registered (no change)")
|
|
else:
|
|
claude_md.write_text(content.rstrip() + _SKILL_REGISTRATION, encoding="utf-8")
|
|
print(f" CLAUDE.md -> skill registered in {claude_md}")
|
|
else:
|
|
claude_md.parent.mkdir(parents=True, exist_ok=True)
|
|
claude_md.write_text(_SKILL_REGISTRATION.lstrip(), encoding="utf-8")
|
|
print(f" CLAUDE.md -> created at {claude_md}")
|
|
|
|
print()
|
|
print("Done. Open your AI coding assistant and type:")
|
|
print()
|
|
print(" /graphify .")
|
|
print()
|
|
|
|
|
|
_CLAUDE_MD_SECTION = """\
|
|
## graphify
|
|
|
|
This project has a graphify knowledge graph at graphify-out/.
|
|
|
|
Rules:
|
|
- Before answering architecture or codebase questions, read graphify-out/GRAPH_REPORT.md for god nodes and community structure
|
|
- If graphify-out/wiki/index.md exists, navigate it instead of reading raw files
|
|
- After modifying code files in this session, run `python3 -c "from graphify.watch import _rebuild_code; from pathlib import Path; _rebuild_code(Path('.'))"` to keep the graph current
|
|
"""
|
|
|
|
_CLAUDE_MD_MARKER = "## graphify"
|
|
|
|
# AGENTS.md section for Codex, OpenCode, and OpenClaw.
|
|
# All three platforms read AGENTS.md in the project root for persistent instructions.
|
|
_AGENTS_MD_SECTION = """\
|
|
## graphify
|
|
|
|
This project has a graphify knowledge graph at graphify-out/.
|
|
|
|
Rules:
|
|
- Before answering architecture or codebase questions, read graphify-out/GRAPH_REPORT.md for god nodes and community structure
|
|
- If graphify-out/wiki/index.md exists, navigate it instead of reading raw files
|
|
- After modifying code files in this session, run `python3 -c "from graphify.watch import _rebuild_code; from pathlib import Path; _rebuild_code(Path('.'))"` to keep the graph current
|
|
"""
|
|
|
|
_AGENTS_MD_MARKER = "## graphify"
|
|
|
|
_GEMINI_MD_SECTION = """\
|
|
## graphify
|
|
|
|
This project has a graphify knowledge graph at graphify-out/.
|
|
|
|
Rules:
|
|
- Before answering architecture or codebase questions, read graphify-out/GRAPH_REPORT.md for god nodes and community structure
|
|
- If graphify-out/wiki/index.md exists, navigate it instead of reading raw files
|
|
- After modifying code files in this session, run `python3 -c "from graphify.watch import _rebuild_code; from pathlib import Path; _rebuild_code(Path('.'))"` to keep the graph current
|
|
"""
|
|
|
|
_GEMINI_MD_MARKER = "## graphify"
|
|
|
|
_GEMINI_HOOK = {
|
|
"matcher": "read_file|list_directory",
|
|
"hooks": [
|
|
{
|
|
"type": "command",
|
|
"command": (
|
|
"[ -f graphify-out/graph.json ] && "
|
|
r"""echo '{"decision":"allow","additionalContext":"graphify: Knowledge graph exists. Read graphify-out/GRAPH_REPORT.md for god nodes and community structure before searching raw files."}' """
|
|
r"""|| echo '{"decision":"allow"}'"""
|
|
),
|
|
}
|
|
],
|
|
}
|
|
|
|
|
|
def gemini_install(project_dir: Path | None = None) -> None:
|
|
"""Copy skill file to ~/.gemini/skills/graphify/, write GEMINI.md section, and install BeforeTool hook."""
|
|
# Copy skill file to ~/.gemini/skills/graphify/SKILL.md
|
|
skill_src = Path(__file__).parent / "skill.md"
|
|
skill_dst = Path.home() / ".gemini" / "skills" / "graphify" / "SKILL.md"
|
|
skill_dst.parent.mkdir(parents=True, exist_ok=True)
|
|
shutil.copy(skill_src, skill_dst)
|
|
(skill_dst.parent / ".graphify_version").write_text(__version__, encoding="utf-8")
|
|
print(f" skill installed -> {skill_dst}")
|
|
|
|
target = (project_dir or Path(".")) / "GEMINI.md"
|
|
|
|
if target.exists():
|
|
content = target.read_text(encoding="utf-8")
|
|
if _GEMINI_MD_MARKER in content:
|
|
print("graphify already configured in GEMINI.md")
|
|
else:
|
|
target.write_text(content.rstrip() + "\n\n" + _GEMINI_MD_SECTION, encoding="utf-8")
|
|
print(f"graphify section written to {target.resolve()}")
|
|
else:
|
|
target.write_text(_GEMINI_MD_SECTION, encoding="utf-8")
|
|
print(f"graphify section written to {target.resolve()}")
|
|
|
|
_install_gemini_hook(project_dir or Path("."))
|
|
print()
|
|
print("Gemini CLI will now check the knowledge graph before answering")
|
|
print("codebase questions and rebuild it after code changes.")
|
|
|
|
|
|
def _install_gemini_hook(project_dir: Path) -> None:
|
|
settings_path = project_dir / ".gemini" / "settings.json"
|
|
settings_path.parent.mkdir(parents=True, exist_ok=True)
|
|
try:
|
|
settings = json.loads(settings_path.read_text(encoding="utf-8")) if settings_path.exists() else {}
|
|
except json.JSONDecodeError:
|
|
settings = {}
|
|
before_tool = settings.setdefault("hooks", {}).setdefault("BeforeTool", [])
|
|
settings["hooks"]["BeforeTool"] = [h for h in before_tool if "graphify" not in str(h)]
|
|
settings["hooks"]["BeforeTool"].append(_GEMINI_HOOK)
|
|
settings_path.write_text(json.dumps(settings, indent=2), encoding="utf-8")
|
|
print(" .gemini/settings.json -> BeforeTool hook registered")
|
|
|
|
|
|
def _uninstall_gemini_hook(project_dir: Path) -> None:
|
|
settings_path = project_dir / ".gemini" / "settings.json"
|
|
if not settings_path.exists():
|
|
return
|
|
try:
|
|
settings = json.loads(settings_path.read_text(encoding="utf-8"))
|
|
except json.JSONDecodeError:
|
|
return
|
|
before_tool = settings.get("hooks", {}).get("BeforeTool", [])
|
|
filtered = [h for h in before_tool if "graphify" not in str(h)]
|
|
if len(filtered) == len(before_tool):
|
|
return
|
|
settings["hooks"]["BeforeTool"] = filtered
|
|
settings_path.write_text(json.dumps(settings, indent=2), encoding="utf-8")
|
|
print(" .gemini/settings.json -> BeforeTool hook removed")
|
|
|
|
|
|
def gemini_uninstall(project_dir: Path | None = None) -> None:
|
|
"""Remove the graphify section from GEMINI.md, uninstall hook, and remove skill file."""
|
|
# Remove skill file
|
|
skill_dst = Path.home() / ".gemini" / "skills" / "graphify" / "SKILL.md"
|
|
if skill_dst.exists():
|
|
skill_dst.unlink()
|
|
print(f" skill removed -> {skill_dst}")
|
|
version_file = skill_dst.parent / ".graphify_version"
|
|
if version_file.exists():
|
|
version_file.unlink()
|
|
for d in (skill_dst.parent, skill_dst.parent.parent):
|
|
try:
|
|
d.rmdir()
|
|
except OSError:
|
|
break
|
|
|
|
target = (project_dir or Path(".")) / "GEMINI.md"
|
|
if not target.exists():
|
|
print("No GEMINI.md found in current directory - nothing to do")
|
|
return
|
|
content = target.read_text(encoding="utf-8")
|
|
if _GEMINI_MD_MARKER not in content:
|
|
print("graphify section not found in GEMINI.md - nothing to do")
|
|
return
|
|
cleaned = re.sub(r"\n*## graphify\n.*?(?=\n## |\Z)", "", content, flags=re.DOTALL).rstrip()
|
|
if cleaned:
|
|
target.write_text(cleaned + "\n", encoding="utf-8")
|
|
print(f"graphify section removed from {target.resolve()}")
|
|
else:
|
|
target.unlink()
|
|
print(f"GEMINI.md was empty after removal - deleted {target.resolve()}")
|
|
_uninstall_gemini_hook(project_dir or Path("."))
|
|
|
|
|
|
_CURSOR_RULE_PATH = Path(".cursor") / "rules" / "graphify.mdc"
|
|
_CURSOR_RULE = """\
|
|
---
|
|
description: graphify knowledge graph context
|
|
alwaysApply: true
|
|
---
|
|
|
|
This project has a graphify knowledge graph at graphify-out/.
|
|
|
|
- Before answering architecture or codebase questions, read graphify-out/GRAPH_REPORT.md for god nodes and community structure
|
|
- If graphify-out/wiki/index.md exists, navigate it instead of reading raw files
|
|
- After modifying code files in this session, run `python3 -c "from graphify.watch import _rebuild_code; from pathlib import Path; _rebuild_code(Path('.'))"` to keep the graph current
|
|
"""
|
|
|
|
|
|
def _cursor_install(project_dir: Path) -> None:
|
|
"""Write .cursor/rules/graphify.mdc with alwaysApply: true."""
|
|
rule_path = (project_dir or Path(".")) / _CURSOR_RULE_PATH
|
|
rule_path.parent.mkdir(parents=True, exist_ok=True)
|
|
if rule_path.exists():
|
|
print(f"graphify rule already exists at {rule_path} (no change)")
|
|
return
|
|
rule_path.write_text(_CURSOR_RULE, encoding="utf-8")
|
|
print(f"graphify rule written to {rule_path.resolve()}")
|
|
print()
|
|
print("Cursor will now always include the knowledge graph context.")
|
|
print("Run /graphify . first to build the graph if you haven't already.")
|
|
|
|
|
|
def _cursor_uninstall(project_dir: Path) -> None:
|
|
"""Remove .cursor/rules/graphify.mdc."""
|
|
rule_path = (project_dir or Path(".")) / _CURSOR_RULE_PATH
|
|
if not rule_path.exists():
|
|
print("No graphify Cursor rule found - nothing to do")
|
|
return
|
|
rule_path.unlink()
|
|
print(f"graphify Cursor rule removed from {rule_path.resolve()}")
|
|
|
|
|
|
# OpenCode tool.execute.before plugin — fires before every tool call.
|
|
# Injects a graph reminder into bash command output when graph.json exists.
|
|
_OPENCODE_PLUGIN_JS = """\
|
|
// graphify OpenCode plugin
|
|
// Injects a knowledge graph reminder before bash tool calls when the graph exists.
|
|
import { existsSync } from "fs";
|
|
import { join } from "path";
|
|
|
|
export const GraphifyPlugin = async ({ directory }) => {
|
|
let reminded = false;
|
|
|
|
return {
|
|
"tool.execute.before": async (input, output) => {
|
|
if (reminded) return;
|
|
if (!existsSync(join(directory, "graphify-out", "graph.json"))) return;
|
|
|
|
if (input.tool === "bash") {
|
|
output.args.command =
|
|
'echo "[graphify] Knowledge graph available. Read graphify-out/GRAPH_REPORT.md for god nodes and architecture context before searching files." && ' +
|
|
output.args.command;
|
|
reminded = true;
|
|
}
|
|
},
|
|
};
|
|
};
|
|
"""
|
|
|
|
_OPENCODE_PLUGIN_PATH = Path(".opencode") / "plugins" / "graphify.js"
|
|
_OPENCODE_CONFIG_PATH = Path("opencode.json")
|
|
|
|
|
|
def _install_opencode_plugin(project_dir: Path) -> None:
|
|
"""Write graphify.js plugin and register it in opencode.json."""
|
|
plugin_file = project_dir / _OPENCODE_PLUGIN_PATH
|
|
plugin_file.parent.mkdir(parents=True, exist_ok=True)
|
|
plugin_file.write_text(_OPENCODE_PLUGIN_JS, encoding="utf-8")
|
|
print(f" {_OPENCODE_PLUGIN_PATH} -> tool.execute.before hook written")
|
|
|
|
config_file = project_dir / _OPENCODE_CONFIG_PATH
|
|
if config_file.exists():
|
|
try:
|
|
config = json.loads(config_file.read_text(encoding="utf-8"))
|
|
except json.JSONDecodeError:
|
|
config = {}
|
|
else:
|
|
config = {}
|
|
|
|
plugins = config.setdefault("plugin", [])
|
|
entry = str(_OPENCODE_PLUGIN_PATH)
|
|
if entry not in plugins:
|
|
plugins.append(entry)
|
|
config_file.write_text(json.dumps(config, indent=2), encoding="utf-8")
|
|
print(f" {_OPENCODE_CONFIG_PATH} -> plugin registered")
|
|
else:
|
|
print(f" {_OPENCODE_CONFIG_PATH} -> plugin already registered (no change)")
|
|
|
|
|
|
def _uninstall_opencode_plugin(project_dir: Path) -> None:
|
|
"""Remove graphify.js plugin and deregister from opencode.json."""
|
|
plugin_file = project_dir / _OPENCODE_PLUGIN_PATH
|
|
if plugin_file.exists():
|
|
plugin_file.unlink()
|
|
print(f" {_OPENCODE_PLUGIN_PATH} -> removed")
|
|
|
|
config_file = project_dir / _OPENCODE_CONFIG_PATH
|
|
if not config_file.exists():
|
|
return
|
|
try:
|
|
config = json.loads(config_file.read_text(encoding="utf-8"))
|
|
except json.JSONDecodeError:
|
|
return
|
|
plugins = config.get("plugin", [])
|
|
entry = str(_OPENCODE_PLUGIN_PATH)
|
|
if entry in plugins:
|
|
plugins.remove(entry)
|
|
if not plugins:
|
|
config.pop("plugin")
|
|
config_file.write_text(json.dumps(config, indent=2), encoding="utf-8")
|
|
print(f" {_OPENCODE_CONFIG_PATH} -> plugin deregistered")
|
|
|
|
|
|
_CODEX_HOOK = {
|
|
"hooks": {
|
|
"PreToolUse": [
|
|
{
|
|
"matcher": "Bash",
|
|
"hooks": [
|
|
{
|
|
"type": "command",
|
|
"command": (
|
|
"[ -f graphify-out/graph.json ] && "
|
|
r"""echo '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"allow"},"systemMessage":"graphify: Knowledge graph exists. Read graphify-out/GRAPH_REPORT.md for god nodes and community structure before searching raw files."}' """
|
|
"|| true"
|
|
),
|
|
}
|
|
],
|
|
}
|
|
]
|
|
}
|
|
}
|
|
|
|
|
|
def _install_codex_hook(project_dir: Path) -> None:
|
|
"""Add graphify PreToolUse hook to .codex/hooks.json."""
|
|
hooks_path = project_dir / ".codex" / "hooks.json"
|
|
hooks_path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
if hooks_path.exists():
|
|
try:
|
|
existing = json.loads(hooks_path.read_text(encoding="utf-8"))
|
|
except json.JSONDecodeError:
|
|
existing = {}
|
|
else:
|
|
existing = {}
|
|
|
|
pre_tool = existing.setdefault("hooks", {}).setdefault("PreToolUse", [])
|
|
existing["hooks"]["PreToolUse"] = [h for h in pre_tool if "graphify" not in str(h)]
|
|
existing["hooks"]["PreToolUse"].extend(_CODEX_HOOK["hooks"]["PreToolUse"])
|
|
hooks_path.write_text(json.dumps(existing, indent=2), encoding="utf-8")
|
|
print(f" .codex/hooks.json -> PreToolUse hook registered")
|
|
|
|
|
|
def _uninstall_codex_hook(project_dir: Path) -> None:
|
|
"""Remove graphify PreToolUse hook from .codex/hooks.json."""
|
|
hooks_path = project_dir / ".codex" / "hooks.json"
|
|
if not hooks_path.exists():
|
|
return
|
|
try:
|
|
existing = json.loads(hooks_path.read_text(encoding="utf-8"))
|
|
except json.JSONDecodeError:
|
|
return
|
|
pre_tool = existing.get("hooks", {}).get("PreToolUse", [])
|
|
filtered = [h for h in pre_tool if "graphify" not in str(h)]
|
|
existing["hooks"]["PreToolUse"] = filtered
|
|
hooks_path.write_text(json.dumps(existing, indent=2), encoding="utf-8")
|
|
print(f" .codex/hooks.json -> PreToolUse hook removed")
|
|
|
|
|
|
def _agents_install(project_dir: Path, platform: str) -> None:
|
|
"""Write the graphify section to the local AGENTS.md (Codex/OpenCode/OpenClaw)."""
|
|
target = (project_dir or Path(".")) / "AGENTS.md"
|
|
|
|
if target.exists():
|
|
content = target.read_text(encoding="utf-8")
|
|
if _AGENTS_MD_MARKER in content:
|
|
print(f"graphify already configured in AGENTS.md")
|
|
else:
|
|
target.write_text(content.rstrip() + "\n\n" + _AGENTS_MD_SECTION, encoding="utf-8")
|
|
print(f"graphify section written to {target.resolve()}")
|
|
else:
|
|
target.write_text(_AGENTS_MD_SECTION, encoding="utf-8")
|
|
print(f"graphify section written to {target.resolve()}")
|
|
|
|
if platform == "codex":
|
|
_install_codex_hook(project_dir or Path("."))
|
|
elif platform == "opencode":
|
|
_install_opencode_plugin(project_dir or Path("."))
|
|
|
|
print()
|
|
print(f"{platform.capitalize()} will now check the knowledge graph before answering")
|
|
print("codebase questions and rebuild it after code changes.")
|
|
if platform not in ("codex", "opencode"):
|
|
print()
|
|
print("Note: unlike Claude Code, there is no PreToolUse hook equivalent for")
|
|
print(f"{platform.capitalize()} — the AGENTS.md rules are the always-on mechanism.")
|
|
|
|
|
|
def _agents_uninstall(project_dir: Path) -> None:
|
|
"""Remove the graphify section from the local AGENTS.md."""
|
|
target = (project_dir or Path(".")) / "AGENTS.md"
|
|
|
|
if not target.exists():
|
|
print("No AGENTS.md found in current directory - nothing to do")
|
|
return
|
|
|
|
content = target.read_text(encoding="utf-8")
|
|
if _AGENTS_MD_MARKER not in content:
|
|
print("graphify section not found in AGENTS.md - nothing to do")
|
|
return
|
|
|
|
cleaned = re.sub(
|
|
r"\n*## graphify\n.*?(?=\n## |\Z)",
|
|
"",
|
|
content,
|
|
flags=re.DOTALL,
|
|
).rstrip()
|
|
if cleaned:
|
|
target.write_text(cleaned + "\n", encoding="utf-8")
|
|
print(f"graphify section removed from {target.resolve()}")
|
|
else:
|
|
target.unlink()
|
|
print(f"AGENTS.md was empty after removal - deleted {target.resolve()}")
|
|
|
|
_uninstall_opencode_plugin(project_dir or Path("."))
|
|
|
|
|
|
def claude_install(project_dir: Path | None = None) -> None:
|
|
"""Write the graphify section to the local CLAUDE.md."""
|
|
target = (project_dir or Path(".")) / "CLAUDE.md"
|
|
|
|
if target.exists():
|
|
content = target.read_text(encoding="utf-8")
|
|
if _CLAUDE_MD_MARKER in content:
|
|
print("graphify already configured in CLAUDE.md")
|
|
return
|
|
new_content = content.rstrip() + "\n\n" + _CLAUDE_MD_SECTION
|
|
else:
|
|
new_content = _CLAUDE_MD_SECTION
|
|
|
|
target.write_text(new_content, encoding="utf-8")
|
|
print(f"graphify section written to {target.resolve()}")
|
|
|
|
# Also write Claude Code PreToolUse hook to .claude/settings.json
|
|
_install_claude_hook(project_dir or Path("."))
|
|
|
|
print()
|
|
print("Claude Code will now check the knowledge graph before answering")
|
|
print("codebase questions and rebuild it after code changes.")
|
|
|
|
|
|
def _install_claude_hook(project_dir: Path) -> None:
|
|
"""Add graphify PreToolUse hook to .claude/settings.json."""
|
|
settings_path = project_dir / ".claude" / "settings.json"
|
|
settings_path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
if settings_path.exists():
|
|
try:
|
|
settings = json.loads(settings_path.read_text(encoding="utf-8"))
|
|
except json.JSONDecodeError:
|
|
settings = {}
|
|
else:
|
|
settings = {}
|
|
|
|
hooks = settings.setdefault("hooks", {})
|
|
pre_tool = hooks.setdefault("PreToolUse", [])
|
|
|
|
hooks["PreToolUse"] = [h for h in pre_tool if not (h.get("matcher") == "Glob|Grep" and "graphify" in str(h))]
|
|
hooks["PreToolUse"].append(_SETTINGS_HOOK)
|
|
settings_path.write_text(json.dumps(settings, indent=2), encoding="utf-8")
|
|
print(f" .claude/settings.json -> PreToolUse hook registered")
|
|
|
|
|
|
def _uninstall_claude_hook(project_dir: Path) -> None:
|
|
"""Remove graphify PreToolUse hook from .claude/settings.json."""
|
|
settings_path = project_dir / ".claude" / "settings.json"
|
|
if not settings_path.exists():
|
|
return
|
|
try:
|
|
settings = json.loads(settings_path.read_text(encoding="utf-8"))
|
|
except json.JSONDecodeError:
|
|
return
|
|
pre_tool = settings.get("hooks", {}).get("PreToolUse", [])
|
|
filtered = [h for h in pre_tool if not (h.get("matcher") == "Glob|Grep" and "graphify" in str(h))]
|
|
if len(filtered) == len(pre_tool):
|
|
return
|
|
settings["hooks"]["PreToolUse"] = filtered
|
|
settings_path.write_text(json.dumps(settings, indent=2), encoding="utf-8")
|
|
print(f" .claude/settings.json -> PreToolUse hook removed")
|
|
|
|
|
|
def claude_uninstall(project_dir: Path | None = None) -> None:
|
|
"""Remove the graphify section from the local CLAUDE.md."""
|
|
target = (project_dir or Path(".")) / "CLAUDE.md"
|
|
|
|
if not target.exists():
|
|
print("No CLAUDE.md found in current directory - nothing to do")
|
|
return
|
|
|
|
content = target.read_text(encoding="utf-8")
|
|
if _CLAUDE_MD_MARKER not in content:
|
|
print("graphify section not found in CLAUDE.md - nothing to do")
|
|
return
|
|
|
|
# Remove the ## graphify section: from the marker to the next ## heading or EOF
|
|
cleaned = re.sub(
|
|
r"\n*## graphify\n.*?(?=\n## |\Z)",
|
|
"",
|
|
content,
|
|
flags=re.DOTALL,
|
|
).rstrip()
|
|
if cleaned:
|
|
target.write_text(cleaned + "\n", encoding="utf-8")
|
|
print(f"graphify section removed from {target.resolve()}")
|
|
else:
|
|
target.unlink()
|
|
print(f"CLAUDE.md was empty after removal - deleted {target.resolve()}")
|
|
|
|
_uninstall_claude_hook(project_dir or Path("."))
|
|
|
|
|
|
def main() -> None:
|
|
# Check all known skill install locations for a stale version stamp
|
|
for cfg in _PLATFORM_CONFIG.values():
|
|
skill_dst = Path.home() / cfg["skill_dst"]
|
|
_check_skill_version(skill_dst)
|
|
|
|
if len(sys.argv) < 2 or sys.argv[1] in ("-h", "--help"):
|
|
print("Usage: graphify <command>")
|
|
print()
|
|
print("Commands:")
|
|
print(" install [--platform P] copy skill to platform config dir (claude|windows|codex|opencode|aider|claw|droid|trae|trae-cn|gemini|cursor)")
|
|
print(" query \"<question>\" BFS traversal of graph.json for a question")
|
|
print(" --dfs use depth-first instead of breadth-first")
|
|
print(" --budget N cap output at N tokens (default 2000)")
|
|
print(" --graph <path> path to graph.json (default graphify-out/graph.json)")
|
|
print(" save-result save a Q&A result to graphify-out/memory/ for graph feedback loop")
|
|
print(" --question Q the question asked")
|
|
print(" --answer A the answer to save")
|
|
print(" --type T query type: query|path_query|explain (default: query)")
|
|
print(" --nodes N1 N2 ... source node labels cited in the answer")
|
|
print(" --memory-dir DIR memory directory (default: graphify-out/memory)")
|
|
print(" benchmark [graph.json] measure token reduction vs naive full-corpus approach")
|
|
print(" hook install install post-commit/post-checkout git hooks (all platforms)")
|
|
print(" hook uninstall remove git hooks")
|
|
print(" hook status check if git hooks are installed")
|
|
print(" gemini install write GEMINI.md section + BeforeTool hook (Gemini CLI)")
|
|
print(" gemini uninstall remove GEMINI.md section + BeforeTool hook")
|
|
print(" cursor install write .cursor/rules/graphify.mdc (Cursor)")
|
|
print(" cursor uninstall remove .cursor/rules/graphify.mdc")
|
|
print(" claude install write graphify section to CLAUDE.md + PreToolUse hook (Claude Code)")
|
|
print(" claude uninstall remove graphify section from CLAUDE.md + PreToolUse hook")
|
|
print(" codex install write graphify section to AGENTS.md (Codex)")
|
|
print(" codex uninstall remove graphify section from AGENTS.md")
|
|
print(" opencode install write graphify section to AGENTS.md + tool.execute.before plugin (OpenCode)")
|
|
print(" opencode uninstall remove graphify section from AGENTS.md + plugin")
|
|
print(" aider install write graphify section to AGENTS.md (Aider)")
|
|
print(" aider uninstall remove graphify section from AGENTS.md")
|
|
print(" copilot install copy graphify skill to ~/.copilot/skills (GitHub Copilot CLI)")
|
|
print(" copilot uninstall remove graphify skill from ~/.copilot/skills")
|
|
print(" claw install write graphify section to AGENTS.md (OpenClaw)")
|
|
print(" claw uninstall remove graphify section from AGENTS.md")
|
|
print(" droid install write graphify section to AGENTS.md (Factory Droid)")
|
|
print(" droid uninstall remove graphify section from AGENTS.md")
|
|
print(" trae install write graphify section to AGENTS.md (Trae)")
|
|
print(" trae uninstall remove graphify section from AGENTS.md")
|
|
print(" trae-cn install write graphify section to AGENTS.md (Trae CN)")
|
|
print(" trae-cn uninstall remove graphify section from AGENTS.md")
|
|
print()
|
|
return
|
|
|
|
cmd = sys.argv[1]
|
|
if cmd == "install":
|
|
# Default to windows platform on Windows, claude elsewhere
|
|
default_platform = "windows" if platform.system() == "Windows" else "claude"
|
|
chosen_platform = default_platform
|
|
args = sys.argv[2:]
|
|
i = 0
|
|
while i < len(args):
|
|
if args[i].startswith("--platform="):
|
|
chosen_platform = args[i].split("=", 1)[1]
|
|
i += 1
|
|
elif args[i] == "--platform" and i + 1 < len(args):
|
|
chosen_platform = args[i + 1]
|
|
i += 2
|
|
else:
|
|
i += 1
|
|
install(platform=chosen_platform)
|
|
elif cmd == "claude":
|
|
subcmd = sys.argv[2] if len(sys.argv) > 2 else ""
|
|
if subcmd == "install":
|
|
claude_install()
|
|
elif subcmd == "uninstall":
|
|
claude_uninstall()
|
|
else:
|
|
print("Usage: graphify claude [install|uninstall]", file=sys.stderr)
|
|
sys.exit(1)
|
|
elif cmd == "gemini":
|
|
subcmd = sys.argv[2] if len(sys.argv) > 2 else ""
|
|
if subcmd == "install":
|
|
gemini_install()
|
|
elif subcmd == "uninstall":
|
|
gemini_uninstall()
|
|
else:
|
|
print("Usage: graphify gemini [install|uninstall]", file=sys.stderr)
|
|
sys.exit(1)
|
|
elif cmd == "cursor":
|
|
subcmd = sys.argv[2] if len(sys.argv) > 2 else ""
|
|
if subcmd == "install":
|
|
_cursor_install(Path("."))
|
|
elif subcmd == "uninstall":
|
|
_cursor_uninstall(Path("."))
|
|
else:
|
|
print("Usage: graphify cursor [install|uninstall]", file=sys.stderr)
|
|
sys.exit(1)
|
|
elif cmd == "copilot":
|
|
subcmd = sys.argv[2] if len(sys.argv) > 2 else ""
|
|
if subcmd == "install":
|
|
install(platform="copilot")
|
|
elif subcmd == "uninstall":
|
|
skill_dst = Path.home() / _PLATFORM_CONFIG["copilot"]["skill_dst"]
|
|
removed = []
|
|
if skill_dst.exists():
|
|
skill_dst.unlink()
|
|
removed.append(f"skill removed: {skill_dst}")
|
|
version_file = skill_dst.parent / ".graphify_version"
|
|
if version_file.exists():
|
|
version_file.unlink()
|
|
for d in (skill_dst.parent, skill_dst.parent.parent, skill_dst.parent.parent.parent):
|
|
try:
|
|
d.rmdir()
|
|
except OSError:
|
|
break
|
|
print("; ".join(removed) if removed else "nothing to remove")
|
|
else:
|
|
print("Usage: graphify copilot [install|uninstall]", file=sys.stderr)
|
|
sys.exit(1)
|
|
elif cmd in ("aider", "codex", "opencode", "claw", "droid", "trae", "trae-cn"):
|
|
subcmd = sys.argv[2] if len(sys.argv) > 2 else ""
|
|
if subcmd == "install":
|
|
_agents_install(Path("."), cmd)
|
|
elif subcmd == "uninstall":
|
|
_agents_uninstall(Path("."))
|
|
if cmd == "codex":
|
|
_uninstall_codex_hook(Path("."))
|
|
else:
|
|
print(f"Usage: graphify {cmd} [install|uninstall]", file=sys.stderr)
|
|
sys.exit(1)
|
|
elif cmd == "hook":
|
|
from graphify.hooks import install as hook_install, uninstall as hook_uninstall, status as hook_status
|
|
subcmd = sys.argv[2] if len(sys.argv) > 2 else ""
|
|
if subcmd == "install":
|
|
print(hook_install(Path(".")))
|
|
elif subcmd == "uninstall":
|
|
print(hook_uninstall(Path(".")))
|
|
elif subcmd == "status":
|
|
print(hook_status(Path(".")))
|
|
else:
|
|
print("Usage: graphify hook [install|uninstall|status]", file=sys.stderr)
|
|
sys.exit(1)
|
|
elif cmd == "query":
|
|
if len(sys.argv) < 3:
|
|
print("Usage: graphify query \"<question>\" [--dfs] [--budget N] [--graph path]", file=sys.stderr)
|
|
sys.exit(1)
|
|
from graphify.serve import _score_nodes, _bfs, _dfs, _subgraph_to_text
|
|
from graphify.security import sanitize_label
|
|
from networkx.readwrite import json_graph
|
|
question = sys.argv[2]
|
|
use_dfs = "--dfs" in sys.argv
|
|
budget = 2000
|
|
graph_path = "graphify-out/graph.json"
|
|
args = sys.argv[3:]
|
|
i = 0
|
|
while i < len(args):
|
|
if args[i] == "--budget" and i + 1 < len(args):
|
|
try:
|
|
budget = int(args[i + 1])
|
|
except ValueError:
|
|
print(f"error: --budget must be an integer", file=sys.stderr)
|
|
sys.exit(1)
|
|
i += 2
|
|
elif args[i].startswith("--budget="):
|
|
try:
|
|
budget = int(args[i].split("=", 1)[1])
|
|
except ValueError:
|
|
print(f"error: --budget must be an integer", file=sys.stderr)
|
|
sys.exit(1)
|
|
i += 1
|
|
elif args[i] == "--graph" and i + 1 < len(args):
|
|
graph_path = args[i + 1]; i += 2
|
|
else:
|
|
i += 1
|
|
# Load graph directly — validate_graph_path restricts to graphify-out/
|
|
# so for custom --graph paths we resolve and load directly after existence check
|
|
gp = Path(graph_path).resolve()
|
|
if not gp.exists():
|
|
print(f"error: graph file not found: {gp}", file=sys.stderr)
|
|
sys.exit(1)
|
|
if not gp.suffix == ".json":
|
|
print(f"error: graph file must be a .json file", file=sys.stderr)
|
|
sys.exit(1)
|
|
try:
|
|
import json as _json
|
|
import networkx as _nx
|
|
_raw = _json.loads(gp.read_text(encoding="utf-8"))
|
|
try:
|
|
G = json_graph.node_link_graph(_raw, edges="links")
|
|
except TypeError:
|
|
G = json_graph.node_link_graph(_raw)
|
|
except Exception as exc:
|
|
print(f"error: could not load graph: {exc}", file=sys.stderr)
|
|
sys.exit(1)
|
|
terms = [t.lower() for t in question.split() if len(t) > 2]
|
|
scored = _score_nodes(G, terms)
|
|
if not scored:
|
|
print("No matching nodes found.")
|
|
sys.exit(0)
|
|
start = [nid for _, nid in scored[:5]]
|
|
nodes, edges = (_dfs if use_dfs else _bfs)(G, start, depth=2)
|
|
print(_subgraph_to_text(G, nodes, edges, token_budget=budget))
|
|
elif cmd == "save-result":
|
|
# graphify save-result --question Q --answer A --type T [--nodes N1 N2 ...]
|
|
import argparse as _ap
|
|
p = _ap.ArgumentParser(prog="graphify save-result")
|
|
p.add_argument("--question", required=True)
|
|
p.add_argument("--answer", required=True)
|
|
p.add_argument("--type", dest="query_type", default="query")
|
|
p.add_argument("--nodes", nargs="*", default=[])
|
|
p.add_argument("--memory-dir", default="graphify-out/memory")
|
|
opts = p.parse_args(sys.argv[2:])
|
|
from graphify.ingest import save_query_result as _sqr
|
|
out = _sqr(
|
|
question=opts.question,
|
|
answer=opts.answer,
|
|
memory_dir=Path(opts.memory_dir),
|
|
query_type=opts.query_type,
|
|
source_nodes=opts.nodes or None,
|
|
)
|
|
print(f"Saved to {out}")
|
|
elif cmd == "benchmark":
|
|
from graphify.benchmark import run_benchmark, print_benchmark
|
|
graph_path = sys.argv[2] if len(sys.argv) > 2 else "graphify-out/graph.json"
|
|
# Try to load corpus_words from detect output
|
|
corpus_words = None
|
|
detect_path = Path(".graphify_detect.json")
|
|
if detect_path.exists():
|
|
try:
|
|
detect_data = json.loads(detect_path.read_text(encoding="utf-8"))
|
|
corpus_words = detect_data.get("total_words")
|
|
except Exception:
|
|
pass
|
|
result = run_benchmark(graph_path, corpus_words=corpus_words)
|
|
print_benchmark(result)
|
|
else:
|
|
print(f"error: unknown command '{cmd}'", file=sys.stderr)
|
|
print("Run 'graphify --help' for usage.", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|