Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/roast/cogs/agent/providers/pi/messages/tool_call_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ def format

private

# Formats a bash tool call.
#
# Input fields:
# :command (String) – shell command to execute [required]
#
# Output: "BASH <command>", with :command truncated to TRUNCATE_LIMIT
# chars. A missing command renders the bare "BASH".
#
# Examples:
# BASH ls -la
# BASH
#
#: () -> String
def format_bash
command = truncate(arguments[:command])
command.empty? ? "BASH" : "BASH #{command}"
end

# Formats a tool call for which Roast has no dedicated formatter.
#
# Output: "<NAME> <key>: <value>, ..." – the upcased tool name, then each
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ def format(context)

private

# Formats a bash tool result.
#
# Content: the command's output text.
#
# Output: "BASH OK <n> <line|lines> · <preview>" – <n> is the line count
# (pluralized) and <preview> is the first line, stripped and truncated to
# TRUNCATE_LIMIT chars. The preview is omitted when there is no output.
#
# Examples:
# BASH OK 12 lines · Cloning into 'roast'...
# BASH OK 1 line · hello world
# BASH OK 0 lines
#
#: () -> String
def format_bash
lines = content.to_s.lines
count = lines.length
preview = truncate(lines.first.to_s.strip)
ok_line("#{count} #{"line".pluralize(count)}", preview)
end

# Formats a result for which Roast has no dedicated formatter.
#
# Content: the tool's output text.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ class ToolCallMessageTest < ActiveSupport::TestCase
assert_nil msg.format
end

test "format renders BASH with the command" do
msg = ToolCallMessage.new(id: "1", name: "bash", arguments: { command: "ls -la" })
assert_equal "BASH ls -la", msg.format
end

test "format renders a bare BASH when the command is missing" do
msg = ToolCallMessage.new(id: "1", name: "bash", arguments: {})
assert_equal "BASH", msg.format
end

test "format truncates a long bash command" do
msg = ToolCallMessage.new(id: "1", name: "bash", arguments: { command: "x" * 100 })
assert_equal "BASH #{"x" * (ToolCallMessage::TRUNCATE_LIMIT - 3)}...", msg.format
end

test "format renders an unhandled tool as NAME key: value, ..." do
msg = ToolCallMessage.new(
id: "1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,39 @@ def setup
@context = Roast::Cogs::Agent::Providers::Pi::PiInvocation::Context.new
end

test "format summarizes bash output with a line count and preview" do
msg = ToolResultMessage.new(
tool_call_id: "1",
tool_name: "bash",
content: "file1.rb\nfile2.rb",
is_error: false,
)

assert_equal "BASH OK 2 lines · file1.rb", msg.format(@context)
end

test "format pluralizes a single line of bash output" do
msg = ToolResultMessage.new(
tool_call_id: "1",
tool_name: "bash",
content: "hello world",
is_error: false,
)

assert_equal "BASH OK 1 line · hello world", msg.format(@context)
end

test "format reports zero lines when bash produced no output" do
msg = ToolResultMessage.new(
tool_call_id: "1",
tool_name: "bash",
content: nil,
is_error: false,
)

assert_equal "BASH OK 0 lines", msg.format(@context)
end

test "format renders NAME ERROR with the message for an error result" do
msg = ToolResultMessage.new(
tool_call_id: "1",
Expand Down
Loading