Class: OllamaChat::Tools::FileContext

Inherits:
Object
  • Object
show all
Includes:
Ollama
Defined in:
lib/ollama_chat/tools/file_context.rb

Overview

A tool for creating file context based on glob patterns.

This tool allows the chat client to generate context information about files matching a specified glob pattern. It integrates with the Ollama tool calling system to provide detailed file information to the language model for more accurate responses.

The tool searches for files using the provided glob pattern and generates structured context data that includes file contents, sizes, and metadata.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOllamaChat::Tools::FileContext

Initializes a new file_context tool instance.



16
17
18
# File 'lib/ollama_chat/tools/file_context.rb', line 16

def initialize
  @name = 'file_context'
end

Instance Attribute Details

#nameString (readonly)

Returns the name of the tool.

Returns:

  • (String)

    the name of the tool (‘file_context’)



23
24
25
# File 'lib/ollama_chat/tools/file_context.rb', line 23

def name
  @name
end

Instance Method Details

#execute(tool_call, **opts) ⇒ String

Executes the file context generation operation.

This method searches for files matching the provided glob pattern and generates structured context data including file contents, sizes, and metadata.

Parameters:

  • tool_call (Ollama::Tool::Call)

    the tool call object containing function details

  • opts (Hash)

    additional options

Options Hash (**opts):

  • :config (ComplexConfig::Settings)

    the configuration object

Returns:

  • (String)

    the formatted context data as a string in the configured format

Raises:

  • (StandardError)

    if there’s an issue with file searching or context generation



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ollama_chat/tools/file_context.rb', line 67

def execute(tool_call, **opts)
  config      = opts[:config]
  pattern     = tool_call.function.arguments.pattern
  directory   = Pathname.new(tool_call.function.arguments.directory || ?.)
  format      = config.context.format
  search_path = directory + pattern

  ContextSpook::generate_context(verbose: true, format:) do |context|
    context do
      Dir.glob(search_path).each do |filename|
        File.file?(filename) or next
        file filename
      end
    end
  end.send("to_#{format.downcase}")
end

#to_hashHash

Converts the tool to a hash representation.

This method provides a standardized way to serialize the tool definition for use in tool calling systems.

Returns:

  • (Hash)

    a hash representation of the tool



90
91
92
# File 'lib/ollama_chat/tools/file_context.rb', line 90

def to_hash
  tool.to_hash
end

#toolOllama::Tool

Creates and returns a tool definition for generating file context.

This method constructs the function signature that describes what the tool does, its parameters, and required fields. The tool expects a pattern parameter to be provided for file searching.

Returns:

  • (Ollama::Tool)

    a tool definition for generating file context



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ollama_chat/tools/file_context.rb', line 32

def tool
  Tool.new(
    type: 'function',
    function: Tool::Function.new(
      name:,
      description: 'Create a context that provides information about files '\
        'and their content in order to give more accurate answers for a query',
      parameters: Tool::Function::Parameters.new(
        type: 'object',
        properties: {
          pattern: Tool::Function::Parameters::Property.new(
            type: 'string',
            description: 'Glob pattern to search for (e.g., "**/*.rb", "lib/**/*.rb")'
          ),
          directory: Tool::Function::Parameters::Property.new(
            type: 'string',
            description: 'Directory to search in (defaults to current directory)'
          ),
        },
        required: ['pattern']
      )
    )
  )
end