Module: OllamaChat::InputContent

Included in:
Chat
Defined in:
lib/ollama_chat/input_content.rb

Overview

A module that provides input content processing functionality for OllamaChat.

The InputContent module encapsulates methods for reading and returning content from selected files, selecting files from a list of matching files, and collecting project context using the context_spook library. It supports interactive file selection and context collection for enhancing chat interactions with local or remote content.

Instance Method Summary collapse

Instance Method Details

#all_file_set(patterns) ⇒ Set<Pathname> (private)

The all_file_set method aggregates files matching the provided glob patterns into a set.

Parameters:

  • patterns (Array<String>)

    an array of glob patterns to match files against

Returns:

  • (Set<Pathname>)

    a set containing all Pathname objects that match any of the given patterns



142
143
144
145
146
147
148
# File 'lib/ollama_chat/input_content.rb', line 142

def all_file_set(patterns)
  files = Set[]
  patterns.each do |pattern|
    files.merge(Pathname.glob(pattern))
  end
  files.map(&:expand_path)
end

#choose_filename(patterns, chosen: nil) ⇒ Pathname? (private)

The choose_filename method selects a file from a list of matching files. It searches for files matching the given pattern, excludes already chosen files, and presents them in an interactive chooser menu.

Parameters:

  • patterns (Array<String>)

    the glob patterns to search for files

  • chosen (Set) (defaults to: nil)

    a set of already chosen filenames to exclude from selection

Returns:

  • (Pathname, nil)

    the selected filename or nil if no file was chosen or user exited



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ollama_chat/input_content.rb', line 53

def choose_filename(patterns, chosen: nil)
  patterns = Array(patterns)
  files = patterns.flat_map { Pathname.glob(_1) }
  files = files.reject { chosen&.member?(_1.expand_path) }.select { _1.file? }
  files.unshift('[EXIT]')
  case chosen_file = OllamaChat::Utils::Chooser.choose(files)
  when '[EXIT]', nil
    STDOUT.puts "Exiting chooser."
    return
  else
    Pathname.new(chosen_file)
  end
end

#composeString? (private)

The compose method opens an editor to compose content.

This method checks for a configured editor and opens a temporary file in that editor for the user to compose content. Upon successful editing, it reads the content from the temporary file and returns it. If the editor fails or no editor is configured, appropriate error messages are displayed and nil is returned.

Returns:

  • (String, nil)

    the composed content if successful, nil otherwise



123
124
125
126
127
128
129
130
131
132
# File 'lib/ollama_chat/input_content.rb', line 123

def compose
  Tempfile.open do |tmp|
    if result = edit_file(tmp.path)
      return File.read(tmp.path)
    else
      STDERR.puts "Editor failed to edit #{tmp.path.inspect}."
    end
  end
  nil
end

#context_spook(patterns, all: false) ⇒ String? (private)

The context_spook method collects and returns project context using the context_spook library.

This method generates structured project context that can be used to provide AI models with comprehensive information about the codebase. It supports both:

  • On-the-fly pattern matching for specific file patterns

  • Loading context from predefined definition files in ./.contexts/

When patterns are provided, it collects files matching the glob patterns and generates context data including file contents, sizes, and metadata. When no patterns are provided, it loads the default context definition file.

Examples:

Collect context for Ruby files only

context_spook(['lib/**/*.rb'])

Collect context for multiple patterns

context_spook(['lib/**/*.rb', 'spec/**/*.rb'])

Load default context

context_spook(nil)

Parameters:

  • patterns (Array<String>, nil)

    Optional array of glob patterns to filter files

Returns:

  • (String, nil)

    JSON string of context data or nil if no context could be generated



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ollama_chat/input_content.rb', line 94

def context_spook(patterns, all: false)
  format = config.context.format
  myself = self
  if patterns
    ContextSpook::generate_context(verbose: true, format:) do |context|
      context do
        myself.file_set_each(patterns, all:) do |filename|
          filename.file? or next
          file filename.to_path
        end
      end
    end.to_json
  else
    if context_filename = choose_filename('.contexts/*.rb')
      ContextSpook.generate_context(context_filename, verbose: true, format:).
        send("to_#{format.downcase}")
    end
  end
end

#file_set_each(patterns, all: false) {|file| ... } ⇒ Array<File>

The file_set_each method iterates over a set of files matched by the given patterns, optionally including all files, and yields each file to the supplied block. It returns the array of files that were processed.

Parameters:

  • patterns (Array<String>)

    the list of patterns to match files against

  • all (TrueClass, FalseClass) (defaults to: false)

    whether to include all files in the set

Yields:

  • (file)

    yields each file to the supplied block

Returns:

  • (Array<File>)

    the array of files that were processed



20
21
22
23
24
# File 'lib/ollama_chat/input_content.rb', line 20

def file_set_each(patterns, all: false, &block)
  files = all ? all_file_set(patterns) : choose_file_set(patterns)
  block and files.each(&block)
  files
end

#input(patterns) ⇒ String (private)

The input method selects and reads content from files matching a pattern.

This method prompts the user to select files matching the given glob pattern, reads their content, and returns a concatenated string with each file’s content preceded by its filename.

Parameters:

  • patterns (Array<String>)

    the glob patterns to search for files (defaults to ‘**/*’)

Returns:

  • (String)

    a concatenated string of file contents with filenames as headers



37
38
39
40
41
42
# File 'lib/ollama_chat/input_content.rb', line 37

def input(patterns)
  files = choose_file_set(patterns)
  files.each_with_object('') do |filename, result|
    result << ("%s:\n\n%s\n\n" % [ filename, filename.read ])
  end.full?
end

#provide_file_set_content(patterns, all: false) {|filename| ... } ⇒ String (private)

The provide_file_set_content method collects content from a set of files that match the supplied patterns, optionally processing all files or allowing the caller to choose a subset interactively. It concatenates each file’s name with the content returned by the provided block, separating each entry with newlines, resulting in a single string containing the processed content for all selected files.

Parameters:

  • patterns (Array<String>)

    # the glob patterns used to find files

  • all (TrueClass, FalseClass) (defaults to: false)

    whether to process all matching files or let the caller choose a subset

Yields:

  • (filename)

    the block that processes each filename

Returns:

  • (String)

    the concatenated result of the block applied to each file



163
164
165
166
167
# File 'lib/ollama_chat/input_content.rb', line 163

def provide_file_set_content(patterns, all: false, &block)
  file_set_each(patterns, all:).each_with_object('') do |filename, result|
    result << ("%s:\n\n%s\n\n" % [ filename, block.(filename) ])
  end.full?
end