forked from cocoindex-io/cocoindex-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindexer.py
More file actions
249 lines (211 loc) · 7.88 KB
/
indexer.py
File metadata and controls
249 lines (211 loc) · 7.88 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
"""CocoIndex app for indexing codebases."""
from __future__ import annotations
from collections.abc import Iterable
from pathlib import Path, PurePath
import cocoindex as coco
from cocoindex.connectors import localfs, sqlite
from cocoindex.connectors.sqlite import Vec0TableDef
from cocoindex.ops.text import RecursiveSplitter, detect_code_language
from cocoindex.resources.chunk import Chunk
from cocoindex.resources.file import FilePathMatcher, PatternFilePathMatcher
from cocoindex.resources.id import IdGenerator
from pathspec import GitIgnoreSpec
from .chunking import CHUNKER_REGISTRY
from .settings import load_gitignore_spec, load_project_settings
from .shared import (
CODEBASE_DIR,
EMBEDDER,
INDEXING_EMBED_PARAMS,
SQLITE_DB,
CodeChunk,
)
# Chunking configuration
CHUNK_SIZE = 1000
MIN_CHUNK_SIZE = 250
CHUNK_OVERLAP = 150
# Chunking splitter (stateless, can be module-level)
splitter = RecursiveSplitter()
def repo_key_for_path(file_path: PurePath, project_root: Path) -> str:
"""Return the relative Git repo root for fast scoped search."""
directory = file_path.parent
while True:
if (project_root / directory / ".git").exists():
repo_key = directory.as_posix()
return repo_key if repo_key != "." else "."
if directory in (PurePath("."), PurePath("")):
break
directory = directory.parent
parts = file_path.parts
return parts[0] if len(parts) > 1 else "."
def _normalize_gitignore_lines(lines: Iterable[str], directory: PurePath) -> list[str]:
"""Normalize .gitignore lines to root-relative gitignore patterns."""
if directory in (PurePath("."), PurePath("")):
prefix = ""
else:
prefix = f"{directory.as_posix().rstrip('/')}/"
normalized: list[str] = []
for raw_line in lines:
line = raw_line.rstrip("\n\r")
if not line:
continue
stripped = line.lstrip()
if not stripped or stripped.startswith("#"):
continue
if line.startswith("\\#") or line.startswith("\\!"):
line = line[1:]
negated = line.startswith("!")
if negated:
line = line[1:]
body = line.strip()
if not body:
continue
anchor = body.startswith("/")
if anchor:
body = body.lstrip("/")
pattern = f"{prefix}{body}" if prefix else body
else:
contains_slash = "/" in body
base = prefix
if contains_slash:
pattern = f"{base}{body}"
else:
if base:
pattern = f"{base}**/{body}"
else:
pattern = f"**/{body}"
if negated:
pattern = f"!{pattern}"
normalized.append(pattern)
return normalized
class GitignoreAwareMatcher(FilePathMatcher):
"""Wraps another matcher and applies .gitignore filtering."""
def __init__(
self,
delegate: FilePathMatcher,
root_spec: GitIgnoreSpec | None,
project_root: Path,
) -> None:
self._delegate = delegate
self._root = project_root
self._spec_cache: dict[PurePath, GitIgnoreSpec | None] = {PurePath("."): root_spec}
def _spec_for(self, directory: PurePath) -> GitIgnoreSpec | None:
if directory in self._spec_cache:
return self._spec_cache[directory]
parent_dir = directory.parent if directory != PurePath(".") else PurePath(".")
parent_spec = self._spec_for(parent_dir)
spec = parent_spec
gitignore_path = (self._root / directory) / ".gitignore"
if gitignore_path.is_file():
try:
lines = gitignore_path.read_text().splitlines()
except (OSError, UnicodeDecodeError):
lines = []
normalized = _normalize_gitignore_lines(lines, directory)
if normalized:
new_spec = GitIgnoreSpec.from_lines(normalized)
spec = new_spec if spec is None else spec + new_spec
self._spec_cache[directory] = spec
return spec
def _is_ignored(self, path: PurePath, is_dir: bool) -> bool:
directory = path if is_dir else path.parent
if directory == PurePath(""):
directory = PurePath(".")
spec = self._spec_for(directory)
if spec is None:
return False
match_path = path.as_posix()
if is_dir and not match_path.endswith("/"):
match_path = f"{match_path}/"
return spec.match_file(match_path)
def is_dir_included(self, path: PurePath) -> bool:
if self._is_ignored(path, True):
return False
return self._delegate.is_dir_included(path)
def is_file_included(self, path: PurePath) -> bool:
if self._is_ignored(path, False):
return False
return self._delegate.is_file_included(path)
@coco.fn(memo=True)
async def process_file(
file: localfs.File,
table: sqlite.TableTarget[CodeChunk],
) -> None:
"""Process a single file: chunk, embed, and store."""
embedder = coco.use_context(EMBEDDER)
indexing_params = coco.use_context(INDEXING_EMBED_PARAMS)
try:
content = await file.read_text()
except UnicodeDecodeError:
return
if not content.strip():
return
project_root = coco.use_context(CODEBASE_DIR)
suffix = file.file_path.path.suffix
repo_key = repo_key_for_path(file.file_path.path, project_root)
ps = load_project_settings(project_root)
ext_lang_map = {f".{lo.ext}": lo.lang for lo in ps.language_overrides}
language = (
ext_lang_map.get(suffix)
or detect_code_language(filename=file.file_path.path.name)
or "text"
)
chunker_registry = coco.use_context(CHUNKER_REGISTRY)
chunker = chunker_registry.get(suffix)
if chunker is not None:
language_override, chunks = chunker(Path(file.file_path.path), content)
if language_override is not None:
language = language_override
else:
chunks = splitter.split(
content,
chunk_size=CHUNK_SIZE,
min_chunk_size=MIN_CHUNK_SIZE,
chunk_overlap=CHUNK_OVERLAP,
language=language,
)
id_gen = IdGenerator()
async def process(chunk: Chunk) -> None:
table.declare_row(
row=CodeChunk(
id=await id_gen.next_id(chunk.text),
file_path=file.file_path.path.as_posix(),
repo_key=repo_key,
language=language,
content=chunk.text,
start_line=chunk.start.line,
end_line=chunk.end.line,
embedding=await embedder.embed(chunk.text, **indexing_params),
)
)
await coco.map(process, chunks)
@coco.fn
async def indexer_main() -> None:
"""Main indexing function - walks files and processes each."""
project_root = coco.use_context(CODEBASE_DIR)
ps = load_project_settings(project_root)
gitignore_spec = load_gitignore_spec(project_root)
table = await sqlite.mount_table_target(
db=SQLITE_DB,
table_name="code_chunks_vec",
table_schema=await sqlite.TableSchema.from_class(
CodeChunk,
primary_key=["id"],
),
virtual_table_def=Vec0TableDef(
partition_key_columns=["repo_key", "language"],
auxiliary_columns=["file_path", "content", "start_line", "end_line"],
),
)
base_matcher = PatternFilePathMatcher(
included_patterns=ps.include_patterns,
excluded_patterns=ps.exclude_patterns,
)
matcher: FilePathMatcher = GitignoreAwareMatcher(base_matcher, gitignore_spec, project_root)
files = localfs.walk_dir(
CODEBASE_DIR,
recursive=True,
path_matcher=matcher,
)
await coco.mount_each(
coco.component_subpath(coco.Symbol("process_file")), process_file, files.items(), table
)