File size: 3,318 Bytes
80b95e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2b471a
 
80b95e8
 
 
 
f2b471a
 
 
 
 
 
 
 
 
 
 
 
 
 
80b95e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2b471a
 
 
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
import typer

from src.embedder import embed_documents
from src.retriever import search_documents
from src.generator import generate_commit_message, fallback_commit_message
from src.diff_analyzer import get_staged_diff_chunks
from src.config_loader import load_config


# Initialize Typer app
app = typer.Typer()
config = load_config("config.yaml")
print("Config loaded:", config)


# ===== CLI Commands =====
# `init` β€” Index Documents
@app.command()
def init(path: str = typer.Argument(...)):
    embeddings = embed_documents(path, config)
    if embeddings:
        typer.echo(f"Documents indexed successfully: {len(embeddings)} documents")
    else:
        typer.echo("No documents were indexed.")


# `search` β€” Semantic Lookup
@app.command()
def search(query: str):
    """

    Perform a semantic search on indexed documents.



    Args:

        query (str): The search query string.

    """
    results = search_documents(query, config)
    for i, result in enumerate(results):
        typer.echo(f"[{i+1}] {result}")


# `ask` β€” Retrieval-Augmented Generation (RAG)
@app.command()
def ask(query: str):
    """

    Perform a retrieval-augmented generation (RAG) process to answer a user query.



    Args:

        query (str): The user query string.

    """
    context = search_documents(query, config)
    prompt = "\n".join(context) + f"\n\nUser question: {query}"
    try:
        response = generate_commit_message(prompt, config)
    except Exception as e:
        typer.echo(f"Generation failed: {e}")
        response = fallback_commit_message([])
    typer.echo(response)


# `serve` β€” Start local API server (placeholder)
@app.command()
def serve(port: int = 8000):
    """

    Start a local API server for integrations.



    Args:

        port (int): Port number to serve on. Defaults to 8000.

    """
    typer.echo(f"API server functionality not implemented yet.")
    typer.echo(f"Would start server on port {port}")
    raise typer.Exit(1)


# `commit` β€” Git Diff + Message Generation
@app.command()
def commit(preview: bool = True, apply: bool = False, dry_run: bool = False):
    """

    Generate a commit message based on staged Git diff chunks.



    Args:

        preview (bool): If True, display the suggested commit message.

        apply (bool): If True, apply the commit with the generated message.

        dry_run (bool): If True, display staged files and chunks without generating a message.

    """
    file_list, diff_chunks = get_staged_diff_chunks()

    if dry_run:
        typer.echo(f"Files staged: {len(file_list)}")
        typer.echo(f"Chunks: {len(diff_chunks)}")
        typer.echo(
            f"Estimated tokens: {sum(len(chunk.split()) for chunk in diff_chunks)}"
        )
        return

    prompt = "\n".join(diff_chunks) + "\n\nGenerate a commit message:"
    try:
        message = generate_commit_message(prompt, config)
    except Exception:
        message = fallback_commit_message(file_list)

    if preview:
        typer.echo(f"Suggested commit message:\n{message}")
    if apply:
        import subprocess

        subprocess.run(["git", "commit", "-m", message])


# Entry point for CLI application
if __name__ == "__main__":
    app()