Module: Kward::ToolCall

Defined in:
lib/kward/tools/tool_call.rb

Constant Summary collapse

TOOL_NAME_MAP =
{
  "read_file" => "read",
  "edit_file" => "edit",
  "write_file" => "write",
  "run_shell_command" => "bash",
  "list_directory" => "list_directory",
  "web_search" => "web_search",
  "read_skill" => "read_skill",
  "ask_user_question" => "ask_user_question"
}.freeze

Class Method Summary collapse

Class Method Details

.arguments(tool_call) ⇒ Object



31
32
33
# File 'lib/kward/tools/tool_call.rb', line 31

def arguments(tool_call)
  parse_arguments(raw_arguments(tool_call))
end

.camelize_args(args) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/kward/tools/tool_call.rb', line 56

def camelize_args(args)
  return {} unless args.is_a?(Hash)

  args.each_with_object({}) do |(key, item), result|
    result[camelize_key(key)] = camelize_value(item)
  end
end

.camelize_key(key) ⇒ Object



83
84
85
# File 'lib/kward/tools/tool_call.rb', line 83

def camelize_key(key)
  key.to_s.gsub(/_([a-z])/) { Regexp.last_match(1).upcase }.to_sym
end

.camelize_value(item) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/kward/tools/tool_call.rb', line 72

def camelize_value(item)
  case item
  when Hash
    camelize_args(item)
  when Array
    item.map { |entry| camelize_value(entry) }
  else
    item
  end
end

.display_name(tool_call) ⇒ Object



26
27
28
29
# File 'lib/kward/tools/tool_call.rb', line 26

def display_name(tool_call)
  raw_name = name(tool_call)
  normalized_name(raw_name) || raw_name || "unknown_tool"
end

.function(tool_call) ⇒ Object



39
40
41
# File 'lib/kward/tools/tool_call.rb', line 39

def function(tool_call)
  value(tool_call, :function) || {}
end

.id(tool_call) ⇒ Object



18
19
20
# File 'lib/kward/tools/tool_call.rb', line 18

def id(tool_call)
  value(tool_call, :id)
end

.name(tool_call) ⇒ Object



22
23
24
# File 'lib/kward/tools/tool_call.rb', line 22

def name(tool_call)
  value(function(tool_call), :name)
end

.normalized_name(name) ⇒ Object



43
44
45
# File 'lib/kward/tools/tool_call.rb', line 43

def normalized_name(name)
  TOOL_NAME_MAP[name.to_s]
end

.parse_arguments(arguments) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/kward/tools/tool_call.rb', line 47

def parse_arguments(arguments)
  return {} if arguments.nil? || (arguments.respond_to?(:empty?) && arguments.empty?)
  return arguments if arguments.is_a?(Hash)

  JSON.parse(arguments.to_s)
rescue JSON::ParserError
  {}
end

.raw_arguments(tool_call) ⇒ Object



35
36
37
# File 'lib/kward/tools/tool_call.rb', line 35

def raw_arguments(tool_call)
  value(function(tool_call), :arguments)
end

.value(object, key) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/kward/tools/tool_call.rb', line 64

def value(object, key)
  return nil unless object.respond_to?(:key?)
  return object[key] if object.key?(key)
  return object[key.to_s] if object.key?(key.to_s)

  nil
end