File size: 2,809 Bytes
7f1b669
ffb0dba
 
d433b55
a8a9533
ffb0dba
 
 
48288dc
d7b4e1d
247566a
8aab1a5
a1a6daf
 
 
8aab1a5
48288dc
 
8aab1a5
 
 
df3243b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd8f105
6a0861b
8aab1a5
6a0861b
 
4ae2179
df3243b
8c05151
df3243b
4ae2179
8aab1a5
4ef1838
8aab1a5
6d03e59
 
 
8aab1a5
4ef1838
 
8aab1a5
873a99f
8aab1a5
241ba68
 
 
 
7f1b669
8aab1a5
bd8f105
231a23d
6d03e59
ffb0dba
8aab1a5
 
 
7f1b669
247566a
 
 
d433b55
247566a
 
13ed034
a1a6daf
 
 
 
 
 
 
13ed034
7f1b669
 
5609571
a8a9533
5609571
 
d13f9cf
 
 
df3243b
13ed034
01b06a3
241ba68
d13f9cf
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<script lang="ts">
	import { goto } from "$app/navigation";
	import { base } from "$app/paths";
	import { page } from "$app/state";
	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";

	let { data } = $props();
	let loading = $state(false);
	let files: File[] = $state([]);

	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;
		}
	}

	onMount(() => {
		// check if there's a ?q query param with a message
		const query = page.url.searchParams.get("q");
		if (query) createConversation(query);
	});

	let currentModel = $derived(
		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
/>