Class: OllamaAgent::Indexing::ContextPacker

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

Overview

Builds a compact, query-relevant context block from the repository. Used to inject surgical context into the system prompt instead of reading entire files blindly.

Examples:

packer = OllamaAgent::Indexing::ContextPacker.new(root: Dir.pwd)
context = packer.pack(query: "fix the authentication module")
# => "# lib/auth/session.rb\n```ruby\n...\n```\n\n# lib/auth/token.rb\n..."

Constant Summary collapse

DEFAULT_MAX_FILES =
15
DEFAULT_MAX_FILE_BYTES =

8 KB per file in context

8_192
DEFAULT_MAX_TOTAL_BYTES =

64 KB total context

65_536

Instance Method Summary collapse

Constructor Details

#initialize(root:, max_files: DEFAULT_MAX_FILES, max_file_bytes: DEFAULT_MAX_FILE_BYTES, max_total_bytes: DEFAULT_MAX_TOTAL_BYTES, indexer: nil) ⇒ ContextPacker

Returns a new instance of ContextPacker.



21
22
23
24
25
26
27
28
29
30
# File 'lib/ollama_agent/indexing/context_packer.rb', line 21

def initialize(root:, max_files: DEFAULT_MAX_FILES,
               max_file_bytes: DEFAULT_MAX_FILE_BYTES,
               max_total_bytes: DEFAULT_MAX_TOTAL_BYTES,
               indexer: nil)
  @root            = File.expand_path(root)
  @max_files       = max_files
  @max_file_bytes  = max_file_bytes
  @max_total_bytes = max_total_bytes
  @indexer         = indexer || FileIndexer.new(root: @root)
end

Instance Method Details

#pack(query: nil, files: nil, languages: nil) ⇒ String

Pack the most relevant files for a query.

Parameters:

  • query (String) (defaults to: nil)

    natural-language query for scoring

  • files (Array, nil) (defaults to: nil)

    explicit relative paths (bypasses scoring)

  • languages (Array, nil) (defaults to: nil)

    filter to specific languages

Returns:

  • (String)

    formatted context block (Markdown fenced code)



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ollama_agent/indexing/context_packer.rb', line 37

def pack(query: nil, files: nil, languages: nil)
  candidates = if files
                 files.map { |f| File.expand_path(f, @root) }
               elsif query
                 relevant_paths(query, languages: languages)
               else
                 recently_modified_paths
               end

  build_context(candidates)
end

#repo_summaryObject

Return a repo-summary block (structure, file counts, recent changes).



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ollama_agent/indexing/context_packer.rb', line 50

def repo_summary
  scanner = RepoScanner.new(root: @root)
  stats   = scanner.stats

  lines = ["## Repository Summary", "Root: #{@root}", ""]
  lines << "### File counts by language"
  stats[:languages]
    .sort_by { |_, v| -v[:files] }
    .first(10)
    .each { |lang, info| lines << "- #{lang}: #{info[:files]} files (#{human_bytes(info[:bytes])})" }

  lines << ""
  lines << "### Recently modified (top 10)"
  scanner.recently_modified(n: 10).each do |f|
    lines << "- #{f.relative_path} (#{human_bytes(f.size)})"
  end

  lines.join("\n")
end