fix: 08-memorybank-setup.sh — REST API agent engine creation, no SDK dependency

This commit is contained in:
chrischristiansen-glitch 2026-05-24 14:07:54 +02:00
parent 9928c595b8
commit c7c12194f5

View File

@ -1,5 +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
# Uses REST API directly — no SDK version dependency
# Idempotent — safe to run multiple times # Idempotent — safe to run multiple times
# Source .env before running: source .env # Source .env before running: source .env
@ -10,10 +11,12 @@ set -euo pipefail
: "${AGENT_SA:?Set AGENT_SA}" : "${AGENT_SA:?Set AGENT_SA}"
MEMORY_INSTANCE_DISPLAY_NAME="${MEMORY_INSTANCE_DISPLAY_NAME:-${PROJECT_ID}-memory-bank}" MEMORY_INSTANCE_DISPLAY_NAME="${MEMORY_INSTANCE_DISPLAY_NAME:-${PROJECT_ID}-memory-bank}"
# Agent Engine lives in the same region as REGION (us-central1 is fine, eu also works)
MEMORY_REGION="${MEMORY_REGION:-${REGION}}"
echo "=== 08: Setting up Vertex AI Memory Bank (Agent Engine) ===" echo "=== 08: Setting up Vertex AI Memory Bank (Agent Engine) ==="
echo " Project : ${PROJECT_ID}" echo " Project : ${PROJECT_ID}"
echo " Region : ${REGION}" echo " Region : ${MEMORY_REGION}"
echo " Instance : ${MEMORY_INSTANCE_DISPLAY_NAME}" echo " Instance : ${MEMORY_INSTANCE_DISPLAY_NAME}"
echo "" echo ""
@ -23,77 +26,96 @@ gcloud services enable aiplatform.googleapis.com \
--project="${PROJECT_ID}" --quiet --project="${PROJECT_ID}" --quiet
echo "✓ APIs enabled" echo "✓ APIs enabled"
# ── Ensure correct SDK version is installed ──────────────────────────────── # ── REST: token + endpoint ────────────────────────────────────────────────────
echo " Ensuring google-cloud-aiplatform>=1.111.0 ..." TOKEN=$(gcloud auth print-access-token)
pip install --quiet --user "google-cloud-aiplatform>=1.111.0" 2>&1 | tail -2 API="https://${MEMORY_REGION}-aiplatform.googleapis.com/v1beta1"
PARENT="projects/${PROJECT_ID}/locations/${MEMORY_REGION}"
# Use the user-local python so .local/lib packages are picked up # ── Check if instance exists ───────────────────────────────────────────────────
PYTHON="$(which python3)" echo " Checking for existing Agent Engine instance..."
if python3 -c "import sys; sys.exit(0 if any('.local' in p for p in sys.path) else 1)" 2>/dev/null; then EXISTING_LIST=$(curl -sf \
PYTHON="python3" -H "Authorization: Bearer ${TOKEN}" \
"${API}/${PARENT}/reasoningEngines" 2>/dev/null || echo '{}')
ENGINE_NAME=$(echo "${EXISTING_LIST}" | python3 -c "
import sys, json
data = json.load(sys.stdin)
for e in data.get('reasoningEngines', []):
if e.get('displayName') == '${MEMORY_INSTANCE_DISPLAY_NAME}':
print(e['name'])
break
" 2>/dev/null || true)
if [[ -n "${ENGINE_NAME}" ]]; then
echo "✓ Memory Bank already exists: ${ENGINE_NAME}"
else else
# Force user site-packages echo " Creating Agent Engine '${MEMORY_INSTANCE_DISPLAY_NAME}' in ${MEMORY_REGION}..."
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? RESPONSE=$(curl -sf -X POST \
if ! python3 -c "import sys, site; sys.path.insert(0, site.getusersitepackages()); from vertexai.preview import agent_engines" 2>/dev/null; then -H "Authorization: Bearer ${TOKEN}" \
echo "ERROR: agent_engines still not importable after install. Try:" -H "Content-Type: application/json" \
echo " pip install --upgrade google-cloud-aiplatform" "${API}/${PARENT}/reasoningEngines" \
-d '{
"displayName": "'"${MEMORY_INSTANCE_DISPLAY_NAME}"'",
"spec": {}
}' 2>/dev/null || echo '{}')
OPERATION=$(echo "${RESPONSE}" | python3 -c "
import sys, json
d = json.load(sys.stdin)
print(d.get('name', ''))
" 2>/dev/null || true)
if [[ -z "${OPERATION}" ]]; then
echo "ERROR: No operation returned. Response:"
echo "${RESPONSE}"
exit 1 exit 1
fi fi
echo "✓ Python SDK ready (agent_engines importable)"
# ── Create or find Memory Bank instance ─────────────────────────────────── echo " Waiting for LRO: ${OPERATION}"
python3 - << 'PYEOF' for i in $(seq 1 30); do
import sys, site sleep 10
sys.path.insert(0, site.getusersitepackages()) LRO=$(curl -sf \
-H "Authorization: Bearer ${TOKEN}" \
import os "https://${MEMORY_REGION}-aiplatform.googleapis.com/v1beta1/${OPERATION}" 2>/dev/null || echo '{}')
DONE=$(echo "${LRO}" | python3 -c "
try: import sys, json
import vertexai print(json.load(sys.stdin).get('done', False))
from vertexai.preview import agent_engines " 2>/dev/null || echo 'False')
except ImportError as e: if [[ "${DONE}" == "True" ]]; then
print(f"ERROR: Failed to import agent_engines: {e}") # Re-list to get name reliably
sys.exit(1) ENGINE_NAME=$(curl -sf \
-H "Authorization: Bearer ${TOKEN}" \
PROJECT_ID = os.environ["PROJECT_ID"] "${API}/${PARENT}/reasoningEngines" 2>/dev/null \
REGION = os.environ["REGION"] | python3 -c "
DISP_NAME = os.environ.get("MEMORY_INSTANCE_DISPLAY_NAME", f"{PROJECT_ID}-memory-bank") import sys, json
data = json.load(sys.stdin)
vertexai.init(project=PROJECT_ID, location=REGION) for e in data.get('reasoningEngines', []):
if e.get('displayName') == '${MEMORY_INSTANCE_DISPLAY_NAME}':
existing = None print(e['name'])
try:
for eng in agent_engines.list():
if hasattr(eng, "display_name") and eng.display_name == DISP_NAME:
existing = eng
print(f"✓ Memory Bank already exists: {eng.resource_name}")
break break
except Exception as e: " 2>/dev/null || true)
print(f" WARNING: Could not list agent engines: {e}") echo "✓ Agent Engine created: ${ENGINE_NAME}"
break
fi
echo " ... still waiting (${i}/30)"
done
if existing is None: if [[ -z "${ENGINE_NAME}" ]]; then
try: echo "ERROR: Agent Engine creation timed out or failed."
instance = agent_engines.create(display_name=DISP_NAME) exit 1
print(f"✓ Memory Bank created: {instance.resource_name}") fi
existing = instance fi
except Exception as e:
print(f"ERROR: Failed to create Memory Bank: {e}")
sys.exit(1)
if existing: # ── Write output ──────────────────────────────────────────────────────────────────
resource_name = existing.resource_name echo "${ENGINE_NAME}" > /tmp/memory_engine_name.txt
with open("/tmp/memory_engine_name.txt", "w") as f:
f.write(resource_name)
print(f"")
print(f" Resource name : {resource_name}")
print(f"")
print(f" ACTION REQUIRED — add to .env:")
print(f' export MEMORY_ENGINE_NAME="{resource_name}"')
PYEOF
echo ""
echo " Resource name : ${ENGINE_NAME}"
echo " Region : ${MEMORY_REGION}"
echo ""
echo " ACTION REQUIRED — add to .env:"
echo " export MEMORY_ENGINE_NAME=\"${ENGINE_NAME}\""
echo "" echo ""
echo "=== 08: Memory Bank setup COMPLETE ===" echo "=== 08: Memory Bank setup COMPLETE ==="
echo " NEXT: copy MEMORY_ENGINE_NAME into .env, then run 05-cloudrun-deploy.sh" echo " NEXT: copy MEMORY_ENGINE_NAME into .env, then run 05-cloudrun-deploy.sh"