Class: Helicone::ToolCall

Inherits:
Object
  • Object
show all
Defined in:
lib/helicone/tool_call.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, name:, arguments:) ⇒ ToolCall

Initialize a tool call

Parameters:

  • id (String) —

    The unique ID for this tool call

  • name (String) —

    The name of the function to call

  • arguments (String, Hash, nil) —

    Arguments (JSON string or Hash)



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/helicone/tool_call.rb', line 12

def initialize(id:, name:, arguments:)
  @id = id
  @name = name
  @arguments = if arguments.nil?
    {}
  elsif arguments.is_a?(String)
    deep_symbolize_keys(JSON.parse(arguments))
  else
    deep_symbolize_keys(arguments)
  end
end

Instance Attribute Details

#arguments ⇒ Object (readonly)

Returns the value of attribute arguments.



5
6
7
# File 'lib/helicone/tool_call.rb', line 5

def arguments
  @arguments
end

#id ⇒ Object (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/helicone/tool_call.rb', line 5

def id
  @id
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/helicone/tool_call.rb', line 5

def name
  @name
end

Class Method Details

.from_response(response) ⇒ Array<Helicone::ToolCall>

Parse tool calls from an API response (expects symbolized keys)

Parameters:

Returns:



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/helicone/tool_call.rb', line 28

def self.from_response(response)
  tool_calls = response.is_a?(Response) ? response.tool_calls : response
  return [] if tool_calls.nil?

  tool_calls.map do |tc|
    new(
      id: tc[:id],
      name: tc.dig(:function, :name),
      arguments: tc.dig(:function, :arguments)
    )
  end
end

.result(tool_call_id:, content:) ⇒ Helicone::Message

Build a tool result message to send back to the API

Parameters:

  • tool_call_id (String) —

    The ID of the tool call being responded to

  • content (String, Hash) —

    The tool result

Returns:



46
47
48
# File 'lib/helicone/tool_call.rb', line 46

def self.result(tool_call_id:, content:)
  Message.tool_result(tool_call_id: tool_call_id, content: content)
end

Instance Method Details

#[](key) ⇒ Object?

Access arguments like a hash (supports both string and symbol keys)

Parameters:

  • key (String, Symbol) —

    Key to access

Returns:

  • (Object, nil)


65
66
67
# File 'lib/helicone/tool_call.rb', line 65

def [](key)
  arguments[key.to_sym]
end

#dig(*keys) ⇒ Object?

Dig into nested data in arguments

Parameters:

  • keys (Array<String, Symbol>) —

    Keys to dig through

Returns:

  • (Object, nil)


73
74
75
# File 'lib/helicone/tool_call.rb', line 73

def dig(*keys)
  arguments.dig(*keys.map(&:to_sym))
end

#to_h ⇒ Hash

Convert to hash for inspection

Returns:

  • (Hash)


53
54
55
56
57
58
59
# File 'lib/helicone/tool_call.rb', line 53

def to_h
  {
    id: id,
    name: name,
    arguments: arguments
  }
end