File size: 1,831 Bytes
e99e7c2
 
 
d3e9153
0c3e3b2
cc54380
e99e7c2
 
 
 
 
 
 
 
 
 
 
df3243b
e99e7c2
0c3e3b2
d3e9153
821697c
e99e7c2
d3e9153
e99e7c2
cc54380
a70e605
cc54380
a70e605
 
 
 
 
 
 
 
 
 
 
 
cc54380
 
e99e7c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { collections } from "$lib/server/database";
import { z } from "zod";
import { authCondition } from "$lib/server/auth";
import { DEFAULT_SETTINGS, type SettingsEditable } from "$lib/types/Settings";
import { toolFromConfigs } from "$lib/server/tools/index.js";
import { ObjectId } from "mongodb";

export async function POST({ request, locals }) {
	const body = await request.json();

	const { ethicsModalAccepted, ...settings } = z
		.object({
			shareConversationsWithModelAuthors: z
				.boolean()
				.default(DEFAULT_SETTINGS.shareConversationsWithModelAuthors),
			hideEmojiOnSidebar: z.boolean().default(DEFAULT_SETTINGS.hideEmojiOnSidebar),
			ethicsModalAccepted: z.boolean().optional(),
			activeModel: z.string().default(DEFAULT_SETTINGS.activeModel),
			customPrompts: z.record(z.string()).default({}),
			tools: z.array(z.string()).optional(),
			disableStream: z.boolean().default(false),
			directPaste: z.boolean().default(false),
		})
		.parse(body) satisfies SettingsEditable;

	// make sure all tools exist
	// either in db or in config
	if (settings.tools) {
		const newTools = [
			...(await collections.tools
				.find({ _id: { $in: settings.tools.map((toolId) => new ObjectId(toolId)) } })
				.project({ _id: 1 })
				.toArray()
				.then((tools) => tools.map((tool) => tool._id.toString()))),
			...toolFromConfigs
				.filter((el) => (settings?.tools ?? []).includes(el._id.toString()))
				.map((el) => el._id.toString()),
		];

		settings.tools = newTools;
	}

	await collections.settings.updateOne(
		authCondition(locals),
		{
			$set: {
				...settings,
				...(ethicsModalAccepted && { ethicsModalAcceptedAt: new Date() }),
				updatedAt: new Date(),
			},
			$setOnInsert: {
				createdAt: new Date(),
			},
		},
		{
			upsert: true,
		}
	);
	// return ok response
	return new Response();
}