#!/usr/bin/env bash
# Fronset cookbook 3 in curl: a batch of lines on a settled catalogue task.
# Needs curl and jq. Keys come from the environment and are never printed.
#
#   export FRONSET_INFERENCE_KEY=llmb_i_...
#   bash cb3_batch_settled_task.sh cb3_input.jsonl
#
# cb3_input.jsonl: one line per request, in template mode, with no "model"
# (it is hoisted on the create call). The Python version builds it from
# The Federalist Nos. 1-50; run it once with --out-dir to get the file.
set -euo pipefail
API="${FRONSET_API_BASE:-https://api.fronset.ai}"
AUTH="Authorization: Bearer ${FRONSET_INFERENCE_KEY}"
INPUT="${1:?usage: cb3_batch_settled_task.sh <input.jsonl>}"

# 1. Upload the file.
FILE_ID=$(curl -sS "$API/v1/files" -H "$AUTH" \
  -F purpose=batch -F "file=@${INPUT}" | jq -r .id)

# 2. Create the batch, with the task hoisted into the llmbench block.
BATCH_ID=$(curl -sS "$API/v1/batches" -H "$AUTH" -H "Content-Type: application/json" \
  -d "{\"input_file_id\": \"$FILE_ID\", \"endpoint\": \"/v1/chat/completions\",
       \"completion_window\": \"24h\", \"metadata\": {\"cookbook\": \"3\"},
       \"llmbench\": {\"model\": \"structured-content-summarization\"}}" | jq -r .id)

# 3. Poll until the batch is terminal. Polling is authoritative.
while :; do
  B=$(curl -sS "$API/v1/batches/$BATCH_ID" -H "$AUTH")
  STATUS=$(jq -r .status <<<"$B")
  echo "$(date -u +%H:%M:%S) $STATUS $(jq -c .request_counts <<<"$B")"
  case "$STATUS" in completed|failed|expired|cancelled) break ;; esac
  sleep 30
done
jq '.llmbench // {}' <<<"$B"

# 4. Download the output and error files, then read each line's mirror.
OUT=$(jq -r '.output_file_id // empty' <<<"$B")
ERR=$(jq -r '.error_file_id // empty' <<<"$B")
[ -n "$OUT" ] && curl -sS "$API/v1/files/$OUT/content" -H "$AUTH" -o cb3_output.jsonl
[ -n "$ERR" ] && curl -sS "$API/v1/files/$ERR/content" -H "$AUTH" -o cb3_error.jsonl

if [ -f cb3_output.jsonl ]; then
  echo "answered by:";  jq -r '.response.body.llmbench.model' cb3_output.jsonl | sort | uniq -c
  echo "service tier:"; jq -r '.response.body.llmbench["service-tier"]' cb3_output.jsonl | sort | uniq -c
  echo -n "sum of cost-usd: "
  jq -s 'map(.response.body.llmbench["cost-usd"] | tonumber) | add' cb3_output.jsonl
  echo -n "tokens in, out: "
  jq -s '[map(.response.body.usage.prompt_tokens) | add,
           map(.response.body.usage.completion_tokens) | add]' -c cb3_output.jsonl
fi
if [ -f cb3_error.jsonl ]; then
  echo "errors:"; jq -r '(.error.code // .response.body.error.code)' cb3_error.jsonl | sort | uniq -c
fi
