6
7
8
9
10
11
12
13
14
15
16
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
45
46
47
48
49
50
|
# File 'lib/smart_brain/retrievers/exact_retriever.rb', line 6
def retrieve(query:, memory_items:, recent_turns:, limit:)
terms = tokenize(query)
memory_hits = memory_items.filter_map do |item|
haystack = "#{item[:key]} #{item[:value_json]}".downcase
score = overlap_score(terms, haystack)
next if score <= 0
{
id: item[:id],
source: 'memory',
source_uri: "smartbrain://memory/#{item[:id]}",
title: item[:key],
snippet: item[:value_json].to_s,
mode: 'exact',
score: score + (item[:confidence] || 0.5),
tier: item[:tier] || 'evidence',
memory_type: item[:type],
memory_key: item[:key],
scope: item[:scope],
scope_id: item[:scope_id],
source_session_id: item[:source_session_id] || item[:session_id],
ref: { memory_item_id: item[:id] }
}
end
turn_hits = recent_turns.filter_map.with_index do |turn, idx|
haystack = turn[:content].to_s.downcase
score = overlap_score(terms, haystack)
next if score <= 0
{
id: "turn-#{idx}",
source: 'memory',
source_uri: 'smartbrain://recent_turn',
title: 'Recent Turn',
snippet: turn[:content].to_s,
mode: 'exact',
score: score,
ref: { turn_id: turn[:turn_id], message_id: turn[:message_id] }
}
end
(memory_hits + turn_hits).sort_by { |h| -h[:score] }.first(limit)
end
|