Files
crumbmissions/crumbforest_roles/crabbyrust_zero.sh
Branko May Trinkwald 271008ea10 📊 Logs jetzt im Repo statt User-Verzeichnis!
**Änderung: Option A - logs/ im Repo**

Vorher:
- Logs in ~/.{character}_logs/ (User Home Directory)
- Multi-Projekt aber verstreut
- Nicht im Repo sichtbar

Nachher:
- Logs in logs/{character}/ (Repo Directory)
- Projekt-spezifisch & übersichtlich
- Mit Fallback: ${CRUMB_LOGS_DIR:-$HOME/.{character}_logs}
- Für Standalone-Nutzung

**Geänderte Files:**
- lib/waldwaechter.sh: CRUMB_LOGS_DIR exportiert
- Alle 17 crumbforest_roles/*: LOGDIR updated
- missions/robots/*: Mission logs → logs/missions/
- .gitignore: logs/ hinzugefügt

**Log-Struktur:**
```
logs/
├── tobi/
│   ├── tobi_history.json
│   ├── token_log.json
│   └── ...
├── crabbyrust/
├── mayaeule/
└── missions/
```

**Tested:**  Tobi funktioniert, Logs landen in logs/tobi/

🌲 "Was kostet die Frage eines Kindes?" - jetzt transparent im Repo!

🤖 Generated with Claude Code
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-21 16:25:10 +01:00

145 lines
4.9 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 🦀 CrabbyRust - Der Security Guardian
# Ownership, Memory Protection, geduldiger Lehrer
# 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/.crabbyrust_logs}/crabbyrust"
mkdir -p "$LOGDIR"
HISTORY_FILE="$LOGDIR/crabbyrust_history.json"
TMP_REQUEST="$LOGDIR/crabbyrust_request.json"
TMP_RESPONSE="$LOGDIR/crabbyrust_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 "🦀 Crabby kneift vorsichtig in den Code..."
echo ""
if [ -z "$API_KEY" ]; then
echo "❗ Kein API-Key gefunden. Bitte setze OPENROUTER_API_KEY in .env"
exit 1
fi
if [ -z "$QUESTION" ]; then
echo "💡 Verwendung: $0 \"Deine Frage an Crabby\""
exit 0
fi
echo "🔒 Frage: $QUESTION"
echo ""
# Check crew context
CREW_CONTEXT=""
if echo "$QUESTION" | grep -qi "snake\|pepper\|schraubaer"; then
echo "🧠 Crabby prüft die Sicherheit der Crew..."
for member in snakepy pepperphp schraubaer; do
if echo "$QUESTION" | grep -qi "$member"; then
MEMBER_CONTEXT=$(read_crew_memory "$member")
[[ -n "$MEMBER_CONTEXT" ]] && CREW_CONTEXT="${CREW_CONTEXT}\n\n${member^}:\n${MEMBER_CONTEXT}"
fi
done
fi
# Build system prompt
SYSTEM_PROMPT="Du bist CrabbyRust der geduldige Security Guardian im Crumbforest.
Du lehrst Rust's Ownership-Model und Memory Protection.
Deine Expertise:
- Rust Basics: Ownership, Borrowing, Lifetimes
- Memory Safety ohne Garbage Collection
- Sicherheit kindgerecht: \"Wem gehört was?\"
- Embedded Rust (für Roboter!)
- Error Handling: Result<T,E> als \"ehrliches Feedback\"
- Concurrency: \"Threads die sich nicht in die Quere kommen\"
Deine Art:
- Geduldig trotz des Namens \"Crabby\"
- Ownership als Besitz erklären: \"Das Spielzeug gehört dir, nicht mir\"
- Compiler-Fehler als Freund: \"Der Compiler hilft dir!\"
- Sicherheit durch Verständnis, nicht Angst
- Metaphern aus der Natur: Krebse schützen ihren Panzer
- Du nutzt Emojis: 🦀 🔒 🛡️ ⚡ 🔐
Du arbeitest mit:
- Snake für System-Vergleiche (Python vs Rust)
- Pepper für Backend-Security
- Schraubbär für Embedded/Hardware-Projekte
Philosophie: \"Sicherheit ist kein Zufall, sondern Verständnis.
Der Rust-Compiler ist dein Freund, nicht dein Feind.\"
Du antwortest in der Sprache der Frage (meist Deutsch).
Geduldig, sicherheitsbewusst, verständnisvoll."
[[ -n "$CREW_CONTEXT" ]] && SYSTEM_PROMPT="${SYSTEM_PROMPT}\n\nCrew-Kontext:${CREW_CONTEXT}"
# Create API request
jq -n --arg model "$MODEL" --arg system "$SYSTEM_PROMPT" --arg user "$QUESTION" \
'{"model": $model, "temperature": 0.7, "messages": [{"role": "system", "content": $system}, {"role": "user", "content": $user}]}' > "$TMP_REQUEST"
echo "💭 Crabby prüft jeden Speicherbereich..."
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 "🚫 Keine Antwort von Crabby."
echo "Debug: $(cat "$TMP_RESPONSE")"
exit 1
else
echo ""
echo "🦀 Crabby erklärt sicher:"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "$RESPONSE_TEXT"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
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 "crabbyrust" --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 "💡 Memory-safe auch bei Tokens."
fi
echo ""
echo "🦀 Crabby bewacht weiter den Speicher..."