fix: 08-memorybank-setup.sh — use user-site python path for agent_engines import

This commit is contained in:
chrischristiansen-glitch 2026-05-23 07:07:30 +02:00
parent 2f5bff562d
commit 7a6a75983b

View File

@ -1,16 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# 08-memorybank-setup.sh — Create Vertex AI Agent Engine (Memory Bank) instance # 08-memorybank-setup.sh — Create Vertex AI Agent Engine (Memory Bank) instance
# Idempotent — safe to run multiple times # Idempotent — safe to run multiple times
#
# KNOWN LIMITATION (OQ-01):
# Memory Bank is Python SDK only — no gcloud CLI equivalent.
# Requires: google-cloud-aiplatform >= 1.111.0
# Install: pip install google-cloud-aiplatform>=1.111.0
#
# KNOWN LIMITATION (OQ-06):
# Memory Bank does NOT support auto-discovery via gcloud.
# The engine resource name must be manually set in .env as MEMORY_ENGINE_NAME.
#
# Source .env before running: source .env # Source .env before running: source .env
set -euo pipefail set -euo pipefail
@ -31,77 +21,80 @@ bash "$(dirname "$0")/00-authcheck.sh"
gcloud services enable aiplatform.googleapis.com \ gcloud services enable aiplatform.googleapis.com \
--project="${PROJECT_ID}" --quiet --project="${PROJECT_ID}" --quiet
echo "✓ APIs enabled" echo "✓ APIs enabled"
# ── Verify Python dependency ─────────────────────────────────────────── # ── Ensure correct SDK version is installed ────────────────────────────────
if ! python3 -c "import vertexai; from vertexai.preview import agent_engines" 2>/dev/null; then echo " Ensuring google-cloud-aiplatform>=1.111.0 ..."
echo " Installing google-cloud-aiplatform>=1.111.0 ..." pip install --quiet --user "google-cloud-aiplatform>=1.111.0" 2>&1 | tail -2
pip install --quiet "google-cloud-aiplatform>=1.111.0" 2>&1 | tail -3
fi
echo "✓ Python SDK available"
# ── Create or find Memory Bank instance via Python SDK ───────────────────── # Use the user-local python so .local/lib packages are picked up
python3 - << PYEOF PYTHON="$(which python3)"
import os, sys if python3 -c "import sys; sys.exit(0 if any('.local' in p for p in sys.path) else 1)" 2>/dev/null; then
PYTHON="python3"
else
# Force user site-packages
PYTHON="python3 -c 'import sys, site; sys.path.insert(0, site.getusersitepackages()); exec(open("/dev/stdin").read())'"
fi
# Simple check: can we import agent_engines?
if ! python3 -c "import sys, site; sys.path.insert(0, site.getusersitepackages()); from vertexai.preview import agent_engines" 2>/dev/null; then
echo "ERROR: agent_engines still not importable after install. Try:"
echo " pip install --upgrade google-cloud-aiplatform"
exit 1
fi
echo "✓ Python SDK ready (agent_engines importable)"
# ── Create or find Memory Bank instance ───────────────────────────────────
python3 - << 'PYEOF'
import sys, site
sys.path.insert(0, site.getusersitepackages())
import os
try: try:
import vertexai import vertexai
from vertexai.preview import agent_engines from vertexai.preview import agent_engines
except ImportError as e: except ImportError as e:
print(f"ERROR: Failed to import agent_engines: {e}") print(f"ERROR: Failed to import agent_engines: {e}")
print("Run: pip install google-cloud-aiplatform>=1.111.0")
sys.exit(1) sys.exit(1)
PROJECT_ID = os.environ["PROJECT_ID"] PROJECT_ID = os.environ["PROJECT_ID"]
REGION = os.environ["REGION"] REGION = os.environ["REGION"]
DISP_NAME = os.environ.get("MEMORY_INSTANCE_DISPLAY_NAME", f"{PROJECT_ID}-memory-bank") DISP_NAME = os.environ.get("MEMORY_INSTANCE_DISPLAY_NAME", f"{PROJECT_ID}-memory-bank")
vertexai.init(project=PROJECT_ID, location=REGION) vertexai.init(project=PROJECT_ID, location=REGION)
# Check if instance already exists
existing = None existing = None
try: try:
for eng in agent_engines.list(): for eng in agent_engines.list():
if hasattr(eng, "display_name") and eng.display_name == DISP_NAME: if hasattr(eng, "display_name") and eng.display_name == DISP_NAME:
existing = eng existing = eng
print(f"✓ Memory Bank instance already exists: {eng.resource_name}") print(f"✓ Memory Bank already exists: {eng.resource_name}")
break break
except Exception as e: except Exception as e:
print(f" WARNING: Could not list agent engines: {e}") print(f" WARNING: Could not list agent engines: {e}")
print(" This may be a transient error. Proceeding with create attempt.")
if existing is None: if existing is None:
try: try:
instance = agent_engines.create( instance = agent_engines.create(display_name=DISP_NAME)
display_name=DISP_NAME, print(f"✓ Memory Bank created: {instance.resource_name}")
)
print(f"✓ Memory Bank instance created: {instance.resource_name}")
existing = instance existing = instance
except Exception as e: except Exception as e:
print(f"ERROR: Failed to create Memory Bank instance: {e}") print(f"ERROR: Failed to create Memory Bank: {e}")
print("")
print("Possible causes:")
print(" 1. SDK version too old — run: pip install google-cloud-aiplatform>=1.111.0")
print(" 2. REGION not supported — try REGION=us-central1")
print(" 3. Quota not enabled — check: https://console.cloud.google.com/iam-admin/quotas")
sys.exit(1) sys.exit(1)
if existing: if existing:
resource_name = existing.resource_name resource_name = existing.resource_name
with open("/tmp/memory_engine_name.txt", "w") as f: with open("/tmp/memory_engine_name.txt", "w") as f:
f.write(resource_name) f.write(resource_name)
print(f"") print(f"")
print(f" Memory Bank resource name : {resource_name}") print(f" Resource name : {resource_name}")
print(f"") print(f"")
print(f" ACTION REQUIRED — add to .env:") print(f" ACTION REQUIRED — add to .env:")
print(f" MEMORY_ENGINE_NAME={resource_name}") print(f' export MEMORY_ENGINE_NAME="{resource_name}"')
PYEOF PYEOF
echo "" echo ""
echo "=== 08: Memory Bank setup COMPLETE ===" echo "=== 08: Memory Bank setup COMPLETE ==="
echo "" echo " NEXT: copy MEMORY_ENGINE_NAME into .env, then run 05-cloudrun-deploy.sh"
echo " NEXT STEP: Copy the MEMORY_ENGINE_NAME value above into your .env file"
echo " Then run: bash infrastructure/05-cloudrun-deploy.sh"
echo "" echo ""