-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
192 lines (164 loc) · 7.24 KB
/
Copy pathmain.py
File metadata and controls
192 lines (164 loc) · 7.24 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
from dotenv import load_dotenv
load_dotenv()
import typer
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from rich.markdown import Markdown
from ingestion.pipeline import ingest_and_index
from ingestion.watcher import start_watching, start_background_watcher
from ingestion.loaders.github import list_cloned_repos, delete_cloned_repo
from agents.orchestrator import Orchestrator
from core.config import get_default_model, normalize_provider
import os
app = typer.Typer(help="Cortex: Your Local AI-Powered Codebase Assistant")
repo_app = typer.Typer(help="Manage cloned GitHub repositories")
app.add_typer(repo_app, name="repo")
console = Console()
PROVIDER_HELP = (
"LLM provider: 'ollama', 'cliproxyapi', or 'openai'. "
"Defaults to CORTEX_LLM_PROVIDER or 'ollama'."
)
MODEL_HELP = "Specific model name. Defaults to the selected provider's configured default model."
@app.command()
def index(
path: str = typer.Argument(".", help="Path to folder or GitHub URL (e.g., https://github.com/user/repo.git)"),
source_type: str = typer.Option("folder", "--type", "-t", help="Type of source: 'folder' or 'github'")
):
"""
Index a codebase for retrieval.
For GitHub repos, use: cortex index https://github.com/user/repo.git --type github
"""
is_github_url = source_type == "github" or path.startswith(("http://", "https://", "git@"))
if is_github_url:
console.print(Panel(f"[bold blue]Indexing GitHub Repository:[/bold blue] {path}", title="Cortex Ingestion"))
else:
project_path = os.path.abspath(path)
console.print(Panel(f"[bold blue]Indexing Source:[/bold blue] {project_path} ({source_type})", title="Cortex Ingestion"))
with console.status("[bold green]Working on indexing...[/bold green]"):
num_indexed, actual_project_path = ingest_and_index(path, source_type)
console.print(f"\n[bold green]Success![/bold green] Indexed {num_indexed} files.")
if is_github_url:
console.print(f"\n[bold cyan]Repository cloned to:[/bold cyan] {actual_project_path}")
console.print(f"[bold cyan]To chat with this repo, use:[/bold cyan] cortex chat --project {actual_project_path}")
@app.command()
def watch(
path: str = typer.Argument(".", help="Path to the folder to watch for changes")
):
"""
Automatically re-index files as they change.
"""
project_path = os.path.abspath(path)
console.print(Panel(f"[bold blue]Watching Path:[/bold blue] {project_path}", title="Cortex Watcher"))
try:
start_watching(project_path)
except KeyboardInterrupt:
console.print("\n[bold red]Stopping watcher...[/bold red]")
@app.command()
def ask(
query: str = typer.Argument(..., help="The question you want to ask about your codebase"),
project: str = typer.Option(".", "--project", "-p", help="Path to the project to query"),
provider: str | None = typer.Option(None, "--provider", help=PROVIDER_HELP),
model: str | None = typer.Option(None, "--model", "-m", help=MODEL_HELP)
):
"""
Ask a single question about the indexed codebase.
"""
project_path = os.path.abspath(project)
resolved_provider = normalize_provider(provider)
resolved_model = get_default_model(resolved_provider, model)
watcher = start_background_watcher(project_path)
try:
console.print(
Panel(
f"[bold blue]Project:[/bold blue] {project_path}\n"
f"[bold blue]Provider:[/bold blue] {resolved_provider}\n"
f"[bold blue]Model:[/bold blue] {resolved_model}\n"
f"[bold blue]Query:[/bold blue] {query}",
title="Cortex Search",
)
)
orchestrator = Orchestrator(project_path=project_path, provider=resolved_provider, model_name=resolved_model)
with console.status("[bold yellow]Agent is thinking and searching...[/bold yellow]"):
response = orchestrator.ask(query)
console.print("\n[bold green]Cortex Agent:[/bold green]")
console.print(Markdown(response))
if orchestrator.last_trace_path:
console.print(f"[dim]Trace saved to {orchestrator.last_trace_path}[/dim]")
finally:
watcher.stop()
watcher.join()
@app.command()
def chat(
project: str = typer.Option(".", "--project", "-p", help="Path to the project to chat with"),
provider: str | None = typer.Option(None, "--provider", help=PROVIDER_HELP),
model: str | None = typer.Option(None, "--model", "-m", help=MODEL_HELP)
):
"""
Start an interactive chat session with your codebase.
"""
project_path = os.path.abspath(project)
resolved_provider = normalize_provider(provider)
resolved_model = get_default_model(resolved_provider, model)
watcher = start_background_watcher(project_path)
try:
console.print(
Panel(
f"[bold magenta]Welcome to Cortex Chat![/bold magenta]\n"
f"[bold blue]Project:[/bold blue] {project_path}\n"
f"[bold blue]Provider:[/bold blue] {resolved_provider}\n"
f"[bold blue]Model:[/bold blue] {resolved_model}\n"
"Type 'exit' or 'quit' to end the session.",
title="Cortex Interactive",
)
)
orchestrator = Orchestrator(project_path=project_path, provider=resolved_provider, model_name=resolved_model)
thread_id = "session_" + os.urandom(4).hex()
while True:
try:
query = console.input("\n[bold cyan]You :[/bold cyan] ")
except EOFError:
break
if query.lower() in ["exit", "quit"]:
console.print("[bold red]Goodbye![/bold red]")
break
with console.status("[bold yellow]Searching & Thinking...[/bold yellow]"):
response = orchestrator.ask(query, thread_id=thread_id)
console.print(f"\n[bold green]Cortex :[/bold green]")
console.print(Markdown(response))
if orchestrator.last_trace_path:
console.print(f"[dim]Trace saved to {orchestrator.last_trace_path}[/dim]")
finally:
watcher.stop()
watcher.join()
@repo_app.command("list")
def repo_list():
"""
List all cloned GitHub repositories.
"""
repos = list_cloned_repos()
if not repos:
console.print("[yellow]No repositories cloned yet.[/yellow]")
return
table = Table(title="Cloned Repositories")
table.add_column("Name", style="cyan")
for repo in repos:
table.add_row(repo)
console.print(table)
@repo_app.command("delete")
def repo_delete(
name: str = typer.Argument(..., help="Name of the repository to delete")
):
"""
Delete a cloned GitHub repository.
"""
confirm = typer.confirm(f"Are you sure you want to delete the repository '{name}'?")
if not confirm:
console.print("[yellow]Aborted.[/yellow]")
return
if delete_cloned_repo(name):
console.print(f"[bold green]Successfully deleted repository:[/bold green] {name}")
else:
console.print(f"[bold red]Error:[/bold red] Repository '{name}' not found.")
if __name__ == "__main__":
app()