#!/bin/bash # TypeScript-based issue handler using Claude Agent SDK # This script replaces the old bash-based Claude Code implementation # Ensure HOME is set (systemd might not set it) if [ -z "$HOME" ]; then export HOME=$(getent passwd "$(whoami)" | cut -d: -f6) fi # Add NODE to PATH export PATH="$HOME/.local/bin:$PATH" # Ensure Claude Code has writable directories for cache and config export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}" export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}" export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" export XDG_STATE_HOME="${XDG_STATE_HOME:-$HOME/.local/state}" # Create these directories if they don't exist mkdir -p "$XDG_CACHE_HOME" "$XDG_CONFIG_HOME" "$XDG_DATA_HOME" "$XDG_STATE_HOME" # Parse arguments OWNER="$1" REPO="$2" PUSHER="$3" ISSUE_NUMBER="$4" ISSUE_TITLE="$5" ISSUE_BODY="$6" ISSUE_ACTION="$7" COMMENT_BODY="$8" ASSIGNEE="$9" # Exit if pusher is claude AND assignee is NOT claude (avoid loops) # Allow Claude to work on issues it created and assigned to itself if [ "$PUSHER" = "claude" ] && [ "$ASSIGNEE" != "claude" ]; then echo "Pusher is claude and issue not assigned to claude, skipping automation to avoid loops" exit 0 fi # Exit if issue is not assigned to claude if [ "$ASSIGNEE" != "claude" ]; then echo "Issue not assigned to claude (assignee: $ASSIGNEE), skipping" exit 0 fi # Exit if issue action is 'closed' (but allow 'reopened') if [ "$ISSUE_ACTION" = "closed" ]; then echo "Issue action is 'closed', skipping automation" exit 0 fi # Determine repo path REPO_PATH="$HOME/$REPO" # Check if repo exists, if not clone it if [ ! -d "$REPO_PATH" ]; then echo "Repository not found at $REPO_PATH, cloning..." mkdir -p "$HOME" GIT_URL="ssh://git@207.154.220.231:10022/$OWNER/$REPO.git" git clone "$GIT_URL" "$REPO_PATH" if [ $? -ne 0 ]; then echo "Failed to clone repository" exit 1 fi echo "Repository cloned successfully" else echo "Repository found at $REPO_PATH" # Pull latest changes cd "$REPO_PATH" || exit 1 echo "Pulling latest changes..." # Try to pull, but don't fail if it errors (might be file system issues) if ! git pull 2>&1; then echo "Warning: git pull failed, continuing with existing repository state" fi fi # Change to repo directory cd "$REPO_PATH" || exit 1 echo "Working directory: $(pwd)" # Load AGENT_MANAGER_PATH from project .env file # Try multiple possible locations for the agent-manager .env file AGENT_MANAGER_ENV_PATHS=( "/home/claude/agent-manager/.env" "/data/agent-manager/.env" "$HOME/agent-manager/.env" ) AGENT_MANAGER_PATH="" for ENV_PATH in "${AGENT_MANAGER_ENV_PATHS[@]}"; do if [ -f "$ENV_PATH" ]; then # Load AGENT_MANAGER_PATH from .env if it exists AGENT_MANAGER_PATH=$(grep -E "^AGENT_MANAGER_PATH=" "$ENV_PATH" | cut -d '=' -f2-) if [ -n "$AGENT_MANAGER_PATH" ]; then echo "Loaded AGENT_MANAGER_PATH from: $ENV_PATH" break fi fi done # Fallback: try to detect from script location if [ -z "$AGENT_MANAGER_PATH" ]; then # Get the directory where this script is located SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Go up one level from scripts/ to get agent-manager root AGENT_MANAGER_PATH="$(dirname "$SCRIPT_DIR")" echo "Using detected path from script location: $AGENT_MANAGER_PATH" fi echo "Calling Claude Agent SDK TypeScript client..." echo "Agent Manager Path: $AGENT_MANAGER_PATH" echo "---" # Call the TypeScript client node "$AGENT_MANAGER_PATH/client/dist/index.js" \ "$OWNER" \ "$REPO" \ "$PUSHER" \ "$ISSUE_NUMBER" \ "$ISSUE_TITLE" \ "$ISSUE_BODY" \ "$ISSUE_ACTION" \ "${COMMENT_BODY:-}" \ "${ASSIGNEE:-}" EXIT_CODE=$? echo "---" echo "Claude Agent SDK execution completed (exit code: $EXIT_CODE)" exit $EXIT_CODE