milestone 3 - ExecutionEngine.py and DebugLogger.py: added formats for output and errors

This commit is contained in:
Franziska Gerold 2026-05-27 00:08:39 +02:00
parent 91873a2e1d
commit e2082e99e8
6 changed files with 88 additions and 12 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
# Created by venv; see https://docs.python.org/3/library/venv.html
.env
.venv
__pycache__/

View File

@ -1,8 +1,26 @@
import streamlit as st
import subprocess
error_log = []
def log_error(error_message):
pass
#if "error_log" not in st.session_state:
# st.session_state["log_error"] = ""
#if "code_error" in st.session_state:
error_log.append(error_message)
def format_debug_output(output):
pass
pass
#qwen
def format_output(text: bytes) -> str:
return text.decode('utf-8', errors='replace').strip()
def format_error(exc: Exception) -> str:
msg = ""
if hasattr(exc, 'stderr') and exc.stderr:
msg = exc.stderr.decode('utf-8', errors='replace').strip()
if isinstance(exc, subprocess.CalledProcessError):
msg = f"[Exit: {exc.returncode}] {msg}"
return msg or "Execution failed"

View File

@ -1,6 +1,7 @@
import subprocess
import streamlit as st
from pathlib import Path
from DebugLogger import log_error, format_output, format_error
def run_code(code: Path):
@ -9,21 +10,52 @@ def run_code(code: Path):
if "code_error" in st.session_state:
del st.session_state['code_error']
try:
sub = subprocess.run(["python", code], capture_output=True, check=True)
st.session_state['code_output'] = sub.stdout
sub = subprocess.run(["python", code], capture_output=True, timeout=20, check=True)
st.session_state['code_output'] = format_output(sub.stdout)
except subprocess.CalledProcessError as exc:
st.session_state['code_error'] = (f"{exc.returncode}")
st.session_state['code_error'] = format_error(exc)
log_error(st.session_state['code_error'])
#st.session_state['code_error'] = (f"{exc.returncode}{exc.stderr}")
#log_error(st.session_state['code_error'])
#if subprocess.CalledProcessError.stderr:
#print(vars(subprocess.CalledProcessError))
except subprocess.TimeoutExpired as exc:
st.session_state['code_error'] = format_error(exc)
log_error(st.session_state['code_error'])
#st.session_state['code_error'] = (f"{exc.stderr}")
#log_error(st.session_state['code_error'])
#print(sub.returncode)
#print(sub.check_returncode)
#print(sub.stderr)
#st.session_state['code_error'] = sub.stderr
#brauchts diese Funktion noch? macht eigentlich schon run_code
def capture_output():
errors = st.session_state['code_error']
#if errors == 0:
# errors = "There are no errors"
return st.session_state['code_output'], errors
#qwen
#def run_code(code: Path):
# for key in ('code_output', 'code_error'):
# st.session_state.pop(key, None)
try:
res = subprocess.run(["python", code], capture_output=True, timeout=20, check=True)
st.session_state['code_output'] = format_output(res.stdout)
except subprocess.CalledProcessError as exc:
st.session_state['code_error'] = format_error(exc)
log_error(st.session_state['code_error'])
except subprocess.TimeoutExpired as exc:
st.session_state['code_error'] = format_error(exc)
log_error(st.session_state['code_error'])

View File

@ -22,7 +22,12 @@ def generate_prompt(user_message: str, file_context=None) -> str:
context = (f" <code> {st.session_state['file_content']} </code>")
else:
context = ""
if "code_error" in st.session_state:
context += (f"<error> {st.session_state['code_error']} </error>")
return system_content + task + context
prompt = system_content + task + context
st.session_state["last_prompt"] = (f"{prompt}")
return prompt

11
app.py
View File

@ -5,7 +5,9 @@ from FileManager import list_files, read_file, save_file, open_file
from ExecutionEngine import run_code
"st.session_state object:", st.session_state
"Last sent prompt:",
if "last_prompt" in st.session_state:
st.session_state["last_prompt"]
st.set_page_config(layout="wide")
@ -24,7 +26,7 @@ col_editor_output, col_chat = st.columns([0.6, 0.4], gap="large", vertical_align
with col_editor_output:
if "file_content" in st.session_state:
text = st.text_area("Code Editor", key="file_content")
text = st.text_area("Code Editor", height="stretch", key="file_content")
# Preferred pattern for editor
if st.session_state["selected_file"] != "":
@ -38,9 +40,11 @@ with col_editor_output:
if "code_output" in st.session_state:
st.text_area("Output", key="code_output")
if "code_error" in st.session_state:
st.text_area("Errors", key="code_error")
#st.button("Ask qwen?", key="debug", on_click=send_message, args=["Explain this error message and help me debug it"])
with col_chat:
@ -62,12 +66,15 @@ with col_chat:
# Input & Response
message = st.chat_input("How can qwen help you?", key="chat")
internet = st.toggle("enable internet search")
if message:
st.session_state.messages.append({"role": "user", "content": message})
response = send_message(message)
st.session_state.messages.append({"role": "assistant", "content": response})
st.rerun()
#st.write("Chat with qwen")
#message = st.chat_input("How can qwen help you?", key="chat")
#if message:

View File

@ -1 +1,11 @@
hiHa
from argparse import ArgumentParser
from time import sleep
parser = ArgumentParser()
parser.add_argument("time", type=int)
args = parser.parse_args()
print(f"Starting timer of {args.time} seconds")
for _ in range(args.time):
print(".", end="", flush=True)
sleep(1)
print("Done!")