mirror of
https://github.com/open-webui/open-webui.git
synced 2026-11-29 08:31:48 -05:00
refac
This commit is contained in:
1 parent
572974f73f
commit
90ef1f6b0d
9 files changed
+207
-45
No files matched your search
@@ -466,41 +466,41 @@
|
||||
: ''} w-full max-w-full min-w-0 flex flex-col"
|
||||
id="channel-container"
|
||||
>
|
||||
<div class="w-full h-full flex">
|
||||
<div class="h-full flex flex-col min-w-0 flex-1 relative">
|
||||
<Navbar
|
||||
{channel}
|
||||
onPin={pinHandler}
|
||||
onUpdate={async () => {
|
||||
channel = await getChannelById(localStorage.token, id).catch((error) => {
|
||||
return null;
|
||||
});
|
||||
}}
|
||||
>
|
||||
<svelte:fragment slot="subtitle">
|
||||
{#if invitationSession}
|
||||
<div
|
||||
class="flex flex-wrap items-center gap-x-1.5 text-[0.6875rem] font-normal text-base-500 dark:text-base-400"
|
||||
>
|
||||
<span>{$i18n.t('Encrypted chat in progress')}</span>
|
||||
<span aria-hidden="true">·</span>
|
||||
<button
|
||||
type="button"
|
||||
aria-label={$i18n.t('Join encrypted chat')}
|
||||
class="text-[0.6875rem] font-normal text-base-700 underline-offset-2 hover:underline dark:text-base-200"
|
||||
on:click={() => {
|
||||
if (!invitationSession || !canUseEncryption($user, $config?.features)) return;
|
||||
enter(id, { action: 'join', session: invitationSession });
|
||||
void goto(`/channels/${encodeURIComponent(id)}/encrypted`);
|
||||
}}
|
||||
>
|
||||
{$i18n.t('Join')}
|
||||
</button>
|
||||
</div>
|
||||
{/if}
|
||||
</svelte:fragment>
|
||||
</Navbar>
|
||||
<Navbar
|
||||
{channel}
|
||||
onPin={pinHandler}
|
||||
onUpdate={async () => {
|
||||
channel = await getChannelById(localStorage.token, id).catch((error) => {
|
||||
return null;
|
||||
});
|
||||
}}
|
||||
>
|
||||
<svelte:fragment slot="subtitle">
|
||||
{#if invitationSession}
|
||||
<div
|
||||
class="flex flex-wrap items-center gap-x-1.5 text-[0.6875rem] font-normal text-base-500 dark:text-base-400"
|
||||
>
|
||||
<span>{$i18n.t('Encrypted chat in progress')}</span>
|
||||
<span aria-hidden="true">·</span>
|
||||
<button
|
||||
type="button"
|
||||
aria-label={$i18n.t('Join encrypted chat')}
|
||||
class="text-[0.6875rem] font-normal text-base-700 underline-offset-2 hover:underline dark:text-base-200"
|
||||
on:click={() => {
|
||||
if (!invitationSession || !canUseEncryption($user, $config?.features)) return;
|
||||
enter(id, { action: 'join', session: invitationSession });
|
||||
void goto(`/channels/${encodeURIComponent(id)}/encrypted`);
|
||||
}}
|
||||
>
|
||||
{$i18n.t('Join')}
|
||||
</button>
|
||||
</div>
|
||||
{/if}
|
||||
</svelte:fragment>
|
||||
</Navbar>
|
||||
|
||||
<div class="w-full flex-1 min-h-0 flex">
|
||||
<div class="h-full flex flex-col min-w-0 flex-1 relative">
|
||||
{#if channel && messages !== null}
|
||||
<div class="flex-1 overflow-y-auto">
|
||||
<div
|
||||
@@ -598,9 +598,12 @@
|
||||
minWidth={320}
|
||||
maxWidth={640}
|
||||
minSiblingWidth={360}
|
||||
className="h-full"
|
||||
subtleDivider
|
||||
className="h-full px-2 pt-1 pb-2"
|
||||
>
|
||||
<div class="h-full w-full border-l border-base-100/60 dark:border-base-800/40">
|
||||
<div
|
||||
class="h-full w-full rounded-3xl border border-base-200/60 dark:border-base-800/50 shadow-[0_6px_24px_-4px_rgba(0,0,0,0.1)]"
|
||||
>
|
||||
<Thread
|
||||
{threadId}
|
||||
{channel}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
|
||||
import Message from './Messages/Message.svelte';
|
||||
import AppChannelReply from './AppChannelReply.svelte';
|
||||
import DateSeparator from '../common/DateSeparator.svelte';
|
||||
import Loader from '../common/Loader.svelte';
|
||||
import Spinner from '../common/Spinner.svelte';
|
||||
import Grid from '$lib/components/icons/Grid.svelte';
|
||||
@@ -26,6 +27,7 @@
|
||||
updateMessage
|
||||
} from '$lib/apis/channels';
|
||||
import { WEBUI_API_BASE_URL } from '$lib/constants';
|
||||
import { needsDateSeparator } from '$lib/utils';
|
||||
|
||||
const i18n = getContext<typeof import('$lib/i18n').default>('i18n');
|
||||
|
||||
@@ -155,6 +157,16 @@
|
||||
{/if}
|
||||
|
||||
{#each messageList as message, messageIdx (id ? `${id}-${message.client_id ?? message.id}` : (message.client_id ?? message.id))}
|
||||
{@const dateSeparator =
|
||||
messageIdx === 0
|
||||
? top
|
||||
: needsDateSeparator(
|
||||
message.created_at / 1000000,
|
||||
messageList[messageIdx - 1].created_at / 1000000
|
||||
)}
|
||||
{#if dateSeparator}
|
||||
<DateSeparator timestamp={message.created_at / 1000000} />
|
||||
{/if}
|
||||
<Message
|
||||
on:reply={(event) =>
|
||||
receiveAppChannelReply({ ...event.detail, parent_id: thread ? id : null })}
|
||||
@@ -166,6 +178,7 @@
|
||||
disabled={!channel?.write_access || message?.temp_id}
|
||||
pending={!!message?.temp_id}
|
||||
showUserProfile={messageIdx === 0 ||
|
||||
dateSeparator ||
|
||||
Object.values(appReplies).some(
|
||||
(event) => event.message_id === messageList.at(messageIdx - 1)?.id
|
||||
) ||
|
||||
|
||||
@@ -235,7 +235,10 @@
|
||||
</script>
|
||||
|
||||
{#if channel}
|
||||
<div bind:this={threadRoot} class="flex flex-col w-full h-full bg-base-0 dark:bg-base-900">
|
||||
<div
|
||||
bind:this={threadRoot}
|
||||
class="flex flex-col w-full h-full rounded-[inherit] bg-base-0 dark:bg-base-900"
|
||||
>
|
||||
<div class="sticky top-0 flex min-h-10 items-center justify-between px-4 py-1.5">
|
||||
<div class=" font-medium text-sm">{$i18n.t('Thread')}</div>
|
||||
|
||||
|
||||
@@ -6,8 +6,9 @@
|
||||
const i18n = getContext('i18n');
|
||||
|
||||
import { settings } from '$lib/stores';
|
||||
import { copyToClipboard, isInternalMessage } from '$lib/utils';
|
||||
import { copyToClipboard, isInternalMessage, needsDateSeparator } from '$lib/utils';
|
||||
|
||||
import DateSeparator from '$lib/components/common/DateSeparator.svelte';
|
||||
import MultiResponseMessages from './MultiResponseMessages.svelte';
|
||||
import ResponseMessage from './ResponseMessage.svelte';
|
||||
import UserMessage from './UserMessage.svelte';
|
||||
@@ -57,6 +58,29 @@
|
||||
isInternalMessage(history.messages[messageId]) &&
|
||||
history.messages[messageId].childrenIds?.length > 0;
|
||||
|
||||
// Compare against the parent in history, as older messages may not be rendered yet.
|
||||
const showsDateSeparator = (history: any, messageId: string) => {
|
||||
const message = history.messages[messageId];
|
||||
if (!message?.timestamp || isInternalMessage(message)) return false;
|
||||
|
||||
const visited = new Set([messageId]);
|
||||
let previous = history.messages[message.parentId];
|
||||
while (previous && (!previous.timestamp || isInternalMessage(previous))) {
|
||||
if (visited.has(previous.id)) return false;
|
||||
visited.add(previous.id);
|
||||
previous = history.messages[previous.parentId];
|
||||
}
|
||||
|
||||
if (!previous) return true;
|
||||
return needsDateSeparator(
|
||||
message.timestamp * 1000,
|
||||
previous.timestamp * 1000,
|
||||
(previous.meta?.response_ended_at ?? previous.timestamp) * 1000
|
||||
);
|
||||
};
|
||||
|
||||
$: dateSeparator = !compactPreview && showsDateSeparator(history, messageId);
|
||||
|
||||
// Keep the latest turn's real height when assigning a chat ID remounts the message list, and once
|
||||
// it is no longer the latest: virtualizing it on screen blanks it before it is drawn again.
|
||||
let wasCurrentTurn = false;
|
||||
@@ -86,6 +110,10 @@
|
||||
class:message-virtualized={!isWebKit && !isFirefox && !wasCurrentTurn}
|
||||
>
|
||||
{#if history.messages[messageId]}
|
||||
{#if dateSeparator}
|
||||
<DateSeparator timestamp={history.messages[messageId].timestamp * 1000} />
|
||||
{/if}
|
||||
|
||||
{#if history.messages[messageId].role === 'user'}
|
||||
<UserMessage
|
||||
{user}
|
||||
|
||||
@@ -91,7 +91,7 @@
|
||||
});
|
||||
await framePending;
|
||||
if (version !== generation) return;
|
||||
send({ type: 'frameAck', window: 3 });
|
||||
send({ type: 'frameAck', window: 3, h264: native.supportsH264() });
|
||||
retryDelay = 250;
|
||||
status = '';
|
||||
} catch (reason) {
|
||||
|
||||
@@ -1,5 +1,65 @@
|
||||
<script lang="ts">
|
||||
import { onDestroy } from 'svelte';
|
||||
let canvas: HTMLCanvasElement;
|
||||
// Browsers only offer WebCodecs over HTTPS or localhost; the server sends JPEG otherwise.
|
||||
let h264 = typeof VideoDecoder !== 'undefined';
|
||||
let decoder: VideoDecoder | null = null;
|
||||
let decoded = (frame: VideoFrame | null) => frame?.close();
|
||||
export const supportsH264 = () => h264;
|
||||
|
||||
const stopVideo = () => {
|
||||
if (decoder && decoder.state !== 'closed') decoder.close();
|
||||
decoder = null;
|
||||
};
|
||||
onDestroy(stopVideo);
|
||||
|
||||
// Resolves with the frame, or null when this browser cannot decode the stream.
|
||||
const decode = (packet: ArrayBuffer, configLength: number) =>
|
||||
new Promise<VideoFrame | null>((resolve) => {
|
||||
const timeout = setTimeout(() => decoded(null), 3000);
|
||||
decoded = (frame) => {
|
||||
clearTimeout(timeout);
|
||||
decoded = (late) => late?.close();
|
||||
resolve(frame);
|
||||
};
|
||||
try {
|
||||
if (configLength) {
|
||||
stopVideo();
|
||||
const description = new Uint8Array(packet, 14, configLength);
|
||||
const profile = Array.from(description.subarray(1, 4), (byte) =>
|
||||
byte.toString(16).padStart(2, '0')
|
||||
).join('');
|
||||
decoder = new VideoDecoder({
|
||||
output: (frame) => decoded(frame),
|
||||
error: () => decoded(null)
|
||||
});
|
||||
decoder.configure({
|
||||
codec: `avc1.${profile}`,
|
||||
description,
|
||||
optimizeForLatency: true
|
||||
});
|
||||
}
|
||||
if (!decoder) throw new Error('Video started without a configuration');
|
||||
decoder.decode(
|
||||
new EncodedVideoChunk({
|
||||
type: configLength ? 'key' : 'delta',
|
||||
timestamp: performance.now() * 1000,
|
||||
data: new Uint8Array(packet, 14 + configLength)
|
||||
})
|
||||
);
|
||||
} catch {
|
||||
decoded(null);
|
||||
}
|
||||
});
|
||||
|
||||
const paint = (image: CanvasImageSource, width: number, height: number, x: number, y: number) => {
|
||||
if (!canvas) return;
|
||||
if (canvas.width !== width || canvas.height !== height) {
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
}
|
||||
canvas.getContext('2d', { alpha: false })?.drawImage(image, x, y);
|
||||
};
|
||||
export const draw = async (packet: ArrayBuffer) => {
|
||||
if (packet.byteLength < 13) throw new Error('Invalid screen update');
|
||||
const header = new DataView(packet);
|
||||
@@ -8,16 +68,27 @@
|
||||
);
|
||||
if (!width || !height || width > 8192 || height > 8192 || x + w > width || y + h > height)
|
||||
throw new Error('Invalid screen dimensions');
|
||||
if (!w && !h) {
|
||||
if (!h264) return;
|
||||
const frame = await decode(packet, header.getUint16(12));
|
||||
if (!frame) {
|
||||
// The next acknowledgment asks the server for JPEG and a full picture.
|
||||
h264 = false;
|
||||
stopVideo();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
paint(frame, width, height, 0, 0);
|
||||
} finally {
|
||||
frame.close();
|
||||
}
|
||||
return;
|
||||
}
|
||||
const image = await createImageBitmap(
|
||||
new Blob([new Uint8Array(packet, 12)], { type: 'image/jpeg' })
|
||||
);
|
||||
try {
|
||||
if (!canvas) return;
|
||||
if (canvas.width !== width || canvas.height !== height) {
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
}
|
||||
canvas.getContext('2d', { alpha: false })?.drawImage(image, x, y, w, h);
|
||||
paint(image, width, height, x, y);
|
||||
} finally {
|
||||
image.close();
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
let pending: Record<string, unknown> | null = null;
|
||||
let frame = 0;
|
||||
export const draw = (packet: ArrayBuffer) => frames.draw(packet);
|
||||
export const supportsH264 = () => frames.supportsH264();
|
||||
const flush = () => {
|
||||
cancelAnimationFrame(frame);
|
||||
if (pending) send(pending);
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<script lang="ts">
|
||||
import { getContext } from 'svelte';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
import { formatDate, formatMessageTimestampFull } from '$lib/utils';
|
||||
import Tooltip from './Tooltip.svelte';
|
||||
|
||||
const i18n = getContext<typeof import('$lib/i18n').default>('i18n');
|
||||
|
||||
// Milliseconds since epoch
|
||||
export let timestamp: number;
|
||||
|
||||
$: date = dayjs(timestamp);
|
||||
$: label = $i18n.t(formatDate(timestamp), {
|
||||
LOCALIZED_TIME: date.locale($i18n.language).format('LT'),
|
||||
LOCALIZED_DATE: date.toDate().toLocaleDateString($i18n.language, {
|
||||
weekday: 'short',
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
...(date.isSame(dayjs(), 'year') ? {} : { year: 'numeric' })
|
||||
})
|
||||
});
|
||||
</script>
|
||||
|
||||
<div class="flex justify-center pt-4 pb-3">
|
||||
<Tooltip content={formatMessageTimestampFull(timestamp, $i18n.language)}>
|
||||
<time
|
||||
datetime={date.toISOString()}
|
||||
class="text-xs tabular-nums text-base-500 dark:text-base-400 select-none">{label}</time
|
||||
>
|
||||
</Tooltip>
|
||||
</div>
|
||||
@@ -557,6 +557,17 @@ export const formatDate = (inputDate) => {
|
||||
}
|
||||
};
|
||||
|
||||
// Whether a message needs a date line above it: the first one, a new day, or an hour after
|
||||
// the previous message finished. All times are in milliseconds.
|
||||
export const needsDateSeparator = (
|
||||
sentAt: number,
|
||||
previousSentAt?: number | null,
|
||||
previousEndedAt = previousSentAt
|
||||
) =>
|
||||
!previousSentAt ||
|
||||
!dayjs(sentAt).isSame(previousSentAt, 'day') ||
|
||||
sentAt - (previousEndedAt ?? previousSentAt) > 60 * 60 * 1000;
|
||||
|
||||
const messageTimestampDate = (inputDate) => {
|
||||
const date = new Date(inputDate < 1_000_000_000_000 ? inputDate * 1000 : inputDate);
|
||||
return Number.isNaN(date.getTime()) ? null : date;
|
||||
|
||||
Reference in new issue
Block a user