Class: ClaudeMemory::Hook::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/claude_memory/hook/handler.rb

Defined Under Namespace

Classes: PayloadError

Constant Summary collapse

DEFAULT_SWEEP_BUDGET =
5

Instance Method Summary collapse

Constructor Details

#initialize(store, env: ENV, manager: nil) ⇒ Handler

Returns a new instance of Handler.



10
11
12
13
14
15
# File 'lib/claude_memory/hook/handler.rb', line 10

def initialize(store, env: ENV, manager: nil)
  @store = store
  @manager = manager
  @config = Configuration.new(env)
  @env = env
end

Instance Method Details

#context(payload) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/claude_memory/hook/handler.rb', line 63

def context(payload)
  manager = @manager || build_manager(payload)
  manager.ensure_both!

  source = payload["source"]
  injector = ContextInjector.new(manager, source: source)
  context_text = injector.generate_context

  {status: :ok, context: context_text}
rescue => e
  {status: :error, context: nil, message: e.message}
end

#ingest(payload) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/claude_memory/hook/handler.rb', line 17

def ingest(payload)
  session_id = payload["session_id"] || @config.session_id
  transcript_path = payload["transcript_path"] || @config.transcript_path
  project_path = payload["project_path"] || @config.project_dir

  raise PayloadError, "Missing required field: session_id" if session_id.nil? || session_id.empty?
  raise PayloadError, "Missing required field: transcript_path" if transcript_path.nil? || transcript_path.empty?

  ingester = Ingest::Ingester.new(@store, env: @env)
  result = ingester.ingest(
    source: "claude_code",
    session_id: session_id,
    transcript_path: transcript_path,
    project_path: project_path
  )

  if result[:status] == :ingested && result[:content_id]
    DistillationRunner.new(@store).distill_item(
      result[:content_id], project_path: project_path
    )
  end

  result
rescue Ingest::TranscriptReader::FileNotFoundError => e
  # Transcript file doesn't exist (e.g., headless Claude session)
  # This is expected, not an error - return success with no-op status
  {status: :skipped, reason: "transcript_not_found", message: e.message}
end

#publish(payload) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/claude_memory/hook/handler.rb', line 54

def publish(payload)
  mode = payload.fetch("mode", "shared").to_sym
  since = payload["since"]
  rules_dir = payload["rules_dir"]

  publisher = Publish.new(@store)
  publisher.publish!(mode: mode, since: since, rules_dir: rules_dir)
end

#sweep(payload) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/claude_memory/hook/handler.rb', line 46

def sweep(payload)
  budget = payload.fetch("budget", DEFAULT_SWEEP_BUDGET).to_i
  sweeper = Sweep::Sweeper.new(@store)
  stats = sweeper.run!(budget_seconds: budget)

  {stats: stats}
end