#!/usr/bin/env bash
set -euo pipefail
if ! command -v jq &>/dev/null; then
echo '{"continue":true}'
exit 0
fi
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
RECORD_USAGE="$SCRIPT_DIR/record-usage.js"
INPUT=$(cat)
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name // empty' 2>/dev/null || echo "")
TOOL_INPUT=$(echo "$INPUT" | jq -r '.tool_input // empty' 2>/dev/null || echo "")
if [ -z "$TOOL_NAME" ]; then
TOOL_NAME="${TOOL_NAME:-}"
fi
if [ -z "$TOOL_NAME" ]; then
echo '{"continue":true}'
exit 0
fi
case "$TOOL_NAME" in
Skill)
SKILL_NAME=$(echo "$TOOL_INPUT" | jq -r '.skill // empty' 2>/dev/null || echo "")
if [ -n "$SKILL_NAME" ]; then
BASE_NAME=$(echo "$SKILL_NAME" | sed 's/.*://')
node "$RECORD_USAGE" skill "$BASE_NAME" >/dev/null 2>&1 || true
if [[ "$BASE_NAME" == "sync-ssot-from-memory" ]] || [[ "$SKILL_NAME" == *"sync-ssot-from-memory"* ]] || [[ "$BASE_NAME" == "memory" ]] || [[ "$SKILL_NAME" == *":memory"* ]]; then
CWD=$(echo "$INPUT" | jq -r '.cwd // empty' 2>/dev/null)
CWD="${CWD:-$(pwd)}"
REPO_ROOT=$(git -C "$CWD" rev-parse --show-toplevel 2>/dev/null) || REPO_ROOT="$CWD"
STATE_DIR="${REPO_ROOT}/.claude/state"
mkdir -p "$STATE_DIR" 2>/dev/null || true
touch "${STATE_DIR}/.ssot-synced-this-session" 2>/dev/null || true
fi
fi
;;
SlashCommand)
CMD_NAME=$(echo "$TOOL_INPUT" | jq -r '.command // .name // empty' 2>/dev/null || echo "")
if [ -n "$CMD_NAME" ]; then
BASE_NAME=$(echo "$CMD_NAME" | sed 's/^\///')
node "$RECORD_USAGE" command "$BASE_NAME" >/dev/null 2>&1 || true
if [[ "$BASE_NAME" == *"sync-ssot-from-memory"* ]] || [[ "$BASE_NAME" == "memory" ]]; then
CWD=$(echo "$INPUT" | jq -r '.cwd // empty' 2>/dev/null)
CWD="${CWD:-$(pwd)}"
REPO_ROOT=$(git -C "$CWD" rev-parse --show-toplevel 2>/dev/null) || REPO_ROOT="$CWD"
STATE_DIR="${REPO_ROOT}/.claude/state"
mkdir -p "$STATE_DIR" 2>/dev/null || true
touch "${STATE_DIR}/.ssot-synced-this-session" 2>/dev/null || true
fi
fi
;;
Task)
AGENT_TYPE=$(echo "$TOOL_INPUT" | jq -r '.subagent_type // empty' 2>/dev/null || echo "")
if [ -n "$AGENT_TYPE" ]; then
node "$RECORD_USAGE" agent "$AGENT_TYPE" >/dev/null 2>&1 || true
fi
;;
esac
echo '{"continue":true}'