|
1 | 1 | #!/bin/bash |
2 | 2 | # QuickClaw Orchestrator Pulse - Triggers orchestrator check-in |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +# Require QUICKCLAW_HOME to be set |
| 6 | +if [ -z "${QUICKCLAW_HOME:-}" ]; then |
| 7 | + echo "ERROR: QUICKCLAW_HOME environment variable is not set" >&2 |
| 8 | + exit 1 |
| 9 | +fi |
| 10 | + |
| 11 | +# Require a message argument |
| 12 | +if [ -z "${1:-}" ]; then |
| 13 | + echo "ERROR: Message argument is required" >&2 |
| 14 | + echo "Usage: $0 <message>" >&2 |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +MESSAGE="$1" |
| 19 | +LOG_FILE="$QUICKCLAW_HOME/logs/orchestrator.log" |
| 20 | +OPENCLAW_PORT="${OPENCLAW_PORT:-18789}" |
| 21 | +OPENCLAW_CONFIG="${OPENCLAW_CONFIG:-$HOME/.openclaw/openclaw.json}" |
3 | 22 |
|
4 | | -LOG_FILE="/Users/clawd/quickclaw/logs/orchestrator.log" |
5 | 23 | mkdir -p "$(dirname "$LOG_FILE")" |
6 | 24 |
|
7 | | -echo "[$(date '+%Y-%m-%d %H:%M:%S')] Orchestrator pulse: $1" >> "$LOG_FILE" |
| 25 | +log() { |
| 26 | + echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE" |
| 27 | +} |
| 28 | + |
| 29 | +log "Orchestrator pulse: $MESSAGE" |
| 30 | + |
| 31 | +# Verify config file exists before attempting to read |
| 32 | +if [ ! -f "$OPENCLAW_CONFIG" ]; then |
| 33 | + log "ERROR: OpenClaw config not found at $OPENCLAW_CONFIG" |
| 34 | + exit 1 |
| 35 | +fi |
8 | 36 |
|
9 | 37 | # Get the auth token from openclaw.json |
10 | | -TOKEN=$(grep -o '"token": "[^"]*"' /Users/clawd/.openclaw/openclaw.json 2>/dev/null | head -1 | cut -d'"' -f4) |
| 38 | +TOKEN=$(grep -o '"token": "[^"]*"' "$OPENCLAW_CONFIG" 2>/dev/null | head -1 | cut -d'"' -f4) |
11 | 39 |
|
12 | 40 | if [ -z "$TOKEN" ]; then |
13 | | - echo "[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: Could not find auth token" >> "$LOG_FILE" |
14 | | - exit 1 |
| 41 | + log "ERROR: Could not find auth token in $OPENCLAW_CONFIG" |
| 42 | + exit 1 |
15 | 43 | fi |
16 | 44 |
|
| 45 | +# Build JSON payload safely using printf to avoid injection |
| 46 | +PAYLOAD=$(printf '{"agentId": "orchestrator", "message": "%s"}' "$MESSAGE") |
| 47 | + |
17 | 48 | # Send message to orchestrator |
18 | | -curl -s -X POST http://localhost:18789/api/v1/sessions \ |
| 49 | +HTTP_STATUS=$(curl -s -o /tmp/quickclaw_response.tmp -w "%{http_code}" \ |
| 50 | + -X POST "http://localhost:${OPENCLAW_PORT}/api/v1/sessions" \ |
19 | 51 | -H "Authorization: Bearer $TOKEN" \ |
20 | 52 | -H "Content-Type: application/json" \ |
21 | | - -d "{\"agentId\": \"orchestrator\", \"message\": \"$1\"}" >> "$LOG_FILE" 2>&1 |
| 53 | + -d "$PAYLOAD") |
| 54 | + |
| 55 | +if [ "$HTTP_STATUS" -ge 200 ] && [ "$HTTP_STATUS" -lt 300 ]; then |
| 56 | + log "Pulse delivered successfully (HTTP $HTTP_STATUS)" |
| 57 | +else |
| 58 | + log "ERROR: Pulse failed (HTTP $HTTP_STATUS): $(cat /tmp/quickclaw_response.tmp 2>/dev/null)" |
| 59 | + rm -f /tmp/quickclaw_response.tmp |
| 60 | + exit 1 |
| 61 | +fi |
22 | 62 |
|
23 | | -echo "" >> "$LOG_FILE" |
| 63 | +rm -f /tmp/quickclaw_response.tmp |
0 commit comments