Skip to content

Commit

Permalink
Add "disable streaming tokens" settings & remove media query (#1299)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsarrazin authored Jun 24, 2024
1 parent d3298e8 commit 8e551b9
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 22 deletions.
15 changes: 5 additions & 10 deletions src/lib/components/chat/ChatMessage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { marked, type MarkedOptions } from "marked";
import markedKatex from "marked-katex-extension";
import type { Message, MessageFile } from "$lib/types/Message";
import { afterUpdate, createEventDispatcher, onMount, tick } from "svelte";
import { afterUpdate, createEventDispatcher, tick } from "svelte";
import { deepestChild } from "$lib/utils/deepestChild";
import { page } from "$app/stores";
Expand Down Expand Up @@ -30,9 +30,9 @@
} from "$lib/types/MessageUpdate";
import { base } from "$app/paths";
import { useConvTreeStore } from "$lib/stores/convTree";
import { isReducedMotion } from "$lib/utils/isReduceMotion";
import Modal from "../Modal.svelte";
import ToolUpdate from "./ToolUpdate.svelte";
import { useSettingsStore } from "$lib/stores/settings";
function sanitizeMd(md: string) {
let ret = md
Expand Down Expand Up @@ -80,9 +80,6 @@
let isCopied = false;
let initialized = false;
let reducedMotionMode = false;
const renderer = new marked.Renderer();
// For code blocks with simple backticks
renderer.codespan = (code) => {
Expand Down Expand Up @@ -118,12 +115,10 @@
$: emptyLoad =
!message.content && (webSearchIsDone || (searchUpdates && searchUpdates.length === 0));
onMount(() => {
reducedMotionMode = isReducedMotion(window);
});
const settings = useSettingsStore();
afterUpdate(() => {
if (reducedMotionMode) {
if ($settings.disableStream) {
return;
}
Expand Down Expand Up @@ -301,7 +296,7 @@
class="prose max-w-none max-sm:prose-sm dark:prose-invert 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"
bind:this={contentEl}
>
{#if isLast && loading && reducedMotionMode}
{#if isLast && loading && $settings.disableStream}
<IconLoading classNames="loading inline ml-2 first:ml-0" />
{/if}
{#each tokens as token}
Expand Down
1 change: 1 addition & 0 deletions src/lib/stores/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type SettingsStore = {
recentlySaved: boolean;
assistants: Array<ObjectId | string>;
tools?: Record<string, boolean>;
disableStream: boolean;
};

type SettingsStoreWritable = Writable<SettingsStore> & {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/types/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ export interface Settings extends Timestamps {

assistants?: Assistant["_id"][];
tools?: Record<string, boolean>;
disableStream: boolean;
}

export type SettingsEditable = Omit<Settings, "ethicsModalAcceptedAt" | "createdAt" | "updatedAt">;
// TODO: move this to a constant file along with other constants
export const DEFAULT_SETTINGS = {
shareConversationsWithModelAuthors: true,
Expand All @@ -32,4 +34,5 @@ export const DEFAULT_SETTINGS = {
customPrompts: {},
assistants: [],
tools: {},
};
disableStream: false,
} satisfies SettingsEditable;
5 changes: 0 additions & 5 deletions src/lib/utils/isReduceMotion.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
customPrompts: settings?.customPrompts ?? {},
assistants: userAssistants,
tools: settings?.tools ?? {},
disableStream: settings?.disableStream ?? DEFAULT_SETTINGS.disableStream,
},
models: models.map((model) => ({
id: model.id,
Expand Down
5 changes: 1 addition & 4 deletions src/routes/conversation/[id]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import { fetchMessageUpdates } from "$lib/utils/messageUpdates";
import { createConvTreeStore } from "$lib/stores/convTree";
import type { v4 } from "uuid";
import { isReducedMotion } from "$lib/utils/isReduceMotion.js";
import { useSettingsStore } from "$lib/stores/settings.js";
export let data;
Expand Down Expand Up @@ -80,8 +79,6 @@
$isAborted = false;
loading = true;
pending = true;
const reducedMotionMode = isReducedMotion(window);
const base64Files = await Promise.all(
(files ?? []).map((file) =>
file2base64(file).then((value) => ({
Expand Down Expand Up @@ -237,7 +234,7 @@
messageUpdates.push(update);
if (update.type === MessageUpdateType.Stream && !reducedMotionMode) {
if (update.type === MessageUpdateType.Stream && !$settings.disableStream) {
messageToWriteTo.content += update.token;
pending = false;
messages = [...messages];
Expand Down
8 changes: 8 additions & 0 deletions src/routes/settings/(nav)/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@
</div>
</label>

<!-- svelte-ignore a11y-label-has-associated-control -->
<label class="mt-6 flex items-center">
<Switch name="disableStream" bind:checked={$settings.disableStream} />
<div class="inline cursor-pointer select-none items-center gap-2 pl-2">
Disable streaming tokens
</div>
</label>

<div class="mt-12 flex flex-col gap-3">
<a
href="https://huggingface.co/spaces/huggingchat/chat-ui/discussions"
Expand Down
5 changes: 3 additions & 2 deletions src/routes/settings/(nav)/+server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { collections } from "$lib/server/database";
import { z } from "zod";
import { authCondition } from "$lib/server/auth";
import { DEFAULT_SETTINGS } from "$lib/types/Settings";
import { DEFAULT_SETTINGS, type SettingsEditable } from "$lib/types/Settings";

export async function POST({ request, locals }) {
const body = await request.json();
Expand All @@ -16,8 +16,9 @@ export async function POST({ request, locals }) {
activeModel: z.string().default(DEFAULT_SETTINGS.activeModel),
customPrompts: z.record(z.string()).default({}),
tools: z.record(z.boolean()).optional(),
disableStream: z.boolean().default(false),
})
.parse(body);
.parse(body) satisfies SettingsEditable;

await collections.settings.updateOne(
authCondition(locals),
Expand Down

0 comments on commit 8e551b9

Please sign in to comment.