From fa265186254077ac0bb6ed23b62a0cf986abb1f2 Mon Sep 17 00:00:00 2001 From: killian <63927363+KillianLucas@users.noreply.github.com> Date: Thu, 14 Nov 2024 00:38:17 -0800 Subject: [PATCH] Better shell integration --- interpreter_1/cli.py | 4 +-- pyproject.toml | 1 + scripts/install.sh | 52 ++++++++++++++++++++++++++++++---- scripts/{setup.py => shell.py} | 12 +++++--- 4 files changed, 57 insertions(+), 12 deletions(-) rename scripts/{setup.py => shell.py} (95%) diff --git a/interpreter_1/cli.py b/interpreter_1/cli.py index 9e6b2f702..cf9866e3f 100644 --- a/interpreter_1/cli.py +++ b/interpreter_1/cli.py @@ -252,8 +252,8 @@ async def async_load(): pass print() - # if interpreter.interactive: - # interpreter.chat() # Continue in interactive mode + if interpreter.interactive: + interpreter.chat() # Continue in interactive mode if __name__ == "__main__": diff --git a/pyproject.toml b/pyproject.toml index 9a72db570..7fbef05a0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,6 +33,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry.scripts] i = "interpreter_1.cli:main" interpreter = "interpreter_1.cli:main" +interpreter-shell = "scripts.shell:main" wtf = "scripts.wtf:main" interpreter-classic = "interpreter.terminal_interface.start_terminal_interface:main" diff --git a/scripts/install.sh b/scripts/install.sh index aa1c4398a..739de9e3f 100644 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -141,12 +141,52 @@ fi # Platform-specific final instructions if [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "win32" ]]; then - info "Installation complete! You can now use 'open-interpreter' from a new terminal window." + info "Installation complete! You can now use 'interpreter' from a new terminal window." elif [[ "$OSTYPE" == "darwin"* ]]; then - info "Installation complete! You can now use the 'open-interpreter' command." - info "Make sure $BIN_DIR is in your PATH by adding this to your ~/.zshrc or ~/.bash_profile:" - info " export PATH=\"\$PATH:$BIN_DIR\"" + info "Installation complete! You can now use the 'interpreter' command." + info "Would you like to add $BIN_DIR to your PATH? [y/N] " + read -r add_to_path + if [[ "$add_to_path" =~ ^[Yy]$ ]]; then + if [[ -f "$HOME/.zshrc" ]]; then + echo "export PATH=\"\$PATH:$BIN_DIR\"" >> "$HOME/.zshrc" + info "Added to ~/.zshrc. Please restart your terminal or run: source ~/.zshrc" + elif [[ -f "$HOME/.bash_profile" ]]; then + echo "export PATH=\"\$PATH:$BIN_DIR\"" >> "$HOME/.bash_profile" + info "Added to ~/.bash_profile. Please restart your terminal or run: source ~/.bash_profile" + else + info "Could not find ~/.zshrc or ~/.bash_profile. Please manually add to your shell's config:" + info " export PATH=\"\$PATH:$BIN_DIR\"" + fi + else + info "You can manually add $BIN_DIR to your PATH by adding this to ~/.zshrc or ~/.bash_profile:" + info " export PATH=\"\$PATH:$BIN_DIR\"" + fi else - info "Installation complete! You can now use the 'open-interpreter' command." - info "Make sure $BIN_DIR is in your PATH." + info "Installation complete! You can now use the 'interpreter' command." + info "Would you like to add $BIN_DIR to your PATH? [y/N] " + read -r add_to_path + if [[ "$add_to_path" =~ ^[Yy]$ ]]; then + if [[ -f "$HOME/.bashrc" ]]; then + echo "export PATH=\"\$PATH:$BIN_DIR\"" >> "$HOME/.bashrc" + info "Added to ~/.bashrc. Please restart your terminal or run: source ~/.bashrc" + else + info "Could not find ~/.bashrc. Please manually add to your shell's config:" + info " export PATH=\"\$PATH:$BIN_DIR\"" + fi + else + info "You can manually add $BIN_DIR to your PATH by adding this to ~/.bashrc:" + info " export PATH=\"\$PATH:$BIN_DIR\"" + fi +fi + +# Offer shell integration +info "Would you like to install shell integration? This allows you to use Open Interpreter directly from your shell - if you type an unrecognized command, it will be passed to Open Interpreter with context about your recent shell history. [y/N] " +read -r install_shell_integration +if [[ "$install_shell_integration" =~ ^[Yy]$ ]]; then + if command_exists interpreter-shell; then + interpreter-shell + info "Shell integration installed successfully! Restart your shell to activate it." + else + error "Could not find interpreter-shell command. Please ensure Open Interpreter was installed correctly." + fi fi diff --git a/scripts/setup.py b/scripts/shell.py similarity index 95% rename from scripts/setup.py rename to scripts/shell.py index 4f7733eeb..eaef99674 100644 --- a/scripts/setup.py +++ b/scripts/shell.py @@ -38,7 +38,7 @@ def get_shell_config(): def get_shell_script(shell_type): """Return the appropriate shell script based on shell type.""" - base_script = """# Create log file if it doesn't exist + base_script = r"""# Create log file if it doesn't exist touch ~/.shell_history_with_output # Function to capture terminal interaction @@ -68,11 +68,12 @@ def get_shell_script(shell_type): # Command not found handler that pipes context to interpreter command_not_found_handler() { local cmd=$1 - # echo "user: $cmd" >> ~/.shell_history_with_output - # echo "computer:" >> ~/.shell_history_with_output # Capture output in temp file, display unstripped version, then process and append stripped version output_file=$(mktemp) + # Add trap to handle SIGINT (Ctrl+C) gracefully + trap "rm -f $output_file; return 0" INT + interpreter --input "$(cat ~/.shell_history_with_output)" --instructions "You are in FAST mode. Be ultra-concise. Determine what the user is trying to do (most recently) and help them." 2>&1 | \ tee "$output_file" cat "$output_file" | sed -r \ @@ -82,7 +83,10 @@ def get_shell_script(shell_type): -e "s/^[[:space:]\.]*//;s/[[:space:]\.]*$//" \ -e "s/─{3,}//g" \ >> ~/.shell_history_with_output - rm "$output_file" + + # Clean up and remove the trap + trap - INT + rm -f "$output_file" return 0 }