Skip to content

Commit

Permalink
Fix/interractive_4 (raphaelmansuy#16)
Browse files Browse the repository at this point in the history
* first_phase_refactoring

* add nice print help

* update

* fix(code2prompt): r(code2prompt): improve token price display

* update

* fix(.): r: remove unused imports and variables

* fix: Improve README.md

* docs: Improve documentation for code2prompt/main.py

* feat: Add improvements.md file with suggested enhancements

* feat: Implement logging in critical areas of the code

* fix: Add log_error function to logging_utils

* fix: Add log_output_created function to logging_utils.py

* feat: Add log_clipboard_copy and log_token_count functions to logging_utils

* feat: Add colored logging to the terminal

* feat: Add log level configuration in command line

* feat: Add logging level configuration to .code2promptrc and analyze-code.j2

* feat: Improve log message formatting

* fix: Add stderr logging for clipboard copy success and failure

* fix: Set default log level to WARNING

* feat: Add color and emoji to clipboard copy success message

* feat: Add header to token price estimation output

* fix: Correct token price output formatting

* fix: Correct the order of input and output prices in log_token_prices function

* fix: Correct the order of "In" and "Out" tokens in the log_token_prices function

* fix: Correct the formatting of the token price table

* fix/aider(.gitignore): add .aider* to ignore file

* fix(code2prompt): update main.py file

* fix(gitignore): add .ruff_cache to gitignore

* first version

* better version

* fix(code2prompt): enhance interactive command with improved cursor navigation and visible line handling

* fix(code2prompt): add terminal resize handling and improve instructions

* fix(code2prompt): move interactive_command.py to commands directory

* fix(include_loader): import jinja2 to resolve undefined name error

fix(logging_utils): remove unused logger variable

chore(deps): add jinja2 dependency to pyproject.toml

test(analyzer): remove unused imports

test(create_template_directory): remove unused imports

test(include_loader): remove unused imports

test(is_filtered): improve readability and consistency

test(template_include): remove unused imports

* fix(code2prompt/utils): Improve logging and file processing criteria

* fix(code2prompt/commands): improve `get_terminal_height`, `get_directory_tree`, and `format_tree` functions

* fix(code2prompt/commands): improve interactive command page up and down functionality

* fix(code2prompt): add interactive file selector for generate command

* fix(code2prompt/commands): Refactored interactive file selector, added support for multiple paths

* fix(code2prompt): Refactor InteractiveFileSelector to handle multiple paths

* fix(code2prompt/commands): improve interactive file selector responsiveness to terminal resize events

* fix(code2prompt): Introduce file_path_retriever module to handle file path filtering and processing

* fix(code2prompt/commands): improve code organization and documentation in the GenerateCommand class

* fix(code2prompt): Improve performance of `_get_directory_tree` method and remove unnecessary validation for `retrieve_file_paths`

* fix(code2prompt/commands): handle invalid or missing paths in interactive selector

* fix(code2prompt/commands): create and use private methods to handle key bindings and application creation

* fix(code2prompt/commands): update interactive file selector to use Path objects

* fix(code2prompt): improve interactive file selector behavior

* fix(code2prompt/commands): improve directory tree generation and formatting

* fix(code2prompt/commands): improve interactive file selector
  • Loading branch information
raphaelmansuy authored and CTY-git committed Sep 23, 2024
1 parent 7a5343d commit c8647d4
Show file tree
Hide file tree
Showing 42 changed files with 2,176 additions and 710 deletions.
8 changes: 6 additions & 2 deletions .code2promptrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"suppress_comments": false,
"line_number": false
}
"line_number": false,
"log_level": "INFO",
"encoding": "cl100k_base",
"filter": "*.py,*.js",
"exclude": "tests/*,docs/*"
}
31 changes: 31 additions & 0 deletions .cursorrules
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

# Role Overview

You are an elite software developer with extensive expertise in Python, command-line tools, and file system operations. Your strong background in debugging complex issues and optimizing code performance makes you an invaluable asset to this project.

## Key Attributes

- **Pragmatic Approach**: You prioritize delivering high-quality, maintainable code that meets project requirements.
- **Modular Design**: You embrace composability and modularity, ensuring that your code is easy to extend and maintain.
- **Principled Coding**: You adhere to the KISS (Keep It Simple, Stupid) and DRY (Don't Repeat Yourself) principles, promoting simplicity and efficiency.
- **Documentation & Testing**: You recognize the importance of clear documentation and thorough testing to guarantee the reliability of your work.
- **Functional Preference**: You prefer using functions and modules over classes, focusing on functional programming paradigms.

## Technological Stack

This project utilizes the following technologies:

- **Python Version**: 3.6+
- **Dependencies**:
- `python = "^3.8,<4.0"`
- `rich = "^13.7.1"` # For rich text and beautiful formatting
- `click = "^8.1.7"` # For creating elegant command-line interfaces
- `jinja2 = "^3.1.4"` # For template rendering
- `prompt-toolkit = "^3.0.47"` # For building powerful interactive command-line applications
- `tiktoken = "^0.7.0"` # For tokenization tasks
- `pyperclip = "^1.9.0"` # For clipboard operations
- `colorama = "^0.4.6"` # For colored terminal text output
- `tqdm = "^4.66.4"` # For progress bars
- `tabulate = "^0.9.0"` # For tabular data formatting
- `pydantic` # For data validation and type checking
- `poetry` # For dependency management
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ __pycache__
*.pyc
.DS_Store
.tasks
cli.log
cli.log
.aider*
.ruff_cache
84 changes: 46 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@

Code2Prompt is a powerful command-line tool that generates comprehensive prompts from codebases, designed to streamline interactions between developers and Large Language Models (LLMs) for code analysis, documentation, and improvement tasks.


## Table of Contents

1. [Why Code2Prompt?](#why-code2prompt)
2. [Features](#features)
3. [Installation](#installation)
4. [Quick Start](#quick-start)
5. [Usage](#usage)
6. [Options](#options)
7. [Examples](#examples)
8. [Templating System](#templating-system)
9. [Integration with LLM CLI](#integration-with-llm-cli)
10. [GitHub Actions Integration](#github-actions-integration)
11. [Configuration File](#configuration-file)
12. [Troubleshooting](#troubleshooting)
13. [Contributing](#contributing)
14. [License](#license)
4. [Getting Started](#getting-started)
5. [Quick Start](#quick-start)
6. [Usage](#usage)
7. [Options](#options)
8. [Examples](#examples)
9. [Templating System](#templating-system)
10. [Integration with LLM CLI](#integration-with-llm-cli)
11. [GitHub Actions Integration](#github-actions-integration)
12. [Configuration File](#configuration-file)
13. [Troubleshooting](#troubleshooting)
14. [Contributing](#contributing)
15. [License](#license)

# Code2Prompt: Transform Your Codebase into AI-Ready Prompts

Expand All @@ -42,25 +42,25 @@ Code2Prompt is a powerful, open-source command-line tool that bridges the gap be

### 🚀 Key Features

- **Holistic Codebase Representation**: Generate a well-structured Markdown prompt that captures your entire project's essence.
- **Intelligent Source Tree Generation**: Create a clear, hierarchical view of your codebase structure.
- **Customizable Prompt Templates**: Tailor your output using Jinja2 templates to suit specific AI tasks.
- **Smart Token Management**: Count and optimize tokens to ensure compatibility with various LLM token limits.
- **Gitignore Integration**: Respect your project's .gitignore rules for accurate representation.
- **Flexible File Handling**: Filter and exclude files using powerful glob patterns.
- **Clipboard Ready**: Instantly copy generated prompts to your clipboard for quick AI interactions.
- **Multiple Output Options**: Save to file or display in the console.
- **Enhanced Code Readability**: Add line numbers to source code blocks for precise referencing.
- **Include file**: Support of template import
- **Input variables**: Support of Input Variables in templates.
- **Holistic Codebase Representation**: Generate a well-structured Markdown prompt that captures your entire project's essence, making it easier for LLMs to understand the context.
- **Intelligent Source Tree Generation**: Create a clear, hierarchical view of your codebase structure, allowing for better navigation and understanding of the project.
- **Customizable Prompt Templates**: Tailor your output using Jinja2 templates to suit specific AI tasks, enhancing the relevance of generated prompts.
- **Smart Token Management**: Count and optimize tokens to ensure compatibility with various LLM token limits, preventing errors during processing.
- **Gitignore Integration**: Respect your project's .gitignore rules for accurate representation, ensuring that irrelevant files are excluded from processing.
- **Flexible File Handling**: Filter and exclude files using powerful glob patterns, giving you control over which files are included in the prompt generation.
- **Clipboard Ready**: Instantly copy generated prompts to your clipboard for quick AI interactions, streamlining your workflow.
- **Multiple Output Options**: Save to file or display in the console, providing flexibility in how you want to use the generated prompts.
- **Enhanced Code Readability**: Add line numbers to source code blocks for precise referencing, making it easier to discuss specific parts of the code.
- **Include file**: Support of template import, allowing for modular template design.
- **Input variables**: Support of Input Variables in templates, enabling dynamic prompt generation based on user input.

### 💡 Why Code2Prompt?

- **Contextual Understanding**: Provide LLMs with a comprehensive view of your project for more accurate suggestions and analysis.
- **Consistency Boost**: Maintain coding style and conventions across your entire project.
- **Efficient Refactoring**: Enable better interdependency analysis and smarter refactoring recommendations.
- **Improved Documentation**: Generate contextually relevant documentation that truly reflects your codebase.
- **Pattern Recognition**: Help LLMs learn and apply your project-specific patterns and idioms.
- **Consistency Boost**: Maintain coding style and conventions across your entire project, improving code quality.
- **Efficient Refactoring**: Enable better interdependency analysis and smarter refactoring recommendations, saving time and effort.
- **Improved Documentation**: Generate contextually relevant documentation that truly reflects your codebase, enhancing maintainability.
- **Pattern Recognition**: Help LLMs learn and apply your project-specific patterns and idioms, improving the quality of AI interactions.

Transform the way you interact with AI for software development. With Code2Prompt, harness the full power of your codebase in every AI conversation.

Expand All @@ -81,6 +81,20 @@ pip install code2prompt
pipx install code2prompt
```

## Getting Started

To get started with Code2Prompt, follow these steps:

1. **Install Code2Prompt**: Use one of the installation methods mentioned above.
2. **Prepare Your Codebase**: Ensure your project is organized and that you have a `.gitignore` file if necessary.
3. **Run Code2Prompt**: Use the command line to generate prompts from your codebase.

For example, to generate a prompt from a single Python file, run:

```bash
code2prompt --path /path/to/your/script.py
```

## Quick Start

1. Generate a prompt from a single Python file:
Expand Down Expand Up @@ -130,7 +144,7 @@ code2prompt --path /path/to/dir1 --path /path/to/file2.py [OPTIONS]
| `--encoding` | | Specify the tokenizer encoding to use (default: "cl100k_base") |
| `--create-templates` | | Create a templates directory with example templates |
| `--version` | `-v` | Show the version and exit |

| `--log-level` | | Set the logging level (e.g., DEBUG, INFO, WARNING, ERROR, CRITICAL) |

## Command Parameters

Expand Down Expand Up @@ -225,7 +239,6 @@ or
By using the `--filter` and `--exclude` options effectively and safely (with proper quoting), you can precisely control which files are processed in your project, ensuring both accuracy and security in your command execution.
## Examples
1. Generate documentation for a Python library:
Expand Down Expand Up @@ -350,7 +363,6 @@ code2prompt --path /your/project --tokens --encoding p50k_base

Understanding token counts is crucial when working with AI models that have token limits, ensuring your prompts fit within the model's context window.


### Token Price Estimation

Code2Prompt now includes a powerful feature for estimating token prices across various AI providers and models. Use the `--price` option in conjunction with `--tokens` to display a comprehensive breakdown of estimated costs. This feature calculates prices based on both input and output tokens, with input tokens determined by your codebase and a default of 1000 output tokens (customizable via `--output-tokens`). You can specify a particular provider or model, or view prices across all available options. This functionality helps developers make informed decisions about AI model usage and cost management. For example:
Expand All @@ -363,7 +375,6 @@ This command will analyze your project, count the tokens, and provide a detailed

![](./docs/screen-example2.png)


## 🔥 Analyzing Codebases

code2prompt now offers a powerful feature to analyze codebases and provide a summary of file extensions. Use the `--analyze` option along with the `-p` (path) option to get an overview of your project's file composition. For example:
Expand Down Expand Up @@ -462,8 +473,6 @@ Start from this codebase:
## The codebase:
<codebase>
```

When you run `code2prompt` with this template, it will automatically detect the `{{input:variable_name}}` patterns and prompt the user to provide values for each variable (extension_name, main_functionality, and target_audience). This allows for flexible and interactive prompt generation, making it easy to create customized AI prompts for various Chrome extension ideas.
Expand All @@ -475,8 +484,7 @@ For example, if a user inputs:

The tool will generate a tailored prompt for an AI to create a detailed plan for this specific Chrome extension. This feature is particularly useful for developers, product managers, or anyone looking to quickly generate customized AI prompts for various projects or ideas.


## 🔥 Feature Highligth "Include File" Feature
## 🔥 Feature Highlight "Include File" Feature

The code2prompt project now supports a powerful "include file" feature, enhancing template modularity and reusability.

Expand Down Expand Up @@ -531,11 +539,11 @@ Example `.code2promptrc`:

## Roadmap

- [ ] Interractive filtering
- [ ] Interactive filtering
- [X] Include system in template to promote re-usability of sub templates.
- [X] Support of input variables
- [ ] Tokens count for Anthropic Models and other models such LLama3 or Mistral
- [X] Cost Estimations for main LLM providers based in token count
- [ ] Tokens count for Anthropic Models and other models such as LLama3 or Mistral
- [X] Cost Estimations for main LLM providers based on token count
- [ ] Integration with [qllm](https://github.com/quantalogic/qllm) (Quantalogic LLM)
- [ ] Embedding of file summary in SQL-Lite
- [ ] Intelligence selection of file based on an LLM
Expand Down
Empty file.
70 changes: 70 additions & 0 deletions code2prompt/commands/analyze.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# code2prompt/commands/analyze.py

from pathlib import Path
from typing import Dict

from code2prompt.commands.base_command import BaseCommand
from code2prompt.utils.analyzer import (
analyze_codebase,
format_flat_output,
format_tree_output,
get_extension_list,
)


class AnalyzeCommand(BaseCommand):
"""Command for analyzing the codebase structure."""

def execute(self) -> None:
"""Execute the analyze command."""
self.logger.info("Analyzing codebase...")

for path in self.config.path:
self._analyze_path(Path(path))

self.logger.info("Analysis complete.")

def _analyze_path(self, path: Path) -> None:
"""
Analyze a single path and output the results.
Args:
path (Path): The path to analyze.
"""
extension_counts, extension_dirs = analyze_codebase(path)

if not extension_counts:
self.logger.warning(f"No files found in {path}")
return

if self.config.format == "flat":
output = format_flat_output(extension_counts)
else:
output = format_tree_output(extension_dirs)

print(output)

print("\nComma-separated list of extensions:")
print(get_extension_list(extension_counts))

if self.config.tokens:
total_tokens = self._count_tokens(extension_counts)
self.logger.info(f"Total tokens in codebase: {total_tokens}")

def _count_tokens(self, extension_counts: Dict[str, int]) -> int:
"""
Count the total number of tokens in the codebase.
Args:
extension_counts (Dict[str, int]): A dictionary of file extensions and their counts.
Returns:
int: The total number of tokens.
"""
total_tokens = 0
for _ext, count in extension_counts.items():
# This is a simplified token count. You might want to implement a more
# sophisticated counting method based on the file type.
total_tokens += count * 100 # Assuming an average of 100 tokens per file

return total_tokens
76 changes: 76 additions & 0 deletions code2prompt/commands/base_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# code2prompt/commands/base_command.py

from abc import ABC, abstractmethod
import logging
from code2prompt.config import Configuration

class BaseCommand(ABC):
"""
Abstract base class for all commands in the code2prompt tool.
This class defines the basic structure and common functionality
for all command classes. It ensures that each command has access
to the configuration and a logger, and defines an abstract execute
method that must be implemented by all subclasses.
Attributes:
config (Configuration): The configuration object for the command.
logger (logging.Logger): The logger instance for the command.
"""

def __init__(self, config: Configuration, logger: logging.Logger):
"""
Initialize the BaseCommand with configuration and logger.
Args:
config (Configuration): The configuration object for the command.
logger (logging.Logger): The logger instance for the command.
"""
self.config = config
self.logger = logger

@abstractmethod
def execute(self) -> None:
"""
Execute the command.
This method must be implemented by all subclasses to define
the specific behavior of each command.
Raises:
NotImplementedError: If the subclass does not implement this method.
"""
raise NotImplementedError("Subclasses must implement execute method")

def log_start(self) -> None:
"""
Log the start of the command execution.
"""
self.logger.info(f"Starting execution of {self.__class__.__name__}")

def log_end(self) -> None:
"""
Log the end of the command execution.
"""
self.logger.info(f"Finished execution of {self.__class__.__name__}")

def handle_error(self, error: Exception) -> None:
"""
Handle and log any errors that occur during command execution.
Args:
error (Exception): The exception that was raised.
"""
self.logger.error(f"Error in {self.__class__.__name__}: {str(error)}", exc_info=True)

def validate_config(self) -> bool:
"""
Validate the configuration for the command.
This method should be overridden by subclasses to perform
command-specific configuration validation.
Returns:
bool: True if the configuration is valid, False otherwise.
"""
return True
Loading

0 comments on commit c8647d4

Please sign in to comment.