2024-10-24 16:35:51 -04:00
|
|
|
'use server';
|
|
|
|
|
|
2024-11-15 13:00:15 -05:00
|
|
|
import { type CoreUserMessage, generateText } from 'ai';
|
2024-10-24 16:35:51 -04:00
|
|
|
import { cookies } from 'next/headers';
|
|
|
|
|
|
2024-11-15 12:33:12 -05:00
|
|
|
import { customModel } from '@/lib/ai';
|
2024-12-05 15:29:33 +03:00
|
|
|
import {
|
|
|
|
|
deleteMessagesByChatIdAfterTimestamp,
|
|
|
|
|
getMessageById,
|
|
|
|
|
} from '@/lib/db/queries';
|
2024-11-05 17:15:51 +03:00
|
|
|
|
2024-10-30 16:01:24 +05:30
|
|
|
export async function saveModelId(model: string) {
|
2024-10-24 16:35:51 -04:00
|
|
|
const cookieStore = await cookies();
|
2024-10-30 16:01:24 +05:30
|
|
|
cookieStore.set('model-id', model);
|
2024-10-24 16:35:51 -04:00
|
|
|
}
|
2024-11-05 17:15:51 +03:00
|
|
|
|
|
|
|
|
export async function generateTitleFromUserMessage({
|
|
|
|
|
message,
|
|
|
|
|
}: {
|
|
|
|
|
message: CoreUserMessage;
|
|
|
|
|
}) {
|
|
|
|
|
const { text: title } = await generateText({
|
2024-11-15 10:15:49 -05:00
|
|
|
model: customModel('gpt-4o-mini'),
|
2024-11-05 17:15:51 +03:00
|
|
|
system: `\n
|
|
|
|
|
- you will generate a short title based on the first message a user begins a conversation with
|
|
|
|
|
- ensure it is not more than 80 characters long
|
|
|
|
|
- the title should be a summary of the user's message
|
|
|
|
|
- do not use quotes or colons`,
|
|
|
|
|
prompt: JSON.stringify(message),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return title;
|
|
|
|
|
}
|
2024-12-05 15:29:33 +03:00
|
|
|
|
|
|
|
|
export async function deleteTrailingMessages({ id }: { id: string }) {
|
|
|
|
|
const [message] = await getMessageById({ id });
|
|
|
|
|
|
|
|
|
|
await deleteMessagesByChatIdAfterTimestamp({
|
|
|
|
|
chatId: message.chatId,
|
|
|
|
|
timestamp: message.createdAt,
|
|
|
|
|
});
|
|
|
|
|
}
|