-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconversation.py
More file actions
352 lines (311 loc) · 11.8 KB
/
Copy pathconversation.py
File metadata and controls
352 lines (311 loc) · 11.8 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
from __future__ import annotations
import logging
from dataclasses import dataclass, field
from typing import Any
from models.errors import SchemaError
from models.from_dict_validation import (
require_dict,
require_key,
require_non_empty_str,
require_non_empty_str_field,
require_type,
)
_logger = logging.getLogger(__name__)
@dataclass(frozen=True)
class Composer:
"""Cursor conversation row from globalStorage cursorDiskKV; requires fullConversationHeadersOnly + createdAt."""
composer_id: str
full_conversation_headers_only: list[dict[str, Any]]
created_at: Any
name: str | None = None
last_updated_at: Any = None
model_config: dict[str, Any] = field(default_factory=dict)
raw: dict[str, Any] = field(default_factory=dict)
@classmethod
def from_dict(cls, raw: dict[str, Any], *, composer_id: str) -> "Composer":
raw = require_dict(raw, model="Composer", field="composerData")
require_non_empty_str(composer_id, model="Composer", field="composerId")
require_key(raw, "fullConversationHeadersOnly", model="Composer")
require_key(raw, "createdAt", model="Composer")
created_at = raw.get("createdAt")
# Numeric-only on purpose: a 2026-05 scan of 17/17 live composers on
# disk stored createdAt as int milliseconds. If Cursor ever switches
# to ISO strings, those rows would disappear from list/search via a
# drift warning — relax the check at that point, don't silently coerce.
if not isinstance(created_at, (int, float)) or isinstance(created_at, bool):
raise SchemaError(
"Composer",
"createdAt",
hint=f"expected timestamp number, got {type(created_at).__name__}",
)
headers_value = raw.get("fullConversationHeadersOnly")
headers = require_type(
headers_value,
list,
model="Composer",
field="fullConversationHeadersOnly",
hint=f"expected list, got {type(headers_value).__name__}",
)
model_config = raw.get("modelConfig") or {}
if not isinstance(model_config, dict):
model_config = {}
return cls(
composer_id=composer_id,
full_conversation_headers_only=headers,
created_at=created_at,
name=raw.get("name"),
last_updated_at=raw.get("lastUpdatedAt"),
model_config=model_config,
raw=raw,
)
@property
def newly_created_files(self) -> list[Any]:
value = self.raw.get("newlyCreatedFiles")
if value is None:
return []
if not isinstance(value, list):
_logger.warning(
"Schema drift in Composer %s: invalid type for newlyCreatedFiles (expected list, got %s)",
self.composer_id,
type(value).__name__,
)
return []
return value
@property
def code_block_data(self) -> dict[str, Any] | None:
value = self.raw.get("codeBlockData")
if value is None:
return None
if not isinstance(value, dict):
_logger.warning(
"Schema drift in Composer %s: invalid type for codeBlockData (expected dict, got %s)",
self.composer_id,
type(value).__name__,
)
return None
return value
@property
def usage_data(self) -> dict[str, Any]:
"""Composer cost rollup; empty dict when absent (common)."""
value = self.raw.get("usageData")
if value is None:
return {}
if not isinstance(value, dict):
suffix = f" {self.composer_id}" if self.composer_id else ""
_logger.warning(
"Schema drift in Composer%s: invalid type for usageData (expected dict, got %s)",
suffix,
type(value).__name__,
)
return {}
return value
def _optional_counter(self, key: str) -> int | float:
value = self.raw.get(key, 0)
if isinstance(value, bool) or not isinstance(value, (int, float)):
if key in self.raw:
suffix = f" {self.composer_id}" if self.composer_id else ""
_logger.warning(
"Schema drift in Composer%s: invalid type for %s (expected number, got %s)",
suffix,
key,
type(value).__name__,
)
return 0
return value
@property
def total_lines_added(self) -> int | float:
return self._optional_counter("totalLinesAdded")
@property
def total_lines_removed(self) -> int | float:
return self._optional_counter("totalLinesRemoved")
@property
def added_files(self) -> int | float:
return self._optional_counter("addedFiles")
@property
def removed_files(self) -> int | float:
return self._optional_counter("removedFiles")
def model_name_from_config(self) -> str | None:
name = self.model_config.get("modelName")
return name if isinstance(name, str) and name else None
@dataclass(frozen=True)
class WorkspaceLocalComposer:
"""Summary composer row from per-workspace state.vscdb ItemTable; only composerId is required."""
composer_id: str
last_updated_at: Any = None
raw: dict[str, Any] = field(default_factory=dict)
@classmethod
def from_dict(cls, raw: dict[str, Any]) -> "WorkspaceLocalComposer":
raw = require_dict(raw, model="WorkspaceLocalComposer", field="composer")
composer_id = require_non_empty_str_field(
raw, "composerId", model="WorkspaceLocalComposer"
)
return cls(
composer_id=composer_id,
last_updated_at=raw.get("lastUpdatedAt"),
raw=raw,
)
@dataclass(frozen=True)
class Bubble:
"""One message in a composer; bubble_id comes from the row key, not the JSON value."""
bubble_id: str
raw: dict[str, Any] = field(default_factory=dict)
@classmethod
def from_dict(cls, raw: dict[str, Any], *, bubble_id: str) -> "Bubble":
raw = require_dict(raw, model="Bubble", field="bubble")
require_non_empty_str(bubble_id, model="Bubble", field="bubbleId")
return cls(bubble_id=bubble_id, raw=raw)
@property
def text(self) -> str | None:
"""Plain ``text`` field; richText is handled by :func:`extract_text_from_bubble`."""
value = self.raw.get("text")
return value if isinstance(value, str) else None
@property
def metadata(self) -> dict[str, Any]:
value = self.raw.get("metadata")
if value is None:
return {}
if not isinstance(value, dict):
_logger.warning(
"Schema drift in Bubble %s: invalid type for metadata (expected dict, got %s)",
self.bubble_id,
type(value).__name__,
)
return {}
return value
@property
def relevant_files(self) -> list[Any]:
value = self.raw.get("relevantFiles")
if value is None:
return []
if not isinstance(value, list):
_logger.warning(
"Schema drift in Bubble %s: invalid type for relevantFiles (expected list, got %s)",
self.bubble_id,
type(value).__name__,
)
return []
return value
@property
def attached_file_code_chunks_uris(self) -> list[Any]:
value = self.raw.get("attachedFileCodeChunksUris")
if value is None:
return []
if not isinstance(value, list):
_logger.warning(
"Schema drift in Bubble %s: invalid type for attachedFileCodeChunksUris (expected list, got %s)",
self.bubble_id,
type(value).__name__,
)
return []
return value
@property
def context(self) -> dict[str, Any]:
value = self.raw.get("context")
if value is None:
return {}
if not isinstance(value, dict):
_logger.warning(
"Schema drift in Bubble %s: invalid type for context (expected dict, got %s)",
self.bubble_id,
type(value).__name__,
)
return {}
return value
@property
def token_count(self) -> dict[str, Any] | None:
value = self.raw.get("tokenCount")
if value is None:
return None
if not isinstance(value, dict):
_logger.warning(
"Schema drift in Bubble %s: invalid type for tokenCount (expected dict, got %s)",
self.bubble_id,
type(value).__name__,
)
return None
return value
@property
def tool_former_data(self) -> dict[str, Any] | None:
value = self.raw.get("toolFormerData")
if value is None:
return None
if not isinstance(value, dict):
_logger.warning(
"Schema drift in Bubble %s: invalid type for toolFormerData (expected dict, got %s)",
self.bubble_id,
type(value).__name__,
)
return None
return value
@property
def model_info(self) -> dict[str, Any]:
value = self.raw.get("modelInfo")
if value is None:
return {}
if not isinstance(value, dict):
_logger.warning(
"Schema drift in Bubble %s: invalid type for modelInfo (expected dict, got %s)",
self.bubble_id,
type(value).__name__,
)
return {}
return value
@property
def thinking(self) -> str | dict[str, Any] | None:
value = self.raw.get("thinking")
if value is None:
return None
if isinstance(value, (str, dict)):
return value
_logger.warning(
"Schema drift in Bubble %s: invalid type for thinking (expected str or dict, got %s)",
self.bubble_id,
type(value).__name__,
)
return None
@property
def thinking_duration_ms(self) -> int | float | None:
value = self.raw.get("thinkingDurationMs")
if value is None:
return None
if isinstance(value, bool) or not isinstance(value, (int, float)):
_logger.warning(
"Schema drift in Bubble %s: invalid type for thinkingDurationMs (expected number, got %s)",
self.bubble_id,
type(value).__name__,
)
return None
return value
@property
def context_window_status_at_creation(self) -> dict[str, Any]:
value = self.raw.get("contextWindowStatusAtCreation")
if value is None:
return {}
if not isinstance(value, dict):
_logger.warning(
"Schema drift in Bubble %s: invalid type for contextWindowStatusAtCreation (expected dict, got %s)",
self.bubble_id,
type(value).__name__,
)
return {}
return value
@property
def tool_results(self) -> list[Any] | None:
value = self.raw.get("toolResults")
if value is None:
return None
if not isinstance(value, list):
_logger.warning(
"Schema drift in Bubble %s: invalid type for toolResults (expected list, got %s)",
self.bubble_id,
type(value).__name__,
)
return None
return value
def bubble_timestamp_ms(self) -> int | float | None:
"""``createdAt`` or ``timestamp`` in milliseconds when present."""
for key in ("createdAt", "timestamp"):
value = self.raw.get(key)
if isinstance(value, (int, float)) and not isinstance(value, bool):
return value
return None