feat: Add files to the chat asynchronously.

This commit is contained in:
Adam Albastov 2024-07-02 20:49:05 +02:00
parent 3953db340a
commit 912bfd1538
No known key found for this signature in database
GPG key ID: EDC12C536A39E0A1

View file

@ -3,10 +3,12 @@
import os import os
import random import random
import sys import sys
import asyncio
import streamlit as st import streamlit as st
from aider import urls from aider import urls
from streamlit.runtime.scriptrunner import add_script_run_ctx
from aider.coders import Coder from aider.coders import Coder
from aider.dump import dump # noqa: F401 from aider.dump import dump # noqa: F401
from aider.io import InputOutput from aider.io import InputOutput
@ -181,10 +183,16 @@ class GUI:
self.do_add_web_page() self.do_add_web_page()
def do_add_files(self): def do_add_files(self):
if 'files_being_added' not in st.session_state:
st.session_state.files_being_added = set()
all_files = self.coder.get_all_relative_files()
current_files = self.coder.get_inchat_relative_files()
fnames = st.multiselect( fnames = st.multiselect(
"Add files to the chat", "Add files to the chat",
self.coder.get_all_relative_files(), all_files,
default=self.state.initial_inchat_files, default=list(current_files),
placeholder="Files to edit", placeholder="Files to edit",
disabled=self.prompt_pending(), disabled=self.prompt_pending(),
help=( help=(
@ -194,15 +202,23 @@ class GUI:
) )
for fname in fnames: for fname in fnames:
if fname not in self.coder.get_inchat_relative_files(): if fname not in current_files and fname not in st.session_state.files_being_added:
self.coder.add_rel_fname(fname) st.session_state.files_being_added.add(fname)
self.info(f"Added {fname} to the chat") asyncio.run(self.add_file_async(fname))
for fname in self.coder.get_inchat_relative_files(): for fname in current_files:
if fname not in fnames: if fname not in fnames:
self.coder.drop_rel_fname(fname) self.coder.drop_rel_fname(fname)
self.info(f"Removed {fname} from the chat") self.info(f"Removed {fname} from the chat")
async def add_file_async(self, fname):
try:
self.coder.add_rel_fname(fname)
self.info(f"Added {fname} to the chat")
finally:
st.session_state.files_being_added.remove(fname)
st.rerun()
def do_add_web_page(self): def do_add_web_page(self):
with st.popover("Add a web page to the chat"): with st.popover("Add a web page to the chat"):
self.do_web() self.do_web()