Class: Legion::LLM::Tools::Adapter

Inherits:
RubyLLM::Tool
  • Object
show all
Includes:
Legion::Logging::Helper
Defined in:
lib/legion/llm/tools/adapter.rb

Constant Summary collapse

MAX_TOOL_NAME_LENGTH =
64

Instance Method Summary collapse

Constructor Details

#initialize(tool_class) ⇒ Adapter

Returns a new instance of Adapter.



15
16
17
18
19
20
21
22
# File 'lib/legion/llm/tools/adapter.rb', line 15

def initialize(tool_class)
  @tool_class = tool_class
  raw_name = tool_class.respond_to?(:tool_name) ? tool_class.tool_name : tool_class.name.to_s
  @tool_name = sanitize_tool_name(raw_name)
  @tool_desc = tool_class.respond_to?(:description) ? tool_class.description.to_s : ''
  @tool_schema = tool_class.respond_to?(:input_schema) ? tool_class.input_schema : nil
  super()
end

Instance Method Details

#descriptionObject



28
29
30
# File 'lib/legion/llm/tools/adapter.rb', line 28

def description
  @tool_desc
end

#execute(**args) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/legion/llm/tools/adapter.rb', line 38

def execute(**args)
  args = Interceptor.intercept(@tool_name, **args)
  log.info("[llm][tools] adapter.execute name=#{@tool_name} arguments=#{summarize_payload(args)}")
  result = @tool_class.call(**args)
  content = extract_content(result)
  log.info("[llm][tools] adapter.result name=#{@tool_name} output=#{summarize_payload(content)}")
  content
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'llm.tools.adapter.execute', tool_name: @tool_name)
  "Tool error: #{e.message}"
end

#nameObject



24
25
26
# File 'lib/legion/llm/tools/adapter.rb', line 24

def name
  @tool_name
end

#params_schemaObject



32
33
34
35
36
# File 'lib/legion/llm/tools/adapter.rb', line 32

def params_schema
  return @params_schema if defined?(@params_schema)

  @params_schema = (RubyLLM::Utils.deep_stringify_keys(@tool_schema) if @tool_schema.is_a?(Hash))
end