Skip to content

Commit e91b76c

Browse files
authored
fix auth on websearch (#410)
1 parent f2c9036 commit e91b76c

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

src/lib/buildPrompt.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,42 @@ import type { BackendModel } from "./server/models";
22
import type { Message } from "./types/Message";
33
import { collections } from "$lib/server/database";
44
import { ObjectId } from "mongodb";
5+
import { authCondition } from "./server/auth";
56
/**
67
* Convert [{user: "assistant", content: "hi"}, {user: "user", content: "hello"}] to:
78
*
89
* <|assistant|>hi<|endoftext|><|prompter|>hello<|endoftext|><|assistant|>
910
*/
1011

11-
export async function buildPrompt(
12-
messages: Pick<Message, "from" | "content">[],
13-
model: BackendModel,
14-
webSearchId?: string,
15-
preprompt?: string
16-
): Promise<string> {
12+
interface buildPromptOptions {
13+
messages: Pick<Message, "from" | "content">[];
14+
model: BackendModel;
15+
locals?: App.Locals;
16+
webSearchId?: string;
17+
preprompt?: string;
18+
}
19+
20+
export async function buildPrompt({
21+
messages,
22+
model,
23+
locals,
24+
webSearchId,
25+
preprompt,
26+
}: buildPromptOptions): Promise<string> {
1727
if (webSearchId) {
1828
const webSearch = await collections.webSearches.findOne({
1929
_id: new ObjectId(webSearchId),
2030
});
2131

2232
if (!webSearch) throw new Error("Web search not found");
33+
if (!locals) throw new Error("User not authenticated");
34+
35+
const conversation = await collections.conversations.findOne({
36+
_id: webSearch.convId,
37+
...authCondition(locals),
38+
});
39+
40+
if (!conversation) throw new Error("Conversation not found");
2341

2442
if (webSearch.summary) {
2543
messages = [

src/routes/conversation/[id]/+server.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,13 @@ export async function POST({ request, fetch, locals, params }) {
9898
];
9999
})() satisfies Message[];
100100

101-
const prompt = await buildPrompt(
101+
const prompt = await buildPrompt({
102102
messages,
103103
model,
104-
web_search_id,
105-
settings?.customPrompts?.[model.id]
106-
);
104+
webSearchId: web_search_id,
105+
preprompt: settings?.customPrompts?.[model.id],
106+
locals: locals,
107+
});
107108

108109
const randomEndpoint = modelEndpoint(model);
109110

src/routes/conversation/[id]/message/[messageId]/prompt/+server.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ export async function GET({ params, locals }) {
3131
throw error(404, "Conversation model not found");
3232
}
3333

34-
const prompt = await buildPrompt(conv.messages.slice(0, messageIndex + 1), model);
34+
const prompt = await buildPrompt({
35+
messages: conv.messages.slice(0, messageIndex + 1),
36+
model: model,
37+
});
3538

3639
return new Response(
3740
JSON.stringify(

src/routes/conversation/[id]/summarize/+server.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ export async function POST({ params, locals }) {
2424
`Please summarize the following message as a single sentence of less than 5 words:\n` +
2525
firstMessage?.content;
2626

27-
const prompt = await buildPrompt([{ from: "user", content: userPrompt }], defaultModel);
27+
const prompt = await buildPrompt({
28+
messages: [{ from: "user", content: userPrompt }],
29+
model: defaultModel,
30+
});
2831
const generated_text = await generateFromDefaultEndpoint(prompt);
2932

3033
if (generated_text) {

src/routes/r/[id]/message/[messageId]/prompt/+server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export async function GET({ params }) {
2626
throw error(404, "Conversation model not found");
2727
}
2828

29-
const prompt = await buildPrompt(conv.messages.slice(0, messageIndex + 1), model);
29+
const prompt = await buildPrompt({ messages: conv.messages.slice(0, messageIndex + 1), model });
3030

3131
return new Response(
3232
JSON.stringify(

0 commit comments

Comments
 (0)