#!/bin/sh # Alter Ego: journal hooks for Claude Code and Codex. Run it yourself, it changes only the hooks of your own config. # curl -fsSL https://alterego.cafeboon.com/install-hooks.sh | sh # curl -fsSL https://alterego.cafeboon.com/install-hooks.sh | sh -s -- --uninstall # Options: --claude or --codex (only that tool), --uninstall, --server NAME (Claude Code connector name, default # "claude.ai Alter Ego"). Needs python3, which macOS and most Linux systems have. Safe to run again. set -eu command -v python3 >/dev/null 2>&1 || { echo "python3 is needed to edit the JSON config. Install it and run this again." >&2; exit 1; } exec python3 - "$@" <<'PY' import json, os, sys, tempfile from pathlib import Path def args(): a = sys.argv[1:] out = {"uninstall": "--uninstall" in a, "claude": "--claude" in a, "codex": "--codex" in a, "server": "claude.ai Alter Ego"} if "--server" in a: out["server"] = a[a.index("--server") + 1] return out def hook(server, turn_id, role, message): return {"type": "mcp_tool", "server": server, "tool": "record_turn", "input": {"turn_id": turn_id, "role": role, "message": message}, "timeout": 30, "onFailure": "continue"} def claude_hooks(server): t = "claude-${session_id}" return {"UserPromptSubmit": [{"hooks": [hook(server, t, "user", "${prompt}")]}], "Stop": [{"hooks": [hook(server, t, "assistant", "${last_assistant_message}")]}], "SubagentStop": [{"matcher": ".", "hooks": [hook(server, t + "-subagent", "assistant", "${last_assistant_message}")]}]} def codex_hooks(): def h(role, message): return {"type": "mcp_tool", "server": "alter_ego", "tool": "record_turn", "input": {"turn_id": "${turn_id}", "role": role, "message": message}, "timeout": 30, "statusMessage": "Saving " + role + " message to Alter Ego"} return {"UserPromptSubmit": [{"hooks": [h("user", "${prompt}")]}], "Stop": [{"hooks": [h("assistant", "${last_assistant_message}")]}]} def ours(group, servers): inner = group.get("hooks") if isinstance(group, dict) else None return bool(inner) and all(isinstance(x, dict) and x.get("type") == "mcp_tool" and x.get("tool") == "record_turn" and x.get("server") in servers for x in inner) def edit(path, wanted, servers, uninstall, label): config = json.loads(path.read_text(encoding="utf-8") or "{}") if path.exists() else {} hooks = config.setdefault("hooks", {}) changed = False for event, groups in wanted.items(): existing = hooks.setdefault(event, []) if uninstall: kept = [g for g in existing if not ours(g, servers)] changed |= len(kept) != len(existing) hooks[event] = kept else: for g in groups: if g not in existing: existing.append(g) changed = True if not hooks[event]: del hooks[event] if not hooks: del config["hooks"] if changed: path.parent.mkdir(parents=True, exist_ok=True) with tempfile.NamedTemporaryFile("w", encoding="utf-8", dir=path.parent, delete=False) as f: json.dump(config, f, indent=2) f.write("\n") os.replace(f.name, path) print(label + ": " + ("hooks removed" if uninstall else "hooks installed") + " in " + str(path) if changed else label + ": nothing to change in " + str(path)) o = args() home = Path.home() claude_dir = Path(os.environ.get("CLAUDE_CONFIG_DIR") or home / ".claude") codex_dir = Path(os.environ.get("CODEX_HOME") or home / ".codex") do_claude = o["claude"] or (not o["codex"] and claude_dir.is_dir()) do_codex = o["codex"] or (not o["claude"] and codex_dir.is_dir()) if not (do_claude or do_codex): sys.exit("Neither " + str(claude_dir) + " nor " + str(codex_dir) + " exists. Use --claude or --codex.") try: if do_claude: edit(claude_dir / "settings.json", claude_hooks(o["server"]), {o["server"]}, o["uninstall"], "Claude Code") if do_codex: edit(codex_dir / "hooks.json", codex_hooks(), {"alter_ego"}, o["uninstall"], "Codex") except ValueError as e: sys.exit("Could not read the config as JSON (" + str(e) + "). Nothing was changed.") if not o["uninstall"]: if do_claude: print("Claude Code: restart it. The connector must be called '" + o["server"] + "' in /mcp, or pass --server NAME.") if do_codex: print("Codex: run 'codex mcp add alter_ego --url https://alterego.cafeboon.com/mcp' and 'codex mcp login alter_ego', then trust the hooks in Codex.") PY