| import type { Conversation } from "$lib/types/Conversation"; |
| import type { Message } from "$lib/types/Message"; |
| import { format } from "date-fns"; |
| import { downloadFile } from "./files/downloadFile"; |
| import { logger } from "$lib/server/logger"; |
|
|
| export async function preprocessMessages( |
| messages: Message[], |
| webSearch: Message["webSearch"], |
| multimodal: boolean, |
| id: Conversation["_id"] |
| ): Promise<Message[]> { |
| return await Promise.all( |
| structuredClone(messages).map(async (message, idx) => { |
| const webSearchContext = webSearch?.contextSources |
| .map(({ context }) => context) |
| .flat() |
| .sort((a, b) => a.idx - b.idx) |
| .map(({ text }) => text) |
| .join(" "); |
| |
| if (idx === messages.length - 1 && webSearch && webSearchContext?.trim()) { |
| const lastQuestion = messages.findLast((el) => el.from === "user")?.content ?? ""; |
| const previousQuestions = messages |
| .filter((el) => el.from === "user") |
| .slice(0, -1) |
| .map((el) => el.content); |
| const currentDate = format(new Date(), "MMMM d, yyyy"); |
|
|
| message.content = `I searched the web using the query: ${webSearch.searchQuery}. |
| Today is ${currentDate} and here are the results: |
| ===================== |
| ${webSearchContext} |
| ===================== |
| ${previousQuestions.length > 0 ? `Previous questions: \n- ${previousQuestions.join("\n- ")}` : ""} |
| Answer the question: ${lastQuestion}`; |
| } |
| |
| if (multimodal) { |
| if (message.files && message.files.length > 0) { |
| const markdowns = await Promise.all( |
| message.files.map(async (hash) => { |
| try { |
| const { content: image, mime } = await downloadFile(hash, id); |
| const b64 = image.toString("base64"); |
| return `})`; |
| } catch (e) { |
| logger.error(e); |
| } |
| }) |
| ); |
| message.content += markdowns.join("\n "); |
| } else { |
| |
| message.content += |
| "\n"; |
| } |
| } |
|
|
| return message; |
| }) |
| ); |
| } |
|
|