kodit 0.3.3__py3-none-any.whl → 0.3.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of kodit might be problematic. Click here for more details.
- kodit/_version.py +2 -2
- kodit/application/factories/code_indexing_factory.py +2 -24
- kodit/application/services/code_indexing_application_service.py +10 -2
- kodit/domain/services/index_service.py +25 -66
- kodit/domain/value_objects.py +10 -22
- kodit/infrastructure/slicing/__init__.py +1 -0
- kodit/infrastructure/slicing/language_detection_service.py +18 -0
- kodit/infrastructure/slicing/slicer.py +894 -0
- kodit/infrastructure/sqlalchemy/index_repository.py +29 -0
- kodit/migrations/versions/4073b33f9436_add_file_processing_flag.py +6 -4
- kodit/migrations/versions/4552eb3f23ce_add_summary.py +4 -4
- kodit/migrations/versions/7c3bbc2ab32b_add_embeddings_table.py +24 -16
- kodit/migrations/versions/85155663351e_initial.py +64 -48
- kodit/migrations/versions/c3f5137d30f5_index_all_the_things.py +20 -14
- {kodit-0.3.3.dist-info → kodit-0.3.4.dist-info}/METADATA +9 -4
- {kodit-0.3.3.dist-info → kodit-0.3.4.dist-info}/RECORD +19 -29
- kodit/infrastructure/snippet_extraction/__init__.py +0 -1
- kodit/infrastructure/snippet_extraction/factories.py +0 -13
- kodit/infrastructure/snippet_extraction/language_detection_service.py +0 -39
- kodit/infrastructure/snippet_extraction/languages/csharp.scm +0 -12
- kodit/infrastructure/snippet_extraction/languages/go.scm +0 -26
- kodit/infrastructure/snippet_extraction/languages/java.scm +0 -12
- kodit/infrastructure/snippet_extraction/languages/javascript.scm +0 -24
- kodit/infrastructure/snippet_extraction/languages/python.scm +0 -22
- kodit/infrastructure/snippet_extraction/languages/typescript.scm +0 -25
- kodit/infrastructure/snippet_extraction/snippet_extraction_factory.py +0 -67
- kodit/infrastructure/snippet_extraction/snippet_query_provider.py +0 -44
- kodit/infrastructure/snippet_extraction/tree_sitter_snippet_extractor.py +0 -182
- kodit/infrastructure/sqlalchemy/file_repository.py +0 -78
- {kodit-0.3.3.dist-info → kodit-0.3.4.dist-info}/WHEEL +0 -0
- {kodit-0.3.3.dist-info → kodit-0.3.4.dist-info}/entry_points.txt +0 -0
- {kodit-0.3.3.dist-info → kodit-0.3.4.dist-info}/licenses/LICENSE +0 -0
kodit/_version.py
CHANGED
|
@@ -13,7 +13,7 @@ from kodit.domain.services.index_query_service import IndexQueryService
|
|
|
13
13
|
from kodit.domain.services.index_service import (
|
|
14
14
|
IndexDomainService,
|
|
15
15
|
)
|
|
16
|
-
from kodit.domain.value_objects import LanguageMapping
|
|
16
|
+
from kodit.domain.value_objects import LanguageMapping
|
|
17
17
|
from kodit.infrastructure.bm25.bm25_factory import bm25_repository_factory
|
|
18
18
|
from kodit.infrastructure.embedding.embedding_factory import (
|
|
19
19
|
embedding_domain_service_factory,
|
|
@@ -31,15 +31,9 @@ from kodit.infrastructure.enrichment.null_enrichment_provider import (
|
|
|
31
31
|
NullEnrichmentProvider,
|
|
32
32
|
)
|
|
33
33
|
from kodit.infrastructure.indexing.fusion_service import ReciprocalRankFusionService
|
|
34
|
-
from kodit.infrastructure.
|
|
35
|
-
create_snippet_query_provider,
|
|
36
|
-
)
|
|
37
|
-
from kodit.infrastructure.snippet_extraction.language_detection_service import (
|
|
34
|
+
from kodit.infrastructure.slicing.language_detection_service import (
|
|
38
35
|
FileSystemLanguageDetectionService,
|
|
39
36
|
)
|
|
40
|
-
from kodit.infrastructure.snippet_extraction.tree_sitter_snippet_extractor import (
|
|
41
|
-
TreeSitterSnippetExtractor,
|
|
42
|
-
)
|
|
43
37
|
from kodit.infrastructure.sqlalchemy.embedding_repository import (
|
|
44
38
|
SqlAlchemyEmbeddingRepository,
|
|
45
39
|
)
|
|
@@ -63,17 +57,9 @@ def create_code_indexing_application_service(
|
|
|
63
57
|
|
|
64
58
|
# Create infrastructure services
|
|
65
59
|
language_detector = FileSystemLanguageDetectionService(language_map)
|
|
66
|
-
query_provider = create_snippet_query_provider()
|
|
67
60
|
|
|
68
|
-
# Create snippet extractors
|
|
69
|
-
method_extractor = TreeSitterSnippetExtractor(query_provider)
|
|
70
|
-
|
|
71
|
-
snippet_extractors = {
|
|
72
|
-
SnippetExtractionStrategy.METHOD_BASED: method_extractor,
|
|
73
|
-
}
|
|
74
61
|
index_domain_service = IndexDomainService(
|
|
75
62
|
language_detector=language_detector,
|
|
76
|
-
snippet_extractors=snippet_extractors,
|
|
77
63
|
enrichment_service=enrichment_service,
|
|
78
64
|
clone_dir=app_context.get_clone_dir(),
|
|
79
65
|
)
|
|
@@ -136,17 +122,9 @@ def create_fast_test_code_indexing_application_service(
|
|
|
136
122
|
|
|
137
123
|
# Create infrastructure services
|
|
138
124
|
language_detector = FileSystemLanguageDetectionService(language_map)
|
|
139
|
-
query_provider = create_snippet_query_provider()
|
|
140
|
-
|
|
141
|
-
# Create snippet extractors
|
|
142
|
-
method_extractor = TreeSitterSnippetExtractor(query_provider)
|
|
143
125
|
|
|
144
|
-
snippet_extractors = {
|
|
145
|
-
SnippetExtractionStrategy.METHOD_BASED: method_extractor,
|
|
146
|
-
}
|
|
147
126
|
index_domain_service = IndexDomainService(
|
|
148
127
|
language_detector=language_detector,
|
|
149
|
-
snippet_extractors=snippet_extractors,
|
|
150
128
|
enrichment_service=enrichment_service,
|
|
151
129
|
clone_dir=app_context.get_clone_dir(),
|
|
152
130
|
)
|
|
@@ -100,6 +100,11 @@ class CodeIndexingApplicationService:
|
|
|
100
100
|
self.log.info("No new changes to index", index_id=index.id)
|
|
101
101
|
return
|
|
102
102
|
|
|
103
|
+
# Delete the old snippets from the files that have changed
|
|
104
|
+
await self.index_repository.delete_snippets_by_file_ids(
|
|
105
|
+
[file.id for file in index.source.working_copy.changed_files() if file.id]
|
|
106
|
+
)
|
|
107
|
+
|
|
103
108
|
# Extract and create snippets (domain service handles progress)
|
|
104
109
|
self.log.info("Creating snippets for files", index_id=index.id)
|
|
105
110
|
index = await self.index_domain_service.extract_snippets_from_index(
|
|
@@ -115,6 +120,9 @@ class CodeIndexingApplicationService:
|
|
|
115
120
|
msg = f"Index {index.id} not found after snippet extraction"
|
|
116
121
|
raise ValueError(msg)
|
|
117
122
|
index = flushed_index
|
|
123
|
+
if len(index.snippets) == 0:
|
|
124
|
+
self.log.info("No snippets to index after extraction", index_id=index.id)
|
|
125
|
+
return
|
|
118
126
|
|
|
119
127
|
# Create BM25 index
|
|
120
128
|
self.log.info("Creating keyword index")
|
|
@@ -154,8 +162,8 @@ class CodeIndexingApplicationService:
|
|
|
154
162
|
# Apply filters if provided
|
|
155
163
|
filtered_snippet_ids: list[int] | None = None
|
|
156
164
|
if request.filters:
|
|
157
|
-
# Use domain service for filtering
|
|
158
|
-
prefilter_request = replace(request, top_k=
|
|
165
|
+
# Use domain service for filtering (use large top_k for pre-filtering)
|
|
166
|
+
prefilter_request = replace(request, top_k=10000)
|
|
159
167
|
snippet_results = await self.index_query_service.search_snippets(
|
|
160
168
|
prefilter_request
|
|
161
169
|
)
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"""Pure domain service for Index aggregate operations."""
|
|
2
2
|
|
|
3
3
|
from abc import ABC, abstractmethod
|
|
4
|
-
from collections.abc import Mapping
|
|
5
4
|
from pathlib import Path
|
|
6
5
|
|
|
7
6
|
import structlog
|
|
@@ -13,14 +12,13 @@ from kodit.domain.services.enrichment_service import EnrichmentDomainService
|
|
|
13
12
|
from kodit.domain.value_objects import (
|
|
14
13
|
EnrichmentIndexRequest,
|
|
15
14
|
EnrichmentRequest,
|
|
16
|
-
|
|
17
|
-
SnippetExtractionResult,
|
|
18
|
-
SnippetExtractionStrategy,
|
|
15
|
+
LanguageMapping,
|
|
19
16
|
)
|
|
20
17
|
from kodit.infrastructure.cloning.git.working_copy import GitWorkingCopyProvider
|
|
21
18
|
from kodit.infrastructure.cloning.metadata import FileMetadataExtractor
|
|
22
19
|
from kodit.infrastructure.git.git_utils import is_valid_clone_target
|
|
23
20
|
from kodit.infrastructure.ignore.ignore_pattern_provider import GitIgnorePatternProvider
|
|
21
|
+
from kodit.infrastructure.slicing.slicer import Slicer
|
|
24
22
|
from kodit.reporting import Reporter
|
|
25
23
|
from kodit.utils.path_utils import path_from_uri
|
|
26
24
|
|
|
@@ -33,14 +31,6 @@ class LanguageDetectionService(ABC):
|
|
|
33
31
|
"""Detect the programming language of a file."""
|
|
34
32
|
|
|
35
33
|
|
|
36
|
-
class SnippetExtractor(ABC):
|
|
37
|
-
"""Abstract interface for snippet extraction."""
|
|
38
|
-
|
|
39
|
-
@abstractmethod
|
|
40
|
-
async def extract(self, file_path: Path, language: str) -> list[str]:
|
|
41
|
-
"""Extract snippets from a file."""
|
|
42
|
-
|
|
43
|
-
|
|
44
34
|
class IndexDomainService:
|
|
45
35
|
"""Pure domain service for Index aggregate operations.
|
|
46
36
|
|
|
@@ -54,14 +44,12 @@ class IndexDomainService:
|
|
|
54
44
|
def __init__(
|
|
55
45
|
self,
|
|
56
46
|
language_detector: LanguageDetectionService,
|
|
57
|
-
snippet_extractors: Mapping[SnippetExtractionStrategy, SnippetExtractor],
|
|
58
47
|
enrichment_service: EnrichmentDomainService,
|
|
59
48
|
clone_dir: Path,
|
|
60
49
|
) -> None:
|
|
61
50
|
"""Initialize the index domain service."""
|
|
62
51
|
self._clone_dir = clone_dir
|
|
63
52
|
self._language_detector = language_detector
|
|
64
|
-
self._snippet_extractors = snippet_extractors
|
|
65
53
|
self._enrichment_service = enrichment_service
|
|
66
54
|
self.log = structlog.get_logger(__name__)
|
|
67
55
|
|
|
@@ -99,7 +87,6 @@ class IndexDomainService:
|
|
|
99
87
|
async def extract_snippets_from_index(
|
|
100
88
|
self,
|
|
101
89
|
index: domain_entities.Index,
|
|
102
|
-
strategy: SnippetExtractionStrategy = SnippetExtractionStrategy.METHOD_BASED,
|
|
103
90
|
progress_callback: ProgressCallback | None = None,
|
|
104
91
|
) -> domain_entities.Index:
|
|
105
92
|
"""Extract code snippets from files in the index."""
|
|
@@ -109,46 +96,40 @@ class IndexDomainService:
|
|
|
109
96
|
"Extracting snippets",
|
|
110
97
|
index_id=index.id,
|
|
111
98
|
file_count=file_count,
|
|
112
|
-
strategy=strategy.value,
|
|
113
99
|
)
|
|
114
100
|
|
|
115
101
|
# Only create snippets for files that have been added or modified
|
|
116
102
|
files = index.source.working_copy.changed_files()
|
|
117
103
|
index.delete_snippets_for_files(files)
|
|
118
104
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
new_snippets = []
|
|
125
|
-
for i, domain_file in enumerate(files, 1):
|
|
105
|
+
# Create a set of languages to extract snippets for
|
|
106
|
+
extensions = {file.extension() for file in files}
|
|
107
|
+
languages = []
|
|
108
|
+
for ext in extensions:
|
|
126
109
|
try:
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
)
|
|
131
|
-
result = await self._extract_snippets(request)
|
|
132
|
-
for snippet_text in result.snippets:
|
|
133
|
-
snippet = domain_entities.Snippet(
|
|
134
|
-
derives_from=[domain_file],
|
|
135
|
-
)
|
|
136
|
-
snippet.add_original_content(snippet_text, result.language)
|
|
137
|
-
new_snippets.append(snippet)
|
|
138
|
-
|
|
139
|
-
except (OSError, ValueError) as e:
|
|
140
|
-
self.log.debug(
|
|
141
|
-
"Skipping file for snippet extraction",
|
|
142
|
-
file_uri=str(domain_file.uri),
|
|
143
|
-
error=str(e),
|
|
144
|
-
)
|
|
110
|
+
languages.append(LanguageMapping.get_language_for_extension(ext))
|
|
111
|
+
except ValueError as e:
|
|
112
|
+
self.log.info("Skipping", error=str(e))
|
|
145
113
|
continue
|
|
146
114
|
|
|
115
|
+
reporter = Reporter(self.log, progress_callback)
|
|
116
|
+
await reporter.start(
|
|
117
|
+
"extract_snippets",
|
|
118
|
+
len(files) * len(languages),
|
|
119
|
+
"Extracting code snippets...",
|
|
120
|
+
)
|
|
121
|
+
# Calculate snippets for each language
|
|
122
|
+
slicer = Slicer()
|
|
123
|
+
for i, language in enumerate(languages):
|
|
147
124
|
await reporter.step(
|
|
148
|
-
"extract_snippets",
|
|
125
|
+
"extract_snippets",
|
|
126
|
+
len(files) * (i + 1),
|
|
127
|
+
len(files) * len(languages),
|
|
128
|
+
"Extracting code snippets...",
|
|
149
129
|
)
|
|
130
|
+
s = slicer.extract_snippets(files, language=language)
|
|
131
|
+
index.snippets.extend(s)
|
|
150
132
|
|
|
151
|
-
index.snippets.extend(new_snippets)
|
|
152
133
|
await reporter.done("extract_snippets")
|
|
153
134
|
return index
|
|
154
135
|
|
|
@@ -187,28 +168,6 @@ class IndexDomainService:
|
|
|
187
168
|
await reporter.done("enrichment")
|
|
188
169
|
return list(snippet_map.values())
|
|
189
170
|
|
|
190
|
-
async def _extract_snippets(
|
|
191
|
-
self, request: SnippetExtractionRequest
|
|
192
|
-
) -> SnippetExtractionResult:
|
|
193
|
-
# Domain logic: validate file exists
|
|
194
|
-
if not request.file_path.exists():
|
|
195
|
-
raise ValueError(f"File does not exist: {request.file_path}")
|
|
196
|
-
|
|
197
|
-
# Domain logic: detect language
|
|
198
|
-
language = await self._language_detector.detect_language(request.file_path)
|
|
199
|
-
|
|
200
|
-
# Domain logic: choose strategy and extractor
|
|
201
|
-
if request.strategy not in self._snippet_extractors:
|
|
202
|
-
raise ValueError(f"Unsupported extraction strategy: {request.strategy}")
|
|
203
|
-
|
|
204
|
-
extractor = self._snippet_extractors[request.strategy]
|
|
205
|
-
snippets = await extractor.extract(request.file_path, language)
|
|
206
|
-
|
|
207
|
-
# Domain logic: filter out empty snippets
|
|
208
|
-
filtered_snippets = [snippet for snippet in snippets if snippet.strip()]
|
|
209
|
-
|
|
210
|
-
return SnippetExtractionResult(snippets=filtered_snippets, language=language)
|
|
211
|
-
|
|
212
171
|
def sanitize_uri(
|
|
213
172
|
self, uri_or_path_like: str
|
|
214
173
|
) -> tuple[AnyUrl, domain_entities.SourceType]:
|
|
@@ -297,7 +256,7 @@ class IndexDomainService:
|
|
|
297
256
|
await metadata_extractor.extract(file_path=file_path)
|
|
298
257
|
)
|
|
299
258
|
except (OSError, ValueError) as e:
|
|
300
|
-
self.log.
|
|
259
|
+
self.log.debug("Skipping file", file=str(file_path), error=str(e))
|
|
301
260
|
continue
|
|
302
261
|
|
|
303
262
|
# Finally check if there are any modified files
|
kodit/domain/value_objects.py
CHANGED
|
@@ -134,14 +134,6 @@ class SearchType(Enum):
|
|
|
134
134
|
HYBRID = "hybrid"
|
|
135
135
|
|
|
136
136
|
|
|
137
|
-
@dataclass
|
|
138
|
-
class SnippetExtractionResult:
|
|
139
|
-
"""Domain model for snippet extraction result."""
|
|
140
|
-
|
|
141
|
-
snippets: list[str]
|
|
142
|
-
language: str
|
|
143
|
-
|
|
144
|
-
|
|
145
137
|
@dataclass
|
|
146
138
|
class Document:
|
|
147
139
|
"""Generic document model for indexing."""
|
|
@@ -640,20 +632,6 @@ class SnippetQuery(BaseModel):
|
|
|
640
632
|
top_k: int = 10
|
|
641
633
|
|
|
642
634
|
|
|
643
|
-
class SnippetExtractionStrategy(str, Enum):
|
|
644
|
-
"""Different strategies for extracting snippets from files."""
|
|
645
|
-
|
|
646
|
-
METHOD_BASED = "method_based"
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
@dataclass
|
|
650
|
-
class SnippetExtractionRequest:
|
|
651
|
-
"""Domain model for snippet extraction request."""
|
|
652
|
-
|
|
653
|
-
file_path: Path
|
|
654
|
-
strategy: SnippetExtractionStrategy = SnippetExtractionStrategy.METHOD_BASED
|
|
655
|
-
|
|
656
|
-
|
|
657
635
|
class FileProcessingStatus(IntEnum):
|
|
658
636
|
"""File processing status."""
|
|
659
637
|
|
|
@@ -661,3 +639,13 @@ class FileProcessingStatus(IntEnum):
|
|
|
661
639
|
ADDED = 1
|
|
662
640
|
MODIFIED = 2
|
|
663
641
|
DELETED = 3
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
@dataclass
|
|
645
|
+
class FunctionDefinition:
|
|
646
|
+
"""Cached function definition."""
|
|
647
|
+
|
|
648
|
+
name: str
|
|
649
|
+
qualified_name: str
|
|
650
|
+
start_byte: int
|
|
651
|
+
end_byte: int
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Slicing infrastructure module."""
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""Language detection service implementation."""
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from kodit.domain.services.index_service import LanguageDetectionService
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class FileSystemLanguageDetectionService(LanguageDetectionService):
|
|
9
|
+
"""Simple file extension based language detection service."""
|
|
10
|
+
|
|
11
|
+
def __init__(self, language_map: dict[str, str]) -> None:
|
|
12
|
+
"""Initialize with a mapping of extensions to languages."""
|
|
13
|
+
self._language_map = language_map
|
|
14
|
+
|
|
15
|
+
async def detect_language(self, file_path: Path) -> str:
|
|
16
|
+
"""Detect language based on file extension."""
|
|
17
|
+
extension = file_path.suffix.lstrip(".")
|
|
18
|
+
return self._language_map.get(extension, "unknown")
|