Просмотр исходного кода

feat: add claude lifecycle logging hooks

lingfengQAQ 2 месяцев назад
Родитель
Сommit
3ee844022e
2 измененных файлов с 234 добавлено и 0 удалено
  1. 182 0
      webnovel-writer/hooks/hooks.json
  2. 52 0
      webnovel-writer/hooks/log-hook-event.ps1

+ 182 - 0
webnovel-writer/hooks/hooks.json

@@ -0,0 +1,182 @@
+{
+  "description": "Monitor Claude Code lifecycle and tool-call events for the webnovel-writer plugin.",
+  "hooks": {
+    "SessionStart": [
+      {
+        "matcher": "*",
+        "hooks": [
+          {
+            "type": "command",
+            "shell": "powershell",
+            "command": "& \"$env:CLAUDE_PLUGIN_ROOT\\hooks\\log-hook-event.ps1\""
+          }
+        ]
+      }
+    ],
+    "InstructionsLoaded": [
+      {
+        "matcher": "*",
+        "hooks": [
+          {
+            "type": "command",
+            "shell": "powershell",
+            "command": "& \"$env:CLAUDE_PLUGIN_ROOT\\hooks\\log-hook-event.ps1\""
+          }
+        ]
+      }
+    ],
+    "UserPromptSubmit": [
+      {
+        "hooks": [
+          {
+            "type": "command",
+            "shell": "powershell",
+            "command": "& \"$env:CLAUDE_PLUGIN_ROOT\\hooks\\log-hook-event.ps1\""
+          }
+        ]
+      }
+    ],
+    "PreToolUse": [
+      {
+        "matcher": "*",
+        "hooks": [
+          {
+            "type": "command",
+            "shell": "powershell",
+            "command": "& \"$env:CLAUDE_PLUGIN_ROOT\\hooks\\log-hook-event.ps1\""
+          }
+        ]
+      }
+    ],
+    "PostToolUse": [
+      {
+        "matcher": "*",
+        "hooks": [
+          {
+            "type": "command",
+            "shell": "powershell",
+            "command": "& \"$env:CLAUDE_PLUGIN_ROOT\\hooks\\log-hook-event.ps1\""
+          }
+        ]
+      }
+    ],
+    "PostToolUseFailure": [
+      {
+        "matcher": "*",
+        "hooks": [
+          {
+            "type": "command",
+            "shell": "powershell",
+            "command": "& \"$env:CLAUDE_PLUGIN_ROOT\\hooks\\log-hook-event.ps1\""
+          }
+        ]
+      }
+    ],
+    "PermissionRequest": [
+      {
+        "matcher": "*",
+        "hooks": [
+          {
+            "type": "command",
+            "shell": "powershell",
+            "command": "& \"$env:CLAUDE_PLUGIN_ROOT\\hooks\\log-hook-event.ps1\""
+          }
+        ]
+      }
+    ],
+    "PermissionDenied": [
+      {
+        "matcher": "*",
+        "hooks": [
+          {
+            "type": "command",
+            "shell": "powershell",
+            "command": "& \"$env:CLAUDE_PLUGIN_ROOT\\hooks\\log-hook-event.ps1\""
+          }
+        ]
+      }
+    ],
+    "SubagentStart": [
+      {
+        "matcher": "*",
+        "hooks": [
+          {
+            "type": "command",
+            "shell": "powershell",
+            "command": "& \"$env:CLAUDE_PLUGIN_ROOT\\hooks\\log-hook-event.ps1\""
+          }
+        ]
+      }
+    ],
+    "SubagentStop": [
+      {
+        "matcher": "*",
+        "hooks": [
+          {
+            "type": "command",
+            "shell": "powershell",
+            "command": "& \"$env:CLAUDE_PLUGIN_ROOT\\hooks\\log-hook-event.ps1\""
+          }
+        ]
+      }
+    ],
+    "TaskCreated": [
+      {
+        "hooks": [
+          {
+            "type": "command",
+            "shell": "powershell",
+            "command": "& \"$env:CLAUDE_PLUGIN_ROOT\\hooks\\log-hook-event.ps1\""
+          }
+        ]
+      }
+    ],
+    "TaskCompleted": [
+      {
+        "hooks": [
+          {
+            "type": "command",
+            "shell": "powershell",
+            "command": "& \"$env:CLAUDE_PLUGIN_ROOT\\hooks\\log-hook-event.ps1\""
+          }
+        ]
+      }
+    ],
+    "PreCompact": [
+      {
+        "matcher": "*",
+        "hooks": [
+          {
+            "type": "command",
+            "shell": "powershell",
+            "command": "& \"$env:CLAUDE_PLUGIN_ROOT\\hooks\\log-hook-event.ps1\""
+          }
+        ]
+      }
+    ],
+    "PostCompact": [
+      {
+        "matcher": "*",
+        "hooks": [
+          {
+            "type": "command",
+            "shell": "powershell",
+            "command": "& \"$env:CLAUDE_PLUGIN_ROOT\\hooks\\log-hook-event.ps1\""
+          }
+        ]
+      }
+    ],
+    "SessionEnd": [
+      {
+        "matcher": "*",
+        "hooks": [
+          {
+            "type": "command",
+            "shell": "powershell",
+            "command": "& \"$env:CLAUDE_PLUGIN_ROOT\\hooks\\log-hook-event.ps1\""
+          }
+        ]
+      }
+    ]
+  }
+}

+ 52 - 0
webnovel-writer/hooks/log-hook-event.ps1

@@ -0,0 +1,52 @@
+$ErrorActionPreference = "Stop"
+
+$raw = [Console]::In.ReadToEnd()
+if ([string]::IsNullOrWhiteSpace($raw)) {
+    exit 0
+}
+
+$pluginData = $env:CLAUDE_PLUGIN_DATA
+if ([string]::IsNullOrWhiteSpace($pluginData)) {
+    $pluginRoot = $env:CLAUDE_PLUGIN_ROOT
+    if ([string]::IsNullOrWhiteSpace($pluginRoot)) {
+        exit 0
+    }
+    $pluginData = Join-Path $pluginRoot ".tmp_plugin_data"
+}
+
+$logDir = Join-Path $pluginData "logs"
+New-Item -ItemType Directory -Force -Path $logDir | Out-Null
+
+$payload = $null
+try {
+    $payload = $raw | ConvertFrom-Json -Depth 100
+} catch {
+    $payload = $null
+}
+
+if ($null -ne $payload) {
+    $eventName = [string]$payload.hook_event_name
+    if ([string]::IsNullOrWhiteSpace($eventName)) {
+        $eventName = "unknown"
+    }
+
+    $record = [ordered]@{
+        observed_at = (Get-Date).ToString("o")
+        event = $eventName
+        session_id = [string]$payload.session_id
+        cwd = [string]$payload.cwd
+        transcript_path = [string]$payload.transcript_path
+        payload = $payload
+    }
+} else {
+    $record = [ordered]@{
+        observed_at = (Get-Date).ToString("o")
+        event = "invalid_json"
+        raw = $raw
+    }
+    $eventName = "invalid_json"
+}
+
+$json = $record | ConvertTo-Json -Depth 100 -Compress
+Add-Content -LiteralPath (Join-Path $logDir "hook-events.jsonl") -Value $json
+Add-Content -LiteralPath (Join-Path $logDir ("{0}.jsonl" -f $eventName)) -Value $json