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
25 changes: 25 additions & 0 deletions lib/roast/cogs/agent/providers/claude/tool_use.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,31 @@ def format_grep
modifiers.empty? ? base : "#{base} (#{modifiers})"
end

# Formats a Write tool-use line.
#
# Input fields:
# :file_path (String) – absolute path to write [required]
# :content (String) – full file contents [required]
#
# Output: 'WRITE <file_path> "<preview>" (+<n> lines)', where
# <preview> is the first line of :content (stripped, truncated to
# TRUNCATE_LIMIT chars) and <n> is the total number of lines written
# (singular " line" when <n> is 1). The count is always shown.
#
# Examples:
# WRITE /app/models/user.rb "class User < ApplicationRecord" (+10 lines)
# WRITE /config/app.yml "enabled: true" (+1 line)
#
#: () -> String
def format_write
file_path, content = input.values_at(:file_path, :content)
lines = content.to_s.lines
preview = truncate(lines.first.to_s.strip)
count = lines.length
line_label = count == 1 ? "line" : "lines"
"WRITE #{file_path} \"#{preview}\" (+#{count} #{line_label})"
end

#: () -> String
def format_unknown
"UNKNOWN [#{name}] #{input.inspect}"
Expand Down
28 changes: 28 additions & 0 deletions test/roast/cogs/agent/providers/claude/tool_use_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,34 @@ class ToolUseTest < ActiveSupport::TestCase
assert_equal "GREP \"#{truncated}\" #{long}", output
end

# format_write

test "format_write shows the preview and a singular count for one line" do
tool_use = ToolUse.new(name: :write, input: { file_path: "/a.rb", content: "hello" })

output = tool_use.format

assert_equal "WRITE /a.rb \"hello\" (+1 line)", output
end

test "format_write strips the preview and counts every line" do
tool_use = ToolUse.new(name: :write, input: { file_path: "/a.rb", content: " def foo\n bar\n baz" })

output = tool_use.format

assert_equal "WRITE /a.rb \"def foo\" (+3 lines)", output
end

test "format_write truncates the preview" do
long = "a" * (ToolUse::TRUNCATE_LIMIT + 10)
truncated = "#{"a" * (ToolUse::TRUNCATE_LIMIT - 3)}..."
tool_use = ToolUse.new(name: :write, input: { file_path: "/a.rb", content: long })

output = tool_use.format

assert_equal "WRITE /a.rb \"#{truncated}\" (+1 line)", output
end

test "format calls format_unknown for unknown tool" do
tool_use = ToolUse.new(name: :unknown_tool, input: { arg: "value" })

Expand Down
Loading