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 up to a specified depth 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 up to the given depth 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



65
66
67
68
69
70
71
# File 'lib/ollama_chat/tools/directory_structure.rb', line 65

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

  structure = generate_structure(path, depth)
  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



79
80
81
# File 'lib/ollama_chat/tools/directory_structure.rb', line 79

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 path and depth parameters 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
50
51
52
53
# 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)'
          ),
          depth: Tool::Function::Parameters::Property.new(
            type: 'integer',
            description: 'Depth of directory traversal (defaults to 2)'
          )
        },
        required: []
      )
    )
  )
end