-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworkspace_listing.py
More file actions
287 lines (263 loc) · 11.4 KB
/
Copy pathworkspace_listing.py
File metadata and controls
287 lines (263 loc) · 11.4 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
from __future__ import annotations
import json
import logging
import os
import sqlite3
from datetime import datetime, timezone
_logger = logging.getLogger(__name__)
from utils.cli_chat_reader import list_cli_projects
from utils.exclusion_rules import build_searchable_text, is_excluded_by_rules
from utils.path_helpers import (
get_workspace_folder_paths,
normalize_file_path,
to_epoch_ms,
warn_workspace_json_read,
)
from utils.workspace_descriptor import read_json_file
from utils.workspace_path import get_cli_chats_path
from models import Composer, SchemaError
from services.workspace_db import (
_build_composer_id_to_workspace_id,
_collect_invalid_workspace_ids,
_collect_workspace_entries,
load_bubble_map,
load_project_layouts_map,
_open_global_db,
)
from services.workspace_resolver import (
_create_project_name_to_workspace_id_map,
_create_workspace_path_to_id_map,
_determine_project_for_conversation,
_get_workspace_display_name,
_infer_invalid_workspace_aliases,
_infer_workspace_name_from_context,
)
def list_workspace_projects(workspace_path: str, rules: list) -> list[dict]:
"""Return the sorted project list that GET /api/workspaces renders."""
workspace_entries = _collect_workspace_entries(workspace_path)
invalid_workspace_ids = _collect_invalid_workspace_ids(workspace_entries)
project_name_map = _create_project_name_to_workspace_id_map(workspace_entries)
workspace_path_map = _create_workspace_path_to_id_map(workspace_entries)
composer_id_to_ws = _build_composer_id_to_workspace_id(workspace_path, workspace_entries)
conversation_map: dict[str, list] = {}
# closing semantics now baked into the context manager (issue #17).
with _open_global_db(workspace_path) as (global_db, _):
if global_db:
def _safe_fetchall(query: str, params: tuple = ()) -> list:
try:
return global_db.execute(query, params).fetchall()
except sqlite3.Error:
return []
try:
composer_rows = _safe_fetchall(
"SELECT key, value FROM cursorDiskKV WHERE key LIKE 'composerData:%' AND LENGTH(value) > 10"
)
project_layouts_map: dict[str, list] = load_project_layouts_map(global_db)
bubble_map: dict[str, dict] = load_bubble_map(global_db)
invalid_workspace_aliases = _infer_invalid_workspace_aliases(
composer_rows=composer_rows,
project_layouts_map=project_layouts_map,
project_name_map=project_name_map,
workspace_path_map=workspace_path_map,
workspace_entries=workspace_entries,
bubble_map=bubble_map,
composer_id_to_ws=composer_id_to_ws,
invalid_workspace_ids=invalid_workspace_ids,
)
for row in composer_rows:
cid = row["key"].split(":")[1]
try:
parsed = json.loads(row["value"])
except (json.JSONDecodeError, TypeError, ValueError) as e:
_logger.warning(
"Failed to decode Composer from composerData:%s: %s",
cid,
e,
)
continue
if not isinstance(parsed, dict):
_logger.warning(
"Failed to parse Composer from composerData:%s: expected object, got %s",
cid,
type(parsed).__name__,
)
continue
try:
composer = Composer.from_dict(parsed, composer_id=cid)
except SchemaError as e:
_logger.warning(
"Failed to parse Composer from composerData:%s: %s",
cid,
e,
)
continue
cd = composer.raw
try:
pid = _determine_project_for_conversation(
cd, cid, project_layouts_map,
project_name_map, workspace_path_map,
workspace_entries, bubble_map, composer_id_to_ws, invalid_workspace_ids,
)
mapped_ws = composer_id_to_ws.get(cid)
if not pid and mapped_ws in invalid_workspace_ids:
pid = invalid_workspace_aliases.get(mapped_ws)
assigned = pid if pid else "global"
headers = cd.get("fullConversationHeadersOnly") or []
has_bubbles = any(
bubble_map.get(h.get("bubbleId"))
for h in headers
if isinstance(h, dict)
)
if not has_bubbles:
continue
conversation_map.setdefault(assigned, []).append({
"composerId": cid,
"name": cd.get("name") or f"Conversation {cid[:8]}",
"lastUpdatedAt": to_epoch_ms(cd.get("lastUpdatedAt")) or to_epoch_ms(cd.get("createdAt")) or 0,
"createdAt": to_epoch_ms(cd.get("createdAt")) or 0,
})
except Exception as e:
_logger.warning(
"Failed to process Composer from composerData:%s: %s",
cid,
e,
)
except Exception as e:
_logger.error(
"Failed to load composer rows from global storage: %s",
e,
exc_info=True,
)
# Group workspace entries by normalized folder path
folder_to_entries: dict[str, list] = {}
entry_folder_map: dict[str, str] = {}
for entry in workspace_entries:
norm_folder = ""
try:
wd = read_json_file(entry["workspaceJsonPath"])
folders = get_workspace_folder_paths(wd)
first_folder = folders[0] if folders else None
if first_folder:
norm_folder = normalize_file_path(first_folder)
except Exception as e:
warn_workspace_json_read(_logger, entry["name"], e)
if not norm_folder:
norm_folder = entry["name"] # fallback to workspace ID
entry_folder_map[entry["name"]] = norm_folder
folder_to_entries.setdefault(norm_folder, []).append(entry)
projects: list[dict] = []
seen_folders: set = set()
for entry in workspace_entries:
norm_folder = entry_folder_map[entry["name"]]
if norm_folder in seen_folders:
continue
seen_folders.add(norm_folder)
group = folder_to_entries[norm_folder]
primary = group[0]
all_ws_ids = [e["name"] for e in group]
try:
mtime = max(
os.path.getmtime(os.path.join(workspace_path, e["name"], "state.vscdb"))
for e in group
if os.path.isfile(os.path.join(workspace_path, e["name"], "state.vscdb"))
)
except Exception as e:
_logger.warning(
"Failed to resolve mtime for workspace folder %s: %s",
norm_folder,
e,
)
mtime = 0
workspace_name = _get_workspace_display_name(workspace_path, primary["name"])
if workspace_name == primary["name"]:
inferred = _infer_workspace_name_from_context(workspace_path, primary["name"])
workspace_name = inferred or f"Project {primary['name'][:8]}"
if is_excluded_by_rules(rules, workspace_name):
continue
convos = []
for ws_id in all_ws_ids:
for c in conversation_map.get(ws_id, []):
searchable = build_searchable_text(
project_name=workspace_name,
chat_title=c.get("name"),
)
if not is_excluded_by_rules(rules, searchable):
convos.append(c)
if not convos:
continue
projects.append({
"id": primary["name"],
"name": workspace_name,
"path": primary["workspaceJsonPath"],
"conversationCount": len(convos),
"lastModified": datetime.fromtimestamp(mtime, tz=timezone.utc).isoformat(),
**({"aliasIds": all_ws_ids} if len(all_ws_ids) > 1 else {}),
})
# Global (unmatched) conversations
global_convos = [
c for c in conversation_map.get("global", [])
if not is_excluded_by_rules(
rules,
build_searchable_text(project_name="Other chats", chat_title=c.get("name")),
)
]
if global_convos:
last_updated = max((c.get("lastUpdatedAt") or 0 for c in global_convos), default=0)
projects.append({
"id": "global",
"name": "Other chats",
"conversationCount": len(global_convos),
"lastModified": (
datetime.fromtimestamp(last_updated / 1000, tz=timezone.utc).isoformat()
if last_updated > 0
else datetime.now(tz=timezone.utc).isoformat()
),
})
# Cursor CLI projects
try:
cli_projects = list_cli_projects(get_cli_chats_path())
for cp in cli_projects:
if not isinstance(cp, dict):
continue
project_id = cp.get("project_id")
if not isinstance(project_id, str) or not project_id:
continue
ws_name = cp.get("workspace_name") or project_id[:12]
if is_excluded_by_rules(rules, ws_name):
continue
sessions = cp.get("sessions") or []
if not isinstance(sessions, list):
continue
cli_convos = []
for s in sessions:
if not isinstance(s, dict):
continue
session_id = s.get("session_id")
if not session_id:
continue
meta = s.get("meta") or {}
session_name = meta.get("name") or f"Session {session_id[:8]}"
searchable = build_searchable_text(
project_name=ws_name,
chat_title=session_name,
)
if not is_excluded_by_rules(rules, searchable):
cli_convos.append(session_name)
if not cli_convos:
continue
last_ms = cp.get("last_updated_ms")
projects.append({
"id": f"cli:{project_id}",
"name": ws_name,
"conversationCount": len(cli_convos),
"lastModified": (
datetime.fromtimestamp(last_ms / 1000, tz=timezone.utc).isoformat()
if last_ms
else datetime.now(tz=timezone.utc).isoformat()
),
"source": "cli",
})
except Exception as e:
_logger.warning("Failed to load CLI projects: %s", e)
projects.sort(key=lambda p: p["lastModified"], reverse=True)
return projects