vision-agent / lib /db /functions.ts
MingruiZhang's picture
feat: Restruct Composer and Homepage (#64)
92f037b unverified
raw
history blame
No virus
4.23 kB
'use server';
import { sessionUser } from '@/auth';
import prisma from './prisma';
import { ChatWithMessages, MessageRaw } from './types';
import { revalidatePath } from 'next/cache';
import { Chat } from '@prisma/client';
/**
* Finds or creates a user in the database based on the provided email and name.
* If the user already exists, it returns the existing user.
* If the user doesn't exist, it creates a new user and returns it.
*
* @param email - The email of the user.
* @param name - The name of the user.
* @returns A promise that resolves to the user object.
*/
export async function dbFindOrCreateUser(email: string, name: string) {
// Try to find the user by email
const user = await prisma.user.findUnique({
where: { email: email },
});
// If the user doesn't exist, create it
if (user) {
return user;
} else {
return prisma.user.create({
data: {
email: email,
name: name,
},
});
}
}
/**
* Retrieves all chat records from the database for the current user.
* @returns A promise that resolves to an array of Chat objects.
*/
export async function dbGetMyChatList(): Promise<Chat[]> {
const { id: userId } = await sessionUser();
if (!userId) return [];
return prisma.chat.findMany({
where: { userId },
});
}
/**
* Retrieves all chats with their associated messages for the current user.
* @returns A promise that resolves to an array of `ChatWithMessages` objects.
*/
export async function dbGetMyChatListWithMessages(): Promise<
ChatWithMessages[]
> {
const { id: userId } = await sessionUser();
if (!userId) return [];
return prisma.chat.findMany({
where: { userId },
include: {
messages: true,
},
});
}
/**
* Retrieves a chat with messages from the database based on the provided ID.
* @param id - The ID of the chat to retrieve.
* @returns A Promise that resolves to a ChatWithMessages object if found, or null if not found.
*/
export async function dbGetChat(id: string): Promise<ChatWithMessages | null> {
return prisma.chat.findUnique({
where: { id },
include: {
messages: true,
},
});
}
/**
* Creates a new chat in the database.
*
* @param {string} options.id - The ID of the chat (optional).
* @param {string} options.mediaUrl - The media URL for the chat.
* @param {MessageRaw[]} options.initMessages - The initial messages for the chat (optional).
* @returns {Promise<Chat>} The created chat object.
*/
export async function dbPostCreateChat({
id,
mediaUrl,
title,
initMessages = [],
}: {
id?: string;
mediaUrl: string;
title?: string;
initMessages?: MessageRaw[];
}) {
const { id: userId } = await sessionUser();
const userConnect = userId
? {
user: {
connect: { id: userId }, // Connect the chat to an existing user
},
}
: {};
try {
const response = await prisma.chat.create({
data: {
id,
mediaUrl: mediaUrl,
...userConnect,
title,
messages: {
create: initMessages.map(message => ({
...message,
...userConnect,
})),
},
},
include: {
messages: true,
},
});
revalidatePath('/chat');
return response;
} catch (error) {
console.error(error);
}
}
/**
* Creates a new message in the database.
* @param chatId - The ID of the chat where the message belongs.
* @param message - The message object to be created.
* @returns A promise that resolves to the created message.
*/
export async function dbPostCreateMessage(chatId: string, message: MessageRaw) {
const { id: userId } = await sessionUser();
const userConnect = userId
? {
user: {
connect: { id: userId }, // Connect the chat to an existing user
},
}
: {};
await prisma.message.create({
data: {
content: message.content,
role: message.role,
chat: {
connect: { id: chatId },
},
...userConnect,
},
});
revalidatePath('/chat');
}
export async function dbDeleteChat(chatId: string) {
await prisma.chat.delete({
where: { id: chatId },
});
revalidatePath('/chat');
return;
}