#!/usr/bin/env bash
# Save as quickstart-request.sh; run: bash quickstart-request.sh
set -euo pipefail
mkdir -p jev-first-decision
cd jev-first-decision
if [[ -z "${TYPESAFE_API_KEY:-}" ]]; then
  read -r -s -p "TypeSafe API key: " TYPESAFE_API_KEY
  printf '\n'
  export TYPESAFE_API_KEY
fi
: "${TYPESAFE_API_KEY:?API key is required}"
cat > request.json <<'JSON'
{
  "model": "jev-latest",
  "state": "I was charged twice. Please fix this today.",
  "questions": {
    "urgency": {
      "type": "noul",
      "instructions": "Does this message express urgency?"
    }
  }
}
JSON
rm -f response.json response.status
if ! status=$(curl --silent --show-error --max-time 30 \
  https://api.typesafe.ai/v1/systemone \
  -H "Authorization: Bearer $TYPESAFE_API_KEY" \
  -H "Content-Type: application/json" \
  --data-binary @request.json --output response.json --write-out '%{http_code}'); then
  printf 'Unfinished: network error or timeout. Do not use a partial response.\n' >&2
  exit 1
fi
printf '%s\n' "$status" > response.status
case "$status" in
  2??) printf 'HTTP %s. Now check jev-first-decision/response.json.\n' "$status" ;;
  *) printf 'Unfinished: HTTP %s. Inspect response.json before retrying.\n' "$status" >&2; exit 1 ;;
esac
