Claude Hooks

Python-Hooks für Claude Code Integration mit Protokollierung, Backup und Task-Erstellung.

Pfad/var/www/tools/ki-protokoll/claude-hook/
Konfiguration.claude/settings.local.json
Datenbankki_dev

Übersicht

HookTriggerFunktion
log_to_db.pyAlle EventsProtokollierung in DB
file_backup_hook.pyPreToolUse (Edit, Write)Datei-Backup vor Änderung
task_hook.pyUserPromptSubmitAuto-Task-Erstellung

log_to_db.py

Protokolliert alle Claude Code Events in der Datenbank.

Features

Verarbeitete Events

EventVerarbeitung
UserPromptSubmitPrompt speichern
PreToolUseTool-Name + Input speichern, pending
PostToolUseResponse zu PreToolUse matchen, completed
SessionStart/EndSession-Lifecycle loggen
Stop/SubagentStopAbschluss loggen

Datenbank-Tabelle

ki_dev.protokoll
├── id, timestamp
├── request_ip, client_name
├── request, request_timestamp
├── response, response_timestamp
├── duration_ms
├── tokens_input, tokens_output, tokens_total
├── model_name, status

file_backup_hook.py

Sichert Dateien automatisch BEVOR sie geändert werden.

Trigger

Backup-Verzeichnisse

/var/www/dev.campus.systemische-tools.de/src
/var/www/dev.campus.systemische-tools.de/public
/var/www/dev.campus.systemische-tools.de/scripts
/var/www/dev.campus.systemische-tools.de/config
/var/www/prod.campus.systemische-tools.de/src
...

Features

Datenbank-Tabelle

ki_dev.file_backup_history
├── id, file_path, version
├── file_content, content_hash, file_size
├── change_type, changed_by, reason
├── created_at

task_hook.py

Erstellt automatisch Tasks aus Patterns in User-Prompts.

Erkannte Patterns

TODO:     → Task erstellen
TASK:     → Task erstellen
@task     → Task erstellen
#task     → Task erstellen

Konfiguration

Hooks werden in .claude/settings.local.json registriert:

{
  "hooks": {
    "UserPromptSubmit": [
      "/var/www/tools/ki-protokoll/claude-hook/log_to_db.py",
      "/var/www/tools/ki-protokoll/claude-hook/task_hook.py"
    ],
    "PreToolUse": [
      "/var/www/tools/ki-protokoll/claude-hook/log_to_db.py",
      "/var/www/tools/ki-protokoll/claude-hook/file_backup_hook.py"
    ],
    "PostToolUse": [
      "/var/www/tools/ki-protokoll/claude-hook/log_to_db.py"
    ]
  }
}

Environment-Variablen

In .env im Hook-Verzeichnis:

CLAUDE_DB_HOST=localhost
CLAUDE_DB_PORT=3306
CLAUDE_DB_USER=root
CLAUDE_DB_PASSWORD=***
CLAUDE_DB_NAME=ki_dev