Hook-Integration
Automatische Task-Erstellung aus Claude Code Prompts via Hook-System.
| Hook-Script | /var/www/tools/ki-protokoll/claude-hook/task_hook.py |
|---|---|
| Trigger | UserPromptSubmit |
| Datenbank | ki_protokoll |
Erkannte Patterns
Der Hook erkennt folgende Patterns in User-Prompts und erstellt automatisch Tasks:
| Pattern | Beispiel | Task-Titel |
|---|---|---|
TODO: <text> | TODO: Tests schreiben | Tests schreiben |
TASK: <text> | TASK: API dokumentieren | API dokumentieren |
@task <text> | @task Refactoring | Refactoring |
#task <text> | #task Bug fixen | Bug fixen |
Funktionsweise
User-Prompt: "Bitte TODO: Logging implementieren und den Code reviewen"
↓
Hook erkennt Pattern
↓
Task erstellt: "Logging implementieren"
↓
type: ai_task
status: pending
created_by: $USER
created_by_type: ai
Hook-Konfiguration
Datei: /var/www/dev.campus.systemische-tools.de/.claude/settings.local.json
{
"hooks": {
"UserPromptSubmit": [
{
"hooks": [
{
"type": "command",
"command": "/var/www/tools/ki-protokoll/claude-hook/log_to_db.py",
"timeout": 5
},
{
"type": "command",
"command": "/var/www/tools/ki-protokoll/claude-hook/task_hook.py",
"timeout": 5
}
]
}
]
}
}
Hook-Script
Konfiguration
# Datenbankverbindung
DB_CONFIG = {
'host': 'localhost',
'port': 3306,
'user': 'claude_code',
'password': '***',
'database': 'ki_protokoll',
'charset': 'utf8mb4'
}
# Erkennungsmuster
TASK_PATTERNS = [
(r'TODO:\s*(.+?)(?:\n|$)', 'todo'),
(r'TASK:\s*(.+?)(?:\n|$)', 'task'),
(r'@task\s+(.+?)(?:\n|$)', 'mention'),
(r'#task\s+(.+?)(?:\n|$)', 'hashtag'),
]
Erstellte Task-Eigenschaften
| Eigenschaft | Wert |
|---|---|
| type | ai_task |
| priority | medium |
| status | pending |
| created_by | $USER oder 'claude-code-hook' |
| created_by_type | ai |
Installation
1. Hook ausführbar machen
chmod +x /var/www/tools/ki-protokoll/claude-hook/task_hook.py
2. Python-Abhängigkeiten
pip3 install pymysql
3. Hook in settings.local.json registrieren
# Füge den task_hook.py zu den UserPromptSubmit Hooks hinzu
4. Testen
# In Claude Code eingeben:
"TODO: Test-Task erstellen"
# Prüfen:
php /var/www/tools/ki-tasks/cli.php list
Fehlerbehandlung
Der Hook ist non-blocking. Fehler werden zu stderr geloggt, aber der Claude Code Workflow wird nicht unterbrochen.
# Immer zurückgegeben:
{"continue": true}
Debugging
# Hook manuell testen
echo '{"hook_event_name":"UserPromptSubmit","prompt":"TODO: Test"}' | python3 /var/www/tools/ki-protokoll/claude-hook/task_hook.py
# Logs prüfen (stderr)
# Task #123 created: Test...
Erweiterungen
Eigene Patterns hinzufügen
# In task_hook.py
TASK_PATTERNS = [
# ... bestehende Patterns ...
(re.compile(r'FIXME:\s*(.+?)(?:\n|$)', re.IGNORECASE), 'fixme'),
(re.compile(r'BUG:\s*(.+?)(?:\n|$)', re.IGNORECASE), 'bug'),
]
Priorität aus Pattern ableiten
# Beispiel: CRITICAL: -> priority=critical
if source == 'critical':
priority = 'critical'
elif source == 'bug':
priority = 'high'
else:
priority = 'medium'
Zusammenspiel mit anderen Hooks
| Hook | Funktion | Event |
|---|---|---|
| log_to_db.py | Protokolliert alle Events | Alle |
| file_backup_hook.py | Backup vor Dateiänderungen | PreToolUse (Edit, Write) |
| task_hook.py | Tasks aus Prompts erstellen | UserPromptSubmit |