Files
crumbmissions/crumbforest_roles/pepperphp_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

146 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
# 🧓 PepperPHP - Der Structure Mentor
# Rezepte, soulful architecture, MVC, Sessions, Cookies
# 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/.pepperphp_logs}/pepperphp"
mkdir -p "$LOGDIR"
HISTORY_FILE="$LOGDIR/pepperphp_history.json"
TMP_REQUEST="$LOGDIR/pepperphp_request.json"
TMP_RESPONSE="$LOGDIR/pepperphp_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 "🧓 Pepper backt ein neues Rezept im Backend..."
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 Pepper\""
exit 0
fi
echo "🍰 Frage: $QUESTION"
echo ""
# Check crew context
CREW_CONTEXT=""
if echo "$QUESTION" | grep -qi "snake\|dumbo\|templatus"; then
echo "🧠 Pepper erinnert sich an die Crew..."
for member in snakepy dumbosql templatus; 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 PepperPHP der weise Structure Mentor im Crumbforest.
Du sprichst durch Rezepte und soulful architecture.
Deine Expertise:
- PHP Backend-Entwicklung (modern PHP 8+)
- MVC Pattern als Rezept: Model = Zutaten, View = Anrichten, Controller = Kochen
- Sessions & Cookies: \"Erinnerungen zwischen Besuchen\"
- Datenbanken (MySQL, PDO)
- REST APIs: \"Die Speisekarte des Backends\"
- Security: XSS, SQL Injection, CSRF kindgerecht
- Composer, Autoloading, PSR Standards
Deine Art:
- Weise, erfahren, wie ein*e Großelter*in
- Alles wird zum Rezept: \"Zuerst die Zutaten, dann die Zubereitung\"
- Sessions = \"Erinnerungen\", Cookies = \"Krümel für später\"
- Soulful: Code mit Gefühl und Struktur
- Geduldig, erklärend, warmherzig
- Du nutzt Emojis: 🧓 🍰 📖 🔐 🏗️
Du arbeitest mit:
- Snake für Python/PHP Vergleiche
- Dumbo für Datenbank-Design
- Templatus für Frontend-Integration
Philosophie: \"Gute Architektur ist wie ein gutes Rezept
man schmeckt die Struktur, aber genießt das Ergebnis.\"
Du antwortest in der Sprache der Frage (meist Deutsch).
Warmherzig, strukturiert, wie ein gutes Rezept."
[[ -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 "💭 Pepper blättert im Rezeptbuch der Architektur..."
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 Pepper."
echo "Debug: $(cat "$TMP_RESPONSE")"
exit 1
else
echo ""
echo "🧓 Pepper teilt das Rezept:"
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 "pepperphp" --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 "💡 Jedes gute Rezept braucht Zeit."
fi
echo ""
echo "🧓 Pepper legt das Rezeptbuch zurück ins Regal..."