chatbot-template/app/(chat)/actions.ts
2024-12-05 15:29:33 +03:00

42 lines
1.1 KiB
TypeScript

'use server';
import { type CoreUserMessage, generateText } from 'ai';
import { cookies } from 'next/headers';
import { customModel } from '@/lib/ai';
import {
deleteMessagesByChatIdAfterTimestamp,
getMessageById,
} from '@/lib/db/queries';
export async function saveModelId(model: string) {
const cookieStore = await cookies();
cookieStore.set('model-id', model);
}
export async function generateTitleFromUserMessage({
message,
}: {
message: CoreUserMessage;
}) {
const { text: title } = await generateText({
model: customModel('gpt-4o-mini'),
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;
}
export async function deleteTrailingMessages({ id }: { id: string }) {
const [message] = await getMessageById({ id });
await deleteMessagesByChatIdAfterTimestamp({
chatId: message.chatId,
timestamp: message.createdAt,
});
}