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

54 lines
1.4 KiB
TypeScript
Raw Normal View History

'use server';
import { generateText, type UIMessage } from 'ai';
import { cookies } from 'next/headers';
import {
deleteMessagesByChatIdAfterTimestamp,
getMessageById,
updateChatVisiblityById,
} from '@/lib/db/queries';
import type { VisibilityType } from '@/components/visibility-selector';
2025-03-04 17:25:46 -08:00
import { myProvider } from '@/lib/ai/providers';
2024-11-05 17:15:51 +03:00
export async function saveChatModelAsCookie(model: string) {
const cookieStore = await cookies();
cookieStore.set('chat-model', model);
}
2024-11-05 17:15:51 +03:00
export async function generateTitleFromUserMessage({
message,
}: {
message: UIMessage;
2024-11-05 17:15:51 +03:00
}) {
const { text: title } = await generateText({
model: myProvider.languageModel('title-model'),
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,
});
}
export async function updateChatVisibility({
chatId,
visibility,
}: {
chatId: string;
visibility: VisibilityType;
}) {
await updateChatVisiblityById({ chatId, visibility });
}