2025-09-20 12:47:10 -07:00
|
|
|
"use server";
|
2024-10-24 16:35:51 -04:00
|
|
|
|
2025-09-20 12:47:10 -07:00
|
|
|
import { generateText, type UIMessage } from "ai";
|
|
|
|
|
import { cookies } from "next/headers";
|
|
|
|
|
import type { VisibilityType } from "@/components/visibility-selector";
|
|
|
|
|
import { myProvider } from "@/lib/ai/providers";
|
2024-12-05 15:29:33 +03:00
|
|
|
import {
|
|
|
|
|
deleteMessagesByChatIdAfterTimestamp,
|
|
|
|
|
getMessageById,
|
2024-12-06 13:36:56 +03:00
|
|
|
updateChatVisiblityById,
|
2025-09-20 12:47:10 -07:00
|
|
|
} from "@/lib/db/queries";
|
2024-11-05 17:15:51 +03:00
|
|
|
|
2025-02-03 20:33:15 +05:30
|
|
|
export async function saveChatModelAsCookie(model: string) {
|
2024-10-24 16:35:51 -04:00
|
|
|
const cookieStore = await cookies();
|
2025-09-20 12:47:10 -07:00
|
|
|
cookieStore.set("chat-model", model);
|
2024-10-24 16:35:51 -04:00
|
|
|
}
|
2024-11-05 17:15:51 +03:00
|
|
|
|
|
|
|
|
export async function generateTitleFromUserMessage({
|
|
|
|
|
message,
|
|
|
|
|
}: {
|
2025-04-26 01:09:01 -07:00
|
|
|
message: UIMessage;
|
2024-11-05 17:15:51 +03:00
|
|
|
}) {
|
|
|
|
|
const { text: title } = await generateText({
|
2025-09-20 12:47:10 -07:00
|
|
|
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;
|
|
|
|
|
}
|
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,
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-12-06 13:36:56 +03:00
|
|
|
|
|
|
|
|
export async function updateChatVisibility({
|
|
|
|
|
chatId,
|
|
|
|
|
visibility,
|
|
|
|
|
}: {
|
|
|
|
|
chatId: string;
|
|
|
|
|
visibility: VisibilityType;
|
|
|
|
|
}) {
|
|
|
|
|
await updateChatVisiblityById({ chatId, visibility });
|
|
|
|
|
}
|