-
Notifications
You must be signed in to change notification settings - Fork 19
Add CLI interface for cortex command - Fixes #11 #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3b5b245
Add CLI interface for cortex command - Fixes #11
Sahilbhatane ae13158
Add multi-step installation coordinator - Fixes #8
Sahilbhatane 04b1f07
Test file update for CLI
Sahilbhatane dfa2794
CLI test integration fix.
Sahilbhatane 7dd7736
Update model selection for OpenAI provider
Sahilbhatane bf047fe
Merge branch 'cortexlinux:main' into main
Sahilbhatane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| __pycache__/ | ||
| *.py[cod] | ||
| *$py.class | ||
| *.so | ||
| .Python | ||
| build/ | ||
| develop-eggs/ | ||
| dist/ | ||
| downloads/ | ||
| eggs/ | ||
| .eggs/ | ||
| lib/ | ||
| lib64/ | ||
| parts/ | ||
| sdist/ | ||
| var/ | ||
| wheels/ | ||
| *.egg-info/ | ||
| .installed.cfg | ||
| *.egg | ||
| MANIFEST | ||
| .env | ||
| .venv | ||
| env/ | ||
| venv/ | ||
| ENV/ | ||
| .mypy_cache/ | ||
| .pytest_cache/ | ||
| .coverage | ||
| htmlcov/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| include README.md | ||
| include LICENSE | ||
| recursive-include LLM *.py | ||
Sahilbhatane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| recursive-include cortex *.py | ||
Sahilbhatane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| include LLM/requirements.txt | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| from .cli import main | ||
| __version__ = "0.1.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| import sys | ||
| import os | ||
| import argparse | ||
| import time | ||
| from typing import List, Optional | ||
Sahilbhatane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import subprocess | ||
Sahilbhatane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) | ||
|
|
||
Sahilbhatane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| from LLM.interpreter import CommandInterpreter | ||
| from cortex.coordinator import InstallationCoordinator, StepStatus | ||
|
|
||
|
|
||
| class CortexCLI: | ||
| def __init__(self): | ||
| self.spinner_chars = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'] | ||
| self.spinner_idx = 0 | ||
Sahilbhatane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def _get_api_key(self) -> Optional[str]: | ||
| api_key = os.environ.get('OPENAI_API_KEY') or os.environ.get('ANTHROPIC_API_KEY') | ||
| if not api_key: | ||
| self._print_error("API key not found. Set OPENAI_API_KEY or ANTHROPIC_API_KEY environment variable.") | ||
| return None | ||
| return api_key | ||
|
|
||
| def _get_provider(self) -> str: | ||
| if os.environ.get('OPENAI_API_KEY'): | ||
| return 'openai' | ||
| elif os.environ.get('ANTHROPIC_API_KEY'): | ||
| return 'claude' | ||
| return 'openai' | ||
|
|
||
| def _print_status(self, emoji: str, message: str): | ||
| print(f"{emoji} {message}") | ||
|
|
||
| def _print_error(self, message: str): | ||
| print(f"❌ Error: {message}", file=sys.stderr) | ||
|
|
||
| def _print_success(self, message: str): | ||
| print(f"✅ {message}") | ||
|
|
||
| def _animate_spinner(self, message: str): | ||
| sys.stdout.write(f"\r{self.spinner_chars[self.spinner_idx]} {message}") | ||
| sys.stdout.flush() | ||
| self.spinner_idx = (self.spinner_idx + 1) % len(self.spinner_chars) | ||
| time.sleep(0.1) | ||
|
|
||
| def _clear_line(self): | ||
| sys.stdout.write('\r\033[K') | ||
| sys.stdout.flush() | ||
|
|
||
| def install(self, software: str, execute: bool = False, dry_run: bool = False): | ||
Sahilbhatane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| api_key = self._get_api_key() | ||
| if not api_key: | ||
| return 1 | ||
|
|
||
| provider = self._get_provider() | ||
|
|
||
| try: | ||
| self._print_status("🧠", "Understanding request...") | ||
|
|
||
| interpreter = CommandInterpreter(api_key=api_key, provider=provider) | ||
|
|
||
| self._print_status("📦", "Planning installation...") | ||
|
|
||
| for _ in range(10): | ||
| self._animate_spinner("Analyzing system requirements...") | ||
| self._clear_line() | ||
|
|
||
| commands = interpreter.parse(f"install {software}") | ||
|
|
||
| if not commands: | ||
| self._print_error("No commands generated. Please try again with a different request.") | ||
| return 1 | ||
|
|
||
| self._print_status("⚙️", f"Installing {software}...") | ||
| print("\nGenerated commands:") | ||
| for i, cmd in enumerate(commands, 1): | ||
| print(f" {i}. {cmd}") | ||
|
|
||
| if dry_run: | ||
| print("\n(Dry run mode - commands not executed)") | ||
| return 0 | ||
|
|
||
| if execute: | ||
| def progress_callback(current, total, step): | ||
| status_emoji = "⏳" | ||
| if step.status == StepStatus.SUCCESS: | ||
| status_emoji = "✅" | ||
| elif step.status == StepStatus.FAILED: | ||
| status_emoji = "❌" | ||
| print(f"\n[{current}/{total}] {status_emoji} {step.description}") | ||
| print(f" Command: {step.command}") | ||
|
|
||
| print("\nExecuting commands...") | ||
|
|
||
| coordinator = InstallationCoordinator( | ||
| commands=commands, | ||
| descriptions=[f"Step {i+1}" for i in range(len(commands))], | ||
| timeout=300, | ||
| stop_on_error=True, | ||
| progress_callback=progress_callback | ||
| ) | ||
|
|
||
| result = coordinator.execute() | ||
|
|
||
| if result.success: | ||
| self._print_success(f"{software} installed successfully!") | ||
| print(f"\nCompleted in {result.total_duration:.2f} seconds") | ||
| return 0 | ||
| else: | ||
| if result.failed_step is not None: | ||
| self._print_error(f"Installation failed at step {result.failed_step + 1}") | ||
| else: | ||
| self._print_error("Installation failed") | ||
| if result.error_message: | ||
| print(f" Error: {result.error_message}", file=sys.stderr) | ||
| return 1 | ||
| else: | ||
| print("\nTo execute these commands, run with --execute flag") | ||
| print("Example: cortex install docker --execute") | ||
|
|
||
| return 0 | ||
|
|
||
| except ValueError as e: | ||
| self._print_error(str(e)) | ||
| return 1 | ||
| except RuntimeError as e: | ||
| self._print_error(f"API call failed: {str(e)}") | ||
| return 1 | ||
| except Exception as e: | ||
| self._print_error(f"Unexpected error: {str(e)}") | ||
| return 1 | ||
|
|
||
|
|
||
| def main(): | ||
Sahilbhatane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| parser = argparse.ArgumentParser( | ||
| prog='cortex', | ||
| description='AI-powered Linux command interpreter', | ||
| formatter_class=argparse.RawDescriptionHelpFormatter, | ||
| epilog=""" | ||
| Examples: | ||
| cortex install docker | ||
| cortex install docker --execute | ||
| cortex install "python 3.11 with pip" | ||
| cortex install nginx --dry-run | ||
| Environment Variables: | ||
| OPENAI_API_KEY OpenAI API key for GPT-4 | ||
| ANTHROPIC_API_KEY Anthropic API key for Claude | ||
| """ | ||
| ) | ||
|
|
||
| subparsers = parser.add_subparsers(dest='command', help='Available commands') | ||
|
|
||
| install_parser = subparsers.add_parser('install', help='Install software using natural language') | ||
| install_parser.add_argument('software', type=str, help='Software to install (natural language)') | ||
| install_parser.add_argument('--execute', action='store_true', help='Execute the generated commands') | ||
| install_parser.add_argument('--dry-run', action='store_true', help='Show commands without executing') | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| if not args.command: | ||
| parser.print_help() | ||
| return 1 | ||
|
|
||
| cli = CortexCLI() | ||
|
|
||
| if args.command == 'install': | ||
| return cli.install(args.software, execute=args.execute, dry_run=args.dry_run) | ||
|
|
||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| sys.exit(main()) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.