2025-09-21 11:02:31 -07:00
|
|
|
"use server";
|
2024-10-24 16:35:51 -04:00
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
import { generateText, type UIMessage } from "ai";
|
|
|
|
|
import { cookies } from "next/headers";
|
2026-03-20 09:37:02 +00:00
|
|
|
import { auth } from "@/app/(auth)/auth";
|
|
|
|
|
import type { VisibilityType } from "@/components/chat/visibility-selector";
|
|
|
|
|
import { titleModel } from "@/lib/ai/models";
|
2025-10-31 20:09:46 -04:00
|
|
|
import { titlePrompt } from "@/lib/ai/prompts";
|
2025-12-14 21:26:49 +00:00
|
|
|
import { getTitleModel } from "@/lib/ai/providers";
|
2024-12-05 15:29:33 +03:00
|
|
|
import {
|
|
|
|
|
deleteMessagesByChatIdAfterTimestamp,
|
2026-03-20 09:37:02 +00:00
|
|
|
getChatById,
|
2024-12-05 15:29:33 +03:00
|
|
|
getMessageById,
|
2025-11-01 01:44:13 +01:00
|
|
|
updateChatVisibilityById,
|
2025-09-21 11:02:31 -07:00
|
|
|
} from "@/lib/db/queries";
|
2025-10-31 20:09:46 -04:00
|
|
|
import { getTextFromMessage } from "@/lib/utils";
|
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-21 11:02:31 -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
|
|
|
}) {
|
2026-01-15 16:06:42 +00:00
|
|
|
const { text } = await generateText({
|
2025-12-14 21:26:49 +00:00
|
|
|
model: getTitleModel(),
|
2025-10-31 20:09:46 -04:00
|
|
|
system: titlePrompt,
|
|
|
|
|
prompt: getTextFromMessage(message),
|
2026-03-20 09:37:02 +00:00
|
|
|
providerOptions: {
|
|
|
|
|
gateway: { order: titleModel.gatewayOrder },
|
|
|
|
|
},
|
2024-11-05 17:15:51 +03:00
|
|
|
});
|
2026-01-15 16:06:42 +00:00
|
|
|
return text
|
|
|
|
|
.replace(/^[#*"\s]+/, "")
|
|
|
|
|
.replace(/["]+$/, "")
|
|
|
|
|
.trim();
|
2024-11-05 17:15:51 +03:00
|
|
|
}
|
2024-12-05 15:29:33 +03:00
|
|
|
|
|
|
|
|
export async function deleteTrailingMessages({ id }: { id: string }) {
|
2026-03-20 09:37:02 +00:00
|
|
|
const session = await auth();
|
|
|
|
|
if (!session?.user?.id) {
|
|
|
|
|
throw new Error("Unauthorized");
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-05 15:29:33 +03:00
|
|
|
const [message] = await getMessageById({ id });
|
2026-03-20 09:37:02 +00:00
|
|
|
if (!message) {
|
|
|
|
|
throw new Error("Message not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const chat = await getChatById({ id: message.chatId });
|
|
|
|
|
if (!chat || chat.userId !== session.user.id) {
|
|
|
|
|
throw new Error("Unauthorized");
|
|
|
|
|
}
|
2024-12-05 15:29:33 +03:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}) {
|
2026-03-20 09:37:02 +00:00
|
|
|
const session = await auth();
|
|
|
|
|
if (!session?.user?.id) {
|
|
|
|
|
throw new Error("Unauthorized");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const chat = await getChatById({ id: chatId });
|
|
|
|
|
if (!chat || chat.userId !== session.user.id) {
|
|
|
|
|
throw new Error("Unauthorized");
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-01 01:44:13 +01:00
|
|
|
await updateChatVisibilityById({ chatId, visibility });
|
2024-12-06 13:36:56 +03:00
|
|
|
}
|