Shell
#!/bin/bash
# claude-corner-observer.sh
# Automatically logs observations to ID8Labs Claude Corner (Supabase)
# Triggered by: commits, PR merges, deployments, session milestones
#
# This hook detects significant events and posts them to the
# /api/claude-observations endpoint which stores them in Supabase.
set -euo pipefail
# Configuration
API_URL="${CLAUDE_CORNER_API_URL:-https://id8labs.app/api/claude-observations}"
LOCAL_API_URL="http://localhost:3000/api/claude-observations"
# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'
log() { echo -e "${BLUE}[OBSERVER]${NC} $1"; }
success() { echo -e "${GREEN}[✓]${NC} $1"; }
warn() { echo -e "${YELLOW}[!]${NC} $1"; }
# Detect if we're in ID8Labs project
is_id8labs_project() {
local current_dir=$(pwd)
[[ "$current_dir" == *"id8labs"* ]] || [[ -f "package.json" && $(grep -q '"name": "id8labs"' package.json 2>/dev/null && echo "yes") == "yes" ]]
}
# Try localhost first (for dev), fall back to production
post_observation() {
local text="$1"
local category="${2:-observation}"
local date="${3:-$(date +%Y-%m-%d)}"
local payload=$(cat <<EOF
{
"text": "$text",
"category": "$category",
"date": "$date"
}
EOF
)
# Try localhost first (bypasses auth in dev)
local response=$(curl -s -X POST "$LOCAL_API_URL" \
-H "Content-Type: application/json" \
-d "$payload" 2>/dev/null || echo "")
if [[ "$response" == *'"created":true'* ]]; then
success "Observation logged via localhost"
return 0
fi
# Fall back to production (requires API key)
if [[ -n "${CLAUDE_OBSERVATIONS_API_KEY:-}" ]]; then
response=$(curl -s -X POST "$API_URL" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $CLAUDE_OBSERVATIONS_API_KEY" \
-d "$payload" 2>/dev/null || echo "")
if [[ "$response" == *'"created":true'* ]]; then
success "Observation logged via production API"
return 0
fi
fi
warn "Could not log observation (API unavailable)"
return 1
}
# Detect milestone from commit message
detect_milestone_from_commit() {
local commit_msg="$1"
# Keywords that indicate milestones
if [[ "$commit_msg" =~ (ship|launch|release|deploy|v[0-9]+\.[0-9]+|complete|finish|implement).*(course|feature|product|module|api|integration) ]]; then
echo "milestone"
elif [[ "$commit_msg" =~ (fix|bug|patch|hotfix) ]]; then
echo "observation"
elif [[ "$commit_msg" =~ (add|create|build|implement) ]]; then
echo "build"
else
echo ""
fi
}
# Generate observation text from commit
generate_observation_from_commit() {
local commit_msg="$1"
local files_changed="${2:-}"
# Clean up commit message for observation
local clean_msg=$(echo "$commit_msg" | head -1 | sed 's/^feat:\s*//i; s/^fix:\s*//i; s/^chore:\s*//i')
# Add context based on files
if [[ "$files_changed" == *"course"* ]]; then
echo "Shipped new course content: $clean_msg"
elif [[ "$files_changed" == *"api"* ]]; then
echo "API update: $clean_msg"
elif [[ "$files_changed" == *"component"* ]]; then
echo "UI shipped: $clean_msg"
else
echo "$clean_msg"
fi
}
# Main hook logic - called after git commits
handle_post_commit() {
if ! is_id8labs_project; then
exit 0
fi
# Get last commit info
local commit_msg=$(git log -1 --pretty=%B 2>/dev/null || echo "")
local files_changed=$(git diff-tree --no-commit-id --name-only -r HEAD 2>/dev/null || echo "")
# Skip if no commit message or if it's a minor commit
if [[ -z "$commit_msg" ]] || [[ ${#commit_msg} -lt 20 ]]; then
exit 0
fi
# Skip merge commits and automated commits
if [[ "$commit_msg" == *"Merge"* ]] || [[ "$commit_msg" == *"Generated with"* ]]; then
exit 0
fi
# Detect if this is worth logging
local category=$(detect_milestone_from_commit "$commit_msg")
if [[ -n "$category" ]]; then
local observation=$(generate_observation_from_commit "$commit_msg" "$files_changed")
log "Detected $category: $observation"
post_observation "$observation" "$category"
fi
}
# Handle milestone trigger from PUT endpoint format
handle_milestone_trigger() {
local trigger_type="${1:-}"
local trigger_data="${2:-}"
case "$trigger_type" in
"course")
post_observation "Shipped new course: $trigger_data" "milestone"
;;
"feature")
post_observation "Feature complete: $trigger_data" "milestone"
;;
"deploy")
post_observation "Deployed to production: $trigger_data" "build"
;;
"session")
post_observation "$trigger_data" "insight"
;;
*)
warn "Unknown trigger type: $trigger_type"
exit 1
;;
esac
}
# CLI interface
main() {
local command="${1:-auto}"
case "$command" in
"auto"|"post-commit")
handle_post_commit
;;
"milestone"|"feature"|"course"|"deploy"|"session")
shift
handle_milestone_trigger "$command" "$*"
;;
"test")
log "Testing observation system..."
post_observation "Test observation from hook - $(date +%H:%M:%S)" "observation"
;;
"help")
echo "Usage: claude-corner-observer.sh [command] [args]"
echo ""
echo "Commands:"
echo " auto, post-commit - Auto-detect from git commit (default)"
echo " milestone <text> - Log a milestone"
echo " feature <text> - Log a feature completion"
echo " course <text> - Log a course shipment"
echo " deploy <text> - Log a deployment"
echo " session <text> - Log a session insight"
echo " test - Test the observation system"
;;
*)
warn "Unknown command: $command"
exit 1
;;
esac
}
main "$@"