Module: Kward::ToolCall

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

Overview

Reads and normalizes model tool-call hashes.

Tool calls arrive from several providers and may be restored from session files. This module keeps provider/string/symbol compatibility in one place and exposes small helpers used by the agent loop, tool registry, transcript formatters, and RPC event normalizers.

Constant Summary collapse

TOOL_NAME_MAP =
{
  "read_file" => "read",
  "edit_file" => "edit",
  "write_file" => "write",
  "run_shell_command" => "bash",
  "list_directory" => "list_directory",
  "code_search" => "code_search",
  "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) ⇒ Hash

Parses the requested tool arguments.

Returns:

  • (Hash)

    decoded argument object, or an empty hash for invalid JSON



48
49
50
# File 'lib/kward/tools/tool_call.rb', line 48

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

.camelize_args(args) ⇒ Hash

Recursively converts snake_case hash keys to camelCase symbols.

Returns:

  • (Hash)

    camelized copy of args



80
81
82
83
84
85
86
# File 'lib/kward/tools/tool_call.rb', line 80

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



103
104
105
# File 'lib/kward/tools/tool_call.rb', line 103

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

.camelize_value(item) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/kward/tools/tool_call.rb', line 92

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) ⇒ String

Returns the short name used in compact UI labels.

Returns:

  • (String)

    display label such as read, edit, or bash



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

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

.function(tool_call) ⇒ Object



56
57
58
# File 'lib/kward/tools/tool_call.rb', line 56

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

.id(tool_call) ⇒ String?

Returns provider tool-call id.

Returns:

  • (String, nil)

    provider tool-call id



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

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

.name(tool_call) ⇒ String?

Returns requested tool/function name.

Returns:

  • (String, nil)

    requested tool/function name



33
34
35
# File 'lib/kward/tools/tool_call.rb', line 33

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

.normalized_name(name) ⇒ Object



60
61
62
# File 'lib/kward/tools/tool_call.rb', line 60

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

.parse_arguments(arguments) ⇒ Object

Converts provider argument payloads into hashes.

Providers normally send JSON strings, while tests and compatibility callers may pass hashes directly.



68
69
70
71
72
73
74
75
# File 'lib/kward/tools/tool_call.rb', line 68

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



52
53
54
# File 'lib/kward/tools/tool_call.rb', line 52

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

.value(object, key) ⇒ Object



88
89
90
# File 'lib/kward/tools/tool_call.rb', line 88

def value(object, key)
  MessageAccess.value(object, key)
end