Spaces:
Sleeping
Sleeping
Improve the run button and behaviour (#4)
Browse files44 unmodified lines45464748495022 unmodified lines73747576777879808182838485864 unmodified lines91929394959630 unmodified lines1271281291301311322 unmodified lines13513613713813914014114214310 unmodified lines15415515615715815916052 unmodified lines213214215216217218124 unmodified lines34334434534634734834935035135235335435535610 unmodified lines36736836937037137237344 unmodified lineslet latency = 0;let messageContainer: HTMLDivElement | null = null;let abortController: AbortController | null = null;onMount(() => {(async () => {22 unmodified lines}function deleteMessage(i: number) {if (i === currentConversation.messages.length - 1 && streamingMessage) {if (abortController) {abortController.abort();abortController = null;}loading = false;streamingMessage = null;}currentConversation.messages = currentConversation.messages.filter((_, j) => j !== i);conversations = conversations;}4 unmodified linesconversations = conversations;}async function submit() {if (!hfToken) {showTokenModal = true;30 unmodified linesabortController);} else {const newMessage = await handleNonStreamingResponse(hf,currentConversation.model,2 unmodified linescurrentConversation.config.maxTokens,currentConversation.config.jsonMode);currentConversation.messages = [...currentConversation.messages, newMessage];conversations = conversations;scrollToBottom();}} catch (error) {if (error.name !== 'AbortError') {10 unmodified lines}function onKeydown(event: KeyboardEvent) {if ((event.ctrlKey || event.metaKey) && event.key === 'Enter') {submit();}}52 unmodified lines{#each conversations as conversation, index}<divclass="flex max-h-[calc(100dvh-5.8rem)] flex-col overflow-y-auto overflow-x-hidden @container"bind:this={messageContainer}>{#if conversations.length > 1}124 unmodified lines<buttonon:click={() => {viewCode = false;submit();}}type="button"disabled={loading}class="flex h-[39px] w-24 items-center justify-center gap-2 rounded-lg bg-black px-5 py-2.5 text-sm font-medium text-white hover:bg-gray-900 focus:outline-none focus:ring-4 focus:ring-gray-300 dark:border-gray-700 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-gray-700">{#if loading}<div class="flex flex-none items-center gap-[3px]"><divclass="h-1 w-1 flex-none animate-bounce rounded-full bg-gray-500 dark:bg-gray-100"style="animation-delay: 0.25s;"10 unmodified lines{:else}Run <spanclass="inline-flex gap-0.5 rounded border border-white/20 bg-white/10 px-0.5 text-xs text-white/70">⌘<span class="translate-y-px">↵</span></span>{/if}</button>44 unmodified lines4546474849505122 unmodified lines7475767778794 unmodified lines8485868788899091929394959697989930 unmodified lines1301311321331341351361372 unmodified lines14014114214314414514614714814915015110 unmodified lines16216316416516616716852 unmodified lines221222223224225226227228124 unmodified lines35335435535635735835936036136236336436536610 unmodified lines37737837938038138238344 unmodified lineslet latency = 0;let messageContainer: HTMLDivElement | null = null;let abortController: AbortController | null = null;let waitForNonStreaming = true;onMount(() => {(async () => {22 unmodified lines}function deleteMessage(i: number) {currentConversation.messages = currentConversation.messages.filter((_, j) => j !== i);conversations = conversations;}4 unmodified linesconversations = conversations;}function abort(){if (streamingMessage && abortController) {abortController.abort();abortController = null;}loading = false;streamingMessage = null;waitForNonStreaming = false;}async function submit() {if (!hfToken) {showTokenModal = true;30 unmodified linesabortController);} else {streamingMessage = null;waitForNonStreaming = true;const newMessage = await handleNonStreamingResponse(hf,currentConversation.model,2 unmodified linescurrentConversation.config.maxTokens,currentConversation.config.jsonMode);// check if the user did not abort the requestif(waitForNonStreaming){currentConversation.messages = [...currentConversation.messages, newMessage];conversations = conversations;scrollToBottom();}}} catch (error) {if (error.name !== 'AbortError') {10 unmodified lines}function onKeydown(event: KeyboardEvent) {if (!event.shiftKey && event.key === 'Enter') {submit();}}52 unmodified lines{#each conversations as conversation, index}<divclass="flex max-h-[calc(100dvh-5.8rem)] flex-col overflow-y-auto overflow-x-hidden @container"class:pointer-events-none={loading}class:animate-pulse={loading && !streamingMessage}bind:this={messageContainer}>{#if conversations.length > 1}124 unmodified lines<buttonon:click={() => {viewCode = false;loading ? abort() : submit();}}type="button"class="flex h-[39px] w-24 items-center justify-center gap-2 rounded-lg px-5 py-2.5 text-sm font-medium text-white focus:outline-none focus:ring-4 focus:ring-gray-300 dark:border-gray-700 dark:focus:ring-gray-700 {loading ? 'bg-red-900 hover:bg-red-800 dark:bg-red-600 dark:hover:bg-red-700' : 'bg-black hover:bg-gray-900 dark:bg-blue-600 dark:hover:bg-blue-700'}">{#if loading}<div class="flex flex-none items-center gap-[3px]"><span class="mr-2">Cancel</span><divclass="h-1 w-1 flex-none animate-bounce rounded-full bg-gray-500 dark:bg-gray-100"style="animation-delay: 0.25s;"10 unmodified lines{:else}Run <spanclass="inline-flex gap-0.5 rounded border border-white/20 bg-white/10 px-0.5 text-xs text-white/70">↵</span>{/if}</button>
src/lib/components/Playground/Playground.svelte+26-16