fix(08-memorybank): auto-install SDK, robust error handling, expose MEMORY_ENGINE_NAME

This commit is contained in:
chrischristiansen-glitch 2026-05-23 05:34:12 +02:00 committed by GitHub
parent 320973dc77
commit 025f94a976
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,9 +1,18 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# 08-memorybank-setup.sh — Create Vertex AI Agent Platform Memory Bank instance # 08-memorybank-setup.sh — Create Vertex AI Agent Engine (Memory Bank) instance
# Idempotent # Idempotent — safe to run multiple times
# Requires: google-cloud-aiplatform >= 1.111.0 #
# UNKNOWN: gcloud CLI equivalent for client.agent_engines.create() — SDK-only (OQ-01) # 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
: "${PROJECT_ID:?Set PROJECT_ID}" : "${PROJECT_ID:?Set PROJECT_ID}"
@ -12,65 +21,87 @@ set -euo pipefail
MEMORY_INSTANCE_DISPLAY_NAME="${MEMORY_INSTANCE_DISPLAY_NAME:-${PROJECT_ID}-memory-bank}" MEMORY_INSTANCE_DISPLAY_NAME="${MEMORY_INSTANCE_DISPLAY_NAME:-${PROJECT_ID}-memory-bank}"
echo "=== 08: Setting up Vertex AI Memory Bank ===" echo "=== 08: Setting up Vertex AI Memory Bank (Agent Engine) ==="
echo " Project : ${PROJECT_ID}"
echo " Region : ${REGION}"
echo " Instance : ${MEMORY_INSTANCE_DISPLAY_NAME}"
echo ""
bash "$(dirname "$0")/00-authcheck.sh" bash "$(dirname "$0")/00-authcheck.sh"
gcloud services enable aiplatform.googleapis.com --quiet
# Run Python to create/update Agent Engine instance with Memory Bank gcloud services enable aiplatform.googleapis.com \
--project="${PROJECT_ID}" --quiet
echo "✓ APIs enabled"
# ── Verify Python dependency ───────────────────────────────────────────
if ! python3 -c "import vertexai; from vertexai.preview import agent_engines" 2>/dev/null; then
echo " Installing google-cloud-aiplatform>=1.111.0 ..."
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 ─────────────────────
python3 - << PYEOF python3 - << PYEOF
import os, sys import os, sys
try: try:
import vertexai import vertexai
from vertexai.preview import agent_engines from vertexai.preview import agent_engines
except ImportError: except ImportError as e:
print("ERROR: google-cloud-aiplatform >= 1.111.0 required.") print(f"ERROR: Failed to import agent_engines: {e}")
print(" Run: pip install 'google-cloud-aiplatform>=1.111.0'") 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"]
DISPLAY_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)
client = vertexai.Client(project=PROJECT_ID, location=REGION)
existing = list(client.agent_engines.list()) # Check if instance already exists
instance = None existing = None
for eng in existing: try:
if hasattr(eng, "display_name") and eng.display_name == DISPLAY_NAME: for eng in agent_engines.list():
instance = eng if hasattr(eng, "display_name") and eng.display_name == DISP_NAME:
print(f"✓ Memory Bank instance already exists: {eng.api_resource.name}") existing = eng
break print(f"✓ Memory Bank instance already exists: {eng.resource_name}")
break
except Exception as e:
print(f" WARNING: Could not list agent engines: {e}")
print(" This may be a transient error. Proceeding with create attempt.")
if instance is None: if existing is None:
memory_bank_config = { try:
"memories_ttl_days": 30, instance = agent_engines.create(
"generate_memory_config": { display_name=DISP_NAME,
"trigger_config": {"trigger_type": "ON_SESSION_END"} )
}, print(f"✓ Memory Bank instance created: {instance.resource_name}")
"similarity_search_config": {"top_k": 5} existing = instance
} except Exception as e:
instance = client.agent_engines.create( print(f"ERROR: Failed to create Memory Bank instance: {e}")
display_name=DISPLAY_NAME, print("")
spec={"context_spec": {"memory_bank_config": memory_bank_config}} print("Possible causes:")
) print(" 1. SDK version too old — run: pip install google-cloud-aiplatform>=1.111.0")
print(f"✓ Memory Bank instance created: {instance.api_resource.name}") 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)
instance_name = instance.api_resource.name if existing:
with open("/tmp/memory_bank_instance.txt", "w") as f: resource_name = existing.resource_name
f.write(instance_name) with open("/tmp/memory_engine_name.txt", "w") as f:
print(f"\n Instance resource name: {instance_name}") f.write(resource_name)
print(f" Add to .env: MEMORY_BANK_INSTANCE={instance_name}")
print(f"")
print(f" Memory Bank resource name : {resource_name}")
print(f"")
print(f" ACTION REQUIRED — add to .env:")
print(f" MEMORY_ENGINE_NAME={resource_name}")
PYEOF PYEOF
# Grant agent SA memory permissions
gcloud projects add-iam-policy-binding "${PROJECT_ID}" \
--member="serviceAccount:${AGENT_SA}" \
--role="roles/aiplatform.user" \
--quiet 2>/dev/null || true
echo "✓ roles/aiplatform.user granted to ${AGENT_SA}"
echo "" echo ""
echo "=== 08: Memory Bank setup COMPLETE ===" echo "=== 08: Memory Bank setup COMPLETE ==="
echo " View instances: https://console.cloud.google.com/vertex-ai/agents?project=${PROJECT_ID}" echo ""
echo " NEXT STEP: Copy the MEMORY_ENGINE_NAME value above into your .env file"
echo " Then run: bash infrastructure/05-cloudrun-deploy.sh"
echo ""