Module: OllamaAgent::RubyIndex::Formatter

Defined in:
lib/ollama_agent/ruby_index/formatter.rb

Overview

Turns index rows into capped plain-text lines for tool responses.

Constant Summary collapse

DEFAULT_MAX_LINES =
200
DEFAULT_MAX_CHARS =
60_000

Class Method Summary collapse

Class Method Details

.cap(lines, max_lines, max_chars) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ollama_agent/ruby_index/formatter.rb', line 51

def cap(lines, max_lines, max_chars)
  out = +""
  lines.first(max_lines).each do |line|
    break if out.bytesize + line.bytesize + 1 > max_chars

    out << line
    out << "\n"
  end
  truncated = lines.size > max_lines || out.bytesize >= max_chars
  out << "(truncated)\n" if truncated
  out
end

.env_int(name) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/ollama_agent/ruby_index/formatter.rb', line 30

def env_int(name)
  v = ENV.fetch(name, nil)
  return nil if v.nil? || v.empty?

  Integer(v)
rescue ArgumentError
  nil
end

.format_constant_row(row) ⇒ Object



39
40
41
42
# File 'lib/ollama_agent/ruby_index/formatter.rb', line 39

def format_constant_row(row)
  kind = row[:kind]
  "#{kind} #{row[:name]}  #{row[:path]}:#{row[:start_line]}-#{row[:end_line]}"
end

.format_constants(records, max_lines: nil, max_chars: nil) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/ollama_agent/ruby_index/formatter.rb', line 12

def format_constants(records, max_lines: nil, max_chars: nil)
  return "(no matches)\n" if records.empty?

  max_lines = (max_lines || env_int("OLLAMA_AGENT_RUBY_INDEX_MAX_LINES") || DEFAULT_MAX_LINES).to_i
  max_chars = (max_chars || env_int("OLLAMA_AGENT_RUBY_INDEX_MAX_CHARS") || DEFAULT_MAX_CHARS).to_i
  lines = records.map { |r| format_constant_row(r) }
  cap(lines, max_lines, max_chars)
end

.format_method_row(row) ⇒ Object



44
45
46
47
48
49
# File 'lib/ollama_agent/ruby_index/formatter.rb', line 44

def format_method_row(row)
  sig = row[:singleton] ? "singleton" : "instance"
  ns = row[:namespace].to_s
  ns_part = ns.empty? ? "(toplevel)" : ns
  "method #{row[:name]}  #{sig}  #{ns_part}  #{row[:path]}:#{row[:start_line]}-#{row[:end_line]}"
end

.format_methods(records, max_lines: nil, max_chars: nil) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/ollama_agent/ruby_index/formatter.rb', line 21

def format_methods(records, max_lines: nil, max_chars: nil)
  return "(no matches)\n" if records.empty?

  max_lines = (max_lines || env_int("OLLAMA_AGENT_RUBY_INDEX_MAX_LINES") || DEFAULT_MAX_LINES).to_i
  max_chars = (max_chars || env_int("OLLAMA_AGENT_RUBY_INDEX_MAX_CHARS") || DEFAULT_MAX_CHARS).to_i
  lines = records.map { |r| format_method_row(r) }
  cap(lines, max_lines, max_chars)
end