diff --git a/lib/waldwaechter.sh b/lib/waldwaechter.sh index b7ff266..1f883c2 100644 --- a/lib/waldwaechter.sh +++ b/lib/waldwaechter.sh @@ -630,9 +630,22 @@ function check_token_budget() { for token_file in "${CRUMB_LOGS_DIR}"/*/token_log.json; do if [[ -f "$token_file" ]]; then - # Summiere nur Tokens von heute - local tokens=$(jq -r --arg today "$today" '[.[] | select(.zeit | startswith($today)) | .usage.total_tokens // 0] | add // 0' "$token_file" 2>/dev/null || echo 0) - total_today=$((total_today + tokens)) + # Summiere nur Tokens von heute (robuster Ansatz für verschiedene Log-Formate) + local tokens=$(grep "$today" "$token_file" 2>/dev/null | grep -v '^\[\]$' | while read line; do + # Versuche total_tokens zu extrahieren (unterstützt usage als String oder Objekt) + echo "$line" | jq -r ' + if .usage | type == "string" then + .usage | fromjson | .total_tokens // 0 + else + .usage.total_tokens // 0 + end + ' 2>/dev/null || echo 0 + done | awk '{sum+=$1} END {print sum+0}') + + # Nur addieren wenn tokens eine gültige Zahl ist + if [[ "$tokens" =~ ^[0-9]+$ ]]; then + total_today=$((total_today + tokens)) + fi fi done