-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider_opencode.py
More file actions
451 lines (416 loc) · 18.4 KB
/
Copy pathprovider_opencode.py
File metadata and controls
451 lines (416 loc) · 18.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
"""OpenCode storage provider adapter.
OpenCode persists session/message/part records in a SQLite database
(``~/.local/share/opencode/opencode.db``). Earlier shipping builds also wrote
per-record JSON files under ``storage/{session,message,part}/`` which some
historical installs still carry; the adapter reads SQLite first and falls back
to the JSON layout when the DB is absent (or for sessions that only exist in
the legacy tree).
"""
from __future__ import annotations
from pathlib import Path
from typing import Dict, List, Optional, Tuple
import hashlib
import json
import logging
import sqlite3
import time
from provider_adapters import (
ProviderHealth,
ProviderParseResult,
ProviderSource,
ProviderWatchRoot,
build_source_id,
canonicalize_path,
normalize_timestamp,
)
logger = logging.getLogger("sessionflow.opencode")
class OpenCodeAdapter:
"""Adapter for OpenCode storage (legacy file tree plus the SQLite store)."""
provider = "opencode"
source_kind = "opencode_storage"
def __init__(
self,
storage_root: str | Path | None = None,
db_path: str | Path | None = None,
settled_seconds: int = 5,
):
"""Set the storage root, SQLite path, and settle window for new writes."""
# OpenCode keeps both the DB and the legacy filesystem tree under the
# same root, so callers normally only need to pass storage_root.
if storage_root is not None:
self.storage_root = Path(storage_root).expanduser()
else:
self.storage_root = Path.home() / ".local" / "share" / "opencode" / "storage"
# `opencode.db` lives one directory up from the legacy storage tree.
if db_path is not None:
self.db_path = Path(db_path).expanduser()
else:
self.db_path = self.storage_root.parent / "opencode.db"
self.settled_seconds = settled_seconds
# Per-pass cache populated by discover_sources(); parse_source() reuses
# message/part rows without re-querying the DB.
self._messages_by_session: Dict[str, List[Dict]] = {}
self._parts_by_message: Dict[str, List[Dict]] = {}
self._session_path_for: Dict[str, str] = {}
# Legacy JSON cache.
self._legacy_messages_by_session: Dict[str, List[Tuple[Path, Dict]]] = {}
self._legacy_parts_by_session: Dict[str, List[Tuple[Path, Dict]]] = {}
self._legacy_all_message_ids: set[str] = set()
self._legacy_all_parts: List[Tuple[Path, Dict]] = []
self._legacy_indexes_built = False
# ---- DB helpers --------------------------------------------------------
def _open_db(self) -> Optional[sqlite3.Connection]:
if not self.db_path.exists():
return None
# `uri=True` lets us open in read-only mode and avoid mutating WAL.
uri = f"{self.db_path.as_uri()}?mode=ro"
try:
conn = sqlite3.connect(uri, uri=True, timeout=5.0)
except sqlite3.Error:
return None
conn.row_factory = sqlite3.Row
return conn
def _refresh_db_indexes(self) -> List[ProviderSource]:
self._messages_by_session = {}
self._parts_by_message = {}
self._session_path_for = {}
conn = self._open_db()
if conn is None:
return []
sources: List[ProviderSource] = []
try:
# Newest-first so the per-run file limit (max_files_per_run)
# never clips today's session off the back of the list.
sessions = conn.execute(
"SELECT id, directory, title, time_created FROM session "
"ORDER BY time_created DESC"
).fetchall()
for row in sessions:
session_id = str(row["id"])
self._session_path_for[session_id] = str(self.db_path)
source_id = build_source_id(
self.provider, session_id, canonicalize_path(self.db_path)
)
directory = row["directory"] or "unknown"
sources.append(
ProviderSource(
provider=self.provider,
source_kind=self.source_kind,
source_class="native",
source_id=source_id,
logical_session_id=session_id,
path=str(self.db_path),
canonical_path=canonicalize_path(self.db_path),
project_root=directory,
timestamp=normalize_timestamp(row["time_created"]),
status="eligible",
)
)
for row in conn.execute(
"SELECT id, session_id, time_created, data FROM message ORDER BY session_id, time_created, id"
):
data = self._safe_json(row["data"])
if not data:
continue
data.setdefault("id", row["id"])
data.setdefault("sessionID", row["session_id"])
data.setdefault("__time_created_ms", row["time_created"])
self._messages_by_session.setdefault(str(row["session_id"]), []).append(data)
for row in conn.execute(
"SELECT id, message_id, session_id, time_created, data FROM part ORDER BY message_id, time_created, id"
):
data = self._safe_json(row["data"])
if not data:
continue
data.setdefault("id", row["id"])
data.setdefault("messageID", row["message_id"])
data.setdefault("sessionID", row["session_id"])
data.setdefault("__time_created_ms", row["time_created"])
self._parts_by_message.setdefault(str(row["message_id"]), []).append(data)
msg_count = sum(len(v) for v in self._messages_by_session.values())
part_count = sum(len(v) for v in self._parts_by_message.values())
if msg_count + part_count > 50_000:
logger.warning(
"_refresh_db_indexes loaded %d messages and %d parts into memory; "
"large OpenCode installs risk OOM — lazy per-session queries are recommended",
msg_count,
part_count,
)
finally:
conn.close()
return sources
@staticmethod
def _safe_json(text: object) -> Dict:
if not isinstance(text, str) or not text:
return {}
try:
return json.loads(text)
except json.JSONDecodeError:
return {}
# ---- Legacy JSON tree (older opencode installs) ------------------------
def _refresh_legacy_indexes(self) -> None:
self._legacy_messages_by_session = {}
self._legacy_parts_by_session = {}
self._legacy_all_message_ids = set()
self._legacy_all_parts = []
message_root = self.storage_root / "message"
if message_root.exists():
for path in sorted(message_root.rglob("*.json")):
data = self._load_json(path)
session_id = data.get("sessionID") or data.get("session_id")
if session_id:
self._legacy_messages_by_session.setdefault(str(session_id), []).append((path, data))
msg_id = data.get("id")
if msg_id:
self._legacy_all_message_ids.add(str(msg_id))
part_root = self.storage_root / "part"
if part_root.exists():
for path in sorted(part_root.rglob("*.json")):
data = self._load_json(path)
self._legacy_all_parts.append((path, data))
session_id = data.get("sessionID") or data.get("session_id")
if session_id:
self._legacy_parts_by_session.setdefault(str(session_id), []).append((path, data))
self._legacy_indexes_built = True
def _legacy_session_files(self) -> List[Path]:
root = self.storage_root / "session"
return sorted(root.rglob("*.json")) if root.exists() else []
def _load_json(self, path: Path) -> Dict:
try:
return json.loads(path.read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError):
return {}
# ---- Public API --------------------------------------------------------
def discover_sources(self) -> List[ProviderSource]:
"""Enumerate OpenCode sessions from the SQLite store and legacy tree."""
sources = self._refresh_db_indexes()
seen = {src.logical_session_id for src in sources}
# Legacy filesystem fallback: only surface sessions the DB doesn't
# already know about so we don't double-emit the same conversation.
self._refresh_legacy_indexes()
for path in self._legacy_session_files():
data = self._load_json(path)
logical_session_id = str(data.get("id") or path.stem)
if logical_session_id in seen:
continue
canonical_path = canonicalize_path(path)
source_id = build_source_id(self.provider, logical_session_id, canonical_path)
created = (
data.get("time", {}).get("created", "")
if isinstance(data.get("time"), dict)
else ""
)
sources.append(
ProviderSource(
provider=self.provider,
source_kind=self.source_kind,
source_class="native",
source_id=source_id,
logical_session_id=logical_session_id,
path=str(path),
canonical_path=canonical_path,
project_root=data.get("cwd") or data.get("directory") or "unknown",
timestamp=normalize_timestamp(created),
status="eligible",
)
)
seen.add(logical_session_id)
return sources
def parse_source(
self,
source: ProviderSource,
cursor: Optional[Dict],
) -> ProviderParseResult:
"""Parse an OpenCode session into normalized turns, resuming from ``cursor``."""
if source.path == str(self.db_path):
return self._parse_db_source(source, cursor)
return self._parse_legacy_source(source, cursor)
def _parse_db_source(
self,
source: ProviderSource,
cursor: Optional[Dict],
) -> ProviderParseResult:
# Refresh if discover wasn't called yet this pass (e.g. resumed cursor).
if source.logical_session_id not in self._messages_by_session and not self._session_path_for:
self._refresh_db_indexes()
messages = self._messages_by_session.get(source.logical_session_id, [])
emitted = set((cursor or {}).get("emitted_ids", []))
turns: List[Dict] = []
for index, message in enumerate(messages):
message_id = str(message.get("id", ""))
role = message.get("role", "")
parts = self._parts_by_message.get(message_id, [])
content_parts = [
part.get("text", "")
for part in parts
if part.get("type") in {"text", "message"} and part.get("text")
]
if not role or not content_parts:
continue
text = "\n".join(content_parts)
doc_hash = hashlib.sha256(
f"{source.logical_session_id}:{message_id}:{text}".encode("utf-8")
).hexdigest()[:16]
doc_id = f"opencode:{source.logical_session_id}:{doc_hash}"
if doc_id in emitted:
continue
emitted.add(doc_id)
raw_ts: object = ""
if isinstance(message.get("time"), dict):
tc = message["time"].get("created")
if tc is not None and tc != "":
raw_ts = tc
if raw_ts == "":
tc2 = message.get("__time_created_ms")
if tc2 is not None and tc2 != "":
raw_ts = tc2
turns.append({
"text": text,
"content": text,
"doc_id": doc_id,
"session_id": source.logical_session_id,
"logical_session_id": source.logical_session_id,
"provider": self.provider,
"source_kind": self.source_kind,
"source_class": "native",
"source_id": source.source_id,
"source_path": source.path,
"transcript_file": Path(source.path).name,
"turn_index": index,
"timestamp": normalize_timestamp(raw_ts) or source.timestamp,
"git_branch": "",
"chunk_type": role or "turn",
"project_root": source.project_root,
})
return ProviderParseResult(
source=source,
turns=turns,
cursor={
"cursor_type": "record_set",
"logical_session_id": source.logical_session_id,
"known_paths": [source.path],
"emitted_ids": sorted(emitted),
"project_root": source.project_root,
},
)
def _parse_legacy_source(
self,
source: ProviderSource,
cursor: Optional[Dict],
) -> ProviderParseResult:
if not self._legacy_indexes_built:
self._refresh_legacy_indexes()
messages = list(self._legacy_messages_by_session.get(source.logical_session_id, []))
parts = list(self._legacy_parts_by_session.get(source.logical_session_id, []))
paths = [Path(source.path)] + [p for p, _ in messages] + [p for p, _ in parts]
if not self._is_settled(paths):
source.status = "pending"
source.reason = "OpenCode records are still inside the settled window."
return ProviderParseResult(source=source, turns=[], cursor=cursor or {})
parts_by_message: Dict[str, List[Dict]] = {}
for _, part in parts:
message_id = part.get("messageID") or part.get("message_id")
if message_id:
parts_by_message.setdefault(str(message_id), []).append(part)
emitted = set((cursor or {}).get("emitted_ids", []))
turns = []
for index, (_, message) in enumerate(messages):
message_id = str(message.get("id", ""))
role = message.get("role", "")
content_parts = [
part.get("text", "")
for part in parts_by_message.get(message_id, [])
if part.get("type") in {"text", "message"} and part.get("text")
]
if not role or not content_parts:
continue
text = "\n".join(content_parts)
doc_hash = hashlib.sha256(
f"{source.logical_session_id}:{message_id}:{text}".encode("utf-8")
).hexdigest()[:16]
doc_id = f"opencode:{source.logical_session_id}:{doc_hash}"
if doc_id in emitted:
continue
emitted.add(doc_id)
timestamp = ""
if isinstance(message.get("time"), dict):
timestamp = message["time"].get("created", "")
turns.append({
"text": text,
"content": text,
"doc_id": doc_id,
"session_id": source.logical_session_id,
"logical_session_id": source.logical_session_id,
"provider": self.provider,
"source_kind": self.source_kind,
"source_class": "native",
"source_id": source.source_id,
"source_path": source.path,
"transcript_file": Path(source.path).name,
"turn_index": index,
"timestamp": normalize_timestamp(timestamp) or source.timestamp,
"git_branch": "",
"chunk_type": role or "turn",
"project_root": source.project_root,
})
return ProviderParseResult(
source=source,
turns=turns,
cursor={
"cursor_type": "record_set",
"logical_session_id": source.logical_session_id,
"known_paths": [str(p) for p in paths],
"emitted_ids": sorted(emitted),
"project_root": source.project_root,
},
)
def _is_settled(self, paths: List[Path]) -> bool:
if self.settled_seconds <= 0:
return True
now = time.time()
return all(now - path.stat().st_mtime >= self.settled_seconds for path in paths if path.exists())
def _legacy_orphan_part_count(self) -> int:
if not self._legacy_indexes_built:
self._refresh_legacy_indexes()
count = 0
for _, data in self._legacy_all_parts:
message_id = data.get("messageID") or data.get("message_id")
if message_id and str(message_id) not in self._legacy_all_message_ids:
count += 1
return count
def watch_roots(self) -> List[ProviderWatchRoot]:
"""Return the storage tree and (if present) the SQLite parent as watch roots."""
# Both the legacy tree and the SQLite WAL live under the same parent.
roots = [ProviderWatchRoot(self.provider, self.source_kind, str(self.storage_root), recursive=True)]
if self.db_path.exists():
roots.append(
ProviderWatchRoot(
self.provider,
self.source_kind,
str(self.db_path.parent),
recursive=False,
)
)
return roots
def health(self) -> ProviderHealth:
"""Report adapter health, including any legacy orphan-part count."""
sources = self.discover_sources()
orphan_parts = self._legacy_orphan_part_count()
if not self.db_path.exists() and not self.storage_root.exists():
status = "missing"
elif orphan_parts:
status = "warning"
else:
status = "ok"
limitations: List[str] = []
if orphan_parts:
limitations.append("Incomplete OpenCode part/message records are left pending.")
return ProviderHealth(
provider=self.provider,
status=status,
source_count=len(sources),
eligible_count=len(sources),
error_count=orphan_parts,
roots=[str(self.storage_root), str(self.db_path)],
limitations=limitations,
)