From a0b04be23cda1e680623e63625302a73f47f004f Mon Sep 17 00:00:00 2001 From: Marcus Schiesser <mail@marcusschiesser.de> Date: Thu, 25 Apr 2024 15:16:06 +0800 Subject: [PATCH] fix: hide events per default and optimize python messaging (#64) --- .../fastapi/app/api/routers/messaging.py | 16 +++++--------- .../app/components/ui/chat/chat-events.tsx | 22 +++++-------------- 2 files changed, 10 insertions(+), 28 deletions(-) diff --git a/templates/types/streaming/fastapi/app/api/routers/messaging.py b/templates/types/streaming/fastapi/app/api/routers/messaging.py index ae5cc193..d13595df 100644 --- a/templates/types/streaming/fastapi/app/api/routers/messaging.py +++ b/templates/types/streaming/fastapi/app/api/routers/messaging.py @@ -79,14 +79,8 @@ class EventCallbackHandler(BaseCallbackHandler): """No-op.""" async def async_event_gen(self) -> AsyncGenerator[CallbackEvent, None]: - while True: - if not self._aqueue.empty() or not self.is_done: - try: - event = await asyncio.wait_for(self._aqueue.get(), timeout=0.1) - except asyncio.TimeoutError: - if self.is_done: - break - continue - yield event - else: - break + while not self._aqueue.empty() or not self.is_done: + try: + yield await asyncio.wait_for(self._aqueue.get(), timeout=0.1) + except asyncio.TimeoutError: + pass diff --git a/templates/types/streaming/nextjs/app/components/ui/chat/chat-events.tsx b/templates/types/streaming/nextjs/app/components/ui/chat/chat-events.tsx index 96d36fef..eb921a98 100644 --- a/templates/types/streaming/nextjs/app/components/ui/chat/chat-events.tsx +++ b/templates/types/streaming/nextjs/app/components/ui/chat/chat-events.tsx @@ -1,5 +1,5 @@ import { ChevronDown, ChevronRight, Loader2 } from "lucide-react"; -import { useEffect, useState } from "react"; +import { useState } from "react"; import { Button } from "../button"; import { Collapsible, @@ -15,24 +15,11 @@ export function ChatEvents({ data: EventData[]; isLoading: boolean; }) { - const [isOpen, setIsOpen] = useState(true); + const [isOpen, setIsOpen] = useState(false); - useEffect(() => { - // Collapse the events when finished streaming - if (!isLoading) { - setIsOpen(false); - } - }, [isLoading]); + const buttonLabel = isOpen ? "Hide events" : "Show events"; - const buttonLabel = isLoading - ? "In progress" - : isOpen - ? "Hide events" - : "Show events"; - - const EventIcon = isLoading ? ( - <Loader2 className="h-4 w-4 animate-spin" /> - ) : isOpen ? ( + const EventIcon = isOpen ? ( <ChevronDown className="h-4 w-4" /> ) : ( <ChevronRight className="h-4 w-4" /> @@ -43,6 +30,7 @@ export function ChatEvents({ <Collapsible open={isOpen} onOpenChange={setIsOpen}> <CollapsibleTrigger asChild> <Button variant="secondary" className="space-x-2"> + {isLoading ? <Loader2 className="h-4 w-4 animate-spin" /> : null} <span>{buttonLabel}</span> {EventIcon} </Button> -- GitLab