Skip to content

Commit 8d1a5e2

Browse files
Kasper JungeRalphify
authored andcommitted
refactor: replace repetitive lambdas in tool registry with factory extractors
Eight inline lambdas in _TOOL_REGISTRY followed two repeated patterns: `lambda i: i.get(key, "")` (5 instances) and `lambda i: _format_params(i, [...])` (3 instances, 2 identical). Extract `_extract_key(key)` and `_extract_params(*keys)` factories so the registry table reads as declarative config instead of ad-hoc closures. Co-authored-by: Ralphify <noreply@ralphify.co>
1 parent a4d41d1 commit 8d1a5e2

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/ralphify/_console_emitter.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,17 @@ def _extract_file_path(i: dict[str, Any]) -> str:
158158
return _shorten_path(i.get("file_path", ""))
159159

160160

161+
def _extract_key(key: str) -> Callable[[dict[str, Any]], str]:
162+
"""Factory for extractors that pull a single key from tool input."""
163+
return lambda i: i.get(key, "")
164+
165+
166+
def _extract_params(*keys: str) -> Callable[[dict[str, Any]], str]:
167+
"""Factory for extractors that format multiple keys as ``key: value`` pairs."""
168+
key_list = list(keys)
169+
return lambda i: _format_params(i, key_list)
170+
171+
161172
def _extract_tool_arg(name: str, tool_input: dict[str, Any]) -> str:
162173
"""Return the most relevant argument string for a tool call."""
163174
cfg = _TOOL_REGISTRY.get(name)
@@ -233,24 +244,18 @@ class _ToolConfig:
233244

234245
_TOOL_REGISTRY: dict[str, _ToolConfig] = {
235246
"Read": _ToolConfig(_brand.BLUE, "read", _extract_file_path),
236-
"Glob": _ToolConfig(_brand.BLUE, "glob", lambda i: i.get("pattern", "")),
237-
"Grep": _ToolConfig(_brand.BLUE, "grep", lambda i: i.get("pattern", "")),
247+
"Glob": _ToolConfig(_brand.BLUE, "glob", _extract_key("pattern")),
248+
"Grep": _ToolConfig(_brand.BLUE, "grep", _extract_key("pattern")),
238249
"Edit": _ToolConfig(_brand.ORANGE, "edit", _extract_file_path),
239250
"MultiEdit": _ToolConfig(_brand.ORANGE, "edit", _extract_file_path),
240251
"Write": _ToolConfig(_brand.ORANGE, "write", _extract_file_path),
241-
"Bash": _ToolConfig(_brand.GREEN, "bash", lambda i: i.get("command", "")),
252+
"Bash": _ToolConfig(_brand.GREEN, "bash", _extract_key("command")),
242253
"BashOutput": _ToolConfig(_brand.GREEN, "bash"),
243-
"WebFetch": _ToolConfig(_brand.LAVENDER, "web", lambda i: i.get("url", "")),
244-
"WebSearch": _ToolConfig(_brand.LAVENDER, "web", lambda i: i.get("query", "")),
245-
"Task": _ToolConfig(
246-
_brand.VIOLET, "task", lambda i: _format_params(i, ["description", "prompt"])
247-
),
248-
"Agent": _ToolConfig(
249-
_brand.VIOLET, "agent", lambda i: _format_params(i, ["description", "prompt"])
250-
),
251-
"ToolSearch": _ToolConfig(
252-
_brand.BLUE, "other", lambda i: _format_params(i, ["query", "max_results"])
253-
),
254+
"WebFetch": _ToolConfig(_brand.LAVENDER, "web", _extract_key("url")),
255+
"WebSearch": _ToolConfig(_brand.LAVENDER, "web", _extract_key("query")),
256+
"Task": _ToolConfig(_brand.VIOLET, "task", _extract_params("description", "prompt")),
257+
"Agent": _ToolConfig(_brand.VIOLET, "agent", _extract_params("description", "prompt")),
258+
"ToolSearch": _ToolConfig(_brand.BLUE, "other", _extract_params("query", "max_results")),
254259
"TodoWrite": _ToolConfig(
255260
_brand.PURPLE, "todo", lambda i: f"{len(i.get('todos', []))} todos"
256261
),

0 commit comments

Comments
 (0)