-
Notifications
You must be signed in to change notification settings - Fork 69
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 |
|---|---|---|
|
|
@@ -25,31 +25,53 @@ class ToolCallMessage | |
| def initialize(id:, name:, arguments:) | ||
| @id = id | ||
| @name = name | ||
| @arguments = arguments | ||
| @arguments = arguments.is_a?(Hash) ? arguments : {} | ||
| end | ||
|
|
||
| #: () -> String? | ||
| 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 | ||
| rescue StandardError | ||
| format_unknown | ||
| end | ||
|
|
||
| # Truncates each value in a formatted tool-call string to keep terminal output concise. | ||
| 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
Contributor
Author
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. Added the fallback! |
||
| label = name.to_s.upcase | ||
| return label if arguments.empty? | ||
|
|
||
| details = arguments.map { |key, value| "#{key}: #{truncate(value.inspect)}" }.sort_by { |s| [s.length, s] }.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 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.