#!/usr/bin/env bash
# claude-slancha — run Claude Code with all inference routed to Slancha via the
# ccshim, instead of the Anthropic subscription. Plain `claude` is untouched.
#
# Install:  ln -s "$PWD/scripts/claude-slancha" ~/.local/bin/claude-slancha
# Usage:    claude-slancha                 # interactive, on Slancha
#           claude-slancha -p "..."        # headless
#           SLANCHA_CC_MODEL=claude-opus-4-7 claude-slancha   # -> accurate pref
set -euo pipefail

PORT="${SLANCHA_CCSHIM_PORT:-8787}"
BASE="http://127.0.0.1:${PORT}"

# 1. The shim must be running (launchd agent com.slancha.ccshim, normally).
if ! curl -fsS -o /dev/null "${BASE}/v1/models" 2>/dev/null; then
  echo "ccshim is not responding on ${BASE}." >&2
  echo "Start it with launchd:" >&2
  echo "  launchctl load -w ~/Library/LaunchAgents/com.slancha.ccshim.plist" >&2
  echo "or run it directly from the slancha-dogfood checkout:" >&2
  echo "  .venv/bin/python -m slancha_dogfood.ccshim" >&2
  exit 1
fi

# 2. Point CC at the shim. ANTHROPIC_API_KEY just has to look like a key (sk-*);
#    the shim ignores its value. We set ANTHROPIC_BASE_URL only for this process,
#    so a separate `claude` keeps using the subscription.
export ANTHROPIC_BASE_URL="${BASE}"
export ANTHROPIC_API_KEY="${ANTHROPIC_API_KEY:-sk-slancha-local}"

# Model family steers Slancha routing: opus->accurate, sonnet->fast, haiku->cheap.
exec claude --model "${SLANCHA_CC_MODEL:-claude-sonnet-4-6}" "$@"
