Skip to content

Commit

Permalink
fix: (#621)
Browse files Browse the repository at this point in the history
- query param sets model correctly
- fix bugs related to preprompt
  • Loading branch information
nsarrazin authored Dec 11, 2023
1 parent 4846e0f commit 0fcf19a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 31 deletions.
7 changes: 4 additions & 3 deletions src/lib/components/chat/ChatIntroduction.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
import AnnouncementBanner from "../AnnouncementBanner.svelte";
import type { Model } from "$lib/types/Model";
import ModelCardMetadata from "../ModelCardMetadata.svelte";
import type { LayoutData } from "../../../routes/$types";
import { findCurrentModel } from "$lib/utils/models";
import { base } from "$app/paths";
import { useSettingsStore } from "$lib/stores/settings";
export let currentModel: Model;
export let settings: LayoutData["settings"];
export let models: Model[];
$: currentModelMetadata = findCurrentModel(models, settings.activeModel);
const settings = useSettingsStore();
$: currentModelMetadata = findCurrentModel(models, $settings.activeModel);
const announcementBanners = PUBLIC_ANNOUNCEMENT_BANNERS
? JSON.parse(PUBLIC_ANNOUNCEMENT_BANNERS)
Expand Down
7 changes: 2 additions & 5 deletions src/lib/components/chat/ChatMessages.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@
import { tick } from "svelte";
import { randomUUID } from "$lib/utils/randomUuid";
import type { Model } from "$lib/types/Model";
import type { LayoutData } from "../../../routes/$types";
import ChatIntroduction from "./ChatIntroduction.svelte";
import ChatMessage from "./ChatMessage.svelte";
import type { WebSearchUpdate } from "$lib/types/MessageUpdate";
import { browser } from "$app/environment";
import SystemPromptModal from "../SystemPromptModal.svelte";
import { page } from "$app/stores";
export let messages: Message[];
export let loading: boolean;
export let pending: boolean;
export let isAuthor: boolean;
export let currentModel: Model;
export let settings: LayoutData["settings"];
export let models: Model[];
export let preprompt: string | undefined;
export let readOnly: boolean;
Expand All @@ -45,7 +42,7 @@
>
<div class="mx-auto flex h-full max-w-3xl flex-col gap-6 px-5 pt-6 sm:gap-8 xl:max-w-4xl">
{#each messages as message, i}
{#if i === 0 && preprompt !== $page.data.settings.customPrompts[currentModel.id]}
{#if i === 0 && preprompt && preprompt != currentModel.preprompt}
<SystemPromptModal preprompt={preprompt ?? ""} />
{/if}
<ChatMessage
Expand All @@ -59,7 +56,7 @@
on:vote
/>
{:else}
<ChatIntroduction {settings} {models} {currentModel} on:message />
<ChatIntroduction {models} {currentModel} on:message />
{/each}
{#if pending}
<ChatMessage
Expand Down
1 change: 0 additions & 1 deletion src/lib/components/chat/ChatWindow.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
<ChatMessages
{loading}
{pending}
settings={$page.data.settings}
{currentModel}
{models}
{messages}
Expand Down
19 changes: 1 addition & 18 deletions src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { redirect } from "@sveltejs/kit";
import type { LayoutServerLoad } from "./$types";
import { collections } from "$lib/server/database";
import type { Conversation } from "$lib/types/Conversation";
Expand All @@ -14,26 +13,10 @@ import {
USE_LOCAL_WEBSEARCH,
} from "$env/static/private";

export const load: LayoutServerLoad = async ({ locals, depends, url }) => {
export const load: LayoutServerLoad = async ({ locals, depends }) => {
const { conversations } = collections;
const urlModel = url.searchParams.get("model");

depends(UrlDependency.ConversationList);

if (urlModel) {
const isValidModel = validateModel(models).safeParse(urlModel).success;

if (isValidModel) {
await collections.settings.updateOne(
authCondition(locals),
{ $set: { activeModel: urlModel } },
{ upsert: true }
);
}

throw redirect(302, url.pathname);
}

const settings = await collections.settings.findOne(authCondition(locals));

// If the active model in settings is not valid, set it to the default model. This can happen if model was disabled.
Expand Down
10 changes: 9 additions & 1 deletion src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import { PUBLIC_APP_ASSETS, PUBLIC_APP_NAME } from "$env/static/public";
import titleUpdate from "$lib/stores/titleUpdate";
import { createSettingsStore } from "$lib/stores/settings";
import { browser } from "$app/environment";
export let data;
Expand Down Expand Up @@ -104,7 +105,14 @@
$titleUpdate = null;
}
createSettingsStore(data.settings);
const settings = createSettingsStore(data.settings);
$: if (browser && $page.url.searchParams.has("model")) {
if ($settings.activeModel === $page.url.searchParams.get("model")) {
goto("/?");
}
$settings.activeModel = $page.url.searchParams.get("model") ?? $settings.activeModel;
}
</script>

<svelte:head>
Expand Down
9 changes: 6 additions & 3 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import ChatWindow from "$lib/components/chat/ChatWindow.svelte";
import { ERROR_MESSAGES, error } from "$lib/stores/errors";
import { pendingMessage } from "$lib/stores/pendingMessage";
import { useSettingsStore } from "$lib/stores/settings.js";
import { findCurrentModel } from "$lib/utils/models";
export let data;
let loading = false;
let files: File[] = [];
const settings = useSettingsStore();
async function createConversation(message: string) {
try {
loading = true;
Expand All @@ -20,8 +23,8 @@
"Content-Type": "application/json",
},
body: JSON.stringify({
model: data.settings.activeModel,
preprompt: data.settings.customPrompts[data.settings.activeModel],
model: $settings.activeModel,
preprompt: $settings.customPrompts[data.settings.activeModel],
}),
});
Expand Down Expand Up @@ -57,7 +60,7 @@
<ChatWindow
on:message={(ev) => createConversation(ev.detail)}
{loading}
currentModel={findCurrentModel([...data.models, ...data.oldModels], data.settings.activeModel)}
currentModel={findCurrentModel([...data.models, ...data.oldModels], $settings.activeModel)}
models={data.models}
bind:files
/>

0 comments on commit 0fcf19a

Please sign in to comment.