Skip to content

Feature/edit code #830

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions interpreter/cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import argparse
import subprocess
import os
import platform
import pkg_resources
import appdirs
from ..utils.display_markdown_message import display_markdown_message
from ..utils.open_file import open_file
from ..terminal_interface.conversation_navigator import conversation_navigator

arguments = [
Expand Down Expand Up @@ -114,16 +112,9 @@ def cli(interpreter):
config_dir = appdirs.user_config_dir("Open Interpreter")
config_path = os.path.join(config_dir, 'config.yaml')
print(f"Opening `{config_path}`...")
# Use the default system editor to open the file
if platform.system() == 'Windows':
os.startfile(config_path) # This will open the file with the default application, e.g., Notepad
else:
try:
# Try using xdg-open on non-Windows platforms
subprocess.call(['xdg-open', config_path])
except FileNotFoundError:
# Fallback to using 'open' on macOS if 'xdg-open' is not available
subprocess.call(['open', config_path])

open_file(file_path=config_path)

return

# TODO Implement model explorer
Expand Down
Empty file.
25 changes: 25 additions & 0 deletions interpreter/code_interpreters/languages/utils/language_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from ...language_map import language_map


def get_language_file_extension(language_name):
"""
Get the file extension for a given language
"""
language = language_map[language_name.lower()]

if language.file_extension:
return language.file_extension
else:
return language


def get_language_proper_name(language_name):
"""
Get the proper name for a given language
"""
language = language_map[language_name.lower()]

if language.proper_name:
return language.proper_name
else:
return language
15 changes: 8 additions & 7 deletions interpreter/core/respond.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ def respond(interpreter):
print("Running code:", interpreter.messages[-1])

try:
# Yield a message, such that the user can stop code execution if they want to
try:
yield {"executing": {"code": interpreter.messages[-1]["code"], "language": interpreter.messages[-1]["language"]}}
except GeneratorExit:
# The user might exit here.
# We need to tell python what we (the generator) should do if they exit
break

# What code do you want to run?
code = interpreter.messages[-1]["code"]

Expand All @@ -105,13 +113,6 @@ def respond(interpreter):
interpreter._code_interpreters[language] = create_code_interpreter(language)
code_interpreter = interpreter._code_interpreters[language]

# Yield a message, such that the user can stop code execution if they want to
try:
yield {"executing": {"code": code, "language": language}}
except GeneratorExit:
# The user might exit here.
# We need to tell python what we (the generator) should do if they exit
break

# Yield each line, also append it to last messages' output
interpreter.messages[-1]["output"] = ""
Expand Down
125 changes: 86 additions & 39 deletions interpreter/terminal_interface/terminal_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ..utils.display_markdown_message import display_markdown_message
from ..utils.truncate_output import truncate_output
from ..utils.scan_code import scan_code
from ..utils.edit_code import edit_code


def terminal_interface(interpreter, message):
Expand Down Expand Up @@ -92,50 +93,96 @@ def terminal_interface(interpreter, message):
active_block.active_line = chunk["active_line"]

# Execution notice

## break_outer_loop var is used to signal another break
## once we exit the following while-loop
break_outer_loop = False
if "executing" in chunk:
if not interpreter.auto_run:
# OI is about to execute code. The user wants to approve this
while True:
if not interpreter.auto_run:
# OI is about to execute code. The user wants to approve this

# End the active block so you can run input() below it
active_block.end()

should_scan_code = False
# End the active block so you can run input() below it
active_block.end()

if not interpreter.safe_mode == "off":
if interpreter.safe_mode == "auto":
should_scan_code = True
elif interpreter.safe_mode == 'ask':
response = input(" Would you like to scan this code? (y/n)\n\n ")
print("") # <- Aesthetic choice
should_scan_code = False

if response.strip().lower() == "y":
if not interpreter.safe_mode == "off":
if interpreter.safe_mode == "auto":
should_scan_code = True

if should_scan_code:
# Get code language and actual code from the chunk
# We need to give these to semgrep when we start our scan
language = chunk["executing"]["language"]
code = chunk["executing"]["code"]

scan_code(code, language, interpreter)

response = input(" Would you like to run this code? (y/n)\n\n ")
print("") # <- Aesthetic choice

if response.strip().lower() == "y":
# Create a new, identical block where the code will actually be run
# Conveniently, the chunk includes everything we need to do this:
active_block = CodeBlock()
active_block.margin_top = False # <- Aesthetic choice
active_block.language = chunk["executing"]["language"]
active_block.code = chunk["executing"]["code"]
else:
# User declined to run code.
interpreter.messages.append({
"role": "user",
"message": "I have declined to run this code."
})
break
elif interpreter.safe_mode == 'ask':
response = input(" Would you like to scan this code? (y/n)\n\n ")
print("") # <- Aesthetic choice

if response.strip().lower() == "y":
should_scan_code = True

if should_scan_code:
# Get code language and actual code from the chunk
# We need to give these to semgrep when we start our scan
language = chunk["executing"]["language"]
code = chunk["executing"]["code"]

scan_code(code, language, interpreter)

response = input(" Would you like to run this code? (y/n/%edit)\n\n ")
print("") # <- Aesthetic choice

if response.strip().lower() == "%edit":
# open a code editor with this code in a temporary file
# when the user saves and exits, run the code
# Get code language and actual code from the chunk
# We need to give these to our editor to open the file
language = chunk["executing"]["language"]
code = chunk["executing"]["code"]

edited_code = edit_code(code, language, interpreter)

# Get the last message, so we can extend it with the edited code
old_message = interpreter.messages[-1]

# Remove the last message, which is the code we're about to edit
interpreter.messages = interpreter.messages[:-1]

ran_code_block = False
render_cursor = False

chunk["executing"]["code"] = edited_code

active_block.end()
active_block = CodeBlock()
active_block.language = chunk["executing"]["language"]
active_block.code = edited_code
active_block.end()

interpreter.messages.append({
**old_message,
"code": edited_code
})


continue

elif response.strip().lower() == "y":
# Create a new, identical block where the code will actually be run
# Conveniently, the chunk includes everything we need to do this:
active_block = CodeBlock()
active_block.margin_top = False # <- Aesthetic choice
active_block.language = chunk["executing"]["language"]
active_block.code = chunk["executing"]["code"]
break
else:
# User declined to run code.
interpreter.messages.append({
"role": "user",
"message": "I have declined to run this code."
})
## need to break twice since goal is to exit nested while-loop
## and parent for-loop and return to interactive input.
break_outer_loop = True
break
if break_outer_loop:
break

# Output
if "output" in chunk:
Expand Down
64 changes: 64 additions & 0 deletions interpreter/utils/edit_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import os
from yaspin import yaspin

from .temporary_file import create_temporary_file, cleanup_temporary_file
from .open_file import open_file
from ..code_interpreters.languages.utils.language_tools import get_language_file_extension, get_language_proper_name


def edit_code(code, language, interpreter):
"""
Edit the code and listen for changes with watchdog
"""

temp_code = code

temp_file = create_temporary_file(
temp_code, get_language_file_extension(language), verbose=interpreter.debug_mode
)

language_name = get_language_proper_name(language)

file_name = os.path.basename(temp_file)

if interpreter.debug_mode:
print(f"Editing {language_name} code in {file_name}")
print("---")

# Run semgrep
try:
print(" Press `ENTER` after you've saved your edits.")

open_file(temp_file)

with yaspin(text=f" Editing {language_name} code...").green.right.dots as loading:
# HACK: we're just listening for the user to come back and hit Enter
# but we aren't actually doing anything with it and since we're inside
# of a yaspin handler, the input prompt doesn't actually render
done = input(" Press `ENTER` when you're ready to continue:")

loading.stop()
loading.hide()

if done == "":
print(f" {language_name} code updated.")
print("") # <- Aesthetic choice

temp_code = open(temp_file).read()

if interpreter.debug_mode:
print(f"Getting updated {language_name} code from {file_name}")
print("---")

cleanup_temporary_file(temp_file, verbose=interpreter.debug_mode)

if interpreter.debug_mode:
print(f"Deleting {file_name}")
print("---")

except Exception as e:
print(f"Could not edit {language} code.")
print(e)
print("") # <- Aesthetic choice

return temp_code
14 changes: 14 additions & 0 deletions interpreter/utils/open_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os
import platform
import subprocess

def open_file(file_path):
if platform.system() == 'Windows':
os.startfile(file_path) # This will open the file with the default application, e.g., Notepad
else:
try:
# Try using xdg-open on non-Windows platforms
subprocess.call(['xdg-open', file_path])
except FileNotFoundError:
# Fallback to using 'open' on macOS if 'xdg-open' is not available
subprocess.call(['open', file_path])
27 changes: 1 addition & 26 deletions interpreter/utils/scan_code.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,9 @@
import os
import subprocess
from yaspin import yaspin
from yaspin.spinners import Spinners

from .temporary_file import create_temporary_file, cleanup_temporary_file
from ..code_interpreters.language_map import language_map


def get_language_file_extension(language_name):
"""
Get the file extension for a given language
"""
language = language_map[language_name.lower()]

if language.file_extension:
return language.file_extension
else:
return language


def get_language_proper_name(language_name):
"""
Get the proper name for a given language
"""
language = language_map[language_name.lower()]

if language.proper_name:
return language.proper_name
else:
return language
from ..code_interpreters.languages.utils.language_tools import get_language_file_extension, get_language_proper_name


def scan_code(code, language, interpreter):
Expand Down