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