Spaces:
				
			
			
	
			
			
		Paused
		
	
	
	
			
			
	
	
	
	
		
		
		Paused
		
	Refactor vote & alternative components (#1673)
Browse files* Refactor vote & alternative components
* no plural
---------
Co-authored-by: Nathan Sarrazin <[email protected]>
    	
        src/lib/components/chat/Alternatives.svelte
    ADDED
    
    | @@ -0,0 +1,54 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            <script lang="ts">
         | 
| 2 | 
            +
            	import type { Message } from "$lib/types/Message";
         | 
| 3 | 
            +
            	import CarbonTrashCan from "~icons/carbon/trash-can";
         | 
| 4 | 
            +
            	import CarbonChevronLeft from "~icons/carbon/chevron-left";
         | 
| 5 | 
            +
            	import CarbonChevronRight from "~icons/carbon/chevron-right";
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            	import { enhance } from "$app/forms";
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            	export let message: Message;
         | 
| 10 | 
            +
            	export let childToRender: number;
         | 
| 11 | 
            +
            	export let nChildren: number;
         | 
| 12 | 
            +
            	export let loading = false;
         | 
| 13 | 
            +
            </script>
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            <div
         | 
| 16 | 
            +
            	class="font-white group/navbranch z-0 -mt-1 ml-3.5 mr-auto flex h-6 w-fit select-none flex-row items-center justify-center gap-1 text-sm"
         | 
| 17 | 
            +
            >
         | 
| 18 | 
            +
            	<button
         | 
| 19 | 
            +
            		class="inline text-lg font-thin text-gray-400 hover:text-gray-800 disabled:pointer-events-none disabled:opacity-25 dark:text-gray-500 dark:hover:text-gray-200"
         | 
| 20 | 
            +
            		on:click={() => (childToRender = Math.max(0, childToRender - 1))}
         | 
| 21 | 
            +
            		disabled={childToRender === 0 || loading}
         | 
| 22 | 
            +
            	>
         | 
| 23 | 
            +
            		<CarbonChevronLeft class="text-sm" />
         | 
| 24 | 
            +
            	</button>
         | 
| 25 | 
            +
            	<span class=" text-gray-400 dark:text-gray-500">
         | 
| 26 | 
            +
            		{childToRender + 1} / {nChildren}
         | 
| 27 | 
            +
            	</span>
         | 
| 28 | 
            +
            	<button
         | 
| 29 | 
            +
            		class="inline text-lg font-thin text-gray-400 hover:text-gray-800 disabled:pointer-events-none disabled:opacity-25 dark:text-gray-500 dark:hover:text-gray-200"
         | 
| 30 | 
            +
            		on:click={() =>
         | 
| 31 | 
            +
            			(childToRender = Math.min(message?.children?.length ?? 1 - 1, childToRender + 1))}
         | 
| 32 | 
            +
            		disabled={childToRender === nChildren - 1 || loading}
         | 
| 33 | 
            +
            	>
         | 
| 34 | 
            +
            		<CarbonChevronRight class="text-sm" />
         | 
| 35 | 
            +
            	</button>
         | 
| 36 | 
            +
            	{#if !loading && message.children}<form
         | 
| 37 | 
            +
            			method="POST"
         | 
| 38 | 
            +
            			action="?/deleteBranch"
         | 
| 39 | 
            +
            			class="hidden group-hover/navbranch:block"
         | 
| 40 | 
            +
            			use:enhance={({ cancel }) => {
         | 
| 41 | 
            +
            				if (!confirm("Are you sure you want to delete this branch?")) {
         | 
| 42 | 
            +
            					cancel();
         | 
| 43 | 
            +
            				}
         | 
| 44 | 
            +
            			}}
         | 
| 45 | 
            +
            		>
         | 
| 46 | 
            +
            			<input name="messageId" value={message.children[childToRender]} type="hidden" />
         | 
| 47 | 
            +
            			<button
         | 
| 48 | 
            +
            				class="flex items-center justify-center text-xs text-gray-400 hover:text-gray-800 dark:text-gray-500 dark:hover:text-gray-200"
         | 
| 49 | 
            +
            				type="submit"
         | 
| 50 | 
            +
            				><CarbonTrashCan />
         | 
| 51 | 
            +
            			</button>
         | 
| 52 | 
            +
            		</form>
         | 
| 53 | 
            +
            	{/if}
         | 
| 54 | 
            +
            </div>
         | 
    	
        src/lib/components/chat/ChatMessage.svelte
    CHANGED
    
    | @@ -7,13 +7,9 @@ | |
| 7 | 
             
            	import CopyToClipBoardBtn from "../CopyToClipBoardBtn.svelte";
         | 
| 8 | 
             
            	import IconLoading from "../icons/IconLoading.svelte";
         | 
| 9 | 
             
            	import CarbonRotate360 from "~icons/carbon/rotate-360";
         | 
| 10 | 
            -
            	import CarbonTrashCan from "~icons/carbon/trash-can";
         | 
| 11 | 
             
            	import CarbonDownload from "~icons/carbon/download";
         | 
| 12 | 
            -
             | 
| 13 | 
            -
            	import CarbonThumbsDown from "~icons/carbon/thumbs-down";
         | 
| 14 | 
             
            	import CarbonPen from "~icons/carbon/pen";
         | 
| 15 | 
            -
            	import CarbonChevronLeft from "~icons/carbon/chevron-left";
         | 
| 16 | 
            -
            	import CarbonChevronRight from "~icons/carbon/chevron-right";
         | 
| 17 | 
             
            	import UploadedFile from "./UploadedFile.svelte";
         | 
| 18 |  | 
| 19 | 
             
            	import OpenWebSearchResults from "../OpenWebSearchResults.svelte";
         | 
| @@ -31,10 +27,11 @@ | |
| 31 | 
             
            	import { useConvTreeStore } from "$lib/stores/convTree";
         | 
| 32 | 
             
            	import ToolUpdate from "./ToolUpdate.svelte";
         | 
| 33 | 
             
            	import { useSettingsStore } from "$lib/stores/settings";
         | 
| 34 | 
            -
            	import { enhance } from "$app/forms";
         | 
| 35 | 
             
            	import { browser } from "$app/environment";
         | 
| 36 | 
             
            	import MarkdownRenderer from "./MarkdownRenderer.svelte";
         | 
| 37 | 
             
            	import OpenReasoningResults from "./OpenReasoningResults.svelte";
         | 
|  | |
|  | |
| 38 |  | 
| 39 | 
             
            	export let id: Message["id"];
         | 
| 40 | 
             
            	export let messages: Message[];
         | 
| @@ -49,7 +46,6 @@ | |
| 49 |  | 
| 50 | 
             
            	const dispatch = createEventDispatcher<{
         | 
| 51 | 
             
            		retry: { content?: string; id: Message["id"] };
         | 
| 52 | 
            -
            		vote: { score: Message["score"]; id: Message["id"] };
         | 
| 53 | 
             
            	}>();
         | 
| 54 |  | 
| 55 | 
             
            	let contentEl: HTMLElement;
         | 
| @@ -148,14 +144,14 @@ | |
| 148 |  | 
| 149 | 
             
            	$: isLast = (message && message.children?.length === 0) ?? false;
         | 
| 150 |  | 
| 151 | 
            -
            	$:  | 
| 152 | 
             
            	$: nChildren = message?.children?.length ?? 0;
         | 
| 153 |  | 
| 154 | 
             
            	$: {
         | 
| 155 | 
             
            		if (initialized) {
         | 
| 156 | 
            -
            			 | 
| 157 | 
             
            		} else {
         | 
| 158 | 
            -
            			 | 
| 159 | 
             
            			initialized = true;
         | 
| 160 | 
             
            		}
         | 
| 161 | 
             
            	}
         | 
| @@ -173,7 +169,7 @@ | |
| 173 | 
             
            	let isRun = false;
         | 
| 174 | 
             
            	$: {
         | 
| 175 | 
             
            		if (message.id && !isRun) {
         | 
| 176 | 
            -
            			if (message.currentChildIndex)  | 
| 177 | 
             
            			isRun = true;
         | 
| 178 | 
             
            		}
         | 
| 179 | 
             
            	}
         | 
| @@ -305,30 +301,7 @@ | |
| 305 | 
             
            	"
         | 
| 306 | 
             
            			>
         | 
| 307 | 
             
            				{#if isAuthor}
         | 
| 308 | 
            -
            					< | 
| 309 | 
            -
            						class="btn rounded-sm p-1 text-sm text-gray-400 hover:text-gray-500 focus:ring-0 dark:text-gray-400 dark:hover:text-gray-300
         | 
| 310 | 
            -
            					{message.score && message.score > 0
         | 
| 311 | 
            -
            							? 'text-green-500 hover:text-green-500 dark:text-green-400 hover:dark:text-green-400'
         | 
| 312 | 
            -
            							: ''}"
         | 
| 313 | 
            -
            						title={message.score === 1 ? "Remove +1" : "+1"}
         | 
| 314 | 
            -
            						type="button"
         | 
| 315 | 
            -
            						on:click={() =>
         | 
| 316 | 
            -
            							dispatch("vote", { score: message.score === 1 ? 0 : 1, id: message.id })}
         | 
| 317 | 
            -
            					>
         | 
| 318 | 
            -
            						<CarbonThumbsUp class="h-[1.14em] w-[1.14em]" />
         | 
| 319 | 
            -
            					</button>
         | 
| 320 | 
            -
            					<button
         | 
| 321 | 
            -
            						class="btn rounded-sm p-1 text-sm text-gray-400 hover:text-gray-500 focus:ring-0 dark:text-gray-400 dark:hover:text-gray-300
         | 
| 322 | 
            -
            					{message.score && message.score < 0
         | 
| 323 | 
            -
            							? 'text-red-500 hover:text-red-500 dark:text-red-400 hover:dark:text-red-400'
         | 
| 324 | 
            -
            							: ''}"
         | 
| 325 | 
            -
            						title={message.score === -1 ? "Remove -1" : "-1"}
         | 
| 326 | 
            -
            						type="button"
         | 
| 327 | 
            -
            						on:click={() =>
         | 
| 328 | 
            -
            							dispatch("vote", { score: message.score === -1 ? 0 : -1, id: message.id })}
         | 
| 329 | 
            -
            					>
         | 
| 330 | 
            -
            						<CarbonThumbsDown class="h-[1.14em] w-[1.14em]" />
         | 
| 331 | 
            -
            					</button>
         | 
| 332 | 
             
            				{/if}
         | 
| 333 | 
             
            				<button
         | 
| 334 | 
             
            					class="btn rounded-sm p-1 text-sm text-gray-400 hover:text-gray-500 focus:ring-0 dark:text-gray-400 dark:hover:text-gray-300"
         | 
| @@ -457,7 +430,7 @@ | |
| 457 | 
             
            {/if}
         | 
| 458 |  | 
| 459 | 
             
            {#if nChildren > 0}
         | 
| 460 | 
            -
            	{@const messageId = messages.find((m) => m.id === id)?.children?.[ | 
| 461 | 
             
            	{#key messageId}
         | 
| 462 | 
             
            		<svelte:self
         | 
| 463 | 
             
            			{loading}
         | 
| @@ -471,49 +444,7 @@ | |
| 471 | 
             
            		>
         | 
| 472 | 
             
            			<svelte:fragment slot="childrenNav">
         | 
| 473 | 
             
            				{#if nChildren > 1 && $convTreeStore.editing === null}
         | 
| 474 | 
            -
            					< | 
| 475 | 
            -
            						class="font-white group/navbranch z-0 -mt-1 ml-3.5 mr-auto flex h-6 w-fit select-none flex-row items-center justify-center gap-1 text-sm"
         | 
| 476 | 
            -
            					>
         | 
| 477 | 
            -
            						<button
         | 
| 478 | 
            -
            							class="inline text-lg font-thin text-gray-400 hover:text-gray-800 disabled:pointer-events-none disabled:opacity-25 dark:text-gray-500 dark:hover:text-gray-200"
         | 
| 479 | 
            -
            							on:click={() => (childrenToRender = Math.max(0, childrenToRender - 1))}
         | 
| 480 | 
            -
            							disabled={childrenToRender === 0 || loading}
         | 
| 481 | 
            -
            						>
         | 
| 482 | 
            -
            							<CarbonChevronLeft class="text-sm" />
         | 
| 483 | 
            -
            						</button>
         | 
| 484 | 
            -
            						<span class=" text-gray-400 dark:text-gray-500">
         | 
| 485 | 
            -
            							{childrenToRender + 1} / {nChildren}
         | 
| 486 | 
            -
            						</span>
         | 
| 487 | 
            -
            						<button
         | 
| 488 | 
            -
            							class="inline text-lg font-thin text-gray-400 hover:text-gray-800 disabled:pointer-events-none disabled:opacity-25 dark:text-gray-500 dark:hover:text-gray-200"
         | 
| 489 | 
            -
            							on:click={() =>
         | 
| 490 | 
            -
            								(childrenToRender = Math.min(
         | 
| 491 | 
            -
            									message?.children?.length ?? 1 - 1,
         | 
| 492 | 
            -
            									childrenToRender + 1
         | 
| 493 | 
            -
            								))}
         | 
| 494 | 
            -
            							disabled={childrenToRender === nChildren - 1 || loading}
         | 
| 495 | 
            -
            						>
         | 
| 496 | 
            -
            							<CarbonChevronRight class="text-sm" />
         | 
| 497 | 
            -
            						</button>
         | 
| 498 | 
            -
            						{#if !loading && message.children}<form
         | 
| 499 | 
            -
            								method="POST"
         | 
| 500 | 
            -
            								action="?/deleteBranch"
         | 
| 501 | 
            -
            								class="hidden group-hover/navbranch:block"
         | 
| 502 | 
            -
            								use:enhance={({ cancel }) => {
         | 
| 503 | 
            -
            									if (!confirm("Are you sure you want to delete this branch?")) {
         | 
| 504 | 
            -
            										cancel();
         | 
| 505 | 
            -
            									}
         | 
| 506 | 
            -
            								}}
         | 
| 507 | 
            -
            							>
         | 
| 508 | 
            -
            								<input name="messageId" value={message.children[childrenToRender]} type="hidden" />
         | 
| 509 | 
            -
            								<button
         | 
| 510 | 
            -
            									class="flex items-center justify-center text-xs text-gray-400 hover:text-gray-800 dark:text-gray-500 dark:hover:text-gray-200"
         | 
| 511 | 
            -
            									type="submit"
         | 
| 512 | 
            -
            									><CarbonTrashCan />
         | 
| 513 | 
            -
            								</button>
         | 
| 514 | 
            -
            							</form>
         | 
| 515 | 
            -
            						{/if}
         | 
| 516 | 
            -
            					</div>
         | 
| 517 | 
             
            				{/if}
         | 
| 518 | 
             
            			</svelte:fragment>
         | 
| 519 | 
             
            		</svelte:self>
         | 
|  | |
| 7 | 
             
            	import CopyToClipBoardBtn from "../CopyToClipBoardBtn.svelte";
         | 
| 8 | 
             
            	import IconLoading from "../icons/IconLoading.svelte";
         | 
| 9 | 
             
            	import CarbonRotate360 from "~icons/carbon/rotate-360";
         | 
|  | |
| 10 | 
             
            	import CarbonDownload from "~icons/carbon/download";
         | 
| 11 | 
            +
             | 
|  | |
| 12 | 
             
            	import CarbonPen from "~icons/carbon/pen";
         | 
|  | |
|  | |
| 13 | 
             
            	import UploadedFile from "./UploadedFile.svelte";
         | 
| 14 |  | 
| 15 | 
             
            	import OpenWebSearchResults from "../OpenWebSearchResults.svelte";
         | 
|  | |
| 27 | 
             
            	import { useConvTreeStore } from "$lib/stores/convTree";
         | 
| 28 | 
             
            	import ToolUpdate from "./ToolUpdate.svelte";
         | 
| 29 | 
             
            	import { useSettingsStore } from "$lib/stores/settings";
         | 
|  | |
| 30 | 
             
            	import { browser } from "$app/environment";
         | 
| 31 | 
             
            	import MarkdownRenderer from "./MarkdownRenderer.svelte";
         | 
| 32 | 
             
            	import OpenReasoningResults from "./OpenReasoningResults.svelte";
         | 
| 33 | 
            +
            	import Alternatives from "./Alternatives.svelte";
         | 
| 34 | 
            +
            	import Vote from "./Vote.svelte";
         | 
| 35 |  | 
| 36 | 
             
            	export let id: Message["id"];
         | 
| 37 | 
             
            	export let messages: Message[];
         | 
|  | |
| 46 |  | 
| 47 | 
             
            	const dispatch = createEventDispatcher<{
         | 
| 48 | 
             
            		retry: { content?: string; id: Message["id"] };
         | 
|  | |
| 49 | 
             
            	}>();
         | 
| 50 |  | 
| 51 | 
             
            	let contentEl: HTMLElement;
         | 
|  | |
| 144 |  | 
| 145 | 
             
            	$: isLast = (message && message.children?.length === 0) ?? false;
         | 
| 146 |  | 
| 147 | 
            +
            	$: childToRender = 0;
         | 
| 148 | 
             
            	$: nChildren = message?.children?.length ?? 0;
         | 
| 149 |  | 
| 150 | 
             
            	$: {
         | 
| 151 | 
             
            		if (initialized) {
         | 
| 152 | 
            +
            			childToRender = Math.max(0, nChildren - 1);
         | 
| 153 | 
             
            		} else {
         | 
| 154 | 
            +
            			childToRender = 0;
         | 
| 155 | 
             
            			initialized = true;
         | 
| 156 | 
             
            		}
         | 
| 157 | 
             
            	}
         | 
|  | |
| 169 | 
             
            	let isRun = false;
         | 
| 170 | 
             
            	$: {
         | 
| 171 | 
             
            		if (message.id && !isRun) {
         | 
| 172 | 
            +
            			if (message.currentChildIndex) childToRender = message.currentChildIndex;
         | 
| 173 | 
             
            			isRun = true;
         | 
| 174 | 
             
            		}
         | 
| 175 | 
             
            	}
         | 
|  | |
| 301 | 
             
            	"
         | 
| 302 | 
             
            			>
         | 
| 303 | 
             
            				{#if isAuthor}
         | 
| 304 | 
            +
            					<Vote {message} on:vote />
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 305 | 
             
            				{/if}
         | 
| 306 | 
             
            				<button
         | 
| 307 | 
             
            					class="btn rounded-sm p-1 text-sm text-gray-400 hover:text-gray-500 focus:ring-0 dark:text-gray-400 dark:hover:text-gray-300"
         | 
|  | |
| 430 | 
             
            {/if}
         | 
| 431 |  | 
| 432 | 
             
            {#if nChildren > 0}
         | 
| 433 | 
            +
            	{@const messageId = messages.find((m) => m.id === id)?.children?.[childToRender]}
         | 
| 434 | 
             
            	{#key messageId}
         | 
| 435 | 
             
            		<svelte:self
         | 
| 436 | 
             
            			{loading}
         | 
|  | |
| 444 | 
             
            		>
         | 
| 445 | 
             
            			<svelte:fragment slot="childrenNav">
         | 
| 446 | 
             
            				{#if nChildren > 1 && $convTreeStore.editing === null}
         | 
| 447 | 
            +
            					<Alternatives {message} bind:childToRender {nChildren} {loading} />
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 448 | 
             
            				{/if}
         | 
| 449 | 
             
            			</svelte:fragment>
         | 
| 450 | 
             
            		</svelte:self>
         | 
    	
        src/lib/components/chat/Vote.svelte
    ADDED
    
    | @@ -0,0 +1,36 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            <script lang="ts">
         | 
| 2 | 
            +
            	import type { Message } from "$lib/types/Message";
         | 
| 3 | 
            +
            	import CarbonThumbsUp from "~icons/carbon/thumbs-up";
         | 
| 4 | 
            +
            	import CarbonThumbsDown from "~icons/carbon/thumbs-down";
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            	import { createEventDispatcher } from "svelte";
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            	export let message: Message;
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            	const dispatch = createEventDispatcher<{
         | 
| 11 | 
            +
            		vote: { score: Message["score"]; id: Message["id"] };
         | 
| 12 | 
            +
            	}>();
         | 
| 13 | 
            +
            </script>
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            <button
         | 
| 16 | 
            +
            	class="btn rounded-sm p-1 text-sm text-gray-400 hover:text-gray-500 focus:ring-0 dark:text-gray-400 dark:hover:text-gray-300
         | 
| 17 | 
            +
            {message.score && message.score > 0
         | 
| 18 | 
            +
            		? 'text-green-500 hover:text-green-500 dark:text-green-400 hover:dark:text-green-400'
         | 
| 19 | 
            +
            		: ''}"
         | 
| 20 | 
            +
            	title={message.score === 1 ? "Remove +1" : "+1"}
         | 
| 21 | 
            +
            	type="button"
         | 
| 22 | 
            +
            	on:click={() => dispatch("vote", { score: message.score === 1 ? 0 : 1, id: message.id })}
         | 
| 23 | 
            +
            >
         | 
| 24 | 
            +
            	<CarbonThumbsUp class="h-[1.14em] w-[1.14em]" />
         | 
| 25 | 
            +
            </button>
         | 
| 26 | 
            +
            <button
         | 
| 27 | 
            +
            	class="btn rounded-sm p-1 text-sm text-gray-400 hover:text-gray-500 focus:ring-0 dark:text-gray-400 dark:hover:text-gray-300
         | 
| 28 | 
            +
            {message.score && message.score < 0
         | 
| 29 | 
            +
            		? 'text-red-500 hover:text-red-500 dark:text-red-400 hover:dark:text-red-400'
         | 
| 30 | 
            +
            		: ''}"
         | 
| 31 | 
            +
            	title={message.score === -1 ? "Remove -1" : "-1"}
         | 
| 32 | 
            +
            	type="button"
         | 
| 33 | 
            +
            	on:click={() => dispatch("vote", { score: message.score === -1 ? 0 : -1, id: message.id })}
         | 
| 34 | 
            +
            >
         | 
| 35 | 
            +
            	<CarbonThumbsDown class="h-[1.14em] w-[1.14em]" />
         | 
| 36 | 
            +
            </button>
         | 
 
			

 
		