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

#choose_filename(pattern) ⇒ String?

The choose_filename method selects a file from a list of matching files.

This method searches for files matching the given glob pattern, presents them in an interactive chooser menu, and returns the selected filename. If the user chooses to exit or no file is selected, the method returns nil.

Parameters:

  • pattern (String)

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

Returns:

  • (String, nil)

    the path to the selected file or nil if no file was chosen



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ollama_chat/input_content.rb', line 36

def choose_filename(pattern)
  files = Dir.glob(pattern).select { File.file?(it) }
  files.unshift('[EXIT]')
  case chosen = OllamaChat::Utils::Chooser.choose(files)
  when '[EXIT]', nil
    STDOUT.puts "Exiting chooser."
    return
  else
    chosen
  end
end

#context_spook(patterns) ⇒ String?

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ollama_chat/input_content.rb', line 75

def context_spook(patterns)
  if patterns
    ContextSpook::generate_context(verbose: false) do |context|
      context do
        Dir.glob(patterns).each do |filename|
          File.file?(filename) or next
          file filename
        end
      end
    end.to_json
  else
    if context_filename = choose_filename('.contexts/*.rb')
      ContextSpook.generate_context(context_filename, verbose: false).to_json
    end
  end
end

#input(pattern) ⇒ String?

The input method reads and returns the content of a selected file.

This method searches for files matching the given pattern and presents them in an interactive chooser menu. If a file is selected, its content is read and returned. If the user chooses to exit or no file is selected, the method returns nil.

Parameters:

  • pattern (String)

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

Returns:

  • (String, nil)

    the content of the selected file or nil if no file was chosen



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

def input(pattern)
  pattern ||= '**/*'
  if filename = choose_filename(pattern)
    File.read(filename)
  end
end