MCPcopy Create free account
hub / github.com/sourcebot-dev/sourcebot / duplicateChat

Function duplicateChat

packages/web/src/features/chat/actions.ts:256–299  ·  view source on GitHub ↗
({ chatId, newName }: { chatId: string, newName: string })

Source from the content-addressed store, hash-verified

254 * The new chat will be owned by the current user (authenticated or anonymous).
255 */
256export const duplicateChat = async ({ chatId, newName }: { chatId: string, newName: string }) => sew(() =>
257 withOptionalAuth(async ({ org, user, prisma }) => {
258 const askError = await checkAskEntitlement();
259 if (askError) {
260 return askError;
261 }
262
263 const originalChat = await prisma.chat.findUnique({
264 where: {
265 id: chatId,
266 orgId: org.id,
267 },
268 });
269
270 if (!originalChat) {
271 return notFound();
272 }
273
274 // Check if user can access the chat (owner, shared, or public)
275 const isOwner = await isOwnerOfChat(originalChat, user);
276 const isSharedWithUser = await isChatSharedWithUser({ prisma, chatId, userId: user?.id });
277 if (originalChat.visibility === ChatVisibility.PRIVATE && !isOwner && !isSharedWithUser) {
278 return notFound();
279 }
280
281 const isGuestUser = user === undefined;
282 const anonymousCreatorId = isGuestUser ? await getOrCreateAnonymousId() : undefined;
283
284 const newChat = await prisma.chat.create({
285 data: {
286 orgId: org.id,
287 name: newName,
288 messages: originalChat.messages as unknown as Prisma.InputJsonValue,
289 createdById: user?.id,
290 anonymousCreatorId,
291 visibility: isGuestUser ? ChatVisibility.PUBLIC : ChatVisibility.PRIVATE,
292 },
293 });
294
295 return {
296 id: newChat.id,
297 };
298 })
299);
300
301/**
302 * Returns the users that have been explicitly shared access to a chat.

Callers 4

ChatHistoryFunction · 0.90
ChatNameFunction · 0.90
ChatRowActionsFunction · 0.90
ChatThreadFunction · 0.90

Calls 7

sewFunction · 0.90
withOptionalAuthFunction · 0.90
checkAskEntitlementFunction · 0.90
notFoundFunction · 0.90
isOwnerOfChatFunction · 0.90
isChatSharedWithUserFunction · 0.90
getOrCreateAnonymousIdFunction · 0.90

Tested by

no test coverage detected