File size: 1,153 Bytes
5da61b4
 
 
6434339
ebac87f
fc15a4c
76d4779
 
5da61b4
fc15a4c
 
 
5da61b4
fc15a4c
 
6434339
 
 
 
 
 
 
 
 
ebac87f
 
 
 
 
6434339
 
 
fc15a4c
5da61b4
9405a81
2606dde
6434339
fc15a4c
76d4779
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
import type { PageServerLoad } from "./$types";
import { collections } from "$lib/server/database";
import { error } from "@sveltejs/kit";
import { ObjectId } from "mongodb";
import type { WebSearchMessageResult, WebSearchMessageSources } from "$lib/types/WebSearch";

export const load: PageServerLoad = async ({ params }) => {
	const conversation = await collections.sharedConversations.findOne({
		_id: params.id,
	});

	if (!conversation) {
		throw error(404, "Conversation not found");
	}

	const webSearchesId = conversation.messages
		.filter((message) => message.webSearchId)
		.map((message) => new ObjectId(message.webSearchId));

	const results = await collections.webSearches.find({ _id: { $in: webSearchesId } }).toArray();

	const searches = Object.fromEntries(
		results.map((x) => [
			x._id.toString(),
			[
				...x.messages,
				{ type: "sources", sources: x.contextSources ?? [] } satisfies WebSearchMessageSources,
				{ type: "result", id: x._id.toString() } satisfies WebSearchMessageResult,
			],
		])
	);

	return {
		messages: conversation.messages,
		title: conversation.title,
		model: conversation.model,
		searches,
	};
};