File size: 2,412 Bytes
ef5513c
fe2328e
 
98b1c51
fe2328e
 
 
0fcf19a
2e28042
1b66f8d
7764421
1b66f8d
0e5c445
1b66f8d
0fcf19a
 
1b66f8d
 
 
992a8de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67867a9
b2387f6
1b66f8d
b2387f6
 
af58e08
992a8de
a32541c
992a8de
af58e08
1b66f8d
c8d90f4
1b66f8d
537b6f5
 
 
1b66f8d
c8d90f4
 
1b66f8d
743240e
1b66f8d
0e5c445
 
 
 
ef5513c
1b66f8d
67867a9
34857c4
537b6f5
fe2328e
1b66f8d
 
 
ef5513c
 
 
7bd994f
98b1c51
7bd994f
 
7764421
 
 
992a8de
0fcf19a
2606dde
0e5c445
7764421
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
<script lang="ts">
	import { goto } from "$app/navigation";
	import { base } from "$app/paths";
	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";

	export let data;
	let loading = false;
	let files: File[] = [];

	const settings = useSettingsStore();

	async function createConversation(message: string) {
		try {
			loading = true;

			// check if $settings.activeModel is a valid model
			// else check if it's an assistant, and use that model
			// else use the first model

			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();

			// Ugly hack to use a store as temp storage, feel free to improve ^^
			pendingMessage.set({
				content: message,
				files,
			});

			// invalidateAll to update list of conversations
			await goto(`${base}/conversation/${conversationId}`, { invalidateAll: true });
		} catch (err) {
			error.set((err as Error).message || ERROR_MESSAGES.default);
			console.error(err);
		} finally {
			loading = false;
		}
	}
</script>

<svelte:head>
	<title>{envPublic.PUBLIC_APP_NAME}</title>
</svelte:head>

<ChatWindow
	on:message={(ev) => createConversation(ev.detail)}
	{loading}
	assistant={data.assistant}
	currentModel={findCurrentModel([...data.models, ...data.oldModels], $settings.activeModel)}
	models={data.models}
	bind:files
/>