#!/bin/bash
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
echo "Usage: nop-run-multi-variants.sh \"<user-requirement>\""
echo ""
echo "Options:"
echo " --help, -h Show this help message"
echo ""
echo "This script can be called from any directory and will:"
echo " 1. Locate project root directory containing .opencode/script/"
echo " 2. Find or create ai-tasks/ directory in project root"
echo " 3. Generate random filename and create output file"
echo " 4. Call nop-ai run to generate configuration"
echo " 5. Execute nop-batch-worktree with generated file"
echo ""
echo "Examples:"
echo " nop-run-multi-variants.sh \"Create a user login feature\""
echo " nop-run-multi-variants.sh \"Implement image upload, generate 5 solutions\""
exit 0
fi
log_debug() {
echo "[DEBUG] $1" >&2
}
find_project_root() {
local current_dir="$(pwd)"
local max_depth=5
local depth=0
log_debug "Finding project root from: $current_dir"
while [ $depth -lt $max_depth ]; do
if [ -d "$current_dir/.git" ]; then
log_debug "Found .git directory at: $current_dir (depth: $depth)"
echo "$current_dir"
return 0
fi
current_dir="$(dirname "$current_dir")"
((depth++))
done
log_debug "Project root not found after searching $max_depth levels"
return 1
}
log_debug "Finding project root..."
PROJECT_ROOT=$(find_project_root)
if [ $? -ne 0 ]; then
echo "Error: Could not find project root directory"
echo "Please run this script from within a git repository"
exit 1
fi
log_debug "Project root found: $PROJECT_ROOT"
AI_TASKS_DIR="$PROJECT_ROOT/ai-tasks"
log_debug "AI tasks directory: $AI_TASKS_DIR"
if [ $# -lt 1 ]; then
echo "Usage: $0 \"<user-requirement>\""
echo ""
echo "Example:"
echo " $0 \"Create a user login feature\""
echo " $0 \"Implement image upload, generate 5 solutions\""
exit 1
fi
USER_REQUIREMENT="$1"
log_debug "User requirement: $USER_REQUIREMENT"
RANDOM_STR=$(head /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 8)
OUTPUT_FILENAME="task-${RANDOM_STR}.txt"
OUTPUT_FILE="$AI_TASKS_DIR/$OUTPUT_FILENAME"
log_debug "Generated filename: $OUTPUT_FILENAME"
log_debug "Output file path: $OUTPUT_FILE"
if [ ! -d "$AI_TASKS_DIR" ]; then
echo "Creating directory: $AI_TASKS_DIR"
mkdir -p "$AI_TASKS_DIR"
log_debug "Created ai-tasks directory"
else
log_debug "AI tasks directory already exists"
fi
echo "=========================================="
echo "Auto-generating multi-solution config"
echo "=========================================="
echo "Requirement: $USER_REQUIREMENT"
echo "Project root: $PROJECT_ROOT"
echo "Output file: $OUTPUT_FILE"
echo ""
echo "Generating configuration..."
echo ""
log_debug "Preparing nop-ai command..."
FULL_MESSAGE=$(printf "@.opencode/command/gen-multi-variants.md --output=%s %s" "$OUTPUT_FILE" "$USER_REQUIREMENT")
log_debug "Full message: $FULL_MESSAGE"
START_TIME=$(date +%s)
log_debug "Start time: $START_TIME"
if ! nop-ai run "$FULL_MESSAGE"; then
echo ""
echo "Error: Failed to generate configuration file"
log_debug "nop-ai run failed"
exit 1
fi
END_TIME=$(date +%s)
ELAPSED=$((END_TIME - START_TIME))
MINUTES=$((ELAPSED / 60))
SECONDS_REMAINDER=$((ELAPSED % 60))
log_debug "nop-ai end time: $END_TIME"
log_debug "Time elapsed: ${MINUTES}m ${SECONDS_REMAINDER}s"
echo ""
echo "=========================================="
echo "Configuration generated successfully!"
echo "=========================================="
echo "[ELAPSED] Time elapsed: ${MINUTES}m ${SECONDS_REMAINDER}s"
if [ ! -f "$OUTPUT_FILE" ]; then
echo "Error: Configuration file was not created at $OUTPUT_FILE"
log_debug "Output file not found!"
exit 1
fi
log_debug "Output file created successfully"
echo ""
echo "Preview:"
echo "---"
head -20 "$OUTPUT_FILE"
echo "..."
echo ""
SECTIONS=$(grep -c "^>>>" "$OUTPUT_FILE" 2>/dev/null || echo "0")
log_debug "Found $SECTIONS solution(s)"
echo "Generated $SECTIONS solutions"
echo ""
echo "Executing nop-batch-worktree..."
echo ""
log_debug "Calling: nop-batch-worktree \"$OUTPUT_FILE\""
if ! nop-batch-worktree "$OUTPUT_FILE"; then
echo ""
echo "Error: Failed to execute batch-worktree"
log_debug "Batch worktree execution failed"
exit 1
fi
log_debug "Batch worktree completed successfully"
echo ""
echo "=========================================="
echo "All tasks completed!"
echo "=========================================="
echo ""
echo "To clean up temporary branches:"
echo " nop-clean-tmp-branches"