forked from cocoindex-io/cocoindex-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.py
More file actions
322 lines (283 loc) · 11.3 KB
/
project.py
File metadata and controls
322 lines (283 loc) · 11.3 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
"""Project management: wraps a CocoIndex Environment + App."""
from __future__ import annotations
import asyncio
import sqlite3
from collections.abc import AsyncIterator, Callable
from pathlib import Path
from typing import Any
import cocoindex as coco
from cocoindex.connectors import sqlite as coco_sqlite
from .chunking import CHUNKER_REGISTRY, ChunkerFn
from .indexer import indexer_main
from .protocol import (
IndexingProgress,
IndexProgressUpdate,
IndexResponse,
IndexStreamResponse,
IndexWaitingNotice,
ProjectStatusResponse,
SearchResult,
)
from .query import query_codebase
from .settings import (
cocoindex_db_path as _cocoindex_db_path,
)
from .settings import (
resolve_db_dir,
)
from .settings import (
target_sqlite_db_path as _target_sqlite_db_path,
)
from .shared import (
CODEBASE_DIR,
EMBEDDER,
INDEXING_EMBED_PARAMS,
QUERY_EMBED_PARAMS,
SQLITE_DB,
Embedder,
)
class Project:
_env: coco.Environment
_app: coco.App[[], None]
_project_root: Path
_index_lock: asyncio.Lock
_initial_index_done: asyncio.Event
_indexing_stats: IndexingProgress | None = None
def close(self) -> None:
"""Close project resources to release file handles (LMDB, SQLite)."""
try:
db = self._env.get_context(SQLITE_DB)
db.close()
except Exception:
pass
# ------------------------------------------------------------------
# Indexing
# ------------------------------------------------------------------
async def run_index(
self,
on_progress: Callable[[IndexingProgress], None] | None = None,
on_started: asyncio.Event | None = None,
) -> None:
"""Acquire the index lock, run indexing, and release.
If *on_started* is provided, it is set once the lock is acquired
(i.e. indexing has truly begun). On completion (success or failure)
``_initial_index_done`` is set.
"""
async with self._index_lock:
self._indexing_stats = IndexingProgress(
num_execution_starts=0,
num_unchanged=0,
num_adds=0,
num_deletes=0,
num_reprocesses=0,
num_errors=0,
)
if on_started is not None:
on_started.set()
await self._run_index_inner(on_progress=on_progress)
async def _run_index_inner(
self,
on_progress: Callable[[IndexingProgress], None] | None = None,
) -> None:
"""Run indexing (lock must already be held)."""
try:
handle = self._app.update()
async for snapshot in handle.watch():
file_stats = snapshot.stats.by_component.get("process_file")
if file_stats is not None:
progress = IndexingProgress(
num_execution_starts=file_stats.num_execution_starts,
num_unchanged=file_stats.num_unchanged,
num_adds=file_stats.num_adds,
num_deletes=file_stats.num_deletes,
num_reprocesses=file_stats.num_reprocesses,
num_errors=file_stats.num_errors,
)
self._indexing_stats = progress
if on_progress is not None:
on_progress(progress)
await asyncio.sleep(0.1)
finally:
self._initial_index_done.set()
self._indexing_stats = None
async def ensure_indexing_started(self) -> None:
"""Kick off background indexing and wait until it has actually started.
Returns once the indexing task holds the lock. Safe to call multiple
times — only the first call spawns a task; subsequent calls return
immediately.
"""
if self._initial_index_done.is_set() or self._index_lock.locked():
return
started = asyncio.Event()
asyncio.create_task(self.run_index(on_started=started))
await started.wait()
async def stream_index(self) -> AsyncIterator[IndexStreamResponse]:
"""Run indexing, streaming progress updates and a final IndexResponse.
If the lock is already held, yields ``IndexWaitingNotice`` first.
The actual indexing runs in a separate task so that client disconnects
(``GeneratorExit``) do not abort the indexing.
"""
if self._index_lock.locked():
yield IndexWaitingNotice()
progress_queue: asyncio.Queue[IndexingProgress] = asyncio.Queue()
index_task = asyncio.create_task(
self.run_index(on_progress=lambda p: progress_queue.put_nowait(p))
)
try:
while not index_task.done():
try:
progress = await asyncio.wait_for(progress_queue.get(), timeout=0.1)
yield IndexProgressUpdate(progress=progress)
except TimeoutError:
continue
while not progress_queue.empty():
yield IndexProgressUpdate(progress=progress_queue.get_nowait())
index_task.result()
yield IndexResponse(success=True)
except GeneratorExit:
return
except Exception as e:
yield IndexResponse(success=False, message=str(e))
# ------------------------------------------------------------------
# Search
# ------------------------------------------------------------------
@property
def should_wait_for_indexing(self) -> bool:
"""True if indexing has been started but not yet completed."""
return not self._initial_index_done.is_set()
async def wait_for_indexing_done(self) -> None:
"""Wait until initial indexing is complete and no indexing is running."""
await self._initial_index_done.wait()
if self._index_lock.locked():
async with self._index_lock:
pass
async def search(
self,
query: str,
languages: list[str] | None = None,
paths: list[str] | None = None,
repo_keys: list[str] | None = None,
limit: int = 5,
offset: int = 0,
) -> list[SearchResult]:
"""Search within this project."""
target_db = _target_sqlite_db_path(self._project_root)
results = await query_codebase(
query=query,
target_sqlite_db_path=target_db,
env=self._env,
limit=limit,
offset=offset,
languages=languages,
paths=paths,
repo_keys=repo_keys,
)
return [
SearchResult(
file_path=r.file_path,
repo_key=r.repo_key,
language=r.language,
content=r.content,
start_line=r.start_line,
end_line=r.end_line,
score=r.score,
)
for r in results
]
# ------------------------------------------------------------------
# Status
# ------------------------------------------------------------------
def get_status(self) -> ProjectStatusResponse:
"""Get index stats by querying the SQLite database."""
db = self._env.get_context(SQLITE_DB)
index_exists = True
try:
with db.readonly() as conn:
total_chunks = conn.execute("SELECT COUNT(*) FROM code_chunks_vec").fetchone()[0]
total_files = conn.execute(
"SELECT COUNT(DISTINCT file_path) FROM code_chunks_vec"
).fetchone()[0]
lang_rows = conn.execute(
"SELECT language, COUNT(*) as cnt FROM code_chunks_vec"
" GROUP BY language ORDER BY cnt DESC"
).fetchall()
except sqlite3.OperationalError:
index_exists = False
total_chunks = 0
total_files = 0
lang_rows = []
is_indexing = self._index_lock.locked()
progress = self._indexing_stats if is_indexing else None
return ProjectStatusResponse(
indexing=is_indexing,
total_chunks=total_chunks,
total_files=total_files,
languages={lang: cnt for lang, cnt in lang_rows},
progress=progress,
index_exists=index_exists,
)
# ------------------------------------------------------------------
# Properties
# ------------------------------------------------------------------
@property
def indexing_stats(self) -> IndexingProgress | None:
return self._indexing_stats
@property
def env(self) -> coco.Environment:
return self._env
# ------------------------------------------------------------------
# Factory
# ------------------------------------------------------------------
@staticmethod
async def create(
project_root: Path,
embedder: Embedder,
indexing_params: dict[str, Any],
query_params: dict[str, Any],
chunker_registry: dict[str, ChunkerFn] | None = None,
) -> Project:
"""Create a project with explicit embedder and per-call params.
Project-level settings and .gitignore are NOT cached here — the
indexer loads them fresh from disk on every run so that user edits
take effect without restarting the daemon.
Args:
project_root: Root directory of the codebase to index.
embedder: Embedding model instance.
indexing_params: Extra kwargs spread into ``embedder.embed()`` during
indexing (e.g. ``{"prompt_name": "passage"}``). Pass ``{}`` for
no extras.
query_params: Extra kwargs spread into ``embedder.embed()`` for the
query side.
chunker_registry: Optional mapping of file suffix (e.g. ``".toml"``)
to a ``ChunkerFn``. When a suffix matches, the registered
chunker is called instead of the built-in splitter.
"""
settings_dir = project_root / ".cocoindex_code"
settings_dir.mkdir(parents=True, exist_ok=True)
db_dir = resolve_db_dir(project_root)
db_dir.mkdir(parents=True, exist_ok=True)
cocoindex_db = _cocoindex_db_path(project_root)
target_sqlite_db = _target_sqlite_db_path(project_root)
settings = coco.Settings.from_env(cocoindex_db)
context = coco.ContextProvider()
context.provide(CODEBASE_DIR, project_root)
context.provide(SQLITE_DB, coco_sqlite.connect(str(target_sqlite_db), load_vec=True))
context.provide(EMBEDDER, embedder)
context.provide(INDEXING_EMBED_PARAMS, dict(indexing_params))
context.provide(QUERY_EMBED_PARAMS, dict(query_params))
context.provide(CHUNKER_REGISTRY, dict(chunker_registry) if chunker_registry else {})
env = coco.Environment(settings, context_provider=context)
app = coco.App(
coco.AppConfig(
name="CocoIndexCode",
environment=env,
),
indexer_main,
)
result = Project.__new__(Project)
result._env = env
result._app = app
result._project_root = project_root
result._index_lock = asyncio.Lock()
result._initial_index_done = asyncio.Event()
return result