#!/bin/bash # 🦊 FunkFox - Der Bash Rapper im Beat # Rhythmus, Flow, Terminal als Musik # Load .env if exists SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ENV_FILE="${SCRIPT_DIR}/../.env" if [[ -f "${ENV_FILE}" ]]; then set -a source "${ENV_FILE}" set +a fi QUESTION="$*" API_KEY="${OPENROUTER_API_KEY}" MODEL="${OPENROUTER_MODEL:-openai/gpt-3.5-turbo}" # Logs LOGDIR="${CRUMB_LOGS_DIR:-$HOME/.funkfox_logs}/funkfox" mkdir -p "$LOGDIR" HISTORY_FILE="$LOGDIR/funkfox_history.json" TMP_REQUEST="$LOGDIR/funkfox_request.json" TMP_RESPONSE="$LOGDIR/funkfox_response.json" LOG_FILE="$LOGDIR/token_log.json" [ ! -f "$HISTORY_FILE" ] && echo "[]" > "$HISTORY_FILE" [ ! -f "$LOG_FILE" ] && echo "[]" > "$LOG_FILE" # === CREW MEMORY FUNCTIONS === function read_crew_memory() { local role="$1" local role_log="$HOME/.${role}_logs/${role}_history.json" if [[ -f "$role_log" ]]; then jq -r '.[-3:] | .[] | "[\(.role)]: \(.content)"' "$role_log" 2>/dev/null | head -n 3 fi } # === MAIN === echo "🦊 FunkFox droppt in den Beat..." echo "🎡 *dum-tss-dum-tss* 🎡" echo "" if [ -z "$API_KEY" ]; then echo "❗ Yo yo! Kein API-Key im Flow. Check die .env, Bro!" exit 1 fi if [ -z "$QUESTION" ]; then echo "πŸ’‘ Verwendung: $0 \"Deine Frage an FunkFox\"" echo "🎀 Gib mir 'nen Beat, dann flow ich los!" exit 0 fi echo "🎀 FunkFox hΓΆrt: $QUESTION" echo "" # Check if question references other crew members CREW_CONTEXT="" if echo "$QUESTION" | grep -qi "deepbit\|dumbo\|taube\|schnippsi"; then echo "🎡 FunkFox checkt was die Crew gedropt hat..." for member in deepbit dumbosql taube; do if echo "$QUESTION" | grep -qi "$member"; then MEMBER_CONTEXT=$(read_crew_memory "$member") if [[ -n "$MEMBER_CONTEXT" ]]; then CREW_CONTEXT="${CREW_CONTEXT}\n\n${member^} hat kΓΌrzlich gesagt:\n${MEMBER_CONTEXT}" fi fi done fi # Build system prompt SYSTEM_PROMPT="Du bist FunkFox – der Bash Rapper im Crumbforest. Du erklΓ€rst Terminal-Kommandos durch Rhythmus, Flow und Bewegung. Deine Expertise: - Bash-Kommandos als rhythmische Flows erklΓ€ren - Pipes | sind wie Beat-ÜbergΓ€nge - Redirects > < sind Flow-Richtungen - Commands sind wie Rap-Lines: kurz, prΓ€zise, kraftvoll - Terminal ist dein Beatpad Deine Art: - Rhythmisch, mit Flow, im Beat - Nutze Rap-Metaphern: \"grep ist wie'n Filter, yo!\" - Kurze Lines, klare Message - Manchmal Reime, immer Rhythmus - \"Dum-tss\" und \"Yo\" sind Teil deines Flows - Du nutzt Emojis: 🦊 🎀 🎡 πŸ’« πŸ”₯ Du arbeitest mit: - Deepbit fΓΌr tiefe technische Details - Dumbo fΓΌr Daten-Struktur - Taichi Taube fΓΌr Balance im Flow WICHTIG: Du rappst nicht komplett, aber deine ErklΓ€rungen haben RHYTHMUS. ErklΓ€re Bash-Konzepte durch Bewegung, Flow und Beat-Metaphern. Du antwortest in der Sprache der Frage (meist Deutsch). Keep it funky, keep it flowing! 🦊" # Add crew context if available if [[ -n "$CREW_CONTEXT" ]]; then SYSTEM_PROMPT="${SYSTEM_PROMPT}\n\nKontext von der Crew:${CREW_CONTEXT}" fi # Create API request jq -n \ --arg model "$MODEL" \ --arg system "$SYSTEM_PROMPT" \ --arg user "$QUESTION" \ '{ "model": $model, "temperature": 0.8, "messages": [ {"role": "system", "content": $system}, {"role": "user", "content": $user} ] }' > "$TMP_REQUEST" # Send request echo "πŸ’­ FunkFox findet den Beat..." echo "🎡 *scratching noises* 🎡" curl -s https://openrouter.ai/api/v1/chat/completions \ -H "Authorization: Bearer $API_KEY" \ -H "Content-Type: application/json" \ -d @"$TMP_REQUEST" > "$TMP_RESPONSE" RESPONSE_TEXT=$(jq -r '.choices[0].message.content // empty' "$TMP_RESPONSE") if [[ -z "$RESPONSE_TEXT" ]]; then echo "🚫 Kein Beat von FunkFox." echo "Debug: $(cat "$TMP_RESPONSE")" exit 1 else echo "" echo "🦊 FunkFox droppt Wissen:" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "$RESPONSE_TEXT" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "" # Store conversation in history jq -n --arg role "assistant" --arg content "$RESPONSE_TEXT" \ '{"role": $role, "content": $content}' > "$LOGDIR/new_entry.json" jq -s '.[0] + [.[1]]' "$HISTORY_FILE" "$LOGDIR/new_entry.json" > "$LOGDIR/new_history.json" && \ mv "$LOGDIR/new_history.json" "$HISTORY_FILE" && rm "$LOGDIR/new_entry.json" fi # Token Tracking if jq -e '.usage' "$TMP_RESPONSE" > /dev/null 2>&1; then TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S") TOKENS_USED=$(jq -r '.usage.total_tokens' "$TMP_RESPONSE") jq -n \ --arg zeit "$TIMESTAMP" \ --arg rolle "funkfox" \ --arg model "$MODEL" \ --argjson usage "$(jq '.usage' "$TMP_RESPONSE")" \ '{zeit: $zeit, rolle: $rolle, model: $model, usage: $usage}' >> "$LOG_FILE" echo "πŸ“Š Token-Verbrauch: $TOKENS_USED Tokens" echo "πŸ’‘ Jeder Token ist ein Beat im Flow." fi echo "" echo "🦊 FunkFox bounced zurΓΌck in den Beat... *mic drop* 🎀"