-
-
Notifications
You must be signed in to change notification settings - Fork 19
Added cortex CLI messaging and documentation Fixes #11 #191
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
9 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 0e096ea
issue #11 - Enhance cortex CLI and tests
Sahilbhatane f3c7bb5
issue #11 - Enhance cortex CLI and tests
Sahilbhatane cdc0349
Update test/test_cli.py
mikejmorgan-ai 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 | ||
| recursive-include cortex *.py | ||
| 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,203 @@ | ||
| """Command-line interface entry point for the Cortex automation toolkit.""" | ||
|
|
||
| import argparse | ||
| import os | ||
| import subprocess | ||
| import sys | ||
| import time | ||
| from typing import Optional | ||
|
|
||
| from LLM.interpreter import CommandInterpreter | ||
| from cortex.coordinator import InstallationCoordinator, StepStatus | ||
|
|
||
|
|
||
| class CortexCLI: | ||
| """Command-line interface for Cortex AI-powered software installation.""" | ||
|
|
||
| def __init__(self) -> None: | ||
| """Initialise spinner state used for interactive progress updates.""" | ||
| self.spinner_chars = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'] | ||
| self.spinner_idx = 0 | ||
|
|
||
| def _get_api_key(self) -> Optional[str]: | ||
| """Return the configured API key or emit an error if missing.""" | ||
| 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: | ||
| """Detect which LLM provider to use based on available credentials.""" | ||
| if os.environ.get('OPENAI_API_KEY'): | ||
| return 'openai' | ||
| if os.environ.get('ANTHROPIC_API_KEY'): | ||
| return 'claude' | ||
| return 'openai' | ||
|
|
||
| def _print_status(self, label: str, message: str) -> None: | ||
| """Emit informational output with a consistent status label.""" | ||
| print(f"{label} {message}") | ||
|
|
||
| def _print_error(self, message: str) -> None: | ||
| """Emit an error message to ``stderr`` with standard formatting.""" | ||
| print(f"[ERROR] {message}", file=sys.stderr) | ||
|
|
||
| def _print_success(self, message: str) -> None: | ||
| """Emit a success message to ``stdout`` with the success label.""" | ||
| print(f"[SUCCESS] {message}") | ||
|
|
||
| def _animate_spinner(self, message: str) -> None: | ||
| """Render a single spinner frame with the supplied ``message``.""" | ||
| 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) -> None: | ||
| """Clear the active terminal line to hide spinner artifacts.""" | ||
| sys.stdout.write('\r\033[K') | ||
| sys.stdout.flush() | ||
|
|
||
| def install(self, software: str, execute: bool = False, dry_run: bool = False) -> int: | ||
| """Interpret a natural-language request and optionally execute the plan.""" | ||
|
|
||
| api_key = self._get_api_key() | ||
| if not api_key: | ||
| return 1 | ||
|
|
||
| provider = self._get_provider() | ||
|
|
||
| try: | ||
| self._print_status("[INFO]", "Understanding request...") | ||
|
|
||
| interpreter = CommandInterpreter(api_key=api_key, provider=provider) | ||
|
|
||
| self._print_status("[PLAN]", "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("[EXEC]", f"Installing {software}...") | ||
| print("\nGenerated commands:") | ||
| for index, command in enumerate(commands, 1): | ||
| print(f" {index}. {command}") | ||
|
|
||
| if dry_run: | ||
| print("\n(Dry run mode - commands not executed)") | ||
| return 0 | ||
|
|
||
| if execute: | ||
| def progress_callback(current: int, total: int, step) -> None: | ||
| status_label = "[PENDING]" | ||
| if step.status == StepStatus.SUCCESS: | ||
| status_label = "[OK]" | ||
| elif step.status == StepStatus.FAILED: | ||
| status_label = "[FAIL]" | ||
| print(f"\n[{current}/{total}] {status_label} {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 | ||
|
|
||
| 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 | ||
|
|
||
| print("\nTo execute these commands, run with --execute flag") | ||
| print("Example: cortex install docker --execute") | ||
| return 0 | ||
|
|
||
| except ValueError as exc: | ||
| self._print_error(str(exc)) | ||
| return 1 | ||
| except RuntimeError as exc: | ||
| self._print_error(f"API call failed: {str(exc)}") | ||
| return 1 | ||
| except Exception as exc: | ||
| self._print_error(f"Unexpected error: {str(exc)}") | ||
| return 1 | ||
|
|
||
|
|
||
| def main() -> int: | ||
| """Entry point for the cortex CLI command.""" | ||
|
|
||
| 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 | ||
| cortex --test | ||
|
|
||
| Environment Variables: | ||
| OPENAI_API_KEY OpenAI API key for GPT-4 | ||
| ANTHROPIC_API_KEY Anthropic API key for Claude | ||
| """ | ||
| ) | ||
|
|
||
| parser.add_argument('--test', action='store_true', help='Run all test suites') | ||
|
|
||
| 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 args.test: | ||
| test_dir = os.path.join(os.path.dirname(__file__), '..', 'test') | ||
| test_runner = os.path.join(test_dir, 'run_all_tests.py') | ||
|
|
||
| if not os.path.exists(test_runner): | ||
| print("[ERROR] Test runner not found", file=sys.stderr) | ||
| return 1 | ||
|
|
||
| result = subprocess.run([sys.executable, test_runner]) | ||
| return result.returncode | ||
|
|
||
| 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.