Class: Rubino::Memory::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/rubino/memory/extractor.rb

Overview

Extracts potential memories from conversation history. Identifies facts, preferences, decisions, and other memorable items.

Constant Summary collapse

PREFERENCE_PATTERNS =

Patterns that suggest extractable memories

[
  /(?:I prefer|I like|I always|I never|I usually|my preferred)/i,
  /(?:please always|please never|don't ever|always use|never use)/i
].freeze
DECISION_PATTERNS =
[
  /(?:we decided|the decision is|let's go with|I chose|we'll use)/i,
  /(?:the approach is|the strategy is|we agreed on)/i
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(store: nil) ⇒ Extractor

Returns a new instance of Extractor.



19
20
21
# File 'lib/rubino/memory/extractor.rb', line 19

def initialize(store: nil)
  @store = store || Store.new
end

Instance Method Details

#extract_from_content(content, session_id = nil) ⇒ Object

Extracts memories from a single content string



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rubino/memory/extractor.rb', line 41

def extract_from_content(content, session_id = nil)
  memories = []

  # Check for preferences
  if matches_patterns?(content, PREFERENCE_PATTERNS)
    memories << save_memory(
      kind: "preference",
      content: content.strip[0..500],
      session_id: session_id
    )
  end

  # Check for technical decisions
  if matches_patterns?(content, DECISION_PATTERNS)
    memories << save_memory(
      kind: "technical_decision",
      content: content.strip[0..500],
      session_id: session_id
    )
  end

  memories.compact
end

#extract_from_session(session_id) ⇒ Object

Extracts memories from a session’s messages



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rubino/memory/extractor.rb', line 24

def extract_from_session(session_id)
  message_store = Session::Store.new
  messages = message_store.for_session(session_id)
  extracted = []

  messages.each do |msg|
    next unless %w[user assistant].include?(msg.role)
    next if msg.content.nil? || msg.content.empty?

    memories = extract_from_content(msg.content, session_id)
    extracted.concat(memories)
  end

  extracted
end