-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_search.go
More file actions
executable file
·60 lines (52 loc) · 1.96 KB
/
memory_search.go
File metadata and controls
executable file
·60 lines (52 loc) · 1.96 KB
1
2
3
4
5
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
51
52
53
54
55
56
57
58
59
60
package main
import (
"errors"
"log"
"math"
"sort"
"strings"
)
// Search compares the input text against stored chunk content only (label is not
// used for similarity). Returns all matches with similarity > delta (sorted by similarity desc).
// If typeFilter is non-empty, only chunks with that type are considered.
// If scope is non-empty, only chunks matching that scope (or global) are considered.
// Hashtags in content (e.g. #decision) are indexed, so "search by #decision" works.
// For each matched chunk, it also returns suggested neighbors (IDs + labels).
func (s *MemoryStore) Search(text string, typeFilter string, scope string) ([]MemoryMatch, error) {
text = strings.TrimSpace(text)
if text == "" {
return nil, errors.New("search text is empty")
}
qVec, _ := vectorize(text)
qTokens := tokenSet(text)
s.mu.RLock()
defer s.mu.RUnlock()
matches := s.scoreChunksLocked(qVec, qTokens, s.similarityDelta, typeFilter, scope)
// Sort by rank score (content similarity * freshness decay) desc.
// Break ties by accessCount (frequently accessed = more likely important),
// then by ID for deterministic ordering.
sort.Slice(matches, func(i, j int) bool {
if math.Abs(matches[i].rankSim-matches[j].rankSim) < simEpsilon {
if matches[i].chunk.accessCount != matches[j].chunk.accessCount {
return matches[i].chunk.accessCount > matches[j].chunk.accessCount
}
return matches[i].chunk.ID < matches[j].chunk.ID
}
return matches[i].rankSim > matches[j].rankSim
})
// Limit before computing Neighbors (which iterates edges for each result).
limit := len(matches)
if s.maxResults > 0 && limit > s.maxResults {
limit = s.maxResults
}
out := make([]MemoryMatch, limit)
for i := 0; i < limit; i++ {
out[i] = MemoryMatch{
Chunk: matches[i].chunk.MemoryChunk,
Similarity: matches[i].sim,
Neighbors: s.neighborsLocked(matches[i].chunk, scope),
}
}
log.Printf("MEMORY: SEARCH '%s'", ExcerptForLog(text, logExcerptLen))
return out, nil
}