diff --git a/native_crumbcore_v1/setup_missions.sh b/native_crumbcore_v1/setup_missions.sh index 952a678..f32d472 100755 --- a/native_crumbcore_v1/setup_missions.sh +++ b/native_crumbcore_v1/setup_missions.sh @@ -209,17 +209,43 @@ TOKEN fi # 7. Global Dependencies (Token Check) -# The "fat" roles rely on 'check_token_budget' being in the PATH. -TOKEN_CHECK_SCRIPT="$ROLES_DIR/token_check.sh" +# We generate the official "Accountant" script directly to /usr/local/bin TARGET_TOKEN_BIN="/usr/local/bin/check_token_budget" -if [ -f "$TOKEN_CHECK_SCRIPT" ]; then - print_info "Installing global check_token_budget command..." - # We copy it to /usr/local/bin/check_token_budget - cp -f "$TOKEN_CHECK_SCRIPT" "$TARGET_TOKEN_BIN" - chmod +x "$TARGET_TOKEN_BIN" - print_info "Installed: $TARGET_TOKEN_BIN" -fi +print_info "Installing global check_token_budget command..." +cat << 'TOKEN' > "$TARGET_TOKEN_BIN" +#!/bin/bash +# Crumbforest Token Budget Checker +# Sums up tokens from all log files + +echo "πŸ“Š Crumbforest Token Budget" +TRAFFIC=0 + +# Find logs (avoiding .bak and .cleaned) +# We look in the current user's home +LOGS=$(find "$HOME" -name "token_log.json" -type f 2>/dev/null) + +for f in $LOGS; do + # Check if valid JSON content exists + if [ -s "$f" ]; then + # Try to sum total_tokens. Handle potential empty lines or [] + # We use inputs to handle stream of objects + SUM=$(jq -n '[inputs | .usage.total_tokens // 0] | add' "$f" 2>/dev/null) + if [ "$SUM" != "null" ] && [ "$SUM" != "" ] && [ "$SUM" != "0" ]; then + echo " πŸ“„ $(basename $(dirname "$f"))/$(basename "$f"): $SUM KrΓΌmel" + TRAFFIC=$((TRAFFIC + SUM)) + fi + fi +done + +echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" +echo "πŸ’° Gesamtverbrauch: $TRAFFIC KrΓΌmel" +TOKEN + +# Ensure it is executable by EVERYONE (755) +chmod 755 "$TARGET_TOKEN_BIN" +chown root:root "$TARGET_TOKEN_BIN" +print_info "Installed: $TARGET_TOKEN_BIN (Mode 755)" # 8. Fix Mission Selector (Waldwaechter Patch) # The script lib/waldwaechter.sh has a bug with 'stat' on Debian, causing export errors.