#!/usr/bin/env bash
# Purpose: start llama-server with the Anthropic-compatible Messages endpoint and tool
#          calling enabled, so an agent that speaks the Anthropic wire format can be
#          pointed at it, and print the two environment variables that agent needs.
# Platform: all (spark, strix, mac, nvidia); the binary must be a build for your backend
# Minimum memory: 16 GB for a 30B-class mixture-of-experts coder at a long context
# Assumes: llama-server on PATH or at $LLAMA_BIN, a GGUF model at $MODEL, and that you
#          have read the lesson: the README makes no strong compatibility claim for this
#          endpoint, and tool use through it requires --jinja.

set -euo pipefail

MODEL="${MODEL:-$HOME/models/unsloth/Qwen3-Coder-30B-A3B-Instruct-GGUF/Qwen3-Coder-30B-A3B-Instruct-Q4_K_M.gguf}"
LLAMA_BIN="${LLAMA_BIN:-llama-server}"
HOST="${HOST:-127.0.0.1}"
PORT="${PORT:-8080}"
CTX="${CTX:-65536}"
ALIAS="${ALIAS:-local-agent-model}"
NGL="${NGL:-999}"

if ! command -v "$LLAMA_BIN" >/dev/null 2>&1 && [ ! -x "$LLAMA_BIN" ]; then
    echo "llama-server not found. Set LLAMA_BIN to the binary from your Part 6 build." >&2
    exit 1
fi

if [ ! -f "$MODEL" ]; then
    echo "Model file not found: $MODEL" >&2
    echo "Set MODEL to a GGUF from your Part 4 model library." >&2
    exit 1
fi

# --jinja is not optional here. The server README states that tool use through the
# Anthropic endpoint requires it, because the tool-call format comes from the model's own
# chat template. Without it the endpoint answers and the agent never sees a tool call.
#
# --cache-reuse is what stops every agent turn re-reading the whole conversation. It is
# the single largest speed setting for agent work; see this part's second lesson.
echo "Starting llama-server on http://${HOST}:${PORT}"
echo "  model:   ${MODEL}"
echo "  context: ${CTX}"
echo
echo "Point an Anthropic-shaped agent at it with:"
echo "  export ANTHROPIC_BASE_URL=http://${HOST}:${PORT}"
echo "  export ANTHROPIC_MODEL=${ALIAS}"
echo

exec "$LLAMA_BIN" \
    --model "$MODEL" \
    --alias "$ALIAS" \
    --ctx-size "$CTX" \
    --n-gpu-layers "$NGL" \
    --jinja \
    --cache-reuse 256 \
    --parallel 2 \
    --host "$HOST" \
    --port "$PORT"
