forked from adilzubair/cortex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
159 lines (133 loc) · 6.01 KB
/
Copy pathmain.py
File metadata and controls
159 lines (133 loc) · 6.01 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
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
import os
from dotenv import load_dotenv
load_dotenv()
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()
@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 = typer.Option("ollama", "--provider", help="LLM Provider: 'ollama' or 'openai'"),
model: str = typer.Option(None, "--model", "-m", help="Specific model name")
):
"""
Ask a single question about the indexed codebase.
"""
project_path = os.path.abspath(project)
watcher = start_background_watcher(project_path)
try:
console.print(Panel(f"[bold blue]Project:[/bold blue] {project_path}\n[bold blue]Query:[/bold blue] {query}", title="Cortex Search"))
orchestrator = Orchestrator(project_path=project_path, provider=provider, model_name=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))
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 = typer.Option("openai", "--provider", help="LLM Provider: 'ollama' or 'openai'"),
model: str = typer.Option("gpt-4o-mini", "--model", "-m", help="Specific model name")
):
"""
Start an interactive chat session with your codebase.
"""
project_path = os.path.abspath(project)
watcher = start_background_watcher(project_path)
try:
console.print(Panel(f"[bold magenta]Welcome to Cortex Chat![/bold magenta]\n[bold blue]Project:[/bold blue] {project_path}\nType 'exit' or 'quit' to end the session.", title="Cortex Interactive"))
orchestrator = Orchestrator(project_path=project_path, provider=provider, model_name=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))
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()