# Sagacity: Enhanced Codebase Explorer

Sagacity is a powerful command-line tool that leverages the Claude AI language model from Anthropic to assist developers in exploring and understanding their codebase. It provides a user-friendly interface for searching, navigating, and interacting with code files, as well as an interactive chat mode for asking questions about the codebase and receiving contextual responses from the AI.

## Features

### Codebase Indexing

Sagacity performs an in-depth scan of your codebase, indexing all Rust (`*.rs`), Python (`*.py`), Go (`*.go`), TOML (`*.toml`), and Markdown (`*.md`) files. For each indexed file, it generates a concise summary using the Claude AI model, providing a high-level overview of the file's purpose and key functionalities. This indexing process ensures that the relevant information is readily available for efficient searching and querying.

### Search Mode

The search mode allows you to find relevant files based on keywords or phrases. When you enter a search query, Sagacity utilizes a combination of keyword-based relevance and AI-based relevance scoring to identify the most pertinent files. It displays a list of matching files along with their summaries, enabling you to quickly grasp the purpose and contents of each file.

### Chat Mode

The chat mode is a core feature of Sagacity, enabling you to engage in interactive conversations with the Claude AI language model, asking questions about your codebase. The AI responds with contextual information based on the relevant file contents, providing insights, explanations, and guidance tailored to your queries.

When you enter a question in the chat mode, Sagacity performs the following steps:

1. **Find Relevant Files**: It searches the indexed files for those most relevant to your query, using a combination of keyword-based and AI-based relevance scoring.
2. **Extract File Paths and Languages**: The relevant file paths and their respective programming languages are extracted from the indexed data.
3. **Prepare Context**: Sagacity gathers the content of the relevant files and prepares a context summary for the AI, including your query.
4. **Generate LLM Response**: The context summary is sent to the Claude AI language model, which generates a response tailored to your query and the provided context.
5. **Update Conversation History**: The conversation history is updated with your query and the AI's response, allowing you to review and reference past interactions.

### Conversation History

Sagacity keeps track of your conversation history with the AI, allowing you to review past interactions, save conversations to a file (`conversation_history.json`), and load previously saved conversations. This feature enables you to pick up where you left off or revisit previous discussions, enhancing the overall exploration experience.

### Command Palette

The command palette provides a user-friendly way to access various actions within Sagacity. You can easily switch between search mode, chat mode, print the indexed file list, or quit the application, streamlining your workflow and improving productivity.

### Autocompletion

When typing commands or file paths, Sagacity offers autocompletion functionality, making it easier to navigate and interact with the application. This feature enhances the user experience and reduces the potential for errors.

### Syntax Highlighting

Sagacity leverages the `syntect` crate to provide syntax highlighting when displaying file contents. This feature makes it easier to read and understand code snippets, improving the overall code exploration experience.

## Technical Details

Sagacity is written in Rust and utilizes several powerful libraries and crates to provide its functionality:

- **reqwest**: A high-level HTTP client library used for sending requests to the Anthropic API and receiving responses.
- **serde**: A framework for serializing and deserializing Rust data structures efficiently and generically.
- **walkdir**: A library for traversing directories recursively, used for scanning the codebase and indexing files.
- **tokio**: An asynchronous runtime for building reliable, high-performance, and scalable network applications.
- **colored**: A library for colorizing terminal output, used for enhancing the visual appearance of the application.
- **dialoguer**: A library for creating user-friendly command-line interfaces, used for the command palette and user interactions.
- **rustyline**: A readline library for creating interactive command-line interfaces, used for the chat mode and user input handling.
- **indicatif**: A library for creating progress bars and spinners, used for providing visual feedback during codebase indexing and AI processing.
- **syntect**: A library for syntax highlighting and code formatting, used for displaying file contents with syntax highlighting.
- **termimad**: A library for terminal user interface (TUI) development, used for displaying the conversation history and AI responses.
- **clipboard**: A library for interacting with the system clipboard, used for copying AI responses to the clipboard.

Sagacity follows a modular design, with separate modules for codebase indexing, file content summarization, search functionality, chat mode interactions, and user interface components. The codebase is organized in a clear and maintainable manner, making it easier to understand and extend its functionality.

## Installation and Usage

For detailed installation instructions, usage guidelines, and examples, please refer to the [README.md](./README.md) file in the project repository.

## Contributing

Contributions to Sagacity are welcome! If you encounter any issues, have suggestions for improvements, or would like to add new features, please open an issue or submit a pull request on the project's GitHub repository.

## License

Sagacity is released under the [MIT License](LICENSE).