Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix VV chat taking over main thread #87

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)