Repository navigation
Expand file tree
/
Copy pathtranscripts.py
More file actions
370 lines (310 loc) · 12.2 KB
/
Copy pathtranscripts.py
File metadata and controls
370 lines (310 loc) · 12.2 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
"""Parse Cursor agent transcripts (``*.jsonl``) into structured messages.
Each line of a transcript file is one JSON object. There are three line shapes:
1. ``{"role": "user"|"assistant", "message": {"content": [...]}}``
- content is an array of blocks: ``{type:"text"|"tool_use", ...}``.
2. ``{"type": "turn_ended", "status": "success"|"error", "error"?: str}``
3. (rare) tool results / system lines — we ignore unknown shapes gracefully.
We deliberately keep parsing defensive: Cursor may add new block types and we
should never crash on a transcript, only degrade the rendering.
"""
from __future__ import annotations
import json
import logging
import os
from dataclasses import dataclass, field
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Iterator
LOG = logging.getLogger("transcripts")
# Cursor stores transcripts under ``%USERPROFILE%\.cursor\projects\<workspace>``.
# The workspace folder name is derived from the project path with non-alphanums
# replaced by dashes, e.g. ``D:\Seafile\Projects\cursor-use`` ->
# ``d-Seafile-Projects-cursor-use``.
DEFAULT_CURSOR_HOME = Path(os.environ.get("USERPROFILE", str(Path.home()))) / ".cursor"
@dataclass
class Block:
type: str # "text" | "tool_use" | "tool_result" | unknown
text: str = ""
tool_name: str = ""
tool_input: dict = field(default_factory=dict)
raw: dict = field(default_factory=dict)
def to_dict(self) -> dict:
return {
"type": self.type,
"text": self.text,
"tool_name": self.tool_name,
"tool_input": self.tool_input,
}
@dataclass
class Message:
role: str # "user" | "assistant" | "system"
blocks: list[Block] = field(default_factory=list)
error: str | None = None # set when followed by a turn_ended error
ts: str = "" # ISO8601, best effort
@property
def text(self) -> str:
"""Concatenated text blocks — handy for previews / search."""
return "\n".join(b.text for b in self.blocks if b.type == "text")
def to_dict(self) -> dict:
return {
"role": self.role,
"blocks": [b.to_dict() for b in self.blocks],
"error": self.error,
"ts": self.ts,
}
@dataclass
class Turn:
"""A user→assistant exchange ending at a ``turn_ended`` marker."""
index: int
messages: list[Message] = field(default_factory=list)
status: str = "success" # "success" | "error" | "partial"
error: str | None = None
def to_dict(self) -> dict:
return {
"index": self.index,
"status": self.status,
"error": self.error,
"messages": [m.to_dict() for m in self.messages],
}
@dataclass
class Transcript:
session_id: str
workspace: str
path: Path
turns: list[Turn] = field(default_factory=list)
mtime: float = 0.0
size: int = 0
@property
def title(self) -> str:
"""First non-empty user text — best-effort session title."""
for turn in self.turns:
for msg in turn.messages:
if msg.role == "user":
t = _strip_cursor_wrappers(msg.text).strip()
if t:
first_line = t.splitlines()[0][:120]
return first_line
return "(empty session)"
@property
def updated_at(self) -> str:
return datetime.fromtimestamp(self.mtime, tz=timezone.utc).isoformat()
def to_summary(self) -> dict:
return {
"session_id": self.session_id,
"workspace": self.workspace,
"title": self.title,
"updated_at": self.updated_at,
"size": self.size,
"turn_count": len(self.turns),
"path": str(self.path),
}
def to_dict(self) -> dict:
return {
**self.to_summary(),
"turns": [t.to_dict() for t in self.turns],
}
# --------------------------------------------------------------------------- #
# Path resolution
# --------------------------------------------------------------------------- #
def workspace_folder_name(project_path: str | Path) -> str:
"""Reproduce Cursor's workspace folder naming.
Empirically (verified against the live folder on this machine):
``D:\\Seafile\\Projects\\cursor-use`` → ``d-Seafile-Projects-cursor-use``
Algorithm: lowercase the drive letter, drop the colon, then replace each
backslash (path separator) with a single dash. Note: ``D:\\`` becomes
``d-`` (lowercase D + dash from the backslash), NOT ``d--`` — the colon
is removed entirely, not replaced.
"""
p = str(project_path).replace("/", "\\")
if len(p) >= 2 and p[1] == ":":
p = p[0].lower() + p[2:] # drop the colon, keep everything after it
return p.replace("\\", "-")
def cursor_projects_root(cursor_home: Path | None = None) -> Path:
return (cursor_home or DEFAULT_CURSOR_HOME) / "projects"
def find_workspace_dir(
project_path: str | None = None,
*,
cursor_home: Path | None = None,
) -> Path | None:
"""Locate the ``.cursor/projects/<workspace>`` directory for a project.
If ``project_path`` is None we auto-detect. The heuristic: among all
workspace dirs that actually contain transcript files, pick the one whose
newest transcript was modified most recently — that's almost certainly the
project the user is actively working on. (Cursor writes plenty of empty
workspace dirs for one-off buffers; those are noise.)
"""
root = cursor_projects_root(cursor_home)
if not root.exists():
return None
if project_path:
candidate = root / workspace_folder_name(project_path)
if candidate.exists():
return candidate
needle = workspace_folder_name(project_path).casefold()
for d in root.iterdir():
if d.is_dir() and d.name.casefold() == needle:
return d
return None
# Auto-detect: only consider dirs that have at least one transcript file,
# then pick by newest transcript mtime.
candidates: list[tuple[float, Path]] = []
for d in root.iterdir():
if not d.is_dir():
continue
tx_files = list_transcripts(d)
if tx_files:
# newest transcript mtime is the workspace's "last active" signal
candidates.append((tx_files[0].stat().st_mtime, d))
if not candidates:
return None
candidates.sort(reverse=True)
return candidates[0][1]
def list_transcripts(workspace_dir: Path) -> list[Path]:
"""All transcript jsonl files in a workspace, newest first."""
base = workspace_dir / "agent-transcripts"
if not base.exists():
return []
files = sorted(
(p for p in base.rglob("*.jsonl") if p.is_file()),
key=lambda p: p.stat().st_mtime,
reverse=True,
)
return files
# --------------------------------------------------------------------------- #
# Parsing
# --------------------------------------------------------------------------- #
def _parse_block(raw: dict) -> Block:
btype = raw.get("type", "unknown")
block = Block(type=btype, raw=raw)
if btype == "text":
block.text = str(raw.get("text", ""))
elif btype == "tool_use":
block.tool_name = str(raw.get("name", ""))
block.tool_input = raw.get("input") if isinstance(raw.get("input"), dict) else {}
elif btype == "tool_result":
# Cursor sometimes inlines tool results; surface them as text for now.
content = raw.get("content")
if isinstance(content, list):
block.text = "\n".join(
str(c.get("text", "")) for c in content if isinstance(c, dict)
)
elif isinstance(content, str):
block.text = content
return block
def _parse_message_line(obj: dict) -> Message | None:
role = obj.get("role")
if role not in ("user", "assistant", "system"):
return None
message = obj.get("message") or {}
content = message.get("content") if isinstance(message, dict) else None
blocks: list[Block] = []
if isinstance(content, list):
for item in content:
if isinstance(item, dict):
blocks.append(_parse_block(item))
elif isinstance(content, str):
blocks.append(Block(type="text", text=content))
return Message(role=role, blocks=blocks)
def _strip_cursor_wrappers(text: str) -> str:
"""Remove Cursor's auto-injected XML wrappers from a user message.
Cursor wraps mobile/forwarded user input like:
``<timestamp>...</timestamp>\\n<user_query>...actual text...</user_query>``
For display we only want the inner text. If no wrapper is present the
input is returned unchanged.
"""
if "<user_query>" not in text:
return text
import re
m = re.search(r"<user_query>\s*([\s\S]*?)\s*</user_query>", text)
return m.group(1).strip() if m else text
def parse_transcript(path: Path, workspace: str) -> Transcript:
"""Parse a single transcript jsonl into a Transcript object.
Never raises on malformed lines — logs and skips. Fail-fast is for
programming errors, not for adversarial input.
"""
session_id = path.stem
transcript = Transcript(
session_id=session_id,
workspace=workspace,
path=path,
mtime=path.stat().st_mtime,
size=path.stat().st_size,
)
current_turn: Turn | None = None
turn_index = 0
with path.open("r", encoding="utf-8") as fh:
for lineno, raw_line in enumerate(fh, 1):
raw_line = raw_line.strip()
if not raw_line:
continue
try:
obj = json.loads(raw_line)
except json.JSONDecodeError:
LOG.debug("skip malformed line %d in %s", lineno, path.name)
continue
if obj.get("type") == "turn_ended":
if current_turn is None:
# turn marker with no preceding messages — skip.
continue
status = obj.get("status", "success")
current_turn.status = status
if status == "error":
current_turn.error = obj.get("error") or "unknown error"
# Attach the error to the last assistant message for rendering.
for msg in reversed(current_turn.messages):
if msg.role == "assistant":
msg.error = current_turn.error
break
transcript.turns.append(current_turn)
current_turn = None
turn_index += 1
continue
msg = _parse_message_line(obj)
if msg is None:
continue
if current_turn is None:
current_turn = Turn(index=turn_index)
current_turn.messages.append(msg)
if current_turn is not None:
# Trailing messages without an explicit turn_ended (live session).
current_turn.status = "partial"
transcript.turns.append(current_turn)
return transcript
def iter_all_transcripts(
project_path: str | None = None,
*,
cursor_home: Path | None = None,
) -> Iterator[Transcript]:
workspace_dir = find_workspace_dir(project_path, cursor_home=cursor_home)
if not workspace_dir:
return
workspace = workspace_dir.name
for path in list_transcripts(workspace_dir):
try:
yield parse_transcript(path, workspace)
except Exception as exc:
LOG.warning("failed to parse %s: %s", path, exc)
def get_transcript(
session_id: str,
project_path: str | None = None,
*,
cursor_home: Path | None = None,
) -> Transcript | None:
for t in iter_all_transcripts(project_path, cursor_home=cursor_home):
if t.session_id == session_id:
return t
return None
def latest_transcript_summary(project_path: str | None = None) -> dict | None:
"""Convenience for the status endpoint."""
workspace_dir = find_workspace_dir(project_path)
if not workspace_dir:
return None
files = list_transcripts(workspace_dir)
if not files:
return None
path = files[0]
try:
t = parse_transcript(path, workspace_dir.name)
return t.to_summary()
except Exception:
return None