๐ง AI-Enhanced Code GenerationMarch 12, 2026โ
Tests passing
Contextual Code Completer
A Python library that provides context-aware code generation by analyzing a project's existing codebase. It integrates with Claude AI to offer intelligent completion, refactoring suggestions, and boilerplate generation based on the existing context of the files. This tool is particularly useful for large projects where maintaining consistent style and functionality across the codebase is challenging.
What It Does
- Analyze Python codebases to extract context.
- Generate code snippets based on queries and context.
- Handle edge cases such as empty directories and API errors.
Installation
Install the required dependencies:
pip install openaiUsage
Run the tool using the CLI:
python contextual_code_completer.py --path /path/to/codebase --query "Generate a new function"Source Code
import os
import openai
import argparse
def analyze_codebase(directory):
"""
Analyzes the codebase in the given directory and extracts context.
Args:
directory (str): Path to the project directory.
Returns:
str: A string representation of the codebase context.
"""
context = []
for root, _, files in os.walk(directory):
for file in files:
if file.endswith('.py'):
file_path = os.path.join(root, file)
try:
with open(file_path, 'r', encoding='utf-8') as f:
code = f.read()
context.append(code)
except Exception as e:
print(f"Error reading file {file_path}: {e}")
return '\n'.join(context)
def generate_code(context, query):
"""
Generates code based on the provided context and query using OpenAI's API.
Args:
context (str): The codebase context.
query (str): The query string describing the desired operation.
Returns:
str: The generated code snippet.
"""
try:
response = openai.Completion.create(
engine="text-davinci-003",
prompt=f"Context:\n{context}\n\nQuery: {query}\n\nGenerated Code:",
max_tokens=150,
temperature=0.7
)
return response["choices"][0]["text"].strip()
except Exception as e:
raise RuntimeError(f"Error generating code: {e}")
def main():
"""
Main function to analyze the codebase and generate code based on the query.
Args:
None
"""
parser = argparse.ArgumentParser(description="Context-aware code generator.")
parser.add_argument('--path', required=True, type=str, help='Path to the project directory.')
parser.add_argument('--query', required=True, type=str, help='Query string for the desired code operation.')
args = parser.parse_args()
try:
print("Analyzing codebase...")
context = analyze_codebase(args.path)
if not context:
print("No Python files found in the specified directory.")
return
print("Generating code...")
generated_code = generate_code(context, args.query)
print("Generated Code:")
print(generated_code)
except RuntimeError as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()Community
Downloads
ยทยทยท
Rate this tool
No ratings yet โ be the first!
Details
- Tool Name
- contextual_code_completer
- Category
- AI-Enhanced Code Generation
- Generated
- March 12, 2026
- Tests
- Passing โ
- Fix Loops
- 2
Quick Install
Clone just this tool:
git clone --depth 1 --filter=blob:none --sparse \ https://github.com/ptulin/autoaiforge.git cd autoaiforge git sparse-checkout set generated_tools/2026-03-12/contextual_code_completer cd generated_tools/2026-03-12/contextual_code_completer pip install -r requirements.txt 2>/dev/null || true python contextual_code_completer.py