Skip to content

Commit

Permalink
Fix VV chat taking over main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
dnakov authored and trufae committed Nov 2, 2024
1 parent 660ca3c commit 096987e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
8 changes: 5 additions & 3 deletions r2ai/ui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "send-button":
self.send_message()

@work
async def on_input_submitted(self, event: Input.Submitted) -> None:
if event.input.id == "chat-input":
await self.send_message()
Expand All @@ -198,7 +197,6 @@ def on_message(self, type: str, message: any) -> None:
elif type == 'tool_response':
self.add_message(message["id"], "Tool Response", message['content'])


async def send_message(self) -> None:
input_widget = self.query_one("#chat-input", Input)
message = input_widget.value.strip()
Expand All @@ -207,9 +205,13 @@ async def send_message(self) -> None:
input_widget.value = ""
try:
await self.validate_model()
await chat(self.ai, message, self.on_message)
self.chat(message)
except Exception as e:
self.notify(str(e), severity="error")

@work(thread=True)
async def chat(self, message: str) -> None:
await chat(self.ai, message, lambda type, message: self.call_from_thread(self.on_message, type, message))

async def validate_model(self) -> None:
model = self.ai.model
Expand Down
14 changes: 1 addition & 13 deletions r2ai/ui/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,4 @@ async def chat(ai, message, cb):

chat_auto = ChatAuto(model, interpreter=ai, system=SYSTEM_PROMPT_AUTO, tools=tools, messages=messages, tool_choice=tool_choice, cb=cb)

original_handler = signal.getsignal(signal.SIGINT)

try:
signal.signal(signal.SIGINT, signal_handler)
return await chat_auto.achat(stream=True)
except KeyboardInterrupt:
tasks = asyncio.all_tasks()
for task in tasks:
task.cancel()
await asyncio.gather(*tasks, return_exceptions=True)
return None
finally:
signal.signal(signal.SIGINT, original_handler)
return await chat_auto.achat(stream=True)

0 comments on commit 096987e

Please sign in to comment.