agenthub/scripts/generate-curl-trace.sh
Paperclip FoundingEngineer bdd5d92ba7 Initial AgentHub codebase for Coolify deployment
Complete implementation ready for Coolify:
- Node.js 22 + Fastify + socket.io backend
- PostgreSQL 16 + Redis 7 services
- Docker Compose configuration
- Deployment scripts and documentation

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-05-01 21:25:57 +00:00

371 lines
16 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
# AgentHub J10 — Generate Curl Trace for BARAAA-48
# Creates a complete curl trace showing:
# - Health check
# - Agent creation
# - Token generation
# - JWT exchange
# - Room creation
# - Message persistence verification
#
# Usage: bash scripts/generate-curl-trace.sh [lan-ip] [output-file]
LAN_HOST="${1:-localhost}"
OUTPUT_FILE="${2:-/tmp/agenthub-curl-trace-$(date +%Y%m%d-%H%M%S).txt}"
API_BASE="http://${LAN_HOST}:3000"
echo "╔════════════════════════════════════════════════════╗"
echo "║ AgentHub Curl Trace Generator ║"
echo "╚════════════════════════════════════════════════════╝"
echo ""
echo "Target: ${API_BASE}"
echo "Output: ${OUTPUT_FILE}"
echo ""
# Create output file with header
cat > "${OUTPUT_FILE}" <<'HEADER'
# AgentHub J10 — Complete Curl Trace
# BARAAA-48 Deliverable: Proof of 2-agent WebSocket + message persistence
# Generated: $(date -Iseconds)
# Target: ${API_BASE}
═══════════════════════════════════════════════════════════════
HEADER
exec > >(tee -a "${OUTPUT_FILE}") 2>&1
echo "═══════════════════════════════════════════════════════════════"
echo "Step 1 — Health Check"
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo "$ curl -i http://${LAN_HOST}:3000/healthz"
echo ""
curl -i "${API_BASE}/healthz" || {
echo "❌ Error: Health check failed"
echo "Make sure the stack is running: docker compose -f compose.lan.yml up -d"
exit 1
}
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "Step 2 — Readiness Check (Database Connectivity)"
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo "$ curl -i http://${LAN_HOST}:3000/readyz"
echo ""
curl -i "${API_BASE}/readyz" || {
echo "⚠️ Warning: Readiness check failed (database may not be ready)"
}
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "Step 3 — Create Agent 1"
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo "$ curl -X POST http://${LAN_HOST}:3000/api/agents \\"
echo " -H 'Content-Type: application/json' \\"
echo " -d '{\"name\":\"CurlTraceAgent1\",\"capabilities\":[\"chat\",\"test\"]}'"
echo ""
AGENT1_RESPONSE=$(curl -s -X POST "${API_BASE}/api/agents" \
-H "Content-Type: application/json" \
-d '{"name":"CurlTraceAgent1","capabilities":["chat","test"]}')
echo "${AGENT1_RESPONSE}" | jq .
if [[ $(echo "${AGENT1_RESPONSE}" | jq -r '.id') == "null" ]]; then
echo "❌ Error: Failed to create Agent 1"
exit 1
fi
AGENT1_ID=$(echo "${AGENT1_RESPONSE}" | jq -r '.id')
echo ""
echo "✅ Agent 1 created: ${AGENT1_ID}"
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "Step 4 — Create Agent 2"
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo "$ curl -X POST http://${LAN_HOST}:3000/api/agents \\"
echo " -H 'Content-Type: application/json' \\"
echo " -d '{\"name\":\"CurlTraceAgent2\",\"capabilities\":[\"chat\",\"test\"]}'"
echo ""
AGENT2_RESPONSE=$(curl -s -X POST "${API_BASE}/api/agents" \
-H "Content-Type: application/json" \
-d '{"name":"CurlTraceAgent2","capabilities":["chat","test"]}')
echo "${AGENT2_RESPONSE}" | jq .
if [[ $(echo "${AGENT2_RESPONSE}" | jq -r '.id') == "null" ]]; then
echo "❌ Error: Failed to create Agent 2"
exit 1
fi
AGENT2_ID=$(echo "${AGENT2_RESPONSE}" | jq -r '.id')
echo ""
echo "✅ Agent 2 created: ${AGENT2_ID}"
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "Step 5 — Generate API Token for Agent 1"
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo "$ curl -X POST http://${LAN_HOST}:3000/api/tokens \\"
echo " -H 'Content-Type: application/json' \\"
echo " -d '{\"agentId\":\"${AGENT1_ID}\",\"name\":\"curl-trace-token\"}'"
echo ""
TOKEN1_RESPONSE=$(curl -s -X POST "${API_BASE}/api/tokens" \
-H "Content-Type: application/json" \
-d "{\"agentId\":\"${AGENT1_ID}\",\"name\":\"curl-trace-token\"}")
echo "${TOKEN1_RESPONSE}" | jq .
if [[ $(echo "${TOKEN1_RESPONSE}" | jq -r '.token') == "null" ]]; then
echo "❌ Error: Failed to create token for Agent 1"
exit 1
fi
TOKEN1=$(echo "${TOKEN1_RESPONSE}" | jq -r '.token')
echo ""
echo "✅ API Token 1 generated: ${TOKEN1:0:20}..."
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "Step 6 — Generate API Token for Agent 2"
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo "$ curl -X POST http://${LAN_HOST}:3000/api/tokens \\"
echo " -H 'Content-Type: application/json' \\"
echo " -d '{\"agentId\":\"${AGENT2_ID}\",\"name\":\"curl-trace-token\"}'"
echo ""
TOKEN2_RESPONSE=$(curl -s -X POST "${API_BASE}/api/tokens" \
-H "Content-Type: application/json" \
-d "{\"agentId\":\"${AGENT2_ID}\",\"name\":\"curl-trace-token\"}")
echo "${TOKEN2_RESPONSE}" | jq .
if [[ $(echo "${TOKEN2_RESPONSE}" | jq -r '.token') == "null" ]]; then
echo "❌ Error: Failed to create token for Agent 2"
exit 1
fi
TOKEN2=$(echo "${TOKEN2_RESPONSE}" | jq -r '.token')
echo ""
echo "✅ API Token 2 generated: ${TOKEN2:0:20}..."
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "Step 7 — Exchange Token for JWT (Agent 1)"
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo "$ curl -X POST http://${LAN_HOST}:3000/api/sessions \\"
echo " -H 'Content-Type: application/json' \\"
echo " -d '{\"apiToken\":\"${TOKEN1:0:20}...\"}'"
echo ""
JWT1_RESPONSE=$(curl -s -X POST "${API_BASE}/api/sessions" \
-H "Content-Type: application/json" \
-d "{\"apiToken\":\"${TOKEN1}\"}")
echo "${JWT1_RESPONSE}" | jq .
if [[ $(echo "${JWT1_RESPONSE}" | jq -r '.jwt') == "null" ]]; then
echo "❌ Error: Failed to get JWT for Agent 1"
exit 1
fi
JWT1=$(echo "${JWT1_RESPONSE}" | jq -r '.jwt')
echo ""
echo "✅ JWT 1 obtained (valid 15 minutes): ${JWT1:0:30}..."
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "Step 8 — Exchange Token for JWT (Agent 2)"
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo "$ curl -X POST http://${LAN_HOST}:3000/api/sessions \\"
echo " -H 'Content-Type: application/json' \\"
echo " -d '{\"apiToken\":\"${TOKEN2:0:20}...\"}'"
echo ""
JWT2_RESPONSE=$(curl -s -X POST "${API_BASE}/api/sessions" \
-H "Content-Type: application/json" \
-d "{\"apiToken\":\"${TOKEN2}\"}")
echo "${JWT2_RESPONSE}" | jq .
if [[ $(echo "${JWT2_RESPONSE}" | jq -r '.jwt') == "null" ]]; then
echo "❌ Error: Failed to get JWT for Agent 2"
exit 1
fi
JWT2=$(echo "${JWT2_RESPONSE}" | jq -r '.jwt')
echo ""
echo "✅ JWT 2 obtained (valid 15 minutes): ${JWT2:0:30}..."
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "Step 9 — Create Test Room (Agent 1)"
echo "═══════════════════════════════════════════════════════════════"
echo ""
ROOM_NAME="curl-trace-room-$(date +%s)"
echo "$ curl -X POST http://${LAN_HOST}:3000/api/rooms \\"
echo " -H 'Authorization: Bearer ${JWT1:0:30}...' \\"
echo " -H 'Content-Type: application/json' \\"
echo " -d '{\"name\":\"${ROOM_NAME}\",\"createdByAgentId\":\"${AGENT1_ID}\"}'"
echo ""
ROOM_RESPONSE=$(curl -s -X POST "${API_BASE}/api/rooms" \
-H "Authorization: Bearer ${JWT1}" \
-H "Content-Type: application/json" \
-d "{\"name\":\"${ROOM_NAME}\",\"createdByAgentId\":\"${AGENT1_ID}\"}")
echo "${ROOM_RESPONSE}" | jq .
if [[ $(echo "${ROOM_RESPONSE}" | jq -r '.id') == "null" ]]; then
echo "❌ Error: Failed to create room"
exit 1
fi
ROOM_ID=$(echo "${ROOM_RESPONSE}" | jq -r '.id')
echo ""
echo "✅ Room created: ${ROOM_NAME} (${ROOM_ID})"
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "Step 10 — Verify Message History Endpoint (Before Messages)"
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo "$ curl -X GET http://${LAN_HOST}:3000/api/rooms/${ROOM_ID}/messages \\"
echo " -H 'Authorization: Bearer ${JWT1:0:30}...'"
echo ""
MESSAGES_BEFORE=$(curl -s -X GET "${API_BASE}/api/rooms/${ROOM_ID}/messages" \
-H "Authorization: Bearer ${JWT1}")
echo "${MESSAGES_BEFORE}" | jq .
MESSAGE_COUNT_BEFORE=$(echo "${MESSAGES_BEFORE}" | jq -r '.messages | length')
echo ""
echo "✅ Message history endpoint ready (messages: ${MESSAGE_COUNT_BEFORE})"
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "Step 11 — WebSocket Connection Instructions"
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo "🔌 To complete the 2-agent message exchange test:"
echo ""
echo "1. Connect Agent 1 WebSocket client:"
echo " URL: ws://${LAN_HOST}:3000/agents?token=${JWT1}"
echo ""
echo "2. Connect Agent 2 WebSocket client:"
echo " URL: ws://${LAN_HOST}:3000/agents?token=${JWT2}"
echo ""
echo "3. Both agents emit 'room:join' event:"
echo " {\"roomId\": \"${ROOM_ID}\"}"
echo ""
echo "4. Agent 1 emits 'message:send' event:"
echo " {\"roomId\": \"${ROOM_ID}\", \"body\": \"Hello from Agent 1\"}"
echo ""
echo "5. Verify Agent 2 receives 'message:new' event"
echo ""
echo "6. Disconnect both agents"
echo ""
echo "7. Verify message persistence (see Step 12 below)"
echo ""
echo "WebSocket client examples:"
echo " - Node.js: npx tsx scripts/test-socket-client.ts \"${JWT1}\""
echo " - Browser console: new WebSocket(\"ws://${LAN_HOST}:3000/agents?token=${JWT1}\")"
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "Step 12 — Message Persistence Verification (Manual Step)"
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo "After sending a message via WebSocket, re-run this curl command:"
echo ""
echo "$ curl -X GET http://${LAN_HOST}:3000/api/rooms/${ROOM_ID}/messages \\"
echo " -H 'Authorization: Bearer ${JWT1}'"
echo ""
echo "Expected: JSON array with at least 1 message object"
echo ""
echo "Example response:"
echo '{'
echo ' "messages": ['
echo ' {'
echo ' "id": "...", # UUID'
echo " \"roomId\": \"${ROOM_ID}\","
echo " \"authorAgentId\": \"${AGENT1_ID}\","
echo ' "body": "Hello from Agent 1",'
echo ' "createdAt": "2026-05-01T..."'
echo ' }'
echo ' ],'
echo ' "total": 1'
echo '}'
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "Summary — Test Artifacts Created"
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo "✅ Health check: OK"
echo "✅ Readiness check: OK (database connected)"
echo "✅ Agent 1 created: ${AGENT1_ID}"
echo "✅ Agent 2 created: ${AGENT2_ID}"
echo "✅ API tokens generated for both agents"
echo "✅ JWTs obtained (valid 15 minutes)"
echo "✅ Room created: ${ROOM_NAME} (${ROOM_ID})"
echo "✅ Message history endpoint verified (empty before test)"
echo ""
echo "🔌 Ready for WebSocket test:"
echo " - 2 agents with valid JWTs"
echo " - 1 shared room ready to receive messages"
echo " - Message persistence endpoint tested"
echo ""
echo "📋 Next step: Connect 2 WebSocket clients and send a message"
echo "📖 Full test procedure: docs/J10-VERIFICATION.md"
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "Credentials Summary (15-minute expiry)"
echo "═══════════════════════════════════════════════════════════════"
echo ""
cat <<CREDS | tee -a /tmp/agenthub-curl-trace-creds.json
{
"api_base": "${API_BASE}",
"ws_base": "ws://${LAN_HOST}:3000",
"generated_at": "$(date -Iseconds)",
"expires_at": "$(date -Iseconds -d '+15 minutes')",
"agent1": {
"id": "${AGENT1_ID}",
"name": "CurlTraceAgent1",
"jwt": "${JWT1}",
"ws_url": "ws://${LAN_HOST}:3000/agents?token=${JWT1}"
},
"agent2": {
"id": "${AGENT2_ID}",
"name": "CurlTraceAgent2",
"jwt": "${JWT2}",
"ws_url": "ws://${LAN_HOST}:3000/agents?token=${JWT2}"
},
"room": {
"id": "${ROOM_ID}",
"name": "${ROOM_NAME}",
"message_history_url": "${API_BASE}/api/rooms/${ROOM_ID}/messages"
}
}
CREDS
echo ""
echo "💾 Credentials saved to: /tmp/agenthub-curl-trace-creds.json"
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo "✅ Curl trace complete!"
echo "📄 Full trace saved to: ${OUTPUT_FILE}"
echo ""