Spaces:
Runtime error
Runtime error
set -e | |
# Check for required environment variables | |
if [ -z "$GOOGLE_GENERATIVE_AI_API_KEY" ]; then | |
echo "Error: GOOGLE_GENERATIVE_AI_API_KEY environment variable is not set." | |
echo "Please set this variable to your Google AI API key." | |
exit 1 | |
fi | |
# Set default values for model if not provided | |
if [ -z "$MODEL_CHOICE" ]; then | |
export MODEL_CHOICE="gemini-2.5-pro-preview-03-25" | |
echo "MODEL_CHOICE not set, defaulting to $MODEL_CHOICE" | |
fi | |
if [ -z "$MODEL_PROVIDER" ]; then | |
export MODEL_PROVIDER="google" | |
echo "MODEL_PROVIDER not set, defaulting to $MODEL_PROVIDER" | |
fi | |
# Start the Sequential Thinking MCP server in the background | |
echo "Starting Sequential Thinking MCP server..." | |
npx -y @modelcontextprotocol/server-sequential-thinking & | |
SEQUENTIAL_THINKING_PID=$! | |
# Start the Cognee MCP server in the background | |
echo "Starting Cognee MCP server..." | |
if [ -d "/app/cognee/cognee-mcp" ]; then | |
cd /app/cognee/cognee-mcp | |
if [ -f "src/server.py" ]; then | |
python -m src.server & | |
COGNEE_PID=$! | |
else | |
echo "Cognee MCP server not found, skipping..." | |
fi | |
else | |
echo "Cognee MCP directory not found, skipping..." | |
fi | |
# Start the MCP agents API in the background | |
echo "Starting MCP agents API..." | |
cd /app/mcp-agents | |
if [ -f "api.py" ]; then | |
uvicorn api:app --host 0.0.0.0 --port 8080 & | |
MCP_AGENTS_PID=$! | |
else | |
echo "MCP agents API not found, skipping..." | |
fi | |
# Start the AI Co-Scientist API in the background | |
echo "Starting AI Co-Scientist API..." | |
cd /app/ai-co-scientist | |
# Set environment variables directly instead of creating config files | |
echo "Setting up AI Co-Scientist configuration..." | |
# Set environment variables that the AI Co-Scientist will use directly | |
export AI_CO_SCIENTIST_MODEL="$MODEL_PROVIDER/$MODEL_CHOICE" | |
export AI_CO_SCIENTIST_API_KEY="$GOOGLE_GENERATIVE_AI_API_KEY" | |
export AI_CO_SCIENTIST_LOG_LEVEL="INFO" | |
export AI_CO_SCIENTIST_HOST="0.0.0.0" | |
export AI_CO_SCIENTIST_PORT="8000" | |
export AI_CO_SCIENTIST_RELOAD="False" | |
# Launch the AI Co-Scientist directly with environment variables | |
if [ -d "app" ] && [ -f "app/main.py" ]; then | |
uvicorn app.main:app --host 0.0.0.0 --port 8000 & | |
AI_CO_SCIENTIST_PID=$! | |
elif [ -f "main.py" ]; then | |
uvicorn main:app --host 0.0.0.0 --port 8000 & | |
AI_CO_SCIENTIST_PID=$! | |
else | |
echo "AI Co-Scientist API not found, skipping..." | |
fi | |
# Start the Deep Research frontend | |
echo "Starting Deep Research frontend..." | |
cd /app/deep-research | |
# Check if the .next directory exists, if not, build the app first | |
if [ ! -d ".next" ] || [ ! -f ".next/BUILD_ID" ] || [ ! -f ".next/routes-manifest.json" ] || [ ! -f ".next/server/pages-manifest.json" ] || [ ! -f ".next/server/middleware-manifest.json" ]; then | |
echo "Next.js build not found or incomplete, building the app first..." | |
# Disable React Compiler to avoid build errors | |
if [ -f "next.config.js" ]; then | |
sed -i 's/reactCompiler: true/reactCompiler: false/g' next.config.js | |
if ! grep -q "reactCompiler" next.config.js; then | |
sed -i 's/experimental: {/experimental: { reactCompiler: false,/g' next.config.js | |
if ! grep -q "experimental" next.config.js; then | |
sed -i 's/module.exports = {/module.exports = { experimental: { reactCompiler: false },/g' next.config.js | |
fi | |
fi | |
else | |
echo 'module.exports = { experimental: { reactCompiler: false } }' > next.config.js | |
fi | |
npm run build || { | |
echo "Next.js build failed, creating a minimal Next.js app..." | |
mkdir -p .next | |
mkdir -p public | |
mkdir -p app | |
# Create a minimal Next.js app | |
cat > app/page.js << EOL | |
export default function Page() { | |
return ( | |
<div className="p-8"> | |
<h1 className="text-2xl font-bold mb-4">Deep Research Agents</h1> | |
<p className="mb-4">Welcome to the Deep Research Agents platform.</p> | |
<p>This is a minimal fallback page. The full application could not be built.</p> | |
</div> | |
); | |
} | |
EOL | |
cat > app/layout.js << EOL | |
export default function RootLayout({ children }) { | |
return ( | |
<html lang="en"> | |
<body>{children}</body> | |
</html> | |
); | |
} | |
EOL | |
# Create next.config.js without experimental features if it doesn't exist | |
if [ ! -f "next.config.js" ]; then | |
echo 'module.exports = { output: "standalone", experimental: { reactCompiler: false } }' > next.config.js | |
fi | |
# Create necessary Next.js directories | |
mkdir -p .next/standalone | |
mkdir -p .next/static | |
mkdir -p .next/server | |
mkdir -p .next/cache | |
# Copy files to standalone directory | |
cp -r app .next/standalone/ 2>/dev/null || mkdir -p .next/standalone/app | |
cp -r public .next/standalone/ 2>/dev/null || mkdir -p .next/standalone/public | |
cp next.config.js .next/standalone/ | |
# Create necessary Next.js files | |
mkdir -p .next/server/pages | |
echo "$(date +%s)" > .next/BUILD_ID | |
echo '{"basePath":"","redirects":[],"headers":[],"rewrites":[],"staticRoutes":[],"dynamicRoutes":[],"dataRoutes":[],"i18n":null}' > .next/routes-manifest.json | |
echo '{"version":1,"pages":{"/_app":{"runtime":"nodejs","env":[],"files":[],"name":"_app","page":"/_app","matchers":[],"prefetchFiles":[],"dataRoutes":[]},"/":{"runtime":"nodejs","env":[],"files":[],"name":"index","page":"/","matchers":[],"prefetchFiles":[],"dataRoutes":[]}}}' > .next/build-manifest.json | |
echo '{"sortedPages":["/","/_app"]}' > .next/prerender-manifest.json | |
echo '{"/":"pages/index.js","/_app":"pages/_app.js","/_error":"pages/_error.js","/_document":"pages/_document.js"}' > .next/server/pages-manifest.json | |
# Create middleware files to avoid middleware errors | |
echo '{"middleware":{"files":[],"name":"middleware","page":"/_middleware","matchers":[],"wasm":[],"assets":[]}}' > .next/server/middleware-manifest.json | |
echo '{"sortedMiddleware":[]}' > .next/server/middleware-build-manifest.json | |
# Create minimal page files | |
cat > .next/server/pages/index.js << EOL | |
export default function Page() { | |
return ( | |
<div> | |
<h1>Deep Research Agents</h1> | |
<p>Welcome to the Deep Research Agents platform.</p> | |
</div> | |
); | |
} | |
EOL | |
cat > .next/server/pages/_app.js << EOL | |
export default function App({ Component, pageProps }) { | |
return <Component {...pageProps} />; | |
} | |
EOL | |
cat > .next/server/pages/_error.js << EOL | |
export default function Error({ statusCode }) { | |
return <p>{statusCode ? \`An error \${statusCode} occurred on server\` : "An error occurred on client"}</p>; | |
} | |
EOL | |
cat > .next/server/pages/_document.js << EOL | |
import { Html, Head, Main, NextScript } from "next/document"; | |
export default function Document() { | |
return <Html><Head /><body><Main /><NextScript /></body></Html>; | |
} | |
EOL | |
} | |
fi | |
# Check if next is installed globally, if not install it | |
if ! command -v next &> /dev/null; then | |
echo "Next.js CLI not found, installing it globally..." | |
npm install -g next | |
fi | |
# Set the port to 7860 for Hugging Face Spaces compatibility | |
PORT=7860 | |
# Check if the start script exists in package.json, otherwise use next start | |
if grep -q '"start"' package.json; then | |
# Check if the start script has a port parameter | |
if grep -q -- "-p" package.json; then | |
npm start || { | |
echo "Failed to start with npm start, falling back to next start..." | |
next start -p $PORT || { | |
echo "Failed to start with next start, falling back to node server.js..." | |
# Create a simple server.js if it doesn't exist | |
if [ ! -f "server.js" ]; then | |
cat > server.js << EOL | |
const http = require('http'); | |
const fs = require('fs'); | |
const path = require('path'); | |
const server = http.createServer((req, res) => { | |
res.writeHead(200, { 'Content-Type': 'text/html' }); | |
res.end('<html><body><h1>Deep Research Agents</h1><p>Welcome to the Deep Research Agents platform.</p></body></html>'); | |
}); | |
const PORT = process.env.PORT || 7860; | |
server.listen(PORT, () => { | |
console.log(\`Server running at http://localhost:\${PORT}/\`); | |
}); | |
EOL | |
fi | |
PORT=$PORT node server.js | |
} | |
} | |
else | |
# If no port parameter in start script, use PORT env variable | |
PORT=$PORT npm start || { | |
echo "Failed to start with npm start, falling back to next start..." | |
next start -p $PORT || { | |
echo "Failed to start with next start, falling back to node server.js..." | |
# Create a simple server.js if it doesn't exist | |
if [ ! -f "server.js" ]; then | |
cat > server.js << EOL | |
const http = require('http'); | |
const fs = require('fs'); | |
const path = require('path'); | |
const server = http.createServer((req, res) => { | |
res.writeHead(200, { 'Content-Type': 'text/html' }); | |
res.end('<html><body><h1>Deep Research Agents</h1><p>Welcome to the Deep Research Agents platform.</p></body></html>'); | |
}); | |
const PORT = process.env.PORT || 7860; | |
server.listen(PORT, () => { | |
console.log(\`Server running at http://localhost:\${PORT}/\`); | |
}); | |
EOL | |
fi | |
PORT=$PORT node server.js | |
} | |
} | |
fi | |
else | |
next start -p $PORT || { | |
echo "Failed to start with next start, falling back to node server.js..." | |
# Create a simple server.js if it doesn't exist | |
if [ ! -f "server.js" ]; then | |
cat > server.js << EOL | |
const http = require('http'); | |
const fs = require('fs'); | |
const path = require('path'); | |
const server = http.createServer((req, res) => { | |
res.writeHead(200, { 'Content-Type': 'text/html' }); | |
res.end('<html><body><h1>Deep Research Agents</h1><p>Welcome to the Deep Research Agents platform.</p></body></html>'); | |
}); | |
const PORT = process.env.PORT || 7860; | |
server.listen(PORT, () => { | |
console.log(\`Server running at http://localhost:\${PORT}/\`); | |
}); | |
EOL | |
fi | |
PORT=$PORT node server.js | |
} | |
fi | |
# Trap SIGTERM and SIGINT to gracefully shut down all processes | |
trap 'kill_processes' SIGTERM SIGINT | |
# Function to kill all running processes | |
kill_processes() { | |
echo "Shutting down all processes..." | |
[ -n "$SEQUENTIAL_THINKING_PID" ] && kill $SEQUENTIAL_THINKING_PID 2>/dev/null || true | |
[ -n "$COGNEE_PID" ] && kill $COGNEE_PID 2>/dev/null || true | |
[ -n "$MCP_AGENTS_PID" ] && kill $MCP_AGENTS_PID 2>/dev/null || true | |
[ -n "$AI_CO_SCIENTIST_PID" ] && kill $AI_CO_SCIENTIST_PID 2>/dev/null || true | |
exit | |
} | |
# Wait for all background processes to finish | |
wait |