Class: OllamaAgent::Tools::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama_agent/tools/base.rb

Overview

Base class for all typed, permissioned, auditable tools.

Subclasses must implement #call(args, context:) and define:

- name         String
- description  String
- input_schema Hash   (JSON schema for arguments)

Examples:

class MyTool < OllamaAgent::Tools::Base
  tool_name        "my_tool"
  tool_description "Does something useful"
  tool_risk        :low
  tool_schema      { type: "object", properties: { ... }, required: [...] }

  def call(args, context:)
    { result: args["input"].upcase }
  end
end

Constant Summary collapse

RISK_LEVELS =
%i[low medium high critical].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



63
64
65
66
67
68
69
70
# File 'lib/ollama_agent/tools/base.rb', line 63

def initialize
  @name               = self.class.tool_name
  @description        = self.class.tool_description
  @input_schema       = self.class.tool_schema
  @output_schema      = self.class.tool_output_schema
  @risk_level         = self.class.tool_risk
  @requires_approval  = self.class.tool_requires_approval
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



61
62
63
# File 'lib/ollama_agent/tools/base.rb', line 61

def description
  @description
end

#input_schemaObject (readonly)

Returns the value of attribute input_schema.



61
62
63
# File 'lib/ollama_agent/tools/base.rb', line 61

def input_schema
  @input_schema
end

#nameObject (readonly)

Returns the value of attribute name.



61
62
63
# File 'lib/ollama_agent/tools/base.rb', line 61

def name
  @name
end

#output_schemaObject (readonly)

Returns the value of attribute output_schema.



61
62
63
# File 'lib/ollama_agent/tools/base.rb', line 61

def output_schema
  @output_schema
end

#requires_approvalObject (readonly)

Returns the value of attribute requires_approval.



61
62
63
# File 'lib/ollama_agent/tools/base.rb', line 61

def requires_approval
  @requires_approval
end

#risk_levelObject (readonly)

Returns the value of attribute risk_level.



61
62
63
# File 'lib/ollama_agent/tools/base.rb', line 61

def risk_level
  @risk_level
end

Class Method Details

.tool_description(desc = nil) ⇒ Object



32
33
34
35
# File 'lib/ollama_agent/tools/base.rb', line 32

def tool_description(desc = nil)
  @tool_description = desc if desc
  @tool_description || ""
end

.tool_name(name = nil) ⇒ Object



27
28
29
30
# File 'lib/ollama_agent/tools/base.rb', line 27

def tool_name(name = nil)
  @tool_name = name.to_s if name
  @tool_name || raise(NotImplementedError, "#{self}.tool_name not set")
end

.tool_output_schema(schema = nil) ⇒ Object



55
56
57
58
# File 'lib/ollama_agent/tools/base.rb', line 55

def tool_output_schema(schema = nil)
  @tool_output_schema = schema if schema
  @tool_output_schema
end

.tool_requires_approval(flag = nil) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/ollama_agent/tools/base.rb', line 42

def tool_requires_approval(flag = nil)
  @tool_requires_approval = flag unless flag.nil?
  return @tool_requires_approval unless @tool_requires_approval.nil?

  risk = @tool_risk || :low
  %i[high critical].include?(risk)
end

.tool_risk(level = nil) ⇒ Object



37
38
39
40
# File 'lib/ollama_agent/tools/base.rb', line 37

def tool_risk(level = nil)
  @tool_risk = level.to_sym if level
  @tool_risk || :low
end

.tool_schema(schema = nil) ⇒ Object



50
51
52
53
# File 'lib/ollama_agent/tools/base.rb', line 50

def tool_schema(schema = nil)
  @tool_schema = schema if schema
  @tool_schema || { type: "object", properties: {}, required: [] }
end

Instance Method Details

#call(args, context: {}) ⇒ String, Hash

Execute the tool.

Parameters:

  • args (Hash)

    validated argument hash

  • context (Hash) (defaults to: {})

    runtime context: { root:, read_only:, run_id:, … }

Returns:

  • (String, Hash)

    result surfaced back to the model

Raises:

  • (NotImplementedError)


76
77
78
# File 'lib/ollama_agent/tools/base.rb', line 76

def call(args, context: {})
  raise NotImplementedError, "#{self.class}#call not implemented"
end

#to_anthropic_schemaObject

Anthropic-format tool definition



93
94
95
96
97
98
99
# File 'lib/ollama_agent/tools/base.rb', line 93

def to_anthropic_schema
  {
    name: @name,
    description: @description,
    input_schema: @input_schema
  }
end

#to_ollama_schemaObject

JSON schema formatted for Ollama / OpenAI tool_call



81
82
83
84
85
86
87
88
89
90
# File 'lib/ollama_agent/tools/base.rb', line 81

def to_ollama_schema
  {
    type: "function",
    function: {
      name: @name,
      description: @description,
      parameters: @input_schema
    }
  }
end

#to_sObject



101
102
103
# File 'lib/ollama_agent/tools/base.rb', line 101

def to_s
  "#<Tool:#{@name} risk=#{@risk_level} approval=#{@requires_approval}>"
end