Repository navigation
Expand file tree
/
Copy pathsearch.py
More file actions
234 lines (191 loc) · 8.42 KB
/
Copy pathsearch.py
File metadata and controls
234 lines (191 loc) · 8.42 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
#!/usr/bin/env python3
import click
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from database import SessionLocal, CodeChunk, CodeFile, Repository
from embedding_service import EmbeddingService
from sqlalchemy import func
import numpy as np
console = Console()
class CodeSearch:
def __init__(self):
self.embedding_service = EmbeddingService()
def search_similar_code(self, query: str, repo_name: str = None, limit: int = 10, similarity_threshold: float = 0.7):
"""Search for similar code chunks"""
try:
# Get query embedding
query_embedding = self.embedding_service.get_embedding(query)
if not query_embedding:
console.print("[red]Failed to generate query embedding[/red]")
return []
# Convert to numpy array for pgvector
query_vector = np.array(query_embedding)
db = SessionLocal()
# Build query
query_builder = db.query(
CodeChunk.content,
CodeChunk.start_line,
CodeChunk.end_line,
CodeChunk.token_count,
CodeFile.file_path,
CodeFile.file_name,
Repository.repo_name,
1 - func.cosine_similarity(CodeChunk.embedding, query_vector).label('similarity')
).join(CodeFile).join(Repository)
if repo_name:
query_builder = query_builder.filter(Repository.repo_name == repo_name)
# Filter by similarity threshold and order by similarity
results = query_builder.filter(
1 - func.cosine_similarity(CodeChunk.embedding, query_vector) > similarity_threshold
).order_by(
func.cosine_similarity(CodeChunk.embedding, query_vector).desc()
).limit(limit).all()
return results
except Exception as e:
console.print(f"[red]Search error: {e}[/red]")
return []
finally:
db.close()
def search_by_file_pattern(self, pattern: str, repo_name: str = None, limit: int = 20):
"""Search for code chunks in files matching a pattern"""
try:
db = SessionLocal()
query_builder = db.query(
CodeChunk.content,
CodeChunk.start_line,
CodeChunk.end_line,
CodeChunk.token_count,
CodeFile.file_path,
CodeFile.file_name,
Repository.repo_name
).join(CodeFile).join(Repository)
if repo_name:
query_builder = query_builder.filter(Repository.repo_name == repo_name)
# Filter by file pattern
results = query_builder.filter(
CodeFile.file_path.ilike(f'%{pattern}%')
).limit(limit).all()
return results
except Exception as e:
console.print(f"[red]Search error: {e}[/red]")
return []
finally:
db.close()
@click.group()
def search_cli():
"""Code Search - Search vectorized codebases"""
pass
@search_cli.command()
@click.argument('query')
@click.option('--repo-name', help='Repository name to search in')
@click.option('--limit', default=10, help='Number of results to return')
@click.option('--threshold', default=0.7, help='Similarity threshold (0.0-1.0)')
def semantic(query, repo_name, limit, threshold):
"""Search for semantically similar code"""
try:
console.print(Panel.fit(
"[bold blue]Code Search[/bold blue]\n"
"Semantic search in vectorized codebases",
border_style="blue"
))
search = CodeSearch()
results = search.search_similar_code(query, repo_name, limit, threshold)
if not results:
console.print("[yellow]No results found[/yellow]")
return
# Display results
table = Table(title=f"Search Results for: '{query}'")
table.add_column("Repository", style="cyan")
table.add_column("File", style="green")
table.add_column("Lines", style="yellow")
table.add_column("Similarity", style="magenta")
table.add_column("Content Preview", style="white")
for content, start_line, end_line, token_count, file_path, file_name, repo_name, similarity in results:
# Truncate content for display
content_preview = content[:100] + "..." if len(content) > 100 else content
table.add_row(
repo_name,
f"{file_path}:{start_line}-{end_line}",
f"{start_line}-{end_line}",
f"{similarity:.3f}",
content_preview
)
console.print(table)
# Show detailed results
console.print("\n[bold]Detailed Results:[/bold]")
for i, (content, start_line, end_line, token_count, file_path, file_name, repo_name, similarity) in enumerate(results, 1):
console.print(f"\n[bold cyan]{i}. {repo_name}/{file_path}:{start_line}-{end_line}[/bold cyan]")
console.print(f"[yellow]Similarity: {similarity:.3f}[/yellow]")
console.print(f"[green]Tokens: {token_count}[/green]")
console.print(f"[white]{content}[/white]")
console.print("-" * 80)
except Exception as e:
console.print(f"[bold red]Error: {str(e)}[/bold red]")
@search_cli.command()
@click.argument('pattern')
@click.option('--repo-name', help='Repository name to search in')
@click.option('--limit', default=20, help='Number of results to return')
def files(pattern, repo_name, limit):
"""Search for code in files matching a pattern"""
try:
console.print(Panel.fit(
"[bold blue]File Search[/bold blue]\n"
"Search code in files matching pattern",
border_style="blue"
))
search = CodeSearch()
results = search.search_by_file_pattern(pattern, repo_name, limit)
if not results:
console.print("[yellow]No results found[/yellow]")
return
# Display results
table = Table(title=f"File Search Results for: '{pattern}'")
table.add_column("Repository", style="cyan")
table.add_column("File", style="green")
table.add_column("Lines", style="yellow")
table.add_column("Tokens", style="magenta")
table.add_column("Content Preview", style="white")
for content, start_line, end_line, token_count, file_path, file_name, repo_name in results:
# Truncate content for display
content_preview = content[:100] + "..." if len(content) > 100 else content
table.add_row(
repo_name,
file_path,
f"{start_line}-{end_line}",
str(token_count),
content_preview
)
console.print(table)
except Exception as e:
console.print(f"[bold red]Error: {str(e)}[/bold red]")
@search_cli.command()
def list_repos():
"""List available repositories for search"""
try:
db = SessionLocal()
repos = db.query(Repository).filter(Repository.status == 'completed').all()
if not repos:
console.print("[yellow]No completed repositories found[/yellow]")
return
table = Table(title="Available Repositories for Search")
table.add_column("Name", style="cyan")
table.add_column("URL", style="green")
table.add_column("Files", style="yellow")
table.add_column("Chunks", style="magenta")
for repo in repos:
file_count = db.query(CodeFile).filter(CodeFile.repository_id == repo.id).count()
chunk_count = db.query(CodeChunk).join(CodeFile).filter(CodeFile.repository_id == repo.id).count()
table.add_row(
repo.repo_name,
repo.repo_url,
str(file_count),
str(chunk_count)
)
console.print(table)
except Exception as e:
console.print(f"[bold red]Error: {str(e)}[/bold red]")
finally:
db.close()
if __name__ == '__main__':
search_cli()