| <script lang="ts"> |
| import { goto } from "$app/navigation"; |
| import { base } from "$app/paths"; |
| import { page } from "$app/stores"; |
| import { env as envPublic } from "$env/dynamic/public"; |
| 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"; |
| import { onMount } from "svelte"; |
| |
| export let data; |
| let loading = false; |
| let files: File[] = []; |
| |
| const settings = useSettingsStore(); |
| |
| async function createConversation(message: string) { |
| try { |
| loading = true; |
| |
| |
| |
| |
| |
| const validModels = data.models.map((model) => model.id); |
| |
| let model; |
| if (validModels.includes($settings.activeModel)) { |
| model = $settings.activeModel; |
| } else { |
| if (validModels.includes(data.assistant?.modelId)) { |
| model = data.assistant?.modelId; |
| } else { |
| model = data.models[0].id; |
| } |
| } |
| const res = await fetch(`${base}/conversation`, { |
| method: "POST", |
| headers: { |
| "Content-Type": "application/json", |
| }, |
| body: JSON.stringify({ |
| model, |
| preprompt: $settings.customPrompts[$settings.activeModel], |
| assistantId: data.assistant?._id, |
| }), |
| }); |
| |
| if (!res.ok) { |
| const errorMessage = (await res.json()).message || ERROR_MESSAGES.default; |
| error.set(errorMessage); |
| console.error("Error while creating conversation: ", errorMessage); |
| return; |
| } |
| |
| const { conversationId } = await res.json(); |
| |
| |
| pendingMessage.set({ |
| content: message, |
| files, |
| }); |
| |
| |
| await goto(`${base}/conversation/${conversationId}`, { invalidateAll: true }); |
| } catch (err) { |
| error.set((err as Error).message || ERROR_MESSAGES.default); |
| console.error(err); |
| } finally { |
| loading = false; |
| } |
| } |
| |
| onMount(() => { |
| |
| const query = $page.url.searchParams.get("q"); |
| if (query) createConversation(query); |
| }); |
| |
| $: currentModel = findCurrentModel( |
| [...data.models, ...data.oldModels], |
| !$settings.assistants.includes($settings.activeModel) |
| ? $settings.activeModel |
| : data.assistant?.modelId |
| ); |
| </script> |
|
|
| <svelte:head> |
| <title>{envPublic.PUBLIC_APP_NAME}</title> |
| </svelte:head> |
|
|
| <ChatWindow |
| on:message={(ev) => createConversation(ev.detail)} |
| {loading} |
| assistant={data.assistant} |
| {currentModel} |
| models={data.models} |
| bind:files |
| /> |
|
|