Spaces:
Running
Running
feat(prompt): enhance prompt preview with assistant, message details & error catching
Browse files
src/routes/conversation/[id]/message/[messageId]/prompt/+server.ts
CHANGED
@@ -36,29 +36,46 @@ export async function GET({ params, locals }) {
|
|
36 |
error(404, "Conversation model not found");
|
37 |
}
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
const messagesUpTo = buildSubtree(conv, messageId);
|
40 |
|
41 |
const prompt = await buildPrompt({
|
42 |
preprompt: conv.preprompt,
|
43 |
messages: messagesUpTo,
|
44 |
model,
|
|
|
|
|
|
|
45 |
});
|
46 |
|
47 |
-
const userMessage = conv.messages[messageIndex];
|
48 |
-
const assistantMessage = conv.messages[messageIndex + 1];
|
49 |
-
|
50 |
return new Response(
|
51 |
JSON.stringify(
|
52 |
{
|
53 |
-
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",
|
54 |
prompt,
|
55 |
model: model.name,
|
|
|
56 |
parameters: {
|
57 |
...model.parameters,
|
|
|
58 |
return_full_text: false,
|
59 |
},
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
},
|
63 |
null,
|
64 |
2
|
|
|
36 |
error(404, "Conversation model not found");
|
37 |
}
|
38 |
|
39 |
+
let assistant;
|
40 |
+
if (conv.assistantId) {
|
41 |
+
assistant = await collections.assistants.findOne({
|
42 |
+
_id: new ObjectId(conv.assistantId),
|
43 |
+
});
|
44 |
+
}
|
45 |
+
|
46 |
const messagesUpTo = buildSubtree(conv, messageId);
|
47 |
|
48 |
const prompt = await buildPrompt({
|
49 |
preprompt: conv.preprompt,
|
50 |
messages: messagesUpTo,
|
51 |
model,
|
52 |
+
}).catch((err) => {
|
53 |
+
console.error(err);
|
54 |
+
return "Prompt generation failed";
|
55 |
});
|
56 |
|
|
|
|
|
|
|
57 |
return new Response(
|
58 |
JSON.stringify(
|
59 |
{
|
|
|
60 |
prompt,
|
61 |
model: model.name,
|
62 |
+
assistant: assistant?.name,
|
63 |
parameters: {
|
64 |
...model.parameters,
|
65 |
+
...(assistant?.generateSettings || {}),
|
66 |
return_full_text: false,
|
67 |
},
|
68 |
+
messages: messagesUpTo.map((msg) => ({
|
69 |
+
role: msg.from,
|
70 |
+
content: msg.content,
|
71 |
+
createdAt: msg.createdAt,
|
72 |
+
updatedAt: msg.updatedAt,
|
73 |
+
reasoning: msg.reasoning,
|
74 |
+
updates: msg.updates?.filter(
|
75 |
+
(u) => (u.type === "webSearch" && u.subtype === "sources") || u.type === "title"
|
76 |
+
),
|
77 |
+
files: msg.files,
|
78 |
+
})),
|
79 |
},
|
80 |
null,
|
81 |
2
|