Class: RobotLab::ToolResultMessage

Inherits:
Message
  • Object
show all
Defined in:
lib/robot_lab/message.rb

Overview

Result from executing a tool

Examples:

Successful result

ToolResultMessage.new(
  tool: ToolMessage.new(id: "call_1", name: "get_weather", input: { location: "Berlin" }),
  content: { data: { temperature: 15, unit: "celsius" } }
)

Error result

ToolResultMessage.new(
  tool: ToolMessage.new(id: "call_1", name: "get_weather", input: {}),
  content: { error: "Location is required" }
)

Constant Summary

Constants inherited from Message

Message::VALID_ROLES, Message::VALID_STOP_REASONS, Message::VALID_TYPES

Instance Attribute Summary collapse

Attributes inherited from Message

#content, #role, #stop_reason, #type

Instance Method Summary collapse

Methods inherited from Message

#assistant?, from_hash, #stopped?, #system?, #text?, #to_json, #tool_call?, #tool_result?, #tool_stop?, #user?

Constructor Details

#initialize(tool:, content:, stop_reason: nil) ⇒ ToolResultMessage

Creates a new ToolResultMessage instance.

Parameters:

  • tool (ToolMessage, Hash)

    the tool call that was executed

  • content (Hash)

    the result content (with :data or :error key)

  • stop_reason (String, Symbol, nil) (defaults to: nil)

    the stop reason (defaults to “tool”)



298
299
300
301
# File 'lib/robot_lab/message.rb', line 298

def initialize(tool:, content:, stop_reason: nil)
  @tool = normalize_tool(tool)
  super(type: "tool_result", role: "tool_result", content: content, stop_reason: stop_reason || "tool")
end

Instance Attribute Details

#toolObject (readonly)

Returns the value of attribute tool.



291
292
293
# File 'lib/robot_lab/message.rb', line 291

def tool
  @tool
end

Instance Method Details

#dataObject?

Returns the result data if successful.

Returns:

  • (Object, nil)

    the result data, or nil if not successful



320
321
322
# File 'lib/robot_lab/message.rb', line 320

def data
  content[:data] if success?
end

#errorString?

Returns the error message if there was an error.

Returns:

  • (String, nil)

    the error message, or nil if no error



327
328
329
# File 'lib/robot_lab/message.rb', line 327

def error
  content[:error] if error?
end

#error?Boolean

Checks if the tool execution resulted in an error.

Returns:

  • (Boolean)

    true if content contains an :error key



313
314
315
# File 'lib/robot_lab/message.rb', line 313

def error?
  content.is_a?(Hash) && content.key?(:error)
end

#success?Boolean

Checks if the tool execution was successful.

Returns:

  • (Boolean)

    true if content contains a :data key



306
307
308
# File 'lib/robot_lab/message.rb', line 306

def success?
  content.is_a?(Hash) && content.key?(:data)
end

#to_hHash

Converts the tool result message to a hash representation.

Returns:

  • (Hash)

    a hash containing the tool result data



334
335
336
337
338
339
340
341
342
# File 'lib/robot_lab/message.rb', line 334

def to_h
  {
    type: type,
    role: role,
    tool: tool.to_h,
    content: content,
    stop_reason: stop_reason
  }
end