ToolResultMessage¶
Result from tool execution.
Class: RobotLab::ToolResultMessage¶
tool = ToolMessage.new(id: "call_abc123", name: "get_weather", input: { city: "NYC" })
message = ToolResultMessage.new(
tool: tool,
content: { data: { temperature: 72, conditions: "sunny" } }
)
Constructor¶
Parameters:
| Name | Type | Description |
|---|---|---|
tool |
ToolMessage |
The tool call that was executed |
content |
Hash |
Result with :data key (success) or :error key (failure) |
stop_reason |
String, nil |
Stop reason (defaults to "tool") |
Attributes¶
tool¶
The ToolMessage representing the tool call that produced this result. Provides access to tool.id, tool.name, and tool.input.
content¶
The result content. Contains either a :data key (success) or an :error key (failure).
role¶
Always returns "tool_result".
type¶
Always returns "tool_result".
stop_reason¶
Defaults to "tool".
Methods¶
success?¶
Returns true if the content contains a :data key.
error?¶
Returns true if the content contains an :error key.
data¶
Returns the result data if successful, nil otherwise.
error¶
Returns the error message if there was an error, nil otherwise.
to_h¶
Hash representation.
Returns:
{
type: "tool_result",
role: "tool_result",
tool: { type: "tool", id: "call_abc123", name: "get_weather", input: { city: "NYC" } },
content: { data: { temperature: 72, conditions: "sunny" } },
stop_reason: "tool"
}
to_json¶
JSON representation.
Predicates¶
message.tool_result? # => true
message.tool_call? # => false
message.text? # => false
message.tool_stop? # => true
Examples¶
Successful Result¶
tool = ToolMessage.new(id: "call_1", name: "search_orders", input: { user_id: "123" })
result = ToolResultMessage.new(
tool: tool,
content: { data: { order_id: "ord_123", status: "shipped" } }
)
result.success? # => true
result.data # => { order_id: "ord_123", status: "shipped" }
Error Result¶
tool = ToolMessage.new(id: "call_order", name: "get_order", input: { id: "bad" })
result = ToolResultMessage.new(
tool: tool,
content: { error: "Order not found" }
)
result.error? # => true
result.error # => "Order not found"
result.data # => nil
Accessing Tool Information¶
result = ToolResultMessage.new(
tool: ToolMessage.new(id: "call_1", name: "get_weather", input: { city: "Berlin" }),
content: { data: { temperature: 15, unit: "celsius" } }
)
result.tool.name # => "get_weather"
result.tool.id # => "call_1"
result.tool.input # => { city: "Berlin" }
result.data # => { temperature: 15, unit: "celsius" }
Matching Tool Calls with Results¶
# Given a ToolCallMessage and its results
tool_call_msg.tools.each do |tool|
# Find the matching result
matching_result = results.find { |r| r.tool.id == tool.id }
if matching_result&.success?
puts "#{tool.name}(#{tool.input}) => #{matching_result.data}"
elsif matching_result&.error?
puts "#{tool.name} failed: #{matching_result.error}"
end
end
In Memory History¶
# Find all tool results from memory
tool_results = memory.messages.select(&:tool_result?)
tool_results.each do |tr|
if tr.success?
puts "#{tr.tool.name}: #{tr.data}"
else
puts "#{tr.tool.name} error: #{tr.error}"
end
end