File size: 1,629 Bytes
2e28042
 
b8a746c
2e28042
b8a746c
 
 
 
a799675
 
 
 
 
 
 
 
 
 
 
b8a746c
 
 
 
 
 
 
 
 
 
 
cf7ac8d
b8a746c
 
 
 
 
a799675
 
e91b76c
a799675
 
 
e91b76c
 
b8a746c
 
 
 
 
 
2fde762
b8a746c
 
 
 
 
 
 
 
 
 
 
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
import { buildPrompt } from "$lib/buildPrompt";
import { authCondition } from "$lib/server/auth";
import { collections } from "$lib/server/database";
import { models } from "$lib/server/models";
import { error } from "@sveltejs/kit";
import { ObjectId } from "mongodb";

export async function GET({ params, locals }) {
	const conv =
		params.id.length === 7
			? await collections.sharedConversations.findOne({
					_id: params.id,
			  })
			: await collections.conversations.findOne({
					_id: new ObjectId(params.id),
					...authCondition(locals),
			  });

	if (conv === null) {
		throw error(404, "Conversation not found");
	}

	const messageId = params.messageId;

	const messageIndex = conv.messages.findIndex((msg) => msg.id === messageId);

	if (messageIndex === -1) {
		throw error(404, "Message not found");
	}

	const model = models.find((m) => m.id === conv.model);

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

	const messagesUpTo = conv.messages.slice(0, messageIndex + 1);

	const prompt = await buildPrompt({
		preprompt: conv.preprompt,
		webSearch: messagesUpTo[messagesUpTo.length - 1].webSearch,
		messages: messagesUpTo,
		model: model,
	});

	return new Response(
		JSON.stringify(
			{
				note: "This is a preview of the prompt that will be sent to the model when retrying the message. It may differ from what was sent in the past if the parameters have been updated since",
				prompt,
				model: model.name,
				parameters: {
					...model.parameters,
					return_full_text: false,
				},
			},
			null,
			2
		),
		{ headers: { "Content-Type": "application/json" } }
	);
}