refactor: Improve error handling and URL processing in chat functionality

This commit is contained in:
Paul Gauthier 2024-10-31 14:13:36 -07:00 committed by Paul Gauthier (aider)
parent 3fcd79e165
commit 17330e53c3
2 changed files with 13 additions and 8 deletions

View file

@ -796,10 +796,8 @@ class Coder:
url_pattern = re.compile(r"(https?://[^\s/$.?#].[^\s]*)") url_pattern = re.compile(r"(https?://[^\s/$.?#].[^\s]*)")
urls = list(set(url_pattern.findall(text))) # Use set to remove duplicates urls = list(set(url_pattern.findall(text))) # Use set to remove duplicates
for url in urls: for url in urls:
dump(url) url = url.rstrip(".',\"")
if self.io.confirm_ask( if self.io.confirm_ask("Open URL for more info about this error?", subject=url):
"Open URL for more info about this error?", subject=url.rstrip(".',")
):
webbrowser.open(url) webbrowser.open(url)
return urls return urls
@ -811,6 +809,7 @@ class Coder:
group = ConfirmGroup(urls) group = ConfirmGroup(urls)
for url in urls: for url in urls:
if url not in self.rejected_urls: if url not in self.rejected_urls:
url = url.rstrip(".',\"")
if self.io.confirm_ask( if self.io.confirm_ask(
"Add URL to the chat?", subject=url, group=group, allow_never=True "Add URL to the chat?", subject=url, group=group, allow_never=True
): ):
@ -1123,6 +1122,8 @@ class Coder:
return chunks return chunks
def send_message(self, inp): def send_message(self, inp):
import openai # for error codes below
self.cur_messages += [ self.cur_messages += [
dict(role="user", content=inp), dict(role="user", content=inp),
] ]
@ -1161,6 +1162,7 @@ class Coder:
self.io.tool_error(err_msg) self.io.tool_error(err_msg)
retry_delay *= 2 retry_delay *= 2
if retry_delay > RETRY_TIMEOUT: if retry_delay > RETRY_TIMEOUT:
self.mdstream = None
self.check_and_open_urls(err_msg) self.check_and_open_urls(err_msg)
break break
self.io.tool_output(f"Retrying in {retry_delay:.1f} seconds...") self.io.tool_output(f"Retrying in {retry_delay:.1f} seconds...")
@ -1190,11 +1192,14 @@ class Coder:
messages.append( messages.append(
dict(role="assistant", content=self.multi_response_content, prefix=True) dict(role="assistant", content=self.multi_response_content, prefix=True)
) )
except Exception as err: except (openai.APIError, openai.APIStatusError) as err:
self.mdstream = None
self.io.tool_error(str(err)) self.io.tool_error(str(err))
self.check_and_open_urls(str(err)) self.check_and_open_urls(str(err))
except Exception as err:
lines = traceback.format_exception(type(err), err, err.__traceback__) lines = traceback.format_exception(type(err), err, err.__traceback__)
self.io.tool_error("".join(lines)) self.io.tool_warning("".join(lines))
self.io.tool_error(str(err))
return return
finally: finally:
if self.mdstream: if self.mdstream:

View file

@ -42,8 +42,8 @@ def retry_exceptions():
openai.UnprocessableEntityError, openai.UnprocessableEntityError,
openai.RateLimitError, openai.RateLimitError,
openai.APIConnectionError, openai.APIConnectionError,
openai.APIError, # openai.APIError,
openai.APIStatusError, # openai.APIStatusError,
openai.InternalServerError, openai.InternalServerError,
) )