({ source }: { source?: string } = {})
| 11 | import { checkAskEntitlement, isChatSharedWithUser, isOwnerOfChat } from "./utils.server"; |
| 12 | |
| 13 | export const createChat = async ({ source }: { source?: string } = {}) => sew(() => |
| 14 | withOptionalAuth(async ({ org, user, prisma }) => { |
| 15 | const askError = await checkAskEntitlement(); |
| 16 | if (askError) { |
| 17 | return askError; |
| 18 | } |
| 19 | |
| 20 | const isGuestUser = user === undefined; |
| 21 | |
| 22 | // For anonymous users, get or create an anonymous ID to track ownership |
| 23 | const anonymousCreatorId = isGuestUser ? await getOrCreateAnonymousId() : undefined; |
| 24 | |
| 25 | const chat = await prisma.chat.create({ |
| 26 | data: { |
| 27 | orgId: org.id, |
| 28 | messages: [] as unknown as Prisma.InputJsonValue, |
| 29 | createdById: user?.id, |
| 30 | anonymousCreatorId, |
| 31 | visibility: isGuestUser ? ChatVisibility.PUBLIC : ChatVisibility.PRIVATE, |
| 32 | }, |
| 33 | }); |
| 34 | |
| 35 | // Only create audit log for authenticated users |
| 36 | if (!isGuestUser) { |
| 37 | await createAudit({ |
| 38 | action: "user.created_ask_chat", |
| 39 | actor: { |
| 40 | id: user.id, |
| 41 | type: "user", |
| 42 | }, |
| 43 | target: { |
| 44 | id: org.id.toString(), |
| 45 | type: "org", |
| 46 | }, |
| 47 | orgId: org.id, |
| 48 | metadata: { source }, |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | await captureEvent('ask_thread_created', { |
| 53 | chatId: chat.id, |
| 54 | isAnonymous: isGuestUser, |
| 55 | source, |
| 56 | }); |
| 57 | |
| 58 | return { |
| 59 | id: chat.id, |
| 60 | isAnonymous: isGuestUser, |
| 61 | } |
| 62 | }) |
| 63 | ); |
| 64 | |
| 65 | export const getChatInfo = async ({ chatId }: { chatId: string }) => sew(() => |
| 66 | withOptionalAuth(async ({ org, user, prisma }) => { |
no test coverage detected