forked from danilofalcao/jarvis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace_manager.py
More file actions
1398 lines (1223 loc) · 59.9 KB
/
workspace_manager.py
File metadata and controls
1398 lines (1223 loc) · 59.9 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Workspace manager module for handling file operations and codebase management."""
# pylama:ignore=E501,C901,E125,E251
import hashlib
import logging
import math
import mmap
import os
import re
import threading
import time
from collections import Counter, defaultdict
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Dict, List, Optional, Set, Tuple, Union
@dataclass
class Document:
path: str
content: str
term_freqs: Counter
length: int
class BM25Search:
def __init__(self, k1: float = 1.5, b: float = 0.75):
self.k1 = k1 # Term frequency scaling parameter
self.b = b # Length normalization parameter
self.documents: Dict[str, Document] = {}
self.avg_doc_length: float = 0
self.total_docs: int = 0
self.idf_cache: Dict[str, float] = {}
self.tokenizer_pattern = re.compile(r"\w+|[^\w\s]")
self._lock = threading.Lock()
# Initialize logging
self.logger = logging.getLogger("BM25Search")
self.logger.setLevel(logging.DEBUG)
def preprocess(self, text: str) -> List[str]:
"""Tokenize and normalize text"""
# Convert to lowercase and tokenize
tokens = self.tokenizer_pattern.findall(text.lower())
# Filter out single character tokens except if they're special
# characters
return [t for t in tokens if len(t) > 1 or not t.isalnum()]
def add_document(self, path: str, content: str) -> None:
"""Add a document to the search index"""
with self._lock:
# Preprocess content
tokens = self.preprocess(content)
# Create document object
doc = Document(
path=path,
content=content,
term_freqs=Counter(tokens),
length=len(tokens),
)
# Update index
self.documents[path] = doc
# Update average document length
self.total_docs = len(self.documents)
total_length = sum(doc.length for doc in self.documents.values())
self.avg_doc_length = (total_length / self.total_docs
if self.total_docs > 0 else 0)
# Clear IDF cache as it needs to be recalculated
self.idf_cache.clear()
def remove_document(self, path: str) -> None:
"""Remove a document from the search index"""
with self._lock:
if path in self.documents:
del self.documents[path]
self.total_docs = len(self.documents)
if self.total_docs > 0:
total_length = sum(doc.length
for doc in self.documents.values())
self.avg_doc_length = total_length / self.total_docs
else:
self.avg_doc_length = 0
self.idf_cache.clear()
def _calculate_idf(self, term: str) -> float:
"""Calculate Inverse Document Frequency for a term"""
if term in self.idf_cache:
return self.idf_cache[term]
# Count documents containing the term
doc_freq = sum(1 for doc in self.documents.values()
if term in doc.term_freqs)
# Calculate IDF with smoothing
idf = math.log(1 + (self.total_docs - doc_freq + 0.5) /
(doc_freq + 0.5))
self.idf_cache[term] = idf
return idf
def search(self,
query: str,
top_k: int = 10) -> List[Tuple[str, float, str]]:
"""Search for documents matching the query"""
start_time = time.time()
self.logger.debug(f"Starting search for query: {query}")
# Preprocess query
query_terms = self.preprocess(query)
scores: Dict[str, float] = defaultdict(float)
# Calculate scores for each document
for path, doc in self.documents.items():
score = 0.0
doc_len_norm = 1 - self.b + self.b * \
(doc.length / self.avg_doc_length)
for term in query_terms:
if term in doc.term_freqs:
tf = doc.term_freqs[term]
idf = self._calculate_idf(term)
# BM25 scoring formula
term_score = (idf * tf * (self.k1 + 1) /
(tf + self.k1 * doc_len_norm))
score += term_score
if score > 0:
scores[path] = score
# Sort results by score
results = []
for path, score in sorted(scores.items(),
key=lambda x: x[1],
reverse=True)[:top_k]:
doc = self.documents[path]
# Get a relevant snippet from the content
snippet = self._get_relevant_snippet(doc.content, query_terms)
results.append((path, score, snippet))
elapsed_time = time.time() - start_time
self.logger.info(
f"Search completed in {elapsed_time:.3f}s, found {len(results)} results"
)
return results
def _get_relevant_snippet(self,
content: str,
query_terms: List[str],
snippet_size: int = 200) -> str:
"""Extract a relevant snippet from the content containing query terms"""
# Find the best window containing query terms
lines = content.split("\n")
best_score = 0
best_snippet = ""
for i in range(len(lines)):
window = " ".join(lines[max(0, i - 2):min(len(lines), i + 3)])
if len(window) > snippet_size * 2:
continue
score = sum(1 for term in query_terms
if term.lower() in window.lower())
if score > best_score:
best_score = score
best_snippet = window
if not best_snippet and lines:
# Fallback to first few lines if no relevant snippet found
best_snippet = " ".join(lines[:5])
# Truncate and add ellipsis if needed
if len(best_snippet) > snippet_size:
best_snippet = best_snippet[:snippet_size] + "..."
return best_snippet
class WorkspaceManager:
# File size thresholds and constants
MAX_FILE_SIZE = 50 * 1024 * 1024 # 50MB
PREVIEW_SIZE = 10 * 1024 # 10KB for previews
CHUNK_SIZE = 1024 * 1024 # 1MB chunks for large file reading
LAZY_LOAD_THRESHOLD = 1000 # Number of files before switching to lazy loading
MAX_CACHE_SIZE = 100 * 1024 * 1024 # 100MB max cache size
MAX_CACHE_ENTRIES = 1000 # Maximum number of cached files
INDEXING_CHUNK_SIZE = 5 * 1024 * 1024 # 5MB chunks for indexing
LARGE_FILE_THRESHOLD = 1 * 1024 * 1024 # 1MB threshold for large files
# File type configurations
BINARY_EXTENSIONS = {".pyc", ".pyo", ".pyd", ".so", ".dll", ".exe", ".bin"}
SKIP_EXTENSIONS = {".db", ".log", ".cache"} | BINARY_EXTENSIONS
SKIP_FOLDERS = {
".git",
"node_modules",
"__pycache__",
"venv",
".venv",
"env",
".env",
"dist",
"build",
"target",
"vendor",
".idea",
".vscode",
"coverage",
".next",
".nuxt",
".output",
"tmp",
"temp",
}
# Language-specific patterns for better context understanding
LANGUAGE_PATTERNS = {
"python": {
"imports":
r"^(?:from|import)\s+[\w.]+(?:\s+(?:as|import)\s+[\w.]+)*",
"classes": r"^class\s+\w+(?:\(.*?\))?:",
"functions": r"^def\s+\w+\s*\([^)]*\)\s*(?:->\s*[\w\[\],\s]+)?:",
},
"javascript": {
"imports": r'^(?:import|export)\s+.*?(?:from\s+[\'"].*?[\'"])?;?$',
"classes":
r"^(?:export\s+)?class\s+\w+(?:\s+extends\s+\w+)?(?:\s+implements\s+\w+(?:\s*,\s*\w+)*)?",
"functions": r"^(?:async\s+)?function\s*\w*\s*\([^)]*\)",
},
}
def __init__(self, workspace_root: str):
"""Initialize workspace manager with enhanced features"""
self.workspace_root = workspace_root
os.makedirs(workspace_root, exist_ok=True)
# Initialize enhanced logging
self.logger = logging.getLogger("WorkspaceManager")
self.logger.setLevel(logging.DEBUG)
# Setup console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_format = logging.Formatter(
"%(asctime)s - %(levelname)s - %(message)s")
console_handler.setFormatter(console_format)
self.logger.addHandler(console_handler)
# Setup file handler
try:
log_file = os.path.join(workspace_root, "workspace_manager.log")
if os.path.exists(log_file):
try:
os.remove(log_file)
except Exception as e:
print(f"Failed to delete old log file: {e}")
file_handler = logging.FileHandler(log_file)
file_handler.setLevel(logging.DEBUG)
file_format = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(funcName)s:%(lineno)d - %(message)s"
)
file_handler.setFormatter(file_format)
self.logger.addHandler(file_handler)
except Exception as e:
print(f"Failed to setup file logging: {e}")
self.logger.info(
f"Initializing WorkspaceManager with root: {workspace_root}")
# Initialize BM25 search
self.search_index = BM25Search()
self.logger.info("Initialized BM25 search index")
# Enhanced caching system with LRU and size tracking
self._content_cache: Dict[str, Tuple[str, float, int]] = {}
self._structure_cache: Dict[str, Tuple[List[dict], float]] = {}
self._chunk_cache: Dict[str, Dict[int, str]] = {}
self._symbol_cache: Dict[str, Dict[str, List[Tuple[int, str]]]] = {}
self._dependency_graph: Dict[str, Set[str]] = defaultdict(set)
self._file_index: Dict[str, Dict[str, Any]] = {}
self._cache_size = 0
self._cache_lock = threading.Lock()
self._executor = ThreadPoolExecutor(max_workers=4)
self._gitignore_patterns: List[str] = []
self.logger.debug("Initialized caching systems and thread pool")
self._load_gitignore()
def _update_cache_size(self, path: str, content: str, is_add: bool = True):
"""Track cache size with thread safety"""
with self._cache_lock:
size = len(content.encode("utf-8"))
if is_add:
self._cache_size += size
# Implement LRU cache eviction if needed
while (self._cache_size > self.MAX_CACHE_SIZE
or len(self._content_cache) > self.MAX_CACHE_ENTRIES):
oldest_path = next(iter(self._content_cache))
oldest_content = self._content_cache.pop(oldest_path)[0]
self._cache_size -= len(oldest_content.encode("utf-8"))
else:
self._cache_size -= size
def _index_file(self,
file_path: str,
content: Optional[str] = None) -> Dict[str, Any]:
"""Index file contents for faster searching and context understanding"""
if not content:
try:
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
except UnicodeDecodeError:
return {} # Skip binary files
ext = Path(file_path).suffix.lower()
lang = "python" if ext == ".py" else "javascript" if ext == ".js" else None
index = {
"symbols": defaultdict(list),
"imports": set(),
"size": os.path.getsize(file_path),
"hash": hashlib.md5(content.encode()).hexdigest(),
"last_modified": os.path.getmtime(file_path),
"language": lang,
}
if lang and lang in self.LANGUAGE_PATTERNS:
patterns = self.LANGUAGE_PATTERNS[lang]
for symbol_type, pattern in patterns.items():
for i, line in enumerate(content.splitlines(), 1):
if re.match(pattern, line.strip()):
index["symbols"][symbol_type].append((i, line.strip()))
if symbol_type == "imports":
index["imports"].add(line.strip())
self._file_index[file_path] = index
return index
def _analyze_dependencies(
self, files_content: Dict[str, str]) -> Dict[str, Set[str]]:
"""Analyze and cache file dependencies"""
dependencies = defaultdict(set)
for file_path, content in files_content.items():
if file_path not in self._file_index:
self._index_file(file_path, content)
if file_path in self._file_index:
index = self._file_index[file_path]
if index.get("language") == "python":
for imp in index.get("imports", set()):
# Convert import statement to module path
match = re.match(r"^from\s+([\w.]+)\s+import", imp)
if match:
module = match.group(1)
dependencies[file_path].add(
module.replace(".", "/") + ".py")
return dependencies
def _get_file_content(self,
file_path: str,
start_chunk: int = 0,
num_chunks: int = 1) -> str:
"""Enhanced file content retrieval with chunked reading and caching"""
try:
file_size = os.path.getsize(file_path)
self.logger.debug(
f"Reading file {file_path} (size: {file_size} bytes)")
# For small files, use content cache
if file_size < self.LARGE_FILE_THRESHOLD:
if file_path in self._content_cache:
content, mtime, size = self._content_cache[file_path]
if os.path.getmtime(
file_path) == mtime and size == file_size:
self.logger.debug(f"Cache hit for {file_path}")
return content
try:
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
self._update_cache_size(file_path, content)
self._content_cache[file_path] = (
content,
os.path.getmtime(file_path),
file_size,
)
# Add to search index if it's a new file
if file_path not in self.search_index.documents:
try:
rel_path = os.path.relpath(
file_path, self.workspace_root)
self.search_index.add_document(
rel_path, content)
self.logger.debug(
f"Added {rel_path} to search index")
except Exception as e:
self.logger.warning(
f"Failed to add {file_path} to search index: {e}"
)
return content
except UnicodeDecodeError:
with open(file_path, "r", encoding="latin-1") as f:
content = f.read()
self._update_cache_size(file_path, content)
self._content_cache[file_path] = (
content,
os.path.getmtime(file_path),
file_size,
)
# Add to search index if it's a new file
if file_path not in self.search_index.documents:
try:
rel_path = os.path.relpath(
file_path, self.workspace_root)
self.search_index.add_document(
rel_path, content)
self.logger.debug(
f"Added {rel_path} to search index")
except Exception as e:
self.logger.warning(
f"Failed to add {file_path} to search index: {e}"
)
return content
# For large files, use chunk cache and mmap for efficiency
if file_path not in self._chunk_cache:
self._chunk_cache[file_path] = {}
chunks = []
for i in range(start_chunk, start_chunk + num_chunks):
if i in self._chunk_cache[file_path]:
chunks.append(self._chunk_cache[file_path][i])
continue
offset = i * self.CHUNK_SIZE
if offset >= file_size:
break
try:
with open(file_path, "rb") as f:
with mmap.mmap(f.fileno(), 0,
access=mmap.ACCESS_READ) as mm:
mm.seek(offset)
chunk_bytes = mm.read(self.CHUNK_SIZE)
try:
chunk = chunk_bytes.decode("utf-8")
except UnicodeDecodeError:
chunk = chunk_bytes.decode("latin-1")
self._chunk_cache[file_path][i] = chunk
chunks.append(chunk)
except (ValueError, OSError) as e:
self.logger.error(
f"Error reading file {file_path} with mmap: {str(e)}")
# Fallback to regular file reading
with open(file_path, "r", encoding="utf-8") as f:
f.seek(offset)
chunk = f.read(self.CHUNK_SIZE)
self._chunk_cache[file_path][i] = chunk
chunks.append(chunk)
content = "".join(chunks)
# Add to search index if it's a new file
if content and file_path not in self.search_index.documents:
try:
rel_path = os.path.relpath(file_path, self.workspace_root)
self.search_index.add_document(rel_path, content)
self.logger.debug(f"Added {rel_path} to search index")
except Exception as e:
self.logger.warning(
f"Failed to add {file_path} to search index: {e}")
return content
except (IOError, UnicodeDecodeError) as e:
self.logger.error(f"Error reading file {file_path}: {str(e)}")
return ""
def get_workspace_files(self,
workspace_dir: str,
query: str = None) -> Dict[str, str]:
"""Enhanced workspace file retrieval with smart filtering and parallel processing"""
start_time = time.time()
self.logger.info(f"Getting workspace files for: {workspace_dir}" +
(f" with query: {query}" if query else ""))
files_content = {}
all_files = []
try:
# Collect all files with parallel processing
self.logger.debug("Starting parallel file scan...")
all_files = self._parallel_scan(workspace_dir)
self.logger.info(f"Found {len(all_files)} files to process")
# If no query, only include small files and files in root directory
if not query:
self.logger.debug(
"No query provided, selecting root and small files...")
for file_path, rel_path in all_files:
try:
if (os.path.dirname(rel_path) == ""
or os.path.getsize(file_path)
< self.LARGE_FILE_THRESHOLD):
content = self._get_file_content(file_path)
if content:
# Truncate content for context
files_content[rel_path] = (
self._truncate_content_for_context(content)
)
self.logger.debug(f"Added file: {rel_path}")
except Exception as e:
self.logger.warning(
f"Could not read file {file_path}: {e}")
elapsed_time = time.time() - start_time
self.logger.info(
f"File collection complete in {elapsed_time:.2f}s. Total files: {len(files_content)}"
)
return files_content
# Enhanced scoring system for file relevance
self.logger.debug("Starting file relevance scoring...")
scored_files = self._score_files(all_files, query)
self.logger.info(f"Scored {len(scored_files)} relevant files")
# Get top relevant files with parallel content loading
top_files = sorted(scored_files, key=lambda x: x[2],
reverse=True)[:10]
self.logger.debug(
f"Selected top {len(top_files)} files for processing")
# Load contents in parallel
with ThreadPoolExecutor(max_workers=4) as executor:
self.logger.debug("Starting parallel content loading...")
results = executor.map(self._load_file_content, top_files)
for result in results:
if result:
rel_path, content = result
# Truncate content for context
files_content[
rel_path] = self._truncate_content_for_context(
content)
self.logger.debug(f"Loaded content for: {rel_path}")
elapsed_time = time.time() - start_time
self.logger.info(
f"File processing complete in {elapsed_time:.2f}s. Files loaded: {len(files_content)}"
)
return files_content
except Exception as e:
self.logger.error(f"Error processing workspace files: {str(e)}",
exc_info=True)
return {}
def _parallel_scan(self, workspace_dir: str) -> List[Tuple[str, str]]:
"""Helper method for parallel directory scanning"""
def process_directory(dir_path: str) -> List[Tuple[str, str]]:
files = []
try:
with os.scandir(dir_path) as it:
for entry in it:
if (entry.is_file() and not entry.name.startswith(".")
and not entry.name.endswith(
tuple(self.SKIP_EXTENSIONS))):
rel_path = os.path.relpath(entry.path,
workspace_dir)
if not self._should_ignore(rel_path):
files.append((entry.path, rel_path))
elif (entry.is_dir() and not entry.name.startswith(".")
and entry.name not in self.SKIP_FOLDERS):
files.extend(process_directory(entry.path))
except OSError as e:
self.logger.error(
f"Error scanning directory {dir_path}: {str(e)}")
return files
subdirs = []
files = []
try:
with os.scandir(workspace_dir) as it:
for entry in it:
if (entry.is_dir() and not entry.name.startswith(".")
and entry.name not in self.SKIP_FOLDERS):
subdirs.append(entry.path)
elif (entry.is_file() and not entry.name.startswith(".")
and not entry.name.endswith(
tuple(self.SKIP_EXTENSIONS))):
rel_path = os.path.relpath(entry.path, workspace_dir)
if not self._should_ignore(rel_path):
files.append((entry.path, rel_path))
except OSError as e:
self.logger.error(f"Error scanning root directory: {str(e)}")
if subdirs:
with ThreadPoolExecutor(max_workers=4) as executor:
subdir_files = list(executor.map(process_directory, subdirs))
for subdir_file_list in subdir_files:
files.extend(subdir_file_list)
return files
def _score_files(self, files: List[Tuple[str, str]],
query: str) -> List[Tuple[str, str, float]]:
"""Score files based on relevance to query"""
scored_files = []
for file_path, rel_path in files:
score = 0
try:
# Check filename relevance
if any(term.lower() in rel_path.lower()
for term in query.lower().split()):
score += 5
self.logger.debug(
f"File {rel_path} matched query in name (+5)")
# Quick content scan for relevance
try:
with open(file_path, "rb") as f:
with mmap.mmap(f.fileno(), 0,
access=mmap.ACCESS_READ) as mm:
preview = mm.read(4096).decode("utf-8",
errors="ignore")
if any(term.lower() in preview.lower()
for term in query.lower().split()):
score += 3
self.logger.debug(
f"File {rel_path} matched query in content (+3)"
)
except (ValueError, OSError):
with open(file_path,
"r",
encoding="utf-8",
errors="ignore") as f:
preview = f.read(4096)
if any(term.lower() in preview.lower()
for term in query.lower().split()):
score += 3
self.logger.debug(
f"File {rel_path} matched query in content (+3)"
)
# Consider file location and type
if os.path.dirname(rel_path) == "":
score += 2
self.logger.debug(
f"File {rel_path} is in root directory (+2)")
if rel_path.endswith(
(".py", ".js", ".html", ".css", ".json", ".yml", ".yaml")):
score += 1
self.logger.debug(
f"File {rel_path} is a primary file type (+1)")
# Consider indexed symbols if available
if file_path in self._file_index:
index = self._file_index[file_path]
for symbol_list in index["symbols"].values():
if any(term.lower() in symbol[1].lower()
for term in query.lower().split()
for symbol in symbol_list):
score += 2
self.logger.debug(
f"File {rel_path} matched query in symbols (+2)"
)
if score > 0:
scored_files.append((file_path, rel_path, score))
self.logger.debug(f"File {rel_path} total score: {score}")
except Exception as e:
self.logger.warning(f"Could not analyze file {file_path}: {e}")
return scored_files
def _load_file_content(
self, file_tuple: Tuple[str, str,
float]) -> Optional[Tuple[str, str]]:
"""Load file content with proper error handling"""
file_path, rel_path, _ = file_tuple
try:
content = self._get_file_content(file_path)
if content:
self.logger.debug(
f"Successfully loaded content for {rel_path}")
return rel_path, content
except Exception as e:
self.logger.warning(f"Could not read file {file_path}: {e}")
return None
def _load_gitignore(self):
"""Load .gitignore patterns if the file exists"""
gitignore_path = os.path.join(self.workspace_root, ".gitignore")
if os.path.exists(gitignore_path):
try:
with open(gitignore_path, "r") as f:
patterns = []
for line in f:
line = line.strip()
if line and not line.startswith("#"):
# Convert glob patterns to regex patterns
pattern = (line.replace(".", r"\.").replace(
"*", ".*").replace("?", "."))
if not line.startswith("/"):
pattern = f".*{pattern}"
if not line.endswith("/"):
pattern = f"{pattern}($|/.*)"
patterns.append(pattern)
self._gitignore_patterns = patterns
except Exception as e:
print(f"Warning: Could not read .gitignore file: {e}")
def _should_ignore(self, path: str) -> bool:
"""Check if a path should be ignored based on gitignore patterns"""
if not self._gitignore_patterns:
return False
normalized_path = path.replace("\\", "/")
return any(
re.match(pattern, normalized_path)
for pattern in self._gitignore_patterns)
def _is_cache_valid(
self, path: str, cache_entry: Tuple[Union[str, List[dict]],
float]) -> bool:
"""Check if cached content is still valid"""
try:
current_mtime = os.path.getmtime(path)
return current_mtime == cache_entry[1]
except OSError:
return False
def get_directory_structure(self,
dir_path: str,
depth: int = 1) -> List[dict]:
"""Get directory structure with lazy loading support"""
try:
# Use the provided path directly if it's absolute, otherwise join
# with workspace root
abs_path = (dir_path if os.path.isabs(dir_path) else os.path.join(
self.workspace_root, dir_path))
result = []
for entry in os.scandir(abs_path):
# Skip hidden files, .git directory, and other ignored
# directories
if entry.name.startswith(".") or (entry.is_dir() and entry.name
in self.SKIP_FOLDERS):
continue
# Get path relative to current directory instead of workspace
# root
rel_path = os.path.relpath(entry.path, abs_path)
# Skip if path matches gitignore patterns
if self._should_ignore(rel_path):
continue
if entry.is_file() and not any(
rel_path.endswith(ext)
for ext in self.SKIP_EXTENSIONS):
result.append({
"type": "file",
"path": rel_path.replace("\\", "/"),
"size": entry.stat().st_size,
})
elif entry.is_dir() and depth > 0:
children = (self.get_directory_structure(
entry.path, depth - 1) if depth > 1 else [])
result.append({
"type":
"directory",
"path":
rel_path.replace("\\", "/"),
"has_children":
bool(children)
or any(True for _ in os.scandir(entry.path)),
"children":
children,
})
return sorted(result,
key=lambda x:
(x["type"] != "directory", x["path"].lower()))
except OSError:
return []
def get_workspace_structure(self, workspace_dir: str) -> List[dict]:
"""Get workspace structure with lazy loading for large directories"""
try:
# Check if we have a valid cached structure
if workspace_dir in self._structure_cache:
structure, mtime = self._structure_cache[workspace_dir]
if os.path.getmtime(workspace_dir) == mtime:
return structure
# Count total files to determine if we should use lazy loading
total_files = 0
print(f"\nCounting files in {workspace_dir}:")
for root, dirs, files in os.walk(workspace_dir):
# Skip .git and other ignored directories
original_dirs = set(dirs)
dirs[:] = [
d for d in dirs
if not d.startswith(".") and d not in self.SKIP_FOLDERS
]
if len(original_dirs) != len(dirs):
print(
f"Skipped directories in {root}: {original_dirs - set(dirs)}"
)
# Filter files based on gitignore and skip patterns
for file in files:
if not file.startswith(".") and not file.endswith(
tuple(self.SKIP_EXTENSIONS)):
rel_path = os.path.relpath(os.path.join(root, file),
workspace_dir)
if not self._should_ignore(rel_path):
total_files += 1
print(f"Counting file: {rel_path}")
else:
print(f"Ignoring file (gitignore): {rel_path}")
else:
print(f"Ignoring file (hidden/extension): {file}")
print(f"\nTotal files counted: {total_files}")
if total_files > self.LAZY_LOAD_THRESHOLD:
# Use lazy loading - only get top-level structure
structure = self.get_directory_structure(workspace_dir,
depth=1)
else:
# Get full structure for smaller workspaces
structure = self.get_directory_structure(workspace_dir,
depth=float("inf"))
self._structure_cache[workspace_dir] = (
structure,
os.path.getmtime(workspace_dir),
)
return structure
except OSError:
return []
def expand_directory(self,
dir_path: str,
workspace_dir: str,
page_size: int = 100,
page: int = 1) -> dict:
"""Expand a directory node for lazy loading with pagination support
Args:
dir_path: Directory path to expand
workspace_dir: The workspace directory containing the files
page_size: Number of items per page
page: Page number (1-based)
Returns:
Dictionary containing:
- items: List of files and directories in the current page
- total_items: Total number of items
- has_more: Whether there are more items
"""
try:
# Ensure we have absolute paths
if os.path.isabs(dir_path):
abs_path = dir_path
# Verify the path is within workspace directory
if not os.path.abspath(abs_path).startswith(
os.path.abspath(workspace_dir)):
raise ValueError("Path is outside workspace directory")
else:
abs_path = os.path.join(workspace_dir, dir_path)
print(f"Expanding directory: {abs_path}") # Debug log
if not os.path.exists(abs_path):
print(f"Directory not found: {abs_path}") # Debug log
raise ValueError(f"Directory not found: {dir_path}")
if not os.path.isdir(abs_path):
print(f"Not a directory: {abs_path}") # Debug log
raise ValueError(f"Not a directory: {dir_path}")
# Get all entries first
entries = []
start_idx = (page - 1) * page_size
try:
with os.scandir(abs_path) as it:
for entry in it:
try:
# Skip hidden files and ignored directories
if entry.name.startswith(".") or (
entry.is_dir()
and entry.name in self.SKIP_FOLDERS):
print(f"Skipping {entry.name} (hidden/ignored)"
) # Debug log
continue
# Get path relative to current directory
entry_rel_path = os.path.relpath(
entry.path, abs_path)
# Skip if path matches gitignore patterns
if self._should_ignore(entry_rel_path):
print(f"Skipping {entry_rel_path} (gitignore)"
) # Debug log
continue
if entry.is_file() and not any(
entry_rel_path.endswith(ext)
for ext in self.SKIP_EXTENSIONS):
# Debug log
print(f"Adding file: {entry_rel_path}")
entries.append({
"type":
"file",
"path":
entry_rel_path.replace("\\", "/"),
"size":
entry.stat().st_size,
})
elif entry.is_dir():
# For directories, check if they have children
# without loading them all
has_children = False
try:
with os.scandir(entry.path) as dir_it:
for child in dir_it:
if not child.name.startswith(
".") and not (
child.is_dir()
and child.name
in self.SKIP_FOLDERS):
has_children = True
break
except OSError as e:
print(
f"Error checking directory contents: {e}"
) # Debug log
pass
print(
f"Adding directory: {entry_rel_path} (has_children={has_children})"
) # Debug log
entries.append({
"type":
"directory",
"path":
entry_rel_path.replace("\\", "/"),
"has_children":
has_children,
})
except OSError as e:
print(f"Error processing entry {entry.name}: {e}"
) # Debug log
continue
except OSError as e:
print(f"Error scanning directory {abs_path}: {e}") # Debug log
raise
# Sort entries (directories first, then alphabetically)
entries.sort(
key=lambda x: (x["type"] != "directory", x["path"].lower()))
# Get total count and slice for current page
total_items = len(entries)
page_entries = entries[start_idx:start_idx + page_size]
print(
f"Directory expansion results: {len(page_entries)} items (total: {total_items})"
) # Debug log
return {
"items": page_entries,
"total_items": total_items,
"has_more": (start_idx + page_size) < total_items,
}
except Exception as e:
print(f"Error in expand_directory: {str(e)}") # Debug log
raise
def clear_cache(self, file_path: Optional[str] = None):
"""Clear cache entries"""
if file_path:
self._content_cache.pop(file_path, None)
self._chunk_cache.pop(file_path, None)