Skip to content

Commit

Permalink
fix: sending message when textbox is unfocused
Browse files Browse the repository at this point in the history
  • Loading branch information
iisakkirotko committed Nov 10, 2023
1 parent 8c42889 commit bd62872
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions wanderlust.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import os
import time
from pathlib import Path
from typing import Callable, cast

import ipyleaflet
from openai import NotFoundError, OpenAI
from openai.types.beta import Thread

import solara
import ipyvue

HERE = Path(__file__).parent

Expand Down Expand Up @@ -110,6 +112,25 @@ def assistant_tool_call(tool_call):
return tool_outputs


def use_event(el: solara.Element, callback: Callable):
callback_ref = solara.use_ref(callback)
callback_ref.current = callback

def add_event_handler():
def on_enter(widget, event, data):
callback_ref.current(widget.v_model)

widget = cast(ipyvue.VueWidget, solara.get_widget(el))
widget.on_event("keyup.enter", on_enter)

def cleanup():
widget.on_event("keyup.enter", on_enter, remove=True)

return cleanup

solara.use_effect(add_event_handler, dependencies=[])


@solara.component
def Map():
ipyleaflet.Map.element( # type: ignore
Expand Down Expand Up @@ -189,7 +210,7 @@ def ChatInterface():
# Create a thread to hold the conversation only once when this component is created
thread: Thread = solara.use_memo(openai.beta.threads.create, dependencies=[])

def add_message(value: str):
def add_message(value):
if value == "":
return
prompt.set("")
Expand Down Expand Up @@ -251,15 +272,16 @@ def poll():
ChatMessage(message)

with solara.Column():
solara.InputText(
input = solara.InputText(
label="Where do you want to go?"
if len(messages.value) == 0
else "Ask more question here",
value=prompt,
style={"flex-grow": "1"},
on_value=add_message,
disabled=result.state == solara.ResultState.RUNNING,
)
# We use use_event to avoid sending the message on blur
use_event(input, add_message)
solara.ProgressLinear(result.state == solara.ResultState.RUNNING)
if result.state == solara.ResultState.ERROR:
solara.Error(repr(result.error))
Expand Down

0 comments on commit bd62872

Please sign in to comment.