|
15 | 15 | tuple there when a new predicate must sit above another. |
16 | 16 |
|
17 | 17 | Predicates live in ``models.tool_results`` (single source of truth for narrowing). |
| 18 | +
|
| 19 | +Adding a new Claude Code **tool use** name (e.g. ``"Read"``, ``"Bash"``): |
| 20 | +
|
| 21 | +1. Add the name to ``KNOWN_TOOL_TYPE_NAMES`` below (alphabetical order). |
| 22 | +2. Wire ``_FILE_ACTIVITY_HANDLERS`` (``None`` if no file/bash/web side effects). |
| 23 | +3. Add a Markdown branch in ``utils/md_exporter.py`` ``_render_tool_use``. |
| 24 | +4. Add ``TOOL_USE_RENDERERS`` entry in ``static/js/render/registry.js``. |
| 25 | +5. Add ``(predicate, builder)`` to ``_TOOL_RESULT_DISPATCH`` when the tool has a |
| 26 | + distinct ``toolUseResult`` shape (see ordering notes above). |
| 27 | +6. Run ``pytest tests/test_tool_dispatch_sync.py -v`` — it fails with the |
| 28 | + missing site if any step was skipped. |
| 29 | +
|
| 30 | +See ``CONTRIBUTING.md`` § "Adding a new tool type". |
18 | 31 | """ |
19 | 32 |
|
20 | | -from typing import cast |
| 33 | +from collections.abc import Callable |
| 34 | +from typing import Any, cast |
21 | 35 |
|
22 | 36 | from models.tool_results import ( |
23 | 37 | ToolResultDict, |
@@ -219,6 +233,84 @@ def _tool_result_build_user_input(tr: ToolResultDict, base: dict[str, object]) - |
219 | 233 | (is_user_input_tool_result, _tool_result_build_user_input), |
220 | 234 | ) |
221 | 235 |
|
| 236 | +# Claude Code assistant tool_use ``name`` values coordinated across parser file |
| 237 | +# activity, Markdown export, and the SPA ``TOOL_USE_RENDERERS`` map. |
| 238 | +KNOWN_TOOL_TYPE_NAMES: tuple[str, ...] = ( |
| 239 | + "AskUserQuestion", |
| 240 | + "Bash", |
| 241 | + "Edit", |
| 242 | + "Glob", |
| 243 | + "Grep", |
| 244 | + "Read", |
| 245 | + "Task", |
| 246 | + "TodoWrite", |
| 247 | + "WebFetch", |
| 248 | + "WebSearch", |
| 249 | + "Write", |
| 250 | +) |
| 251 | +KNOWN_TOOL_TYPES: frozenset[str] = frozenset(KNOWN_TOOL_TYPE_NAMES) |
| 252 | + |
| 253 | + |
| 254 | +def _file_activity_read(tool_input: dict[str, Any], metadata: dict[str, Any]) -> None: |
| 255 | + raw_fp = tool_input.get("file_path", "") |
| 256 | + fp = raw_fp if isinstance(raw_fp, str) else "" |
| 257 | + if fp: |
| 258 | + metadata["files_read"].add(fp) |
| 259 | + |
| 260 | + |
| 261 | +def _file_activity_write(tool_input: dict[str, Any], metadata: dict[str, Any]) -> None: |
| 262 | + raw_fp = tool_input.get("file_path", "") |
| 263 | + fp = raw_fp if isinstance(raw_fp, str) else "" |
| 264 | + if fp: |
| 265 | + metadata["files_created"].add(fp) |
| 266 | + |
| 267 | + |
| 268 | +def _file_activity_edit(tool_input: dict[str, Any], metadata: dict[str, Any]) -> None: |
| 269 | + raw_fp = tool_input.get("file_path", "") |
| 270 | + fp = raw_fp if isinstance(raw_fp, str) else "" |
| 271 | + if fp: |
| 272 | + metadata["files_written"].add(fp) |
| 273 | + |
| 274 | + |
| 275 | +def _file_activity_bash(tool_input: dict[str, Any], metadata: dict[str, Any]) -> None: |
| 276 | + cmd = tool_input.get("command", "") |
| 277 | + if isinstance(cmd, str) and cmd: |
| 278 | + metadata["bash_commands"].append(cmd) |
| 279 | + |
| 280 | + |
| 281 | +def _file_activity_web(tool_input: dict[str, Any], metadata: dict[str, Any]) -> None: |
| 282 | + url_or_query = tool_input.get("url") or tool_input.get("query", "") |
| 283 | + if isinstance(url_or_query, str) and url_or_query: |
| 284 | + metadata["web_fetches"].append(url_or_query) |
| 285 | + |
| 286 | + |
| 287 | +# Every ``KNOWN_TOOL_TYPES`` entry must appear here (``None`` = no side effects). |
| 288 | +_FILE_ACTIVITY_HANDLERS: dict[str, Callable[[dict[str, Any], dict[str, Any]], None] | None] = { |
| 289 | + "AskUserQuestion": None, |
| 290 | + "Bash": _file_activity_bash, |
| 291 | + "Edit": _file_activity_edit, |
| 292 | + "Glob": None, |
| 293 | + "Grep": None, |
| 294 | + "Read": _file_activity_read, |
| 295 | + "Task": None, |
| 296 | + "TodoWrite": None, |
| 297 | + "WebFetch": _file_activity_web, |
| 298 | + "WebSearch": _file_activity_web, |
| 299 | + "Write": _file_activity_write, |
| 300 | +} |
| 301 | +FILE_ACTIVITY_TOOL_TYPES: frozenset[str] = frozenset(_FILE_ACTIVITY_HANDLERS) |
| 302 | + |
| 303 | + |
| 304 | +def track_tool_file_activity( |
| 305 | + tool_name: str, tool_input: dict[str, Any], metadata: dict[str, Any] |
| 306 | +) -> None: |
| 307 | + """Record file/bash/web side effects for tools listed in ``KNOWN_TOOL_TYPES``.""" |
| 308 | + if tool_name not in KNOWN_TOOL_TYPES: |
| 309 | + return |
| 310 | + handler = _FILE_ACTIVITY_HANDLERS[tool_name] |
| 311 | + if handler is not None: |
| 312 | + handler(tool_input, metadata) |
| 313 | + |
222 | 314 |
|
223 | 315 | def _parse_tool_result( |
224 | 316 | tool_result: ToolResultUnion | None, slug: str | None = None |
|
0 commit comments