File size: 14,295 Bytes
973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 1536d5d 973a4e3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | <script lang="ts">
import type { Message } from "$lib/types/Message";
import { createEventDispatcher, tick } from "svelte";
import { page } from "$app/state";
import CopyToClipBoardBtn from "../CopyToClipBoardBtn.svelte";
import IconLoading from "../icons/IconLoading.svelte";
import CarbonRotate360 from "~icons/carbon/rotate-360";
import CarbonDownload from "~icons/carbon/download";
import CarbonPen from "~icons/carbon/pen";
import CarbonTime from "~icons/carbon/time";
import UploadedFile from "./UploadedFile.svelte";
import OpenWebSearchResults from "../OpenWebSearchResults.svelte";
import {
MessageUpdateType,
MessageWebSearchUpdateType,
type MessageToolUpdate,
type MessageWebSearchSourcesUpdate,
type MessageWebSearchUpdate,
type MessageFinalAnswerUpdate,
type MessageReasoningUpdate,
MessageReasoningUpdateType,
} from "$lib/types/MessageUpdate";
import { base } from "$app/paths";
import ToolUpdate from "./ToolUpdate.svelte";
import MarkdownRenderer from "./MarkdownRenderer.svelte";
import OpenReasoningResults from "./OpenReasoningResults.svelte";
import Alternatives from "./Alternatives.svelte";
import Vote from "./Vote.svelte";
interface Props {
message: Message;
loading?: boolean;
isAuthor?: boolean;
readOnly?: boolean;
isTapped?: boolean;
alternatives?: Message["id"][];
editMsdgId?: Message["id"] | null;
isLast?: boolean;
}
let {
message,
loading = false,
isAuthor = true,
readOnly = false,
isTapped = $bindable(false),
alternatives = [],
editMsdgId = $bindable(null),
isLast = false,
}: Props = $props();
const dispatch = createEventDispatcher<{
retry: { content?: string; id: Message["id"] };
}>();
let contentEl: HTMLElement | undefined = $state();
let isCopied = $state(false);
function handleKeyDown(e: KeyboardEvent) {
if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) {
editFormEl?.requestSubmit();
}
}
let editContentEl: HTMLTextAreaElement | undefined = $state();
let editFormEl: HTMLFormElement | undefined = $state();
let searchUpdates = $derived(
(message.updates?.filter(({ type }) => type === MessageUpdateType.WebSearch) ??
[]) as MessageWebSearchUpdate[]
);
let reasoningUpdates = $derived(
(message.updates?.filter(({ type }) => type === MessageUpdateType.Reasoning) ??
[]) as MessageReasoningUpdate[]
);
let messageFinalAnswer = $derived(
message.updates?.find(
({ type }) => type === MessageUpdateType.FinalAnswer
) as MessageFinalAnswerUpdate
);
let toolUpdates = $derived(
message.updates
?.filter(({ type }) => type === "tool")
.reduce(
(acc, update) => {
if (update.type !== "tool") {
return acc;
}
acc[update.uuid] = acc[update.uuid] ?? [];
acc[update.uuid].push(update);
return acc;
},
{} as Record<string, MessageToolUpdate[]>
)
);
let urlNotTrailing = $derived(page.url.pathname.replace(/\/$/, ""));
let downloadLink = $derived(urlNotTrailing + `/message/${message.id}/prompt`);
let webSearchSources = $derived(
searchUpdates?.find(
(update): update is MessageWebSearchSourcesUpdate =>
update.subtype === MessageWebSearchUpdateType.Sources
)?.sources
);
$effect(() => {
if (isCopied) {
setTimeout(() => {
isCopied = false;
}, 1000);
}
});
let editMode = $derived(editMsdgId === message.id);
$effect(() => {
if (editMode) {
tick();
if (editContentEl) {
editContentEl.value = message.content;
editContentEl?.focus();
}
}
});
function formatTime(timestamp: string | undefined) {
if (!timestamp) return '';
const date = new Date(timestamp);
return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
}
</script>
{#if message.from === "assistant"}
<div
class="group relative -mb-4 flex items-start justify-start gap-4 pb-4 leading-relaxed animate-slide-in"
data-message-id={message.id}
data-message-role="assistant"
role="presentation"
onclick={() => (isTapped = !isTapped)}
onkeydown={() => (isTapped = !isTapped)}
>
{#if page.data?.assistant?.avatar}
<img
src="{base}/settings/assistants/{page.data.assistant._id}/avatar.jpg"
alt="Avatar"
class="mt-5 h-8 w-8 flex-none select-none rounded-full shadow-lg ring-2 ring-white dark:ring-gray-800"
/>
{:else}
<img
alt=""
src="https://huggingface.co/avatars/2edb18bd0206c16b433841a47f53fa8e.svg"
class="mt-5 h-8 w-8 flex-none select-none rounded-full shadow-lg ring-2 ring-white dark:ring-gray-800"
/>
{/if}
<div
class="relative flex min-h-[calc(2rem+theme(spacing[3.5])*2)] min-w-[60px] flex-col gap-2 break-words rounded-xl border border-gray-100 bg-white/80 backdrop-blur-sm px-5 py-3.5 text-gray-600 prose-pre:my-2 shadow-sm hover:shadow-md transition-shadow duration-200 dark:border-gray-800 dark:from-gray-800/40 dark:text-gray-300"
>
{#if message.files?.length}
<div class="flex h-fit flex-wrap gap-x-5 gap-y-2">
{#each message.files as file}
<UploadedFile {file} canClose={false} />
{/each}
</div>
{/if}
{#if searchUpdates && searchUpdates.length > 0}
<OpenWebSearchResults webSearchMessages={searchUpdates} />
{/if}
{#if reasoningUpdates && reasoningUpdates.length > 0 && message.reasoning && message.reasoning.trim().length > 0}
{@const summaries = reasoningUpdates
.filter((u) => u.subtype === MessageReasoningUpdateType.Status)
.map((u) => u.status)}
<OpenReasoningResults
summary={summaries[summaries.length - 1] || ""}
content={message.reasoning || ""}
loading={loading && message.content.length === 0}
/>
{/if}
{#if toolUpdates}
{#each Object.values(toolUpdates) as tool}
{#if tool.length}
{#key tool[0].uuid}
<ToolUpdate {tool} {loading} />
{/key}
{/if}
{/each}
{/if}
<div
bind:this={contentEl}
class:mt-2={reasoningUpdates.length > 0 || searchUpdates.length > 0}
>
{#if isLast && loading && message.content.length === 0}
<IconLoading classNames="loading inline ml-2 first:ml-0" />
{/if}
<div
class="prose max-w-none dark:prose-invert max-sm:prose-sm prose-headings:font-semibold prose-h1:text-lg prose-h2:text-base prose-h3:text-base prose-pre:bg-gray-800 dark:prose-pre:bg-gray-900"
>
<MarkdownRenderer content={message.content} sources={webSearchSources} />
</div>
</div>
<!-- Web Search sources -->
{#if webSearchSources?.length}
<div class="mt-4 flex flex-wrap items-center gap-x-2 gap-y-1.5 text-sm">
<div class="text-gray-400">Sources:</div>
{#each webSearchSources as { link, title }}
<a
class="flex items-center gap-2 whitespace-nowrap rounded-lg border bg-white px-2 py-1.5 leading-none hover:border-gray-300 dark:border-gray-800 dark:bg-gray-900 dark:hover:border-gray-700 transition-colors duration-200"
href={link}
target="_blank"
>
<img
class="h-3.5 w-3.5 rounded"
src="https://www.google.com/s2/favicons?sz=64&domain_url={new URL(link).hostname ||
'placeholder'}"
alt="{title} favicon"
/>
<div>{new URL(link).hostname.replace(/^www\./, "")}</div>
</a>
{/each}
</div>
{/if}
<!-- Endpoint web sources -->
{#if messageFinalAnswer?.webSources && messageFinalAnswer.webSources.length}
<div class="mt-4 flex flex-wrap items-center gap-x-2 gap-y-1.5 text-sm">
<div class="text-gray-400">Sources:</div>
{#each messageFinalAnswer.webSources as { uri, title }}
<a
class="flex items-center gap-2 whitespace-nowrap rounded-lg border bg-white px-2 py-1.5 leading-none hover:border-gray-300 dark:border-gray-800 dark:bg-gray-900 dark:hover:border-gray-700 transition-colors duration-200"
href={uri}
target="_blank"
>
<img
class="h-3.5 w-3.5 rounded"
src="https://www.google.com/s2/favicons?sz=64&domain_url={new URL(uri).hostname ||
'placeholder'}"
alt="{title} favicon"
/>
<div>{title}</div>
</a>
{/each}
</div>
{/if}
</div>
{#if message.createdAt}
<div
class="absolute -bottom-5 left-12 text-xs text-gray-400 flex items-center gap-1"
>
<CarbonTime class="text-[0.7rem]" />
{formatTime(message.createdAt)}
</div>
{/if}
{#if !loading && (message.content || toolUpdates)}
<div
class="absolute -bottom-4 right-0 flex max-md:transition-all md:group-hover:visible md:group-hover:opacity-100
{message.score ? 'visible opacity-100' : 'invisible max-md:-translate-y-4 max-md:opacity-0'}
{isTapped || isCopied ? 'max-md:visible max-md:translate-y-0 max-md:opacity-100' : ''}
"
>
{#if isAuthor}
<Vote {message} on:vote />
{/if}
<button
class="btn rounded-full p-1.5 text-sm text-gray-400 hover:text-primary-600 hover:bg-primary-50 focus:ring-0 dark:text-gray-400 dark:hover:text-gray-300 dark:hover:bg-gray-800 transition-all duration-200"
title="Retry"
type="button"
onclick={() => {
dispatch("retry", { id: message.id });
}}
>
<CarbonRotate360 />
</button>
<CopyToClipBoardBtn
onClick={() => {
isCopied = true;
}}
classNames="btn rounded-full p-1.5 text-sm text-gray-400 hover:text-primary-600 hover:bg-primary-50 focus:ring-0 dark:text-gray-400 dark:hover:text-gray-300 dark:hover:bg-gray-800 transition-all duration-200"
value={message.content}
/>
</div>
{/if}
</div>
{#if alternatives.length > 1 && editMsdgId === null}
<Alternatives {message} {alternatives} {loading} on:showAlternateMsg />
{/if}
{/if}
{#if message.from === "user"}
<div
class="group relative w-full items-start justify-start gap-4 max-sm:text-sm animate-slide-in"
data-message-id={message.id}
data-message-type="user"
role="presentation"
onclick={() => (isTapped = !isTapped)}
onkeydown={() => (isTapped = !isTapped)}
>
<div class="flex w-full flex-col gap-2">
{#if message.files?.length}
<div class="flex w-fit gap-4 px-5">
{#each message.files as file}
<UploadedFile {file} canClose={false} />
{/each}
</div>
{/if}
<div class="flex w-full flex-row flex-nowrap">
{#if !editMode}
<p
class="disabled w-full appearance-none whitespace-break-spaces text-wrap break-words bg-primary-50/70 dark:bg-primary-900/20 backdrop-blur-sm px-5 py-3.5 text-gray-700 dark:text-gray-200 rounded-2xl shadow-sm"
>
{message.content.trim()}
</p>
{:else}
<form
class="flex w-full flex-col"
bind:this={editFormEl}
onsubmit={(e) => {
e.preventDefault();
dispatch("retry", { content: editContentEl?.value, id: message.id });
editMsdgId = null;
}}
>
<textarea
class="w-full whitespace-break-spaces break-words rounded-xl bg-gray-100 px-5 py-3.5 text-gray-500 *:h-max dark:bg-gray-800 dark:text-gray-400 focus:ring-2 focus:ring-primary-500 dark:focus:ring-primary-400 transition-all duration-200"
rows="5"
bind:this={editContentEl}
value={message.content.trim()}
onkeydown={handleKeyDown}
required
></textarea>
<div class="flex w-full flex-row flex-nowrap items-center justify-center gap-2 pt-2">
<button
type="submit"
class="btn rounded-lg px-4 py-2 text-sm font-medium
{loading
? 'bg-gray-300 text-gray-400 dark:bg-gray-700 dark:text-gray-600'
: 'bg-primary-500 text-white hover:bg-primary-600 focus:ring-2 focus:ring-primary-300 dark:bg-primary-600 dark:hover:bg-primary-700'} transition-all duration-200
"
disabled={loading}
>
Submit
</button>
<button
type="button"
class="btn rounded-lg px-4 py-2 text-sm text-gray-600 hover:text-gray-800 hover:bg-gray-100 focus:ring-0 dark:text-gray-400 dark:hover:text-gray-300 dark:hover:bg-gray-800 transition-all duration-200"
onclick={() => {
editMsdgId = null;
}}
>
Cancel
</button>
</div>
</form>
{/if}
{#if !loading && !editMode}
<div
class="
max-md:opacity-0' invisible absolute
right-0 top-3.5 z-10 h-max max-md:-translate-y-4 max-md:transition-all md:bottom-0 md:group-hover:visible md:group-hover:opacity-100 {isTapped ||
isCopied
? 'max-md:visible max-md:translate-y-0 max-md:opacity-100'
: ''}"
>
<div class="mx-auto flex flex-row flex-nowrap gap-2">
<a
class="rounded-full border border-gray-100 bg-gray-100 p-1.5 text-xs text-gray-400 group-hover:block hover:text-primary-600 hover:bg-primary-50 dark:border-gray-800 dark:bg-gray-800 dark:text-gray-400 dark:hover:text-gray-300 dark:hover:bg-gray-700 max-sm:!hidden md:hidden transition-all duration-200"
title="Download prompt and parameters"
type="button"
target="_blank"
href={downloadLink}
>
<CarbonDownload />
</a>
{#if !readOnly}
<button
class="cursor-pointer rounded-full border border-gray-100 bg-gray-100 p-1.5 text-xs text-gray-400 group-hover:block hover:text-primary-600 hover:bg-primary-50 dark:border-gray-800 dark:bg-gray-800 dark:text-gray-400 dark:hover:text-gray-300 dark:hover:bg-gray-700 md:hidden lg:-right-2 transition-all duration-200"
title="Branch"
type="button"
onclick={() => (editMsdgId = message.id)}
>
<CarbonPen />
</button>
{/if}
</div>
</div>
{/if}
</div>
{#if alternatives.length > 1 && editMsdgId === null}
<Alternatives {message} {alternatives} {loading} on:showAlternateMsg />
{/if}
</div>
{#if message.createdAt}
<div
class="mt-2 text-xs text-gray-400 flex items-center gap-1 ml-auto w-fit"
>
<CarbonTime class="text-[0.7rem]" />
{formatTime(message.createdAt)}
</div>
{/if}
</div>
{/if}
<style lang="postcss">
@keyframes loading {
to {
stroke-dashoffset: 122.9;
}
}
@keyframes slide-in {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.animate-slide-in {
animation: slide-in 0.3s ease-out;
}
</style>
|