kodit 0.2.4__py3-none-any.whl → 0.2.6__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.

Files changed (118) hide show
  1. kodit/_version.py +2 -2
  2. kodit/application/__init__.py +1 -0
  3. kodit/application/commands/__init__.py +1 -0
  4. kodit/application/commands/snippet_commands.py +22 -0
  5. kodit/application/services/__init__.py +1 -0
  6. kodit/application/services/indexing_application_service.py +387 -0
  7. kodit/application/services/snippet_application_service.py +149 -0
  8. kodit/cli.py +118 -82
  9. kodit/database.py +0 -22
  10. kodit/domain/__init__.py +1 -0
  11. kodit/{source/source_models.py → domain/entities.py} +88 -19
  12. kodit/domain/enums.py +9 -0
  13. kodit/domain/errors.py +5 -0
  14. kodit/domain/interfaces.py +27 -0
  15. kodit/domain/repositories.py +95 -0
  16. kodit/domain/services/__init__.py +1 -0
  17. kodit/domain/services/bm25_service.py +124 -0
  18. kodit/domain/services/embedding_service.py +155 -0
  19. kodit/domain/services/enrichment_service.py +48 -0
  20. kodit/domain/services/ignore_service.py +45 -0
  21. kodit/domain/services/indexing_service.py +203 -0
  22. kodit/domain/services/snippet_extraction_service.py +89 -0
  23. kodit/domain/services/source_service.py +85 -0
  24. kodit/domain/value_objects.py +215 -0
  25. kodit/infrastructure/__init__.py +1 -0
  26. kodit/infrastructure/bm25/__init__.py +1 -0
  27. kodit/infrastructure/bm25/bm25_factory.py +28 -0
  28. kodit/{bm25/local_bm25.py → infrastructure/bm25/local_bm25_repository.py} +33 -22
  29. kodit/{bm25/vectorchord_bm25.py → infrastructure/bm25/vectorchord_bm25_repository.py} +40 -35
  30. kodit/infrastructure/cloning/__init__.py +1 -0
  31. kodit/infrastructure/cloning/folder/__init__.py +1 -0
  32. kodit/infrastructure/cloning/folder/factory.py +128 -0
  33. kodit/infrastructure/cloning/folder/working_copy.py +38 -0
  34. kodit/infrastructure/cloning/git/__init__.py +1 -0
  35. kodit/infrastructure/cloning/git/factory.py +147 -0
  36. kodit/infrastructure/cloning/git/working_copy.py +32 -0
  37. kodit/infrastructure/cloning/metadata.py +127 -0
  38. kodit/infrastructure/embedding/__init__.py +1 -0
  39. kodit/infrastructure/embedding/embedding_factory.py +87 -0
  40. kodit/infrastructure/embedding/embedding_providers/__init__.py +1 -0
  41. kodit/infrastructure/embedding/embedding_providers/batching.py +93 -0
  42. kodit/infrastructure/embedding/embedding_providers/hash_embedding_provider.py +79 -0
  43. kodit/infrastructure/embedding/embedding_providers/local_embedding_provider.py +129 -0
  44. kodit/infrastructure/embedding/embedding_providers/openai_embedding_provider.py +113 -0
  45. kodit/infrastructure/embedding/local_vector_search_repository.py +114 -0
  46. kodit/{embedding/vectorchord_vector_search_service.py → infrastructure/embedding/vectorchord_vector_search_repository.py} +65 -46
  47. kodit/infrastructure/enrichment/__init__.py +1 -0
  48. kodit/{enrichment → infrastructure/enrichment}/enrichment_factory.py +28 -12
  49. kodit/infrastructure/enrichment/legacy_enrichment_models.py +42 -0
  50. kodit/{enrichment/enrichment_provider → infrastructure/enrichment}/local_enrichment_provider.py +38 -26
  51. kodit/infrastructure/enrichment/null_enrichment_provider.py +25 -0
  52. kodit/infrastructure/enrichment/openai_enrichment_provider.py +89 -0
  53. kodit/infrastructure/git/__init__.py +1 -0
  54. kodit/{source/git.py → infrastructure/git/git_utils.py} +10 -2
  55. kodit/infrastructure/ignore/__init__.py +1 -0
  56. kodit/{source/ignore.py → infrastructure/ignore/ignore_pattern_provider.py} +23 -6
  57. kodit/infrastructure/indexing/__init__.py +1 -0
  58. kodit/infrastructure/indexing/fusion_service.py +55 -0
  59. kodit/infrastructure/indexing/index_repository.py +291 -0
  60. kodit/infrastructure/indexing/indexing_factory.py +113 -0
  61. kodit/infrastructure/snippet_extraction/__init__.py +1 -0
  62. kodit/infrastructure/snippet_extraction/language_detection_service.py +39 -0
  63. kodit/infrastructure/snippet_extraction/snippet_extraction_factory.py +95 -0
  64. kodit/infrastructure/snippet_extraction/snippet_query_provider.py +45 -0
  65. kodit/{snippets/method_snippets.py → infrastructure/snippet_extraction/tree_sitter_snippet_extractor.py} +123 -61
  66. kodit/infrastructure/sqlalchemy/__init__.py +1 -0
  67. kodit/{embedding → infrastructure/sqlalchemy}/embedding_repository.py +40 -26
  68. kodit/infrastructure/sqlalchemy/file_repository.py +78 -0
  69. kodit/infrastructure/sqlalchemy/repository.py +133 -0
  70. kodit/infrastructure/sqlalchemy/snippet_repository.py +79 -0
  71. kodit/infrastructure/ui/__init__.py +1 -0
  72. kodit/infrastructure/ui/progress.py +127 -0
  73. kodit/{util → infrastructure/ui}/spinner.py +19 -4
  74. kodit/mcp.py +51 -28
  75. kodit/migrations/env.py +1 -4
  76. kodit/reporting.py +78 -0
  77. {kodit-0.2.4.dist-info → kodit-0.2.6.dist-info}/METADATA +1 -1
  78. kodit-0.2.6.dist-info/RECORD +100 -0
  79. kodit/bm25/__init__.py +0 -1
  80. kodit/bm25/keyword_search_factory.py +0 -17
  81. kodit/bm25/keyword_search_service.py +0 -34
  82. kodit/embedding/__init__.py +0 -1
  83. kodit/embedding/embedding_factory.py +0 -69
  84. kodit/embedding/embedding_models.py +0 -28
  85. kodit/embedding/embedding_provider/__init__.py +0 -1
  86. kodit/embedding/embedding_provider/embedding_provider.py +0 -92
  87. kodit/embedding/embedding_provider/hash_embedding_provider.py +0 -86
  88. kodit/embedding/embedding_provider/local_embedding_provider.py +0 -96
  89. kodit/embedding/embedding_provider/openai_embedding_provider.py +0 -73
  90. kodit/embedding/local_vector_search_service.py +0 -87
  91. kodit/embedding/vector_search_service.py +0 -55
  92. kodit/enrichment/__init__.py +0 -1
  93. kodit/enrichment/enrichment_provider/__init__.py +0 -1
  94. kodit/enrichment/enrichment_provider/enrichment_provider.py +0 -36
  95. kodit/enrichment/enrichment_provider/openai_enrichment_provider.py +0 -79
  96. kodit/enrichment/enrichment_service.py +0 -45
  97. kodit/indexing/__init__.py +0 -1
  98. kodit/indexing/fusion.py +0 -67
  99. kodit/indexing/indexing_models.py +0 -43
  100. kodit/indexing/indexing_repository.py +0 -216
  101. kodit/indexing/indexing_service.py +0 -344
  102. kodit/snippets/__init__.py +0 -1
  103. kodit/snippets/languages/__init__.py +0 -53
  104. kodit/snippets/snippets.py +0 -50
  105. kodit/source/__init__.py +0 -1
  106. kodit/source/source_factories.py +0 -356
  107. kodit/source/source_repository.py +0 -169
  108. kodit/source/source_service.py +0 -150
  109. kodit/util/__init__.py +0 -1
  110. kodit-0.2.4.dist-info/RECORD +0 -71
  111. /kodit/{snippets → infrastructure/snippet_extraction}/languages/csharp.scm +0 -0
  112. /kodit/{snippets → infrastructure/snippet_extraction}/languages/go.scm +0 -0
  113. /kodit/{snippets → infrastructure/snippet_extraction}/languages/javascript.scm +0 -0
  114. /kodit/{snippets → infrastructure/snippet_extraction}/languages/python.scm +0 -0
  115. /kodit/{snippets → infrastructure/snippet_extraction}/languages/typescript.scm +0 -0
  116. {kodit-0.2.4.dist-info → kodit-0.2.6.dist-info}/WHEEL +0 -0
  117. {kodit-0.2.4.dist-info → kodit-0.2.6.dist-info}/entry_points.txt +0 -0
  118. {kodit-0.2.4.dist-info → kodit-0.2.6.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,291 @@
1
+ """Infrastructure implementation of the index repository."""
2
+
3
+ from datetime import UTC, datetime
4
+ from typing import TypeVar
5
+
6
+ from sqlalchemy import delete, func, select
7
+ from sqlalchemy.ext.asyncio import AsyncSession
8
+
9
+ from kodit.domain.entities import Embedding, File, Index, Snippet, Source
10
+ from kodit.domain.services.indexing_service import IndexRepository
11
+ from kodit.domain.value_objects import IndexView
12
+
13
+ T = TypeVar("T")
14
+
15
+
16
+ class SQLAlchemyIndexRepository(IndexRepository):
17
+ """SQLAlchemy implementation of the index repository."""
18
+
19
+ def __init__(self, session: AsyncSession) -> None:
20
+ """Initialize the index repository.
21
+
22
+ Args:
23
+ session: The SQLAlchemy async session to use for database operations.
24
+
25
+ """
26
+ self.session = session
27
+
28
+ async def create_index(self, source_id: int) -> IndexView:
29
+ """Create a new index for a source.
30
+
31
+ Args:
32
+ source_id: The ID of the source to create an index for.
33
+
34
+ Returns:
35
+ The created index view.
36
+
37
+ """
38
+ # Check if index already exists
39
+ existing_index = await self.get_index_by_source_id(source_id)
40
+ if existing_index:
41
+ return existing_index
42
+
43
+ index = Index(source_id=source_id)
44
+ self.session.add(index)
45
+
46
+ # Get source for the view
47
+ source_query = select(Source).where(Source.id == source_id)
48
+ source_result = await self.session.execute(source_query)
49
+ source = source_result.scalar_one()
50
+
51
+ return IndexView(
52
+ id=index.id,
53
+ created_at=index.created_at,
54
+ updated_at=index.updated_at,
55
+ source=source.uri,
56
+ num_snippets=0,
57
+ )
58
+
59
+ async def _get_index_view(self, index: Index, source: Source) -> IndexView:
60
+ """Create an IndexView from Index and Source entities.
61
+
62
+ Args:
63
+ index: The index entity
64
+ source: The source entity
65
+
66
+ Returns:
67
+ The index view
68
+
69
+ """
70
+ num_snippets = await self.num_snippets_for_index(index.id)
71
+ return IndexView(
72
+ id=index.id,
73
+ created_at=index.created_at,
74
+ updated_at=index.updated_at,
75
+ source=source.uri,
76
+ num_snippets=num_snippets,
77
+ )
78
+
79
+ async def get_index_by_id(self, index_id: int) -> IndexView | None:
80
+ """Get an index by its ID.
81
+
82
+ Args:
83
+ index_id: The ID of the index to retrieve.
84
+
85
+ Returns:
86
+ The index view if found, None otherwise.
87
+
88
+ """
89
+ query = (
90
+ select(Index, Source)
91
+ .join(Source, Index.source_id == Source.id)
92
+ .where(Index.id == index_id)
93
+ )
94
+ result = await self.session.execute(query)
95
+ row = result.first()
96
+
97
+ if not row:
98
+ return None
99
+
100
+ index, source = row
101
+ return await self._get_index_view(index, source)
102
+
103
+ async def get_index_by_source_id(self, source_id: int) -> IndexView | None:
104
+ """Get an index by its source ID.
105
+
106
+ Args:
107
+ source_id: The ID of the source to retrieve an index for.
108
+
109
+ Returns:
110
+ The index view if found, None otherwise.
111
+
112
+ """
113
+ query = (
114
+ select(Index, Source)
115
+ .join(Source, Index.source_id == Source.id)
116
+ .where(Index.source_id == source_id)
117
+ )
118
+ result = await self.session.execute(query)
119
+ row = result.first()
120
+
121
+ if not row:
122
+ return None
123
+
124
+ index, source = row
125
+ return await self._get_index_view(index, source)
126
+
127
+ async def list_indexes(self) -> list[IndexView]:
128
+ """List all indexes.
129
+
130
+ Returns:
131
+ A list of index views.
132
+
133
+ """
134
+ query = select(Index, Source).join(
135
+ Source, Index.source_id == Source.id, full=True
136
+ )
137
+ result = await self.session.execute(query)
138
+ rows = result.tuples()
139
+
140
+ indexes = []
141
+ for index, source in rows:
142
+ index_view = await self._get_index_view(index, source)
143
+ indexes.append(index_view)
144
+
145
+ return indexes
146
+
147
+ async def update_index_timestamp(self, index_id: int) -> None:
148
+ """Update the timestamp of an index.
149
+
150
+ Args:
151
+ index_id: The ID of the index to update.
152
+
153
+ """
154
+ query = select(Index).where(Index.id == index_id)
155
+ result = await self.session.execute(query)
156
+ index = result.scalar_one_or_none()
157
+
158
+ if index:
159
+ index.updated_at = datetime.now(UTC)
160
+
161
+ async def delete_all_snippets(self, index_id: int) -> None:
162
+ """Delete all snippets for an index.
163
+
164
+ Args:
165
+ index_id: The ID of the index to delete snippets for.
166
+
167
+ """
168
+ # First get all snippets for this index
169
+ snippets = await self.get_snippets_for_index(index_id)
170
+
171
+ # Delete all embeddings for these snippets, if there are any
172
+ for snippet in snippets:
173
+ query = delete(Embedding).where(Embedding.snippet_id == snippet.id)
174
+ await self.session.execute(query)
175
+
176
+ # Now delete the snippets
177
+ query = delete(Snippet).where(Snippet.index_id == index_id)
178
+ await self.session.execute(query)
179
+
180
+ async def get_snippets_for_index(self, index_id: int) -> list[Snippet]:
181
+ """Get all snippets for an index.
182
+
183
+ Args:
184
+ index_id: The ID of the index to get snippets for.
185
+
186
+ Returns:
187
+ A list of Snippet entities.
188
+
189
+ """
190
+ query = select(Snippet).where(Snippet.index_id == index_id)
191
+ result = await self.session.execute(query)
192
+ return list(result.scalars())
193
+
194
+ async def add_snippet(self, snippet: dict) -> None:
195
+ """Add a snippet to the database.
196
+
197
+ Args:
198
+ snippet: The snippet to add.
199
+
200
+ """
201
+ db_snippet = Snippet(
202
+ file_id=snippet["file_id"],
203
+ index_id=snippet["index_id"],
204
+ content=snippet["content"],
205
+ )
206
+ self.session.add(db_snippet)
207
+
208
+ async def update_snippet_content(self, snippet_id: int, content: str) -> None:
209
+ """Update the content of an existing snippet.
210
+
211
+ Args:
212
+ snippet_id: The ID of the snippet to update.
213
+ content: The new content for the snippet.
214
+
215
+ """
216
+ query = select(Snippet).where(Snippet.id == snippet_id)
217
+ result = await self.session.execute(query)
218
+ snippet = result.scalar_one_or_none()
219
+
220
+ if snippet:
221
+ snippet.content = content
222
+ # SQLAlchemy will automatically track this change
223
+
224
+ async def list_snippets_by_ids(self, ids: list[int]) -> list[tuple[dict, dict]]:
225
+ """List snippets by IDs.
226
+
227
+ Args:
228
+ ids: List of snippet IDs to retrieve.
229
+
230
+ Returns:
231
+ List of (file, snippet) tuples.
232
+
233
+ """
234
+ query = (
235
+ select(Snippet, File)
236
+ .where(Snippet.id.in_(ids))
237
+ .join(File, Snippet.file_id == File.id)
238
+ )
239
+ rows = await self.session.execute(query)
240
+
241
+ # Create a dictionary for O(1) lookup of results by ID
242
+ id_to_result = {}
243
+ for snippet, file in rows.all():
244
+ id_to_result[snippet.id] = (
245
+ {
246
+ "id": file.id,
247
+ "source_id": file.source_id,
248
+ "mime_type": file.mime_type,
249
+ "uri": file.uri,
250
+ "cloned_path": file.cloned_path,
251
+ "sha256": file.sha256,
252
+ "size_bytes": file.size_bytes,
253
+ "extension": file.extension,
254
+ "created_at": file.created_at,
255
+ "updated_at": file.updated_at,
256
+ },
257
+ {
258
+ "id": snippet.id,
259
+ "file_id": snippet.file_id,
260
+ "index_id": snippet.index_id,
261
+ "content": snippet.content,
262
+ "created_at": snippet.created_at,
263
+ "updated_at": snippet.updated_at,
264
+ },
265
+ )
266
+
267
+ # Check that all IDs are present
268
+ if len(id_to_result) != len(ids):
269
+ # Create a list of missing IDs
270
+ missing_ids = [
271
+ snippet_id for snippet_id in ids if snippet_id not in id_to_result
272
+ ]
273
+ msg = f"Some IDs are not present: {missing_ids}"
274
+ raise ValueError(msg)
275
+
276
+ # Rebuild the list in the same order that it was passed in
277
+ return [id_to_result[i] for i in ids]
278
+
279
+ async def num_snippets_for_index(self, index_id: int) -> int:
280
+ """Get the number of snippets for an index.
281
+
282
+ Args:
283
+ index_id: The ID of the index.
284
+
285
+ Returns:
286
+ The number of snippets.
287
+
288
+ """
289
+ query = select(func.count()).where(Snippet.index_id == index_id)
290
+ result = await self.session.execute(query)
291
+ return result.scalar_one()
@@ -0,0 +1,113 @@
1
+ """Factory for creating indexing services."""
2
+
3
+ from typing import Any
4
+
5
+ from sqlalchemy.ext.asyncio import AsyncSession
6
+
7
+ from kodit.application.services.indexing_application_service import (
8
+ IndexingApplicationService,
9
+ )
10
+ from kodit.application.services.snippet_application_service import (
11
+ SnippetApplicationService,
12
+ )
13
+ from kodit.domain.services.bm25_service import BM25DomainService
14
+ from kodit.domain.services.indexing_service import IndexingDomainService
15
+ from kodit.domain.services.source_service import SourceService
16
+ from kodit.infrastructure.bm25.bm25_factory import bm25_repository_factory
17
+ from kodit.infrastructure.embedding.embedding_factory import (
18
+ embedding_domain_service_factory,
19
+ )
20
+ from kodit.infrastructure.enrichment.enrichment_factory import (
21
+ create_enrichment_domain_service,
22
+ )
23
+ from kodit.infrastructure.indexing.fusion_service import ReciprocalRankFusionService
24
+ from kodit.infrastructure.indexing.index_repository import SQLAlchemyIndexRepository
25
+ from kodit.infrastructure.snippet_extraction.snippet_extraction_factory import (
26
+ create_snippet_extraction_domain_service,
27
+ )
28
+ from kodit.infrastructure.sqlalchemy.file_repository import (
29
+ SqlAlchemyFileRepository,
30
+ )
31
+ from kodit.infrastructure.sqlalchemy.snippet_repository import (
32
+ SqlAlchemySnippetRepository,
33
+ )
34
+
35
+
36
+ def create_snippet_application_service(
37
+ session: AsyncSession,
38
+ ) -> SnippetApplicationService:
39
+ """Create a snippet application service with all dependencies."""
40
+ # Create domain service
41
+ snippet_extraction_service = create_snippet_extraction_domain_service()
42
+
43
+ # Create repositories
44
+ snippet_repository = SqlAlchemySnippetRepository(session)
45
+ file_repository = SqlAlchemyFileRepository(session)
46
+
47
+ # Create application service
48
+ from kodit.application.services.snippet_application_service import (
49
+ SnippetApplicationService,
50
+ )
51
+
52
+ return SnippetApplicationService(
53
+ snippet_extraction_service=snippet_extraction_service,
54
+ snippet_repository=snippet_repository,
55
+ file_repository=file_repository,
56
+ session=session,
57
+ )
58
+
59
+
60
+ def create_indexing_domain_service(session: AsyncSession) -> IndexingDomainService:
61
+ """Create an indexing domain service.
62
+
63
+ Args:
64
+ session: The database session.
65
+
66
+ Returns:
67
+ An indexing domain service instance.
68
+
69
+ """
70
+ index_repository = SQLAlchemyIndexRepository(session)
71
+ fusion_service = ReciprocalRankFusionService()
72
+
73
+ return IndexingDomainService(
74
+ index_repository=index_repository,
75
+ fusion_service=fusion_service,
76
+ )
77
+
78
+
79
+ def create_indexing_application_service(
80
+ app_context: Any,
81
+ session: AsyncSession,
82
+ source_service: SourceService,
83
+ snippet_application_service: SnippetApplicationService,
84
+ ) -> IndexingApplicationService:
85
+ """Create an indexing application service.
86
+
87
+ Args:
88
+ app_context: The application context.
89
+ session: The database session.
90
+ source_service: The source service.
91
+ snippet_application_service: The snippet application service.
92
+
93
+ Returns:
94
+ An indexing application service instance.
95
+
96
+ """
97
+ # Create domain services
98
+ indexing_domain_service = create_indexing_domain_service(session)
99
+ bm25_service = BM25DomainService(bm25_repository_factory(app_context, session))
100
+ code_search_service = embedding_domain_service_factory("code", app_context, session)
101
+ text_search_service = embedding_domain_service_factory("text", app_context, session)
102
+ enrichment_service = create_enrichment_domain_service(app_context)
103
+
104
+ return IndexingApplicationService(
105
+ indexing_domain_service=indexing_domain_service,
106
+ source_service=source_service,
107
+ bm25_service=bm25_service,
108
+ code_search_service=code_search_service,
109
+ text_search_service=text_search_service,
110
+ enrichment_service=enrichment_service,
111
+ snippet_application_service=snippet_application_service,
112
+ session=session,
113
+ )
@@ -0,0 +1 @@
1
+ """Infrastructure services for snippet extraction."""
@@ -0,0 +1,39 @@
1
+ """Infrastructure implementation for language detection."""
2
+
3
+ from pathlib import Path
4
+
5
+ from kodit.domain.services.snippet_extraction_service import LanguageDetectionService
6
+
7
+
8
+ class FileSystemLanguageDetectionService(LanguageDetectionService):
9
+ """Infrastructure implementation for language detection."""
10
+
11
+ def __init__(self, language_map: dict[str, str]) -> None:
12
+ """Initialize the language detection service.
13
+
14
+ Args:
15
+ language_map: Mapping of file extensions to programming languages
16
+
17
+ """
18
+ self.language_map = language_map
19
+
20
+ async def detect_language(self, file_path: Path) -> str:
21
+ """Detect language based on file extension.
22
+
23
+ Args:
24
+ file_path: Path to the file to detect language for
25
+
26
+ Returns:
27
+ The detected programming language
28
+
29
+ Raises:
30
+ ValueError: If the language is not supported
31
+
32
+ """
33
+ suffix = file_path.suffix.removeprefix(".").lower()
34
+ language = self.language_map.get(suffix)
35
+
36
+ if language is None:
37
+ raise ValueError(f"Unsupported language for file suffix: {suffix}")
38
+
39
+ return language
@@ -0,0 +1,95 @@
1
+ """Factory for creating snippet extraction services."""
2
+
3
+ from pathlib import Path
4
+
5
+ from sqlalchemy.ext.asyncio import AsyncSession
6
+
7
+ from kodit.domain.enums import SnippetExtractionStrategy
8
+ from kodit.domain.repositories import FileRepository, SnippetRepository
9
+ from kodit.domain.services.snippet_extraction_service import (
10
+ SnippetExtractionDomainService,
11
+ )
12
+ from kodit.infrastructure.snippet_extraction.language_detection_service import (
13
+ FileSystemLanguageDetectionService,
14
+ )
15
+ from kodit.infrastructure.snippet_extraction.snippet_query_provider import (
16
+ FileSystemSnippetQueryProvider,
17
+ )
18
+ from kodit.infrastructure.snippet_extraction.tree_sitter_snippet_extractor import (
19
+ TreeSitterSnippetExtractor,
20
+ )
21
+ from kodit.infrastructure.sqlalchemy.file_repository import SqlAlchemyFileRepository
22
+ from kodit.infrastructure.sqlalchemy.snippet_repository import (
23
+ SqlAlchemySnippetRepository,
24
+ )
25
+
26
+
27
+ def create_snippet_extraction_domain_service() -> SnippetExtractionDomainService:
28
+ """Create a snippet extraction domain service with all dependencies.
29
+
30
+ Returns:
31
+ Configured snippet extraction domain service
32
+
33
+ """
34
+ # Language mapping from the existing languages module
35
+ language_map = {
36
+ # JavaScript/TypeScript
37
+ "js": "javascript",
38
+ "jsx": "javascript",
39
+ "ts": "typescript",
40
+ "tsx": "typescript",
41
+ # Python
42
+ "py": "python",
43
+ # Rust
44
+ "rs": "rust",
45
+ # Go
46
+ "go": "go",
47
+ # C/C++
48
+ "cpp": "cpp",
49
+ "hpp": "cpp",
50
+ "c": "c",
51
+ "h": "c",
52
+ # C#
53
+ "cs": "csharp",
54
+ # Ruby
55
+ "rb": "ruby",
56
+ # Java
57
+ "java": "java",
58
+ # PHP
59
+ "php": "php",
60
+ # Swift
61
+ "swift": "swift",
62
+ # Kotlin
63
+ "kt": "kotlin",
64
+ }
65
+
66
+ # Create infrastructure services
67
+ language_detector = FileSystemLanguageDetectionService(language_map)
68
+ query_provider = FileSystemSnippetQueryProvider(Path(__file__).parent / "languages")
69
+
70
+ # Create snippet extractors
71
+ method_extractor = TreeSitterSnippetExtractor(query_provider)
72
+
73
+ snippet_extractors = {
74
+ SnippetExtractionStrategy.METHOD_BASED: method_extractor,
75
+ }
76
+
77
+ # Create domain service
78
+ return SnippetExtractionDomainService(language_detector, snippet_extractors)
79
+
80
+
81
+ def create_snippet_repositories(
82
+ session: AsyncSession,
83
+ ) -> tuple[SnippetRepository, FileRepository]:
84
+ """Create snippet and file repositories.
85
+
86
+ Args:
87
+ session: SQLAlchemy session
88
+
89
+ Returns:
90
+ Tuple of (snippet_repository, file_repository)
91
+
92
+ """
93
+ snippet_repository = SqlAlchemySnippetRepository(session)
94
+ file_repository = SqlAlchemyFileRepository(session)
95
+ return snippet_repository, file_repository
@@ -0,0 +1,45 @@
1
+ """Infrastructure implementation for loading snippet queries from files."""
2
+
3
+ from abc import ABC, abstractmethod
4
+ from pathlib import Path
5
+
6
+
7
+ class SnippetQueryProvider(ABC):
8
+ """Abstract interface for providing snippet queries."""
9
+
10
+ @abstractmethod
11
+ async def get_query(self, language: str) -> str:
12
+ """Get the query for a specific language."""
13
+
14
+
15
+ class FileSystemSnippetQueryProvider(SnippetQueryProvider):
16
+ """Infrastructure implementation for loading snippet queries from files."""
17
+
18
+ def __init__(self, query_directory: Path) -> None:
19
+ """Initialize the query provider.
20
+
21
+ Args:
22
+ query_directory: Directory containing query files
23
+
24
+ """
25
+ self.query_directory = query_directory
26
+
27
+ async def get_query(self, language: str) -> str:
28
+ """Load query from file system.
29
+
30
+ Args:
31
+ language: The programming language to get the query for
32
+
33
+ Returns:
34
+ The query string for the language
35
+
36
+ Raises:
37
+ FileNotFoundError: If the query file doesn't exist
38
+
39
+ """
40
+ query_path = self.query_directory / f"{language}.scm"
41
+
42
+ if not query_path.exists():
43
+ raise FileNotFoundError(f"Query file not found: {query_path}")
44
+
45
+ return query_path.read_text()