|
| 1 | +"""TypedDict shapes for Claude Code toolUseResult blobs at the JSONL parse boundary. |
| 2 | +
|
| 3 | +Ground truth: tests/test_jsonl_parser.py, tests/test_real_session_fixtures.py, |
| 4 | +and utils/tool_dispatch.py predicate order (first match wins). |
| 5 | +""" |
| 6 | + |
| 7 | +from typing import Literal, TypedDict, TypeGuard |
| 8 | + |
| 9 | + |
| 10 | +class BashToolResultDict(TypedDict, total=False): |
| 11 | + stdout: str |
| 12 | + stderr: str |
| 13 | + exitCode: int |
| 14 | + interrupted: bool |
| 15 | + is_error: bool |
| 16 | + returnCodeInterpretation: str |
| 17 | + |
| 18 | + |
| 19 | +class FileEditToolResultDict(TypedDict, total=False): |
| 20 | + structuredPatch: str |
| 21 | + filePath: str |
| 22 | + newString: str |
| 23 | + replaceAll: bool |
| 24 | + |
| 25 | + |
| 26 | +class PlanToolResultDict(TypedDict, total=False): |
| 27 | + plan: list[object] |
| 28 | + filePath: str |
| 29 | + content: str |
| 30 | + |
| 31 | + |
| 32 | +class FileWriteToolResultDict(TypedDict, total=False): |
| 33 | + filePath: str |
| 34 | + content: str |
| 35 | + |
| 36 | + |
| 37 | +class GlobToolResultDict(TypedDict, total=False): |
| 38 | + filenames: list[str] |
| 39 | + numFiles: int |
| 40 | + truncated: bool |
| 41 | + durationMs: int |
| 42 | + |
| 43 | + |
| 44 | +class GrepToolResultDict(TypedDict, total=False): |
| 45 | + mode: str |
| 46 | + numFiles: int |
| 47 | + numLines: int |
| 48 | + content: str |
| 49 | + durationMs: int |
| 50 | + |
| 51 | + |
| 52 | +class ReadFileObjDict(TypedDict, total=False): |
| 53 | + filePath: str |
| 54 | + numLines: int |
| 55 | + content: str |
| 56 | + |
| 57 | + |
| 58 | +class ReadToolResultDict(TypedDict, total=False): |
| 59 | + file: ReadFileObjDict |
| 60 | + content: list[object] |
| 61 | + |
| 62 | + |
| 63 | +class WebSearchToolResultDict(TypedDict, total=False): |
| 64 | + query: str |
| 65 | + results: list[object] | None |
| 66 | + durationSeconds: float |
| 67 | + |
| 68 | + |
| 69 | +class WebFetchToolResultDict(TypedDict, total=False): |
| 70 | + url: str |
| 71 | + code: int |
| 72 | + durationMs: int |
| 73 | + |
| 74 | + |
| 75 | +class TaskMessageToolResultDict(TypedDict, total=False): |
| 76 | + task_id: str |
| 77 | + task_type: str |
| 78 | + message: str |
| 79 | + agentId: str |
| 80 | + |
| 81 | + |
| 82 | +class TaskRetrievalToolResultDict(TypedDict, total=False): |
| 83 | + retrieval_status: str |
| 84 | + task: dict[str, object] |
| 85 | + |
| 86 | + |
| 87 | +class TaskCompletedToolResultDict(TypedDict, total=False): |
| 88 | + agentId: str |
| 89 | + totalDurationMs: int |
| 90 | + status: str |
| 91 | + totalTokens: int |
| 92 | + totalToolUseCount: int |
| 93 | + |
| 94 | + |
| 95 | +class TaskAsyncToolResultDict(TypedDict, total=False): |
| 96 | + agentId: str |
| 97 | + isAsync: bool |
| 98 | + status: str |
| 99 | + description: str |
| 100 | + |
| 101 | + |
| 102 | +class TodoItemDict(TypedDict, total=False): |
| 103 | + id: str |
| 104 | + content: str |
| 105 | + |
| 106 | + |
| 107 | +class TodoWriteToolResultDict(TypedDict, total=False): |
| 108 | + newTodos: list[TodoItemDict] |
| 109 | + oldTodos: list[TodoItemDict] |
| 110 | + |
| 111 | + |
| 112 | +class UserInputToolResultDict(TypedDict, total=False): |
| 113 | + questions: list[dict[str, object]] |
| 114 | + answers: dict[str, object] |
| 115 | + |
| 116 | + |
| 117 | +class ToolResultContentBlockDict(TypedDict, total=False): |
| 118 | + type: str |
| 119 | + source: dict[str, object] |
| 120 | + |
| 121 | + |
| 122 | +class ToolResultWithContentDict(TypedDict, total=False): |
| 123 | + """Read-on-image and similar payloads that embed content blocks.""" |
| 124 | + |
| 125 | + content: list[ToolResultContentBlockDict] |
| 126 | + |
| 127 | + |
| 128 | +# Dict passed into dispatch predicates (structural superset of all tool blobs). |
| 129 | +ToolResultDict = dict[str, object] |
| 130 | + |
| 131 | +ToolResultUnion = ( |
| 132 | + str |
| 133 | + | BashToolResultDict |
| 134 | + | FileEditToolResultDict |
| 135 | + | PlanToolResultDict |
| 136 | + | FileWriteToolResultDict |
| 137 | + | GlobToolResultDict |
| 138 | + | GrepToolResultDict |
| 139 | + | ReadToolResultDict |
| 140 | + | WebSearchToolResultDict |
| 141 | + | WebFetchToolResultDict |
| 142 | + | TaskMessageToolResultDict |
| 143 | + | TaskRetrievalToolResultDict |
| 144 | + | TaskCompletedToolResultDict |
| 145 | + | TaskAsyncToolResultDict |
| 146 | + | TodoWriteToolResultDict |
| 147 | + | UserInputToolResultDict |
| 148 | + | ToolResultWithContentDict |
| 149 | + | dict[str, object] |
| 150 | +) |
| 151 | + |
| 152 | + |
| 153 | +def is_tool_result_dict(tr: ToolResultUnion | None) -> TypeGuard[ToolResultDict]: |
| 154 | + return isinstance(tr, dict) |
| 155 | + |
| 156 | + |
| 157 | +def is_bash_tool_result(tr: ToolResultDict) -> TypeGuard[BashToolResultDict]: |
| 158 | + return "stdout" in tr or "stderr" in tr |
| 159 | + |
| 160 | + |
| 161 | +def is_file_edit_tool_result(tr: ToolResultDict) -> TypeGuard[FileEditToolResultDict]: |
| 162 | + return "structuredPatch" in tr or ("filePath" in tr and "newString" in tr) |
| 163 | + |
| 164 | + |
| 165 | +def is_plan_tool_result(tr: ToolResultDict) -> TypeGuard[PlanToolResultDict]: |
| 166 | + return "plan" in tr and "filePath" in tr |
| 167 | + |
| 168 | + |
| 169 | +def is_file_write_tool_result(tr: ToolResultDict) -> TypeGuard[FileWriteToolResultDict]: |
| 170 | + return "filePath" in tr and "content" in tr |
| 171 | + |
| 172 | + |
| 173 | +def is_glob_tool_result(tr: ToolResultDict) -> TypeGuard[GlobToolResultDict]: |
| 174 | + filenames = tr.get("filenames") |
| 175 | + return "filenames" in tr and isinstance(filenames, list) |
| 176 | + |
| 177 | + |
| 178 | +def is_grep_tool_result(tr: ToolResultDict) -> TypeGuard[GrepToolResultDict]: |
| 179 | + return "mode" in tr and "numFiles" in tr |
| 180 | + |
| 181 | + |
| 182 | +def is_read_tool_result(tr: ToolResultDict) -> TypeGuard[ReadToolResultDict]: |
| 183 | + file_obj = tr.get("file") |
| 184 | + return "file" in tr and isinstance(file_obj, dict) |
| 185 | + |
| 186 | + |
| 187 | +def is_web_search_tool_result(tr: ToolResultDict) -> TypeGuard[WebSearchToolResultDict]: |
| 188 | + return "query" in tr and "results" in tr |
| 189 | + |
| 190 | + |
| 191 | +def is_web_fetch_tool_result(tr: ToolResultDict) -> TypeGuard[WebFetchToolResultDict]: |
| 192 | + return "url" in tr and "code" in tr |
| 193 | + |
| 194 | + |
| 195 | +def is_task_message_tool_result(tr: ToolResultDict) -> TypeGuard[TaskMessageToolResultDict]: |
| 196 | + # Broad: matches ``task_id`` OR ``message``. Runs before retrieval/completed/async |
| 197 | + # arms in tool_dispatch — same short-circuit order as the historical if/elif chain. |
| 198 | + return "task_id" in tr or "message" in tr |
| 199 | + |
| 200 | + |
| 201 | +def is_task_retrieval_tool_result(tr: ToolResultDict) -> TypeGuard[TaskRetrievalToolResultDict]: |
| 202 | + return "retrieval_status" in tr and "task" in tr |
| 203 | + |
| 204 | + |
| 205 | +def is_task_completed_tool_result(tr: ToolResultDict) -> TypeGuard[TaskCompletedToolResultDict]: |
| 206 | + return "agentId" in tr and "totalDurationMs" in tr |
| 207 | + |
| 208 | + |
| 209 | +def is_task_async_tool_result(tr: ToolResultDict) -> TypeGuard[TaskAsyncToolResultDict]: |
| 210 | + return "agentId" in tr and "isAsync" in tr |
| 211 | + |
| 212 | + |
| 213 | +def is_todo_write_tool_result(tr: ToolResultDict) -> TypeGuard[TodoWriteToolResultDict]: |
| 214 | + return "newTodos" in tr or "oldTodos" in tr |
| 215 | + |
| 216 | + |
| 217 | +def is_user_input_tool_result(tr: ToolResultDict) -> TypeGuard[UserInputToolResultDict]: |
| 218 | + return "questions" in tr and "answers" in tr |
| 219 | + |
| 220 | + |
| 221 | +# Tool names on assistant tool_use blocks — pairs with slug on user tool_result rows. |
| 222 | +ToolNameLiteral = Literal[ |
| 223 | + "Bash", |
| 224 | + "Read", |
| 225 | + "Write", |
| 226 | + "Edit", |
| 227 | + "Glob", |
| 228 | + "Grep", |
| 229 | + "Task", |
| 230 | + "TodoWrite", |
| 231 | + "WebFetch", |
| 232 | + "WebSearch", |
| 233 | +] |
0 commit comments