chatbot-template/app/(chat)/actions.ts

43 lines
1.1 KiB
TypeScript
Raw Normal View History

'use server';
2024-11-15 13:00:15 -05:00
import { type CoreUserMessage, generateText } from 'ai';
import { cookies } from 'next/headers';
2024-11-15 12:33:12 -05:00
import { customModel } from '@/lib/ai';
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) {
const cookieStore = await cookies();
2024-10-30 16:01:24 +05:30
cookieStore.set('model-id', model);
}
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;
}
export async function deleteTrailingMessages({ id }: { id: string }) {
const [message] = await getMessageById({ id });
await deleteMessagesByChatIdAfterTimestamp({
chatId: message.chatId,
timestamp: message.createdAt,
});
}