#!/bin/bash # Note: no set -e — sync failures must not kill the container # Create agentmemory data dir mkdir -p /home/user/.agentmemory # ============================================================================= # Persistent storage via HF Dataset repo (free) # Secrets to set in HF Space settings: # HF_TOKEN — write access to the dataset repo # GEMINI_API_KEY — powers graph, embeddings, compression, crystals # AGENTMEMORY_DATASET_REPO — optional override (default: Yash030/agentmemory-data) # ============================================================================= export AGENTMEMORY_DATASET_REPO="${AGENTMEMORY_DATASET_REPO:-Yash030/agentmemory-data}" echo "[start] Restoring data from HF Dataset..." python3 /app/sync.py restore # Background sync loop — backs up every 5 minutes ( while true; do sleep 300 python3 /app/sync.py backup done ) & # Internal service URLs (daemon talks to itself on localhost) export AGENTMEMORY_URL=http://localhost:3111 export III_ENGINE_URL=ws://localhost:49134 # Build CORS allowed origins — always include HF Space public host CORS_ORIGINS="http://localhost:3111,http://localhost:3113,http://127.0.0.1:3111,http://127.0.0.1:3113" if [ -n "${SPACE_HOST}" ]; then CORS_ORIGINS="${CORS_ORIGINS},https://${SPACE_HOST},http://${SPACE_HOST}" export VIEWER_ALLOWED_HOSTS="${VIEWER_ALLOWED_HOSTS:-${SPACE_HOST},${SPACE_HOST}:443,${SPACE_HOST}:7860}" export VIEWER_ALLOWED_ORIGINS="${VIEWER_ALLOWED_ORIGINS:-https://${SPACE_HOST},http://${SPACE_HOST},http://localhost:3111,http://localhost:3113,http://127.0.0.1:3111,http://127.0.0.1:3113}" fi # Generate HMAC secret on first boot, persist it so it survives dataset restore HMAC_FILE="/home/user/.agentmemory/.hmac" if [ ! -s "$HMAC_FILE" ]; then SECRET="$(openssl rand -hex 32)" printf '%s\n' "$SECRET" > "$HMAC_FILE" chmod 600 "$HMAC_FILE" echo "================================================================" echo "agentmemory: generated HMAC secret on first boot" echo "AGENTMEMORY_SECRET=$SECRET" echo "Copy this to your Space secrets as AGENTMEMORY_SECRET." echo "It will not be printed again." echo "================================================================" fi export AGENTMEMORY_SECRET="${AGENTMEMORY_SECRET:-$(cat "$HMAC_FILE")}" # Write .env config for the daemon cat > /home/user/.agentmemory/.env < /opt/agentmemory/node_modules/@agentmemory/agentmemory/dist/iii-config.yaml <