#!/bin/sh -e
#
# ollama-chat - chat with local llm
# dependencies: ollama | jq

MODEL=${MODEL:-"$(ollama-current)"}
echo "🧠 ${MODEL} REPL - press Ctrl+D to exit"

while true; do
  read -r -p "> " input || break
  [ -z "${input}" ] && continue

  # shellcheck disable=SC2312
  curl -s http://localhost:11434/api/generate \
    -H "Content-Type: application/json" \
    -d "$(jq --null-input --color-output --arg model "${MODEL}" --arg prompt \
      "${input}" '{model: $model, prompt: $prompt, stream: false}')" \
    | jq --raw-output '.response // "⚠️ Empty response or model error"' \
    | (glow --width=200 --style=auto || bat) 2>/dev/null
done
