#!/bin/bash # Voxyz Daily Mission Generation Cron Job # Runs at 7:00 AM daily to generate mission proposals # Phase 10: Autonomous Architecture set -e # Configuration API_BASE_URL="${VOXYZ_API_URL:-http://localhost:3001/api/voxyz}" API_KEY="${VOXYZ_API_KEY:-}" LOG_FILE="${VOXYZ_LOG_FILE:-/tmp/voxyz-daily-missions.log}" # Timestamp TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S') echo "[$TIMESTAMP] Starting Voxyz daily mission generation..." >> "$LOG_FILE" # Function to make API calls make_api_call() { local endpoint=$1 local method=$2 local data=$3 if [ -n "$API_KEY" ]; then curl -s -X "$method" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $API_KEY" \ -d "$data" \ "${API_BASE_URL}${endpoint}" else curl -s -X "$method" \ -H "Content-Type: application/json" \ -d "$data" \ "${API_BASE_URL}${endpoint}" fi } # Step 1: Check if missions already generated for today echo "[$TIMESTAMP] Checking for existing daily missions..." >> "$LOG_FILE" EXISTING=$(make_api_call "/daily-missions" "GET" "") if echo "$EXISTING" | grep -q '"dailyMission":null'; then echo "[$TIMESTAMP] No existing daily mission found. Generating..." >> "$LOG_FILE" # Step 2: Generate daily missions RESULT=$(make_api_call "/daily-missions" "POST" '{"targetMissionCount": 3, "includeReactions": true}') if echo "$RESULT" | grep -q '"dailyMission"'; then echo "[$TIMESTAMP] Daily missions generated successfully" >> "$LOG_FILE" # Extract proposal count PROPOSAL_COUNT=$(echo "$RESULT" | grep -o '"proposals":\[[^]]*\]' | tr ',' '\n' | wc -l) echo "[$TIMESTAMP] Generated $PROPOSAL_COUNT proposals" >> "$LOG_FILE" # Step 3: Run trigger monitor to check for urgent items echo "[$TIMESTAMP] Running trigger monitor..." >> "$LOG_FILE" TRIGGER_RESULT=$(make_api_call "/trigger-rules" "POST" '{"action": "run-monitor"}') echo "[$TIMESTAMP] Trigger monitor completed" >> "$LOG_FILE" # Step 4: Clean up expired proposals echo "[$TIMESTAMP] Cleaning up expired proposals..." >> "$LOG_FILE" EXPIRED_RESULT=$(make_api_call "/proposals" "DELETE" "") EXPIRED_COUNT=$(echo "$EXPIRED_RESULT" | grep -o '"expiredCount":[0-9]*' | cut -d: -f2) echo "[$TIMESTAMP] Expired $EXPIRED_COUNT old proposals" >> "$LOG_FILE" echo "[$TIMESTAMP] Daily mission generation completed successfully" >> "$LOG_FILE" exit 0 else echo "[$TIMESTAMP] ERROR: Failed to generate daily missions" >> "$LOG_FILE" echo "[$TIMESTAMP] Response: $RESULT" >> "$LOG_FILE" exit 1 fi else echo "[$TIMESTAMP] Daily missions already exist for today. Skipping generation." >> "$LOG_FILE" exit 0 fi