-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc_index.py
More file actions
218 lines (178 loc) · 6.63 KB
/
doc_index.py
File metadata and controls
218 lines (178 loc) · 6.63 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
import json
import re
import tempfile
import time
from dataclasses import dataclass
from pathlib import Path
from aider.dap_paths import legacy_project_state_dir, project_state_dir
@dataclass(frozen=True)
class DocSearchResult:
rel_path: str
score: int
line: int | None
snippet: str
class DocIndex:
VERSION = 1
def __init__(self, root, *, encoding="utf-8"):
self.root = Path(root)
self.encoding = encoding
self.preferred_store_dir = project_state_dir(self.root)
self.legacy_store_dir = legacy_project_state_dir(self.root)
preferred = self.preferred_store_dir / "doc_index.json"
legacy = self.legacy_store_dir / "doc_index.json"
self.index_path = preferred if preferred.exists() else (legacy if legacy.exists() else preferred)
def rebuild(self):
docs = {}
for rel_path in self._iter_doc_rel_paths():
abs_path = self.root / rel_path
rec = self._read_doc(abs_path)
if rec is not None:
docs[rel_path] = rec
payload = {
"version": self.VERSION,
"built_at": int(time.time()),
"docs": docs,
}
self._write_index(payload)
return len(docs)
def update(self):
payload = self._load_index_payload()
existing = payload.get("docs", {}) if payload else {}
current_paths = set(self._iter_doc_rel_paths())
next_docs = {}
for rel_path in sorted(current_paths):
abs_path = self.root / rel_path
try:
stat = abs_path.stat()
except OSError:
continue
prev = existing.get(rel_path)
if prev and prev.get("mtime_ns") == stat.st_mtime_ns and prev.get("size") == stat.st_size:
next_docs[rel_path] = prev
continue
rec = self._read_doc(abs_path)
if rec is not None:
next_docs[rel_path] = rec
payload = {
"version": self.VERSION,
"built_at": int(time.time()),
"docs": next_docs,
}
self._write_index(payload)
return {"indexed": len(next_docs), "removed": len(set(existing) - current_paths)}
def status(self):
payload = self._load_index_payload()
if not payload:
return {"exists": False, "path": str(self.index_path)}
return {
"exists": True,
"path": str(self.index_path),
"built_at": payload.get("built_at"),
"docs": len(payload.get("docs", {}) or {}),
}
def search(self, query, *, topk=5, context_lines=2):
query = (query or "").strip()
if not query:
return []
payload = self._load_index_payload()
if not payload:
self.rebuild()
payload = self._load_index_payload() or {}
docs = payload.get("docs", {}) or {}
q_tokens = [t for t in re.split(r"\s+", query.casefold()) if t]
if not q_tokens:
return []
scored = []
for rel_path, rec in docs.items():
text = (rec.get("text") or "").casefold()
if not text:
continue
score = 0
for tok in q_tokens:
score += text.count(tok)
score += 2 * rel_path.casefold().count(tok)
score += 2 * text.count(query.casefold())
if score <= 0:
continue
line, snippet = self._make_snippet(rec.get("text") or "", query, q_tokens, context_lines)
scored.append(DocSearchResult(rel_path=rel_path, score=score, line=line, snippet=snippet))
scored.sort(key=lambda r: (-r.score, r.rel_path))
return scored[: max(1, int(topk))]
def _iter_doc_rel_paths(self):
paths = []
overview = self.root / "PROJECT_OVERVIEW.md"
if overview.is_file():
paths.append("PROJECT_OVERVIEW.md")
for abs_path in self.root.rglob("AI_ARCH.md"):
try:
rel = abs_path.relative_to(self.root).as_posix()
except ValueError:
continue
if rel.startswith("DAP/") or rel.startswith(".aider/") or rel.startswith(".git/"):
continue
paths.append(rel)
return sorted(set(paths))
def _read_doc(self, abs_path):
try:
text = abs_path.read_text(encoding=self.encoding, errors="replace")
stat = abs_path.stat()
except OSError:
return None
return {"mtime_ns": stat.st_mtime_ns, "size": stat.st_size, "text": text}
def _load_index_payload(self):
try:
raw = self.index_path.read_text(encoding="utf-8")
except OSError:
return None
try:
payload = json.loads(raw)
except Exception:
return None
if payload.get("version") != self.VERSION:
return None
if not isinstance(payload.get("docs"), dict):
return None
return payload
def _write_index(self, payload):
self.preferred_store_dir.mkdir(parents=True, exist_ok=True)
tmp_path = None
try:
with tempfile.NamedTemporaryFile(
"w",
encoding="utf-8",
delete=False,
dir=str(self.preferred_store_dir),
prefix="doc_index_",
suffix=".json",
) as f:
json.dump(payload, f, ensure_ascii=False)
f.write("\n")
tmp_path = Path(f.name)
tmp_path.replace(self.preferred_store_dir / "doc_index.json")
self.index_path = self.preferred_store_dir / "doc_index.json"
finally:
if tmp_path and tmp_path.exists() and tmp_path != self.index_path:
try:
tmp_path.unlink()
except OSError:
pass
def _make_snippet(self, text, query, q_tokens, context_lines):
lines = text.splitlines()
if not lines:
return None, ""
q_cf = query.casefold()
first_line = None
for i, line in enumerate(lines):
line_cf = line.casefold()
if q_cf in line_cf:
first_line = i
break
if any(tok in line_cf for tok in q_tokens):
first_line = i
break
if first_line is None:
first_line = 0
start = max(0, first_line - int(context_lines))
end = min(len(lines), first_line + int(context_lines) + 1)
snippet = "\n".join(lines[start:end]).rstrip()
return first_line + 1, snippet