-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtool_parser.py
More file actions
188 lines (165 loc) · 7.3 KB
/
Copy pathtool_parser.py
File metadata and controls
188 lines (165 loc) · 7.3 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
"""
Shared utility for parsing Cursor's toolFormerData into structured tool call objects.
Used by both workspaces.py (browser API) and export_api.py (bulk export).
"""
from __future__ import annotations
import json
from typing import Any
def short_path(p: str) -> str:
"""Shorten a file path for display."""
if not p:
return ""
parts = p.replace("\\", "/").split("/")
if len(parts) > 3:
return ".../" + "/".join(parts[-3:])
return p
def parse_tool_call(tfd: dict[str, Any]) -> dict[str, str]:
"""Parse toolFormerData into a structured tool call object with human-readable summaries."""
name = tfd.get("name") or "unknown"
status = tfd.get("status") or ""
# Parse params — try params first, then rawArgs
params_raw = tfd.get("params") or tfd.get("rawArgs") or ""
params_parsed = {}
if isinstance(params_raw, str) and params_raw:
try:
params_parsed = json.loads(params_raw)
except Exception:
pass
elif isinstance(params_raw, dict):
params_parsed = params_raw
# Parse result
result_raw = tfd.get("result") or ""
result_parsed = {}
if isinstance(result_raw, str) and result_raw:
try:
result_parsed = json.loads(result_raw)
except Exception:
result_parsed = {"output": result_raw}
# Build human-readable summary and structured output based on tool name
summary = ""
input_display = ""
output_display = ""
if name == "read_file_v2":
fp = params_parsed.get("targetFile") or params_parsed.get("path") or ""
offset = params_parsed.get("offset")
limit = params_parsed.get("limit")
range_str = ""
if offset is not None and limit is not None:
range_str = f" (lines {offset}-{offset + limit})"
elif offset is not None:
range_str = f" (from line {offset})"
summary = f"Read: {short_path(fp)}{range_str}"
input_display = fp
contents = result_parsed.get("contents") or ""
output_display = contents
elif name == "edit_file_v2":
fp = params_parsed.get("relativeWorkspacePath") or params_parsed.get("targetFile") or ""
summary = f"Edit: {short_path(fp)}"
input_display = fp
before_id = result_parsed.get("beforeContentId", "")
after_id = result_parsed.get("afterContentId", "")
if before_id or after_id:
output_display = "File modified"
streaming = params_parsed.get("streamingContent") or ""
if streaming:
output_display = streaming
elif name == "run_terminal_command_v2":
cmd = params_parsed.get("command") or ""
summary = f"Terminal: {cmd[:80]}{'...' if len(cmd) > 80 else ''}"
input_display = cmd
output = result_parsed.get("output") or ""
output_display = output
elif name == "ripgrep_raw_search":
pattern = params_parsed.get("pattern") or ""
path = params_parsed.get("path") or ""
summary = f"Search: /{pattern}/ in {short_path(path)}"
input_display = f"Pattern: {pattern}\nPath: {path}"
success = result_parsed.get("success", {})
if isinstance(success, dict):
ws_results = success.get("workspaceResults", {})
if isinstance(ws_results, dict):
all_content = []
for ws_path_key, ws_data in ws_results.items():
if isinstance(ws_data, dict):
content = ws_data.get("content", {})
if isinstance(content, dict):
for match_file, match_data in content.items():
all_content.append(f"# {match_file}")
if isinstance(match_data, dict):
lines = match_data.get("lines") or match_data.get("content") or ""
if lines:
all_content.append(str(lines))
output_display = "\n".join(all_content) if all_content else "No matches"
else:
output_display = str(success)
else:
output_display = str(result_parsed)
elif name == "semantic_search_full":
query = params_parsed.get("query") or ""
summary = f"Semantic search: {query[:60]}"
input_display = query
code_results = result_parsed.get("codeResults", [])
if isinstance(code_results, list):
lines = []
for cr in code_results:
if isinstance(cr, dict):
cb = cr.get("codeBlock", {})
if isinstance(cb, dict):
fp = cb.get("relativeWorkspacePath") or ""
lines.append(f"# {fp}")
contents = cb.get("contents") or ""
if contents:
lines.append(contents)
output_display = "\n".join(lines) if lines else "No results"
elif name == "glob_file_search":
pattern = params_parsed.get("pattern") or params_parsed.get("glob") or params_parsed.get("query") or ""
summary = f"Glob: {pattern}" if pattern else "Glob search"
input_display = json.dumps(params_parsed, indent=2) if params_parsed else pattern
files = result_parsed.get("files") or result_parsed.get("results") or []
if isinstance(files, list):
output_display = "\n".join(str(f) for f in files)
elif name == "list_dir_v2":
path = params_parsed.get("path") or params_parsed.get("relativeWorkspacePath") or ""
summary = f"List dir: {short_path(path)}"
input_display = path
output_display = str(result_parsed)
elif name == "web_search":
query = params_parsed.get("searchTerm") or params_parsed.get("query") or params_parsed.get("search_term") or ""
summary = f"Web search: {query[:60]}" if query else "Web search"
input_display = query
output_display = str(result_parsed)
elif name == "web_fetch":
url = params_parsed.get("url") or ""
summary = f"Fetch: {url[:60]}"
input_display = url
output_display = str(result_parsed)
elif name == "todo_write":
summary = "Todo write"
todos = result_parsed.get("finalTodos") or []
if isinstance(todos, list):
lines = []
for t in todos:
if isinstance(t, dict):
lines.append(f"[{t.get('status', '?')}] {t.get('content', '')}")
output_display = "\n".join(lines)
elif name == "task_v2":
desc = params_parsed.get("description") or ""
summary = f"Task: {desc[:60]}"
input_display = desc
output_display = str(result_parsed)
elif name == "read_lints":
path = params_parsed.get("path") or ""
summary = f"Read lints: {short_path(path)}"
output_display = str(result_parsed)
else:
# Generic fallback — preserve full content for all tool types
summary = f"{name}"
input_display = json.dumps(params_parsed, indent=2) if params_parsed else ""
output_display = json.dumps(result_parsed, indent=2) if result_parsed else ""
return {
"name": name,
"status": status,
"summary": summary,
"input": input_display,
"output": output_display,
}