mishig HF staff commited on
Commit
b20a4ed
·
unverified ·
2 Parent(s): fc38da9 299cbc7

Improve the run button and behaviour (#4)

Browse files
src/lib/components/Playground/Playground.svelte CHANGED
@@ -45,6 +45,7 @@
45
  let latency = 0;
46
  let messageContainer: HTMLDivElement | null = null;
47
  let abortController: AbortController | null = null;
 
48
 
49
  onMount(() => {
50
  (async () => {
@@ -73,14 +74,6 @@
73
  }
74
 
75
  function deleteMessage(i: number) {
76
- if (i === currentConversation.messages.length - 1 && streamingMessage) {
77
- if (abortController) {
78
- abortController.abort();
79
- abortController = null;
80
- }
81
- loading = false;
82
- streamingMessage = null;
83
- }
84
  currentConversation.messages = currentConversation.messages.filter((_, j) => j !== i);
85
  conversations = conversations;
86
  }
@@ -91,6 +84,16 @@
91
  conversations = conversations;
92
  }
93
 
 
 
 
 
 
 
 
 
 
 
94
  async function submit() {
95
  if (!hfToken) {
96
  showTokenModal = true;
@@ -127,6 +130,8 @@
127
  abortController
128
  );
129
  } else {
 
 
130
  const newMessage = await handleNonStreamingResponse(
131
  hf,
132
  currentConversation.model,
@@ -135,9 +140,12 @@
135
  currentConversation.config.maxTokens,
136
  currentConversation.config.jsonMode
137
  );
138
- currentConversation.messages = [...currentConversation.messages, newMessage];
139
- conversations = conversations;
140
- scrollToBottom();
 
 
 
141
  }
142
  } catch (error) {
143
  if (error.name !== 'AbortError') {
@@ -154,7 +162,7 @@
154
  }
155
 
156
  function onKeydown(event: KeyboardEvent) {
157
- if ((event.ctrlKey || event.metaKey) && event.key === 'Enter') {
158
  submit();
159
  }
160
  }
@@ -213,6 +221,8 @@
213
  {#each conversations as conversation, index}
214
  <div
215
  class="flex max-h-[calc(100dvh-5.8rem)] flex-col overflow-y-auto overflow-x-hidden @container"
 
 
216
  bind:this={messageContainer}
217
  >
218
  {#if conversations.length > 1}
@@ -343,14 +353,14 @@
343
  <button
344
  on:click={() => {
345
  viewCode = false;
346
- submit();
347
  }}
348
  type="button"
349
- disabled={loading}
350
- 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"
351
  >
352
  {#if loading}
353
  <div class="flex flex-none items-center gap-[3px]">
 
354
  <div
355
  class="h-1 w-1 flex-none animate-bounce rounded-full bg-gray-500 dark:bg-gray-100"
356
  style="animation-delay: 0.25s;"
@@ -367,7 +377,7 @@
367
  {:else}
368
  Run <span
369
  class="inline-flex gap-0.5 rounded border border-white/20 bg-white/10 px-0.5 text-xs text-white/70"
370
- >⌘<span class="translate-y-px">↵</span></span
371
  >
372
  {/if}
373
  </button>
 
45
  let latency = 0;
46
  let messageContainer: HTMLDivElement | null = null;
47
  let abortController: AbortController | null = null;
48
+ let waitForNonStreaming = true;
49
 
50
  onMount(() => {
51
  (async () => {
 
74
  }
75
 
76
  function deleteMessage(i: number) {
 
 
 
 
 
 
 
 
77
  currentConversation.messages = currentConversation.messages.filter((_, j) => j !== i);
78
  conversations = conversations;
79
  }
 
84
  conversations = conversations;
85
  }
86
 
87
+ function abort(){
88
+ if (streamingMessage && abortController) {
89
+ abortController.abort();
90
+ abortController = null;
91
+ }
92
+ loading = false;
93
+ streamingMessage = null;
94
+ waitForNonStreaming = false;
95
+ }
96
+
97
  async function submit() {
98
  if (!hfToken) {
99
  showTokenModal = true;
 
130
  abortController
131
  );
132
  } else {
133
+ streamingMessage = null;
134
+ waitForNonStreaming = true;
135
  const newMessage = await handleNonStreamingResponse(
136
  hf,
137
  currentConversation.model,
 
140
  currentConversation.config.maxTokens,
141
  currentConversation.config.jsonMode
142
  );
143
+ // check if the user did not abort the request
144
+ if(waitForNonStreaming){
145
+ currentConversation.messages = [...currentConversation.messages, newMessage];
146
+ conversations = conversations;
147
+ scrollToBottom();
148
+ }
149
  }
150
  } catch (error) {
151
  if (error.name !== 'AbortError') {
 
162
  }
163
 
164
  function onKeydown(event: KeyboardEvent) {
165
+ if (!event.shiftKey && event.key === 'Enter') {
166
  submit();
167
  }
168
  }
 
221
  {#each conversations as conversation, index}
222
  <div
223
  class="flex max-h-[calc(100dvh-5.8rem)] flex-col overflow-y-auto overflow-x-hidden @container"
224
+ class:pointer-events-none={loading}
225
+ class:animate-pulse={loading && !streamingMessage}
226
  bind:this={messageContainer}
227
  >
228
  {#if conversations.length > 1}
 
353
  <button
354
  on:click={() => {
355
  viewCode = false;
356
+ loading ? abort() : submit();
357
  }}
358
  type="button"
359
+ 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'}"
 
360
  >
361
  {#if loading}
362
  <div class="flex flex-none items-center gap-[3px]">
363
+ <span class="mr-2">Cancel</span>
364
  <div
365
  class="h-1 w-1 flex-none animate-bounce rounded-full bg-gray-500 dark:bg-gray-100"
366
  style="animation-delay: 0.25s;"
 
377
  {:else}
378
  Run <span
379
  class="inline-flex gap-0.5 rounded border border-white/20 bg-white/10 px-0.5 text-xs text-white/70"
380
+ >↵</span
381
  >
382
  {/if}
383
  </button>