-
Notifications
You must be signed in to change notification settings - Fork 70
restructure Pi tool formatting into per-method dispatch with generic fallback #1012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,24 +32,44 @@ def initialize(id:, name:, arguments:) | |
| def format | ||
| return unless name | ||
|
|
||
| case name.to_s.downcase | ||
| when "bash" | ||
| "BASH #{arguments[:command]}" | ||
| when "read" | ||
| "READ #{arguments[:path]}" | ||
| when "edit" | ||
| "EDIT #{arguments[:path]}" | ||
| when "write" | ||
| "WRITE #{arguments[:path]}" | ||
| when "grep" | ||
| "GREP #{arguments[:pattern]} #{arguments[:path]}" | ||
| when "find" | ||
| "FIND #{arguments[:pattern]} #{arguments[:path]}" | ||
| when "ls" | ||
| "LS #{arguments[:path]}" | ||
| else | ||
| "TOOL [#{name}] #{arguments.inspect}" | ||
| end | ||
| format_method_name = "format_#{name.to_s.downcase}".to_sym | ||
| return send(format_method_name) if respond_to?(format_method_name, true) | ||
|
|
||
| format_unknown | ||
| end | ||
|
|
||
| # Truncate long formatted tool-call strings to keep terminal output to generally one line, accounting for logger prefixing. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line-length contract vs. what the code enforces. This comment (and its result-side twin) says the goal is to "keep terminal output to generally one line," but |
||
| TRUNCATE_LIMIT = 45 | ||
|
|
||
| private | ||
|
|
||
| # Formats a tool call for which Roast has no dedicated formatter. | ||
| # | ||
| # Output: "<NAME> <key>: <value>, ..." – the upcased tool name, then each | ||
| # argument as "<key>: <inspected value>", ordered shortest pair first so the | ||
| # most compact arguments stay visible, joined with ", ". Every value is | ||
| # truncated to TRUNCATE_LIMIT chars so one large argument can't flood the | ||
| # line; keys are always shown. No arguments renders the bare "<NAME>". | ||
| # | ||
| # Examples: | ||
| # WEB_SEARCH max_results: 5, query: "ruby pluralize" | ||
| # DEPLOY | ||
| # | ||
| #: () -> String | ||
| def format_unknown | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Robustness: dispatch trusts |
||
| label = name.to_s.upcase | ||
| return label if arguments.empty? | ||
|
|
||
| details = arguments.map { |key, value| "#{key}: #{truncate(value.inspect)}" }.sort_by(&:length).join(", ") | ||
| "#{label} #{details}" | ||
| end | ||
|
|
||
| # Truncates to TRUNCATE_LIMIT chars, appending "..." when cut. nil -> "". | ||
| # | ||
| #: (String?) -> String | ||
| def truncate(str) | ||
| s = str.to_s | ||
| s.length > TRUNCATE_LIMIT ? "#{s[0...TRUNCATE_LIMIT - 3]}..." : s | ||
| end | ||
| end | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,23 +30,75 @@ def initialize(tool_call_id:, tool_name:, content:, is_error:) | |
| @tool_name = tool_name | ||
| @content = content | ||
| @is_error = is_error | ||
| @name = (tool_name || "unknown").to_s #: String | ||
| @input = {} #: Hash[Symbol, untyped] | ||
| end | ||
|
|
||
| #: (PiInvocation::Context) -> String? | ||
| def format(context) | ||
| tool_call = context.tool_call(tool_call_id) | ||
| name = tool_name || tool_call&.name || "unknown" | ||
| status = is_error ? "ERROR" : "OK" | ||
|
|
||
| # Truncate long tool results for progress display | ||
| c = content | ||
| display_content = if c && c.length > 200 | ||
| "#{c[0..197]}..." | ||
| else | ||
| c | ||
| end | ||
|
|
||
| "#{name.upcase} #{status}#{display_content ? " #{display_content}" : ""}" | ||
| call = context.tool_call(tool_call_id) | ||
| @name = (tool_name || call&.name || "unknown").to_s | ||
| @input = call&.arguments || {} | ||
|
|
||
| return error_line if is_error | ||
|
|
||
| format_method_name = "format_#{@name.downcase}".to_sym | ||
| return send(format_method_name) if respond_to?(format_method_name, true) | ||
|
|
||
| format_unknown | ||
| end | ||
|
|
||
| # Truncate long formatted tool-result strings to keep terminal output to generally one line, accounting for logger prefixing. | ||
| TRUNCATE_LIMIT = 45 | ||
|
|
||
| private | ||
|
|
||
| # Formats a result for which Roast has no dedicated formatter. | ||
| # | ||
| # Content: the tool's output text. | ||
| # | ||
| # Output: "<NAME> OK <preview>" – the first line of content, stripped and | ||
| # truncated to TRUNCATE_LIMIT chars. The preview is omitted when there is | ||
| # no content. | ||
| # | ||
| # Examples: | ||
| # WEB_SEARCH OK 3 results for "ruby pluralize" | ||
| # DEPLOY OK | ||
| # | ||
| #: () -> String | ||
| def format_unknown | ||
| preview = truncate(content.to_s.lines.first.to_s.strip) | ||
| ok_line(preview) | ||
| end | ||
|
|
||
| # Renders "<TOOL> OK[ <part> · <part> · ...]"; the success-side twin of | ||
| # #error_line. Blank/nil parts are dropped and the rest joined with " · ", | ||
| # so callers pass each piece of the summary without minding separators. | ||
| # | ||
| #: (*String?) -> String | ||
| def ok_line(*parts) | ||
| summary = parts.select(&:present?).join(" · ") | ||
| prefix = "#{@name.upcase} OK" | ||
| summary.present? ? "#{prefix} #{summary}" : prefix | ||
| end | ||
|
|
||
| # Renders "<TOOL> ERROR <message>". The content is shown as-is and intentionally not truncated, | ||
| # preserving the full diagnostic. | ||
| # | ||
| # Examples: | ||
| # READ ERROR ENOENT: no such file or directory | ||
| # | ||
| #: () -> String | ||
| def error_line | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: the outer |
||
| "#{@name.upcase} ERROR #{content.to_s.strip}".strip | ||
| end | ||
|
|
||
| # Truncates to TRUNCATE_LIMIT chars, appending "..." when cut. nil -> "". | ||
| # | ||
| #: (String?) -> String | ||
| def truncate(str) | ||
| s = str.to_s | ||
| s.length > TRUNCATE_LIMIT ? "#{s[0...TRUNCATE_LIMIT - 3]}..." : s | ||
| end | ||
| end | ||
| end | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.