Class: OllamaAgent::Indexing::FileIndexer

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama_agent/indexing/file_indexer.rb

Overview

Builds a searchable in-memory index of files in the repository. Extracts: file paths, function/class names (Ruby), and word tokens. Used by ContextPacker to score files for relevance to a query.

Defined Under Namespace

Classes: IndexEntry

Constant Summary collapse

MAX_FILE_BYTES =

500 KB — skip larger files for indexing

512_000

Instance Method Summary collapse

Constructor Details

#initialize(root:, scanner: nil) ⇒ FileIndexer

Returns a new instance of FileIndexer.



15
16
17
18
19
# File 'lib/ollama_agent/indexing/file_indexer.rb', line 15

def initialize(root:, scanner: nil)
  @root    = File.expand_path(root)
  @scanner = scanner || RepoScanner.new(root: @root)
  @index   = nil
end

Instance Method Details

#build(force: false) ⇒ Array<IndexEntry>

Build or return the cached index.

Parameters:

  • force (Boolean) (defaults to: false)

    rebuild even if cached

Returns:



24
25
26
27
28
# File 'lib/ollama_agent/indexing/file_indexer.rb', line 24

def build(force: false)
  return @index if @index && !force

  @index = @scanner.scan.filter_map { |entry| index_file(entry) }
end

#indexed_countObject



58
59
60
# File 'lib/ollama_agent/indexing/file_indexer.rb', line 58

def indexed_count
  build.size
end

#refresh!Object

Invalidate and rebuild the index.



54
55
56
# File 'lib/ollama_agent/indexing/file_indexer.rb', line 54

def refresh!
  build(force: true)
end

#search(query, top_n: 20, languages: nil) ⇒ Object

Search index for entries relevant to a query string. Returns entries sorted by score (highest first).

Parameters:

  • query (String)
  • top_n (Integer) (defaults to: 20)
  • languages (Array<Symbol>, nil) (defaults to: nil)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ollama_agent/indexing/file_indexer.rb', line 35

def search(query, top_n: 20, languages: nil)
  idx = build

  keywords = tokenize(query).uniq
  return idx.first(top_n) if keywords.empty?

  scored = idx.map do |entry|
    next if languages && !languages.include?(entry.language)

    score = score_entry(entry, keywords)
    [entry, score]
  end.compact

  scored.sort_by { |_, s| -s }
        .first(top_n)
        .map(&:first)
end