Spaces:
Running
Running
File size: 6,060 Bytes
5ec491a a1c5622 1c2515f a1c5622 71679ee 009c95b 1006c22 5ec491a a1c5622 5ec491a 04735a9 5ec491a 3c8b24f 5ec491a 04735a9 5ec491a 04735a9 009c95b 4af6326 009c95b 5ec491a 4af6326 5ec491a a1c5622 5ec491a a1c5622 5ec491a 009c95b a1c5622 5ec491a 009c95b a1c5622 5ec491a a1c5622 5ec491a 71679ee 5ec491a a1c5622 009c95b 5ec491a a1c5622 5ec491a 71679ee 009c95b 71679ee 5ec491a a1c5622 5ec491a a1c5622 5ec491a a1c5622 5ec491a a1c5622 5ec491a a1c5622 7286745 a1c5622 bc1cf4e 7286745 a1c5622 5ec491a 92f037b 7286745 5ec491a 71679ee 5ec491a 71679ee 009c95b 71679ee 5ec491a 1006c22 1c2515f 1006c22 1c2515f 1006c22 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
'use server';
import { sessionUser } from '@/auth';
import prisma from './prisma';
import {
ChatWithMessages,
MessageAssistantResponse,
MessageFilterParams,
MessageUserInput,
} from '../types';
import { revalidatePath } from 'next/cache';
import { Chat } from '@prisma/client';
import { isValidDate } from '../utils';
async function getUserConnect() {
const { id: userId } = await sessionUser();
return userId
? {
user: {
connect: { id: userId }, // Connect the chat to an existing user
},
}
: {};
}
/**
* 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,
avatar?: string | null,
) {
// 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) {
if (!user.avatar && avatar) {
await prisma.user.update({
where: { email: email },
data: { avatar: avatar },
});
}
return user;
} else {
return prisma.user.create({
data: {
email: email,
name: name,
avatar: avatar,
},
});
}
}
/**
* Retrieves a user from the database based on the provided ID.
* @param id - The ID of the user to retrieve.
* @returns A Promise that resolves to the user object if found, or null if not found.
*/
export async function dbGetUser(id: string) {
// Try to find the user by email
return await prisma.user.findUnique({
where: { id },
});
}
/**
* 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 },
orderBy: {
createdAt: 'desc',
},
});
}
/**
* 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: {
orderBy: {
createdAt: 'asc',
},
},
},
});
}
/**
* Creates a new chat in the database with the provided information.
* @param {Object} options - The options for creating the chat.
* @param {string} options.id - The ID of the chat (optional).
* @param {string} options.title - The title of the chat (optional).
* @param {MessageUserInput} options.message - The message to be added to the chat.
* @returns {Promise<Chat>} - A promise that resolves to the created chat.
*/
export async function dbPostCreateChat({
id,
title,
mediaUrl,
message,
}: {
id?: string;
title?: string;
mediaUrl: string;
message: MessageUserInput;
}) {
const userConnect = await getUserConnect();
try {
const response = await prisma.chat.create({
data: {
id,
...userConnect,
mediaUrl,
title,
messages: {
create: [{ ...message, ...userConnect }],
},
},
include: {
messages: true,
},
});
revalidatePath('/chat');
return response;
} catch (error) {
console.error(error);
}
}
/**
* Creates a new message in the database for a given chat.
* @param chatId - The ID of the chat where the message will be created.
* @param message - The message to be created.
*/
export async function dbPostCreateMessage(
chatId: string,
message: MessageUserInput,
) {
const userConnect = await getUserConnect();
const resp = await prisma.message.create({
data: {
...message,
...userConnect,
chat: {
connect: { id: chatId },
},
},
});
revalidatePath('/chat');
return resp;
}
/**
* Updates a message in the database with the provided response.
* @param messageId - The ID of the message to update.
* @param messageResponse - The response to update the message with.
*/
export async function dbPostUpdateMessageResponse(
messageId: string,
messageResponse: MessageAssistantResponse,
shouldRevalidatePath = true,
) {
await prisma.message.update({
data: {
response: messageResponse.response,
result: messageResponse.result ?? undefined,
responseBody: messageResponse.responseBody,
streamDuration: messageResponse.streamDuration,
},
where: {
id: messageId,
},
});
shouldRevalidatePath && revalidatePath('/chat');
}
export async function dbDeleteChat(chatId: string) {
await prisma.chat.delete({
where: { id: chatId },
});
revalidatePath('/chat');
return;
}
/**
* Retrieves all messages from the database based on a given date.
* @param date - The date in the format 'YYYY-MM-DD'.
* @returns A promise that resolves to an array of messages.
* @throws Error if the provided date is not valid.
*/
export async function dbInternalGetAllMessageByDate(
messageFilter: MessageFilterParams,
) {
const { date, includeExamples } = messageFilter;
// date must be in the format 'YYYY-MM-DD'
if (!isValidDate(date)) {
throw Error('Not valid date');
}
return prisma.message.findMany({
select: {
id: true,
chatId: true,
prompt: true,
mediaUrl: true,
result: true,
},
where: {
createdAt: {
gte: new Date(date + 'T00:00:00Z'),
lt: new Date(date + 'T23:59:59Z'),
},
mediaUrl: includeExamples
? undefined
: { not: { contains: '/examples/' } },
},
});
}
|