CodeXchange CLI
A CLI tool that converts code between programming languages using local LLMs (Ollama), preserving logic, comments, and code style. Supports multi-language conversion with batch processing, syntax verification, and configurable model selection.
Features
- Multi-language Conversion: Convert between JavaScript, TypeScript, Python, and Java
- Comment Preservation: Maintains all comments, docstrings, and inline documentation
- Local LLM Integration: Uses Ollama for privacy and cost-free conversions
- Configurable Models: Choose from available Ollama models
- Batch Processing: Convert entire directories recursively
- Syntax Verification: Validate converted code syntax
Installation
From Source
git clone https://7000pct.gitea.bloupla.net/7000pctAUTO/codexchange-cli.git
cd codexchange-cli
pip install -e .
Prerequisites
- Python 3.10 or higher
- Ollama installed and running locally
- Required Ollama models (e.g.,
codellama,llama2)
Quick Start
-
Make sure Ollama is running:
ollama serve -
Pull a code model:
ollama pull codellama -
Convert a single file:
codexchange convert input.py --from python --to javascript -o output.js -
List available models:
codexchange list-models -
List supported languages:
codexchange list-languages
Configuration
CodeXchange uses a configuration file at ~/.codexchange.yaml:
ollama_host: http://localhost:11434
default_model: codellama
timeout: 300
Environment Variables
Override configuration with environment variables:
| Variable | Description | Default |
|---|---|---|
CODEXCHANGE_OLLAMA_HOST |
Ollama server URL | http://localhost:11434 |
CODEXCHANGE_DEFAULT_MODEL |
Default model name | codellama |
CODEXCHANGE_TIMEOUT |
Request timeout (seconds) | 300 |
Usage
Convert a Single File
codexchange convert <input_file> --from <source_lang> --to <target_lang> [OPTIONS]
Options:
-o, --output FILE: Output file path-m, --model MODEL: Ollama model to use-v, --verify: Verify syntax after conversion
Example:
codexchange convert script.py --from python --to typescript -o script.ts --model codellama
Batch Conversion
Convert all files in a directory:
codexchange batch-convert <directory> --from <source_lang> --to <target_lang> [OPTIONS]
Options:
-o, --output DIRECTORY: Output directory-m, --model MODEL: Ollama model to use-r, --recursive: Recursively process subdirectories (default: True)-c, --concurrency N: Number of concurrent conversions (default: 1)-v, --verify: Verify syntax after conversion
Example:
codexchange batch-convert ./src --from python --to javascript --output ./dest -c 2
List Available Models
codexchange list-models
List Supported Languages
codexchange list-languages
Supported Languages
| Language | Extensions |
|---|---|
| JavaScript | .js, .jsx |
| TypeScript | .ts, .tsx |
| Python | .py |
| Java | .java |
Supported Conversions
- JavaScript <-> TypeScript
- JavaScript <-> Python
- JavaScript <-> Java
- TypeScript <-> Python
- TypeScript <-> Java
- Python <-> Java
Examples
Convert Python to JavaScript
# input.py
def greet(name: str) -> str:
"""Return a greeting message."""
return f"Hello, {name}!"
// output.js
function greet(name) {
/** Return a greeting message. */
return `Hello, ${name}!`;
}
Convert TypeScript to Python
// input.ts
interface User {
id: number;
name: string;
}
function getUser(id: number): User {
return { id, name: "User" };
}
# output.py
class User:
def __init__(self, id: int, name: str):
self.id = id
self.name = name
def get_user(id: int) -> User:
return User(id=id, name="User")
Batch Convert with Syntax Verification
codexchange batch-convert ./python-src --from python --to javascript --output ./js-dest --verify -c 4
Syntax Verification
The --verify flag enables post-conversion syntax checking:
- Python: Uses
ast.parse()for validation - TypeScript: Uses
tsc --noEmit(requires TypeScript) - JavaScript: Uses ESLint (requires Node.js)
- Java: Basic brace/parentheses matching
Warnings are displayed but don't block conversion completion.
Development
Running Tests
pytest tests/ -v
Type Checking
mypy src/codexchange/
Linting
ruff check src/codexchange/ tests/
Troubleshooting
Ollama connection failed
Error: Could not connect to Ollama at http://localhost:11434
Solution:
- Make sure Ollama is running:
ollama serve - Verify host URL in config:
~/.codexchange.yaml - Test connection:
codexchange list-models
Model not found
Error: model not found
Solution:
- List available models:
codexchange list-models - Pull the model:
ollama pull <model-name> - Specify a different model:
codexchange convert ... --model <model>
Invalid language pair
Error: Conversion from X to Y is not supported
Solution:
- Check supported languages:
codexchange list-languages - Verify both source and target languages are valid
Conversion timeout
Error: Request timed out
Solution:
- Increase timeout in config:
timeout: 600 - Use a smaller model (e.g.,
codellamainstead ofllama2) - Simplify the code being converted
Syntax verification failed
Error: Syntax warnings after conversion
Solution:
- Review the converted code for errors
- Try a different model
- Manually fix any issues
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests:
pytest tests/ -v - Submit a pull request
License
MIT License - see LICENSE file for details.