#!/bin/bash set -e echo "🚀 Starting LLM API Backend..." echo "📍 Working directory: $(pwd)" echo "🔢 Node version: $(node --version)" # Change to backend directory cd /app/backend # Verify Encore CLI is available if ! command -v encore &> /dev/null; then echo "❌ ERROR: Encore CLI not found in PATH" echo "Available binaries:" ls -la /usr/local/bin/ | grep encore || echo "No encore binary found" ls -la /root/.encore/bin/ 2>/dev/null | grep encore || echo "No encore binary in home" exit 1 fi echo "✅ Encore CLI found: $(which encore)" echo "📦 Encore version: $(encore version)" # Configure secrets from environment variables echo "🔐 Configuring secrets from environment..." if [ -z "$LLMProvider" ]; then echo "⚠️ Warning: LLMProvider not set. Please configure in Space settings." echo " Add: LLMProvider = 'huggingface' or 'ollama'" else echo "✅ LLMProvider: $LLMProvider" encore secret set --type=local LLMProvider "$LLMProvider" 2>/dev/null || echo " Note: Secret may already be set" fi if [ "$LLMProvider" = "huggingface" ]; then if [ -z "$HuggingFaceAPIKey" ]; then echo "❌ ERROR: HuggingFaceAPIKey required for Hugging Face provider" exit 1 else echo "✅ HuggingFaceAPIKey configured" encore secret set --type=local HuggingFaceAPIKey "$HuggingFaceAPIKey" 2>/dev/null || echo " Note: Secret may already be set" fi if [ -n "$DefaultModel" ]; then echo "✅ DefaultModel: $DefaultModel" encore secret set --type=local DefaultModel "$DefaultModel" 2>/dev/null || echo " Note: Secret may already be set" fi fi if [ "$LLMProvider" = "ollama" ]; then if [ -z "$OllamaBaseURL" ]; then echo "⚠️ Warning: OllamaBaseURL not set for Ollama provider" echo " Add: OllamaBaseURL = 'http://localhost:11434'" else echo "✅ OllamaBaseURL: $OllamaBaseURL" encore secret set --type=local OllamaBaseURL "$OllamaBaseURL" 2>/dev/null || echo " Note: Secret may already be set" fi fi # Start the application echo "" echo "✨ Starting Encore application on port ${PORT:-7860}..." echo "📡 Health endpoint: http://localhost:${PORT:-7860}/health" echo "💬 Chat endpoint: http://localhost:${PORT:-7860}/chat" echo "" exec encore run --port="${PORT:-7860}"