Class: OllamaChat::Tools::DirectoryStructure

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

Overview

A tool for retrieving directory structure and file hierarchy.

This tool allows the chat client to retrieve the directory structure and file hierarchy for a given path. It integrates with the Ollama tool calling system to provide detailed directory information to the language model.

The tool supports traversing directories and returns a structured representation of the file system hierarchy.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOllamaChat::Tools::DirectoryStructure

Initializes a new directory_structure tool instance.



15
16
17
# File 'lib/ollama_chat/tools/directory_structure.rb', line 15

def initialize
  @name = 'directory_structure'
end

Instance Attribute Details

#nameString (readonly)

Returns the name of the tool.

Returns:

  • (String)

    the name of the tool (‘directory_structure’)



22
23
24
# File 'lib/ollama_chat/tools/directory_structure.rb', line 22

def name
  @name
end

Instance Method Details

#execute(tool_call, **opts) ⇒ String

Executes the directory structure retrieval operation.

This method traverses the directory structure starting from the specified path and returns a structured representation of the file system hierarchy.

Parameters:

  • tool_call (Ollama::Tool::Call)

    the tool call object containing function details

  • opts (Hash)

    additional options

Returns:

  • (String)

    the directory structure as a JSON string

Raises:

  • (StandardError)

    if there’s an issue with directory traversal or JSON serialization



61
62
63
64
65
66
# File 'lib/ollama_chat/tools/directory_structure.rb', line 61

def execute(tool_call, **opts)
  path = Pathname.new(tool_call.function.arguments.path || '.')

  structure = generate_structure(path)
  structure.to_json
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



74
75
76
# File 'lib/ollama_chat/tools/directory_structure.rb', line 74

def to_hash
  tool.to_hash
end

#toolOllama::Tool

Creates and returns a tool definition for retrieving directory structure.

This method constructs the function signature that describes what the tool does, its parameters, and required fields. The tool accepts a path parameter for directory traversal.

Returns:

  • (Ollama::Tool)

    a tool definition for retrieving directory structure



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ollama_chat/tools/directory_structure.rb', line 31

def tool
  Tool.new(
    type: 'function',
    function: Tool::Function.new(
      name:,
      description: 'Retrieve the directory structure and file hierarchy for a given path',
      parameters: Tool::Function::Parameters.new(
        type: 'object',
        properties: {
          path: Tool::Function::Parameters::Property.new(
            type: 'string',
            description: 'Path to directory to list (defaults to current directory)'
          ),
        },
        required: []
      )
    )
  )
end