Module: OllamaChat::Tools::Concern

Extended by:
Tins::Concern
Includes:
Feedback, Utils::Backup, Utils::Redact, Utils::ValueFormatter
Included in:
RunTests
Defined in:
lib/ollama_chat/tools/concern.rb

Overview

A module that provides common functionality for OllamaChat tools.

This module serves as a base class for all tool implementations in the OllamaChat application, providing shared behavior and methods that tools can inherit from. It includes delegation to the tool name and registration functionality to integrate tools into the chat system's tool registry.

Constant Summary

Constants included from Feedback

Feedback::PREFIXES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Feedback

#feedback

Methods included from Utils::Backup

#perform_backup

Methods included from Utils::Redact

#redact_password, redact_password

Methods included from Utils::ValueFormatter

#format_bytes, #format_tokens

Instance Attribute Details

#chat ⇒ OllamaChat::Chat (readonly)

The chat attribute reader.

Returns:



70
71
72
# File 'lib/ollama_chat/tools/concern.rb', line 70

def chat
  @chat
end

Instance Method Details

#allowed_paths ⇒ Array<String> (private)

Returns the list of allowed filesystem paths for this tool, as configured in the tool's allowed setting.

Expands each configured path and filters out non-existent entries. Returns an empty array if the tool has no allowed paths configured.

Returns:

  • (Array<String>) —

    absolute paths the tool is permitted to operate on



117
118
119
120
121
122
# File 'lib/ollama_chat/tools/concern.rb', line 117

def allowed_paths
  tool_config&.allowed?.to_a.filter_map {
    pathname = Pathname.new(_1).expand_path
    pathname.exist?.full? { pathname.to_path }
  }
end

#initialize(chat) ⇒ Object

Initializes the tool instance with a reference to the chat session.

Parameters:



56
57
58
# File 'lib/ollama_chat/tools/concern.rb', line 56

def initialize(chat)
  @chat = chat
end

#name ⇒ String

The name method returns the registered name of the tool.

Returns:

  • (String) —

    the registered name of the tool instance



63
64
65
# File 'lib/ollama_chat/tools/concern.rb', line 63

def name
  self.class.register_name
end

#to_hash ⇒ Hash

The to_hash method converts the tool to a hash representation.

Returns:

  • (Hash) —

    a hash representation of the tool



75
76
77
# File 'lib/ollama_chat/tools/concern.rb', line 75

def to_hash
  tool.to_hash
end

#tool_config ⇒ ComplexConfig::Settings? (private)

Returns the configuration hash for this tool from the application config (config[:tools][:functions][<name>]).

Used to read tool-specific settings such as allowed: paths, require_confirmation:, result_display_timeout:, and (for execute_git) the commands: subcommand map.

Returns:

  • (ComplexConfig::Settings, nil) —

    the tool's config node, or nil if the tool has no entry in config[:tools][:functions]



106
107
108
# File 'lib/ollama_chat/tools/concern.rb', line 106

def tool_config
  chat.config.tools.functions[name]
end

#valid_json? ⇒ Proc (private)

The valid_json? method returns a proc that validates JSON data from a temporary file.

Returns:

  • (Proc) —

    a proc that takes a temporary file and returns its JSON content or raises an error



86
87
88
89
90
91
92
93
94
95
# File 'lib/ollama_chat/tools/concern.rb', line 86

def valid_json?
  -> tmp {
    if data = tmp.read.full?
      JSON.parse(data)
      return data
    else
      raise JSON::ParserError, 'require JSON data'
    end
  }
end