#!/usr/bin/env bash
#
# quickstart.sh -- the smallest no-SDK example: list the catalog, pick the
# first model, and run one prompt, all via plain curl. The SDK-less analogue
# of each language quickstart (examples/<lang>/quickstart).
#
# Prerequisites:
#   curl, jq
#   AGENTROUTER_API_KEY    a credential -- an issued key (looks like
#                          <keyid>.<secret>) or a session token. Carries its own
#                          customer + project, so there is nothing else to pass.
#   AGENTROUTER_BASE_URL   optional; defaults to https://api.tetrate.ai
#
# Run it:
#   export AGENTROUTER_API_KEY=<keyid>.<secret>   # from: tare api keys create
#   bash quickstart.sh
#
# A local dev stack uses a self-signed cert; set AGENTROUTER_CURL_INSECURE=1
# to add `curl -k` (or point CURL_CA_BUNDLE at the dev CA instead).
#
# By default this lists the catalog and uses the first available model. Set
# AGENTROUTER_QUICKSTART_MODEL to pin a specific one, e.g. claude-haiku-4-5.
set -euo pipefail

BASE_URL="${AGENTROUTER_BASE_URL:-https://api.tetrate.ai}"
API_KEY="${AGENTROUTER_API_KEY:?set AGENTROUTER_API_KEY (a key from 'tare api keys create', or a session token)}"
MODEL="${AGENTROUTER_QUICKSTART_MODEL:-}"

# Optional -k for the self-signed dev cert (off by default).
INSECURE=()
[[ "${AGENTROUTER_CURL_INSECURE:-0}" == "1" ]] && INSECURE=(-k)

# Resolve the model. The inference handler requires a non-empty model, so when
# AGENTROUTER_QUICKSTART_MODEL is unset we list the catalog and use the first
# model's bare name (e.g. claude-haiku-4-5), which resolves its provider
# server-side. The "${INSECURE[@]+...}" guard keeps an empty array safe under
# `set -u` on bash 3.2 (the default /bin/bash on macOS).
if [[ -z "$MODEL" ]]; then
  MODEL="$(curl ${INSECURE[@]+"${INSECURE[@]}"} -sS \
    -H "Authorization: Bearer ${API_KEY}" \
    "${BASE_URL}/v1/catalog/models" |
    jq -r '.models[0].name // empty')"
fi
if [[ -z "$MODEL" ]]; then
  echo "no models in catalog -- ask an admin to import a catalog bundle" >&2
  exit 1
fi

# Edit this prompt to ask your own question.
PROMPT="Happy happy joy joy"

# Build the JSON body with jq so the prompt/model are escaped safely. Customer +
# project are omitted so the server resolves the caller's default tenant from
# the credential's session.
BODY="$(jq -nc --arg model "$MODEL" --arg prompt "$PROMPT" '{model: $model, prompt: $prompt}')"

# The connect-style Prompt route accepts the empty customer/project default;
# the Connect-Protocol-Version header selects that procedure route. The
# "${INSECURE[@]+...}" guard keeps an empty array safe under `set -u` on bash 3.2
# (the default /bin/bash on macOS).
curl ${INSECURE[@]+"${INSECURE[@]}"} -sS \
  -X POST \
  -H "Authorization: Bearer ${API_KEY}" \
  -H "Connect-Protocol-Version: 1" \
  -H "Content-Type: application/json" \
  -v --data "$BODY" \
  "${BASE_URL}/tars.inference.v1.InferenceIngressService/Prompt" |
  jq -r '"model:      \(.model)\ncompletion: \(.completion)\ntokens:     \(.promptTokens // .prompt_tokens) prompt / \(.completionTokens // .completion_tokens) completion"'
