Files
crumbmissions/crumbmission/crumb_logger.sh
Branko May Trinkwald 2915828adf Add complete Crumbforest mission system
- Interactive mission selector with metadata-driven design
- 5 educational missions (basics + advanced)
- AI assistant roles (Deepbit, Bugsy, Schnippsi, Tobi)
- SnakeCam gesture recognition system
- Token tracking utilities
- CLAUDE.md documentation
- .gitignore for logs and secrets
2025-12-21 01:16:48 +01:00

42 lines
1.4 KiB
Bash
Executable File

#!/bin/bash
crumb_log() {
ROLE="$1"
RESPONSE_TEXT="$2"
TMP_RESPONSE="$3"
LOGDIR="$HOME/.${ROLE,,}_logs"
mkdir -p "$LOGDIR"
HISTORY_FILE="$LOGDIR/${ROLE,,}_history.json"
TOKEN_LOG="$LOGDIR/token_log.json"
# Init logs if missing or broken
[[ ! -f "$HISTORY_FILE" ]] && echo "[]" > "$HISTORY_FILE"
[[ ! -f "$TOKEN_LOG" ]] && echo "[]" > "$TOKEN_LOG"
if ! jq -e 'type=="array"' "$HISTORY_FILE" >/dev/null 2>&1; then echo "[]" > "$HISTORY_FILE"; fi
if ! jq -e 'type=="array"' "$TOKEN_LOG" >/dev/null 2>&1; then echo "[]" > "$TOKEN_LOG"; fi
# Add response to history
jq -n --arg role "assistant" --arg content "$RESPONSE_TEXT" \
'{role: $role, content: $content}' > "$LOGDIR/new_history.json"
jq '. + [ input ]' "$HISTORY_FILE" "$LOGDIR/new_history.json" > "$LOGDIR/tmp_history.json" \
&& mv "$LOGDIR/tmp_history.json" "$HISTORY_FILE" \
&& rm "$LOGDIR/new_history.json"
# Add usage if present
if jq -e '.usage' "$TMP_RESPONSE" >/dev/null; then
TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
USAGE_JSON=$(jq '.usage' "$TMP_RESPONSE")
jq -n --arg zeit "$TIMESTAMP" --arg rolle "$ROLE" --argjson usage "$USAGE_JSON" \
'{zeit: $zeit, rolle: $rolle, usage: $usage}' > "$LOGDIR/new_token.json"
jq '. + [ input ]' "$TOKEN_LOG" "$LOGDIR/new_token.json" > "$LOGDIR/tmp_token.json" \
&& mv "$LOGDIR/tmp_token.json" "$TOKEN_LOG" \
&& rm "$LOGDIR/new_token.json"
fi
}