hide previous undo button during streaming

This commit is contained in:
Paul Gauthier 2024-04-27 11:27:16 -07:00
parent 1b6f038590
commit c32666b1c5

View file

@ -3,7 +3,6 @@
import os import os
import random import random
import sys import sys
from pathlib import Path
import streamlit as st import streamlit as st
@ -12,12 +11,10 @@ from aider.dump import dump # noqa: F401
from aider.main import main as cli_main from aider.main import main as cli_main
# st.cache_data def init_state(key, val):
def get_diff(): if key in st.session_state:
return Path("/Users/gauthier/Projects/aider/aider/tmp.diff").read_text() return
setattr(st.session_state, key, val)
diff = get_diff()
def recent_msgs(): def recent_msgs():
@ -76,6 +73,7 @@ class GUI:
def show_edit_info(self, edit): def show_edit_info(self, edit):
commit_hash = edit.get("commit_hash") commit_hash = edit.get("commit_hash")
commit_message = edit.get("commit_message") commit_message = edit.get("commit_message")
diff = edit.get("diff")
fnames = edit.get("fnames") fnames = edit.get("fnames")
if fnames: if fnames:
fnames = sorted(fnames) fnames = sorted(fnames)
@ -98,10 +96,24 @@ class GUI:
fnames = ", ".join(fnames) fnames = ", ".join(fnames)
res += f"Applied edits to {fnames}." res += f"Applied edits to {fnames}."
with st.container(border=True): if diff:
st.write(res) with st.expander(res):
if show_undo: st.code(diff, language="diff")
st.button(f"Undo commit `{commit_hash}`", key=f"undo_{commit_hash}") if show_undo:
self.add_undo(commit_hash)
else:
with st.container(border=True):
st.write(res)
if show_undo:
self.add_undo(commit_hash)
def add_undo(self, commit_hash):
if self.last_undo_button:
self.last_undo_button.empty()
self.last_undo_button = st.empty()
with self.last_undo_button:
st.button(f"Undo commit `{commit_hash}`", key=f"undo_{commit_hash}")
def do_sidebar(self): def do_sidebar(self):
with st.sidebar: with st.sidebar:
@ -236,18 +248,15 @@ class GUI:
else: else:
st.dict(msg) st.dict(msg)
def init_state(self): def initialize_state(self):
if "messages" not in st.session_state: messages = [{"role": "assistant", "content": "How can I help you?"}]
st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}] init_state("messages", messages)
init_state("recent_msgs_num", 0)
if "recent_msgs_num" not in st.session_state: init_state("last_aider_commit_hash", self.coder.last_aider_commit_hash)
st.session_state.recent_msgs_num = 0
if "last_aider_commit_hash" not in st.session_state:
st.session_state.last_aider_commit_hash = self.coder.last_aider_commit_hash
def __init__(self, coder): def __init__(self, coder):
self.coder = coder self.coder = coder
self.last_undo_button = None
# Force the coder to cooperate, regardless of cmd line args # Force the coder to cooperate, regardless of cmd line args
self.coder.yield_stream = True self.coder.yield_stream = True
@ -255,7 +264,7 @@ class GUI:
self.coder.io.yes = True self.coder.io.yes = True
self.coder.pretty = False self.coder.pretty = False
self.init_state() self.initialize_state()
self.do_sidebar() self.do_sidebar()
self.do_cmd_tab() self.do_cmd_tab()
@ -301,6 +310,13 @@ class GUI:
if st.session_state.last_aider_commit_hash != self.coder.last_aider_commit_hash: if st.session_state.last_aider_commit_hash != self.coder.last_aider_commit_hash:
edit["commit_hash"] = self.coder.last_aider_commit_hash edit["commit_hash"] = self.coder.last_aider_commit_hash
edit["commit_message"] = self.coder.last_aider_commit_message edit["commit_message"] = self.coder.last_aider_commit_message
commits = f"{self.coder.last_aider_commit_hash}~1"
diff = self.coder.repo.diff_commits(
self.coder.pretty,
commits,
self.coder.last_aider_commit_hash,
)
edit["diff"] = diff
st.session_state.last_aider_commit_hash = self.coder.last_aider_commit_hash st.session_state.last_aider_commit_hash = self.coder.last_aider_commit_hash
st.session_state.messages.append(edit) st.session_state.messages.append(edit)