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";
|
|
|
|
|
import type { VisibilityType } from "@/components/visibility-selector";
|
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,
|
|
|
|
|
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),
|
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 }) {
|
|
|
|
|
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;
|
|
|
|
|
}) {
|
2025-11-01 01:44:13 +01:00
|
|
|
await updateChatVisibilityById({ chatId, visibility });
|
2024-12-06 13:36:56 +03:00
|
|
|
}
|