diff --git a/wanderlust.py b/wanderlust.py index f86d484..554b09f 100644 --- a/wanderlust.py +++ b/wanderlust.py @@ -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 @@ -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 @@ -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("") @@ -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))