MCPcopy Index your code
hub / github.com/sourcebot-dev/sourcebot / createChat

Function createChat

packages/web/src/features/chat/actions.ts:13–63  ·  view source on GitHub ↗
({ source }: { source?: string } = {})

Source from the content-addressed store, hash-verified

11import { checkAskEntitlement, isChatSharedWithUser, isOwnerOfChat } from "./utils.server";
12
13export 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
65export const getChatInfo = async ({ chatId }: { chatId: string }) => sew(() =>
66 withOptionalAuth(async ({ org, user, prisma }) => {

Callers 1

useCreateNewChatThreadFunction · 0.90

Calls 6

sewFunction · 0.90
withOptionalAuthFunction · 0.90
checkAskEntitlementFunction · 0.90
getOrCreateAnonymousIdFunction · 0.90
createAuditFunction · 0.90
captureEventFunction · 0.90

Tested by

no test coverage detected