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