#!/usr/bin/env bash set -euo pipefail red() { printf "\033[31m%s\033[0m\n" "$*"; } green() { printf "\033[32m%s\033[0m\n" "$*"; } yellow() { printf "\033[33m%s\033[0m\n" "$*"; } blue() { printf "\033[34m%s\033[0m\n" "$*"; } fail() { red "FEHLER: $*" exit 1 } check_cmd() { command -v "$1" >/dev/null 2>&1 || fail "'$1' nicht gefunden" } mask() { local s="${1:-}" local n=${#s} if [ "$n" -le 8 ]; then printf '********' else printf '%s****%s' "${s:0:4}" "${s: -4}" fi } check_env() { [ -n "${COPILOT_PROVIDER_TYPE:-}" ] || fail "COPILOT_PROVIDER_TYPE fehlt" [ -n "${COPILOT_PROVIDER_API_KEY:-}" ] || fail "COPILOT_PROVIDER_API_KEY fehlt" [ -n "${COPILOT_PROVIDER_BASE_URL:-}" ] || fail "COPILOT_PROVIDER_BASE_URL fehlt" [ -n "${COPILOT_MODEL:-}" ] || fail "COPILOT_MODEL fehlt" } print_env_summary() { blue "== Konfiguration ==" echo "Provider : ${COPILOT_PROVIDER_TYPE}" echo "Base URL : ${COPILOT_PROVIDER_BASE_URL}" echo "Modell : ${COPILOT_MODEL}" echo "API Key : $(mask "${COPILOT_PROVIDER_API_KEY}")" echo } probe_models() { blue "== Prüfe Models-Endpoint ==" local models_url="${COPILOT_PROVIDER_BASE_URL%/}/models" local http_code http_code="$(curl -sS -o /tmp/copilot_models_probe.json \ -w "%{http_code}" \ -H "Authorization: Bearer ${COPILOT_PROVIDER_API_KEY}" \ "$models_url" || true)" echo "GET $models_url" echo "HTTP $http_code" if [ "$http_code" = "200" ]; then green "Models-Endpoint erreichbar" if command -v jq >/dev/null 2>&1; then echo yellow "Gefundene Modelle (erste 20):" jq -r ' if .data then .data[]?.id elif type=="array" then .[]?.id else empty end ' /tmp/copilot_models_probe.json | head -20 echo if jq -e --arg model "${COPILOT_MODEL}" ' if .data then any(.data[]?; .id == $model) elif type=="array" then any(.[]?; .id == $model) else false end ' /tmp/copilot_models_probe.json >/dev/null 2>&1; then green "Modell '${COPILOT_MODEL}' im Models-Endpoint gefunden" else yellow "Modell '${COPILOT_MODEL}' nicht eindeutig im Models-Endpoint gefunden" fi else yellow "jq nicht installiert; JSON-Antwort nicht geparst" fi else red "Models-Endpoint nicht erfolgreich erreichbar" echo sed -n '1,120p' /tmp/copilot_models_probe.json || true echo fi } probe_chat() { blue "== Prüfe Chat-Completions minimal ==" local chat_url="${COPILOT_PROVIDER_BASE_URL%/}/chat/completions" cat >/tmp/copilot_chat_probe.json </dev/null 2>&1; then echo yellow "Antwortauszug:" jq -r '.choices[0].message.content // .choices[0].delta.content // "keine textantwort gefunden"' \ /tmp/copilot_chat_probe_response.json echo else sed -n '1,80p' /tmp/copilot_chat_probe_response.json || true fi else red "Chat-Completions fehlgeschlagen" echo sed -n '1,160p' /tmp/copilot_chat_probe_response.json || true echo fi } run_copilot() { blue "== Starte Copilot CLI ==" exec copilot "$@" } main() { check_cmd curl check_cmd copilot check_env print_env_summary probe_models echo probe_chat echo run_copilot "$@" } main "$@"