Class: Riffer::Messages::Tool

Inherits:
Base
  • Object
show all
Defined in:
lib/riffer/messages/tool.rb

Overview

Represents a tool execution result in a conversation.

Instance Attribute Summary collapse

Attributes inherited from Base

#content, #id

Instance Method Summary collapse

Methods inherited from Base

#+, from_hash, #has_tool_calls?

Constructor Details

#initialize(content, tool_call_id:, name:, id: nil, error: nil, error_type: nil) ⇒ Tool

– : (String, tool_call_id: String, name: String, ?id: String?, ?error: String?, ?error_type: Symbol?) -> void



20
21
22
23
24
25
26
# File 'lib/riffer/messages/tool.rb', line 20

def initialize(content, tool_call_id:, name:, id: nil, error: nil, error_type: nil)
  super(content, id: id)
  @tool_call_id = tool_call_id
  @name = name
  @error = error
  @error_type = error_type
end

Instance Attribute Details

#errorObject (readonly)

The error message if the tool execution failed.



13
14
15
# File 'lib/riffer/messages/tool.rb', line 13

def error
  @error
end

#error_typeObject (readonly)

The type of error (:unknown_tool, :validation_error, :execution_error, :timeout_error).



16
17
18
# File 'lib/riffer/messages/tool.rb', line 16

def error_type
  @error_type
end

#nameObject (readonly)

The name of the tool that was called.



10
11
12
# File 'lib/riffer/messages/tool.rb', line 10

def name
  @name
end

#tool_call_idObject (readonly)

The ID of the tool call this result responds to.



7
8
9
# File 'lib/riffer/messages/tool.rb', line 7

def tool_call_id
  @tool_call_id
end

Instance Method Details

#error?Boolean

Returns true if the tool execution resulted in an error.

– : () -> bool

Returns:

  • (Boolean)


32
33
34
# File 'lib/riffer/messages/tool.rb', line 32

def error?
  !@error.nil?
end

#roleObject

– : () -> Symbol



38
39
40
# File 'lib/riffer/messages/tool.rb', line 38

def role
  :tool
end

#to_hObject

Converts the message to a hash.

– : () -> Hash[Symbol, untyped]



46
47
48
49
50
51
52
53
54
# File 'lib/riffer/messages/tool.rb', line 46

def to_h
  hash = {role: role, content: content, tool_call_id: tool_call_id, name: name} #: Hash[Symbol, untyped]
  hash[:id] = id unless id.nil?
  if error?
    hash[:error] = error
    hash[:error_type] = error_type
  end
  hash
end