1
0
Fork 0
mirror of https://github.com/imjasonh/cnotes synced 2026-07-12 10:51:20 +00:00
cnotes/internal/handlers/precompact.go
Jason Hall 45ef980233
Scaffold all 7 hook events and eliminate type casting
- Add missing hook handlers: Stop, SubagentStop, PreCompact
- Replace all string type casting with proper JSON unmarshaling
- Change ToolUseRequest.Parameters from map[string]interface{} to json.RawMessage
- Update helper methods to use json.Unmarshal directly to typed structs
- Replace speech notifications with simple logging notifications
- Update documentation to reflect all hook events and remove speech references
- Add comprehensive CLAUDE.md with architecture overview and development guidance

All hooks now use type-safe JSON handling without .(string) assertions.
Complete coverage of Claude Code events: PreToolUse, PostToolUse,
UserPromptSubmit, Notification, Stop, SubagentStop, PreCompact.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-21 12:01:55 -04:00

69 lines
2.2 KiB
Go

package handlers
import (
"context"
"fmt"
"log/slog"
"time"
"github.com/imjasonh/hooks/internal/hooks"
)
func init() {
hooks.RegisterHook(hooks.EventPreCompact, "*", HandlePreCompact)
}
// HandlePreCompact is triggered before Claude Code runs a compact operation to
// reduce context window size. This can happen manually via /compact command
// or automatically when the context window fills up.
//
// The matcher indicates the compaction trigger:
// - "manual": User ran /compact command
// - "auto": Automatic compaction due to context window limit
//
// Common use cases:
// - Save conversation state before compaction
// - Warn user about potential data loss
// - Export important context to external files
// - Create conversation backups
// - Log compaction events for analysis
func HandlePreCompact(ctx context.Context, input hooks.HookInput) (hooks.HookOutput, error) {
// Determine compaction type from the tool name/matcher
compactionType := "unknown"
if input.Tool == "manual" || input.ToolName == "manual" {
compactionType = "manual"
} else if input.Tool == "auto" || input.ToolName == "auto" {
compactionType = "auto"
}
slog.Info("pre-compaction hook triggered",
"session_id", input.SessionID,
"compaction_type", compactionType,
"cwd", input.CWD,
"transcript_path", input.TranscriptPath,
"timestamp", time.Now().Unix())
// Add context warning for the user
var warningContext string
if compactionType == "manual" {
warningContext = "Manual compaction requested - some conversation history will be summarized"
} else if compactionType == "auto" {
warningContext = "Automatic compaction triggered - context window limit reached"
} else {
warningContext = "Context compaction about to occur"
}
// TODO: Future enhancements could include:
// - Export full conversation before compaction
// - Save important code snippets or commands
// - Create conversation timeline backup
// - Allow user to specify what to preserve
// - Integration with external note-taking systems
// - Automatic git commit of current work
// - Context importance scoring and preservation
return hooks.HookOutput{
Decision: "approve",
AdditionalContext: fmt.Sprintf("🔄 %s", warningContext),
}, nil
}