Skip to content

Commit 0cf7e28

Browse files
Kasper JungeRalphify
authored andcommitted
refactor: deduplicate tool arg extraction in _console_emitter.py
Extract _extract_tool_arg() helper to share the extractor-lookup-or- fallback logic between _format_tool_summary() and the live panel's tool_use rendering. Also replace four identical file_path lambdas with a single _extract_file_path() function. Co-authored-by: Ralphify <noreply@ralphify.co>
1 parent 8cceca4 commit 0cf7e28

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/ralphify/_console_emitter.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,14 @@ def _format_params(tool_input: dict[str, Any], keys: list[str]) -> str:
153153
return " · ".join(parts) if parts else ""
154154

155155

156+
def _extract_file_path(i: dict[str, Any]) -> str:
157+
return _shorten_path(i.get("file_path", ""))
158+
156159
_TOOL_ARG_EXTRACTORS: dict[str, Callable[[dict[str, Any]], str]] = {
157-
"Read": lambda i: _shorten_path(i.get("file_path", "")),
158-
"Write": lambda i: _shorten_path(i.get("file_path", "")),
159-
"Edit": lambda i: _shorten_path(i.get("file_path", "")),
160-
"MultiEdit": lambda i: _shorten_path(i.get("file_path", "")),
160+
"Read": _extract_file_path,
161+
"Write": _extract_file_path,
162+
"Edit": _extract_file_path,
163+
"MultiEdit": _extract_file_path,
161164
"Glob": lambda i: i.get("pattern", ""),
162165
"Grep": lambda i: i.get("pattern", ""),
163166
"Bash": lambda i: i.get("command", ""),
@@ -170,13 +173,17 @@ def _format_params(tool_input: dict[str, Any], keys: list[str]) -> str:
170173
}
171174

172175

173-
def _format_tool_summary(name: str, tool_input: dict[str, Any]) -> str:
174-
"""Return a compact one-liner describing a tool call."""
176+
def _extract_tool_arg(name: str, tool_input: dict[str, Any]) -> str:
177+
"""Return the most relevant argument string for a tool call."""
175178
extractor = _TOOL_ARG_EXTRACTORS.get(name)
176179
if extractor is not None:
177-
arg = extractor(tool_input)
178-
else:
179-
arg = ", ".join(sorted(tool_input.keys()))
180+
return extractor(tool_input)
181+
return ", ".join(sorted(tool_input.keys()))
182+
183+
184+
def _format_tool_summary(name: str, tool_input: dict[str, Any]) -> str:
185+
"""Return a compact one-liner describing a tool call."""
186+
arg = _extract_tool_arg(name, tool_input)
180187
if arg:
181188
return f"{name} {arg}"
182189
return name
@@ -418,10 +425,7 @@ def _apply_assistant(self, raw: dict[str, Any]) -> None:
418425
color, cat = _tool_style_for(name)
419426
self._tool_categories[cat] = self._tool_categories.get(cat, 0) + 1
420427

421-
if _TOOL_ARG_EXTRACTORS.get(name) is not None:
422-
arg = _TOOL_ARG_EXTRACTORS[name](tool_input)
423-
else:
424-
arg = ", ".join(sorted(tool_input.keys()))
428+
arg = _extract_tool_arg(name, tool_input)
425429

426430
# Pad short names to a fixed column so arguments line up;
427431
# longer names get a guaranteed two-space gap so the arg

0 commit comments

Comments
 (0)